1 ////////////////////////////////////////////////////////////
\r
3 // SFML - Simple and Fast Multimedia Library
\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
25 #ifndef SFML_IMAGE_HPP
\r
26 #define SFML_IMAGE_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
31 #include <SFML/System/Resource.hpp>
\r
32 #include <SFML/Graphics/Color.hpp>
\r
33 #include <SFML/Graphics/Rect.hpp>
\r
42 ////////////////////////////////////////////////////////////
\r
43 /// Image is the low-level class for loading and
\r
44 /// manipulating images
\r
45 ////////////////////////////////////////////////////////////
\r
46 class SFML_API Image : public Resource<Image>
\r
50 ////////////////////////////////////////////////////////////
\r
51 /// Default constructor
\r
53 ////////////////////////////////////////////////////////////
\r
56 ////////////////////////////////////////////////////////////
\r
57 /// Copy constructor
\r
59 /// \param Copy : instance to copy
\r
61 ////////////////////////////////////////////////////////////
\r
62 Image(const Image& Copy);
\r
64 ////////////////////////////////////////////////////////////
\r
65 /// Construct an empty image
\r
67 /// \param Width : Image width
\r
68 /// \param Height : Image height
\r
69 /// \param Col : Image color (black by default)
\r
71 ////////////////////////////////////////////////////////////
\r
72 Image(unsigned int Width, unsigned int Height, const Color& Col = Color(0, 0, 0, 255));
\r
74 ////////////////////////////////////////////////////////////
\r
75 /// Construct the image from pixels in memory
\r
77 /// \param Width : Image width
\r
78 /// \param Height : Image height
\r
79 /// \param Data : Pointer to the pixels in memory (assumed format is RGBA)
\r
81 ////////////////////////////////////////////////////////////
\r
82 Image(unsigned int Width, unsigned int Height, const Uint8* Data);
\r
84 ////////////////////////////////////////////////////////////
\r
87 ////////////////////////////////////////////////////////////
\r
90 ////////////////////////////////////////////////////////////
\r
91 /// Load the image from a file
\r
93 /// \param Filename : Path of the image file to load
\r
95 /// \return True if loading was successful
\r
97 ////////////////////////////////////////////////////////////
\r
98 bool LoadFromFile(const std::string& Filename);
\r
100 ////////////////////////////////////////////////////////////
\r
101 /// Load the image from a file in memory
\r
103 /// \param Data : Pointer to the file data in memory
\r
104 /// \param SizeInBytes : Size of the data to load, in bytes
\r
106 /// \return True if loading was successful
\r
108 ////////////////////////////////////////////////////////////
\r
109 bool LoadFromMemory(const char* Data, std::size_t SizeInBytes);
\r
111 ////////////////////////////////////////////////////////////
\r
112 /// Load the image directly from an array of pixels
\r
114 /// \param Width : Image width
\r
115 /// \param Height : Image height
\r
116 /// \param Data : Pointer to the pixels in memory (assumed format is RGBA)
\r
118 /// \return True if loading was successful
\r
120 ////////////////////////////////////////////////////////////
\r
121 bool LoadFromPixels(unsigned int Width, unsigned int Height, const Uint8* Data);
\r
123 ////////////////////////////////////////////////////////////
\r
124 /// Save the content of the image to a file
\r
126 /// \param Filename : Path of the file to save (overwritten if already exist)
\r
128 /// \return True if saving was successful
\r
130 ////////////////////////////////////////////////////////////
\r
131 bool SaveToFile(const std::string& Filename) const;
\r
133 ////////////////////////////////////////////////////////////
\r
134 /// Create an empty image
\r
136 /// \param Width : Image width
\r
137 /// \param Height : Image height
\r
138 /// \param Col : Image color (black by default)
\r
140 /// \return True if creation was successful
\r
142 ////////////////////////////////////////////////////////////
\r
143 bool Create(unsigned int Width, unsigned int Height, Color Col = Color(0, 0, 0, 255));
\r
145 ////////////////////////////////////////////////////////////
\r
146 /// Create transparency mask from a specified colorkey
\r
148 /// \param ColorKey : Color to become transparent
\r
149 /// \param Alpha : Alpha value to use for transparent pixels (0 by default)
\r
151 ////////////////////////////////////////////////////////////
\r
152 void CreateMaskFromColor(Color ColorKey, Uint8 Alpha = 0);
\r
154 ////////////////////////////////////////////////////////////
\r
155 /// Copy pixels from another image onto this one.
\r
156 /// This function does a slow pixel copy and should only
\r
157 /// be used at initialization time
\r
159 /// \param Source : Source image to copy
\r
160 /// \param DestX : X coordinate of the destination position
\r
161 /// \param DestY : Y coordinate of the destination position
\r
162 /// \param SourceRect : Sub-rectangle of the source image to copy (empty by default - entire image)
\r
163 /// \param ApplyAlpha : Should the copy take in account the source transparency? (false by default)
\r
165 ////////////////////////////////////////////////////////////
\r
166 void Copy(const Image& Source, unsigned int DestX, unsigned int DestY, const IntRect& SourceRect = IntRect(0, 0, 0, 0), bool ApplyAlpha = false);
\r
168 ////////////////////////////////////////////////////////////
\r
169 /// Create the image from the current contents of the
\r
172 /// \param Window : Window to capture
\r
173 /// \param SourceRect : Sub-rectangle of the screen to copy (empty by default - entire image)
\r
175 /// \return True if copy was successful
\r
177 ////////////////////////////////////////////////////////////
\r
178 bool CopyScreen(RenderWindow& Window, const IntRect& SourceRect = IntRect(0, 0, 0, 0));
\r
180 ////////////////////////////////////////////////////////////
\r
181 /// Change the color of a pixel
\r
183 /// \param X : X coordinate of pixel in the image
\r
184 /// \param Y : Y coordinate of pixel in the image
\r
185 /// \param Col : New color for pixel (X, Y)
\r
187 ////////////////////////////////////////////////////////////
\r
188 void SetPixel(unsigned int X, unsigned int Y, const Color& Col);
\r
190 ////////////////////////////////////////////////////////////
\r
191 /// Get a pixel from the image
\r
193 /// \param X : X coordinate of pixel in the image
\r
194 /// \param Y : Y coordinate of pixel in the image
\r
196 /// \return Color of pixel (X, Y)
\r
198 ////////////////////////////////////////////////////////////
\r
199 const Color& GetPixel(unsigned int X, unsigned int Y) const;
\r
201 ////////////////////////////////////////////////////////////
\r
202 /// Get a read-only pointer to the array of pixels (RGBA 8 bits integers components)
\r
203 /// Array size is GetWidth() x GetHeight() x 4
\r
204 /// This pointer becomes invalid if you reload or resize the image
\r
206 /// \return Const pointer to the array of pixels
\r
208 ////////////////////////////////////////////////////////////
\r
209 const Uint8* GetPixelsPtr() const;
\r
211 ////////////////////////////////////////////////////////////
\r
212 /// Bind the image for rendering
\r
214 ////////////////////////////////////////////////////////////
\r
217 ////////////////////////////////////////////////////////////
\r
218 /// Enable or disable image smooth filter.
\r
219 /// This parameter is enabled by default
\r
221 /// \param Smooth : True to enable smoothing filter, false to disable it
\r
223 ////////////////////////////////////////////////////////////
\r
224 void SetSmooth(bool Smooth);
\r
226 ////////////////////////////////////////////////////////////
\r
227 /// Return the width of the image
\r
229 /// \return Width in pixels
\r
231 ////////////////////////////////////////////////////////////
\r
232 unsigned int GetWidth() const;
\r
234 ////////////////////////////////////////////////////////////
\r
235 /// Return the height of the image
\r
237 /// \return Height in pixels
\r
239 ////////////////////////////////////////////////////////////
\r
240 unsigned int GetHeight() const;
\r
242 ////////////////////////////////////////////////////////////
\r
243 /// Tells whether the smooth filtering is enabled or not
\r
245 /// \return True if image smoothing is enabled
\r
247 ////////////////////////////////////////////////////////////
\r
248 bool IsSmooth() const;
\r
250 ////////////////////////////////////////////////////////////
\r
251 /// Convert a subrect expressed in pixels, into float
\r
252 /// texture coordinates
\r
254 /// \param Rect : Sub-rectangle of image to convert
\r
256 /// \return Texture coordinates corresponding to the sub-rectangle
\r
258 ////////////////////////////////////////////////////////////
\r
259 FloatRect GetTexCoords(const IntRect& Rect) const;
\r
261 ////////////////////////////////////////////////////////////
\r
262 /// Get a valid texture size according to hardware support
\r
264 /// \param Size : Size to convert
\r
266 /// \return Valid nearest size (greater than or equal to specified size)
\r
268 ////////////////////////////////////////////////////////////
\r
269 static unsigned int GetValidTextureSize(unsigned int Size);
\r
271 ////////////////////////////////////////////////////////////
\r
272 /// Assignment operator
\r
274 /// \param Other : instance to assign
\r
276 /// \return Reference to the image
\r
278 ////////////////////////////////////////////////////////////
\r
279 Image& operator =(const Image& Other);
\r
283 ////////////////////////////////////////////////////////////
\r
284 /// Create the OpenGL texture
\r
286 /// \return True if texture has been successfully created
\r
288 ////////////////////////////////////////////////////////////
\r
289 bool CreateTexture();
\r
291 ////////////////////////////////////////////////////////////
\r
292 /// Make sure the texture in video memory is updated with the
\r
293 /// array of pixels
\r
294 ////////////////////////////////////////////////////////////
\r
295 void EnsureTextureUpdate() const;
\r
297 ////////////////////////////////////////////////////////////
\r
298 /// Make sure the array of pixels is updated with the
\r
299 /// texture in video memory
\r
300 ////////////////////////////////////////////////////////////
\r
301 void EnsureArrayUpdate() const;
\r
303 ////////////////////////////////////////////////////////////
\r
304 /// Reset the image attributes
\r
306 ////////////////////////////////////////////////////////////
\r
309 ////////////////////////////////////////////////////////////
\r
310 /// Destroy the OpenGL texture
\r
312 ////////////////////////////////////////////////////////////
\r
313 void DestroyTexture();
\r
315 ////////////////////////////////////////////////////////////
\r
317 ////////////////////////////////////////////////////////////
\r
318 unsigned int myWidth; ///< Image width
\r
319 unsigned int myHeight; ///< Image Height
\r
320 unsigned int myTextureWidth; ///< Actual texture width (can be greater than image width because of padding)
\r
321 unsigned int myTextureHeight; ///< Actual texture height (can be greater than image height because of padding)
\r
322 unsigned int myTexture; ///< Internal texture identifier
\r
323 bool myIsSmooth; ///< Status of the smooth filter
\r
324 mutable std::vector<Color> myPixels; ///< Pixels of the image
\r
325 mutable bool myNeedTextureUpdate; ///< Status of synchronization between pixels in central memory and the internal texture un video memory
\r
326 mutable bool myNeedArrayUpdate; ///< Status of synchronization between pixels in central memory and the internal texture un video memory
\r
332 #endif // SFML_IMAGE_HPP
\r