MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ImageData.cpp
Go to the documentation of this file.
1#include "ImageData.hpp"
2
3namespace MayaFlux::Kakshya {
4
5std::optional<ImageData> ImageData::allocate(
6 uint32_t width,
7 uint32_t height,
8 uint32_t channels,
10{
11 using F = ImageFormat;
12
13 if (width == 0 || height == 0 || channels == 0) {
14 return std::nullopt;
15 }
16
17 ImageData result;
18 result.width = width;
19 result.height = height;
20 result.channels = channels;
21 result.format = format;
22
23 const size_t element_count = static_cast<size_t>(width) * height * channels;
24
25 switch (format) {
26 case F::R8:
27 case F::RG8:
28 case F::RGBA8:
29 case F::RGBA8_SRGB:
30 case F::BGRA8:
31 case F::BGRA8_SRGB:
32 result.pixels.emplace<std::vector<uint8_t>>(element_count);
33 break;
34
35 case F::R16:
36 case F::RG16:
37 case F::RGBA16:
38 case F::R16F:
39 case F::RG16F:
40 case F::RGBA16F:
41 result.pixels.emplace<std::vector<uint16_t>>(element_count);
42 break;
43
44 case F::R32F:
45 case F::RG32F:
46 case F::RGBA32F:
47 result.pixels.emplace<std::vector<float>>(element_count);
48 break;
49
50 default:
51 return std::nullopt;
52 }
53
54 return result;
55}
56
57std::optional<ImageData> ImageData::from_bytes(
58 uint32_t width,
59 uint32_t height,
60 uint32_t channels,
62 std::span<const uint8_t> bytes)
63{
64 auto result = allocate(width, height, channels, format);
65 if (!result || result->byte_size() != bytes.size()) {
66 return std::nullopt;
67 }
68
69 std::memcpy(result->data(), bytes.data(), bytes.size());
70 return result;
71}
72
74{
75 using F = ImageFormat;
76
77 const bool has_u8 = std::holds_alternative<std::vector<uint8_t>>(pixels);
78 const bool has_u16 = std::holds_alternative<std::vector<uint16_t>>(pixels);
79 const bool has_f32 = std::holds_alternative<std::vector<float>>(pixels);
80
81 switch (format) {
82 case F::R8:
83 case F::RG8:
84 case F::RGBA8:
85 case F::RGBA8_SRGB:
86 case F::BGRA8:
87 case F::BGRA8_SRGB:
88 return has_u8;
89
90 case F::R16:
91 case F::RG16:
92 case F::RGBA16:
93 return has_u16;
94
95 case F::R16F:
96 case F::RG16F:
97 case F::RGBA16F:
98 return has_u16;
99
100 case F::R32F:
101 case F::RG32F:
102 case F::RGBA32F:
103 return has_f32;
104
105 default:
106 return false;
107 }
108}
109
110} // namespace MayaFlux::Kakshya
bool format
uint32_t channels
uint32_t width
uint32_t height
ImageFormat
User-friendly image format enum.
Definition ImageData.hpp:12
size_t element_count() const
Number of pixel elements (not bytes), dispatched on variant.
static std::optional< ImageData > allocate(uint32_t width, uint32_t height, uint32_t channels, ImageFormat format)
Allocate zeroed pixel storage of the variant format calls for.
Definition ImageData.cpp:5
static std::optional< ImageData > from_bytes(uint32_t width, uint32_t height, uint32_t channels, ImageFormat format, std::span< const uint8_t > bytes)
Build an ImageData from raw pixel bytes in format's layout.
Definition ImageData.cpp:57
bool is_consistent() const
Check that the active variant matches the declared format.
Definition ImageData.cpp:73
Raw image data loaded from file or read back from a GPU image.
Definition ImageData.hpp:55