-
Notifications
You must be signed in to change notification settings - Fork 6
/
MemImage.h
183 lines (158 loc) · 5.36 KB
/
MemImage.h
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef MEMIMAGE_H
#define MEMIMAGE_H
#define BLPCONV_MAX_PATH 260
#define LOG ::printf
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
#define NULL 0
#define MEMIMAGE_PALETTEBYTES (3*256)
/*
enum BLPType
{
BLPTYPE_UNSPECIFIED,
BLPTYPE_PAL_ALPHA0, // compression = 0, alphaBitDepth = 0
BLPTYPE_PAL_ALPHA1, // compression = 0, alphaBitDepth = 1
BLPTYPE_PAL_ALPHA4, // compression = 0, alphaBitDepth = 4 - This has been seen but only on alpha-less textures.
BLPTYPE_PAL_ALPHA8, // compression = 0, alphaBitDepth = 8
BLPTYPE_DXT1_ALPHA0, // (DXT1) compression = 1, alphaBitDepth = 0
BLPTYPE_DXT1_ALPHA1, // (DXT1) compression = 1, alphaBitDepth = 1
BLPTYPE_DXT3, // (DXT3) compression = 1, alphaBitDepth = 8
BLPTYPE_DXT5, // (DXT5) compression = 1, alphaBitDepth = 8, alphaUnknown = 7
BLPTYPE_COUNT
};
// NOTE: These arrays need to be updated when the above enum changes.
const char* BLPTypeDescriptions[];
const char* BLPTypeNames[];
enum PNGType
{
PNGTYPE_UNSPECIFIED,
PNGTYPE_PALETTIZED,
PNGTYPE_PALETTIZED_ALPHAMASK,
PNGTYPE_RGB,
PNGTYPE_RGBA,
PNGTYPE_COUNT
};
// NOTE: These arrays need to be updated when the above enum changes.
const char* PNGTypeDescriptions[];
const char* PNGTypeNames[];
*/
enum FORMATID
{
FORMAT_UNSPECIFIED,
BLPTYPE_FIRST,
BLPTYPE_PAL_ALPHA0 = BLPTYPE_FIRST, // 1 - compression = 0, alphaBitDepth = 0
BLPTYPE_PAL_ALPHA1, // 2 - compression = 0, alphaBitDepth = 1
BLPTYPE_PAL_ALPHA4, // 3 - compression = 0, alphaBitDepth = 4
BLPTYPE_PAL_ALPHA8, // 4 - compression = 0, alphaBitDepth = 8
BLPTYPE_DXT1_ALPHA0, // 5 - (DXT1) compression = 1, alphaBitDepth = 0
BLPTYPE_DXT1_ALPHA1, // 6 - (DXT1) compression = 1, alphaBitDepth = 1
BLPTYPE_DXT3, // 7 - (DXT3) compression = 1, alphaBitDepth = 8
BLPTYPE_DXT5, // 8 - (DXT5) compression = 1, alphaBitDepth = 8, alphaUnknown = 7
BLPTYPE_BGRA, // 9 - (BGRa) compression = 2
BLPTYPE_LAST = BLPTYPE_BGRA,
PNGTYPE_FIRST,
PNGTYPE_PALETTIZED = PNGTYPE_FIRST,
PNGTYPE_PALETTIZED_ALPHAMASK,
PNGTYPE_RGB,
PNGTYPE_RGBA,
PNGTYPE_LAST = PNGTYPE_RGBA,
FORMAT_COUNT
};
const char* FORMATIDNames[];
const char* FORMATIDDescriptions[];
#define ISBLP(format) (BLPTYPE_FIRST <= format && format <= BLPTYPE_LAST)
#define ISPNG(format) (PNGTYPE_FIRST <= format && format <= PNGTYPE_LAST)
enum FileType
{
FILETYPE_UNSPECIFIED,
FILETYPE_PNG,
FILETYPE_BLP,
FILETYPE_COUNT
};
// BLP Load result. Used as program return code if != 0.
enum LoadResult
{
LOADRESULT_SUCCESS = 0,
LOADRESULT_PATCH = -1, // .blp is a patch file
LOADRESULT_NOTBLP = -2, // Bad BLP2 id.
LOADRESULT_VERSION = -3, // Header version field unrecognized.
LOADRESULT_MEMORY = -4, // OOM
LOADRESULT_ERROR = -5, // Generic error
LOADRESULT_FILEERROR = -6, // File system error
};
class MemImage
{
public:
// Public settings.
static bool s_bCreateMipTestImage;
static float s_fGammaFactor; // Legacy.
static BYTE s_byAlphaThreshold;
static bool s_bVerbose;
static bool s_bNoMips;
static FORMATID s_ruleTable[FORMAT_COUNT];
static bool s_bOnlyWarnOnPatch;
protected:
DWORD m_width;
DWORD m_height;
// These two variables define the 4 formats of data MemImage can contain.
bool m_bPalettized;
bool m_bHasAlpha;
// This contains the actual data in the following formats:
// - m_bPalettized == true && m_bHasAlpha = false -> 1-byte/entry
// - m_bPalettized == true && m_bHasAlpha = true -> 1-byte/entry, then 1-alpha byte/entry [image buffer][alpha buffer]
// - m_bPalettized == false && m_bHasAlpha == false -> 3 bpp rgb
// - m_bPalettized == false && m_bHasAlpha == true -> 4 bpp rgba
BYTE* m_buffer;
DWORD m_bufferBytes;
BYTE m_palette[MEMIMAGE_PALETTEBYTES]; // RGB
public:
MemImage();
MemImage(const MemImage& rhs);
~MemImage();
MemImage& operator=(const MemImage& rhs);
bool Init(DWORD width, DWORD height, bool hasAlpha, bool palettized);
void Copy(const MemImage& rhs);
void Clear();
// If bytes == 0 then it calculates the proper size with the CalculateBufferBytes function.
bool AllocateBuffer(DWORD bytes);
bool HasAlpha() { return m_bHasAlpha; }
bool IsPalettized() { return m_bPalettized; }
// Load functions.
LoadResult LoadFromBLP(const char* filename, FORMATID* blpTypeRet = NULL);
bool LoadFromPNG(const char* filename, FORMATID* pngTypeRet = NULL);
// Save functions. All are guaranteed not to change the image data at all. If conversions
// need to be made a temp image will be created.
bool Save(const char* filename, FORMATID type) const;
bool SaveToBLP(const char* filename, FORMATID type = FORMAT_UNSPECIFIED) const;
bool SaveToPNG(const char* filename, FORMATID type = FORMAT_UNSPECIFIED) const;
// If palettizes or ff already palettized returns true. Returns false on error.
bool Palettize();
// Does nothing if not palettized.
void Depalettize();
// Returns false on error, true otherwise. No error if it does nothing.
bool RemoveAlpha();
bool AddAlpha();
bool BuildMipmap(const MemImage& sourceMip);
bool SaveMipDebugImage(const char* pszBaseFilename, const MemImage* mips, int mipCount);
public:
static DWORD CalculateBufferBytes(DWORD width, DWORD height, bool bHasAlpha, bool bPalettized)
{
DWORD pixelCount = width*height;
DWORD bpp = 1;
if (bPalettized)
{
if (bHasAlpha)
bpp = 2;
}
else
{
if (bHasAlpha)
bpp = 4;
else
bpp = 3;
}
return pixelCount * bpp;
}
};
#endif // MEMIMAGE_H