Skip to content

Commit 2462099

Browse files
committed
Added RawTextureFactory as a full PNG loader
1 parent d24872f commit 2462099

2 files changed

Lines changed: 40 additions & 66 deletions

File tree

src/port/importer/RawTextureFactory.cpp

Lines changed: 38 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,8 @@
22
#include "fast/resource/type/Texture.h"
33
#include "spdlog/spdlog.h"
44
#include <stb_image.h>
5-
#include <ship/Context.h>
6-
#include "ship/resource/archive/ArchiveManager.h"
7-
#include "ship/resource/ResourceManager.h"
85

96
namespace MK64 {
10-
std::unordered_map<Fast::TextureType, float> TexturePixelMultipliers = {
11-
{ Fast::TextureType::RGBA32bpp, 4.0f }, { Fast::TextureType::RGBA16bpp, 2.0f },
12-
{ Fast::TextureType::Palette4bpp, 0.5f }, { Fast::TextureType::Palette8bpp, 1.0f },
13-
{ Fast::TextureType::Grayscale4bpp, 0.5f }, { Fast::TextureType::Grayscale8bpp, 1.0f },
14-
{ Fast::TextureType::GrayscaleAlpha4bpp, 0.5f }, { Fast::TextureType::GrayscaleAlpha8bpp, 1.0f },
15-
{ Fast::TextureType::GrayscaleAlpha16bpp, 2.0f },
16-
};
17-
18-
std::shared_ptr<Ship::IResource> loadPngTexture(std::shared_ptr<Ship::File> filePng,
19-
std::shared_ptr<Ship::ResourceInitData> initData) {
20-
auto texture = std::make_shared<Fast::Texture>(initData);
21-
const auto res = std::static_pointer_cast<Fast::Texture>(
22-
Ship::Context::GetInstance()->GetResourceManager()->LoadResource(initData->Path, true));
23-
24-
int height, width = 0;
25-
texture->ImageData = stbi_load_from_memory((const stbi_uc*)filePng->Buffer.get()->data(),
26-
filePng->Buffer.get()->size(), &width, &height, nullptr, 4);
27-
texture->Width = width;
28-
texture->Height = height;
29-
texture->Type = Fast::TextureType::RGBA32bpp;
30-
texture->ImageDataSize = texture->Width * texture->Height * 4;
31-
texture->Flags = TEX_FLAG_LOAD_AS_IMG;
32-
if (res != nullptr) {
33-
texture->HByteScale = (res->Width / texture->Width) *
34-
(TexturePixelMultipliers[texture->Type] / TexturePixelMultipliers[res->Type]);
35-
texture->VPixelScale = res->Height / texture->Height;
36-
} else {
37-
texture->VPixelScale = 1.0f;
38-
texture->HByteScale = 1.0f;
39-
}
40-
return texture;
41-
}
42-
43-
std::vector<std::string> extension = { ".png", ".PNG", ".jpg", ".JPG", ".jpeg", ".JPEG", ".bmp", ".BMP" };
447

458
std::shared_ptr<Ship::IResource>
469
ResourceFactoryBinaryRawTextureV0::ReadResource(std::shared_ptr<Ship::File> file,
@@ -49,24 +12,32 @@ ResourceFactoryBinaryRawTextureV0::ReadResource(std::shared_ptr<Ship::File> file
4912
return nullptr;
5013
}
5114

52-
for (const auto& ext : extension) {
53-
auto filePng = Ship::Context::GetInstance()->GetResourceManager()->LoadFileProcess(initData->Path + ext);
54-
55-
if (filePng != nullptr) {
56-
return loadPngTexture(filePng, initData);
57-
}
58-
}
59-
6015
auto texture = std::make_shared<Fast::Texture>(initData);
6116
auto reader = std::get<std::shared_ptr<Ship::BinaryReader>>(file->Reader);
6217

6318
texture->Type = (Fast::TextureType)reader->ReadUInt32();
6419
texture->Width = reader->ReadUInt32();
6520
texture->Height = reader->ReadUInt32();
6621
texture->ImageDataSize = reader->ReadUInt32();
67-
texture->ImageData = new uint8_t[texture->ImageDataSize];
6822

69-
reader->Read((char*)texture->ImageData, texture->ImageDataSize);
23+
std::vector<uint8_t> buf(texture->ImageDataSize);
24+
reader->Read(reinterpret_cast<char*>(buf.data()), texture->ImageDataSize);
25+
26+
int w, h;
27+
texture->ImageData = stbi_load_from_memory(buf.data(), static_cast<int>(buf.size()), &w, &h, nullptr, 4);
28+
29+
if (!texture->ImageData) {
30+
SPDLOG_ERROR("RawTextureFactory V0: stbi failed for {}: {}", initData->Path, stbi_failure_reason());
31+
return nullptr;
32+
}
33+
34+
texture->Width = static_cast<uint32_t>(w);
35+
texture->Height = static_cast<uint32_t>(h);
36+
texture->Type = Fast::TextureType::RGBA32bpp;
37+
texture->ImageDataSize = texture->Width * texture->Height * 4;
38+
texture->Flags = TEX_FLAG_LOAD_AS_IMG;
39+
texture->HByteScale = 1.0f;
40+
texture->VPixelScale = 1.0f;
7041

7142
return texture;
7243
}
@@ -78,20 +49,6 @@ ResourceFactoryBinaryRawTextureV1::ReadResource(std::shared_ptr<Ship::File> file
7849
return nullptr;
7950
}
8051

81-
for (const auto& ext : extension) {
82-
std::shared_ptr<Ship::File> texture;
83-
84-
if (initData->Path.find(ext) != std::string::npos) {
85-
texture = Ship::Context::GetInstance()->GetResourceManager()->LoadFileProcess(initData->Path);
86-
} else {
87-
texture = Ship::Context::GetInstance()->GetResourceManager()->LoadFileProcess(initData->Path + ext);
88-
}
89-
90-
if (texture != nullptr) {
91-
return loadPngTexture(texture, initData);
92-
}
93-
}
94-
9552
auto texture = std::make_shared<Fast::Texture>(initData);
9653
auto reader = std::get<std::shared_ptr<Ship::BinaryReader>>(file->Reader);
9754

@@ -102,10 +59,27 @@ ResourceFactoryBinaryRawTextureV1::ReadResource(std::shared_ptr<Ship::File> file
10259
texture->HByteScale = reader->ReadFloat();
10360
texture->VPixelScale = reader->ReadFloat();
10461
texture->ImageDataSize = reader->ReadUInt32();
105-
texture->ImageData = new uint8_t[texture->ImageDataSize];
10662

107-
reader->Read((char*)texture->ImageData, texture->ImageDataSize);
63+
std::vector<uint8_t> buf(texture->ImageDataSize);
64+
reader->Read(reinterpret_cast<char*>(buf.data()), texture->ImageDataSize);
65+
66+
int w, h;
67+
texture->ImageData = stbi_load_from_memory(buf.data(), static_cast<int>(buf.size()), &w, &h, nullptr, 4);
68+
69+
if (!texture->ImageData) {
70+
SPDLOG_ERROR("RawTextureFactory V1: stbi failed for {}: {}", initData->Path, stbi_failure_reason());
71+
return nullptr;
72+
}
73+
74+
texture->Width = static_cast<uint32_t>(w);
75+
texture->Height = static_cast<uint32_t>(h);
76+
texture->Type = Fast::TextureType::RGBA32bpp;
77+
texture->ImageDataSize = texture->Width * texture->Height * 4;
78+
texture->Flags = TEX_FLAG_LOAD_AS_IMG;
79+
texture->HByteScale = 1.0f;
80+
texture->VPixelScale = 1.0f;
10881

10982
return texture;
11083
}
111-
} // namespace MK64
84+
85+
} // namespace MK64

tools/merge.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,10 @@ def preprocess_asset(bytes_data: bytes, filename: str) -> bytes:
409409
return bytes_data
410410

411411
elif file.endswith('.png'):
412-
img = Image.open(BytesIO(bytes_data)).convert('RGBA')
412+
img = Image.open(BytesIO(bytes_data))
413413
width, height = img.size
414414
texture_type = TextureType.RGBA32bpp.value
415-
raw = img.tobytes()
415+
raw = bytes_data
416416
resource_type = 0x52544558 # RTEX
417417

418418
tex_data_size = len(raw)

0 commit comments

Comments
 (0)