-
Notifications
You must be signed in to change notification settings - Fork 637
/
Copy pathpngimage.c
1722 lines (1454 loc) · 50.6 KB
/
pngimage.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* pngimage.c
*
* Copyright (c) 2021 Cosmin Truta
* Copyright (c) 2015,2016 John Cunningham Bowler
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*
* Test the png_read_png and png_write_png interfaces. Given a PNG file load it
* using png_read_png and then write with png_write_png. Test all possible
* transforms.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
# include <config.h>
#endif
/* Define the following to use this test against your installed libpng, rather
* than the one being built here:
*/
#ifdef PNG_FREESTANDING_TESTS
# include <png.h>
#else
# include "../../png.h"
#endif
#ifndef PNG_SETJMP_SUPPORTED
# include <setjmp.h> /* because png.h did *not* include this */
#endif
/* 1.6.1 added support for the configure test harness, which uses 77 to indicate
* a skipped test, in earlier versions we need to succeed on a skipped test, so:
*/
#if PNG_LIBPNG_VER >= 10601 && defined(HAVE_CONFIG_H)
# define SKIP 77
#else
# define SKIP 0
#endif
#if PNG_LIBPNG_VER < 10700
/* READ_PNG and WRITE_PNG were not defined, so: */
# ifdef PNG_INFO_IMAGE_SUPPORTED
# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
# define PNG_READ_PNG_SUPPORTED
# endif /* SEQUENTIAL_READ */
# ifdef PNG_WRITE_SUPPORTED
# define PNG_WRITE_PNG_SUPPORTED
# endif /* WRITE */
# endif /* INFO_IMAGE */
#endif /* pre 1.7.0 */
#ifdef PNG_READ_PNG_SUPPORTED
/* If a transform is valid on both read and write this implies that if the
* transform is applied to read it must also be applied on write to produce
* meaningful data. This is because these transforms when performed on read
* produce data with a memory format that does not correspond to a PNG format.
*
* Most of these transforms are invertible; after applying the transform on
* write the result is the original PNG data that would have would have been
* read if no transform were applied.
*
* The exception is _SHIFT, which destroys the low order bits marked as not
* significant in a PNG with the sBIT chunk.
*
* The following table lists, for each transform, the conditions under which it
* is expected to do anything. Conditions are defined as follows:
*
* 1) Color mask bits required - simply a mask to AND with color_type; one of
* these must be present for the transform to fire, except that 0 means
* 'always'.
* 2) Color mask bits which must be absent - another mask - none of these must
* be present.
* 3) Bit depths - a mask of component bit depths for the transform to fire.
* 4) 'read' - the transform works in png_read_png.
* 5) 'write' - the transform works in png_write_png.
* 6) PNG_INFO_chunk; a mask of the chunks that must be present for the
* transform to fire. All must be present - the requirement is that
* png_get_valid() & mask == mask, so if mask is 0 there is no requirement.
*
* The condition refers to the original image state - if multiple transforms are
* used together it is possible to cause a transform that wouldn't fire on the
* original image to fire.
*/
static struct transform_info
{
const char *name;
int transform;
png_uint_32 valid_chunks;
# define CHUNK_NONE 0
# define CHUNK_sBIT PNG_INFO_sBIT
# define CHUNK_tRNS PNG_INFO_tRNS
png_byte color_mask_required;
png_byte color_mask_absent;
# define COLOR_MASK_X 0
# define COLOR_MASK_P PNG_COLOR_MASK_PALETTE
# define COLOR_MASK_C PNG_COLOR_MASK_COLOR
# define COLOR_MASK_A PNG_COLOR_MASK_ALPHA
# define COLOR_MASK_ALL (PALETTE+COLOR+ALPHA) /* absent = gray, no alpha */
png_byte bit_depths;
# define BD_ALL (1 + 2 + 4 + 8 + 16)
# define BD_PAL (1 + 2 + 4 + 8)
# define BD_LOW (1 + 2 + 4)
# define BD_16 16
# define BD_TRUE (8+16) /* i.e. true-color depths */
png_byte when;
# define TRANSFORM_R 1
# define TRANSFORM_W 2
# define TRANSFORM_RW 3
png_byte tested; /* the transform was tested somewhere */
} transform_info[] =
{
/* List ALL the PNG_TRANSFORM_ macros here. Check for support using the READ
* macros; even if the transform is supported on write it cannot be tested
* without the read support.
*/
# define T(name,chunk,cm_required,cm_absent,bd,when)\
{ #name, PNG_TRANSFORM_ ## name, CHUNK_ ## chunk,\
COLOR_MASK_ ## cm_required, COLOR_MASK_ ## cm_absent, BD_ ## bd,\
TRANSFORM_ ## when, 0/*!tested*/ }
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
T(STRIP_16, NONE, X, X, 16, R),
/* drops the bottom 8 bits when bit depth is 16 */
#endif
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
T(STRIP_ALPHA, NONE, A, X, ALL, R),
/* removes the alpha channel if present */
#endif
#ifdef PNG_WRITE_PACK_SUPPORTED
# define TRANSFORM_RW_PACK TRANSFORM_RW
#else
# define TRANSFORM_RW_PACK TRANSFORM_R
#endif
#ifdef PNG_READ_PACK_SUPPORTED
T(PACKING, NONE, X, X, LOW, RW_PACK),
/* unpacks low-bit-depth components into 1 byte per component on read,
* reverses this on write.
*/
#endif
#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
# define TRANSFORM_RW_PACKSWAP TRANSFORM_RW
#else
# define TRANSFORM_RW_PACKSWAP TRANSFORM_R
#endif
#ifdef PNG_READ_PACKSWAP_SUPPORTED
T(PACKSWAP, NONE, X, X, LOW, RW_PACKSWAP),
/* reverses the order of low-bit-depth components packed into a byte */
#endif
#ifdef PNG_READ_EXPAND_SUPPORTED
T(EXPAND, NONE, P, X, ALL, R),
/* expands PLTE PNG files to RGB (no tRNS) or RGBA (tRNS) *
* Note that the 'EXPAND' transform does lots of different things: */
T(EXPAND, NONE, X, C, ALL, R),
/* expands grayscale PNG files to RGB, or RGBA */
T(EXPAND, tRNS, X, A, ALL, R),
/* expands the tRNS chunk in files without alpha */
#endif
#ifdef PNG_WRITE_INVERT_SUPPORTED
# define TRANSFORM_RW_INVERT TRANSFORM_RW
#else
# define TRANSFORM_RW_INVERT TRANSFORM_R
#endif
#ifdef PNG_READ_INVERT_SUPPORTED
T(INVERT_MONO, NONE, X, C, ALL, RW_INVERT),
/* converts gray-scale components to 1..0 from 0..1 */
#endif
#ifdef PNG_WRITE_SHIFT_SUPPORTED
# define TRANSFORM_RW_SHIFT TRANSFORM_RW
#else
# define TRANSFORM_RW_SHIFT TRANSFORM_R
#endif
#ifdef PNG_READ_SHIFT_SUPPORTED
T(SHIFT, sBIT, X, X, ALL, RW_SHIFT),
/* reduces component values to the original range based on the sBIT chunk,
* this is only partially reversible - the low bits are lost and cannot be
* recovered on write. In fact write code replicates the bits to generate
* new low-order bits.
*/
#endif
#ifdef PNG_WRITE_BGR_SUPPORTED
# define TRANSFORM_RW_BGR TRANSFORM_RW
#else
# define TRANSFORM_RW_BGR TRANSFORM_R
#endif
#ifdef PNG_READ_BGR_SUPPORTED
T(BGR, NONE, C, P, TRUE, RW_BGR),
/* reverses the rgb component values of true-color pixels */
#endif
#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
# define TRANSFORM_RW_SWAP_ALPHA TRANSFORM_RW
#else
# define TRANSFORM_RW_SWAP_ALPHA TRANSFORM_R
#endif
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
T(SWAP_ALPHA, NONE, A, X, TRUE, RW_SWAP_ALPHA),
/* swaps the alpha channel of RGBA or GA pixels to the front - ARGB or
* AG, on write reverses the process.
*/
#endif
#ifdef PNG_WRITE_SWAP_SUPPORTED
# define TRANSFORM_RW_SWAP TRANSFORM_RW
#else
# define TRANSFORM_RW_SWAP TRANSFORM_R
#endif
#ifdef PNG_READ_SWAP_SUPPORTED
T(SWAP_ENDIAN, NONE, X, P, 16, RW_SWAP),
/* byte-swaps 16-bit component values */
#endif
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
# define TRANSFORM_RW_INVERT_ALPHA TRANSFORM_RW
#else
# define TRANSFORM_RW_INVERT_ALPHA TRANSFORM_R
#endif
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
T(INVERT_ALPHA, NONE, A, X, TRUE, RW_INVERT_ALPHA),
/* converts an alpha channel from 0..1 to 1..0 */
#endif
#ifdef PNG_WRITE_FILLER_SUPPORTED
T(STRIP_FILLER_BEFORE, NONE, A, P, TRUE, W), /* 'A' for a filler! */
/* on write skips a leading filler channel; testing requires data with a
* filler channel so this is produced from RGBA or GA images by removing
* the 'alpha' flag from the color type in place.
*/
T(STRIP_FILLER_AFTER, NONE, A, P, TRUE, W),
/* on write strips a trailing filler channel */
#endif
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
T(GRAY_TO_RGB, NONE, X, C, ALL, R),
/* expands grayscale images to RGB, also causes the palette part of
* 'EXPAND' to happen. Low bit depth grayscale images are expanded to
* 8-bits per component and no attempt is made to convert the image to a
* palette image. While this transform is partially reversible
* png_write_png does not currently support this.
*/
T(GRAY_TO_RGB, NONE, P, X, ALL, R),
/* The 'palette' side effect mentioned above; a bit bogus but this is the
* way the libpng code works.
*/
#endif
#ifdef PNG_READ_EXPAND_16_SUPPORTED
T(EXPAND_16, NONE, X, X, PAL, R),
/* expands images to 16-bits per component, as a side effect expands
* palette images to RGB and expands the tRNS chunk if present, so it can
* modify 16-bit per component images as well:
*/
T(EXPAND_16, tRNS, X, A, 16, R),
/* side effect of EXPAND_16 - expands the tRNS chunk in an RGB or G 16-bit
* image.
*/
#endif
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
T(SCALE_16, NONE, X, X, 16, R),
/* scales 16-bit components to 8-bits. */
#endif
{ NULL /*name*/, 0, 0, 0, 0, 0, 0, 0/*!tested*/ }
#undef T
};
#define ARRAY_SIZE(a) ((sizeof a)/(sizeof a[0]))
#define TTABLE_SIZE ARRAY_SIZE(transform_info)
/* Some combinations of options that should be reversible are not; these cases
* are bugs.
*/
static int known_bad_combos[][2] =
{
/* problem, antidote */
{ PNG_TRANSFORM_SHIFT | PNG_TRANSFORM_INVERT_ALPHA, 0/*antidote*/ }
};
static int
is_combo(int transforms)
{
return transforms & (transforms-1); /* non-zero if more than one set bit */
}
static int
first_transform(int transforms)
{
return transforms & -transforms; /* lowest set bit */
}
static int
is_bad_combo(int transforms)
{
unsigned int i;
for (i=0; i<ARRAY_SIZE(known_bad_combos); ++i)
{
int combo = known_bad_combos[i][0];
if ((combo & transforms) == combo &&
(transforms & known_bad_combos[i][1]) == 0)
return 1;
}
return 0; /* combo is ok */
}
static const char *
transform_name(int t)
/* The name, if 't' has multiple bits set the name of the lowest set bit is
* returned.
*/
{
unsigned int i;
t &= -t; /* first set bit */
for (i=0; i<TTABLE_SIZE; ++i)
if (transform_info[i].name != NULL)
if ((transform_info[i].transform & t) != 0)
return transform_info[i].name;
return "invalid transform";
}
/* Variables calculated by validate_T below and used to record all the supported
* transforms. Need (unsigned int) here because of the places where these
* values are used (unsigned compares in the 'exhaustive' iterator.)
*/
static unsigned int read_transforms, write_transforms, rw_transforms;
static void
validate_T(void)
/* Validate the above table - this just builds the above values */
{
unsigned int i;
for (i=0; i<TTABLE_SIZE; ++i)
{
if (transform_info[i].name != NULL)
{
if (transform_info[i].when & TRANSFORM_R)
read_transforms |= transform_info[i].transform;
if (transform_info[i].when & TRANSFORM_W)
write_transforms |= transform_info[i].transform;
}
}
/* Reversible transforms are those which are supported on both read and
* write.
*/
rw_transforms = read_transforms & write_transforms;
}
/* FILE DATA HANDLING
* The original file is cached in memory. During write the output file is
* written to memory.
*
* In both cases the file data is held in a linked list of buffers - not all
* of these are in use at any time.
*/
#define NEW(type) ((type *)malloc(sizeof (type)))
#define DELETE(ptr) (free(ptr))
struct buffer_list
{
struct buffer_list *next; /* next buffer in list */
png_byte buffer[1024]; /* the actual buffer */
};
struct buffer
{
struct buffer_list *last; /* last buffer in use */
size_t end_count; /* bytes in the last buffer */
struct buffer_list *current; /* current buffer being read */
size_t read_count; /* count of bytes read from current */
struct buffer_list first; /* the very first buffer */
};
static void
buffer_init(struct buffer *buffer)
/* Call this only once for a given buffer */
{
buffer->first.next = NULL;
buffer->last = NULL;
buffer->current = NULL;
}
static void
buffer_destroy_list(struct buffer_list *list)
{
if (list != NULL)
{
struct buffer_list *next = list->next;
DELETE(list);
buffer_destroy_list(next);
}
}
static void
buffer_destroy(struct buffer *buffer)
{
struct buffer_list *list = buffer->first.next;
buffer_init(buffer);
buffer_destroy_list(list);
}
#ifdef PNG_WRITE_PNG_SUPPORTED
static void
buffer_start_write(struct buffer *buffer)
{
buffer->last = &buffer->first;
buffer->end_count = 0;
buffer->current = NULL;
}
#endif
static void
buffer_start_read(struct buffer *buffer)
{
buffer->current = &buffer->first;
buffer->read_count = 0;
}
#ifdef ENOMEM /* required by POSIX 1003.1 */
# define MEMORY ENOMEM
#else
# define MEMORY ERANGE /* required by ANSI-C */
#endif
static struct buffer *
get_buffer(png_structp pp)
/* Used from libpng callbacks to get the current buffer */
{
return (struct buffer*)png_get_io_ptr(pp);
}
static struct buffer_list *
buffer_extend(struct buffer_list *current)
{
struct buffer_list *add;
assert(current->next == NULL);
add = NEW(struct buffer_list);
if (add == NULL)
return NULL;
add->next = NULL;
current->next = add;
return add;
}
/* Load a buffer from a file; does the equivalent of buffer_start_write. On a
* read error returns an errno value, else returns 0.
*/
static int
buffer_from_file(struct buffer *buffer, FILE *fp)
{
struct buffer_list *last = &buffer->first;
size_t count = 0;
for (;;)
{
size_t r = fread(last->buffer+count, 1/*size*/,
(sizeof last->buffer)-count, fp);
if (r > 0)
{
count += r;
if (count >= sizeof last->buffer)
{
assert(count == sizeof last->buffer);
count = 0;
if (last->next == NULL)
{
last = buffer_extend(last);
if (last == NULL)
return MEMORY;
}
else
last = last->next;
}
}
else /* fread failed - probably end of file */
{
if (feof(fp))
{
buffer->last = last;
buffer->end_count = count;
return 0; /* no error */
}
/* Some kind of funky error; errno should be non-zero */
return errno == 0 ? ERANGE : errno;
}
}
}
/* This structure is used to control the test of a single file. */
typedef enum
{
VERBOSE, /* switches on all messages */
INFORMATION,
WARNINGS, /* switches on warnings */
LIBPNG_WARNING,
APP_WARNING,
ERRORS, /* just errors */
APP_FAIL, /* continuable error - no need to longjmp */
LIBPNG_ERROR, /* this and higher cause a longjmp */
LIBPNG_BUG, /* erroneous behavior in libpng */
APP_ERROR, /* such as out-of-memory in a callback */
QUIET, /* no normal messages */
USER_ERROR, /* such as file-not-found */
INTERNAL_ERROR
} error_level;
#define LEVEL_MASK 0xf /* where the level is in 'options' */
#define EXHAUSTIVE 0x010 /* Test all combinations of active options */
#define STRICT 0x020 /* Fail on warnings as well as errors */
#define LOG 0x040 /* Log pass/fail to stdout */
#define CONTINUE 0x080 /* Continue on APP_FAIL errors */
#define SKIP_BUGS 0x100 /* Skip over known bugs */
#define LOG_SKIPPED 0x200 /* Log skipped bugs */
#define FIND_BAD_COMBOS 0x400 /* Attempt to deduce bad combos */
#define LIST_COMBOS 0x800 /* List combos by name */
/* Result masks apply to the result bits in the 'results' field below; these
* bits are simple 1U<<error_level. A pass requires either nothing worse than
* warnings (--relaxes) or nothing worse than information (--strict)
*/
#define RESULT_STRICT(r) (((r) & ~((1U<<WARNINGS)-1)) == 0)
#define RESULT_RELAXED(r) (((r) & ~((1U<<ERRORS)-1)) == 0)
struct display
{
jmp_buf error_return; /* Where to go to on error */
const char *filename; /* The name of the original file */
const char *operation; /* Operation being performed */
int transforms; /* Transform used in operation */
png_uint_32 options; /* See display_log below */
png_uint_32 results; /* A mask of errors seen */
png_structp original_pp; /* used on the original read */
png_infop original_ip; /* set by the original read */
size_t original_rowbytes; /* of the original rows: */
png_bytepp original_rows; /* from the original read */
/* Original chunks valid */
png_uint_32 chunks;
/* Original IHDR information */
png_uint_32 width;
png_uint_32 height;
int bit_depth;
int color_type;
int interlace_method;
int compression_method;
int filter_method;
/* Derived information for the original image. */
int active_transforms; /* transforms that do something on read */
int ignored_transforms; /* transforms that should do nothing */
/* Used on a read, both the original read and when validating a written
* image.
*/
png_structp read_pp;
png_infop read_ip;
# ifdef PNG_WRITE_PNG_SUPPORTED
/* Used to write a new image (the original info_ptr is used) */
png_structp write_pp;
struct buffer written_file; /* where the file gets written */
# endif
struct buffer original_file; /* Data read from the original file */
};
static void
display_init(struct display *dp)
/* Call this only once right at the start to initialize the control
* structure, the (struct buffer) lists are maintained across calls - the
* memory is not freed.
*/
{
memset(dp, 0, sizeof *dp);
dp->options = WARNINGS; /* default to !verbose, !quiet */
dp->filename = NULL;
dp->operation = NULL;
dp->original_pp = NULL;
dp->original_ip = NULL;
dp->original_rows = NULL;
dp->read_pp = NULL;
dp->read_ip = NULL;
buffer_init(&dp->original_file);
# ifdef PNG_WRITE_PNG_SUPPORTED
dp->write_pp = NULL;
buffer_init(&dp->written_file);
# endif
}
static void
display_clean_read(struct display *dp)
{
if (dp->read_pp != NULL)
png_destroy_read_struct(&dp->read_pp, &dp->read_ip, NULL);
}
#ifdef PNG_WRITE_PNG_SUPPORTED
static void
display_clean_write(struct display *dp)
{
if (dp->write_pp != NULL)
png_destroy_write_struct(&dp->write_pp, NULL);
}
#endif
static void
display_clean(struct display *dp)
{
# ifdef PNG_WRITE_PNG_SUPPORTED
display_clean_write(dp);
# endif
display_clean_read(dp);
dp->original_rowbytes = 0;
dp->original_rows = NULL;
dp->chunks = 0;
png_destroy_read_struct(&dp->original_pp, &dp->original_ip, NULL);
/* leave the filename for error detection */
dp->results = 0; /* reset for next time */
}
static void
display_destroy(struct display *dp)
{
/* Release any memory held in the display. */
# ifdef PNG_WRITE_PNG_SUPPORTED
buffer_destroy(&dp->written_file);
# endif
buffer_destroy(&dp->original_file);
}
static struct display *
get_dp(png_structp pp)
/* The display pointer is always stored in the png_struct error pointer */
{
struct display *dp = (struct display*)png_get_error_ptr(pp);
if (dp == NULL)
{
fprintf(stderr, "pngimage: internal error (no display)\n");
exit(99); /* prevents a crash */
}
return dp;
}
/* error handling */
#ifdef __GNUC__
# define VGATTR __attribute__((__format__ (__printf__,3,4)))
/* Required to quiet GNUC warnings when the compiler sees a stdarg function
* that calls one of the stdio v APIs.
*/
#else
# define VGATTR
#endif
static void VGATTR
display_log(struct display *dp, error_level level, const char *fmt, ...)
/* 'level' is as above, fmt is a stdio style format string. This routine
* does not return if level is above LIBPNG_WARNING
*/
{
dp->results |= 1U << level;
if (level > (error_level)(dp->options & LEVEL_MASK))
{
const char *lp;
va_list ap;
switch (level)
{
case INFORMATION: lp = "information"; break;
case LIBPNG_WARNING: lp = "warning(libpng)"; break;
case APP_WARNING: lp = "warning(pngimage)"; break;
case APP_FAIL: lp = "error(continuable)"; break;
case LIBPNG_ERROR: lp = "error(libpng)"; break;
case LIBPNG_BUG: lp = "bug(libpng)"; break;
case APP_ERROR: lp = "error(pngimage)"; break;
case USER_ERROR: lp = "error(user)"; break;
case INTERNAL_ERROR: /* anything unexpected is an internal error: */
case VERBOSE: case WARNINGS: case ERRORS: case QUIET:
default: lp = "bug(pngimage)"; break;
}
fprintf(stderr, "%s: %s: %s",
dp->filename != NULL ? dp->filename : "<stdin>", lp, dp->operation);
if (dp->transforms != 0)
{
int tr = dp->transforms;
if (is_combo(tr))
{
if (dp->options & LIST_COMBOS)
{
int trx = tr;
fprintf(stderr, "(");
if (trx)
{
int start = 0;
while (trx)
{
int trz = trx & -trx;
if (start) fprintf(stderr, "+");
fprintf(stderr, "%s", transform_name(trz));
start = 1;
trx &= ~trz;
}
}
else
fprintf(stderr, "-");
fprintf(stderr, ")");
}
else
fprintf(stderr, "(0x%x)", tr);
}
else
fprintf(stderr, "(%s)", transform_name(tr));
}
fprintf(stderr, ": ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
}
/* else do not output any message */
/* Errors cause this routine to exit to the fail code */
if (level > APP_FAIL || (level > ERRORS && !(dp->options & CONTINUE)))
longjmp(dp->error_return, level);
}
/* error handler callbacks for libpng */
static void PNGCBAPI
display_warning(png_structp pp, png_const_charp warning)
{
display_log(get_dp(pp), LIBPNG_WARNING, "%s", warning);
}
static void PNGCBAPI
display_error(png_structp pp, png_const_charp error)
{
struct display *dp = get_dp(pp);
display_log(dp, LIBPNG_ERROR, "%s", error);
}
static void
display_cache_file(struct display *dp, const char *filename)
/* Does the initial cache of the file. */
{
FILE *fp;
int ret;
dp->filename = filename;
if (filename != NULL)
{
fp = fopen(filename, "rb");
if (fp == NULL)
display_log(dp, USER_ERROR, "open failed: %s", strerror(errno));
}
else
fp = stdin;
ret = buffer_from_file(&dp->original_file, fp);
fclose(fp);
if (ret != 0)
display_log(dp, APP_ERROR, "read failed: %s", strerror(ret));
}
static void
buffer_read(struct display *dp, struct buffer *bp, png_bytep data,
size_t size)
{
struct buffer_list *last = bp->current;
size_t read_count = bp->read_count;
while (size > 0)
{
size_t avail;
if (last == NULL ||
(last == bp->last && read_count >= bp->end_count))
{
display_log(dp, USER_ERROR, "file truncated (%lu bytes)",
(unsigned long)size);
/*NOTREACHED*/
break;
}
else if (read_count >= sizeof last->buffer)
{
/* Move to the next buffer: */
last = last->next;
read_count = 0;
bp->current = last; /* Avoid update outside the loop */
/* And do a sanity check (the EOF case is caught above) */
if (last == NULL)
{
display_log(dp, INTERNAL_ERROR, "damaged buffer list");
/*NOTREACHED*/
break;
}
}
avail = (sizeof last->buffer) - read_count;
if (avail > size)
avail = size;
memcpy(data, last->buffer + read_count, avail);
read_count += avail;
size -= avail;
data += avail;
}
bp->read_count = read_count;
}
static void PNGCBAPI
read_function(png_structp pp, png_bytep data, size_t size)
{
buffer_read(get_dp(pp), get_buffer(pp), data, size);
}
static void
read_png(struct display *dp, struct buffer *bp, const char *operation,
int transforms)
{
png_structp pp;
png_infop ip;
/* This cleans out any previous read and sets operation and transforms to
* empty.
*/
display_clean_read(dp);
if (operation != NULL) /* else this is a verify and do not overwrite info */
{
dp->operation = operation;
dp->transforms = transforms;
}
dp->read_pp = pp = png_create_read_struct(PNG_LIBPNG_VER_STRING, dp,
display_error, display_warning);
if (pp == NULL)
display_log(dp, LIBPNG_ERROR, "failed to create read struct");
/* The png_read_png API requires us to make the info struct, but it does the
* call to png_read_info.
*/
dp->read_ip = ip = png_create_info_struct(pp);
if (ip == NULL)
display_log(dp, LIBPNG_ERROR, "failed to create info struct");
# ifdef PNG_SET_USER_LIMITS_SUPPORTED
/* Remove the user limits, if any */
png_set_user_limits(pp, 0x7fffffff, 0x7fffffff);
# endif
/* Set the IO handling */
buffer_start_read(bp);
png_set_read_fn(pp, bp, read_function);
png_read_png(pp, ip, transforms, NULL/*params*/);
#if 0 /* crazy debugging */
{
png_bytep pr = png_get_rows(pp, ip)[0];
size_t rb = png_get_rowbytes(pp, ip);
size_t cb;
char c = ' ';
fprintf(stderr, "%.4x %2d (%3lu bytes):", transforms, png_get_bit_depth(pp,ip), (unsigned long)rb);
for (cb=0; cb<rb; ++cb)
fputc(c, stderr), fprintf(stderr, "%.2x", pr[cb]), c='.';
fputc('\n', stderr);
}
#endif
}
static void
update_display(struct display *dp)
/* called once after the first read to update all the info, original_pp and
* original_ip must have been filled in.
*/
{
png_structp pp;
png_infop ip;
/* Now perform the initial read with a 0 transform. */
read_png(dp, &dp->original_file, "original read", 0/*no transform*/);
/* Move the result to the 'original' fields */
dp->original_pp = pp = dp->read_pp, dp->read_pp = NULL;
dp->original_ip = ip = dp->read_ip, dp->read_ip = NULL;
dp->original_rowbytes = png_get_rowbytes(pp, ip);
if (dp->original_rowbytes == 0)
display_log(dp, LIBPNG_BUG, "png_get_rowbytes returned 0");
dp->chunks = png_get_valid(pp, ip, 0xffffffff);
if ((dp->chunks & PNG_INFO_IDAT) == 0) /* set by png_read_png */
display_log(dp, LIBPNG_BUG, "png_read_png did not set IDAT flag");
dp->original_rows = png_get_rows(pp, ip);
if (dp->original_rows == NULL)
display_log(dp, LIBPNG_BUG, "png_read_png did not create row buffers");
if (!png_get_IHDR(pp, ip,
&dp->width, &dp->height, &dp->bit_depth, &dp->color_type,
&dp->interlace_method, &dp->compression_method, &dp->filter_method))
display_log(dp, LIBPNG_BUG, "png_get_IHDR failed");
/* 'active' transforms are discovered based on the original image format;
* running one active transform can activate others. At present the code
* does not attempt to determine the closure.
*/
{
png_uint_32 chunks = dp->chunks;
int active = 0, inactive = 0;
int ct = dp->color_type;
int bd = dp->bit_depth;
unsigned int i;
for (i=0; i<TTABLE_SIZE; ++i)
{
if (transform_info[i].name != NULL)
{
int transform = transform_info[i].transform;
if ((transform_info[i].valid_chunks == 0 ||
(transform_info[i].valid_chunks & chunks) != 0) &&
(transform_info[i].color_mask_required & ct) ==
transform_info[i].color_mask_required &&
(transform_info[i].color_mask_absent & ct) == 0 &&
(transform_info[i].bit_depths & bd) != 0 &&
(transform_info[i].when & TRANSFORM_R) != 0)
active |= transform;
else if ((transform_info[i].when & TRANSFORM_R) != 0)
inactive |= transform;
}
}
/* Some transforms appear multiple times in the table; the 'active' status
* is the logical OR of these and the inactive status must be adjusted to
* take this into account.
*/
inactive &= ~active;
dp->active_transforms = active;
dp->ignored_transforms = inactive; /* excluding write-only transforms */
}
}
static int
compare_read(struct display *dp, int applied_transforms)