commit cbd557156b5f6cd51aa7f4f08784bc52978230f4
parent d80d1b79e1f7c463df6669ec73fbeaef640c9300
Author: Andrew Laack <andrew@laack.co>
Date: Wed, 16 Jul 2025 22:53:56 -0500
I have fixed the issue, but not in the most elegant way.
Diffstat:
1 file changed, 16 insertions(+), 12 deletions(-)
diff --git a/src/chunk.cpp b/src/chunk.cpp
@@ -6,6 +6,8 @@
# include "../include/debug.h"
+const int IMAGE_HEADER_SIZE = 848;
+
// read in saved chunk from file
Chunk::Chunk(std::string filename) {
@@ -55,11 +57,16 @@ Chunk::Chunk(std::string filename) {
std::string content = "";
+ // this would be better if we didn't preallocate a size for this section
+ // but that requires quite a bit more work for what I think is minimal benefit.
+
// ENCODED HEADER (this data is encoded in the image itself):
- // chunk number 4 bytes
- // characters 4 bytes
- // fnamelen 4 bytes
- // filename fnamelen
+ // chunk number 4 bytes (16 bits)
+ // characters 4 bytes (16 bits)
+ // fnamelen 4 bytes (16 bits)
+ // filename fnamelen (max 800 bits - 100 chars)
+ //
+ // 16 + 16 + 16 + 800 = 848
@@ -131,10 +138,13 @@ Chunk::Chunk(std::string filename, uint start, uint x, uint y) {
file.open(filename, std::ios::binary);
file.seekg(start);
+ uint bytesToLoad = ((x * y) / 8) - IMAGE_HEADER_SIZE;
+ uint fSize = std::filesystem::file_size(filename);
- uint bytesToLoad = (x * y) / 8;
+ if (fSize < bytesToLoad){
+ bytesToLoad = fSize;
+ }
- uint fSize = std::filesystem::file_size(filename);
LOG("FILE SIZE: " + std::to_string(fSize))
LOG("LOADING IN " + std::to_string(bytesToLoad) + " BYTES")
@@ -150,8 +160,6 @@ std::vector<char> Chunk::getChunk() {
return this->chunk;
}
-
-
// use if header info is populated in the object
// and you are trying to write back out what you read in.
// append if not the first chunk.
@@ -284,10 +292,6 @@ void Chunk::writeImage(std::string filename, uint chunkNumber, std::string origi
}
LOG(std::to_string(added))
- LOG(std::to_string(added))
- LOG(std::to_string(added))
- LOG(std::to_string(added))
- LOG(std::to_string(added))
this->written = added;