From 17522b98d422d1f8ada53f6c4490217c8e05710f Mon Sep 17 00:00:00 2001 From: Christian Semmler Date: Mon, 25 Dec 2023 21:58:39 -0500 Subject: [PATCH] Refactor MxBitmap inline functions, match ImportBitmap --- LEGO1/mxbitmap.cpp | 26 ++++---------------------- LEGO1/mxbitmap.h | 23 +++++++++++++++++++---- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/LEGO1/mxbitmap.cpp b/LEGO1/mxbitmap.cpp index 30ba032d..4f2393cf 100644 --- a/LEGO1/mxbitmap.cpp +++ b/LEGO1/mxbitmap.cpp @@ -10,22 +10,6 @@ DECOMP_SIZE_ASSERT(MxBITMAPINFO, 0x428); // (1998) GLOBAL: LEGO1 0x10102184 MxU16 g_bitmapSignature = TWOCC('B', 'M'); -// Bit mask trick to round up to the nearest multiple of four. -// Pixel data may be stored with padding. -// https://learn.microsoft.com/en-us/windows/win32/medfound/image-stride -inline MxLong AlignToFourByte(MxLong p_value) -{ - return (p_value + 3) & -4; -} - -// Same as the one from legoutil.h, but flipped the other way -// TODO: While it's not outside the realm of possibility that they -// reimplemented Abs for only this file, that seems odd, right? -inline MxLong AbsFlipped(MxLong p_value) -{ - return p_value > 0 ? p_value : -p_value; -} - // FUNCTION: LEGO1 0x1004e0d0 int MxBitmap::VTable0x28(int) { @@ -140,16 +124,14 @@ MxResult MxBitmap::ImportBitmap(MxBitmap* p_bitmap) this->m_info = new MxBITMAPINFO; if (this->m_info) { - MxLong height = AbsFlipped(p_bitmap->m_bmiHeader->biHeight); - this->m_data = new MxU8[AlignToFourByte(p_bitmap->m_bmiHeader->biWidth) * height]; + this->m_data = new MxU8[p_bitmap->GetDataSize()]; if (this->m_data) { - memcpy(this->m_info, p_bitmap->m_info, sizeof(*this->m_info)); - height = AbsFlipped(p_bitmap->m_bmiHeader->biHeight); - memcpy(this->m_data, p_bitmap->m_data, AlignToFourByte(p_bitmap->m_bmiHeader->biWidth) * height); + memcpy(this->m_info, p_bitmap->GetBitmapInfo(), MxBITMAPINFO::Size()); + memcpy(this->m_data, p_bitmap->GetBitmapData(), p_bitmap->GetDataSize()); - result = SUCCESS; this->m_bmiHeader = &this->m_info->m_bmiHeader; this->m_paletteData = this->m_info->m_bmiColors; + result = SUCCESS; } } diff --git a/LEGO1/mxbitmap.h b/LEGO1/mxbitmap.h index b87bcdcf..ed944138 100644 --- a/LEGO1/mxbitmap.h +++ b/LEGO1/mxbitmap.h @@ -19,6 +19,8 @@ struct MxBITMAPINFO { BITMAPINFOHEADER m_bmiHeader; RGBQUAD m_bmiColors[256]; + + static MxU32 Size() { return sizeof(MxBITMAPINFO); } }; // Non-standard value for biCompression in the BITMAPINFOHEADER struct. @@ -56,16 +58,29 @@ class MxBitmap : public MxCore { MxS32 p_destHeight ); // vtable+40 + // Bit mask trick to round up to the nearest multiple of four. + // Pixel data may be stored with padding. + // https://learn.microsoft.com/en-us/windows/win32/medfound/image-stride + inline MxLong AlignToFourByte(MxLong p_value) const { return (p_value + 3) & -4; } + + // Same as the one from legoutil.h, but flipped the other way + // TODO: While it's not outside the realm of possibility that they + // reimplemented Abs for only this file, that seems odd, right? + inline MxLong AbsFlipped(MxLong p_value) const { return p_value > 0 ? p_value : -p_value; } + inline BITMAPINFOHEADER* GetBmiHeader() const { return m_bmiHeader; } inline MxLong GetBmiWidth() const { return m_bmiHeader->biWidth; } inline MxLong GetBmiStride() const { return ((m_bmiHeader->biWidth + 3) & -4); } inline MxLong GetBmiHeight() const { return m_bmiHeader->biHeight; } - inline MxLong GetBmiHeightAbs() const - { - return m_bmiHeader->biHeight > 0 ? m_bmiHeader->biHeight : -m_bmiHeader->biHeight; - } + inline MxLong GetBmiHeightAbs() const { return AbsFlipped(m_bmiHeader->biHeight); } inline MxU8* GetBitmapData() const { return m_data; } inline MxBITMAPINFO* GetBitmapInfo() const { return m_info; } + inline MxLong GetDataSize() const + { + MxLong absHeight = GetBmiHeightAbs(); + MxLong alignedWidth = AlignToFourByte(m_bmiHeader->biWidth); + return alignedWidth * absHeight; + } private: MxResult ImportColorsToPalette(RGBQUAD*, MxPalette*);