Rendering Engine 0.2.17
Modular Graphics Rendering Engine | v0.2.17
Loading...
Searching...
No Matches
rendering_engine::ImageData Class Reference

Represents raw 2D image data stored in memory. More...

#include <image_data.hpp>

Public Member Functions

 ImageData ()
 Constructs an empty image.
 
 ImageData (unsigned int width, unsigned int height)
 Constructs a blank image with specified dimensions.
 
 ImageData (std::string filepath)
 Loads image data from a file.
 
 ImageData (std::vector< uint8_t > const &fileBytes)
 Constructs an image by decoding raw file bytes.
 
 ImageData (unsigned int width, unsigned int height, std::vector< std::uint8_t > const &pixelsRGBA)
 Constructs image from raw RGBA pixel buffer.
 
 ~ImageData ()
 Destructor that frees memory.
 
 ImageData (const ImageData &src)
 Copy constructor.
 
ImageData & operator= (const ImageData &rhs)
 Copy assignment operator.
 
unsigned int GetWidth () const
 Returns the image width.
 
unsigned int GetHeight () const
 Returns the image height.
 
void Fill (Color color)
 Fills the image with a solid color.
 
void SetPixel (unsigned int x, unsigned int y, Color color)
 Sets the color of a specific pixel.
 
const Color GetPixel (unsigned int x, unsigned int y) const
 Retrieves the color of a specific pixel.
 
std::vector< uint8_t > GetImageDataRGBA () const
 Gets raw image data in RGBA format.
 
std::vector< uint8_t > GetImageDataRGB () const
 Gets raw image data in RGB format.
 
size_t GetSizeInBytes () const
 Gets the total size of the image in memory.
 
void WriteJpegFile (char const *filename)
 Writes the image data to a JPEG file.
 
void WritePngFile (char const *filename)
 Writes the image data to a PNG file.
 

Static Public Member Functions

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.
 

Protected Member Functions

void AllocateMemory (unsigned int width, unsigned int height)
 Allocates memory for internal pixel storage.
 
void CleanAllocatedMemory ()
 Frees allocated memory.
 
void LoadImageDataRGBA (std::vector< std::uint8_t > const &pixels)
 Loads image data from a 32-bit RGBA buffer.
 
void LoadImageDataRGB (std::vector< std::uint8_t > const &pixels)
 Loads image data from a 24-bit RGB buffer.
 
bool LoadTexturePngFile (char const *filename)
 Loads image from a PNG file.
 
bool LoadTextureJpegFile (char const *filename)
 Loads image from a JPEG file.
 
size_t GetLinearIndex (unsigned int x, unsigned int y) const
 
Color & PixelRef (unsigned int x, unsigned int y)
 
const Color & PixelRef (unsigned int x, unsigned int y) const
 

Detailed Description

Represents raw 2D image data stored in memory.

Definition at line 79 of file image_data.hpp.

Constructor & Destructor Documentation

◆ ImageData() [1/6]

rendering_engine::ImageData::ImageData ( )

Constructs an empty image.

Definition at line 12 of file image_data.cpp.

13 :
15{
16}
ImageData()
Constructs an empty image.

◆ ImageData() [2/6]

rendering_engine::ImageData::ImageData ( unsigned int  width,
unsigned int  height 
)

Constructs a blank image with specified dimensions.

Parameters
widthImage width in pixels
heightImage height in pixels

Definition at line 18 of file image_data.cpp.

19 :
20 mWidth( width ),
21 mHeight( height )
22{
23 AllocateMemory( mWidth, mHeight );
24}
void AllocateMemory(unsigned int width, unsigned int height)
Allocates memory for internal pixel storage.

◆ ImageData() [3/6]

rendering_engine::ImageData::ImageData ( std::string  filepath)

Loads image data from a file.

Parameters
filepathPath to the image file (JPG or PNG)

Definition at line 26 of file image_data.cpp.

