![]() |
Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
|
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 (Span< const 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... | |
Image & | operator= (const Image &)=default |
Default copy assignment. More... | |
Image (Image &&)=default | |
Default move constructor. More... | |
Image & | operator= (Image &&)=default |
Default move assignment. More... | |
bool | saveToFile (const Path &filename) const |
Save the image to a file on disk. More... | |
Image | subImage (const RectI &area) const |
Create a sub-image of the image from a defined area. 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... | |
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:
gf::Image::Image | ( | ) |
Default constructor.
Creates an empty image.
gf::Image::Image | ( | Vector2i | size | ) |
Create the image.
The initial color of the pixels is opaque white
size | Size of the image |
Create the image and fill it with a unique color.
size | Size of the image |
color | Fill color |
Create the image and fill it with a unique color.
size | Size of the image |
color | Fill color |
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.
size | Size of the image |
pixels | Array of pixels to copy to the image |
format | Format of the pixels |
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.
filename | Path of the image file to load |
gf::Image::Image | ( | Span< const 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.
content | Content of the file data in memory |
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.
stream | Source stream to read from |
|
default |
Default copy constructor.
|
default |
Default move constructor.
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.
color | Color to make transparent |
alpha | Alpha value to assign to transparent pixels |
void gf::Image::flipHorizontally | ( | ) |
Flip the pixels horizontally.
This function is needed internally. But you can use it if you want.
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.
pos | Coordinate of pixel to get |
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.
Vector2i gf::Image::getSize | ( | ) | const |
Return the size (width and height) of the image.
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.
filename | Path of the file to save |
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.
pos | Coordinate of pixel to change |
color | New color of the pixel |
Create a sub-image of the image from a defined area.
If the area doesn't contain the image, an empty image is returned. If the area is bigger than the size of the image, the image itself is returned.
area | Sub-area of the image |