SFML 3.0.2
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-2025 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
30#include <SFML/Graphics/Export.hpp>
31
32#include <SFML/Graphics/Color.hpp>
33#include <SFML/Graphics/Rect.hpp>
34
35#include <SFML/System/Vector2.hpp>
36
37#include <filesystem>
38#include <optional>
39#include <string_view>
40#include <vector>
41
42#include <cstddef>
43#include <cstdint>
44
45
46 namespace sf
47{
48 class InputStream;
49
54 class SFML_GRAPHICS_API Image
55{
56 public:
65 Image() = default;
66
74 explicit Image(Vector2u size, Color color = Color::Black);
75
87 Image(Vector2u size, const std::uint8_t* pixels);
88
103 explicit Image(const std::filesystem::path& filename);
104
120 Image(const void* data, std::size_t size);
121
136 explicit Image(InputStream& stream);
137
145 void resize(Vector2u size, Color color = Color::Black);
146
158 void resize(Vector2u size, const std::uint8_t* pixels);
159
175 [[nodiscard]] bool loadFromFile(const std::filesystem::path& filename);
176
193 [[nodiscard]] bool loadFromMemory(const void* data, std::size_t size);
194
210 [[nodiscard]] bool loadFromStream(InputStream& stream);
211
227 [[nodiscard]] bool saveToFile(const std::filesystem::path& filename) const;
228
245 [[nodiscard]] std::optional<std::vector<std::uint8_t>> saveToMemory(std::string_view format) const;
246
253 [[nodiscard]] Vector2u getSize() const;
254
266 void createMaskFromColor(Color color, std::uint8_t alpha = 0);
267
300 [[nodiscard]] bool copy(const Image& source, Vector2u dest, const IntRect& sourceRect = {}, bool applyAlpha = false);
301
315 void setPixel(Vector2u coords, Color color);
316
331 [[nodiscard]] Color getPixel(Vector2u coords) const;
332
346 [[nodiscard]] const std::uint8_t* getPixelsPtr() const;
347
352 void flipHorizontally();
353
358 void flipVertically();
359
360 private:
362 // Member data
364 Vector2u m_size;
365 std::vector<std::uint8_t> m_pixels;
366};
367
368} // namespace sf
369
370
Color.hpp
Export.hpp
SFML_GRAPHICS_API
#define SFML_GRAPHICS_API
Definition Graphics/Export.hpp:42
Rect.hpp
Vector2.hpp
sf::Color
Utility class for manipulating RGBA colors.
Definition Color.hpp:40
sf::Color::Black
static const Color Black
Black predefined color.
Definition Color.hpp:82
sf::Image::getPixel
Color getPixel(Vector2u coords) const
Get the color of a pixel.
sf::Image::loadFromStream
bool loadFromStream(InputStream &stream)
Load the image from a custom stream.
sf::Image::Image
Image(const std::filesystem::path &filename)
Construct the image from a file on disk.
sf::Image::saveToFile
bool saveToFile(const std::filesystem::path &filename) const
Save the image to a file on disk.
sf::Image::Image
Image(Vector2u size, const std::uint8_t *pixels)
Construct the image from an array of pixels.
sf::Image::copy
bool copy(const Image &source, Vector2u dest, const IntRect &sourceRect={}, bool applyAlpha=false)
Copy pixels from another image onto this one.
sf::Image::flipHorizontally
void flipHorizontally()
Flip the image horizontally (left <-> right)
sf::Image::saveToMemory
std::optional< std::vector< std::uint8_t > > saveToMemory(std::string_view format) const
Save the image to a buffer in memory.
sf::Image::Image
Image(const void *data, std::size_t size)
Construct the image from a file in memory.
sf::Image::createMaskFromColor
void createMaskFromColor(Color color, std::uint8_t alpha=0)
Create a transparency mask from a specified color-key.
sf::Image::flipVertically
void flipVertically()
Flip the image vertically (top <-> bottom)
sf::Image::getSize
Vector2u getSize() const
Return the size (width and height) of the image.
sf::Image::getPixelsPtr
const std::uint8_t * getPixelsPtr() const
Get a read-only pointer to the array of pixels.
sf::Image::Image
Image()=default
Default constructor.
sf::Image::resize
void resize(Vector2u size, const std::uint8_t *pixels)
Resize the image from an array of pixels.
sf::Image::loadFromMemory
bool loadFromMemory(const void *data, std::size_t size)
Load the image from a file in memory.
sf::Image::Image
Image(Vector2u size, Color color=Color::Black)
Construct the image and fill it with a unique color.
sf::Image::loadFromFile
bool loadFromFile(const std::filesystem::path &filename)
Load the image from a file on disk.
sf::Image::Image
Image(InputStream &stream)
Construct the image from a custom stream.
sf::Image::setPixel
void setPixel(Vector2u coords, Color color)
Change the color of a pixel.
sf::Image::resize
void resize(Vector2u size, Color color=Color::Black)
Resize the image and fill it with a unique color.
sf::InputStream
Abstract class for custom file input streams.
Definition InputStream.hpp:46
sf
Definition AudioResource.hpp:36
sf::Vector2u
Vector2< unsigned int > Vector2u
Definition Vector2.hpp:209
sf::IntRect
Rect< int > IntRect
Definition Rect.hpp:146