commit 4786ce3c8e82c653dae2cf85685c6c538890ba0a
parent 7d71416da192a7e98953395036aad058a57a0e5c
Author: Andrew Laack <andrew@laack.co>
Date: Wed, 16 Jul 2025 13:29:52 -0500
I think there is an issue with sizing
Diffstat:
3 files changed, 47 insertions(+), 33 deletions(-)
diff --git a/include/chunk.h b/include/chunk.h
@@ -26,7 +26,7 @@ private:
public:
Chunk(std::string filename, uint start, uint size);
Chunk(std::vector<char> chunkData);
- // read in the written chunk from tga format.
+ // read in the written chunk from pbm format.
Chunk(std::string filename);
std::vector<char> getChunk();
// assumes header is populated in private member.
diff --git a/src/chunk.cpp b/src/chunk.cpp
@@ -23,7 +23,7 @@ Chunk::Chunk(std::string filename) {
file.close();
- // header (.tga header), comment (.tga comment), dims (.tga spec)
+ // header (.pbm header), comment (.pbm comment), dims (.pbm spec)
std::string header = "";
std::string comment = "";
@@ -209,10 +209,8 @@ void Chunk::writeImage(std::string filename, uint chunkNumber, std::string origi
std::vector<char> data = chunk.getChunk();
-
-
std::string firstLine = "P1\n";
- std::string comment = "# Encoded with Andrew's awesome .tga encoder.\n";
+ std::string comment = "# Encoded with Andrew's awesome .pbm encoder.\n";
std::string chunkNumberEncoded = std::bitset<8*4>(chunkNumber).to_string();
std::string charactersEncoded = std::bitset<8*4>(data.size()).to_string();
@@ -258,12 +256,6 @@ void Chunk::writeImage(std::string filename, uint chunkNumber, std::string origi
this->header.characters = data.size();
- // this seems to run when it shouldn't...
- // if (x * y < (data.size() * 8) + header.size()) {
-
- // throw std::invalid_argument("x and y dimensions are too small for the chunk.");
- // }
-
uint xPos = 0;
for (char bit : header) {
@@ -281,6 +273,7 @@ void Chunk::writeImage(std::string filename, uint chunkNumber, std::string origi
int target = x * y;
int added = 0;
+
for (char chr : data) {
std::string current = std::bitset<8>(chr).to_string();
for (int i = 0 ; i < 8; ++i) {
diff --git a/src/encode.cpp b/src/encode.cpp
@@ -28,37 +28,58 @@ int main(int argc, char* argv[]) {
int chunkSize = ((x * y) - HEADER_ALLOCATION);
- Chunk chunk = Chunk(source, 0, chunkSize);
- if (chunk.end < std::filesystem::file_size(source)){
- LOG("FILE NOT ENTIRELY READ")
- std::exit(-10);
- }
+ uint end = 0;
+ uint itr = 0;
- std::vector<char> bytes = chunk.getChunk();
- std::ofstream outFile;
- Chunk outChunk = Chunk(bytes);
- outChunk.writeImage(destination, 1, source, x, y);
+ std::vector<Chunk> chunks;
- Chunk readChunk = Chunk(destination);
+ while (end < std::filesystem::file_size(source)){
+ LOG("READING CHUNK # " + std::to_string(itr))
+ Chunk chunk = Chunk(source, end, chunkSize);
+ end = chunk.end;
+ chunks.push_back(chunk);
+ itr += 1;
+ LOG(end)
+ }
- std::string beforeChunk = readChunk.toString();
- std::string afterChunk = outChunk.toString();
- if (beforeChunk == afterChunk) {
- LOG("CHUNKS MATCH")
- } else {
- LOG("CHUNKS DON'T MATCH")
+ std::string destinationWithoutExtension = destination.replace(destination.size() - 4, 4, "");
+
+ uint i = 0;
+ for (auto chunk : chunks){
+ // std::string filename, uint chunkNumber, std::string originalFilename, uint x, uint y
+ std::string chunkFname = destinationWithoutExtension + std::to_string(i) + ".pbm";
+ LOG(chunkFname)
+ chunk.writeImage(chunkFname, i, source, x, y);
+ i += 1;
}
- // this writes out to a specific file.
- std::string destinationWithoutExtension = destination.replace(destination.size() - 4, 4, "");
+ //std::vector<char> bytes = chunk.getChunk();
+ //std::ofstream outFile;
+ //Chunk outChunk = Chunk(bytes);
+ //outChunk.writeImage(destination, 1, source, x, y);
+
+ //Chunk readChunk = Chunk(destination);
+
+ //std::string beforeChunk = readChunk.toString();
+ //std::string afterChunk = outChunk.toString();
+
+ //if (beforeChunk == afterChunk) {
+ // LOG("CHUNKS MATCH")
+ //} else {
+ // LOG("CHUNKS DON'T MATCH")
+ //}
+
+
+ //// this writes out to a specific file.
+ //std::string destinationWithoutExtension = destination.replace(destination.size() - 4, 4, "");
- outChunk.writeChunk("after-" + destinationWithoutExtension + ".txt");
- // this writes out to the file that was
- // specified in the .tga header file.
- // readChunk.writeChunk();
+ //outChunk.writeChunk("after-" + destinationWithoutExtension + ".txt");
+ //// this writes out to the file that was
+ //// specified in the .pbm header file.
+ //// readChunk.writeChunk();
return 0;
}