aboutsummaryrefslogtreecommitdiff
path: root/src/PNGImage.cpp
diff options
context:
space:
mode:
authorDylan <boss@tehbox.org>2025-08-08 15:22:35 +1200
committerDylan <boss@tehbox.org>2025-08-08 15:38:59 +1200
commit41ddbec10d11b01ccc10bf7e1dc862a9f3e4c85f (patch)
treec4775a629588419803320818fc1458f6d35e8ae8 /src/PNGImage.cpp
parent5823a8dff5c6c565c9b253b122d2baeb767b72e2 (diff)
downloadtehimage-41ddbec10d11b01ccc10bf7e1dc862a9f3e4c85f.tar.gz
tehimage-41ddbec10d11b01ccc10bf7e1dc862a9f3e4c85f.zip
feat: Simplified APIv0.0.1
Had to restrict images to 8 bit depth sadly This could potentially be changed in the future by setting bit depth with templates and changing the data to be the specified bit depth
Diffstat (limited to 'src/PNGImage.cpp')
-rw-r--r--src/PNGImage.cpp35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/PNGImage.cpp b/src/PNGImage.cpp
index b6002ca..7df04ad 100644
--- a/src/PNGImage.cpp
+++ b/src/PNGImage.cpp
@@ -78,9 +78,11 @@ bool PNGImage::readNextChunk()
char chunkType[4];
reader->readBytes(chunkType, 4);
std::string chunkName(chunkType, 4);
- cout << "-------------" << endl;
- cout << "|Chunk: " << chunkName << "|" << endl;
- cout << "-------------" << endl;
+ debug(
+ cout << "-------------" << endl;
+ cout << "|Chunk: " << chunkName << "|" << endl;
+ cout << "-------------" << endl;
+ );
if(chunkReaders.count(chunkName) == 0)
{
@@ -117,6 +119,9 @@ DEFINE_CHUNK_READER(IHDR)
if(colorType != 2 && colorType != 6)
throw std::invalid_argument("Only color types 2 and 6 are supported");
+ if(bitDepth != 8)
+ throw std::invalid_argument("Only bit depth of 8 is supported");
+
switch(colorType)
{
case 2: colorValues = 3; break;
@@ -126,12 +131,12 @@ DEFINE_CHUNK_READER(IHDR)
bpp = colorValues * (bitDepth/8);
- unsigned long imageDataSize = width * height * bpp;
- //imageDataSize = imageDataSize;
+ // unsigned long imageDataSize = width * height * bpp;
+ // imageDataSize = imageDataSize;
- cout << "Assigning " << imageDataSize << " bytes for image" << endl;
+ // cout << "Assigning " << imageDataSize << " bytes for image" << endl;
- imageData = std::make_unique<uint8_t[]>(imageDataSize);
+ pixels = std::make_unique<Pixel[]>(width * height);
/*
Scanline<RGBPixel<uint8_t>>* lines = new Scanline<RGBPixel<uint8_t>> [height];
@@ -319,11 +324,12 @@ DEFINE_CHUNK_READER(IEND)
{
unsigned long imageDataSize = height * (width * bpp + 1);
uint8_t* pngImageData = new uint8_t[imageDataSize];
+ uint8_t* rawImage = new uint8_t[width * height * bpp];
cout << "My inflate " << zlib.decodeData(idatData, idatDataSize, pngImageData, imageDataSize) << endl;
end = true;
reader->close();
-#define imageDataIndex(x, y) imageData[y*width*bpp + x]
+#define imageDataIndex(x, y) rawImage[y*width*bpp + x]
#define pngImageDataIndex(x, y) pngImageData[y*(width*bpp + 1) + x + 1]
#define filterByte(y) pngImageDataIndex(-1, y)
@@ -375,6 +381,19 @@ DEFINE_CHUNK_READER(IEND)
#undef imageDataIndex
#undef pngImageDataIndex
#undef filterByte
+
+ for(int y = 0; y < height; y++)
+ {
+ for(int x = 0; x < width; x++)
+ {
+ (*this)[x, y].r = rawImage[(y*width + x)*colorValues];
+ (*this)[x, y].g = rawImage[(y*width + x)*colorValues + 1*bitDepth/8];
+ (*this)[x, y].b = rawImage[(y*width + x)*colorValues + 2*bitDepth/8];
+ (*this)[x, y].a = (colorValues == 4)?rawImage[(y*width + x)*colorValues]:255;
+
+ }
+ }
delete [] pngImageData;
+ delete [] rawImage;
}