Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / png / contrib / libtests / makepng.c
blob9f11b29645b579cac244bb0523a0891039fcc1b9
1 /* makepng.c
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
9 * and license in png.h
11 * Make a test PNG image. The arguments are as follows:
13 * makepng [--sRGB|--linear|--1.8] [--color=<color>] color-type bit-depth \
14 * [file-name]
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
47 * entry 1: 40%-red
48 * entry 2: 80%-blue
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
60 * 16 bits: 256x256
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
69 * type.
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 */
79 #include <stdlib.h>
80 #include <stdio.h>
81 #include <string.h>
82 #include <ctype.h>
83 #include <math.h>
84 #include <errno.h>
86 #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
87 # include <config.h>
88 #endif
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
94 # include <png.h>
95 #else
96 # include "../../png.h"
97 #endif
99 /* This structure is used for inserting extra chunks (the --insert argument, not
100 * documented above.)
102 typedef struct chunk_insert
104 struct chunk_insert *next;
105 void (*insert)(png_structp, png_infop, int, png_charpp);
106 int nparams;
107 png_charp parameters[1];
108 } chunk_insert;
110 static int
111 channels_of_type(int color_type)
113 if (color_type & PNG_COLOR_MASK_PALETTE)
114 return 1;
116 else
118 int channels = 1;
120 if (color_type & PNG_COLOR_MASK_COLOR)
121 channels = 3;
123 if (color_type & PNG_COLOR_MASK_ALPHA)
124 return channels + 1;
126 else
127 return channels;
131 static int
132 pixel_depth_of_type(int color_type, int bit_depth)
134 return channels_of_type(color_type) * bit_depth;
137 static unsigned int
138 image_size_of_type(int color_type, int bit_depth, unsigned int *colors)
140 if (*colors)
141 return 16;
143 else
145 int pixel_depth = pixel_depth_of_type(color_type, bit_depth);
147 if (pixel_depth < 8)
148 return 64;
150 else if (pixel_depth > 16)
151 return 1024;
153 else
154 return 256;
158 static void
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;
169 static int
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
176 * entry 1: 40%-red
177 * entry 2: 80%-blue
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
182 switch (colors[0])
184 default:
185 fprintf(stderr, "makepng: --colors=...: invalid count %u\n",
186 colors[0]);
187 exit(1);
189 case 1:
190 set_color(palette+0, trans+0, colors[1], colors[1], colors[1], 255,
191 gamma_table);
192 return 1;
194 case 2:
195 set_color(palette+0, trans+0, colors[1], colors[1], colors[1],
196 colors[2], gamma_table);
197 return 1;
199 case 3:
200 set_color(palette+0, trans+0, colors[1], colors[2], colors[3], 255,
201 gamma_table);
202 return 1;
204 case 4:
205 set_color(palette+0, trans+0, colors[1], colors[2], colors[3],
206 colors[4], gamma_table);
207 return 1;
209 case 0:
210 if (bit_depth == 1)
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);
214 return 2;
217 else
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)
224 ip = x + (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))) /\
231 (size*size))
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),
240 gamma_table);
243 return ip+1;
248 static void
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 */
256 if (value <= mask)
258 png_uint_32 offset = x >> 3;
260 if (offset < rowbytes && (bit_depth < 16 || offset+1 < rowbytes))
262 row += offset;
264 switch (bit_depth)
266 case 1:
267 case 2:
268 case 4:
269 /* Don't gamma correct - values get smashed */
271 unsigned int shift = (8 - bit_depth) - (x & 0x7U);
273 mask <<= shift;
274 value = (value << shift) & mask;
275 *row = (png_byte)((*row & ~mask) | value);
277 return;
279 default:
280 fprintf(stderr, "makepng: bad bit depth (internal error)\n");
281 exit(1);
283 case 16:
284 value = (unsigned int)floor(65535*pow(value/65535.,conv)+.5);
285 *row++ = (png_byte)(value >> 8);
286 *row = (png_byte)value;
287 return;
289 case 8:
290 *row = gamma_table[value];
291 return;
295 else
297 fprintf(stderr, "makepng: row buffer overflow (internal error)\n");
298 exit(1);
302 else
304 fprintf(stderr, "makepng: component overflow (internal error)\n");
305 exit(1);
309 static void
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.
322 case 1:
324 png_uint_32 x;
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
332 * 0..depth_max
334 luma = (luma*depth_max + size_max) / (2*size_max);
335 set_value(row, rowbytes, x, bit_depth, luma, gamma_table, conv);
338 break;
340 /* 2 channels: the color channel increases in luminosity from top to bottom,
341 * the alpha channel increases in opacity from left to right.
343 case 2:
345 png_uint_32 alpha = (depth_max * y * 2 + size_max) / (2 * size_max);
346 png_uint_32 x;
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,
352 conv);
353 set_value(row, rowbytes, 2*x+1, bit_depth, alpha, gamma_table,
354 conv);
357 break;
359 /* 3 channels: linear combinations of, from the top-left corner clockwise,
360 * black, green, white, red.
362 case 3:
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);
370 png_uint_32 x;
372 /* Interpolate x/depth_max from start to end:
374 * start end difference
375 * red: Y Y 0
376 * green: 0 depth_max depth_max
377 * blue: 0 Y Y
379 for (x=0; x<=size_max; ++x)
381 set_value(row, rowbytes, 3*x+0, bit_depth, /* red */ Y,
382 gamma_table, conv);
383 set_value(row, rowbytes, 3*x+1, bit_depth, /* green */
384 (depth_max * x * 2 + size_max) / (2 * size_max),
385 gamma_table, conv);
386 set_value(row, rowbytes, 3*x+2, bit_depth, /* blue */
387 (Y * x * 2 + size_max) / (2 * size_max),
388 gamma_table, conv);
391 break;
393 /* 4 channels: linear combinations of, from the top-left corner clockwise,
394 * transparent, red, green, blue.
396 case 4:
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
402 * depth_max).
404 png_uint_32 Y = (depth_max * y * 2 + size_max) / (2 * size_max);
405 png_uint_32 x;
407 /* Interpolate x/depth_max from start to end:
409 * start end difference
410 * red: 0 depth_max-Y depth_max-Y
411 * green: 0 Y Y
412 * blue: Y 0 -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),
419 gamma_table, conv);
420 set_value(row, rowbytes, 4*x+1, bit_depth, /* green */
421 (Y * x * 2 + size_max) / (2 * size_max),
422 gamma_table, conv);
423 set_value(row, rowbytes, 4*x+2, bit_depth, /* blue */
424 Y - (Y * x * 2 + size_max) / (2 * size_max),
425 gamma_table, conv);
426 set_value(row, rowbytes, 4*x+3, bit_depth, /* alpha */
427 Y + ((depth_max-Y) * x * 2 + size_max) / (2 * size_max),
428 gamma_table, conv);
431 break;
433 default:
434 fprintf(stderr, "makepng: internal bad channel count\n");
435 exit(2);
438 else if (color_type & PNG_COLOR_MASK_PALETTE)
440 /* Palette with fixed color: the image rows are all 0 and the image width
441 * is 16.
443 memset(row, 0, rowbytes);
446 else if (colors[0] == channels_of_type(color_type))
447 switch (channels_of_type(color_type))
449 case 1:
451 const png_uint_32 luma = colors[1];
452 png_uint_32 x;
454 for (x=0; x<=size_max; ++x)
455 set_value(row, rowbytes, x, bit_depth, luma, gamma_table,
456 conv);
458 break;
460 case 2:
462 const png_uint_32 luma = colors[1];
463 const png_uint_32 alpha = colors[2];
464 png_uint_32 x;
466 for (x=0; x<size_max; ++x)
468 set_value(row, rowbytes, 2*x, bit_depth, luma, gamma_table,
469 conv);
470 set_value(row, rowbytes, 2*x+1, bit_depth, alpha, gamma_table,
471 conv);
474 break;
476 case 3:
478 const png_uint_32 red = colors[1];
479 const png_uint_32 green = colors[2];
480 const png_uint_32 blue = colors[3];
481 png_uint_32 x;
483 for (x=0; x<=size_max; ++x)
485 set_value(row, rowbytes, 3*x+0, bit_depth, red, gamma_table,
486 conv);
487 set_value(row, rowbytes, 3*x+1, bit_depth, green, gamma_table,
488 conv);
489 set_value(row, rowbytes, 3*x+2, bit_depth, blue, gamma_table,
490 conv);
493 break;
495 case 4:
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];
501 png_uint_32 x;
503 for (x=0; x<=size_max; ++x)
505 set_value(row, rowbytes, 4*x+0, bit_depth, red, gamma_table,
506 conv);
507 set_value(row, rowbytes, 4*x+1, bit_depth, green, gamma_table,
508 conv);
509 set_value(row, rowbytes, 4*x+2, bit_depth, blue, gamma_table,
510 conv);
511 set_value(row, rowbytes, 4*x+3, bit_depth, alpha, gamma_table,
512 conv);
515 break;
517 default:
518 fprintf(stderr, "makepng: internal bad channel count\n");
519 exit(2);
522 else
524 fprintf(stderr,
525 "makepng: --color: count(%u) does not match channels(%u)\n",
526 colors[0], channels_of_type(color_type));
527 exit(1);
532 static void PNGCBAPI
533 makepng_warning(png_structp png_ptr, png_const_charp message)
535 const char **ep = png_get_error_ptr(png_ptr);
536 const char *name;
538 if (ep != NULL && *ep != NULL)
539 name = *ep;
541 else
542 name = "makepng";
544 fprintf(stderr, "%s: warning: %s\n", name, message);
547 static void PNGCBAPI
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;
564 if (png_ptr == NULL)
566 fprintf(stderr, "makepng: OOM allocating write structure\n");
567 return 1;
570 if (setjmp(png_jmpbuf(png_ptr)))
572 png_structp nv_ptr = png_ptr;
573 png_infop nv_info = info_ptr;
575 png_ptr = NULL;
576 info_ptr = NULL;
577 png_destroy_write_struct(&nv_ptr, &nv_info);
578 if (row != NULL) free(row);
579 return 1;
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];
594 double conv;
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)
600 gamma = 65909;
602 else if (gamma > 0 && gamma < 1000)
603 gamma = PNG_FP_1;
605 if (gamma > 0)
606 real_gamma = gamma;
609 unsigned int i;
611 if (real_gamma == 45455) for (i=0; i<256; ++i)
613 gamma_table[i] = (png_byte)i;
614 conv = 1.;
617 else
619 /* Convert 'i' from sRGB (45455) to real_gamma, this makes
620 * the images look the same regardless of the gAMA chunk.
622 conv = real_gamma;
623 conv /= 45455;
625 gamma_table[0] = 0;
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)
639 int npalette;
640 png_color palette[256];
641 png_byte trans[256];
643 npalette = generate_palette(palette, trans, bit_depth, gamma_table,
644 colors);
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,
663 /* color x y */
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);
686 int pass;
687 png_size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
689 row = malloc(rowbytes);
691 if (row == NULL)
692 png_error(png_ptr, "OOM allocating row buffer");
694 for (pass = 0; pass < passes; ++pass)
696 unsigned int y;
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;
715 png_ptr = NULL;
716 info_ptr = NULL;
717 png_destroy_write_struct(&nv_ptr, &nv_info);
719 free(row);
720 return 0;
724 static size_t
725 load_file(png_const_charp name, png_bytepp result)
727 FILE *fp = tmpfile();
729 if (fp != NULL)
731 FILE *ip = fopen(name, "rb");
733 if (ip != NULL)
735 size_t total = 0;
736 int ch;
738 for (;;)
740 ch = getc(ip);
741 if (ch == EOF) break;
742 putc(ch, fp);
743 ++total;
746 if (ferror(ip))
748 perror(name);
749 fprintf(stderr, "%s: read error\n", name);
750 (void)fclose(ip);
753 else
755 (void)fclose(ip);
757 if (ferror(fp))
759 perror("temporary file");
760 fprintf(stderr, "temporary file write error\n");
763 else
765 rewind(fp);
767 if (total > 0)
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);
774 if (data != NULL)
776 size_t new_size = 0;
778 for (;;)
780 ch = getc(fp);
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");
789 free(data);
792 else
794 (void)fclose(fp);
795 *result = data;
796 return total;
800 else
801 fprintf(stderr, "%s: out of memory loading file\n", name);
804 else
805 fprintf(stderr, "%s: empty file\n", name);
810 else
812 perror(name);
813 fprintf(stderr, "%s: open failed\n", name);
816 fclose(fp);
819 else
820 fprintf(stderr, "makepng: %s: could not open temporary file\n", name);
822 exit(1);
823 return 0;
826 static png_size_t
827 load_fake(png_charp param, png_bytepp profile)
829 char *endptr = NULL;
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;
845 if (len == 1)
846 memset(out, *endptr, result);
848 else
850 while (size >= len)
852 memcpy(out, endptr, len);
853 out += len;
854 size -= len;
856 memcpy(out, endptr, size);
859 return result;
862 else
864 fprintf(stderr, "%s: size exceeds system limits\n", param);
865 exit(1);
869 return 0;
872 static void
873 check_param_count(int nparams, int expect)
875 if (nparams != expect)
877 fprintf(stderr, "bad parameter count (internal error)\n");
878 exit(1);
882 static void
883 insert_iCCP(png_structp png_ptr, png_infop info_ptr, int nparams,
884 png_charpp params)
886 png_bytep profile = NULL;
887 png_uint_32 proflen = 0;
888 int result;
890 check_param_count(nparams, 2);
892 switch (params[1][0])
894 case '<':
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);
901 exit(1);
904 proflen = (png_uint_32)filelen;
906 break;
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 */
917 fprintf(stderr,
918 "%s: fake data too long (%lu) for an ICC profile\n",
919 params[1], (unsigned long)fake_len);
920 exit(1);
922 proflen = (png_uint_32)(fake_len & ~3U);
923 /* Always fix up the profile length. */
924 png_save_uint_32(profile, proflen);
925 break;
929 default:
930 fprintf(stderr, "--insert iCCP \"%s\": unrecognized\n", params[1]);
931 fprintf(stderr, " use '<' to read a file: \"<filename\"\n");
932 exit(1);
935 result = 1;
937 if (proflen & 3)
939 fprintf(stderr,
940 "makepng: --insert iCCP %s: profile length made a multiple of 4\n",
941 params[1]);
943 /* load_file allocates extra space for this padding, the ICC spec requires
944 * padding with zero bytes.
946 while (proflen & 3)
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",
957 params[1]);
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,
966 profile, proflen);
968 if (profile)
969 free(profile);
971 if (!result)
972 exit(1);
975 static void
976 clear_text(png_text *text, png_charp keyword)
978 text->compression = -1; /* none */
979 text->key = keyword;
980 text->text = NULL;
981 text->text_length = 0; /* libpng calculates this */
982 text->itxt_length = 0; /* libpng calculates this */
983 text->lang = NULL;
984 text->lang_key = NULL;
987 static void
988 set_text(png_structp png_ptr, png_infop info_ptr, png_textp text,
989 png_charp param)
991 switch (param[0])
993 case '<':
995 png_bytep file = NULL;
997 text->text_length = load_file(param+1, &file);
998 text->text = (png_charp)file;
1000 break;
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;
1012 break;
1016 default:
1017 text->text = param;
1018 break;
1021 png_set_text(png_ptr, info_ptr, text, 1);
1023 if (text->text != param)
1024 free(text->text);
1027 static void
1028 insert_tEXt(png_structp png_ptr, png_infop info_ptr, int nparams,
1029 png_charpp params)
1031 png_text text;
1033 check_param_count(nparams, 2);
1034 clear_text(&text, params[0]);
1035 set_text(png_ptr, info_ptr, &text, params[1]);
1038 static void
1039 insert_zTXt(png_structp png_ptr, png_infop info_ptr, int nparams,
1040 png_charpp params)
1042 png_text text;
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]);
1050 static void
1051 insert_iTXt(png_structp png_ptr, png_infop info_ptr, int nparams,
1052 png_charpp params)
1054 png_text text;
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]);
1064 static void
1065 insert_hIST(png_structp png_ptr, png_infop info_ptr, int nparams, png_charpp params)
1067 int i;
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;
1082 else
1084 fprintf(stderr, "hIST[%d]: %s: invalid frequency\n", i, params[i]);
1085 exit(1);
1089 png_set_hIST(png_ptr, info_ptr, freq);
1092 #if 0
1093 static void
1094 insert_sPLT(png_structp png_ptr, png_infop info_ptr, int nparams, png_charpp params)
1096 fprintf(stderr, "insert sPLT: NYI\n");
1098 #endif
1100 static int
1101 find_parameters(png_const_charp what, png_charp param, png_charp *list,
1102 int nparams)
1104 /* Parameters are separated by '\n' or ':' characters, up to nparams are
1105 * accepted (more is an error) and the number found is returned.
1107 int i;
1108 for (i=0; *param && i<nparams; ++i)
1110 list[i] = param;
1111 while (*++param) if (*param == '\n' || *param == ':')
1113 *param++ = 0; /* Terminate last parameter */
1114 break; /* And start a new one. */
1118 if (*param)
1120 fprintf(stderr, "--insert %s: too many parameters (%s)\n", what, param);
1121 exit(1);
1124 list[i] = NULL; /* terminates list */
1125 return i; /* number of parameters filled in */
1128 static void
1129 bad_parameter_count(png_const_charp what, int nparams)
1131 fprintf(stderr, "--insert %s: bad parameter count %d\n", what, nparams);
1132 exit(1);
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)
1140 int i;
1141 chunk_insert *cip;
1143 cip = malloc(offsetof(chunk_insert,parameters) +
1144 nparams * sizeof (png_charp));
1146 if (cip == NULL)
1148 fprintf(stderr, "--insert %s: out of memory allocating %d parameters\n",
1149 what, nparams);
1150 exit(1);
1153 cip->next = NULL;
1154 cip->insert = insert;
1155 cip->nparams = nparams;
1156 for (i=0; i<nparams; ++i)
1157 cip->parameters[i] = list[i];
1159 return cip;
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];
1167 int i, nparams;
1169 /* Assemble the chunk name */
1170 for (i=0; i<4; ++i)
1172 char ch = what[i];
1174 if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))
1175 chunk = (chunk << 8) + what[i];
1177 else
1178 break;
1181 if (i < 4 || what[4] != 0)
1183 fprintf(stderr, "makepng --insert \"%s\": invalid chunk name\n", what);
1184 exit(1);
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))
1192 switch (chunk)
1194 case CHUNK(105,67,67,80): /* iCCP */
1195 if (nparams == 2)
1196 return make_insert(what, insert_iCCP, nparams, parameter_list);
1197 break;
1199 case CHUNK(116,69,88,116): /* tEXt */
1200 if (nparams == 2)
1201 return make_insert(what, insert_tEXt, nparams, parameter_list);
1202 break;
1204 case CHUNK(122,84,88,116): /* zTXt */
1205 if (nparams == 2)
1206 return make_insert(what, insert_zTXt, nparams, parameter_list);
1207 break;
1209 case CHUNK(105,84,88,116): /* iTXt */
1210 if (nparams == 4)
1211 return make_insert(what, insert_iTXt, nparams, parameter_list);
1212 break;
1214 case CHUNK(104,73,83,84): /* hIST */
1215 if (nparams <= 256)
1216 return make_insert(what, insert_hIST, nparams, parameter_list);
1217 break;
1219 #if 0
1220 case CHUNK(115,80,76,84): /* sPLT */
1221 return make_insert(what, insert_sPLT, nparams, parameter_list);
1222 #endif
1224 default:
1225 fprintf(stderr, "makepng --insert \"%s\": unrecognized chunk name\n",
1226 what);
1227 exit(1);
1230 bad_parameter_count(what, nparams);
1231 return NULL;
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
1236 * combinations.
1238 static void
1239 parse_color(char *arg, unsigned int *colors)
1241 unsigned int ncolors = 0;
1243 while (*arg && ncolors < 4)
1245 char *ep = arg;
1247 unsigned long ul = strtoul(arg, &ep, 0);
1249 if (ul > 65535)
1251 fprintf(stderr, "makepng --color=...'%s': too big\n", arg);
1252 exit(1);
1255 if (ep == arg)
1257 fprintf(stderr, "makepng --color=...'%s': not a valid color\n", arg);
1258 exit(1);
1261 if (*ep) ++ep; /* skip a separator */
1262 arg = ep;
1264 colors[++ncolors] = (unsigned int)ul; /* checked above */
1267 if (*arg)
1269 fprintf(stderr, "makepng --color=...'%s': too many values\n", arg);
1270 exit(1);
1273 *colors = ncolors;
1277 main(int argc, char **argv)
1279 FILE *fp = stdout;
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);
1291 while (--argc > 0)
1293 char *arg = *++argv;
1295 if (strcmp(arg, "--sRGB") == 0)
1297 gamma = PNG_DEFAULT_sRGB;
1298 continue;
1301 if (strcmp(arg, "--linear") == 0)
1303 gamma = PNG_FP_1;
1304 continue;
1307 if (strcmp(arg, "--1.8") == 0)
1309 gamma = PNG_GAMMA_MAC_18;
1310 continue;
1313 if (strcmp(arg, "--nofilters") == 0)
1315 filters = PNG_FILTER_NONE;
1316 continue;
1319 if (strncmp(arg, "--color=", 8) == 0)
1321 parse_color(arg+8, colors);
1322 continue;
1325 if (argc >= 3 && strcmp(arg, "--insert") == 0)
1327 png_const_charp what = *++argv;
1328 png_charp param = *++argv;
1329 chunk_insert *new_insert;
1331 argc -= 2;
1333 new_insert = find_insert(what, param);
1335 if (new_insert != NULL)
1337 *insert_ptr = new_insert;
1338 insert_ptr = &new_insert->next;
1341 continue;
1344 if (arg[0] == '-')
1346 fprintf(stderr, "makepng: %s: invalid option\n", arg);
1347 exit(1);
1350 if (strcmp(arg, "palette") == 0)
1352 color_type = PNG_COLOR_TYPE_PALETTE;
1353 continue;
1356 if (strncmp(arg, "gray", 4) == 0)
1358 if (arg[4] == 0)
1360 color_type = PNG_COLOR_TYPE_GRAY;
1361 continue;
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;
1369 continue;
1373 if (strncmp(arg, "rgb", 3) == 0)
1375 if (arg[3] == 0)
1377 color_type = PNG_COLOR_TYPE_RGB;
1378 continue;
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;
1386 continue;
1390 if (color_type == 8 && isdigit(arg[0]))
1392 color_type = atoi(arg);
1393 if (color_type < 0 || color_type > 6 || color_type == 1 ||
1394 color_type == 5)
1396 fprintf(stderr, "makepng: %s: not a valid color type\n", arg);
1397 exit(1);
1400 continue;
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);
1410 exit(1);
1413 continue;
1416 if (argc == 1) /* It's the file name */
1418 fp = fopen(arg, "wb");
1419 if (fp == NULL)
1421 fprintf(stderr, "%s: %s: could not open\n", arg, strerror(errno));
1422 exit(1);
1425 file_name = arg;
1426 continue;
1429 fprintf(stderr, "makepng: %s: unknown argument\n", arg);
1430 exit(1);
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");
1438 exit(1);
1441 /* Check the colors */
1443 const unsigned int lim = (color_type == PNG_COLOR_TYPE_PALETTE ? 255U :
1444 (1U<<bit_depth)-1);
1445 unsigned int i;
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",
1451 colors[i], lim);
1452 exit(1);
1456 /* Restrict the filters for more speed to those we know are used for the
1457 * generated images.
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 */
1466 if (bit_depth == 8)
1467 filters &= ~(PNG_FILTER_NONE | PNG_FILTER_AVG);
1469 else
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)
1482 remove(file_name);
1484 return ret;