27 :
29{
30 auto const pathToTexture = std::filesystem::path(filepath);
31 if(std::filesystem::exists(pathToTexture) && std::filesystem::is_regular_file(pathToTexture) )
32 {
33 size_t const dot = pathToTexture.string().find_last_of(".");
34 std::string const fileExtension = pathToTexture.string().substr(dot + 1);
35
36 if( std::string{ "jpg" } == fileExtension )
37 {
38 LoadTextureJpegFile(pathToTexture.string().c_str());
39 }
40 else
41 {
42 if( std::string{ "png" } == fileExtension )
43 {
44 LoadTexturePngFile(pathToTexture.string().c_str());
45 }
46 else
47 {
48 throw std::runtime_error("Unsupported texture file format!");
49 }
50 }
51 }
52 else
53 {
54 throw std::runtime_error("Path to texture file is incorrect.");
55 }
56}
bool LoadTextureJpegFile(char const *filename)
Loads image from a JPEG file.
bool LoadTexturePngFile(char const *filename)
Loads image from a PNG file.

◆ ImageData() [4/6]

rendering_engine::ImageData::ImageData ( std::vector< uint8_t > const &  fileBytes)

Constructs an image by decoding raw file bytes.

This constructor allows loading an image from a memory buffer rather than a file on disk. The data is interpreted as either PNG or JPEG depending on format detection. Decoded pixel data is stored internally in 32-bit RGBA format.

Parameters
fileBytesRaw contents of an image file (PNG or JPEG).

Definition at line 58 of file image_data.cpp.

59{
60 if (fileBytes.size() < 4)
61 throw std::runtime_error("Invalid image buffer");
62
63 // PNG signature
64 if (fileBytes[0] == 0x89 &&
65 fileBytes[1] == 'P' &&
66 fileBytes[2] == 'N' &&
67 fileBytes[3] == 'G')
68 {
69 std::vector<std::uint8_t> pixelsRGBA;
70
71 if (!ReadPngFromMemory(fileBytes.data(), fileBytes.size(), mWidth, mHeight, pixelsRGBA))
72 throw std::runtime_error("PNG memory decode failed");
73
74 AllocateMemory(mWidth, mHeight);
75 Fill(Color(0, 0, 0, 255));
76 LoadImageDataRGBA(pixelsRGBA);
77 return;
78 }
79
80 // JPEG signature: FF D8 FF
81 if (fileBytes[0] == 0xFF &&
82 fileBytes[1] == 0xD8 &&
83 fileBytes[2] == 0xFF)
84 {
85 std::vector<std::uint8_t> pixelsRGB;
86
87 if (!ReadJpegFromMemory(fileBytes.data(), fileBytes.size(), mWidth, mHeight, pixelsRGB))
88 throw std::runtime_error("JPEG memory decode failed");
89
90 AllocateMemory(mWidth, mHeight);
91 LoadImageDataRGB(pixelsRGB);
92
93 return;
94 }
95
96 throw std::runtime_error("Unsupported image format in memory buffer");
97}
void LoadImageDataRGBA(std::vector< std::uint8_t > const &pixels)
Loads image data from a 32-bit RGBA buffer.
void Fill(Color color)
Fills the image with a solid color.
void LoadImageDataRGB(std::vector< std::uint8_t > const &pixels)
Loads image data from a 24-bit RGB buffer.
static bool ReadJpegFromMemory(const unsigned char *memory, size_t memorySize, unsigned int &width, unsigned int &height, std::vector< std::uint8_t > &rgbImageDataVector)
Decode a JPEG image directly from a memory buffer.
static bool ReadPngFromMemory(const unsigned char *memory, size_t memorySize, unsigned int &width, unsigned int &height, std::vector< std::uint8_t > &rgbaImageDataVector)
Reads a PNG image from memory into RGBA image data.

◆ ImageData() [5/6]

rendering_engine::ImageData::ImageData ( unsigned int  width,
unsigned int  height,
std::vector< std::uint8_t > const &  pixelsRGBA 
)

Constructs image from raw RGBA pixel buffer.

Parameters
widthImage width
heightImage height
pixelsRGBAVector of RGBA pixel data (4 bytes per pixel)

Definition at line 99 of file image_data.cpp.

100 :
101ImageData::ImageData(width, height)
102{
103 Fill(Color(255, 255, 255, 255));
104 LoadImageDataRGBA( pixelsRGBA );
105}

◆ ~ImageData()

rendering_engine::ImageData::~ImageData ( )

Destructor that frees memory.

Definition at line 107 of file image_data.cpp.

108{
110}
void CleanAllocatedMemory()
Frees allocated memory.

◆ ImageData() [6/6]

rendering_engine::ImageData::ImageData ( const ImageData &  src)

