diff options
| author | Dylan <boss@tehbox.org> | 2026-05-04 18:36:15 +1200 |
|---|---|---|
| committer | Dylan <boss@tehbox.org> | 2026-05-04 18:36:15 +1200 |
| commit | a75bdd0e167140eeb4afb091c9dedd84474c8531 (patch) | |
| tree | 9c94262edc1c1eea4bd4977f76c7cd8ec6a43da1 /src/BMPImage.cpp | |
| parent | 500151be4794923cee6034c26881effeb1bb056d (diff) | |
| download | tehimage-a75bdd0e167140eeb4afb091c9dedd84474c8531.tar.gz tehimage-a75bdd0e167140eeb4afb091c9dedd84474c8531.zip | |
feat: Added a writer class
Also moved both the reader and writer class to the same files, named
files.{cpp,h}
Diffstat (limited to 'src/BMPImage.cpp')
| -rw-r--r-- | src/BMPImage.cpp | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/src/BMPImage.cpp b/src/BMPImage.cpp index b79bc0c..a22e65d 100644 --- a/src/BMPImage.cpp +++ b/src/BMPImage.cpp @@ -1,5 +1,5 @@ #include "BMPImage.h" -#include "reader.h" +#include "files.h" #include <cstdint> #include <iostream> @@ -92,42 +92,42 @@ namespace TehImage int BMPImage::writeToFile(std::string filename) { - FILE* fd = fopen(filename.c_str(), "w"); + Writer writer(filename, LITTLE); char magic[] = "BM"; uint32_t rowSize = (bpp * width + 31)/32; uint32_t fileSize = 14 + 12 + rowSize*height*/*(bitDepth/8)*/8*3; - char zero[] = "\0\0\0\0"; + // char zero[] = "\0\0\0\0"; uint32_t offset = 26; uint32_t headerSize = 12; uint16_t width = this->width; uint16_t height = this->height; uint16_t colorPlanes = 1; - uint16_t bitsPerPixel = /*bitDepth*/8*3; - - fwrite(magic, sizeof(char), 2, fd); - fwrite(&fileSize, sizeof(uint32_t), 1, fd); - fwrite(zero, sizeof(char), 4, fd); - fwrite(&offset, sizeof(uint32_t), 1, fd); - fwrite(&headerSize, sizeof(uint32_t), 1, fd); + uint16_t bitsPerPixel = /*bpp*/8*3; + + writer.writeBytes(magic, 2); + writer.writeData(fileSize); + writer.zeroBytes(4); + writer.writeData(offset); + writer.writeData(headerSize); - fwrite(&width, sizeof(uint16_t), 1, fd); - fwrite(&height, sizeof(uint16_t), 1, fd); - fwrite(&colorPlanes, sizeof(uint16_t), 1, fd); - fwrite(&bitsPerPixel, sizeof(uint16_t), 1, fd); + writer.writeData(width); + writer.writeData(height); + writer.writeData(colorPlanes); + writer.writeData(bitsPerPixel); for(int y = height-1; y >= 0; y--) { for(int x = 0; x < width; x++) { Pixel& pixel = (*this)[x,y]; - fwrite(&pixel.b, sizeof(uint8_t), 1, fd); - fwrite(&pixel.g, sizeof(uint8_t), 1, fd); - fwrite(&pixel.r, sizeof(uint8_t), 1, fd); + writer.writeData(pixel.b); + writer.writeData(pixel.g); + writer.writeData(pixel.r); } - fwrite(zero, sizeof(char), (4-((width * 3)%4))%4, fd); + writer.zeroBytes((4-((width * 3)%4))%4); } - fclose(fd); + writer.close(); return 0; }; |
