00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024
00026
00028 #include <SFML/Graphics/ImageLoader.hpp>
00029 #include <SFML/Graphics/DevIL/il.h>
00030 #include <iostream>
00031
00032
00033 namespace sf_private
00034 {
00038 sfImageLoader& sfImageLoader::GetInstance()
00039 {
00040 static sfImageLoader Instance;
00041
00042 return Instance;
00043 }
00044
00045
00049 sfImageLoader::sfImageLoader()
00050 {
00051
00052 ilInit();
00053 ILenum ErrorCode = ilGetError();
00054 if (ErrorCode != IL_NO_ERROR)
00055 {
00056 std::cerr << "Failed to initialize DevIL library (error code : " << ErrorCode << ")" << std::endl;
00057 return;
00058 }
00059
00060
00061 ILint ILVersion = ilGetInteger(IL_VERSION_NUM);
00062 if (ILVersion < IL_VERSION)
00063 {
00064 std::cerr << "DevIL library has incorrect version (is " << ILVersion << ", should be " << IL_VERSION << ")" << std::endl;
00065 return;
00066 }
00067
00068
00069 ilEnable(IL_FILE_OVERWRITE);
00070
00071
00072 ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
00073 ilEnable(IL_ORIGIN_SET);
00074
00075
00076 ilSetInteger(IL_FORMAT_MODE, IL_RGBA);
00077 ilEnable(IL_FORMAT_SET);
00078 }
00079
00080
00084 sfImageLoader::~sfImageLoader()
00085 {
00086
00087 ilShutDown();
00088 }
00089
00090
00094 bool sfImageLoader::LoadImageFromFile(const std::string& Filename, std::vector<sfUint32>& Pixels, unsigned int& Width, unsigned int& Height)
00095 {
00096
00097 ILuint Image = ilGenImage();
00098 ilBindImage(Image);
00099
00100
00101 if (ilLoadImage(Filename.c_str()) == false)
00102 {
00103 std::cerr << "Failed to load image from file \"" << Filename << "\"" << std::endl;
00104 ilDeleteImage(Image);
00105 return false;
00106 }
00107
00108
00109 Width = ilGetInteger(IL_IMAGE_WIDTH);
00110 Height = ilGetInteger(IL_IMAGE_HEIGHT);
00111
00112
00113 const sfUint32* PixelsPtr = reinterpret_cast<const sfUint32*>(ilGetData());
00114 Pixels.assign(PixelsPtr, PixelsPtr + Width * Height);
00115
00116
00117 ilDeleteImage(Image);
00118
00119 return true;
00120 }
00121
00122
00126 bool sfImageLoader::SaveImageToFile(const std::string& Filename, const std::vector<sfUint32>& Pixels, unsigned int Width, unsigned int Height)
00127 {
00128
00129 std::vector<sfUint32> TempPixels(Width * Height, 0);
00130 for (unsigned int i = 0; i < Width; ++i)
00131 for (unsigned int j = 0; j < Height; ++j)
00132 TempPixels[i + j * Width] = Pixels[i + (Height - 1 - j) * Width];
00133
00134
00135 ILuint Image = ilGenImage();
00136 ilBindImage(Image);
00137
00138
00139 if (!ilTexImage(Width, Height, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, const_cast<sfUint32*>(&TempPixels[0])))
00140 {
00141 std::cerr << "Failed to save image to file \"" << Filename << "\"" << std::endl;
00142 ilDeleteImage(Image);
00143 return false;
00144 }
00145
00146
00147 if (!ilSaveImage(Filename.c_str()))
00148 {
00149 std::cerr << "Failed to save image to file \"" << Filename << "\"" << std::endl;
00150 ilDeleteImage(Image);
00151 return false;
00152 }
00153
00154
00155 ilDeleteImage(Image);
00156
00157 return true;
00158 }
00159
00160 }