Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libpng/pngpread.c
9833 views
1
/* pngpread.c - read a png file in push mode
2
*
3
* Copyright (c) 2018-2025 Cosmin Truta
4
* Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
5
* Copyright (c) 1996-1997 Andreas Dilger
6
* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
7
*
8
* This code is released under the libpng license.
9
* For conditions of distribution and use, see the disclaimer
10
* and license in png.h
11
*/
12
13
#include "pngpriv.h"
14
15
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
16
17
/* Push model modes */
18
#define PNG_READ_SIG_MODE 0
19
#define PNG_READ_CHUNK_MODE 1
20
#define PNG_READ_IDAT_MODE 2
21
#define PNG_READ_tEXt_MODE 4
22
#define PNG_READ_zTXt_MODE 5
23
#define PNG_READ_DONE_MODE 6
24
#define PNG_READ_iTXt_MODE 7
25
#define PNG_ERROR_MODE 8
26
27
#define PNG_PUSH_SAVE_BUFFER_IF_FULL \
28
if (png_ptr->push_length + 4 > png_ptr->buffer_size) \
29
{ png_push_save_buffer(png_ptr); return; }
30
#define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \
31
if (png_ptr->buffer_size < N) \
32
{ png_push_save_buffer(png_ptr); return; }
33
34
#ifdef PNG_READ_INTERLACING_SUPPORTED
35
/* Arrays to facilitate interlacing - use pass (0 - 6) as index. */
36
37
/* Start of interlace block */
38
static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
39
/* Offset to next interlace block */
40
static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
41
/* Start of interlace block in the y direction */
42
static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
43
/* Offset to next interlace block in the y direction */
44
static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
45
46
/* TODO: Move these arrays to a common utility module to avoid duplication. */
47
#endif
48
49
void PNGAPI
50
png_process_data(png_structrp png_ptr, png_inforp info_ptr,
51
png_bytep buffer, size_t buffer_size)
52
{
53
if (png_ptr == NULL || info_ptr == NULL)
54
return;
55
56
png_push_restore_buffer(png_ptr, buffer, buffer_size);
57
58
while (png_ptr->buffer_size)
59
{
60
png_process_some_data(png_ptr, info_ptr);
61
}
62
}
63
64
size_t PNGAPI
65
png_process_data_pause(png_structrp png_ptr, int save)
66
{
67
if (png_ptr != NULL)
68
{
69
/* It's easiest for the caller if we do the save; then the caller doesn't
70
* have to supply the same data again:
71
*/
72
if (save != 0)
73
png_push_save_buffer(png_ptr);
74
else
75
{
76
/* This includes any pending saved bytes: */
77
size_t remaining = png_ptr->buffer_size;
78
png_ptr->buffer_size = 0;
79
80
/* So subtract the saved buffer size, unless all the data
81
* is actually 'saved', in which case we just return 0
82
*/
83
if (png_ptr->save_buffer_size < remaining)
84
return remaining - png_ptr->save_buffer_size;
85
}
86
}
87
88
return 0;
89
}
90
91
png_uint_32 PNGAPI
92
png_process_data_skip(png_structrp png_ptr)
93
{
94
/* TODO: Deprecate and remove this API.
95
* Somewhere the implementation of this seems to have been lost,
96
* or abandoned. It was only to support some internal back-door access
97
* to png_struct) in libpng-1.4.x.
98
*/
99
png_app_warning(png_ptr,
100
"png_process_data_skip is not implemented in any current version of libpng");
101
return 0;
102
}
103
104
/* What we do with the incoming data depends on what we were previously
105
* doing before we ran out of data...
106
*/
107
void /* PRIVATE */
108
png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
109
{
110
if (png_ptr == NULL)
111
return;
112
113
switch (png_ptr->process_mode)
114
{
115
case PNG_READ_SIG_MODE:
116
{
117
png_push_read_sig(png_ptr, info_ptr);
118
break;
119
}
120
121
case PNG_READ_CHUNK_MODE:
122
{
123
png_push_read_chunk(png_ptr, info_ptr);
124
break;
125
}
126
127
case PNG_READ_IDAT_MODE:
128
{
129
png_push_read_IDAT(png_ptr);
130
break;
131
}
132
133
default:
134
{
135
png_ptr->buffer_size = 0;
136
break;
137
}
138
}
139
}
140
141
/* Read any remaining signature bytes from the stream and compare them with
142
* the correct PNG signature. It is possible that this routine is called
143
* with bytes already read from the signature, either because they have been
144
* checked by the calling application, or because of multiple calls to this
145
* routine.
146
*/
147
void /* PRIVATE */
148
png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
149
{
150
size_t num_checked = png_ptr->sig_bytes; /* SAFE, does not exceed 8 */
151
size_t num_to_check = 8 - num_checked;
152
153
if (png_ptr->buffer_size < num_to_check)
154
{
155
num_to_check = png_ptr->buffer_size;
156
}
157
158
png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
159
num_to_check);
160
png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
161
162
if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
163
{
164
if (num_checked < 4 &&
165
png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4) != 0)
166
png_error(png_ptr, "Not a PNG file");
167
168
else
169
png_error(png_ptr, "PNG file corrupted by ASCII conversion");
170
}
171
else
172
{
173
if (png_ptr->sig_bytes >= 8)
174
{
175
png_ptr->process_mode = PNG_READ_CHUNK_MODE;
176
}
177
}
178
}
179
180
void /* PRIVATE */
181
png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
182
{
183
png_uint_32 chunk_name;
184
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
185
int keep; /* unknown handling method */
186
#endif
187
188
/* First we make sure we have enough data for the 4-byte chunk name
189
* and the 4-byte chunk length before proceeding with decoding the
190
* chunk data. To fully decode each of these chunks, we also make
191
* sure we have enough data in the buffer for the 4-byte CRC at the
192
* end of every chunk (except IDAT, which is handled separately).
193
*/
194
if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
195
{
196
PNG_PUSH_SAVE_BUFFER_IF_LT(8)
197
png_ptr->push_length = png_read_chunk_header(png_ptr);
198
png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
199
}
200
201
chunk_name = png_ptr->chunk_name;
202
203
if (chunk_name == png_IDAT)
204
{
205
if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
206
png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
207
208
/* If we reach an IDAT chunk, this means we have read all of the
209
* header chunks, and we can start reading the image (or if this
210
* is called after the image has been read - we have an error).
211
*/
212
if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
213
png_error(png_ptr, "Missing IHDR before IDAT");
214
215
else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
216
(png_ptr->mode & PNG_HAVE_PLTE) == 0)
217
png_error(png_ptr, "Missing PLTE before IDAT");
218
219
png_ptr->process_mode = PNG_READ_IDAT_MODE;
220
221
if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
222
if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
223
if (png_ptr->push_length == 0)
224
return;
225
226
png_ptr->mode |= PNG_HAVE_IDAT;
227
228
if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
229
png_benign_error(png_ptr, "Too many IDATs found");
230
}
231
232
if (chunk_name == png_IHDR)
233
{
234
if (png_ptr->push_length != 13)
235
png_error(png_ptr, "Invalid IHDR length");
236
237
PNG_PUSH_SAVE_BUFFER_IF_FULL
238
png_handle_chunk(png_ptr, info_ptr, png_ptr->push_length);
239
}
240
241
else if (chunk_name == png_IEND)
242
{
243
PNG_PUSH_SAVE_BUFFER_IF_FULL
244
png_handle_chunk(png_ptr, info_ptr, png_ptr->push_length);
245
246
png_ptr->process_mode = PNG_READ_DONE_MODE;
247
png_push_have_end(png_ptr, info_ptr);
248
}
249
250
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
251
else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
252
{
253
PNG_PUSH_SAVE_BUFFER_IF_FULL
254
png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
255
256
if (chunk_name == png_PLTE)
257
png_ptr->mode |= PNG_HAVE_PLTE;
258
}
259
#endif
260
261
else if (chunk_name == png_IDAT)
262
{
263
png_ptr->idat_size = png_ptr->push_length;
264
png_ptr->process_mode = PNG_READ_IDAT_MODE;
265
png_push_have_info(png_ptr, info_ptr);
266
png_ptr->zstream.avail_out =
267
(uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
268
png_ptr->iwidth) + 1;
269
png_ptr->zstream.next_out = png_ptr->row_buf;
270
return;
271
}
272
273
else
274
{
275
PNG_PUSH_SAVE_BUFFER_IF_FULL
276
png_handle_chunk(png_ptr, info_ptr, png_ptr->push_length);
277
}
278
279
png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
280
}
281
282
void PNGCBAPI
283
png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, size_t length)
284
{
285
png_bytep ptr;
286
287
if (png_ptr == NULL)
288
return;
289
290
ptr = buffer;
291
if (png_ptr->save_buffer_size != 0)
292
{
293
size_t save_size;
294
295
if (length < png_ptr->save_buffer_size)
296
save_size = length;
297
298
else
299
save_size = png_ptr->save_buffer_size;
300
301
memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
302
length -= save_size;
303
ptr += save_size;
304
png_ptr->buffer_size -= save_size;
305
png_ptr->save_buffer_size -= save_size;
306
png_ptr->save_buffer_ptr += save_size;
307
}
308
if (length != 0 && png_ptr->current_buffer_size != 0)
309
{
310
size_t save_size;
311
312
if (length < png_ptr->current_buffer_size)
313
save_size = length;
314
315
else
316
save_size = png_ptr->current_buffer_size;
317
318
memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
319
png_ptr->buffer_size -= save_size;
320
png_ptr->current_buffer_size -= save_size;
321
png_ptr->current_buffer_ptr += save_size;
322
}
323
}
324
325
void /* PRIVATE */
326
png_push_save_buffer(png_structrp png_ptr)
327
{
328
if (png_ptr->save_buffer_size != 0)
329
{
330
if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
331
{
332
size_t i, istop;
333
png_bytep sp;
334
png_bytep dp;
335
336
istop = png_ptr->save_buffer_size;
337
for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
338
i < istop; i++, sp++, dp++)
339
{
340
*dp = *sp;
341
}
342
}
343
}
344
if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
345
png_ptr->save_buffer_max)
346
{
347
size_t new_max;
348
png_bytep old_buffer;
349
350
if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
351
(png_ptr->current_buffer_size + 256))
352
{
353
png_error(png_ptr, "Potential overflow of save_buffer");
354
}
355
356
new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
357
old_buffer = png_ptr->save_buffer;
358
png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
359
(size_t)new_max);
360
361
if (png_ptr->save_buffer == NULL)
362
{
363
png_free(png_ptr, old_buffer);
364
png_error(png_ptr, "Insufficient memory for save_buffer");
365
}
366
367
if (old_buffer)
368
memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
369
else if (png_ptr->save_buffer_size)
370
png_error(png_ptr, "save_buffer error");
371
png_free(png_ptr, old_buffer);
372
png_ptr->save_buffer_max = new_max;
373
}
374
if (png_ptr->current_buffer_size)
375
{
376
memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
377
png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
378
png_ptr->save_buffer_size += png_ptr->current_buffer_size;
379
png_ptr->current_buffer_size = 0;
380
}
381
png_ptr->save_buffer_ptr = png_ptr->save_buffer;
382
png_ptr->buffer_size = 0;
383
}
384
385
void /* PRIVATE */
386
png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
387
size_t buffer_length)
388
{
389
png_ptr->current_buffer = buffer;
390
png_ptr->current_buffer_size = buffer_length;
391
png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
392
png_ptr->current_buffer_ptr = png_ptr->current_buffer;
393
}
394
395
void /* PRIVATE */
396
png_push_read_IDAT(png_structrp png_ptr)
397
{
398
if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
399
{
400
png_byte chunk_length[4];
401
png_byte chunk_tag[4];
402
403
/* TODO: this code can be commoned up with the same code in push_read */
404
PNG_PUSH_SAVE_BUFFER_IF_LT(8)
405
png_push_fill_buffer(png_ptr, chunk_length, 4);
406
png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
407
png_reset_crc(png_ptr);
408
png_crc_read(png_ptr, chunk_tag, 4);
409
png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
410
png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
411
412
if (png_ptr->chunk_name != png_IDAT)
413
{
414
png_ptr->process_mode = PNG_READ_CHUNK_MODE;
415
416
if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
417
png_error(png_ptr, "Not enough compressed data");
418
419
return;
420
}
421
422
png_ptr->idat_size = png_ptr->push_length;
423
}
424
425
if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
426
{
427
size_t save_size = png_ptr->save_buffer_size;
428
png_uint_32 idat_size = png_ptr->idat_size;
429
430
/* We want the smaller of 'idat_size' and 'current_buffer_size', but they
431
* are of different types and we don't know which variable has the fewest
432
* bits. Carefully select the smaller and cast it to the type of the
433
* larger - this cannot overflow. Do not cast in the following test - it
434
* will break on either 16-bit or 64-bit platforms.
435
*/
436
if (idat_size < save_size)
437
save_size = (size_t)idat_size;
438
439
else
440
idat_size = (png_uint_32)save_size;
441
442
png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
443
444
png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
445
446
png_ptr->idat_size -= idat_size;
447
png_ptr->buffer_size -= save_size;
448
png_ptr->save_buffer_size -= save_size;
449
png_ptr->save_buffer_ptr += save_size;
450
}
451
452
if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
453
{
454
size_t save_size = png_ptr->current_buffer_size;
455
png_uint_32 idat_size = png_ptr->idat_size;
456
457
/* We want the smaller of 'idat_size' and 'current_buffer_size', but they
458
* are of different types and we don't know which variable has the fewest
459
* bits. Carefully select the smaller and cast it to the type of the
460
* larger - this cannot overflow.
461
*/
462
if (idat_size < save_size)
463
save_size = (size_t)idat_size;
464
465
else
466
idat_size = (png_uint_32)save_size;
467
468
png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
469
470
png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
471
472
png_ptr->idat_size -= idat_size;
473
png_ptr->buffer_size -= save_size;
474
png_ptr->current_buffer_size -= save_size;
475
png_ptr->current_buffer_ptr += save_size;
476
}
477
478
if (png_ptr->idat_size == 0)
479
{
480
PNG_PUSH_SAVE_BUFFER_IF_LT(4)
481
png_crc_finish(png_ptr, 0);
482
png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
483
png_ptr->mode |= PNG_AFTER_IDAT;
484
png_ptr->zowner = 0;
485
}
486
}
487
488
void /* PRIVATE */
489
png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
490
size_t buffer_length)
491
{
492
/* The caller checks for a non-zero buffer length. */
493
if (!(buffer_length > 0) || buffer == NULL)
494
png_error(png_ptr, "No IDAT data (internal error)");
495
496
/* This routine must process all the data it has been given
497
* before returning, calling the row callback as required to
498
* handle the uncompressed results.
499
*/
500
png_ptr->zstream.next_in = buffer;
501
/* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
502
png_ptr->zstream.avail_in = (uInt)buffer_length;
503
504
/* Keep going until the decompressed data is all processed
505
* or the stream marked as finished.
506
*/
507
while (png_ptr->zstream.avail_in > 0 &&
508
(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
509
{
510
int ret;
511
512
/* We have data for zlib, but we must check that zlib
513
* has someplace to put the results. It doesn't matter
514
* if we don't expect any results -- it may be the input
515
* data is just the LZ end code.
516
*/
517
if (!(png_ptr->zstream.avail_out > 0))
518
{
519
/* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
520
png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
521
png_ptr->iwidth) + 1);
522
523
png_ptr->zstream.next_out = png_ptr->row_buf;
524
}
525
526
/* Using Z_SYNC_FLUSH here means that an unterminated
527
* LZ stream (a stream with a missing end code) can still
528
* be handled, otherwise (Z_NO_FLUSH) a future zlib
529
* implementation might defer output and therefore
530
* change the current behavior (see comments in inflate.c
531
* for why this doesn't happen at present with zlib 1.2.5).
532
*/
533
ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH);
534
535
/* Check for any failure before proceeding. */
536
if (ret != Z_OK && ret != Z_STREAM_END)
537
{
538
/* Terminate the decompression. */
539
png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
540
png_ptr->zowner = 0;
541
542
/* This may be a truncated stream (missing or
543
* damaged end code). Treat that as a warning.
544
*/
545
if (png_ptr->row_number >= png_ptr->num_rows ||
546
png_ptr->pass > 6)
547
png_warning(png_ptr, "Truncated compressed data in IDAT");
548
549
else
550
{
551
if (ret == Z_DATA_ERROR)
552
png_benign_error(png_ptr, "IDAT: ADLER32 checksum mismatch");
553
else
554
png_error(png_ptr, "Decompression error in IDAT");
555
}
556
557
/* Skip the check on unprocessed input */
558
return;
559
}
560
561
/* Did inflate output any data? */
562
if (png_ptr->zstream.next_out != png_ptr->row_buf)
563
{
564
/* Is this unexpected data after the last row?
565
* If it is, artificially terminate the LZ output
566
* here.
567
*/
568
if (png_ptr->row_number >= png_ptr->num_rows ||
569
png_ptr->pass > 6)
570
{
571
/* Extra data. */
572
png_warning(png_ptr, "Extra compressed data in IDAT");
573
png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
574
png_ptr->zowner = 0;
575
576
/* Do no more processing; skip the unprocessed
577
* input check below.
578
*/
579
return;
580
}
581
582
/* Do we have a complete row? */
583
if (png_ptr->zstream.avail_out == 0)
584
png_push_process_row(png_ptr);
585
}
586
587
/* And check for the end of the stream. */
588
if (ret == Z_STREAM_END)
589
png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
590
}
591
592
/* All the data should have been processed, if anything
593
* is left at this point we have bytes of IDAT data
594
* after the zlib end code.
595
*/
596
if (png_ptr->zstream.avail_in > 0)
597
png_warning(png_ptr, "Extra compression data in IDAT");
598
}
599
600
void /* PRIVATE */
601
png_push_process_row(png_structrp png_ptr)
602
{
603
/* 1.5.6: row_info moved out of png_struct to a local here. */
604
png_row_info row_info;
605
606
row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
607
row_info.color_type = png_ptr->color_type;
608
row_info.bit_depth = png_ptr->bit_depth;
609
row_info.channels = png_ptr->channels;
610
row_info.pixel_depth = png_ptr->pixel_depth;
611
row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
612
613
if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
614
{
615
if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
616
png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
617
png_ptr->prev_row + 1, png_ptr->row_buf[0]);
618
else
619
png_error(png_ptr, "bad adaptive filter value");
620
}
621
622
/* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
623
* 1.5.6, while the buffer really is this big in current versions of libpng
624
* it may not be in the future, so this was changed just to copy the
625
* interlaced row count:
626
*/
627
memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
628
629
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
630
if (png_ptr->transformations != 0)
631
png_do_read_transformations(png_ptr, &row_info);
632
#endif
633
634
/* The transformed pixel depth should match the depth now in row_info. */
635
if (png_ptr->transformed_pixel_depth == 0)
636
{
637
png_ptr->transformed_pixel_depth = row_info.pixel_depth;
638
if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
639
png_error(png_ptr, "progressive row overflow");
640
}
641
642
else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
643
png_error(png_ptr, "internal progressive row size calculation error");
644
645
646
#ifdef PNG_READ_INTERLACING_SUPPORTED
647
/* Expand interlaced rows to full size */
648
if (png_ptr->interlaced != 0 &&
649
(png_ptr->transformations & PNG_INTERLACE) != 0)
650
{
651
if (png_ptr->pass < 6)
652
png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
653
png_ptr->transformations);
654
655
switch (png_ptr->pass)
656
{
657
case 0:
658
{
659
int i;
660
for (i = 0; i < 8 && png_ptr->pass == 0; i++)
661
{
662
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
663
png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
664
}
665
666
if (png_ptr->pass == 2) /* Pass 1 might be empty */
667
{
668
for (i = 0; i < 4 && png_ptr->pass == 2; i++)
669
{
670
png_push_have_row(png_ptr, NULL);
671
png_read_push_finish_row(png_ptr);
672
}
673
}
674
675
if (png_ptr->pass == 4 && png_ptr->height <= 4)
676
{
677
for (i = 0; i < 2 && png_ptr->pass == 4; i++)
678
{
679
png_push_have_row(png_ptr, NULL);
680
png_read_push_finish_row(png_ptr);
681
}
682
}
683
684
if (png_ptr->pass == 6 && png_ptr->height <= 4)
685
{
686
png_push_have_row(png_ptr, NULL);
687
png_read_push_finish_row(png_ptr);
688
}
689
690
break;
691
}
692
693
case 1:
694
{
695
int i;
696
for (i = 0; i < 8 && png_ptr->pass == 1; i++)
697
{
698
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
699
png_read_push_finish_row(png_ptr);
700
}
701
702
if (png_ptr->pass == 2) /* Skip top 4 generated rows */
703
{
704
for (i = 0; i < 4 && png_ptr->pass == 2; i++)
705
{
706
png_push_have_row(png_ptr, NULL);
707
png_read_push_finish_row(png_ptr);
708
}
709
}
710
711
break;
712
}
713
714
case 2:
715
{
716
int i;
717
718
for (i = 0; i < 4 && png_ptr->pass == 2; i++)
719
{
720
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
721
png_read_push_finish_row(png_ptr);
722
}
723
724
for (i = 0; i < 4 && png_ptr->pass == 2; i++)
725
{
726
png_push_have_row(png_ptr, NULL);
727
png_read_push_finish_row(png_ptr);
728
}
729
730
if (png_ptr->pass == 4) /* Pass 3 might be empty */
731
{
732
for (i = 0; i < 2 && png_ptr->pass == 4; i++)
733
{
734
png_push_have_row(png_ptr, NULL);
735
png_read_push_finish_row(png_ptr);
736
}
737
}
738
739
break;
740
}
741
742
case 3:
743
{
744
int i;
745
746
for (i = 0; i < 4 && png_ptr->pass == 3; i++)
747
{
748
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
749
png_read_push_finish_row(png_ptr);
750
}
751
752
if (png_ptr->pass == 4) /* Skip top two generated rows */
753
{
754
for (i = 0; i < 2 && png_ptr->pass == 4; i++)
755
{
756
png_push_have_row(png_ptr, NULL);
757
png_read_push_finish_row(png_ptr);
758
}
759
}
760
761
break;
762
}
763
764
case 4:
765
{
766
int i;
767
768
for (i = 0; i < 2 && png_ptr->pass == 4; i++)
769
{
770
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
771
png_read_push_finish_row(png_ptr);
772
}
773
774
for (i = 0; i < 2 && png_ptr->pass == 4; i++)
775
{
776
png_push_have_row(png_ptr, NULL);
777
png_read_push_finish_row(png_ptr);
778
}
779
780
if (png_ptr->pass == 6) /* Pass 5 might be empty */
781
{
782
png_push_have_row(png_ptr, NULL);
783
png_read_push_finish_row(png_ptr);
784
}
785
786
break;
787
}
788
789
case 5:
790
{
791
int i;
792
793
for (i = 0; i < 2 && png_ptr->pass == 5; i++)
794
{
795
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
796
png_read_push_finish_row(png_ptr);
797
}
798
799
if (png_ptr->pass == 6) /* Skip top generated row */
800
{
801
png_push_have_row(png_ptr, NULL);
802
png_read_push_finish_row(png_ptr);
803
}
804
805
break;
806
}
807
808
default:
809
case 6:
810
{
811
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
812
png_read_push_finish_row(png_ptr);
813
814
if (png_ptr->pass != 6)
815
break;
816
817
png_push_have_row(png_ptr, NULL);
818
png_read_push_finish_row(png_ptr);
819
}
820
}
821
}
822
else
823
#endif
824
{
825
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
826
png_read_push_finish_row(png_ptr);
827
}
828
}
829
830
void /* PRIVATE */
831
png_read_push_finish_row(png_structrp png_ptr)
832
{
833
png_ptr->row_number++;
834
if (png_ptr->row_number < png_ptr->num_rows)
835
return;
836
837
#ifdef PNG_READ_INTERLACING_SUPPORTED
838
if (png_ptr->interlaced != 0)
839
{
840
png_ptr->row_number = 0;
841
memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
842
843
do
844
{
845
png_ptr->pass++;
846
if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
847
(png_ptr->pass == 3 && png_ptr->width < 3) ||
848
(png_ptr->pass == 5 && png_ptr->width < 2))
849
png_ptr->pass++;
850
851
if (png_ptr->pass > 7)
852
png_ptr->pass--;
853
854
if (png_ptr->pass >= 7)
855
break;
856
857
png_ptr->iwidth = (png_ptr->width +
858
png_pass_inc[png_ptr->pass] - 1 -
859
png_pass_start[png_ptr->pass]) /
860
png_pass_inc[png_ptr->pass];
861
862
if ((png_ptr->transformations & PNG_INTERLACE) != 0)
863
break;
864
865
png_ptr->num_rows = (png_ptr->height +
866
png_pass_yinc[png_ptr->pass] - 1 -
867
png_pass_ystart[png_ptr->pass]) /
868
png_pass_yinc[png_ptr->pass];
869
870
} while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
871
}
872
#endif /* READ_INTERLACING */
873
}
874
875
void /* PRIVATE */
876
png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
877
{
878
if (png_ptr->info_fn != NULL)
879
(*(png_ptr->info_fn))(png_ptr, info_ptr);
880
}
881
882
void /* PRIVATE */
883
png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
884
{
885
if (png_ptr->end_fn != NULL)
886
(*(png_ptr->end_fn))(png_ptr, info_ptr);
887
}
888
889
void /* PRIVATE */
890
png_push_have_row(png_structrp png_ptr, png_bytep row)
891
{
892
if (png_ptr->row_fn != NULL)
893
(*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
894
(int)png_ptr->pass);
895
}
896
897
#ifdef PNG_READ_INTERLACING_SUPPORTED
898
void PNGAPI
899
png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
900
png_const_bytep new_row)
901
{
902
if (png_ptr == NULL)
903
return;
904
905
/* new_row is a flag here - if it is NULL then the app callback was called
906
* from an empty row (see the calls to png_struct::row_fn below), otherwise
907
* it must be png_ptr->row_buf+1
908
*/
909
if (new_row != NULL)
910
png_combine_row(png_ptr, old_row, 1/*blocky display*/);
911
}
912
#endif /* READ_INTERLACING */
913
914
void PNGAPI
915
png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
916
png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
917
png_progressive_end_ptr end_fn)
918
{
919
if (png_ptr == NULL)
920
return;
921
922
png_ptr->info_fn = info_fn;
923
png_ptr->row_fn = row_fn;
924
png_ptr->end_fn = end_fn;
925
926
png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
927
}
928
929
png_voidp PNGAPI
930
png_get_progressive_ptr(png_const_structrp png_ptr)
931
{
932
if (png_ptr == NULL)
933
return NULL;
934
935
return png_ptr->io_ptr;
936
}
937
#endif /* PROGRESSIVE_READ */
938
939