lib: implement bmp chunking

This commit is contained in:
itsmattkc 2022-07-21 17:06:28 -07:00
parent 85e786924d
commit 1f453fa10e
2 changed files with 33 additions and 15 deletions

View file

@ -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; i<data_.size(); i++) {
size += data_.at(i).size();
}
// Write BMP header
f->WriteU16(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; i<data_.size(); i++) {
f->WriteBytes(data_.at(i));
}
size_t len = f->pos();
f->seek(sz_loc);
f->WriteU32(len);
break;
}
case MxOb::FLC:

View file

@ -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