Copy constructor.

Definition at line 112 of file image_data.cpp.

113 :
114 mWidth(src.mWidth),
115 mHeight(src.mHeight),
116 mData(src.mData)
117{
118}

Member Function Documentation

◆ AllocateMemory()

void rendering_engine::ImageData::AllocateMemory ( unsigned int  width,
unsigned int  height 
)
protected

Allocates memory for internal pixel storage.

Parameters
widthImage width
heightImage height

Definition at line 311 of file image_data.cpp.

312{
313 mWidth = width;
314 mHeight = height;
315 mData.resize(static_cast<size_t>(mWidth) * static_cast<size_t>(mHeight));
316}

◆ CleanAllocatedMemory()

void rendering_engine::ImageData::CleanAllocatedMemory ( )
protected

Frees allocated memory.

Definition at line 318 of file image_data.cpp.

319{
320 mData.clear();
321
322 mWidth = 0;
323 mHeight = 0;
324}

◆ DrawImageOnImageAtPos()

void rendering_engine::ImageData::DrawImageOnImageAtPos ( unsigned int const  x,
unsigned int const  y,
ImageData &  toImage,
ImageData &  fromImage 
)
static

Overlays one image on top of another at a given position.

Parameters
xTop-left X coordinate for the overlay
yTop-left Y coordinate for the overlay
toImageDestination image
fromImageSource image

Definition at line 157 of file image_data.cpp.

158{
159 // If fromImage can not be placed entirely, it will be cropped on right and bottom side
160
161 // Source image draw from top left pixel
162
163 // At first check if point pos is inside toImage
164 if (x >= toImage.GetWidth() || y >= toImage.GetHeight())
165 {
166 return;
167 }
168
169 if (toImage.GetWidth() == 0U || toImage.GetHeight() == 0U ||
170 fromImage.GetWidth() == 0U || fromImage.GetHeight() == 0U)
171 {
172 return;
173 }
174
175 // Crop
176 unsigned int left;
177 if ((toImage.GetWidth() - x) < fromImage.GetWidth())
178 {
179 left = toImage.GetWidth() - x;
180 }
181 else
182 {
183 left = fromImage.GetWidth();
184 }
185
186 unsigned int bottom;
187 if ((toImage.GetHeight() - y) < fromImage.GetHeight())
188 {
189 bottom = toImage.GetHeight() - y;
190 }
191 else
192 {
193 bottom = fromImage.GetHeight();
194 }
195
196 if (left == 0U || bottom == 0U)
197 {
198 return;
199 }
200
201 for (unsigned int deltaY = 0; deltaY < bottom; ++deltaY)
202 {
203 const size_t srcRowOffset =
204 static_cast<size_t>(deltaY) * static_cast<size_t>(fromImage.GetWidth());
205
206 const size_t dstRowOffset =
207 static_cast<size_t>(y + deltaY) * static_cast<size_t>(toImage.GetWidth()) +
208 static_cast<size_t>(x);
209
210 std::memcpy(
211 toImage.mData.data() + dstRowOffset,
212 fromImage.mData.data() + srcRowOffset,
213 static_cast<size_t>(left) * sizeof(Color));
214 }
215}

◆ Fill()

void rendering_engine::ImageData::Fill ( Color  color)

Fills the image with a solid color.

Parameters
colorThe color to fill with

Definition at line 133 of file image_data.cpp.

134{
135 std::fill(mData.begin(), mData.end(), color);
136}

◆ GetHeight()

unsigned int rendering_engine::ImageData::GetHeight ( ) const
inline

Returns the image height.

Definition at line 145 of file image_data.hpp.

146 {
147 return mHeight;
148 }

◆ GetImageDataRGB()

std::vector< uint8_t > rendering_engine::ImageData::GetImageDataRGB ( ) const

Gets raw image data in RGB format.

Returns
A vector containing 3 bytes per pixel (RGB)

Definition at line 233 of file image_data.cpp.

234{
235 if (mWidth == 0 || mHeight == 0)
236 {
237 return {};
238 }
239
240 const size_t pixelCount =
241 static_cast<size_t>(mWidth) * static_cast<size_t>(mHeight);
242
243 std::vector<uint8_t> result(pixelCount * 3);
244
245 const Color* src = mData.data();
246 uint8_t* dst = result.data();
247
248 for (size_t i = 0; i < pixelCount; ++i)
249 {
250 dst[i * 3 + 0] = src[i].r;
251 dst[i * 3 + 1] = src[i].g;
252 dst[i * 3 + 2] = src[i].b;
253 }
254
255 return result;
256}

