D:/Programmation/Cpp/SFML/src/SFML/Graphics/ImageLoader.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
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     // Initialize DevIL
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     // Check DevIL version
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     // Allow files to be overwritten by save operations if they already exist
00069     ilEnable(IL_FILE_OVERWRITE);
00070 
00071     // Force image origin to upper left corner
00072     ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
00073     ilEnable(IL_ORIGIN_SET);
00074 
00075     // Force load format to 32 bits RGBA
00076     ilSetInteger(IL_FORMAT_MODE, IL_RGBA);
00077     ilEnable(IL_FORMAT_SET);
00078 }
00079 
00080 
00084 sfImageLoader::~sfImageLoader()
00085 {
00086     // Shutdown DevIL library
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     // First generate a new DevIL image
00097     ILuint Image = ilGenImage();
00098     ilBindImage(Image);
00099 
00100     // Load from file
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     // Get image dimensions
00109     Width  = ilGetInteger(IL_IMAGE_WIDTH);
00110     Height = ilGetInteger(IL_IMAGE_HEIGHT);
00111 
00112     // Copy pixels to pixel buffer
00113     const sfUint32* PixelsPtr = reinterpret_cast<const sfUint32*>(ilGetData());
00114     Pixels.assign(PixelsPtr, PixelsPtr + Width * Height);
00115 
00116     // Destroy DevIL image, we don't need it anymore
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     // Flip original image
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     // Generate a new DevIL image
00135     ILuint Image = ilGenImage();
00136     ilBindImage(Image);
00137 
00138     // Set its size and format to match those of the image to save
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     // Save image
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     // Destroy DevIL image, we don't need it anymore
00155     ilDeleteImage(Image);
00156 
00157     return true;
00158 }
00159 
00160 } // namespace sf_private

Generated for SFML by  doxygen 1.5.2