Gamedev Framework (gf)
0.3.0
A C++11 framework for 2D games
|
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... | |
Image & | operator= (const Image &)=default |
Default copy assignment. More... | |
Image (Image &&)=default | |
Default move constructor. More... | |
Image & | operator= (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... | |
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:
gf::Image::Image | ( | ) |
Default constructor.
Creates an empty image.
|
default |
Default copy constructor.
|
default |
Default move constructor.
Create the image and fill it with a unique color.
size | Size of the image |
color | Fill 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.
size | Size of the image |
pixels | Array 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.
color | Color to make transparent |
alpha | Alpha 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.
size | Size of the image |
pixels | Array 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.
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.
Vector2u gf::Image::getSize | ( | ) | const |
Return the size (width and height) of the image.
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.
filename | Path of the image file to load |
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.
data | Pointer to the file data in memory |
length | Length of the data to load, in bytes |
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.
stream | Source stream to read from |
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 |