6#include <boost/filesystem.hpp>
29 auto const pathToTexture = boost::filesystem::path(filepath);
30 if( boost::filesystem::exists(pathToTexture) && boost::filesystem::is_regular_file(pathToTexture) )
32 boost::filesystem::path
const pathToTexture = boost::filesystem::path(filepath);
34 size_t const dot = pathToTexture.string().find_last_of(
".");
35 std::string
const fileExtension = pathToTexture.string().substr(dot + 1);
37 if( std::string{
"jpg" } == fileExtension )
43 if( std::string{
"png" } == fileExtension )
49 throw std::runtime_error(
"Unsupported texture file format!");
55 throw std::runtime_error(
"Path to texture file is incorrect.");
61 if (fileBytes.size() < 4)
62 throw std::runtime_error(
"Invalid image buffer");
65 if (fileBytes[0] == 0x89 &&
66 fileBytes[1] ==
'P' &&
67 fileBytes[2] ==
'N' &&
70 std::vector<unsigned int> pixelsRGBA;
72 if (!
ReadPngFromMemory(fileBytes.data(), fileBytes.size(), mWidth, mHeight, pixelsRGBA))
73 throw std::runtime_error(
"PNG memory decode failed");
82 if (fileBytes[0] == 0xFF &&
83 fileBytes[1] == 0xD8 &&
86 std::vector<unsigned int> pixelsRGB;
88 if (!
ReadJpegFromMemory(fileBytes.data(), fileBytes.size(), mWidth, mHeight, pixelsRGB))
89 throw std::runtime_error(
"JPEG memory decode failed");
97 throw std::runtime_error(
"Unsupported image format in memory buffer");
120 for(
unsigned int y = 0; y < mHeight; y++ )
122 for(
unsigned int x = 0; x < mWidth; x++ )
124 mData[x][y] = src.mData[x][y];
138 for(
unsigned int y = 0; y < mHeight; y++ )
140 for(
unsigned int x = 0; x < mWidth; x++ )
142 mData[x][y] = rhs.mData[x][y];
151 for(
unsigned int y = 0; y < mHeight; y++ )
153 for(
unsigned int x = 0; x < mWidth; x++ )
162 if( x < mWidth && y < mHeight )
170 if( x < mWidth && y < mHeight )
172 return (mData[x][y]);
176 throw std::runtime_error(
"Inapropriate access to data");
213 for(
int deltaY = 0; deltaY < bottom; deltaY++ )
215 for(
int deltaX = 0; deltaX < left; deltaX++ )
217 toImage.
SetPixel(x + deltaX, y + deltaY, fromImage.
GetPixel(deltaX, deltaY));
224 std::vector<uint8_t> result;
230 for(
unsigned int y = 0; y < mHeight; y++ )
232 for(
unsigned int x = 0; x < mWidth; x++ )
245 std::vector<uint8_t> result;
251 for(
unsigned int y = 0; y < mHeight; y++ )
253 for(
unsigned int x = 0; x < mWidth; x++ )
271 unsigned int width = 0;
272 unsigned int height = 0;
273 std::vector<unsigned int> rgbImageDataVector;
275 bool result =
ReadJpegFile(filename, width, height, rgbImageDataVector);
276 if(result && (rgbImageDataVector.size() == (3U * width * height)))
295 unsigned int width = 0;
296 unsigned int height = 0;
297 std::vector<unsigned int> rgbaImageDataVector;
299 bool result =
ReadPngFile(filename, width, height, rgbaImageDataVector);
301 if( result && (rgbaImageDataVector.size() == (4U * width * height)) )
315 mData =
new Color*[mWidth];
317 for(
unsigned int x = 0; x < mWidth; x++ )
319 mData[x] =
new Color[mHeight];
325 for(
unsigned int x = 0; x < mWidth; x++ )
346 throw std::runtime_error(
"Array data size doesn't match image dimension.");
348 auto it = pixels.begin();
349 for(
unsigned int y = 0; y <
GetHeight(); y++ )
351 for(
unsigned int x = 0; x <
GetWidth(); x++ )
353 uint8_t
const r = *it;
355 uint8_t
const g = *it;
357 uint8_t
const b = *it;
359 uint8_t
const a = *it;
361 Color const color(r, g, b, a);
372 throw std::runtime_error(
"Array data size doesn't match image dimension.");
374 auto it = pixels.begin();
375 for(
unsigned int y = 0; y <
GetHeight(); y++ )
377 for(
unsigned int x = 0; x <
GetWidth(); x++ )
379 uint8_t
const r =
static_cast<uint8_t
>(*it);
381 uint8_t
const g =
static_cast<uint8_t
>(*it);
383 uint8_t
const b =
static_cast<uint8_t
>(*it);
386 Color const color(r, g, b);
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.
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.
JPEG read/write backend using libjpeg.
static bool ReadJpegFile(char const *filename, unsigned int &width, unsigned int &height, std::vector< unsigned int > &rgbImageDataVector)
Reads a JPEG file into an RGB image buffer.
static bool ReadJpegFromMemory(const unsigned char *memory, size_t memorySize, unsigned int &width, unsigned int &height, std::vector< unsigned int > &rgbImageDataVector)
Decode a JPEG image directly from a memory buffer.
static void SaveTextureFileJpeg(rendering_engine::ImageData const &imageData, char const *filename)
Saves image data to a JPEG file.
PNG read/write backend using libpng.
static bool ReadPngFromMemory(const unsigned char *memory, size_t memorySize, unsigned int &width, unsigned int &height, std::vector< unsigned int > &rgbaImageDataVector)
Reads a PNG image from memory into RGBA image data.
static bool ReadPngFile(char const *filename, unsigned int &width, unsigned int &height, std::vector< unsigned int > &rgbaImageDataVector)
Reads a PNG file into RGBA image data.
static void SaveTextureFilePng(rendering_engine::ImageData const &imageData, char const *filename)
Saves image data to a PNG file.
Represents a color with red, green, blue, and alpha channels.