From 7711536253315ba3b3628b8c6706573317c7e201 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sun, 24 May 2026 22:59:09 +1200 Subject: feat: Cleanups Cleaning up code so its ready for release --- src/HashTable.cpp | 10 +++++----- src/HashTable.h | 2 +- src/PNGImage.cpp | 40 ++++++++++++++++++++-------------------- src/zlib.cpp | 4 ++-- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/HashTable.cpp b/src/HashTable.cpp index 685dad6..e7da244 100644 --- a/src/HashTable.cpp +++ b/src/HashTable.cpp @@ -15,22 +15,22 @@ namespace TehImage return hash % TABLE_SIZE; } - void HashTable::insert(char key[3], unsigned int value) + void HashTable::insertOrUpdate(char key[3], unsigned int value) { unsigned int index = hashFunction(key); - unsigned int keyMem = 0; - std::memcpy(&keyMem, key, 3); + unsigned int keyValue = 0; + std::memcpy(&keyValue, key, 3); for(auto& pair : table[index]) { - if(pair.first == keyMem) + if(pair.first == keyValue) { pair.second = value; return; } } - table[index].push_back({keyMem, value}); + table[index].push_back({keyValue, value}); } void HashTable::remove(char key[3]) diff --git a/src/HashTable.h b/src/HashTable.h index 1ede7be..b1bf88a 100644 --- a/src/HashTable.h +++ b/src/HashTable.h @@ -18,7 +18,7 @@ namespace TehImage HashTable(size_t size); unsigned int hashFunction(char key[3]); - void insert(char key[3], unsigned int value); + void insertOrUpdate(char key[3], unsigned int value); void remove(char key[3]); unsigned int get(char key[3]); bool contains(char key[3]); diff --git a/src/PNGImage.cpp b/src/PNGImage.cpp index 22a6218..3c6205d 100644 --- a/src/PNGImage.cpp +++ b/src/PNGImage.cpp @@ -95,13 +95,13 @@ namespace TehImage reader->readBytes(chunkType, 4); chunkType[4] = 0; std::string chunkName(chunkType, 4); - cout << "-------------" << endl; - cout << "|Chunk: " << chunkName << "|" << endl; - cout << "-------------" << endl; + // cout << "-------------" << endl; + // cout << "|Chunk: " << chunkName << "|" << endl; + // cout << "-------------" << endl; if(chunkReaders.count(chunkName) == 0) { - cout << "Chunk reader not found!!! " << chunkName << endl; + // cout << "Chunk reader not found!!! " << chunkName << endl; // for(int i = 0; i < 4; i++) // cout << 0+chunkType[i] << ' '; // cout << endl; @@ -109,10 +109,10 @@ namespace TehImage reader->skipBytes(chunkSize + 4); if(islower(chunkType[0])) { - cout << "\tAble to skip chunk" << endl; + // cout << "\tAble to skip chunk" << endl; return true; } - cout << "\tFatal error" << endl; + // cout << "\tFatal error" << endl; return false; } @@ -133,7 +133,7 @@ namespace TehImage compressionMethod = reader->readData(); filterMethod = reader->readData(); interlaceMethod = reader->readData(); - cout << "Width: " << width << ", Height: " << height << ", Bit depth: " << 0+bitDepth << ", Color type: " << 0+colorType << ", Compression method: " << 0+compressionMethod << ", Filter method: " << 0+filterMethod << ", Interlace method: " << 0+interlaceMethod << endl; + // cout << "Width: " << width << ", Height: " << height << ", Bit depth: " << 0+bitDepth << ", Color type: " << 0+colorType << ", Compression method: " << 0+compressionMethod << ", Filter method: " << 0+filterMethod << ", Interlace method: " << 0+interlaceMethod << endl; if(colorType != 2 && colorType != 6) throw std::invalid_argument("Only color types 2 and 6 are supported"); @@ -172,7 +172,7 @@ namespace TehImage // cout << profileName << endl; uint8_t compresssionMethod = reader->readByte(); chunkSize--; - cout << 0+compresssionMethod << endl; + // cout << 0+compresssionMethod << endl; uint8_t CMF = reader->readByte(); uint8_t CM = CMF & 0b00001111; uint8_t CINFO = (CMF & 0b11110000) >> 4; @@ -193,7 +193,7 @@ namespace TehImage ZLib inflator; inflator.decodeData((uint8_t*)compressedData, chunkSize - 4, (uint8_t*)outData, 1024); - cout << "iCCP not supported" << endl; + // cout << "iCCP not supported" << endl; uint32_t checkValue = reader->readData(); @@ -203,7 +203,7 @@ namespace TehImage DEFINE_CHUNK_READER(sRGB) { renderingIntent = reader->readData(); - cout << "Rendering intent: " << 0+renderingIntent << endl; + // cout << "Rendering intent: " << 0+renderingIntent << endl; } DEFINE_CHUNK_READER(eXIf) @@ -231,7 +231,7 @@ namespace TehImage DEFINE_CHUNK_READER(iDOT) { - cout << "!!! Ignoring iDOT !!!" << endl; + // cout << "!!! Ignoring iDOT !!!" << endl; reader->skipBytes(chunkSize); } @@ -240,7 +240,7 @@ namespace TehImage pixelsPerX = reader->readData(); pixelsPerY = reader->readData(); unit = reader->readData(); - cout << "Pixels per unit (x): " << pixelsPerX << ", Pixels per unit (y): " << pixelsPerY << ", unit: " << 0+unit << endl; + // cout << "Pixels per unit (x): " << pixelsPerX << ", Pixels per unit (y): " << pixelsPerY << ", unit: " << 0+unit << endl; } DEFINE_CHUNK_READER(tIME) @@ -251,7 +251,7 @@ namespace TehImage hour = reader->readData(); minute = reader->readData(); second = reader->readData(); - cout << "Image last modified: " << 0+hour << ":" << 0+minute << ":" << 0+second << " " << 0+day << "-" << 0+month << "-" << 0+year << endl; + // cout << "Image last modified: " << 0+hour << ":" << 0+minute << ":" << 0+second << " " << 0+day << "-" << 0+month << "-" << 0+year << endl; } DEFINE_CHUNK_READER(tEXt) @@ -266,7 +266,7 @@ namespace TehImage c = reader->readByte(); chunkSize--; } - cout << keyword << endl; + // cout << keyword << endl; std::string textString; c = reader->readByte(); chunkSize--; @@ -277,7 +277,7 @@ namespace TehImage chunkSize--; } textString.push_back(c); - cout << textString << endl; + // cout << textString << endl; } DEFINE_CHUNK_READER(IDAT) @@ -295,7 +295,7 @@ namespace TehImage chunkSize--; if(CM != 8) cout << "Invalid CM: " << 0+CM << endl; - cout << 0+CM << ", " << 0+CINFO << ", " << (check?"Valid":"Failed checksum") << ", " << (FDICT?"Dict is present":"No dict present") << ", " << 0+FLEVEL << endl; + // cout << 0+CM << ", " << 0+CINFO << ", " << (check?"Valid":"Failed checksum") << ", " << (FDICT?"Dict is present":"No dict present") << ", " << 0+FLEVEL << endl; // cout << 0 + CMF << " " << 0 + FLG << endl; idatData = (uint8_t*)malloc(0); } @@ -336,8 +336,7 @@ namespace TehImage 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; + zlib.decodeData(idatData, idatDataSize, pngImageData, imageDataSize); reader->close(); #define imageDataIndex(x, y) rawImage[y*width*bpp + x] @@ -502,8 +501,9 @@ namespace TehImage char header[] = "IDAT"; writer->writeBytes(header, 4); - writer->writeByte(120); - writer->writeByte(1); + // Should rewrite to calculate properly, currently just copying from another PNG image + writer->writeByte(120); // CMF + writer->writeByte(1); // FLG writer->writeBytes((char*)compressed, encodedSize); diff --git a/src/zlib.cpp b/src/zlib.cpp index 221137c..0b9cdb5 100644 --- a/src/zlib.cpp +++ b/src/zlib.cpp @@ -134,7 +134,7 @@ namespace TehImage { if(staticTree) return; - cout << "Building static tree" << endl; + // cout << "Building static tree" << endl; buildStaticHuffmanTree(&tree, &distTree); staticTree = true; haveTree = true; @@ -510,7 +510,7 @@ namespace TehImage else { writeCode(&outStream, codes[data[inPos]], codeLens[data[inPos]]); - hashTable.insert(curr, inPos); + hashTable.insertOrUpdate(curr, inPos); inPos++; } } -- cgit v1.2.3