]> git.sesse.net Git - casparcg/blob - SFML-1.6/include/SFML/Graphics/Image.hpp
(no commit message)
[casparcg] / SFML-1.6 / include / SFML / Graphics / Image.hpp
1 ////////////////////////////////////////////////////////////\r
2 //\r
3 // SFML - Simple and Fast Multimedia Library\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)\r
5 //\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
8 //\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
12 //\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
17 //\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
20 //\r
21 // 3. This notice may not be removed or altered from any source distribution.\r
22 //\r
23 ////////////////////////////////////////////////////////////\r
24 \r
25 #ifndef SFML_IMAGE_HPP\r
26 #define SFML_IMAGE_HPP\r
27 \r
28 ////////////////////////////////////////////////////////////\r
29 // Headers\r
30 ////////////////////////////////////////////////////////////\r
31 #include <SFML/System/Resource.hpp>\r
32 #include <SFML/Graphics/Color.hpp>\r
33 #include <SFML/Graphics/Rect.hpp>\r
34 #include <string>\r
35 #include <vector>\r
36 \r
37 \r
38 namespace sf\r
39 {\r
40 class RenderWindow;\r
41 \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
47 {\r
48 public :\r
49 \r
50     ////////////////////////////////////////////////////////////\r
51     /// Default constructor\r
52     ///\r
53     ////////////////////////////////////////////////////////////\r
54     Image();\r
55 \r
56     ////////////////////////////////////////////////////////////\r
57     /// Copy constructor\r
58     ///\r
59     /// \param Copy : instance to copy\r
60     ///\r
61     ////////////////////////////////////////////////////////////\r
62     Image(const Image& Copy);\r
63 \r
64     ////////////////////////////////////////////////////////////\r
65     /// Construct an empty image\r
66     ///\r
67     /// \param Width :  Image width\r
68     /// \param Height : Image height\r
69     /// \param Col :    Image color (black by default)\r
70     ///\r
71     ////////////////////////////////////////////////////////////\r
72     Image(unsigned int Width, unsigned int Height, const Color& Col = Color(0, 0, 0, 255));\r
73 \r
74     ////////////////////////////////////////////////////////////\r
75     /// Construct the image from pixels in memory\r
76     ///\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
80     ///\r
81     ////////////////////////////////////////////////////////////\r
82     Image(unsigned int Width, unsigned int Height, const Uint8* Data);\r
83 \r
84     ////////////////////////////////////////////////////////////\r
85     /// Destructor\r
86     ///\r
87     ////////////////////////////////////////////////////////////\r
88     ~Image();\r
89 \r
90     ////////////////////////////////////////////////////////////\r
91     /// Load the image from a file\r
92     ///\r
93     /// \param Filename : Path of the image file to load\r
94     ///\r
95     /// \return True if loading was successful\r
96     ///\r
97     ////////////////////////////////////////////////////////////\r
98     bool LoadFromFile(const std::string& Filename);\r
99 \r
100     ////////////////////////////////////////////////////////////\r
101     /// Load the image from a file in memory\r
102     ///\r
103     /// \param Data :        Pointer to the file data in memory\r
104     /// \param SizeInBytes : Size of the data to load, in bytes\r
105     ///\r
106     /// \return True if loading was successful\r
107     ///\r
108     ////////////////////////////////////////////////////////////\r
109     bool LoadFromMemory(const char* Data, std::size_t SizeInBytes);\r
110 \r
111     ////////////////////////////////////////////////////////////\r
112     /// Load the image directly from an array of pixels\r
113     ///\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
117     ///\r
118     /// \return True if loading was successful\r
119     ///\r
120     ////////////////////////////////////////////////////////////\r
121     bool LoadFromPixels(unsigned int Width, unsigned int Height, const Uint8* Data);\r
122 \r
123     ////////////////////////////////////////////////////////////\r
124     /// Save the content of the image to a file\r
125     ///\r
126     /// \param Filename : Path of the file to save (overwritten if already exist)\r
127     ///\r
128     /// \return True if saving was successful\r
129     ///\r
130     ////////////////////////////////////////////////////////////\r
131     bool SaveToFile(const std::string& Filename) const;\r
132 \r
133     ////////////////////////////////////////////////////////////\r
134     /// Create an empty image\r
135     ///\r
136     /// \param Width :  Image width\r
137     /// \param Height : Image height\r
138     /// \param Col :    Image color (black by default)\r
139     ///\r
140     /// \return True if creation was successful\r
141     ///\r
142     ////////////////////////////////////////////////////////////\r
143     bool Create(unsigned int Width, unsigned int Height, Color Col = Color(0, 0, 0, 255));\r
144 \r
145     ////////////////////////////////////////////////////////////\r
146     /// Create transparency mask from a specified colorkey\r
147     ///\r
148     /// \param ColorKey : Color to become transparent\r
149     /// \param Alpha :    Alpha value to use for transparent pixels (0 by default)\r
150     ///\r
151     ////////////////////////////////////////////////////////////\r
152     void CreateMaskFromColor(Color ColorKey, Uint8 Alpha = 0);\r
153 \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
158     ///\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
164     ///\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
167 \r
168     ////////////////////////////////////////////////////////////\r
169     /// Create the image from the current contents of the\r
170     /// given window\r
171     ///\r
172     /// \param Window :     Window to capture\r
173     /// \param SourceRect : Sub-rectangle of the screen to copy (empty by default - entire image)\r
174     ///\r
175     /// \return True if copy was successful\r
176     ///\r
177     ////////////////////////////////////////////////////////////\r
178     bool CopyScreen(RenderWindow& Window, const IntRect& SourceRect = IntRect(0, 0, 0, 0));\r
179 \r
180     ////////////////////////////////////////////////////////////\r
181     /// Change the color of a pixel\r
182     ///\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
186     ///\r
187     ////////////////////////////////////////////////////////////\r
188     void SetPixel(unsigned int X, unsigned int Y, const Color& Col);\r
189 \r
190     ////////////////////////////////////////////////////////////\r
191     /// Get a pixel from the image\r
192     ///\r
193     /// \param X : X coordinate of pixel in the image\r
194     /// \param Y : Y coordinate of pixel in the image\r
195     ///\r
196     /// \return Color of pixel (X, Y)\r
197     ///\r
198     ////////////////////////////////////////////////////////////\r
199     const Color& GetPixel(unsigned int X, unsigned int Y) const;\r
200 \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
205     ///\r
206     /// \return Const pointer to the array of pixels\r
207     ///\r
208     ////////////////////////////////////////////////////////////\r
209     const Uint8* GetPixelsPtr() const;\r
210 \r
211     ////////////////////////////////////////////////////////////\r
212     /// Bind the image for rendering\r
213     ///\r
214     ////////////////////////////////////////////////////////////\r
215     void Bind() const;\r
216 \r
217     ////////////////////////////////////////////////////////////\r
218     /// Enable or disable image smooth filter.\r
219     /// This parameter is enabled by default\r
220     ///\r
221     /// \param Smooth : True to enable smoothing filter, false to disable it\r
222     ///\r
223     ////////////////////////////////////////////////////////////\r
224     void SetSmooth(bool Smooth);\r
225 \r
226     ////////////////////////////////////////////////////////////\r
227     /// Return the width of the image\r
228     ///\r
229     /// \return Width in pixels\r
230     ///\r
231     ////////////////////////////////////////////////////////////\r
232     unsigned int GetWidth() const;\r
233 \r
234     ////////////////////////////////////////////////////////////\r
235     /// Return the height of the image\r
236     ///\r
237     /// \return Height in pixels\r
238     ///\r
239     ////////////////////////////////////////////////////////////\r
240     unsigned int GetHeight() const;\r
241 \r
242     ////////////////////////////////////////////////////////////\r
243     /// Tells whether the smooth filtering is enabled or not\r
244     ///\r
245     /// \return True if image smoothing is enabled\r
246     ///\r
247     ////////////////////////////////////////////////////////////\r
248     bool IsSmooth() const;\r
249 \r
250     ////////////////////////////////////////////////////////////\r
251     /// Convert a subrect expressed in pixels, into float\r
252     /// texture coordinates\r
253     ///\r
254     /// \param Rect : Sub-rectangle of image to convert\r
255     ///\r
256     /// \return Texture coordinates corresponding to the sub-rectangle\r
257     ///\r
258     ////////////////////////////////////////////////////////////\r
259     FloatRect GetTexCoords(const IntRect& Rect) const;\r
260 \r
261     ////////////////////////////////////////////////////////////\r
262     /// Get a valid texture size according to hardware support\r
263     ///\r
264     /// \param Size : Size to convert\r
265     ///\r
266     /// \return Valid nearest size (greater than or equal to specified size)\r
267     ///\r
268     ////////////////////////////////////////////////////////////\r
269     static unsigned int GetValidTextureSize(unsigned int Size);\r
270 \r
271     ////////////////////////////////////////////////////////////\r
272     /// Assignment operator\r
273     ///\r
274     /// \param Other : instance to assign\r
275     ///\r
276     /// \return Reference to the image\r
277     ///\r
278     ////////////////////////////////////////////////////////////\r
279     Image& operator =(const Image& Other);\r
280 \r
281 private :\r
282 \r
283     ////////////////////////////////////////////////////////////\r
284     /// Create the OpenGL texture\r
285     ///\r
286     /// \return True if texture has been successfully created\r
287     ///\r
288     ////////////////////////////////////////////////////////////\r
289     bool CreateTexture();\r
290 \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
296 \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
302 \r
303     ////////////////////////////////////////////////////////////\r
304     /// Reset the image attributes\r
305     ///\r
306     ////////////////////////////////////////////////////////////\r
307     void Reset();\r
308 \r
309     ////////////////////////////////////////////////////////////\r
310     /// Destroy the OpenGL texture\r
311     ///\r
312     ////////////////////////////////////////////////////////////\r
313     void DestroyTexture();\r
314 \r
315     ////////////////////////////////////////////////////////////\r
316     // Member data\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
327 };\r
328 \r
329 } // namespace sf\r
330 \r
331 \r
332 #endif // SFML_IMAGE_HPP\r