summaryrefslogtreecommitdiff
path: root/src/factories/TextureFactory.cpp
blob: 12d8540552f61a3d4bc60a8bd94588a2042ffa17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "TextureFactory.h"

#include "Companion.h"
#include "utils/MIODecoder.h"

namespace fs = std::filesystem;

static const std::unordered_map <std::string, TextureType> gTextureTypes = {
    { "RGBA16", TextureType::RGBA16bpp },
    { "RGBA32", TextureType::RGBA32bpp },
    { "IA1", TextureType::GrayscaleAlpha4bpp },
	{ "IA4", TextureType::GrayscaleAlpha4bpp },
	{ "IA8", TextureType::GrayscaleAlpha8bpp },
	{ "IA16", TextureType::GrayscaleAlpha16bpp },
};

bool TextureFactory::process(LUS::BinaryWriter* writer, YAML::Node& data, std::vector<uint8_t>& buffer) {

    auto format = data["format"].as<std::string>();
    auto width = data["width"].as<uint32_t>();
    auto height = data["height"].as<uint32_t>();
    auto size = data["size"].as<size_t>();
    auto offset = data["offset"].as<size_t>();

	if(!gTextureTypes.contains(format)) {
		return false;
	}

	TextureType type = gTextureTypes.at(format);

    WRITE_HEADER(LUS::ResourceType::Texture, 0);

    WRITE_U32(type);
    WRITE_U32(width);
    WRITE_U32(height);

    if(data["mio0"]){
        auto mio0 = data["mio0"].as<size_t>();
        auto decoded = MIO0Decoder::Decode(buffer, offset);
        WRITE_U32(size);
        WRITE_ARRAY(decoded.data() + mio0, size);
    } else {
        WRITE_U32(size);
        WRITE_ARRAY(buffer.data() + offset, size);
    }

	return true;
}