View on GitHub

DSFML

dsfml.graphics.image



class Image;
Class for loading, manipulating and saving images.

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.

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 Color. All the functions that return an array of pixels follow this rule, and all parameters that you pass to Image functions (such as loadFromPixels) must use this representation as well.

A 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.

Authors:
Laurent Gomila, Jeremy DeHaan

See Also:


http:
//www.sfml-dev.org/documentation/2.0/classsf_1_1Image.php#details

void create(uint width, uint height, Color color);
Create the image and fill it with a unique color.

Params:
uint width Width of the image
uint height Height of the image
Color color Fill color

void create(uint width, uint height, ref const ubyte[] pixels);
Create the image from an array of pixels.

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

Params:
uint width Width of the image
uint height Height of the image
ubyte[] pixels Array of pixels to copy to the image

bool loadFromFile(string 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.

Params:
filename Path of the image file to load

Returns:
True if loading succeeded, false if it failed

bool loadFromMemory(const(void)[] data);
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.

Params:
const(void)[] data Data file in memory to load

Returns:
True if loading succeeded, false if it failed

bool 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.

Params:
InputStream stream Source stream to read from

Returns:
True if loading succeeded, false if it failed

Color getPixel(uint x, uint y);
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 behaviour.

Params:
uint x X coordinate of the pixel to get
uint y Y coordinate of the pixel to get

Returns:
Color of the pixel at coordinates (x, y)

const(ubyte)[] getPixelArray();
Get the read-only array of pixels that make up the image.

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 (getSize().x * getSize().y * 4). Warning: the returned pointer may become invalid if you modify the image, so you should never store it for too long.

Returns:
Read-only array of pixels that make up the image.

Vector2u getSize();
Return the size (width and height) of the image.

Returns:
Size of the image, in pixels.

void setPixel(uint x, uint y, Color 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 behaviour.

Params:
uint x X coordinate of pixel to change
uint y Y coordinate of pixel to change
Color color New color of the pixel

void copyImage(ref const Image source, uint destX, uint destY, IntRect sourceRect = IntRect(0, 0, 0, 0), bool applyAlpha = false);
Copy pixels from another image onto this one.

This function does a slow pixel copy and should not be used intensively. It can be used to prepare a complex static image from several others, but if you need this kind of feature in real-time you'd better use RenderTexture.

If sourceRect is empty, the whole image is copied. If applyAlpha is set to true, the transparency of source pixels is applied. If it is false, the pixels are copied unchanged with their alpha value.

Params:
Image source Source image to copy
uint destX X coordinate of the destination position
uint destY Y coordinate of the destination position
IntRect sourceRect Sub-rectangle of the source image to copy
bool applyAlpha Should the copy take the source transparency into account?

void createMaskFromColor(Color maskColor, ubyte 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.

Params:
color Color to make transparent
ubyte alpha Alpha value to assign to transparent pixels

void flipHorizontally();
Flip the image horizontally (left <-> right)

void flipVertically();
Flip the image vertically (top <-> bottom)

bool saveToFile(string fileName);
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, tga and jpg. The destination file is overwritten if it already exists. This function fails if the image is empty.

Params:
filename Path of the file to save

Returns:
True if saving was successful