KeosImagesLoader.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 
00022 #include "KeosImagesLoader.h"
00023 #include "KeosImage.h"
00024 #include "../Extlibs/DevIL/il.h"
00025 #include "KeosPixelUtils.h"
00026 #include "KeosLogger.h"
00027 
00028 namespace Keos
00029 {
00030   //=======================================================================
00031   // CImagesLoader implementation
00032   //=======================================================================
00033 
00034   //-----------------------------------------------------------------------
00035   CImagesLoader::CImagesLoader()
00036   {
00037     // DevIL initialization
00038     ilInit();
00039 
00040     // We indicate that the origin of the images is on the upper-left corner
00041     ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
00042     ilEnable(IL_ORIGIN_SET);
00043 
00044     // We authorize the destruction of already existing files, for the saving
00045     ilEnable(IL_FILE_OVERWRITE);
00046 
00047     // We force the load of the images in 32 bits BGRA
00048     ilSetInteger(IL_FORMAT_MODE, IL_BGRA);
00049     ilEnable(IL_FORMAT_SET);
00050   }
00051 
00052   //-----------------------------------------------------------------------
00053   CImagesLoader::~CImagesLoader()
00054   {
00055     // DevIL shutdown
00056     ilShutDown();
00057   }
00058 
00059   //-----------------------------------------------------------------------
00060   CImage* CImagesLoader::LoadFromFile(const String& strFilename)
00061   {
00062     // Generation of a new texture
00063     ILuint Texture;
00064     ilGenImages(1, &Texture);
00065     ilBindImage(Texture);
00066 
00067     // Load of the image
00068     if (!ilLoadImage(const_cast<ILstring>(strFilename.c_str())))
00069       LOADINGFAILED_EXCEPT(strFilename, "DevIL error : call to ilLoadImage failed", "CImagesLoader::LoadFromFile");
00070 
00071     // Image size
00072     TVector2I Size(ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT));
00073 
00074     // Image pixels
00075     const uchar* pPixels = ilGetData();
00076 
00077     // Creation of the image
00078     CImage* Image = new CImage(Size, PXF_A8R8G8B8, pPixels);
00079 
00080     // Delete of the texture
00081     ilBindImage(0);
00082     ilDeleteImages(1, &Texture);
00083 
00084     ILogger::Log("Load of the image : %s", strFilename.c_str());
00085 
00086     return Image;
00087   }
00088 
00089   //-----------------------------------------------------------------------
00090   void CImagesLoader::SaveToFile(const CImage* pObject, const String& strFilename)
00091   {
00092     // We create a copy of the image in a compatible format with DevIL (ARGB 32 bits)
00093     CImage Image(pObject->GetSize(), PXF_A8R8G8B8);
00094     Image.CopyImage(*pObject);
00095 
00096     Image.Flip();
00097 
00098     // Generation of a new texture
00099     ILuint Texture;
00100     ilGenImages(1, &Texture);
00101     ilBindImage(Texture);
00102 
00103     // Image size
00104     const TVector2I& Size = Image.GetSize();
00105 
00106     // Completion of the texture with the converted pixels
00107     if (!ilTexImage(Size.x, Size.y, 1, GetBytesPerPixel(Image.GetFormat()), IL_BGRA, IL_UNSIGNED_BYTE, (void*)Image.GetData()))
00108       LOADINGFAILED_EXCEPT(strFilename, "DevIL error : call to ilTexImage failed", "CImagesLoader::SaveToFile");
00109     // Save of the texture
00110     if (!ilSaveImage(const_cast<ILstring>(strFilename.c_str())))
00111       LOADINGFAILED_EXCEPT(strFilename, "DevIL error : call to ilSaveImage failed", "CImagesLoader::SaveToFile");
00112 
00113     // Delete of the texture
00114     ilBindImage(0);
00115     ilDeleteImages(1, &Texture);
00116 
00117     ILogger::Log("Save of the image : %s", strFilename.c_str());
00118   }
00119 
00120 } // namespace Keos

Generated on Fri Mar 9 14:29:02 2007 for Keos by  doxygen 1.5.1-p1