◆ GetImageDataRGBA()

std::vector< uint8_t > rendering_engine::ImageData::GetImageDataRGBA ( ) const

Gets raw image data in RGBA format.

Returns
A vector containing 4 bytes per pixel (RGBA)

Definition at line 217 of file image_data.cpp.

218{
219 if (mWidth == 0 || mHeight == 0)
220 {
221 return {};
222 }
223
224 const size_t totalBytes =
225 static_cast<size_t>(mWidth) * static_cast<size_t>(mHeight) * 4;
226
227 std::vector<uint8_t> result(totalBytes);
228 std::memcpy(result.data(), mData.data(), totalBytes);
229
230 return result;
231}

◆ GetLinearIndex()

size_t rendering_engine::ImageData::GetLinearIndex ( unsigned int  x,
unsigned int  y 
) const
inlineprotected

Definition at line 246 of file image_data.hpp.

247 {
248 return static_cast<size_t>(y) * static_cast<size_t>(mWidth) + static_cast<size_t>(x);
249 }

◆ GetPixel()

const Color rendering_engine::ImageData::GetPixel ( unsigned int  x,
unsigned int  y 
) const

Retrieves the color of a specific pixel.

Parameters
xX coordinate
yY coordinate
Returns
The color of the pixel

Definition at line 146 of file image_data.cpp.

147{
148 if (x >= mWidth || y >= mHeight)
149 {
150 LOG_ERROR("Out of bounds access in GetPixel");
151 return Color{};
152 }
153
154 return PixelRef(x, y);
155}
Color & PixelRef(unsigned int x, unsigned int y)
#define LOG_ERROR(msg)
Definition logger.hpp:41

◆ GetSizeInBytes()

size_t rendering_engine::ImageData::GetSizeInBytes ( ) const
inline

Gets the total size of the image in memory.

Returns
Size in bytes

Definition at line 197 of file image_data.hpp.

198 {
199 return static_cast<size_t>(mWidth) * static_cast<size_t>(mHeight) * sizeof(Color);
200 }

◆ GetWidth()

unsigned int rendering_engine::ImageData::GetWidth ( ) const
inline

Returns the image width.

Definition at line 137 of file image_data.hpp.

138 {
139 return mWidth;
140 }

◆ LoadImageDataRGB()

void rendering_engine::ImageData::LoadImageDataRGB ( std::vector< std::uint8_t > const &  pixels)
protected

Loads image data from a 24-bit RGB buffer.

Definition at line 342 of file image_data.cpp.

343{
344 const size_t pixelCount =
345 static_cast<size_t>(mWidth) * static_cast<size_t>(mHeight);
346
347 const size_t expectedSize = pixelCount * 3;
348 if (pixels.size() != expectedSize)
349 {
350 throw std::runtime_error("Array data size doesn't match image dimension.");
351 }
352
353 mData.resize(pixelCount);
354
355 const uint8_t* src = pixels.data();
356 Color* dst = mData.data();
357
358 for (size_t i = 0; i < pixelCount; ++i)
359 {
360 dst[i].r = src[i * 3 + 0];
361 dst[i].g = src[i * 3 + 1];
362 dst[i].b = src[i * 3 + 2];
363 dst[i].a = 255;
364 }
365}

◆ LoadImageDataRGBA()

void rendering_engine::ImageData::LoadImageDataRGBA ( std::vector< std::uint8_t > const &  pixels)
protected

Loads image data from a 32-bit RGBA buffer.

Definition at line 326 of file image_data.cpp.

327{
328 const size_t pixelCount = static_cast<size_t>(mWidth) * static_cast<size_t>(mHeight);
329
330 const size_t expectedSize = pixelCount * 4U;
331
332 if (pixels.size() != expectedSize)
333 {
334 throw std::runtime_error("Array data size doesn't match image dimension.");
335 }
336
337 mData.resize(pixelCount);
338
339 std::memcpy(mData.data(), pixels.data(), expectedSize);
340}

◆ LoadTextureJpegFile()

