-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathDC6.cpp
350 lines (290 loc) · 7.61 KB
/
DC6.cpp
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "DC6.hpp"
#include "Logging.hpp"
#include "FileSystem.hpp"
#define DC6_HEADER_VERSION 6
#define DECODE_BUFFER_SIZE 2048 * 2048
namespace DC6
{
static BYTE gpDecodeBuffer[DECODE_BUFFER_SIZE];
static BYTE gpReadBuffer[DECODE_BUFFER_SIZE];
/*
* Decode a DC6 frame in place
* @author Necrolis/eezstreet
* TODO: optimize this a lot
*/
static void DecodeFrame(BYTE* pPixels, BYTE* pOutPixels, DC6Frame* pFrame)
{
DWORD x = 0, y;
if (pFrame->fh.dwFlip > 0)
{
y = 0;
}
else
{
y = pFrame->fh.dwHeight - 1;
}
for (size_t i = 0; i < pFrame->fh.dwLength; i++)
{
BYTE pixel = pPixels[i];
if (pixel == 0x80)
{ // pixel row termination
x = 0;
if (pFrame->fh.dwFlip > 0)
{
y++;
}
else
{
y--;
}
}
else if (pixel & 0x80)
{ // Write PIXEL & 0x7F transparent pixels
x += pixel & 0x7F;
}
else
{
while (pixel--)
{
pOutPixels[(y * pFrame->fh.dwWidth) + x++] = pPixels[++i];
}
}
}
}
/*
* Loads a DC6 from an MPQ
* @author eezstreet
*/
void LoadImage(const char* szPath, DC6Image* pImage)
{
int i, j;
size_t dwFileSize;
BYTE* pByteReadHead = gpReadBuffer;
DWORD* pFramePointers;
DWORD dwFramePos, dwNumFrames;
DWORD dwOffset = 0, dwTotalPixels = 0, dwWidth = 0, dwHeight = 0, dwDirectionWidth = 0, dwDirectionHeight = 0;
memset(pImage, 0, sizeof(DC6Image));
memset(gpDecodeBuffer, 0, sizeof(BYTE) * DECODE_BUFFER_SIZE);
dwFileSize = FS::Open(szPath, &pImage->f, FS_READ, true);
Log_WarnAssert(pImage->f != INVALID_HANDLE);
Log_WarnAssert(dwFileSize < DECODE_BUFFER_SIZE);
// Now comes the fun part: reading and decoding the actual thing
FS::Read(pImage->f, gpReadBuffer, DECODE_BUFFER_SIZE);
FS::CloseFile(pImage->f);
memcpy(&pImage->header, pByteReadHead, sizeof(pImage->header));
pByteReadHead += sizeof(pImage->header);
// Validate the header
Log_WarnAssert(pImage->header.dwVersion == DC6_HEADER_VERSION);
// Table of pointers
pFramePointers = (DWORD*)pByteReadHead;
dwNumFrames = pImage->header.dwDirections * pImage->header.dwFrames;
pByteReadHead += sizeof(DWORD) * dwNumFrames;
pImage->pFrames = (DC6Frame*)malloc(sizeof(DC6Frame) * dwNumFrames);
Log_ErrorAssert(pImage->pFrames != nullptr);
pImage->dwMaxFrameWidth = 0;
pImage->dwMaxFrameHeight = 0;
// Read each frame's header, and then decode the blocks
for (i = 0; i < pImage->header.dwDirections; i++)
{
for (j = 0; j < pImage->header.dwFrames; j++)
{
DC6Frame* pFrame;
dwFramePos = (i * pImage->header.dwFrames) + j;
pFrame = &pImage->pFrames[dwFramePos];
memcpy(&pFrame->fh, gpReadBuffer + pFramePointers[dwFramePos], sizeof(DC6Frame::DC6FrameHeader));
pFrame->fh.dwNextBlock = dwOffset;
if (j > 0)
{
pFrame->dwDeltaY = pImage->pFrames[j - 1].fh.dwHeight - pFrame->fh.dwHeight;
}
else
{
pFrame->dwDeltaY = 0;
}
pByteReadHead = gpReadBuffer + pFramePointers[dwFramePos] + sizeof(DC6Frame::DC6FrameHeader);
DecodeFrame(pByteReadHead, gpDecodeBuffer + dwOffset, pFrame);
dwTotalPixels += pFrame->fh.dwWidth * pFrame->fh.dwHeight;
dwOffset += pFrame->fh.dwWidth * pFrame->fh.dwHeight;
dwDirectionWidth += pFrame->fh.dwWidth;
if (pFrame->fh.dwHeight > dwDirectionHeight)
{
dwDirectionHeight = pFrame->fh.dwHeight;
}
if (pFrame->fh.dwWidth > pImage->dwMaxFrameWidth)
{
pImage->dwMaxFrameWidth = pFrame->fh.dwWidth;
}
if (pFrame->fh.dwHeight > pImage->dwMaxFrameHeight)
{
pImage->dwMaxFrameHeight = pFrame->fh.dwHeight;
}
}
if (dwDirectionWidth > dwWidth)
{
dwWidth = dwDirectionWidth;
}
dwHeight += dwDirectionHeight;
pImage->dwDirectionHeights[i] = dwDirectionHeight;
dwDirectionWidth = 0;
dwDirectionHeight = 0;
}
pImage->dwTotalWidth = dwWidth;
pImage->dwTotalHeight = dwHeight;
// Allocate the pixels that we need
pImage->pPixels = (BYTE*)malloc(dwTotalPixels);
Log_ErrorAssert(pImage->pPixels != nullptr);
memcpy(pImage->pPixels, gpDecodeBuffer, dwTotalPixels);
pImage->bPixelsFreed = false;
}
/*
* Frees a DC6 resource
* @author eezstreet
*/
void UnloadImage(DC6Image* pImage)
{
if (!pImage->bPixelsFreed)
{
free(pImage->pPixels);
}
free(pImage->pFrames);
}
/*
* Frees up a DC6's pixels. They won't be accessible after this, so be careful!
* @author eezstreet
*/
void FreePixels(DC6Image* pImage)
{
if (pImage->bPixelsFreed)
{
return;
}
free(pImage->pPixels);
pImage->bPixelsFreed = true;
}
/*
* Get pointer to pixels at specific frame, and how many pixels there are
*/
BYTE* GetPixelsAtFrame(DC6Image* pImage, int nDirection, int nFrame, size_t* pNumPixels)
{
if (pImage == nullptr)
{
return nullptr;
}
if (nDirection >= pImage->header.dwDirections)
{
return nullptr;
}
if (nFrame >= pImage->header.dwFrames)
{
return nullptr;
}
DC6Frame* pFrame = &pImage->pFrames[(nDirection * pImage->header.dwFrames) + nFrame];
if (pNumPixels != nullptr)
{
*pNumPixels = pFrame->fh.dwWidth * pFrame->fh.dwHeight;
}
return (pImage->pPixels + pFrame->fh.dwNextBlock);
}
/*
* Retrieve some data about a DC6 frame
*/
void PollFrame(DC6Image* pImage, DWORD nDirection, DWORD nFrame,
DWORD* dwWidth, DWORD* dwHeight, DWORD* dwOffsetX, DWORD* dwOffsetY)
{
if (pImage == nullptr)
{
return;
}
if (nDirection >= pImage->header.dwDirections)
{
return;
}
if (nFrame >= pImage->header.dwFrames)
{
return;
}
DC6Frame* pFrame = &pImage->pFrames[(nDirection * pImage->header.dwFrames) + nFrame];
if (dwWidth != nullptr)
{
*dwWidth = pFrame->fh.dwWidth;
}
if (dwHeight != nullptr)
{
*dwHeight = pFrame->fh.dwHeight;
}
if (dwOffsetX != nullptr)
{
*dwOffsetX = pFrame->fh.dwOffsetX;
}
if (dwOffsetY != nullptr)
{
*dwOffsetY = pFrame->fh.dwOffsetY;
}
}
/**
* Get row width and height (for atlassed DC6)
*/
void AtlasStats(DC6Image* pImage, DWORD dwStart, DWORD dwEnd, DWORD* pTotalWidth, DWORD* pTotalHeight)
{
Log_WarnAssert(dwStart >= 0 && dwStart <= dwEnd && dwStart < pImage->header.dwFrames);
Log_WarnAssert(dwEnd >= 0 && dwEnd < pImage->header.dwFrames);
Log_WarnAssert(pTotalWidth && pTotalHeight);
*pTotalWidth = 0;
*pTotalHeight = 0;
int currentX = 0;
int maxX = 0;
int currentY = 0;
for (int i = dwStart; i <= dwEnd; i++)
{
currentX += pImage->dwMaxFrameWidth;
if (currentX + pImage->dwMaxFrameWidth > 4096)
{
maxX = currentX - pImage->dwMaxFrameWidth;
currentX = 0;
currentY += pImage->dwMaxFrameHeight;
}
}
if (maxX == 0)
{
maxX = currentX;
}
*pTotalWidth = maxX + pImage->dwMaxFrameWidth;
*pTotalHeight = currentY + pImage->dwMaxFrameHeight;
}
/*
* Retrieve row width and height (for stitched DC6)
*/
void StitchStats(DC6Image* pImage,
DWORD dwStart, DWORD dwEnd, DWORD* pWidth, DWORD* pHeight, DWORD* pTotalWidth, DWORD* pTotalHeight)
{
DC6Frame* pFrame;
Log_WarnAssert(dwStart >= 0 && dwStart <= dwEnd && dwStart < pImage->header.dwFrames);
Log_WarnAssert(dwEnd >= 0 && dwEnd < pImage->header.dwFrames);
Log_WarnAssert(pWidth && pHeight);
Log_WarnAssert(pTotalWidth && pTotalHeight);
*pWidth = 0;
*pHeight = 0;
*pTotalWidth = 0;
*pTotalHeight = 0;
for (int i = dwStart; i <= dwEnd; i++)
{
pFrame = &pImage->pFrames[i];
(*pWidth)++;
(*pTotalWidth) += pFrame->fh.dwWidth;
if (pFrame->fh.dwWidth != MAX_DC6_CELL_SIZE)
{
break;
}
}
for (int i = dwStart; i <= dwEnd; i += (*pWidth))
{
pFrame = &pImage->pFrames[i];
(*pHeight)++;
(*pTotalHeight) += pFrame->fh.dwHeight;
if (pFrame->fh.dwHeight != MAX_DC6_CELL_SIZE)
{
break;
}
}
}
}