From d60976a70e68e1cd312b0e56fe6fbe6c7428cbaa Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 6 May 2026 13:39:47 +1200 Subject: feat: Started on PNG writing implementation Currently writes IHDR and IEND chunks correctly CRC implementation is borrowed from the specification Writer class now also has a buffer for the CRC calculation --- src/files.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'src/files.cpp') diff --git a/src/files.cpp b/src/files.cpp index b757811..20f2672 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -6,6 +6,7 @@ #include #include #include +#include using std::cout, std::endl; @@ -124,14 +125,17 @@ namespace TehImage void Writer::writeByte(char toWrite) { - fwrite(&toWrite, sizeof(char), 1, file); + if(pos == WRITER_BUFFER_SIZE) + throw std::out_of_range("Writer buffer ran out single-byte!!!"); + buffer[pos++] = toWrite; } void Writer::writeBytes(char toWrite[], std::size_t len) { - // for(int i = 0; i < len; i++) - // cout << toWrite[i] << endl; - fwrite(toWrite, sizeof(char), len, file); + if(pos + len > WRITER_BUFFER_SIZE) + throw std::out_of_range("Writer buffer ran out multi-byte!!!"); + memcpy(&(buffer[pos]), toWrite, len); + pos += len; } void Writer::zeroBytes(std::size_t len) @@ -145,10 +149,21 @@ namespace TehImage void Writer::close() { if(ready) + { + flushBuffer(); fclose(file); + } ready = false; } + void Writer::flushBuffer() + { + if(!ready) + throw std::logic_error("Cannot flush buffer before opening file"); + fwrite(buffer, sizeof(char), pos, file); + pos = 0; + } + template <> void Writer::writeData(uint8_t toWrite) { writeByte(toWrite); -- cgit v1.2.3