diff options
| author | Dylan <boss@tehbox.org> | 2026-04-30 01:11:11 +1200 |
|---|---|---|
| committer | Dylan <boss@tehbox.org> | 2026-04-30 01:11:11 +1200 |
| commit | b1eeaeb1e899dab64ce7bd21611e072cbca4107c (patch) | |
| tree | b196802e2499aead4809ed4af6816b1cdcfc896d | |
| parent | e9a32a966d49ab798a4c6f24471669fc03adb395 (diff) | |
| download | tehimage-b1eeaeb1e899dab64ce7bd21611e072cbca4107c.tar.gz tehimage-b1eeaeb1e899dab64ce7bd21611e072cbca4107c.zip | |
feat: Started implementing BMP reading
This is so I can test PNG writing by reading a BMP and writing it to a
PNG, effectively the reverse of the PNG reading testing
| -rw-r--r-- | src/BMPImage.cpp | 52 | ||||
| -rw-r--r-- | src/BMPImage.h | 1 | ||||
| -rw-r--r-- | test/main.cpp | 63 |
3 files changed, 85 insertions, 31 deletions
diff --git a/src/BMPImage.cpp b/src/BMPImage.cpp index b0b7c55..b45ff3f 100644 --- a/src/BMPImage.cpp +++ b/src/BMPImage.cpp @@ -1,7 +1,9 @@ #include "BMPImage.h" +#include "reader.h" #include <cstdint> #include <iostream> +#include <memory> using std::cout, std::endl; @@ -10,7 +12,55 @@ namespace TehImage int BMPImage::readFromFile(std::string filename) { - cout << "Not implemented" << endl; + Reader reader(filename, FileEndianness::LITTLE); + + char magic[2]; + uint8_t expected[] = {0x42, 0x4D}; + reader.readBytes(magic, 2); + uint32_t fileSize = reader.readData<uint32_t>(); + reader.skipBytes(4); + uint32_t offset = reader.readData<uint32_t>(); + + if(strncmp(magic, (char*)expected, 2) != 0) + { + cout << "Not a BMP" << endl; + return 1; + } + + uint32_t headerSize = reader.readData<uint32_t>(); + + // BITMAPINFOHEADER + if(headerSize == 40) + { + width = reader.readData<uint32_t>(); + height = reader.readData<uint32_t>(); + uint16_t colorPlanes = reader.readData<uint16_t>(); + bpp = reader.readData<uint16_t>(); + uint32_t compressionMethod = reader.readData<uint32_t>(); + uint32_t size = reader.readData<uint32_t>(); + uint32_t horrRes = reader.readData<uint32_t>(); + uint32_t vertRes = reader.readData<uint32_t>(); + uint32_t paletteSize = reader.readData<uint32_t>(); + uint32_t importantColors = reader.readData<uint32_t>(); + + pixels = std::make_unique<Pixel[]>(width * height); + + for(int y = height-1; y >= 0; y--) + { + for(int x = 0; x < width; x++) + { + Pixel& pixel = (*this)[x,y]; + pixel.b = reader.readByte(); + pixel.g = reader.readByte(); + pixel.r = reader.readByte(); + pixel.a = 255; + } + } + + // cout << compressionMethod << " " << size << endl; + } + + // cout << "Not implemented" << endl; return 2; }; diff --git a/src/BMPImage.h b/src/BMPImage.h index 651192b..e9da6f5 100644 --- a/src/BMPImage.h +++ b/src/BMPImage.h @@ -12,6 +12,7 @@ namespace TehImage private: public: // template<std::derived_from<Image> T> BMPImage(const T& other) : Image(other) { } + BMPImage() = default; BMPImage(const Image& other) : Image(other) {} int readFromFile(std::string filename) override; diff --git a/test/main.cpp b/test/main.cpp index 8dc9de5..6e4cb91 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -19,47 +19,50 @@ int main(int argc, char* argv[]) return 1; } - TehImage::ZLib encoder; - TehImage::ZLib decoder; + // TehImage::ZLib encoder; + // TehImage::ZLib decoder; - const int inSize = 1000; - uint8_t in[inSize]; - for(int i = 0; i < 10; i++) - { - for(int j = 0; j < 100; j++) - { - in[i*100+j] = i; - } - } + // const int inSize = 1000; + // uint8_t in[inSize]; + // for(int i = 0; i < 10; i++) + // { + // for(int j = 0; j < 100; j++) + // { + // in[i*100+j] = i; + // } + // } - const int encodedSize = 1024; - uint8_t encoded[encodedSize]; + // const int encodedSize = 1024; + // uint8_t encoded[encodedSize]; - int compressedSize = encoder.encodeData(in, inSize, encoded, encodedSize); + // int compressedSize = encoder.encodeData(in, inSize, encoded, encodedSize); - // for(int i = 0; i < compressedSize; i++) - // { - // cout << std::bitset<8>(encoded[i]) << endl; - // } + // // for(int i = 0; i < compressedSize; i++) + // // { + // // cout << std::bitset<8>(encoded[i]) << endl; + // // } - uint8_t out[inSize]; - int decodedSize = decoder.decodeData(encoded, encodedSize, out, inSize); - cout << ((memcmp(in, out, inSize) == 0)?"Pass":"Fail") << ", Size: " << compressedSize << endl; + // uint8_t out[inSize]; + // int decodedSize = decoder.decodeData(encoded, encodedSize, out, inSize); + // cout << ((memcmp(in, out, inSize) == 0)?"Pass":"Fail") << ", Size: " << compressedSize << endl; - // for(int i = 0; i < inSize; i++) - // { - // cout << 0 + in[i] << " " << 0 + out[i] << endl; - // } + // // for(int i = 0; i < inSize; i++) + // // { + // // cout << 0 + in[i] << " " << 0 + out[i] << endl; + // // } - return 0; + // return 0; std::string infile = argv[1]; std::string outfile = argv[2]; + + TehImage::BMPImage bmp; + bmp.readFromFile(infile); - TehImage::PNGImage png; - png.readFromFile(infile); + // TehImage::PNGImage png; + // png.readFromFile(infile); - TehImage::BMPImage bmp(png); - bmp.writeToFile(outfile); + // TehImage::BMPImage bmp(png); + // bmp.writeToFile(outfile); } |
