MFXImageHelper.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // missing_desc
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 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) break;
00113     }
00114     if (2 * newHeight - image->getHeight() < image->getHeight() - newHeight) {
00115         newHeight *= 2;
00116     }
00117     FXint newWidth;
00118     for (FXint exp = 30; exp >= 0; exp--) {
00119         newWidth = 2 << exp;
00120         if (image->getWidth() & newWidth) break;
00121     }
00122     if (2 * newWidth - image->getWidth() < image->getWidth() - newWidth) {
00123         newWidth *= 2;
00124     }
00125     if (newHeight == image->getHeight() && newWidth == image->getWidth()) {
00126         return false;
00127     }
00128     image->scale(newWidth, newHeight);
00129     return true;
00130 }
00131 
00132 
00133 // smell: yellow (the save functions may have additional options, not regarded)
00134 // Save file
00135 FXbool
00136 MFXImageHelper::saveImage(const std::string& file,
00137                           int width, int height, FXColor *data) {
00138     FXString ext=FXPath::extension(file.c_str());
00139     checkSupported(ext);
00140     FXFileStream stream;
00141     if (!stream.open(file.c_str(), FXStreamSave)) {
00142         throw InvalidArgument("Could not open file for writing!");
00143     }
00144     if (comparecase(ext,"gif")==0) {
00145         return fxsaveGIF(stream, data, width, height, false /* !!! "fast" */);
00146     } else if (comparecase(ext,"bmp")==0) {
00147         return fxsaveBMP(stream, data, width, height);
00148     } else if (comparecase(ext,"xpm")==0) {
00149         return fxsaveXPM(stream, data, width, height);
00150     } else if (comparecase(ext,"pcx")==0) {
00151         return fxsavePCX(stream, data, width, height);
00152     } else if (comparecase(ext,"ico")==0 || comparecase(ext,"cur")==0) {
00153         return fxsaveICO(stream, data, width, height);
00154     } else if (comparecase(ext,"tga")==0) {
00155         return fxsaveTGA(stream, data, width, height);
00156     } else if (comparecase(ext,"rgb")==0) {
00157         return fxsaveRGB(stream, data, width, height);
00158     } else if (comparecase(ext,"xbm")==0) {
00159         return fxsaveXBM(stream, data, width, height);
00160     } else if (comparecase(ext,"png")==0) {
00161         return fxsavePNG(stream, data, width, height);
00162     } else if (comparecase(ext,"jpg")==0 || comparecase(ext,"jpeg")==0) {
00163         return fxsaveJPG(stream, data, width, height, 75);
00164     } else if (comparecase(ext,"tif")==0 || comparecase(ext,"tiff")==0) {
00165         return fxsaveTIF(stream, data, width, height, 0);
00166     }
00167     throw InvalidArgument("Unknown file extension for image!");
00168 }
00169 
00170 
00171 
00172 /****************************************************************************/
00173 

Generated on Wed May 5 00:06:30 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6