MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ImageData.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace MayaFlux::Kakshya {
4
5/**
6 * @enum ImageFormat
7 * @brief User-friendly image format enum
8 *
9 * Abstracts Vulkan formats for API convenience. Portal maps it to
10 * vk::Format; this layer carries no Vulkan types.
11 */
12enum class ImageFormat : uint8_t {
13 // Normalized formats
14 R8, ///< Single channel 8-bit
15 RG8, ///< Two channel 8-bit
16 RGB8, ///< Three channel 8-bit
17 RGBA8, ///< Four channel 8-bit
18 RGBA8_SRGB, ///< Four channel 8-bit sRGB
19
20 BGRA8, ///< 8-bit BGRA unsigned normalized
21 BGRA8_SRGB, ///< 8-bit BGRA sRGB
22
23 // Floating point formats
24 R16F, ///< Single channel 16-bit float
25 RG16F, ///< Two channel 16-bit float
26 RGBA16F, ///< Four channel 16-bit float
27 R32F, ///< Single channel 32-bit float
28 RG32F, ///< Two channel 32-bit float
29 RGBA32F, ///< Four channel 32-bit float
30
31 R16, ///< Single channel 16-bit unsigned integer
32 RG16, ///< Two channel 16-bit unsigned integer
33 RGBA16, ///< Four channel 16-bit unsigned integer
34
35 // Depth/stencil formats
36 DEPTH16, ///< 16-bit depth
37 DEPTH24, ///< 24-bit depth
38 DEPTH32F, ///< 32-bit float depth
39 DEPTH24_STENCIL8 ///< 24-bit depth + 8-bit stencil
40};
41
42/**
43 * @struct ImageData
44 * @brief Raw image data loaded from file or read back from a GPU image.
45 *
46 * Pixel storage is a variant over uint8, uint16, and float, chosen by the
47 * loader based on the source format. 8-bit formats (PNG, JPG, BMP, TGA)
48 * populate the uint8 variant. 16-bit PNG populates the uint16 variant.
49 * Floating-point formats (EXR, HDR) populate the float variant.
50 *
51 * The declared ImageFormat must match the active variant. Accessors below
52 * enforce this; direct member access is permitted but callers are
53 * responsible for consistency.
54 */
55struct MAYAFLUX_API ImageData {
56 using PixelStorage = std::variant<
57 std::vector<uint8_t>,
58 std::vector<uint16_t>,
59 std::vector<float>>;
60
62 uint32_t width { 0 };
63 uint32_t height { 0 };
64 uint32_t channels { 0 };
65 ImageFormat format { ImageFormat::RGBA8 };
66
67 /**
68 * @brief Total byte size of pixel storage, dispatched on variant.
69 */
70 [[nodiscard]] size_t byte_size() const
71 {
72 return std::visit(
73 [](const auto& vec) { return vec.size() * sizeof(typename std::decay_t<decltype(vec)>::value_type); },
74 pixels);
75 }
76
77 /**
78 * @brief Raw data pointer, dispatched on variant. For upload paths.
79 */
80 [[nodiscard]] const void* data() const
81 {
82 return std::visit(
83 [](const auto& vec) -> const void* { return vec.data(); },
84 pixels);
85 }
86
87 /**
88 * @brief Writable raw data pointer, dispatched on variant. For download paths.
89 */
90 [[nodiscard]] void* data()
91 {
92 return std::visit(
93 [](auto& vec) -> void* { return vec.data(); },
94 pixels);
95 }
96
97 /**
98 * @brief Allocate zeroed pixel storage of the variant @p format calls for.
99 *
100 * The variant matches what is_consistent() expects: uint8 for the 8-bit
101 * formats, uint16 for the 16-bit and half-float formats, float for the
102 * 32-bit float formats. Holds width * height * channels elements.
103 *
104 * @return The sized ImageData, or nullopt when any dimension or the
105 * channel count is zero, or the format has no storage mapping.
106 */
107 [[nodiscard]] static std::optional<ImageData> allocate(
108 uint32_t width,
109 uint32_t height,
110 uint32_t channels,
112
113 /**
114 * @brief Build an ImageData from raw pixel bytes in @p format's layout.
115 *
116 * Allocates as allocate() does and copies @p bytes in.
117 *
118 * @return The populated ImageData, or nullopt when allocate() would fail
119 * or @p bytes is not exactly width * height * channels elements.
120 */
121 [[nodiscard]] static std::optional<ImageData> from_bytes(
122 uint32_t width,
123 uint32_t height,
124 uint32_t channels,
126 std::span<const uint8_t> bytes);
127
128 /**
129 * @brief Number of pixel elements (not bytes), dispatched on variant.
130 */
131 [[nodiscard]] size_t element_count() const
132 {
133 return std::visit(
134 [](const auto& vec) { return vec.size(); },
135 pixels);
136 }
137
138 /**
139 * @brief Check that the active variant matches the declared format.
140 *
141 * Callers producing ImageData should invoke this to validate before
142 * handing the data to downstream consumers.
143 */
144 [[nodiscard]] bool is_consistent() const;
145
146 /**
147 * @brief Typed accessors. Return nullptr if variant does not match.
148 */
149 [[nodiscard]] const std::vector<uint8_t>* as_uint8() const { return std::get_if<std::vector<uint8_t>>(&pixels); }
150 [[nodiscard]] const std::vector<uint16_t>* as_uint16() const { return std::get_if<std::vector<uint16_t>>(&pixels); }
151 [[nodiscard]] const std::vector<float>* as_float() const { return std::get_if<std::vector<float>>(&pixels); }
152
153 [[nodiscard]] std::vector<uint8_t>* as_uint8() { return std::get_if<std::vector<uint8_t>>(&pixels); }
154 [[nodiscard]] std::vector<uint16_t>* as_uint16() { return std::get_if<std::vector<uint16_t>>(&pixels); }
155 [[nodiscard]] std::vector<float>* as_float() { return std::get_if<std::vector<float>>(&pixels); }
156};
157
158} // namespace MayaFlux::Kakshya
const std::vector< float > * pixels
Definition Decoder.cpp:48
bool format
uint32_t channels
uint32_t width
uint32_t height
ImageFormat
User-friendly image format enum.
Definition ImageData.hpp:12
@ DEPTH24_STENCIL8
24-bit depth + 8-bit stencil
@ RGB8
Three channel 8-bit.
@ DEPTH32F
32-bit float depth
@ RG16
Two channel 16-bit unsigned integer.
@ BGRA8
8-bit BGRA unsigned normalized
@ RGBA16
Four channel 16-bit unsigned integer.
@ RGBA32F
Four channel 32-bit float.
@ R16F
Single channel 16-bit float.
@ R16
Single channel 16-bit unsigned integer.
@ RGBA16F
Four channel 16-bit float.
@ RGBA8
Four channel 8-bit.
@ RG32F
Two channel 32-bit float.
@ R32F
Single channel 32-bit float.
@ R8
Single channel 8-bit.
@ RG16F
Two channel 16-bit float.
@ RGBA8_SRGB
Four channel 8-bit sRGB.
size_t element_count() const
Number of pixel elements (not bytes), dispatched on variant.
std::vector< uint8_t > * as_uint8()
const std::vector< float > * as_float() const
std::vector< uint16_t > * as_uint16()
const std::vector< uint16_t > * as_uint16() const
const std::vector< uint8_t > * as_uint8() const
Typed accessors.
size_t byte_size() const
Total byte size of pixel storage, dispatched on variant.
Definition ImageData.hpp:70
const void * data() const
Raw data pointer, dispatched on variant.
Definition ImageData.hpp:80
std::vector< float > * as_float()
std::variant< std::vector< uint8_t >, std::vector< uint16_t >, std::vector< float > > PixelStorage
Definition ImageData.hpp:59
void * data()
Writable raw data pointer, dispatched on variant.
Definition ImageData.hpp:90
Raw image data loaded from file or read back from a GPU image.
Definition ImageData.hpp:55