|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // missing_desc 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors 00011 /****************************************************************************/ 00012 // 00013 // This program is free software; you can redistribute it and/or modify 00014 // it under the terms of the GNU General Public License as published by 00015 // the Free Software Foundation; either version 2 of the License, or 00016 // (at your option) any later version. 00017 // 00018 /****************************************************************************/ 00019 00020 00021 // =========================================================================== 00022 // included modules 00023 // =========================================================================== 00024 #ifdef _MSC_VER 00025 #include <windows_config.h> 00026 #else 00027 #include <config.h> 00028 #endif 00029 00030 #include <string> 00031 #include <fx.h> 00032 #include <FXPNGImage.h> 00033 #include <FXJPGImage.h> 00034 #include <FXTIFImage.h> 00035 #include "MFXImageHelper.h" 00036 00037 #include <cassert> 00038 00039 #ifdef CHECK_MEMORY_LEAKS 00040 #include <foreign/nvwa/debug_new.h> 00041 #endif // CHECK_MEMORY_LEAKS 00042 00043 void 00044 MFXImageHelper::checkSupported(FXString ext) throw(InvalidArgument) { 00045 if (comparecase(ext,"png")==0) { 00046 if (!FXPNGImage::supported) { 00047 throw InvalidArgument("Fox was compiled without png support!"); 00048 } 00049 } else if (comparecase(ext,"jpg")==0 || comparecase(ext,"jpeg")==0) { 00050 if (!FXJPGImage::supported) { 00051 throw InvalidArgument("Fox was compiled without jpg support!"); 00052 } 00053 } else if (comparecase(ext,"tif")==0 || comparecase(ext,"tiff")==0) { 00054 if (!FXTIFImage::supported) { 00055 throw InvalidArgument("Fox was compiled without tif support!"); 00056 } 00057 } 00058 } 00059 00060 00061 FXImage* 00062 MFXImageHelper::loadImage(FXApp* a, const std::string& file) { 00063 FXString ext=FXPath::extension(file.c_str()); 00064 checkSupported(ext); 00065 FXImage* img=NULL; 00066 if (comparecase(ext,"gif")==0) { 00067 img=new FXGIFImage(a,NULL,IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP); 00068 } else if (comparecase(ext,"bmp")==0) { 00069 img=new FXBMPImage(a,NULL,IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP); 00070 } else if (comparecase(ext,"xpm")==0) { 00071 img=new FXXPMImage(a,NULL,IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP); 00072 } else if (comparecase(ext,"pcx")==0) { 00073 img=new FXPCXImage(a,NULL,IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP); 00074 } else if (comparecase(ext,"ico")==0 || comparecase(ext,"cur")==0) { 00075 img=new FXICOImage(a,NULL,IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP); 00076 } else if (comparecase(ext,"tga")==0) { 00077 img=new FXTGAImage(a,NULL,IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP); 00078 } else if (comparecase(ext,"rgb")==0) { 00079 img=new FXRGBImage(a,NULL,IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP); 00080 } else if (comparecase(ext,"xbm")==0) { 00081 img=new FXXBMImage(a,NULL,NULL,IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP); 00082 } else if (comparecase(ext,"png")==0) { 00083 img=new FXPNGImage(a,NULL,IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP); 00084 } else if (comparecase(ext,"jpg")==0 || comparecase(ext,"jpeg")==0) { 00085 img=new FXJPGImage(a,NULL,IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP); 00086 } else if (comparecase(ext,"tif")==0 || comparecase(ext,"tiff")==0) { 00087 img=new FXTIFImage(a,NULL,IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP); 00088 } else { 00089 throw InvalidArgument("Unknown file extension for image '" + file + "'!"); 00090 } 00091 00092 FXFileStream stream; 00093 if (img != NULL && stream.open(file.c_str(), FXStreamLoad)) { 00094 a->beginWaitCursor(); 00095 img->loadPixels(stream); 00096 stream.close(); 00097 00098 img->create(); 00099 a->endWaitCursor(); 00100 } else { 00101 throw InvalidArgument("Loading failed!"); 00102 } 00103 return img; 00104 } 00105 00106 00107 FXbool 00108 MFXImageHelper::scalePower2(FXImage* image) { 00109 FXint newHeight; 00110 for (FXint exp = 30; exp >= 0; exp--) { 00111 newHeight = 2 << exp; 00112 if (image->getHeight() & newHeight) { 00113 break; 00114 } 00115 } 00116 if (2 * newHeight - image->getHeight() < image->getHeight() - newHeight) { 00117 newHeight *= 2; 00118 } 00119 FXint newWidth; 00120 for (FXint exp = 30; exp >= 0; exp--) { 00121 newWidth = 2 << exp; 00122 if (image->getWidth() & newWidth) { 00123 break; 00124 } 00125 } 00126 if (2 * newWidth - image->getWidth() < image->getWidth() - newWidth) { 00127 newWidth *= 2; 00128 } 00129 if (newHeight == image->getHeight() && newWidth == image->getWidth()) { 00130 return false; 00131 } 00132 image->scale(newWidth, newHeight); 00133 return true; 00134 } 00135 00136 00137 // smell: yellow (the save functions may have additional options, not regarded) 00138 // Save file 00139 FXbool 00140 MFXImageHelper::saveImage(const std::string& file, 00141 int width, int height, FXColor* data) { 00142 FXString ext=FXPath::extension(file.c_str()); 00143 checkSupported(ext); 00144 FXFileStream stream; 00145 if (!stream.open(file.c_str(), FXStreamSave)) { 00146 throw InvalidArgument("Could not open file for writing!"); 00147 } 00148 if (comparecase(ext,"gif")==0) { 00149 return fxsaveGIF(stream, data, width, height, false /* !!! "fast" */); 00150 } else if (comparecase(ext,"bmp")==0) { 00151 return fxsaveBMP(stream, data, width, height); 00152 } else if (comparecase(ext,"xpm")==0) { 00153 return fxsaveXPM(stream, data, width, height); 00154 } else if (comparecase(ext,"pcx")==0) { 00155 return fxsavePCX(stream, data, width, height); 00156 } else if (comparecase(ext,"ico")==0 || comparecase(ext,"cur")==0) { 00157 return fxsaveICO(stream, data, width, height); 00158 } else if (comparecase(ext,"tga")==0) { 00159 return fxsaveTGA(stream, data, width, height); 00160 } else if (comparecase(ext,"rgb")==0) { 00161 return fxsaveRGB(stream, data, width, height); 00162 } else if (comparecase(ext,"xbm")==0) { 00163 return fxsaveXBM(stream, data, width, height); 00164 } else if (comparecase(ext,"png")==0) { 00165 return fxsavePNG(stream, data, width, height); 00166 } else if (comparecase(ext,"jpg")==0 || comparecase(ext,"jpeg")==0) { 00167 return fxsaveJPG(stream, data, width, height, 75); 00168 } else if (comparecase(ext,"tif")==0 || comparecase(ext,"tiff")==0) { 00169 return fxsaveTIF(stream, data, width, height, 0); 00170 } 00171 throw InvalidArgument("Unknown file extension for image!"); 00172 } 00173 00174 00175 00176 /****************************************************************************/ 00177