diff --git a/Engine/Source/BLPFile/Types.h b/Engine/Source/BLPFile/Types.h index f285856..8acb6c0 100644 --- a/Engine/Source/BLPFile/Types.h +++ b/Engine/Source/BLPFile/Types.h @@ -4,7 +4,12 @@ #include #include -struct C4Pixel; +struct C4Pixel { + /* 0000 */ uint8_t m_b; + /* 0001 */ uint8_t m_g; + /* 0002 */ uint8_t m_r; + /* 0003 */ uint8_t m_a; +}; struct BlpPalPixel { /* 0000 */ uint8_t m_b; diff --git a/Engine/Source/BlizzardCore/SFile2-Core.cpp b/Engine/Source/BlizzardCore/SFile2-Core.cpp index b873023..0dc044a 100644 --- a/Engine/Source/BlizzardCore/SFile2-Core.cpp +++ b/Engine/Source/BlizzardCore/SFile2-Core.cpp @@ -831,6 +831,15 @@ bool System_SFile2::FindFile(const char* name, char* path, uint32_t size, uint32 return false; } +// VERIFY: 6401cce4 auto +// OFFSET: 0x00424B10 +int32_t STORMAPI SFile::FileExistsEx(const char* name, uint32_t flags) { + char path[260]; + uint32_t type; + + return System_SFile2::FindFile(name, path, sizeof(path), flags, &type, nullptr); +} + // VERIFY: 8c71c618 manual // OFFSET: 0x00424B50 int32_t STORMAPI SFile::OpenEx(SArchive* archive, const char* name, uint32_t flags, SFileImpl** result) { diff --git a/Engine/Source/BlizzardCore/SFile2-Core.h b/Engine/Source/BlizzardCore/SFile2-Core.h index a286592..bda5798 100644 --- a/Engine/Source/BlizzardCore/SFile2-Core.h +++ b/Engine/Source/BlizzardCore/SFile2-Core.h @@ -69,6 +69,7 @@ class SFile { static void DisableSFileCheckDisk(); static int32_t STORMAPI EnableDirectAccess(uint32_t flags); static int32_t STORMAPI FileExists(const char* name); + static int32_t STORMAPI FileExistsEx(const char* name, uint32_t flags); static int32_t STORMAPI FileIsLocal(SFileImpl* file, int32_t flags); static int32_t STORMAPI GetActualFileName(SFileImpl* file, char* buffer, uint32_t size); static int32_t STORMAPI GetBasePath(char* path, uint32_t size); diff --git a/Engine/Source/Images/tga.cpp b/Engine/Source/Images/tga.cpp index 1ca5d7f..e7b7170 100644 --- a/Engine/Source/Images/tga.cpp +++ b/Engine/Source/Images/tga.cpp @@ -1 +1,707 @@ -#include "tga.h" +#include +#include +#include +#include "Engine/Source/Images/tga.h" +#include "Engine/Source/BlizzardCore/SFile2-Core.h" +#include "Engine/Source/Os/OsFile.h" +#include "Engine/Source/Storm/H/storm.h" + +// INLINED +CTgaFile::CTgaFile() { + this->m_file = nullptr; + this->m_image = nullptr; + this->m_addlHeaderData = nullptr; + this->m_imageBytes = 0; + this->m_colorMap = nullptr; +} + +// INLINED +CTgaFile::~CTgaFile() { + this->Close(); +} + +// INLINED +uint32_t CTgaFile::AlphaBits() const { + return this->m_header.m_desc.m_bAlphaChannelBits; +} + +// INLINED +int32_t CTgaFile::BytesPerPixel() const { + return (this->m_header.m_bPixelDepth + 7) / 8; +} + +// INLINED +uint32_t CTgaFile::Height() const { + return this->m_header.m_wHeight; +} + +// INLINED +int32_t CTgaFile::IsTopDown() const { + return this->m_header.m_desc.m_bTopBottomOrder; +} + +// INLINED +uint32_t CTgaFile::Width() const { + return this->m_header.m_wWidth; +} + +// VERIFY: ebdf4fdf manual +// OFFSET: 0x006AA350 +uint32_t CTgaFile::ColorMapEntryBytes() { + int32_t bits = this->m_header.m_bColorMapEntrySize / 3; + + if (bits >= 8) { + bits = 8; + } + + return 3 * bits / 8; +} + +// VERIFY: 473a99ae manual +// OFFSET: 0x006AA380 +uint32_t CTgaFile::ColorMapBytes() { + return this->m_header.m_wColorMapEntries * this->ColorMapEntryBytes(); +} + +// VERIFY: ff3c9ea4 auto +// OFFSET: 0x006AA3B0 +int32_t CTgaFile::ValidateColorDepth() { + if (this->m_header.m_bPixelDepth - this->AlphaBits() == 24) { + return 1; + } + + SErrSetLastError(this->m_header.m_bPixelDepth == 16 ? 0xF720007C : 0xF720007D); + + return 0; +} + +// VERIFY: 9527dbf2 manual +// OFFSET: 0x006AA3E0 +uint32_t CTgaFile::PreImageBytes() { + return sizeof(TGAHeader) + this->m_header.m_bIDLength + this->ColorMapBytes(); +} + +// VERIFY: 624d1043 manual +// OFFSET: 0x006AA420 +void CTgaFile::AddAlphaChannel(uint8_t* dest, uint8_t* source, const uint8_t* alpha) { + uint32_t pixels = this->Width() * this->Height(); + + if (alpha) { + while (pixels--) { + memmove(dest, source, this->BytesPerPixel()); + source += this->BytesPerPixel(); + dest += this->BytesPerPixel(); + *dest++ = *alpha++; + } + } else { + while (pixels--) { + memmove(dest, source, this->BytesPerPixel()); + source += this->BytesPerPixel(); + dest += this->BytesPerPixel(); + *dest++ = 0xFF; + } + } + + uint32_t alphaBits = this->AlphaBits(); + this->m_header.m_desc.m_bAlphaChannelBits = 8; + this->m_header.m_bPixelDepth += 8 - alphaBits; + this->m_imageBytes = this->Width() * this->Height() * this->BytesPerPixel(); +} + +// VERIFY: 2da73a68 manual +// OFFSET: 0x006AA520 +int32_t CTgaFile::ReadRawImage(uint32_t flags) { + int32_t addAlpha = (flags & 1) && !this->AlphaBits() ? 1 : 0; + + if (SFile::SetFilePointer(this->m_file, this->PreImageBytes(), nullptr, 0) == -1) { + return 0; + } + + uint32_t bytes = this->Width() * this->Height() * this->BytesPerPixel(); + this->m_image = static_cast(SMemAlloc(bytes + addAlpha * this->Width() * this->Height(), __FILE__, __LINE__, 0)); + + if (!this->m_image || !SFileReadFile(this->m_file, &this->m_image[addAlpha * this->Width() * this->Height()], bytes, nullptr, nullptr, 0)) { + return 0; + } + + if (addAlpha) { + this->AddAlphaChannel(this->m_image, &this->m_image[this->Width() * this->Height()], nullptr); + } + + return 1; +} + +// VERIFY: 6803203f manual +// OFFSET: 0x006AA630 +int32_t CTgaFile::RLEDecompressImage(uint8_t* rleData, uint8_t* data) { + int32_t pixels = this->Width() * this->Height(); + int32_t bytesPerPixel = this->BytesPerPixel(); + + while (pixels) { + uint8_t packet = *rleData++; + + if (packet & 0x80) { + int32_t count = (packet & 0x7F) + 1; + pixels -= count; + + while (count--) { + memcpy(data, rleData, bytesPerPixel); + data += bytesPerPixel; + } + + rleData += bytesPerPixel; + } else { + int32_t count = packet + 1; + pixels -= count; + memcpy(data, rleData, bytesPerPixel * count); + rleData += bytesPerPixel * count; + data += bytesPerPixel * count; + } + + if (pixels < 0) { + SErrSetLastError(0xF7200077); + return 0; + } + } + + this->m_header.m_bImageType -= 8; + + return 1; +} + +// VERIFY: c8c32a57 manual +// OFFSET: 0x006AA700 +int32_t CTgaFile::SetTopDown(int32_t topDown) { + if (topDown) { + if (this->IsTopDown()) { + return 1; + } + } else if (!this->IsTopDown()) { + return 1; + } + + if (this->m_header.m_bImageType >= 9 && this->m_header.m_bImageType <= 11) { + SErrSetLastError(0xF7200083); + return 0; + } + + uint8_t* image = static_cast(SMemAlloc(this->Width() * this->Height() * this->BytesPerPixel(), __FILE__, __LINE__, 0)); + uint8_t* source = this->m_image; + uint8_t* dest = &image[this->Width() * this->BytesPerPixel() * (this->Height() - 1)]; + + for (uint32_t row = this->Height(); row; row--) { + memcpy(dest, source, this->Width() * this->BytesPerPixel()); + source += this->Width() * this->BytesPerPixel(); + dest -= this->Width() * this->BytesPerPixel(); + } + + SMemFree(this->m_image, __FILE__, __LINE__, 0); + this->m_header.m_desc.m_bTopBottomOrder = topDown != 0; + this->m_image = image; + + return 1; +} + +// VERIFY: e8b28006 auto +// OFFSET: 0x006AA820 +uint8_t* CTgaFile::Image() { + if (!this->m_image) { + SErrSetLastError(0xF720007F); + return nullptr; + } + + return this->m_image; +} + +// VERIFY: 76a8714f auto +// OFFSET: 0x006AA840 +TGA32Pixel* CTgaFile::ImageTGA32Pixel() { + if (!this->m_image) { + SErrSetLastError(0xF720007F); + return nullptr; + } + + if (this->m_header.m_bPixelDepth != 32) { + SErrSetLastError(0xF720007D); + return nullptr; + } + + return reinterpret_cast(this->m_image); +} + +// VERIFY: 186ad0ea manual +// OFFSET: 0x006AA870 +int32_t CTgaFile::RemoveAlphaChannels() { + if (!this->m_image) { + SErrSetLastError(0xF720007F); + return 0; + } + + if (!this->AlphaBits()) { + if (this->m_header.m_bPixelDepth == 24) { + return 1; + } + + SErrSetLastError(0x8720012D); + this->m_header.m_desc.m_bAlphaChannelBits = this->m_header.m_bPixelDepth - 8; + } + + if (this->AlphaBits() != 8) { + SErrSetLastError(0xF7200082); + return 0; + } + + this->m_header.m_bPixelDepth -= 8; + this->m_header.m_desc.m_bAlphaChannelBits = 0; + + uint32_t pixels = this->Width() * this->Height(); + uint8_t* dest = this->m_image; + uint8_t* source = this->m_image; + + while (pixels--) { + memmove(dest, source, this->BytesPerPixel()); + dest += this->BytesPerPixel(); + source += this->BytesPerPixel() + 1; + } + + return 1; +} + +// VERIFY: 57c94d27 manual +// OFFSET: 0x006AA950 +int32_t CTgaFile::SetImage(const void* bits, uint16_t width, uint16_t height, uint8_t pixelDepth, uint8_t alphaBits, int32_t topBottomOrder, int32_t leftRightOrder) { + if (!bits || (pixelDepth != 32 && pixelDepth != 24) || (alphaBits && alphaBits != 8)) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + memset(&this->m_header, 0, sizeof(this->m_header)); + this->m_header.m_wHeight = height; + this->m_header.m_desc.m_bAlphaChannelBits = alphaBits; + this->m_header.m_desc.m_bLeftRightOrder = leftRightOrder != 0; + this->m_header.m_desc.m_bTopBottomOrder = topBottomOrder != 0; + this->m_header.m_bPixelDepth = pixelDepth; + this->m_header.m_wWidth = width; + this->m_imageBytes = width * height * ((pixelDepth + 7) / 8); + this->m_header.m_bImageType = 2; + + if (this->m_image) { + SMemFree(this->m_image, __FILE__, __LINE__, 0); + } + + this->m_image = static_cast(SMemAlloc(this->m_imageBytes, __FILE__, __LINE__, 0)); + + if (!this->m_image) { + return 0; + } + + memcpy(this->m_image, bits, this->m_imageBytes); + this->m_footer.m_dwExtensionOffset = 0; + this->m_footer.m_dwDeveloperOffset = 0; + SStrCopy(this->m_footer.m_szSigniture, "TRUEVISION-XFILE.", sizeof(this->m_footer.m_szSigniture)); + + return 1; +} + +// VERIFY: 10a5f209 manual +// OFFSET: 0x006AAA70 +int32_t CTgaFile::CountRun(const uint8_t* source, int32_t count) { + int32_t bytesPerPixel = this->BytesPerPixel(); + int32_t run = 0; + uint32_t pixel = 0; + memcpy(&pixel, source, bytesPerPixel); + + if (count > 128) { + count = 128; + } + + while (count--) { + if (memcmp(source, &pixel, bytesPerPixel)) { + break; + } + + source += bytesPerPixel; + run++; + } + + return run; +} + +// VERIFY: 4aec56da manual +// OFFSET: 0x006AAB70 +int32_t CTgaFile::RleCompressLine(uint8_t** source, uint8_t** dest) { + uint8_t* in = *source; + uint8_t* out = *dest; + uint8_t* rawPacket = nullptr; + int32_t remaining = this->Width(); + + while (remaining) { + int32_t run = this->CountRun(in, remaining); + + if (run >= 2) { + this->m_imageBytes += this->BytesPerPixel() + 1; + rawPacket = nullptr; + + if (this->m_imageBytes >= this->BytesPerPixel() * this->Width() * this->Height()) { + return 0; + } + + *out++ = (run - 1) | 0x80; + memcpy(out, in, this->BytesPerPixel()); + out += this->BytesPerPixel(); + in += run * this->BytesPerPixel(); + remaining -= run; + } else { + if (!rawPacket || *rawPacket == 127) { + rawPacket = out; + *out++ = 0; + this->m_imageBytes++; + } else { + (*rawPacket)++; + } + + this->m_imageBytes += this->BytesPerPixel(); + + if (this->m_imageBytes >= this->BytesPerPixel() * this->Width() * this->Height()) { + return 0; + } + + memcpy(out, in, this->BytesPerPixel()); + out += this->BytesPerPixel(); + remaining--; + in += this->BytesPerPixel(); + } + } + + *source = in; + *dest = out; + + return 1; +} + +// VERIFY: 37799519 manual +// OFFSET: 0x006AACE0 +int32_t CTgaFile::Compress() { + if (!this->m_image) { + SErrSetLastError(0xF7200081); + return 0; + } + + if (this->m_header.m_bImageType >= 9) { + SErrSetLastError(0xF7200083); + return 0; + } + + uint8_t* source = this->m_image; + uint8_t* compressed = static_cast(SMemAlloc(this->m_imageBytes, __FILE__, __LINE__, 0)); + uint8_t* dest = compressed; + + if (!compressed) { + return 0; + } + + this->m_imageBytes = 0; + + for (uint32_t row = this->Height(); row--; ) { + if (!this->RleCompressLine(&source, &dest)) { + SMemFree(compressed, __FILE__, __LINE__, 0); + this->m_imageBytes = this->Height() * this->Width() * this->BytesPerPixel(); + return 1; + } + } + + this->m_header.m_bImageType += 8; + SMemFree(this->m_image, __FILE__, __LINE__, 0); + this->m_image = compressed; + + return 1; +} + +// VERIFY: 6d8bf332 manual +// OFFSET: 0x006AADE0 +int32_t CTgaFile::Write(const char* fileName) { + if (!fileName) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + if (!this->m_image) { + SErrSetLastError(0xF7200081); + return 0; + } + + HOSFILE file = OsCreateFile(fileName, OS_GENERIC_WRITE, 0, OS_CREATE_ALWAYS, OS_FILE_ATTRIBUTE_NORMAL, OS_FILE_TYPE_ANY); + + if (file == OS_INVALID_FILE) { + return 0; + } + + int32_t result = 0; + uint32_t written; + + if (OsWriteFile(file, &this->m_header, sizeof(this->m_header), &written) && written == sizeof(this->m_header) + && (!this->m_header.m_bIDLength || (OsWriteFile(file, this->m_addlHeaderData, this->m_header.m_bIDLength, &written) && written == this->m_header.m_bIDLength)) + && (!this->m_header.m_bColorMapType || (OsWriteFile(file, this->m_colorMap, this->ColorMapBytes(), &written) && written == this->ColorMapBytes())) + && OsWriteFile(file, this->m_image, this->m_imageBytes, &written) && written == this->m_imageBytes + && OsWriteFile(file, &this->m_footer, sizeof(this->m_footer), &written) && written == sizeof(this->m_footer)) { + result = 1; + } + + OsCloseFile(file); + + if (!result) { + OsDeleteFile(fileName); + } + + return result; +} + +// VERIFY: 7fb86d57 manual +// OFFSET: 0x006AAF40 +void CTgaFile::Close() { + if (this->m_image) { + SMemFree(this->m_image, __FILE__, __LINE__, 0); + } + + this->m_image = nullptr; + + if (this->m_file) { + SFileCloseFile(this->m_file); + } + + this->m_file = nullptr; + + if (this->m_addlHeaderData) { + SMemFree(this->m_addlHeaderData, __FILE__, __LINE__, 0); + } + + this->m_addlHeaderData = nullptr; + this->m_header.m_bIDLength = 0; + + if (this->m_colorMap) { + SMemFree(this->m_colorMap, __FILE__, __LINE__, 0); + } + + this->m_colorMap = nullptr; +} + +// VERIFY: 3fd6ee84 manual +// OFFSET: 0x006AAFB0 +int32_t CTgaFile::Open(const char* fileName, int32_t flags) { + if (!fileName || !*fileName) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + this->Close(); + + if (!SFile::OpenEx(nullptr, fileName, flags != 0, &this->m_file)) { + return 0; + } + + if (!SFileReadFile(this->m_file, &this->m_header, sizeof(this->m_header), nullptr, nullptr, 0)) { + return 0; + } + + if (this->m_header.m_bIDLength) { + this->m_addlHeaderData = static_cast(SMemAlloc(this->m_header.m_bIDLength, __FILE__, __LINE__, 0)); + + if (!SFileReadFile(this->m_file, this->m_addlHeaderData, this->m_header.m_bIDLength, nullptr, nullptr, 0)) { + return 0; + } + } else { + this->m_addlHeaderData = nullptr; + } + + if (this->m_header.m_bColorMapType) { + this->m_colorMap = static_cast(SMemAlloc(this->ColorMapBytes(), __FILE__, __LINE__, 0)); + + if (!SFileReadFile(this->m_file, this->m_colorMap, this->ColorMapBytes(), nullptr, nullptr, 0)) { + return 0; + } + } else { + this->m_colorMap = nullptr; + } + + if (this->m_header.m_bPixelDepth == 24 && this->AlphaBits() == 8) { + this->m_header.m_desc.m_bAlphaChannelBits = 0; + SErrSetLastError(0x8720012E); + } + + if (this->m_header.m_bPixelDepth == 32 && !this->AlphaBits()) { + this->m_header.m_desc.m_bAlphaChannelBits = 8; + SErrSetLastError(0x8720012F); + } + + return 1; +} + +// VERIFY: e42758f1 manual +// OFFSET: 0x006AB0E0 +void CTgaFile::ConvertColorMapped(uint32_t flags) { + uint8_t* indices = this->m_image; + uint32_t pixelDepth = this->AlphaBits() + 24; + uint32_t addAlpha = flags & 1; + this->m_image = static_cast(SMemAlloc(addAlpha * this->Width() * this->Height() + this->Width() * this->Height() * (pixelDepth >> 3), __FILE__, __LINE__, 0)); + + uint8_t* dest = &this->m_image[addAlpha * this->Width() * this->Height()]; + uint8_t* index = indices; + + for (uint32_t pixels = this->Width() * this->Height(); pixels; pixels--) { + memcpy(dest, &this->m_colorMap[(*index++ - this->m_header.m_wColorMapStartIndex) * this->ColorMapEntryBytes()], this->ColorMapEntryBytes()); + dest += this->ColorMapEntryBytes(); + } + + SMemFree(indices, __FILE__, __LINE__, 0); + SMemFree(this->m_colorMap, __FILE__, __LINE__, 0); + this->m_colorMap = nullptr; + this->m_header.m_wColorMapEntries = 0; + this->m_header.m_bColorMapType = 0; + this->m_header.m_bImageType = 2; + this->m_header.m_bPixelDepth = pixelDepth; + this->m_imageBytes = this->Width() * this->Height() * ((pixelDepth + 7) / 8); + + if (addAlpha) { + this->AddAlphaChannel(this->m_image, &this->m_image[this->Width() * this->Height()], nullptr); + } +} + +// VERIFY: e3c0914e manual +// OFFSET: 0x006AB220 +int32_t CTgaFile::ReadRleImage(uint32_t flags) { + int32_t addAlpha = (flags & 1) && !this->AlphaBits() ? 1 : 0; + this->m_image = static_cast(SMemAlloc(this->Height() * this->Width() * this->BytesPerPixel() + addAlpha * this->Height() * this->Width(), __FILE__, __LINE__, 0)); + + if (!this->m_image) { + return 0; + } + + uint32_t bytes = SFileGetFileSize(this->m_file, nullptr) - this->PreImageBytes(); + + if (bytes == 0xFFFFFFFF) { + return 0; + } + + uint8_t* rleData = static_cast(SMemAlloc(bytes, __FILE__, __LINE__, 0)); + + if (!rleData) { + return 0; + } + + if (SFile::SetFilePointer(this->m_file, this->PreImageBytes(), nullptr, 0) == -1) { + return 0; + } + + if (!SFileReadFile(this->m_file, rleData, bytes, nullptr, nullptr, 0)) { + return 0; + } + + int32_t result = this->RLEDecompressImage(rleData, &this->m_image[addAlpha * this->Height() * this->Width()]); + SMemFree(rleData, __FILE__, __LINE__, 0); + + if (!result) { + return 0; + } + + if (addAlpha) { + this->AddAlphaChannel(this->m_image, &this->m_image[this->Height() * this->Width()], nullptr); + } + + return 1; +} + +// VERIFY: a86156e1 manual +// OFFSET: 0x006AB390 +int32_t CTgaFile::AddAlphaChannel(const void* alpha) { + if (!this->m_image) { + SErrSetLastError(0xF720007F); + return 0; + } + + if (this->m_header.m_bImageType >= 9 && this->m_header.m_bImageType <= 11) { + SErrSetLastError(0xF7200083); + return 0; + } + + if (this->AlphaBits()) { + this->RemoveAlphaChannels(); + } + + uint8_t* image = static_cast(SMemAlloc(this->Width() * this->Height() * (this->BytesPerPixel() + 1), __FILE__, __LINE__, 0)); + + if (!image) { + return 0; + } + + this->AddAlphaChannel(image, this->m_image, static_cast(alpha)); + SMemFree(this->m_image, __FILE__, __LINE__, 0); + this->m_image = image; + + return 1; +} + +// VERIFY: 94d6cd9e auto +// OFFSET: 0x006AB450 +int32_t CTgaFile::ReadColorMappedImage(uint32_t flags) { + if (!this->m_header.m_bColorMapType) { + SErrSetLastError(0xF7200084); + return 0; + } + + int32_t result = this->m_header.m_bImageType >= 9 ? this->ReadRleImage(0) : this->ReadRawImage(0); + + if (this->m_header.m_bColorMapType && (flags & 2)) { + this->ConvertColorMapped(flags); + } + + return result; +} + +// VERIFY: 68530b8f manual +// OFFSET: 0x006AB4B0 +int32_t CTgaFile::LoadImageData(uint32_t flags) { + if (this->m_image) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 1; + } + + if (!this->m_file) { + SErrSetLastError(0xF720007E); + return 0; + } + + this->m_imageBytes = this->Width() * this->Height() * this->BytesPerPixel(); + + switch (this->m_header.m_bImageType) { + case 0: + SErrSetLastError(0xF7200078); + return 0; + + case 1: + case 9: + return this->ReadColorMappedImage(flags); + + case 2: + if (!this->ValidateColorDepth()) { + return 0; + } + + return this->ReadRawImage(flags); + + case 3: + case 11: + SErrSetLastError(0xF720007A); + return 0; + + case 10: + if (!this->ValidateColorDepth()) { + return 0; + } + + return this->ReadRleImage(flags); + + default: + SErrSetLastError(0xF720007B); + return 0; + } +} diff --git a/Engine/Source/Images/tga.h b/Engine/Source/Images/tga.h index 1ce3519..53ed514 100644 --- a/Engine/Source/Images/tga.h +++ b/Engine/Source/Images/tga.h @@ -4,41 +4,13 @@ #include #include -class SFile; +class SFileImpl; -struct TGAFooter { - /* 0000 */ uint32_t m_dwExtensionOffset; - /* 0004 */ uint32_t m_dwDeveloperOffset; - /* 0008 */ uint8_t m_szSigniture [18]; - /* 001A */ uint8_t unk_001A[2]; -}; - -struct TGAHeader { - /* 0000 */ uint8_t m_bIDLength; - /* 0001 */ uint8_t m_bColorMapType; - /* 0002 */ uint8_t m_bImageType; - /* 0003 */ uint8_t unk_0003[1]; - /* 0004 */ uint16_t m_wColorMapStartIndex; - /* 0006 */ uint16_t m_wColorMapEntries; - /* 0008 */ uint8_t m_bColorMapEntrySize; - /* 0009 */ uint8_t unk_0009[1]; - /* 000A */ uint16_t m_wXOrigin; - /* 000C */ uint16_t m_wYOrigin; - /* 000E */ uint16_t m_wWidth; - /* 0010 */ uint16_t m_wHeight; - /* 0012 */ uint8_t m_bPixelDepth; - /* 0013 */ uint8_t unk_0013[1]; -}; - -class CTgaFile { - public: - /* 0000 */ SFile* m_file; - /* 0004 */ uint8_t* m_image; - /* 0008 */ TGAHeader m_header; - /* 001C */ uint8_t* m_addlHeaderData; - /* 0020 */ TGAFooter m_footer; - /* 003C */ uint32_t m_imageBytes; - /* 0040 */ uint8_t* m_colorMap; +struct TGA32Pixel { + /* 0000 */ uint8_t m_b; + /* 0001 */ uint8_t m_g; + /* 0002 */ uint8_t m_r; + /* 0003 */ uint8_t m_a; }; struct TGAImageDesc { @@ -48,4 +20,71 @@ struct TGAImageDesc { /* 0000 */ uint8_t m_bReserved : 2; }; +#pragma pack(push, 1) + +struct TGAHeader { + /* 0000 */ uint8_t m_bIDLength; + /* 0001 */ uint8_t m_bColorMapType; + /* 0002 */ uint8_t m_bImageType; + /* 0003 */ uint16_t m_wColorMapStartIndex; + /* 0005 */ uint16_t m_wColorMapEntries; + /* 0007 */ uint8_t m_bColorMapEntrySize; + /* 0008 */ uint16_t m_wXOrigin; + /* 000A */ uint16_t m_wYOrigin; + /* 000C */ uint16_t m_wWidth; + /* 000E */ uint16_t m_wHeight; + /* 0010 */ uint8_t m_bPixelDepth; + /* 0011 */ TGAImageDesc m_desc; +}; + +struct TGAFooter { + /* 0000 */ uint32_t m_dwExtensionOffset; + /* 0004 */ uint32_t m_dwDeveloperOffset; + /* 0008 */ char m_szSigniture[18]; +}; + +#pragma pack(pop) + +class CTgaFile { + public: + /* 0000 */ SFileImpl* m_file; + /* 0004 */ uint8_t* m_image; + /* 0008 */ TGAHeader m_header; + /* 001C */ uint8_t* m_addlHeaderData; + /* 0020 */ TGAFooter m_footer; + /* 003C */ uint32_t m_imageBytes; + /* 0040 */ uint8_t* m_colorMap; + + CTgaFile(); + ~CTgaFile(); + int32_t AddAlphaChannel(const void* alpha); + void AddAlphaChannel(uint8_t* dest, uint8_t* source, const uint8_t* alpha); + uint32_t AlphaBits() const; + int32_t BytesPerPixel() const; + void Close(); + uint32_t ColorMapBytes(); + uint32_t ColorMapEntryBytes(); + int32_t Compress(); + void ConvertColorMapped(uint32_t flags); + int32_t CountRun(const uint8_t* source, int32_t count); + uint32_t Height() const; + uint8_t* Image(); + TGA32Pixel* ImageTGA32Pixel(); + int32_t IsTopDown() const; + int32_t LoadImageData(uint32_t flags); + int32_t Open(const char* fileName, int32_t flags); + uint32_t PreImageBytes(); + int32_t ReadColorMappedImage(uint32_t flags); + int32_t ReadRawImage(uint32_t flags); + int32_t ReadRleImage(uint32_t flags); + int32_t RemoveAlphaChannels(); + int32_t RleCompressLine(uint8_t** source, uint8_t** dest); + int32_t RLEDecompressImage(uint8_t* rleData, uint8_t* data); + int32_t SetImage(const void* bits, uint16_t width, uint16_t height, uint8_t pixelDepth, uint8_t alphaBits, int32_t topBottomOrder, int32_t leftRightOrder); + int32_t SetTopDown(int32_t topDown); + int32_t ValidateColorDepth(); + uint32_t Width() const; + int32_t Write(const char* fileName); +}; + #endif diff --git a/Engine/Source/Os/OsFile.h b/Engine/Source/Os/OsFile.h index 45b7719..9733792 100644 --- a/Engine/Source/Os/OsFile.h +++ b/Engine/Source/Os/OsFile.h @@ -7,6 +7,12 @@ struct HOSFILE__; typedef HOSFILE__* HOSFILE; +#define OS_CREATE_ALWAYS 2 +#define OS_FILE_ATTRIBUTE_NORMAL 0x80 +#define OS_FILE_TYPE_ANY 0x3F3F3F3F +#define OS_GENERIC_WRITE 0x40000000 +#define OS_INVALID_FILE reinterpret_cast(static_cast(-1)) + struct OS_FILE_DATA { /* 0000 */ uint32_t size; /* 0004 */ uint32_t flags; diff --git a/Engine/Source/Services/Texture.cpp b/Engine/Source/Services/Texture.cpp index f03fd88..d39a63e 100644 --- a/Engine/Source/Services/Texture.cpp +++ b/Engine/Source/Services/Texture.cpp @@ -8,10 +8,12 @@ #include "Engine/Source/Gx/CGxDevice/CGxDevice.h" #include "Engine/Source/Gx/Gx.h" #include "Engine/Source/Gxu/Gxu.h" +#include "Engine/Source/Images/tga.h" #include "Engine/Source/Os/TimeManager.h" #include "Engine/Source/Services/AsyncFile.h" #include "Engine/Source/Services/SysMessage.h" #include "Engine/Source/Services/TextureBlob.h" +#include "Engine/Source/Services/TextureInt.h" #include "Engine/Source/Storm/H/storm.h" #include "_Triage/MippedImg.h" @@ -21,6 +23,9 @@ static const uint16_t s_texFormatBitDepth[GxTexFormats_Last] = { 0, 32, 32, 16, // STATIC: 0x009F1058 static const uint16_t s_texFormatBytesPerBlock[GxTexFormats_Last] = { 0, 0, 0, 0, 0, 0, 8, 16, 16, 0, 0, 0, 0 }; +// STATIC: 0x009F1074 +static const int32_t s_pixelFormatToMipBitsCache[NUM_PIXEL_FORMATS] = { -1, -1, -1, 0, 0, 0, -1, -1, -1, 1 }; + // STATIC: 0x009F1120 static const uint32_t s_texFormatBytesPerTexel[GxTexFormats_Last] = { 0, 4, 4, 2, 2, 2, 8, 16, 16, 2, 4, 4, 4 }; @@ -48,6 +53,9 @@ static uint32_t s_defaultColor = 0xFF00FF00; // STATIC: 0x00AC3358 static TSExplicitList s_gxTexCacheFree; +// STATIC: 0x00AC3364 +static TSExplicitList s_mipBitsCacheFree; + // STATIC: 0x00AC3370 static TSExplicitList s_atlasList; @@ -90,6 +98,9 @@ static TSHashTableReuse s_textureCache; // STATIC: 0x00B49CD8 static TSExplicitList s_gxTexCache[6][6]; +// STATIC: 0x00B49E88 +static TSExplicitList s_mipBitsCache[6][6][2]; + // INLINED HASHKEY_TEXTUREFILE::HASHKEY_TEXTUREFILE() { this->m_filename = nullptr; @@ -101,11 +112,29 @@ bool HASHKEY_TEXTUREFILE::operator==(const HASHKEY_TEXTUREFILE& key) const { return !SStrCmpI(this->m_filename, key.m_filename, 260) && (this->m_anyTexFlags || key.m_anyTexFlags || this->m_texFlags == key.m_texFlags); } +// INLINED +CMipBitsCache::CMipBitsCache() { + this->m_bits = nullptr; +} + +// INLINED +CMipBitsCache::~CMipBitsCache() { + if (this->m_bits) { + SMemFree(this->m_bits, __FILE__, __LINE__, 0); + } +} + // VERIFY: c74642f9 manual // OFFSET: 0x005EEB70 void GxTexNullUserFunc(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t face, uint32_t level, void* userArg, uint32_t& texelStrideInBytes, const void*& texels) { } +// VERIFY: 9d282c43 auto +// OFFSET: 0x004B50C0 +EGxTexFormat TexturePixelFormatToGxTexFormat(PIXEL_FORMAT format) { + return s_pixelFormatToGxTexFormat[format]; +} + // VERIFY: 10032e81 auto // OFFSET: 0x004B50D0 void CTextureAtlas::Update(CTexture* texture) { @@ -202,6 +231,89 @@ void AsyncTextureFailureCallback(CTexture* texture) { SMemFree(buffer, __FILE__, __LINE__, 0); } +// VERIFY: 83041e31 auto +// OFFSET: 0x004B5340 +SFileImpl* TextureCreateOpenFile(const char* fileName, int32_t flags) { + SFileImpl* file = nullptr; + SErrSetLastError(0); + + if (SFile::OpenEx(nullptr, fileName, flags != 0, &file)) { + return file; + } + + if (!SErrGetLastError()) { + SErrSetLastError(STORM_ERROR_FILE_NOT_FOUND); + } + + return nullptr; +} + +// VERIFY: 693ca984 manual +// OFFSET: 0x004B5510 +int32_t TextureCalcMipCount(uint32_t width, uint32_t height) { + int32_t count = 1; + + while (width > 1 || height > 1) { + width >>= 1; + count++; + + if (!width) { + width = 1; + } + + height >>= 1; + + if (!height) { + height = 1; + } + } + + return count; +} + +// VERIFY: 2866c66e manual +// OFFSET: 0x004B5550 +void TextureGenerateMips(uint32_t width, uint32_t height, uint32_t firstLevel, uint32_t levelCount, MipBits* images) { + uint32_t sourceWidth = width; + uint32_t sourceHeight = height; + uint32_t sourceLevel = 0; + + if (firstLevel) { + sourceWidth = width >> (firstLevel - 1); + sourceHeight = height >> (firstLevel - 1); + sourceLevel = firstLevel - 1; + } + + for (uint32_t level = 1; level < levelCount; level++) { + width = width >> 1 ? width >> 1 : 1; + height = height >> 1 ? height >> 1 : 1; + + if (level >= firstLevel) { + FullShrink(images->m_mip[level], width, height, images->m_mip[sourceLevel], sourceWidth, sourceHeight); + } + } +} + +// VERIFY: 806f5e3e manual +// OFFSET: 0x004B55E0 +void TextureFreeMippedImg(MipBits* bits) { + if (bits) { + SMemFree(bits, __FILE__, __LINE__, 0); + } +} + +// VERIFY: 4ee5ff1e auto +// OFFSET: 0x004B5780 +int32_t TextureLoadBlob(const char* fileName) { + return CTextureBlob::Load(fileName); +} + +// VERIFY: 669249b5 auto +// OFFSET: 0x004B5790 +int32_t TextureUnloadBlob(const char* fileName) { + return CTextureBlob::Unload(fileName); +} + // VERIFY: 762ce4de manual // OFFSET: 0x004B5930 void ResolveMipLevel(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t face, uint32_t level, void* userArg, uint32_t& texelStrideInBytes, const void*& texels) { @@ -246,6 +358,64 @@ void ResolveMipLevel(EGxTexCommand command, uint32_t width, uint32_t height, uin } } +// VERIFY: 30b88d54 manual +// OFFSET: 0x004B5A00 +uint32_t LoadPredrawnMips(CTgaFile* image, const char* format, int32_t flags, MipBits* images) { + uint32_t level = 1; + uint32_t count = TextureCalcMipCount(image->Width(), image->Height()) - 1; + uint32_t width = image->Width() >> 1; + uint32_t height = image->Height() >> 1; + + if (!count) { + return level; + } + + int32_t onDisk = flags != 0; + + do { + char fileName[260]; + SStrPrintf(fileName, sizeof(fileName), format, level); + + if (!SFile::FileExistsEx(fileName, onDisk)) { + break; + } + + CTgaFile mip; + + if (!mip.Open(fileName, flags) || width != mip.Width() || height != mip.Height() || !mip.LoadImageData(2)) { + return level; + } + + if (!mip.AlphaBits()) { + mip.AddAlphaChannel(nullptr); + } + + mip.SetTopDown(1); + + const TGA32Pixel* source = mip.ImageTGA32Pixel(); + C4Pixel* dest = images->m_mip[level++]; + + for (uint32_t pixels = width * height; pixels; pixels--) { + dest->m_b = source->m_b; + dest->m_g = source->m_g; + dest->m_r = source->m_r; + dest->m_a = source->m_a; + dest++; + source++; + } + + if (width > 1) { + width >>= 1; + } + + if (height > 1) { + height >>= 1; + } + } while (--count); + + return level; +} + // VERIFY: 7a812510 auto // OFFSET: 0x004B5BB0 void RequestImageDimensions(uint32_t* width, uint32_t* height, uint32_t* bestMip) { @@ -955,6 +1125,91 @@ int32_t TextureUpdate(const void* data, void* param) { return 1; } +// VERIFY: fea7fb26 manual +// OFFSET: 0x004B7220 +MipBits* TextureAllocMippedImg(PIXEL_FORMAT format, uint32_t width, uint32_t height) { + int32_t cache = s_pixelFormatToMipBitsCache[format]; + + if (width < 8 || height < 8 || width > 256 || height > 256 || cache == -1) { + return MippedImgAllocA(format, width, height, __FILE__, __LINE__); + } + + uint32_t w = width >> 3; + uint32_t h = height >> 3; + uint32_t x = 0; + uint32_t y = 0; + + if (!(width & 8)) { + do { + w >>= 1; + x++; + } while (!(w & 1)); + } + + if (!(height & 8)) { + do { + h >>= 1; + y++; + } while (!(h & 1)); + } + + CMipBitsCache* entry = s_mipBitsCache[x][y][cache].Head(); + + if (!entry) { + return MippedImgAllocA(format, width, height, __FILE__, __LINE__); + } + + entry->m_link.Unlink(); + MipBits* bits = entry->m_bits; + entry->m_bits = nullptr; + s_mipBitsCacheFree.LinkToTail(entry); + + return bits; +} + +// VERIFY: 8f6a0308 manual +// OFFSET: 0x004B7300 +void TextureFreeMippedImg(MipBits* bits, PIXEL_FORMAT format, uint32_t width, uint32_t height) { + int32_t cache = s_pixelFormatToMipBitsCache[format]; + + if (width < 8 || height < 8 || width > 256 || height > 256 || cache == -1) { + if (bits) { + SMemFree(bits, __FILE__, __LINE__, 0); + } + + return; + } + + uint32_t w = width >> 3; + uint32_t h = height >> 3; + uint32_t x = 0; + uint32_t y = 0; + + if (!(width & 8)) { + do { + w >>= 1; + x++; + } while (!(w & 1)); + } + + if (!(height & 8)) { + do { + h >>= 1; + y++; + } while (!(h & 1)); + } + + CMipBitsCache* entry = s_mipBitsCacheFree.Head(); + + if (!entry) { + entry = s_mipBitsCacheFree.NewNode(2, 0, 0); + entry->m_link.Unlink(); + } + + entry->m_bits = bits; + s_mipBitsCache[x][y][cache].LinkToTail(entry); +} + // VERIFY: a81fc586 manual // OFFSET: 0x004B76E0 CTextureAtlas::~CTextureAtlas() { @@ -1015,6 +1270,155 @@ CTextureAtlas* CTextureAtlas::Get(CTexture* texture) { return atlas; } +// VERIFY: b9a802ce manual +// OFFSET: 0x004B78A0 +int32_t LoadTgaMips(char* fileName, char* extension, int32_t flags, MipBits** images, uint32_t* width, uint32_t* height, EGxTexFormat* gxTexFormat, int32_t* opaque, uint32_t* alphaBits, PIXEL_FORMAT* pixelFormat) { + if (extension) { + strcpy(extension, ".tga"); + } + + CTgaFile image; + + if (!image.Open(fileName, flags)) { + return 0; + } + + if (opaque) { + *opaque = image.AlphaBits() == 0; + } + + if (!image.LoadImageData(3)) { + return 0; + } + + image.SetTopDown(1); + + uint32_t imageWidth = image.Width(); + uint32_t imageHeight = image.Height(); + uint32_t mipCount = TextureCalcMipCount(imageWidth, imageHeight); + + if (*images) { + MippedImgSet(PIXEL_ARGB8888, imageWidth, imageHeight, *images); + } else { + *images = TextureAllocMippedImg(PIXEL_ARGB8888, imageWidth, imageHeight); + } + + const TGA32Pixel* source = image.ImageTGA32Pixel(); + C4Pixel* dest = (*images)->m_mip[0]; + + for (uint32_t pixels = imageWidth * imageHeight; pixels; pixels--) { + dest->m_b = source->m_b; + dest->m_g = source->m_g; + dest->m_r = source->m_r; + dest->m_a = source->m_a; + dest++; + source++; + } + + image.Close(); + + char mipName[260]; + SStrCopy(mipName, fileName, sizeof(mipName)); + char* dot = SStrChrR(mipName, '.'); + + if (dot) { + *dot = '\0'; + } + + SStrPack(mipName, "_mip%d.tga", sizeof(mipName)); + uint32_t predrawn = LoadPredrawnMips(&image, mipName, flags, *images); + TextureGenerateMips(image.Width(), image.Height(), predrawn, mipCount, *images); + + if (width) { + *width = image.Width(); + } + + if (height) { + *height = image.Height(); + } + + if (gxTexFormat) { + *gxTexFormat = GxTex_Argb8888; + } + + if (alphaBits) { + *alphaBits = image.AlphaBits(); + } + + if (pixelFormat) { + *pixelFormat = PIXEL_ARGB8888; + } + + return 1; +} + +// VERIFY: 8ee83473 manual +// OFFSET: 0x004B7AA0 +void UpdateTgaTexture(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t face, uint32_t level, void* userArg, uint32_t& texelStrideInBytes, const void*& texels) { + CTexture* texture = static_cast(userArg); + + switch (command) { + case GxTex_Lock: + if (!s_pumping) { + char* extension = texture->m_filename + SStrLen(texture->m_filename); + int32_t result = LoadTgaMips(texture->m_filename, extension, texture->m_flags & 2, &s_mipBits, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr); + *extension = '\0'; + + if (!result) { + GetDefaultTexture(width, height); + GetGlobalStatusObj().Add(STATUS_ERROR, "Texture %s not loaded -- replaced with default.\n", texture->m_filename); + } + } + + break; + + case GxTex_Latch: { + texelStrideInBytes = 4 * width; + + const uint8_t* bits = reinterpret_cast(s_mipBits[level].m_mip[0]); + texels = bits; + + if (texture->m_gxTexTarget == GxTex_CubeMap) { + uint32_t order = face; + + switch (face) { + case 0: + order = 0; + break; + + case 1: + order = 2; + break; + + case 2: + order = 4; + break; + + case 3: + order = 5; + break; + + case 4: + order = 3; + break; + + case 5: + order = 1; + break; + } + + texels = bits + order * texelStrideInBytes; + texelStrideInBytes *= 6; + } + + break; + } + + default: + break; + } +} + // VERIFY: c0a503e5 manual // OFFSET: 0x004B7BD0 int32_t PumpBlpTextureAsync(CTexture* texture, void* data) { @@ -1368,6 +1772,56 @@ CTexture* CreateBlpTexture(const char* fileName, char* extension, uint32_t flags return reinterpret_cast(HandleCreate(texture, "HTEXTURE")); } +// VERIFY: 42c19e46 manual +// OFFSET: 0x004B8D70 +CTexture* TextureCreateBlob(char* fileName, CGxTexFlags texFlags, CStatus* status, int32_t defaultFilter) { + if (!fileName) { + DebugValidate("fileName"); + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return nullptr; + } + + if (!fileName[0]) { + DebugValidate("fileName[0]"); + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return nullptr; + } + + if (!status) { + DebugValidate("status"); + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return nullptr; + } + + if (defaultFilter) { + texFlags.m_filter = s_filterMode; + } + + texFlags.m_maxAnisotropy = texFlags.m_filter == GxTex_Anisotropic ? s_maxAnisotropy : 1; + char* extension = OsPathFindExtensionWithDot(fileName); + + if (extension) { + *extension = '\0'; + } + + CTextureBlobTexture* blobTexture = CTextureBlob::GetTexture(fileName); + CTexture* handle = nullptr; + + if (blobTexture) { + CTexture* texture = new ("HTEXTURE", SLOG_OBJECT) CTexture; + texture->m_gxTexFlags = texFlags; + SStrCopy(texture->m_filename, fileName, STORM_MAX_STR); + CTextureBlob::CreateTexture(texture, blobTexture); + handle = reinterpret_cast(HandleCreate(texture, "HTEXTURE")); + } + + if (extension) { + *extension = '.'; + } + + return handle; +} + // VERIFY: 07bcfe26 manual // OFFSET: 0x004B9480 void TextureCacheNewTexture(CTexture* texture, CGxTexFlags texFlags) { @@ -1410,6 +1864,69 @@ CTexture* TextureCreateSolid(const CImVector& color) { return handle; } +// VERIFY: ea203deb manual +// OFFSET: 0x004B95B0 +CTexture* CreateTgaTexture(const char* fileName, char* extension, int32_t flags, CGxTexFlags texFlags, CStatus* status) { + if (extension) { + SStrCopy(extension, ".tga", STORM_MAX_STR); + } + + CTgaFile image; + + if (!image.Open(fileName, flags)) { + return nullptr; + } + + uint32_t width = image.Width(); + uint32_t height = image.Height(); + EGxTexTarget target = GxTex_2d; + + if (width == 6 * height) { + target = GxTex_CubeMap; + width /= 6; + } + + if (width < 8 || height < 8) { + status->Add(STATUS_FATAL, "Error loading file \"%s\": Texture size must be at least %dx%d\n", fileName, 8, 8); + return TextureCreateSolid(*reinterpret_cast(&s_defaultColor)); + } + + CTexture* texture = new ("HTEXTURE", SLOG_OBJECT) CTexture; + + if (!texture) { + return nullptr; + } + + texture->m_alphaBits = image.AlphaBits(); + + if (!texture->m_alphaBits) { + texture->m_flags |= 1; + } + + if (flags) { + texture->m_flags |= 2; + } + + if (extension) { + *extension = '\0'; + } + + SStrCopy(texture->m_filename, fileName, sizeof(texture->m_filename)); + texture->m_gxTex = TextureAllocGxTex(target, width, height, 0, GxTex_Argb8888, texFlags, texture, UpdateTgaTexture, GxTex_Argb8888, texture->m_filename); + + if (!texture->m_gxTex) { + texture->~CTexture(); + SMemFree(texture, __FILE__, __LINE__, 0); + return nullptr; + } + + texture->m_gxTexTarget = target; + texture->m_gxWidth = width; + texture->m_gxHeight = height; + + return reinterpret_cast(HandleCreate(texture, "HTEXTURE")); +} + // VERIFY: 8821b223 manual // OFFSET: 0x004B9760 CTexture* TextureCreate(const char* fileName, CGxTexFlags texFlags, CStatus* status, uint32_t flags) { diff --git a/Engine/Source/Services/Texture.h b/Engine/Source/Services/Texture.h index 977e1c4..4443fe6 100644 --- a/Engine/Source/Services/Texture.h +++ b/Engine/Source/Services/Texture.h @@ -16,6 +16,7 @@ class CTexture; class CTextureAtlas; class CTextureBlobTexture; +class CTgaFile; class CGxTexCache { public: @@ -101,11 +102,14 @@ CTexture* CreateTgaTexture(const char* fileName, char* extension, int32_t flags, void FillInSolidTexture(const CImVector& color, CTexture* texture); MipBits* GetDefaultTexture(uint32_t width, uint32_t height); int32_t LoadBlpWithAlias(char* fileName, int32_t flags, MipBits** images); +uint32_t LoadPredrawnMips(CTgaFile* image, const char* format, int32_t flags, MipBits* images); +int32_t LoadTgaMips(char* fileName, char* extension, int32_t flags, MipBits** images, uint32_t* width, uint32_t* height, EGxTexFormat* gxTexFormat, int32_t* opaque, uint32_t* alphaBits, PIXEL_FORMAT* pixelFormat); char* OsPathFindExtensionWithDot(const char* path); int32_t PumpBlpTextureAsync(CTexture* texture, void* data); void GxTexNullUserFunc(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t face, uint32_t level, void* userArg, uint32_t& texelStrideInBytes, const void*& texels); void RequestImageDimensions(uint32_t* width, uint32_t* height, uint32_t* bestMip); void ResolveMipLevel(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t face, uint32_t level, void* userArg, uint32_t& texelStrideInBytes, const void*& texels); +MipBits* TextureAllocMippedImg(PIXEL_FORMAT format, uint32_t width, uint32_t height); CGxTex* TextureAllocGxTex(EGxTexTarget target, uint32_t width, uint32_t height, uint32_t depth, EGxTexFormat format, CGxTexFlags flags, void* userArg, GxTexUserFunc userFunc, EGxTexFormat dataFormat, const char* name); int32_t TextureBuildAliasPath(char* buffer, const char* fileName); CTexture* TextureCacheGetTexture(char* fileName, char* extension, CGxTexFlags texFlags); @@ -114,15 +118,24 @@ void TextureCacheUpdate(); void TextureCacheNewTexture(CTexture* texture, CGxTexFlags texFlags); void TextureCacheNewTexture(CTexture* texture, const CImVector& color); uint32_t TextureCalcGxTexSize(EGxTexFormat format, uint32_t width, uint32_t height, EGxTexFilter filter); +int32_t TextureCalcMipCount(uint32_t width, uint32_t height); CTexture* TextureCreate(const char* fileName, CGxTexFlags texFlags, CStatus* status, uint32_t flags); +CTexture* TextureCreateBlob(char* fileName, CGxTexFlags texFlags, CStatus* status, int32_t defaultFilter); +SFileImpl* TextureCreateOpenFile(const char* fileName, int32_t flags); CTexture* TextureCreateSolid(const CImVector& color); void TextureFreeGxTex(CGxTex* gxTex, const char* name); +void TextureFreeMippedImg(MipBits* bits); +void TextureFreeMippedImg(MipBits* bits, PIXEL_FORMAT format, uint32_t width, uint32_t height); +void TextureGenerateMips(uint32_t width, uint32_t height, uint32_t firstLevel, uint32_t levelCount, MipBits* images); CGxTex* TextureGetGxTex(CTexture* texture, int32_t wait, CStatus* status); void TextureGetTextureFormats(PIXEL_FORMAT& pixelFormat, EGxTexFormat& gxTexFormat, PIXEL_FORMAT preferredFormat, uint32_t alphaBits); void TextureIncreasePriority(CTexture* texture); void TextureInitialize(); +int32_t TextureLoadBlob(const char* fileName); int32_t TextureLoadBlpMips(char* fileName, char* extension, int32_t flags, MipBits** images, uint32_t* width, uint32_t* height, EGxTexFormat* gxTexFormat, int32_t* opaque, uint32_t* alphaBits, PIXEL_FORMAT* pixelFormat); +EGxTexFormat TexturePixelFormatToGxTexFormat(PIXEL_FORMAT format); void TextureReportLoadError(const char* type, const char* fileName, CStatus* status); +int32_t TextureUnloadBlob(const char* fileName); int32_t TextureUpdate(const void* data, void* param); void UpdateBlpTextureAsync(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t face, uint32_t level, void* userArg, uint32_t& texelStrideInBytes, const void*& texels); void UpdateTgaTexture(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t face, uint32_t level, void* userArg, uint32_t& texelStrideInBytes, const void*& texels); diff --git a/Engine/Source/Services/TextureBlob.cpp b/Engine/Source/Services/TextureBlob.cpp index 1197e7e..ff44782 100644 --- a/Engine/Source/Services/TextureBlob.cpp +++ b/Engine/Source/Services/TextureBlob.cpp @@ -1 +1,334 @@ -#include "TextureBlob.h" +#include +#include +#include "Engine/Source/Services/TextureBlob.h" +#include "Engine/Source/BlizzardCore/SFile2-Core.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Services/Texture.h" +#include "Engine/Source/Storm/H/storm.h" + +// STATIC: 0x00AC3754 +static TSExplicitList s_textureBlobList; + +// STATIC: 0x00B4A2D0 +static TSHashTableReuse s_textureBlobTextureHash; + +// INLINED +HASHKEY_TEXTUREBLOBFILE::HASHKEY_TEXTUREBLOBFILE() { + this->m_filename = nullptr; +} + +// INLINED +bool HASHKEY_TEXTUREBLOBFILE::operator==(const HASHKEY_TEXTUREBLOBFILE& key) const { + return !SStrCmpI(this->m_filename, key.m_filename, 260); +} + +// INLINED +CTextureBlobTexture::CTextureBlobTexture() { + this->m_blob = nullptr; + this->m_texture = nullptr; +} + +// INLINED +CTextureBlobTexture::~CTextureBlobTexture() { + this->m_blob = nullptr; + this->m_texture = nullptr; +} + +// INLINED +CTextureBlob::CTextureBlob() { + this->m_filename[0] = '\0'; + this->m_data = nullptr; + this->m_textures = nullptr; + this->m_textureCount = 0; + this->m_names = nullptr; + this->m_levelData = nullptr; +} + +// VERIFY: 9d0b7dab manual +// OFFSET: 0x004BFE70 +const uint8_t* CTextureBlobTexture::GetLevel(uint32_t level) { + SBlobTexture* texture = this->m_texture; + int32_t shift = texture->m_format & 0xF ? 0 : 1; + uint32_t width = texture->m_width; + uint32_t height = texture->m_height; + const uint8_t* data = this->m_blob->m_levelData + texture->m_dataOffset + 8; + + for (; level; level--) { + uint32_t size = height * width; + + if (shift) { + size >>= shift; + } + + if (size < 8) { + size = 8; + } + + data += size; + height >>= 1; + width >>= 1; + } + + return data; +} + +// VERIFY: 1d6f67aa manual +// OFFSET: 0x004BFF30 +CTextureBlob::~CTextureBlob() { + this->m_filename[0] = '\0'; + + if (this->m_data) { + SMemFree(this->m_data, __FILE__, __LINE__, 0); + } + + this->m_data = nullptr; + this->m_textures = nullptr; + this->m_textureCount = 0; + this->m_names = nullptr; + this->m_levelData = nullptr; +} + +// VERIFY: 04b4d023 auto +// OFFSET: 0x004C0210 +CTextureBlob* CTextureBlob::FindBlob(const char* fileName) { + for (CTextureBlob* blob = s_textureBlobList.Head(); !s_textureBlobList.IsEnd(blob); blob = s_textureBlobList.RawNext(blob)) { + if (!SStrCmpI(fileName, blob->m_filename, STORM_MAX_STR)) { + return blob; + } + } + + return nullptr; +} + +// VERIFY: 28ffc9a7 auto +// OFFSET: 0x004C02F0 +CTextureBlobTexture* CTextureBlob::GetTexture(const char* fileName) { + if (!s_textureBlobList.Head()) { + return nullptr; + } + + HASHKEY_TEXTUREBLOBFILE key; + key.m_filename = fileName; + + return s_textureBlobTextureHash.Ptr(SStrHashHT(fileName), key); +} + +// VERIFY: c1e2e8db manual +// OFFSET: 0x004C0330 +void CTextureBlob::RemoveTextures() { + SBlobTexture* texture = this->m_textures; + + for (uint32_t i = 0; i < this->m_textureCount; i++, texture++) { + if (texture->m_levels >= 0) { + const char* name = this->m_names + texture->m_nameOffset; + HASHKEY_TEXTUREBLOBFILE key; + key.m_filename = name; + CTextureBlobTexture* blobTexture = s_textureBlobTextureHash.Ptr(SStrHashHT(name), key); + s_textureBlobTextureHash.Delete(blobTexture); + } + + texture->m_levels &= ~0x80; + } +} + +// VERIFY: a2e0b946 manual +// OFFSET: 0x004C03C0 +void UpdateBlobTexture(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t face, uint32_t level, void* userArg, uint32_t& texelStrideInBytes, const void*& texels) { + CTexture* texture = static_cast(userArg); + CTextureBlobTexture* blobTexture = CTextureBlob::GetTexture(texture->m_filename); + + if (command != GxTex_Latch) { + return; + } + + texelStrideInBytes = GxCalcTexelStrideInBytes(texture->m_dataFormat, width); + const uint8_t* bits = blobTexture->GetLevel(level); + texels = bits; + + if (texture->m_gxTexTarget == GxTex_CubeMap) { + uint32_t order = face; + + switch (face) { + case 0: + order = 0; + break; + + case 1: + order = 2; + break; + + case 2: + order = 4; + break; + + case 3: + order = 5; + break; + + case 4: + order = 3; + break; + + case 5: + order = 1; + break; + } + + texels = bits + order * texelStrideInBytes; + texelStrideInBytes *= 6; + } +} + +// VERIFY: 328747cb manual +// OFFSET: 0x004C06C0 +void CTextureBlob::UnloadAll() { + for (CTextureBlob* blob = s_textureBlobList.Head(); !s_textureBlobList.IsEnd(blob); ) { + CTextureBlob* next = s_textureBlobList.RawNext(blob); + blob->RemoveTextures(); + s_textureBlobList.DeleteNode(blob); + blob = next; + } +} + +// VERIFY: d589605f manual +// OFFSET: 0x004C0710 +int32_t CTextureBlob::CreateTexture(CTexture* texture, CTextureBlobTexture* blobTexture) { + texture->m_alphaBits = blobTexture->m_texture->m_format >> 4; + + if (!texture->m_alphaBits) { + texture->m_flags |= 1; + } + + uint32_t width = blobTexture->m_texture->m_width; + uint32_t height = blobTexture->m_texture->m_height; + texture->m_bestMip = 0; + + PIXEL_FORMAT pixelFormat; + EGxTexFormat gxTexFormat; + uint32_t encoding = blobTexture->m_texture->m_format & 0xF; + TextureGetTextureFormats(pixelFormat, gxTexFormat, encoding == 1 ? PIXEL_DXT3 : encoding == 2 ? PIXEL_DXT5 : PIXEL_DXT1, texture->m_alphaBits); + + EGxTexTarget target = GxTex_2d; + + if (width == 6 * height) { + target = GxTex_CubeMap; + width /= 6; + } + + encoding = blobTexture->m_texture->m_format & 0xF; + EGxTexFormat dataFormat = TexturePixelFormatToGxTexFormat(encoding == 1 ? PIXEL_DXT3 : encoding == 2 ? PIXEL_DXT5 : PIXEL_DXT1); + texture->m_gxTexFormat = gxTexFormat; + texture->m_dataFormat = dataFormat; + texture->m_gxTexTarget = target; + texture->m_gxWidth = width; + texture->m_gxHeight = height; + + uint32_t filter = texture->m_gxTexFlags.m_filter; + + if (filter != GxTex_Nearest && filter != GxTex_Linear && blobTexture->m_texture->m_levels == 1) { + texture->m_gxTexFlags.m_filter = filter == GxTex_NearestMipNearest ? GxTex_Nearest : GxTex_Linear; + } + + texture->m_gxTex = TextureAllocGxTex(target, width, height, 0, gxTexFormat, texture->m_gxTexFlags, texture, UpdateBlobTexture, dataFormat, texture->m_filename); + + if (!texture->m_gxTex) { + texture->m_status.Add(STATUS_FATAL, "BLP Texture failure: \"%s\" allocating %dx%d texture failed.\n", texture->m_filename, width, height); + return 0; + } + + GxTexUpdate(texture->m_gxTex, 0, 0, width, height, 1); + + return 1; +} + +// VERIFY: 74fd3352 manual +// OFFSET: 0x004C0E60 +void CTextureBlob::HashTexture(SBlobTexture* texture) { + const char* name = this->m_names + texture->m_nameOffset; + uint32_t hash = SStrHashHT(name); + HASHKEY_TEXTUREBLOBFILE key; + key.m_filename = name; + + if (s_textureBlobTextureHash.Ptr(hash, key)) { + texture->m_levels |= 0x80; + return; + } + + CTextureBlobTexture* blobTexture = s_textureBlobTextureHash.New(hash, key, 0, 0); + blobTexture->m_blob = this; + blobTexture->m_texture = texture; +} + +// VERIFY: cdf3ceec auto +// OFFSET: 0x004C0F20 +void CTextureBlob::HashTextures() { + SBlobTexture* texture = this->m_textures; + + for (uint32_t i = 0; i < this->m_textureCount; i++, texture++) { + this->HashTexture(texture); + } +} + +// VERIFY: 6fbb3a53 manual +// OFFSET: 0x004C0F50 +int32_t CTextureBlob::Load(const char* fileName) { + if (CTextureBlob::FindBlob(fileName)) { + return 1; + } + + SFileImpl* file = TextureCreateOpenFile(fileName, 0); + + if (!file) { + return 0; + } + + CTextureBlob* blob = s_textureBlobList.NewNode(2, 0, 0); + SStrCopy(blob->m_filename, fileName, STORM_MAX_STR); + + uint32_t size = SFileGetFileSize(file, nullptr); + blob->m_data = static_cast(SMemAlloc(size, __FILE__, __LINE__, 0)); + SFileReadFile(file, blob->m_data, size, nullptr, nullptr, 0); + SFileCloseFile(file); + + uint8_t* textures = blob->m_data + *reinterpret_cast(blob->m_data + 4) + 8; + blob->m_textures = reinterpret_cast(textures + 8); + blob->m_textureCount = *reinterpret_cast(textures + 4) / sizeof(SBlobTexture); + + uint8_t* names = textures + 8 + *reinterpret_cast(textures + 4); + blob->m_names = reinterpret_cast(names + 8); + blob->m_levelData = names + 8 + *reinterpret_cast(names + 4); + blob->HashTextures(); + + return 1; +} + +// VERIFY: 8ace8e6f manual +// OFFSET: 0x004C1030 +void CTextureBlob::RebuildHashTable() { + for (CTextureBlob* blob = s_textureBlobList.Head(); !s_textureBlobList.IsEnd(blob); blob = s_textureBlobList.RawNext(blob)) { + blob->RemoveTextures(); + } + + for (CTextureBlob* blob = s_textureBlobList.Head(); !s_textureBlobList.IsEnd(blob); blob = s_textureBlobList.RawNext(blob)) { + blob->HashTextures(); + } +} + +// VERIFY: e70e77fa manual +// OFFSET: 0x004C10C0 +int32_t CTextureBlob::Unload(const char* fileName) { + for (CTextureBlob* blob = s_textureBlobList.Head(); !s_textureBlobList.IsEnd(blob); ) { + CTextureBlob* next = s_textureBlobList.RawNext(blob); + + if (!SStrCmpI(fileName, blob->m_filename, STORM_MAX_STR)) { + blob->RemoveTextures(); + s_textureBlobList.DeleteNode(blob); + CTextureBlob::RebuildHashTable(); + return 1; + } + + blob = next; + } + + return 0; +} diff --git a/Engine/Source/Services/TextureBlob.h b/Engine/Source/Services/TextureBlob.h index 95068e4..a7ce629 100644 --- a/Engine/Source/Services/TextureBlob.h +++ b/Engine/Source/Services/TextureBlob.h @@ -3,14 +3,63 @@ #include #include +#include "Engine/Source/Gx/Types.h" +#include "Engine/Source/Storm/H/stpl.h" class CTexture; -class CTextureBlobTexture; +class CTextureBlob; + +struct SBlobTexture { + /* 0000 */ uint32_t m_nameOffset; + /* 0004 */ uint32_t m_dataOffset; + /* 0008 */ uint8_t m_width; + /* 0009 */ uint8_t m_height; + /* 000A */ int8_t m_levels; + /* 000B */ uint8_t m_format; +}; + +struct HASHKEY_TEXTUREBLOBFILE { + /* 0000 */ const char* m_filename; + + HASHKEY_TEXTUREBLOBFILE(); + bool operator==(const HASHKEY_TEXTUREBLOBFILE& key) const; +}; + +class CTextureBlobTexture : public TSHashObject { + public: + /* 0018 */ CTextureBlob* m_blob; + /* 001C */ SBlobTexture* m_texture; + + CTextureBlobTexture(); + ~CTextureBlobTexture(); + const uint8_t* GetLevel(uint32_t level); +}; class CTextureBlob { public: - static void CreateTexture(CTexture* texture, CTextureBlobTexture* blobTexture); + static int32_t CreateTexture(CTexture* texture, CTextureBlobTexture* blobTexture); + static CTextureBlob* FindBlob(const char* fileName); static CTextureBlobTexture* GetTexture(const char* fileName); + static int32_t Load(const char* fileName); + static void RebuildHashTable(); + static int32_t Unload(const char* fileName); + static void UnloadAll(); + + /* 0000 */ char m_filename[260]; + /* 0104 */ uint8_t* m_data; + /* 0108 */ SBlobTexture* m_textures; + /* 010C */ uint32_t m_textureCount; + /* 0110 */ const char* m_names; + /* 0114 */ uint8_t* m_levelData; + /* 0118 */ TSLink m_link; + + CTextureBlob(); + ~CTextureBlob(); + void HashTexture(SBlobTexture* texture); + void HashTextures(); + void RemoveTextures(); }; +void UpdateBlobTexture(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t face, uint32_t level, void* userArg, uint32_t& texelStrideInBytes, const void*& texels); + #endif diff --git a/Engine/Source/Services/TextureInt.h b/Engine/Source/Services/TextureInt.h index ab25095..c6e2915 100644 --- a/Engine/Source/Services/TextureInt.h +++ b/Engine/Source/Services/TextureInt.h @@ -1,4 +1,18 @@ #ifndef ENGINE_SOURCE_SERVICES_TEXTUREINT_H #define ENGINE_SOURCE_SERVICES_TEXTUREINT_H +#include +#include +#include "Engine/Source/BLPFile/Types.h" +#include "Engine/Source/Storm/H/stpl.h" + +class CMipBitsCache { + public: + /* 0000 */ MipBits* m_bits; + /* 0004 */ TSLink m_link; + + CMipBitsCache(); + ~CMipBitsCache(); +}; + #endif diff --git a/Engine/Source/Services/Types.h b/Engine/Source/Services/Types.h index 9280194..7f82a41 100644 --- a/Engine/Source/Services/Types.h +++ b/Engine/Source/Services/Types.h @@ -43,11 +43,4 @@ enum PIXEL_FORMAT { NUM_PIXEL_FORMATS = 10, }; -struct TGA32Pixel { - /* 0000 */ uint8_t m_b; - /* 0001 */ uint8_t m_g; - /* 0002 */ uint8_t m_r; - /* 0003 */ uint8_t m_a; -}; - #endif diff --git a/WoW/Source/Ui/Types.h b/WoW/Source/Ui/Types.h index 48bff8b..71c2c97 100644 --- a/WoW/Source/Ui/Types.h +++ b/WoW/Source/Ui/Types.h @@ -3,6 +3,7 @@ #include #include +#include "Engine/Source/BLPFile/Types.h" namespace BSN { struct BSNPrimitiveEncoder { @@ -13,13 +14,6 @@ struct BSNPrimitiveEncoder { }; } -struct C4Pixel { - /* 0000 */ uint8_t m_b; - /* 0001 */ uint8_t m_g; - /* 0002 */ uint8_t m_r; - /* 0003 */ uint8_t m_a; -}; - enum EGxuDrawListCategory { GxuCat_0 = 0, GxuCat_1 = 1, diff --git a/_Triage/MippedImg.cpp b/_Triage/MippedImg.cpp index 0632737..2cc759b 100644 --- a/_Triage/MippedImg.cpp +++ b/_Triage/MippedImg.cpp @@ -117,3 +117,60 @@ void MippedImgSet(uint32_t format, uint32_t width, uint32_t height, MipBits* ima offset += CalcLevelSize(i, width, height, format); } } + +// VERIFY: aa44b82f manual +// OFFSET: 0x006AB860 +void FullShrink(C4Pixel* dest, uint32_t destWidth, uint32_t destHeight, C4Pixel* source, uint32_t sourceWidth, uint32_t sourceHeight) { + uint32_t xRatio = sourceWidth / destWidth; + uint32_t yRatio = sourceHeight / destHeight; + + for (uint32_t y = 0; y < destHeight; y++) { + for (uint32_t x = 0; x < destWidth; x++) { + int64_t weightedB = 0; + int64_t weightedG = 0; + int64_t weightedR = 0; + int64_t alpha = 0; + int64_t b = 0; + int64_t g = 0; + int64_t r = 0; + int64_t count = 0; + C4Pixel* row = source; + + for (uint32_t j = 0; j < yRatio; j++) { + C4Pixel* pixel = row; + + for (uint32_t i = 0; i < xRatio; i++) { + alpha += pixel->m_a; + weightedR += pixel->m_a * pixel->m_r; + weightedG += pixel->m_a * pixel->m_g; + weightedB += pixel->m_a * pixel->m_b; + count++; + r += pixel->m_r; + g += pixel->m_g; + b += pixel->m_b; + pixel++; + } + + row += sourceWidth; + } + + C4Pixel result; + + if (alpha) { + result.m_r = static_cast(weightedR / alpha); + result.m_g = static_cast(weightedG / alpha); + result.m_b = static_cast(weightedB / alpha); + } else { + result.m_r = static_cast(r / count); + result.m_g = static_cast(g / count); + result.m_b = static_cast(b / count); + } + + result.m_a = static_cast(alpha / (xRatio * yRatio)); + *dest++ = result; + source += xRatio; + } + + source += sourceWidth * (yRatio - 1); + } +} diff --git a/_Triage/MippedImg.h b/_Triage/MippedImg.h index ba2c29f..6bbf8d3 100644 --- a/_Triage/MippedImg.h +++ b/_Triage/MippedImg.h @@ -7,6 +7,7 @@ uint32_t CalcLevelCount(uint32_t width, uint32_t height); uint32_t CalcLevelOffset(uint32_t level, uint32_t width, uint32_t height, uint32_t format); uint32_t CalcLevelSize(uint32_t level, uint32_t width, uint32_t height, uint32_t format); +void FullShrink(C4Pixel* dest, uint32_t destWidth, uint32_t destHeight, C4Pixel* source, uint32_t sourceWidth, uint32_t sourceHeight); uint32_t GetBitDepth(uint32_t format); MipBits* MippedImgAllocA(uint32_t format, uint32_t width, uint32_t height, const char* fileName, int32_t lineNumber); uint32_t MippedImgCalcSize(uint32_t format, uint32_t width, uint32_t height);