Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Image.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 Julien Bernard
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
20 *
21 * Part of this file comes from SFML, with the same license:
22 * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23 */
24#ifndef GF_IMAGE_H
25#define GF_IMAGE_H
26
27#include <cstddef>
28#include <cstdint>
29#include <vector>
30
31#include "CoreApi.h"
32#include "Path.h"
33#include "Span.h"
34#include "Vector.h"
35#include "Rect.h"
36
37namespace gf {
38#ifndef DOXYGEN_SHOULD_SKIP_THIS
39inline namespace v1 {
40#endif
41
42 class InputStream;
43
51 };
52
81 class GF_CORE_API Image {
82 public:
89
98
105 Image(Vector2i size, Color4u color);
106
113 Image(Vector2i size, Color3u color);
114
126 Image(Vector2i size, const uint8_t *pixels, PixelFormat format = PixelFormat::Rgba32);
127
138 Image(const Path& filename);
139
151
163
167 Image(const Image&) = default;
168
172 Image& operator=(const Image&) = default;
173
177 Image(Image&&) = default;
178
182 Image& operator=(Image&&) = default;
183
196 bool saveToFile(const Path& filename) const;
197
208 Image subImage(const RectI& area) const;
209
216
227 void createMaskFromColor(const Color4u& color, uint8_t alpha = 0);
228
241 void setPixel(Vector2i pos, const Color4u& color);
242
257
270 const uint8_t* getPixelsPtr() const;
271
278
279 private:
280 Vector2i m_size;
281 std::vector<uint8_t> m_pixels;
282
283 };
284
285#ifndef DOXYGEN_SHOULD_SKIP_THIS
286}
287#endif
288}
289
290#endif // GF_IMAGE_H
Class for loading, manipulating and saving images.
Definition: Image.h:81
Image(Image &&)=default
Default move constructor.
void createMaskFromColor(const Color4u &color, uint8_t alpha=0)
Create a transparency mask from a specified color-key.
Image(Vector2i size, Color4u color)
Create the image and fill it with a unique color.
Image subImage(const RectI &area) const
Create a sub-image of the image from a defined area.
Image()
Default constructor.
void flipHorizontally()
Flip the pixels horizontally.
Image(Vector2i size)
Create the image.
Image & operator=(const Image &)=default
Default copy assignment.
Vector2i getSize() const
Return the size (width and height) of the image.
Image(const Path &filename)
Load the image from a file on disk.
Image(Vector2i size, const uint8_t *pixels, PixelFormat format=PixelFormat::Rgba32)
Create the image from an array of pixels.
Image(const Image &)=default
Default copy constructor.
void setPixel(Vector2i pos, const Color4u &color)
Change the color of a pixel.
Color4u getPixel(Vector2i pos) const
Get the color of a pixel.
Image(Vector2i size, Color3u color)
Create the image and fill it with a unique color.
bool saveToFile(const Path &filename) const
Save the image to a file on disk.
Image & operator=(Image &&)=default
Default move assignment.
const uint8_t * getPixelsPtr() const
Get a read-only pointer to the array of pixels.
Image(InputStream &stream)
Load the image from a custom stream.
Image(Span< const uint8_t > content)
Load the image from a file in memory.
Abstract class for custom file input streams.
Definition: Stream.h:54
PixelFormat
Pixel format.
Definition: Image.h:48
@ Rgba32
Four 8-bit channels.
Definition: Image.h:49
@ Rgb24
Three 8-bit channels.
Definition: Image.h:50
std::filesystem::path Path
A path in the filesystem.
Definition: Path.h:40
The namespace for gf classes.
A 3D vector.
Definition: Vector.h:570
A 4D vector.
Definition: Vector.h:852