4 #define COPYRIGHT "\251 2013,2015 John Cunningham Bowler"
6 * Last changed in libpng 1.6.20 [November 24, 2015]
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 * Make a test PNG image. The arguments are as follows:
14 * makepng [--sRGB|--linear|--1.8] [--tRNS] [--nofilters] \
15 * color-type bit-depth [file-name]
17 * The color-type may be numeric (and must match the numbers used by the PNG
18 * specification) or one of the format names listed below. The bit-depth is the
19 * component bit depth, or the pixel bit-depth for a color-mapped image.
21 * Without any options no color-space information is written, with the options
22 * an sRGB or the appropriate gAMA chunk is written. "1.8" refers to the
23 * display system used on older Apple computers to correct for high ambient
24 * light levels in the viewing environment; it applies a transform of
25 * approximately value^(1/1.45) to the color values and so a gAMA chunk of 65909
26 * is written (1.45/2.2).
28 * The image data is generated internally. Unless --color is given the images
29 * used are as follows:
31 * 1 channel: a square image with a diamond, the least luminous colors are on
32 * the edge of the image, the most luminous in the center.
34 * 2 channels: the color channel increases in luminosity from top to bottom, the
35 * alpha channel increases in opacity from left to right.
37 * 3 channels: linear combinations of, from the top-left corner clockwise,
38 * black, green, white, red.
40 * 4 channels: linear combinations of, from the top-left corner clockwise,
41 * transparent, red, green, blue.
43 * For color-mapped images a four channel color-map is used and if --tRNS is
44 * given the PNG file has a tRNS chunk, as follows:
46 * 1-bit: entry 0 is transparent-red, entry 1 is opaque-white
47 * 2-bit: entry 0: transparent-green
50 * entry 3: opaque-white
51 * 4-bit: the 16 combinations of the 2-bit case
52 * 8-bit: the 256 combinations of the 4-bit case
54 * The palette always has 2^bit-depth entries and the tRNS chunk one fewer. The
55 * image is the 1-channel diamond, but using palette index, not luminosity.
57 * For formats other than color-mapped ones if --tRNS is specified a tRNS chunk
58 * is generated with all channels equal to the low bits of 0x0101.
60 * Image size is determined by the final pixel depth in bits, i.e. channels x
61 * bit-depth, as follows:
63 * 8 bits or less: 64x64
65 * More than 16 bits: 1024x1024
67 * Row filtering is the libpng default but may be turned off (the 'none' filter
68 * is used on every row) with the --nofilters option.
70 * The images are not interlaced.
72 * If file-name is given then the PNG is written to that file, else it is
73 * written to stdout. Notice that stdout is not supported on systems where, by
74 * default, it assumes text output; this program makes no attempt to change the
75 * text mode of stdout!
77 * makepng --color=<color> ...
79 * If --color is given then the whole image has that color, color-mapped images
80 * will have exactly one palette entry and all image files with be 16x16 in
81 * size. The color value is 1 to 4 decimal numbers as appropriate for the color
86 * If --small is given the images are no larger than required to include every
87 * possible pixel value for the format.
89 * For formats with pixels 8 bits or fewer in size the images consist of a
90 * single row with 2^pixel-depth pixels, one of every possible value.
92 * For formats with 16-bit pixels a 256x256 image is generated containing every
93 * possible pixel value.
95 * For larger pixel sizes a 256x256 image is generated where the first row
96 * consists of each pixel that has identical byte values throughout the pixel
97 * followed by rows where the byte values differ within the pixel.
99 * In all cases the pixel values are arranged in such a way that the SUB and UP
100 * filters give byte sequences for maximal zlib compression. By default (if
101 * --nofilters is not given) the SUB filter is used on the first row and the UP
102 * filter on all following rows.
104 * The --small option is meant to provide good test-case coverage, however the
105 * images are not easy to examine visually. Without the --small option the
106 * images contain identical color values; the pixel values are adjusted
107 * according to the gamma encoding with no gamma encoding being interpreted as
113 * This code is copyright of the authors, see the COPYRIGHT define above. The
114 * code is licensed as above, using the libpng license. The code generates
115 * images which are solely the product of the code; the options choose which of
116 * the many possibilities to generate. The images that result (but not the code
117 * which generates them) are licensed as defined here:
119 * IMPORTANT: the COPYRIGHT #define must contain ISO-Latin-1 characters, the
120 * IMAGE_LICENSING #define must contain UTF-8 characters. The 'copyright'
121 * symbol 0xA9U (\251) in ISO-Latin-1 encoding and 0xC20xA9 (\302\251) in UTF-8.
123 #define IMAGE_LICENSING "Dedicated to the public domain per Creative Commons "\
124 "license \"CC0 1.0\"; https://creativecommons.org/publicdomain/zero/1.0/"
126 #include <stddef.h> /* for offsetof */
136 #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
140 /* Define the following to use this test against your installed libpng, rather
141 * than the one being built here:
143 #ifdef PNG_FREESTANDING_TESTS
146 # include "../../png.h"
151 /* Work round for GCC complaints about casting a (double) function result to
158 return (unsigned int)d
;
168 /* This structure is used for inserting extra chunks (the --insert argument, not
171 typedef struct chunk_insert
173 struct chunk_insert
*next
;
174 void (*insert
)(png_structp
, png_infop
, int, png_charpp
);
176 png_charp parameters
[1];
180 channels_of_type(int color_type
)
182 if (color_type
& PNG_COLOR_MASK_PALETTE
)
189 if (color_type
& PNG_COLOR_MASK_COLOR
)
192 if (color_type
& PNG_COLOR_MASK_ALPHA
)
201 pixel_depth_of_type(int color_type
, int bit_depth
)
203 return channels_of_type(color_type
) * bit_depth
;
207 image_size_of_type(int color_type
, int bit_depth
, unsigned int *colors
,
215 int pixel_depth
= pixel_depth_of_type(color_type
, bit_depth
);
219 if (pixel_depth
<= 8) /* there will be one row */
220 return 1 << pixel_depth
;
226 else if (pixel_depth
< 8)
229 else if (pixel_depth
> 16)
238 set_color(png_colorp color
, png_bytep trans
, unsigned int red
,
239 unsigned int green
, unsigned int blue
, unsigned int alpha
,
240 png_const_bytep gamma_table
)
242 color
->red
= gamma_table
[red
];
243 color
->green
= gamma_table
[green
];
244 color
->blue
= gamma_table
[blue
];
245 *trans
= (png_byte
)alpha
;
249 generate_palette(png_colorp palette
, png_bytep trans
, int bit_depth
,
250 png_const_bytep gamma_table
, unsigned int *colors
)
253 * 1-bit: entry 0 is transparent-red, entry 1 is opaque-white
254 * 2-bit: entry 0: transparent-green
257 * entry 3: opaque-white
258 * 4-bit: the 16 combinations of the 2-bit case
259 * 8-bit: the 256 combinations of the 4-bit case
264 fprintf(stderr
, "makepng: --colors=...: invalid count %u\n",
269 set_color(palette
+0, trans
+0, colors
[1], colors
[1], colors
[1], 255,
274 set_color(palette
+0, trans
+0, colors
[1], colors
[1], colors
[1],
275 colors
[2], gamma_table
);
279 set_color(palette
+0, trans
+0, colors
[1], colors
[2], colors
[3], 255,
284 set_color(palette
+0, trans
+0, colors
[1], colors
[2], colors
[3],
285 colors
[4], gamma_table
);
291 set_color(palette
+0, trans
+0, 255, 0, 0, 0, gamma_table
);
292 set_color(palette
+1, trans
+1, 255, 255, 255, 255, gamma_table
);
298 unsigned int size
= 1U << (bit_depth
/2); /* 2, 4 or 16 */
300 volatile unsigned int ip
= 0;
302 for (x
=0; x
<size
; ++x
) for (y
=0; y
<size
; ++y
)
306 /* size is at most 16, so the scaled value below fits in 16 bits
308 # define interp(pos, c1, c2) ((pos * c1) + ((size-pos) * c2))
309 # define xyinterp(x, y, c1, c2, c3, c4) (((size * size / 2) +\
310 (interp(x, c1, c2) * y + (size-y) * interp(x, c3, c4))) /\
313 set_color(palette
+ip
, trans
+ip
,
314 /* color: green, red,blue,white */
315 xyinterp(x
, y
, 0, 255, 0, 255),
316 xyinterp(x
, y
, 255, 0, 0, 255),
317 xyinterp(x
, y
, 0, 0, 255, 255),
318 /* alpha: 0, 102, 204, 255) */
319 xyinterp(x
, y
, 0, 102, 204, 255),
329 set_value(png_bytep row
, size_t rowbytes
, png_uint_32 x
, unsigned int bit_depth
,
330 png_uint_32 value
, png_const_bytep gamma_table
, double conv
)
332 unsigned int mask
= (1U << bit_depth
)-1;
334 x
*= bit_depth
; /* Maximum x is 4*1024, maximum bit_depth is 16 */
338 png_uint_32 offset
= x
>> 3;
340 if (offset
< rowbytes
&& (bit_depth
< 16 || offset
+1 < rowbytes
))
349 /* Don't gamma correct - values get smashed */
351 unsigned int shift
= (8 - bit_depth
) - (x
& 0x7U
);
354 value
= (value
<< shift
) & mask
;
355 *row
= (png_byte
)((*row
& ~mask
) | value
);
360 fprintf(stderr
, "makepng: bad bit depth (internal error)\n");
364 value
= flooru(65535*pow(value
/65535.,conv
)+.5);
365 *row
++ = (png_byte
)(value
>> 8);
366 *row
= (png_byte
)value
;
370 *row
= gamma_table
[value
];
377 fprintf(stderr
, "makepng: row buffer overflow (internal error)\n");
384 fprintf(stderr
, "makepng: component overflow (internal error)\n");
389 static int /* filter mask for row */
390 generate_row(png_bytep row
, size_t rowbytes
, unsigned int y
, int color_type
,
391 int bit_depth
, png_const_bytep gamma_table
, double conv
,
392 unsigned int *colors
, int small
)
394 int filters
= 0; /* file *MASK*, 0 means the default, not NONE */
395 png_uint_32 size_max
=
396 image_size_of_type(color_type
, bit_depth
, colors
, small
)-1;
397 png_uint_32 depth_max
= (1U << bit_depth
)-1; /* up to 65536 */
399 if (colors
[0] == 0) if (small
)
401 unsigned int pixel_depth
= pixel_depth_of_type(color_type
, bit_depth
);
403 /* For pixel depths less than 16 generate a single row containing all the
404 * possible pixel values. For 16 generate all 65536 byte pair
405 * combinations in a 256x256 pixel array.
410 assert(y
== 0 && rowbytes
== 1 && size_max
== 1);
411 row
[0] = 0x6CU
; /* binary: 01101100, only top 2 bits used */
412 filters
= PNG_FILTER_NONE
;
416 assert(y
== 0 && rowbytes
== 1 && size_max
== 3);
417 row
[0] = 0x1BU
; /* binary 00011011, all bits used */
418 filters
= PNG_FILTER_NONE
;
422 assert(y
== 0 && rowbytes
== 8 && size_max
== 15);
424 row
[1] = 0x23U
; /* SUB gives 0x22U for all following bytes */
431 filters
= PNG_FILTER_SUB
;
435 /* The row will have all the pixel values in order starting with
436 * '1', the SUB filter will change every byte into '1' (including
437 * the last, which generates pixel value '0'). Since the SUB filter
438 * has value 1 this should result in maximum compression.
440 assert(y
== 0 && rowbytes
== 256 && size_max
== 255);
443 row
[size_max
] = 0xFFU
& (size_max
+1);
448 filters
= PNG_FILTER_SUB
;
452 /* Rows are generated such that each row has a constant difference
453 * between the first and second byte of each pixel and so that the
454 * difference increases by 1 at each row. The rows start with the
455 * first byte value of 0 and the value increases to 255 across the
458 * The difference starts at 1, so the first row is:
460 * 0 1 1 2 2 3 3 4 ... 254 255 255 0
462 * This means that running the SUB filter on the first row produces:
464 * [SUB==1] 0 1 0 1 0 1...
466 * Then the difference is 2 on the next row, giving:
468 * 0 2 1 3 2 4 3 5 ... 254 0 255 1
470 * When the UP filter is run on this libpng produces:
472 * [UP ==2] 0 1 0 1 0 1...
474 * And so on for all the remain rows to the final two * rows:
476 * row 254: 0 255 1 0 2 1 3 2 4 3 ... 254 253 255 254
477 * row 255: 0 0 1 1 2 2 3 3 4 4 ... 254 254 255 255
479 assert(rowbytes
== 512 && size_max
== 255);
482 row
[2*size_max
] = 0xFFU
& size_max
;
483 row
[2*size_max
+1] = 0xFFU
& (size_max
+y
+1);
488 /* The first row must include PNG_FILTER_UP so that libpng knows we
489 * need to keep it for the following row:
491 filters
= (y
== 0 ? PNG_FILTER_SUB
+PNG_FILTER_UP
: PNG_FILTER_UP
);
498 /* The rows are filled by an alogorithm similar to the above, in the
499 * first row pixel bytes are all equal, increasing from 0 by 1 for
500 * each pixel. In the second row the bytes within a pixel are
501 * incremented 1,3,5,7,... from the previous row byte. Using an odd
502 * number ensures all the possible byte values are used.
504 assert(size_max
== 255 && rowbytes
== 256*(pixel_depth
>>3));
505 pixel_depth
>>= 3; /* now in bytes */
508 const size_t pixel_index
= --rowbytes
/pixel_depth
;
511 row
[rowbytes
] = 0xFFU
& pixel_index
;
515 const size_t byte_offset
=
516 rowbytes
- pixel_index
* pixel_depth
;
519 0xFFU
& (pixel_index
+ (byte_offset
* 2*y
) + 1);
522 filters
= (y
== 0 ? PNG_FILTER_SUB
+PNG_FILTER_UP
: PNG_FILTER_UP
);
526 assert(0/*NOT REACHED*/);
530 else switch (channels_of_type(color_type
))
532 /* 1 channel: a square image with a diamond, the least luminous colors are on
533 * the edge of the image, the most luminous in the center.
538 png_uint_32 base
= 2*size_max
- abs(2*y
-size_max
);
540 for (x
=0; x
<=size_max
; ++x
)
542 png_uint_32 luma
= base
- abs(2*x
-size_max
);
544 /* 'luma' is now in the range 0..2*size_max, we need
547 luma
= (luma
*depth_max
+ size_max
) / (2*size_max
);
548 set_value(row
, rowbytes
, x
, bit_depth
, luma
, gamma_table
, conv
);
553 /* 2 channels: the color channel increases in luminosity from top to bottom,
554 * the alpha channel increases in opacity from left to right.
558 png_uint_32 alpha
= (depth_max
* y
* 2 + size_max
) / (2 * size_max
);
561 for (x
=0; x
<=size_max
; ++x
)
563 set_value(row
, rowbytes
, 2*x
, bit_depth
,
564 (depth_max
* x
* 2 + size_max
) / (2 * size_max
), gamma_table
,
566 set_value(row
, rowbytes
, 2*x
+1, bit_depth
, alpha
, gamma_table
,
572 /* 3 channels: linear combinations of, from the top-left corner clockwise,
573 * black, green, white, red.
577 /* x0: the black->red scale (the value of the red component) at the
578 * start of the row (blue and green are 0).
579 * x1: the green->white scale (the value of the red and blue
580 * components at the end of the row; green is depth_max).
582 png_uint_32 Y
= (depth_max
* y
* 2 + size_max
) / (2 * size_max
);
585 /* Interpolate x/depth_max from start to end:
587 * start end difference
589 * green: 0 depth_max depth_max
592 for (x
=0; x
<=size_max
; ++x
)
594 set_value(row
, rowbytes
, 3*x
+0, bit_depth
, /* red */ Y
,
596 set_value(row
, rowbytes
, 3*x
+1, bit_depth
, /* green */
597 (depth_max
* x
* 2 + size_max
) / (2 * size_max
),
599 set_value(row
, rowbytes
, 3*x
+2, bit_depth
, /* blue */
600 (Y
* x
* 2 + size_max
) / (2 * size_max
),
606 /* 4 channels: linear combinations of, from the top-left corner clockwise,
607 * transparent, red, green, blue.
611 /* x0: the transparent->blue scale (the value of the blue and alpha
612 * components) at the start of the row (red and green are 0).
613 * x1: the red->green scale (the value of the red and green
614 * components at the end of the row; blue is 0 and alpha is
617 png_uint_32 Y
= (depth_max
* y
* 2 + size_max
) / (2 * size_max
);
620 /* Interpolate x/depth_max from start to end:
622 * start end difference
623 * red: 0 depth_max-Y depth_max-Y
626 * alpha: Y depth_max depth_max-Y
628 for (x
=0; x
<=size_max
; ++x
)
630 set_value(row
, rowbytes
, 4*x
+0, bit_depth
, /* red */
631 ((depth_max
-Y
) * x
* 2 + size_max
) / (2 * size_max
),
633 set_value(row
, rowbytes
, 4*x
+1, bit_depth
, /* green */
634 (Y
* x
* 2 + size_max
) / (2 * size_max
),
636 set_value(row
, rowbytes
, 4*x
+2, bit_depth
, /* blue */
637 Y
- (Y
* x
* 2 + size_max
) / (2 * size_max
),
639 set_value(row
, rowbytes
, 4*x
+3, bit_depth
, /* alpha */
640 Y
+ ((depth_max
-Y
) * x
* 2 + size_max
) / (2 * size_max
),
647 fprintf(stderr
, "makepng: internal bad channel count\n");
651 else if (color_type
& PNG_COLOR_MASK_PALETTE
)
653 /* Palette with fixed color: the image rows are all 0 and the image width
656 memset(row
, 0, rowbytes
);
659 else if (colors
[0] == channels_of_type(color_type
))
660 switch (channels_of_type(color_type
))
664 png_uint_32 luma
= colors
[1];
667 for (x
=0; x
<=size_max
; ++x
)
668 set_value(row
, rowbytes
, x
, bit_depth
, luma
, gamma_table
,
675 png_uint_32 luma
= colors
[1];
676 png_uint_32 alpha
= colors
[2];
679 for (x
=0; x
<size_max
; ++x
)
681 set_value(row
, rowbytes
, 2*x
, bit_depth
, luma
, gamma_table
,
683 set_value(row
, rowbytes
, 2*x
+1, bit_depth
, alpha
, gamma_table
,
691 png_uint_32 red
= colors
[1];
692 png_uint_32 green
= colors
[2];
693 png_uint_32 blue
= colors
[3];
696 for (x
=0; x
<=size_max
; ++x
)
698 set_value(row
, rowbytes
, 3*x
+0, bit_depth
, red
, gamma_table
,
700 set_value(row
, rowbytes
, 3*x
+1, bit_depth
, green
, gamma_table
,
702 set_value(row
, rowbytes
, 3*x
+2, bit_depth
, blue
, gamma_table
,
710 png_uint_32 red
= colors
[1];
711 png_uint_32 green
= colors
[2];
712 png_uint_32 blue
= colors
[3];
713 png_uint_32 alpha
= colors
[4];
716 for (x
=0; x
<=size_max
; ++x
)
718 set_value(row
, rowbytes
, 4*x
+0, bit_depth
, red
, gamma_table
,
720 set_value(row
, rowbytes
, 4*x
+1, bit_depth
, green
, gamma_table
,
722 set_value(row
, rowbytes
, 4*x
+2, bit_depth
, blue
, gamma_table
,
724 set_value(row
, rowbytes
, 4*x
+3, bit_depth
, alpha
, gamma_table
,
731 fprintf(stderr
, "makepng: internal bad channel count\n");
738 "makepng: --color: count(%u) does not match channels(%u)\n",
739 colors
[0], channels_of_type(color_type
));
748 makepng_warning(png_structp png_ptr
, png_const_charp message
)
750 const char **ep
= png_get_error_ptr(png_ptr
);
753 if (ep
!= NULL
&& *ep
!= NULL
)
759 fprintf(stderr
, "%s: warning: %s\n", name
, message
);
763 makepng_error(png_structp png_ptr
, png_const_charp message
)
765 makepng_warning(png_ptr
, message
);
766 png_longjmp(png_ptr
, 1);
769 static int /* 0 on success, else an error code */
770 write_png(const char **name
, FILE *fp
, int color_type
, int bit_depth
,
771 volatile png_fixed_point gamma
, chunk_insert
* volatile insert
,
772 unsigned int filters
, unsigned int *colors
, int small
, int tRNS
)
774 png_structp png_ptr
= png_create_write_struct(PNG_LIBPNG_VER_STRING
,
775 name
, makepng_error
, makepng_warning
);
776 volatile png_infop info_ptr
= NULL
;
777 volatile png_bytep row
= NULL
;
781 fprintf(stderr
, "makepng: OOM allocating write structure\n");
785 if (setjmp(png_jmpbuf(png_ptr
)))
787 png_structp nv_ptr
= png_ptr
;
788 png_infop nv_info
= info_ptr
;
792 png_destroy_write_struct(&nv_ptr
, &nv_info
);
793 if (row
!= NULL
) free(row
);
797 /* Allow benign errors so that we can write PNGs with errors */
798 png_set_benign_errors(png_ptr
, 1/*allowed*/);
800 /* Max out the text compression level in an attempt to make the license
801 * small. If --small then do the same for the IDAT.
804 png_set_compression_level(png_ptr
, Z_BEST_COMPRESSION
);
806 png_set_text_compression_level(png_ptr
, Z_BEST_COMPRESSION
);
808 png_init_io(png_ptr
, fp
);
810 info_ptr
= png_create_info_struct(png_ptr
);
811 if (info_ptr
== NULL
)
812 png_error(png_ptr
, "OOM allocating info structure");
816 image_size_of_type(color_type
, bit_depth
, colors
, small
);
818 png_fixed_point real_gamma
= 45455; /* For sRGB */
819 png_byte gamma_table
[256];
822 /* Normally images are square, but with 'small' we want to simply generate
823 * all the pixel values, or all that we reasonably can:
827 unsigned int pixel_depth
=
828 pixel_depth_of_type(color_type
, bit_depth
);
830 if (pixel_depth
<= 8U)
832 assert(size
== (1U<<pixel_depth
));
838 assert(size
== 256U);
846 /* This function uses the libpng values used on read to carry extra
847 * information about the gamma:
849 if (gamma
== PNG_GAMMA_MAC_18
)
852 else if (gamma
> 0 && gamma
< 1000)
861 if (real_gamma
== 45455) for (i
=0; i
<256; ++i
)
863 gamma_table
[i
] = (png_byte
)i
;
869 /* Convert 'i' from sRGB (45455) to real_gamma, this makes
870 * the images look the same regardless of the gAMA chunk.
877 for (i
=1; i
<255; ++i
)
878 gamma_table
[i
] = floorb(pow(i
/255.,conv
) * 255 + .5);
880 gamma_table
[255] = 255;
884 png_set_IHDR(png_ptr
, info_ptr
, size
, ysize
, bit_depth
, color_type
,
885 PNG_INTERLACE_NONE
, PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
887 if (color_type
& PNG_COLOR_MASK_PALETTE
)
890 png_color palette
[256];
893 npalette
= generate_palette(palette
, trans
, bit_depth
, gamma_table
,
895 png_set_PLTE(png_ptr
, info_ptr
, palette
, npalette
);
898 png_set_tRNS(png_ptr
, info_ptr
, trans
, npalette
-1,
899 NULL
/*transparent color*/);
901 /* Reset gamma_table to prevent the image rows being changed */
902 for (npalette
=0; npalette
<256; ++npalette
)
903 gamma_table
[npalette
] = (png_byte
)npalette
;
910 col
.red
= col
.green
= col
.blue
= col
.gray
=
911 0x0101U
& ((1U<<bit_depth
)-1U);
913 png_set_tRNS(png_ptr
, info_ptr
, NULL
/*trans*/, 1U, &col
);
916 if (gamma
== PNG_DEFAULT_sRGB
)
917 png_set_sRGB(png_ptr
, info_ptr
, PNG_sRGB_INTENT_ABSOLUTE
);
919 else if (gamma
> 0) /* Else don't set color space information */
921 png_set_gAMA_fixed(png_ptr
, info_ptr
, real_gamma
);
923 /* Just use the sRGB values here. */
924 png_set_cHRM_fixed(png_ptr
, info_ptr
,
926 /* white */ 31270, 32900,
927 /* red */ 64000, 33000,
928 /* green */ 30000, 60000,
929 /* blue */ 15000, 6000
933 /* Insert extra information. */
934 while (insert
!= NULL
)
936 insert
->insert(png_ptr
, info_ptr
, insert
->nparams
, insert
->parameters
);
937 insert
= insert
->next
;
940 /* Write the file header. */
941 png_write_info(png_ptr
, info_ptr
);
943 /* Restrict the filters */
944 png_set_filter(png_ptr
, PNG_FILTER_TYPE_BASE
, filters
);
947 # ifdef PNG_WRITE_INTERLACING_SUPPORTED
948 int passes
= png_set_interlace_handling(png_ptr
);
949 # else /* !WRITE_INTERLACING */
951 # endif /* !WRITE_INTERLACING */
953 size_t rowbytes
= png_get_rowbytes(png_ptr
, info_ptr
);
955 row
= malloc(rowbytes
);
958 png_error(png_ptr
, "OOM allocating row buffer");
960 for (pass
= 0; pass
< passes
; ++pass
)
964 for (y
=0; y
<ysize
; ++y
)
966 unsigned int row_filters
=
967 generate_row(row
, rowbytes
, y
, color_type
, bit_depth
,
968 gamma_table
, conv
, colors
, small
);
970 if (row_filters
!= 0 && filters
== PNG_ALL_FILTERS
)
971 png_set_filter(png_ptr
, PNG_FILTER_TYPE_BASE
, row_filters
);
973 png_write_row(png_ptr
, row
);
979 /* Finish writing the file. */
980 png_write_end(png_ptr
, info_ptr
);
983 png_structp nv_ptr
= png_ptr
;
984 png_infop nv_info
= info_ptr
;
988 png_destroy_write_struct(&nv_ptr
, &nv_info
);
996 load_file(png_const_charp name
, png_bytepp result
)
998 FILE *fp
= tmpfile();
1002 FILE *ip
= fopen(name
, "rb");
1012 if (ch
== EOF
) break;
1020 fprintf(stderr
, "%s: read error\n", name
);
1030 perror("temporary file");
1031 fprintf(stderr
, "temporary file write error\n");
1040 /* Round up to a multiple of 4 here to allow an iCCP profile
1041 * to be padded to a 4x boundary.
1043 png_bytep data
= malloc((total
+3)&~3);
1047 size_t new_size
= 0;
1052 if (ch
== EOF
) break;
1053 data
[new_size
++] = (png_byte
)ch
;
1056 if (ferror(fp
) || new_size
!= total
)
1058 perror("temporary file");
1059 fprintf(stderr
, "temporary file read error\n");
1072 fprintf(stderr
, "%s: out of memory loading file\n", name
);
1076 fprintf(stderr
, "%s: empty file\n", name
);
1084 fprintf(stderr
, "%s: open failed\n", name
);
1091 fprintf(stderr
, "makepng: %s: could not open temporary file\n", name
);
1098 load_fake(png_charp param
, png_bytepp profile
)
1100 char *endptr
= NULL
;
1101 uint64_t size
= strtoull(param
, &endptr
, 0/*base*/);
1103 /* The 'fake' format is <number>*[string] */
1104 if (endptr
!= NULL
&& *endptr
== '*')
1106 size_t len
= strlen(++endptr
);
1107 size_t result
= (size_t)size
;
1109 if (len
== 0) len
= 1; /* capture the terminating '\0' */
1111 /* Now repeat that string to fill 'size' bytes. */
1112 if (result
== size
&& (*profile
= malloc(result
)) != NULL
)
1114 png_bytep out
= *profile
;
1117 memset(out
, *endptr
, result
);
1123 memcpy(out
, endptr
, len
);
1127 memcpy(out
, endptr
, size
);
1135 fprintf(stderr
, "%s: size exceeds system limits\n", param
);
1144 check_param_count(int nparams
, int expect
)
1146 if (nparams
!= expect
)
1148 fprintf(stderr
, "bad parameter count (internal error)\n");
1154 insert_iCCP(png_structp png_ptr
, png_infop info_ptr
, int nparams
,
1157 png_bytep profile
= NULL
;
1158 png_uint_32 proflen
= 0;
1161 check_param_count(nparams
, 2);
1163 switch (params
[1][0])
1167 size_t filelen
= load_file(params
[1]+1, &profile
);
1168 if (filelen
> 0xfffffffc) /* Maximum profile length */
1170 fprintf(stderr
, "%s: file too long (%lu) for an ICC profile\n",
1171 params
[1]+1, (unsigned long)filelen
);
1175 proflen
= (png_uint_32
)filelen
;
1179 case '0': case '1': case '2': case '3': case '4':
1180 case '5': case '6': case '7': case '8': case '9':
1182 size_t fake_len
= load_fake(params
[1], &profile
);
1184 if (fake_len
> 0) /* else a simple parameter */
1186 if (fake_len
> 0xffffffff) /* Maximum profile length */
1189 "%s: fake data too long (%lu) for an ICC profile\n",
1190 params
[1], (unsigned long)fake_len
);
1193 proflen
= (png_uint_32
)(fake_len
& ~3U);
1194 /* Always fix up the profile length. */
1195 png_save_uint_32(profile
, proflen
);
1201 fprintf(stderr
, "--insert iCCP \"%s\": unrecognized\n", params
[1]);
1202 fprintf(stderr
, " use '<' to read a file: \"<filename\"\n");
1211 "makepng: --insert iCCP %s: profile length made a multiple of 4\n",
1214 /* load_file allocates extra space for this padding, the ICC spec requires
1215 * padding with zero bytes.
1218 profile
[proflen
++] = 0;
1221 if (profile
!= NULL
&& proflen
> 3)
1223 png_uint_32 prof_header
= png_get_uint_32(profile
);
1225 if (prof_header
!= proflen
)
1227 fprintf(stderr
, "--insert iCCP %s: profile length field wrong:\n",
1229 fprintf(stderr
, " actual %lu, recorded value %lu (corrected)\n",
1230 (unsigned long)proflen
, (unsigned long)prof_header
);
1231 png_save_uint_32(profile
, proflen
);
1235 if (result
&& profile
!= NULL
&& proflen
>=4)
1236 png_set_iCCP(png_ptr
, info_ptr
, params
[0], PNG_COMPRESSION_TYPE_BASE
,
1247 clear_text(png_text
*text
, png_charp keyword
)
1249 text
->compression
= -1; /* none */
1250 text
->key
= keyword
;
1252 text
->text_length
= 0; /* libpng calculates this */
1253 text
->itxt_length
= 0; /* libpng calculates this */
1255 text
->lang_key
= NULL
;
1259 set_text(png_structp png_ptr
, png_infop info_ptr
, png_textp text
,
1266 png_bytep file
= NULL
;
1268 text
->text_length
= load_file(param
+1, &file
);
1269 text
->text
= (png_charp
)file
;
1273 case '0': case '1': case '2': case '3': case '4':
1274 case '5': case '6': case '7': case '8': case '9':
1276 png_bytep data
= NULL
;
1277 size_t fake_len
= load_fake(param
, &data
);
1279 if (fake_len
> 0) /* else a simple parameter */
1281 text
->text_length
= fake_len
;
1282 text
->text
= (png_charp
)data
;
1292 png_set_text(png_ptr
, info_ptr
, text
, 1);
1294 if (text
->text
!= param
)
1299 insert_tEXt(png_structp png_ptr
, png_infop info_ptr
, int nparams
,
1304 check_param_count(nparams
, 2);
1305 clear_text(&text
, params
[0]);
1306 set_text(png_ptr
, info_ptr
, &text
, params
[1]);
1310 insert_zTXt(png_structp png_ptr
, png_infop info_ptr
, int nparams
,
1315 check_param_count(nparams
, 2);
1316 clear_text(&text
, params
[0]);
1317 text
.compression
= 0; /* deflate */
1318 set_text(png_ptr
, info_ptr
, &text
, params
[1]);
1322 insert_iTXt(png_structp png_ptr
, png_infop info_ptr
, int nparams
,
1327 check_param_count(nparams
, 4);
1328 clear_text(&text
, params
[0]);
1329 text
.compression
= 2; /* iTXt + deflate */
1330 text
.lang
= params
[1];/* language tag */
1331 text
.lang_key
= params
[2]; /* translated keyword */
1332 set_text(png_ptr
, info_ptr
, &text
, params
[3]);
1336 insert_hIST(png_structp png_ptr
, png_infop info_ptr
, int nparams
,
1340 png_uint_16 freq
[256];
1342 /* libpng takes the count from the PLTE count; we don't check it here but we
1343 * do set the array to 0 for unspecified entries.
1345 memset(freq
, 0, sizeof freq
);
1346 for (i
=0; i
<nparams
; ++i
)
1348 char *endptr
= NULL
;
1349 unsigned long int l
= strtoul(params
[i
], &endptr
, 0/*base*/);
1351 if (params
[i
][0] && *endptr
== 0 && l
<= 65535)
1352 freq
[i
] = (png_uint_16
)l
;
1356 fprintf(stderr
, "hIST[%d]: %s: invalid frequency\n", i
, params
[i
]);
1361 png_set_hIST(png_ptr
, info_ptr
, freq
);
1365 bval(png_const_structrp png_ptr
, png_charp param
, unsigned int maxval
)
1367 char *endptr
= NULL
;
1368 unsigned long int l
= strtoul(param
, &endptr
, 0/*base*/);
1370 if (param
[0] && *endptr
== 0 && l
<= maxval
)
1374 png_error(png_ptr
, "sBIT: invalid sBIT value");
1378 insert_sBIT(png_structp png_ptr
, png_infop info_ptr
, int nparams
,
1381 int ct
= png_get_color_type(png_ptr
, info_ptr
);
1382 int c
= (ct
& PNG_COLOR_MASK_COLOR
? 3 : 1) +
1383 (ct
& PNG_COLOR_MASK_ALPHA
? 1 : 0);
1384 unsigned int maxval
=
1385 ct
& PNG_COLOR_MASK_PALETTE
? 8U : png_get_bit_depth(png_ptr
, info_ptr
);
1389 png_error(png_ptr
, "sBIT: incorrect parameter count");
1391 if (ct
& PNG_COLOR_MASK_COLOR
)
1393 sBIT
.red
= bval(png_ptr
, params
[0], maxval
);
1394 sBIT
.green
= bval(png_ptr
, params
[1], maxval
);
1395 sBIT
.blue
= bval(png_ptr
, params
[2], maxval
);
1401 sBIT
.red
= sBIT
.green
= sBIT
.blue
= 42;
1402 sBIT
.gray
= bval(png_ptr
, params
[0], maxval
);
1405 if (ct
& PNG_COLOR_MASK_ALPHA
)
1406 sBIT
.alpha
= bval(png_ptr
, params
[nparams
-1], maxval
);
1411 png_set_sBIT(png_ptr
, info_ptr
, &sBIT
);
1416 insert_sPLT(png_structp png_ptr
, png_infop info_ptr
, int nparams
, png_charpp params
)
1418 fprintf(stderr
, "insert sPLT: NYI\n");
1423 find_parameters(png_const_charp what
, png_charp param
, png_charp
*list
,
1426 /* Parameters are separated by '\n' or ':' characters, up to nparams are
1427 * accepted (more is an error) and the number found is returned.
1430 for (i
=0; *param
&& i
<nparams
; ++i
)
1433 while (*++param
) if (*param
== '\n' || *param
== ':')
1435 *param
++ = 0; /* Terminate last parameter */
1436 break; /* And start a new one. */
1442 fprintf(stderr
, "--insert %s: too many parameters (%s)\n", what
, param
);
1446 list
[i
] = NULL
; /* terminates list */
1447 return i
; /* number of parameters filled in */
1451 bad_parameter_count(png_const_charp what
, int nparams
)
1453 fprintf(stderr
, "--insert %s: bad parameter count %d\n", what
, nparams
);
1457 static chunk_insert
*
1458 make_insert(png_const_charp what
,
1459 void (*insert
)(png_structp
, png_infop
, int, png_charpp
),
1460 int nparams
, png_charpp list
)
1465 cip
= malloc(offsetof(chunk_insert
,parameters
) +
1466 nparams
* sizeof (png_charp
));
1470 fprintf(stderr
, "--insert %s: out of memory allocating %d parameters\n",
1476 cip
->insert
= insert
;
1477 cip
->nparams
= nparams
;
1478 for (i
=0; i
<nparams
; ++i
)
1479 cip
->parameters
[i
] = list
[i
];
1484 static chunk_insert
*
1485 find_insert(png_const_charp what
, png_charp param
)
1487 png_uint_32 chunk
= 0;
1488 png_charp parameter_list
[1024];
1491 /* Assemble the chunk name */
1496 if ((ch
>= 65 && ch
<= 90) || (ch
>= 97 && ch
<= 122))
1497 chunk
= (chunk
<< 8) + what
[i
];
1503 if (i
< 4 || what
[4] != 0)
1505 fprintf(stderr
, "makepng --insert \"%s\": invalid chunk name\n", what
);
1509 /* Assemble the parameter list. */
1510 nparams
= find_parameters(what
, param
, parameter_list
, 1024);
1512 # define CHUNK(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d))
1516 case CHUNK(105,67,67,80): /* iCCP */
1518 return make_insert(what
, insert_iCCP
, nparams
, parameter_list
);
1521 case CHUNK(116,69,88,116): /* tEXt */
1523 return make_insert(what
, insert_tEXt
, nparams
, parameter_list
);
1526 case CHUNK(122,84,88,116): /* zTXt */
1528 return make_insert(what
, insert_zTXt
, nparams
, parameter_list
);
1531 case CHUNK(105,84,88,116): /* iTXt */
1533 return make_insert(what
, insert_iTXt
, nparams
, parameter_list
);
1536 case CHUNK(104,73,83,84): /* hIST */
1538 return make_insert(what
, insert_hIST
, nparams
, parameter_list
);
1541 case CHUNK(115,66,73,84): /* sBIT */
1543 return make_insert(what
, insert_sBIT
, nparams
, parameter_list
);
1547 case CHUNK(115,80,76,84): /* sPLT */
1548 return make_insert(what
, insert_sPLT
, nparams
, parameter_list
);
1552 fprintf(stderr
, "makepng --insert \"%s\": unrecognized chunk name\n",
1557 bad_parameter_count(what
, nparams
);
1561 /* This is necessary because libpng expects writeable strings for things like
1562 * text chunks (maybe this should be fixed...)
1565 strstash(png_const_charp foo
)
1567 /* The program indicates a memory allocation error by crashing, this is by
1572 png_charp bar
= malloc(strlen(foo
)+1);
1573 return strcpy(bar
, foo
);
1580 strstash_list(const png_const_charp
*text
)
1583 png_charp result
, bar
;
1584 const png_const_charp
*line
= text
;
1586 while (*line
!= NULL
)
1587 foo
+= strlen(*line
++);
1589 result
= bar
= malloc(foo
+1);
1592 while (*line
!= NULL
)
1594 foo
= strlen(*line
);
1595 memcpy(bar
, *line
++, foo
);
1603 /* These are used to insert Copyright and Licence fields, they allow the text to
1604 * have \n unlike the --insert option.
1606 static chunk_insert
*
1607 add_tEXt(const char *key
, const png_const_charp
*text
)
1609 static char what
[5] = { 116, 69, 88, 116, 0 };
1610 png_charp parameter_list
[3];
1612 parameter_list
[0] = strstash(key
);
1613 parameter_list
[1] = strstash_list(text
);
1614 parameter_list
[2] = NULL
;
1616 return make_insert(what
, insert_tEXt
, 2, parameter_list
);
1619 static chunk_insert
*
1620 add_iTXt(const char *key
, const char *language
, const char *language_key
,
1621 const png_const_charp
*text
)
1623 static char what
[5] = { 105, 84, 88, 116, 0 };
1624 png_charp parameter_list
[5];
1626 parameter_list
[0] = strstash(key
);
1627 parameter_list
[1] = strstash(language
);
1628 parameter_list
[2] = strstash(language_key
);
1629 parameter_list
[3] = strstash_list(text
);
1630 parameter_list
[4] = NULL
;
1632 return make_insert(what
, insert_iTXt
, 4, parameter_list
);
1635 /* This is a not-very-good parser for a sequence of numbers (including 0). It
1636 * doesn't accept some apparently valid things, but it accepts all the sensible
1640 parse_color(char *arg
, unsigned int *colors
)
1642 unsigned int ncolors
= 0;
1644 while (*arg
&& ncolors
< 4)
1648 unsigned long ul
= strtoul(arg
, &ep
, 0);
1652 fprintf(stderr
, "makepng --color=...'%s': too big\n", arg
);
1658 fprintf(stderr
, "makepng --color=...'%s': not a valid color\n", arg
);
1662 if (*ep
) ++ep
; /* skip a separator */
1665 colors
[++ncolors
] = (unsigned int)ul
; /* checked above */
1670 fprintf(stderr
, "makepng --color=...'%s': too many values\n", arg
);
1678 main(int argc
, char **argv
)
1681 const char *file_name
= NULL
;
1682 int color_type
= 8; /* invalid */
1683 int bit_depth
= 32; /* invalid */
1684 int small
= 0; /* make full size images */
1685 int tRNS
= 0; /* don't output a tRNS chunk */
1686 unsigned int colors
[5];
1687 unsigned int filters
= PNG_ALL_FILTERS
;
1688 png_fixed_point gamma
= 0; /* not set */
1689 chunk_insert
*head_insert
= NULL
;
1690 chunk_insert
**insert_ptr
= &head_insert
;
1692 memset(colors
, 0, sizeof colors
);
1696 char *arg
= *++argv
;
1698 if (strcmp(arg
, "--small") == 0)
1704 if (strcmp(arg
, "--tRNS") == 0)
1710 if (strcmp(arg
, "--sRGB") == 0)
1712 gamma
= PNG_DEFAULT_sRGB
;
1716 if (strcmp(arg
, "--linear") == 0)
1722 if (strcmp(arg
, "--1.8") == 0)
1724 gamma
= PNG_GAMMA_MAC_18
;
1728 if (strcmp(arg
, "--nofilters") == 0)
1730 filters
= PNG_FILTER_NONE
;
1734 if (strncmp(arg
, "--color=", 8) == 0)
1736 parse_color(arg
+8, colors
);
1740 if (argc
>= 3 && strcmp(arg
, "--insert") == 0)
1742 png_const_charp what
= *++argv
;
1743 png_charp param
= *++argv
;
1744 chunk_insert
*new_insert
;
1748 new_insert
= find_insert(what
, param
);
1750 if (new_insert
!= NULL
)
1752 *insert_ptr
= new_insert
;
1753 insert_ptr
= &new_insert
->next
;
1761 fprintf(stderr
, "makepng: %s: invalid option\n", arg
);
1765 if (strcmp(arg
, "palette") == 0)
1767 color_type
= PNG_COLOR_TYPE_PALETTE
;
1771 if (strncmp(arg
, "gray", 4) == 0)
1775 color_type
= PNG_COLOR_TYPE_GRAY
;
1779 else if (strcmp(arg
+4, "a") == 0 ||
1780 strcmp(arg
+4, "alpha") == 0 ||
1781 strcmp(arg
+4, "-alpha") == 0)
1783 color_type
= PNG_COLOR_TYPE_GRAY_ALPHA
;
1788 if (strncmp(arg
, "rgb", 3) == 0)
1792 color_type
= PNG_COLOR_TYPE_RGB
;
1796 else if (strcmp(arg
+3, "a") == 0 ||
1797 strcmp(arg
+3, "alpha") == 0 ||
1798 strcmp(arg
+3, "-alpha") == 0)
1800 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
1805 if (color_type
== 8 && isdigit(arg
[0]))
1807 color_type
= atoi(arg
);
1808 if (color_type
< 0 || color_type
> 6 || color_type
== 1 ||
1811 fprintf(stderr
, "makepng: %s: not a valid color type\n", arg
);
1818 if (bit_depth
== 32 && isdigit(arg
[0]))
1820 bit_depth
= atoi(arg
);
1821 if (bit_depth
<= 0 || bit_depth
> 16 ||
1822 (bit_depth
& -bit_depth
) != bit_depth
)
1824 fprintf(stderr
, "makepng: %s: not a valid bit depth\n", arg
);
1831 if (argc
== 1) /* It's the file name */
1833 fp
= fopen(arg
, "wb");
1836 fprintf(stderr
, "%s: %s: could not open\n", arg
, strerror(errno
));
1844 fprintf(stderr
, "makepng: %s: unknown argument\n", arg
);
1846 } /* argument while loop */
1848 if (color_type
== 8 || bit_depth
== 32)
1850 fprintf(stderr
, "usage: makepng [--small] [--sRGB|--linear|--1.8] "
1851 "[--color=...] color-type bit-depth [file-name]\n"
1852 " Make a test PNG file, by default writes to stdout.\n"
1853 " Other options are available, UTSL.\n");
1857 /* Check the colors */
1859 unsigned int lim
= (color_type
== PNG_COLOR_TYPE_PALETTE
? 255U :
1863 for (i
=1; i
<=colors
[0]; ++i
)
1864 if (colors
[i
] > lim
)
1866 fprintf(stderr
, "makepng: --color=...: %u out of range [0..%u]\n",
1872 /* small and colors are incomparible (will probably crash if both are used at
1875 if (small
&& colors
[0] != 0)
1877 fprintf(stderr
, "makepng: --color --small: only one at a time!\n");
1881 /* Restrict the filters for more speed to those we know are used for the
1884 if (filters
== PNG_ALL_FILTERS
&& !small
/*small provides defaults*/)
1886 if ((color_type
& PNG_COLOR_MASK_PALETTE
) != 0 || bit_depth
< 8)
1887 filters
= PNG_FILTER_NONE
;
1889 else if (color_type
& PNG_COLOR_MASK_COLOR
) /* rgb */
1892 filters
&= ~(PNG_FILTER_NONE
| PNG_FILTER_AVG
);
1895 filters
= PNG_FILTER_SUB
| PNG_FILTER_PAETH
;
1898 else /* gray 8 or 16-bit */
1899 filters
&= ~PNG_FILTER_NONE
;
1902 /* Insert standard copyright and licence text. */
1904 static png_const_charp copyright
[] =
1906 COPYRIGHT
, /* ISO-Latin-1 */
1909 static png_const_charp licensing
[] =
1911 IMAGE_LICENSING
, /* UTF-8 */
1915 chunk_insert
*new_insert
;
1917 new_insert
= add_tEXt("Copyright", copyright
);
1918 if (new_insert
!= NULL
)
1920 *insert_ptr
= new_insert
;
1921 insert_ptr
= &new_insert
->next
;
1924 new_insert
= add_iTXt("Licensing", "en", NULL
, licensing
);
1925 if (new_insert
!= NULL
)
1927 *insert_ptr
= new_insert
;
1928 insert_ptr
= &new_insert
->next
;
1933 int ret
= write_png(&file_name
, fp
, color_type
, bit_depth
, gamma
,
1934 head_insert
, filters
, colors
, small
, tRNS
);
1936 if (ret
!= 0 && file_name
!= NULL
)