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 <iostream>
00030 #undef _UNICODE
00031 #include <IL/il.h>
00032 
00033 
00034 namespace sf
00035 {
00036 namespace priv
00037 {
00041 ImageLoader& ImageLoader::GetInstance()
00042 {
00043     static ImageLoader Instance;
00044 
00045     return Instance;
00046 }
00047 
00048 
00052 ImageLoader::ImageLoader()
00053 {
00054     // Initialize DevIL
00055     ilInit();
00056     ILenum ErrorCode = ilGetError();
00057     if (ErrorCode != IL_NO_ERROR)
00058     {
00059         std::cerr << "Failed to initialize DevIL library (error code : " << ErrorCode << ")" << std::endl;
00060         return;
00061     }
00062 
00063     // Check DevIL version
00064     ILint ILVersion = ilGetInteger(IL_VERSION_NUM);
00065     if (ILVersion < IL_VERSION)
00066     {
00067         std::cerr << "DevIL library has incorrect version (is " << ILVersion << ", should be " << IL_VERSION << ")" << std::endl;
00068         return;
00069     }
00070 
00071     // Allow files to be overwritten by save operations if they already exist
00072     ilEnable(IL_FILE_OVERWRITE);
00073 
00074     // Force image origin to upper left corner
00075     ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
00076     ilEnable(IL_ORIGIN_SET);
00077 
00078     // Force load format to 32 bits RGBA
00079     ilSetInteger(IL_FORMAT_MODE, IL_RGBA);
00080     ilEnable(IL_FORMAT_SET);
00081 }
00082 
00083 
00087 ImageLoader::~ImageLoader()
00088 {
00089     // Shutdown DevIL library
00090     ilShutDown();
00091 }
00092 
00093 
00097 bool ImageLoader::LoadImageFromFile(const std::string& Filename, std::vector<Uint32>& Pixels, unsigned int& Width, unsigned int& Height)
00098 {
00099     // First generate a new DevIL image
00100     ILuint Image;
00101     ilGenImages(1, &Image);
00102     ilBindImage(Image);
00103 
00104     // Load from file
00105     if (ilLoadImage(const_cast<char*>(Filename.c_str())) == false)
00106     {
00107         std::cerr << "Failed to load image from file \"" << Filename << "\"" << std::endl;
00108         ilDeleteImages(1, &Image);
00109         return false;
00110     }
00111 
00112     // Get image dimensions
00113     Width  = ilGetInteger(IL_IMAGE_WIDTH);
00114     Height = ilGetInteger(IL_IMAGE_HEIGHT);
00115 
00116     // Copy pixels to pixel buffer
00117     const Uint32* PixelsPtr = reinterpret_cast<const Uint32*>(ilGetData());
00118     Pixels.assign(PixelsPtr, PixelsPtr + Width * Height);
00119 
00120     // Destroy DevIL image, we don't need it anymore
00121     ilDeleteImages(1, &Image);
00122 
00123     return true;
00124 }
00125 
00126 
00130 bool ImageLoader::SaveImageToFile(const std::string& Filename, const std::vector<Uint32>& Pixels, unsigned int Width, unsigned int Height)
00131 {
00132     // Generate a new DevIL image
00133     ILuint Image;
00134     ilGenImages(1, &Image);
00135     ilBindImage(Image);
00136 
00137     // Set its size and format to match those of the image to save
00138     if (!ilTexImage(Width, Height, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, const_cast<Uint32*>(&Pixels[0])))
00139     {
00140         std::cerr << "Failed to save image to file \"" << Filename << "\"" << std::endl;
00141         ilDeleteImages(1, &Image);
00142         return false;
00143     }
00144 
00145     // Save image
00146     if (!ilSaveImage(const_cast<char*>(Filename.c_str())))
00147     {
00148         std::cerr << "Failed to save image to file \"" << Filename << "\"" << std::endl;
00149         ilDeleteImages(1, &Image);
00150         return false;
00151     }
00152 
00153     // Destroy DevIL image, we don't need it anymore
00154     ilDeleteImages(1, &Image);
00155 
00156     return true;
00157 }
00158 
00159 } // namespace priv
00160 
00161 } // namespace sf