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