SFML 2.3.2

- Main Page
- Related Pages
- Modules
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
sf::Image Class Reference
Graphics module

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

`#include <Image.hpp>`

## Public Member Functions
| Image ()
| Default constructor. More...
| ~Image ()
| Destructor. More...
void | create (unsigned int width, unsigned int height, const Color &color=Color(0, 0, 0))
| Create the image and fill it with a unique color. More...
void | create (unsigned int width, unsigned int height, const Uint8 *pixels)
| Create the image from an array of pixels. More...
bool | loadFromFile (const std::string &filename)
| Load the image from a file on disk. More...
bool | loadFromMemory (const void *data, std::size_t size)
| Load the image from a file in memory. More...
bool | loadFromStream (InputStream &stream)
| Load the image from a custom stream. More...
bool | saveToFile (const std::string &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 Color &color, Uint8 alpha=0)
| Create a transparency mask from a specified color-key. More...
void | copy (const Image &source, unsigned int destX, unsigned int destY, const IntRect &sourceRect=IntRect(0, 0, 0, 0), bool applyAlpha=false)
| Copy pixels from another image onto this one. More...
void | setPixel (unsigned int x, unsigned int y, const Color &color)
| Change the color of a pixel. More...
Color | getPixel (unsigned int x, unsigned int y) const
| Get the color of a pixel. More...
const Uint8 * | getPixelsPtr () const
| Get a read-only pointer to the array of pixels. More...
void | flipHorizontally ()
| Flip the image horizontally (left <-> right) More...
void | flipVertically ()
| Flip the image vertically (top <-> bottom) More...

## Detailed Description

Class for loading, manipulating and saving images.

sf::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.

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

A sf::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
sf::Image background;
if (!background.loadFromFile("background.jpg"))
return -1;
// Create a 20x20 image filled with black color
sf::Image image;
image.create(20, 20, sf::Color::Black);
// Copy image1 on image2 at position (10, 10)
image.copy(background, 10, 10);
// Make the top-left pixel transparent
sf::Color 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
  sf::Texture

Definition at line 46 of file Image.hpp.

## Constructor & Destructor Documentation

sf::Image::Image | ( |  | ) |

Default constructor.

Creates an empty image.

sf::Image::~Image | ( |  | ) |

Destructor.

## Member Function Documentation

void sf::Image::copy | ( | const Image & | source,
|  | unsigned int | destX,
|  | unsigned int | destY,
|  | const 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 sf::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.

Parameters

  source | Source image to copy
  destX | X coordinate of the destination position
  destY | Y coordinate of the destination position
  sourceRect | Sub-rectangle of the source image to copy
  applyAlpha | Should the copy take into account the source transparency?

void sf::Image::create | ( | unsigned int | width,
|  | unsigned int | height,
|  | const Color & | color = `Color(0, 0, 0)`
| ) |  |

Create the image and fill it with a unique color.

Parameters

  width | Width of the image
  height | Height of the image
  color | Fill color

void sf::Image::create | ( | unsigned int | width,
|  | unsigned int | height,
|  | const Uint8 * | 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 behavior. If pixels is null, an empty image is created.

Parameters

  width | Width of the image
  height | Height of the image
  pixels | Array of pixels to copy to the image

void sf::Image::createMaskFromColor | ( | const Color & | color,
|  | Uint8 | 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

  color | Color to make transparent
  alpha | Alpha value to assign to transparent pixels

void sf::Image::flipHorizontally | ( |  | ) |

Flip the image horizontally (left <-> right)

void sf::Image::flipVertically | ( |  | ) |

Flip the image vertically (top <-> bottom)

Color sf::Image::getPixel | ( | unsigned int | x,
|  | unsigned int | y
| ) |  | 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

  x | X coordinate of pixel to get
  y | Y coordinate of pixel to get

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

See also
  setPixel

const Uint8* sf::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 (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. If the image is empty, a null pointer is returned.

Returns
  Read-only pointer to the array of pixels

Vector2u sf::Image::getSize | ( |  | ) | const

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

Returns
  Size of the image, in pixels

bool sf::Image::loadFromFile | ( | const std::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.

Parameters

  filename | Path of the image file to load

Returns
  True if loading was successful

See also
  loadFromMemory, loadFromStream, saveToFile

bool sf::Image::loadFromMemory | ( | const void * | data,
|  | std::size_t | size
| ) |  |

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

  data | Pointer to the file data in memory
  size | Size of the data to load, in bytes

Returns
  True if loading was successful

See also
  loadFromFile, loadFromStream

bool sf::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

  stream | Source stream to read from

Returns
  True if loading was successful

See also
  loadFromFile, loadFromMemory

bool sf::Image::saveToFile | ( | const std::string & | 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, tga and jpg. The destination file is overwritten if it already exists. This function fails if the image is empty.

Parameters

  filename | Path of the file to save

Returns
  True if saving was successful

See also
  create, loadFromFile, loadFromMemory

void sf::Image::setPixel | ( | unsigned int | x,
|  | unsigned int | y,
|  | const 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 behavior.

Parameters

  x | X coordinate of pixel to change
  y | Y coordinate of pixel to change
  color | New color of the pixel

See also
  getPixel

The documentation for this class was generated from the following file:

- Image.hpp