VirtualBox

source: vbox/trunk/src/libs/libpng-1.6.45/contrib/libtests/pngimage.c

Last change on this file was 107813, checked in by vboxsync, 2 months ago

libpng-1.6.45: Applied and adjusted our libpng changes to 1.6.45. bugref:8515

  • Property svn:eol-style set to native
File size: 50.8 KB
Line 
1/* pngimage.c
2 *
3 * Copyright (c) 2021 Cosmin Truta
4 * Copyright (c) 2015,2016 John Cunningham Bowler
5 *
6 * This code is released under the libpng license.
7 * For conditions of distribution and use, see the disclaimer
8 * and license in png.h
9 *
10 * Test the png_read_png and png_write_png interfaces. Given a PNG file load it
11 * using png_read_png and then write with png_write_png. Test all possible
12 * transforms.
13 */
14
15#include <stdarg.h>
16#include <stdlib.h>
17#include <string.h>
18#include <errno.h>
19#include <stdio.h>
20#include <assert.h>
21
22#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
23# include <config.h>
24#endif
25
26/* Define the following to use this test against your installed libpng, rather
27 * than the one being built here:
28 */
29#ifdef PNG_FREESTANDING_TESTS
30# include <png.h>
31#else
32# include "../../png.h"
33#endif
34
35#ifndef PNG_SETJMP_SUPPORTED
36# include <setjmp.h> /* because png.h did *not* include this */
37#endif
38
39/* 1.6.1 added support for the configure test harness, which uses 77 to indicate
40 * a skipped test, in earlier versions we need to succeed on a skipped test, so:
41 */
42#if PNG_LIBPNG_VER >= 10601 && defined(HAVE_CONFIG_H)
43# define SKIP 77
44#else
45# define SKIP 0
46#endif
47
48#if PNG_LIBPNG_VER < 10700
49 /* READ_PNG and WRITE_PNG were not defined, so: */
50# ifdef PNG_INFO_IMAGE_SUPPORTED
51# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
52# define PNG_READ_PNG_SUPPORTED
53# endif /* SEQUENTIAL_READ */
54# ifdef PNG_WRITE_SUPPORTED
55# define PNG_WRITE_PNG_SUPPORTED
56# endif /* WRITE */
57# endif /* INFO_IMAGE */
58#endif /* pre 1.7.0 */
59
60#ifdef PNG_READ_PNG_SUPPORTED
61/* If a transform is valid on both read and write this implies that if the
62 * transform is applied to read it must also be applied on write to produce
63 * meaningful data. This is because these transforms when performed on read
64 * produce data with a memory format that does not correspond to a PNG format.
65 *
66 * Most of these transforms are invertible; after applying the transform on
67 * write the result is the original PNG data that would have would have been
68 * read if no transform were applied.
69 *
70 * The exception is _SHIFT, which destroys the low order bits marked as not
71 * significant in a PNG with the sBIT chunk.
72 *
73 * The following table lists, for each transform, the conditions under which it
74 * is expected to do anything. Conditions are defined as follows:
75 *
76 * 1) Color mask bits required - simply a mask to AND with color_type; one of
77 * these must be present for the transform to fire, except that 0 means
78 * 'always'.
79 * 2) Color mask bits which must be absent - another mask - none of these must
80 * be present.
81 * 3) Bit depths - a mask of component bit depths for the transform to fire.
82 * 4) 'read' - the transform works in png_read_png.
83 * 5) 'write' - the transform works in png_write_png.
84 * 6) PNG_INFO_chunk; a mask of the chunks that must be present for the
85 * transform to fire. All must be present - the requirement is that
86 * png_get_valid() & mask == mask, so if mask is 0 there is no requirement.
87 *
88 * The condition refers to the original image state - if multiple transforms are
89 * used together it is possible to cause a transform that wouldn't fire on the
90 * original image to fire.
91 */
92static struct transform_info
93{
94 const char *name;
95 int transform;
96 png_uint_32 valid_chunks;
97# define CHUNK_NONE 0
98# define CHUNK_sBIT PNG_INFO_sBIT
99# define CHUNK_tRNS PNG_INFO_tRNS
100 png_byte color_mask_required;
101 png_byte color_mask_absent;
102# define COLOR_MASK_X 0
103# define COLOR_MASK_P PNG_COLOR_MASK_PALETTE
104# define COLOR_MASK_C PNG_COLOR_MASK_COLOR
105# define COLOR_MASK_A PNG_COLOR_MASK_ALPHA
106# define COLOR_MASK_ALL (PALETTE+COLOR+ALPHA) /* absent = gray, no alpha */
107 png_byte bit_depths;
108# define BD_ALL (1 + 2 + 4 + 8 + 16)
109# define BD_PAL (1 + 2 + 4 + 8)
110# define BD_LOW (1 + 2 + 4)
111# define BD_16 16
112# define BD_TRUE (8+16) /* i.e. true-color depths */
113 png_byte when;
114# define TRANSFORM_R 1
115# define TRANSFORM_W 2
116# define TRANSFORM_RW 3
117 png_byte tested; /* the transform was tested somewhere */
118} transform_info[] =
119{
120 /* List ALL the PNG_TRANSFORM_ macros here. Check for support using the READ
121 * macros; even if the transform is supported on write it cannot be tested
122 * without the read support.
123 */
124# define T(name,chunk,cm_required,cm_absent,bd,when)\
125 { #name, PNG_TRANSFORM_ ## name, CHUNK_ ## chunk,\
126 COLOR_MASK_ ## cm_required, COLOR_MASK_ ## cm_absent, BD_ ## bd,\
127 TRANSFORM_ ## when, 0/*!tested*/ }
128
129#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
130 T(STRIP_16, NONE, X, X, 16, R),
131 /* drops the bottom 8 bits when bit depth is 16 */
132#endif
133#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
134 T(STRIP_ALPHA, NONE, A, X, ALL, R),
135 /* removes the alpha channel if present */
136#endif
137#ifdef PNG_WRITE_PACK_SUPPORTED
138# define TRANSFORM_RW_PACK TRANSFORM_RW
139#else
140# define TRANSFORM_RW_PACK TRANSFORM_R
141#endif
142#ifdef PNG_READ_PACK_SUPPORTED
143 T(PACKING, NONE, X, X, LOW, RW_PACK),
144 /* unpacks low-bit-depth components into 1 byte per component on read,
145 * reverses this on write.
146 */
147#endif
148#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
149# define TRANSFORM_RW_PACKSWAP TRANSFORM_RW
150#else
151# define TRANSFORM_RW_PACKSWAP TRANSFORM_R
152#endif
153#ifdef PNG_READ_PACKSWAP_SUPPORTED
154 T(PACKSWAP, NONE, X, X, LOW, RW_PACKSWAP),
155 /* reverses the order of low-bit-depth components packed into a byte */
156#endif
157#ifdef PNG_READ_EXPAND_SUPPORTED
158 T(EXPAND, NONE, P, X, ALL, R),
159 /* expands PLTE PNG files to RGB (no tRNS) or RGBA (tRNS) *
160 * Note that the 'EXPAND' transform does lots of different things: */
161 T(EXPAND, NONE, X, C, ALL, R),
162 /* expands grayscale PNG files to RGB, or RGBA */
163 T(EXPAND, tRNS, X, A, ALL, R),
164 /* expands the tRNS chunk in files without alpha */
165#endif
166#ifdef PNG_WRITE_INVERT_SUPPORTED
167# define TRANSFORM_RW_INVERT TRANSFORM_RW
168#else
169# define TRANSFORM_RW_INVERT TRANSFORM_R
170#endif
171#ifdef PNG_READ_INVERT_SUPPORTED
172 T(INVERT_MONO, NONE, X, C, ALL, RW_INVERT),
173 /* converts gray-scale components to 1..0 from 0..1 */
174#endif
175#ifdef PNG_WRITE_SHIFT_SUPPORTED
176# define TRANSFORM_RW_SHIFT TRANSFORM_RW
177#else
178# define TRANSFORM_RW_SHIFT TRANSFORM_R
179#endif
180#ifdef PNG_READ_SHIFT_SUPPORTED
181 T(SHIFT, sBIT, X, X, ALL, RW_SHIFT),
182 /* reduces component values to the original range based on the sBIT chunk,
183 * this is only partially reversible - the low bits are lost and cannot be
184 * recovered on write. In fact write code replicates the bits to generate
185 * new low-order bits.
186 */
187#endif
188#ifdef PNG_WRITE_BGR_SUPPORTED
189# define TRANSFORM_RW_BGR TRANSFORM_RW
190#else
191# define TRANSFORM_RW_BGR TRANSFORM_R
192#endif
193#ifdef PNG_READ_BGR_SUPPORTED
194 T(BGR, NONE, C, P, TRUE, RW_BGR),
195 /* reverses the rgb component values of true-color pixels */
196#endif
197#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
198# define TRANSFORM_RW_SWAP_ALPHA TRANSFORM_RW
199#else
200# define TRANSFORM_RW_SWAP_ALPHA TRANSFORM_R
201#endif
202#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
203 T(SWAP_ALPHA, NONE, A, X, TRUE, RW_SWAP_ALPHA),
204 /* swaps the alpha channel of RGBA or GA pixels to the front - ARGB or
205 * AG, on write reverses the process.
206 */
207#endif
208#ifdef PNG_WRITE_SWAP_SUPPORTED
209# define TRANSFORM_RW_SWAP TRANSFORM_RW
210#else
211# define TRANSFORM_RW_SWAP TRANSFORM_R
212#endif
213#ifdef PNG_READ_SWAP_SUPPORTED
214 T(SWAP_ENDIAN, NONE, X, P, 16, RW_SWAP),
215 /* byte-swaps 16-bit component values */
216#endif
217#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
218# define TRANSFORM_RW_INVERT_ALPHA TRANSFORM_RW
219#else
220# define TRANSFORM_RW_INVERT_ALPHA TRANSFORM_R
221#endif
222#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
223 T(INVERT_ALPHA, NONE, A, X, TRUE, RW_INVERT_ALPHA),
224 /* converts an alpha channel from 0..1 to 1..0 */
225#endif
226#ifdef PNG_WRITE_FILLER_SUPPORTED
227 T(STRIP_FILLER_BEFORE, NONE, A, P, TRUE, W), /* 'A' for a filler! */
228 /* on write skips a leading filler channel; testing requires data with a
229 * filler channel so this is produced from RGBA or GA images by removing
230 * the 'alpha' flag from the color type in place.
231 */
232 T(STRIP_FILLER_AFTER, NONE, A, P, TRUE, W),
233 /* on write strips a trailing filler channel */
234#endif
235#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
236 T(GRAY_TO_RGB, NONE, X, C, ALL, R),
237 /* expands grayscale images to RGB, also causes the palette part of
238 * 'EXPAND' to happen. Low bit depth grayscale images are expanded to
239 * 8-bits per component and no attempt is made to convert the image to a
240 * palette image. While this transform is partially reversible
241 * png_write_png does not currently support this.
242 */
243 T(GRAY_TO_RGB, NONE, P, X, ALL, R),
244 /* The 'palette' side effect mentioned above; a bit bogus but this is the
245 * way the libpng code works.
246 */
247#endif
248#ifdef PNG_READ_EXPAND_16_SUPPORTED
249 T(EXPAND_16, NONE, X, X, PAL, R),
250 /* expands images to 16-bits per component, as a side effect expands
251 * palette images to RGB and expands the tRNS chunk if present, so it can
252 * modify 16-bit per component images as well:
253 */
254 T(EXPAND_16, tRNS, X, A, 16, R),
255 /* side effect of EXPAND_16 - expands the tRNS chunk in an RGB or G 16-bit
256 * image.
257 */
258#endif
259#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
260 T(SCALE_16, NONE, X, X, 16, R),
261 /* scales 16-bit components to 8-bits. */
262#endif
263
264 { NULL /*name*/, 0, 0, 0, 0, 0, 0, 0/*!tested*/ }
265
266#undef T
267};
268
269#define ARRAY_SIZE(a) ((sizeof a)/(sizeof a[0]))
270#define TTABLE_SIZE ARRAY_SIZE(transform_info)
271
272/* Some combinations of options that should be reversible are not; these cases
273 * are bugs.
274 */
275static int known_bad_combos[][2] =
276{
277 /* problem, antidote */
278 { PNG_TRANSFORM_SHIFT | PNG_TRANSFORM_INVERT_ALPHA, 0/*antidote*/ }
279};
280
281static int
282is_combo(int transforms)
283{
284 return transforms & (transforms-1); /* non-zero if more than one set bit */
285}
286
287static int
288first_transform(int transforms)
289{
290 return transforms & -transforms; /* lowest set bit */
291}
292
293static int
294is_bad_combo(int transforms)
295{
296 unsigned int i;
297
298 for (i=0; i<ARRAY_SIZE(known_bad_combos); ++i)
299 {
300 int combo = known_bad_combos[i][0];
301
302 if ((combo & transforms) == combo &&
303 (transforms & known_bad_combos[i][1]) == 0)
304 return 1;
305 }
306
307 return 0; /* combo is ok */
308}
309
310static const char *
311transform_name(int t)
312 /* The name, if 't' has multiple bits set the name of the lowest set bit is
313 * returned.
314 */
315{
316 unsigned int i;
317
318 t &= -t; /* first set bit */
319
320 for (i=0; i<TTABLE_SIZE; ++i)
321 if (transform_info[i].name != NULL)
322 if ((transform_info[i].transform & t) != 0)
323 return transform_info[i].name;
324
325 return "invalid transform";
326}
327
328/* Variables calculated by validate_T below and used to record all the supported
329 * transforms. Need (unsigned int) here because of the places where these
330 * values are used (unsigned compares in the 'exhaustive' iterator.)
331 */
332static unsigned int read_transforms, write_transforms, rw_transforms;
333
334static void
335validate_T(void)
336 /* Validate the above table - this just builds the above values */
337{
338 unsigned int i;
339
340 for (i=0; i<TTABLE_SIZE; ++i)
341 {
342 if (transform_info[i].name != NULL)
343 {
344 if (transform_info[i].when & TRANSFORM_R)
345 read_transforms |= transform_info[i].transform;
346
347 if (transform_info[i].when & TRANSFORM_W)
348 write_transforms |= transform_info[i].transform;
349 }
350 }
351
352 /* Reversible transforms are those which are supported on both read and
353 * write.
354 */
355 rw_transforms = read_transforms & write_transforms;
356}
357
358/* FILE DATA HANDLING
359 * The original file is cached in memory. During write the output file is
360 * written to memory.
361 *
362 * In both cases the file data is held in a linked list of buffers - not all
363 * of these are in use at any time.
364 */
365#define NEW(type) ((type *)malloc(sizeof (type)))
366#define DELETE(ptr) (free(ptr))
367
368struct buffer_list
369{
370 struct buffer_list *next; /* next buffer in list */
371 png_byte buffer[1024]; /* the actual buffer */
372};
373
374struct buffer
375{
376 struct buffer_list *last; /* last buffer in use */
377 size_t end_count; /* bytes in the last buffer */
378 struct buffer_list *current; /* current buffer being read */
379 size_t read_count; /* count of bytes read from current */
380 struct buffer_list first; /* the very first buffer */
381};
382
383static void
384buffer_init(struct buffer *buffer)
385 /* Call this only once for a given buffer */
386{
387 buffer->first.next = NULL;
388 buffer->last = NULL;
389 buffer->current = NULL;
390}
391
392static void
393buffer_destroy_list(struct buffer_list *list)
394{
395 if (list != NULL)
396 {
397 struct buffer_list *next = list->next;
398 DELETE(list);
399 buffer_destroy_list(next);
400 }
401}
402
403static void
404buffer_destroy(struct buffer *buffer)
405{
406 struct buffer_list *list = buffer->first.next;
407 buffer_init(buffer);
408 buffer_destroy_list(list);
409}
410
411#ifdef PNG_WRITE_PNG_SUPPORTED
412static void
413buffer_start_write(struct buffer *buffer)
414{
415 buffer->last = &buffer->first;
416 buffer->end_count = 0;
417 buffer->current = NULL;
418}
419#endif
420
421static void
422buffer_start_read(struct buffer *buffer)
423{
424 buffer->current = &buffer->first;
425 buffer->read_count = 0;
426}
427
428#ifdef ENOMEM /* required by POSIX 1003.1 */
429# define MEMORY ENOMEM
430#else
431# define MEMORY ERANGE /* required by ANSI-C */
432#endif
433static struct buffer *
434get_buffer(png_structp pp)
435 /* Used from libpng callbacks to get the current buffer */
436{
437 return (struct buffer*)png_get_io_ptr(pp);
438}
439
440static struct buffer_list *
441buffer_extend(struct buffer_list *current)
442{
443 struct buffer_list *add;
444
445 assert(current->next == NULL);
446
447 add = NEW(struct buffer_list);
448 if (add == NULL)
449 return NULL;
450
451 add->next = NULL;
452 current->next = add;
453
454 return add;
455}
456
457/* Load a buffer from a file; does the equivalent of buffer_start_write. On a
458 * read error returns an errno value, else returns 0.
459 */
460static int
461buffer_from_file(struct buffer *buffer, FILE *fp)
462{
463 struct buffer_list *last = &buffer->first;
464 size_t count = 0;
465
466 for (;;)
467 {
468 size_t r = fread(last->buffer+count, 1/*size*/,
469 (sizeof last->buffer)-count, fp);
470
471 if (r > 0)
472 {
473 count += r;
474
475 if (count >= sizeof last->buffer)
476 {
477 assert(count == sizeof last->buffer);
478 count = 0;
479
480 if (last->next == NULL)
481 {
482 last = buffer_extend(last);
483 if (last == NULL)
484 return MEMORY;
485 }
486
487 else
488 last = last->next;
489 }
490 }
491
492 else /* fread failed - probably end of file */
493 {
494 if (feof(fp))
495 {
496 buffer->last = last;
497 buffer->end_count = count;
498 return 0; /* no error */
499 }
500
501 /* Some kind of funky error; errno should be non-zero */
502 return errno == 0 ? ERANGE : errno;
503 }
504 }
505}
506
507/* This structure is used to control the test of a single file. */
508typedef enum
509{
510 VERBOSE, /* switches on all messages */
511 INFORMATION,
512 WARNINGS, /* switches on warnings */
513 LIBPNG_WARNING,
514 APP_WARNING,
515 ERRORS, /* just errors */
516 APP_FAIL, /* continuable error - no need to longjmp */
517 LIBPNG_ERROR, /* this and higher cause a longjmp */
518 LIBPNG_BUG, /* erroneous behavior in libpng */
519 APP_ERROR, /* such as out-of-memory in a callback */
520 QUIET, /* no normal messages */
521 USER_ERROR, /* such as file-not-found */
522 INTERNAL_ERROR
523} error_level;
524#define LEVEL_MASK 0xf /* where the level is in 'options' */
525
526#define EXHAUSTIVE 0x010 /* Test all combinations of active options */
527#define STRICT 0x020 /* Fail on warnings as well as errors */
528#define LOG 0x040 /* Log pass/fail to stdout */
529#define CONTINUE 0x080 /* Continue on APP_FAIL errors */
530#define SKIP_BUGS 0x100 /* Skip over known bugs */
531#define LOG_SKIPPED 0x200 /* Log skipped bugs */
532#define FIND_BAD_COMBOS 0x400 /* Attempt to deduce bad combos */
533#define LIST_COMBOS 0x800 /* List combos by name */
534
535/* Result masks apply to the result bits in the 'results' field below; these
536 * bits are simple 1U<<error_level. A pass requires either nothing worse than
537 * warnings (--relaxes) or nothing worse than information (--strict)
538 */
539#define RESULT_STRICT(r) (((r) & ~((1U<<WARNINGS)-1)) == 0)
540#define RESULT_RELAXED(r) (((r) & ~((1U<<ERRORS)-1)) == 0)
541
542struct display
543{
544 jmp_buf error_return; /* Where to go to on error */
545
546 const char *filename; /* The name of the original file */
547 const char *operation; /* Operation being performed */
548 int transforms; /* Transform used in operation */
549 png_uint_32 options; /* See display_log below */
550 png_uint_32 results; /* A mask of errors seen */
551
552
553 png_structp original_pp; /* used on the original read */
554 png_infop original_ip; /* set by the original read */
555
556 size_t original_rowbytes; /* of the original rows: */
557 png_bytepp original_rows; /* from the original read */
558
559 /* Original chunks valid */
560 png_uint_32 chunks;
561
562 /* Original IHDR information */
563 png_uint_32 width;
564 png_uint_32 height;
565 int bit_depth;
566 int color_type;
567 int interlace_method;
568 int compression_method;
569 int filter_method;
570
571 /* Derived information for the original image. */
572 int active_transforms; /* transforms that do something on read */
573 int ignored_transforms; /* transforms that should do nothing */
574
575 /* Used on a read, both the original read and when validating a written
576 * image.
577 */
578 png_structp read_pp;
579 png_infop read_ip;
580
581# ifdef PNG_WRITE_PNG_SUPPORTED
582 /* Used to write a new image (the original info_ptr is used) */
583 png_structp write_pp;
584 struct buffer written_file; /* where the file gets written */
585# endif
586
587 struct buffer original_file; /* Data read from the original file */
588};
589
590static void
591display_init(struct display *dp)
592 /* Call this only once right at the start to initialize the control
593 * structure, the (struct buffer) lists are maintained across calls - the
594 * memory is not freed.
595 */
596{
597 memset(dp, 0, sizeof *dp);
598 dp->options = WARNINGS; /* default to !verbose, !quiet */
599 dp->filename = NULL;
600 dp->operation = NULL;
601 dp->original_pp = NULL;
602 dp->original_ip = NULL;
603 dp->original_rows = NULL;
604 dp->read_pp = NULL;
605 dp->read_ip = NULL;
606 buffer_init(&dp->original_file);
607
608# ifdef PNG_WRITE_PNG_SUPPORTED
609 dp->write_pp = NULL;
610 buffer_init(&dp->written_file);
611# endif
612}
613
614static void
615display_clean_read(struct display *dp)
616{
617 if (dp->read_pp != NULL)
618 png_destroy_read_struct(&dp->read_pp, &dp->read_ip, NULL);
619}
620
621#ifdef PNG_WRITE_PNG_SUPPORTED
622static void
623display_clean_write(struct display *dp)
624{
625 if (dp->write_pp != NULL)
626 png_destroy_write_struct(&dp->write_pp, NULL);
627}
628#endif
629
630static void
631display_clean(struct display *dp)
632{
633# ifdef PNG_WRITE_PNG_SUPPORTED
634 display_clean_write(dp);
635# endif
636 display_clean_read(dp);
637
638 dp->original_rowbytes = 0;
639 dp->original_rows = NULL;
640 dp->chunks = 0;
641
642 png_destroy_read_struct(&dp->original_pp, &dp->original_ip, NULL);
643 /* leave the filename for error detection */
644 dp->results = 0; /* reset for next time */
645}
646
647static void
648display_destroy(struct display *dp)
649{
650 /* Release any memory held in the display. */
651# ifdef PNG_WRITE_PNG_SUPPORTED
652 buffer_destroy(&dp->written_file);
653# endif
654
655 buffer_destroy(&dp->original_file);
656}
657
658static struct display *
659get_dp(png_structp pp)
660 /* The display pointer is always stored in the png_struct error pointer */
661{
662 struct display *dp = (struct display*)png_get_error_ptr(pp);
663
664 if (dp == NULL)
665 {
666 fprintf(stderr, "pngimage: internal error (no display)\n");
667 exit(99); /* prevents a crash */
668 }
669
670 return dp;
671}
672
673/* error handling */
674#ifdef __GNUC__
675# define VGATTR __attribute__((__format__ (__printf__,3,4)))
676 /* Required to quiet GNUC warnings when the compiler sees a stdarg function
677 * that calls one of the stdio v APIs.
678 */
679#else
680# define VGATTR
681#endif
682static void VGATTR
683display_log(struct display *dp, error_level level, const char *fmt, ...)
684 /* 'level' is as above, fmt is a stdio style format string. This routine
685 * does not return if level is above LIBPNG_WARNING
686 */
687{
688 dp->results |= 1U << level;
689
690 if (level > (error_level)(dp->options & LEVEL_MASK))
691 {
692 const char *lp;
693 va_list ap;
694
695 switch (level)
696 {
697 case INFORMATION: lp = "information"; break;
698 case LIBPNG_WARNING: lp = "warning(libpng)"; break;
699 case APP_WARNING: lp = "warning(pngimage)"; break;
700 case APP_FAIL: lp = "error(continuable)"; break;
701 case LIBPNG_ERROR: lp = "error(libpng)"; break;
702 case LIBPNG_BUG: lp = "bug(libpng)"; break;
703 case APP_ERROR: lp = "error(pngimage)"; break;
704 case USER_ERROR: lp = "error(user)"; break;
705
706 case INTERNAL_ERROR: /* anything unexpected is an internal error: */
707 case VERBOSE: case WARNINGS: case ERRORS: case QUIET:
708 default: lp = "bug(pngimage)"; break;
709 }
710
711 fprintf(stderr, "%s: %s: %s",
712 dp->filename != NULL ? dp->filename : "<stdin>", lp, dp->operation);
713
714 if (dp->transforms != 0)
715 {
716 int tr = dp->transforms;
717
718 if (is_combo(tr))
719 {
720 if (dp->options & LIST_COMBOS)
721 {
722 int trx = tr;
723
724 fprintf(stderr, "(");
725 if (trx)
726 {
727 int start = 0;
728
729 while (trx)
730 {
731 int trz = trx & -trx;
732
733 if (start) fprintf(stderr, "+");
734 fprintf(stderr, "%s", transform_name(trz));
735 start = 1;
736 trx &= ~trz;
737 }
738 }
739
740 else
741 fprintf(stderr, "-");
742 fprintf(stderr, ")");
743 }
744
745 else
746 fprintf(stderr, "(0x%x)", tr);
747 }
748
749 else
750 fprintf(stderr, "(%s)", transform_name(tr));
751 }
752
753 fprintf(stderr, ": ");
754
755 va_start(ap, fmt);
756 vfprintf(stderr, fmt, ap);
757 va_end(ap);
758
759 fputc('\n', stderr);
760 }
761 /* else do not output any message */
762
763 /* Errors cause this routine to exit to the fail code */
764 if (level > APP_FAIL || (level > ERRORS && !(dp->options & CONTINUE)))
765 longjmp(dp->error_return, level);
766}
767
768/* error handler callbacks for libpng */
769static void PNGCBAPI
770display_warning(png_structp pp, png_const_charp warning)
771{
772 display_log(get_dp(pp), LIBPNG_WARNING, "%s", warning);
773}
774
775static void PNGCBAPI
776display_error(png_structp pp, png_const_charp error)
777{
778 struct display *dp = get_dp(pp);
779
780 display_log(dp, LIBPNG_ERROR, "%s", error);
781}
782
783static void
784display_cache_file(struct display *dp, const char *filename)
785 /* Does the initial cache of the file. */
786{
787 FILE *fp;
788 int ret;
789
790 dp->filename = filename;
791
792 if (filename != NULL)
793 {
794 fp = fopen(filename, "rb");
795 if (fp == NULL)
796 display_log(dp, USER_ERROR, "open failed: %s", strerror(errno));
797 }
798
799 else
800 fp = stdin;
801
802 ret = buffer_from_file(&dp->original_file, fp);
803
804 fclose(fp);
805
806 if (ret != 0)
807 display_log(dp, APP_ERROR, "read failed: %s", strerror(ret));
808}
809
810static void
811buffer_read(struct display *dp, struct buffer *bp, png_bytep data,
812 size_t size)
813{
814 struct buffer_list *last = bp->current;
815 size_t read_count = bp->read_count;
816
817 while (size > 0)
818 {
819 size_t avail;
820
821 if (last == NULL ||
822 (last == bp->last && read_count >= bp->end_count))
823 {
824 display_log(dp, USER_ERROR, "file truncated (%lu bytes)",
825 (unsigned long)size);
826 /*NOTREACHED*/
827 break;
828 }
829
830 else if (read_count >= sizeof last->buffer)
831 {
832 /* Move to the next buffer: */
833 last = last->next;
834 read_count = 0;
835 bp->current = last; /* Avoid update outside the loop */
836
837 /* And do a sanity check (the EOF case is caught above) */
838 if (last == NULL)
839 {
840 display_log(dp, INTERNAL_ERROR, "damaged buffer list");
841 /*NOTREACHED*/
842 break;
843 }
844 }
845
846 avail = (sizeof last->buffer) - read_count;
847 if (avail > size)
848 avail = size;
849
850 memcpy(data, last->buffer + read_count, avail);
851 read_count += avail;
852 size -= avail;
853 data += avail;
854 }
855
856 bp->read_count = read_count;
857}
858
859static void PNGCBAPI
860read_function(png_structp pp, png_bytep data, size_t size)
861{
862 buffer_read(get_dp(pp), get_buffer(pp), data, size);
863}
864
865static void
866read_png(struct display *dp, struct buffer *bp, const char *operation,
867 int transforms)
868{
869 png_structp pp;
870 png_infop ip;
871
872 /* This cleans out any previous read and sets operation and transforms to
873 * empty.
874 */
875 display_clean_read(dp);
876
877 if (operation != NULL) /* else this is a verify and do not overwrite info */
878 {
879 dp->operation = operation;
880 dp->transforms = transforms;
881 }
882
883 dp->read_pp = pp = png_create_read_struct(PNG_LIBPNG_VER_STRING, dp,
884 display_error, display_warning);
885 if (pp == NULL)
886 display_log(dp, LIBPNG_ERROR, "failed to create read struct");
887
888 /* The png_read_png API requires us to make the info struct, but it does the
889 * call to png_read_info.
890 */
891 dp->read_ip = ip = png_create_info_struct(pp);
892 if (ip == NULL)
893 display_log(dp, LIBPNG_ERROR, "failed to create info struct");
894
895# ifdef PNG_SET_USER_LIMITS_SUPPORTED
896 /* Remove the user limits, if any */
897 png_set_user_limits(pp, 0x7fffffff, 0x7fffffff);
898# endif
899
900 /* Set the IO handling */
901 buffer_start_read(bp);
902 png_set_read_fn(pp, bp, read_function);
903
904 png_read_png(pp, ip, transforms, NULL/*params*/);
905
906#if 0 /* crazy debugging */
907 {
908 png_bytep pr = png_get_rows(pp, ip)[0];
909 size_t rb = png_get_rowbytes(pp, ip);
910 size_t cb;
911 char c = ' ';
912
913 fprintf(stderr, "%.4x %2d (%3lu bytes):", transforms, png_get_bit_depth(pp,ip), (unsigned long)rb);
914
915 for (cb=0; cb<rb; ++cb)
916 fputc(c, stderr), fprintf(stderr, "%.2x", pr[cb]), c='.';
917
918 fputc('\n', stderr);
919 }
920#endif
921}
922
923static void
924update_display(struct display *dp)
925 /* called once after the first read to update all the info, original_pp and
926 * original_ip must have been filled in.
927 */
928{
929 png_structp pp;
930 png_infop ip;
931
932 /* Now perform the initial read with a 0 transform. */
933 read_png(dp, &dp->original_file, "original read", 0/*no transform*/);
934
935 /* Move the result to the 'original' fields */
936 dp->original_pp = pp = dp->read_pp, dp->read_pp = NULL;
937 dp->original_ip = ip = dp->read_ip, dp->read_ip = NULL;
938
939 dp->original_rowbytes = png_get_rowbytes(pp, ip);
940 if (dp->original_rowbytes == 0)
941 display_log(dp, LIBPNG_BUG, "png_get_rowbytes returned 0");
942
943 dp->chunks = png_get_valid(pp, ip, 0xffffffff);
944 if ((dp->chunks & PNG_INFO_IDAT) == 0) /* set by png_read_png */
945 display_log(dp, LIBPNG_BUG, "png_read_png did not set IDAT flag");
946
947 dp->original_rows = png_get_rows(pp, ip);
948 if (dp->original_rows == NULL)
949 display_log(dp, LIBPNG_BUG, "png_read_png did not create row buffers");
950
951 if (!png_get_IHDR(pp, ip,
952 &dp->width, &dp->height, &dp->bit_depth, &dp->color_type,
953 &dp->interlace_method, &dp->compression_method, &dp->filter_method))
954 display_log(dp, LIBPNG_BUG, "png_get_IHDR failed");
955
956 /* 'active' transforms are discovered based on the original image format;
957 * running one active transform can activate others. At present the code
958 * does not attempt to determine the closure.
959 */
960 {
961 png_uint_32 chunks = dp->chunks;
962 int active = 0, inactive = 0;
963 int ct = dp->color_type;
964 int bd = dp->bit_depth;
965 unsigned int i;
966
967 for (i=0; i<TTABLE_SIZE; ++i)
968 {
969 if (transform_info[i].name != NULL)
970 {
971 int transform = transform_info[i].transform;
972
973 if ((transform_info[i].valid_chunks == 0 ||
974 (transform_info[i].valid_chunks & chunks) != 0) &&
975 (transform_info[i].color_mask_required & ct) ==
976 transform_info[i].color_mask_required &&
977 (transform_info[i].color_mask_absent & ct) == 0 &&
978 (transform_info[i].bit_depths & bd) != 0 &&
979 (transform_info[i].when & TRANSFORM_R) != 0)
980 active |= transform;
981
982 else if ((transform_info[i].when & TRANSFORM_R) != 0)
983 inactive |= transform;
984 }
985 }
986
987 /* Some transforms appear multiple times in the table; the 'active' status
988 * is the logical OR of these and the inactive status must be adjusted to
989 * take this into account.
990 */
991 inactive &= ~active;
992
993 dp->active_transforms = active;
994 dp->ignored_transforms = inactive; /* excluding write-only transforms */
995 }
996}
997
998static int
999compare_read(struct display *dp, int applied_transforms)
1000{
1001 /* Compare the png_info from read_ip with original_info */
1002 size_t rowbytes;
1003 png_uint_32 width, height;
1004 int bit_depth, color_type;
1005 int interlace_method, compression_method, filter_method;
1006 const char *e = NULL;
1007
1008 if (!png_get_IHDR(dp->read_pp, dp->read_ip, &width, &height, &bit_depth,
1009 &color_type, &interlace_method, &compression_method, &filter_method))
1010 display_log(dp, LIBPNG_BUG, "png_get_IHDR failed");
1011
1012# define C(item) if (item != dp->item) \
1013 display_log(dp, APP_WARNING, "IHDR " #item "(%lu) changed to %lu",\
1014 (unsigned long)dp->item, (unsigned long)item), e = #item
1015
1016 /* The IHDR should be identical: */
1017 C(width);
1018 C(height);
1019 C(bit_depth);
1020 C(color_type);
1021# ifdef PNG_WRITE_INTERLACING_SUPPORTED
1022 /* If write interlace has been disabled, the PNG file is still
1023 * written correctly, but as a regular (not-interlaced) PNG.
1024 */
1025 C(interlace_method);
1026# endif
1027 C(compression_method);
1028 C(filter_method);
1029
1030 /* 'e' remains set to the name of the last thing changed: */
1031 if (e)
1032 display_log(dp, APP_ERROR, "IHDR changed (%s)", e);
1033
1034 /* All the chunks from the original PNG should be preserved in the output PNG
1035 * because the PNG format has not been changed.
1036 */
1037 {
1038 unsigned long chunks =
1039 png_get_valid(dp->read_pp, dp->read_ip, 0xffffffff);
1040
1041 if (chunks != dp->chunks)
1042 display_log(dp, APP_FAIL, "PNG chunks changed from 0x%lx to 0x%lx",
1043 (unsigned long)dp->chunks, chunks);
1044 }
1045
1046 /* rowbytes should be the same */
1047 rowbytes = png_get_rowbytes(dp->read_pp, dp->read_ip);
1048
1049 /* NOTE: on 64-bit systems this may trash the top bits of rowbytes,
1050 * which could lead to weird error messages.
1051 */
1052 if (rowbytes != dp->original_rowbytes)
1053 display_log(dp, APP_ERROR, "PNG rowbytes changed from %lu to %lu",
1054 (unsigned long)dp->original_rowbytes, (unsigned long)rowbytes);
1055
1056 /* The rows should be the same too, unless the applied transforms includes
1057 * the shift transform, in which case low bits may have been lost.
1058 */
1059 {
1060 png_bytepp rows = png_get_rows(dp->read_pp, dp->read_ip);
1061 unsigned int mask; /* mask (if not zero) for the final byte */
1062
1063 if (bit_depth < 8)
1064 {
1065 /* Need the stray bits at the end, this depends only on the low bits
1066 * of the image width; overflow does not matter. If the width is an
1067 * exact multiple of 8 bits this gives a mask of 0, not 0xff.
1068 */
1069 mask = 0xff & (0xff00 >> ((bit_depth * width) & 7));
1070 }
1071
1072 else
1073 mask = 0;
1074
1075 if (rows == NULL)
1076 display_log(dp, LIBPNG_BUG, "png_get_rows returned NULL");
1077
1078 if ((applied_transforms & PNG_TRANSFORM_SHIFT) == 0 ||
1079 (dp->active_transforms & PNG_TRANSFORM_SHIFT) == 0 ||
1080 color_type == PNG_COLOR_TYPE_PALETTE)
1081 {
1082 unsigned long y;
1083
1084 for (y=0; y<height; ++y)
1085 {
1086 png_bytep row = rows[y];
1087 png_bytep orig = dp->original_rows[y];
1088
1089 if (memcmp(row, orig, rowbytes-(mask != 0)) != 0 || (mask != 0 &&
1090 ((row[rowbytes-1] & mask) != (orig[rowbytes-1] & mask))))
1091 {
1092 size_t x;
1093
1094 /* Find the first error */
1095 for (x=0; x<rowbytes-1; ++x)
1096 if (row[x] != orig[x])
1097 break;
1098
1099 display_log(dp, APP_FAIL,
1100 "byte(%lu,%lu) changed 0x%.2x -> 0x%.2x",
1101 (unsigned long)x, (unsigned long)y, orig[x], row[x]);
1102 return 0; /* don't keep reporting failed rows on 'continue' */
1103 }
1104 }
1105 }
1106
1107 else
1108# ifdef PNG_sBIT_SUPPORTED
1109 {
1110 unsigned long y;
1111 int bpp; /* bits-per-pixel then bytes-per-pixel */
1112 /* components are up to 8 bytes in size */
1113 png_byte sig_bits[8];
1114 png_color_8p sBIT;
1115
1116 if (png_get_sBIT(dp->read_pp, dp->read_ip, &sBIT) != PNG_INFO_sBIT)
1117 display_log(dp, INTERNAL_ERROR,
1118 "active shift transform but no sBIT in file");
1119
1120 switch (color_type)
1121 {
1122 case PNG_COLOR_TYPE_GRAY:
1123 sig_bits[0] = sBIT->gray;
1124 bpp = bit_depth;
1125 break;
1126
1127 case PNG_COLOR_TYPE_GA:
1128 sig_bits[0] = sBIT->gray;
1129 sig_bits[1] = sBIT->alpha;
1130 bpp = 2 * bit_depth;
1131 break;
1132
1133 case PNG_COLOR_TYPE_RGB:
1134 sig_bits[0] = sBIT->red;
1135 sig_bits[1] = sBIT->green;
1136 sig_bits[2] = sBIT->blue;
1137 bpp = 3 * bit_depth;
1138 break;
1139
1140 case PNG_COLOR_TYPE_RGBA:
1141 sig_bits[0] = sBIT->red;
1142 sig_bits[1] = sBIT->green;
1143 sig_bits[2] = sBIT->blue;
1144 sig_bits[3] = sBIT->alpha;
1145 bpp = 4 * bit_depth;
1146 break;
1147
1148 default:
1149 display_log(dp, LIBPNG_ERROR, "invalid colour type %d",
1150 color_type);
1151 /*NOTREACHED*/
1152 memset(sig_bits, 0, sizeof(sig_bits));
1153 bpp = 0;
1154 break;
1155 }
1156
1157 {
1158 int b;
1159
1160 for (b=0; 8*b<bpp; ++b)
1161 {
1162 /* libpng should catch this; if not there is a security issue
1163 * because an app (like this one) may overflow an array. In fact
1164 * libpng doesn't catch this at present.
1165 */
1166 if (sig_bits[b] == 0 || sig_bits[b] > bit_depth/*!palette*/)
1167 display_log(dp, LIBPNG_BUG,
1168 "invalid sBIT[%u] value %d returned for PNG bit depth %d",
1169 b, sig_bits[b], bit_depth);
1170 }
1171 }
1172
1173 if (bpp < 8 && bpp != bit_depth)
1174 {
1175 /* sanity check; this is a grayscale PNG; something is wrong in the
1176 * code above.
1177 */
1178 display_log(dp, INTERNAL_ERROR, "invalid bpp %u for bit_depth %u",
1179 bpp, bit_depth);
1180 }
1181
1182 switch (bit_depth)
1183 {
1184 int b;
1185
1186 case 16: /* Two bytes per component, big-endian */
1187 for (b = (bpp >> 4); b > 0; --b)
1188 {
1189 unsigned int sig = (unsigned int)(0xffff0000 >> sig_bits[b]);
1190
1191 sig_bits[2*b+1] = (png_byte)sig;
1192 sig_bits[2*b+0] = (png_byte)(sig >> 8); /* big-endian */
1193 }
1194 break;
1195
1196 case 8: /* One byte per component */
1197 for (b=0; b*8 < bpp; ++b)
1198 sig_bits[b] = (png_byte)(0xff00 >> sig_bits[b]);
1199 break;
1200
1201 case 1: /* allowed, but dumb */
1202 /* Value is 1 */
1203 sig_bits[0] = 0xff;
1204 break;
1205
1206 case 2: /* Replicate 4 times */
1207 /* Value is 1 or 2 */
1208 b = 0x3 & ((0x3<<2) >> sig_bits[0]);
1209 b |= b << 2;
1210 b |= b << 4;
1211 sig_bits[0] = (png_byte)b;
1212 break;
1213
1214 case 4: /* Replicate twice */
1215 /* Value is 1, 2, 3 or 4 */
1216 b = 0xf & ((0xf << 4) >> sig_bits[0]);
1217 b |= b << 4;
1218 sig_bits[0] = (png_byte)b;
1219 break;
1220
1221 default:
1222 display_log(dp, LIBPNG_BUG, "invalid bit depth %d", bit_depth);
1223 break;
1224 }
1225
1226 /* Convert bpp to bytes; this gives '1' for low-bit depth grayscale,
1227 * where there are multiple pixels per byte.
1228 */
1229 bpp = (bpp+7) >> 3;
1230
1231 /* The mask can be combined with sig_bits[0] */
1232 if (mask != 0)
1233 {
1234 mask &= sig_bits[0];
1235
1236 if (bpp != 1 || mask == 0)
1237 display_log(dp, INTERNAL_ERROR, "mask calculation error %u, %u",
1238 bpp, mask);
1239 }
1240
1241 for (y=0; y<height; ++y)
1242 {
1243 png_bytep row = rows[y];
1244 png_bytep orig = dp->original_rows[y];
1245 unsigned long x;
1246
1247 for (x=0; x<(width-(mask!=0)); ++x)
1248 {
1249 int b;
1250
1251 for (b=0; b<bpp; ++b)
1252 {
1253 if ((*row++ & sig_bits[b]) != (*orig++ & sig_bits[b]))
1254 {
1255 display_log(dp, APP_FAIL,
1256 "significant bits at (%lu[%u],%lu) changed %.2x->%.2x",
1257 x, b, y, orig[-1], row[-1]);
1258 return 0;
1259 }
1260 }
1261 }
1262
1263 if (mask != 0 && (*row & mask) != (*orig & mask))
1264 {
1265 display_log(dp, APP_FAIL,
1266 "significant bits at (%lu[end],%lu) changed", x, y);
1267 return 0;
1268 }
1269 } /* for y */
1270 }
1271# else /* !sBIT */
1272 display_log(dp, INTERNAL_ERROR,
1273 "active shift transform but no sBIT support");
1274# endif /* !sBIT */
1275 }
1276
1277 return 1; /* compare succeeded */
1278}
1279
1280#ifdef PNG_WRITE_PNG_SUPPORTED
1281static void
1282buffer_write(struct display *dp, struct buffer *buffer, png_bytep data,
1283 size_t size)
1284 /* Generic write function used both from the write callback provided to
1285 * libpng and from the generic read code.
1286 */
1287{
1288 /* Write the data into the buffer, adding buffers as required */
1289 struct buffer_list *last = buffer->last;
1290 size_t end_count = buffer->end_count;
1291
1292 while (size > 0)
1293 {
1294 size_t avail;
1295
1296 if (end_count >= sizeof last->buffer)
1297 {
1298 if (last->next == NULL)
1299 {
1300 last = buffer_extend(last);
1301
1302 if (last == NULL)
1303 display_log(dp, APP_ERROR, "out of memory saving file");
1304 }
1305
1306 else
1307 last = last->next;
1308
1309 buffer->last = last; /* avoid the need to rewrite every time */
1310 end_count = 0;
1311 }
1312
1313 avail = (sizeof last->buffer) - end_count;
1314 if (avail > size)
1315 avail = size;
1316
1317 memcpy(last->buffer + end_count, data, avail);
1318 end_count += avail;
1319 size -= avail;
1320 data += avail;
1321 }
1322
1323 buffer->end_count = end_count;
1324}
1325
1326static void PNGCBAPI
1327write_function(png_structp pp, png_bytep data, size_t size)
1328{
1329 buffer_write(get_dp(pp), get_buffer(pp), data, size);
1330}
1331
1332static void
1333write_png(struct display *dp, png_infop ip, int transforms)
1334{
1335 display_clean_write(dp); /* safety */
1336
1337 buffer_start_write(&dp->written_file);
1338 dp->operation = "write";
1339 dp->transforms = transforms;
1340
1341 dp->write_pp = png_create_write_struct(PNG_LIBPNG_VER_STRING, dp,
1342 display_error, display_warning);
1343
1344 if (dp->write_pp == NULL)
1345 display_log(dp, APP_ERROR, "failed to create write png_struct");
1346
1347 png_set_write_fn(dp->write_pp, &dp->written_file, write_function,
1348 NULL/*flush*/);
1349
1350# ifdef PNG_SET_USER_LIMITS_SUPPORTED
1351 /* Remove the user limits, if any */
1352 png_set_user_limits(dp->write_pp, 0x7fffffff, 0x7fffffff);
1353# endif
1354
1355 /* Certain transforms require the png_info to be zapped to allow the
1356 * transform to work correctly.
1357 */
1358 if (transforms & (PNG_TRANSFORM_PACKING|
1359 PNG_TRANSFORM_STRIP_FILLER|
1360 PNG_TRANSFORM_STRIP_FILLER_BEFORE))
1361 {
1362 int ct = dp->color_type;
1363
1364 if (transforms & (PNG_TRANSFORM_STRIP_FILLER|
1365 PNG_TRANSFORM_STRIP_FILLER_BEFORE))
1366 ct &= ~PNG_COLOR_MASK_ALPHA;
1367
1368 png_set_IHDR(dp->write_pp, ip, dp->width, dp->height, dp->bit_depth, ct,
1369 dp->interlace_method, dp->compression_method, dp->filter_method);
1370 }
1371
1372 png_write_png(dp->write_pp, ip, transforms, NULL/*params*/);
1373
1374 /* Clean it on the way out - if control returns to the caller then the
1375 * written_file contains the required data.
1376 */
1377 display_clean_write(dp);
1378}
1379#endif /* WRITE_PNG */
1380
1381static int
1382skip_transform(struct display *dp, int tr)
1383 /* Helper to test for a bad combo and log it if it is skipped */
1384{
1385 if ((dp->options & SKIP_BUGS) != 0 && is_bad_combo(tr))
1386 {
1387 /* Log this to stdout if logging is on, otherwise just do an information
1388 * display_log.
1389 */
1390 if ((dp->options & LOG_SKIPPED) != 0)
1391 {
1392 printf("SKIP: %s transforms ", dp->filename);
1393
1394 while (tr != 0)
1395 {
1396 int next = first_transform(tr);
1397 tr &= ~next;
1398
1399 printf("%s", transform_name(next));
1400 if (tr != 0)
1401 putchar('+');
1402 }
1403
1404 putchar('\n');
1405 }
1406
1407 else
1408 display_log(dp, INFORMATION, "%s: skipped known bad combo 0x%x",
1409 dp->filename, tr);
1410
1411 return 1; /* skip */
1412 }
1413
1414 return 0; /* don't skip */
1415}
1416
1417static void
1418test_one_file(struct display *dp, const char *filename)
1419{
1420 /* First cache the file and update the display original file
1421 * information for the new file.
1422 */
1423 dp->operation = "cache file";
1424 dp->transforms = 0;
1425 display_cache_file(dp, filename);
1426 update_display(dp);
1427
1428 /* First test: if there are options that should be ignored for this file
1429 * verify that they really are ignored.
1430 */
1431 if (dp->ignored_transforms != 0)
1432 {
1433 read_png(dp, &dp->original_file, "ignored transforms",
1434 dp->ignored_transforms);
1435
1436 /* The result should be identical to the original_rows */
1437 if (!compare_read(dp, 0/*transforms applied*/))
1438 return; /* no point testing more */
1439 }
1440
1441#ifdef PNG_WRITE_PNG_SUPPORTED
1442 /* Second test: write the original PNG data out to a new file (to test the
1443 * write side) then read the result back in and make sure that it hasn't
1444 * changed.
1445 */
1446 dp->operation = "write";
1447 write_png(dp, dp->original_ip, 0/*transforms*/);
1448 read_png(dp, &dp->written_file, NULL, 0/*transforms*/);
1449 if (!compare_read(dp, 0/*transforms applied*/))
1450 return;
1451#endif
1452
1453 /* Third test: the active options. Test each in turn, or, with the
1454 * EXHAUSTIVE option, test all possible combinations.
1455 */
1456 {
1457 /* Use unsigned int here because the code below to increment through all
1458 * the possibilities exhaustively has to use a compare and that must be
1459 * unsigned, because some transforms are negative on a 16-bit system.
1460 */
1461 unsigned int active = dp->active_transforms;
1462 int exhaustive = (dp->options & EXHAUSTIVE) != 0;
1463 unsigned int current = first_transform(active);
1464 unsigned int bad_transforms = 0;
1465 unsigned int bad_combo = ~0U; /* bitwise AND of failing transforms */
1466 unsigned int bad_combo_list = 0; /* bitwise OR of failures */
1467
1468 for (;;)
1469 {
1470 read_png(dp, &dp->original_file, "active transforms", current);
1471
1472 /* If this involved any irreversible transformations then if we write
1473 * it out with just the reversible transformations and read it in again
1474 * with the same transforms we should get the same thing. At present
1475 * this isn't done - it just seems like a waste of time and it would
1476 * require two sets of read png_struct/png_info.
1477 *
1478 * If there were no irreversible transformations then if we write it
1479 * out and read it back in again (without the reversible transforms)
1480 * we should get back to the place where we started.
1481 */
1482#ifdef PNG_WRITE_PNG_SUPPORTED
1483 if ((current & write_transforms) == current)
1484 {
1485 /* All transforms reversible: write the PNG with the transformations
1486 * reversed, then read it back in with no transformations. The
1487 * result should be the same as the original apart from the loss of
1488 * low order bits because of the SHIFT/sBIT transform.
1489 */
1490 dp->operation = "reversible transforms";
1491 write_png(dp, dp->read_ip, current);
1492
1493 /* And if this is read back in, because all the transformations were
1494 * reversible, the result should be the same.
1495 */
1496 read_png(dp, &dp->written_file, NULL, 0);
1497 if (!compare_read(dp, current/*for the SHIFT/sBIT transform*/))
1498 {
1499 /* This set of transforms failed. If a single bit is set - if
1500 * there is just one transform - don't include this in further
1501 * 'exhaustive' tests. Notice that each transform is tested on
1502 * its own before testing combos in the exhaustive case.
1503 */
1504 if (is_combo(current))
1505 {
1506 bad_combo &= current;
1507 bad_combo_list |= current;
1508 }
1509
1510 else
1511 bad_transforms |= current;
1512 }
1513 }
1514#endif
1515
1516 /* Now move to the next transform */
1517 if (exhaustive) /* all combinations */
1518 {
1519 unsigned int next = current;
1520
1521 do
1522 {
1523 if (next == read_transforms) /* Everything tested */
1524 goto combo;
1525
1526 ++next;
1527 } /* skip known bad combos if the relevant option is set; skip
1528 * combos involving known bad single transforms in all cases.
1529 */
1530 while ( (next & read_transforms) <= current
1531 || (next & active) == 0 /* skip cases that do nothing */
1532 || (next & bad_transforms) != 0
1533 || skip_transform(dp, next));
1534
1535 assert((next & read_transforms) == next);
1536 current = next;
1537 }
1538
1539 else /* one at a time */
1540 {
1541 active &= ~current;
1542
1543 if (active == 0)
1544 goto combo;
1545
1546 current = first_transform(active);
1547 }
1548 }
1549
1550combo:
1551 if (dp->options & FIND_BAD_COMBOS)
1552 {
1553 /* bad_combos identifies the combos that occur in all failing cases;
1554 * bad_combo_list identifies transforms that do not prevent the
1555 * failure.
1556 */
1557 if (bad_combo != ~0U)
1558 printf("%s[0x%x]: PROBLEM: 0x%x[0x%x] ANTIDOTE: 0x%x\n",
1559 dp->filename, active, bad_combo, bad_combo_list,
1560 rw_transforms & ~bad_combo_list);
1561
1562 else
1563 printf("%s: no %sbad combos found\n", dp->filename,
1564 (dp->options & SKIP_BUGS) ? "additional " : "");
1565 }
1566 }
1567}
1568
1569static int
1570do_test(struct display *dp, const char *file)
1571 /* Exists solely to isolate the setjmp clobbers */
1572{
1573 int ret = setjmp(dp->error_return);
1574
1575 if (ret == 0)
1576 {
1577 test_one_file(dp, file);
1578 return 0;
1579 }
1580
1581 else if (ret < ERRORS) /* shouldn't longjmp on warnings */
1582 display_log(dp, INTERNAL_ERROR, "unexpected return code %d", ret);
1583
1584 return ret;
1585}
1586
1587int
1588main(int argc, char **argv)
1589{
1590 /* For each file on the command line test it with a range of transforms */
1591 int option_end, ilog = 0;
1592 struct display d;
1593
1594 validate_T();
1595 display_init(&d);
1596
1597 for (option_end=1; option_end<argc; ++option_end)
1598 {
1599 const char *name = argv[option_end];
1600
1601 if (strcmp(name, "--verbose") == 0)
1602 d.options = (d.options & ~LEVEL_MASK) | VERBOSE;
1603
1604 else if (strcmp(name, "--warnings") == 0)
1605 d.options = (d.options & ~LEVEL_MASK) | WARNINGS;
1606
1607 else if (strcmp(name, "--errors") == 0)
1608 d.options = (d.options & ~LEVEL_MASK) | ERRORS;
1609
1610 else if (strcmp(name, "--quiet") == 0)
1611 d.options = (d.options & ~LEVEL_MASK) | QUIET;
1612
1613 else if (strcmp(name, "--exhaustive") == 0)
1614 d.options |= EXHAUSTIVE;
1615
1616 else if (strcmp(name, "--fast") == 0)
1617 d.options &= ~EXHAUSTIVE;
1618
1619 else if (strcmp(name, "--strict") == 0)
1620 d.options |= STRICT;
1621
1622 else if (strcmp(name, "--relaxed") == 0)
1623 d.options &= ~STRICT;
1624
1625 else if (strcmp(name, "--log") == 0)
1626 {
1627 ilog = option_end; /* prevent display */
1628 d.options |= LOG;
1629 }
1630
1631 else if (strcmp(name, "--nolog") == 0)
1632 d.options &= ~LOG;
1633
1634 else if (strcmp(name, "--continue") == 0)
1635 d.options |= CONTINUE;
1636
1637 else if (strcmp(name, "--stop") == 0)
1638 d.options &= ~CONTINUE;
1639
1640 else if (strcmp(name, "--skip-bugs") == 0)
1641 d.options |= SKIP_BUGS;
1642
1643 else if (strcmp(name, "--test-all") == 0)
1644 d.options &= ~SKIP_BUGS;
1645
1646 else if (strcmp(name, "--log-skipped") == 0)
1647 d.options |= LOG_SKIPPED;
1648
1649 else if (strcmp(name, "--nolog-skipped") == 0)
1650 d.options &= ~LOG_SKIPPED;
1651
1652 else if (strcmp(name, "--find-bad-combos") == 0)
1653 d.options |= FIND_BAD_COMBOS;
1654
1655 else if (strcmp(name, "--nofind-bad-combos") == 0)
1656 d.options &= ~FIND_BAD_COMBOS;
1657
1658 else if (strcmp(name, "--list-combos") == 0)
1659 d.options |= LIST_COMBOS;
1660
1661 else if (strcmp(name, "--nolist-combos") == 0)
1662 d.options &= ~LIST_COMBOS;
1663
1664 else if (name[0] == '-' && name[1] == '-')
1665 {
1666 fprintf(stderr, "pngimage: %s: unknown option\n", name);
1667 return 99;
1668 }
1669
1670 else
1671 break; /* Not an option */
1672 }
1673
1674 {
1675 int i;
1676 int errors = 0;
1677
1678 for (i=option_end; i<argc; ++i)
1679 {
1680 {
1681 int ret = do_test(&d, argv[i]);
1682
1683 if (ret > QUIET) /* abort on user or internal error */
1684 return 99;
1685 }
1686
1687 /* Here on any return, including failures, except user/internal issues
1688 */
1689 {
1690 int pass = (d.options & STRICT) ?
1691 RESULT_STRICT(d.results) : RESULT_RELAXED(d.results);
1692
1693 if (!pass)
1694 ++errors;
1695
1696 if (d.options & LOG)
1697 {
1698 int j;
1699
1700 printf("%s: pngimage ", pass ? "PASS" : "FAIL");
1701
1702 for (j=1; j<option_end; ++j)
1703 if (j != ilog)
1704 printf("%s ", argv[j]);
1705
1706 printf("%s\n", d.filename);
1707 }
1708 }
1709
1710 display_clean(&d);
1711 }
1712
1713 /* Release allocated memory */
1714 display_destroy(&d);
1715
1716 return errors != 0;
1717 }
1718}
1719#else /* !READ_PNG */
1720int
1721main(void)
1722{
1723 fprintf(stderr, "pngimage: no support for png_read/write_image\n");
1724 return SKIP;
1725}
1726#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette