Gamedev Framework (gf)  0.17.0
A C++14 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 (Vector2i size)
 Create the image. More...
 
 Image (Vector2i size, Color4u color)
 Create the image and fill it with a unique color. More...
 
 Image (Vector2i size, Color3u color)
 Create the image and fill it with a unique color. More...
 
 Image (Vector2i size, const uint8_t *pixels, PixelFormat format=PixelFormat::Rgba32)
 Create the image from an array of pixels. More...
 
 Image (const Path &filename)
 Load the image from a file on disk. More...
 
 Image (ArrayRef< uint8_t > content)
 Load the image from a file in memory. More...
 
 Image (InputStream &stream)
 Load the image from a custom stream. 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...
 
bool saveToFile (const Path &filename) const
 Save the image to a file on disk. More...
 
Vector2i 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 (Vector2i pos, const Color4u &color)
 Change the color of a pixel. More...
 
Color4u getPixel (Vector2i 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 constructors 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("background.jpg");
// Create a 20x20 image filled with black color
gf::Image image({ 20, 20 }, gf::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")) {
// error
return;
}
See also
gf::Texture

Constructor & Destructor Documentation

◆ Image() [1/10]

gf::Image::Image ( )

Default constructor.

Creates an empty image.

◆ Image() [2/10]

gf::Image::Image ( Vector2i  size)

Create the image.

The initial color of the pixels is opaque white

Parameters
sizeSize of the image

◆ Image() [3/10]

gf::Image::Image ( Vector2i  size,
Color4u  color 
)

Create the image and fill it with a unique color.

Parameters
sizeSize of the image
colorFill color

◆ Image() [4/10]

gf::Image::Image ( Vector2i  size,
Color3u  color 
)

Create the image and fill it with a unique color.

Parameters
sizeSize of the image
colorFill color

◆ Image() [5/10]

gf::Image::Image ( Vector2i  size,
const uint8_t *  pixels,
PixelFormat  format = PixelFormat::Rgba32 
)

Create the image from an array of pixels.

The pixels array is assumed to contain pixels with format given by format pixel format, and have the given size. If not, this is an undefined behavior.

Parameters
sizeSize of the image
pixelsArray of pixels to copy to the image
formatFormat of the pixels

◆ Image() [6/10]

gf::Image::Image ( 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

◆ Image() [7/10]

gf::Image::Image ( ArrayRef< uint8_t >  content)

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
contentContent of the file data in memory

◆ Image() [8/10]

gf::Image::Image ( 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

◆ Image() [9/10]

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

Default copy constructor.

◆ Image() [10/10]

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

Default move constructor.

Member Function Documentation

◆ createMaskFromColor()

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

◆ flipHorizontally()

void gf::Image::flipHorizontally ( )

Flip the pixels horizontally.

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

◆ getPixel()

Color4u gf::Image::getPixel ( Vector2i  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

◆ getPixelsPtr()

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

◆ getSize()

Vector2i gf::Image::getSize ( ) const

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

Returns
Size of the image, in pixels

◆ operator=() [1/2]

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

Default copy assignment.

◆ operator=() [2/2]

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

Default move assignment.

◆ saveToFile()

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

◆ setPixel()

void gf::Image::setPixel ( Vector2i  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