libdonut 2.3.6
Application framework for cross-platform game development in C++20
Loading...
Searching...
No Matches
Image.hpp
Go to the documentation of this file.
1#ifndef DONUT_GRAPHICS_IMAGE_HPP
2#define DONUT_GRAPHICS_IMAGE_HPP
3
6
7#include <cassert> // assert
8#include <cstddef> // std::size_t
9#include <cstdint> // std::uint32_t
10#include <optional> // std::optional
11
12namespace donut::graphics {
13
18enum class PixelFormat : std::uint32_t {
19 R = 0x1903,
20 RG = 0x8227,
21 RGB = 0x1907,
22 RGBA = 0x1908,
23};
24
28enum class PixelComponentType : std::uint32_t {
29 U8 = 0x1401,
30 F16 = 0x140B,
31 F32 = 0x1406,
32};
33
39class ImageView {
40public:
44 constexpr ImageView() noexcept = default;
45
60 constexpr ImageView(std::size_t width, std::size_t height, PixelFormat pixelFormat, PixelComponentType pixelComponentType, const void* pixels) noexcept
61 : pixels(pixels)
62 , width(width)
63 , height(height)
64 , pixelFormat(pixelFormat)
65 , pixelComponentType(pixelComponentType) {
66 assert(pixels || (width == 0 && height == 0 && pixelFormat == PixelFormat::R && pixelComponentType == PixelComponentType::U8));
67 }
68
74 explicit operator bool() const noexcept {
75 return static_cast<bool>(pixels);
76 }
77
87 [[nodiscard]] constexpr std::size_t getWidth() const noexcept {
88 return width;
89 }
90
100 [[nodiscard]] constexpr std::size_t getHeight() const noexcept {
101 return height;
102 }
103
112 [[nodiscard]] constexpr PixelFormat getPixelFormat() const noexcept {
113 return pixelFormat;
114 }
115
124 [[nodiscard]] constexpr PixelComponentType getPixelComponentType() const noexcept {
125 return pixelComponentType;
126 }
127
156 [[nodiscard]] constexpr const void* getPixels() const noexcept {
157 return pixels;
158 }
159
170 [[nodiscard]] constexpr std::size_t getChannelCount() const noexcept {
171 if (pixels) {
172 switch (pixelFormat) {
173 case PixelFormat::R: return 1;
174 case PixelFormat::RG: return 2;
175 case PixelFormat::RGB: return 3;
176 case PixelFormat::RGBA: return 4;
177 }
178 }
179 return 0;
180 }
181
192 [[nodiscard]] constexpr std::size_t getPixelComponentSize() const noexcept {
193 if (pixels) {
194 switch (pixelComponentType) {
195 case PixelComponentType::U8: return 1;
196 case PixelComponentType::F16: return 2;
197 case PixelComponentType::F32: return 4;
198 }
199 }
200 return 0;
201 }
202
214 [[nodiscard]] constexpr std::size_t getPixelStride() const noexcept {
216 }
217
228 [[nodiscard]] constexpr std::size_t getSizeInBytes() const noexcept {
229 return getWidth() * getHeight() * getPixelStride();
230 }
231
232private:
233 const void* pixels = nullptr;
234 std::size_t width = 0;
235 std::size_t height = 0;
236 PixelFormat pixelFormat = PixelFormat::R;
237 PixelComponentType pixelComponentType = PixelComponentType::U8;
238};
239
252
256 bool flipVertically = false;
257};
258
266 bool flipVertically = false;
267};
268
279 bool useRleCompression = true;
280
284 bool flipVertically = false;
285};
286
297 int quality = 90;
298
302 bool flipVertically = false;
303};
304
312 bool flipVertically = false;
313};
314
322 bool flipVertically = false;
323};
324
332 std::optional<PixelFormat> desiredFormat{};
333
345 bool highDynamicRange = false;
346
350 bool flipVertically = false;
351};
352
358class Image {
359public:
375 static void savePNG(const ImageView& image, Filesystem& filesystem, const char* filepath, const ImageSavePNGOptions& options = {});
376
392 static void saveBMP(const ImageView& image, Filesystem& filesystem, const char* filepath, const ImageSaveBMPOptions& options = {});
393
409 static void saveTGA(const ImageView& image, Filesystem& filesystem, const char* filepath, const ImageSaveTGAOptions& options = {});
410
426 static void saveJPG(const ImageView& image, Filesystem& filesystem, const char* filepath, const ImageSaveJPGOptions& options = {});
427
444 static void saveHDR(const ImageView& image, Filesystem& filesystem, const char* filepath, const ImageSaveHDROptions& options = {});
445
461 static void save(const ImageView& image, Filesystem& filesystem, const char* filepath, const ImageSaveOptions& options = {});
462
466 Image() noexcept = default;
467
488 Image(std::size_t width, std::size_t height, PixelFormat pixelFormat, PixelComponentType pixelComponentType, const void* pixels);
489
497 explicit Image(const ImageView& image);
498
538 explicit Image(const Filesystem& filesystem, const char* filepath, const ImageOptions& options = {});
539
545 explicit operator bool() const noexcept {
546 return static_cast<bool>(pixels);
547 }
548
556 operator ImageView() const noexcept {
558 }
559
563 void reset() noexcept {
564 *this = Image{};
565 }
566
576 [[nodiscard]] std::size_t getWidth() const noexcept {
577 return width;
578 }
579
589 [[nodiscard]] std::size_t getHeight() const noexcept {
590 return height;
591 }
592
601 [[nodiscard]] PixelFormat getPixelFormat() const noexcept {
602 return pixelFormat;
603 }
604
613 [[nodiscard]] PixelComponentType getPixelComponentType() const noexcept {
614 return pixelComponentType;
615 }
616
632 [[nodiscard]] void* getPixels() noexcept {
633 return pixels.get();
634 }
635
651 [[nodiscard]] const void* getPixels() const noexcept {
652 return pixels.get();
653 }
654
663 [[nodiscard]] std::size_t getChannelCount() const noexcept {
664 return ImageView{*this}.getChannelCount();
665 }
666
676 [[nodiscard]] std::size_t getPixelComponentSize() const noexcept {
677 return ImageView{*this}.getPixelComponentSize();
678 }
679
690 [[nodiscard]] std::size_t getPixelStride() const noexcept {
691 return ImageView{*this}.getPixelStride();
692 }
693
704 [[nodiscard]] std::size_t getSizeInBytes() const noexcept {
705 return ImageView{*this}.getSizeInBytes();
706 }
707
708private:
709 struct PixelsDeleter {
710 void operator()(void* handle) const noexcept;
711 };
712
714 std::size_t width = 0;
715 std::size_t height = 0;
716 PixelFormat pixelFormat = PixelFormat::R;
717 PixelComponentType pixelComponentType = PixelComponentType::U8;
718};
719
720} // namespace donut::graphics
721
722#endif
Persistent system for managing the virtual filesystem.
Definition Filesystem.hpp:185
Generic nullable RAII resource handle with exclusive ownership of a resource that is automatically de...
Definition UniqueHandle.hpp:21
constexpr Handle get() const noexcept
Get the value of the underlying resource handle.
Definition UniqueHandle.hpp:152
Read-only non-owning view over a 2D image.
Definition Image.hpp:39
constexpr std::size_t getPixelStride() const noexcept
Get the stride in bytes of the pixels in the image referenced by this view.
Definition Image.hpp:214
constexpr PixelFormat getPixelFormat() const noexcept
Get the pixel format of the image referenced by this view.
Definition Image.hpp:112
constexpr std::size_t getPixelComponentSize() const noexcept
Get the size in bytes of a single component of a pixel in the image referenced by this view.
Definition Image.hpp:192
constexpr std::size_t getHeight() const noexcept
Get the height of the image referenced by this view.
Definition Image.hpp:100
constexpr std::size_t getChannelCount() const noexcept
Get the number of component channels in the pixel format of the image referenced by this view.
Definition Image.hpp:170
constexpr std::size_t getSizeInBytes() const noexcept
Get the size in bytes of the image referenced by this view.
Definition Image.hpp:228
constexpr ImageView() noexcept=default
Construct a view that does not reference an image.
constexpr std::size_t getWidth() const noexcept
Get the width of the image referenced by this view.
Definition Image.hpp:87
constexpr const void * getPixels() const noexcept
Get the pixel data referenced by this view.
Definition Image.hpp:156
constexpr PixelComponentType getPixelComponentType() const noexcept
Get the pixel component type of the image referenced by this view.
Definition Image.hpp:124
Container for a 2D image.
Definition Image.hpp:358
PixelComponentType getPixelComponentType() const noexcept
Get the pixel component type of the image.
Definition Image.hpp:613
static void saveHDR(const ImageView &image, Filesystem &filesystem, const char *filepath, const ImageSaveHDROptions &options={})
Save a floating-point 32-bit-per-channel image to a Radiance HDR RGBE file.
std::size_t getWidth() const noexcept
Get the width of the image.
Definition Image.hpp:576
static void saveTGA(const ImageView &image, Filesystem &filesystem, const char *filepath, const ImageSaveTGAOptions &options={})
Save an 8-bit-per-channel image to a Truevision TARGA file.
PixelFormat getPixelFormat() const noexcept
Get the pixel format of the image.
Definition Image.hpp:601
std::size_t getSizeInBytes() const noexcept
Get the size in bytes of this image.
Definition Image.hpp:704
void * getPixels() noexcept
Get the pixel data of this image.
Definition Image.hpp:632
static void savePNG(const ImageView &image, Filesystem &filesystem, const char *filepath, const ImageSavePNGOptions &options={})
Save an 8-bit-per-channel image to a PNG file.
std::size_t getPixelComponentSize() const noexcept
Get the size in bytes of a single component of a pixel in this image.
Definition Image.hpp:676
static void saveBMP(const ImageView &image, Filesystem &filesystem, const char *filepath, const ImageSaveBMPOptions &options={})
Save an 8-bit-per-channel image to a Windows Bitmap file.
const void * getPixels() const noexcept
Get the pixel data of this image.
Definition Image.hpp:651
std::size_t getPixelStride() const noexcept
Get the stride in bytes of the pixels in this image.
Definition Image.hpp:690
static void save(const ImageView &image, Filesystem &filesystem, const char *filepath, const ImageSaveOptions &options={})
Save an image to a file.
Image() noexcept=default
Construct an empty image without a value.
void reset() noexcept
Remove the value from this image and reset it to an empty image.
Definition Image.hpp:563
std::size_t getHeight() const noexcept
Get the height of the image.
Definition Image.hpp:589
std::size_t getChannelCount() const noexcept
Get the number of component channels in the pixel format of this image.
Definition Image.hpp:663
static void saveJPG(const ImageView &image, Filesystem &filesystem, const char *filepath, const ImageSaveJPGOptions &options={})
Save an 8-bit-per-channel image to a JPEG file.
Definition Buffer.hpp:7
PixelComponentType
Description of the data type of the pixel components of an image.
Definition Image.hpp:28
@ F32
Each pixel component is a 32-bit floating-point number.
@ F16
Each pixel component is a 16-bit floating-point number.
@ U8
Each pixel component is an 8-bit unsigned integer.
PixelFormat
Description of the number and meaning of the pixel component channels of an image.
Definition Image.hpp:18
@ RGB
Each pixel comprises 3 components: red, green, blue.
@ RG
Each pixel comprises 2 components: red, green.
@ R
Each pixel comprises 1 component: red.
@ RGBA
Each pixel comprises 4 components: red, green, blue, alpha.
Options for loading an image.
Definition Image.hpp:328
bool flipVertically
Flip the loaded image vertically.
Definition Image.hpp:350
bool highDynamicRange
Load and store the image with high dynamic range.
Definition Image.hpp:345
std::optional< PixelFormat > desiredFormat
If set, request the loaded image to be converted to this format.
Definition Image.hpp:332
Options for saving an image in Windows Bitmap format.
Definition Image.hpp:262
bool flipVertically
Flip the saved image vertically.
Definition Image.hpp:266
Options for saving an image in Radiance HDR RGBE format.
Definition Image.hpp:308
bool flipVertically
Flip the saved image vertically.
Definition Image.hpp:312
Options for saving an image in JPEG format.
Definition Image.hpp:290
bool flipVertically
Flip the saved image vertically.
Definition Image.hpp:302
int quality
JPEG quality.
Definition Image.hpp:297
Options for saving an image in any format.
Definition Image.hpp:318
bool flipVertically
Flip the saved image vertically.
Definition Image.hpp:322
Options for saving an image in PNG format.
Definition Image.hpp:243
bool flipVertically
Flip the saved image vertically.
Definition Image.hpp:256
int compressionLevel
PNG compression level.
Definition Image.hpp:251
Options for saving an image in Truevision TARGA format.
Definition Image.hpp:272
bool useRleCompression
Use run-length encoding to compress the image.
Definition Image.hpp:279
bool flipVertically
Flip the saved image vertically.
Definition Image.hpp:284