00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00032
00033
00034
00035 CImagesLoader::CImagesLoader()
00036 {
00037
00038 ilInit();
00039
00040
00041 ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
00042 ilEnable(IL_ORIGIN_SET);
00043
00044
00045 ilEnable(IL_FILE_OVERWRITE);
00046
00047
00048 ilSetInteger(IL_FORMAT_MODE, IL_BGRA);
00049 ilEnable(IL_FORMAT_SET);
00050 }
00051
00052
00053 CImagesLoader::~CImagesLoader()
00054 {
00055
00056 ilShutDown();
00057 }
00058
00059
00060 CImage* CImagesLoader::LoadFromFile(const String& strFilename)
00061 {
00062
00063 ILuint Texture;
00064 ilGenImages(1, &Texture);
00065 ilBindImage(Texture);
00066
00067
00068 if (!ilLoadImage(const_cast<ILstring>(strFilename.c_str())))
00069 LOADINGFAILED_EXCEPT(strFilename, "DevIL error : call to ilLoadImage failed", "CImagesLoader::LoadFromFile");
00070
00071
00072 TVector2I Size(ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT));
00073
00074
00075 const uchar* pPixels = ilGetData();
00076
00077
00078 CImage* Image = new CImage(Size, PXF_A8R8G8B8, pPixels);
00079
00080
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
00093 CImage Image(pObject->GetSize(), PXF_A8R8G8B8);
00094 Image.CopyImage(*pObject);
00095
00096 Image.Flip();
00097
00098
00099 ILuint Texture;
00100 ilGenImages(1, &Texture);
00101 ilBindImage(Texture);
00102
00103
00104 const TVector2I& Size = Image.GetSize();
00105
00106
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
00110 if (!ilSaveImage(const_cast<ILstring>(strFilename.c_str())))
00111 LOADINGFAILED_EXCEPT(strFilename, "DevIL error : call to ilSaveImage failed", "CImagesLoader::SaveToFile");
00112
00113
00114 ilBindImage(0);
00115 ilDeleteImages(1, &Texture);
00116
00117 ILogger::Log("Save of the image : %s", strFilename.c_str());
00118 }
00119
00120 }