-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathpng.c
413 lines (356 loc) · 12.4 KB
/
png.c
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright 2020 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------
// Uncomment one of these #define lines to test and bench alternative mimic
// libraries (libspng, lodepng or stb_image) instead of libpng.
//
// These are collectively referred to as
// WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBPNG.
//
// #define WUFFS_MIMICLIB_USE_LIBSPNG_INSTEAD_OF_LIBPNG 1
// #define WUFFS_MIMICLIB_USE_LODEPNG_INSTEAD_OF_LIBPNG 1
// #define WUFFS_MIMICLIB_USE_STB_IMAGE_INSTEAD_OF_LIBPNG 1
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBPNG
#if defined(WUFFS_MIMICLIB_USE_LIBSPNG_INSTEAD_OF_LIBPNG)
// We #include a foo.c file, not a foo.h file, as libspng is a "single file C
// library".
#include "/path/to/your/copy/of/github.com/randy408/libspng/spng/spng.c"
// We deliberately do not define the
// WUFFS_MIMICLIB_PNG_DOES_NOT_SUPPORT_QUIRK_IGNORE_CHECKSUM macro.
// We deliberately do not define the
// WUFFS_MIMICLIB_PNG_DOES_NOT_VERIFY_CHECKSUM macro.
// libspng (version 0.6.2, released November 2020) calculates but does not
// verify the CRC-32 checksum on the final IDAT chunk. It also does not verify
// the Adler-32 checksum. After calling spng_decode_image, it ends in
// SPNG_STATE_EOI but not a later state such as SPNG_STATE_AFTER_IDAT.
//
// libspng commit 9c35dc3 "fix handling of SPNG_CTX_IGNORE_ADLER32", submitted
// December 2020, fixed Adler-32 but not CRC-32.
#define WUFFS_MIMICLIB_PNG_DOES_NOT_VERIFY_FINAL_IDAT_CHECKSUMS 1
const char* //
mimic_png_decode(uint64_t* n_bytes_out,
wuffs_base__io_buffer* dst,
uint32_t wuffs_initialize_flags,
wuffs_base__pixel_format pixfmt,
uint32_t* quirks_ptr,
size_t quirks_len,
wuffs_base__io_buffer* src) {
wuffs_base__io_buffer dst_fallback =
wuffs_base__slice_u8__writer(g_mimiclib_scratch_slice_u8);
if (!dst) {
dst = &dst_fallback;
}
const char* ret = NULL;
spng_ctx* ctx = spng_ctx_new(0);
size_t i;
for (i = 0; i < quirks_len; i++) {
uint32_t q = quirks_ptr[i];
if (q == WUFFS_BASE__QUIRK_IGNORE_CHECKSUM) {
spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE);
}
}
if (spng_set_png_buffer(ctx, wuffs_base__io_buffer__reader_pointer(src),
wuffs_base__io_buffer__reader_length(src))) {
ret = "mimic_png_decode: spng_set_png_buffer failed";
goto cleanup0;
}
int fmt = 0;
bool swap_bgra_rgba = false;
switch (pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__Y:
fmt = SPNG_FMT_G8;
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
// libspng doesn't do BGRA8. RGBA8 is the closest approximation. We'll
// fix it up later.
fmt = SPNG_FMT_RGBA8;
swap_bgra_rgba = true;
break;
default:
ret = "mimic_png_decode: unsupported pixfmt";
goto cleanup0;
}
size_t n = 0;
if (spng_decoded_image_size(ctx, fmt, &n)) {
ret = "mimic_png_decode: spng_decoded_image_size failed";
goto cleanup0;
}
if (n > wuffs_base__io_buffer__writer_length(dst)) {
ret = "mimic_png_decode: image is too large";
goto cleanup0;
}
uint8_t* dst_ptr = wuffs_base__io_buffer__writer_pointer(dst);
if (spng_decode_image(ctx, dst_ptr, n, fmt, 0)) {
ret = "mimic_png_decode: spng_decode_image failed";
goto cleanup0;
}
dst->meta.wi += n;
if (n_bytes_out) {
*n_bytes_out += n;
}
// Fix up BGRA8 vs RGBA8.
if (swap_bgra_rgba) {
for (; n >= 4; n -= 4) {
uint8_t swap = dst_ptr[0];
dst_ptr[0] = dst_ptr[2];
dst_ptr[2] = swap;
dst_ptr += 4;
}
}
cleanup0:;
spng_ctx_free(ctx);
return ret;
}
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBPNG
#elif defined(WUFFS_MIMICLIB_USE_LODEPNG_INSTEAD_OF_LIBPNG)
// We #include a foo.cpp file, not a foo.h file, as lodepng is a "single file
// C++ library".
#include "/path/to/your/copy/of/github.com/lvandeve/lodepng/lodepng.cpp"
#define WUFFS_MIMICLIB_PNG_DOES_NOT_SUPPORT_QUIRK_IGNORE_CHECKSUM 1
// We deliberately do not define the
// WUFFS_MIMICLIB_PNG_DOES_NOT_VERIFY_CHECKSUM macro.
// We deliberately do not define the
// WUFFS_MIMICLIB_PNG_DOES_NOT_VERIFY_FINAL_IDAT_CHECKSUMS macro.
const char* //
mimic_png_decode(uint64_t* n_bytes_out,
wuffs_base__io_buffer* dst,
uint32_t wuffs_initialize_flags,
wuffs_base__pixel_format pixfmt,
uint32_t* quirks_ptr,
size_t quirks_len,
wuffs_base__io_buffer* src) {
wuffs_base__io_buffer dst_fallback =
wuffs_base__slice_u8__writer(g_mimiclib_scratch_slice_u8);
if (!dst) {
dst = &dst_fallback;
}
uint64_t n = 0;
LodePNGColorType color_type = 0;
unsigned int bitdepth = 8;
bool swap_bgra_rgba = false;
switch (pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__Y:
n = 1;
color_type = LCT_GREY;
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
n = 4;
// lodepng doesn't do BGRA8. RGBA8 is the closest approximation. We'll
// fix it up later.
color_type = LCT_RGBA;
swap_bgra_rgba = true;
break;
default:
return "mimic_png_decode: unsupported pixfmt";
}
unsigned char* output = 0;
unsigned int width = 0;
unsigned int height = 0;
unsigned int err =
lodepng_decode_memory(&output, &width, &height, //
wuffs_base__io_buffer__reader_pointer(src), //
wuffs_base__io_buffer__reader_length(src), //
color_type, bitdepth);
if (err) {
return lodepng_error_text(err);
}
const char* ret = NULL;
if ((width > 0xFFFF) || (height > 0xFFFF)) {
ret = "mimic_png_decode: image is too large";
goto cleanup0;
}
n *= ((uint64_t)width) * ((uint64_t)height);
if (n > wuffs_base__io_buffer__writer_length(dst)) {
ret = "mimic_png_decode: image is too large";
goto cleanup0;
}
// Copy from the mimic library's output buffer to Wuffs' dst buffer.
uint8_t* dst_ptr = wuffs_base__io_buffer__writer_pointer(dst);
memcpy(dst_ptr, output, n);
dst->meta.wi += n;
if (n_bytes_out) {
*n_bytes_out += n;
}
// Fix up BGRA8 vs RGBA8.
if (swap_bgra_rgba) {
for (; n >= 4; n -= 4) {
uint8_t swap = dst_ptr[0];
dst_ptr[0] = dst_ptr[2];
dst_ptr[2] = swap;
dst_ptr += 4;
}
}
cleanup0:;
free(output);
return ret;
}
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBPNG
#elif defined(WUFFS_MIMICLIB_USE_STB_IMAGE_INSTEAD_OF_LIBPNG)
// We #include a foo.cpp file, not a foo.h file, as stb_image is a "single file
// C library".
#define STB_IMAGE_IMPLEMENTATION
#include "/path/to/your/copy/of/github.com/nothings/stb/stb_image.h"
// We deliberately do not define the
// WUFFS_MIMICLIB_PNG_DOES_NOT_SUPPORT_QUIRK_IGNORE_CHECKSUM macro. The
// stb_image library always ignores checksums.
#define WUFFS_MIMICLIB_PNG_DOES_NOT_VERIFY_CHECKSUM 1
// We deliberately do not define the
// WUFFS_MIMICLIB_PNG_DOES_NOT_VERIFY_FINAL_IDAT_CHECKSUMS macro.
const char* //
mimic_png_decode(uint64_t* n_bytes_out,
wuffs_base__io_buffer* dst,
uint32_t wuffs_initialize_flags,
wuffs_base__pixel_format pixfmt,
uint32_t* quirks_ptr,
size_t quirks_len,
wuffs_base__io_buffer* src) {
wuffs_base__io_buffer dst_fallback =
wuffs_base__slice_u8__writer(g_mimiclib_scratch_slice_u8);
if (!dst) {
dst = &dst_fallback;
}
uint64_t n = 0;
bool swap_bgra_rgba = false;
switch (pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__Y:
n = 1;
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
n = 4;
// stb_image doesn't do BGRA8. RGBA8 is the closest approximation. We'll
// fix it up later.
swap_bgra_rgba = true;
break;
default:
return "mimic_png_decode: unsupported pixfmt";
}
int width = 0;
int height = 0;
int channels_in_file = 0;
unsigned char* output =
stbi_load_from_memory(wuffs_base__io_buffer__reader_pointer(src), //
wuffs_base__io_buffer__reader_length(src), //
&width, &height, &channels_in_file, n);
if (!output) {
return "mimic_png_decode: could not load image";
}
const char* ret = NULL;
if ((width > 0xFFFF) || (height > 0xFFFF)) {
ret = "mimic_png_decode: image is too large";
goto cleanup0;
}
n *= ((uint64_t)width) * ((uint64_t)height);
if (n > wuffs_base__io_buffer__writer_length(dst)) {
ret = "mimic_png_decode: image is too large";
goto cleanup0;
}
// Copy from the mimic library's output buffer to Wuffs' dst buffer.
uint8_t* dst_ptr = wuffs_base__io_buffer__writer_pointer(dst);
memcpy(dst_ptr, output, n);
dst->meta.wi += n;
if (n_bytes_out) {
*n_bytes_out += n;
}
// Fix up BGRA8 vs RGBA8.
if (swap_bgra_rgba) {
for (; n >= 4; n -= 4) {
uint8_t swap = dst_ptr[0];
dst_ptr[0] = dst_ptr[2];
dst_ptr[2] = swap;
dst_ptr += 4;
}
}
cleanup0:;
stbi_image_free(output);
return ret;
}
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBPNG
#else
#include "png.h"
#define WUFFS_MIMICLIB_PNG_DOES_NOT_SUPPORT_QUIRK_IGNORE_CHECKSUM 1
// We deliberately do not define the
// WUFFS_MIMICLIB_PNG_DOES_NOT_VERIFY_CHECKSUM macro.
// We deliberately do not define the
// WUFFS_MIMICLIB_PNG_DOES_NOT_VERIFY_FINAL_IDAT_CHECKSUMS macro.
const char* //
mimic_png_decode(uint64_t* n_bytes_out,
wuffs_base__io_buffer* dst,
uint32_t wuffs_initialize_flags,
wuffs_base__pixel_format pixfmt,
uint32_t* quirks_ptr,
size_t quirks_len,
wuffs_base__io_buffer* src) {
wuffs_base__io_buffer dst_fallback =
wuffs_base__slice_u8__writer(g_mimiclib_scratch_slice_u8);
if (!dst) {
dst = &dst_fallback;
}
const char* ret = NULL;
png_image pi;
memset(&pi, 0, (sizeof pi));
pi.version = PNG_IMAGE_VERSION;
if (!png_image_begin_read_from_memory(&pi, src->data.ptr + src->meta.ri,
src->meta.wi - src->meta.ri)) {
ret = "mimic_png_decode: png_image_begin_read_from_memory failed";
goto cleanup0;
}
size_t i;
for (i = 0; i < quirks_len; i++) {
uint32_t q = quirks_ptr[i];
if (q == WUFFS_BASE__QUIRK_IGNORE_CHECKSUM) {
// TODO: configure libpng to ignore Adler-32 and CRC-32 checksums.
//
// The libpng "simplified API" (based on the png_image struct type) does
// not offer an equivalent of libpng's lower level API's
// png_set_crc_action and png_set_option(etc, PNG_IGNORE_ADLER32, etc).
// But the lower level API is complicated (hence why libpng introduced a
// "simplified API" in the first place), especially trying to make the
// equivalent of the png_image.format field work.
//
// Instead, we simply disable (via the
// WUFFS_MIMICLIB_PNG_DOES_NOT_SUPPORT_QUIRK_IGNORE_CHECKSUM macro) any
// tests or benches that try to use WUFFS_BASE__QUIRK_IGNORE_CHECKSUM.
}
}
switch (pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__Y:
pi.format = PNG_FORMAT_GRAY;
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
pi.format = PNG_FORMAT_BGRA;
break;
default:
ret = "mimic_png_decode: unsupported pixfmt";
goto cleanup0;
}
size_t n = PNG_IMAGE_SIZE(pi);
if (n > wuffs_base__io_buffer__writer_length(dst)) {
ret = "mimic_png_decode: image is too large";
goto cleanup0;
}
if (!png_image_finish_read(
&pi, NULL, wuffs_base__io_buffer__writer_pointer(dst), 0, NULL)) {
ret = "mimic_png_decode: png_image_finish_read failed";
goto cleanup0;
}
dst->meta.wi += n;
if (n_bytes_out) {
*n_bytes_out += n;
}
cleanup0:;
png_image_free(&pi);
return ret;
}
#endif
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBPNG