fukasawa e60969
/* makepng.c */
fukasawa e60969
#define _ISOC99_SOURCE
fukasawa e60969
/* Copyright: */
fukasawa e60969
#define COPYRIGHT "\251 2013,2015 John Cunningham Bowler"
fukasawa e60969
/*
fukasawa e60969
 * Last changed in libpng 1.6.20 [November 24, 2015]
fukasawa e60969
 *
fukasawa e60969
 * This code is released under the libpng license.
fukasawa e60969
 * For conditions of distribution and use, see the disclaimer
fukasawa e60969
 * and license in png.h
fukasawa e60969
 *
fukasawa e60969
 * Make a test PNG image.  The arguments are as follows:
fukasawa e60969
 *
fukasawa e60969
 *    makepng [--sRGB|--linear|--1.8] [--tRNS] [--nofilters] \
fukasawa e60969
 *       color-type bit-depth [file-name]
fukasawa e60969
 *
fukasawa e60969
 * The color-type may be numeric (and must match the numbers used by the PNG
fukasawa e60969
 * specification) or one of the format names listed below.  The bit-depth is the
fukasawa e60969
 * component bit depth, or the pixel bit-depth for a color-mapped image.
fukasawa e60969
 *
fukasawa e60969
 * Without any options no color-space information is written, with the options
fukasawa e60969
 * an sRGB or the appropriate gAMA chunk is written.  "1.8" refers to the
fukasawa e60969
 * display system used on older Apple computers to correct for high ambient
fukasawa e60969
 * light levels in the viewing environment; it applies a transform of
fukasawa e60969
 * approximately value^(1/1.45) to the color values and so a gAMA chunk of 65909
fukasawa e60969
 * is written (1.45/2.2).
fukasawa e60969
 *
fukasawa e60969
 * The image data is generated internally.  Unless --color is given the images
fukasawa e60969
 * used are as follows:
fukasawa e60969
 *
fukasawa e60969
 * 1 channel: a square image with a diamond, the least luminous colors are on
fukasawa e60969
 *    the edge of the image, the most luminous in the center.
fukasawa e60969
 *
fukasawa e60969
 * 2 channels: the color channel increases in luminosity from top to bottom, the
fukasawa e60969
 *    alpha channel increases in opacity from left to right.
fukasawa e60969
 *
fukasawa e60969
 * 3 channels: linear combinations of, from the top-left corner clockwise,
fukasawa e60969
 *    black, green, white, red.
fukasawa e60969
 *
fukasawa e60969
 * 4 channels: linear combinations of, from the top-left corner clockwise,
fukasawa e60969
 *    transparent, red, green, blue.
fukasawa e60969
 *
fukasawa e60969
 * For color-mapped images a four channel color-map is used and if --tRNS is
fukasawa e60969
 * given the PNG file has a tRNS chunk, as follows:
fukasawa e60969
 *
fukasawa e60969
 * 1-bit: entry 0 is transparent-red, entry 1 is opaque-white
fukasawa e60969
 * 2-bit: entry 0: transparent-green
fukasawa e60969
 *        entry 1: 40%-red
fukasawa e60969
 *        entry 2: 80%-blue
fukasawa e60969
 *        entry 3: opaque-white
fukasawa e60969
 * 4-bit: the 16 combinations of the 2-bit case
fukasawa e60969
 * 8-bit: the 256 combinations of the 4-bit case
fukasawa e60969
 *
fukasawa e60969
 * The palette always has 2^bit-depth entries and the tRNS chunk one fewer.  The
fukasawa e60969
 * image is the 1-channel diamond, but using palette index, not luminosity.
fukasawa e60969
 *
fukasawa e60969
 * For formats other than color-mapped ones if --tRNS is specified a tRNS chunk
fukasawa e60969
 * is generated with all channels equal to the low bits of 0x0101.
fukasawa e60969
 *
fukasawa e60969
 * Image size is determined by the final pixel depth in bits, i.e. channels x
fukasawa e60969
 * bit-depth, as follows:
fukasawa e60969
 *
fukasawa e60969
 * 8 bits or less:    64x64
fukasawa e60969
 * 16 bits:           256x256
fukasawa e60969
 * More than 16 bits: 1024x1024
fukasawa e60969
 *
fukasawa e60969
 * Row filtering is the libpng default but may be turned off (the 'none' filter
fukasawa e60969
 * is used on every row) with the --nofilters option.
fukasawa e60969
 *
fukasawa e60969
 * The images are not interlaced.
fukasawa e60969
 *
fukasawa e60969
 * If file-name is given then the PNG is written to that file, else it is
fukasawa e60969
 * written to stdout.  Notice that stdout is not supported on systems where, by
fukasawa e60969
 * default, it assumes text output; this program makes no attempt to change the
fukasawa e60969
 * text mode of stdout!
fukasawa e60969
 *
fukasawa e60969
 *    makepng --color=<color> ...</color>
fukasawa e60969
 *
fukasawa e60969
 * If --color is given then the whole image has that color, color-mapped images
fukasawa e60969
 * will have exactly one palette entry and all image files with be 16x16 in
fukasawa e60969
 * size.  The color value is 1 to 4 decimal numbers as appropriate for the color
fukasawa e60969
 * type.
fukasawa e60969
 *
fukasawa e60969
 *    makepng --small ...
fukasawa e60969
 *
fukasawa e60969
 * If --small is given the images are no larger than required to include every
fukasawa e60969
 * possible pixel value for the format.
fukasawa e60969
 *
fukasawa e60969
 * For formats with pixels 8 bits or fewer in size the images consist of a
fukasawa e60969
 * single row with 2^pixel-depth pixels, one of every possible value.
fukasawa e60969
 *
fukasawa e60969
 * For formats with 16-bit pixels a 256x256 image is generated containing every
fukasawa e60969
 * possible pixel value.
fukasawa e60969
 *
fukasawa e60969
 * For larger pixel sizes a 256x256 image is generated where the first row
fukasawa e60969
 * consists of each pixel that has identical byte values throughout the pixel
fukasawa e60969
 * followed by rows where the byte values differ within the pixel.
fukasawa e60969
 *
fukasawa e60969
 * In all cases the pixel values are arranged in such a way that the SUB and UP
fukasawa e60969
 * filters give byte sequences for maximal zlib compression.  By default (if
fukasawa e60969
 * --nofilters is not given) the SUB filter is used on the first row and the UP
fukasawa e60969
 * filter on all following rows.
fukasawa e60969
 *
fukasawa e60969
 * The --small option is meant to provide good test-case coverage, however the
fukasawa e60969
 * images are not easy to examine visually.  Without the --small option the
fukasawa e60969
 * images contain identical color values; the pixel values are adjusted
fukasawa e60969
 * according to the gamma encoding with no gamma encoding being interpreted as
fukasawa e60969
 * sRGB.
fukasawa e60969
 *
fukasawa e60969
 * LICENSING
fukasawa e60969
 * =========
fukasawa e60969
 *
fukasawa e60969
 * This code is copyright of the authors, see the COPYRIGHT define above.  The
fukasawa e60969
 * code is licensed as above, using the libpng license.  The code generates
fukasawa e60969
 * images which are solely the product of the code; the options choose which of
fukasawa e60969
 * the many possibilities to generate.  The images that result (but not the code
fukasawa e60969
 * which generates them) are licensed as defined here:
fukasawa e60969
 *
fukasawa e60969
 * IMPORTANT: the COPYRIGHT #define must contain ISO-Latin-1 characters, the
fukasawa e60969
 * IMAGE_LICENSING #define must contain UTF-8 characters.  The 'copyright'
fukasawa e60969
 * symbol 0xA9U (\251) in ISO-Latin-1 encoding and 0xC20xA9 (\302\251) in UTF-8.
fukasawa e60969
 */
fukasawa e60969
#define IMAGE_LICENSING "Dedicated to the public domain per Creative Commons "\
fukasawa e60969
    "license \"CC0 1.0\"; https://creativecommons.org/publicdomain/zero/1.0/"
fukasawa e60969
fukasawa e60969
#include <stddef.h> /* for offsetof */</stddef.h>
fukasawa e60969
#include <stdlib.h></stdlib.h>
fukasawa e60969
#include <stdio.h></stdio.h>
fukasawa e60969
#include <string.h></string.h>
fukasawa e60969
#include <ctype.h></ctype.h>
fukasawa e60969
#include <math.h></math.h>
fukasawa e60969
#include <errno.h></errno.h>
fukasawa e60969
#include <assert.h></assert.h>
fukasawa e60969
#include <stdint.h></stdint.h>
fukasawa e60969
fukasawa e60969
#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
fukasawa e60969
#  include <config.h></config.h>
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Define the following to use this test against your installed libpng, rather
fukasawa e60969
 * than the one being built here:
fukasawa e60969
 */
fukasawa e60969
#ifdef PNG_FREESTANDING_TESTS
fukasawa e60969
#  include <png.h></png.h>
fukasawa e60969
#else
fukasawa e60969
#  include "../../png.h"
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#include <zlib.h></zlib.h>
fukasawa e60969
fukasawa e60969
/* Work round for GCC complaints about casting a (double) function result to
fukasawa e60969
 * an unsigned:
fukasawa e60969
 */
fukasawa e60969
static unsigned int
fukasawa e60969
flooru(double d)
fukasawa e60969
{
fukasawa e60969
   d = floor(d);
fukasawa e60969
   return (unsigned int)d;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_byte
fukasawa e60969
floorb(double d)
fukasawa e60969
{
fukasawa e60969
   d = floor(d);
fukasawa e60969
   return (png_byte)d;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* This structure is used for inserting extra chunks (the --insert argument, not
fukasawa e60969
 * documented above.)
fukasawa e60969
 */
fukasawa e60969
typedef struct chunk_insert
fukasawa e60969
{
fukasawa e60969
   struct chunk_insert *next;
fukasawa e60969
   void               (*insert)(png_structp, png_infop, int, png_charpp);
fukasawa e60969
   int                  nparams;
fukasawa e60969
   png_charp            parameters[1];
fukasawa e60969
} chunk_insert;
fukasawa e60969
fukasawa e60969
static unsigned int
fukasawa e60969
channels_of_type(int color_type)
fukasawa e60969
{
fukasawa e60969
   if (color_type & PNG_COLOR_MASK_PALETTE)
fukasawa e60969
      return 1;
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      int channels = 1;
fukasawa e60969
fukasawa e60969
      if (color_type & PNG_COLOR_MASK_COLOR)
fukasawa e60969
         channels = 3;
fukasawa e60969
fukasawa e60969
      if (color_type & PNG_COLOR_MASK_ALPHA)
fukasawa e60969
         return channels + 1;
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         return channels;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static unsigned int
fukasawa e60969
pixel_depth_of_type(int color_type, int bit_depth)
fukasawa e60969
{
fukasawa e60969
   return channels_of_type(color_type) * bit_depth;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static unsigned int
fukasawa e60969
image_size_of_type(int color_type, int bit_depth, unsigned int *colors,
fukasawa e60969
   int small)
fukasawa e60969
{
fukasawa e60969
   if (*colors)
fukasawa e60969
      return 16;
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      int pixel_depth = pixel_depth_of_type(color_type, bit_depth);
fukasawa e60969
fukasawa e60969
      if (small)
fukasawa e60969
      {
fukasawa e60969
         if (pixel_depth <= 8) /* there will be one row */
fukasawa e60969
            return 1 << pixel_depth;
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            return 256;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (pixel_depth < 8)
fukasawa e60969
         return 64;
fukasawa e60969
fukasawa e60969
      else if (pixel_depth > 16)
fukasawa e60969
         return 1024;
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         return 256;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
set_color(png_colorp color, png_bytep trans, unsigned int red,
fukasawa e60969
   unsigned int green, unsigned int blue, unsigned int alpha,
fukasawa e60969
   png_const_bytep gamma_table)
fukasawa e60969
{
fukasawa e60969
   color->red = gamma_table[red];
fukasawa e60969
   color->green = gamma_table[green];
fukasawa e60969
   color->blue = gamma_table[blue];
fukasawa e60969
   *trans = (png_byte)alpha;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
generate_palette(png_colorp palette, png_bytep trans, int bit_depth,
fukasawa e60969
   png_const_bytep gamma_table, unsigned int *colors)
fukasawa e60969
{
fukasawa e60969
   /*
fukasawa e60969
    * 1-bit: entry 0 is transparent-red, entry 1 is opaque-white
fukasawa e60969
    * 2-bit: entry 0: transparent-green
fukasawa e60969
    *        entry 1: 40%-red
fukasawa e60969
    *        entry 2: 80%-blue
fukasawa e60969
    *        entry 3: opaque-white
fukasawa e60969
    * 4-bit: the 16 combinations of the 2-bit case
fukasawa e60969
    * 8-bit: the 256 combinations of the 4-bit case
fukasawa e60969
    */
fukasawa e60969
   switch (colors[0])
fukasawa e60969
   {
fukasawa e60969
      default:
fukasawa e60969
         fprintf(stderr, "makepng: --colors=...: invalid count %u\n",
fukasawa e60969
            colors[0]);
fukasawa e60969
         exit(1);
fukasawa e60969
fukasawa e60969
      case 1:
fukasawa e60969
         set_color(palette+0, trans+0, colors[1], colors[1], colors[1], 255,
fukasawa e60969
            gamma_table);
fukasawa e60969
         return 1;
fukasawa e60969
fukasawa e60969
      case 2:
fukasawa e60969
         set_color(palette+0, trans+0, colors[1], colors[1], colors[1],
fukasawa e60969
            colors[2], gamma_table);
fukasawa e60969
         return 1;
fukasawa e60969
fukasawa e60969
      case 3:
fukasawa e60969
         set_color(palette+0, trans+0, colors[1], colors[2], colors[3], 255,
fukasawa e60969
            gamma_table);
fukasawa e60969
         return 1;
fukasawa e60969
fukasawa e60969
      case 4:
fukasawa e60969
         set_color(palette+0, trans+0, colors[1], colors[2], colors[3],
fukasawa e60969
            colors[4], gamma_table);
fukasawa e60969
         return 1;
fukasawa e60969
fukasawa e60969
      case 0:
fukasawa e60969
         if (bit_depth == 1)
fukasawa e60969
         {
fukasawa e60969
            set_color(palette+0, trans+0, 255, 0, 0, 0, gamma_table);
fukasawa e60969
            set_color(palette+1, trans+1, 255, 255, 255, 255, gamma_table);
fukasawa e60969
            return 2;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
         {
fukasawa e60969
            unsigned int size = 1U << (bit_depth/2); /* 2, 4 or 16 */
fukasawa e60969
            unsigned int x, y;
fukasawa e60969
            volatile unsigned int ip = 0;
fukasawa e60969
fukasawa e60969
            for (x=0; x
fukasawa e60969
            {
fukasawa e60969
               ip = x + (size * y);
fukasawa e60969
fukasawa e60969
               /* size is at most 16, so the scaled value below fits in 16 bits
fukasawa e60969
                */
fukasawa e60969
#              define interp(pos, c1, c2) ((pos * c1) + ((size-pos) * c2))
fukasawa e60969
#              define xyinterp(x, y, c1, c2, c3, c4) (((size * size / 2) +\
fukasawa e60969
                  (interp(x, c1, c2) * y + (size-y) * interp(x, c3, c4))) /\
fukasawa e60969
                  (size*size))
fukasawa e60969
fukasawa e60969
               set_color(palette+ip, trans+ip,
fukasawa e60969
                  /* color:    green, red,blue,white */
fukasawa e60969
                  xyinterp(x, y,   0, 255,   0, 255),
fukasawa e60969
                  xyinterp(x, y, 255,   0,   0, 255),
fukasawa e60969
                  xyinterp(x, y,   0,   0, 255, 255),
fukasawa e60969
                  /* alpha:        0, 102, 204, 255) */
fukasawa e60969
                  xyinterp(x, y,   0, 102, 204, 255),
fukasawa e60969
                  gamma_table);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            return ip+1;
fukasawa e60969
         }
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
set_value(png_bytep row, size_t rowbytes, png_uint_32 x, unsigned int bit_depth,
fukasawa e60969
   png_uint_32 value, png_const_bytep gamma_table, double conv)
fukasawa e60969
{
fukasawa e60969
   unsigned int mask = (1U << bit_depth)-1;
fukasawa e60969
fukasawa e60969
   x *= bit_depth;  /* Maximum x is 4*1024, maximum bit_depth is 16 */
fukasawa e60969
fukasawa e60969
   if (value <= mask)
fukasawa e60969
   {
fukasawa e60969
      png_uint_32 offset = x >> 3;
fukasawa e60969
fukasawa e60969
      if (offset < rowbytes && (bit_depth < 16 || offset+1 < rowbytes))
fukasawa e60969
      {
fukasawa e60969
         row += offset;
fukasawa e60969
fukasawa e60969
         switch (bit_depth)
fukasawa e60969
         {
fukasawa e60969
            case 1:
fukasawa e60969
            case 2:
fukasawa e60969
            case 4:
fukasawa e60969
               /* Don't gamma correct - values get smashed */
fukasawa e60969
               {
fukasawa e60969
                  unsigned int shift = (8 - bit_depth) - (x & 0x7U);
fukasawa e60969
fukasawa e60969
                  mask <<= shift;
fukasawa e60969
                  value = (value << shift) & mask;
fukasawa e60969
                  *row = (png_byte)((*row & ~mask) | value);
fukasawa e60969
               }
fukasawa e60969
               return;
fukasawa e60969
fukasawa e60969
            default:
fukasawa e60969
               fprintf(stderr, "makepng: bad bit depth (internal error)\n");
fukasawa e60969
               exit(1);
fukasawa e60969
fukasawa e60969
            case 16:
fukasawa e60969
               value = flooru(65535*pow(value/65535.,conv)+.5);
fukasawa e60969
               *row++ = (png_byte)(value >> 8);
fukasawa e60969
               *row = (png_byte)value;
fukasawa e60969
               return;
fukasawa e60969
fukasawa e60969
            case 8:
fukasawa e60969
               *row = gamma_table[value];
fukasawa e60969
               return;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "makepng: row buffer overflow (internal error)\n");
fukasawa e60969
         exit(1);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "makepng: component overflow (internal error)\n");
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int /* filter mask for row */
fukasawa e60969
generate_row(png_bytep row, size_t rowbytes, unsigned int y, int color_type,
fukasawa e60969
   int bit_depth, png_const_bytep gamma_table, double conv,
fukasawa e60969
   unsigned int *colors, int small)
fukasawa e60969
{
fukasawa e60969
   int filters = 0; /* file *MASK*, 0 means the default, not NONE */
fukasawa e60969
   png_uint_32 size_max =
fukasawa e60969
      image_size_of_type(color_type, bit_depth, colors, small)-1;
fukasawa e60969
   png_uint_32 depth_max = (1U << bit_depth)-1; /* up to 65536 */
fukasawa e60969
fukasawa e60969
   if (colors[0] == 0) if (small)
fukasawa e60969
   {
fukasawa e60969
      unsigned int pixel_depth = pixel_depth_of_type(color_type, bit_depth);
fukasawa e60969
fukasawa e60969
      /* For pixel depths less than 16 generate a single row containing all the
fukasawa e60969
       * possible pixel values.  For 16 generate all 65536 byte pair
fukasawa e60969
       * combinations in a 256x256 pixel array.
fukasawa e60969
       */
fukasawa e60969
      switch (pixel_depth)
fukasawa e60969
      {
fukasawa e60969
         case 1:
fukasawa e60969
            assert(y == 0 && rowbytes == 1 && size_max == 1);
fukasawa e60969
            row[0] = 0x6CU; /* binary: 01101100, only top 2 bits used */
fukasawa e60969
            filters = PNG_FILTER_NONE;
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 2:
fukasawa e60969
            assert(y == 0 && rowbytes == 1 && size_max == 3);
fukasawa e60969
            row[0] = 0x1BU; /* binary 00011011, all bits used */
fukasawa e60969
            filters = PNG_FILTER_NONE;
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 4:
fukasawa e60969
            assert(y == 0 && rowbytes == 8 && size_max == 15);
fukasawa e60969
            row[0] = 0x01U;
fukasawa e60969
            row[1] = 0x23U; /* SUB gives 0x22U for all following bytes */
fukasawa e60969
            row[2] = 0x45U;
fukasawa e60969
            row[3] = 0x67U;
fukasawa e60969
            row[4] = 0x89U;
fukasawa e60969
            row[5] = 0xABU;
fukasawa e60969
            row[6] = 0xCDU;
fukasawa e60969
            row[7] = 0xEFU;
fukasawa e60969
            filters = PNG_FILTER_SUB;
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 8:
fukasawa e60969
            /* The row will have all the pixel values in order starting with
fukasawa e60969
             * '1', the SUB filter will change every byte into '1' (including
fukasawa e60969
             * the last, which generates pixel value '0').  Since the SUB filter
fukasawa e60969
             * has value 1 this should result in maximum compression.
fukasawa e60969
             */
fukasawa e60969
            assert(y == 0 && rowbytes == 256 && size_max == 255);
fukasawa e60969
            for (;;)
fukasawa e60969
            {
fukasawa e60969
               row[size_max] = 0xFFU & (size_max+1);
fukasawa e60969
               if (size_max == 0)
fukasawa e60969
                  break;
fukasawa e60969
               --size_max;
fukasawa e60969
            }
fukasawa e60969
            filters = PNG_FILTER_SUB;
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 16:
fukasawa e60969
            /* Rows are generated such that each row has a constant difference
fukasawa e60969
             * between the first and second byte of each pixel and so that the
fukasawa e60969
             * difference increases by 1 at each row.  The rows start with the
fukasawa e60969
             * first byte value of 0 and the value increases to 255 across the
fukasawa e60969
             * row.
fukasawa e60969
             *
fukasawa e60969
             * The difference starts at 1, so the first row is:
fukasawa e60969
             *
fukasawa e60969
             *     0 1 1 2 2 3 3 4 ... 254 255 255 0
fukasawa e60969
             *
fukasawa e60969
             * This means that running the SUB filter on the first row produces:
fukasawa e60969
             *
fukasawa e60969
             *   [SUB==1] 0 1 0 1 0 1...
fukasawa e60969
             *
fukasawa e60969
             * Then the difference is 2 on the next row, giving:
fukasawa e60969
             *
fukasawa e60969
             *    0 2 1 3 2 4 3 5 ... 254 0 255 1
fukasawa e60969
             *
fukasawa e60969
             * When the UP filter is run on this libpng produces:
fukasawa e60969
             *
fukasawa e60969
             *   [UP ==2] 0 1 0 1 0 1...
fukasawa e60969
             *
fukasawa e60969
             * And so on for all the remain rows to the final two * rows:
fukasawa e60969
             *
fukasawa e60969
             *    row 254: 0 255 1 0 2 1 3 2 4 3 ... 254 253 255 254
fukasawa e60969
             *    row 255: 0   0 1 1 2 2 3 3 4 4 ... 254 254 255 255
fukasawa e60969
             */
fukasawa e60969
            assert(rowbytes == 512 && size_max == 255);
fukasawa e60969
            for (;;)
fukasawa e60969
            {
fukasawa e60969
               row[2*size_max  ] = 0xFFU & size_max;
fukasawa e60969
               row[2*size_max+1] = 0xFFU & (size_max+y+1);
fukasawa e60969
               if (size_max == 0)
fukasawa e60969
                  break;
fukasawa e60969
               --size_max;
fukasawa e60969
            }
fukasawa e60969
            /* The first row must include PNG_FILTER_UP so that libpng knows we
fukasawa e60969
             * need to keep it for the following row:
fukasawa e60969
             */
fukasawa e60969
            filters = (y == 0 ? PNG_FILTER_SUB+PNG_FILTER_UP : PNG_FILTER_UP);
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 24:
fukasawa e60969
         case 32:
fukasawa e60969
         case 48:
fukasawa e60969
         case 64:
fukasawa e60969
            /* The rows are filled by an alogorithm similar to the above, in the
fukasawa e60969
             * first row pixel bytes are all equal, increasing from 0 by 1 for
fukasawa e60969
             * each pixel.  In the second row the bytes within a pixel are
fukasawa e60969
             * incremented 1,3,5,7,... from the previous row byte.  Using an odd
fukasawa e60969
             * number ensures all the possible byte values are used.
fukasawa e60969
             */
fukasawa e60969
            assert(size_max == 255 && rowbytes == 256*(pixel_depth>>3));
fukasawa e60969
            pixel_depth >>= 3; /* now in bytes */
fukasawa e60969
            while (rowbytes > 0)
fukasawa e60969
            {
fukasawa e60969
               const size_t pixel_index = --rowbytes/pixel_depth;
fukasawa e60969
fukasawa e60969
               if (y == 0)
fukasawa e60969
                  row[rowbytes] = 0xFFU & pixel_index;
fukasawa e60969
fukasawa e60969
               else
fukasawa e60969
               {
fukasawa e60969
                  const size_t byte_offset =
fukasawa e60969
                     rowbytes - pixel_index * pixel_depth;
fukasawa e60969
fukasawa e60969
                  row[rowbytes] =
fukasawa e60969
                     0xFFU & (pixel_index + (byte_offset * 2*y) + 1);
fukasawa e60969
               }
fukasawa e60969
            }
fukasawa e60969
            filters = (y == 0 ? PNG_FILTER_SUB+PNG_FILTER_UP : PNG_FILTER_UP);
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         default:
fukasawa e60969
            assert(0/*NOT REACHED*/);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else switch (channels_of_type(color_type))
fukasawa e60969
   {
fukasawa e60969
   /* 1 channel: a square image with a diamond, the least luminous colors are on
fukasawa e60969
    *    the edge of the image, the most luminous in the center.
fukasawa e60969
    */
fukasawa e60969
      case 1:
fukasawa e60969
         {
fukasawa e60969
            png_uint_32 x;
fukasawa e60969
            png_uint_32 base = 2*size_max - abs(2*y-size_max);
fukasawa e60969
fukasawa e60969
            for (x=0; x<=size_max; ++x)
fukasawa e60969
            {
fukasawa e60969
               png_uint_32 luma = base - abs(2*x-size_max);
fukasawa e60969
fukasawa e60969
               /* 'luma' is now in the range 0..2*size_max, we need
fukasawa e60969
                * 0..depth_max
fukasawa e60969
                */
fukasawa e60969
               luma = (luma*depth_max + size_max) / (2*size_max);
fukasawa e60969
               set_value(row, rowbytes, x, bit_depth, luma, gamma_table, conv);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
   /* 2 channels: the color channel increases in luminosity from top to bottom,
fukasawa e60969
    *    the alpha channel increases in opacity from left to right.
fukasawa e60969
    */
fukasawa e60969
      case 2:
fukasawa e60969
         {
fukasawa e60969
            png_uint_32 alpha = (depth_max * y * 2 + size_max) / (2 * size_max);
fukasawa e60969
            png_uint_32 x;
fukasawa e60969
fukasawa e60969
            for (x=0; x<=size_max; ++x)
fukasawa e60969
            {
fukasawa e60969
               set_value(row, rowbytes, 2*x, bit_depth,
fukasawa e60969
                  (depth_max * x * 2 + size_max) / (2 * size_max), gamma_table,
fukasawa e60969
                  conv);
fukasawa e60969
               set_value(row, rowbytes, 2*x+1, bit_depth, alpha, gamma_table,
fukasawa e60969
                  conv);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
   /* 3 channels: linear combinations of, from the top-left corner clockwise,
fukasawa e60969
    *    black, green, white, red.
fukasawa e60969
    */
fukasawa e60969
      case 3:
fukasawa e60969
         {
fukasawa e60969
            /* x0: the black->red scale (the value of the red component) at the
fukasawa e60969
             *     start of the row (blue and green are 0).
fukasawa e60969
             * x1: the green->white scale (the value of the red and blue
fukasawa e60969
             *     components at the end of the row; green is depth_max).
fukasawa e60969
             */
fukasawa e60969
            png_uint_32 Y = (depth_max * y * 2 + size_max) / (2 * size_max);
fukasawa e60969
            png_uint_32 x;
fukasawa e60969
fukasawa e60969
            /* Interpolate x/depth_max from start to end:
fukasawa e60969
             *
fukasawa e60969
             *        start end         difference
fukasawa e60969
             * red:     Y    Y            0
fukasawa e60969
             * green:   0   depth_max   depth_max
fukasawa e60969
             * blue:    0    Y            Y
fukasawa e60969
             */
fukasawa e60969
            for (x=0; x<=size_max; ++x)
fukasawa e60969
            {
fukasawa e60969
               set_value(row, rowbytes, 3*x+0, bit_depth, /* red */ Y,
fukasawa e60969
                     gamma_table, conv);
fukasawa e60969
               set_value(row, rowbytes, 3*x+1, bit_depth, /* green */
fukasawa e60969
                  (depth_max * x * 2 + size_max) / (2 * size_max),
fukasawa e60969
                  gamma_table, conv);
fukasawa e60969
               set_value(row, rowbytes, 3*x+2, bit_depth, /* blue */
fukasawa e60969
                  (Y * x * 2 + size_max) / (2 * size_max),
fukasawa e60969
                  gamma_table, conv);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
   /* 4 channels: linear combinations of, from the top-left corner clockwise,
fukasawa e60969
    *    transparent, red, green, blue.
fukasawa e60969
    */
fukasawa e60969
      case 4:
fukasawa e60969
         {
fukasawa e60969
            /* x0: the transparent->blue scale (the value of the blue and alpha
fukasawa e60969
             *     components) at the start of the row (red and green are 0).
fukasawa e60969
             * x1: the red->green scale (the value of the red and green
fukasawa e60969
             *     components at the end of the row; blue is 0 and alpha is
fukasawa e60969
             *     depth_max).
fukasawa e60969
             */
fukasawa e60969
            png_uint_32 Y = (depth_max * y * 2 + size_max) / (2 * size_max);
fukasawa e60969
            png_uint_32 x;
fukasawa e60969
fukasawa e60969
            /* Interpolate x/depth_max from start to end:
fukasawa e60969
             *
fukasawa e60969
             *        start    end       difference
fukasawa e60969
             * red:     0   depth_max-Y depth_max-Y
fukasawa e60969
             * green:   0       Y             Y
fukasawa e60969
             * blue:    Y       0            -Y
fukasawa e60969
             * alpha:   Y    depth_max  depth_max-Y
fukasawa e60969
             */
fukasawa e60969
            for (x=0; x<=size_max; ++x)
fukasawa e60969
            {
fukasawa e60969
               set_value(row, rowbytes, 4*x+0, bit_depth, /* red */
fukasawa e60969
                  ((depth_max-Y) * x * 2 + size_max) / (2 * size_max),
fukasawa e60969
                  gamma_table, conv);
fukasawa e60969
               set_value(row, rowbytes, 4*x+1, bit_depth, /* green */
fukasawa e60969
                  (Y * x * 2 + size_max) / (2 * size_max),
fukasawa e60969
                  gamma_table, conv);
fukasawa e60969
               set_value(row, rowbytes, 4*x+2, bit_depth, /* blue */
fukasawa e60969
                  Y - (Y * x * 2 + size_max) / (2 * size_max),
fukasawa e60969
                  gamma_table, conv);
fukasawa e60969
               set_value(row, rowbytes, 4*x+3, bit_depth, /* alpha */
fukasawa e60969
                  Y + ((depth_max-Y) * x * 2 + size_max) / (2 * size_max),
fukasawa e60969
                  gamma_table, conv);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      default:
fukasawa e60969
         fprintf(stderr, "makepng: internal bad channel count\n");
fukasawa e60969
         exit(2);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (color_type & PNG_COLOR_MASK_PALETTE)
fukasawa e60969
   {
fukasawa e60969
      /* Palette with fixed color: the image rows are all 0 and the image width
fukasawa e60969
       * is 16.
fukasawa e60969
       */
fukasawa e60969
      memset(row, 0, rowbytes);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (colors[0] == channels_of_type(color_type))
fukasawa e60969
      switch (channels_of_type(color_type))
fukasawa e60969
      {
fukasawa e60969
         case 1:
fukasawa e60969
            {
fukasawa e60969
               const png_uint_32 luma = colors[1];
fukasawa e60969
               png_uint_32 x;
fukasawa e60969
fukasawa e60969
               for (x=0; x<=size_max; ++x)
fukasawa e60969
                  set_value(row, rowbytes, x, bit_depth, luma, gamma_table,
fukasawa e60969
                     conv);
fukasawa e60969
            }
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 2:
fukasawa e60969
            {
fukasawa e60969
               const png_uint_32 luma = colors[1];
fukasawa e60969
               const png_uint_32 alpha = colors[2];
fukasawa e60969
               png_uint_32 x;
fukasawa e60969
fukasawa e60969
               for (x=0; x
fukasawa e60969
               {
fukasawa e60969
                  set_value(row, rowbytes, 2*x, bit_depth, luma, gamma_table,
fukasawa e60969
                     conv);
fukasawa e60969
                  set_value(row, rowbytes, 2*x+1, bit_depth, alpha, gamma_table,
fukasawa e60969
                     conv);
fukasawa e60969
               }
fukasawa e60969
            }
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 3:
fukasawa e60969
            {
fukasawa e60969
               const png_uint_32 red = colors[1];
fukasawa e60969
               const png_uint_32 green = colors[2];
fukasawa e60969
               const png_uint_32 blue = colors[3];
fukasawa e60969
               png_uint_32 x;
fukasawa e60969
fukasawa e60969
               for (x=0; x<=size_max; ++x)
fukasawa e60969
               {
fukasawa e60969
                  set_value(row, rowbytes, 3*x+0, bit_depth, red, gamma_table,
fukasawa e60969
                     conv);
fukasawa e60969
                  set_value(row, rowbytes, 3*x+1, bit_depth, green, gamma_table,
fukasawa e60969
                     conv);
fukasawa e60969
                  set_value(row, rowbytes, 3*x+2, bit_depth, blue, gamma_table,
fukasawa e60969
                     conv);
fukasawa e60969
               }
fukasawa e60969
            }
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 4:
fukasawa e60969
            {
fukasawa e60969
               const png_uint_32 red = colors[1];
fukasawa e60969
               const png_uint_32 green = colors[2];
fukasawa e60969
               const png_uint_32 blue = colors[3];
fukasawa e60969
               const png_uint_32 alpha = colors[4];
fukasawa e60969
               png_uint_32 x;
fukasawa e60969
fukasawa e60969
               for (x=0; x<=size_max; ++x)
fukasawa e60969
               {
fukasawa e60969
                  set_value(row, rowbytes, 4*x+0, bit_depth, red, gamma_table,
fukasawa e60969
                     conv);
fukasawa e60969
                  set_value(row, rowbytes, 4*x+1, bit_depth, green, gamma_table,
fukasawa e60969
                     conv);
fukasawa e60969
                  set_value(row, rowbytes, 4*x+2, bit_depth, blue, gamma_table,
fukasawa e60969
                     conv);
fukasawa e60969
                  set_value(row, rowbytes, 4*x+3, bit_depth, alpha, gamma_table,
fukasawa e60969
                     conv);
fukasawa e60969
               }
fukasawa e60969
            }
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
         default:
fukasawa e60969
            fprintf(stderr, "makepng: internal bad channel count\n");
fukasawa e60969
            exit(2);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr,
fukasawa e60969
         "makepng: --color: count(%u) does not match channels(%u)\n",
fukasawa e60969
         colors[0], channels_of_type(color_type));
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return filters;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
makepng_warning(png_structp png_ptr, png_const_charp message)
fukasawa e60969
{
fukasawa e60969
   const char **ep = png_get_error_ptr(png_ptr);
fukasawa e60969
   const char *name;
fukasawa e60969
fukasawa e60969
   if (ep != NULL && *ep != NULL)
fukasawa e60969
      name = *ep;
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      name = "makepng";
fukasawa e60969
fukasawa e60969
  fprintf(stderr, "%s: warning: %s\n", name, message);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
makepng_error(png_structp png_ptr, png_const_charp message)
fukasawa e60969
{
fukasawa e60969
   makepng_warning(png_ptr, message);
fukasawa e60969
   png_longjmp(png_ptr, 1);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int /* 0 on success, else an error code */
fukasawa e60969
write_png(const char **name, FILE *fp, int color_type, int bit_depth,
fukasawa e60969
   volatile png_fixed_point gamma, chunk_insert * volatile insert,
fukasawa e60969
   unsigned int filters, unsigned int *colors, int small, int tRNS)
fukasawa e60969
{
fukasawa e60969
   png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
fukasawa e60969
      name, makepng_error, makepng_warning);
fukasawa e60969
   volatile png_infop info_ptr = NULL;
fukasawa e60969
   volatile png_bytep row = NULL;
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "makepng: OOM allocating write structure\n");
fukasawa e60969
      return 1;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (setjmp(png_jmpbuf(png_ptr)))
fukasawa e60969
   {
fukasawa e60969
      png_structp nv_ptr = png_ptr;
fukasawa e60969
      png_infop nv_info = info_ptr;
fukasawa e60969
fukasawa e60969
      png_ptr = NULL;
fukasawa e60969
      info_ptr = NULL;
fukasawa e60969
      png_destroy_write_struct(&nv_ptr, &nv_info);
fukasawa e60969
      if (row != NULL) free(row);
fukasawa e60969
      return 1;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Allow benign errors so that we can write PNGs with errors */
fukasawa e60969
   png_set_benign_errors(png_ptr, 1/*allowed*/);
fukasawa e60969
fukasawa e60969
   /* Max out the text compression level in an attempt to make the license
fukasawa e60969
    * small.   If --small then do the same for the IDAT.
fukasawa e60969
    */
fukasawa e60969
   if (small)
fukasawa e60969
      png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
fukasawa e60969
fukasawa e60969
   png_set_text_compression_level(png_ptr, Z_BEST_COMPRESSION);
fukasawa e60969
fukasawa e60969
   png_init_io(png_ptr, fp);
fukasawa e60969
fukasawa e60969
   info_ptr = png_create_info_struct(png_ptr);
fukasawa e60969
   if (info_ptr == NULL)
fukasawa e60969
      png_error(png_ptr, "OOM allocating info structure");
fukasawa e60969
fukasawa e60969
   {
fukasawa e60969
      const unsigned int size =
fukasawa e60969
         image_size_of_type(color_type, bit_depth, colors, small);
fukasawa e60969
      unsigned int ysize;
fukasawa e60969
      png_fixed_point real_gamma = 45455; /* For sRGB */
fukasawa e60969
      png_byte gamma_table[256];
fukasawa e60969
      double conv;
fukasawa e60969
fukasawa e60969
      /* Normally images are square, but with 'small' we want to simply generate
fukasawa e60969
       * all the pixel values, or all that we reasonably can:
fukasawa e60969
       */
fukasawa e60969
      if (small)
fukasawa e60969
      {
fukasawa e60969
         const unsigned int pixel_depth =
fukasawa e60969
            pixel_depth_of_type(color_type, bit_depth);
fukasawa e60969
fukasawa e60969
         if (pixel_depth <= 8U)
fukasawa e60969
         {
fukasawa e60969
            assert(size == (1U<
fukasawa e60969
            ysize = 1U;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
         {
fukasawa e60969
            assert(size == 256U);
fukasawa e60969
            ysize = 256U;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         ysize = size;
fukasawa e60969
fukasawa e60969
      /* This function uses the libpng values used on read to carry extra
fukasawa e60969
       * information about the gamma:
fukasawa e60969
       */
fukasawa e60969
      if (gamma == PNG_GAMMA_MAC_18)
fukasawa e60969
         gamma = 65909;
fukasawa e60969
fukasawa e60969
      else if (gamma > 0 && gamma < 1000)
fukasawa e60969
         gamma = PNG_FP_1;
fukasawa e60969
fukasawa e60969
      if (gamma > 0)
fukasawa e60969
         real_gamma = gamma;
fukasawa e60969
fukasawa e60969
      {
fukasawa e60969
         unsigned int i;
fukasawa e60969
fukasawa e60969
         if (real_gamma == 45455) for (i=0; i<256; ++i)
fukasawa e60969
         {
fukasawa e60969
            gamma_table[i] = (png_byte)i;
fukasawa e60969
            conv = 1.;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
         {
fukasawa e60969
            /* Convert 'i' from sRGB (45455) to real_gamma, this makes
fukasawa e60969
             * the images look the same regardless of the gAMA chunk.
fukasawa e60969
             */
fukasawa e60969
            conv = real_gamma;
fukasawa e60969
            conv /= 45455;
fukasawa e60969
fukasawa e60969
            gamma_table[0] = 0;
fukasawa e60969
fukasawa e60969
            for (i=1; i<255; ++i)
fukasawa e60969
               gamma_table[i] = floorb(pow(i/255.,conv) * 255 + .5);
fukasawa e60969
fukasawa e60969
            gamma_table[255] = 255;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      png_set_IHDR(png_ptr, info_ptr, size, ysize, bit_depth, color_type,
fukasawa e60969
         PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
fukasawa e60969
fukasawa e60969
      if (color_type & PNG_COLOR_MASK_PALETTE)
fukasawa e60969
      {
fukasawa e60969
         int npalette;
fukasawa e60969
         png_color palette[256];
fukasawa e60969
         png_byte trans[256];
fukasawa e60969
fukasawa e60969
         npalette = generate_palette(palette, trans, bit_depth, gamma_table,
fukasawa e60969
            colors);
fukasawa e60969
         png_set_PLTE(png_ptr, info_ptr, palette, npalette);
fukasawa e60969
fukasawa e60969
         if (tRNS)
fukasawa e60969
            png_set_tRNS(png_ptr, info_ptr, trans, npalette-1,
fukasawa e60969
               NULL/*transparent color*/);
fukasawa e60969
fukasawa e60969
         /* Reset gamma_table to prevent the image rows being changed */
fukasawa e60969
         for (npalette=0; npalette<256; ++npalette)
fukasawa e60969
            gamma_table[npalette] = (png_byte)npalette;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (tRNS)
fukasawa e60969
      {
fukasawa e60969
         png_color_16 col;
fukasawa e60969
fukasawa e60969
         col.red = col.green = col.blue = col.gray =
fukasawa e60969
            0x0101U & ((1U<
fukasawa e60969
         col.index = 0U;
fukasawa e60969
         png_set_tRNS(png_ptr, info_ptr, NULL/*trans*/, 1U, &col);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (gamma == PNG_DEFAULT_sRGB)
fukasawa e60969
         png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_ABSOLUTE);
fukasawa e60969
fukasawa e60969
      else if (gamma > 0) /* Else don't set color space information */
fukasawa e60969
      {
fukasawa e60969
         png_set_gAMA_fixed(png_ptr, info_ptr, real_gamma);
fukasawa e60969
fukasawa e60969
         /* Just use the sRGB values here. */
fukasawa e60969
         png_set_cHRM_fixed(png_ptr, info_ptr,
fukasawa e60969
            /* color      x       y */
fukasawa e60969
            /* white */ 31270, 32900,
fukasawa e60969
            /* red   */ 64000, 33000,
fukasawa e60969
            /* green */ 30000, 60000,
fukasawa e60969
            /* blue  */ 15000,  6000
fukasawa e60969
         );
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Insert extra information. */
fukasawa e60969
      while (insert != NULL)
fukasawa e60969
      {
fukasawa e60969
         insert->insert(png_ptr, info_ptr, insert->nparams, insert->parameters);
fukasawa e60969
         insert = insert->next;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Write the file header. */
fukasawa e60969
      png_write_info(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
      /* Restrict the filters */
fukasawa e60969
      png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, filters);
fukasawa e60969
fukasawa e60969
      {
fukasawa e60969
#        ifdef PNG_WRITE_INTERLACING_SUPPORTED
fukasawa e60969
            int passes = png_set_interlace_handling(png_ptr);
fukasawa e60969
#        else /* !WRITE_INTERLACING */
fukasawa e60969
            int passes = 1;
fukasawa e60969
#        endif /* !WRITE_INTERLACING */
fukasawa e60969
         int pass;
fukasawa e60969
         png_size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
         row = malloc(rowbytes);
fukasawa e60969
fukasawa e60969
         if (row == NULL)
fukasawa e60969
            png_error(png_ptr, "OOM allocating row buffer");
fukasawa e60969
fukasawa e60969
         for (pass = 0; pass < passes; ++pass)
fukasawa e60969
         {
fukasawa e60969
            unsigned int y;
fukasawa e60969
fukasawa e60969
            for (y=0; y
fukasawa e60969
            {
fukasawa e60969
               unsigned int row_filters =
fukasawa e60969
                  generate_row(row, rowbytes, y, color_type, bit_depth,
fukasawa e60969
                        gamma_table, conv, colors, small);
fukasawa e60969
fukasawa e60969
               if (row_filters != 0 && filters == PNG_ALL_FILTERS)
fukasawa e60969
                  png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, row_filters);
fukasawa e60969
fukasawa e60969
               png_write_row(png_ptr, row);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Finish writing the file. */
fukasawa e60969
   png_write_end(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
   {
fukasawa e60969
      png_structp nv_ptr = png_ptr;
fukasawa e60969
      png_infop nv_info = info_ptr;
fukasawa e60969
fukasawa e60969
      png_ptr = NULL;
fukasawa e60969
      info_ptr = NULL;
fukasawa e60969
      png_destroy_write_struct(&nv_ptr, &nv_info);
fukasawa e60969
   }
fukasawa e60969
   free(row);
fukasawa e60969
   return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
fukasawa e60969
static size_t
fukasawa e60969
load_file(png_const_charp name, png_bytepp result)
fukasawa e60969
{
fukasawa e60969
   FILE *fp = tmpfile();
fukasawa e60969
fukasawa e60969
   if (fp != NULL)
fukasawa e60969
   {
fukasawa e60969
      FILE *ip = fopen(name, "rb");
fukasawa e60969
fukasawa e60969
      if (ip != NULL)
fukasawa e60969
      {
fukasawa e60969
         size_t total = 0;
fukasawa e60969
         int ch;
fukasawa e60969
fukasawa e60969
         for (;;)
fukasawa e60969
         {
fukasawa e60969
            ch = getc(ip);
fukasawa e60969
            if (ch == EOF) break;
fukasawa e60969
            putc(ch, fp);
fukasawa e60969
            ++total;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         if (ferror(ip))
fukasawa e60969
         {
fukasawa e60969
            perror(name);
fukasawa e60969
            fprintf(stderr, "%s: read error\n", name);
fukasawa e60969
            (void)fclose(ip);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
         {
fukasawa e60969
            (void)fclose(ip);
fukasawa e60969
fukasawa e60969
            if (ferror(fp))
fukasawa e60969
            {
fukasawa e60969
               perror("temporary file");
fukasawa e60969
               fprintf(stderr, "temporary file write error\n");
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            else
fukasawa e60969
            {
fukasawa e60969
               rewind(fp);
fukasawa e60969
fukasawa e60969
               if (total > 0)
fukasawa e60969
               {
fukasawa e60969
                  /* Round up to a multiple of 4 here to allow an iCCP profile
fukasawa e60969
                   * to be padded to a 4x boundary.
fukasawa e60969
                   */
fukasawa e60969
                  png_bytep data = malloc((total+3)&~3);
fukasawa e60969
fukasawa e60969
                  if (data != NULL)
fukasawa e60969
                  {
fukasawa e60969
                     size_t new_size = 0;
fukasawa e60969
fukasawa e60969
                     for (;;)
fukasawa e60969
                     {
fukasawa e60969
                        ch = getc(fp);
fukasawa e60969
                        if (ch == EOF) break;
fukasawa e60969
                        data[new_size++] = (png_byte)ch;
fukasawa e60969
                     }
fukasawa e60969
fukasawa e60969
                     if (ferror(fp) || new_size != total)
fukasawa e60969
                     {
fukasawa e60969
                        perror("temporary file");
fukasawa e60969
                        fprintf(stderr, "temporary file read error\n");
fukasawa e60969
                        free(data);
fukasawa e60969
                     }
fukasawa e60969
fukasawa e60969
                     else
fukasawa e60969
                     {
fukasawa e60969
                        (void)fclose(fp);
fukasawa e60969
                        *result = data;
fukasawa e60969
                        return total;
fukasawa e60969
                     }
fukasawa e60969
                  }
fukasawa e60969
fukasawa e60969
                  else
fukasawa e60969
                     fprintf(stderr, "%s: out of memory loading file\n", name);
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               else
fukasawa e60969
                  fprintf(stderr, "%s: empty file\n", name);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         perror(name);
fukasawa e60969
         fprintf(stderr, "%s: open failed\n", name);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      fclose(fp);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      fprintf(stderr, "makepng: %s: could not open temporary file\n", name);
fukasawa e60969
fukasawa e60969
   exit(1);
fukasawa e60969
   return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_size_t
fukasawa e60969
load_fake(png_charp param, png_bytepp profile)
fukasawa e60969
{
fukasawa e60969
   char *endptr = NULL;
fukasawa e60969
   uint64_t size = strtoull(param, &endptr, 0/*base*/);
fukasawa e60969
fukasawa e60969
   /* The 'fake' format is <number>*[string] */</number>
fukasawa e60969
   if (endptr != NULL && *endptr == '*')
fukasawa e60969
   {
fukasawa e60969
      size_t len = strlen(++endptr);
fukasawa e60969
      size_t result = (size_t)size;
fukasawa e60969
fukasawa e60969
      if (len == 0) len = 1; /* capture the terminating '\0' */
fukasawa e60969
fukasawa e60969
      /* Now repeat that string to fill 'size' bytes. */
fukasawa e60969
      if (result == size && (*profile = malloc(result)) != NULL)
fukasawa e60969
      {
fukasawa e60969
         png_bytep out = *profile;
fukasawa e60969
fukasawa e60969
         if (len == 1)
fukasawa e60969
            memset(out, *endptr, result);
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
         {
fukasawa e60969
            while (size >= len)
fukasawa e60969
            {
fukasawa e60969
               memcpy(out, endptr, len);
fukasawa e60969
               out += len;
fukasawa e60969
               size -= len;
fukasawa e60969
            }
fukasawa e60969
            memcpy(out, endptr, size);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         return result;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "%s: size exceeds system limits\n", param);
fukasawa e60969
         exit(1);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
check_param_count(int nparams, int expect)
fukasawa e60969
{
fukasawa e60969
   if (nparams != expect)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "bad parameter count (internal error)\n");
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
insert_iCCP(png_structp png_ptr, png_infop info_ptr, int nparams,
fukasawa e60969
   png_charpp params)
fukasawa e60969
{
fukasawa e60969
   png_bytep profile = NULL;
fukasawa e60969
   png_uint_32 proflen = 0;
fukasawa e60969
   int result;
fukasawa e60969
fukasawa e60969
   check_param_count(nparams, 2);
fukasawa e60969
fukasawa e60969
   switch (params[1][0])
fukasawa e60969
   {
fukasawa e60969
      case '<':
fukasawa e60969
         {
fukasawa e60969
            png_size_t filelen = load_file(params[1]+1, &profile);
fukasawa e60969
            if (filelen > 0xfffffffc) /* Maximum profile length */
fukasawa e60969
            {
fukasawa e60969
               fprintf(stderr, "%s: file too long (%lu) for an ICC profile\n",
fukasawa e60969
                  params[1]+1, (unsigned long)filelen);
fukasawa e60969
               exit(1);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            proflen = (png_uint_32)filelen;
fukasawa e60969
         }
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case '0': case '1': case '2': case '3': case '4':
fukasawa e60969
      case '5': case '6': case '7': case '8': case '9':
fukasawa e60969
         {
fukasawa e60969
            png_size_t fake_len = load_fake(params[1], &profile);
fukasawa e60969
fukasawa e60969
            if (fake_len > 0) /* else a simple parameter */
fukasawa e60969
            {
fukasawa e60969
               if (fake_len > 0xffffffff) /* Maximum profile length */
fukasawa e60969
               {
fukasawa e60969
                  fprintf(stderr,
fukasawa e60969
                     "%s: fake data too long (%lu) for an ICC profile\n",
fukasawa e60969
                     params[1], (unsigned long)fake_len);
fukasawa e60969
                  exit(1);
fukasawa e60969
               }
fukasawa e60969
               proflen = (png_uint_32)(fake_len & ~3U);
fukasawa e60969
               /* Always fix up the profile length. */
fukasawa e60969
               png_save_uint_32(profile, proflen);
fukasawa e60969
               break;
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
      default:
fukasawa e60969
         fprintf(stderr, "--insert iCCP \"%s\": unrecognized\n", params[1]);
fukasawa e60969
         fprintf(stderr, "  use '<' to read a file: \"
fukasawa e60969
         exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   result = 1;
fukasawa e60969
fukasawa e60969
   if (proflen & 3)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr,
fukasawa e60969
         "makepng: --insert iCCP %s: profile length made a multiple of 4\n",
fukasawa e60969
         params[1]);
fukasawa e60969
fukasawa e60969
      /* load_file allocates extra space for this padding, the ICC spec requires
fukasawa e60969
       * padding with zero bytes.
fukasawa e60969
       */
fukasawa e60969
      while (proflen & 3)
fukasawa e60969
         profile[proflen++] = 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (profile != NULL && proflen > 3)
fukasawa e60969
   {
fukasawa e60969
      png_uint_32 prof_header = png_get_uint_32(profile);
fukasawa e60969
fukasawa e60969
      if (prof_header != proflen)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "--insert iCCP %s: profile length field wrong:\n",
fukasawa e60969
            params[1]);
fukasawa e60969
         fprintf(stderr, "  actual %lu, recorded value %lu (corrected)\n",
fukasawa e60969
            (unsigned long)proflen, (unsigned long)prof_header);
fukasawa e60969
         png_save_uint_32(profile, proflen);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (result && profile != NULL && proflen >=4)
fukasawa e60969
      png_set_iCCP(png_ptr, info_ptr, params[0], PNG_COMPRESSION_TYPE_BASE,
fukasawa e60969
         profile, proflen);
fukasawa e60969
fukasawa e60969
   if (profile)
fukasawa e60969
      free(profile);
fukasawa e60969
fukasawa e60969
   if (!result)
fukasawa e60969
      exit(1);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
clear_text(png_text *text, png_charp keyword)
fukasawa e60969
{
fukasawa e60969
   text->compression = -1; /* none */
fukasawa e60969
   text->key = keyword;
fukasawa e60969
   text->text = NULL;
fukasawa e60969
   text->text_length = 0; /* libpng calculates this */
fukasawa e60969
   text->itxt_length = 0; /* libpng calculates this */
fukasawa e60969
   text->lang = NULL;
fukasawa e60969
   text->lang_key = NULL;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
set_text(png_structp png_ptr, png_infop info_ptr, png_textp text,
fukasawa e60969
   png_charp param)
fukasawa e60969
{
fukasawa e60969
   switch (param[0])
fukasawa e60969
   {
fukasawa e60969
      case '<':
fukasawa e60969
         {
fukasawa e60969
            png_bytep file = NULL;
fukasawa e60969
fukasawa e60969
            text->text_length = load_file(param+1, &file);
fukasawa e60969
            text->text = (png_charp)file;
fukasawa e60969
         }
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case '0': case '1': case '2': case '3': case '4':
fukasawa e60969
      case '5': case '6': case '7': case '8': case '9':
fukasawa e60969
         {
fukasawa e60969
            png_bytep data = NULL;
fukasawa e60969
            png_size_t fake_len = load_fake(param, &data);
fukasawa e60969
fukasawa e60969
            if (fake_len > 0) /* else a simple parameter */
fukasawa e60969
            {
fukasawa e60969
               text->text_length = fake_len;
fukasawa e60969
               text->text = (png_charp)data;
fukasawa e60969
               break;
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
      default:
fukasawa e60969
         text->text = param;
fukasawa e60969
         break;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_set_text(png_ptr, info_ptr, text, 1);
fukasawa e60969
fukasawa e60969
   if (text->text != param)
fukasawa e60969
      free(text->text);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
insert_tEXt(png_structp png_ptr, png_infop info_ptr, int nparams,
fukasawa e60969
   png_charpp params)
fukasawa e60969
{
fukasawa e60969
   png_text text;
fukasawa e60969
fukasawa e60969
   check_param_count(nparams, 2);
fukasawa e60969
   clear_text(&text, params[0]);
fukasawa e60969
   set_text(png_ptr, info_ptr, &text, params[1]);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
insert_zTXt(png_structp png_ptr, png_infop info_ptr, int nparams,
fukasawa e60969
   png_charpp params)
fukasawa e60969
{
fukasawa e60969
   png_text text;
fukasawa e60969
fukasawa e60969
   check_param_count(nparams, 2);
fukasawa e60969
   clear_text(&text, params[0]);
fukasawa e60969
   text.compression = 0; /* deflate */
fukasawa e60969
   set_text(png_ptr, info_ptr, &text, params[1]);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
insert_iTXt(png_structp png_ptr, png_infop info_ptr, int nparams,
fukasawa e60969
   png_charpp params)
fukasawa e60969
{
fukasawa e60969
   png_text text;
fukasawa e60969
fukasawa e60969
   check_param_count(nparams, 4);
fukasawa e60969
   clear_text(&text, params[0]);
fukasawa e60969
   text.compression = 2; /* iTXt + deflate */
fukasawa e60969
   text.lang = params[1];/* language tag */
fukasawa e60969
   text.lang_key = params[2]; /* translated keyword */
fukasawa e60969
   set_text(png_ptr, info_ptr, &text, params[3]);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
insert_hIST(png_structp png_ptr, png_infop info_ptr, int nparams,
fukasawa e60969
      png_charpp params)
fukasawa e60969
{
fukasawa e60969
   int i;
fukasawa e60969
   png_uint_16 freq[256];
fukasawa e60969
fukasawa e60969
   /* libpng takes the count from the PLTE count; we don't check it here but we
fukasawa e60969
    * do set the array to 0 for unspecified entries.
fukasawa e60969
    */
fukasawa e60969
   memset(freq, 0, sizeof freq);
fukasawa e60969
   for (i=0; i
fukasawa e60969
   {
fukasawa e60969
      char *endptr = NULL;
fukasawa e60969
      unsigned long int l = strtoul(params[i], &endptr, 0/*base*/);
fukasawa e60969
fukasawa e60969
      if (params[i][0] && *endptr == 0 && l <= 65535)
fukasawa e60969
         freq[i] = (png_uint_16)l;
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "hIST[%d]: %s: invalid frequency\n", i, params[i]);
fukasawa e60969
         exit(1);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_set_hIST(png_ptr, info_ptr, freq);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_byte
fukasawa e60969
bval(png_const_structrp png_ptr, png_charp param, unsigned int maxval)
fukasawa e60969
{
fukasawa e60969
   char *endptr = NULL;
fukasawa e60969
   unsigned long int l = strtoul(param, &endptr, 0/*base*/);
fukasawa e60969
fukasawa e60969
   if (param[0] && *endptr == 0 && l <= maxval)
fukasawa e60969
      return (png_byte)l;
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      png_error(png_ptr, "sBIT: invalid sBIT value");
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
insert_sBIT(png_structp png_ptr, png_infop info_ptr, int nparams,
fukasawa e60969
      png_charpp params)
fukasawa e60969
{
fukasawa e60969
   const int ct = png_get_color_type(png_ptr, info_ptr);
fukasawa e60969
   const int c = (ct & PNG_COLOR_MASK_COLOR ? 3 : 1) +
fukasawa e60969
      (ct & PNG_COLOR_MASK_ALPHA ? 1 : 0);
fukasawa e60969
   const unsigned int maxval =
fukasawa e60969
      ct & PNG_COLOR_MASK_PALETTE ? 8U : png_get_bit_depth(png_ptr, info_ptr);
fukasawa e60969
   png_color_8 sBIT;
fukasawa e60969
fukasawa e60969
   if (nparams != c)
fukasawa e60969
      png_error(png_ptr, "sBIT: incorrect parameter count");
fukasawa e60969
fukasawa e60969
   if (ct & PNG_COLOR_MASK_COLOR)
fukasawa e60969
   {
fukasawa e60969
      sBIT.red = bval(png_ptr, params[0], maxval);
fukasawa e60969
      sBIT.green = bval(png_ptr, params[1], maxval);
fukasawa e60969
      sBIT.blue = bval(png_ptr, params[2], maxval);
fukasawa e60969
      sBIT.gray = 42;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      sBIT.red = sBIT.green = sBIT.blue = 42;
fukasawa e60969
      sBIT.gray = bval(png_ptr, params[0], maxval);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (ct & PNG_COLOR_MASK_ALPHA)
fukasawa e60969
      sBIT.alpha = bval(png_ptr, params[nparams-1], maxval);
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      sBIT.alpha = 42;
fukasawa e60969
fukasawa e60969
   png_set_sBIT(png_ptr, info_ptr, &sBIT);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#if 0
fukasawa e60969
static void
fukasawa e60969
insert_sPLT(png_structp png_ptr, png_infop info_ptr, int nparams, png_charpp params)
fukasawa e60969
{
fukasawa e60969
   fprintf(stderr, "insert sPLT: NYI\n");
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
find_parameters(png_const_charp what, png_charp param, png_charp *list,
fukasawa e60969
   int nparams)
fukasawa e60969
{
fukasawa e60969
   /* Parameters are separated by '\n' or ':' characters, up to nparams are
fukasawa e60969
    * accepted (more is an error) and the number found is returned.
fukasawa e60969
    */
fukasawa e60969
   int i;
fukasawa e60969
   for (i=0; *param && i
fukasawa e60969
   {
fukasawa e60969
      list[i] = param;
fukasawa e60969
      while (*++param) if (*param == '\n' || *param == ':')
fukasawa e60969
      {
fukasawa e60969
         *param++ = 0; /* Terminate last parameter */
fukasawa e60969
         break;        /* And start a new one. */
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (*param)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "--insert %s: too many parameters (%s)\n", what, param);
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   list[i] = NULL; /* terminates list */
fukasawa e60969
   return i; /* number of parameters filled in */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
bad_parameter_count(png_const_charp what, int nparams)
fukasawa e60969
{
fukasawa e60969
   fprintf(stderr, "--insert %s: bad parameter count %d\n", what, nparams);
fukasawa e60969
   exit(1);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static chunk_insert *
fukasawa e60969
make_insert(png_const_charp what,
fukasawa e60969
   void (*insert)(png_structp, png_infop, int, png_charpp),
fukasawa e60969
   int nparams, png_charpp list)
fukasawa e60969
{
fukasawa e60969
   int i;
fukasawa e60969
   chunk_insert *cip;
fukasawa e60969
fukasawa e60969
   cip = malloc(offsetof(chunk_insert,parameters) +
fukasawa e60969
      nparams * sizeof (png_charp));
fukasawa e60969
fukasawa e60969
   if (cip == NULL)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "--insert %s: out of memory allocating %d parameters\n",
fukasawa e60969
         what, nparams);
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   cip->next = NULL;
fukasawa e60969
   cip->insert = insert;
fukasawa e60969
   cip->nparams = nparams;
fukasawa e60969
   for (i=0; i
fukasawa e60969
      cip->parameters[i] = list[i];
fukasawa e60969
fukasawa e60969
   return cip;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static chunk_insert *
fukasawa e60969
find_insert(png_const_charp what, png_charp param)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 chunk = 0;
fukasawa e60969
   png_charp parameter_list[1024];
fukasawa e60969
   int i, nparams;
fukasawa e60969
fukasawa e60969
   /* Assemble the chunk name */
fukasawa e60969
   for (i=0; i<4; ++i)
fukasawa e60969
   {
fukasawa e60969
      char ch = what[i];
fukasawa e60969
fukasawa e60969
      if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))
fukasawa e60969
         chunk = (chunk << 8) + what[i];
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         break;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (i < 4 || what[4] != 0)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "makepng --insert \"%s\": invalid chunk name\n", what);
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Assemble the parameter list. */
fukasawa e60969
   nparams = find_parameters(what, param, parameter_list, 1024);
fukasawa e60969
fukasawa e60969
#  define CHUNK(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d))
fukasawa e60969
fukasawa e60969
   switch (chunk)
fukasawa e60969
   {
fukasawa e60969
      case CHUNK(105,67,67,80):  /* iCCP */
fukasawa e60969
         if (nparams == 2)
fukasawa e60969
            return make_insert(what, insert_iCCP, nparams, parameter_list);
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case CHUNK(116,69,88,116): /* tEXt */
fukasawa e60969
         if (nparams == 2)
fukasawa e60969
            return make_insert(what, insert_tEXt, nparams, parameter_list);
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case CHUNK(122,84,88,116): /* zTXt */
fukasawa e60969
         if (nparams == 2)
fukasawa e60969
            return make_insert(what, insert_zTXt, nparams, parameter_list);
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case CHUNK(105,84,88,116): /* iTXt */
fukasawa e60969
         if (nparams == 4)
fukasawa e60969
            return make_insert(what, insert_iTXt, nparams, parameter_list);
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case CHUNK(104,73,83,84):  /* hIST */
fukasawa e60969
         if (nparams <= 256)
fukasawa e60969
            return make_insert(what, insert_hIST, nparams, parameter_list);
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case CHUNK(115,66,73,84): /* sBIT */
fukasawa e60969
         if (nparams <= 4)
fukasawa e60969
            return make_insert(what, insert_sBIT, nparams, parameter_list);
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
#if 0
fukasawa e60969
      case CHUNK(115,80,76,84):  /* sPLT */
fukasawa e60969
         return make_insert(what, insert_sPLT, nparams, parameter_list);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
      default:
fukasawa e60969
         fprintf(stderr, "makepng --insert \"%s\": unrecognized chunk name\n",
fukasawa e60969
            what);
fukasawa e60969
         exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   bad_parameter_count(what, nparams);
fukasawa e60969
   return NULL;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* This is necessary because libpng expects writeable strings for things like
fukasawa e60969
 * text chunks (maybe this should be fixed...)
fukasawa e60969
 */
fukasawa e60969
static png_charp
fukasawa e60969
strstash(png_const_charp foo)
fukasawa e60969
{
fukasawa e60969
   /* The program indicates a memory allocation error by crashing, this is by
fukasawa e60969
    * design.
fukasawa e60969
    */
fukasawa e60969
   if (foo != NULL)
fukasawa e60969
   {
fukasawa e60969
      png_charp bar = malloc(strlen(foo)+1);
fukasawa e60969
      return strcpy(bar, foo);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return NULL;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_charp
fukasawa e60969
strstash_list(const png_const_charp *text)
fukasawa e60969
{
fukasawa e60969
   size_t foo = 0;
fukasawa e60969
   png_charp result, bar;
fukasawa e60969
   const png_const_charp *line = text;
fukasawa e60969
fukasawa e60969
   while (*line != NULL)
fukasawa e60969
      foo += strlen(*line++);
fukasawa e60969
fukasawa e60969
   result = bar = malloc(foo+1);
fukasawa e60969
fukasawa e60969
   line = text;
fukasawa e60969
   while (*line != NULL)
fukasawa e60969
   {
fukasawa e60969
      foo = strlen(*line);
fukasawa e60969
      memcpy(bar, *line++, foo);
fukasawa e60969
      bar += foo;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   *bar = 0;
fukasawa e60969
   return result;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* These are used to insert Copyright and Licence fields, they allow the text to
fukasawa e60969
 * have \n unlike the --insert option.
fukasawa e60969
 */
fukasawa e60969
static chunk_insert *
fukasawa e60969
add_tEXt(const char *key, const png_const_charp *text)
fukasawa e60969
{
fukasawa e60969
   static char what[5] = { 116, 69, 88, 116, 0 };
fukasawa e60969
   png_charp parameter_list[3];
fukasawa e60969
fukasawa e60969
   parameter_list[0] = strstash(key);
fukasawa e60969
   parameter_list[1] = strstash_list(text);
fukasawa e60969
   parameter_list[2] = NULL;
fukasawa e60969
fukasawa e60969
   return make_insert(what, insert_tEXt, 2, parameter_list);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static chunk_insert *
fukasawa e60969
add_iTXt(const char *key, const char *language, const char *language_key,
fukasawa e60969
      const png_const_charp *text)
fukasawa e60969
{
fukasawa e60969
   static char what[5] = { 105, 84, 88, 116, 0 };
fukasawa e60969
   png_charp parameter_list[5];
fukasawa e60969
fukasawa e60969
   parameter_list[0] = strstash(key);
fukasawa e60969
   parameter_list[1] = strstash(language);
fukasawa e60969
   parameter_list[2] = strstash(language_key);
fukasawa e60969
   parameter_list[3] = strstash_list(text);
fukasawa e60969
   parameter_list[4] = NULL;
fukasawa e60969
fukasawa e60969
   return make_insert(what, insert_iTXt, 4, parameter_list);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* This is a not-very-good parser for a sequence of numbers (including 0).  It
fukasawa e60969
 * doesn't accept some apparently valid things, but it accepts all the sensible
fukasawa e60969
 * combinations.
fukasawa e60969
 */
fukasawa e60969
static void
fukasawa e60969
parse_color(char *arg, unsigned int *colors)
fukasawa e60969
{
fukasawa e60969
   unsigned int ncolors = 0;
fukasawa e60969
fukasawa e60969
   while (*arg && ncolors < 4)
fukasawa e60969
   {
fukasawa e60969
      char *ep = arg;
fukasawa e60969
fukasawa e60969
      unsigned long ul = strtoul(arg, &ep, 0);
fukasawa e60969
fukasawa e60969
      if (ul > 65535)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "makepng --color=...'%s': too big\n", arg);
fukasawa e60969
         exit(1);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (ep == arg)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "makepng --color=...'%s': not a valid color\n", arg);
fukasawa e60969
         exit(1);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (*ep) ++ep; /* skip a separator */
fukasawa e60969
      arg = ep;
fukasawa e60969
fukasawa e60969
      colors[++ncolors] = (unsigned int)ul; /* checked above */
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (*arg)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "makepng --color=...'%s': too many values\n", arg);
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   *colors = ncolors;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
int
fukasawa e60969
main(int argc, char **argv)
fukasawa e60969
{
fukasawa e60969
   FILE *fp = stdout;
fukasawa e60969
   const char *file_name = NULL;
fukasawa e60969
   int color_type = 8; /* invalid */
fukasawa e60969
   int bit_depth = 32; /* invalid */
fukasawa e60969
   int small = 0; /* make full size images */
fukasawa e60969
   int tRNS = 0; /* don't output a tRNS chunk */
fukasawa e60969
   unsigned int colors[5];
fukasawa e60969
   unsigned int filters = PNG_ALL_FILTERS;
fukasawa e60969
   png_fixed_point gamma = 0; /* not set */
fukasawa e60969
   chunk_insert *head_insert = NULL;
fukasawa e60969
   chunk_insert **insert_ptr = &head_insert;
fukasawa e60969
fukasawa e60969
   memset(colors, 0, sizeof colors);
fukasawa e60969
fukasawa e60969
   while (--argc > 0)
fukasawa e60969
   {
fukasawa e60969
      char *arg = *++argv;
fukasawa e60969
fukasawa e60969
      if (strcmp(arg, "--small") == 0)
fukasawa e60969
      {
fukasawa e60969
         small = 1;
fukasawa e60969
         continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (strcmp(arg, "--tRNS") == 0)
fukasawa e60969
      {
fukasawa e60969
         tRNS = 1;
fukasawa e60969
         continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (strcmp(arg, "--sRGB") == 0)
fukasawa e60969
      {
fukasawa e60969
         gamma = PNG_DEFAULT_sRGB;
fukasawa e60969
         continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (strcmp(arg, "--linear") == 0)
fukasawa e60969
      {
fukasawa e60969
         gamma = PNG_FP_1;
fukasawa e60969
         continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (strcmp(arg, "--1.8") == 0)
fukasawa e60969
      {
fukasawa e60969
         gamma = PNG_GAMMA_MAC_18;
fukasawa e60969
         continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (strcmp(arg, "--nofilters") == 0)
fukasawa e60969
      {
fukasawa e60969
         filters = PNG_FILTER_NONE;
fukasawa e60969
         continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (strncmp(arg, "--color=", 8) == 0)
fukasawa e60969
      {
fukasawa e60969
          parse_color(arg+8, colors);
fukasawa e60969
          continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (argc >= 3 && strcmp(arg, "--insert") == 0)
fukasawa e60969
      {
fukasawa e60969
         png_const_charp what = *++argv;
fukasawa e60969
         png_charp param = *++argv;
fukasawa e60969
         chunk_insert *new_insert;
fukasawa e60969
fukasawa e60969
         argc -= 2;
fukasawa e60969
fukasawa e60969
         new_insert = find_insert(what, param);
fukasawa e60969
fukasawa e60969
         if (new_insert != NULL)
fukasawa e60969
         {
fukasawa e60969
            *insert_ptr = new_insert;
fukasawa e60969
            insert_ptr = &new_insert->next;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (arg[0] == '-')
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "makepng: %s: invalid option\n", arg);
fukasawa e60969
         exit(1);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (strcmp(arg, "palette") == 0)
fukasawa e60969
      {
fukasawa e60969
         color_type = PNG_COLOR_TYPE_PALETTE;
fukasawa e60969
         continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (strncmp(arg, "gray", 4) == 0)
fukasawa e60969
      {
fukasawa e60969
         if (arg[4] == 0)
fukasawa e60969
         {
fukasawa e60969
            color_type = PNG_COLOR_TYPE_GRAY;
fukasawa e60969
            continue;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else if (strcmp(arg+4, "a") == 0 ||
fukasawa e60969
            strcmp(arg+4, "alpha") == 0 ||
fukasawa e60969
            strcmp(arg+4, "-alpha") == 0)
fukasawa e60969
         {
fukasawa e60969
            color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
fukasawa e60969
            continue;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (strncmp(arg, "rgb", 3) == 0)
fukasawa e60969
      {
fukasawa e60969
         if (arg[3] == 0)
fukasawa e60969
         {
fukasawa e60969
            color_type = PNG_COLOR_TYPE_RGB;
fukasawa e60969
            continue;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else if (strcmp(arg+3, "a") == 0 ||
fukasawa e60969
            strcmp(arg+3, "alpha") == 0 ||
fukasawa e60969
            strcmp(arg+3, "-alpha") == 0)
fukasawa e60969
         {
fukasawa e60969
            color_type = PNG_COLOR_TYPE_RGB_ALPHA;
fukasawa e60969
            continue;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (color_type == 8 && isdigit(arg[0]))
fukasawa e60969
      {
fukasawa e60969
         color_type = atoi(arg);
fukasawa e60969
         if (color_type < 0 || color_type > 6 || color_type == 1 ||
fukasawa e60969
            color_type == 5)
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "makepng: %s: not a valid color type\n", arg);
fukasawa e60969
            exit(1);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (bit_depth == 32 && isdigit(arg[0]))
fukasawa e60969
      {
fukasawa e60969
         bit_depth = atoi(arg);
fukasawa e60969
         if (bit_depth <= 0 || bit_depth > 16 ||
fukasawa e60969
            (bit_depth & -bit_depth) != bit_depth)
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "makepng: %s: not a valid bit depth\n", arg);
fukasawa e60969
            exit(1);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (argc == 1) /* It's the file name */
fukasawa e60969
      {
fukasawa e60969
         fp = fopen(arg, "wb");
fukasawa e60969
         if (fp == NULL)
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "%s: %s: could not open\n", arg, strerror(errno));
fukasawa e60969
            exit(1);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         file_name = arg;
fukasawa e60969
         continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      fprintf(stderr, "makepng: %s: unknown argument\n", arg);
fukasawa e60969
      exit(1);
fukasawa e60969
   } /* argument while loop */
fukasawa e60969
fukasawa e60969
   if (color_type == 8 || bit_depth == 32)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "usage: makepng [--small] [--sRGB|--linear|--1.8] "
fukasawa e60969
         "[--color=...] color-type bit-depth [file-name]\n"
fukasawa e60969
         "  Make a test PNG file, by default writes to stdout.\n"
fukasawa e60969
         "  Other options are available, UTSL.\n");
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Check the colors */
fukasawa e60969
   {
fukasawa e60969
      const unsigned int lim = (color_type == PNG_COLOR_TYPE_PALETTE ? 255U :
fukasawa e60969
         (1U<
fukasawa e60969
      unsigned int i;
fukasawa e60969
fukasawa e60969
      for (i=1; i<=colors[0]; ++i)
fukasawa e60969
         if (colors[i] > lim)
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "makepng: --color=...: %u out of range [0..%u]\n",
fukasawa e60969
               colors[i], lim);
fukasawa e60969
            exit(1);
fukasawa e60969
         }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* small and colors are incomparible (will probably crash if both are used at
fukasawa e60969
    * the same time!)
fukasawa e60969
    */
fukasawa e60969
   if (small && colors[0] != 0)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "makepng: --color --small: only one at a time!\n");
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Restrict the filters for more speed to those we know are used for the
fukasawa e60969
    * generated images.
fukasawa e60969
    */
fukasawa e60969
   if (filters == PNG_ALL_FILTERS && !small/*small provides defaults*/)
fukasawa e60969
   {
fukasawa e60969
      if ((color_type & PNG_COLOR_MASK_PALETTE) != 0 || bit_depth < 8)
fukasawa e60969
         filters = PNG_FILTER_NONE;
fukasawa e60969
fukasawa e60969
      else if (color_type & PNG_COLOR_MASK_COLOR) /* rgb */
fukasawa e60969
      {
fukasawa e60969
         if (bit_depth == 8)
fukasawa e60969
            filters &= ~(PNG_FILTER_NONE | PNG_FILTER_AVG);
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            filters = PNG_FILTER_SUB | PNG_FILTER_PAETH;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else /* gray 8 or 16-bit */
fukasawa e60969
         filters &= ~PNG_FILTER_NONE;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Insert standard copyright and licence text. */
fukasawa e60969
   {
fukasawa e60969
      static png_const_charp copyright[] =
fukasawa e60969
      {
fukasawa e60969
         COPYRIGHT, /* ISO-Latin-1 */
fukasawa e60969
         NULL
fukasawa e60969
      };
fukasawa e60969
      static png_const_charp licensing[] =
fukasawa e60969
      {
fukasawa e60969
         IMAGE_LICENSING, /* UTF-8 */
fukasawa e60969
         NULL
fukasawa e60969
      };
fukasawa e60969
fukasawa e60969
      chunk_insert *new_insert;
fukasawa e60969
fukasawa e60969
      new_insert = add_tEXt("Copyright", copyright);
fukasawa e60969
      if (new_insert != NULL)
fukasawa e60969
      {
fukasawa e60969
         *insert_ptr = new_insert;
fukasawa e60969
         insert_ptr = &new_insert->next;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      new_insert = add_iTXt("Licensing", "en", NULL, licensing);
fukasawa e60969
      if (new_insert != NULL)
fukasawa e60969
      {
fukasawa e60969
         *insert_ptr = new_insert;
fukasawa e60969
         insert_ptr = &new_insert->next;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   {
fukasawa e60969
      int ret = write_png(&file_name, fp, color_type, bit_depth, gamma,
fukasawa e60969
         head_insert, filters, colors, small, tRNS);
fukasawa e60969
fukasawa e60969
      if (ret != 0 && file_name != NULL)
fukasawa e60969
         remove(file_name);
fukasawa e60969
fukasawa e60969
      return ret;
fukasawa e60969
   }
fukasawa e60969
}