Refactor MxBitmap (again) (#961)

* Remove this

* Starting list of beta addrs

* Static for height-specific abs, fix StrechBits

* MxBitmap refactor
This commit is contained in:
MS
2024-05-28 03:32:51 -04:00
committed by GitHub
parent 219b65f5ad
commit a88546baad
2 changed files with 209 additions and 138 deletions

View File

@@ -34,6 +34,7 @@ struct MxBITMAPINFO {
// SIZE 0x20 // SIZE 0x20
// VTABLE: LEGO1 0x100dc7b0 // VTABLE: LEGO1 0x100dc7b0
// VTABLE: BETA10 0x101c21f8
class MxBitmap : public MxCore { class MxBitmap : public MxCore {
public: public:
MxBitmap(); MxBitmap();
@@ -46,6 +47,7 @@ public:
virtual MxLong Read(const char* p_filename); // vtable+24 virtual MxLong Read(const char* p_filename); // vtable+24
// FUNCTION: LEGO1 0x1004e0d0 // FUNCTION: LEGO1 0x1004e0d0
// FUNCTION: BETA10 0x10060fc0
virtual int VTable0x28(int) { return -1; } // vtable+28 virtual int VTable0x28(int) { return -1; } // vtable+28
virtual void BitBlt( virtual void BitBlt(
@@ -82,26 +84,35 @@ public:
// Bit mask trick to round up to the nearest multiple of four. // Bit mask trick to round up to the nearest multiple of four.
// Pixel data may be stored with padding. // Pixel data may be stored with padding.
// https://learn.microsoft.com/en-us/windows/win32/medfound/image-stride // https://learn.microsoft.com/en-us/windows/win32/medfound/image-stride
// FUNCTION: BETA10 0x1002c510
inline MxLong AlignToFourByte(MxLong p_value) const { return (p_value + 3) & -4; } inline MxLong AlignToFourByte(MxLong p_value) const { return (p_value + 3) & -4; }
// Same as the one from legoutils.h, but flipped the other way // DECOMP: This could be a free function. It is static here because it has no
// TODO: While it's not outside the realm of possibility that they // reference to "this". In the beta it is called in two places:
// reimplemented Abs for only this file, that seems odd, right? // 1. GetBmiHeightAbs
inline MxLong AbsFlipped(MxLong p_value) const { return p_value > 0 ? p_value : -p_value; } // 2. at 0x101523b9, in reference to BITMAPINFOHEADER.biHeight
// FUNCTION: BETA10 0x1002c690
static MxLong HeightAbs(MxLong p_value) { return p_value > 0 ? p_value : -p_value; }
inline BITMAPINFOHEADER* GetBmiHeader() const { return m_bmiHeader; } inline BITMAPINFOHEADER* GetBmiHeader() const { return m_bmiHeader; }
// FUNCTION: BETA10 0x1002c440
inline MxLong GetBmiWidth() const { return m_bmiHeader->biWidth; } inline MxLong GetBmiWidth() const { return m_bmiHeader->biWidth; }
inline MxLong GetBmiStride() const { return ((m_bmiHeader->biWidth + 3) & -4); } inline MxLong GetBmiStride() const { return ((m_bmiHeader->biWidth + 3) & -4); }
inline MxLong GetBmiHeight() const { return m_bmiHeader->biHeight; } inline MxLong GetBmiHeight() const { return m_bmiHeader->biHeight; }
inline MxLong GetBmiHeightAbs() const { return AbsFlipped(m_bmiHeader->biHeight); }
// FUNCTION: BETA10 0x1002c470
inline MxLong GetBmiHeightAbs() const { return HeightAbs(m_bmiHeader->biHeight); }
// FUNCTION: BETA10 0x10083900
inline MxU8* GetImage() const { return m_data; } inline MxU8* GetImage() const { return m_data; }
// FUNCTION: BETA10 0x100838d0
inline MxBITMAPINFO* GetBitmapInfo() const { return m_info; } inline MxBITMAPINFO* GetBitmapInfo() const { return m_info; }
inline MxLong GetDataSize() const
{ // FUNCTION: BETA10 0x100982b0
MxLong absHeight = GetBmiHeightAbs(); inline MxLong GetDataSize() const { return AlignToFourByte(m_bmiHeader->biWidth) * GetBmiHeightAbs(); }
MxLong alignedWidth = AlignToFourByte(m_bmiHeader->biWidth);
return alignedWidth * absHeight;
}
inline MxLong GetAdjustedStride() inline MxLong GetAdjustedStride()
{ {
if (m_bmiHeader->biCompression == BI_RGB_TOPDOWN || m_bmiHeader->biHeight < 0) { if (m_bmiHeader->biCompression == BI_RGB_TOPDOWN || m_bmiHeader->biHeight < 0) {
@@ -138,9 +149,24 @@ public:
} }
// SYNTHETIC: LEGO1 0x100bc9f0 // SYNTHETIC: LEGO1 0x100bc9f0
// SYNTHETIC: BETA10 0x1013dcd0
// MxBitmap::`scalar deleting destructor' // MxBitmap::`scalar deleting destructor'
private: private:
// FUNCTION: BETA10 0x1013dd10
inline MxLong MxBitmapInfoSize() const { return sizeof(MxBITMAPINFO); }
// FUNCTION: BETA10 0x1013dd30
inline MxBool IsBottomUp()
{
if (m_bmiHeader->biCompression == BI_RGB_TOPDOWN) {
return FALSE;
}
else {
return m_bmiHeader->biHeight > 0;
}
}
MxResult ImportColorsToPalette(RGBQUAD*, MxPalette*); MxResult ImportColorsToPalette(RGBQUAD*, MxPalette*);
MxBITMAPINFO* m_info; // 0x08 MxBITMAPINFO* m_info; // 0x08

View File

@@ -8,66 +8,79 @@ DECOMP_SIZE_ASSERT(MxBitmap, 0x20);
DECOMP_SIZE_ASSERT(MxBITMAPINFO, 0x428); DECOMP_SIZE_ASSERT(MxBITMAPINFO, 0x428);
// GLOBAL: LEGO1 0x10102184 // GLOBAL: LEGO1 0x10102184
// GLOBAL: BETA10 0x10203030
MxU16 g_bitmapSignature = TWOCC('B', 'M'); MxU16 g_bitmapSignature = TWOCC('B', 'M');
// FUNCTION: LEGO1 0x100bc980 // FUNCTION: LEGO1 0x100bc980
// FUNCTION: BETA10 0x1013cab0
MxBitmap::MxBitmap() MxBitmap::MxBitmap()
{ {
this->m_info = NULL; m_info = NULL;
this->m_bmiHeader = NULL; m_bmiHeader = NULL;
this->m_paletteData = NULL; m_paletteData = NULL;
this->m_data = NULL; m_data = NULL;
this->m_isHighColor = FALSE; m_isHighColor = FALSE;
this->m_palette = NULL; m_palette = NULL;
} }
// FUNCTION: LEGO1 0x100bca10 // FUNCTION: LEGO1 0x100bca10
// FUNCTION: BETA10 0x1013cb58
MxBitmap::~MxBitmap() MxBitmap::~MxBitmap()
{ {
if (this->m_info) { if (m_info) {
delete m_info; delete m_info;
} }
if (this->m_data) { if (m_data) {
delete m_data; delete m_data;
} }
if (this->m_palette) { if (m_palette) {
delete m_palette; delete m_palette;
} }
} }
// FUNCTION: LEGO1 0x100bcaa0 // FUNCTION: LEGO1 0x100bcaa0
// FUNCTION: BETA10 0x1013cc47
MxResult MxBitmap::SetSize(MxS32 p_width, MxS32 p_height, MxPalette* p_palette, MxBool p_isHighColor) MxResult MxBitmap::SetSize(MxS32 p_width, MxS32 p_height, MxPalette* p_palette, MxBool p_isHighColor)
{ {
MxResult ret = FAILURE; MxResult ret = FAILURE;
MxLong size = AlignToFourByte(p_width) * p_height; MxLong size = AlignToFourByte(p_width) * p_height;
m_info = new MxBITMAPINFO; m_info = (MxBITMAPINFO*) new MxU8[MxBitmapInfoSize()];
if (m_info) { if (!m_info) {
m_data = new MxU8[size]; goto done;
if (m_data) {
m_bmiHeader = &m_info->m_bmiHeader;
m_paletteData = m_info->m_bmiColors;
memset(&m_info->m_bmiHeader, 0, sizeof(m_info->m_bmiHeader));
m_bmiHeader->biSize = sizeof(*m_bmiHeader); // should be 40 bytes
m_bmiHeader->biWidth = p_width;
m_bmiHeader->biHeight = p_height;
m_bmiHeader->biPlanes = 1;
m_bmiHeader->biBitCount = 8;
m_bmiHeader->biCompression = 0;
m_bmiHeader->biSizeImage = size;
if (!ImportColorsToPalette(m_paletteData, p_palette)) {
if (!SetBitDepth(p_isHighColor)) {
ret = SUCCESS;
}
}
}
} }
m_data = new MxU8[size];
if (!m_data) {
goto done;
}
m_bmiHeader = &m_info->m_bmiHeader;
m_paletteData = m_info->m_bmiColors;
memset(m_bmiHeader, 0, sizeof(m_info->m_bmiHeader));
m_bmiHeader->biSize = sizeof(*m_bmiHeader); // should be 40 bytes
m_bmiHeader->biWidth = p_width;
m_bmiHeader->biHeight = p_height;
m_bmiHeader->biPlanes = 1;
m_bmiHeader->biBitCount = 8;
m_bmiHeader->biCompression = 0;
m_bmiHeader->biSizeImage = size;
if (ImportColorsToPalette(m_paletteData, p_palette)) {
goto done;
}
if (SetBitDepth(p_isHighColor)) {
goto done;
}
ret = SUCCESS;
done:
if (ret) { if (ret) {
if (m_info) { if (m_info) {
delete m_info; delete[] m_info;
m_info = NULL; m_info = NULL;
} }
@@ -81,33 +94,37 @@ MxResult MxBitmap::SetSize(MxS32 p_width, MxS32 p_height, MxPalette* p_palette,
} }
// FUNCTION: LEGO1 0x100bcba0 // FUNCTION: LEGO1 0x100bcba0
// FUNCTION: BETA10 0x1013ce25
MxResult MxBitmap::ImportBitmapInfo(MxBITMAPINFO* p_info) MxResult MxBitmap::ImportBitmapInfo(MxBITMAPINFO* p_info)
{ {
MxResult result = FAILURE; MxResult result = FAILURE;
MxLong width = p_info->m_bmiHeader.biWidth; MxLong size = AlignToFourByte(p_info->m_bmiHeader.biWidth) * p_info->m_bmiHeader.biHeight;
MxLong height = p_info->m_bmiHeader.biHeight;
MxLong size = AlignToFourByte(width) * height;
this->m_info = new MxBITMAPINFO; m_info = (MxBITMAPINFO*) new MxU8[MxBitmapInfoSize()];
if (this->m_info) { if (!m_info) {
this->m_data = new MxU8[size]; goto done;
if (this->m_data) {
memcpy(this->m_info, p_info, sizeof(*this->m_info));
this->m_bmiHeader = &this->m_info->m_bmiHeader;
this->m_paletteData = this->m_info->m_bmiColors;
result = SUCCESS;
}
} }
m_data = new MxU8[size];
if (!m_data) {
goto done;
}
memcpy(m_info, p_info, MxBitmapInfoSize());
m_bmiHeader = &m_info->m_bmiHeader;
m_paletteData = m_info->m_bmiColors;
result = SUCCESS;
done:
if (result != SUCCESS) { if (result != SUCCESS) {
if (this->m_info) { if (m_info) {
delete this->m_info; delete[] m_info;
this->m_info = NULL; m_info = NULL;
} }
if (this->m_data) { if (m_data) {
delete[] this->m_data; delete[] m_data;
this->m_data = NULL; m_data = NULL;
} }
} }
@@ -115,32 +132,38 @@ MxResult MxBitmap::ImportBitmapInfo(MxBITMAPINFO* p_info)
} }
// FUNCTION: LEGO1 0x100bcc40 // FUNCTION: LEGO1 0x100bcc40
// FUNCTION: BETA10 0x1013cf6d
MxResult MxBitmap::ImportBitmap(MxBitmap* p_bitmap) MxResult MxBitmap::ImportBitmap(MxBitmap* p_bitmap)
{ {
MxResult result = FAILURE; MxResult result = FAILURE;
this->m_info = new MxBITMAPINFO; m_info = (MxBITMAPINFO*) new MxU8[p_bitmap->MxBitmapInfoSize()];
if (this->m_info) { if (!m_info) {
this->m_data = new MxU8[p_bitmap->GetDataSize()]; goto done;
if (this->m_data) {
memcpy(this->m_info, p_bitmap->GetBitmapInfo(), MxBITMAPINFO::Size());
memcpy(this->m_data, p_bitmap->GetImage(), p_bitmap->GetDataSize());
this->m_bmiHeader = &this->m_info->m_bmiHeader;
this->m_paletteData = this->m_info->m_bmiColors;
result = SUCCESS;
}
} }
m_data = new MxU8[p_bitmap->GetDataSize()];
if (!m_data) {
goto done;
}
memcpy(m_info, p_bitmap->GetBitmapInfo(), p_bitmap->MxBitmapInfoSize());
memcpy(m_data, p_bitmap->GetImage(), p_bitmap->GetDataSize());
m_bmiHeader = &m_info->m_bmiHeader;
m_paletteData = m_info->m_bmiColors;
result = SUCCESS;
done:
if (result != SUCCESS) { if (result != SUCCESS) {
if (this->m_info) { if (m_info) {
delete this->m_info; delete[] m_info;
this->m_info = NULL; m_info = NULL;
} }
if (this->m_data) { if (m_data) {
delete this->m_data; delete[] m_data;
this->m_data = NULL; m_data = NULL;
} }
} }
@@ -148,16 +171,25 @@ MxResult MxBitmap::ImportBitmap(MxBitmap* p_bitmap)
} }
// FUNCTION: LEGO1 0x100bcd10 // FUNCTION: LEGO1 0x100bcd10
// FUNCTION: BETA10 0x1013d0c7
MxLong MxBitmap::Read(const char* p_filename) MxLong MxBitmap::Read(const char* p_filename)
{ {
MxResult result = FAILURE; MxResult result = FAILURE;
HANDLE handle = HANDLE handle = 0;
CreateFileA(p_filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle != INVALID_HANDLE_VALUE && !LoadFile(handle)) { handle = CreateFileA(p_filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
result = SUCCESS;
if (handle == INVALID_HANDLE_VALUE) {
goto done;
} }
if (LoadFile(handle)) {
goto done;
}
result = SUCCESS;
done:
if (handle) { if (handle) {
CloseHandle(handle); CloseHandle(handle);
} }
@@ -166,46 +198,64 @@ MxLong MxBitmap::Read(const char* p_filename)
} }
// FUNCTION: LEGO1 0x100bcd60 // FUNCTION: LEGO1 0x100bcd60
// FUNCTION: BETA10 0x1013d169
MxResult MxBitmap::LoadFile(HANDLE p_handle) MxResult MxBitmap::LoadFile(HANDLE p_handle)
{ {
MxResult result = FAILURE; MxResult result = FAILURE;
MxLong unused = 0;
MxLong size;
DWORD bytesRead; DWORD bytesRead;
BITMAPFILEHEADER hdr; BITMAPFILEHEADER hdr;
if (!ReadFile(p_handle, &hdr, sizeof(hdr), &bytesRead, NULL)) {
BOOL ret = ReadFile(p_handle, &hdr, sizeof(hdr), &bytesRead, NULL); goto done;
if (ret && (hdr.bfType == g_bitmapSignature)) {
this->m_info = new MxBITMAPINFO;
if (this->m_info) {
ret = ReadFile(p_handle, this->m_info, sizeof(*this->m_info), &bytesRead, NULL);
if (ret && (this->m_info->m_bmiHeader.biBitCount == 8)) {
MxLong size = hdr.bfSize - (sizeof(MxBITMAPINFO) + sizeof(BITMAPFILEHEADER));
this->m_data = new MxU8[size];
if (this->m_data) {
ret = ReadFile(p_handle, this->m_data, size, &bytesRead, NULL);
if (ret) {
this->m_bmiHeader = &this->m_info->m_bmiHeader;
this->m_paletteData = this->m_info->m_bmiColors;
if (this->m_info->m_bmiHeader.biSizeImage == 0) {
MxLong height = AbsFlipped(this->m_info->m_bmiHeader.biHeight);
this->m_info->m_bmiHeader.biSizeImage =
AlignToFourByte(this->m_info->m_bmiHeader.biWidth) * height;
}
result = SUCCESS;
}
}
}
}
} }
if (hdr.bfType != g_bitmapSignature) {
goto done;
}
m_info = (MxBITMAPINFO*) new MxU8[MxBitmapInfoSize()];
if (!m_info) {
goto done;
}
if (!ReadFile(p_handle, m_info, MxBitmapInfoSize(), &bytesRead, NULL)) {
goto done;
}
if (m_info->m_bmiHeader.biBitCount != 8) {
goto done;
}
size = hdr.bfSize - sizeof(BITMAPFILEHEADER) - MxBitmapInfoSize();
m_data = new MxU8[size];
if (!m_data) {
goto done;
}
if (!ReadFile(p_handle, m_data, size, &bytesRead, NULL)) {
goto done;
}
m_bmiHeader = &m_info->m_bmiHeader;
m_paletteData = m_info->m_bmiColors;
if (m_info->m_bmiHeader.biSizeImage == 0) {
m_info->m_bmiHeader.biSizeImage = GetDataSize();
}
result = SUCCESS;
done:
if (result != SUCCESS) { if (result != SUCCESS) {
if (this->m_info) { if (m_info) {
delete this->m_info; delete[] m_info;
this->m_info = NULL; m_info = NULL;
} }
if (this->m_data) { if (m_data) {
delete this->m_data; delete[] m_data;
this->m_data = NULL; m_data = NULL;
} }
} }
@@ -213,6 +263,7 @@ MxResult MxBitmap::LoadFile(HANDLE p_handle)
} }
// FUNCTION: LEGO1 0x100bce70 // FUNCTION: LEGO1 0x100bce70
// FUNCTION: BETA10 0x1013d399
void MxBitmap::BitBlt( void MxBitmap::BitBlt(
MxBitmap* p_src, MxBitmap* p_src,
MxS32 p_srcLeft, MxS32 p_srcLeft,
@@ -252,6 +303,7 @@ void MxBitmap::BitBlt(
} }
// FUNCTION: LEGO1 0x100bd020 // FUNCTION: LEGO1 0x100bd020
// FUNCTION: BETA10 0x1013d4ea
void MxBitmap::BitBltTransparent( void MxBitmap::BitBltTransparent(
MxBitmap* p_src, MxBitmap* p_src,
MxS32 p_srcLeft, MxS32 p_srcLeft,
@@ -298,24 +350,21 @@ void MxBitmap::BitBltTransparent(
} }
// FUNCTION: LEGO1 0x100bd1c0 // FUNCTION: LEGO1 0x100bd1c0
// FUNCTION: BETA10 0x1013d684
MxPalette* MxBitmap::CreatePalette() MxPalette* MxBitmap::CreatePalette()
{ {
MxBool success = FALSE; MxBool success = FALSE;
MxPalette* palette = NULL; MxPalette* palette = NULL;
switch (this->m_isHighColor) { switch (m_isHighColor) {
case FALSE: case FALSE:
palette = new MxPalette(this->m_paletteData); if (!(palette = new MxPalette(m_paletteData))) {
if (!palette) {
goto done; goto done;
} }
break; break;
case TRUE: case TRUE:
palette = this->m_palette->Clone(); if (!(palette = m_palette->Clone())) {
if (!palette) {
goto done; goto done;
} }
@@ -336,24 +385,24 @@ done:
} }
// FUNCTION: LEGO1 0x100bd280 // FUNCTION: LEGO1 0x100bd280
// FUNCTION: BETA10 0x1013d80e
void MxBitmap::ImportPalette(MxPalette* p_palette) void MxBitmap::ImportPalette(MxPalette* p_palette)
{ {
// Odd to use a switch on a boolean, but it matches. // Odd to use a switch on a boolean, but it matches.
switch (this->m_isHighColor) { switch (m_isHighColor) {
case FALSE: case FALSE:
ImportColorsToPalette(this->m_paletteData, p_palette); ImportColorsToPalette(m_paletteData, p_palette);
break; break;
case TRUE: case TRUE:
if (this->m_palette) { delete m_palette;
delete this->m_palette; m_palette = p_palette->Clone();
}
this->m_palette = p_palette->Clone();
break; break;
} }
} }
// FUNCTION: LEGO1 0x100bd2d0 // FUNCTION: LEGO1 0x100bd2d0
// FUNCTION: BETA10 0x1013d8a9
MxResult MxBitmap::SetBitDepth(MxBool p_isHighColor) MxResult MxBitmap::SetBitDepth(MxBool p_isHighColor)
{ {
MxResult ret = FAILURE; MxResult ret = FAILURE;
@@ -368,17 +417,11 @@ MxResult MxBitmap::SetBitDepth(MxBool p_isHighColor)
switch (p_isHighColor) { switch (p_isHighColor) {
case FALSE: case FALSE:
ImportColorsToPalette(m_paletteData, m_palette); ImportColorsToPalette(m_paletteData, m_palette);
if (m_palette) { delete m_palette;
delete m_palette;
}
m_palette = NULL; m_palette = NULL;
break; break;
case TRUE: { case TRUE: {
pal = NULL; if (!(pal = new MxPalette(m_paletteData))) {
pal = new MxPalette(m_paletteData);
if (!pal) {
goto done; goto done;
} }
@@ -409,6 +452,7 @@ done:
} }
// FUNCTION: LEGO1 0x100bd3e0 // FUNCTION: LEGO1 0x100bd3e0
// FUNCTION: BETA10 0x1013dad2
MxResult MxBitmap::StretchBits( MxResult MxBitmap::StretchBits(
HDC p_hdc, HDC p_hdc,
MxS32 p_xSrc, MxS32 p_xSrc,
@@ -420,8 +464,8 @@ MxResult MxBitmap::StretchBits(
) )
{ {
// Compression fix? // Compression fix?
if ((this->m_bmiHeader->biCompression != BI_RGB_TOPDOWN) && (0 < this->m_bmiHeader->biHeight)) { if (IsBottomUp()) {
p_ySrc = (this->m_bmiHeader->biHeight - p_destHeight) - p_ySrc; p_ySrc = GetBmiHeightAbs() - p_ySrc - p_destHeight;
} }
return StretchDIBits( return StretchDIBits(
@@ -434,14 +478,15 @@ MxResult MxBitmap::StretchBits(
p_ySrc, p_ySrc,
p_destWidth, p_destWidth,
p_destHeight, p_destHeight,
this->m_data, m_data,
(BITMAPINFO*) this->m_info, (BITMAPINFO*) m_info,
this->m_isHighColor, m_isHighColor,
SRCCOPY SRCCOPY
); );
} }
// FUNCTION: LEGO1 0x100bd450 // FUNCTION: LEGO1 0x100bd450
// FUNCTION: BETA10 0x1013db55
MxResult MxBitmap::ImportColorsToPalette(RGBQUAD* p_rgbquad, MxPalette* p_palette) MxResult MxBitmap::ImportColorsToPalette(RGBQUAD* p_rgbquad, MxPalette* p_palette)
{ {
MxResult ret = FAILURE; MxResult ret = FAILURE;