Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
image_data.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
6#pragma once
7
8#include <cstdint>
9#include <iostream>
10#include <vector>
11#include <algorithm>
12
14
15namespace rendering_engine
16{
17
18/**
19* @struct Color
20* @brief Represents a color with red, green, blue, and alpha channels.
21*/
22struct Color
23{
24 /**
25 * @brief Default constructor. Initializes to black with full opacity.
26 */
28 :
29 Color( uint8_t{0U}, uint8_t{0U}, uint8_t{0U}, uint8_t{255U} )
30 {}
31 /**
32 * @brief Constructs a fully opaque RGB color.
33 * @param iR Red channel [0�255]
34 * @param iG Green channel [0�255]
35 * @param iB Blue channel [0�255]
36 */
37 Color( uint8_t iR, uint8_t iG, uint8_t iB )
38 :
39 Color( iR, iG, iB, uint8_t{255U} )
40 {}
41 /**
42 * @brief Constructs a color with specified RGBA channels.
43 * @param iR Red channel [0�255]
44 * @param iG Green channel [0�255]
45 * @param iB Blue channel [0�255]
46 * @param iA Alpha channel [0�255]
47 */
48 Color( uint8_t iR, uint8_t iG, uint8_t iB, uint8_t iA )
49 :
50 r(std::clamp(iR, uint8_t{0U}, uint8_t{255U})),
51 g(std::clamp(iG, uint8_t{0U}, uint8_t{255U})),
52 b(std::clamp(iB, uint8_t{0U}, uint8_t{255U})),
53 a(std::clamp(iA, uint8_t{0U}, uint8_t{255U}))
54 {}
55
56 /**
57 * @brief Equality operator for comparing two colors.
58 */
59 inline bool operator==(Color const& rhs) const
60 {
61 return (this->r == rhs.r) && (this->g == rhs.g) && (this->b == rhs.b) && (this->a == rhs.a);
62 }
63
64 uint8_t r = 0;
65 uint8_t g = 0;
66 uint8_t b = 0;
67 uint8_t a = 255;
68};
69
70// Color is used for GPU upload and image storage.
71// Must remain tightly packed RGBA8.
72static_assert(sizeof(Color) == 4, "Color must be exactly 4 bytes");
73static_assert(std::is_trivially_copyable_v<Color>, "Color must be trivially copyable");
74
75/**
76 * @class ImageData
77 * @brief Represents raw 2D image data stored in memory.
78 */
80{
81public:
82 /**
83 * @brief Constructs an empty image.
84 */
85 ImageData();
86
87 /**
88 * @brief Constructs a blank image with specified dimensions.
89 * @param width Image width in pixels
90 * @param height Image height in pixels
91 */
92 ImageData(unsigned int width, unsigned int height);
93
94 /**
95 * @brief Loads image data from a file.
96 * @param filepath Path to the image file (JPG or PNG)
97 */
98 ImageData(std::string filepath);
99
100 /**
101 * @brief Constructs an image by decoding raw file bytes.
102 *
103 * This constructor allows loading an image from a memory buffer rather than a file
104 * on disk. The data is interpreted as either PNG or JPEG depending on format
105 * detection. Decoded pixel data is stored internally in 32-bit RGBA format.
106 *
107 * @param fileBytes Raw contents of an image file (PNG or JPEG).
108 */
109 ImageData(std::vector<uint8_t> const& fileBytes);
110
111 /**
112 * @brief Constructs image from raw RGBA pixel buffer.
113 * @param width Image width
114 * @param height Image height
115 * @param pixelsRGBA Vector of RGBA pixel data (4 bytes per pixel)
116 */
117 ImageData(unsigned int width, unsigned int height, std::vector<std::uint8_t> const& pixelsRGBA);
118
119 /**
120 * @brief Destructor that frees memory.
121 */
122 ~ImageData();
123
124 /**
125 * @brief Copy constructor.
126 */
127 ImageData(const ImageData& src);
128
129 /**
130 * @brief Copy assignment operator.
131 */
132 ImageData& operator=(const ImageData& rhs);
133
134 /**
135 * @brief Returns the image width.
136 */
137 inline unsigned int GetWidth() const
138 {
139 return mWidth;
140 }
141
142 /**
143 * @brief Returns the image height.
144 */
145 inline unsigned int GetHeight() const
146 {
147 return mHeight;
148 }
149
150 /**
151 * @brief Fills the image with a solid color.
152 * @param color The color to fill with
153 */
154 void Fill( Color color );
155
156 /**
157 * @brief Sets the color of a specific pixel.
158 * @param x X coordinate
159 * @param y Y coordinate
160 * @param color The color to set
161 */
162 void SetPixel( unsigned int x, unsigned int y, Color color );
163
164 /**
165 * @brief Retrieves the color of a specific pixel.
166 * @param x X coordinate
167 * @param y Y coordinate
168 * @return The color of the pixel
169 */
170 const Color GetPixel( unsigned int x, unsigned int y ) const;
171
172 /**
173 * @brief Overlays one image on top of another at a given position.
174 * @param x Top-left X coordinate for the overlay
175 * @param y Top-left Y coordinate for the overlay
176 * @param toImage Destination image
177 * @param fromImage Source image
178 */
179 static void DrawImageOnImageAtPos(unsigned int const x, unsigned int const y, ImageData& toImage, ImageData& fromImage);
180
181 /**
182 * @brief Gets raw image data in RGBA format.
183 * @return A vector containing 4 bytes per pixel (RGBA)
184 */
185 std::vector<uint8_t> GetImageDataRGBA() const;
186
187 /**
188 * @brief Gets raw image data in RGB format.
189 * @return A vector containing 3 bytes per pixel (RGB)
190 */
191 std::vector<uint8_t> GetImageDataRGB() const;
192
193 /**
194 * @brief Gets the total size of the image in memory.
195 * @return Size in bytes
196 */
197 size_t GetSizeInBytes() const
198 {
199 return static_cast<size_t>(mWidth) * static_cast<size_t>(mHeight) * sizeof(Color);
200 }
201
202 /**
203 * @brief Writes the image data to a JPEG file.
204 * @param filename Path to output file
205 */
206 void WriteJpegFile(char const* filename);
207
208
209 /**
210 * @brief Writes the image data to a PNG file.
211 * @param filename Path to output file
212 */
213 void WritePngFile(char const* filename);
214
215protected:
216 /**
217 * @brief Allocates memory for internal pixel storage.
218 * @param width Image width
219 * @param height Image height
220 */
221 void AllocateMemory( unsigned int width, unsigned int height );
222
223 /**
224 * @brief Frees allocated memory.
225 */
226 void CleanAllocatedMemory();
227 /**
228 * @brief Loads image data from a 32-bit RGBA buffer.
229 */
230 void LoadImageDataRGBA(std::vector<std::uint8_t> const& pixels);
231 /**
232 * @brief Loads image data from a 24-bit RGB buffer.
233 */
234 void LoadImageDataRGB(std::vector<std::uint8_t> const& pixels);
235
236 /**
237 * @brief Loads image from a PNG file.
238 */
239 bool LoadTexturePngFile(char const* filename);
240
241 /**
242 * @brief Loads image from a JPEG file.
243 */
244 bool LoadTextureJpegFile(char const* filename);
245
246 inline size_t GetLinearIndex(unsigned int x, unsigned int y) const
247 {
248 return static_cast<size_t>(y) * static_cast<size_t>(mWidth) + static_cast<size_t>(x);
249 }
250
251 inline Color& PixelRef(unsigned int x, unsigned int y)
252 {
253 return mData[GetLinearIndex(x, y)];
254 }
255
256 inline const Color& PixelRef(unsigned int x, unsigned int y) const
257 {
258 return mData[GetLinearIndex(x, y)];
259 }
260
261private:
262 unsigned int mWidth;
263 unsigned int mHeight;
264
265 std::vector<Color> mData;
266};
267
268} //namespace rendering_engine
Represents raw 2D image data stored in memory.
Definition: image_data.hpp:80
const Color & PixelRef(unsigned int x, unsigned int y) const
Definition: image_data.hpp:256
size_t GetLinearIndex(unsigned int x, unsigned int y) const
Definition: image_data.hpp:246
unsigned int GetWidth() const
Returns the image width.
Definition: image_data.hpp:137
unsigned int GetHeight() const
Returns the image height.
Definition: image_data.hpp:145
Color & PixelRef(unsigned int x, unsigned int y)
Definition: image_data.hpp:251
size_t GetSizeInBytes() const
Gets the total size of the image in memory.
Definition: image_data.hpp:197
#define RE_API
Represents a color with red, green, blue, and alpha channels.
Definition: image_data.hpp:23
Color()
Default constructor. Initializes to black with full opacity.
Definition: image_data.hpp:27
Color(uint8_t iR, uint8_t iG, uint8_t iB)
Constructs a fully opaque RGB color.
Definition: image_data.hpp:37
Color(uint8_t iR, uint8_t iG, uint8_t iB, uint8_t iA)
Constructs a color with specified RGBA channels.
Definition: image_data.hpp:48
bool operator==(Color const &rhs) const
Equality operator for comparing two colors.
Definition: image_data.hpp:59