forked from AliveTeam/alive_reversing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PNGFile.cpp
356 lines (300 loc) · 8.24 KB
/
PNGFile.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
351
352
353
354
355
356
#include "PNGFile.hpp"
#include "rgb_conversion.hpp"
#include "file_system.hpp"
#include "../../AliveLibCommon/logger.hpp"
#include "../../AliveLibCommon/FatalError.hpp"
#include "AnimationConverter.hpp"
#include <spng/spng.h>
class PngContext final
{
public:
enum class CtxType
{
Decode,
Encode,
};
explicit PngContext(CtxType type)
{
ctx = spng_ctx_new(type == CtxType::Encode ? SPNG_CTX_ENCODER : 0);
}
~PngContext()
{
if (ctx)
{
spng_ctx_free(ctx);
}
}
spng_ctx* ctx = nullptr;
};
class PngApi final
{
public:
explicit PngApi(PngContext::CtxType ctx)
: mCtx(ctx)
{
}
void DisableCRC()
{
// Don't calc CRCs
s32 ret = spng_set_crc_action(mCtx.ctx, SPNG_CRC_USE, SPNG_CRC_USE);
if (ret)
{
ALIVE_FATAL("spng_set_crc_action failed");
}
}
void SetFile(FILE* file)
{
s32 ret = spng_set_png_file(mCtx.ctx, file);
if (ret)
{
ALIVE_FATAL("spng_set_png_file failed");
}
}
void LimitChunkSize()
{
// Memory limit for unknown chunks
const std::size_t limit = 1024 * 1024 * 64;
s32 ret = spng_set_chunk_limits(mCtx.ctx, limit, limit);
if (ret)
{
ALIVE_FATAL("spng_set_chunk_limits failed");
}
}
spng_ihdr GetHeader()
{
spng_ihdr ihdr;
s32 ret = spng_get_ihdr(mCtx.ctx, &ihdr);
if (ret)
{
ALIVE_FATAL("spng_get_ihdr() error: %s", spng_strerror(ret));
}
return ihdr;
}
void SetHeader(spng_ihdr& hdr)
{
s32 ret = spng_set_ihdr(mCtx.ctx, &hdr);
if (ret)
{
ALIVE_FATAL("spng_set_ihdr failed");
}
}
spng_plte GetPal()
{
// Real pal, if any
spng_plte plte = {};
s32 ret = spng_get_plte(mCtx.ctx, &plte);
if (ret)
{
if (ret == SPNG_ECHUNKAVAIL)
{
ALIVE_FATAL("PNG has no pal (not an indexed png");
}
ALIVE_FATAL("Failed to load png pal");
}
return plte;
}
void SetPal(spng_plte& pal)
{
s32 ret = spng_set_plte(mCtx.ctx, &pal);
if (ret)
{
ALIVE_FATAL("spng_set_plte failed");
}
}
spng_trns GetTrans()
{
spng_trns trns = {};
s32 ret = spng_get_trns(mCtx.ctx, &trns);
if (ret && ret != SPNG_ECHUNKAVAIL)
{
ALIVE_FATAL("spng_get_trns failed");
}
return trns;
}
void SetTrans(spng_trns& trans)
{
s32 ret = spng_set_trns(mCtx.ctx, &trans);
if (ret)
{
ALIVE_FATAL("spng_set_trns failed");
}
}
std::size_t ImageSize()
{
std::size_t image_size = 0;
s32 ret = spng_decoded_image_size(mCtx.ctx, SPNG_FMT_PNG, &image_size);
if (ret)
{
ALIVE_FATAL("Failed to get decoded png size");
}
return image_size;
}
PngContext mCtx;
};
static int spng_rw_fn_impl(spng_ctx* /*ctx*/, void* user, void* dst_src, size_t length)
{
PNGFile* ptr = (PNGFile*) user;
if (ptr->mWrite)
{
for (size_t i = 0; i < length; i++)
{
ptr->mOutBuffer.push_back(((u8*) dst_src)[i]);
}
}
else
{
memcpy(dst_src, ptr->mPtr, length);
ptr->mPtr += static_cast<u32>(length);
}
return 0;
}
std::vector<u8> PNGFile::Encode(const u32* pixelData, u32 width, u32 height)
{
PngApi api(PngContext::CtxType::Encode);
mWrite = true;
mOutBuffer.clear();
mOffset = 0;
mPtr = (u8*)reinterpret_cast<const u8*>(pixelData);
spng_set_png_stream(api.mCtx.ctx, spng_rw_fn_impl, this);
spng_ihdr header = {};
header.width = width;
header.height = height;
header.color_type = SPNG_COLOR_TYPE_TRUECOLOR_ALPHA;
header.bit_depth = 8;
api.SetHeader(header);
std::size_t len = api.ImageSize();
s32 ret = spng_encode_image(api.mCtx.ctx, pixelData, len, SPNG_FMT_PNG, SPNG_ENCODE_FINALIZE);
if (ret)
{
ALIVE_FATAL("spng_encode_image failed");
}
return this->mOutBuffer;
}
void PNGFile::Decode(const std::vector<u8>& pngData, std::vector<u8>& rawPixels, u32& width, u32& height)
{
PngApi api(PngContext::CtxType::Decode);
mWrite = false;
mOutBuffer.clear();
mOffset = 0;
mPtr = (u8*)pngData.data();
spng_set_png_stream(api.mCtx.ctx, spng_rw_fn_impl, this);
spng_ihdr header = api.GetHeader();
width = header.width;
height = header.height;
std::size_t image_size = api.ImageSize();
rawPixels.resize(image_size);
s32 ret = spng_decode_image(api.mCtx.ctx, rawPixels.data(), rawPixels.size(), SPNG_FMT_PNG, 0);
if (ret)
{
ALIVE_FATAL("spng_decode_image failed");
}
}
void PNGFile::Load(const char_type* pFileName, std::vector<u8>& pixelData, u32& width, u32& height)
{
FileSystem fs;
std::vector<u8> buffer;
fs.LoadToVec(pFileName, buffer);
Decode(buffer, pixelData, width, height);
}
void PNGFile::Load(const char_type* pFileName, AnimationPal& pal256, std::vector<u8>& pixelData, u32& width, u32& height)
{
AutoFILE f;
f.Open(pFileName, "rb", false);
PngApi api(PngContext::CtxType::Decode);
api.DisableCRC();
api.LimitChunkSize();
api.SetFile(f.GetFile());
const spng_ihdr ihdr = api.GetHeader();
width = ihdr.width;
height = ihdr.height;
spng_plte plte = api.GetPal();
spng_trns trns = api.GetTrans();
for (u32 i = 0; i < plte.n_entries; i++)
{
u8 alpha = 0;
spng_plte_entry* color = &plte.entries[i];
bool isBlack = color->red == 0 && color->green == 0 && color->blue == 0;
if (i < trns.n_type3_entries)
{
alpha = trns.type3_alpha[i];
}
if (alpha == 0)
{
pal256.mPal[i] = {0, 0, 0, 0};
}
else if (alpha == 255)
{
pal256.mPal[i] = {
color->red,
color->green,
color->blue,
static_cast<u8>(isBlack ? 255 : 0)
};
}
else
{
pal256.mPal[i] = {
color->red,
color->green,
color->blue,
255
};
}
}
if (ihdr.color_type != SPNG_COLOR_TYPE_INDEXED)
{
ALIVE_FATAL("PNG is not indexed");
}
std::size_t image_size = api.ImageSize();
pixelData.resize(image_size);
s32 ret = spng_decode_image(api.mCtx.ctx, pixelData.data(), pixelData.size(), SPNG_FMT_PNG, 0);
if (ret)
{
ALIVE_FATAL("spng_decode_image failed");
}
}
void PNGFile::Save(const char_type* pFileName, const AnimationPal& pal256, const std::vector<u8>& pixelData, u32 width, u32 height)
{
// The TGA header uses a var length id string which means we can't just use
// a struct to represent it since the alignment is not fixed until after this field.
AutoFILE f;
f.Open(pFileName, "wb", false);
PngApi api(PngContext::CtxType::Encode);
api.SetFile(f.GetFile());
spng_ihdr ihdr = {};
ihdr.width = width;
ihdr.height = height;
ihdr.color_type = SPNG_COLOR_TYPE_INDEXED;
ihdr.bit_depth = 8;
api.SetHeader(ihdr);
spng_plte plte = {};
spng_trns trns = {};
trns.n_type3_entries = 255;
plte.n_entries = 255;
for (u32 i = 0; i < 255; i++)
{
const RGBA32* color = &pal256.mPal[i];
if (color->a == 255) // STP 1
{
plte.entries[i].red = color->r;
plte.entries[i].green = color->g;
plte.entries[i].blue = color->b;
trns.type3_alpha[i] = 127;
}
else
{
bool isBlack = color->r == 0 && color->g == 0 && color->b == 0;
plte.entries[i].red = color->r;
plte.entries[i].green = color->g;
plte.entries[i].blue = color->b;
trns.type3_alpha[i] = isBlack ? 0 : 255;
}
}
api.SetPal(plte);
api.SetTrans(trns);
s32 ret = spng_encode_image(api.mCtx.ctx, pixelData.data(), pixelData.size(), SPNG_FMT_PNG, SPNG_ENCODE_FINALIZE);
if (ret)
{
ALIVE_FATAL("spng_encode_image failed");
}
}