ImageFactory.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <image/ImageFactory.h>
00022 #include <image/ImageBitmap.h>
00023 #include <image/ImageJpg.h>
00024 #include <image/ImagePng.h>
00025 #include <string>
00026 
00027 ImageFactory::ImageFactory()
00028 {
00029 }
00030 
00031 Image *ImageFactory::loadAlphaImage(
00032         const std::string &filename)
00033 {
00034         std::string extension(filename);
00035         _strlwr((char *) extension.c_str());
00036 
00037         if (strstr(extension.c_str(), ".png"))
00038         {
00039                 ImagePng *png = new ImagePng();
00040                 png->loadFromFile(filename.c_str(), true);
00041                 return png;
00042         }
00043         else if (strstr(extension.c_str(), ".jpg"))
00044         {
00045                 ImageJpg *jpg = new ImageJpg();
00046                 jpg->loadFromFile(filename.c_str(), true);
00047                 return jpg;
00048         }
00049 
00050         // Failsafe !!
00051         ImageBitmap *bitmap = new ImageBitmap();
00052         bitmap->loadFromFile(filename.c_str(), true);
00053         return bitmap;
00054 }
00055 Image *ImageFactory::loadImage(
00056         const std::string &filename, 
00057         const std::string &alphafilename, 
00058         bool invert)
00059 {
00060         std::string extension(filename);
00061         _strlwr((char *) extension.c_str());
00062 
00063         if (strstr(extension.c_str(), ".png"))
00064         {
00065                 if (!alphafilename.empty())
00066                 {
00067                         ImagePng *png = new ImagePng();
00068                         png->loadFromFile(filename.c_str(), alphafilename.c_str(), invert);
00069                         return png;
00070                 }
00071                 ImagePng *png = new ImagePng();
00072                 png->loadFromFile(filename.c_str());
00073                 return png;
00074         } else if (strstr(extension.c_str(), ".jpg"))
00075         {
00076                 if (!alphafilename.empty())
00077                 {
00078                         ImageJpg *jpg = new ImageJpg();
00079                         jpg->loadFromFile(filename.c_str(), alphafilename.c_str(), invert);
00080                         return jpg;
00081                 }
00082                 ImageJpg *jpg = new ImageJpg();
00083                 jpg->loadFromFile(filename.c_str());
00084                 return jpg;
00085         } 
00086 
00087         // Failsafe !!
00088         if (!alphafilename.empty())
00089         {
00090                 ImageBitmap *bitmap = new ImageBitmap();
00091                 bitmap->loadFromFile(filename.c_str(), alphafilename.c_str(), invert);
00092                 return bitmap;
00093         }
00094         ImageBitmap *bitmap = new ImageBitmap();
00095         bitmap->loadFromFile(filename.c_str());
00096         return bitmap;
00097 }
00098 
00099 ImageHandle ImageFactory::loadImageHandle(
00100         const std::string &filename, 
00101         const std::string &alphafilename, 
00102         bool invert)
00103 {
00104         Image *image = loadImage(filename, alphafilename, invert);
00105 
00106         ImageHandle handle(*image);
00107         delete image;
00108         return handle;
00109 }
00110 
00111 ImageHandle ImageFactory::loadAlphaImageHandle(
00112         const std::string &filename)
00113 {
00114         Image *image = loadAlphaImage(filename);
00115 
00116         ImageHandle handle(*image);
00117         delete image;
00118         return handle;
00119 }
00120 
00121 ImageHandle ImageFactory::createBlank(int width, int height, bool alpha, unsigned char fill)
00122 {
00123         ImageBitmap result(width, height, alpha, fill);
00124         return ImageHandle(result);
00125 }
00126 
00127 #ifndef S3D_SERVER
00128 
00129 #include <GLEXT/GLState.h>
00130 #include <common/Defines.h>
00131 
00132 ImageHandle ImageFactory::grabScreen()
00133 {
00134         GLint           viewport[4];            /* Current viewport */
00135         glGetIntegerv(GL_VIEWPORT, viewport);
00136 
00137         ImageHandle map = ImageFactory::createBlank(viewport[2], viewport[3], false);
00138 
00139         glFinish();                             /* Finish all OpenGL commands */
00140         glPixelStorei(GL_PACK_ALIGNMENT, 4);    /* Force 4-byte alignment */
00141         glPixelStorei(GL_PACK_ROW_LENGTH, 0);
00142         glPixelStorei(GL_PACK_SKIP_ROWS, 0);
00143         glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
00144 
00145         glReadPixels(0, 0, map.getWidth(), map.getHeight(), 
00146                 GL_RGB, GL_UNSIGNED_BYTE, map.getBits());
00147 
00148         return ImageHandle(map);
00149 }
00150 #endif

Generated on Mon Feb 16 15:14:50 2009 for Scorched3D by  doxygen 1.5.3