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
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
353
359
360private:
362 // Member data
364 Vector2u m_size;
365 std::vector<std::uint8_t> m_pixels;
366};
367
368} // namespace sf
369
370
#define SFML_GRAPHICS_API
Utility class for manipulating RGBA colors.
Definition Color.hpp:40
Class for loading, manipulating and saving images.
Definition Image.hpp:55
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.