summaryrefslogtreecommitdiff
path: root/ZAPD/ZTexture.h
blob: 1461ff95a69bcc8d633fcff12def9daeb0ac1070 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once

#include "ImageBackend.h"
#include "ZResource.h"
#include "tinyxml2.h"

enum class TextureType
{
	Error,
	RGBA32bpp,
	RGBA16bpp,
	Palette4bpp,
	Palette8bpp,
	Grayscale4bpp,
	Grayscale8bpp,
	GrayscaleAlpha4bpp,
	GrayscaleAlpha8bpp,
	GrayscaleAlpha16bpp,
};

class ZTexture : public ZResource
{
protected:
	TextureType format = TextureType::Error;
	uint32_t width, height;

	ImageBackend textureData;
	std::vector<uint8_t> textureDataRaw;  // When reading from a PNG file.
	uint32_t tlutOffset = static_cast<uint32_t>(-1);
	ZTexture* tlut = nullptr;
	bool splitTlut;

	// The following functions convert from N64 binary data to a bitmap to be saved to a PNG.
	void ConvertN64ToBitmap_RGBA16();
	void ConvertN64ToBitmap_RGBA32();
	void ConvertN64ToBitmap_Grayscale8();
	void ConvertN64ToBitmap_GrayscaleAlpha8();
	void ConvertN64ToBitmap_Grayscale4();
	void ConvertN64ToBitmap_GrayscaleAlpha4();
	void ConvertN64ToBitmap_GrayscaleAlpha16();
	void ConvertN64ToBitmap_Palette4();
	void ConvertN64ToBitmap_Palette8();

	// The following functions convert from a bitmap to N64 binary data.
	void PrepareRawDataFromFile(const fs::path& inFolder);
	void ConvertBitmapToN64_RGBA16();
	void ConvertBitmapToN64_RGBA32();
	void ConvertBitmapToN64_Grayscale4();
	void ConvertBitmapToN64_Grayscale8();
	void ConvertBitmapToN64_GrayscaleAlpha4();
	void ConvertBitmapToN64_GrayscaleAlpha8();
	void ConvertBitmapToN64_GrayscaleAlpha16();
	void ConvertBitmapToN64_Palette4();
	void ConvertBitmapToN64_Palette8();

public:
	ZTexture(ZFile* nParent);

	bool isPalette = false;
	bool dWordAligned = true;

	void ExtractFromBinary(uint32_t nRawDataIndex, int32_t nWidth, int32_t nHeight,
	                       TextureType nType, bool nIsPalette);
	void FromPNG(const fs::path& pngFilePath, TextureType texType);
	static TextureType GetTextureTypeFromString(const std::string& str);

	void ParseXML(tinyxml2::XMLElement* reader) override;
	void ParseRawData() override;
	void DeclareReferences(const std::string& prefix) override;

	Declaration* DeclareVar(const std::string& prefix, const std::string& bodyStr) override;
	std::string GetBodySourceCode() const override;

	/// <summary>
	/// Calculates the hash of this texture, for use with the texture pool.
	/// </summary>
	void CalcHash() override;

	void Save(const fs::path& outFolder) override;

	std::string GetHeaderDefines() const;
	bool IsExternalResource() const override;
	std::string GetSourceTypeName() const override;
	ZResourceType GetResourceType() const override;
	std::string GetExternalExtension() const override;

	size_t GetRawDataSize() const override;
	std::string GetIMFmtFromType();
	std::string GetIMSizFromType();
	std::string GetDefaultName(const std::string& prefix) const override;
	uint32_t GetWidth() const;
	uint32_t GetHeight() const;
	void SetDimensions(uint32_t nWidth, uint32_t nHeight);

	/// <summary>
	/// Returns how many bytes each pixel takes up.
	/// </summary>
	/// <returns></returns>
	float GetPixelMultiplyer() const;

	TextureType GetTextureType() const;

	/// <summary>
	/// Returns the path to the texture pool, taken from the config file.
	/// </summary>
	/// <param name="defaultValue"></param>
	/// <returns></returns>
	fs::path GetPoolOutPath(const fs::path& defaultValue);

	/// <summary>
	/// Returns if this texture uses a palette.
	/// </summary>
	/// <returns></returns>
	bool IsColorIndexed() const;

	void SetTlut(ZTexture* nTlut);
	bool HasTlut() const;
	void ParseRawDataLate() override;
};