4 * Last changed in libpng 1.6.31 [July 27, 2017]
5 * Copyright (c) 2013-2017 John Cunningham Bowler
7 * This code is released under the libpng license.
8 * For conditions of distribution and use, see the disclaimer
11 * Test for the PNG 'simplified' APIs.
13 #define _ISOC90_SOURCE 1
14 #define MALLOC_CHECK_ 2/*glibc facility: turn on debugging*/
24 #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
28 /* Define the following to use this test against your installed libpng, rather
29 * than the one being built here:
31 #ifdef PNG_FREESTANDING_TESTS
34 # include "../../png.h"
37 /* 1.6.1 added support for the configure test harness, which uses 77 to indicate
38 * a skipped test, in earlier versions we need to succeed on a skipped test, so:
40 #if PNG_LIBPNG_VER >= 10601 && defined(HAVE_CONFIG_H)
46 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED /* Else nothing can be done */
47 #include "../tools/sRGB.h"
51 * These defines switch on alternate algorithms for format conversions to match
52 * the current libpng implementation; they are set to allow pngstest to pass
53 * even though libpng is producing answers that are not as correct as they
56 #define ALLOW_UNUSED_GPC 0
57 /* If true include unused static GPC functions and declare an external array
58 * of them to hide the fact that they are unused. This is for development
59 * use while testing the correct function to use to take into account libpng
60 * misbehavior, such as using a simple power law to correct sRGB to linear.
63 /* The following is to support direct compilation of this file as C++ */
65 # define voidcast(type, value) static_cast<type>(value)
66 # define aligncastconst(type, value) \
67 static_cast<type>(static_cast<const void*>(value))
69 # define voidcast(type, value) (value)
70 # define aligncastconst(type, value) ((const void*)(value))
71 #endif /* __cplusplus */
73 /* During parallel runs of pngstest each temporary file needs a unique name,
74 * this is used to permit uniqueness using a command line argument which can be
75 * up to 22 characters long.
77 static char tmpf
[23] = "TMP";
79 /* Generate random bytes. This uses a boring repeatable algorithm and it
80 * is implemented here so that it gives the same set of numbers on every
81 * architecture. It's a linear congruential generator (Knuth or Sedgewick
82 * "Algorithms") but it comes from the 'feedback taps' table in Horowitz and
83 * Hill, "The Art of Electronics".
86 make_random_bytes(png_uint_32
* seed
, void* pv
, size_t size
)
88 png_uint_32 u0
= seed
[0], u1
= seed
[1];
89 png_bytep bytes
= voidcast(png_bytep
, pv
);
91 /* There are thirty three bits, the next bit in the sequence is bit-33 XOR
92 * bit-20. The top 1 bit is in u1, the bottom 32 are in u0.
95 for (i
=0; i
<size
; ++i
)
97 /* First generate 8 new bits then shift them in at the end. */
98 png_uint_32 u
= ((u0
>> (20-8)) ^ ((u1
<< 7) | (u0
>> (32-7)))) & 0xff;
103 *bytes
++ = (png_byte
)u
;
110 static png_uint_32 color_seed
[2];
115 color_seed
[0] = 0x12345678U
;
116 color_seed
[1] = 0x9abcdefU
;
120 random_color(png_colorp color
)
122 make_random_bytes(color_seed
, color
, sizeof *color
);
125 /* Math support - neither Cygwin nor Visual Studio have C99 support and we need
126 * a predictable rounding function, so make one here:
129 closestinteger(double x
)
131 return floor(x
+ .5);
134 /* Cast support: remove GCC whines. */
138 d
= closestinteger(d
);
145 d
= closestinteger(d
);
146 return (png_uint_16
)d
;
149 /* sRGB support: use exact calculations rounded to the nearest int, see the
150 * fesetround() call in main(). sRGB_to_d optimizes the 8 to 16-bit conversion.
152 static double sRGB_to_d
[256];
153 static double g22_to_d
[256];
161 for (i
=1; i
<255; ++i
)
162 sRGB_to_d
[i
] = linear_from_sRGB(i
/255.);
166 for (i
=1; i
<255; ++i
)
167 g22_to_d
[i
] = pow(i
/255., 1/.45455);
172 sRGB(double linear
/*range 0.0 .. 1.0*/)
174 return u8d(255 * sRGB_from_linear(linear
));
178 isRGB(int fixed_linear
)
180 return sRGB(fixed_linear
/ 65535.);
185 unpremultiply(int component
, int alpha
)
187 if (alpha
<= component
)
188 return 255; /* Arbitrary, but consistent with the libpng code */
190 else if (alpha
>= 65535)
191 return isRGB(component
);
194 return sRGB((double)component
/ alpha
);
199 ilinear(int fixed_srgb
)
201 return u16d(65535 * sRGB_to_d
[fixed_srgb
]);
205 ilineara(int fixed_srgb
, int alpha
)
207 return u16d((257 * alpha
) * sRGB_to_d
[fixed_srgb
]);
211 ilinear_g22(int fixed_srgb
)
213 return u16d(65535 * g22_to_d
[fixed_srgb
]);
218 ilineara_g22(int fixed_srgb
, int alpha
)
220 return u16d((257 * alpha
) * g22_to_d
[fixed_srgb
]);
225 YfromRGBint(int ir
, int ig
, int ib
)
230 return YfromRGB(r
, g
, b
);
234 /* The error that results from using a 2.2 power law in place of the correct
235 * sRGB transform, given an 8-bit value which might be either sRGB or power-law.
238 power_law_error8(int value
)
240 if (value
> 0 && value
< 255)
242 double vd
= value
/ 255.;
244 pow(sRGB_to_d
[value
], 1/2.2) - sRGB_from_linear(pow(vd
, 2.2)));
246 /* Always allow an extra 1 here for rounding errors */
247 e
= 1+floor(255 * e
);
254 static int error_in_sRGB_roundtrip
= 56; /* by experiment */
256 power_law_error16(int value
)
258 if (value
> 0 && value
< 65535)
260 /* Round trip the value through an 8-bit representation but using
261 * non-matching to/from conversions.
263 double vd
= value
/ 65535.;
265 pow(sRGB_from_linear(vd
), 2.2) - linear_from_sRGB(pow(vd
, 1/2.2)));
267 /* Always allow an extra 1 here for rounding errors */
268 e
= error_in_sRGB_roundtrip
+floor(65535 * e
);
276 compare_8bit(int v1
, int v2
, int error_limit
, int multiple_algorithms
)
281 if (e
<= error_limit
)
284 if (!multiple_algorithms
)
287 ev1
= power_law_error8(v1
);
291 ev2
= power_law_error8(v2
);
299 compare_16bit(int v1
, int v2
, int error_limit
, int multiple_algorithms
)
304 if (e
<= error_limit
)
307 /* "multiple_algorithms" in this case means that a color-map has been
308 * involved somewhere, so we can deduce that the values were forced to 8-bit
309 * (like the via_linear case for 8-bit.)
311 if (!multiple_algorithms
)
314 ev1
= power_law_error16(v1
);
318 ev2
= power_law_error16(v2
);
326 #define USE_FILE 1 /* else memory */
327 #define USE_STDIO 2 /* else use file name */
328 #define STRICT 4 /* fail on warnings too */
330 #define KEEP_TMPFILES 16 /* else delete temporary files */
331 #define KEEP_GOING 32
332 #define ACCUMULATE 64
333 #define FAST_WRITE 128
334 #define sRGB_16BIT 256
335 #define NO_RESEED 512 /* do not reseed on each new file */
336 #define GBG_ERROR 1024 /* do not ignore the gamma+background_rgb_to_gray
340 print_opts(png_uint_32 opts
)
344 if (opts
& USE_STDIO
)
346 if (!(opts
& STRICT
))
347 printf(" --nostrict");
349 printf(" --verbose");
350 if (opts
& KEEP_TMPFILES
)
351 printf(" --preserve");
352 if (opts
& KEEP_GOING
)
353 printf(" --keep-going");
354 if (opts
& ACCUMULATE
)
355 printf(" --accumulate");
356 if (!(opts
& FAST_WRITE
)) /* --fast is currently the default */
358 if (opts
& sRGB_16BIT
)
359 printf(" --sRGB-16bit");
360 if (opts
& NO_RESEED
)
361 printf(" --noreseed");
362 #if PNG_LIBPNG_VER < 10700 /* else on by default */
363 if (opts
& GBG_ERROR
)
364 printf(" --fault-gbg-warning");
368 #define FORMAT_NO_CHANGE 0x80000000 /* additional flag */
370 /* A name table for all the formats - defines the format of the '+' arguments to
373 #define FORMAT_COUNT 64
374 #define FORMAT_MASK 0x3f
375 static const char * const format_names
[FORMAT_COUNT
] =
386 "color-mapped-sRGB-gray",
387 "color-mapped-sRGB-gray+alpha",
388 "color-mapped-sRGB-rgb",
389 "color-mapped-sRGB-rgb+alpha",
390 "color-mapped-linear-gray",
391 "color-mapped-linear-gray+alpha",
392 "color-mapped-linear-rgb",
393 "color-mapped-linear-rgb+alpha",
404 "color-mapped-sRGB-gray",
405 "color-mapped-sRGB-gray+alpha",
406 "color-mapped-sRGB-bgr",
407 "color-mapped-sRGB-bgr+alpha",
408 "color-mapped-linear-gray",
409 "color-mapped-linear-gray+alpha",
410 "color-mapped-linear-bgr",
411 "color-mapped-linear-bgr+alpha",
422 "color-mapped-sRGB-gray",
423 "color-mapped-alpha+sRGB-gray",
424 "color-mapped-sRGB-rgb",
425 "color-mapped-alpha+sRGB-rgb",
426 "color-mapped-linear-gray",
427 "color-mapped-alpha+linear-gray",
428 "color-mapped-linear-rgb",
429 "color-mapped-alpha+linear-rgb",
440 "color-mapped-sRGB-gray",
441 "color-mapped-alpha+sRGB-gray",
442 "color-mapped-sRGB-bgr",
443 "color-mapped-alpha+sRGB-bgr",
444 "color-mapped-linear-gray",
445 "color-mapped-alpha+linear-gray",
446 "color-mapped-linear-bgr",
447 "color-mapped-alpha+linear-bgr",
450 /* Decode an argument to a format number. */
452 formatof(const char *arg
)
455 unsigned long format
= strtoul(arg
, &ep
, 0);
457 if (ep
> arg
&& *ep
== 0 && format
< FORMAT_COUNT
)
458 return (png_uint_32
)format
;
460 else for (format
=0; format
< FORMAT_COUNT
; ++format
)
462 if (strcmp(format_names
[format
], arg
) == 0)
463 return (png_uint_32
)format
;
466 fprintf(stderr
, "pngstest: format name '%s' invalid\n", arg
);
470 /* Bitset/test functions for formats */
471 #define FORMAT_SET_COUNT (FORMAT_COUNT / 32)
474 png_uint_32 bits
[FORMAT_SET_COUNT
];
478 static void format_init(format_list
*pf
)
481 for (i
=0; i
<FORMAT_SET_COUNT
; ++i
)
482 pf
->bits
[i
] = 0; /* All off */
485 #if 0 /* currently unused */
486 static void format_clear(format_list
*pf
)
489 for (i
=0; i
<FORMAT_SET_COUNT
; ++i
)
494 static int format_is_initial(format_list
*pf
)
497 for (i
=0; i
<FORMAT_SET_COUNT
; ++i
)
498 if (pf
->bits
[i
] != 0)
504 static int format_set(format_list
*pf
, png_uint_32 format
)
506 if (format
< FORMAT_COUNT
)
507 return pf
->bits
[format
>> 5] |= ((png_uint_32
)1) << (format
& 31);
512 #if 0 /* currently unused */
513 static int format_unset(format_list
*pf
, png_uint_32 format
)
515 if (format
< FORMAT_COUNT
)
516 return pf
->bits
[format
>> 5] &= ~((png_uint_32
)1) << (format
& 31);
522 static int format_isset(format_list
*pf
, png_uint_32 format
)
524 return format
< FORMAT_COUNT
&&
525 (pf
->bits
[format
>> 5] & (((png_uint_32
)1) << (format
& 31))) != 0;
528 static void format_default(format_list
*pf
, int redundant
)
534 /* set everything, including flags that are pointless */
535 for (i
=0; i
<FORMAT_SET_COUNT
; ++i
)
536 pf
->bits
[i
] = ~(png_uint_32
)0;
543 for (f
=0; f
<FORMAT_COUNT
; ++f
)
545 /* Eliminate redundant and unsupported settings. */
546 # ifdef PNG_FORMAT_BGR_SUPPORTED
547 /* BGR is meaningless if no color: */
548 if ((f
& PNG_FORMAT_FLAG_COLOR
) == 0 &&
549 (f
& PNG_FORMAT_FLAG_BGR
) != 0)
551 if ((f
& 0x10U
/*HACK: fixed value*/) != 0)
555 /* AFIRST is meaningless if no alpha: */
556 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
557 if ((f
& PNG_FORMAT_FLAG_ALPHA
) == 0 &&
558 (f
& PNG_FORMAT_FLAG_AFIRST
) != 0)
560 if ((f
& 0x20U
/*HACK: fixed value*/) != 0)
569 /* THE Image STRUCTURE */
570 /* The super-class of a png_image, contains the decoded image plus the input
571 * data necessary to re-read the file with a different format.
577 const char *file_name
;
580 png_voidp input_memory
;
581 size_t input_memory_size
;
586 char tmpfile_name
[32];
587 png_uint_16 colormap
[256*4];
591 /* Initializer: also sets the permitted error limit for 16-bit operations. */
593 newimage(Image
*image
)
595 memset(image
, 0, sizeof *image
);
598 /* Reset the image to be read again - only needs to rewind the FILE* at present.
601 resetimage(Image
*image
)
603 if (image
->input_file
!= NULL
)
604 rewind(image
->input_file
);
607 /* Free the image buffer; the buffer is re-used on a re-read, this is just for
611 freebuffer(Image
*image
)
613 if (image
->buffer
) free(image
->buffer
);
614 image
->buffer
= NULL
;
616 image
->allocsize
= 0;
619 /* Delete function; cleans out all the allocated data and the temporary file in
623 freeimage(Image
*image
)
626 png_image_free(&image
->image
);
628 if (image
->input_file
!= NULL
)
630 fclose(image
->input_file
);
631 image
->input_file
= NULL
;
634 if (image
->input_memory
!= NULL
)
636 free(image
->input_memory
);
637 image
->input_memory
= NULL
;
638 image
->input_memory_size
= 0;
641 if (image
->tmpfile_name
[0] != 0 && (image
->opts
& KEEP_TMPFILES
) == 0)
643 (void)remove(image
->tmpfile_name
);
644 image
->tmpfile_name
[0] = 0;
648 /* This is actually a re-initializer; allows an image structure to be re-used by
649 * freeing everything that relates to an old image.
651 static void initimage(Image
*image
, png_uint_32 opts
, const char *file_name
,
655 memset(&image
->image
, 0, sizeof image
->image
);
657 image
->file_name
= file_name
;
658 image
->stride_extra
= stride_extra
;
661 /* Make sure the image buffer is big enough; allows re-use of the buffer if the
664 #define BUFFER_INIT8 73
666 allocbuffer(Image
*image
)
668 size_t size
= PNG_IMAGE_BUFFER_SIZE(image
->image
, image
->stride
);
670 if (size
+32 > image
->bufsize
)
673 image
->buffer
= voidcast(png_bytep
, malloc(size
+32));
674 if (image
->buffer
== NULL
)
678 "simpletest: out of memory allocating %lu(+32) byte buffer\n",
679 (unsigned long)size
);
682 image
->bufsize
= size
+32;
685 memset(image
->buffer
, 95, image
->bufsize
);
686 memset(image
->buffer
+16, BUFFER_INIT8
, size
);
687 image
->allocsize
= size
;
690 /* Make sure 16 bytes match the given byte. */
692 check16(png_const_bytep bp
, int b
)
697 if (*bp
!= b
) return 1;
703 /* Check for overwrite in the image buffer. */
705 checkbuffer(Image
*image
, const char *arg
)
707 if (check16(image
->buffer
, 95))
710 fprintf(stderr
, "%s: overwrite at start of image buffer\n", arg
);
714 if (check16(image
->buffer
+16+image
->allocsize
, 95))
717 fprintf(stderr
, "%s: overwrite at end of image buffer\n", arg
);
723 /* Log a terminal error, also frees the libpng part of the image if necessary.
726 logerror(Image
*image
, const char *a1
, const char *a2
, const char *a3
)
729 if (image
->image
.warning_or_error
)
730 fprintf(stderr
, "%s%s%s: %s\n", a1
, a2
, a3
, image
->image
.message
);
733 fprintf(stderr
, "%s%s%s\n", a1
, a2
, a3
);
735 if (image
->image
.opaque
!= NULL
)
737 fprintf(stderr
, "%s: image opaque pointer non-NULL on error\n",
739 png_image_free(&image
->image
);
745 /* Log an error and close a file (just a utility to do both things in one
749 logclose(Image
*image
, FILE *f
, const char *name
, const char *operation
)
754 return logerror(image
, name
, operation
, strerror(e
));
757 /* Make sure the png_image has been freed - validates that libpng is doing what
758 * the spec says and freeing the image.
761 checkopaque(Image
*image
)
763 if (image
->image
.opaque
!= NULL
)
765 png_image_free(&image
->image
);
766 return logerror(image
, image
->file_name
, ": opaque not NULL", "");
769 /* Separate out the gamma+background_rgb_to_gray warning because it may
770 * produce opaque component errors:
772 else if (image
->image
.warning_or_error
!= 0 &&
773 (strcmp(image
->image
.message
,
774 "libpng does not support gamma+background+rgb_to_gray") == 0 ?
775 (image
->opts
& GBG_ERROR
) != 0 : (image
->opts
& STRICT
) != 0))
776 return logerror(image
, image
->file_name
, (image
->opts
& GBG_ERROR
) != 0 ?
777 " --fault-gbg-warning" : " --strict", "");
783 /* IMAGE COMPARISON/CHECKING */
784 /* Compare the pixels of two images, which should be the same but aren't. The
785 * images must have been checked for a size match.
789 /* The components, for grayscale images the gray value is in 'g' and if alpha
790 * is not present 'a' is set to 255 or 65535 according to format.
797 /* The background as the original sRGB 8-bit value converted to the final
798 * integer format and as a double precision linear value in the range 0..1
799 * for with partially transparent pixels.
802 double dr
, dg
, db
; /* linear r,g,b scaled to 0..1 */
805 /* Basic image formats; control the data but not the layout thereof. */
806 #define BASE_FORMATS\
807 (PNG_FORMAT_FLAG_ALPHA|PNG_FORMAT_FLAG_COLOR|PNG_FORMAT_FLAG_LINEAR)
809 /* Read a Pixel from a buffer. The code below stores the correct routine for
810 * the format in a function pointer, these are the routines:
813 gp_g8(Pixel
*p
, png_const_voidp pb
)
815 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
817 p
->r
= p
->g
= p
->b
= pp
[0];
822 gp_ga8(Pixel
*p
, png_const_voidp pb
)
824 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
826 p
->r
= p
->g
= p
->b
= pp
[0];
830 #ifdef PNG_FORMAT_AFIRST_SUPPORTED
832 gp_ag8(Pixel
*p
, png_const_voidp pb
)
834 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
836 p
->r
= p
->g
= p
->b
= pp
[1];
842 gp_rgb8(Pixel
*p
, png_const_voidp pb
)
844 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
852 #ifdef PNG_FORMAT_BGR_SUPPORTED
854 gp_bgr8(Pixel
*p
, png_const_voidp pb
)
856 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
866 gp_rgba8(Pixel
*p
, png_const_voidp pb
)
868 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
876 #ifdef PNG_FORMAT_BGR_SUPPORTED
878 gp_bgra8(Pixel
*p
, png_const_voidp pb
)
880 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
889 #ifdef PNG_FORMAT_AFIRST_SUPPORTED
891 gp_argb8(Pixel
*p
, png_const_voidp pb
)
893 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
902 #if defined(PNG_FORMAT_AFIRST_SUPPORTED) && defined(PNG_FORMAT_BGR_SUPPORTED)
904 gp_abgr8(Pixel
*p
, png_const_voidp pb
)
906 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
916 gp_g16(Pixel
*p
, png_const_voidp pb
)
918 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
920 p
->r
= p
->g
= p
->b
= pp
[0];
925 gp_ga16(Pixel
*p
, png_const_voidp pb
)
927 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
929 p
->r
= p
->g
= p
->b
= pp
[0];
933 #ifdef PNG_FORMAT_AFIRST_SUPPORTED
935 gp_ag16(Pixel
*p
, png_const_voidp pb
)
937 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
939 p
->r
= p
->g
= p
->b
= pp
[1];
945 gp_rgb16(Pixel
*p
, png_const_voidp pb
)
947 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
955 #ifdef PNG_FORMAT_BGR_SUPPORTED
957 gp_bgr16(Pixel
*p
, png_const_voidp pb
)
959 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
969 gp_rgba16(Pixel
*p
, png_const_voidp pb
)
971 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
979 #ifdef PNG_FORMAT_BGR_SUPPORTED
981 gp_bgra16(Pixel
*p
, png_const_voidp pb
)
983 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
992 #ifdef PNG_FORMAT_AFIRST_SUPPORTED
994 gp_argb16(Pixel
*p
, png_const_voidp pb
)
996 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
1005 #if defined(PNG_FORMAT_AFIRST_SUPPORTED) && defined(PNG_FORMAT_BGR_SUPPORTED)
1007 gp_abgr16(Pixel
*p
, png_const_voidp pb
)
1009 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
1018 /* Given a format, return the correct one of the above functions. */
1020 get_pixel(png_uint_32 format
))(Pixel
*p
, png_const_voidp pb
)
1022 /* The color-map flag is irrelevant here - the caller of the function
1023 * returned must either pass the buffer or, for a color-mapped image, the
1024 * correct entry in the color-map.
1026 if (format
& PNG_FORMAT_FLAG_LINEAR
)
1028 if (format
& PNG_FORMAT_FLAG_COLOR
)
1030 # ifdef PNG_FORMAT_BGR_SUPPORTED
1031 if (format
& PNG_FORMAT_FLAG_BGR
)
1033 if (format
& PNG_FORMAT_FLAG_ALPHA
)
1035 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1036 if (format
& PNG_FORMAT_FLAG_AFIRST
)
1051 if (format
& PNG_FORMAT_FLAG_ALPHA
)
1053 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1054 if (format
& PNG_FORMAT_FLAG_AFIRST
)
1069 if (format
& PNG_FORMAT_FLAG_ALPHA
)
1071 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1072 if (format
& PNG_FORMAT_FLAG_AFIRST
)
1087 if (format
& PNG_FORMAT_FLAG_COLOR
)
1089 # ifdef PNG_FORMAT_BGR_SUPPORTED
1090 if (format
& PNG_FORMAT_FLAG_BGR
)
1092 if (format
& PNG_FORMAT_FLAG_ALPHA
)
1094 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1095 if (format
& PNG_FORMAT_FLAG_AFIRST
)
1110 if (format
& PNG_FORMAT_FLAG_ALPHA
)
1112 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1113 if (format
& PNG_FORMAT_FLAG_AFIRST
)
1128 if (format
& PNG_FORMAT_FLAG_ALPHA
)
1130 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1131 if (format
& PNG_FORMAT_FLAG_AFIRST
)
1145 /* Conversion between pixel formats. The code above effectively eliminates the
1146 * component ordering changes leaving three basic changes:
1148 * 1) Remove an alpha channel by pre-multiplication or compositing on a
1149 * background color. (Adding an alpha channel is a no-op.)
1151 * 2) Remove color by mapping to grayscale. (Grayscale to color is a no-op.)
1153 * 3) Convert between 8-bit and 16-bit components. (Both directtions are
1156 * This gives the following base format conversion matrix:
1158 * OUT: ----- 8-bit ----- ----- 16-bit -----
1159 * IN G GA RGB RGBA G GA RGB RGBA
1160 * 8 G . . . . lin lin lin lin
1161 * 8 GA bckg . bckc . pre' pre pre' pre
1162 * 8 RGB g8 g8 . . glin glin lin lin
1163 * 8 RGBA g8b g8 bckc . gpr' gpre pre' pre
1164 * 16 G sRGB sRGB sRGB sRGB . . . .
1165 * 16 GA b16g unpg b16c unpc A . A .
1166 * 16 RGB sG sG sRGB sRGB g16 g16 . .
1167 * 16 RGBA gb16 sGp cb16 sCp g16 g16' A .
1170 * bckg: composite on gray background
1171 * bckc: composite on color background
1172 * g8: convert sRGB components to sRGB grayscale
1173 * g8b: convert sRGB components to grayscale and composite on gray background
1176 * lin: make sRGB components linear, alpha := 65535
1177 * pre: make sRGB components linear and premultiply by alpha (scale alpha)
1178 * pre': as 'pre' but alpha := 65535
1179 * glin: make sRGB components linear, convert to grayscale, alpha := 65535
1180 * gpre: make sRGB components grayscale and linear and premultiply by alpha
1181 * gpr': as 'gpre' but alpha := 65535
1184 * sRGB: convert linear components to sRGB, alpha := 255
1185 * unpg: unpremultiply gray component and convert to sRGB (scale alpha)
1186 * unpc: unpremultiply color components and convert to sRGB (scale alpha)
1187 * b16g: composite linear onto gray background and convert the result to sRGB
1188 * b16c: composite linear onto color background and convert the result to sRGB
1189 * sG: convert linear RGB to sRGB grayscale
1190 * sGp: unpremultiply RGB then convert to sRGB grayscale
1191 * sCp: unpremultiply RGB then convert to sRGB
1192 * gb16: composite linear onto background and convert to sRGB grayscale
1193 * (order doesn't matter, the composite and grayscale operations permute)
1194 * cb16: composite linear onto background and convert to sRGB
1197 * A: set alpha to 65535
1198 * g16: convert linear RGB to linear grayscale (alpha := 65535)
1199 * g16': as 'g16' but alpha is unchanged
1203 gpc_noop(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1212 #if ALLOW_UNUSED_GPC
1214 gpc_nop8(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1218 out
->r
= out
->g
= out
->b
= 255;
1231 #if ALLOW_UNUSED_GPC
1233 gpc_nop6(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1237 out
->r
= out
->g
= out
->b
= 65535;
1250 /* 8-bit to 8-bit conversions */
1251 /* bckg: composite on gray background */
1253 gpc_bckg(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1256 out
->r
= out
->g
= out
->b
= back
->ig
;
1258 else if (in
->a
>= 255)
1259 out
->r
= out
->g
= out
->b
= in
->g
;
1263 double a
= in
->a
/ 255.;
1265 out
->r
= out
->g
= out
->b
= sRGB(sRGB_to_d
[in
->g
] * a
+ back
->dg
* (1-a
));
1271 /* bckc: composite on color background */
1273 gpc_bckc(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1282 else if (in
->a
>= 255)
1291 double a
= in
->a
/ 255.;
1293 out
->r
= sRGB(sRGB_to_d
[in
->r
] * a
+ back
->dr
* (1-a
));
1294 out
->g
= sRGB(sRGB_to_d
[in
->g
] * a
+ back
->dg
* (1-a
));
1295 out
->b
= sRGB(sRGB_to_d
[in
->b
] * a
+ back
->db
* (1-a
));
1301 /* g8: convert sRGB components to sRGB grayscale */
1303 gpc_g8(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1307 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1308 out
->r
= out
->g
= out
->b
= in
->g
;
1311 out
->r
= out
->g
= out
->b
=
1312 sRGB(YfromRGB(sRGB_to_d
[in
->r
], sRGB_to_d
[in
->g
], sRGB_to_d
[in
->b
]));
1317 /* g8b: convert sRGB components to grayscale and composite on gray background */
1319 gpc_g8b(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1322 out
->r
= out
->g
= out
->b
= back
->ig
;
1324 else if (in
->a
>= 255)
1326 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1327 out
->r
= out
->g
= out
->b
= in
->g
;
1330 out
->r
= out
->g
= out
->b
= sRGB(YfromRGB(
1331 sRGB_to_d
[in
->r
], sRGB_to_d
[in
->g
], sRGB_to_d
[in
->b
]));
1336 double a
= in
->a
/255.;
1338 out
->r
= out
->g
= out
->b
= sRGB(a
* YfromRGB(sRGB_to_d
[in
->r
],
1339 sRGB_to_d
[in
->g
], sRGB_to_d
[in
->b
]) + back
->dg
* (1-a
));
1345 /* 8-bit to 16-bit conversions */
1346 /* lin: make sRGB components linear, alpha := 65535 */
1348 gpc_lin(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1352 out
->r
= ilinear(in
->r
);
1362 out
->b
= ilinear(in
->b
);
1367 out
->g
= ilinear(in
->g
);
1372 else if (in
->b
== in
->g
)
1376 out
->b
= ilinear(in
->b
);
1382 /* pre: make sRGB components linear and premultiply by alpha (scale alpha) */
1384 gpc_pre(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1388 out
->r
= ilineara(in
->r
, in
->a
);
1398 out
->b
= ilineara(in
->b
, in
->a
);
1403 out
->g
= ilineara(in
->g
, in
->a
);
1408 else if (in
->b
== in
->g
)
1412 out
->b
= ilineara(in
->b
, in
->a
);
1415 out
->a
= in
->a
* 257;
1418 /* pre': as 'pre' but alpha := 65535 */
1420 gpc_preq(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1424 out
->r
= ilineara(in
->r
, in
->a
);
1434 out
->b
= ilineara(in
->b
, in
->a
);
1439 out
->g
= ilineara(in
->g
, in
->a
);
1444 else if (in
->b
== in
->g
)
1448 out
->b
= ilineara(in
->b
, in
->a
);
1454 /* glin: make sRGB components linear, convert to grayscale, alpha := 65535 */
1456 gpc_glin(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1460 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1461 out
->r
= out
->g
= out
->b
= ilinear(in
->g
);
1464 out
->r
= out
->g
= out
->b
= u16d(65535 *
1465 YfromRGB(sRGB_to_d
[in
->r
], sRGB_to_d
[in
->g
], sRGB_to_d
[in
->b
]));
1470 /* gpre: make sRGB components grayscale and linear and premultiply by alpha */
1472 gpc_gpre(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1476 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1477 out
->r
= out
->g
= out
->b
= ilineara(in
->g
, in
->a
);
1480 out
->r
= out
->g
= out
->b
= u16d(in
->a
* 257 *
1481 YfromRGB(sRGB_to_d
[in
->r
], sRGB_to_d
[in
->g
], sRGB_to_d
[in
->b
]));
1483 out
->a
= 257 * in
->a
;
1486 /* gpr': as 'gpre' but alpha := 65535 */
1488 gpc_gprq(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1492 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1493 out
->r
= out
->g
= out
->b
= ilineara(in
->g
, in
->a
);
1496 out
->r
= out
->g
= out
->b
= u16d(in
->a
* 257 *
1497 YfromRGB(sRGB_to_d
[in
->r
], sRGB_to_d
[in
->g
], sRGB_to_d
[in
->b
]));
1502 /* 8-bit to 16-bit conversions for gAMA 45455 encoded values */
1503 /* Lin: make gAMA 45455 components linear, alpha := 65535 */
1505 gpc_Lin(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1509 out
->r
= ilinear_g22(in
->r
);
1519 out
->b
= ilinear_g22(in
->b
);
1524 out
->g
= ilinear_g22(in
->g
);
1529 else if (in
->b
== in
->g
)
1533 out
->b
= ilinear_g22(in
->b
);
1539 #if ALLOW_UNUSED_GPC
1540 /* Pre: make gAMA 45455 components linear and premultiply by alpha (scale alpha)
1543 gpc_Pre(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1547 out
->r
= ilineara_g22(in
->r
, in
->a
);
1557 out
->b
= ilineara_g22(in
->b
, in
->a
);
1562 out
->g
= ilineara_g22(in
->g
, in
->a
);
1567 else if (in
->b
== in
->g
)
1571 out
->b
= ilineara_g22(in
->b
, in
->a
);
1574 out
->a
= in
->a
* 257;
1578 #if ALLOW_UNUSED_GPC
1579 /* Pre': as 'Pre' but alpha := 65535 */
1581 gpc_Preq(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1585 out
->r
= ilineara_g22(in
->r
, in
->a
);
1595 out
->b
= ilineara_g22(in
->b
, in
->a
);
1600 out
->g
= ilineara_g22(in
->g
, in
->a
);
1605 else if (in
->b
== in
->g
)
1609 out
->b
= ilineara_g22(in
->b
, in
->a
);
1616 #if ALLOW_UNUSED_GPC
1617 /* Glin: make gAMA 45455 components linear, convert to grayscale, alpha := 65535
1620 gpc_Glin(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1624 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1625 out
->r
= out
->g
= out
->b
= ilinear_g22(in
->g
);
1628 out
->r
= out
->g
= out
->b
= u16d(65535 *
1629 YfromRGB(g22_to_d
[in
->r
], g22_to_d
[in
->g
], g22_to_d
[in
->b
]));
1635 #if ALLOW_UNUSED_GPC
1636 /* Gpre: make gAMA 45455 components grayscale and linear and premultiply by
1640 gpc_Gpre(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1644 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1645 out
->r
= out
->g
= out
->b
= ilineara_g22(in
->g
, in
->a
);
1648 out
->r
= out
->g
= out
->b
= u16d(in
->a
* 257 *
1649 YfromRGB(g22_to_d
[in
->r
], g22_to_d
[in
->g
], g22_to_d
[in
->b
]));
1651 out
->a
= 257 * in
->a
;
1655 #if ALLOW_UNUSED_GPC
1656 /* Gpr': as 'Gpre' but alpha := 65535 */
1658 gpc_Gprq(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1662 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1663 out
->r
= out
->g
= out
->b
= ilineara_g22(in
->g
, in
->a
);
1666 out
->r
= out
->g
= out
->b
= u16d(in
->a
* 257 *
1667 YfromRGB(g22_to_d
[in
->r
], g22_to_d
[in
->g
], g22_to_d
[in
->b
]));
1673 /* 16-bit to 8-bit conversions */
1674 /* sRGB: convert linear components to sRGB, alpha := 255 */
1676 gpc_sRGB(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1680 out
->r
= isRGB(in
->r
);
1690 out
->b
= isRGB(in
->b
);
1695 out
->g
= isRGB(in
->g
);
1700 else if (in
->b
== in
->g
)
1704 out
->b
= isRGB(in
->b
);
1710 /* unpg: unpremultiply gray component and convert to sRGB (scale alpha) */
1712 gpc_unpg(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1718 out
->r
= out
->g
= out
->b
= 255;
1724 out
->r
= out
->g
= out
->b
= sRGB((double)in
->g
/ in
->a
);
1725 out
->a
= u8d(in
->a
/ 257.);
1729 /* unpc: unpremultiply color components and convert to sRGB (scale alpha) */
1731 gpc_unpc(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1737 out
->r
= out
->g
= out
->b
= 255;
1743 out
->r
= sRGB((double)in
->r
/ in
->a
);
1744 out
->g
= sRGB((double)in
->g
/ in
->a
);
1745 out
->b
= sRGB((double)in
->b
/ in
->a
);
1746 out
->a
= u8d(in
->a
/ 257.);
1750 /* b16g: composite linear onto gray background and convert the result to sRGB */
1752 gpc_b16g(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1755 out
->r
= out
->g
= out
->b
= back
->ig
;
1759 double a
= in
->a
/65535.;
1763 out
->r
= out
->g
= out
->b
= sRGB(in
->g
* a
+ back
->dg
* a1
);
1769 /* b16c: composite linear onto color background and convert the result to sRGB*/
1771 gpc_b16c(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1782 double a
= in
->a
/65535.;
1786 out
->r
= sRGB(in
->r
* a
+ back
->dr
* a1
);
1787 out
->g
= sRGB(in
->g
* a
+ back
->dg
* a1
);
1788 out
->b
= sRGB(in
->b
* a
+ back
->db
* a1
);
1794 /* sG: convert linear RGB to sRGB grayscale */
1796 gpc_sG(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1800 out
->r
= out
->g
= out
->b
= sRGB(YfromRGBint(in
->r
, in
->g
, in
->b
)/65535);
1804 /* sGp: unpremultiply RGB then convert to sRGB grayscale */
1806 gpc_sGp(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1812 out
->r
= out
->g
= out
->b
= 255;
1818 out
->r
= out
->g
= out
->b
= sRGB(YfromRGBint(in
->r
, in
->g
, in
->b
)/in
->a
);
1819 out
->a
= u8d(in
->a
/ 257.);
1823 /* sCp: unpremultiply RGB then convert to sRGB */
1825 gpc_sCp(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1831 out
->r
= out
->g
= out
->b
= 255;
1837 out
->r
= sRGB((double)in
->r
/ in
->a
);
1838 out
->g
= sRGB((double)in
->g
/ in
->a
);
1839 out
->b
= sRGB((double)in
->b
/ in
->a
);
1840 out
->a
= u8d(in
->a
/ 257.);
1844 /* gb16: composite linear onto background and convert to sRGB grayscale */
1845 /* (order doesn't matter, the composite and grayscale operations permute) */
1847 gpc_gb16(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1850 out
->r
= out
->g
= out
->b
= back
->ig
;
1852 else if (in
->a
>= 65535)
1853 out
->r
= out
->g
= out
->b
= isRGB(in
->g
);
1857 double a
= in
->a
/ 65535.;
1861 out
->r
= out
->g
= out
->b
= sRGB(in
->g
* a
+ back
->dg
* a1
);
1867 /* cb16: composite linear onto background and convert to sRGB */
1869 gpc_cb16(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1878 else if (in
->a
>= 65535)
1880 out
->r
= isRGB(in
->r
);
1881 out
->g
= isRGB(in
->g
);
1882 out
->b
= isRGB(in
->b
);
1887 double a
= in
->a
/ 65535.;
1891 out
->r
= sRGB(in
->r
* a
+ back
->dr
* a1
);
1892 out
->g
= sRGB(in
->g
* a
+ back
->dg
* a1
);
1893 out
->b
= sRGB(in
->b
* a
+ back
->db
* a1
);
1899 /* 16-bit to 16-bit conversions */
1900 /* A: set alpha to 65535 */
1902 gpc_A(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1911 /* g16: convert linear RGB to linear grayscale (alpha := 65535) */
1913 gpc_g16(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1916 out
->r
= out
->g
= out
->b
= u16d(YfromRGBint(in
->r
, in
->g
, in
->b
));
1920 /* g16': as 'g16' but alpha is unchanged */
1922 gpc_g16q(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1925 out
->r
= out
->g
= out
->b
= u16d(YfromRGBint(in
->r
, in
->g
, in
->b
));
1929 #if ALLOW_UNUSED_GPC
1930 /* Unused functions (to hide them from GCC unused function warnings) */
1931 void (* const gpc_unused
[])
1932 (Pixel
*out
, const Pixel
*in
, const Background
*back
) =
1934 gpc_Pre
, gpc_Preq
, gpc_Glin
, gpc_Gpre
, gpc_Gprq
, gpc_nop8
, gpc_nop6
1938 /* OUT: ----- 8-bit ----- ----- 16-bit -----
1939 * IN G GA RGB RGBA G GA RGB RGBA
1940 * 8 G . . . . lin lin lin lin
1941 * 8 GA bckg . bckc . pre' pre pre' pre
1942 * 8 RGB g8 g8 . . glin glin lin lin
1943 * 8 RGBA g8b g8 bckc . gpr' gpre pre' pre
1944 * 16 G sRGB sRGB sRGB sRGB . . . .
1945 * 16 GA b16g unpg b16c unpc A . A .
1946 * 16 RGB sG sG sRGB sRGB g16 g16 . .
1947 * 16 RGBA gb16 sGp cb16 sCp g16 g16' A .
1949 * The matrix is held in an array indexed thus:
1951 * gpc_fn[out_format & BASE_FORMATS][in_format & BASE_FORMATS];
1953 /* This will produce a compile time error if the FORMAT_FLAG values don't
1954 * match the above matrix!
1956 #if PNG_FORMAT_FLAG_ALPHA == 1 && PNG_FORMAT_FLAG_COLOR == 2 &&\
1957 PNG_FORMAT_FLAG_LINEAR == 4
1958 static void (* const gpc_fn
[8/*in*/][8/*out*/])
1959 (Pixel
*out
, const Pixel
*in
, const Background
*back
) =
1961 /*out: G-8 GA-8 RGB-8 RGBA-8 G-16 GA-16 RGB-16 RGBA-16 */
1962 {gpc_noop
,gpc_noop
,gpc_noop
,gpc_noop
, gpc_Lin
, gpc_Lin
, gpc_Lin
, gpc_Lin
},
1963 {gpc_bckg
,gpc_noop
,gpc_bckc
,gpc_noop
, gpc_preq
,gpc_pre
, gpc_preq
,gpc_pre
},
1964 {gpc_g8
, gpc_g8
, gpc_noop
,gpc_noop
, gpc_glin
,gpc_glin
,gpc_lin
, gpc_lin
},
1965 {gpc_g8b
, gpc_g8
, gpc_bckc
,gpc_noop
, gpc_gprq
,gpc_gpre
,gpc_preq
,gpc_pre
},
1966 {gpc_sRGB
,gpc_sRGB
,gpc_sRGB
,gpc_sRGB
, gpc_noop
,gpc_noop
,gpc_noop
,gpc_noop
},
1967 {gpc_b16g
,gpc_unpg
,gpc_b16c
,gpc_unpc
, gpc_A
, gpc_noop
,gpc_A
, gpc_noop
},
1968 {gpc_sG
, gpc_sG
, gpc_sRGB
,gpc_sRGB
, gpc_g16
, gpc_g16
, gpc_noop
,gpc_noop
},
1969 {gpc_gb16
,gpc_sGp
, gpc_cb16
,gpc_sCp
, gpc_g16
, gpc_g16q
,gpc_A
, gpc_noop
}
1972 /* The array is repeated for the cases where both the input and output are color
1973 * mapped because then different algorithms are used.
1975 static void (* const gpc_fn_colormapped
[8/*in*/][8/*out*/])
1976 (Pixel
*out
, const Pixel
*in
, const Background
*back
) =
1978 /*out: G-8 GA-8 RGB-8 RGBA-8 G-16 GA-16 RGB-16 RGBA-16 */
1979 {gpc_noop
,gpc_noop
,gpc_noop
,gpc_noop
, gpc_lin
, gpc_lin
, gpc_lin
, gpc_lin
},
1980 {gpc_bckg
,gpc_noop
,gpc_bckc
,gpc_noop
, gpc_preq
,gpc_pre
, gpc_preq
,gpc_pre
},
1981 {gpc_g8
, gpc_g8
, gpc_noop
,gpc_noop
, gpc_glin
,gpc_glin
,gpc_lin
, gpc_lin
},
1982 {gpc_g8b
, gpc_g8
, gpc_bckc
,gpc_noop
, gpc_gprq
,gpc_gpre
,gpc_preq
,gpc_pre
},
1983 {gpc_sRGB
,gpc_sRGB
,gpc_sRGB
,gpc_sRGB
, gpc_noop
,gpc_noop
,gpc_noop
,gpc_noop
},
1984 {gpc_b16g
,gpc_unpg
,gpc_b16c
,gpc_unpc
, gpc_A
, gpc_noop
,gpc_A
, gpc_noop
},
1985 {gpc_sG
, gpc_sG
, gpc_sRGB
,gpc_sRGB
, gpc_g16
, gpc_g16
, gpc_noop
,gpc_noop
},
1986 {gpc_gb16
,gpc_sGp
, gpc_cb16
,gpc_sCp
, gpc_g16
, gpc_g16q
,gpc_A
, gpc_noop
}
1989 /* The error arrays record the error in the same matrix; 64 entries, however
1990 * the different algorithms used in libpng for colormap and direct conversions
1991 * mean that four separate matrices are used (for each combination of
1992 * colormapped and direct.)
1994 * In some cases the conversion between sRGB formats goes via a linear
1995 * intermediate; an sRGB to linear conversion (as above) is followed by a simple
1996 * linear to sRGB step with no other conversions. This is done by a separate
1997 * error array from an arbitrary 'in' format to one of the four basic outputs
1998 * (since final output is always sRGB not colormapped).
2000 * These arrays may be modified if the --accumulate flag is set during the run;
2001 * then instead of logging errors they are simply added in.
2003 * The three entries are currently for transparent, partially transparent and
2004 * opaque input pixel values. Notice that alpha should be exact in each case.
2006 * Errors in alpha should only occur when converting from a direct format
2007 * to a colormapped format, when alpha is effectively smashed (so large
2008 * errors can occur.) There should be no error in the '0' and 'opaque'
2009 * values. The fourth entry in the array is used for the alpha error (and it
2010 * should always be zero for the 'via linear' case since this is never color
2013 * Mapping to a colormap smashes the colors, it is necessary to have separate
2014 * values for these cases because they are much larger; it is very much
2015 * impossible to obtain a reasonable result, these are held in
2016 * gpc_error_to_colormap.
2018 #if PNG_FORMAT_FLAG_COLORMAP == 8 /* extra check also required */
2019 # include "pngstest-errors.h" /* machine generated */
2020 #endif /* COLORMAP flag check */
2021 #endif /* flag checks */
2025 /* Basic pixel information: */
2026 Image
* in_image
; /* Input image */
2027 const Image
* out_image
; /* Output image */
2029 /* 'background' is the value passed to the gpc_ routines, it may be NULL if
2030 * it should not be used (*this* program has an error if it crashes as a
2033 Background background_color
;
2034 const Background
* background
;
2036 /* Precalculated values: */
2037 int in_opaque
; /* Value of input alpha that is opaque */
2038 int is_palette
; /* Sample values come from the palette */
2039 int accumulate
; /* Accumulate component errors (don't log) */
2040 int output_8bit
; /* Output is 8-bit (else 16-bit) */
2042 void (*in_gp
)(Pixel
*, png_const_voidp
);
2043 void (*out_gp
)(Pixel
*, png_const_voidp
);
2045 void (*transform
)(Pixel
*out
, const Pixel
*in
, const Background
*back
);
2046 /* A function to perform the required transform */
2048 void (*from_linear
)(Pixel
*out
, const Pixel
*in
, const Background
*back
);
2049 /* For 'via_linear' transforms the final, from linear, step, else NULL */
2051 png_uint_16 error
[4];
2052 /* Three error values for transparent, partially transparent and opaque
2053 * input pixels (in turn).
2056 png_uint_16
*error_ptr
;
2057 /* Where these are stored in the static array (for 'accumulate') */
2061 /* Return a 'transform' as above for the given format conversion. */
2063 transform_from_formats(Transform
*result
, Image
*in_image
,
2064 const Image
*out_image
, png_const_colorp background
, int via_linear
)
2066 png_uint_32 in_format
, out_format
;
2067 png_uint_32 in_base
, out_base
;
2069 memset(result
, 0, sizeof *result
);
2071 /* Store the original images for error messages */
2072 result
->in_image
= in_image
;
2073 result
->out_image
= out_image
;
2075 in_format
= in_image
->image
.format
;
2076 out_format
= out_image
->image
.format
;
2078 if (in_format
& PNG_FORMAT_FLAG_LINEAR
)
2079 result
->in_opaque
= 65535;
2081 result
->in_opaque
= 255;
2083 result
->output_8bit
= (out_format
& PNG_FORMAT_FLAG_LINEAR
) == 0;
2085 result
->is_palette
= 0; /* set by caller if required */
2086 result
->accumulate
= (in_image
->opts
& ACCUMULATE
) != 0;
2088 /* The loaders (which need the ordering information) */
2089 result
->in_gp
= get_pixel(in_format
);
2090 result
->out_gp
= get_pixel(out_format
);
2092 /* Remove the ordering information: */
2093 in_format
&= BASE_FORMATS
| PNG_FORMAT_FLAG_COLORMAP
;
2094 in_base
= in_format
& BASE_FORMATS
;
2095 out_format
&= BASE_FORMATS
| PNG_FORMAT_FLAG_COLORMAP
;
2096 out_base
= out_format
& BASE_FORMATS
;
2100 /* Check for an error in this program: */
2101 if (out_format
& (PNG_FORMAT_FLAG_LINEAR
|PNG_FORMAT_FLAG_COLORMAP
))
2103 fprintf(stderr
, "internal transform via linear error 0x%x->0x%x\n",
2104 in_format
, out_format
);
2108 result
->transform
= gpc_fn
[in_base
][out_base
| PNG_FORMAT_FLAG_LINEAR
];
2109 result
->from_linear
= gpc_fn
[out_base
| PNG_FORMAT_FLAG_LINEAR
][out_base
];
2110 result
->error_ptr
= gpc_error_via_linear
[in_format
][out_format
];
2113 else if (~in_format
& out_format
& PNG_FORMAT_FLAG_COLORMAP
)
2115 /* The input is not colormapped but the output is, the errors will
2116 * typically be large (only the grayscale-no-alpha case permits preserving
2117 * even 8-bit values.)
2119 result
->transform
= gpc_fn
[in_base
][out_base
];
2120 result
->from_linear
= NULL
;
2121 result
->error_ptr
= gpc_error_to_colormap
[in_base
][out_base
];
2126 /* The caller handles the colormap->pixel value conversion, so the
2127 * transform function just gets a pixel value, however because libpng
2128 * currently contains a different implementation for mapping a colormap if
2129 * both input and output are colormapped we need different conversion
2130 * functions to deal with errors in the libpng implementation.
2132 if (in_format
& out_format
& PNG_FORMAT_FLAG_COLORMAP
)
2133 result
->transform
= gpc_fn_colormapped
[in_base
][out_base
];
2135 result
->transform
= gpc_fn
[in_base
][out_base
];
2136 result
->from_linear
= NULL
;
2137 result
->error_ptr
= gpc_error
[in_format
][out_format
];
2140 /* Follow the libpng simplified API rules to work out what to pass to the gpc
2141 * routines as a background value, if one is not required pass NULL so that
2142 * this program crashes in the even of a programming error.
2144 result
->background
= NULL
; /* default: not required */
2146 /* Rule 1: background only need be supplied if alpha is to be removed */
2147 if (in_format
& ~out_format
& PNG_FORMAT_FLAG_ALPHA
)
2149 /* The input value is 'NULL' to use the background and (otherwise) an sRGB
2150 * background color (to use a solid color). The code above uses a fixed
2151 * byte value, BUFFER_INIT8, for buffer even for 16-bit output. For
2152 * linear (16-bit) output the sRGB background color is ignored; the
2153 * composition is always on the background (so BUFFER_INIT8 * 257), except
2154 * that for the colormap (i.e. linear colormapped output) black is used.
2156 result
->background
= &result
->background_color
;
2158 if (out_format
& PNG_FORMAT_FLAG_LINEAR
|| via_linear
)
2160 if (out_format
& PNG_FORMAT_FLAG_COLORMAP
)
2162 result
->background_color
.ir
=
2163 result
->background_color
.ig
=
2164 result
->background_color
.ib
= 0;
2165 result
->background_color
.dr
=
2166 result
->background_color
.dg
=
2167 result
->background_color
.db
= 0;
2172 result
->background_color
.ir
=
2173 result
->background_color
.ig
=
2174 result
->background_color
.ib
= BUFFER_INIT8
* 257;
2175 result
->background_color
.dr
=
2176 result
->background_color
.dg
=
2177 result
->background_color
.db
= 0;
2181 else /* sRGB output */
2183 if (background
!= NULL
)
2185 if (out_format
& PNG_FORMAT_FLAG_COLOR
)
2187 result
->background_color
.ir
= background
->red
;
2188 result
->background_color
.ig
= background
->green
;
2189 result
->background_color
.ib
= background
->blue
;
2190 /* TODO: sometimes libpng uses the power law conversion here, how
2193 result
->background_color
.dr
= sRGB_to_d
[background
->red
];
2194 result
->background_color
.dg
= sRGB_to_d
[background
->green
];
2195 result
->background_color
.db
= sRGB_to_d
[background
->blue
];
2198 else /* grayscale: libpng only looks at 'g' */
2200 result
->background_color
.ir
=
2201 result
->background_color
.ig
=
2202 result
->background_color
.ib
= background
->green
;
2203 /* TODO: sometimes libpng uses the power law conversion here, how
2206 result
->background_color
.dr
=
2207 result
->background_color
.dg
=
2208 result
->background_color
.db
= sRGB_to_d
[background
->green
];
2212 else if ((out_format
& PNG_FORMAT_FLAG_COLORMAP
) == 0)
2214 result
->background_color
.ir
=
2215 result
->background_color
.ig
=
2216 result
->background_color
.ib
= BUFFER_INIT8
;
2217 /* TODO: sometimes libpng uses the power law conversion here, how
2220 result
->background_color
.dr
=
2221 result
->background_color
.dg
=
2222 result
->background_color
.db
= sRGB_to_d
[BUFFER_INIT8
];
2225 /* Else the output is colormapped and a background color must be
2226 * provided; if pngstest crashes then that is a bug in this program
2227 * (though libpng should png_error as well.)
2230 result
->background
= NULL
;
2234 if (result
->background
== NULL
)
2236 result
->background_color
.ir
=
2237 result
->background_color
.ig
=
2238 result
->background_color
.ib
= -1; /* not used */
2239 result
->background_color
.dr
=
2240 result
->background_color
.dg
=
2241 result
->background_color
.db
= 1E30
; /* not used */
2245 /* Copy the error values into the Transform: */
2246 result
->error
[0] = result
->error_ptr
[0];
2247 result
->error
[1] = result
->error_ptr
[1];
2248 result
->error
[2] = result
->error_ptr
[2];
2249 result
->error
[3] = result
->error_ptr
[3];
2253 /* Compare two pixels.
2256 static int error_to_linear = 811; * by experiment *
2257 static int error_to_linear_grayscale = 424; * by experiment *
2258 static int error_to_sRGB = 6; * by experiment *
2259 static int error_to_sRGB_grayscale = 17; * libpng error by calculation +
2261 static int error_in_compose = 2; * by experiment *
2262 static int error_in_premultiply = 1;
2264 * The following is *just* the result of a round trip from 8-bit sRGB to linear
2265 * then back to 8-bit sRGB when it is done by libpng. There are two problems:
2267 * 1) libpng currently uses a 2.2 power law with no linear segment, this results
2268 * in instability in the low values and even with 16-bit precision sRGB(1) ends
2269 * up mapping to sRGB(0) as a result of rounding in the 16-bit representation.
2270 * This gives an error of 1 in the handling of value 1 only.
2272 * 2) libpng currently uses an intermediate 8-bit linear value in gamma
2273 * correction of 8-bit values. This results in many more errors, the worse of
2274 * which is mapping sRGB(14) to sRGB(0).
2276 * The general 'error_via_linear' is more complex because of pre-multiplication,
2277 * this compounds the 8-bit errors according to the alpha value of the pixel.
2278 * As a result 256 values are pre-calculated for error_via_linear.
2281 static int error_in_libpng_gamma
;
2282 static int error_via_linear
[256]; /* Indexed by 8-bit alpha */
2285 init_error_via_linear(void)
2289 error_via_linear
[0] = 255; /* transparent pixel */
2291 for (alpha
=1; alpha
<=255; ++alpha
)
2293 /* 16-bit values less than 128.5 get rounded to 8-bit 0 and so the worst
2294 * case error arises with 16-bit 128.5, work out what sRGB
2295 * (non-associated) value generates 128.5; any value less than this is
2296 * going to map to 0, so the worst error is floor(value).
2298 * Note that errors are considerably higher (more than a factor of 2)
2299 * because libpng uses a simple power law for sRGB data at present.
2301 * Add .1 for arithmetic errors inside libpng.
2303 double v
= floor(255*pow(.5/*(128.5 * 255 / 65535)*/ / alpha
, 1/2.2)+.1);
2305 error_via_linear
[alpha
] = (int)v
;
2308 /* This is actually 14.99, but, despite the closeness to 15, 14 seems to work
2311 error_in_libpng_gamma
= 14;
2316 print_pixel(char string
[64], const Pixel
*pixel
, png_uint_32 format
)
2318 switch (format
& (PNG_FORMAT_FLAG_ALPHA
|PNG_FORMAT_FLAG_COLOR
))
2321 sprintf(string
, "%s(%d)", format_names
[format
], pixel
->g
);
2324 case PNG_FORMAT_FLAG_ALPHA
:
2325 sprintf(string
, "%s(%d,%d)", format_names
[format
], pixel
->g
,
2329 case PNG_FORMAT_FLAG_COLOR
:
2330 sprintf(string
, "%s(%d,%d,%d)", format_names
[format
],
2331 pixel
->r
, pixel
->g
, pixel
->b
);
2334 case PNG_FORMAT_FLAG_COLOR
|PNG_FORMAT_FLAG_ALPHA
:
2335 sprintf(string
, "%s(%d,%d,%d,%d)", format_names
[format
],
2336 pixel
->r
, pixel
->g
, pixel
->b
, pixel
->a
);
2340 sprintf(string
, "invalid-format");
2346 logpixel(const Transform
*transform
, png_uint_32 x
, png_uint_32 y
,
2347 const Pixel
*in
, const Pixel
*calc
, const Pixel
*out
, const char *reason
)
2349 png_uint_32 in_format
= transform
->in_image
->image
.format
;
2350 png_uint_32 out_format
= transform
->out_image
->image
.format
;
2352 png_uint_32 back_format
= out_format
& ~PNG_FORMAT_FLAG_ALPHA
;
2353 const char *via_linear
= "";
2355 char pixel_in
[64], pixel_calc
[64], pixel_out
[64], pixel_loc
[64];
2356 char background_info
[100];
2358 print_pixel(pixel_in
, in
, in_format
);
2359 print_pixel(pixel_calc
, calc
, out_format
);
2360 print_pixel(pixel_out
, out
, out_format
);
2362 if (transform
->is_palette
)
2363 sprintf(pixel_loc
, "palette: %lu", (unsigned long)y
);
2365 sprintf(pixel_loc
, "%lu,%lu", (unsigned long)x
, (unsigned long)y
);
2367 if (transform
->from_linear
!= NULL
)
2369 via_linear
= " (via linear)";
2370 /* And as a result the *read* format which did any background processing
2371 * was itself linear, so the background color information is also
2374 back_format
|= PNG_FORMAT_FLAG_LINEAR
;
2377 if (transform
->background
!= NULL
)
2380 char pixel_back
[64];
2382 back
.r
= transform
->background
->ir
;
2383 back
.g
= transform
->background
->ig
;
2384 back
.b
= transform
->background
->ib
;
2385 back
.a
= -1; /* not used */
2387 print_pixel(pixel_back
, &back
, back_format
);
2388 sprintf(background_info
, " on background %s", pixel_back
);
2392 background_info
[0] = 0;
2394 if (transform
->in_image
->file_name
!= transform
->out_image
->file_name
)
2396 char error_buffer
[512];
2397 sprintf(error_buffer
,
2398 "(%s) %s error%s:\n %s%s ->\n %s\n not: %s.\n"
2399 "Use --preserve and examine: ", pixel_loc
, reason
, via_linear
,
2400 pixel_in
, background_info
, pixel_out
, pixel_calc
);
2401 return logerror(transform
->in_image
, transform
->in_image
->file_name
,
2402 error_buffer
, transform
->out_image
->file_name
);
2407 char error_buffer
[512];
2408 sprintf(error_buffer
,
2409 "(%s) %s error%s:\n %s%s ->\n %s\n not: %s.\n"
2410 " The error happened when reading the original file with this format.",
2411 pixel_loc
, reason
, via_linear
, pixel_in
, background_info
, pixel_out
,
2413 return logerror(transform
->in_image
, transform
->in_image
->file_name
,
2419 cmppixel(Transform
*transform
, png_const_voidp in
, png_const_voidp out
,
2420 png_uint_32 x
, png_uint_32 y
/*or palette index*/)
2423 png_const_charp errmsg
;
2424 Pixel pixel_in
, pixel_calc
, pixel_out
;
2426 transform
->in_gp(&pixel_in
, in
);
2428 if (transform
->from_linear
== NULL
)
2429 transform
->transform(&pixel_calc
, &pixel_in
, transform
->background
);
2433 transform
->transform(&pixel_out
, &pixel_in
, transform
->background
);
2434 transform
->from_linear(&pixel_calc
, &pixel_out
, NULL
);
2437 transform
->out_gp(&pixel_out
, out
);
2439 /* Eliminate the case where the input and output values match exactly. */
2440 if (pixel_calc
.a
== pixel_out
.a
&& pixel_calc
.r
== pixel_out
.r
&&
2441 pixel_calc
.g
== pixel_out
.g
&& pixel_calc
.b
== pixel_out
.b
)
2444 /* Eliminate the case where the output pixel is transparent and the output
2445 * is 8-bit - any component values are valid. Don't check the input alpha
2446 * here to also skip the 16-bit small alpha cases.
2448 if (transform
->output_8bit
&& pixel_calc
.a
== 0 && pixel_out
.a
== 0)
2451 /* Check for alpha errors first; an alpha error can damage the components too
2452 * so avoid spurious checks on components if one is found.
2456 int err_a
= abs(pixel_calc
.a
-pixel_out
.a
);
2458 if (err_a
> transform
->error
[3])
2460 /* If accumulating check the components too */
2461 if (transform
->accumulate
)
2462 transform
->error
[3] = (png_uint_16
)err_a
;
2469 /* Now if *either* of the output alphas are 0 but alpha is within tolerance
2470 * eliminate the 8-bit component comparison.
2472 if (errmsg
== NULL
&& transform
->output_8bit
&&
2473 (pixel_calc
.a
== 0 || pixel_out
.a
== 0))
2476 if (errmsg
== NULL
) /* else just signal an alpha error */
2478 int err_r
= abs(pixel_calc
.r
- pixel_out
.r
);
2479 int err_g
= abs(pixel_calc
.g
- pixel_out
.g
);
2480 int err_b
= abs(pixel_calc
.b
- pixel_out
.b
);
2483 if ((err_r
| err_g
| err_b
) == 0)
2484 return 1; /* exact match */
2486 /* Mismatch on a component, check the input alpha */
2487 if (pixel_in
.a
>= transform
->in_opaque
)
2489 errmsg
= "opaque component";
2490 limit
= 2; /* opaque */
2493 else if (pixel_in
.a
> 0)
2495 errmsg
= "alpha component";
2496 limit
= 1; /* partially transparent */
2501 errmsg
= "transparent component (background)";
2502 limit
= 0; /* transparent */
2506 if (maxerr
< err_g
) maxerr
= err_g
;
2507 if (maxerr
< err_b
) maxerr
= err_b
;
2509 if (maxerr
<= transform
->error
[limit
])
2510 return 1; /* within the error limits */
2512 /* Handle a component mis-match; log it, just return an error code, or
2515 if (transform
->accumulate
)
2517 transform
->error
[limit
] = (png_uint_16
)maxerr
;
2518 return 1; /* to cause the caller to keep going */
2522 /* Failure to match and not accumulating, so the error must be logged. */
2523 return logpixel(transform
, x
, y
, &pixel_in
, &pixel_calc
, &pixel_out
, errmsg
);
2527 component_loc(png_byte loc
[4], png_uint_32 format
)
2529 /* Given a format return the number of channels and the location of
2532 * The mask 'loc' contains the component offset of the channels in the
2533 * following order. Note that if 'format' is grayscale the entries 1-3 must
2534 * all contain the location of the gray channel.
2543 if (format
& PNG_FORMAT_FLAG_COLOR
)
2549 # ifdef PNG_FORMAT_BGR_SUPPORTED
2550 if (format
& PNG_FORMAT_FLAG_BGR
)
2567 loc
[1] = loc
[2] = loc
[3] = 0;
2570 if (format
& PNG_FORMAT_FLAG_ALPHA
)
2572 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
2573 if (format
& PNG_FORMAT_FLAG_AFIRST
)
2589 loc
[0] = 4; /* not present */
2594 /* Compare two images, the original 'a', which was written out then read back in
2595 * to * give image 'b'. The formats may have been changed.
2598 compare_two_images(Image
*a
, Image
*b
, int via_linear
,
2599 png_const_colorp background
)
2601 ptrdiff_t stridea
= a
->stride
;
2602 ptrdiff_t strideb
= b
->stride
;
2603 png_const_bytep rowa
= a
->buffer
+16;
2604 png_const_bytep rowb
= b
->buffer
+16;
2605 png_uint_32 width
= a
->image
.width
;
2606 png_uint_32 height
= a
->image
.height
;
2607 png_uint_32 formata
= a
->image
.format
;
2608 png_uint_32 formatb
= b
->image
.format
;
2609 unsigned int a_sample
= PNG_IMAGE_SAMPLE_SIZE(formata
);
2610 unsigned int b_sample
= PNG_IMAGE_SAMPLE_SIZE(formatb
);
2611 int alpha_added
, alpha_removed
;
2615 int btoa
[4]={0,0,0,0};
2617 /* This should never happen: */
2618 if (width
!= b
->image
.width
|| height
!= b
->image
.height
)
2619 return logerror(a
, a
->file_name
, ": width x height changed: ",
2622 /* Set up the background and the transform */
2623 transform_from_formats(&tr
, a
, b
, background
, via_linear
);
2625 /* Find the first row and inter-row space. */
2626 if (!(formata
& PNG_FORMAT_FLAG_COLORMAP
) &&
2627 (formata
& PNG_FORMAT_FLAG_LINEAR
))
2630 if (!(formatb
& PNG_FORMAT_FLAG_COLORMAP
) &&
2631 (formatb
& PNG_FORMAT_FLAG_LINEAR
))
2634 if (stridea
< 0) rowa
+= (height
-1) * (-stridea
);
2635 if (strideb
< 0) rowb
+= (height
-1) * (-strideb
);
2637 /* First shortcut the two colormap case by comparing the image data; if it
2638 * matches then we expect the colormaps to match, although this is not
2639 * absolutely necessary for an image match. If the colormaps fail to match
2640 * then there is a problem in libpng.
2642 if (formata
& formatb
& PNG_FORMAT_FLAG_COLORMAP
)
2644 /* Only check colormap entries that actually exist; */
2645 png_const_bytep ppa
, ppb
;
2647 png_byte in_use
[256], amax
= 0, bmax
= 0;
2649 memset(in_use
, 0, sizeof in_use
);
2654 /* Do this the slow way to accumulate the 'in_use' flags, don't break out
2655 * of the loop until the end; this validates the color-mapped data to
2656 * ensure all pixels are valid color-map indexes.
2658 for (y
=0, match
=1; y
<height
&& match
; ++y
, ppa
+= stridea
, ppb
+= strideb
)
2662 for (x
=0; x
<width
; ++x
)
2664 png_byte bval
= ppb
[x
];
2665 png_byte aval
= ppa
[x
];
2679 /* If the buffers match then the colormaps must too. */
2682 /* Do the color-maps match, entry by entry? Only check the 'in_use'
2683 * entries. An error here should be logged as a color-map error.
2685 png_const_bytep a_cmap
= (png_const_bytep
)a
->colormap
;
2686 png_const_bytep b_cmap
= (png_const_bytep
)b
->colormap
;
2687 int result
= 1; /* match by default */
2689 /* This is used in logpixel to get the error message correct. */
2692 for (y
=0; y
<256; ++y
, a_cmap
+= a_sample
, b_cmap
+= b_sample
)
2695 /* The colormap entries should be valid, but because libpng doesn't
2696 * do any checking at present the original image may contain invalid
2697 * pixel values. These cause an error here (at present) unless
2698 * accumulating errors in which case the program just ignores them.
2700 if (y
>= a
->image
.colormap_entries
)
2702 if ((a
->opts
& ACCUMULATE
) == 0)
2705 sprintf(pindex
, "%lu[%lu]", (unsigned long)y
,
2706 (unsigned long)a
->image
.colormap_entries
);
2707 logerror(a
, a
->file_name
, ": bad pixel index: ", pindex
);
2712 else if (y
>= b
->image
.colormap_entries
)
2714 if ((b
->opts
& ACCUMULATE
) == 0)
2717 sprintf(pindex
, "%lu[%lu]", (unsigned long)y
,
2718 (unsigned long)b
->image
.colormap_entries
);
2719 logerror(b
, b
->file_name
, ": bad pixel index: ", pindex
);
2724 /* All the mismatches are logged here; there can only be 256! */
2725 else if (!cmppixel(&tr
, a_cmap
, b_cmap
, 0, y
))
2729 /* If requested, copy the error values back from the Transform. */
2730 if (a
->opts
& ACCUMULATE
)
2732 tr
.error_ptr
[0] = tr
.error
[0];
2733 tr
.error_ptr
[1] = tr
.error
[1];
2734 tr
.error_ptr
[2] = tr
.error
[2];
2735 tr
.error_ptr
[3] = tr
.error
[3];
2736 result
= 1; /* force a continue */
2742 /* else the image buffers don't match pixel-wise so compare sample values
2743 * instead, but first validate that the pixel indexes are in range (but
2744 * only if not accumulating, when the error is ignored.)
2746 else if ((a
->opts
& ACCUMULATE
) == 0)
2749 # define BYTE_CHARS 20 /* 2^32: GCC sprintf warning */
2751 # define BYTE_CHARS 3 /* 2^8: real maximum value */
2753 /* Check the original image first,
2754 * TODO: deal with input images with bad pixel values?
2756 if (amax
>= a
->image
.colormap_entries
)
2758 char pindex
[3+2*BYTE_CHARS
];
2759 sprintf(pindex
, "%d[%u]", amax
,
2760 (png_byte
)/*SAFE*/a
->image
.colormap_entries
);
2761 return logerror(a
, a
->file_name
, ": bad pixel index: ", pindex
);
2764 else if (bmax
>= b
->image
.colormap_entries
)
2766 char pindex
[3+2*BYTE_CHARS
];
2767 sprintf(pindex
, "%d[%u]", bmax
,
2768 (png_byte
)/*SAFE*/b
->image
.colormap_entries
);
2769 return logerror(b
, b
->file_name
, ": bad pixel index: ", pindex
);
2774 /* We can directly compare pixel values without the need to use the read
2775 * or transform support (i.e. a memory compare) if:
2777 * 1) The bit depth has not changed.
2778 * 2) RGB to grayscale has not been done (the reverse is ok; we just compare
2779 * the three RGB values to the original grayscale.)
2780 * 3) An alpha channel has not been removed from an 8-bit format, or the
2781 * 8-bit alpha value of the pixel was 255 (opaque).
2783 * If an alpha channel has been *added* then it must have the relevant opaque
2784 * value (255 or 65535).
2786 * The fist two the tests (in the order given above) (using the boolean
2787 * equivalence !a && !b == !(a || b))
2789 if (!(((formata
^ formatb
) & PNG_FORMAT_FLAG_LINEAR
) |
2790 (formata
& (formatb
^ PNG_FORMAT_FLAG_COLOR
) & PNG_FORMAT_FLAG_COLOR
)))
2792 /* Was an alpha channel changed? */
2793 png_uint_32 alpha_changed
= (formata
^ formatb
) & PNG_FORMAT_FLAG_ALPHA
;
2795 /* Was an alpha channel removed? (The third test.) If so the direct
2796 * comparison is only possible if the input alpha is opaque.
2798 alpha_removed
= (formata
& alpha_changed
) != 0;
2800 /* Was an alpha channel added? */
2801 alpha_added
= (formatb
& alpha_changed
) != 0;
2803 /* The channels may have been moved between input and output, this finds
2804 * out how, recording the result in the btoa array, which says where in
2805 * 'a' to find each channel of 'b'. If alpha was added then btoa[alpha]
2806 * ends up as 4 (and is not used.)
2813 /* The following are used only if the formats match, except that
2814 * 'bchannels' is a flag for matching formats. btoa[x] says, for each
2815 * channel in b, where to find the corresponding value in a, for the
2816 * bchannels. achannels may be different for a gray to rgb transform
2817 * (a will be 1 or 2, b will be 3 or 4 channels.)
2819 (void)component_loc(aloc
, formata
);
2820 bchannels
= component_loc(bloc
, formatb
);
2822 /* Hence the btoa array. */
2823 for (i
=0; i
<4; ++i
) if (bloc
[i
] < 4)
2824 btoa
[bloc
[i
]] = aloc
[i
]; /* may be '4' for alpha */
2827 alpha_added
= bloc
[0]; /* location of alpha channel in image b */
2830 alpha_added
= 4; /* Won't match an image b channel */
2833 alpha_removed
= aloc
[0]; /* location of alpha channel in image a */
2842 /* Direct compare is not possible, cancel out all the corresponding local
2846 alpha_removed
= alpha_added
= 4;
2847 btoa
[3] = btoa
[2] = btoa
[1] = btoa
[0] = 4; /* 4 == not present */
2850 for (y
=0; y
<height
; ++y
, rowa
+= stridea
, rowb
+= strideb
)
2852 png_const_bytep ppa
, ppb
;
2855 for (x
=0, ppa
=rowa
, ppb
=rowb
; x
<width
; ++x
)
2857 png_const_bytep psa
, psb
;
2859 if (formata
& PNG_FORMAT_FLAG_COLORMAP
)
2860 psa
= (png_const_bytep
)a
->colormap
+ a_sample
* *ppa
++;
2862 psa
= ppa
, ppa
+= a_sample
;
2864 if (formatb
& PNG_FORMAT_FLAG_COLORMAP
)
2865 psb
= (png_const_bytep
)b
->colormap
+ b_sample
* *ppb
++;
2867 psb
= ppb
, ppb
+= b_sample
;
2869 /* Do the fast test if possible. */
2872 /* Check each 'b' channel against either the corresponding 'a'
2873 * channel or the opaque alpha value, as appropriate. If
2874 * alpha_removed value is set (not 4) then also do this only if the
2875 * 'a' alpha channel (alpha_removed) is opaque; only relevant for
2878 if (formatb
& PNG_FORMAT_FLAG_LINEAR
) /* 16-bit checks */
2880 png_const_uint_16p pua
= aligncastconst(png_const_uint_16p
, psa
);
2881 png_const_uint_16p pub
= aligncastconst(png_const_uint_16p
, psb
);
2886 if (pua
[btoa
[3]] != pub
[3]) break;
2889 if (pua
[btoa
[2]] != pub
[2]) break;
2892 if (pua
[btoa
[1]] != pub
[1]) break;
2895 if (pua
[btoa
[0]] != pub
[0]) break;
2896 if (alpha_added
!= 4 && pub
[alpha_added
] != 65535) break;
2897 continue; /* x loop */
2899 break; /* impossible */
2903 else if (alpha_removed
== 4 || psa
[alpha_removed
] == 255)
2908 if (psa
[btoa
[3]] != psb
[3]) break;
2911 if (psa
[btoa
[2]] != psb
[2]) break;
2914 if (psa
[btoa
[1]] != psb
[1]) break;
2917 if (psa
[btoa
[0]] != psb
[0]) break;
2918 if (alpha_added
!= 4 && psb
[alpha_added
] != 255) break;
2919 continue; /* x loop */
2921 break; /* impossible */
2926 /* If we get to here the fast match failed; do the slow match for this
2929 if (!cmppixel(&tr
, psa
, psb
, x
, y
) && (a
->opts
& KEEP_GOING
) == 0)
2930 return 0; /* error case */
2934 /* If requested, copy the error values back from the Transform. */
2935 if (a
->opts
& ACCUMULATE
)
2937 tr
.error_ptr
[0] = tr
.error
[0];
2938 tr
.error_ptr
[1] = tr
.error
[1];
2939 tr
.error_ptr
[2] = tr
.error
[2];
2940 tr
.error_ptr
[3] = tr
.error
[3];
2946 /* Read the file; how the read gets done depends on which of input_file and
2947 * input_memory have been set.
2950 read_file(Image
*image
, png_uint_32 format
, png_const_colorp background
)
2952 memset(&image
->image
, 0, sizeof image
->image
);
2953 image
->image
.version
= PNG_IMAGE_VERSION
;
2955 if (image
->input_memory
!= NULL
)
2957 if (!png_image_begin_read_from_memory(&image
->image
, image
->input_memory
,
2958 image
->input_memory_size
))
2959 return logerror(image
, "memory init: ", image
->file_name
, "");
2962 # ifdef PNG_STDIO_SUPPORTED
2963 else if (image
->input_file
!= NULL
)
2965 if (!png_image_begin_read_from_stdio(&image
->image
, image
->input_file
))
2966 return logerror(image
, "stdio init: ", image
->file_name
, "");
2971 if (!png_image_begin_read_from_file(&image
->image
, image
->file_name
))
2972 return logerror(image
, "file init: ", image
->file_name
, "");
2977 return logerror(image
, "unsupported file/stdio init: ",
2978 image
->file_name
, "");
2982 /* This must be set after the begin_read call: */
2983 if (image
->opts
& sRGB_16BIT
)
2984 image
->image
.flags
|= PNG_IMAGE_FLAG_16BIT_sRGB
;
2986 /* Have an initialized image with all the data we need plus, maybe, an
2987 * allocated file (myfile) or buffer (mybuffer) that need to be freed.
2991 png_uint_32 image_format
;
2993 /* Print both original and output formats. */
2994 image_format
= image
->image
.format
;
2996 if (image
->opts
& VERBOSE
)
2998 printf("%s %lu x %lu %s -> %s", image
->file_name
,
2999 (unsigned long)image
->image
.width
,
3000 (unsigned long)image
->image
.height
,
3001 format_names
[image_format
& FORMAT_MASK
],
3002 (format
& FORMAT_NO_CHANGE
) != 0 || image
->image
.format
== format
3003 ? "no change" : format_names
[format
& FORMAT_MASK
]);
3005 if (background
!= NULL
)
3006 printf(" background(%d,%d,%d)\n", background
->red
,
3007 background
->green
, background
->blue
);
3014 /* 'NO_CHANGE' combined with the color-map flag forces the base format
3015 * flags to be set on read to ensure that the original representation is
3016 * not lost in the pass through a colormap format.
3018 if ((format
& FORMAT_NO_CHANGE
) != 0)
3020 if ((format
& PNG_FORMAT_FLAG_COLORMAP
) != 0 &&
3021 (image_format
& PNG_FORMAT_FLAG_COLORMAP
) != 0)
3022 format
= (image_format
& ~BASE_FORMATS
) | (format
& BASE_FORMATS
);
3025 format
= image_format
;
3028 image
->image
.format
= format
;
3030 image
->stride
= PNG_IMAGE_ROW_STRIDE(image
->image
) + image
->stride_extra
;
3033 result
= png_image_finish_read(&image
->image
, background
,
3034 image
->buffer
+16, (png_int_32
)image
->stride
, image
->colormap
);
3036 checkbuffer(image
, image
->file_name
);
3039 return checkopaque(image
);
3042 return logerror(image
, image
->file_name
, ": image read failed", "");
3046 /* Reads from a filename, which must be in image->file_name, but uses
3047 * image->opts to choose the method. The file is always read in its native
3048 * format (the one the simplified API suggests).
3051 read_one_file(Image
*image
)
3053 if (!(image
->opts
& USE_FILE
) || (image
->opts
& USE_STDIO
))
3055 /* memory or stdio. */
3056 FILE *f
= fopen(image
->file_name
, "rb");
3060 if (image
->opts
& USE_FILE
)
3061 image
->input_file
= f
;
3065 if (fseek(f
, 0, SEEK_END
) == 0)
3067 long int cb
= ftell(f
);
3071 #ifndef __COVERITY__
3072 if ((unsigned long int)cb
<= (size_t)~(size_t)0)
3075 png_bytep b
= voidcast(png_bytep
, malloc((size_t)cb
));
3081 if (fread(b
, (size_t)cb
, 1, f
) == 1)
3084 image
->input_memory_size
= cb
;
3085 image
->input_memory
= b
;
3091 return logclose(image
, f
, image
->file_name
,
3097 return logclose(image
, f
, image
->file_name
,
3098 ": out of memory: ");
3102 return logclose(image
, f
, image
->file_name
,
3103 ": file too big for this architecture: ");
3104 /* cb is the length of the file as a (long) and
3105 * this is greater than the maximum amount of
3106 * memory that can be requested from malloc.
3111 return logclose(image
, f
, image
->file_name
,
3115 return logclose(image
, f
, image
->file_name
,
3120 return logclose(image
, f
, image
->file_name
, ": seek failed: ");
3125 return logerror(image
, image
->file_name
, ": open failed: ",
3129 return read_file(image
, FORMAT_NO_CHANGE
, NULL
);
3132 #ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
3134 write_one_file(Image
*output
, Image
*image
, int convert_to_8bit
)
3136 if (image
->opts
& FAST_WRITE
)
3137 image
->image
.flags
|= PNG_IMAGE_FLAG_FAST
;
3139 if (image
->opts
& USE_STDIO
)
3141 #ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
3142 #ifndef __COVERITY__
3143 FILE *f
= tmpfile();
3145 /* Experimental. Coverity says tmpfile() is insecure because it
3146 * generates predictable names.
3148 * It is possible to satisfy Coverity by using mkstemp(); however,
3149 * any platform supporting mkstemp() undoubtedly has a secure tmpfile()
3150 * implementation as well, and doesn't need the fix. Note that
3151 * the fix won't work on platforms that don't support mkstemp().
3153 * https://www.securecoding.cert.org/confluence/display/c/
3154 * FIO21-C.+Do+not+create+temporary+files+in+shared+directories
3155 * says that most historic implementations of tmpfile() provide
3156 * only a limited number of possible temporary file names
3157 * (usually 26) before file names are recycled. That article also
3158 * provides a secure solution that unfortunately depends upon mkstemp().
3160 char tmpfile
[] = "pngstest-XXXXXX";
3164 filedes
= mkstemp(tmpfile
);
3169 f
= fdopen(filedes
,"w+");
3170 /* Hide the filename immediately and ensure that the file does
3171 * not exist after the program ends
3173 (void) unlink(tmpfile
);
3179 if (png_image_write_to_stdio(&image
->image
, f
, convert_to_8bit
,
3180 image
->buffer
+16, (png_int_32
)image
->stride
, image
->colormap
))
3185 initimage(output
, image
->opts
, "tmpfile", image
->stride_extra
);
3186 output
->input_file
= f
;
3187 if (!checkopaque(image
))
3192 return logclose(image
, f
, "tmpfile", ": flush: ");
3198 return logerror(image
, "tmpfile", ": write failed", "");
3203 return logerror(image
, "tmpfile", ": open: ", strerror(errno
));
3204 #else /* SIMPLIFIED_WRITE_STDIO */
3205 return logerror(image
, "tmpfile", ": open: unsupported", "");
3206 #endif /* SIMPLIFIED_WRITE_STDIO */
3209 else if (image
->opts
& USE_FILE
)
3211 #ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
3212 static int counter
= 0;
3215 sprintf(name
, "%s%d.png", tmpf
, ++counter
);
3217 if (png_image_write_to_file(&image
->image
, name
, convert_to_8bit
,
3218 image
->buffer
+16, (png_int_32
)image
->stride
, image
->colormap
))
3220 initimage(output
, image
->opts
, output
->tmpfile_name
,
3221 image
->stride_extra
);
3222 /* Afterwards, or freeimage will delete it! */
3223 strcpy(output
->tmpfile_name
, name
);
3225 if (!checkopaque(image
))
3230 return logerror(image
, name
, ": write failed", "");
3231 #else /* SIMPLIFIED_WRITE_STDIO */
3232 return logerror(image
, "stdio", ": open: unsupported", "");
3233 #endif /* SIMPLIFIED_WRITE_STDIO */
3236 else /* use memory */
3238 png_alloc_size_t size
;
3240 if (png_image_write_get_memory_size(image
->image
, size
, convert_to_8bit
,
3241 image
->buffer
+16, (png_int_32
)image
->stride
, image
->colormap
))
3243 /* This is non-fatal but ignoring it was causing serious problems in
3244 * the macro to be ignored:
3246 if (size
> PNG_IMAGE_PNG_SIZE_MAX(image
->image
))
3247 return logerror(image
, "memory", ": PNG_IMAGE_SIZE_MAX wrong", "");
3249 initimage(output
, image
->opts
, "memory", image
->stride_extra
);
3250 output
->input_memory
= malloc(size
);
3252 if (output
->input_memory
!= NULL
)
3254 output
->input_memory_size
= size
;
3256 if (png_image_write_to_memory(&image
->image
, output
->input_memory
,
3257 &output
->input_memory_size
, convert_to_8bit
, image
->buffer
+16,
3258 (png_int_32
)image
->stride
, image
->colormap
))
3260 /* This is also non-fatal but it safes safer to error out anyway:
3262 if (size
!= output
->input_memory_size
)
3263 return logerror(image
, "memory", ": memory size wrong", "");
3267 return logerror(image
, "memory", ": write failed", "");
3271 return logerror(image
, "memory", ": out of memory", "");
3275 return logerror(image
, "memory", ": get size:", "");
3278 /* 'output' has an initialized temporary image, read this back in and compare
3279 * this against the original: there should be no change since the original
3280 * format was written unmodified unless 'convert_to_8bit' was specified.
3281 * However, if the original image was color-mapped, a simple read will zap
3282 * the linear, color and maybe alpha flags, this will cause spurious failures
3283 * under some circumstances.
3285 if (read_file(output
, image
->image
.format
| FORMAT_NO_CHANGE
, NULL
))
3287 png_uint_32 original_format
= image
->image
.format
;
3289 if (convert_to_8bit
)
3290 original_format
&= ~PNG_FORMAT_FLAG_LINEAR
;
3292 if ((output
->image
.format
& BASE_FORMATS
) !=
3293 (original_format
& BASE_FORMATS
))
3294 return logerror(image
, image
->file_name
, ": format changed on read: ",
3297 return compare_two_images(image
, output
, 0/*via linear*/, NULL
);
3301 return logerror(output
, output
->tmpfile_name
,
3302 ": read of new file failed", "");
3307 testimage(Image
*image
, png_uint_32 opts
, format_list
*pf
)
3312 /* Copy the original data, stealing it from 'image' */
3321 image
->input_file
= NULL
;
3322 image
->input_memory
= NULL
;
3323 image
->input_memory_size
= 0;
3324 image
->tmpfile_name
[0] = 0;
3327 png_uint_32 counter
;
3334 /* Use the low bit of 'counter' to indicate whether or not to do alpha
3335 * removal with a background color or by composting onto the image; this
3336 * step gets skipped if it isn't relevant
3338 for (counter
=0; counter
<2*FORMAT_COUNT
; ++counter
)
3339 if (format_isset(pf
, counter
>> 1))
3341 png_uint_32 format
= counter
>> 1;
3343 png_color background_color
;
3344 png_colorp background
= NULL
;
3346 /* If there is a format change that removes the alpha channel then
3347 * the background is relevant. If the output is 8-bit color-mapped
3348 * then a background color *must* be provided, otherwise there are
3349 * two tests to do - one with a color, the other with NULL. The
3350 * NULL test happens second.
3352 if ((counter
& 1) == 0)
3354 if ((format
& PNG_FORMAT_FLAG_ALPHA
) == 0 &&
3355 (image
->image
.format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3357 /* Alpha/transparency will be removed, the background is
3358 * relevant: make it a color the first time
3360 random_color(&background_color
);
3361 background
= &background_color
;
3363 /* BUT if the output is to a color-mapped 8-bit format then
3364 * the background must always be a color, so increment 'counter'
3365 * to skip the NULL test.
3367 if ((format
& PNG_FORMAT_FLAG_COLORMAP
) != 0 &&
3368 (format
& PNG_FORMAT_FLAG_LINEAR
) == 0)
3372 /* Otherwise an alpha channel is not being eliminated, just leave
3373 * background NULL and skip the (counter & 1) NULL test.
3378 /* else just use NULL for background */
3381 copy
.opts
= opts
; /* in case read_file needs to change it */
3383 result
= read_file(©
, format
, background
);
3387 /* Make sure the file just read matches the original file. */
3388 result
= compare_two_images(image
, ©
, 0/*via linear*/, background
);
3392 # ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
3393 /* Write the *copy* just made to a new file to make sure the write
3394 * side works ok. Check the conversion to sRGB if the copy is
3398 result
= write_one_file(&output
, ©
, 0/*convert to 8bit*/);
3402 /* Validate against the original too; the background is needed here
3403 * as well so that compare_two_images knows what color was used.
3405 result
= compare_two_images(image
, &output
, 0, background
);
3409 if ((format
& PNG_FORMAT_FLAG_LINEAR
) != 0 &&
3410 (format
& PNG_FORMAT_FLAG_COLORMAP
) == 0)
3412 /* 'output' is linear, convert to the corresponding sRGB format.
3415 result
= write_one_file(&output
, ©
, 1/*convert to 8bit*/);
3419 /* This may involve a conversion via linear; in the ideal world
3420 * this would round-trip correctly, but libpng 1.5.7 is not the
3421 * ideal world so allow a drift (error_via_linear).
3423 * 'image' has an alpha channel but 'output' does not then there
3424 * will a strip-alpha-channel operation (because 'output' is
3425 * linear), handle this by composing on black when doing the
3428 result
= compare_two_images(image
, &output
, 1/*via_linear*/,
3433 # endif /* PNG_SIMPLIFIED_WRITE_SUPPORTED */
3445 test_one_file(const char *file_name
, format_list
*formats
, png_uint_32 opts
,
3446 int stride_extra
, int log_pass
)
3451 if (!(opts
& NO_RESEED
))
3452 reseed(); /* ensure that the random numbers don't depend on file order */
3454 initimage(&image
, opts
, file_name
, stride_extra
);
3455 result
= read_one_file(&image
);
3457 result
= testimage(&image
, opts
, formats
);
3460 /* Ensure that stderr is flushed into any log file */
3471 # ifndef PNG_SIMPLIFIED_WRITE_SUPPORTED
3472 printf(" (no write)");
3476 printf(" %s\n", file_name
);
3477 /* stdout may not be line-buffered if it is piped to a file, so: */
3488 main(int argc
, char **argv
)
3490 png_uint_32 opts
= FAST_WRITE
| STRICT
;
3491 format_list formats
;
3492 const char *touch
= NULL
;
3495 int stride_extra
= 0;
3499 #if PNG_LIBPNG_VER >= 10700
3500 /* This error should not exist in 1.7 or later: */
3506 init_error_via_linear();
3508 format_init(&formats
);
3509 reseed(); /* initialize random number seeds */
3511 for (c
=1; c
<argc
; ++c
)
3513 const char *arg
= argv
[c
];
3515 if (strcmp(arg
, "--log") == 0)
3517 else if (strcmp(arg
, "--fresh") == 0)
3519 memset(gpc_error
, 0, sizeof gpc_error
);
3520 memset(gpc_error_via_linear
, 0, sizeof gpc_error_via_linear
);
3522 else if (strcmp(arg
, "--file") == 0)
3523 # ifdef PNG_STDIO_SUPPORTED
3526 return SKIP
; /* skipped: no support */
3528 else if (strcmp(arg
, "--memory") == 0)
3530 else if (strcmp(arg
, "--stdio") == 0)
3531 # ifdef PNG_STDIO_SUPPORTED
3534 return SKIP
; /* skipped: no support */
3536 else if (strcmp(arg
, "--name") == 0)
3538 else if (strcmp(arg
, "--verbose") == 0)
3540 else if (strcmp(arg
, "--quiet") == 0)
3542 else if (strcmp(arg
, "--preserve") == 0)
3543 opts
|= KEEP_TMPFILES
;
3544 else if (strcmp(arg
, "--nopreserve") == 0)
3545 opts
&= ~KEEP_TMPFILES
;
3546 else if (strcmp(arg
, "--keep-going") == 0)
3548 else if (strcmp(arg
, "--fast") == 0)
3550 else if (strcmp(arg
, "--slow") == 0)
3551 opts
&= ~FAST_WRITE
;
3552 else if (strcmp(arg
, "--accumulate") == 0)
3554 else if (strcmp(arg
, "--redundant") == 0)
3556 else if (strcmp(arg
, "--stop") == 0)
3557 opts
&= ~KEEP_GOING
;
3558 else if (strcmp(arg
, "--strict") == 0)
3560 else if (strcmp(arg
, "--nostrict") == 0)
3562 else if (strcmp(arg
, "--sRGB-16bit") == 0)
3564 else if (strcmp(arg
, "--linear-16bit") == 0)
3565 opts
&= ~sRGB_16BIT
;
3566 else if (strcmp(arg
, "--noreseed") == 0)
3568 else if (strcmp(arg
, "--fault-gbg-warning") == 0)
3570 else if (strcmp(arg
, "--tmpfile") == 0)
3574 if (strlen(argv
[++c
]) >= sizeof tmpf
)
3577 fprintf(stderr
, "%s: %s is too long for a temp file prefix\n",
3582 /* Safe: checked above */
3583 strncpy(tmpf
, argv
[c
], sizeof (tmpf
)-1);
3589 fprintf(stderr
, "%s: %s requires a temporary file prefix\n",
3594 else if (strcmp(arg
, "--touch") == 0)
3602 fprintf(stderr
, "%s: %s requires a file name argument\n",
3607 else if (arg
[0] == '+')
3609 png_uint_32 format
= formatof(arg
+1);
3611 if (format
> FORMAT_COUNT
)
3614 format_set(&formats
, format
);
3616 else if (arg
[0] == '-' && arg
[1] != 0 && (arg
[1] != '0' || arg
[2] != 0))
3619 fprintf(stderr
, "%s: unknown option: %s\n", argv
[0], arg
);
3624 if (format_is_initial(&formats
))
3625 format_default(&formats
, redundant
);
3629 int term
= (arg
[1] == '0' ? 0 : '\n');
3630 unsigned int ich
= 0;
3632 /* Loop reading files, use a static buffer to simplify this and just
3633 * stop if the name gets to long.
3635 static char buffer
[4096];
3641 /* Don't allow '\0' in file names, and terminate with '\n' or,
3642 * for -0, just '\0' (use -print0 to find to make this work!)
3644 if (ch
== EOF
|| ch
== term
|| ch
== 0)
3648 if (ich
> 0 && !test_one_file(buffer
, &formats
, opts
,
3649 stride_extra
, log_pass
))
3656 --ich
; /* so that the increment below sets it to 0 again */
3660 buffer
[ich
] = (char)ch
;
3661 } while (++ich
< sizeof buffer
);
3667 fprintf(stderr
, "%s...%s: file name too long\n", buffer
,
3673 else if (!test_one_file(arg
, &formats
, opts
, stride_extra
, log_pass
))
3678 if (opts
& ACCUMULATE
)
3682 printf("/* contrib/libtests/pngstest-errors.h\n");
3684 printf(" * BUILT USING:" PNG_HEADER_VERSION_STRING
);
3686 printf(" * This code is released under the libpng license.\n");
3687 printf(" * For conditions of distribution and use, see the disclaimer\n");
3688 printf(" * and license in png.h\n");
3690 printf(" * THIS IS A MACHINE GENERATED FILE: do not edit it directly!\n");
3691 printf(" * Instead run:\n");
3693 printf(" * pngstest --accumulate\n");
3695 printf(" * on as many PNG files as possible; at least PNGSuite and\n");
3696 printf(" * contrib/libtests/testpngs.\n");
3699 printf("static png_uint_16 gpc_error[16/*in*/][16/*out*/][4/*a*/] =\n");
3701 for (in
=0; in
<16; ++in
)
3704 printf(" { /* input: %s */\n ", format_names
[in
]);
3705 for (out
=0; out
<16; ++out
)
3709 for (alpha
=0; alpha
<4; ++alpha
)
3711 printf(" %d", gpc_error
[in
][out
][alpha
]);
3712 if (alpha
< 3) putchar(',');
3718 if (out
% 4 == 3) printf("\n ");
3730 printf("static png_uint_16 gpc_error_via_linear[16][4/*out*/][4] =\n");
3732 for (in
=0; in
<16; ++in
)
3735 printf(" { /* input: %s */\n ", format_names
[in
]);
3736 for (out
=0; out
<4; ++out
)
3740 for (alpha
=0; alpha
<4; ++alpha
)
3742 printf(" %d", gpc_error_via_linear
[in
][out
][alpha
]);
3743 if (alpha
< 3) putchar(',');
3758 printf("static png_uint_16 gpc_error_to_colormap[8/*i*/][8/*o*/][4] =\n");
3760 for (in
=0; in
<8; ++in
)
3763 printf(" { /* input: %s */\n ", format_names
[in
]);
3764 for (out
=0; out
<8; ++out
)
3768 for (alpha
=0; alpha
<4; ++alpha
)
3770 printf(" %d", gpc_error_to_colormap
[in
][out
][alpha
]);
3771 if (alpha
< 3) putchar(',');
3777 if (out
% 4 == 3) printf("\n ");
3788 printf("/* END MACHINE GENERATED */\n");
3791 if (retval
== 0 && touch
!= NULL
)
3793 FILE *fsuccess
= fopen(touch
, "wt");
3795 if (fsuccess
!= NULL
)
3798 fprintf(fsuccess
, "PNG simple API tests succeeded\n");
3800 error
= ferror(fsuccess
);
3802 if (fclose(fsuccess
) || error
)
3805 fprintf(stderr
, "%s: write failed\n", touch
);
3813 fprintf(stderr
, "%s: open failed\n", touch
);
3821 #else /* !PNG_SIMPLIFIED_READ_SUPPORTED */
3824 fprintf(stderr
, "pngstest: no read support in libpng, test skipped\n");
3825 /* So the test is skipped: */
3828 #endif /* PNG_SIMPLIFIED_READ_SUPPORTED */