Generate IWI instead of PGM

Also remove PGM loading code
This commit is contained in:
GEEKiDoS 2021-06-29 21:06:50 +08:00
parent ecc21b4ac2
commit 3785322171
5 changed files with 34 additions and 112 deletions

View File

@ -101,7 +101,6 @@ namespace Components
Loader::Register(new StructuredData());
Loader::Register(new ConnectProtocol());
Loader::Register(new StartupMessages());
Loader::Register(new Images());
Loader::Register(new Client());

View File

@ -130,6 +130,5 @@ namespace Components
#include "Modules/ConnectProtocol.hpp"
#include "Modules/StartupMessages.hpp"
#include "Modules/Stats.hpp"
#include "Modules/Images.hpp"
#include "Modules/Client.hpp"

View File

@ -202,14 +202,43 @@ namespace Assets
// Save generated image
Utils::IO::CreateDir("userraw\\images");
std::string buf = Utils::String::VA("P5\n%d\n%d\n255\n", w, h);
int headerSize = buf.size();
int fileSize = w * h * 4;
int iwiHeaderSize = static_cast<int>(sizeof(Game::GfxImageFileHeader));
buf.resize(headerSize + w * h);
std::memcpy(buf.data() + headerSize, pixels, w * h);
Game::GfxImageFileHeader iwiHeader =
{
{ 'I', 'W', 'i' },
/* version */
8,
/* flags */
2,
/* format */
Game::IMG_FORMAT_BITMAP_RGBA,
0,
/* dimensions(x, y, z) */
{ static_cast<short>(w), static_cast<short>(h), 1 },
/* fileSizeForPicmip (mipSize in bytes + sizeof(GfxImageFileHeader)) */
{ fileSize + iwiHeaderSize, fileSize, fileSize, fileSize }
};
Utils::IO::WriteFile(Utils::String::VA("userraw\\images\\%s.pgm", texName), buf);
std::string outIwi;
outIwi.resize(fileSize + sizeof(Game::GfxImageFileHeader));
std::memcpy(outIwi.data(), &iwiHeader, sizeof(Game::GfxImageFileHeader));
// Generate RGBA data
auto* rgbaPixels = outIwi.data() + sizeof(Game::GfxImageFileHeader);
for (int i = 0; i < w * h * 4; i += 4)
{
rgbaPixels[i + 0] = static_cast<char>(255);
rgbaPixels[i + 1] = static_cast<char>(255);
rgbaPixels[i + 2] = static_cast<char>(255);
rgbaPixels[i + 3] = static_cast<char>(pixels[i / 4]);
}
Utils::IO::WriteFile(Utils::String::VA("userraw\\images\\%s.iwi", texName), outIwi);
Utils::Memory::Free(pixels);
}
}

View File

@ -1,95 +0,0 @@
#include "STDInclude.hpp"
namespace Components
{
bool Image_LoadFontTextureFromPgm(Game::GfxImage* image)
{
auto pgmName = Utils::String::VA("images/%s.pgm", image->name);
if (!Game::FS_FileExists(pgmName))
return false;
auto pgmFile = FileSystem::File(pgmName);
// Build a header
Game::GfxImageFileHeader header =
{
{ 'I', 'W', 'i' },
/* version */
8,
/* flags */
2,
/* format */
Game::IMG_FORMAT_BITMAP_RGBA,
0,
/* dimensions(x, y, z) */
{ 0, 0, 1 },
/* fileSizeForPicmip (mipSize in bytes + sizeof(GfxImageFileHeader)) */
{ 0, 0, 0, 0 }
};
int w, h;
auto* buffer = pgmFile.getBuffer().data();
if (sscanf_s(buffer, "P5\n%d\n%d\n255\n", &w, &h) != 2)
{
Logger::SoftError("Image_LoadFontTextureFromPgm: Pgm %s is broken!\n", pgmFile.getName().data());
return false;
}
int newLineCounter = 0;
while (newLineCounter < 4)
newLineCounter += *(buffer++) == '\n' ? 1 : 0;
auto* pixels = Utils::Memory::AllocateArray<uint8_t>(w * h * 4);
// Our font texture contains alpha channel only
for (int i = 0; i < w * h * 4; i += 4)
{
pixels[i + 0] = 255;
pixels[i + 1] = 255;
pixels[i + 2] = 255;
pixels[i + 3] = buffer[i / 4];
}
header.dimensions[0] = static_cast<short>(w);
header.dimensions[1] = static_cast<short>(h);
auto fileSize = w * h * 4;
for (int i = 0; i < 4; ++i)
header.fileSizeForPicmip[i] = fileSize;
header.fileSizeForPicmip[0] += sizeof(Game::GfxImageFileHeader);
image->noPicmip = 0;
image->width = header.dimensions[0];
image->height = header.dimensions[1];
image->depth = 1;
Game::Image_PicmipForSemantic(image->semantic, &image->picmip);
Game::Image_LoadFromData(image, &header, pixels);
Utils::Memory::Free(pixels);
return true;
}
bool Image_LoadFromFileWithReader_stub(Game::GfxImage* image, Game::Reader_t reader)
{
if (Utils::String::StartsWith(image->name, "if_") && Image_LoadFontTextureFromPgm(image))
return true;
return Game::Image_LoadFromFileWithReader(image, reader);
}
Images::Images()
{
Utils::Hook(0x51F486, Image_LoadFromFileWithReader_stub, HOOK_CALL).install()->quick();
Utils::Hook(0x51F595, Image_LoadFromFileWithReader_stub, HOOK_CALL).install()->quick();
Utils::Hook(0x51F809, Image_LoadFromFileWithReader_stub, HOOK_CALL).install()->quick();
Utils::Hook(0x51F896, Image_LoadFromFileWithReader_stub, HOOK_CALL).install()->quick();
}
}

View File

@ -1,10 +0,0 @@
#pragma once
namespace Components
{
class Images : public Component
{
public:
Images();
};
}