From 3e4cad0bfd36536ffc870d16a6e26aac5049dd0b Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Sat, 28 Jun 2025 00:44:40 +1200 Subject: fixup: Updated some stuff Mainly just cleaning up code --- src/image.h | 72 ++++++++++++++++++++++++++----------------------------------- 1 file changed, 31 insertions(+), 41 deletions(-) (limited to 'src/image.h') diff --git a/src/image.h b/src/image.h index 52f4279..be26437 100644 --- a/src/image.h +++ b/src/image.h @@ -2,6 +2,7 @@ #include #include +#include #include template @@ -10,32 +11,24 @@ struct Pixel T r, g, b, a; }; -struct ImageData -{ - - - - - //unsigned long imageDataSize = 0; -}; class Image { protected: - uint8_t* imageData; + std::unique_ptr imageData; + // uint8_t* imageData; uint8_t colorValues; uint8_t bpp; public: Image() = default; - ~Image(); - template T> - Image(const T& other); + // template T> + Image(const Image& other); virtual int readFromFile(std::string filename) = 0; virtual int writeToFile(std::string filename) = 0; - template + template Pixel getPixel(unsigned int x, unsigned int y); uint32_t width = 0; @@ -48,39 +41,36 @@ public: }; - -template T> Image::Image(const T& other) -{ - this->colorValues = other.colorValues; - this->bpp = other.bpp; - - this->width = other.width; - this->height = other.height; - this->bitDepth = other.bitDepth; - this->colorType = other.colorType; - this->compressionMethod = other.compressionMethod; - this->filterMethod = other.filterMethod; - this->interlaceMethod = other.interlaceMethod; - - unsigned long imageDataSize = width * height * bpp; - imageData = new uint8_t[imageDataSize]; - mempcpy(imageData, other.imageData, imageDataSize); -} - - template Pixel Image::getPixel(unsigned int x, unsigned int y) { Pixel pixel; - pixel.r = (T)imageData[y * width * colorValues * bitDepth/8 + x * colorValues * bitDepth/8]; - pixel.g = (T)imageData[y * width * colorValues * bitDepth/8 + x * colorValues * bitDepth/8 + 1]; - pixel.b = (T)imageData[y * width * colorValues * bitDepth/8 + x * colorValues * bitDepth/8 + 2]; - - if(colorValues == 4) - pixel.a = imageData[y * width * colorValues * bitDepth/8 + x * colorValues * bitDepth/8 + 3]; + if(sizeof(T)*8 == bitDepth) + { + unsigned long rIndex = y * width * colorValues + x * colorValues; + pixel.r = ((T*)imageData.get())[rIndex]; + pixel.g = ((T*)imageData.get())[rIndex + 1]; + pixel.b = ((T*)imageData.get())[rIndex + 2]; + + if(colorValues == 4) + pixel.a = ((T*)imageData.get())[rIndex + 3]; + else + pixel.a = 0; + } else - pixel.a = 0; - + { + unsigned long startIndex = y * width * colorValues * bitDepth/8 + x * colorValues * bitDepth/8; + + uint32_t pixelData[4]; + int tsize = sizeof(T); + int bytesToTake = std::min(tsize, bitDepth/8); + for(int i = 0; i < colorValues; i++) + { + for(int j = 0; j < bytesToTake; j++) + pixelData[i] += imageData[startIndex++] << (bitDepth - j*8 - 8); + startIndex += bitDepth/8 - bytesToTake; + } + } return pixel; } -- cgit v1.2.3