Image.hpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef SFML_IMAGE_HPP
26#define SFML_IMAGE_HPP
27
29// Headers
31#include <SFML/System/Resource.hpp>
32#include <SFML/Graphics/Color.hpp>
33#include <SFML/Graphics/Rect.hpp>
34#include <string>
35#include <vector>
36
37
38namespace sf
39{
40class RenderWindow;
41
46class SFML_API Image : public Resource<Image>
47{
48public :
49
54 Image();
55
62 Image(const Image& Copy);
63
72 Image(unsigned int Width, unsigned int Height, const Color& Col = Color(0, 0, 0, 255));
73
82 Image(unsigned int Width, unsigned int Height, const Uint8* Data);
83
88 ~Image();
89
98 bool LoadFromFile(const std::string& Filename);
99
109 bool LoadFromMemory(const char* Data, std::size_t SizeInBytes);
110
121 bool LoadFromPixels(unsigned int Width, unsigned int Height, const Uint8* Data);
122
131 bool SaveToFile(const std::string& Filename) const;
132
143 bool Create(unsigned int Width, unsigned int Height, Color Col = Color(0, 0, 0, 255));
144
152 void CreateMaskFromColor(Color ColorKey, Uint8 Alpha = 0);
153
166 void Copy(const Image& Source, unsigned int DestX, unsigned int DestY, const IntRect& SourceRect = IntRect(0, 0, 0, 0), bool ApplyAlpha = false);
167
178 bool CopyScreen(RenderWindow& Window, const IntRect& SourceRect = IntRect(0, 0, 0, 0));
179
188 void SetPixel(unsigned int X, unsigned int Y, const Color& Col);
189
199 const Color& GetPixel(unsigned int X, unsigned int Y) const;
200
209 const Uint8* GetPixelsPtr() const;
210
215 void Bind() const;
216
224 void SetSmooth(bool Smooth);
225
232 unsigned int GetWidth() const;
233
240 unsigned int GetHeight() const;
241
248 bool IsSmooth() const;
249
259 FloatRect GetTexCoords(const IntRect& Rect) const;
260
269 static unsigned int GetValidTextureSize(unsigned int Size);
270
279 Image& operator =(const Image& Other);
280
281private :
282
289 bool CreateTexture();
290
295 void EnsureTextureUpdate() const;
296
301 void EnsureArrayUpdate() const;
302
307 void Reset();
308
313 void DestroyTexture();
314
316 // Member data
318 unsigned int myWidth;
319 unsigned int myHeight;
320 unsigned int myTextureWidth;
321 unsigned int myTextureHeight;
322 unsigned int myTexture;
323 bool myIsSmooth;
324 mutable std::vector<Color> myPixels;
325 mutable bool myNeedTextureUpdate;
326 mutable bool myNeedArrayUpdate;
327};
328
329} // namespace sf
330
331
332#endif // SFML_IMAGE_HPP
Color is an utility class for manipulating 32-bits RGBA colors.
Definition Color.hpp:41
unsigned int GetHeight() const
Return the height of the image.
Definition Image.cpp:526
void Bind() const
Bind the image for rendering.
Definition Image.cpp:473
bool SaveToFile(const std::string &Filename) const
Save the content of the image to a file.
Definition Image.cpp:209
const Uint8 * GetPixelsPtr() const
Get a read-only pointer to the array of pixels (RGBA 8 bits integers components) Array size is GetWid...
Definition Image.cpp:453
void Copy(const Image &Source, unsigned int DestX, unsigned int DestY, const IntRect &SourceRect=IntRect(0, 0, 0, 0), bool ApplyAlpha=false)
Copy pixels from another image onto this one.
Definition Image.cpp:270
bool Create(unsigned int Width, unsigned int Height, Color Col=Color(0, 0, 0, 255))
Create an empty image.
Definition Image.cpp:222
void SetPixel(unsigned int X, unsigned int Y, const Color &Col)
Change the color of a pixel.
Definition Image.cpp:408
bool LoadFromPixels(unsigned int Width, unsigned int Height, const Uint8 *Data)
Load the image directly from an array of pixels.
Definition Image.cpp:174
unsigned int GetWidth() const
Return the width of the image.
Definition Image.cpp:517
bool LoadFromMemory(const char *Data, std::size_t SizeInBytes)
Load the image from a file in memory.
Definition Image.cpp:145
const Color & GetPixel(unsigned int X, unsigned int Y) const
Get a pixel from the image.
Definition Image.cpp:431
bool LoadFromFile(const std::string &Filename)
Load the image from a file.
Definition Image.cpp:123
bool CopyScreen(RenderWindow &Window, const IntRect &SourceRect=IntRect(0, 0, 0, 0))
Create the image from the current contents of the given window.
Definition Image.cpp:358
static unsigned int GetValidTextureSize(unsigned int Size)
Get a valid texture size according to hardware support.
Definition Image.cpp:560
Image()
Default constructor.
Definition Image.cpp:43
FloatRect GetTexCoords(const IntRect &Rect) const
Convert a subrect expressed in pixels, into float texture coordinates.
Definition Image.cpp:545
void SetSmooth(bool Smooth)
Enable or disable image smooth filter.
Definition Image.cpp:490
bool IsSmooth() const
Tells whether the smooth filtering is enabled or not.
Definition Image.cpp:535
void CreateMaskFromColor(Color ColorKey, Uint8 Alpha=0)
Create transparency mask from a specified colorkey.
Definition Image.cpp:249
Rect is an utility class for manipulating rectangles.
Definition Rect.hpp:42
Simple wrapper for sf::Window that allows easy 2D rendering.
Window is a rendering window ; it can create a new window or connect to an existing one.