D:/Programmation/Cpp/SFML/src/SFML/Graphics/Image.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/Image.hpp>
00029 #include <SFML/Graphics/ImageLoader.hpp>
00030 #include <iostream>
00031 #include <vector>
00032 
00033 
00037 sfImage::sfImage()
00038 {
00039 
00040 }
00041 
00042 
00046 sfImage::sfImage(unsigned int Width, unsigned int Height, const sfColor& Color) :
00047 myImpl(NULL)
00048 {
00049     Create(Width, Height, Color);
00050 }
00051 
00052 
00056 sfImage::sfImage(unsigned int Width, unsigned int Height, const void* Data) :
00057 myImpl(NULL)
00058 {
00059     LoadFromMemory(Width, Height, Data);
00060 }
00061 
00062 
00066 bool sfImage::LoadFromFile(const std::string& Filename)
00067 {
00068     std::vector<sfUint32> Pixels;
00069     unsigned int Width, Height;
00070 
00071     if (sf_private::sfImageLoader::GetInstance().LoadImageFromFile(Filename, Pixels, Width, Height))
00072     {
00073         // Loading succeeded : create a new impl with loaded data
00074         myImpl = new sf_private::sfImageImpl(Width, Height, Pixels);
00075         return true;
00076     }
00077     else
00078     {
00079         // Loading failed : reset impl
00080         myImpl = NULL;
00081         return false;
00082     }
00083 }
00084 
00085 
00089 bool sfImage::SaveToFile(const std::string& Filename) const
00090 {
00091     if (myImpl)
00092         return sf_private::sfImageLoader::GetInstance().SaveImageToFile(Filename, myImpl->GetPixels(), myImpl->GetWidth(), myImpl->GetHeight());
00093 
00094     return false;
00095 }
00096 
00097 
00101 void sfImage::Create(unsigned int Width, unsigned int Height, const sfColor& Color)
00102 {
00103     // Create a pixel buffer filled with specified color
00104     std::vector<sfUint32> Pixels(Width * Height, Color.ToRGBA());
00105 
00106     // Create a new implementation
00107     myImpl = new sf_private::sfImageImpl(Width, Height, Pixels);
00108 }
00109 
00110 
00114 void sfImage::LoadFromMemory(unsigned int Width, unsigned int Height, const void* Data)
00115 {
00116     if (Data)
00117     {
00118         // Create a pixel buffer filled with specified raw data
00119         const sfUint32* Ptr = reinterpret_cast<const sfUint32*>(Data);
00120         std::vector<sfUint32> Pixels(Ptr, Ptr + Width * Height);
00121 
00122         // Create a new implementation
00123         myImpl = new sf_private::sfImageImpl(Width, Height, Pixels);
00124     }
00125     else
00126     {
00127         // No data provided : create a white image
00128         Create(Width, Height, sfColor::White);
00129     }
00130 }
00131 
00132 
00136 void sfImage::CreateMaskFromColor(const sfColor& ColorKey)
00137 {
00138     if (myImpl)
00139     {
00140         // Ensure impl is not shared (do copy-on-write)
00141         if (myImpl.GetRefCount() > 1)
00142             myImpl = new sf_private::sfImageImpl(myImpl->GetWidth(), myImpl->GetHeight(), myImpl->GetPixels());
00143 
00144         // Forward call to impl
00145         myImpl->CreateMaskFromColor(ColorKey.ToRGBA());
00146     }
00147 }
00148 
00149 
00154 void sfImage::Resize(unsigned int Width, unsigned int Height, const sfColor& Color)
00155 {
00156     // Check size
00157     if ((Width == 0) || (Height == 0))
00158     {
00159         std::cerr << "Invalid new size for image (width = " << Width << ", height = " << Height << ")" << std::endl;
00160         return;
00161     }
00162 
00163     // Create a new pixel array with desired size
00164     std::vector<sfUint32> Pixels(Width * Height, Color.ToRGBA());
00165 
00166     // If implementation already exist, we must copy its pixel buffer into the new one
00167     if (myImpl)
00168     {
00169         for (unsigned int i = 0; i < std::min(Width, GetWidth()); ++i)
00170             for (unsigned int j = 0; j < std::min(Height, GetHeight()); ++j)
00171                 Pixels[i + j * Width] = myImpl->GetPixels()[i + j * GetWidth()];
00172     }
00173 
00174     // Create a new impl with the new pixel buffer
00175     myImpl = new sf_private::sfImageImpl(Width, Height, Pixels);
00176 }
00177 
00178 
00183 void sfImage::SetPixel(unsigned int X, unsigned int Y, const sfColor& Color)
00184 {
00185     if (myImpl)
00186     {
00187         // Check pixel is whithin the image bounds
00188         if ((X >= GetWidth()) || (Y >= GetHeight()))
00189         {
00190             std::cerr << "Cannot set pixel (" << X << "," << Y << ") for image "
00191                       << "(width = " << GetWidth() << ", height = " << GetHeight() << ")" << std::endl;
00192             return;
00193         }
00194 
00195         // Ensure impl is not shared (do copy-on-write)
00196         if (myImpl.GetRefCount() > 1)
00197             myImpl = new sf_private::sfImageImpl(myImpl->GetWidth(), myImpl->GetHeight(), myImpl->GetPixels());
00198 
00199         // Forward call to impl
00200         myImpl->SetPixel(X, Y, Color.ToRGBA());
00201     }
00202 }
00203 
00204 
00208 sfColor sfImage::GetPixel(unsigned int X, unsigned int Y) const
00209 {
00210     if (myImpl)
00211     {
00212         // Check pixel is whithin the image bounds
00213         if ((X >= GetWidth()) || (Y >= GetHeight()))
00214         {
00215             std::cerr << "Cannot get pixel (" << X << "," << Y << ") for image "
00216                       << "(width = " << GetWidth() << ", height = " << GetHeight() << ")" << std::endl;
00217             return sfColor::Black;
00218         }
00219 
00220         return sfColor(myImpl->GetPixels()[X + Y * GetWidth()]);
00221     }
00222     else
00223     {
00224         return sfColor::Black;
00225     }
00226 }
00227 
00228 
00234 const sfUint32* sfImage::GetPixelsPtr() const
00235 {
00236     if (myImpl && !myImpl->GetPixels().empty())
00237     {
00238         return &myImpl->GetPixels()[0];
00239     }
00240     else
00241     {
00242         std::cerr << "Trying to access pixels of an empty image" << std::endl;
00243         return NULL;
00244     }
00245 }
00246 
00247 
00251 void sfImage::Update()
00252 {
00253     if (myImpl)
00254         myImpl->Update();
00255 }
00256 
00257 
00261 void sfImage::Bind() const
00262 {
00263     if (myImpl)
00264         myImpl->Bind();
00265 }
00266 
00267 
00271 void sfImage::SetSmooth(bool Smooth) const
00272 {
00273     if (myImpl)
00274         myImpl->SetSmooth(Smooth);
00275 }
00276 
00277 
00282 void sfImage::SetRepeat(bool Repeat) const
00283 {
00284     if (myImpl)
00285         myImpl->SetRepeat(Repeat);
00286 }
00287 
00288 
00292 unsigned int sfImage::GetWidth() const
00293 {
00294     return myImpl ? myImpl->GetWidth() : 0;
00295 }
00296 
00297 
00301 unsigned int sfImage::GetHeight() const
00302 {
00303     return myImpl ? myImpl->GetHeight() : 0;
00304 }
00305 
00306 
00311 sfFloatRect sfImage::GetTexCoords(const sfIntRect& Rect) const
00312 {
00313     return myImpl ? myImpl->GetTexCoords(Rect) : sfFloatRect();
00314 }
00315 
00316 
00320 unsigned int sfImage::GetValidTextureSize(unsigned int Size)
00321 {
00322     return sf_private::sfImageImpl::GetValidTextureSize(Size);
00323 }

Generated for SFML by  doxygen 1.5.2