forked from HaloMods/SparkEdit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HaloBmp2.h
227 lines (193 loc) · 6.55 KB
/
HaloBmp2.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*-------------------------------------------------------------------
* Original Author: Grenadiac
*
* Contributing Authors:
*
* File Description:
*
*
*-----------------------------------------------------------------*/
#if !defined(AFX_HALOBMP_H__CE1D3B80_72DD_4649_8845_C1DCFBF3A291__INCLUDED_)
#define AFX_HALOBMP_H__CE1D3B80_72DD_4649_8845_C1DCFBF3A291__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "HaloStructDefs.h"
#include "Util.h"
typedef struct
{
char unk1[4];
UINT version;
UINT unk2[6];
char unk3[32];
float unk4[3];
UINT unk5[3];
//int unknown[22]; // [7] == [6]+108[9]
int offset_to_first;
int unknown23; // always 0x0
int image_count;
int image_offset;
int unknown25; // always 0x0
} bitm_header_t;
typedef struct
{
int unknown[16];
} bitm_first_t;
typedef struct
{
int id; // 'bitm'
short width;
short height;
short depth;
short type;
short format;
short flags;
short reg_point_x;
short reg_point_y;
short num_mipmaps;
short pixel_offset;
int offset;
int size;
int unknown8;
int unknown9; // always 0xFFFFFFFF?
int unknown10; // always 0x00000000?
int unknown11; // always 0x024F0040?
} bitm_image_t;
// formats
#define BITM_FORMAT_A8 0x00
#define BITM_FORMAT_Y8 0x01
#define BITM_FORMAT_AY8 0x02
#define BITM_FORMAT_A8Y8 0x03
#define BITM_FORMAT_R5G6B5 0x06
#define BITM_FORMAT_A1R5G5B5 0x08
#define BITM_FORMAT_A4R4G4B4 0x09
#define BITM_FORMAT_X8R8G8B8 0x0A
#define BITM_FORMAT_A8R8G8B8 0x0B
#define BITM_FORMAT_DXT1 0x0E
#define BITM_FORMAT_DXT2AND3 0x0F
#define BITM_FORMAT_DXT4AND5 0x10
#define BITM_FORMAT_P8 0x11
// Types
#define BITM_TYPE_2D 0x00
#define BITM_TYPE_3D 0x01
#define BITM_TYPE_CUBEMAP 0x02
// Flags
#define BITM_FLAG_LINEAR (1 << 4)
struct rgba_color_t
{
unsigned int r, g, b, a;
};
typedef struct
{
unsigned char id_length, colormap_type, image_type;
unsigned short colormap_index, colormap_length;
unsigned char colormap_size;
unsigned short x_org, y_org, width, height;
unsigned char pixel_size, attributes;
} MTarga_Header;
static unsigned int rgba_to_int (rgba_color_t color)
{
return (color.a << 24) | (color.r << 16) | (color.g << 8) | color.b;
}
static rgba_color_t short_to_rgba (unsigned short color)
{
rgba_color_t rc;
rc.r = (((color >> 11) & 0x1F) * 0xFF) / 31;
rc.g = (((color >> 5) & 0x3F) * 0xFF) / 63;
rc.b = (((color >> 0) & 0x1F) * 0xFF) / 31;
rc.a = 255;
return rc;
}
// Swizzle class. I've got no idea how this works, but it does. Thanks Stephen. :)
// Hmm, maybe there are some problems with it...
class Swizzler
{
public:
Swizzler(int Width, int Height, int Depth = 0)
{
m_MaskX = 0;
m_MaskY = 0;
m_MaskZ = 0;
for(int Bit(1), Idx(1); (Bit < Width) || (Bit < Height) || (Bit < Depth); Bit <<= 1)
{
if (Bit < Width)
{
m_MaskX |= Idx;
Idx <<= 1;
}
if (Bit < Height)
{
m_MaskY |= Idx;
Idx <<= 1;
}
if (Bit < Depth)
{
m_MaskZ |= Idx;
Idx <<= 1;
}
}
}
public:
unsigned int Swizzle(int Sx, int Sy, int Sz = -1)
{
return SwizzleAxis(Sx, m_MaskX) | SwizzleAxis(Sy, m_MaskY) | ((Sz != -1) ? SwizzleAxis(Sz, m_MaskZ) : 0);
}
private:
unsigned int SwizzleAxis(int Value, const int Mask)
{
unsigned int Result(0);
for(int Bit = 1; Bit <= Mask; Bit <<= 1)
{
if (Mask & Bit)
Result |= Value & Bit;
else
Value <<= 1;
}
return Result;
}
private:
int m_MaskX, m_MaskY, m_MaskZ;
};
class CHaloBmp : public CUtil
{
public:
BOOL DecompressDXT(bitm_image_t *pHeader,
int inbuf_size,
BYTE *pInBuf,
UINT *pOutBuf);
CHaloBmp();
virtual ~CHaloBmp();
void ReadHeader(CFile *pMapFile, INDEX_ITEM *item, int magic, CString Name);
void read_bitm(CFile *pMapFile, INDEX_ITEM *item, int magic, CString Name);
void writeTGA(UINT *texdata, int height, int width, CFile *pOutFile);
UINT m_Version;
protected:
rgba_color_t GradientColorsHalf (rgba_color_t Col1, rgba_color_t Col2);
rgba_color_t GradientColors (rgba_color_t Col1, rgba_color_t Col2);
void DecodeDXT1(int Height, int Width, byte* IData, unsigned int* PData);
void DecodeDXT2And3 (int Height, int Width, byte* IData, unsigned int* PData);
void DecodeDXT4And5 (int Height, int Width, byte* IData, unsigned int* PData);
void DecodeBitmSurface (byte *data, int width, int height, int depth, int format, int flags, UINT* pOutBuf);
int GetImageSize (int format, int width, int height);
void DecodeLinearA8 (int width, int height, byte *texdata, unsigned int *outdata);
void DecodeSwizzledA8 (int width, int height, int depth, byte *texdata, unsigned int *outdata);
void DecodeLinearY8 (int width, int height, byte *texdata, unsigned int *outdata);
void DecodeSwizzledY8 (int width, int height, int depth, byte *texdata, unsigned int *outdata);
void DecodeLinearAY8 (int width, int height, byte *texdata, unsigned int *outdata);
void DecodeSwizzledAY8 (int width, int height, int depth, byte *texdata, unsigned int *outdata);
void DecodeLinearA8Y8 (int width, int height, byte *texdata, unsigned int *outdata);
void DecodeSwizzledA8Y8 (int width, int height, int depth, byte *texdata, unsigned int *outdata);
void DecodeLinearR5G6B5 (int width, int height, byte *texdata, unsigned int *outdata);
void DecodeSwizzledR5G6B5 (int width, int height, int depth, byte *texdata, unsigned int *outdata);
void DecodeLinearA1R5G5B5 (int width, int height, byte *texdata, unsigned int *outdata);
void DecodeSwizzledA1R5G5B5 (int width, int height, int depth, byte *texdata, unsigned int *outdata);
void DecodeLinearA4R4G4B4 (int width, int height, byte *texdata, unsigned int *outdata);
void DecodeSwizzledA4R4G4B4 (int width, int height, int depth, byte *texdata, unsigned int *outdata);
void DecodeLinearX8R8G8B8 (int width, int height, byte *texdata, unsigned int *outdata);
void DecodeSwizzledX8R8G8B8 (int width, int height, int depth, byte *texdata, unsigned int *outdata);
void DecodeLinearA8R8G8B8 (int width, int height, byte *texdata, unsigned int *outdata);
void DecodeSwizzledA8R8G8B8 (int width, int height, int depth, byte *texdata, unsigned int *outdata);
void DecodeLinearP8 (int width, int height, byte *texdata, unsigned int *outdata);
void DecodeSwizzledP8 (int width, int height, int depth, byte *texdata, unsigned int *outdata);
};
#endif // !defined(AFX_HALOBMP_H__CE1D3B80_72DD_4649_8845_C1DCFBF3A291__INCLUDED_)