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