SFML
Simple and Fast Multimedia Library
Loading...
Searching...
No Matches
Image.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages 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 freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
31
34
36
37#include <filesystem>
38#include <optional>
39#include <string_view>
40#include <vector>
41
42#include <cstddef>
43#include <cstdint>
44
45
46namespace sf
47{
48class InputStream;
49
55{
56public:
65 Image() = default;
66
74 explicit Image(Vector2u size, Color color = Color::Black);
75
88 Image(Vector2u size, const std::uint8_t* pixels);
89
104 explicit Image(const std::filesystem::path& filename);
105
121 Image(const void* data, std::size_t size);
122
137 explicit Image(InputStream& stream);
138
146 void resize(Vector2u size, Color color = Color::Black);
147
160 void resize(Vector2u size, const std::uint8_t* pixels);
161
177 [[nodiscard]] bool loadFromFile(const std::filesystem::path& filename);
178
195 [[nodiscard]] bool loadFromMemory(const void* data, std::size_t size);
196
212 [[nodiscard]] bool loadFromStream(InputStream& stream);
213
229 [[nodiscard]] bool saveToFile(const std::filesystem::path& filename) const;
230
247 [[nodiscard]] std::optional<std::vector<std::uint8_t>> saveToMemory(std::string_view format) const;
248
255 [[nodiscard]] Vector2u getSize() const;
256
268 void createMaskFromColor(Color color, std::uint8_t alpha = 0);
269
302 [[nodiscard]] bool copy(const Image& source, Vector2u dest, const IntRect& sourceRect = {}, bool applyAlpha = false);
303
317 void setPixel(Vector2u coords, Color color);
318
333 [[nodiscard]] Color getPixel(Vector2u coords) const;
334
348 [[nodiscard]] const std::uint8_t* getPixelsPtr() const;
349
355
361
362private:
364 // Member data
366 Vector2u m_size;
367 std::vector<std::uint8_t> m_pixels;
368};
369
370} // namespace sf
371
372
#define SFML_GRAPHICS_API
Definition Export.hpp:42
Utility class for manipulating RGBA colors.
Definition Color.hpp:40
static const Color Black
Black predefined color.
Definition Color.hpp:82
Color getPixel(Vector2u coords) const
Get the color of a pixel.
bool loadFromStream(InputStream &stream)
Load the image from a custom stream.
Image(const std::filesystem::path &filename)
Construct the image from a file on disk.
bool saveToFile(const std::filesystem::path &filename) const
Save the image to a file on disk.
Image(Vector2u size, const std::uint8_t *pixels)
Construct the image from an array of pixels.
bool copy(const Image &source, Vector2u dest, const IntRect &sourceRect={}, bool applyAlpha=false)
Copy pixels from another image onto this one.
void flipHorizontally()
Flip the image horizontally (left <-> right).
std::optional< std::vector< std::uint8_t > > saveToMemory(std::string_view format) const
Save the image to a buffer in memory.
Image(const void *data, std::size_t size)
Construct the image from a file in memory.
void createMaskFromColor(Color color, std::uint8_t alpha=0)
Create a transparency mask from a specified color-key.
void flipVertically()
Flip the image vertically (top <-> bottom).
Vector2u getSize() const
Return the size (width and height) of the image.
const std::uint8_t * getPixelsPtr() const
Get a read-only pointer to the array of pixels.
Image()=default
Default constructor.
void resize(Vector2u size, const std::uint8_t *pixels)
Resize the image from an array of pixels.
bool loadFromMemory(const void *data, std::size_t size)
Load the image from a file in memory.
Image(Vector2u size, Color color=Color::Black)
Construct the image and fill it with a unique color.
bool loadFromFile(const std::filesystem::path &filename)
Load the image from a file on disk.
Image(InputStream &stream)
Construct the image from a custom stream.
void setPixel(Vector2u coords, Color color)
Change the color of a pixel.
void resize(Vector2u size, Color color=Color::Black)
Resize the image and fill it with a unique color.
Abstract class for custom file input streams.
Definition InputStream.hpp:46
Definition BlendMode.hpp:34
Vector2< unsigned int > Vector2u
Definition Vector2.hpp:225
Rect< int > IntRect
Definition Rect.hpp:153