From 1f453fa10e0a2670ccb466b22e76bec55745aa5a Mon Sep 17 00:00:00 2001 From: itsmattkc <34096995+itsmattkc@users.noreply.github.com> Date: Thu, 21 Jul 2022 17:06:28 -0700 Subject: [PATCH] lib: implement bmp chunking --- lib/object.cpp | 38 +++++++++++++++++++++++--------------- lib/othertypes.h | 10 ++++++++++ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/lib/object.cpp b/lib/object.cpp index dab5736..d30428f 100644 --- a/lib/object.cpp +++ b/lib/object.cpp @@ -128,6 +128,19 @@ bool Object::ReplaceWithFile(FileBase *f) } return true; } + case MxOb::STL: + { + BMP bmp; + f->ReadData(&bmp, sizeof(bmp)); + + bytearray info_header = f->ReadBytes(bmp.DataOffset - f->pos()); + data_.push_back(info_header); + + bytearray pixels = f->ReadBytes(bmp.FileSize - f->pos()); + data_.push_back(pixels); + + return true; + } default: LogWarning() << "Don't yet know how to chunk type " << RIFF::PrintU32AsString(this->filetype()) << std::endl; break; @@ -172,28 +185,23 @@ bool Object::ExtractToFile(FileBase *f) const } case MxOb::STL: { - static const uint32_t BMP_HDR_SZ = 14; + uint32_t size = sizeof(BMP); + for (size_t i=0; iWriteU16(0x4D42); + BMP bmp; + bmp.Signature = 0x4D42; // 'BM' + bmp.FileSize = size; + bmp.Reserved = 0; + bmp.DataOffset = data_.at(0).size() + sizeof(BMP); - // Write placeholder for size - size_t sz_loc = f->pos(); - f->WriteU32(0); - - // Write "reserved" bytes - f->WriteU32(0); - - // Write data offset - f->WriteU32(data_.at(0).size() + BMP_HDR_SZ); + f->WriteData(&bmp, sizeof(bmp)); for (size_t i=0; iWriteBytes(data_.at(i)); } - - size_t len = f->pos(); - f->seek(sz_loc); - f->WriteU32(len); break; } case MxOb::FLC: diff --git a/lib/othertypes.h b/lib/othertypes.h index 607d3fe..b748d3b 100644 --- a/lib/othertypes.h +++ b/lib/othertypes.h @@ -72,6 +72,16 @@ public: uint32_t Dummy; }; +// Analogous to BITMAPFILEHEADER, copied from http://www.ece.ualberta.ca/~elliott/ee552/studentAppNotes/2003_w/misc/bmp_file_format/bmp_file_format.htm +LIBWEAVER_PACK(class BMP +{ +public: + uint16_t Signature; // Should always be BM + uint32_t FileSize; // Size of total file including header and 'BM' + uint32_t Reserved; // Unused (always 0) + uint32_t DataOffset; // Offset of actual data after BITMAPINFOHEADER +}); + } #endif // OTHERTYPES_H