Skip to content

Latest commit

 

History

History
3829 lines (3254 loc) · 103 KB

File metadata and controls

3829 lines (3254 loc) · 103 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*-
* pngstest.c
*
* Last changed in libpng 1.6.31 [July 27, 2017]
* Copyright (c) 2013-2017 John Cunningham Bowler
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*
* Test for the PNG 'simplified' APIs.
*/
#define _ISOC90_SOURCE 1
#define MALLOC_CHECK_ 2/*glibc facility: turn on debugging*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <math.h>
#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
# include <config.h>
#endif
/* Define the following to use this test against your installed libpng, rather
* than the one being built here:
*/
#ifdef PNG_FREESTANDING_TESTS
# include <png.h>
#else
# include "../../png.h"
#endif
/* 1.6.1 added support for the configure test harness, which uses 77 to indicate
* a skipped test, in earlier versions we need to succeed on a skipped test, so:
*/
#if PNG_LIBPNG_VER >= 10601 && defined(HAVE_CONFIG_H)
# define SKIP 77
#else
# define SKIP 0
#endif
#ifdef PNG_SIMPLIFIED_READ_SUPPORTED /* Else nothing can be done */
#include "../tools/sRGB.h"
/* KNOWN ISSUES
*
* These defines switch on alternate algorithms for format conversions to match
* the current libpng implementation; they are set to allow pngstest to pass
* even though libpng is producing answers that are not as correct as they
* should be.
*/
#define ALLOW_UNUSED_GPC 0
/* If true include unused static GPC functions and declare an external array
* of them to hide the fact that they are unused. This is for development
* use while testing the correct function to use to take into account libpng
* misbehavior, such as using a simple power law to correct sRGB to linear.
*/
/* The following is to support direct compilation of this file as C++ */
#ifdef __cplusplus
# define voidcast(type, value) static_cast<type>(value)
# define aligncastconst(type, value) \
static_cast<type>(static_cast<const void*>(value))
#else
# define voidcast(type, value) (value)
# define aligncastconst(type, value) ((const void*)(value))
#endif /* __cplusplus */
/* During parallel runs of pngstest each temporary file needs a unique name,
* this is used to permit uniqueness using a command line argument which can be
* up to 22 characters long.
*/
static char tmpf[23] = "TMP";
/* Generate random bytes. This uses a boring repeatable algorithm and it
* is implemented here so that it gives the same set of numbers on every
* architecture. It's a linear congruential generator (Knuth or Sedgewick
* "Algorithms") but it comes from the 'feedback taps' table in Horowitz and
* Hill, "The Art of Electronics".
*/
static void
make_random_bytes(png_uint_32* seed, void* pv, size_t size)
{
png_uint_32 u0 = seed[0], u1 = seed[1];
png_bytep bytes = voidcast(png_bytep, pv);
/* There are thirty three bits, the next bit in the sequence is bit-33 XOR
* bit-20. The top 1 bit is in u1, the bottom 32 are in u0.
*/
size_t i;
for (i=0; i<size; ++i)
{
/* First generate 8 new bits then shift them in at the end. */
png_uint_32 u = ((u0 >> (20-8)) ^ ((u1 << 7) | (u0 >> (32-7)))) & 0xff;
u1 <<= 8;
u1 |= u0 >> 24;
u0 <<= 8;
u0 |= u;
*bytes++ = (png_byte)u;
}
seed[0] = u0;
seed[1] = u1;
}
static png_uint_32 color_seed[2];
static void
reseed(void)
{
color_seed[0] = 0x12345678U;
color_seed[1] = 0x9abcdefU;
}
static void
random_color(png_colorp color)
{
make_random_bytes(color_seed, color, sizeof *color);
}
/* Math support - neither Cygwin nor Visual Studio have C99 support and we need
* a predictable rounding function, so make one here:
*/
static double
closestinteger(double x)
{
return floor(x + .5);
}
/* Cast support: remove GCC whines. */
static png_byte
u8d(double d)
{
d = closestinteger(d);
return (png_byte)d;
}
static png_uint_16
u16d(double d)
{
d = closestinteger(d);
return (png_uint_16)d;
}
/* sRGB support: use exact calculations rounded to the nearest int, see the
* fesetround() call in main(). sRGB_to_d optimizes the 8 to 16-bit conversion.
*/
static double sRGB_to_d[256];
static double g22_to_d[256];
static void
init_sRGB_to_d(void)
{
int i;
sRGB_to_d[0] = 0;
for (i=1; i<255; ++i)
sRGB_to_d[i] = linear_from_sRGB(i/255.);
sRGB_to_d[255] = 1;
g22_to_d[0] = 0;
for (i=1; i<255; ++i)
g22_to_d[i] = pow(i/255., 1/.45455);
g22_to_d[255] = 1;
}
static png_byte
sRGB(double linear /*range 0.0 .. 1.0*/)
{
return u8d(255 * sRGB_from_linear(linear));
}
static png_byte
isRGB(int fixed_linear)
{
return sRGB(fixed_linear / 65535.);
}
#if 0 /* not used */
static png_byte
unpremultiply(int component, int alpha)
{
if (alpha <= component)
return 255; /* Arbitrary, but consistent with the libpng code */
else if (alpha >= 65535)
return isRGB(component);
else
return sRGB((double)component / alpha);
}
#endif
static png_uint_16
ilinear(int fixed_srgb)
{
return u16d(65535 * sRGB_to_d[fixed_srgb]);
}
static png_uint_16
ilineara(int fixed_srgb, int alpha)
{
return u16d((257 * alpha) * sRGB_to_d[fixed_srgb]);
}
static png_uint_16
ilinear_g22(int fixed_srgb)
{
return u16d(65535 * g22_to_d[fixed_srgb]);
}
#if ALLOW_UNUSED_GPC
static png_uint_16
ilineara_g22(int fixed_srgb, int alpha)
{
return u16d((257 * alpha) * g22_to_d[fixed_srgb]);
}
#endif
static double
YfromRGBint(int ir, int ig, int ib)
{
double r = ir;
double g = ig;
double b = ib;
return YfromRGB(r, g, b);
}
#if 0 /* unused */
/* The error that results from using a 2.2 power law in place of the correct
* sRGB transform, given an 8-bit value which might be either sRGB or power-law.
*/
static int
power_law_error8(int value)
{
if (value > 0 && value < 255)
{
double vd = value / 255.;
double e = fabs(
pow(sRGB_to_d[value], 1/2.2) - sRGB_from_linear(pow(vd, 2.2)));
/* Always allow an extra 1 here for rounding errors */
e = 1+floor(255 * e);
return (int)e;
}
return 0;
}
static int error_in_sRGB_roundtrip = 56; /* by experiment */
static int
power_law_error16(int value)
{
if (value > 0 && value < 65535)
{
/* Round trip the value through an 8-bit representation but using
* non-matching to/from conversions.
*/
double vd = value / 65535.;
double e = fabs(
pow(sRGB_from_linear(vd), 2.2) - linear_from_sRGB(pow(vd, 1/2.2)));
/* Always allow an extra 1 here for rounding errors */
e = error_in_sRGB_roundtrip+floor(65535 * e);
return (int)e;
}
return 0;
}
static int
compare_8bit(int v1, int v2, int error_limit, int multiple_algorithms)
{
int e = abs(v1-v2);
int ev1, ev2;
if (e <= error_limit)
return 1;
if (!multiple_algorithms)
return 0;
ev1 = power_law_error8(v1);
if (e <= ev1)
return 1;
ev2 = power_law_error8(v2);
if (e <= ev2)
return 1;
return 0;
}
static int
compare_16bit(int v1, int v2, int error_limit, int multiple_algorithms)
{
int e = abs(v1-v2);
int ev1, ev2;
if (e <= error_limit)
return 1;
/* "multiple_algorithms" in this case means that a color-map has been
* involved somewhere, so we can deduce that the values were forced to 8-bit
* (like the via_linear case for 8-bit.)
*/
if (!multiple_algorithms)
return 0;
ev1 = power_law_error16(v1);
if (e <= ev1)
return 1;
ev2 = power_law_error16(v2);
if (e <= ev2)
return 1;
return 0;
}
#endif /* unused */
#define USE_FILE 1 /* else memory */
#define USE_STDIO 2 /* else use file name */
#define STRICT 4 /* fail on warnings too */
#define VERBOSE 8
#define KEEP_TMPFILES 16 /* else delete temporary files */
#define KEEP_GOING 32
#define ACCUMULATE 64
#define FAST_WRITE 128
#define sRGB_16BIT 256
#define NO_RESEED 512 /* do not reseed on each new file */
#define GBG_ERROR 1024 /* do not ignore the gamma+background_rgb_to_gray
* libpng warning. */
static void
print_opts(png_uint_32 opts)
{
if (opts & USE_FILE)
printf(" --file");
if (opts & USE_STDIO)
printf(" --stdio");
if (!(opts & STRICT))
printf(" --nostrict");
if (opts & VERBOSE)
printf(" --verbose");
if (opts & KEEP_TMPFILES)
printf(" --preserve");
if (opts & KEEP_GOING)
printf(" --keep-going");
if (opts & ACCUMULATE)
printf(" --accumulate");
if (!(opts & FAST_WRITE)) /* --fast is currently the default */
printf(" --slow");
if (opts & sRGB_16BIT)
printf(" --sRGB-16bit");
if (opts & NO_RESEED)
printf(" --noreseed");
#if PNG_LIBPNG_VER < 10700 /* else on by default */
if (opts & GBG_ERROR)
printf(" --fault-gbg-warning");
#endif
}
#define FORMAT_NO_CHANGE 0x80000000 /* additional flag */
/* A name table for all the formats - defines the format of the '+' arguments to
* pngstest.
*/
#define FORMAT_COUNT 64
#define FORMAT_MASK 0x3f
static PNG_CONST char * PNG_CONST format_names[FORMAT_COUNT] =
{
"sRGB-gray",
"sRGB-gray+alpha",
"sRGB-rgb",
"sRGB-rgb+alpha",
"linear-gray",
"linear-gray+alpha",
"linear-rgb",
"linear-rgb+alpha",
"color-mapped-sRGB-gray",
"color-mapped-sRGB-gray+alpha",
"color-mapped-sRGB-rgb",
"color-mapped-sRGB-rgb+alpha",
"color-mapped-linear-gray",
"color-mapped-linear-gray+alpha",
"color-mapped-linear-rgb",
"color-mapped-linear-rgb+alpha",
"sRGB-gray",
"sRGB-gray+alpha",
"sRGB-bgr",
"sRGB-bgr+alpha",
"linear-gray",
"linear-gray+alpha",
"linear-bgr",
"linear-bgr+alpha",
"color-mapped-sRGB-gray",
"color-mapped-sRGB-gray+alpha",
"color-mapped-sRGB-bgr",
"color-mapped-sRGB-bgr+alpha",
"color-mapped-linear-gray",
"color-mapped-linear-gray+alpha",
"color-mapped-linear-bgr",
"color-mapped-linear-bgr+alpha",
"sRGB-gray",
"alpha+sRGB-gray",
"sRGB-rgb",
"alpha+sRGB-rgb",
"linear-gray",
"alpha+linear-gray",
"linear-rgb",
"alpha+linear-rgb",
"color-mapped-sRGB-gray",
"color-mapped-alpha+sRGB-gray",
"color-mapped-sRGB-rgb",
"color-mapped-alpha+sRGB-rgb",
"color-mapped-linear-gray",
"color-mapped-alpha+linear-gray",
"color-mapped-linear-rgb",
"color-mapped-alpha+linear-rgb",
"sRGB-gray",
"alpha+sRGB-gray",
"sRGB-bgr",
"alpha+sRGB-bgr",
"linear-gray",
"alpha+linear-gray",
"linear-bgr",
"alpha+linear-bgr",
"color-mapped-sRGB-gray",
"color-mapped-alpha+sRGB-gray",
"color-mapped-sRGB-bgr",
"color-mapped-alpha+sRGB-bgr",
"color-mapped-linear-gray",
"color-mapped-alpha+linear-gray",
"color-mapped-linear-bgr",
"color-mapped-alpha+linear-bgr",
};
/* Decode an argument to a format number. */
static png_uint_32
formatof(const char *arg)
{
char *ep;
unsigned long format = strtoul(arg, &ep, 0);
if (ep > arg && *ep == 0 && format < FORMAT_COUNT)
return (png_uint_32)format;
else for (format=0; format < FORMAT_COUNT; ++format)
{
if (strcmp(format_names[format], arg) == 0)
return (png_uint_32)format;
}
fprintf(stderr, "pngstest: format name '%s' invalid\n", arg);
return FORMAT_COUNT;
}
/* Bitset/test functions for formats */
#define FORMAT_SET_COUNT (FORMAT_COUNT / 32)
typedef struct
{
png_uint_32 bits[FORMAT_SET_COUNT];
}
format_list;
static void format_init(format_list *pf)
{
int i;
for (i=0; i<FORMAT_SET_COUNT; ++i)
pf->bits[i] = 0; /* All off */
}
#if 0 /* currently unused */
static void format_clear(format_list *pf)
{
int i;
for (i=0; i<FORMAT_SET_COUNT; ++i)
pf->bits[i] = 0;
}
#endif
static int format_is_initial(format_list *pf)
{
int i;
for (i=0; i<FORMAT_SET_COUNT; ++i)
if (pf->bits[i] != 0)
return 0;
return 1;
}
static int format_set(format_list *pf, png_uint_32 format)
{
if (format < FORMAT_COUNT)
return pf->bits[format >> 5] |= ((png_uint_32)1) << (format & 31);
return 0;
}
#if 0 /* currently unused */
static int format_unset(format_list *pf, png_uint_32 format)
{
if (format < FORMAT_COUNT)
return pf->bits[format >> 5] &= ~((png_uint_32)1) << (format & 31);
return 0;
}
#endif
static int format_isset(format_list *pf, png_uint_32 format)
{
return format < FORMAT_COUNT &&
(pf->bits[format >> 5] & (((png_uint_32)1) << (format & 31))) != 0;
}
static void format_default(format_list *pf, int redundant)
{
if (redundant)
{
int i;
/* set everything, including flags that are pointless */
for (i=0; i<FORMAT_SET_COUNT; ++i)
pf->bits[i] = ~(png_uint_32)0;
}
else
{
png_uint_32 f;
for (f=0; f<FORMAT_COUNT; ++f)
{
/* Eliminate redundant and unsupported settings. */
# ifdef PNG_FORMAT_BGR_SUPPORTED
/* BGR is meaningless if no color: */
if ((f & PNG_FORMAT_FLAG_COLOR) == 0 &&
(f & PNG_FORMAT_FLAG_BGR) != 0)
# else
if ((f & 0x10U/*HACK: fixed value*/) != 0)
# endif
continue;
/* AFIRST is meaningless if no alpha: */
# ifdef PNG_FORMAT_AFIRST_SUPPORTED
if ((f & PNG_FORMAT_FLAG_ALPHA) == 0 &&
(f & PNG_FORMAT_FLAG_AFIRST) != 0)
# else
if ((f & 0x20U/*HACK: fixed value*/) != 0)
# endif
continue;
format_set(pf, f);
}
}
}
/* THE Image STRUCTURE */
/* The super-class of a png_image, contains the decoded image plus the input
* data necessary to re-read the file with a different format.
*/
typedef struct
{
png_image image;
png_uint_32 opts;
const char *file_name;
int stride_extra;
FILE *input_file;
png_voidp input_memory;
Nov 4, 2018
Nov 4, 2018
581
size_t input_memory_size;
582
583
png_bytep buffer;
ptrdiff_t stride;
Nov 4, 2018
Nov 4, 2018
584
585
size_t bufsize;
size_t allocsize;
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
char tmpfile_name[32];
png_uint_16 colormap[256*4];
}
Image;
/* Initializer: also sets the permitted error limit for 16-bit operations. */
static void
newimage(Image *image)
{
memset(image, 0, sizeof *image);
}
/* Reset the image to be read again - only needs to rewind the FILE* at present.
*/
static void
resetimage(Image *image)
{
if (image->input_file != NULL)
rewind(image->input_file);
}
/* Free the image buffer; the buffer is re-used on a re-read, this is just for
* cleanup.
*/
static void
freebuffer(Image *image)
{
if (image->buffer) free(image->buffer);
image->buffer = NULL;
image->bufsize = 0;
image->allocsize = 0;
}
/* Delete function; cleans out all the allocated data and the temporary file in
* the image.
*/
static void
freeimage(Image *image)
{
freebuffer(image);
png_image_free(&image->image);
if (image->input_file != NULL)
{
fclose(image->input_file);
image->input_file = NULL;
}
if (image->input_memory != NULL)
{
free(image->input_memory);
image->input_memory = NULL;
image->input_memory_size = 0;
}
if (image->tmpfile_name[0] != 0 && (image->opts & KEEP_TMPFILES) == 0)
{
(void)remove(image->tmpfile_name);
image->tmpfile_name[0] = 0;
}
}
/* This is actually a re-initializer; allows an image structure to be re-used by
* freeing everything that relates to an old image.
*/
static void initimage(Image *image, png_uint_32 opts, const char *file_name,
int stride_extra)
{
freeimage(image);
memset(&image->image, 0, sizeof image->image);
image->opts = opts;
image->file_name = file_name;
image->stride_extra = stride_extra;
}
/* Make sure the image buffer is big enough; allows re-use of the buffer if the
* image is re-read.
*/
#define BUFFER_INIT8 73
static void
allocbuffer(Image *image)
{
Nov 4, 2018
Nov 4, 2018
668
size_t size = PNG_IMAGE_BUFFER_SIZE(image->image, image->stride);
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if (size+32 > image->bufsize)
{
freebuffer(image);
image->buffer = voidcast(png_bytep, malloc(size+32));
if (image->buffer == NULL)
{
fflush(stdout);
fprintf(stderr,
"simpletest: out of memory allocating %lu(+32) byte buffer\n",
(unsigned long)size);
exit(1);
}
image->bufsize = size+32;
}
memset(image->buffer, 95, image->bufsize);
memset(image->buffer+16, BUFFER_INIT8, size);
image->allocsize = size;
}
/* Make sure 16 bytes match the given byte. */
static int
check16(png_const_bytep bp, int b)
{
int i = 16;
do
if (*bp != b) return 1;
while (--i);
return 0;
}
/* Check for overwrite in the image buffer. */
static void
checkbuffer(Image *image, const char *arg)
{
if (check16(image->buffer, 95))
{
fflush(stdout);
fprintf(stderr, "%s: overwrite at start of image buffer\n", arg);
exit(1);
}
if (check16(image->buffer+16+image->allocsize, 95))
{
fflush(stdout);
fprintf(stderr, "%s: overwrite at end of image buffer\n", arg);
exit(1);
}
}
/* ERROR HANDLING */
/* Log a terminal error, also frees the libpng part of the image if necessary.
*/
static int
logerror(Image *image, const char *a1, const char *a2, const char *a3)
{
fflush(stdout);
if (image->image.warning_or_error)
fprintf(stderr, "%s%s%s: %s\n", a1, a2, a3, image->image.message);
else
fprintf(stderr, "%s%s%s\n", a1, a2, a3);
if (image->image.opaque != NULL)
{
fprintf(stderr, "%s: image opaque pointer non-NULL on error\n",
image->file_name);
png_image_free(&image->image);
}
return 0;
}
/* Log an error and close a file (just a utility to do both things in one
* function call.)
*/
static int
logclose(Image *image, FILE *f, const char *name, const char *operation)
{
int e = errno;
fclose(f);
return logerror(image, name, operation, strerror(e));
}
/* Make sure the png_image has been freed - validates that libpng is doing what
* the spec says and freeing the image.
*/
static int
checkopaque(Image *image)
{
if (image->image.opaque != NULL)
{
png_image_free(&image->image);
return logerror(image, image->file_name, ": opaque not NULL", "");
}
/* Separate out the gamma+background_rgb_to_gray warning because it may
* produce opaque component errors:
*/
else if (image->image.warning_or_error != 0 &&
(strcmp(image->image.message,
"libpng does not support gamma+background+rgb_to_gray") == 0 ?
(image->opts & GBG_ERROR) != 0 : (image->opts & STRICT) != 0))
return logerror(image, image->file_name, (image->opts & GBG_ERROR) != 0 ?
" --fault-gbg-warning" : " --strict", "");
else
return 1;
}
/* IMAGE COMPARISON/CHECKING */
/* Compare the pixels of two images, which should be the same but aren't. The
* images must have been checked for a size match.
*/
typedef struct
{
/* The components, for grayscale images the gray value is in 'g' and if alpha
* is not present 'a' is set to 255 or 65535 according to format.
*/
int r, g, b, a;
} Pixel;
typedef struct
{
/* The background as the original sRGB 8-bit value converted to the final
* integer format and as a double precision linear value in the range 0..1
* for with partially transparent pixels.
*/
int ir, ig, ib;
double dr, dg, db; /* linear r,g,b scaled to 0..1 */
} Background;
/* Basic image formats; control the data but not the layout thereof. */
#define BASE_FORMATS\
(PNG_FORMAT_FLAG_ALPHA|PNG_FORMAT_FLAG_COLOR|PNG_FORMAT_FLAG_LINEAR)
/* Read a Pixel from a buffer. The code below stores the correct routine for
* the format in a function pointer, these are the routines:
*/
static void
gp_g8(Pixel *p, png_const_voidp pb)
{
png_const_bytep pp = voidcast(png_const_bytep, pb);
p->r = p->g = p->b = pp[0];
p->a = 255;
}
static void
gp_ga8(Pixel *p, png_const_voidp pb)
{
png_const_bytep pp = voidcast(png_const_bytep, pb);
p->r = p->g = p->b = pp[0];
p->a = pp[1];
}
#ifdef PNG_FORMAT_AFIRST_SUPPORTED
static void
gp_ag8(Pixel *p, png_const_voidp pb)
{
png_const_bytep pp = voidcast(png_const_bytep, pb);
p->r = p->g = p->b = pp[1];
p->a = pp[0];
}
#endif
static void
gp_rgb8(Pixel *p, png_const_voidp pb)
{
png_const_bytep pp = voidcast(png_const_bytep, pb);
p->r = pp[0];
p->g = pp[1];
p->b = pp[2];
p->a = 255;
}
#ifdef PNG_FORMAT_BGR_SUPPORTED
static void
gp_bgr8(Pixel *p, png_const_voidp pb)
{
png_const_bytep pp = voidcast(png_const_bytep, pb);
p->r = pp[2];
p->g = pp[1];
p->b = pp[0];
p->a = 255;
}
#endif
static void
gp_rgba8(Pixel *p, png_const_voidp pb)
{
png_const_bytep pp = voidcast(png_const_bytep, pb);
p->r = pp[0];
p->g = pp[1];
p->b = pp[2];
p->a = pp[3];
}
#ifdef PNG_FORMAT_BGR_SUPPORTED
static void
gp_bgra8(Pixel *p, png_const_voidp pb)
{
png_const_bytep pp = voidcast(png_const_bytep, pb);
p->r = pp[2];
p->g = pp[1];
p->b = pp[0];
p->a = pp[3];
}
#endif
#ifdef PNG_FORMAT_AFIRST_SUPPORTED
static void
gp_argb8(Pixel *p, png_const_voidp pb)
{
png_const_bytep pp = voidcast(png_const_bytep, pb);
p->r = pp[1];
p->g = pp[2];
p->b = pp[3];
p->a = pp[0];
}
#endif
#if defined(PNG_FORMAT_AFIRST_SUPPORTED) && defined(PNG_FORMAT_BGR_SUPPORTED)
static void
gp_abgr8(Pixel *p, png_const_voidp pb)
{
png_const_bytep pp = voidcast(png_const_bytep, pb);
p->r = pp[3];
p->g = pp[2];
p->b = pp[1];
p->a = pp[0];
}
#endif
static void
gp_g16(Pixel *p, png_const_voidp pb)
{
png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
p->r = p->g = p->b = pp[0];
p->a = 65535;
}
static void
gp_ga16(Pixel *p, png_const_voidp pb)
{
png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
p->r = p->g = p->b = pp[0];
p->a = pp[1];
}
#ifdef PNG_FORMAT_AFIRST_SUPPORTED
static void
gp_ag16(Pixel *p, png_const_voidp pb)
{
png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
p->r = p->g = p->b = pp[1];
p->a = pp[0];
}
#endif
static void
gp_rgb16(Pixel *p, png_const_voidp pb)
{
png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
p->r = pp[0];
p->g = pp[1];
p->b = pp[2];
p->a = 65535;
}
#ifdef PNG_FORMAT_BGR_SUPPORTED
static void
gp_bgr16(Pixel *p, png_const_voidp pb)
{
png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
p->r = pp[2];
p->g = pp[1];
p->b = pp[0];
p->a = 65535;
}
#endif
static void
gp_rgba16(Pixel *p, png_const_voidp pb)
{
png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
p->r = pp[0];
p->g = pp[1];
p->b = pp[2];
p->a = pp[3];
}
#ifdef PNG_FORMAT_BGR_SUPPORTED
static void
gp_bgra16(Pixel *p, png_const_voidp pb)
{
png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
p->r = pp[2];
p->g = pp[1];
p->b = pp[0];
p->a = pp[3];
}
#endif
#ifdef PNG_FORMAT_AFIRST_SUPPORTED
static void
gp_argb16(Pixel *p, png_const_voidp pb)
{
png_const_uint_16p pp = voidcast(png_const_uint_16p, pb);
p->r = pp[1];
p->g = pp[2];
p->b = pp[3];