4 * Copyright (c) 2013 John Cunningham Bowler
6 * Last changed in libpng 1.6.8 [December 19, 2013]
8 * This code is released under the libpng license.
9 * For conditions of distribution and use, see the disclaimer
10 * and license in png.h
12 * Test for the PNG 'simplified' APIs.
14 #define _ISOC90_SOURCE 1
15 #define MALLOC_CHECK_ 2/*glibc facility: turn on debugging*/
25 #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
29 /* Define the following to use this test against your installed libpng, rather
30 * than the one being built here:
32 #ifdef PNG_FREESTANDING_TESTS
35 # include "../../png.h"
38 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED /* Else nothing can be done */
39 #include "../tools/sRGB.h"
43 * These defines switch on alternate algorithms for format conversions to match
44 * the current libpng implementation; they are set to allow pngstest to pass
45 * even though libpng is producing answers that are not as correct as they
48 #define ALLOW_UNUSED_GPC 0
49 /* If true include unused static GPC functions and declare an external array
50 * of them to hide the fact that they are unused. This is for development
51 * use while testing the correct function to use to take into account libpng
52 * misbehavior, such as using a simple power law to correct sRGB to linear.
55 /* The following is to support direct compilation of this file as C++ */
57 # define voidcast(type, value) static_cast<type>(value)
58 # define aligncastconst(type, value) \
59 static_cast<type>(static_cast<const void*>(value))
61 # define voidcast(type, value) (value)
62 # define aligncastconst(type, value) ((const void*)(value))
63 #endif /* __cplusplus */
65 /* During parallel runs of pngstest each temporary file needs a unique name,
66 * this is used to permit uniqueness using a command line argument which can be
67 * up to 22 characters long.
69 static char tmpf
[23] = "TMP";
71 /* Generate random bytes. This uses a boring repeatable algorithm and it
72 * is implemented here so that it gives the same set of numbers on every
73 * architecture. It's a linear congruential generator (Knuth or Sedgewick
74 * "Algorithms") but it comes from the 'feedback taps' table in Horowitz and
75 * Hill, "The Art of Electronics".
78 make_random_bytes(png_uint_32
* seed
, void* pv
, size_t size
)
80 png_uint_32 u0
= seed
[0], u1
= seed
[1];
81 png_bytep bytes
= voidcast(png_bytep
, pv
);
83 /* There are thirty three bits, the next bit in the sequence is bit-33 XOR
84 * bit-20. The top 1 bit is in u1, the bottom 32 are in u0.
87 for (i
=0; i
<size
; ++i
)
89 /* First generate 8 new bits then shift them in at the end. */
90 png_uint_32 u
= ((u0
>> (20-8)) ^ ((u1
<< 7) | (u0
>> (32-7)))) & 0xff;
95 *bytes
++ = (png_byte
)u
;
103 random_color(png_colorp color
)
105 static png_uint_32 color_seed
[2] = { 0x12345678, 0x9abcdef };
106 make_random_bytes(color_seed
, color
, sizeof *color
);
109 /* Math support - neither Cygwin nor Visual Studio have C99 support and we need
110 * a predictable rounding function, so make one here:
113 closestinteger(double x
)
115 return floor(x
+ .5);
118 /* Cast support: remove GCC whines. */
122 d
= closestinteger(d
);
129 d
= closestinteger(d
);
130 return (png_uint_16
)d
;
133 /* sRGB support: use exact calculations rounded to the nearest int, see the
134 * fesetround() call in main(). sRGB_to_d optimizes the 8 to 16-bit conversion.
136 static double sRGB_to_d
[256];
137 static double g22_to_d
[256];
145 for (i
=1; i
<255; ++i
)
146 sRGB_to_d
[i
] = linear_from_sRGB(i
/255.);
150 for (i
=1; i
<255; ++i
)
151 g22_to_d
[i
] = pow(i
/255., 1/.45455);
156 sRGB(double linear
/*range 0.0 .. 1.0*/)
158 return u8d(255 * sRGB_from_linear(linear
));
162 isRGB(int fixed_linear
)
164 return sRGB(fixed_linear
/ 65535.);
169 unpremultiply(int component
, int alpha
)
171 if (alpha
<= component
)
172 return 255; /* Arbitrary, but consistent with the libpng code */
174 else if (alpha
>= 65535)
175 return isRGB(component
);
178 return sRGB((double)component
/ alpha
);
183 ilinear(int fixed_srgb
)
185 return u16d(65535 * sRGB_to_d
[fixed_srgb
]);
189 ilineara(int fixed_srgb
, int alpha
)
191 return u16d((257 * alpha
) * sRGB_to_d
[fixed_srgb
]);
195 ilinear_g22(int fixed_srgb
)
197 return u16d(65535 * g22_to_d
[fixed_srgb
]);
202 ilineara_g22(int fixed_srgb
, int alpha
)
204 return u16d((257 * alpha
) * g22_to_d
[fixed_srgb
]);
209 YfromRGBint(int ir
, int ig
, int ib
)
214 return YfromRGB(r
, g
, b
);
218 /* The error that results from using a 2.2 power law in place of the correct
219 * sRGB transform, given an 8-bit value which might be either sRGB or power-law.
222 power_law_error8(int value
)
224 if (value
> 0 && value
< 255)
226 double vd
= value
/ 255.;
228 pow(sRGB_to_d
[value
], 1/2.2) - sRGB_from_linear(pow(vd
, 2.2)));
230 /* Always allow an extra 1 here for rounding errors */
231 e
= 1+floor(255 * e
);
238 static int error_in_sRGB_roundtrip
= 56; /* by experiment */
240 power_law_error16(int value
)
242 if (value
> 0 && value
< 65535)
244 /* Round trip the value through an 8-bit representation but using
245 * non-matching to/from conversions.
247 double vd
= value
/ 65535.;
249 pow(sRGB_from_linear(vd
), 2.2) - linear_from_sRGB(pow(vd
, 1/2.2)));
251 /* Always allow an extra 1 here for rounding errors */
252 e
= error_in_sRGB_roundtrip
+floor(65535 * e
);
260 compare_8bit(int v1
, int v2
, int error_limit
, int multiple_algorithms
)
265 if (e
<= error_limit
)
268 if (!multiple_algorithms
)
271 ev1
= power_law_error8(v1
);
275 ev2
= power_law_error8(v2
);
283 compare_16bit(int v1
, int v2
, int error_limit
, int multiple_algorithms
)
288 if (e
<= error_limit
)
291 /* "multiple_algorithms" in this case means that a color-map has been
292 * involved somewhere, so we can deduce that the values were forced to 8-bit
293 * (like the via_linear case for 8-bit.)
295 if (!multiple_algorithms
)
298 ev1
= power_law_error16(v1
);
302 ev2
= power_law_error16(v2
);
310 #define READ_FILE 1 /* else memory */
311 #define USE_STDIO 2 /* else use file name */
312 #define STRICT 4 /* fail on warnings too */
314 #define KEEP_TMPFILES 16 /* else delete temporary files */
315 #define KEEP_GOING 32
316 #define ACCUMULATE 64
317 #define FAST_WRITE 128
318 #define sRGB_16BIT 256
321 print_opts(png_uint_32 opts
)
323 if (opts
& READ_FILE
)
325 if (opts
& USE_STDIO
)
330 printf(" --verbose");
331 if (opts
& KEEP_TMPFILES
)
332 printf(" --preserve");
333 if (opts
& KEEP_GOING
)
334 printf(" --keep-going");
335 if (opts
& ACCUMULATE
)
336 printf(" --accumulate");
337 if (!(opts
& FAST_WRITE
)) /* --fast is currently the default */
339 if (opts
& sRGB_16BIT
)
340 printf(" --sRGB-16bit");
343 #define FORMAT_NO_CHANGE 0x80000000 /* additional flag */
345 /* A name table for all the formats - defines the format of the '+' arguments to
348 #define FORMAT_COUNT 64
349 #define FORMAT_MASK 0x3f
350 static PNG_CONST
char * PNG_CONST format_names
[FORMAT_COUNT
] =
361 "color-mapped-sRGB-gray",
362 "color-mapped-sRGB-gray+alpha",
363 "color-mapped-sRGB-rgb",
364 "color-mapped-sRGB-rgb+alpha",
365 "color-mapped-linear-gray",
366 "color-mapped-linear-gray+alpha",
367 "color-mapped-linear-rgb",
368 "color-mapped-linear-rgb+alpha",
379 "color-mapped-sRGB-gray",
380 "color-mapped-sRGB-gray+alpha",
381 "color-mapped-sRGB-bgr",
382 "color-mapped-sRGB-bgr+alpha",
383 "color-mapped-linear-gray",
384 "color-mapped-linear-gray+alpha",
385 "color-mapped-linear-bgr",
386 "color-mapped-linear-bgr+alpha",
397 "color-mapped-sRGB-gray",
398 "color-mapped-alpha+sRGB-gray",
399 "color-mapped-sRGB-rgb",
400 "color-mapped-alpha+sRGB-rgb",
401 "color-mapped-linear-gray",
402 "color-mapped-alpha+linear-gray",
403 "color-mapped-linear-rgb",
404 "color-mapped-alpha+linear-rgb",
415 "color-mapped-sRGB-gray",
416 "color-mapped-alpha+sRGB-gray",
417 "color-mapped-sRGB-bgr",
418 "color-mapped-alpha+sRGB-bgr",
419 "color-mapped-linear-gray",
420 "color-mapped-alpha+linear-gray",
421 "color-mapped-linear-bgr",
422 "color-mapped-alpha+linear-bgr",
425 /* Decode an argument to a format number. */
427 formatof(const char *arg
)
430 unsigned long format
= strtoul(arg
, &ep
, 0);
432 if (ep
> arg
&& *ep
== 0 && format
< FORMAT_COUNT
)
433 return (png_uint_32
)format
;
435 else for (format
=0; format
< FORMAT_COUNT
; ++format
)
437 if (strcmp(format_names
[format
], arg
) == 0)
438 return (png_uint_32
)format
;
441 fprintf(stderr
, "pngstest: format name '%s' invalid\n", arg
);
445 /* Bitset/test functions for formats */
446 #define FORMAT_SET_COUNT (FORMAT_COUNT / 32)
449 png_uint_32 bits
[FORMAT_SET_COUNT
];
453 static void format_init(format_list
*pf
)
456 for (i
=0; i
<FORMAT_SET_COUNT
; ++i
)
457 pf
->bits
[i
] = 0; /* All off */
460 #if 0 /* currently unused */
461 static void format_clear(format_list
*pf
)
464 for (i
=0; i
<FORMAT_SET_COUNT
; ++i
)
469 static int format_is_initial(format_list
*pf
)
472 for (i
=0; i
<FORMAT_SET_COUNT
; ++i
)
473 if (pf
->bits
[i
] != 0)
479 static int format_set(format_list
*pf
, png_uint_32 format
)
481 if (format
< FORMAT_COUNT
)
482 return pf
->bits
[format
>> 5] |= ((png_uint_32
)1) << (format
& 31);
487 #if 0 /* currently unused */
488 static int format_unset(format_list
*pf
, png_uint_32 format
)
490 if (format
< FORMAT_COUNT
)
491 return pf
->bits
[format
>> 5] &= ~((png_uint_32
)1) << (format
& 31);
497 static int format_isset(format_list
*pf
, png_uint_32 format
)
499 return format
< FORMAT_COUNT
&&
500 (pf
->bits
[format
>> 5] & (((png_uint_32
)1) << (format
& 31))) != 0;
503 static void format_default(format_list
*pf
, int redundant
)
509 /* set everything, including flags that are pointless */
510 for (i
=0; i
<FORMAT_SET_COUNT
; ++i
)
511 pf
->bits
[i
] = ~(png_uint_32
)0;
518 for (f
=0; f
<FORMAT_COUNT
; ++f
)
520 /* Eliminate redundant and unsupported settings. */
521 # ifdef PNG_FORMAT_BGR_SUPPORTED
522 /* BGR is meaningless if no color: */
523 if ((f
& PNG_FORMAT_FLAG_COLOR
) == 0 &&
524 (f
& PNG_FORMAT_FLAG_BGR
) != 0)
526 if ((f
& 0x10U
/*HACK: fixed value*/) != 0)
530 /* AFIRST is meaningless if no alpha: */
531 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
532 if ((f
& PNG_FORMAT_FLAG_ALPHA
) == 0 &&
533 (f
& PNG_FORMAT_FLAG_AFIRST
) != 0)
535 if ((f
& 0x20U
/*HACK: fixed value*/) != 0)
544 /* THE Image STRUCTURE */
545 /* The super-class of a png_image, contains the decoded image plus the input
546 * data necessary to re-read the file with a different format.
552 const char *file_name
;
555 png_voidp input_memory
;
556 png_size_t input_memory_size
;
560 png_size_t allocsize
;
561 char tmpfile_name
[32];
562 png_uint_16 colormap
[256*4];
566 /* Initializer: also sets the permitted error limit for 16-bit operations. */
568 newimage(Image
*image
)
570 memset(image
, 0, sizeof *image
);
573 /* Reset the image to be read again - only needs to rewind the FILE* at present.
576 resetimage(Image
*image
)
578 if (image
->input_file
!= NULL
)
579 rewind(image
->input_file
);
582 /* Free the image buffer; the buffer is re-used on a re-read, this is just for
586 freebuffer(Image
*image
)
588 if (image
->buffer
) free(image
->buffer
);
589 image
->buffer
= NULL
;
591 image
->allocsize
= 0;
594 /* Delete function; cleans out all the allocated data and the temporary file in
598 freeimage(Image
*image
)
601 png_image_free(&image
->image
);
603 if (image
->input_file
!= NULL
)
605 fclose(image
->input_file
);
606 image
->input_file
= NULL
;
609 if (image
->input_memory
!= NULL
)
611 free(image
->input_memory
);
612 image
->input_memory
= NULL
;
613 image
->input_memory_size
= 0;
616 if (image
->tmpfile_name
[0] != 0 && (image
->opts
& KEEP_TMPFILES
) == 0)
618 remove(image
->tmpfile_name
);
619 image
->tmpfile_name
[0] = 0;
623 /* This is actually a re-initializer; allows an image structure to be re-used by
624 * freeing everything that relates to an old image.
626 static void initimage(Image
*image
, png_uint_32 opts
, const char *file_name
,
630 memset(&image
->image
, 0, sizeof image
->image
);
632 image
->file_name
= file_name
;
633 image
->stride_extra
= stride_extra
;
636 /* Make sure the image buffer is big enough; allows re-use of the buffer if the
639 #define BUFFER_INIT8 73
641 allocbuffer(Image
*image
)
643 png_size_t size
= PNG_IMAGE_BUFFER_SIZE(image
->image
, image
->stride
);
645 if (size
+32 > image
->bufsize
)
648 image
->buffer
= voidcast(png_bytep
, malloc(size
+32));
649 if (image
->buffer
== NULL
)
653 "simpletest: out of memory allocating %lu(+32) byte buffer\n",
654 (unsigned long)size
);
657 image
->bufsize
= size
+32;
660 memset(image
->buffer
, 95, image
->bufsize
);
661 memset(image
->buffer
+16, BUFFER_INIT8
, size
);
662 image
->allocsize
= size
;
665 /* Make sure 16 bytes match the given byte. */
667 check16(png_const_bytep bp
, int b
)
672 if (*bp
!= b
) return 1;
678 /* Check for overwrite in the image buffer. */
680 checkbuffer(Image
*image
, const char *arg
)
682 if (check16(image
->buffer
, 95))
685 fprintf(stderr
, "%s: overwrite at start of image buffer\n", arg
);
689 if (check16(image
->buffer
+16+image
->allocsize
, 95))
692 fprintf(stderr
, "%s: overwrite at end of image buffer\n", arg
);
698 /* Log a terminal error, also frees the libpng part of the image if necessary.
701 logerror(Image
*image
, const char *a1
, const char *a2
, const char *a3
)
704 if (image
->image
.warning_or_error
)
705 fprintf(stderr
, "%s%s%s: %s\n", a1
, a2
, a3
, image
->image
.message
);
708 fprintf(stderr
, "%s%s%s\n", a1
, a2
, a3
);
710 if (image
->image
.opaque
!= NULL
)
712 fprintf(stderr
, "%s: image opaque pointer non-NULL on error\n",
714 png_image_free(&image
->image
);
720 /* Log an error and close a file (just a utility to do both things in one
724 logclose(Image
*image
, FILE *f
, const char *name
, const char *operation
)
729 return logerror(image
, name
, operation
, strerror(e
));
732 /* Make sure the png_image has been freed - validates that libpng is doing what
733 * the spec says and freeing the image.
736 checkopaque(Image
*image
)
738 if (image
->image
.opaque
!= NULL
)
740 png_image_free(&image
->image
);
741 return logerror(image
, image
->file_name
, ": opaque not NULL", "");
744 else if (image
->image
.warning_or_error
!= 0 && (image
->opts
& STRICT
) != 0)
745 return logerror(image
, image
->file_name
, " --strict", "");
751 /* IMAGE COMPARISON/CHECKING */
752 /* Compare the pixels of two images, which should be the same but aren't. The
753 * images must have been checked for a size match.
757 /* The components, for grayscale images the gray value is in 'g' and if alpha
758 * is not present 'a' is set to 255 or 65535 according to format.
765 /* The background as the original sRGB 8-bit value converted to the final
766 * integer format and as a double precision linear value in the range 0..1
767 * for with partially transparent pixels.
770 double dr
, dg
, db
; /* linear r,g,b scaled to 0..1 */
773 /* Basic image formats; control the data but not the layout thereof. */
774 #define BASE_FORMATS\
775 (PNG_FORMAT_FLAG_ALPHA|PNG_FORMAT_FLAG_COLOR|PNG_FORMAT_FLAG_LINEAR)
777 /* Read a Pixel from a buffer. The code below stores the correct routine for
778 * the format in a function pointer, these are the routines:
781 gp_g8(Pixel
*p
, png_const_voidp pb
)
783 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
785 p
->r
= p
->g
= p
->b
= pp
[0];
790 gp_ga8(Pixel
*p
, png_const_voidp pb
)
792 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
794 p
->r
= p
->g
= p
->b
= pp
[0];
798 #ifdef PNG_FORMAT_AFIRST_SUPPORTED
800 gp_ag8(Pixel
*p
, png_const_voidp pb
)
802 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
804 p
->r
= p
->g
= p
->b
= pp
[1];
810 gp_rgb8(Pixel
*p
, png_const_voidp pb
)
812 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
820 #ifdef PNG_FORMAT_BGR_SUPPORTED
822 gp_bgr8(Pixel
*p
, png_const_voidp pb
)
824 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
834 gp_rgba8(Pixel
*p
, png_const_voidp pb
)
836 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
844 #ifdef PNG_FORMAT_BGR_SUPPORTED
846 gp_bgra8(Pixel
*p
, png_const_voidp pb
)
848 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
857 #ifdef PNG_FORMAT_AFIRST_SUPPORTED
859 gp_argb8(Pixel
*p
, png_const_voidp pb
)
861 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
870 #if defined(PNG_FORMAT_AFIRST_SUPPORTED) && defined(PNG_FORMAT_BGR_SUPPORTED)
872 gp_abgr8(Pixel
*p
, png_const_voidp pb
)
874 png_const_bytep pp
= voidcast(png_const_bytep
, pb
);
884 gp_g16(Pixel
*p
, png_const_voidp pb
)
886 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
888 p
->r
= p
->g
= p
->b
= pp
[0];
893 gp_ga16(Pixel
*p
, png_const_voidp pb
)
895 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
897 p
->r
= p
->g
= p
->b
= pp
[0];
901 #ifdef PNG_FORMAT_AFIRST_SUPPORTED
903 gp_ag16(Pixel
*p
, png_const_voidp pb
)
905 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
907 p
->r
= p
->g
= p
->b
= pp
[1];
913 gp_rgb16(Pixel
*p
, png_const_voidp pb
)
915 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
923 #ifdef PNG_FORMAT_BGR_SUPPORTED
925 gp_bgr16(Pixel
*p
, png_const_voidp pb
)
927 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
937 gp_rgba16(Pixel
*p
, png_const_voidp pb
)
939 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
947 #ifdef PNG_FORMAT_BGR_SUPPORTED
949 gp_bgra16(Pixel
*p
, png_const_voidp pb
)
951 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
960 #ifdef PNG_FORMAT_AFIRST_SUPPORTED
962 gp_argb16(Pixel
*p
, png_const_voidp pb
)
964 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
973 #if defined(PNG_FORMAT_AFIRST_SUPPORTED) && defined(PNG_FORMAT_BGR_SUPPORTED)
975 gp_abgr16(Pixel
*p
, png_const_voidp pb
)
977 png_const_uint_16p pp
= voidcast(png_const_uint_16p
, pb
);
986 /* Given a format, return the correct one of the above functions. */
988 get_pixel(png_uint_32 format
))(Pixel
*p
, png_const_voidp pb
)
990 /* The color-map flag is irrelevant here - the caller of the function
991 * returned must either pass the buffer or, for a color-mapped image, the
992 * correct entry in the color-map.
994 if (format
& PNG_FORMAT_FLAG_LINEAR
)
996 if (format
& PNG_FORMAT_FLAG_COLOR
)
998 # ifdef PNG_FORMAT_BGR_SUPPORTED
999 if (format
& PNG_FORMAT_FLAG_BGR
)
1001 if (format
& PNG_FORMAT_FLAG_ALPHA
)
1003 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1004 if (format
& PNG_FORMAT_FLAG_AFIRST
)
1019 if (format
& PNG_FORMAT_FLAG_ALPHA
)
1021 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1022 if (format
& PNG_FORMAT_FLAG_AFIRST
)
1037 if (format
& PNG_FORMAT_FLAG_ALPHA
)
1039 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1040 if (format
& PNG_FORMAT_FLAG_AFIRST
)
1055 if (format
& PNG_FORMAT_FLAG_COLOR
)
1057 # ifdef PNG_FORMAT_BGR_SUPPORTED
1058 if (format
& PNG_FORMAT_FLAG_BGR
)
1060 if (format
& PNG_FORMAT_FLAG_ALPHA
)
1062 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1063 if (format
& PNG_FORMAT_FLAG_AFIRST
)
1078 if (format
& PNG_FORMAT_FLAG_ALPHA
)
1080 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1081 if (format
& PNG_FORMAT_FLAG_AFIRST
)
1096 if (format
& PNG_FORMAT_FLAG_ALPHA
)
1098 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
1099 if (format
& PNG_FORMAT_FLAG_AFIRST
)
1113 /* Convertion between pixel formats. The code above effectively eliminates the
1114 * component ordering changes leaving three basic changes:
1116 * 1) Remove an alpha channel by pre-multiplication or compositing on a
1117 * background color. (Adding an alpha channel is a no-op.)
1119 * 2) Remove color by mapping to grayscale. (Grayscale to color is a no-op.)
1121 * 3) Convert between 8-bit and 16-bit components. (Both directtions are
1124 * This gives the following base format conversion matrix:
1126 * OUT: ----- 8-bit ----- ----- 16-bit -----
1127 * IN G GA RGB RGBA G GA RGB RGBA
1128 * 8 G . . . . lin lin lin lin
1129 * 8 GA bckg . bckc . pre' pre pre' pre
1130 * 8 RGB g8 g8 . . glin glin lin lin
1131 * 8 RGBA g8b g8 bckc . gpr' gpre pre' pre
1132 * 16 G sRGB sRGB sRGB sRGB . . . .
1133 * 16 GA b16g unpg b16c unpc A . A .
1134 * 16 RGB sG sG sRGB sRGB g16 g16 . .
1135 * 16 RGBA gb16 sGp cb16 sCp g16 g16' A .
1138 * bckg: composite on gray background
1139 * bckc: composite on color background
1140 * g8: convert sRGB components to sRGB grayscale
1141 * g8b: convert sRGB components to grayscale and composite on gray background
1144 * lin: make sRGB components linear, alpha := 65535
1145 * pre: make sRGB components linear and premultiply by alpha (scale alpha)
1146 * pre': as 'pre' but alpha := 65535
1147 * glin: make sRGB components linear, convert to grayscale, alpha := 65535
1148 * gpre: make sRGB components grayscale and linear and premultiply by alpha
1149 * gpr': as 'gpre' but alpha := 65535
1152 * sRGB: convert linear components to sRGB, alpha := 255
1153 * unpg: unpremultiply gray component and convert to sRGB (scale alpha)
1154 * unpc: unpremultiply color components and convert to sRGB (scale alpha)
1155 * b16g: composite linear onto gray background and convert the result to sRGB
1156 * b16c: composite linear onto color background and convert the result to sRGB
1157 * sG: convert linear RGB to sRGB grayscale
1158 * sGp: unpremultiply RGB then convert to sRGB grayscale
1159 * sCp: unpremultiply RGB then convert to sRGB
1160 * gb16: composite linear onto background and convert to sRGB grayscale
1161 * (order doesn't matter, the composite and grayscale operations permute)
1162 * cb16: composite linear onto background and convert to sRGB
1165 * A: set alpha to 65535
1166 * g16: convert linear RGB to linear grayscale (alpha := 65535)
1167 * g16': as 'g16' but alpha is unchanged
1171 gpc_noop(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1180 #if ALLOW_UNUSED_GPC
1182 gpc_nop8(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1186 out
->r
= out
->g
= out
->b
= 255;
1199 #if ALLOW_UNUSED_GPC
1201 gpc_nop6(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1205 out
->r
= out
->g
= out
->b
= 65535;
1218 /* 8-bit to 8-bit conversions */
1219 /* bckg: composite on gray background */
1221 gpc_bckg(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1224 out
->r
= out
->g
= out
->b
= back
->ig
;
1226 else if (in
->a
>= 255)
1227 out
->r
= out
->g
= out
->b
= in
->g
;
1231 double a
= in
->a
/ 255.;
1233 out
->r
= out
->g
= out
->b
= sRGB(sRGB_to_d
[in
->g
] * a
+ back
->dg
* (1-a
));
1239 /* bckc: composite on color background */
1241 gpc_bckc(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1250 else if (in
->a
>= 255)
1259 double a
= in
->a
/ 255.;
1261 out
->r
= sRGB(sRGB_to_d
[in
->r
] * a
+ back
->dr
* (1-a
));
1262 out
->g
= sRGB(sRGB_to_d
[in
->g
] * a
+ back
->dg
* (1-a
));
1263 out
->b
= sRGB(sRGB_to_d
[in
->b
] * a
+ back
->db
* (1-a
));
1269 /* g8: convert sRGB components to sRGB grayscale */
1271 gpc_g8(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1275 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1276 out
->r
= out
->g
= out
->b
= in
->g
;
1279 out
->r
= out
->g
= out
->b
=
1280 sRGB(YfromRGB(sRGB_to_d
[in
->r
], sRGB_to_d
[in
->g
], sRGB_to_d
[in
->b
]));
1285 /* g8b: convert sRGB components to grayscale and composite on gray background */
1287 gpc_g8b(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1290 out
->r
= out
->g
= out
->b
= back
->ig
;
1292 else if (in
->a
>= 255)
1294 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1295 out
->r
= out
->g
= out
->b
= in
->g
;
1298 out
->r
= out
->g
= out
->b
= sRGB(YfromRGB(
1299 sRGB_to_d
[in
->r
], sRGB_to_d
[in
->g
], sRGB_to_d
[in
->b
]));
1304 double a
= in
->a
/255.;
1306 out
->r
= out
->g
= out
->b
= sRGB(a
* YfromRGB(sRGB_to_d
[in
->r
],
1307 sRGB_to_d
[in
->g
], sRGB_to_d
[in
->b
]) + back
->dg
* (1-a
));
1313 /* 8-bit to 16-bit conversions */
1314 /* lin: make sRGB components linear, alpha := 65535 */
1316 gpc_lin(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1320 out
->r
= ilinear(in
->r
);
1330 out
->b
= ilinear(in
->b
);
1335 out
->g
= ilinear(in
->g
);
1340 else if (in
->b
== in
->g
)
1344 out
->b
= ilinear(in
->b
);
1350 /* pre: make sRGB components linear and premultiply by alpha (scale alpha) */
1352 gpc_pre(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1356 out
->r
= ilineara(in
->r
, in
->a
);
1366 out
->b
= ilineara(in
->b
, in
->a
);
1371 out
->g
= ilineara(in
->g
, in
->a
);
1376 else if (in
->b
== in
->g
)
1380 out
->b
= ilineara(in
->b
, in
->a
);
1383 out
->a
= in
->a
* 257;
1386 /* pre': as 'pre' but alpha := 65535 */
1388 gpc_preq(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1392 out
->r
= ilineara(in
->r
, in
->a
);
1402 out
->b
= ilineara(in
->b
, in
->a
);
1407 out
->g
= ilineara(in
->g
, in
->a
);
1412 else if (in
->b
== in
->g
)
1416 out
->b
= ilineara(in
->b
, in
->a
);
1422 /* glin: make sRGB components linear, convert to grayscale, alpha := 65535 */
1424 gpc_glin(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1428 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1429 out
->r
= out
->g
= out
->b
= ilinear(in
->g
);
1432 out
->r
= out
->g
= out
->b
= u16d(65535 *
1433 YfromRGB(sRGB_to_d
[in
->r
], sRGB_to_d
[in
->g
], sRGB_to_d
[in
->b
]));
1438 /* gpre: make sRGB components grayscale and linear and premultiply by alpha */
1440 gpc_gpre(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1444 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1445 out
->r
= out
->g
= out
->b
= ilineara(in
->g
, in
->a
);
1448 out
->r
= out
->g
= out
->b
= u16d(in
->a
* 257 *
1449 YfromRGB(sRGB_to_d
[in
->r
], sRGB_to_d
[in
->g
], sRGB_to_d
[in
->b
]));
1451 out
->a
= 257 * in
->a
;
1454 /* gpr': as 'gpre' but alpha := 65535 */
1456 gpc_gprq(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
= ilineara(in
->g
, in
->a
);
1464 out
->r
= out
->g
= out
->b
= u16d(in
->a
* 257 *
1465 YfromRGB(sRGB_to_d
[in
->r
], sRGB_to_d
[in
->g
], sRGB_to_d
[in
->b
]));
1470 /* 8-bit to 16-bit conversions for gAMA 45455 encoded values */
1471 /* Lin: make gAMA 45455 components linear, alpha := 65535 */
1473 gpc_Lin(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1477 out
->r
= ilinear_g22(in
->r
);
1487 out
->b
= ilinear_g22(in
->b
);
1492 out
->g
= ilinear_g22(in
->g
);
1497 else if (in
->b
== in
->g
)
1501 out
->b
= ilinear_g22(in
->b
);
1507 #if ALLOW_UNUSED_GPC
1508 /* Pre: make gAMA 45455 components linear and premultiply by alpha (scale alpha)
1511 gpc_Pre(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1515 out
->r
= ilineara_g22(in
->r
, in
->a
);
1525 out
->b
= ilineara_g22(in
->b
, in
->a
);
1530 out
->g
= ilineara_g22(in
->g
, in
->a
);
1535 else if (in
->b
== in
->g
)
1539 out
->b
= ilineara_g22(in
->b
, in
->a
);
1542 out
->a
= in
->a
* 257;
1546 #if ALLOW_UNUSED_GPC
1547 /* Pre': as 'Pre' but alpha := 65535 */
1549 gpc_Preq(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1553 out
->r
= ilineara_g22(in
->r
, in
->a
);
1563 out
->b
= ilineara_g22(in
->b
, in
->a
);
1568 out
->g
= ilineara_g22(in
->g
, in
->a
);
1573 else if (in
->b
== in
->g
)
1577 out
->b
= ilineara_g22(in
->b
, in
->a
);
1584 #if ALLOW_UNUSED_GPC
1585 /* Glin: make gAMA 45455 components linear, convert to grayscale, alpha := 65535
1588 gpc_Glin(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1592 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1593 out
->r
= out
->g
= out
->b
= ilinear_g22(in
->g
);
1596 out
->r
= out
->g
= out
->b
= u16d(65535 *
1597 YfromRGB(g22_to_d
[in
->r
], g22_to_d
[in
->g
], g22_to_d
[in
->b
]));
1603 #if ALLOW_UNUSED_GPC
1604 /* Gpre: make gAMA 45455 components grayscale and linear and premultiply by
1608 gpc_Gpre(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1612 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1613 out
->r
= out
->g
= out
->b
= ilineara_g22(in
->g
, in
->a
);
1616 out
->r
= out
->g
= out
->b
= u16d(in
->a
* 257 *
1617 YfromRGB(g22_to_d
[in
->r
], g22_to_d
[in
->g
], g22_to_d
[in
->b
]));
1619 out
->a
= 257 * in
->a
;
1623 #if ALLOW_UNUSED_GPC
1624 /* Gpr': as 'Gpre' but alpha := 65535 */
1626 gpc_Gprq(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1630 if (in
->r
== in
->g
&& in
->g
== in
->b
)
1631 out
->r
= out
->g
= out
->b
= ilineara_g22(in
->g
, in
->a
);
1634 out
->r
= out
->g
= out
->b
= u16d(in
->a
* 257 *
1635 YfromRGB(g22_to_d
[in
->r
], g22_to_d
[in
->g
], g22_to_d
[in
->b
]));
1641 /* 16-bit to 8-bit conversions */
1642 /* sRGB: convert linear components to sRGB, alpha := 255 */
1644 gpc_sRGB(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1648 out
->r
= isRGB(in
->r
);
1658 out
->b
= isRGB(in
->b
);
1663 out
->g
= isRGB(in
->g
);
1668 else if (in
->b
== in
->g
)
1672 out
->b
= isRGB(in
->b
);
1678 /* unpg: unpremultiply gray component and convert to sRGB (scale alpha) */
1680 gpc_unpg(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1686 out
->r
= out
->g
= out
->b
= 255;
1692 out
->r
= out
->g
= out
->b
= sRGB((double)in
->g
/ in
->a
);
1693 out
->a
= u8d(in
->a
/ 257.);
1697 /* unpc: unpremultiply color components and convert to sRGB (scale alpha) */
1699 gpc_unpc(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1705 out
->r
= out
->g
= out
->b
= 255;
1711 out
->r
= sRGB((double)in
->r
/ in
->a
);
1712 out
->g
= sRGB((double)in
->g
/ in
->a
);
1713 out
->b
= sRGB((double)in
->b
/ in
->a
);
1714 out
->a
= u8d(in
->a
/ 257.);
1718 /* b16g: composite linear onto gray background and convert the result to sRGB */
1720 gpc_b16g(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1723 out
->r
= out
->g
= out
->b
= back
->ig
;
1727 double a
= in
->a
/65535.;
1731 out
->r
= out
->g
= out
->b
= sRGB(in
->g
* a
+ back
->dg
* a1
);
1737 /* b16c: composite linear onto color background and convert the result to sRGB*/
1739 gpc_b16c(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1750 double a
= in
->a
/65535.;
1754 out
->r
= sRGB(in
->r
* a
+ back
->dr
* a1
);
1755 out
->g
= sRGB(in
->g
* a
+ back
->dg
* a1
);
1756 out
->b
= sRGB(in
->b
* a
+ back
->db
* a1
);
1762 /* sG: convert linear RGB to sRGB grayscale */
1764 gpc_sG(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1768 out
->r
= out
->g
= out
->b
= sRGB(YfromRGBint(in
->r
, in
->g
, in
->b
)/65535);
1772 /* sGp: unpremultiply RGB then convert to sRGB grayscale */
1774 gpc_sGp(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1780 out
->r
= out
->g
= out
->b
= 255;
1786 out
->r
= out
->g
= out
->b
= sRGB(YfromRGBint(in
->r
, in
->g
, in
->b
)/in
->a
);
1787 out
->a
= u8d(in
->a
/ 257.);
1791 /* sCp: unpremultiply RGB then convert to sRGB */
1793 gpc_sCp(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1799 out
->r
= out
->g
= out
->b
= 255;
1805 out
->r
= sRGB((double)in
->r
/ in
->a
);
1806 out
->g
= sRGB((double)in
->g
/ in
->a
);
1807 out
->b
= sRGB((double)in
->b
/ in
->a
);
1808 out
->a
= u8d(in
->a
/ 257.);
1812 /* gb16: composite linear onto background and convert to sRGB grayscale */
1813 /* (order doesn't matter, the composite and grayscale operations permute) */
1815 gpc_gb16(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1818 out
->r
= out
->g
= out
->b
= back
->ig
;
1820 else if (in
->a
>= 65535)
1821 out
->r
= out
->g
= out
->b
= isRGB(in
->g
);
1825 double a
= in
->a
/ 65535.;
1829 out
->r
= out
->g
= out
->b
= sRGB(in
->g
* a
+ back
->dg
* a1
);
1835 /* cb16: composite linear onto background and convert to sRGB */
1837 gpc_cb16(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1846 else if (in
->a
>= 65535)
1848 out
->r
= isRGB(in
->r
);
1849 out
->g
= isRGB(in
->g
);
1850 out
->b
= isRGB(in
->b
);
1855 double a
= in
->a
/ 65535.;
1859 out
->r
= sRGB(in
->r
* a
+ back
->dr
* a1
);
1860 out
->g
= sRGB(in
->g
* a
+ back
->dg
* a1
);
1861 out
->b
= sRGB(in
->b
* a
+ back
->db
* a1
);
1867 /* 16-bit to 16-bit conversions */
1868 /* A: set alpha to 65535 */
1870 gpc_A(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1879 /* g16: convert linear RGB to linear grayscale (alpha := 65535) */
1881 gpc_g16(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1884 out
->r
= out
->g
= out
->b
= u16d(YfromRGBint(in
->r
, in
->g
, in
->b
));
1888 /* g16': as 'g16' but alpha is unchanged */
1890 gpc_g16q(Pixel
*out
, const Pixel
*in
, const Background
*back
)
1893 out
->r
= out
->g
= out
->b
= u16d(YfromRGBint(in
->r
, in
->g
, in
->b
));
1897 #if ALLOW_UNUSED_GPC
1898 /* Unused functions (to hide them from GCC unused function warnings) */
1899 void (* const gpc_unused
[])
1900 (Pixel
*out
, const Pixel
*in
, const Background
*back
) =
1902 gpc_Pre
, gpc_Preq
, gpc_Glin
, gpc_Gpre
, gpc_Gprq
, gpc_nop8
, gpc_nop6
1906 /* OUT: ----- 8-bit ----- ----- 16-bit -----
1907 * IN G GA RGB RGBA G GA RGB RGBA
1908 * 8 G . . . . lin lin lin lin
1909 * 8 GA bckg . bckc . pre' pre pre' pre
1910 * 8 RGB g8 g8 . . glin glin lin lin
1911 * 8 RGBA g8b g8 bckc . gpr' gpre pre' pre
1912 * 16 G sRGB sRGB sRGB sRGB . . . .
1913 * 16 GA b16g unpg b16c unpc A . A .
1914 * 16 RGB sG sG sRGB sRGB g16 g16 . .
1915 * 16 RGBA gb16 sGp cb16 sCp g16 g16' A .
1917 * The matrix is held in an array indexed thus:
1919 * gpc_fn[out_format & BASE_FORMATS][in_format & BASE_FORMATS];
1921 /* This will produce a compile time error if the FORMAT_FLAG values don't
1922 * match the above matrix!
1924 #if PNG_FORMAT_FLAG_ALPHA == 1 && PNG_FORMAT_FLAG_COLOR == 2 &&\
1925 PNG_FORMAT_FLAG_LINEAR == 4
1926 static void (* const gpc_fn
[8/*in*/][8/*out*/])
1927 (Pixel
*out
, const Pixel
*in
, const Background
*back
) =
1929 /*out: G-8 GA-8 RGB-8 RGBA-8 G-16 GA-16 RGB-16 RGBA-16 */
1930 {gpc_noop
,gpc_noop
,gpc_noop
,gpc_noop
, gpc_Lin
, gpc_Lin
, gpc_Lin
, gpc_Lin
},
1931 {gpc_bckg
,gpc_noop
,gpc_bckc
,gpc_noop
, gpc_preq
,gpc_pre
, gpc_preq
,gpc_pre
},
1932 {gpc_g8
, gpc_g8
, gpc_noop
,gpc_noop
, gpc_glin
,gpc_glin
,gpc_lin
, gpc_lin
},
1933 {gpc_g8b
, gpc_g8
, gpc_bckc
,gpc_noop
, gpc_gprq
,gpc_gpre
,gpc_preq
,gpc_pre
},
1934 {gpc_sRGB
,gpc_sRGB
,gpc_sRGB
,gpc_sRGB
, gpc_noop
,gpc_noop
,gpc_noop
,gpc_noop
},
1935 {gpc_b16g
,gpc_unpg
,gpc_b16c
,gpc_unpc
, gpc_A
, gpc_noop
,gpc_A
, gpc_noop
},
1936 {gpc_sG
, gpc_sG
, gpc_sRGB
,gpc_sRGB
, gpc_g16
, gpc_g16
, gpc_noop
,gpc_noop
},
1937 {gpc_gb16
,gpc_sGp
, gpc_cb16
,gpc_sCp
, gpc_g16
, gpc_g16q
,gpc_A
, gpc_noop
}
1940 /* The array is repeated for the cases where both the input and output are color
1941 * mapped because then different algorithms are used.
1943 static void (* const gpc_fn_colormapped
[8/*in*/][8/*out*/])
1944 (Pixel
*out
, const Pixel
*in
, const Background
*back
) =
1946 /*out: G-8 GA-8 RGB-8 RGBA-8 G-16 GA-16 RGB-16 RGBA-16 */
1947 {gpc_noop
,gpc_noop
,gpc_noop
,gpc_noop
, gpc_lin
, gpc_lin
, gpc_lin
, gpc_lin
},
1948 {gpc_bckg
,gpc_noop
,gpc_bckc
,gpc_noop
, gpc_preq
,gpc_pre
, gpc_preq
,gpc_pre
},
1949 {gpc_g8
, gpc_g8
, gpc_noop
,gpc_noop
, gpc_glin
,gpc_glin
,gpc_lin
, gpc_lin
},
1950 {gpc_g8b
, gpc_g8
, gpc_bckc
,gpc_noop
, gpc_gprq
,gpc_gpre
,gpc_preq
,gpc_pre
},
1951 {gpc_sRGB
,gpc_sRGB
,gpc_sRGB
,gpc_sRGB
, gpc_noop
,gpc_noop
,gpc_noop
,gpc_noop
},
1952 {gpc_b16g
,gpc_unpg
,gpc_b16c
,gpc_unpc
, gpc_A
, gpc_noop
,gpc_A
, gpc_noop
},
1953 {gpc_sG
, gpc_sG
, gpc_sRGB
,gpc_sRGB
, gpc_g16
, gpc_g16
, gpc_noop
,gpc_noop
},
1954 {gpc_gb16
,gpc_sGp
, gpc_cb16
,gpc_sCp
, gpc_g16
, gpc_g16q
,gpc_A
, gpc_noop
}
1957 /* The error arrays record the error in the same matrix; 64 entries, however
1958 * the different algorithms used in libpng for colormap and direct conversions
1959 * mean that four separate matrices are used (for each combination of
1960 * colormapped and direct.)
1962 * In some cases the conversion between sRGB formats goes via a linear
1963 * intermediate; an sRGB to linear conversion (as above) is followed by a simple
1964 * linear to sRGB step with no other conversions. This is done by a separate
1965 * error array from an arbitrary 'in' format to one of the four basic outputs
1966 * (since final output is always sRGB not colormapped).
1968 * These arrays may be modified if the --accumulate flag is set during the run;
1969 * then instead of logging errors they are simply added in.
1971 * The three entries are currently for transparent, partially transparent and
1972 * opaque input pixel values. Notice that alpha should be exact in each case.
1974 * Errors in alpha should only occur when converting from a direct format
1975 * to a colormapped format, when alpha is effectively smashed (so large
1976 * errors can occur.) There should be no error in the '0' and 'opaque'
1977 * values. The fourth entry in the array is used for the alpha error (and it
1978 * should always be zero for the 'via linear' case since this is never color
1981 * Mapping to a colormap smashes the colors, it is necessary to have separate
1982 * values for these cases because they are much larger; it is very much
1983 * impossible to obtain a reasonable result, these are held in
1984 * gpc_error_to_colormap.
1986 #if PNG_FORMAT_FLAG_COLORMAP == 8 /* extra check also required */
1987 /* START MACHINE GENERATED */
1988 static png_uint_16 gpc_error
[16/*in*/][16/*out*/][4/*a*/] =
1990 { /* input: sRGB-gray */
1991 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
1992 { 0, 0, 372, 0 }, { 0, 0, 372, 0 }, { 0, 0, 372, 0 }, { 0, 0, 372, 0 },
1993 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
1994 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
1995 }, { /* input: sRGB-gray+alpha */
1996 { 0, 18, 0, 0 }, { 0, 0, 0, 0 }, { 0, 20, 0, 0 }, { 0, 0, 0, 0 },
1997 { 0, 897, 788, 0 }, { 0, 897, 788, 0 }, { 0, 897, 788, 0 }, { 0, 897, 788, 0 },
1998 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
1999 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2000 }, { /* input: sRGB-rgb */
2001 { 0, 0, 19, 0 }, { 0, 0, 19, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2002 { 0, 0, 893, 0 }, { 0, 0, 893, 0 }, { 0, 0, 811, 0 }, { 0, 0, 811, 0 },
2003 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2004 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2005 }, { /* input: sRGB-rgb+alpha */
2006 { 0, 4, 13, 0 }, { 0, 14, 13, 0 }, { 0, 19, 0, 0 }, { 0, 0, 0, 0 },
2007 { 0, 832, 764, 0 }, { 0, 832, 764, 0 }, { 0, 897, 788, 0 }, { 0, 897, 788, 0 },
2008 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2009 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2010 }, { /* input: linear-gray */
2011 { 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 },
2012 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2013 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2014 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2015 }, { /* input: linear-gray+alpha */
2016 { 0, 74, 9, 0 }, { 0, 20, 9, 0 }, { 0, 74, 9, 0 }, { 0, 20, 9, 0 },
2017 { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 0, 0 },
2018 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2019 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2020 }, { /* input: linear-rgb */
2021 { 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 },
2022 { 0, 0, 4, 0 }, { 0, 0, 4, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2023 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2024 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2025 }, { /* input: linear-rgb+alpha */
2026 { 0, 126, 143, 0 }, { 0, 9, 7, 0 }, { 0, 74, 9, 0 }, { 0, 16, 9, 0 },
2027 { 0, 4, 4, 0 }, { 0, 5, 4, 0 }, { 0, 0, 0, 0 }, { 0, 1, 0, 0 },
2028 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2029 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2030 }, { /* input: color-mapped-sRGB-gray */
2031 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2032 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2033 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2034 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2035 }, { /* input: color-mapped-sRGB-gray+alpha */
2036 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2037 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2038 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2039 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2040 }, { /* input: color-mapped-sRGB-rgb */
2041 { 0, 0, 13, 0 }, { 0, 0, 13, 0 }, { 0, 0, 8, 0 }, { 0, 0, 8, 0 },
2042 { 0, 0, 673, 0 }, { 0, 0, 673, 0 }, { 0, 0, 674, 0 }, { 0, 0, 674, 0 },
2043 { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2044 { 0, 0, 460, 0 }, { 0, 0, 460, 0 }, { 0, 0, 263, 0 }, { 0, 0, 263, 0 }
2045 }, { /* input: color-mapped-sRGB-rgb+alpha */
2046 { 0, 6, 8, 0 }, { 0, 7, 8, 0 }, { 0, 75, 8, 0 }, { 0, 9, 8, 0 },
2047 { 0, 585, 427, 0 }, { 0, 585, 427, 0 }, { 0, 717, 409, 0 }, { 0, 717, 409, 0 },
2048 { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 },
2049 { 0, 13323, 460, 0 }, { 0, 334, 460, 0 }, { 0, 16480, 263, 0 }, { 0, 243, 263, 0 }
2050 }, { /* input: color-mapped-linear-gray */
2051 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2052 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2053 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2054 { 0, 0, 282, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2055 }, { /* input: color-mapped-linear-gray+alpha */
2056 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2057 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2058 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2059 { 0, 0, 0, 0 }, { 0, 253, 282, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2060 }, { /* input: color-mapped-linear-rgb */
2061 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2062 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2063 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2064 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 265, 0 }, { 0, 0, 0, 0 }
2065 }, { /* input: color-mapped-linear-rgb+alpha */
2066 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2067 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2068 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
2069 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 243, 265, 0 }
2072 static png_uint_16 gpc_error_via_linear
[16][4/*out*/][4] =
2074 { /* input: sRGB-gray */
2075 { 0, 0, 7, 0 }, { 0, 0, 7, 0 }, { 0, 0, 7, 0 }, { 0, 0, 7, 0 }
2076 }, { /* input: sRGB-gray+alpha */
2077 { 0, 15, 15, 0 }, { 0, 186, 15, 0 }, { 0, 15, 15, 0 }, { 0, 186, 15, 0 }
2078 }, { /* input: sRGB-rgb */
2079 { 0, 0, 19, 0 }, { 0, 0, 19, 0 }, { 0, 0, 15, 0 }, { 0, 0, 15, 0 }
2080 }, { /* input: sRGB-rgb+alpha */
2081 { 0, 12, 14, 0 }, { 0, 180, 14, 0 }, { 0, 14, 15, 0 }, { 0, 186, 15, 0 }
2082 }, { /* input: linear-gray */
2083 { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }
2084 }, { /* input: linear-gray+alpha */
2085 { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }
2086 }, { /* input: linear-rgb */
2087 { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }
2088 }, { /* input: linear-rgb+alpha */
2089 { 0, 1, 1, 0 }, { 0, 8, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }
2090 }, { /* input: color-mapped-sRGB-gray */
2091 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2092 }, { /* input: color-mapped-sRGB-gray+alpha */
2093 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2094 }, { /* input: color-mapped-sRGB-rgb */
2095 { 0, 0, 13, 0 }, { 0, 0, 13, 0 }, { 0, 0, 14, 0 }, { 0, 0, 14, 0 }
2096 }, { /* input: color-mapped-sRGB-rgb+alpha */
2097 { 0, 4, 8, 0 }, { 0, 9, 8, 0 }, { 0, 8, 3, 0 }, { 0, 32, 3, 0 }
2098 }, { /* input: color-mapped-linear-gray */
2099 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2100 }, { /* input: color-mapped-linear-gray+alpha */
2101 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2102 }, { /* input: color-mapped-linear-rgb */
2103 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2104 }, { /* input: color-mapped-linear-rgb+alpha */
2105 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
2108 static png_uint_16 gpc_error_to_colormap
[8/*i*/][8/*o*/][4] =
2110 { /* input: sRGB-gray */
2111 { 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 0, 0, 9, 0 },
2112 { 0, 0, 560, 0 }, { 0, 0, 560, 0 }, { 0, 0, 560, 0 }, { 0, 0, 560, 0 }
2113 }, { /* input: sRGB-gray+alpha */
2114 { 0, 19, 2, 0 }, { 0, 255, 2, 25 }, { 0, 88, 2, 0 }, { 0, 255, 2, 25 },
2115 { 0, 1012, 745, 0 }, { 0, 16026, 745, 6425 }, { 0, 1012, 745, 0 }, { 0, 16026, 745, 6425 }
2116 }, { /* input: sRGB-rgb */
2117 { 0, 0, 19, 0 }, { 0, 0, 19, 0 }, { 0, 0, 25, 0 }, { 0, 0, 25, 0 },
2118 { 0, 0, 937, 0 }, { 0, 0, 937, 0 }, { 0, 0, 13677, 0 }, { 0, 0, 13677, 0 }
2119 }, { /* input: sRGB-rgb+alpha */
2120 { 0, 63, 77, 0 }, { 0, 255, 19, 25 }, { 0, 220, 25, 0 }, { 0, 255, 25, 67 },
2121 { 0, 17534, 18491, 0 }, { 0, 15614, 2824, 6425 }, { 0, 14019, 13677, 0 }, { 0, 48573, 13677, 17219 }
2122 }, { /* input: linear-gray */
2123 { 0, 0, 73, 0 }, { 0, 0, 73, 0 }, { 0, 0, 73, 0 }, { 0, 0, 73, 0 },
2124 { 0, 0, 18817, 0 }, { 0, 0, 18817, 0 }, { 0, 0, 18817, 0 }, { 0, 0, 18817, 0 }
2125 }, { /* input: linear-gray+alpha */
2126 { 0, 74, 74, 0 }, { 0, 255, 74, 25 }, { 0, 97, 74, 0 }, { 0, 255, 74, 25 },
2127 { 0, 18919, 18907, 0 }, { 0, 24549, 18907, 6552 }, { 0, 18919, 18907, 0 }, { 0, 24549, 18907, 6552 }
2128 }, { /* input: linear-rgb */
2129 { 0, 0, 73, 0 }, { 0, 0, 73, 0 }, { 0, 0, 98, 0 }, { 0, 0, 98, 0 },
2130 { 0, 0, 18664, 0 }, { 0, 0, 18664, 0 }, { 0, 0, 24998, 0 }, { 0, 0, 24998, 0 }
2131 }, { /* input: linear-rgb+alpha */
2132 { 0, 181, 196, 0 }, { 0, 255, 61, 25 }, { 206, 187, 98, 0 }, { 0, 255, 98, 67 },
2133 { 0, 18141, 18137, 0 }, { 0, 17494, 17504, 6553 }, { 0, 24979, 24992, 0 }, { 0, 46509, 24992, 17347 }
2136 /* END MACHINE GENERATED */
2137 #endif /* COLORMAP flag check */
2138 #endif /* flag checks */
2142 /* Basic pixel information: */
2143 Image
* in_image
; /* Input image */
2144 const Image
* out_image
; /* Output image */
2146 /* 'background' is the value passed to the gpc_ routines, it may be NULL if
2147 * it should not be used (*this* program has an error if it crashes as a
2150 Background background_color
;
2151 const Background
* background
;
2153 /* Precalculated values: */
2154 int in_opaque
; /* Value of input alpha that is opaque */
2155 int is_palette
; /* Sample values come from the palette */
2156 int accumulate
; /* Accumlate component errors (don't log) */
2157 int output_8bit
; /* Output is 8 bit (else 16 bit) */
2159 void (*in_gp
)(Pixel
*, png_const_voidp
);
2160 void (*out_gp
)(Pixel
*, png_const_voidp
);
2162 void (*transform
)(Pixel
*out
, const Pixel
*in
, const Background
*back
);
2163 /* A function to perform the required transform */
2165 void (*from_linear
)(Pixel
*out
, const Pixel
*in
, const Background
*back
);
2166 /* For 'via_linear' transforms the final, from linear, step, else NULL */
2168 png_uint_16 error
[4];
2169 /* Three error values for transparent, partially transparent and opaque
2170 * input pixels (in turn).
2173 png_uint_16
*error_ptr
;
2174 /* Where these are stored in the static array (for 'accumulate') */
2178 /* Return a 'transform' as above for the given format conversion. */
2180 transform_from_formats(Transform
*result
, Image
*in_image
,
2181 const Image
*out_image
, png_const_colorp background
, int via_linear
)
2183 png_uint_32 in_format
, out_format
;
2184 png_uint_32 in_base
, out_base
;
2186 memset(result
, 0, sizeof *result
);
2188 /* Store the original images for error messages */
2189 result
->in_image
= in_image
;
2190 result
->out_image
= out_image
;
2192 in_format
= in_image
->image
.format
;
2193 out_format
= out_image
->image
.format
;
2195 if (in_format
& PNG_FORMAT_FLAG_LINEAR
)
2196 result
->in_opaque
= 65535;
2198 result
->in_opaque
= 255;
2200 result
->output_8bit
= (out_format
& PNG_FORMAT_FLAG_LINEAR
) == 0;
2202 result
->is_palette
= 0; /* set by caller if required */
2203 result
->accumulate
= (in_image
->opts
& ACCUMULATE
) != 0;
2205 /* The loaders (which need the ordering information) */
2206 result
->in_gp
= get_pixel(in_format
);
2207 result
->out_gp
= get_pixel(out_format
);
2209 /* Remove the ordering information: */
2210 in_format
&= BASE_FORMATS
| PNG_FORMAT_FLAG_COLORMAP
;
2211 in_base
= in_format
& BASE_FORMATS
;
2212 out_format
&= BASE_FORMATS
| PNG_FORMAT_FLAG_COLORMAP
;
2213 out_base
= out_format
& BASE_FORMATS
;
2217 /* Check for an error in this program: */
2218 if (out_format
& (PNG_FORMAT_FLAG_LINEAR
|PNG_FORMAT_FLAG_COLORMAP
))
2220 fprintf(stderr
, "internal transform via linear error 0x%x->0x%x\n",
2221 in_format
, out_format
);
2225 result
->transform
= gpc_fn
[in_base
][out_base
| PNG_FORMAT_FLAG_LINEAR
];
2226 result
->from_linear
= gpc_fn
[out_base
| PNG_FORMAT_FLAG_LINEAR
][out_base
];
2227 result
->error_ptr
= gpc_error_via_linear
[in_format
][out_format
];
2230 else if (~in_format
& out_format
& PNG_FORMAT_FLAG_COLORMAP
)
2232 /* The input is not colormapped but the output is, the errors will
2233 * typically be large (only the grayscale-no-alpha case permits preserving
2234 * even 8-bit values.)
2236 result
->transform
= gpc_fn
[in_base
][out_base
];
2237 result
->from_linear
= NULL
;
2238 result
->error_ptr
= gpc_error_to_colormap
[in_base
][out_base
];
2243 /* The caller handles the colormap->pixel value conversion, so the
2244 * transform function just gets a pixel value, however because libpng
2245 * currently contains a different implementation for mapping a colormap if
2246 * both input and output are colormapped we need different conversion
2247 * functions to deal with errors in the libpng implementation.
2249 if (in_format
& out_format
& PNG_FORMAT_FLAG_COLORMAP
)
2250 result
->transform
= gpc_fn_colormapped
[in_base
][out_base
];
2252 result
->transform
= gpc_fn
[in_base
][out_base
];
2253 result
->from_linear
= NULL
;
2254 result
->error_ptr
= gpc_error
[in_format
][out_format
];
2257 /* Follow the libpng simplified API rules to work out what to pass to the gpc
2258 * routines as a background value, if one is not required pass NULL so that
2259 * this program crashes in the even of a programming error.
2261 result
->background
= NULL
; /* default: not required */
2263 /* Rule 1: background only need be supplied if alpha is to be removed */
2264 if (in_format
& ~out_format
& PNG_FORMAT_FLAG_ALPHA
)
2266 /* The input value is 'NULL' to use the background and (otherwise) an sRGB
2267 * background color (to use a solid color). The code above uses a fixed
2268 * byte value, BUFFER_INIT8, for buffer even for 16-bit output. For
2269 * linear (16-bit) output the sRGB background color is ignored; the
2270 * composition is always on the background (so BUFFER_INIT8 * 257), except
2271 * that for the colormap (i.e. linear colormapped output) black is used.
2273 result
->background
= &result
->background_color
;
2275 if (out_format
& PNG_FORMAT_FLAG_LINEAR
|| via_linear
)
2277 if (out_format
& PNG_FORMAT_FLAG_COLORMAP
)
2279 result
->background_color
.ir
=
2280 result
->background_color
.ig
=
2281 result
->background_color
.ib
= 0;
2282 result
->background_color
.dr
=
2283 result
->background_color
.dg
=
2284 result
->background_color
.db
= 0;
2289 result
->background_color
.ir
=
2290 result
->background_color
.ig
=
2291 result
->background_color
.ib
= BUFFER_INIT8
* 257;
2292 result
->background_color
.dr
=
2293 result
->background_color
.dg
=
2294 result
->background_color
.db
= 0;
2298 else /* sRGB output */
2300 if (background
!= NULL
)
2302 if (out_format
& PNG_FORMAT_FLAG_COLOR
)
2304 result
->background_color
.ir
= background
->red
;
2305 result
->background_color
.ig
= background
->green
;
2306 result
->background_color
.ib
= background
->blue
;
2307 /* TODO: sometimes libpng uses the power law conversion here, how
2310 result
->background_color
.dr
= sRGB_to_d
[background
->red
];
2311 result
->background_color
.dg
= sRGB_to_d
[background
->green
];
2312 result
->background_color
.db
= sRGB_to_d
[background
->blue
];
2315 else /* grayscale: libpng only looks at 'g' */
2317 result
->background_color
.ir
=
2318 result
->background_color
.ig
=
2319 result
->background_color
.ib
= background
->green
;
2320 /* TODO: sometimes libpng uses the power law conversion here, how
2323 result
->background_color
.dr
=
2324 result
->background_color
.dg
=
2325 result
->background_color
.db
= sRGB_to_d
[background
->green
];
2329 else if ((out_format
& PNG_FORMAT_FLAG_COLORMAP
) == 0)
2331 result
->background_color
.ir
=
2332 result
->background_color
.ig
=
2333 result
->background_color
.ib
= BUFFER_INIT8
;
2334 /* TODO: sometimes libpng uses the power law conversion here, how
2337 result
->background_color
.dr
=
2338 result
->background_color
.dg
=
2339 result
->background_color
.db
= sRGB_to_d
[BUFFER_INIT8
];
2342 /* Else the output is colormapped and a background color must be
2343 * provided; if pngstest crashes then that is a bug in this program
2344 * (though libpng should png_error as well.)
2347 result
->background
= NULL
;
2351 if (result
->background
== NULL
)
2353 result
->background_color
.ir
=
2354 result
->background_color
.ig
=
2355 result
->background_color
.ib
= -1; /* not used */
2356 result
->background_color
.dr
=
2357 result
->background_color
.dg
=
2358 result
->background_color
.db
= 1E30
; /* not used */
2362 /* Copy the error values into the Transform: */
2363 result
->error
[0] = result
->error_ptr
[0];
2364 result
->error
[1] = result
->error_ptr
[1];
2365 result
->error
[2] = result
->error_ptr
[2];
2366 result
->error
[3] = result
->error_ptr
[3];
2370 /* Compare two pixels.
2373 static int error_to_linear = 811; * by experiment *
2374 static int error_to_linear_grayscale = 424; * by experiment *
2375 static int error_to_sRGB = 6; * by experiment *
2376 static int error_to_sRGB_grayscale = 17; * libpng error by calculation +
2378 static int error_in_compose = 2; * by experiment *
2379 static int error_in_premultiply = 1;
2381 * The following is *just* the result of a round trip from 8-bit sRGB to linear
2382 * then back to 8-bit sRGB when it is done by libpng. There are two problems:
2384 * 1) libpng currently uses a 2.2 power law with no linear segment, this results
2385 * in instability in the low values and even with 16-bit precision sRGB(1) ends
2386 * up mapping to sRGB(0) as a result of rounding in the 16-bit representation.
2387 * This gives an error of 1 in the handling of value 1 only.
2389 * 2) libpng currently uses an intermediate 8-bit linear value in gamma
2390 * correction of 8-bit values. This results in many more errors, the worse of
2391 * which is mapping sRGB(14) to sRGB(0).
2393 * The general 'error_via_linear' is more complex because of pre-multiplication,
2394 * this compounds the 8-bit errors according to the alpha value of the pixel.
2395 * As a result 256 values are pre-calculated for error_via_linear.
2398 static int error_in_libpng_gamma
;
2399 static int error_via_linear
[256]; /* Indexed by 8-bit alpha */
2402 init_error_via_linear(void)
2406 error_via_linear
[0] = 255; /* transparent pixel */
2408 for (alpha
=1; alpha
<=255; ++alpha
)
2410 /* 16-bit values less than 128.5 get rounded to 8-bit 0 and so the worst
2411 * case error arises with 16-bit 128.5, work out what sRGB
2412 * (non-associated) value generates 128.5; any value less than this is
2413 * going to map to 0, so the worst error is floor(value).
2415 * Note that errors are considerably higher (more than a factor of 2)
2416 * because libpng uses a simple power law for sRGB data at present.
2418 * Add .1 for arithmetic errors inside libpng.
2420 double v
= floor(255*pow(.5/*(128.5 * 255 / 65535)*/ / alpha
, 1/2.2)+.1);
2422 error_via_linear
[alpha
] = (int)v
;
2425 /* This is actually 14.99, but, despite the closeness to 15, 14 seems to work
2428 error_in_libpng_gamma
= 14;
2433 print_pixel(char string
[64], const Pixel
*pixel
, png_uint_32 format
)
2435 switch (format
& (PNG_FORMAT_FLAG_ALPHA
|PNG_FORMAT_FLAG_COLOR
))
2438 sprintf(string
, "%s(%d)", format_names
[format
], pixel
->g
);
2441 case PNG_FORMAT_FLAG_ALPHA
:
2442 sprintf(string
, "%s(%d,%d)", format_names
[format
], pixel
->g
,
2446 case PNG_FORMAT_FLAG_COLOR
:
2447 sprintf(string
, "%s(%d,%d,%d)", format_names
[format
],
2448 pixel
->r
, pixel
->g
, pixel
->b
);
2451 case PNG_FORMAT_FLAG_COLOR
|PNG_FORMAT_FLAG_ALPHA
:
2452 sprintf(string
, "%s(%d,%d,%d,%d)", format_names
[format
],
2453 pixel
->r
, pixel
->g
, pixel
->b
, pixel
->a
);
2457 sprintf(string
, "invalid-format");
2463 logpixel(const Transform
*transform
, png_uint_32 x
, png_uint_32 y
,
2464 const Pixel
*in
, const Pixel
*calc
, const Pixel
*out
, const char *reason
)
2466 const png_uint_32 in_format
= transform
->in_image
->image
.format
;
2467 const png_uint_32 out_format
= transform
->out_image
->image
.format
;
2469 png_uint_32 back_format
= out_format
& ~PNG_FORMAT_FLAG_ALPHA
;
2470 const char *via_linear
= "";
2472 char pixel_in
[64], pixel_calc
[64], pixel_out
[64], pixel_loc
[64];
2473 char background_info
[100];
2475 print_pixel(pixel_in
, in
, in_format
);
2476 print_pixel(pixel_calc
, calc
, out_format
);
2477 print_pixel(pixel_out
, out
, out_format
);
2479 if (transform
->is_palette
)
2480 sprintf(pixel_loc
, "palette: %lu", (unsigned long)y
);
2482 sprintf(pixel_loc
, "%lu,%lu", (unsigned long)x
, (unsigned long)y
);
2484 if (transform
->from_linear
!= NULL
)
2486 via_linear
= " (via linear)";
2487 /* And as a result the *read* format which did any background processing
2488 * was itself linear, so the background color information is also
2491 back_format
|= PNG_FORMAT_FLAG_LINEAR
;
2494 if (transform
->background
!= NULL
)
2497 char pixel_back
[64];
2499 back
.r
= transform
->background
->ir
;
2500 back
.g
= transform
->background
->ig
;
2501 back
.b
= transform
->background
->ib
;
2502 back
.a
= -1; /* not used */
2504 print_pixel(pixel_back
, &back
, back_format
);
2505 sprintf(background_info
, " on background %s", pixel_back
);
2509 background_info
[0] = 0;
2511 if (transform
->in_image
->file_name
!= transform
->out_image
->file_name
)
2513 char error_buffer
[512];
2514 sprintf(error_buffer
,
2515 "(%s) %s error%s:\n %s%s ->\n %s\n not: %s.\n"
2516 "Use --preserve and examine: ", pixel_loc
, reason
, via_linear
,
2517 pixel_in
, background_info
, pixel_out
, pixel_calc
);
2518 return logerror(transform
->in_image
, transform
->in_image
->file_name
,
2519 error_buffer
, transform
->out_image
->file_name
);
2524 char error_buffer
[512];
2525 sprintf(error_buffer
,
2526 "(%s) %s error%s:\n %s%s ->\n %s\n not: %s.\n"
2527 " The error happened when reading the original file with this format.",
2528 pixel_loc
, reason
, via_linear
, pixel_in
, background_info
, pixel_out
,
2530 return logerror(transform
->in_image
, transform
->in_image
->file_name
,
2536 cmppixel(Transform
*transform
, png_const_voidp in
, png_const_voidp out
,
2537 png_uint_32 x
, png_uint_32 y
/*or palette index*/)
2540 png_const_charp errmsg
;
2541 Pixel pixel_in
, pixel_calc
, pixel_out
;
2543 transform
->in_gp(&pixel_in
, in
);
2545 if (transform
->from_linear
== NULL
)
2546 transform
->transform(&pixel_calc
, &pixel_in
, transform
->background
);
2550 transform
->transform(&pixel_out
, &pixel_in
, transform
->background
);
2551 transform
->from_linear(&pixel_calc
, &pixel_out
, NULL
);
2554 transform
->out_gp(&pixel_out
, out
);
2556 /* Eliminate the case where the input and output values match exactly. */
2557 if (pixel_calc
.a
== pixel_out
.a
&& pixel_calc
.r
== pixel_out
.r
&&
2558 pixel_calc
.g
== pixel_out
.g
&& pixel_calc
.b
== pixel_out
.b
)
2561 /* Eliminate the case where the output pixel is transparent and the output
2562 * is 8-bit - any component values are valid. Don't check the input alpha
2563 * here to also skip the 16-bit small alpha cases.
2565 if (transform
->output_8bit
&& pixel_calc
.a
== 0 && pixel_out
.a
== 0)
2568 /* Check for alpha errors first; an alpha error can damage the components too
2569 * so avoid spurious checks on components if one is found.
2573 int err_a
= abs(pixel_calc
.a
-pixel_out
.a
);
2575 if (err_a
> transform
->error
[3])
2577 /* If accumulating check the components too */
2578 if (transform
->accumulate
)
2579 transform
->error
[3] = (png_uint_16
)err_a
;
2586 /* Now if *either* of the output alphas are 0 but alpha is within tolerance
2587 * eliminate the 8-bit component comparison.
2589 if (errmsg
== NULL
&& transform
->output_8bit
&&
2590 (pixel_calc
.a
== 0 || pixel_out
.a
== 0))
2593 if (errmsg
== NULL
) /* else just signal an alpha error */
2595 int err_r
= abs(pixel_calc
.r
- pixel_out
.r
);
2596 int err_g
= abs(pixel_calc
.g
- pixel_out
.g
);
2597 int err_b
= abs(pixel_calc
.b
- pixel_out
.b
);
2600 if ((err_r
| err_g
| err_b
) == 0)
2601 return 1; /* exact match */
2603 /* Mismatch on a component, check the input alpha */
2604 if (pixel_in
.a
>= transform
->in_opaque
)
2606 errmsg
= "opaque component";
2607 limit
= 2; /* opaque */
2610 else if (pixel_in
.a
> 0)
2612 errmsg
= "alpha component";
2613 limit
= 1; /* partially transparent */
2618 errmsg
= "transparent component (background)";
2619 limit
= 0; /* transparent */
2623 if (maxerr
< err_g
) maxerr
= err_g
;
2624 if (maxerr
< err_b
) maxerr
= err_b
;
2626 if (maxerr
<= transform
->error
[limit
])
2627 return 1; /* within the error limits */
2629 /* Handle a component mis-match; log it, just return an error code, or
2632 if (transform
->accumulate
)
2634 transform
->error
[limit
] = (png_uint_16
)maxerr
;
2635 return 1; /* to cause the caller to keep going */
2639 /* Failure to match and not accumulating, so the error must be logged. */
2640 return logpixel(transform
, x
, y
, &pixel_in
, &pixel_calc
, &pixel_out
, errmsg
);
2644 component_loc(png_byte loc
[4], png_uint_32 format
)
2646 /* Given a format return the number of channels and the location of
2649 * The mask 'loc' contains the component offset of the channels in the
2650 * following order. Note that if 'format' is grayscale the entries 1-3 must
2651 * all contain the location of the gray channel.
2660 if (format
& PNG_FORMAT_FLAG_COLOR
)
2666 # ifdef PNG_FORMAT_BGR_SUPPORTED
2667 if (format
& PNG_FORMAT_FLAG_BGR
)
2684 loc
[1] = loc
[2] = loc
[3] = 0;
2687 if (format
& PNG_FORMAT_FLAG_ALPHA
)
2689 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
2690 if (format
& PNG_FORMAT_FLAG_AFIRST
)
2706 loc
[0] = 4; /* not present */
2711 /* Compare two images, the original 'a', which was written out then read back in
2712 * to * give image 'b'. The formats may have been changed.
2715 compare_two_images(Image
*a
, Image
*b
, int via_linear
,
2716 png_const_colorp background
)
2718 ptrdiff_t stridea
= a
->stride
;
2719 ptrdiff_t strideb
= b
->stride
;
2720 png_const_bytep rowa
= a
->buffer
+16;
2721 png_const_bytep rowb
= b
->buffer
+16;
2722 const png_uint_32 width
= a
->image
.width
;
2723 const png_uint_32 height
= a
->image
.height
;
2724 const png_uint_32 formata
= a
->image
.format
;
2725 const png_uint_32 formatb
= b
->image
.format
;
2726 const unsigned int a_sample
= PNG_IMAGE_SAMPLE_SIZE(formata
);
2727 const unsigned int b_sample
= PNG_IMAGE_SAMPLE_SIZE(formatb
);
2728 int alpha_added
, alpha_removed
;
2734 /* This should never happen: */
2735 if (width
!= b
->image
.width
|| height
!= b
->image
.height
)
2736 return logerror(a
, a
->file_name
, ": width x height changed: ",
2739 /* Set up the background and the transform */
2740 transform_from_formats(&tr
, a
, b
, background
, via_linear
);
2742 /* Find the first row and inter-row space. */
2743 if (!(formata
& PNG_FORMAT_FLAG_COLORMAP
) &&
2744 (formata
& PNG_FORMAT_FLAG_LINEAR
))
2747 if (!(formatb
& PNG_FORMAT_FLAG_COLORMAP
) &&
2748 (formatb
& PNG_FORMAT_FLAG_LINEAR
))
2751 if (stridea
< 0) rowa
+= (height
-1) * (-stridea
);
2752 if (strideb
< 0) rowb
+= (height
-1) * (-strideb
);
2754 /* First shortcut the two colormap case by comparing the image data; if it
2755 * matches then we expect the colormaps to match, although this is not
2756 * absolutely necessary for an image match. If the colormaps fail to match
2757 * then there is a problem in libpng.
2759 if (formata
& formatb
& PNG_FORMAT_FLAG_COLORMAP
)
2761 /* Only check colormap entries that actually exist; */
2762 png_const_bytep ppa
, ppb
;
2764 png_byte in_use
[256], amax
= 0, bmax
= 0;
2766 memset(in_use
, 0, sizeof in_use
);
2771 /* Do this the slow way to accumulate the 'in_use' flags, don't break out
2772 * of the loop until the end; this validates the color-mapped data to
2773 * ensure all pixels are valid color-map indexes.
2775 for (y
=0, match
=1; y
<height
&& match
; ++y
, ppa
+= stridea
, ppb
+= strideb
)
2779 for (x
=0; x
<width
; ++x
)
2781 png_byte bval
= ppb
[x
];
2782 png_byte aval
= ppa
[x
];
2796 /* If the buffers match then the colormaps must too. */
2799 /* Do the color-maps match, entry by entry? Only check the 'in_use'
2800 * entries. An error here should be logged as a color-map error.
2802 png_const_bytep a_cmap
= (png_const_bytep
)a
->colormap
;
2803 png_const_bytep b_cmap
= (png_const_bytep
)b
->colormap
;
2804 int result
= 1; /* match by default */
2806 /* This is used in logpixel to get the error message correct. */
2809 for (y
=0; y
<256; ++y
, a_cmap
+= a_sample
, b_cmap
+= b_sample
)
2812 /* The colormap entries should be valid, but because libpng doesn't
2813 * do any checking at present the original image may contain invalid
2814 * pixel values. These cause an error here (at present) unless
2815 * accumulating errors in which case the program just ignores them.
2817 if (y
>= a
->image
.colormap_entries
)
2819 if ((a
->opts
& ACCUMULATE
) == 0)
2822 sprintf(pindex
, "%lu[%lu]", (unsigned long)y
,
2823 (unsigned long)a
->image
.colormap_entries
);
2824 logerror(a
, a
->file_name
, ": bad pixel index: ", pindex
);
2829 else if (y
>= b
->image
.colormap_entries
)
2831 if ((a
->opts
& ACCUMULATE
) == 0)
2834 sprintf(pindex
, "%lu[%lu]", (unsigned long)y
,
2835 (unsigned long)b
->image
.colormap_entries
);
2836 logerror(b
, b
->file_name
, ": bad pixel index: ", pindex
);
2841 /* All the mismatches are logged here; there can only be 256! */
2842 else if (!cmppixel(&tr
, a_cmap
, b_cmap
, 0, y
))
2846 /* If reqested copy the error values back from the Transform. */
2847 if (a
->opts
& ACCUMULATE
)
2849 tr
.error_ptr
[0] = tr
.error
[0];
2850 tr
.error_ptr
[1] = tr
.error
[1];
2851 tr
.error_ptr
[2] = tr
.error
[2];
2852 tr
.error_ptr
[3] = tr
.error
[3];
2853 result
= 1; /* force a continue */
2859 /* else the image buffers don't match pixel-wise so compare sample values
2860 * instead, but first validate that the pixel indexes are in range (but
2861 * only if not accumulating, when the error is ignored.)
2863 else if ((a
->opts
& ACCUMULATE
) == 0)
2865 /* Check the original image first,
2866 * TODO: deal with input images with bad pixel values?
2868 if (amax
>= a
->image
.colormap_entries
)
2871 sprintf(pindex
, "%d[%lu]", amax
,
2872 (unsigned long)a
->image
.colormap_entries
);
2873 return logerror(a
, a
->file_name
, ": bad pixel index: ", pindex
);
2876 else if (bmax
>= b
->image
.colormap_entries
)
2879 sprintf(pindex
, "%d[%lu]", bmax
,
2880 (unsigned long)b
->image
.colormap_entries
);
2881 return logerror(b
, b
->file_name
, ": bad pixel index: ", pindex
);
2886 /* We can directly compare pixel values without the need to use the read
2887 * or transform support (i.e. a memory compare) if:
2889 * 1) The bit depth has not changed.
2890 * 2) RGB to grayscale has not been done (the reverse is ok; we just compare
2891 * the three RGB values to the original grayscale.)
2892 * 3) An alpha channel has not been removed from an 8-bit format, or the
2893 * 8-bit alpha value of the pixel was 255 (opaque).
2895 * If an alpha channel has been *added* then it must have the relevant opaque
2896 * value (255 or 65535).
2898 * The fist two the tests (in the order given above) (using the boolean
2899 * equivalence !a && !b == !(a || b))
2901 if (!(((formata
^ formatb
) & PNG_FORMAT_FLAG_LINEAR
) |
2902 (formata
& (formatb
^ PNG_FORMAT_FLAG_COLOR
) & PNG_FORMAT_FLAG_COLOR
)))
2904 /* Was an alpha channel changed? */
2905 const png_uint_32 alpha_changed
= (formata
^ formatb
) &
2906 PNG_FORMAT_FLAG_ALPHA
;
2908 /* Was an alpha channel removed? (The third test.) If so the direct
2909 * comparison is only possible if the input alpha is opaque.
2911 alpha_removed
= (formata
& alpha_changed
) != 0;
2913 /* Was an alpha channel added? */
2914 alpha_added
= (formatb
& alpha_changed
) != 0;
2916 /* The channels may have been moved between input and output, this finds
2917 * out how, recording the result in the btoa array, which says where in
2918 * 'a' to find each channel of 'b'. If alpha was added then btoa[alpha]
2919 * ends up as 4 (and is not used.)
2926 /* The following are used only if the formats match, except that
2927 * 'bchannels' is a flag for matching formats. btoa[x] says, for each
2928 * channel in b, where to find the corresponding value in a, for the
2929 * bchannels. achannels may be different for a gray to rgb transform
2930 * (a will be 1 or 2, b will be 3 or 4 channels.)
2932 (void)component_loc(aloc
, formata
);
2933 bchannels
= component_loc(bloc
, formatb
);
2935 /* Hence the btoa array. */
2936 for (i
=0; i
<4; ++i
) if (bloc
[i
] < 4)
2937 btoa
[bloc
[i
]] = aloc
[i
]; /* may be '4' for alpha */
2940 alpha_added
= bloc
[0]; /* location of alpha channel in image b */
2943 alpha_added
= 4; /* Won't match an image b channel */
2946 alpha_removed
= aloc
[0]; /* location of alpha channel in image a */
2955 /* Direct compare is not possible, cancel out all the corresponding local
2959 alpha_removed
= alpha_added
= 4;
2960 btoa
[3] = btoa
[2] = btoa
[1] = btoa
[0] = 4; /* 4 == not present */
2963 for (y
=0; y
<height
; ++y
, rowa
+= stridea
, rowb
+= strideb
)
2965 png_const_bytep ppa
, ppb
;
2968 for (x
=0, ppa
=rowa
, ppb
=rowb
; x
<width
; ++x
)
2970 png_const_bytep psa
, psb
;
2972 if (formata
& PNG_FORMAT_FLAG_COLORMAP
)
2973 psa
= (png_const_bytep
)a
->colormap
+ a_sample
* *ppa
++;
2975 psa
= ppa
, ppa
+= a_sample
;
2977 if (formatb
& PNG_FORMAT_FLAG_COLORMAP
)
2978 psb
= (png_const_bytep
)b
->colormap
+ b_sample
* *ppb
++;
2980 psb
= ppb
, ppb
+= b_sample
;
2982 /* Do the fast test if possible. */
2985 /* Check each 'b' channel against either the corresponding 'a'
2986 * channel or the opaque alpha value, as appropriate. If
2987 * alpha_removed value is set (not 4) then also do this only if the
2988 * 'a' alpha channel (alpha_removed) is opaque; only relevant for
2991 if (formatb
& PNG_FORMAT_FLAG_LINEAR
) /* 16-bit checks */
2993 png_const_uint_16p pua
= aligncastconst(png_const_uint_16p
, psa
);
2994 png_const_uint_16p pub
= aligncastconst(png_const_uint_16p
, psb
);
2999 if (pua
[btoa
[3]] != pub
[3]) break;
3001 if (pua
[btoa
[2]] != pub
[2]) break;
3003 if (pua
[btoa
[1]] != pub
[1]) break;
3005 if (pua
[btoa
[0]] != pub
[0]) break;
3006 if (alpha_added
!= 4 && pub
[alpha_added
] != 65535) break;
3007 continue; /* x loop */
3009 break; /* impossible */
3013 else if (alpha_removed
== 4 || psa
[alpha_removed
] == 255)
3018 if (psa
[btoa
[3]] != psb
[3]) break;
3020 if (psa
[btoa
[2]] != psb
[2]) break;
3022 if (psa
[btoa
[1]] != psb
[1]) break;
3024 if (psa
[btoa
[0]] != psb
[0]) break;
3025 if (alpha_added
!= 4 && psb
[alpha_added
] != 255) break;
3026 continue; /* x loop */
3028 break; /* impossible */
3033 /* If we get to here the fast match failed; do the slow match for this
3036 if (!cmppixel(&tr
, psa
, psb
, x
, y
) && (a
->opts
& KEEP_GOING
) == 0)
3037 return 0; /* error case */
3041 /* If reqested copy the error values back from the Transform. */
3042 if (a
->opts
& ACCUMULATE
)
3044 tr
.error_ptr
[0] = tr
.error
[0];
3045 tr
.error_ptr
[1] = tr
.error
[1];
3046 tr
.error_ptr
[2] = tr
.error
[2];
3047 tr
.error_ptr
[3] = tr
.error
[3];
3053 /* Read the file; how the read gets done depends on which of input_file and
3054 * input_memory have been set.
3057 read_file(Image
*image
, png_uint_32 format
, png_const_colorp background
)
3059 memset(&image
->image
, 0, sizeof image
->image
);
3060 image
->image
.version
= PNG_IMAGE_VERSION
;
3062 if (image
->input_memory
!= NULL
)
3064 if (!png_image_begin_read_from_memory(&image
->image
, image
->input_memory
,
3065 image
->input_memory_size
))
3066 return logerror(image
, "memory init: ", image
->file_name
, "");
3069 # ifdef PNG_STDIO_SUPPORTED
3070 else if (image
->input_file
!= NULL
)
3072 if (!png_image_begin_read_from_stdio(&image
->image
, image
->input_file
))
3073 return logerror(image
, "stdio init: ", image
->file_name
, "");
3078 if (!png_image_begin_read_from_file(&image
->image
, image
->file_name
))
3079 return logerror(image
, "file init: ", image
->file_name
, "");
3084 return logerror(image
, "unsupported file/stdio init: ",
3085 image
->file_name
, "");
3089 /* This must be set after the begin_read call: */
3090 if (image
->opts
& sRGB_16BIT
)
3091 image
->image
.flags
|= PNG_IMAGE_FLAG_16BIT_sRGB
;
3093 /* Have an initialized image with all the data we need plus, maybe, an
3094 * allocated file (myfile) or buffer (mybuffer) that need to be freed.
3098 png_uint_32 image_format
;
3100 /* Print both original and output formats. */
3101 image_format
= image
->image
.format
;
3103 if (image
->opts
& VERBOSE
)
3105 printf("%s %lu x %lu %s -> %s", image
->file_name
,
3106 (unsigned long)image
->image
.width
,
3107 (unsigned long)image
->image
.height
,
3108 format_names
[image_format
& FORMAT_MASK
],
3109 (format
& FORMAT_NO_CHANGE
) != 0 || image
->image
.format
== format
3110 ?
"no change" : format_names
[format
& FORMAT_MASK
]);
3112 if (background
!= NULL
)
3113 printf(" background(%d,%d,%d)\n", background
->red
,
3114 background
->green
, background
->blue
);
3121 /* 'NO_CHANGE' combined with the color-map flag forces the base format
3122 * flags to be set on read to ensure that the original representation is
3123 * not lost in the pass through a colormap format.
3125 if ((format
& FORMAT_NO_CHANGE
) != 0)
3127 if ((format
& PNG_FORMAT_FLAG_COLORMAP
) != 0 &&
3128 (image_format
& PNG_FORMAT_FLAG_COLORMAP
) != 0)
3129 format
= (image_format
& ~BASE_FORMATS
) | (format
& BASE_FORMATS
);
3132 format
= image_format
;
3135 image
->image
.format
= format
;
3137 image
->stride
= PNG_IMAGE_ROW_STRIDE(image
->image
) + image
->stride_extra
;
3140 result
= png_image_finish_read(&image
->image
, background
,
3141 image
->buffer
+16, (png_int_32
)image
->stride
, image
->colormap
);
3143 checkbuffer(image
, image
->file_name
);
3146 return checkopaque(image
);
3149 return logerror(image
, image
->file_name
, ": image read failed", "");
3153 /* Reads from a filename, which must be in image->file_name, but uses
3154 * image->opts to choose the method. The file is always read in its native
3155 * format (the one the simplified API suggests).
3158 read_one_file(Image
*image
)
3160 if (!(image
->opts
& READ_FILE
) || (image
->opts
& USE_STDIO
))
3162 /* memory or stdio. */
3163 FILE *f
= fopen(image
->file_name
, "rb");
3167 if (image
->opts
& READ_FILE
)
3168 image
->input_file
= f
;
3172 if (fseek(f
, 0, SEEK_END
) == 0)
3174 long int cb
= ftell(f
);
3176 if (cb
> 0 && (unsigned long int)cb
< (size_t)~(size_t)0)
3178 png_bytep b
= voidcast(png_bytep
, malloc((size_t)cb
));
3184 if (fread(b
, (size_t)cb
, 1, f
) == 1)
3187 image
->input_memory_size
= cb
;
3188 image
->input_memory
= b
;
3194 return logclose(image
, f
, image
->file_name
,
3200 return logclose(image
, f
, image
->file_name
,
3201 ": out of memory: ");
3205 return logclose(image
, f
, image
->file_name
,
3209 return logclose(image
, f
, image
->file_name
,
3214 return logclose(image
, f
, image
->file_name
, ": seek failed: ");
3219 return logerror(image
, image
->file_name
, ": open failed: ",
3223 return read_file(image
, FORMAT_NO_CHANGE
, NULL
);
3226 #ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
3228 write_one_file(Image
*output
, Image
*image
, int convert_to_8bit
)
3230 if (image
->opts
& FAST_WRITE
)
3231 image
->image
.flags
|= PNG_IMAGE_FLAG_FAST
;
3233 if (image
->opts
& USE_STDIO
)
3235 FILE *f
= tmpfile();
3239 if (png_image_write_to_stdio(&image
->image
, f
, convert_to_8bit
,
3240 image
->buffer
+16, (png_int_32
)image
->stride
, image
->colormap
))
3245 initimage(output
, image
->opts
, "tmpfile", image
->stride_extra
);
3246 output
->input_file
= f
;
3247 if (!checkopaque(image
))
3252 return logclose(image
, f
, "tmpfile", ": flush: ");
3258 return logerror(image
, "tmpfile", ": write failed", "");
3263 return logerror(image
, "tmpfile", ": open: ", strerror(errno
));
3268 static int counter
= 0;
3271 sprintf(name
, "%s%d.png", tmpf
, ++counter
);
3273 if (png_image_write_to_file(&image
->image
, name
, convert_to_8bit
,
3274 image
->buffer
+16, (png_int_32
)image
->stride
, image
->colormap
))
3276 initimage(output
, image
->opts
, output
->tmpfile_name
,
3277 image
->stride_extra
);
3278 /* Afterwards, or freeimage will delete it! */
3279 strcpy(output
->tmpfile_name
, name
);
3281 if (!checkopaque(image
))
3286 return logerror(image
, name
, ": write failed", "");
3289 /* 'output' has an initialized temporary image, read this back in and compare
3290 * this against the original: there should be no change since the original
3291 * format was written unmodified unless 'convert_to_8bit' was specified.
3292 * However, if the original image was color-mapped, a simple read will zap
3293 * the linear, color and maybe alpha flags, this will cause spurious failures
3294 * under some circumstances.
3296 if (read_file(output
, image
->image
.format
| FORMAT_NO_CHANGE
, NULL
))
3298 png_uint_32 original_format
= image
->image
.format
;
3300 if (convert_to_8bit
)
3301 original_format
&= ~PNG_FORMAT_FLAG_LINEAR
;
3303 if ((output
->image
.format
& BASE_FORMATS
) !=
3304 (original_format
& BASE_FORMATS
))
3305 return logerror(image
, image
->file_name
, ": format changed on read: ",
3308 return compare_two_images(image
, output
, 0/*via linear*/, NULL
);
3312 return logerror(output
, output
->tmpfile_name
,
3313 ": read of new file failed", "");
3318 testimage(Image
*image
, png_uint_32 opts
, format_list
*pf
)
3323 /* Copy the original data, stealing it from 'image' */
3332 image
->input_file
= NULL
;
3333 image
->input_memory
= NULL
;
3334 image
->input_memory_size
= 0;
3335 image
->tmpfile_name
[0] = 0;
3338 png_uint_32 counter
;
3345 /* Use the low bit of 'counter' to indicate whether or not to do alpha
3346 * removal with a background color or by composting onto the image; this
3347 * step gets skipped if it isn't relevant
3349 for (counter
=0; counter
<2*FORMAT_COUNT
; ++counter
)
3350 if (format_isset(pf
, counter
>> 1))
3352 png_uint_32 format
= counter
>> 1;
3354 png_color background_color
;
3355 png_colorp background
= NULL
;
3357 /* If there is a format change that removes the alpha channel then
3358 * the background is relevant. If the output is 8-bit color-mapped
3359 * then a background color *must* be provided, otherwise there are
3360 * two tests to do - one with a color, the other with NULL. The
3361 * NULL test happens second.
3363 if ((counter
& 1) == 0)
3365 if ((format
& PNG_FORMAT_FLAG_ALPHA
) == 0 &&
3366 (image
->image
.format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
3368 /* Alpha/transparency will be removed, the background is
3369 * relevant: make it a color the first time
3371 random_color(&background_color
);
3372 background
= &background_color
;
3374 /* BUT if the output is to a color-mapped 8-bit format then
3375 * the background must always be a color, so increment 'counter'
3376 * to skip the NULL test.
3378 if ((format
& PNG_FORMAT_FLAG_COLORMAP
) != 0 &&
3379 (format
& PNG_FORMAT_FLAG_LINEAR
) == 0)
3383 /* Otherwise an alpha channel is not being eliminated, just leave
3384 * background NULL and skip the (counter & 1) NULL test.
3389 /* else just use NULL for background */
3392 copy
.opts
= opts
; /* in case read_file needs to change it */
3394 result
= read_file(©
, format
, background
);
3398 /* Make sure the file just read matches the original file. */
3399 result
= compare_two_images(image
, ©
, 0/*via linear*/, background
);
3403 # ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
3404 /* Write the *copy* just made to a new file to make sure the write
3405 * side works ok. Check the conversion to sRGB if the copy is
3409 result
= write_one_file(&output
, ©
, 0/*convert to 8bit*/);
3413 /* Validate against the original too; the background is needed here
3414 * as well so that compare_two_images knows what color was used.
3416 result
= compare_two_images(image
, &output
, 0, background
);
3420 if ((format
& PNG_FORMAT_FLAG_LINEAR
) != 0 &&
3421 (format
& PNG_FORMAT_FLAG_COLORMAP
) == 0)
3423 /* 'output' is linear, convert to the corresponding sRGB format.
3426 result
= write_one_file(&output
, ©
, 1/*convert to 8bit*/);
3430 /* This may involve a conversion via linear; in the ideal world
3431 * this would round-trip correctly, but libpng 1.5.7 is not the
3432 * ideal world so allow a drift (error_via_linear).
3434 * 'image' has an alpha channel but 'output' does not then there
3435 * will a strip-alpha-channel operation (because 'output' is
3436 * linear), handle this by composing on black when doing the
3439 result
= compare_two_images(image
, &output
, 1/*via_linear*/,
3444 # endif /* PNG_SIMPLIFIED_WRITE_SUPPORTED */
3456 test_one_file(const char *file_name
, format_list
*formats
, png_uint_32 opts
,
3457 int stride_extra
, int log_pass
)
3463 initimage(&image
, opts
, file_name
, stride_extra
);
3464 result
= read_one_file(&image
);
3466 result
= testimage(&image
, opts
, formats
);
3469 /* Ensure that stderr is flushed into any log file */
3480 # ifndef PNG_SIMPLIFIED_WRITE_SUPPORTED
3481 printf(" (no write)");
3485 printf(" %s\n", file_name
);
3486 /* stdout may not be line-buffered if it is piped to a file, so: */
3497 main(int argc
, char **argv
)
3499 png_uint_32 opts
= FAST_WRITE
;
3500 format_list formats
;
3501 const char *touch
= NULL
;
3504 int stride_extra
= 0;
3510 init_error_via_linear();
3512 format_init(&formats
);
3514 for (c
=1; c
<argc
; ++c
)
3516 const char *arg
= argv
[c
];
3518 if (strcmp(arg
, "--log") == 0)
3520 else if (strcmp(arg
, "--fresh") == 0)
3522 memset(gpc_error
, 0, sizeof gpc_error
);
3523 memset(gpc_error_via_linear
, 0, sizeof gpc_error_via_linear
);
3525 else if (strcmp(arg
, "--file") == 0)
3526 # ifdef PNG_STDIO_SUPPORTED
3529 return 77; /* skipped: no support */
3531 else if (strcmp(arg
, "--memory") == 0)
3533 else if (strcmp(arg
, "--stdio") == 0)
3534 # ifdef PNG_STDIO_SUPPORTED
3537 return 77; /* skipped: no support */
3539 else if (strcmp(arg
, "--name") == 0)
3541 else if (strcmp(arg
, "--verbose") == 0)
3543 else if (strcmp(arg
, "--quiet") == 0)
3545 else if (strcmp(arg
, "--preserve") == 0)
3546 opts
|= KEEP_TMPFILES
;
3547 else if (strcmp(arg
, "--nopreserve") == 0)
3548 opts
&= ~KEEP_TMPFILES
;
3549 else if (strcmp(arg
, "--keep-going") == 0)
3551 else if (strcmp(arg
, "--fast") == 0)
3553 else if (strcmp(arg
, "--slow") == 0)
3554 opts
&= ~FAST_WRITE
;
3555 else if (strcmp(arg
, "--accumulate") == 0)
3557 else if (strcmp(arg
, "--redundant") == 0)
3559 else if (strcmp(arg
, "--stop") == 0)
3560 opts
&= ~KEEP_GOING
;
3561 else if (strcmp(arg
, "--strict") == 0)
3563 else if (strcmp(arg
, "--sRGB-16bit") == 0)
3565 else if (strcmp(arg
, "--linear-16bit") == 0)
3566 opts
&= ~sRGB_16BIT
;
3567 else if (strcmp(arg
, "--tmpfile") == 0)
3571 if (strlen(argv
[++c
]) >= sizeof tmpf
)
3574 fprintf(stderr
, "%s: %s is too long for a temp file prefix\n",
3579 /* Safe: checked above */
3580 strcpy(tmpf
, argv
[c
]);
3586 fprintf(stderr
, "%s: %s requires a temporary file prefix\n",
3591 else if (strcmp(arg
, "--touch") == 0)
3599 fprintf(stderr
, "%s: %s requires a file name argument\n",
3604 else if (arg
[0] == '+')
3606 png_uint_32 format
= formatof(arg
+1);
3608 if (format
> FORMAT_COUNT
)
3611 format_set(&formats
, format
);
3613 else if (arg
[0] == '-' && arg
[1] != 0 && (arg
[1] != '0' || arg
[2] != 0))
3616 fprintf(stderr
, "%s: unknown option: %s\n", argv
[0], arg
);
3621 if (format_is_initial(&formats
))
3622 format_default(&formats
, redundant
);
3626 const int term
= (arg
[1] == '0' ?
0 : '\n');
3627 unsigned int ich
= 0;
3629 /* Loop reading files, use a static buffer to simplify this and just
3630 * stop if the name gets to long.
3632 static char buffer
[4096];
3638 /* Don't allow '\0' in file names, and terminate with '\n' or,
3639 * for -0, just '\0' (use -print0 to find to make this work!)
3641 if (ch
== EOF
|| ch
== term
|| ch
== 0)
3645 if (ich
> 0 && !test_one_file(buffer
, &formats
, opts
,
3646 stride_extra
, log_pass
))
3653 --ich
; /* so that the increment below sets it to 0 again */
3657 buffer
[ich
] = (char)ch
;
3658 } while (++ich
< sizeof buffer
);
3664 fprintf(stderr
, "%s...%s: file name too long\n", buffer
,
3670 else if (!test_one_file(arg
, &formats
, opts
, stride_extra
, log_pass
))
3675 if (opts
& ACCUMULATE
)
3679 printf("static png_uint_16 gpc_error[16/*in*/][16/*out*/][4/*a*/] =\n");
3681 for (in
=0; in
<16; ++in
)
3684 printf(" { /* input: %s */\n ", format_names
[in
]);
3685 for (out
=0; out
<16; ++out
)
3689 for (alpha
=0; alpha
<4; ++alpha
)
3691 printf(" %d", gpc_error
[in
][out
][alpha
]);
3692 if (alpha
< 3) putchar(',');
3698 if (out
% 4 == 3) printf("\n ");
3710 printf("static png_uint_16 gpc_error_via_linear[16][4/*out*/][4] =\n");
3712 for (in
=0; in
<16; ++in
)
3715 printf(" { /* input: %s */\n ", format_names
[in
]);
3716 for (out
=0; out
<4; ++out
)
3720 for (alpha
=0; alpha
<4; ++alpha
)
3722 printf(" %d", gpc_error_via_linear
[in
][out
][alpha
]);
3723 if (alpha
< 3) putchar(',');
3738 printf("static png_uint_16 gpc_error_to_colormap[8/*i*/][8/*o*/][4] =\n");
3740 for (in
=0; in
<8; ++in
)
3743 printf(" { /* input: %s */\n ", format_names
[in
]);
3744 for (out
=0; out
<8; ++out
)
3748 for (alpha
=0; alpha
<4; ++alpha
)
3750 printf(" %d", gpc_error_to_colormap
[in
][out
][alpha
]);
3751 if (alpha
< 3) putchar(',');
3757 if (out
% 4 == 3) printf("\n ");
3770 if (retval
== 0 && touch
!= NULL
)
3772 FILE *fsuccess
= fopen(touch
, "wt");
3774 if (fsuccess
!= NULL
)
3777 fprintf(fsuccess
, "PNG simple API tests succeeded\n");
3779 error
= ferror(fsuccess
);
3781 if (fclose(fsuccess
) || error
)
3784 fprintf(stderr
, "%s: write failed\n", touch
);
3792 fprintf(stderr
, "%s: open failed\n", touch
);
3800 #else /* !PNG_SIMPLIFIED_READ_SUPPORTED */
3803 fprintf(stderr
, "pngstest: no read support in libpng, test skipped\n");
3804 /* So the test is skipped: */
3807 #endif /* PNG_SIMPLIFIED_READ_SUPPORTED */