bool rendering_engine::ImageData::LoadTextureJpegFile ( char const *  filename)
protected

Loads image from a JPEG file.

Definition at line 263 of file image_data.cpp.

264{
266 unsigned int width = 0;
267 unsigned int height = 0;
268 std::vector<std::uint8_t> rgbImageDataVector;
269
270 bool result = ReadJpegFile(filename, width, height, rgbImageDataVector);
271 const size_t expectedSize =
272 static_cast<size_t>(width) * static_cast<size_t>(height) * 3U;
273
274 if (result && rgbImageDataVector.size() == expectedSize)
275 {
276 mWidth = width;
277 mHeight = height;
278 AllocateMemory(width, height);
279 LoadImageDataRGB(rgbImageDataVector);
280 }
281
282 return result;
283}
static bool ReadJpegFile(char const *filename, unsigned int &width, unsigned int &height, std::vector< std::uint8_t > &rgbImageDataVector)
Reads a JPEG file into an RGB image buffer.

◆ LoadTexturePngFile()

bool rendering_engine::ImageData::LoadTexturePngFile ( char const *  filename)
protected

Loads image from a PNG file.

Definition at line 290 of file image_data.cpp.

291{
293 unsigned int width = 0;
294 unsigned int height = 0;
295 std::vector<std::uint8_t> rgbaImageDataVector;
296
297 bool result = ReadPngFile(filename, width, height, rgbaImageDataVector);
298
299 const size_t expectedSize = static_cast<size_t>(width) * static_cast<size_t>(height) * 4U;
300
301 if (result && rgbaImageDataVector.size() == expectedSize)
302 {
303 mWidth = width;
304 mHeight = height;
305 AllocateMemory(width, height);
306 LoadImageDataRGBA(rgbaImageDataVector);
307 }
308 return true;
309}
static bool ReadPngFile(char const *filename, unsigned int &width, unsigned int &height, std::vector< std::uint8_t > &rgbaImageDataVector)
Reads a PNG file into RGBA image data.

◆ operator=()

ImageData & rendering_engine::ImageData::operator= ( const ImageData &  rhs)

Copy assignment operator.

Definition at line 120 of file image_data.cpp.

121{
122 if (this == &rhs)
123 {
124 return *this;
125 }
126
127 mWidth = rhs.mWidth;
128 mHeight = rhs.mHeight;
129 mData = rhs.mData;
130 return *this;
131}

◆ PixelRef() [1/2]

Color & rendering_engine::ImageData::PixelRef ( unsigned int  x,
unsigned int  y 
)
inlineprotected

Definition at line 251 of file image_data.hpp.

252 {
253 return mData[GetLinearIndex(x, y)];
254 }
size_t GetLinearIndex(unsigned int x, unsigned int y) const

◆ PixelRef() [2/2]

const Color & rendering_engine::ImageData::PixelRef ( unsigned int  x,
unsigned int  y 
) const
inlineprotected

Definition at line 256 of file image_data.hpp.

257 {
258 return mData[GetLinearIndex(x, y)];
259 }

◆ SetPixel()

void rendering_engine::ImageData::SetPixel ( unsigned int  x,
unsigned int  y,
Color  color 
)

Sets the color of a specific pixel.

Parameters
xX coordinate
yY coordinate
colorThe color to set

Definition at line 138 of file image_data.cpp.

139{
140 if (x < mWidth && y < mHeight)
141 {
142 PixelRef(x, y) = color;
143 }
144}

◆ WriteJpegFile()

void rendering_engine::ImageData::WriteJpegFile ( char const *  filename)

Writes the image data to a JPEG file.

Parameters
filenamePath to output file

Definition at line 258 of file image_data.cpp.

259{
260 SaveTextureFileJpeg(*this, filename);
261}
static void SaveTextureFileJpeg(rendering_engine::ImageData const &imageData, char const *filename)
Saves image data to a JPEG file.

◆ WritePngFile()

void rendering_engine::ImageData::WritePngFile ( char const *  filename)

Writes the image data to a PNG file.

Parameters
filenamePath to output file

Definition at line 285 of file image_data.cpp.

286{
287 SaveTextureFilePng(*this, filename);
288}
static void SaveTextureFilePng(rendering_engine::ImageData const &imageData, char const *filename)
Saves image data to a PNG file.

The documentation for this class was generated from the following files: