3 * Copyright (c) 2013 John Cunningham Bowler
5 * Last changed in libpng 1.6.1 [March 28, 2013]
7 * This code is released under the libpng license.
8 * For conditions of distribution and use, see the disclaimer
11 * Make a test PNG image. The arguments are as follows:
13 * makepng [--sRGB|--linear|--1.8] [--color=<color>] color-type bit-depth \
16 * The color-type may be numeric (and must match the numbers used by the PNG
17 * specification) or one of the format names listed below. The bit-depth is the
18 * component bit depth, or the pixel bit-depth for a color-mapped image.
20 * Without any options no color-space information is written, with the options
21 * an sRGB or the appropriate gAMA chunk is written. "1.8" refers to the
22 * display system used on older Apple computers to correct for high ambient
23 * light levels in the viewing environment; it applies a transform of
24 * approximately value^(1/1.45) to the color values and so a gAMA chunk of 65909
25 * is written (1.45/2.2).
27 * The image data is generated internally. Unless --color is given the images
28 * used are as follows:
30 * 1 channel: a square image with a diamond, the least luminous colors are on
31 * the edge of the image, the most luminous in the center.
33 * 2 channels: the color channel increases in luminosity from top to bottom, the
34 * alpha channel increases in opacity from left to right.
36 * 3 channels: linear combinations of, from the top-left corner clockwise,
37 * black, green, white, red.
39 * 4 channels: linear combinations of, from the top-left corner clockwise,
40 * transparent, red, green, blue.
42 * For color-mapped images a four channel color-map is used and the PNG file has
43 * a tRNS chunk, as follows:
45 * 1-bit: entry 0 is transparent-red, entry 1 is opaque-white
46 * 2-bit: entry 0: transparent-green
49 * entry 3: opaque-white
50 * 4-bit: the 16 combinations of the 2-bit case
51 * 8-bit: the 256 combinations of the 4-bit case
53 * The palette always has 2^bit-depth entries and the tRNS chunk one fewer. The
54 * image is the 1-channel diamond, but using palette index, not luminosity.
56 * Image size is determined by the final pixel depth in bits, i.e. channels x
57 * bit-depth, as follows:
59 * 8 bits or less: 64x64
61 * More than 16 bits: 1024x1024
63 * Row filtering is turned off (the 'none' filter is used on every row) and the
64 * images are not interlaced.
66 * If --color is given then the whole image has that color, color-mapped images
67 * will have exactly one palette entry and all image files with be 16x16 in
68 * size. The color value is 1 to 4 decimal numbers as appropriate for the color
71 * If file-name is given then the PNG is written to that file, else it is
72 * written to stdout. Notice that stdout is not supported on systems where, by
73 * default, it assumes text output; this program makes no attempt to change the
74 * text mode of stdout!
76 #define _ISOC99_SOURCE /* for strtoull */
78 #include <stddef.h> /* for offsetof */
86 #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
90 /* Define the following to use this test against your installed libpng, rather
91 * than the one being built here:
93 #ifdef PNG_FREESTANDING_TESTS
96 # include "../../png.h"
99 /* This structure is used for inserting extra chunks (the --insert argument, not
102 typedef struct chunk_insert
104 struct chunk_insert
*next
;
105 void (*insert
)(png_structp
, png_infop
, int, png_charpp
);
107 png_charp parameters
[1];
111 channels_of_type(int color_type
)
113 if (color_type
& PNG_COLOR_MASK_PALETTE
)
120 if (color_type
& PNG_COLOR_MASK_COLOR
)
123 if (color_type
& PNG_COLOR_MASK_ALPHA
)
132 pixel_depth_of_type(int color_type
, int bit_depth
)
134 return channels_of_type(color_type
) * bit_depth
;
138 image_size_of_type(int color_type
, int bit_depth
, unsigned int *colors
)
145 int pixel_depth
= pixel_depth_of_type(color_type
, bit_depth
);
150 else if (pixel_depth
> 16)
159 set_color(png_colorp color
, png_bytep trans
, unsigned int red
,
160 unsigned int green
, unsigned int blue
, unsigned int alpha
,
161 png_const_bytep gamma_table
)
163 color
->red
= gamma_table
[red
];
164 color
->green
= gamma_table
[green
];
165 color
->blue
= gamma_table
[blue
];
166 *trans
= (png_byte
)alpha
;
170 generate_palette(png_colorp palette
, png_bytep trans
, int bit_depth
,
171 png_const_bytep gamma_table
, unsigned int *colors
)
174 * 1-bit: entry 0 is transparent-red, entry 1 is opaque-white
175 * 2-bit: entry 0: transparent-green
178 * entry 3: opaque-white
179 * 4-bit: the 16 combinations of the 2-bit case
180 * 8-bit: the 256 combinations of the 4-bit case
185 fprintf(stderr
, "makepng: --colors=...: invalid count %u\n",
190 set_color(palette
+0, trans
+0, colors
[1], colors
[1], colors
[1], 255,
195 set_color(palette
+0, trans
+0, colors
[1], colors
[1], colors
[1],
196 colors
[2], gamma_table
);
200 set_color(palette
+0, trans
+0, colors
[1], colors
[2], colors
[3], 255,
205 set_color(palette
+0, trans
+0, colors
[1], colors
[2], colors
[3],
206 colors
[4], gamma_table
);
212 set_color(palette
+0, trans
+0, 255, 0, 0, 0, gamma_table
);
213 set_color(palette
+1, trans
+1, 255, 255, 255, 255, gamma_table
);
219 unsigned int size
= 1U << (bit_depth
/2); /* 2, 4 or 16 */
220 unsigned int x
, y
, ip
;
222 for (x
=0; x
<size
; ++x
) for (y
=0; y
<size
; ++y
)
226 /* size is at most 16, so the scaled value below fits in 16 bits
228 # define interp(pos, c1, c2) ((pos * c1) + ((size-pos) * c2))
229 # define xyinterp(x, y, c1, c2, c3, c4) (((size * size / 2) +\
230 (interp(x, c1, c2) * y + (size-y) * interp(x, c3, c4))) /\
233 set_color(palette
+ip
, trans
+ip
,
234 /* color: green, red,blue,white */
235 xyinterp(x
, y
, 0, 255, 0, 255),
236 xyinterp(x
, y
, 255, 0, 0, 255),
237 xyinterp(x
, y
, 0, 0, 255, 255),
238 /* alpha: 0, 102, 204, 255) */
239 xyinterp(x
, y
, 0, 102, 204, 255),
249 set_value(png_bytep row
, size_t rowbytes
, png_uint_32 x
, unsigned int bit_depth
,
250 png_uint_32 value
, png_const_bytep gamma_table
, double conv
)
252 unsigned int mask
= (1U << bit_depth
)-1;
254 x
*= bit_depth
; /* Maximum x is 4*1024, maximum bit_depth is 16 */
258 png_uint_32 offset
= x
>> 3;
260 if (offset
< rowbytes
&& (bit_depth
< 16 || offset
+1 < rowbytes
))
269 /* Don't gamma correct - values get smashed */
271 unsigned int shift
= (8 - bit_depth
) - (x
& 0x7U
);
274 value
= (value
<< shift
) & mask
;
275 *row
= (png_byte
)((*row
& ~mask
) | value
);
280 fprintf(stderr
, "makepng: bad bit depth (internal error)\n");
284 value
= (unsigned int)floor(65535*pow(value
/65535.,conv
)+.5);
285 *row
++ = (png_byte
)(value
>> 8);
286 *row
= (png_byte
)value
;
290 *row
= gamma_table
[value
];
297 fprintf(stderr
, "makepng: row buffer overflow (internal error)\n");
304 fprintf(stderr
, "makepng: component overflow (internal error)\n");
310 generate_row(png_bytep row
, size_t rowbytes
, unsigned int y
, int color_type
,
311 int bit_depth
, png_const_bytep gamma_table
, double conv
,
312 unsigned int *colors
)
314 png_uint_32 size_max
= image_size_of_type(color_type
, bit_depth
, colors
)-1;
315 png_uint_32 depth_max
= (1U << bit_depth
)-1; /* up to 65536 */
317 if (colors
[0] == 0) switch (channels_of_type(color_type
))
319 /* 1 channel: a square image with a diamond, the least luminous colors are on
320 * the edge of the image, the most luminous in the center.
325 png_uint_32 base
= 2*size_max
- abs(2*y
-size_max
);
327 for (x
=0; x
<=size_max
; ++x
)
329 png_uint_32 luma
= base
- abs(2*x
-size_max
);
331 /* 'luma' is now in the range 0..2*size_max, we need
334 luma
= (luma
*depth_max
+ size_max
) / (2*size_max
);
335 set_value(row
, rowbytes
, x
, bit_depth
, luma
, gamma_table
, conv
);
340 /* 2 channels: the color channel increases in luminosity from top to bottom,
341 * the alpha channel increases in opacity from left to right.
345 png_uint_32 alpha
= (depth_max
* y
* 2 + size_max
) / (2 * size_max
);
348 for (x
=0; x
<=size_max
; ++x
)
350 set_value(row
, rowbytes
, 2*x
, bit_depth
,
351 (depth_max
* x
* 2 + size_max
) / (2 * size_max
), gamma_table
,
353 set_value(row
, rowbytes
, 2*x
+1, bit_depth
, alpha
, gamma_table
,
359 /* 3 channels: linear combinations of, from the top-left corner clockwise,
360 * black, green, white, red.
364 /* x0: the black->red scale (the value of the red component) at the
365 * start of the row (blue and green are 0).
366 * x1: the green->white scale (the value of the red and blue
367 * components at the end of the row; green is depth_max).
369 png_uint_32 Y
= (depth_max
* y
* 2 + size_max
) / (2 * size_max
);
372 /* Interpolate x/depth_max from start to end:
374 * start end difference
376 * green: 0 depth_max depth_max
379 for (x
=0; x
<=size_max
; ++x
)
381 set_value(row
, rowbytes
, 3*x
+0, bit_depth
, /* red */ Y
,
383 set_value(row
, rowbytes
, 3*x
+1, bit_depth
, /* green */
384 (depth_max
* x
* 2 + size_max
) / (2 * size_max
),
386 set_value(row
, rowbytes
, 3*x
+2, bit_depth
, /* blue */
387 (Y
* x
* 2 + size_max
) / (2 * size_max
),
393 /* 4 channels: linear combinations of, from the top-left corner clockwise,
394 * transparent, red, green, blue.
398 /* x0: the transparent->blue scale (the value of the blue and alpha
399 * components) at the start of the row (red and green are 0).
400 * x1: the red->green scale (the value of the red and green
401 * components at the end of the row; blue is 0 and alpha is
404 png_uint_32 Y
= (depth_max
* y
* 2 + size_max
) / (2 * size_max
);
407 /* Interpolate x/depth_max from start to end:
409 * start end difference
410 * red: 0 depth_max-Y depth_max-Y
413 * alpha: Y depth_max depth_max-Y
415 for (x
=0; x
<=size_max
; ++x
)
417 set_value(row
, rowbytes
, 4*x
+0, bit_depth
, /* red */
418 ((depth_max
-Y
) * x
* 2 + size_max
) / (2 * size_max
),
420 set_value(row
, rowbytes
, 4*x
+1, bit_depth
, /* green */
421 (Y
* x
* 2 + size_max
) / (2 * size_max
),
423 set_value(row
, rowbytes
, 4*x
+2, bit_depth
, /* blue */
424 Y
- (Y
* x
* 2 + size_max
) / (2 * size_max
),
426 set_value(row
, rowbytes
, 4*x
+3, bit_depth
, /* alpha */
427 Y
+ ((depth_max
-Y
) * x
* 2 + size_max
) / (2 * size_max
),
434 fprintf(stderr
, "makepng: internal bad channel count\n");
438 else if (color_type
& PNG_COLOR_MASK_PALETTE
)
440 /* Palette with fixed color: the image rows are all 0 and the image width
443 memset(row
, 0, rowbytes
);
446 else if (colors
[0] == channels_of_type(color_type
))
447 switch (channels_of_type(color_type
))
451 const png_uint_32 luma
= colors
[1];
454 for (x
=0; x
<=size_max
; ++x
)
455 set_value(row
, rowbytes
, x
, bit_depth
, luma
, gamma_table
,
462 const png_uint_32 luma
= colors
[1];
463 const png_uint_32 alpha
= colors
[2];
466 for (x
=0; x
<size_max
; ++x
)
468 set_value(row
, rowbytes
, 2*x
, bit_depth
, luma
, gamma_table
,
470 set_value(row
, rowbytes
, 2*x
+1, bit_depth
, alpha
, gamma_table
,
478 const png_uint_32 red
= colors
[1];
479 const png_uint_32 green
= colors
[2];
480 const png_uint_32 blue
= colors
[3];
483 for (x
=0; x
<=size_max
; ++x
)
485 set_value(row
, rowbytes
, 3*x
+0, bit_depth
, red
, gamma_table
,
487 set_value(row
, rowbytes
, 3*x
+1, bit_depth
, green
, gamma_table
,
489 set_value(row
, rowbytes
, 3*x
+2, bit_depth
, blue
, gamma_table
,
497 const png_uint_32 red
= colors
[1];
498 const png_uint_32 green
= colors
[2];
499 const png_uint_32 blue
= colors
[3];
500 const png_uint_32 alpha
= colors
[4];
503 for (x
=0; x
<=size_max
; ++x
)
505 set_value(row
, rowbytes
, 4*x
+0, bit_depth
, red
, gamma_table
,
507 set_value(row
, rowbytes
, 4*x
+1, bit_depth
, green
, gamma_table
,
509 set_value(row
, rowbytes
, 4*x
+2, bit_depth
, blue
, gamma_table
,
511 set_value(row
, rowbytes
, 4*x
+3, bit_depth
, alpha
, gamma_table
,
518 fprintf(stderr
, "makepng: internal bad channel count\n");
525 "makepng: --color: count(%u) does not match channels(%u)\n",
526 colors
[0], channels_of_type(color_type
));
533 makepng_warning(png_structp png_ptr
, png_const_charp message
)
535 const char **ep
= png_get_error_ptr(png_ptr
);
538 if (ep
!= NULL
&& *ep
!= NULL
)
544 fprintf(stderr
, "%s: warning: %s\n", name
, message
);
548 makepng_error(png_structp png_ptr
, png_const_charp message
)
550 makepng_warning(png_ptr
, message
);
551 png_longjmp(png_ptr
, 1);
554 static int /* 0 on success, else an error code */
555 write_png(const char **name
, FILE *fp
, int color_type
, int bit_depth
,
556 volatile png_fixed_point gamma
, chunk_insert
* volatile insert
,
557 unsigned int filters
, unsigned int *colors
)
559 png_structp png_ptr
= png_create_write_struct(PNG_LIBPNG_VER_STRING
,
560 name
, makepng_error
, makepng_warning
);
561 volatile png_infop info_ptr
= NULL
;
562 volatile png_bytep row
= NULL
;
566 fprintf(stderr
, "makepng: OOM allocating write structure\n");
570 if (setjmp(png_jmpbuf(png_ptr
)))
572 png_structp nv_ptr
= png_ptr
;
573 png_infop nv_info
= info_ptr
;
577 png_destroy_write_struct(&nv_ptr
, &nv_info
);
578 if (row
!= NULL
) free(row
);
582 /* Allow benign errors so that we can write PNGs with errors */
583 png_set_benign_errors(png_ptr
, 1/*allowed*/);
584 png_init_io(png_ptr
, fp
);
586 info_ptr
= png_create_info_struct(png_ptr
);
587 if (info_ptr
== NULL
)
588 png_error(png_ptr
, "OOM allocating info structure");
591 unsigned int size
= image_size_of_type(color_type
, bit_depth
, colors
);
592 png_fixed_point real_gamma
= 45455; /* For sRGB */
593 png_byte gamma_table
[256];
596 /* This function uses the libpng values used on read to carry extra
597 * information about the gamma:
599 if (gamma
== PNG_GAMMA_MAC_18
)
602 else if (gamma
> 0 && gamma
< 1000)
611 if (real_gamma
== 45455) for (i
=0; i
<256; ++i
)
613 gamma_table
[i
] = (png_byte
)i
;
619 /* Convert 'i' from sRGB (45455) to real_gamma, this makes
620 * the images look the same regardless of the gAMA chunk.
627 for (i
=1; i
<255; ++i
)
628 gamma_table
[i
] = (png_byte
)floor(pow(i
/255.,conv
) * 255 + .5);
630 gamma_table
[255] = 255;
634 png_set_IHDR(png_ptr
, info_ptr
, size
, size
, bit_depth
, color_type
,
635 PNG_INTERLACE_NONE
, PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
637 if (color_type
& PNG_COLOR_MASK_PALETTE
)
640 png_color palette
[256];
643 npalette
= generate_palette(palette
, trans
, bit_depth
, gamma_table
,
645 png_set_PLTE(png_ptr
, info_ptr
, palette
, npalette
);
646 png_set_tRNS(png_ptr
, info_ptr
, trans
, npalette
-1,
647 NULL
/*transparent color*/);
649 /* Reset gamma_table to prevent the image rows being changed */
650 for (npalette
=0; npalette
<256; ++npalette
)
651 gamma_table
[npalette
] = (png_byte
)npalette
;
654 if (gamma
== PNG_DEFAULT_sRGB
)
655 png_set_sRGB(png_ptr
, info_ptr
, PNG_sRGB_INTENT_ABSOLUTE
);
657 else if (gamma
> 0) /* Else don't set color space information */
659 png_set_gAMA_fixed(png_ptr
, info_ptr
, real_gamma
);
661 /* Just use the sRGB values here. */
662 png_set_cHRM_fixed(png_ptr
, info_ptr
,
664 /* white */ 31270, 32900,
665 /* red */ 64000, 33000,
666 /* green */ 30000, 60000,
667 /* blue */ 15000, 6000
671 /* Insert extra information. */
672 while (insert
!= NULL
)
674 insert
->insert(png_ptr
, info_ptr
, insert
->nparams
, insert
->parameters
);
675 insert
= insert
->next
;
678 /* Write the file header. */
679 png_write_info(png_ptr
, info_ptr
);
681 /* Restrict the filters */
682 png_set_filter(png_ptr
, PNG_FILTER_TYPE_BASE
, filters
);
685 int passes
= png_set_interlace_handling(png_ptr
);
687 png_size_t rowbytes
= png_get_rowbytes(png_ptr
, info_ptr
);
689 row
= malloc(rowbytes
);
692 png_error(png_ptr
, "OOM allocating row buffer");
694 for (pass
= 0; pass
< passes
; ++pass
)
698 for (y
=0; y
<size
; ++y
)
700 generate_row(row
, rowbytes
, y
, color_type
, bit_depth
,
701 gamma_table
, conv
, colors
);
702 png_write_row(png_ptr
, row
);
708 /* Finish writing the file. */
709 png_write_end(png_ptr
, info_ptr
);
712 png_structp nv_ptr
= png_ptr
;
713 png_infop nv_info
= info_ptr
;
717 png_destroy_write_struct(&nv_ptr
, &nv_info
);
725 load_file(png_const_charp name
, png_bytepp result
)
727 FILE *fp
= tmpfile();
731 FILE *ip
= fopen(name
, "rb");
741 if (ch
== EOF
) break;
749 fprintf(stderr
, "%s: read error\n", name
);
759 perror("temporary file");
760 fprintf(stderr
, "temporary file write error\n");
769 /* Round up to a multiple of 4 here to allow an iCCP profile
770 * to be padded to a 4x boundary.
772 png_bytep data
= malloc((total
+3)&~3);
781 if (ch
== EOF
) break;
782 data
[new_size
++] = (png_byte
)ch
;
785 if (ferror(fp
) || new_size
!= total
)
787 perror("temporary file");
788 fprintf(stderr
, "temporary file read error\n");
801 fprintf(stderr
, "%s: out of memory loading file\n", name
);
805 fprintf(stderr
, "%s: empty file\n", name
);
813 fprintf(stderr
, "%s: open failed\n", name
);
820 fprintf(stderr
, "makepng: %s: could not open temporary file\n", name
);
827 load_fake(png_charp param
, png_bytepp profile
)
830 unsigned long long int size
= strtoull(param
, &endptr
, 0/*base*/);
832 /* The 'fake' format is <number>*[string] */
833 if (endptr
!= NULL
&& *endptr
== '*')
835 size_t len
= strlen(++endptr
);
836 size_t result
= (size_t)size
;
838 if (len
== 0) len
= 1; /* capture the terminating '\0' */
840 /* Now repeat that string to fill 'size' bytes. */
841 if (result
== size
&& (*profile
= malloc(result
)) != NULL
)
843 png_bytep out
= *profile
;
846 memset(out
, *endptr
, result
);
852 memcpy(out
, endptr
, len
);
856 memcpy(out
, endptr
, size
);
864 fprintf(stderr
, "%s: size exceeds system limits\n", param
);
873 check_param_count(int nparams
, int expect
)
875 if (nparams
!= expect
)
877 fprintf(stderr
, "bad parameter count (internal error)\n");
883 insert_iCCP(png_structp png_ptr
, png_infop info_ptr
, int nparams
,
886 png_bytep profile
= NULL
;
887 png_uint_32 proflen
= 0;
890 check_param_count(nparams
, 2);
892 switch (params
[1][0])
896 png_size_t filelen
= load_file(params
[1]+1, &profile
);
897 if (filelen
> 0xfffffffc) /* Maximum profile length */
899 fprintf(stderr
, "%s: file too long (%lu) for an ICC profile\n",
900 params
[1]+1, (unsigned long)filelen
);
904 proflen
= (png_uint_32
)filelen
;
908 case '0': case '1': case '2': case '3': case '4':
909 case '5': case '6': case '7': case '8': case '9':
911 png_size_t fake_len
= load_fake(params
[1], &profile
);
913 if (fake_len
> 0) /* else a simple parameter */
915 if (fake_len
> 0xffffffff) /* Maximum profile length */
918 "%s: fake data too long (%lu) for an ICC profile\n",
919 params
[1], (unsigned long)fake_len
);
922 proflen
= (png_uint_32
)(fake_len
& ~3U);
923 /* Always fix up the profile length. */
924 png_save_uint_32(profile
, proflen
);
930 fprintf(stderr
, "--insert iCCP \"%s\": unrecognized\n", params
[1]);
931 fprintf(stderr
, " use '<' to read a file: \"<filename\"\n");
940 "makepng: --insert iCCP %s: profile length made a multiple of 4\n",
943 /* load_file allocates extra space for this padding, the ICC spec requires
944 * padding with zero bytes.
947 profile
[proflen
++] = 0;
950 if (profile
!= NULL
&& proflen
> 3)
952 png_uint_32 prof_header
= png_get_uint_32(profile
);
954 if (prof_header
!= proflen
)
956 fprintf(stderr
, "--insert iCCP %s: profile length field wrong:\n",
958 fprintf(stderr
, " actual %lu, recorded value %lu (corrected)\n",
959 (unsigned long)proflen
, (unsigned long)prof_header
);
960 png_save_uint_32(profile
, proflen
);
964 if (result
&& profile
!= NULL
&& proflen
>=4)
965 png_set_iCCP(png_ptr
, info_ptr
, params
[0], PNG_COMPRESSION_TYPE_BASE
,
976 clear_text(png_text
*text
, png_charp keyword
)
978 text
->compression
= -1; /* none */
981 text
->text_length
= 0; /* libpng calculates this */
982 text
->itxt_length
= 0; /* libpng calculates this */
984 text
->lang_key
= NULL
;
988 set_text(png_structp png_ptr
, png_infop info_ptr
, png_textp text
,
995 png_bytep file
= NULL
;
997 text
->text_length
= load_file(param
+1, &file
);
998 text
->text
= (png_charp
)file
;
1002 case '0': case '1': case '2': case '3': case '4':
1003 case '5': case '6': case '7': case '8': case '9':
1005 png_bytep data
= NULL
;
1006 png_size_t fake_len
= load_fake(param
, &data
);
1008 if (fake_len
> 0) /* else a simple parameter */
1010 text
->text_length
= fake_len
;
1011 text
->text
= (png_charp
)data
;
1021 png_set_text(png_ptr
, info_ptr
, text
, 1);
1023 if (text
->text
!= param
)
1028 insert_tEXt(png_structp png_ptr
, png_infop info_ptr
, int nparams
,
1033 check_param_count(nparams
, 2);
1034 clear_text(&text
, params
[0]);
1035 set_text(png_ptr
, info_ptr
, &text
, params
[1]);
1039 insert_zTXt(png_structp png_ptr
, png_infop info_ptr
, int nparams
,
1044 check_param_count(nparams
, 2);
1045 clear_text(&text
, params
[0]);
1046 text
.compression
= 0; /* deflate */
1047 set_text(png_ptr
, info_ptr
, &text
, params
[1]);
1051 insert_iTXt(png_structp png_ptr
, png_infop info_ptr
, int nparams
,
1056 check_param_count(nparams
, 4);
1057 clear_text(&text
, params
[0]);
1058 text
.compression
= 2; /* iTXt + deflate */
1059 text
.lang
= params
[1];/* language tag */
1060 text
.lang_key
= params
[2]; /* translated keyword */
1061 set_text(png_ptr
, info_ptr
, &text
, params
[3]);
1065 insert_hIST(png_structp png_ptr
, png_infop info_ptr
, int nparams
, png_charpp params
)
1068 png_uint_16 freq
[256];
1070 /* libpng takes the count from the PLTE count; we don't check it here but we
1071 * do set the array to 0 for unspecified entries.
1073 memset(freq
, 0, sizeof freq
);
1074 for (i
=0; i
<nparams
; ++i
)
1076 char *endptr
= NULL
;
1077 unsigned long int l
= strtoul(params
[i
], &endptr
, 0/*base*/);
1079 if (params
[i
][0] && *endptr
== 0 && l
<= 65535)
1080 freq
[i
] = (png_uint_16
)l
;
1084 fprintf(stderr
, "hIST[%d]: %s: invalid frequency\n", i
, params
[i
]);
1089 png_set_hIST(png_ptr
, info_ptr
, freq
);
1094 insert_sPLT(png_structp png_ptr
, png_infop info_ptr
, int nparams
, png_charpp params
)
1096 fprintf(stderr
, "insert sPLT: NYI\n");
1101 find_parameters(png_const_charp what
, png_charp param
, png_charp
*list
,
1104 /* Parameters are separated by '\n' or ':' characters, up to nparams are
1105 * accepted (more is an error) and the number found is returned.
1108 for (i
=0; *param
&& i
<nparams
; ++i
)
1111 while (*++param
) if (*param
== '\n' || *param
== ':')
1113 *param
++ = 0; /* Terminate last parameter */
1114 break; /* And start a new one. */
1120 fprintf(stderr
, "--insert %s: too many parameters (%s)\n", what
, param
);
1124 list
[i
] = NULL
; /* terminates list */
1125 return i
; /* number of parameters filled in */
1129 bad_parameter_count(png_const_charp what
, int nparams
)
1131 fprintf(stderr
, "--insert %s: bad parameter count %d\n", what
, nparams
);
1135 static chunk_insert
*
1136 make_insert(png_const_charp what
,
1137 void (*insert
)(png_structp
, png_infop
, int, png_charpp
),
1138 int nparams
, png_charpp list
)
1143 cip
= malloc(offsetof(chunk_insert
,parameters
) +
1144 nparams
* sizeof (png_charp
));
1148 fprintf(stderr
, "--insert %s: out of memory allocating %d parameters\n",
1154 cip
->insert
= insert
;
1155 cip
->nparams
= nparams
;
1156 for (i
=0; i
<nparams
; ++i
)
1157 cip
->parameters
[i
] = list
[i
];
1162 static chunk_insert
*
1163 find_insert(png_const_charp what
, png_charp param
)
1165 png_uint_32 chunk
= 0;
1166 png_charp parameter_list
[1024];
1169 /* Assemble the chunk name */
1174 if ((ch
>= 65 && ch
<= 90) || (ch
>= 97 && ch
<= 122))
1175 chunk
= (chunk
<< 8) + what
[i
];
1181 if (i
< 4 || what
[4] != 0)
1183 fprintf(stderr
, "makepng --insert \"%s\": invalid chunk name\n", what
);
1187 /* Assemble the parameter list. */
1188 nparams
= find_parameters(what
, param
, parameter_list
, 1024);
1190 # define CHUNK(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d))
1194 case CHUNK(105,67,67,80): /* iCCP */
1196 return make_insert(what
, insert_iCCP
, nparams
, parameter_list
);
1199 case CHUNK(116,69,88,116): /* tEXt */
1201 return make_insert(what
, insert_tEXt
, nparams
, parameter_list
);
1204 case CHUNK(122,84,88,116): /* zTXt */
1206 return make_insert(what
, insert_zTXt
, nparams
, parameter_list
);
1209 case CHUNK(105,84,88,116): /* iTXt */
1211 return make_insert(what
, insert_iTXt
, nparams
, parameter_list
);
1214 case CHUNK(104,73,83,84): /* hIST */
1216 return make_insert(what
, insert_hIST
, nparams
, parameter_list
);
1220 case CHUNK(115,80,76,84): /* sPLT */
1221 return make_insert(what
, insert_sPLT
, nparams
, parameter_list
);
1225 fprintf(stderr
, "makepng --insert \"%s\": unrecognized chunk name\n",
1230 bad_parameter_count(what
, nparams
);
1234 /* This is a not-very-good parser for a sequence of numbers (including 0). It
1235 * doesn't accept some apparently valid things, but it accepts all the sensible
1239 parse_color(char *arg
, unsigned int *colors
)
1241 unsigned int ncolors
= 0;
1243 while (*arg
&& ncolors
< 4)
1247 unsigned long ul
= strtoul(arg
, &ep
, 0);
1251 fprintf(stderr
, "makepng --color=...'%s': too big\n", arg
);
1257 fprintf(stderr
, "makepng --color=...'%s': not a valid color\n", arg
);
1261 if (*ep
) ++ep
; /* skip a separator */
1264 colors
[++ncolors
] = (unsigned int)ul
; /* checked above */
1269 fprintf(stderr
, "makepng --color=...'%s': too many values\n", arg
);
1277 main(int argc
, char **argv
)
1280 const char *file_name
= NULL
;
1281 int color_type
= 8; /* invalid */
1282 int bit_depth
= 32; /* invalid */
1283 unsigned int colors
[5];
1284 unsigned int filters
= PNG_ALL_FILTERS
;
1285 png_fixed_point gamma
= 0; /* not set */
1286 chunk_insert
*head_insert
= NULL
;
1287 chunk_insert
**insert_ptr
= &head_insert
;
1289 memset(colors
, 0, sizeof colors
);
1293 char *arg
= *++argv
;
1295 if (strcmp(arg
, "--sRGB") == 0)
1297 gamma
= PNG_DEFAULT_sRGB
;
1301 if (strcmp(arg
, "--linear") == 0)
1307 if (strcmp(arg
, "--1.8") == 0)
1309 gamma
= PNG_GAMMA_MAC_18
;
1313 if (strcmp(arg
, "--nofilters") == 0)
1315 filters
= PNG_FILTER_NONE
;
1319 if (strncmp(arg
, "--color=", 8) == 0)
1321 parse_color(arg
+8, colors
);
1325 if (argc
>= 3 && strcmp(arg
, "--insert") == 0)
1327 png_const_charp what
= *++argv
;
1328 png_charp param
= *++argv
;
1329 chunk_insert
*new_insert
;
1333 new_insert
= find_insert(what
, param
);
1335 if (new_insert
!= NULL
)
1337 *insert_ptr
= new_insert
;
1338 insert_ptr
= &new_insert
->next
;
1346 fprintf(stderr
, "makepng: %s: invalid option\n", arg
);
1350 if (strcmp(arg
, "palette") == 0)
1352 color_type
= PNG_COLOR_TYPE_PALETTE
;
1356 if (strncmp(arg
, "gray", 4) == 0)
1360 color_type
= PNG_COLOR_TYPE_GRAY
;
1364 else if (strcmp(arg
+4, "a") == 0 ||
1365 strcmp(arg
+4, "alpha") == 0 ||
1366 strcmp(arg
+4, "-alpha") == 0)
1368 color_type
= PNG_COLOR_TYPE_GRAY_ALPHA
;
1373 if (strncmp(arg
, "rgb", 3) == 0)
1377 color_type
= PNG_COLOR_TYPE_RGB
;
1381 else if (strcmp(arg
+3, "a") == 0 ||
1382 strcmp(arg
+3, "alpha") == 0 ||
1383 strcmp(arg
+3, "-alpha") == 0)
1385 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
1390 if (color_type
== 8 && isdigit(arg
[0]))
1392 color_type
= atoi(arg
);
1393 if (color_type
< 0 || color_type
> 6 || color_type
== 1 ||
1396 fprintf(stderr
, "makepng: %s: not a valid color type\n", arg
);
1403 if (bit_depth
== 32 && isdigit(arg
[0]))
1405 bit_depth
= atoi(arg
);
1406 if (bit_depth
<= 0 || bit_depth
> 16 ||
1407 (bit_depth
& -bit_depth
) != bit_depth
)
1409 fprintf(stderr
, "makepng: %s: not a valid bit depth\n", arg
);
1416 if (argc
== 1) /* It's the file name */
1418 fp
= fopen(arg
, "wb");
1421 fprintf(stderr
, "%s: %s: could not open\n", arg
, strerror(errno
));
1429 fprintf(stderr
, "makepng: %s: unknown argument\n", arg
);
1431 } /* argument while loop */
1433 if (color_type
== 8 || bit_depth
== 32)
1435 fprintf(stderr
, "usage: makepng [--sRGB|--linear|--1.8] "
1436 "[--color=...] color-type bit-depth [file-name]\n"
1437 " Make a test PNG file, by default writes to stdout.\n");
1441 /* Check the colors */
1443 const unsigned int lim
= (color_type
== PNG_COLOR_TYPE_PALETTE
? 255U :
1447 for (i
=1; i
<=colors
[0]; ++i
)
1448 if (colors
[i
] > lim
)
1450 fprintf(stderr
, "makepng: --color=...: %u out of range [0..%u]\n",
1456 /* Restrict the filters for more speed to those we know are used for the
1459 if (filters
== PNG_ALL_FILTERS
)
1461 if ((color_type
& PNG_COLOR_MASK_PALETTE
) != 0 || bit_depth
< 8)
1462 filters
= PNG_FILTER_NONE
;
1464 else if (color_type
& PNG_COLOR_MASK_COLOR
) /* rgb */
1467 filters
&= ~(PNG_FILTER_NONE
| PNG_FILTER_AVG
);
1470 filters
= PNG_FILTER_SUB
| PNG_FILTER_PAETH
;
1473 else /* gray 8 or 16-bit */
1474 filters
&= ~PNG_FILTER_NONE
;
1478 int ret
= write_png(&file_name
, fp
, color_type
, bit_depth
, gamma
,
1479 head_insert
, filters
, colors
);
1481 if (ret
!= 0 && file_name
!= NULL
)