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
96namespace 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
458std::shared_ptr<Ship::IResource>
469ResourceFactoryBinaryRawTextureV0::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
0 commit comments