00001
00026 #include <libnebular.hpp>
00027
00028 #include <iostream>
00029 #include <stdexcept>
00030 #include <string>
00031 #include <cstdlib>
00032
00033 #include <boost/shared_array.hpp>
00034
00035 #include <SDL/SDL.h>
00036
00037 #include "sdl-utils.hpp"
00038
00039 using namespace std;
00040
00041 using libnebular::Picture;
00042 using libnebular::String;
00043 using libnebular::PictureProps;
00044
00046 void display(Picture &pic, const string &caption = "Some picture"){
00047 ManagerSdl managerSdl;
00048 managerSdl.setVideoMode(
00049 pic.get<Integer>("bm/x-size"),
00050 pic.get<Integer>("bm/y-size")
00051 );
00052
00053 SDL_WM_SetCaption(
00054 caption.c_str(),
00055 NULL
00056 );
00057
00058 SdlSurfaceHolder picSdlSurface =
00059 pictureToSdlSurface(pic);
00060 SdlSurfaceHolder screen = SdlSurfaceHolder::createWithNullDeleter(
00061 SDL_GetVideoSurface()
00062 );
00063
00064 CHECKNZ_SDL( screen.get() );
00065 CHECKZ_SDL( SDL_BlitSurface(picSdlSurface.get(), NULL, screen.get(), NULL) );
00066 CHECKZ_SDL( SDL_Flip(screen.get()) );
00067
00068
00069 {
00070 SDL_Event event;
00071 bool isExit = false;
00072 while ( !isExit && SDL_WaitEvent(&event) ) {
00073 if(event.type == SDL_QUIT){
00074 clog << "INFO: Exit by SDL_QUIT message" << endl;
00075 isExit = true;
00076 }
00077 }
00078 }
00079 }
00080
00082 void listProperties(Picture &pic){
00083 cout << "\"d\" means default" << endl;
00084 for(
00085 Picture::PropKeyIterator key = pic.propKeyBegin();
00086 key != pic.propKeyEnd();
00087 ++key
00088 ){
00089 cout <<
00090 "(" <<
00091 (pic.isDefault(*key) ? "d" : ".") <<
00092 ") " <<
00093 *key <<
00094 " = \"" << pic.get<String>(*key) << "\""
00095 << endl;
00096 }
00097
00098 }
00099
00101 void warning(const std::string &message){
00102 cerr << "WARNING: " << message << endl;
00103 }
00104
00106 void info(const std::string &message){
00107 cerr << "INFO: " << message << endl;
00108 }
00109
00110 int main(int argc, char **argv){
00111 try{
00112 string fName;
00113 bool haveToDisplay = false;
00114
00115
00116 {
00117 if(argc == 2){
00118 fName = argv[1];
00119 }else if(argc == 3){
00120 if(string(argv[1]) == "-d"){
00121 haveToDisplay = true;
00122 }else{
00123 throw runtime_error("Wrong arguments");
00124 }
00125 fName = argv[2];
00126 }else{
00127 throw runtime_error("Wrong arguments");
00128 }
00129 }
00130
00131 libnebular::LogListener::setWarningCallback(warning);
00132 libnebular::LogListener::setInfoCallback(info);
00133
00134 string pluginDir = "../lib";
00135 libnebular::Core().get().setPluginDir(pluginDir);
00136
00137 Picture pic(fName);
00138 pic.readFileInfo();
00139
00140
00141 boost::shared_array<char> userBmData;
00142 {
00143 pic.calculate<PictureProps::PropCalcBmDataSize>();
00144
00145 userBmData = boost::shared_array<char>(new char[
00146 pic.get<Integer>("calc/bm/data-size")
00147 ]);
00148 pic.set<Blob>("bm/data",
00149 Blob::createAndAttach(
00150 userBmData.get(),
00151 pic.get<Integer>("calc/bm/data-size")
00152 )
00153 );
00154 }
00155 pic.readBm();
00156
00157
00158 {
00159 pic.calculate<PictureProps::PropCalcBmSizeLengthUnits>();
00160 pic.calculate<PictureProps::PropCalcBmPixelMaskMisc>();
00161 if(pic.get<Integer>("bm/bpp") % bitsPerByte == 0){
00162 pic.calculate<PictureProps::PropCalcBmColorsUsed>();
00163 }else{
00164
00165 }
00166 pic.calculate<PictureProps::PropCalcBmSizeLengthUnits>();
00167
00168 listProperties(pic);
00169 }
00170
00171
00172 if(haveToDisplay){
00173 if(pic.get<String>("bm/pixel-format-type") == "rgb"){
00174 Picture picDisp;
00175
00176
00177 picDisp.set("opt/bm/bpp", "16");
00178 picDisp.set("opt/bm/pixel-mask/r", "0x1f");
00179 picDisp.set("opt/bm/pixel-mask/g", "0x7e0");
00180 picDisp.set("opt/bm/pixel-mask/b", "0xf800");
00181 picDisp.set("opt/bm/pixel-mask/a", "0");
00182
00183 picDisp.reproduce(pic);
00184
00185 display(picDisp, string() + "A picture from file: \"" + pic.get<libnebular::String>("file/url") + "\"");
00186 }else if(pic.get<String>("bm/pixel-format-type") == "pal"){
00187 display(*(
00188 libnebular::createSubtreeProxy<Picture>(
00189 pic,
00190 "bm/palette/"
00191 )
00192 ), string() + "Palette of picture from file: \"" + pic.get<libnebular::String>("file/url") + "\"");
00193 }else{
00194 throw runtime_error("Nothing to display");
00195 }
00196 }
00197
00198
00199 {
00200 if(pic.get<Integer>("bm/bpp") % bitsPerByte == 0){
00201 pic.set<String>("opt/file/url", "../share/test/test-out.jpeg");
00202 pic.set<String>("opt/file/mimetype", "image/jpeg");
00203
00204 pic.set<Integer>("opt/file/jpeg-quality", 25);
00205 pic.set<String>("opt/file/url", "../share/test/test-out-jpeg-quality-25.jpeg");
00206
00207 pic.write();
00208 }else{
00209
00210 }
00211 }
00212
00213 return EXIT_SUCCESS;
00214 }catch(libnebular::Error &x){
00215 cerr << "ERROR, from <libnebular>: " << x.what() << endl;
00216 }catch(runtime_error &x){
00217 cerr << "ERROR, runtime: " << x.what() << endl;
00218 }catch(logic_error &x){
00219 cerr << "ERROR, logic: " << x.what() << endl;
00220 }catch(...){
00221 cerr << "ERROR, unknown" << endl;
00222 }
00223 return EXIT_FAILURE;
00224 }