diff options
| author | Dylan <boss@tehbox.org> | 2026-05-06 13:39:47 +1200 |
|---|---|---|
| committer | Dylan <boss@tehbox.org> | 2026-05-06 13:39:47 +1200 |
| commit | d60976a70e68e1cd312b0e56fe6fbe6c7428cbaa (patch) | |
| tree | 00bc987a6d935ca26e4d4c2d61ad70d7aa3c2f4c /src/files.cpp | |
| parent | a75bdd0e167140eeb4afb091c9dedd84474c8531 (diff) | |
| download | tehimage-d60976a70e68e1cd312b0e56fe6fbe6c7428cbaa.tar.gz tehimage-d60976a70e68e1cd312b0e56fe6fbe6c7428cbaa.zip | |
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
Diffstat (limited to 'src/files.cpp')
| -rw-r--r-- | src/files.cpp | 23 |
1 files changed, 19 insertions, 4 deletions
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 <cstdio> #include <cstring> #include <iostream> +#include <stdexcept> 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); |
