-
Notifications
You must be signed in to change notification settings - Fork 30
Images
In a previous chapter we learned about Textures. Textures are images stored on the graphics card for quick rendering. However, that way it cannot be modified directly. In order to that, the image data needs to be stored in the RAM.
The Image class represents images that are stored in the RAM and not in the graphics card's memory, which opens up the possibility of manipulating them. They cannot be used for rendering directly, but they can be transferred from and to Textures. Apart from that, they can be loaded from and saved to files directly.
##Getting images
As mentioned already, images can be loaded from a file just like a texture, using the [loadFromFile](http://jsfml.org/javadoc/org/jsfml/graphics/Image.html#loadFromFile(java.nio.file.Path\)) method, which throws an IOException
if something goes wrong.
You can also create an image from a texure that is located in the graphics card storage. The Texture class provides the [copyToImage](http://www.jsfml.org/javadoc/org/jsfml/graphics/Texture.html#copyToImage(\)) method which does just that: create a copy of the image in the RAM. This is a potentially heavy operation and should only be done if really needed. A typical use case for this is taking screenshots, which will be covered in the Screenshots tutorial.
Last but not least, you can create empty images using the [create](http://jsfml.org/javadoc/org/jsfml/graphics/Image.html#create(int, int, org.jsfml.graphics.Color)) method.
##Saving images
An image can be saved using the [saveToFile](http://jsfml.org/javadoc/org/jsfml/graphics/Image.html#saveToFile(java.nio.file.Path\)) method. The extension of the file name determines the format the image is saved in. Supported formats are bmp, png, tga, jpg
.
##Uploading images "Uploading" an image means making it a Texture that resides in the graphics card's memory. Ideally, this is done only once when you are done modifying the image, because it is a heavy operation.
The Texture class provides the [loadFromImage](http://www.jsfml.org/javadoc/org/jsfml/graphics/Texture.html#loadFromImage(org.jsfml.graphics.Image\)) method that creates a texture from the given image instead of loading from a file.
#Manipulating images The image data (more specifically: the colors of the pixels in the image) can be changed in a number of ways.
##Pixel manipulation The most simplistic of all operations is to alter single pixels. To get the color of a certain pixel in the image, use the [getPixel](http://jsfml.org/javadoc/org/jsfml/graphics/Image.html#getPixel(int, int)) method. Using [setPixel](http://jsfml.org/javadoc/org/jsfml/graphics/Image.html#setPixel(int, int, org.jsfml.graphics.Color)), you can change its color.
##Blitting Copying one part of an image onto another image is known as blitting. This was the standard "rendering" technique before hardware acceleration was common and was used in almost every 2D game before the 2000s. Nowadays, because this is a relatively heavy operation, the use cases are limited.
The various overloads of the [copy](http://jsfml.org/javadoc/org/jsfml/graphics/Image.html#copy(org.jsfml.graphics.Image, int, int)) method can be used to copy parts of one image onto another.
##Flipping An image can be flipped (a.k.a. mirrored) at the X or Y axes using the [flipHorizontally](http://jsfml.org/javadoc/org/jsfml/graphics/Image.html#flipHorizontally(\)) and [flipVertically](http://jsfml.org/javadoc/org/jsfml/graphics/Image.html#flipVertically(\)) methods.
Note that you should not use this to ultimately flip Sprites. Sprites can be flipped by giving it a negative width or height in the [texture rectangle](http://jsfml.org/javadoc/org/jsfml/graphics/Sprite.html#setTextureRect(org.jsfml.graphics.IntRect\)). That is hardware accelerated and does not need an excursion over images.
##Color keys Color keying or color masking is replacing a certain color on the image by transparency. Unless you use texture files which have an alpha channel (e.g. 32-bit PNGs), you will need the [createMaskFromColor](http://jsfml.org/javadoc/org/jsfml/graphics/Image.html#createMaskFromColor(org.jsfml.graphics.Color\)) method to replace a color by transparency.
For example, if you have a sprite sheet using a blue background color that should be replaced by transparency in the texture, you would do it this way:
//Prepare the texture
Texture texture = new Texture();
try {
//Load the image.
Image image = new Image();
image.loadFromFile(Paths.get("spritesheet.png"));
//Apply the color mask
image.createMaskFromColor(Color.BLUE);
//Load the masked image into the texture
texture.loadFromImage(image);
} catch(IOException|TextureCreationException ex) {
System.err.println("Something went wrong:");
ex.printStackTrace();
}