diff options
| author | BossCode45 <boss@tehbox.org> | 2025-06-28 00:44:40 +1200 |
|---|---|---|
| committer | BossCode45 <boss@tehbox.org> | 2025-07-24 12:48:21 +1200 |
| commit | 3e4cad0bfd36536ffc870d16a6e26aac5049dd0b (patch) | |
| tree | 3f0ecd09760267a108f5d97e43d31cfb8741cf55 /src/image.h | |
| parent | 078b4e08fe3bccb7424dac76e158bf8bf48a182d (diff) | |
| download | tehimage-3e4cad0bfd36536ffc870d16a6e26aac5049dd0b.tar.gz tehimage-3e4cad0bfd36536ffc870d16a6e26aac5049dd0b.zip | |
fixup: Updated some stuff
Mainly just cleaning up code
Diffstat (limited to 'src/image.h')
| -rw-r--r-- | src/image.h | 72 |
1 files changed, 31 insertions, 41 deletions
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 <cstdint> #include <cstring> +#include <memory> #include <string> template <typename T> @@ -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<uint8_t[]> imageData; + // uint8_t* imageData; uint8_t colorValues; uint8_t bpp; public: Image() = default; - ~Image(); - template<std::derived_from<Image> T> - Image(const T& other); + // template<std::derived_from<Image> T> + Image(const Image& other); virtual int readFromFile(std::string filename) = 0; virtual int writeToFile(std::string filename) = 0; - template <typename T> + template <typename T = uint8_t> Pixel<T> getPixel(unsigned int x, unsigned int y); uint32_t width = 0; @@ -48,39 +41,36 @@ public: }; - -template<std::derived_from<Image> 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 <typename T> Pixel<T> Image::getPixel(unsigned int x, unsigned int y) { Pixel<T> 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; } |
