Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Public Member Functions | List of all members
gf::Image Class Reference

Class for loading, manipulating and saving images. More...

#include <gf/Image.h>

Public Member Functions

 Image ()
 Default constructor. More...
 
 Image (const Image &)=default
 Default copy constructor. More...
 
Imageoperator= (const Image &)=default
 Default copy assignment. More...
 
 Image (Image &&)=default
 Default move constructor. More...
 
Imageoperator= (Image &&)=default
 Default move assignment. More...
 
void create (Vector2u size, const Color4u &color=Color4u{0x00, 0x00, 0x00, 0xFF})
 Create the image and fill it with a unique color. More...
 
void create (Vector2u size, const uint8_t *pixels)
 Create the image from an array of pixels. More...
 
void createRGB (Vector2u size, const uint8_t *pixels)
 Create the image from an array of pixels. More...
 
bool loadFromFile (const Path &filename)
 Load the image from a file on disk. More...
 
bool loadFromMemory (const uint8_t *data, std::size_t length)
 Load the image from a file in memory. More...
 
bool loadFromStream (InputStream &stream)
 Load the image from a custom stream. More...
 
bool saveToFile (const Path &filename) const
 Save the image to a file on disk. More...
 
Vector2u getSize () const
 Return the size (width and height) of the image. More...
 
void createMaskFromColor (const Color4u &color, uint8_t alpha=0)
 Create a transparency mask from a specified color-key. More...
 
void setPixel (Vector2u pos, const Color4u &color)
 Change the color of a pixel. More...
 
Color4u getPixel (Vector2u pos) const
 Get the color of a pixel. More...
 
const uint8_t * getPixelsPtr () const
 Get a read-only pointer to the array of pixels. More...
 
void flipHorizontally ()
 Flip the pixels horizontally. More...
 

Detailed Description

Class for loading, manipulating and saving images.

gf::Image is an abstraction to manipulate images as bidimensional arrays of pixels. The class provides functions to load, read, write and save pixels, as well as many other useful functions.

gf::Image can handle a unique internal representation of pixels, which is RGBA 32 bits. This means that a pixel must be composed of 8 bits red, green, blue and alpha channels – just like a gf::Color4u.

All the functions that return an array of pixels follow this rule, and all parameters that you pass to gf::Image functions (such as loadFromMemory()) must use this representation as well.

A gf::Image can be copied, but it is a heavy resource and if possible you should always use (const) references to pass or return them to avoid useless copies.

Usage example:

// Load an image file from a file
gf::Image background;
if (!background.loadFromFile("background.jpg")) {
return -1;
}
// Create a 20x20 image filled with black color
gf::Image image;
image.create({ 20, 20 }, sf::Color4u{0xFF, 0xFF, 0xFF, 0xFF});
// Make the top-left pixel transparent
gf::Color4u color = image.getPixel({ 0, 0 });
color.a = 0;
image.setPixel({ 0, 0 }, color);
// Save the image to a file
if (!image.saveToFile("result.png")) {
return -1;
}
See also
gf::Texture

Constructor & Destructor Documentation

gf::Image::Image ( )

Default constructor.

Creates an empty image.

gf::Image::Image ( const Image )
default

Default copy constructor.

gf::Image::Image ( Image &&  )
default

Default move constructor.

Member Function Documentation

void gf::Image::create ( Vector2u  size,
const Color4u color = Color4u{0x00, 0x00, 0x00, 0xFF} 
)

Create the image and fill it with a unique color.

Parameters
sizeSize of the image
colorFill color
void gf::Image::create ( Vector2u  size,
const uint8_t *  pixels 
)

Create the image from an array of pixels.

The pixel array is assumed to contain 32-bits RGBA pixels, and have the given size. If not, this is an undefined behavior. If pixels is null, an empty image is created.

Parameters
sizeSize of the image
pixelsArray of pixels to copy to the image
void gf::Image::createMaskFromColor ( const Color4u color,
uint8_t  alpha = 0 
)

Create a transparency mask from a specified color-key.

This function sets the alpha value of every pixel matching the given color to alpha (0 by default), so that they become transparent.

Parameters
colorColor to make transparent
alphaAlpha value to assign to transparent pixels
void gf::Image::createRGB ( Vector2u  size,
const uint8_t *  pixels 
)

Create the image from an array of pixels.

The pixel array is assumed to contain 24-bits RGB pixels, and have the given size. If not, this is an undefined behavior. If pixels is null, an empty image is created.

Parameters
sizeSize of the image
pixelsArray of pixels to copy to the image
void gf::Image::flipHorizontally ( )

Flip the pixels horizontally.

This function is needed internally. But you can use it if you want.

Color4u gf::Image::getPixel ( Vector2u  pos) const

Get the color of a pixel.

This function doesn't check the validity of the pixel coordinates, using out-of-range values will result in an undefined behavior.

Parameters
posCoordinate of pixel to get
Returns
Color of the pixel at coordinates (x, y)
See also
setPixel
const uint8_t* gf::Image::getPixelsPtr ( ) const

Get a read-only pointer to the array of pixels.

The returned value points to an array of RGBA pixels made of 8 bits integers components. The size of the array is width * height * 4. Warning: the returned pointer may become invalid if you modify the image, so you should never store it for too long. If the image is empty, a null pointer is returned.

Returns
Read-only pointer to the array of pixels
Vector2u gf::Image::getSize ( ) const

Return the size (width and height) of the image.

Returns
Size of the image, in pixels
bool gf::Image::loadFromFile ( const Path filename)

Load the image from a file on disk.

The supported image formats are bmp, png, tga, jpg, gif, psd, hdr and pic. Some format options are not supported, like progressive jpeg. If this function fails, the image is left unchanged.

Parameters
filenamePath of the image file to load
Returns
true if loading was successful
See also
loadFromMemory(), loadFromStream(), saveToFile()
bool gf::Image::loadFromMemory ( const uint8_t *  data,
std::size_t  length 
)

Load the image from a file in memory.

The supported image formats are bmp, png, tga, jpg, gif, psd, hdr and pic. Some format options are not supported, like progressive jpeg. If this function fails, the image is left unchanged.

Parameters
dataPointer to the file data in memory
lengthLength of the data to load, in bytes
Returns
true if loading was successful
See also
loadFromFile(), loadFromStream(), saveToFile()
bool gf::Image::loadFromStream ( InputStream stream)

Load the image from a custom stream.

The supported image formats are bmp, png, tga, jpg, gif, psd, hdr and pic. Some format options are not supported, like progressive jpeg. If this function fails, the image is left unchanged.

Parameters
streamSource stream to read from
Returns
True if loading was successful
See also
loadFromFile(), loadFromMemory()
Image& gf::Image::operator= ( const Image )
default

Default copy assignment.

Image& gf::Image::operator= ( Image &&  )
default

Default move assignment.

bool gf::Image::saveToFile ( const Path filename) const

Save the image to a file on disk.

The format of the image is automatically deduced from the extension. The supported image formats are bmp, png and tga. The destination file is overwritten if it already exists. This function fails if the image is empty.

Parameters
filenamePath of the file to save
Returns
true if saving was successful
See also
create, loadFromFile(), loadFromMemory(), loadFromStream()
void gf::Image::setPixel ( Vector2u  pos,
const Color4u color 
)

Change the color of a pixel.

This function doesn't check the validity of the pixel coordinates, using out-of-range values will result in an undefined behavior.

Parameters
posCoordinate of pixel to change
colorNew color of the pixel
See also
getPixel