fukasawa e60969
/* pngimage.c
fukasawa e60969
 *
fukasawa e60969
 * Copyright (c) 2015 John Cunningham Bowler
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.6.20 [December 3, 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
 * Test the png_read_png and png_write_png interfaces.  Given a PNG file load it
fukasawa e60969
 * using png_read_png and then write with png_write_png.  Test all possible
fukasawa e60969
 * transforms.
fukasawa e60969
 */
fukasawa e60969
#include <stdarg.h></stdarg.h>
fukasawa e60969
#include <stdlib.h></stdlib.h>
fukasawa e60969
#include <string.h></string.h>
fukasawa e60969
#include <errno.h></errno.h>
fukasawa e60969
#include <stdio.h></stdio.h>
fukasawa e60969
#include <assert.h></assert.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
#ifndef PNG_SETJMP_SUPPORTED
fukasawa e60969
#  include <setjmp.h> /* because png.h did *not* include this */</setjmp.h>
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#if defined(PNG_INFO_IMAGE_SUPPORTED) && defined(PNG_SEQUENTIAL_READ_SUPPORTED)\
fukasawa e60969
    && (defined(PNG_READ_PNG_SUPPORTED) || PNG_LIBPNG_VER < 10700)
fukasawa e60969
/* If a transform is valid on both read and write this implies that if the
fukasawa e60969
 * transform is applied to read it must also be applied on write to produce
fukasawa e60969
 * meaningful data.  This is because these transforms when performed on read
fukasawa e60969
 * produce data with a memory format that does not correspond to a PNG format.
fukasawa e60969
 *
fukasawa e60969
 * Most of these transforms are invertible; after applying the transform on
fukasawa e60969
 * write the result is the original PNG data that would have would have been
fukasawa e60969
 * read if no transform were applied.
fukasawa e60969
 *
fukasawa e60969
 * The exception is _SHIFT, which destroys the low order bits marked as not
fukasawa e60969
 * significant in a PNG with the sBIT chunk.
fukasawa e60969
 *
fukasawa e60969
 * The following table lists, for each transform, the conditions under which it
fukasawa e60969
 * is expected to do anything.  Conditions are defined as follows:
fukasawa e60969
 *
fukasawa e60969
 * 1) Color mask bits required - simply a mask to AND with color_type; one of
fukasawa e60969
 *    these must be present for the transform to fire, except that 0 means
fukasawa e60969
 *    'always'.
fukasawa e60969
 * 2) Color mask bits which must be absent - another mask - none of these must
fukasawa e60969
 *    be present.
fukasawa e60969
 * 3) Bit depths - a mask of component bit depths for the transform to fire.
fukasawa e60969
 * 4) 'read' - the transform works in png_read_png.
fukasawa e60969
 * 5) 'write' - the transform works in png_write_png.
fukasawa e60969
 * 6) PNG_INFO_chunk; a mask of the chunks that must be present for the
fukasawa e60969
 *    transform to fire.  All must be present - the requirement is that
fukasawa e60969
 *    png_get_valid() & mask == mask, so if mask is 0 there is no requirement.
fukasawa e60969
 *
fukasawa e60969
 * The condition refers to the original image state - if multiple transforms are
fukasawa e60969
 * used together it is possible to cause a transform that wouldn't fire on the
fukasawa e60969
 * original image to fire.
fukasawa e60969
 */
fukasawa e60969
static struct transform_info
fukasawa e60969
{
fukasawa e60969
   const char *name;
fukasawa e60969
   int         transform;
fukasawa e60969
   png_uint_32 valid_chunks;
fukasawa e60969
#     define CHUNK_NONE 0
fukasawa e60969
#     define CHUNK_sBIT PNG_INFO_sBIT
fukasawa e60969
#     define CHUNK_tRNS PNG_INFO_tRNS
fukasawa e60969
   png_byte    color_mask_required;
fukasawa e60969
   png_byte    color_mask_absent;
fukasawa e60969
#     define COLOR_MASK_X   0
fukasawa e60969
#     define COLOR_MASK_P   PNG_COLOR_MASK_PALETTE
fukasawa e60969
#     define COLOR_MASK_C   PNG_COLOR_MASK_COLOR
fukasawa e60969
#     define COLOR_MASK_A   PNG_COLOR_MASK_ALPHA
fukasawa e60969
#     define COLOR_MASK_ALL (PALETTE+COLOR+ALPHA)  /* absent = gray, no alpha */
fukasawa e60969
   png_byte    bit_depths;
fukasawa e60969
#     define BD_ALL  (1 + 2 + 4 + 8 + 16)
fukasawa e60969
#     define BD_PAL  (1 + 2 + 4 + 8)
fukasawa e60969
#     define BD_LOW  (1 + 2 + 4)
fukasawa e60969
#     define BD_16   16
fukasawa e60969
#     define BD_TRUE (8+16) /* i.e. true-color depths */
fukasawa e60969
   png_byte    when;
fukasawa e60969
#     define TRANSFORM_R  1
fukasawa e60969
#     define TRANSFORM_W  2
fukasawa e60969
#     define TRANSFORM_RW 3
fukasawa e60969
   png_byte    tested; /* the transform was tested somewhere */
fukasawa e60969
} transform_info[] =
fukasawa e60969
{
fukasawa e60969
   /* List ALL the PNG_TRANSFORM_ macros here.  Check for support using the READ
fukasawa e60969
    * macros; even if the transform is supported on write it cannot be tested
fukasawa e60969
    * without the read support.
fukasawa e60969
    */
fukasawa e60969
#  define T(name,chunk,cm_required,cm_absent,bd,when)\
fukasawa e60969
   {  #name, PNG_TRANSFORM_ ## name, CHUNK_ ## chunk,\
fukasawa e60969
      COLOR_MASK_ ## cm_required, COLOR_MASK_ ## cm_absent, BD_ ## bd,\
fukasawa e60969
      TRANSFORM_ ## when, 0/*!tested*/ }
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
fukasawa e60969
   T(STRIP_16,            NONE, X,   X,   16,  R),
fukasawa e60969
      /* drops the bottom 8 bits when bit depth is 16 */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
fukasawa e60969
   T(STRIP_ALPHA,         NONE, A,   X,  ALL,  R),
fukasawa e60969
      /* removes the alpha channel if present */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_PACK_SUPPORTED
fukasawa e60969
#  define TRANSFORM_RW_PACK TRANSFORM_RW
fukasawa e60969
#else
fukasawa e60969
#  define TRANSFORM_RW_PACK TRANSFORM_R
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_PACK_SUPPORTED
fukasawa e60969
   T(PACKING,             NONE, X,   X,  LOW, RW_PACK),
fukasawa e60969
      /* unpacks low-bit-depth components into 1 byte per component on read,
fukasawa e60969
       * reverses this on write.
fukasawa e60969
       */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
fukasawa e60969
#  define TRANSFORM_RW_PACKSWAP TRANSFORM_RW
fukasawa e60969
#else
fukasawa e60969
#  define TRANSFORM_RW_PACKSWAP TRANSFORM_R
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_PACKSWAP_SUPPORTED
fukasawa e60969
   T(PACKSWAP,            NONE, X,   X,  LOW, RW_PACKSWAP),
fukasawa e60969
      /* reverses the order of low-bit-depth components packed into a byte */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_EXPAND_SUPPORTED
fukasawa e60969
   T(EXPAND,              NONE, P,   X,  ALL,  R),
fukasawa e60969
      /* expands PLTE PNG files to RGB (no tRNS) or RGBA (tRNS) *
fukasawa e60969
       * Note that the 'EXPAND' transform does lots of different things: */
fukasawa e60969
   T(EXPAND,              NONE, X,   C,  ALL,  R),
fukasawa e60969
      /* expands grayscale PNG files to RGB, or RGBA */
fukasawa e60969
   T(EXPAND,              tRNS, X,   A,  ALL,  R),
fukasawa e60969
      /* expands the tRNS chunk in files without alpha */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_INVERT_SUPPORTED
fukasawa e60969
#  define TRANSFORM_RW_INVERT TRANSFORM_RW
fukasawa e60969
#else
fukasawa e60969
#  define TRANSFORM_RW_INVERT TRANSFORM_R
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_INVERT_SUPPORTED
fukasawa e60969
   T(INVERT_MONO,         NONE, X,   C,  ALL, RW_INVERT),
fukasawa e60969
      /* converts gray-scale components to 1..0 from 0..1 */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_SHIFT_SUPPORTED
fukasawa e60969
#  define TRANSFORM_RW_SHIFT TRANSFORM_RW
fukasawa e60969
#else
fukasawa e60969
#  define TRANSFORM_RW_SHIFT TRANSFORM_R
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_SHIFT_SUPPORTED
fukasawa e60969
   T(SHIFT,               sBIT, X,   X,  ALL, RW_SHIFT),
fukasawa e60969
      /* reduces component values to the original range based on the sBIT chunk,
fukasawa e60969
       * this is only partially reversible - the low bits are lost and cannot be
fukasawa e60969
       * recovered on write.  In fact write code replicates the bits to generate
fukasawa e60969
       * new low-order bits.
fukasawa e60969
       */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_BGR_SUPPORTED
fukasawa e60969
#  define TRANSFORM_RW_BGR TRANSFORM_RW
fukasawa e60969
#else
fukasawa e60969
#  define TRANSFORM_RW_BGR TRANSFORM_R
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_BGR_SUPPORTED
fukasawa e60969
   T(BGR,                 NONE, C,   P, TRUE, RW_BGR),
fukasawa e60969
      /* reverses the rgb component values of true-color pixels */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
fukasawa e60969
#  define TRANSFORM_RW_SWAP_ALPHA TRANSFORM_RW
fukasawa e60969
#else
fukasawa e60969
#  define TRANSFORM_RW_SWAP_ALPHA TRANSFORM_R
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
fukasawa e60969
   T(SWAP_ALPHA,          NONE, A,   X, TRUE, RW_SWAP_ALPHA),
fukasawa e60969
      /* swaps the alpha channel of RGBA or GA pixels to the front - ARGB or
fukasawa e60969
       * AG, on write reverses the process.
fukasawa e60969
       */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_SWAP_SUPPORTED
fukasawa e60969
#  define TRANSFORM_RW_SWAP TRANSFORM_RW
fukasawa e60969
#else
fukasawa e60969
#  define TRANSFORM_RW_SWAP TRANSFORM_R
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_SWAP_SUPPORTED
fukasawa e60969
   T(SWAP_ENDIAN,         NONE, X,   P,   16, RW_SWAP),
fukasawa e60969
      /* byte-swaps 16-bit component values */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
fukasawa e60969
#  define TRANSFORM_RW_INVERT_ALPHA TRANSFORM_RW
fukasawa e60969
#else
fukasawa e60969
#  define TRANSFORM_RW_INVERT_ALPHA TRANSFORM_R
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
fukasawa e60969
   T(INVERT_ALPHA,        NONE, A,   X, TRUE, RW_INVERT_ALPHA),
fukasawa e60969
      /* converts an alpha channel from 0..1 to 1..0 */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_FILLER_SUPPORTED
fukasawa e60969
   T(STRIP_FILLER_BEFORE, NONE, A,   P, TRUE,  W), /* 'A' for a filler! */
fukasawa e60969
      /* on write skips a leading filler channel; testing requires data with a
fukasawa e60969
       * filler channel so this is produced from RGBA or GA images by removing
fukasawa e60969
       * the 'alpha' flag from the color type in place.
fukasawa e60969
       */
fukasawa e60969
   T(STRIP_FILLER_AFTER,  NONE, A,   P, TRUE,  W),
fukasawa e60969
      /* on write strips a trailing filler channel */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
fukasawa e60969
   T(GRAY_TO_RGB,         NONE, X,   C,  ALL,  R),
fukasawa e60969
      /* expands grayscale images to RGB, also causes the palette part of
fukasawa e60969
       * 'EXPAND' to happen.  Low bit depth grayscale images are expanded to
fukasawa e60969
       * 8-bits per component and no attempt is made to convert the image to a
fukasawa e60969
       * palette image.  While this transform is partially reversible
fukasawa e60969
       * png_write_png does not currently support this.
fukasawa e60969
       */
fukasawa e60969
   T(GRAY_TO_RGB,         NONE, P,   X,  ALL,  R),
fukasawa e60969
      /* The 'palette' side effect mentioned above; a bit bogus but this is the
fukasawa e60969
       * way the libpng code works.
fukasawa e60969
       */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_EXPAND_16_SUPPORTED
fukasawa e60969
   T(EXPAND_16,           NONE, X,   X,  PAL,  R),
fukasawa e60969
      /* expands images to 16-bits per component, as a side effect expands
fukasawa e60969
       * palette images to RGB and expands the tRNS chunk if present, so it can
fukasawa e60969
       * modify 16-bit per component images as well:
fukasawa e60969
       */
fukasawa e60969
   T(EXPAND_16,           tRNS, X,   A,   16,  R),
fukasawa e60969
      /* side effect of EXPAND_16 - expands the tRNS chunk in an RGB or G 16-bit
fukasawa e60969
       * image.
fukasawa e60969
       */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
fukasawa e60969
   T(SCALE_16,            NONE, X,   X,   16,  R),
fukasawa e60969
      /* scales 16-bit components to 8-bits. */
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   { NULL /*name*/, 0, 0, 0, 0, 0, 0, 0/*!tested*/ }
fukasawa e60969
fukasawa e60969
#undef T
fukasawa e60969
};
fukasawa e60969
fukasawa e60969
#define ARRAY_SIZE(a) ((sizeof a)/(sizeof a[0]))
fukasawa e60969
#define TTABLE_SIZE ARRAY_SIZE(transform_info)
fukasawa e60969
fukasawa e60969
/* Some combinations of options that should be reversible are not; these cases
fukasawa e60969
 * are bugs.
fukasawa e60969
 */
fukasawa e60969
static int known_bad_combos[][2] =
fukasawa e60969
{
fukasawa e60969
   /* problem, antidote */
fukasawa e60969
   { PNG_TRANSFORM_SHIFT | PNG_TRANSFORM_INVERT_ALPHA, 0/*antidote*/ }
fukasawa e60969
};
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
is_combo(int transforms)
fukasawa e60969
{
fukasawa e60969
   return transforms & (transforms-1); /* non-zero if more than one set bit */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
first_transform(int transforms)
fukasawa e60969
{
fukasawa e60969
   return transforms & -transforms; /* lowest set bit */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
is_bad_combo(int transforms)
fukasawa e60969
{
fukasawa e60969
   unsigned int i;
fukasawa e60969
fukasawa e60969
   for (i=0; i
fukasawa e60969
   {
fukasawa e60969
      int combo = known_bad_combos[i][0];
fukasawa e60969
fukasawa e60969
      if ((combo & transforms) == combo &&
fukasawa e60969
         (transforms & known_bad_combos[i][1]) == 0)
fukasawa e60969
         return 1;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 0; /* combo is ok */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static const char *
fukasawa e60969
transform_name(int t)
fukasawa e60969
   /* The name, if 't' has multiple bits set the name of the lowest set bit is
fukasawa e60969
    * returned.
fukasawa e60969
    */
fukasawa e60969
{
fukasawa e60969
   unsigned int i;
fukasawa e60969
fukasawa e60969
   t &= -t; /* first set bit */
fukasawa e60969
fukasawa e60969
   for (i=0; i
fukasawa e60969
   {
fukasawa e60969
      if ((transform_info[i].transform & t) != 0)
fukasawa e60969
         return transform_info[i].name;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return "invalid transform";
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Variables calculated by validate_T below and used to record all the supported
fukasawa e60969
 * transforms.  Need (unsigned int) here because of the places where these
fukasawa e60969
 * values are used (unsigned compares in the 'exhaustive' iterator.)
fukasawa e60969
 */
fukasawa e60969
static unsigned int read_transforms, write_transforms, rw_transforms;
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
validate_T(void)
fukasawa e60969
   /* Validate the above table - this just builds the above values */
fukasawa e60969
{
fukasawa e60969
   unsigned int i;
fukasawa e60969
fukasawa e60969
   for (i=0; i
fukasawa e60969
   {
fukasawa e60969
      if (transform_info[i].when & TRANSFORM_R)
fukasawa e60969
         read_transforms |= transform_info[i].transform;
fukasawa e60969
fukasawa e60969
      if (transform_info[i].when & TRANSFORM_W)
fukasawa e60969
         write_transforms |= transform_info[i].transform;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Reversible transforms are those which are supported on both read and
fukasawa e60969
    * write.
fukasawa e60969
    */
fukasawa e60969
   rw_transforms = read_transforms & write_transforms;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* FILE DATA HANDLING
fukasawa e60969
 *    The original file is cached in memory.  During write the output file is
fukasawa e60969
 *    written to memory.
fukasawa e60969
 *
fukasawa e60969
 *    In both cases the file data is held in a linked list of buffers - not all
fukasawa e60969
 *    of these are in use at any time.
fukasawa e60969
 */
fukasawa e60969
#define NEW(type) ((type *)malloc(sizeof (type)))
fukasawa e60969
#define DELETE(ptr) (free(ptr))
fukasawa e60969
fukasawa e60969
struct buffer_list
fukasawa e60969
{
fukasawa e60969
   struct buffer_list *next;         /* next buffer in list */
fukasawa e60969
   png_byte            buffer[1024]; /* the actual buffer */
fukasawa e60969
};
fukasawa e60969
fukasawa e60969
struct buffer
fukasawa e60969
{
fukasawa e60969
   struct buffer_list  *last;       /* last buffer in use */
fukasawa e60969
   size_t               end_count;  /* bytes in the last buffer */
fukasawa e60969
   struct buffer_list  *current;    /* current buffer being read */
fukasawa e60969
   size_t               read_count; /* count of bytes read from current */
fukasawa e60969
   struct buffer_list   first;      /* the very first buffer */
fukasawa e60969
};
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
buffer_init(struct buffer *buffer)
fukasawa e60969
   /* Call this only once for a given buffer */
fukasawa e60969
{
fukasawa e60969
   buffer->first.next = NULL;
fukasawa e60969
   buffer->last = NULL;
fukasawa e60969
   buffer->current = NULL;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
buffer_destroy_list(struct buffer_list *list)
fukasawa e60969
{
fukasawa e60969
   if (list != NULL)
fukasawa e60969
   {
fukasawa e60969
      struct buffer_list *next = list->next;
fukasawa e60969
      DELETE(list);
fukasawa e60969
      buffer_destroy_list(next);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
buffer_destroy(struct buffer *buffer)
fukasawa e60969
{
fukasawa e60969
   struct buffer_list *list = buffer->first.next;
fukasawa e60969
   buffer_init(buffer);
fukasawa e60969
   buffer_destroy_list(list);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
static void
fukasawa e60969
buffer_start_write(struct buffer *buffer)
fukasawa e60969
{
fukasawa e60969
   buffer->last = &buffer->first;
fukasawa e60969
   buffer->end_count = 0;
fukasawa e60969
   buffer->current = NULL;
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
buffer_start_read(struct buffer *buffer)
fukasawa e60969
{
fukasawa e60969
   buffer->current = &buffer->first;
fukasawa e60969
   buffer->read_count = 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef ENOMEM /* required by POSIX 1003.1 */
fukasawa e60969
#  define MEMORY ENOMEM
fukasawa e60969
#else
fukasawa e60969
#  define MEMORY ERANGE /* required by ANSI-C */
fukasawa e60969
#endif
fukasawa e60969
static struct buffer *
fukasawa e60969
get_buffer(png_structp pp)
fukasawa e60969
   /* Used from libpng callbacks to get the current buffer */
fukasawa e60969
{
fukasawa e60969
   return (struct buffer*)png_get_io_ptr(pp);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static struct buffer_list *
fukasawa e60969
buffer_extend(struct buffer_list *current)
fukasawa e60969
{
fukasawa e60969
   struct buffer_list *add;
fukasawa e60969
fukasawa e60969
   assert(current->next == NULL);
fukasawa e60969
fukasawa e60969
   add = NEW(struct buffer_list);
fukasawa e60969
   if (add == NULL)
fukasawa e60969
      return NULL;
fukasawa e60969
fukasawa e60969
   add->next = NULL;
fukasawa e60969
   current->next = add;
fukasawa e60969
fukasawa e60969
   return add;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Load a buffer from a file; does the equivalent of buffer_start_write.  On a
fukasawa e60969
 * read error returns an errno value, else returns 0.
fukasawa e60969
 */
fukasawa e60969
static int
fukasawa e60969
buffer_from_file(struct buffer *buffer, FILE *fp)
fukasawa e60969
{
fukasawa e60969
   struct buffer_list *last = &buffer->first;
fukasawa e60969
   size_t count = 0;
fukasawa e60969
fukasawa e60969
   for (;;)
fukasawa e60969
   {
fukasawa e60969
      size_t r = fread(last->buffer+count, 1/*size*/,
fukasawa e60969
         (sizeof last->buffer)-count, fp);
fukasawa e60969
fukasawa e60969
      if (r > 0)
fukasawa e60969
      {
fukasawa e60969
         count += r;
fukasawa e60969
fukasawa e60969
         if (count >= sizeof last->buffer)
fukasawa e60969
         {
fukasawa e60969
            assert(count == sizeof last->buffer);
fukasawa e60969
            count = 0;
fukasawa e60969
fukasawa e60969
            if (last->next == NULL)
fukasawa e60969
            {
fukasawa e60969
               last = buffer_extend(last);
fukasawa e60969
               if (last == NULL)
fukasawa e60969
                  return MEMORY;
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            else
fukasawa e60969
               last = last->next;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else /* fread failed - probably end of file */
fukasawa e60969
      {
fukasawa e60969
         if (feof(fp))
fukasawa e60969
         {
fukasawa e60969
            buffer->last = last;
fukasawa e60969
            buffer->end_count = count;
fukasawa e60969
            return 0; /* no error */
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         /* Some kind of funky error; errno should be non-zero */
fukasawa e60969
         return errno == 0 ? ERANGE : errno;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* This structure is used to control the test of a single file. */
fukasawa e60969
typedef enum
fukasawa e60969
{
fukasawa e60969
   VERBOSE,        /* switches on all messages */
fukasawa e60969
   INFORMATION,
fukasawa e60969
   WARNINGS,       /* switches on warnings */
fukasawa e60969
   LIBPNG_WARNING,
fukasawa e60969
   APP_WARNING,
fukasawa e60969
   ERRORS,         /* just errors */
fukasawa e60969
   APP_FAIL,       /* continuable error - no need to longjmp */
fukasawa e60969
   LIBPNG_ERROR,   /* this and higher cause a longjmp */
fukasawa e60969
   LIBPNG_BUG,     /* erroneous behavior in libpng */
fukasawa e60969
   APP_ERROR,      /* such as out-of-memory in a callback */
fukasawa e60969
   QUIET,          /* no normal messages */
fukasawa e60969
   USER_ERROR,     /* such as file-not-found */
fukasawa e60969
   INTERNAL_ERROR
fukasawa e60969
} error_level;
fukasawa e60969
#define LEVEL_MASK      0xf   /* where the level is in 'options' */
fukasawa e60969
fukasawa e60969
#define EXHAUSTIVE      0x010 /* Test all combinations of active options */
fukasawa e60969
#define STRICT          0x020 /* Fail on warnings as well as errors */
fukasawa e60969
#define LOG             0x040 /* Log pass/fail to stdout */
fukasawa e60969
#define CONTINUE        0x080 /* Continue on APP_FAIL errors */
fukasawa e60969
#define SKIP_BUGS       0x100 /* Skip over known bugs */
fukasawa e60969
#define LOG_SKIPPED     0x200 /* Log skipped bugs */
fukasawa e60969
#define FIND_BAD_COMBOS 0x400 /* Attempt to deduce bad combos */
fukasawa e60969
#define LIST_COMBOS     0x800 /* List combos by name */
fukasawa e60969
fukasawa e60969
/* Result masks apply to the result bits in the 'results' field below; these
fukasawa e60969
 * bits are simple 1U<
fukasawa e60969
 * warnings (--relaxes) or nothing worse than information (--strict)
fukasawa e60969
 */
fukasawa e60969
#define RESULT_STRICT(r)   (((r) & ~((1U<
fukasawa e60969
#define RESULT_RELAXED(r)  (((r) & ~((1U<
fukasawa e60969
fukasawa e60969
struct display
fukasawa e60969
{
fukasawa e60969
   jmp_buf        error_return;      /* Where to go to on error */
fukasawa e60969
fukasawa e60969
   const char    *filename;          /* The name of the original file */
fukasawa e60969
   const char    *operation;         /* Operation being performed */
fukasawa e60969
   int            transforms;        /* Transform used in operation */
fukasawa e60969
   png_uint_32    options;           /* See display_log below */
fukasawa e60969
   png_uint_32    results;           /* A mask of errors seen */
fukasawa e60969
fukasawa e60969
fukasawa e60969
   png_structp    original_pp;       /* used on the original read */
fukasawa e60969
   png_infop      original_ip;       /* set by the original read */
fukasawa e60969
fukasawa e60969
   png_size_t     original_rowbytes; /* of the original rows: */
fukasawa e60969
   png_bytepp     original_rows;     /* from the original read */
fukasawa e60969
fukasawa e60969
   /* Original chunks valid */
fukasawa e60969
   png_uint_32    chunks;
fukasawa e60969
fukasawa e60969
   /* Original IHDR information */
fukasawa e60969
   png_uint_32    width;
fukasawa e60969
   png_uint_32    height;
fukasawa e60969
   int            bit_depth;
fukasawa e60969
   int            color_type;
fukasawa e60969
   int            interlace_method;
fukasawa e60969
   int            compression_method;
fukasawa e60969
   int            filter_method;
fukasawa e60969
fukasawa e60969
   /* Derived information for the original image. */
fukasawa e60969
   int            active_transforms;  /* transforms that do something on read */
fukasawa e60969
   int            ignored_transforms; /* transforms that should do nothing */
fukasawa e60969
fukasawa e60969
   /* Used on a read, both the original read and when validating a written
fukasawa e60969
    * image.
fukasawa e60969
    */
fukasawa e60969
   png_structp    read_pp;
fukasawa e60969
   png_infop      read_ip;
fukasawa e60969
fukasawa e60969
#  ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
      /* Used to write a new image (the original info_ptr is used) */
fukasawa e60969
      png_structp   write_pp;
fukasawa e60969
      struct buffer written_file;   /* where the file gets written */
fukasawa e60969
#  endif
fukasawa e60969
fukasawa e60969
   struct buffer  original_file;     /* Data read from the original file */
fukasawa e60969
};
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
display_init(struct display *dp)
fukasawa e60969
   /* Call this only once right at the start to initialize the control
fukasawa e60969
    * structure, the (struct buffer) lists are maintained across calls - the
fukasawa e60969
    * memory is not freed.
fukasawa e60969
    */
fukasawa e60969
{
fukasawa e60969
   memset(dp, 0, sizeof *dp);
fukasawa e60969
   dp->options = WARNINGS; /* default to !verbose, !quiet */
fukasawa e60969
   dp->filename = NULL;
fukasawa e60969
   dp->operation = NULL;
fukasawa e60969
   dp->original_pp = NULL;
fukasawa e60969
   dp->original_ip = NULL;
fukasawa e60969
   dp->original_rows = NULL;
fukasawa e60969
   dp->read_pp = NULL;
fukasawa e60969
   dp->read_ip = NULL;
fukasawa e60969
   buffer_init(&dp->original_file);
fukasawa e60969
fukasawa e60969
#  ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
      dp->write_pp = NULL;
fukasawa e60969
      buffer_init(&dp->written_file);
fukasawa e60969
#  endif
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
display_clean_read(struct display *dp)
fukasawa e60969
{
fukasawa e60969
   if (dp->read_pp != NULL)
fukasawa e60969
      png_destroy_read_struct(&dp->read_pp, &dp->read_ip, NULL);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
static void
fukasawa e60969
display_clean_write(struct display *dp)
fukasawa e60969
{
fukasawa e60969
      if (dp->write_pp != NULL)
fukasawa e60969
         png_destroy_write_struct(&dp->write_pp, NULL);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
display_clean(struct display *dp)
fukasawa e60969
{
fukasawa e60969
#  ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
      display_clean_write(dp);
fukasawa e60969
#  endif
fukasawa e60969
   display_clean_read(dp);
fukasawa e60969
fukasawa e60969
   dp->original_rowbytes = 0;
fukasawa e60969
   dp->original_rows = NULL;
fukasawa e60969
   dp->chunks = 0;
fukasawa e60969
fukasawa e60969
   png_destroy_read_struct(&dp->original_pp, &dp->original_ip, NULL);
fukasawa e60969
   /* leave the filename for error detection */
fukasawa e60969
   dp->results = 0; /* reset for next time */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
display_destroy(struct display *dp)
fukasawa e60969
{
fukasawa e60969
    /* Release any memory held in the display. */
fukasawa e60969
#  ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
      buffer_destroy(&dp->written_file);
fukasawa e60969
#  endif
fukasawa e60969
fukasawa e60969
   buffer_destroy(&dp->original_file);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static struct display *
fukasawa e60969
get_dp(png_structp pp)
fukasawa e60969
   /* The display pointer is always stored in the png_struct error pointer */
fukasawa e60969
{
fukasawa e60969
   struct display *dp = (struct display*)png_get_error_ptr(pp);
fukasawa e60969
fukasawa e60969
   if (dp == NULL)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "pngimage: internal error (no display)\n");
fukasawa e60969
      exit(99); /* prevents a crash */
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return dp;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* error handling */
fukasawa e60969
#ifdef __GNUC__
fukasawa e60969
#  define VGATTR __attribute__((__format__ (__printf__,3,4)))
fukasawa e60969
   /* Required to quiet GNUC warnings when the compiler sees a stdarg function
fukasawa e60969
    * that calls one of the stdio v APIs.
fukasawa e60969
    */
fukasawa e60969
#else
fukasawa e60969
#  define VGATTR
fukasawa e60969
#endif
fukasawa e60969
static void VGATTR
fukasawa e60969
display_log(struct display *dp, error_level level, const char *fmt, ...)
fukasawa e60969
   /* 'level' is as above, fmt is a stdio style format string.  This routine
fukasawa e60969
    * does not return if level is above LIBPNG_WARNING
fukasawa e60969
    */
fukasawa e60969
{
fukasawa e60969
   dp->results |= 1U << level;
fukasawa e60969
fukasawa e60969
   if (level > (error_level)(dp->options & LEVEL_MASK))
fukasawa e60969
   {
fukasawa e60969
      const char *lp;
fukasawa e60969
      va_list ap;
fukasawa e60969
fukasawa e60969
      switch (level)
fukasawa e60969
      {
fukasawa e60969
         case INFORMATION:    lp = "information"; break;
fukasawa e60969
         case LIBPNG_WARNING: lp = "warning(libpng)"; break;
fukasawa e60969
         case APP_WARNING:    lp = "warning(pngimage)"; break;
fukasawa e60969
         case APP_FAIL:       lp = "error(continuable)"; break;
fukasawa e60969
         case LIBPNG_ERROR:   lp = "error(libpng)"; break;
fukasawa e60969
         case LIBPNG_BUG:     lp = "bug(libpng)"; break;
fukasawa e60969
         case APP_ERROR:      lp = "error(pngimage)"; break;
fukasawa e60969
         case USER_ERROR:     lp = "error(user)"; break;
fukasawa e60969
fukasawa e60969
         case INTERNAL_ERROR: /* anything unexpected is an internal error: */
fukasawa e60969
         case VERBOSE: case WARNINGS: case ERRORS: case QUIET:
fukasawa e60969
         default:             lp = "bug(pngimage)"; break;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      fprintf(stderr, "%s: %s: %s",
fukasawa e60969
         dp->filename != NULL ? dp->filename : "<stdin>", lp, dp->operation);</stdin>
fukasawa e60969
fukasawa e60969
      if (dp->transforms != 0)
fukasawa e60969
      {
fukasawa e60969
         int tr = dp->transforms;
fukasawa e60969
fukasawa e60969
         if (is_combo(tr))
fukasawa e60969
         {
fukasawa e60969
            if (dp->options & LIST_COMBOS)
fukasawa e60969
            {
fukasawa e60969
               int trx = tr;
fukasawa e60969
fukasawa e60969
               fprintf(stderr, "(");
fukasawa e60969
               if (trx)
fukasawa e60969
               {
fukasawa e60969
                  int start = 0;
fukasawa e60969
fukasawa e60969
                  while (trx)
fukasawa e60969
                  {
fukasawa e60969
                     int trz = trx & -trx;
fukasawa e60969
fukasawa e60969
                     if (start) fprintf(stderr, "+");
fukasawa e60969
                     fprintf(stderr, "%s", transform_name(trz));
fukasawa e60969
                     start = 1;
fukasawa e60969
                     trx &= ~trz;
fukasawa e60969
                  }
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               else
fukasawa e60969
                  fprintf(stderr, "-");
fukasawa e60969
               fprintf(stderr, ")");
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            else
fukasawa e60969
               fprintf(stderr, "(0x%x)", tr);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            fprintf(stderr, "(%s)", transform_name(tr));
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      fprintf(stderr, ": ");
fukasawa e60969
fukasawa e60969
      va_start(ap, fmt);
fukasawa e60969
      vfprintf(stderr, fmt, ap);
fukasawa e60969
      va_end(ap);
fukasawa e60969
fukasawa e60969
      fputc('\n', stderr);
fukasawa e60969
   }
fukasawa e60969
   /* else do not output any message */
fukasawa e60969
fukasawa e60969
   /* Errors cause this routine to exit to the fail code */
fukasawa e60969
   if (level > APP_FAIL || (level > ERRORS && !(dp->options & CONTINUE)))
fukasawa e60969
      longjmp(dp->error_return, level);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* error handler callbacks for libpng */
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
display_warning(png_structp pp, png_const_charp warning)
fukasawa e60969
{
fukasawa e60969
   display_log(get_dp(pp), LIBPNG_WARNING, "%s", warning);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
display_error(png_structp pp, png_const_charp error)
fukasawa e60969
{
fukasawa e60969
   struct display *dp = get_dp(pp);
fukasawa e60969
fukasawa e60969
   display_log(dp, LIBPNG_ERROR, "%s", error);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
display_cache_file(struct display *dp, const char *filename)
fukasawa e60969
   /* Does the initial cache of the file. */
fukasawa e60969
{
fukasawa e60969
   FILE *fp;
fukasawa e60969
   int ret;
fukasawa e60969
fukasawa e60969
   dp->filename = filename;
fukasawa e60969
fukasawa e60969
   if (filename != NULL)
fukasawa e60969
   {
fukasawa e60969
      fp = fopen(filename, "rb");
fukasawa e60969
      if (fp == NULL)
fukasawa e60969
         display_log(dp, USER_ERROR, "open failed: %s", strerror(errno));
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      fp = stdin;
fukasawa e60969
fukasawa e60969
   ret = buffer_from_file(&dp->original_file, fp);
fukasawa e60969
fukasawa e60969
   fclose(fp);
fukasawa e60969
fukasawa e60969
   if (ret != 0)
fukasawa e60969
      display_log(dp, APP_ERROR, "read failed: %s", strerror(ret));
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
buffer_read(struct display *dp, struct buffer *bp, png_bytep data,
fukasawa e60969
   png_size_t size)
fukasawa e60969
{
fukasawa e60969
   struct buffer_list *last = bp->current;
fukasawa e60969
   size_t read_count = bp->read_count;
fukasawa e60969
fukasawa e60969
   while (size > 0)
fukasawa e60969
   {
fukasawa e60969
      size_t avail;
fukasawa e60969
fukasawa e60969
      if (last == NULL ||
fukasawa e60969
         (last == bp->last && read_count >= bp->end_count))
fukasawa e60969
      {
fukasawa e60969
         display_log(dp, USER_ERROR, "file truncated (%lu bytes)",
fukasawa e60969
            (unsigned long)size);
fukasawa e60969
         /*NOTREACHED*/
fukasawa e60969
         break;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (read_count >= sizeof last->buffer)
fukasawa e60969
      {
fukasawa e60969
         /* Move to the next buffer: */
fukasawa e60969
         last = last->next;
fukasawa e60969
         read_count = 0;
fukasawa e60969
         bp->current = last; /* Avoid update outside the loop */
fukasawa e60969
fukasawa e60969
         /* And do a sanity check (the EOF case is caught above) */
fukasawa e60969
         if (last == NULL)
fukasawa e60969
         {
fukasawa e60969
            display_log(dp, INTERNAL_ERROR, "damaged buffer list");
fukasawa e60969
            /*NOTREACHED*/
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      avail = (sizeof last->buffer) - read_count;
fukasawa e60969
      if (avail > size)
fukasawa e60969
         avail = size;
fukasawa e60969
fukasawa e60969
      memcpy(data, last->buffer + read_count, avail);
fukasawa e60969
      read_count += avail;
fukasawa e60969
      size -= avail;
fukasawa e60969
      data += avail;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   bp->read_count = read_count;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
read_function(png_structp pp, png_bytep data, png_size_t size)
fukasawa e60969
{
fukasawa e60969
   buffer_read(get_dp(pp), get_buffer(pp), data, size);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
read_png(struct display *dp, struct buffer *bp, const char *operation,
fukasawa e60969
   int transforms)
fukasawa e60969
{
fukasawa e60969
   png_structp pp;
fukasawa e60969
   png_infop   ip;
fukasawa e60969
fukasawa e60969
   /* This cleans out any previous read and sets operation and transforms to
fukasawa e60969
    * empty.
fukasawa e60969
    */
fukasawa e60969
   display_clean_read(dp);
fukasawa e60969
fukasawa e60969
   if (operation != NULL) /* else this is a verify and do not overwrite info */
fukasawa e60969
   {
fukasawa e60969
      dp->operation = operation;
fukasawa e60969
      dp->transforms = transforms;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   dp->read_pp = pp = png_create_read_struct(PNG_LIBPNG_VER_STRING, dp,
fukasawa e60969
      display_error, display_warning);
fukasawa e60969
   if (pp == NULL)
fukasawa e60969
      display_log(dp, LIBPNG_ERROR, "failed to create read struct");
fukasawa e60969
fukasawa e60969
   /* The png_read_png API requires us to make the info struct, but it does the
fukasawa e60969
    * call to png_read_info.
fukasawa e60969
    */
fukasawa e60969
   dp->read_ip = ip = png_create_info_struct(pp);
fukasawa e60969
   if (ip == NULL)
fukasawa e60969
      display_log(dp, LIBPNG_ERROR, "failed to create info struct");
fukasawa e60969
fukasawa e60969
#  ifdef PNG_SET_USER_LIMITS_SUPPORTED
fukasawa e60969
      /* Remove the user limits, if any */
fukasawa e60969
      png_set_user_limits(pp, 0x7fffffff, 0x7fffffff);
fukasawa e60969
#  endif
fukasawa e60969
fukasawa e60969
   /* Set the IO handling */
fukasawa e60969
   buffer_start_read(bp);
fukasawa e60969
   png_set_read_fn(pp, bp, read_function);
fukasawa e60969
fukasawa e60969
   png_read_png(pp, ip, transforms, NULL/*params*/);
fukasawa e60969
fukasawa e60969
#if 0 /* crazy debugging */
fukasawa e60969
   {
fukasawa e60969
      png_bytep pr = png_get_rows(pp, ip)[0];
fukasawa e60969
      size_t rb = png_get_rowbytes(pp, ip);
fukasawa e60969
      size_t cb;
fukasawa e60969
      char c = ' ';
fukasawa e60969
fukasawa e60969
      fprintf(stderr, "%.4x %2d (%3lu bytes):", transforms, png_get_bit_depth(pp,ip), (unsigned long)rb);
fukasawa e60969
fukasawa e60969
      for (cb=0; cb
fukasawa e60969
         fputc(c, stderr), fprintf(stderr, "%.2x", pr[cb]), c='.';
fukasawa e60969
fukasawa e60969
      fputc('\n', stderr);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
update_display(struct display *dp)
fukasawa e60969
   /* called once after the first read to update all the info, original_pp and
fukasawa e60969
    * original_ip must have been filled in.
fukasawa e60969
    */
fukasawa e60969
{
fukasawa e60969
   png_structp pp;
fukasawa e60969
   png_infop   ip;
fukasawa e60969
fukasawa e60969
   /* Now perform the initial read with a 0 tranform. */
fukasawa e60969
   read_png(dp, &dp->original_file, "original read", 0/*no transform*/);
fukasawa e60969
fukasawa e60969
   /* Move the result to the 'original' fields */
fukasawa e60969
   dp->original_pp = pp = dp->read_pp, dp->read_pp = NULL;
fukasawa e60969
   dp->original_ip = ip = dp->read_ip, dp->read_ip = NULL;
fukasawa e60969
fukasawa e60969
   dp->original_rowbytes = png_get_rowbytes(pp, ip);
fukasawa e60969
   if (dp->original_rowbytes == 0)
fukasawa e60969
      display_log(dp, LIBPNG_BUG, "png_get_rowbytes returned 0");
fukasawa e60969
fukasawa e60969
   dp->chunks = png_get_valid(pp, ip, 0xffffffff);
fukasawa e60969
   if ((dp->chunks & PNG_INFO_IDAT) == 0) /* set by png_read_png */
fukasawa e60969
      display_log(dp, LIBPNG_BUG, "png_read_png did not set IDAT flag");
fukasawa e60969
fukasawa e60969
   dp->original_rows = png_get_rows(pp, ip);
fukasawa e60969
   if (dp->original_rows == NULL)
fukasawa e60969
      display_log(dp, LIBPNG_BUG, "png_read_png did not create row buffers");
fukasawa e60969
fukasawa e60969
   if (!png_get_IHDR(pp, ip,
fukasawa e60969
      &dp->width, &dp->height, &dp->bit_depth, &dp->color_type,
fukasawa e60969
      &dp->interlace_method, &dp->compression_method, &dp->filter_method))
fukasawa e60969
      display_log(dp, LIBPNG_BUG, "png_get_IHDR failed");
fukasawa e60969
fukasawa e60969
   /* 'active' transforms are discovered based on the original image format;
fukasawa e60969
    * running one active transform can activate others.  At present the code
fukasawa e60969
    * does not attempt to determine the closure.
fukasawa e60969
    */
fukasawa e60969
   {
fukasawa e60969
      png_uint_32 chunks = dp->chunks;
fukasawa e60969
      int active = 0, inactive = 0;
fukasawa e60969
      int ct = dp->color_type;
fukasawa e60969
      int bd = dp->bit_depth;
fukasawa e60969
      unsigned int i;
fukasawa e60969
fukasawa e60969
      for (i=0; i
fukasawa e60969
      {
fukasawa e60969
         int transform = transform_info[i].transform;
fukasawa e60969
fukasawa e60969
         if ((transform_info[i].valid_chunks == 0 ||
fukasawa e60969
               (transform_info[i].valid_chunks & chunks) != 0) &&
fukasawa e60969
            (transform_info[i].color_mask_required & ct) ==
fukasawa e60969
               transform_info[i].color_mask_required &&
fukasawa e60969
            (transform_info[i].color_mask_absent & ct) == 0 &&
fukasawa e60969
            (transform_info[i].bit_depths & bd) != 0 &&
fukasawa e60969
            (transform_info[i].when & TRANSFORM_R) != 0)
fukasawa e60969
            active |= transform;
fukasawa e60969
fukasawa e60969
         else if ((transform_info[i].when & TRANSFORM_R) != 0)
fukasawa e60969
            inactive |= transform;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Some transforms appear multiple times in the table; the 'active' status
fukasawa e60969
       * is the logical OR of these and the inactive status must be adjusted to
fukasawa e60969
       * take this into account.
fukasawa e60969
       */
fukasawa e60969
      inactive &= ~active;
fukasawa e60969
fukasawa e60969
      dp->active_transforms = active;
fukasawa e60969
      dp->ignored_transforms = inactive; /* excluding write-only transforms */
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
compare_read(struct display *dp, int applied_transforms)
fukasawa e60969
{
fukasawa e60969
   /* Compare the png_info from read_ip with original_info */
fukasawa e60969
   size_t rowbytes;
fukasawa e60969
   png_uint_32 width, height;
fukasawa e60969
   int bit_depth, color_type;
fukasawa e60969
   int interlace_method, compression_method, filter_method;
fukasawa e60969
   const char *e = NULL;
fukasawa e60969
fukasawa e60969
   png_get_IHDR(dp->read_pp, dp->read_ip, &width, &height, &bit_depth,
fukasawa e60969
      &color_type, &interlace_method, &compression_method, &filter_method);
fukasawa e60969
fukasawa e60969
#  define C(item) if (item != dp->item) \
fukasawa e60969
      display_log(dp, APP_WARNING, "IHDR " #item "(%lu) changed to %lu",\
fukasawa e60969
         (unsigned long)dp->item, (unsigned long)item), e = #item
fukasawa e60969
fukasawa e60969
   /* The IHDR should be identical: */
fukasawa e60969
   C(width);
fukasawa e60969
   C(height);
fukasawa e60969
   C(bit_depth);
fukasawa e60969
   C(color_type);
fukasawa e60969
   C(interlace_method);
fukasawa e60969
   C(compression_method);
fukasawa e60969
   C(filter_method);
fukasawa e60969
fukasawa e60969
   /* 'e' remains set to the name of the last thing changed: */
fukasawa e60969
   if (e)
fukasawa e60969
      display_log(dp, APP_ERROR, "IHDR changed (%s)", e);
fukasawa e60969
fukasawa e60969
   /* All the chunks from the original PNG should be preserved in the output PNG
fukasawa e60969
    * because the PNG format has not been changed.
fukasawa e60969
    */
fukasawa e60969
   {
fukasawa e60969
      unsigned long chunks =
fukasawa e60969
         png_get_valid(dp->read_pp, dp->read_ip, 0xffffffff);
fukasawa e60969
fukasawa e60969
      if (chunks != dp->chunks)
fukasawa e60969
         display_log(dp, APP_FAIL, "PNG chunks changed from 0x%lx to 0x%lx",
fukasawa e60969
            (unsigned long)dp->chunks, chunks);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* rowbytes should be the same */
fukasawa e60969
   rowbytes = png_get_rowbytes(dp->read_pp, dp->read_ip);
fukasawa e60969
fukasawa e60969
   /* NOTE: on 64-bit systems this may trash the top bits of rowbytes,
fukasawa e60969
    * which could lead to weird error messages.
fukasawa e60969
    */
fukasawa e60969
   if (rowbytes != dp->original_rowbytes)
fukasawa e60969
      display_log(dp, APP_ERROR, "PNG rowbytes changed from %lu to %lu",
fukasawa e60969
         (unsigned long)dp->original_rowbytes, (unsigned long)rowbytes);
fukasawa e60969
fukasawa e60969
   /* The rows should be the same too, unless the applied transforms includes
fukasawa e60969
    * the shift transform, in which case low bits may have been lost.
fukasawa e60969
    */
fukasawa e60969
   {
fukasawa e60969
      png_bytepp rows = png_get_rows(dp->read_pp, dp->read_ip);
fukasawa e60969
      unsigned int mask;  /* mask (if not zero) for the final byte */
fukasawa e60969
fukasawa e60969
      if (bit_depth < 8)
fukasawa e60969
      {
fukasawa e60969
         /* Need the stray bits at the end, this depends only on the low bits
fukasawa e60969
          * of the image width; overflow does not matter.  If the width is an
fukasawa e60969
          * exact multiple of 8 bits this gives a mask of 0, not 0xff.
fukasawa e60969
          */
fukasawa e60969
         mask = 0xff & (0xff00 >> ((bit_depth * width) & 7));
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         mask = 0;
fukasawa e60969
fukasawa e60969
      if (rows == NULL)
fukasawa e60969
         display_log(dp, LIBPNG_BUG, "png_get_rows returned NULL");
fukasawa e60969
fukasawa e60969
      if ((applied_transforms & PNG_TRANSFORM_SHIFT) == 0 ||
fukasawa e60969
         (dp->active_transforms & PNG_TRANSFORM_SHIFT) == 0 ||
fukasawa e60969
         color_type == PNG_COLOR_TYPE_PALETTE)
fukasawa e60969
      {
fukasawa e60969
         unsigned long y;
fukasawa e60969
fukasawa e60969
         for (y=0; y
fukasawa e60969
         {
fukasawa e60969
            png_bytep row = rows[y];
fukasawa e60969
            png_bytep orig = dp->original_rows[y];
fukasawa e60969
fukasawa e60969
            if (memcmp(row, orig, rowbytes-(mask != 0)) != 0 || (mask != 0 &&
fukasawa e60969
               ((row[rowbytes-1] & mask) != (orig[rowbytes-1] & mask))))
fukasawa e60969
            {
fukasawa e60969
               size_t x;
fukasawa e60969
fukasawa e60969
               /* Find the first error */
fukasawa e60969
               for (x=0; x
fukasawa e60969
                  break;
fukasawa e60969
fukasawa e60969
               display_log(dp, APP_FAIL,
fukasawa e60969
                  "byte(%lu,%lu) changed 0x%.2x -> 0x%.2x",
fukasawa e60969
                  (unsigned long)x, (unsigned long)y, orig[x], row[x]);
fukasawa e60969
               return 0; /* don't keep reporting failed rows on 'continue' */
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         unsigned long y;
fukasawa e60969
         int bpp;   /* bits-per-pixel then bytes-per-pixel */
fukasawa e60969
         /* components are up to 8 bytes in size */
fukasawa e60969
         png_byte sig_bits[8];
fukasawa e60969
         png_color_8p sBIT;
fukasawa e60969
fukasawa e60969
         if (png_get_sBIT(dp->read_pp, dp->read_ip, &sBIT) != PNG_INFO_sBIT)
fukasawa e60969
            display_log(dp, INTERNAL_ERROR,
fukasawa e60969
               "active shift transform but no sBIT in file");
fukasawa e60969
fukasawa e60969
         switch (color_type)
fukasawa e60969
         {
fukasawa e60969
            case PNG_COLOR_TYPE_GRAY:
fukasawa e60969
               sig_bits[0] = sBIT->gray;
fukasawa e60969
               bpp = bit_depth;
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case PNG_COLOR_TYPE_GA:
fukasawa e60969
               sig_bits[0] = sBIT->gray;
fukasawa e60969
               sig_bits[1] = sBIT->alpha;
fukasawa e60969
               bpp = 2 * bit_depth;
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case PNG_COLOR_TYPE_RGB:
fukasawa e60969
               sig_bits[0] = sBIT->red;
fukasawa e60969
               sig_bits[1] = sBIT->green;
fukasawa e60969
               sig_bits[2] = sBIT->blue;
fukasawa e60969
               bpp = 3 * bit_depth;
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case PNG_COLOR_TYPE_RGBA:
fukasawa e60969
               sig_bits[0] = sBIT->red;
fukasawa e60969
               sig_bits[1] = sBIT->green;
fukasawa e60969
               sig_bits[2] = sBIT->blue;
fukasawa e60969
               sig_bits[3] = sBIT->alpha;
fukasawa e60969
               bpp = 4 * bit_depth;
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            default:
fukasawa e60969
               display_log(dp, LIBPNG_ERROR, "invalid colour type %d",
fukasawa e60969
                  color_type);
fukasawa e60969
               /*NOTREACHED*/
fukasawa e60969
               bpp = 0;
fukasawa e60969
               break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         {
fukasawa e60969
            int b;
fukasawa e60969
fukasawa e60969
            for (b=0; 8*b
fukasawa e60969
            {
fukasawa e60969
               /* libpng should catch this; if not there is a security issue
fukasawa e60969
                * because an app (like this one) may overflow an array. In fact
fukasawa e60969
                * libpng doesn't catch this at present.
fukasawa e60969
                */
fukasawa e60969
               if (sig_bits[b] == 0 || sig_bits[b] > bit_depth/*!palette*/)
fukasawa e60969
                  display_log(dp, LIBPNG_BUG,
fukasawa e60969
                     "invalid sBIT[%u]  value %d returned for PNG bit depth %d",
fukasawa e60969
                     b, sig_bits[b], bit_depth);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         if (bpp < 8 && bpp != bit_depth)
fukasawa e60969
         {
fukasawa e60969
            /* sanity check; this is a grayscale PNG; something is wrong in the
fukasawa e60969
             * code above.
fukasawa e60969
             */
fukasawa e60969
            display_log(dp, INTERNAL_ERROR, "invalid bpp %u for bit_depth %u",
fukasawa e60969
               bpp, bit_depth);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         switch (bit_depth)
fukasawa e60969
         {
fukasawa e60969
            int b;
fukasawa e60969
fukasawa e60969
            case 16: /* Two bytes per component, big-endian */
fukasawa e60969
               for (b = (bpp >> 4); b > 0; --b)
fukasawa e60969
               {
fukasawa e60969
                  unsigned int sig = (unsigned int)(0xffff0000 >> sig_bits[b]);
fukasawa e60969
fukasawa e60969
                  sig_bits[2*b+1] = (png_byte)sig;
fukasawa e60969
                  sig_bits[2*b+0] = (png_byte)(sig >> 8); /* big-endian */
fukasawa e60969
               }
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case 8: /* One byte per component */
fukasawa e60969
               for (b=0; b*8 < bpp; ++b)
fukasawa e60969
                  sig_bits[b] = (png_byte)(0xff00 >> sig_bits[b]);
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case 1: /* allowed, but dumb */
fukasawa e60969
               /* Value is 1 */
fukasawa e60969
               sig_bits[0] = 0xff;
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case 2: /* Replicate 4 times */
fukasawa e60969
               /* Value is 1 or 2 */
fukasawa e60969
               b = 0x3 & ((0x3<<2) >> sig_bits[0]);
fukasawa e60969
               b |= b << 2;
fukasawa e60969
               b |= b << 4;
fukasawa e60969
               sig_bits[0] = (png_byte)b;
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case 4: /* Relicate twice */
fukasawa e60969
               /* Value is 1, 2, 3 or 4 */
fukasawa e60969
               b = 0xf & ((0xf << 4) >> sig_bits[0]);
fukasawa e60969
               b |= b << 4;
fukasawa e60969
               sig_bits[0] = (png_byte)b;
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            default:
fukasawa e60969
               display_log(dp, LIBPNG_BUG, "invalid bit depth %d", bit_depth);
fukasawa e60969
               break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         /* Convert bpp to bytes; this gives '1' for low-bit depth grayscale,
fukasawa e60969
          * where there are multiple pixels per byte.
fukasawa e60969
          */
fukasawa e60969
         bpp = (bpp+7) >> 3;
fukasawa e60969
fukasawa e60969
         /* The mask can be combined with sig_bits[0] */
fukasawa e60969
         if (mask != 0)
fukasawa e60969
         {
fukasawa e60969
            mask &= sig_bits[0];
fukasawa e60969
fukasawa e60969
            if (bpp != 1 || mask == 0)
fukasawa e60969
               display_log(dp, INTERNAL_ERROR, "mask calculation error %u, %u",
fukasawa e60969
                  bpp, mask);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         for (y=0; y
fukasawa e60969
         {
fukasawa e60969
            png_bytep row = rows[y];
fukasawa e60969
            png_bytep orig = dp->original_rows[y];
fukasawa e60969
            unsigned long x;
fukasawa e60969
fukasawa e60969
            for (x=0; x<(width-(mask!=0)); ++x)
fukasawa e60969
            {
fukasawa e60969
               int b;
fukasawa e60969
fukasawa e60969
               for (b=0; b
fukasawa e60969
               {
fukasawa e60969
                  if ((*row++ & sig_bits[b]) != (*orig++ & sig_bits[b]))
fukasawa e60969
                  {
fukasawa e60969
                     display_log(dp, APP_FAIL,
fukasawa e60969
                        "significant bits at (%lu[%u],%lu) changed %.2x->%.2x",
fukasawa e60969
                        x, b, y, orig[-1], row[-1]);
fukasawa e60969
                     return 0;
fukasawa e60969
                  }
fukasawa e60969
               }
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            if (mask != 0 && (*row & mask) != (*orig & mask))
fukasawa e60969
            {
fukasawa e60969
               display_log(dp, APP_FAIL,
fukasawa e60969
                  "significant bits at (%lu[end],%lu) changed", x, y);
fukasawa e60969
               return 0;
fukasawa e60969
            }
fukasawa e60969
         } /* for y */
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 1; /* compare succeeded */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
static void
fukasawa e60969
buffer_write(struct display *dp, struct buffer *buffer, png_bytep data,
fukasawa e60969
   png_size_t size)
fukasawa e60969
   /* Generic write function used both from the write callback provided to
fukasawa e60969
    * libpng and from the generic read code.
fukasawa e60969
    */
fukasawa e60969
{
fukasawa e60969
   /* Write the data into the buffer, adding buffers as required */
fukasawa e60969
   struct buffer_list *last = buffer->last;
fukasawa e60969
   size_t end_count = buffer->end_count;
fukasawa e60969
fukasawa e60969
   while (size > 0)
fukasawa e60969
   {
fukasawa e60969
      size_t avail;
fukasawa e60969
fukasawa e60969
      if (end_count >= sizeof last->buffer)
fukasawa e60969
      {
fukasawa e60969
         if (last->next == NULL)
fukasawa e60969
         {
fukasawa e60969
            last = buffer_extend(last);
fukasawa e60969
fukasawa e60969
            if (last == NULL)
fukasawa e60969
               display_log(dp, APP_ERROR, "out of memory saving file");
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            last = last->next;
fukasawa e60969
fukasawa e60969
         buffer->last = last; /* avoid the need to rewrite every time */
fukasawa e60969
         end_count = 0;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      avail = (sizeof last->buffer) - end_count;
fukasawa e60969
      if (avail > size)
fukasawa e60969
         avail = size;
fukasawa e60969
fukasawa e60969
      memcpy(last->buffer + end_count, data, avail);
fukasawa e60969
      end_count += avail;
fukasawa e60969
      size -= avail;
fukasawa e60969
      data += avail;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   buffer->end_count = end_count;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
write_function(png_structp pp, png_bytep data, png_size_t size)
fukasawa e60969
{
fukasawa e60969
   buffer_write(get_dp(pp), get_buffer(pp), data, size);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
write_png(struct display *dp, png_infop ip, int transforms)
fukasawa e60969
{
fukasawa e60969
   display_clean_write(dp); /* safety */
fukasawa e60969
fukasawa e60969
   buffer_start_write(&dp->written_file);
fukasawa e60969
   dp->operation = "write";
fukasawa e60969
   dp->transforms = transforms;
fukasawa e60969
fukasawa e60969
   dp->write_pp = png_create_write_struct(PNG_LIBPNG_VER_STRING, dp,
fukasawa e60969
      display_error, display_warning);
fukasawa e60969
fukasawa e60969
   if (dp->write_pp == NULL)
fukasawa e60969
      display_log(dp, APP_ERROR, "failed to create write png_struct");
fukasawa e60969
fukasawa e60969
   png_set_write_fn(dp->write_pp, &dp->written_file, write_function,
fukasawa e60969
      NULL/*flush*/);
fukasawa e60969
fukasawa e60969
#  ifdef PNG_SET_USER_LIMITS_SUPPORTED
fukasawa e60969
      /* Remove the user limits, if any */
fukasawa e60969
      png_set_user_limits(dp->write_pp, 0x7fffffff, 0x7fffffff);
fukasawa e60969
#  endif
fukasawa e60969
fukasawa e60969
   /* Certain transforms require the png_info to be zapped to allow the
fukasawa e60969
    * transform to work correctly.
fukasawa e60969
    */
fukasawa e60969
   if (transforms & (PNG_TRANSFORM_PACKING|
fukasawa e60969
                     PNG_TRANSFORM_STRIP_FILLER|
fukasawa e60969
                     PNG_TRANSFORM_STRIP_FILLER_BEFORE))
fukasawa e60969
   {
fukasawa e60969
      int ct = dp->color_type;
fukasawa e60969
fukasawa e60969
      if (transforms & (PNG_TRANSFORM_STRIP_FILLER|
fukasawa e60969
                        PNG_TRANSFORM_STRIP_FILLER_BEFORE))
fukasawa e60969
         ct &= ~PNG_COLOR_MASK_ALPHA;
fukasawa e60969
fukasawa e60969
      png_set_IHDR(dp->write_pp, ip, dp->width, dp->height, dp->bit_depth, ct,
fukasawa e60969
         dp->interlace_method, dp->compression_method, dp->filter_method);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_write_png(dp->write_pp, ip, transforms, NULL/*params*/);
fukasawa e60969
fukasawa e60969
   /* Clean it on the way out - if control returns to the caller then the
fukasawa e60969
    * written_file contains the required data.
fukasawa e60969
    */
fukasawa e60969
   display_clean_write(dp);
fukasawa e60969
}
fukasawa e60969
#endif /* WRITE_SUPPORTED */
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
skip_transform(struct display *dp, int tr)
fukasawa e60969
   /* Helper to test for a bad combo and log it if it is skipped */
fukasawa e60969
{
fukasawa e60969
   if ((dp->options & SKIP_BUGS) != 0 && is_bad_combo(tr))
fukasawa e60969
   {
fukasawa e60969
      /* Log this to stdout if logging is on, otherwise just do an information
fukasawa e60969
       * display_log.
fukasawa e60969
       */
fukasawa e60969
      if ((dp->options & LOG_SKIPPED) != 0)
fukasawa e60969
      {
fukasawa e60969
         printf("SKIP: %s transforms ", dp->filename);
fukasawa e60969
fukasawa e60969
         while (tr != 0)
fukasawa e60969
         {
fukasawa e60969
            int next = first_transform(tr);
fukasawa e60969
            tr &= ~next;
fukasawa e60969
fukasawa e60969
            printf("%s", transform_name(next));
fukasawa e60969
            if (tr != 0)
fukasawa e60969
               putchar('+');
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         putchar('\n');
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         display_log(dp, INFORMATION, "%s: skipped known bad combo 0x%x",
fukasawa e60969
            dp->filename, tr);
fukasawa e60969
fukasawa e60969
      return 1; /* skip */
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 0; /* don't skip */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
test_one_file(struct display *dp, const char *filename)
fukasawa e60969
{
fukasawa e60969
   /* First cache the file and update the display original file
fukasawa e60969
    * information for the new file.
fukasawa e60969
    */
fukasawa e60969
   dp->operation = "cache file";
fukasawa e60969
   dp->transforms = 0;
fukasawa e60969
   display_cache_file(dp, filename);
fukasawa e60969
   update_display(dp);
fukasawa e60969
fukasawa e60969
   /* First test: if there are options that should be ignored for this file
fukasawa e60969
    * verify that they really are ignored.
fukasawa e60969
    */
fukasawa e60969
   if (dp->ignored_transforms != 0)
fukasawa e60969
   {
fukasawa e60969
      read_png(dp, &dp->original_file, "ignored transforms",
fukasawa e60969
         dp->ignored_transforms);
fukasawa e60969
fukasawa e60969
      /* The result should be identical to the original_rows */
fukasawa e60969
      if (!compare_read(dp, 0/*transforms applied*/))
fukasawa e60969
         return; /* no point testing more */
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
   /* Second test: write the original PNG data out to a new file (to test the
fukasawa e60969
    * write side) then read the result back in and make sure that it hasn't
fukasawa e60969
    * changed.
fukasawa e60969
    */
fukasawa e60969
   dp->operation = "write";
fukasawa e60969
   write_png(dp, dp->original_ip, 0/*transforms*/);
fukasawa e60969
   read_png(dp, &dp->written_file, NULL, 0/*transforms*/);
fukasawa e60969
   if (!compare_read(dp, 0/*transforms applied*/))
fukasawa e60969
      return;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Third test: the active options.  Test each in turn, or, with the
fukasawa e60969
    * EXHAUSTIVE option, test all possible combinations.
fukasawa e60969
    */
fukasawa e60969
   {
fukasawa e60969
      /* Use unsigned int here because the code below to increment through all
fukasawa e60969
       * the possibilities exhaustively has to use a compare and that must be
fukasawa e60969
       * unsigned, because some transforms are negative on a 16-bit system.
fukasawa e60969
       */
fukasawa e60969
      unsigned int active = dp->active_transforms;
fukasawa e60969
      const int exhaustive = (dp->options & EXHAUSTIVE) != 0;
fukasawa e60969
      unsigned int current = first_transform(active);
fukasawa e60969
      unsigned int bad_transforms = 0;
fukasawa e60969
      unsigned int bad_combo = ~0U;    /* bitwise AND of failing transforms */
fukasawa e60969
      unsigned int bad_combo_list = 0; /* bitwise OR of failures */
fukasawa e60969
fukasawa e60969
      for (;;)
fukasawa e60969
      {
fukasawa e60969
         read_png(dp, &dp->original_file, "active transforms", current);
fukasawa e60969
fukasawa e60969
         /* If this involved any irreversible transformations then if we write
fukasawa e60969
          * it out with just the reversible transformations and read it in again
fukasawa e60969
          * with the same transforms we should get the same thing.  At present
fukasawa e60969
          * this isn't done - it just seems like a waste of time and it would
fukasawa e60969
          * require two sets of read png_struct/png_info.
fukasawa e60969
          *
fukasawa e60969
          * If there were no irreversible transformations then if we write it
fukasawa e60969
          * out and read it back in again (without the reversible transforms)
fukasawa e60969
          * we should get back to the place where we started.
fukasawa e60969
          */
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
         if ((current & write_transforms) == current)
fukasawa e60969
         {
fukasawa e60969
            /* All transforms reversible: write the PNG with the transformations
fukasawa e60969
             * reversed, then read it back in with no transformations.  The
fukasawa e60969
             * result should be the same as the original apart from the loss of
fukasawa e60969
             * low order bits because of the SHIFT/sBIT transform.
fukasawa e60969
             */
fukasawa e60969
            dp->operation = "reversible transforms";
fukasawa e60969
            write_png(dp, dp->read_ip, current);
fukasawa e60969
fukasawa e60969
            /* And if this is read back in, because all the transformations were
fukasawa e60969
             * reversible, the result should be the same.
fukasawa e60969
             */
fukasawa e60969
            read_png(dp, &dp->written_file, NULL, 0);
fukasawa e60969
            if (!compare_read(dp, current/*for the SHIFT/sBIT transform*/))
fukasawa e60969
            {
fukasawa e60969
               /* This set of transforms failed.  If a single bit is set - if
fukasawa e60969
                * there is just one transform - don't include this in further
fukasawa e60969
                * 'exhaustive' tests.  Notice that each transform is tested on
fukasawa e60969
                * its own before testing combos in the exhaustive case.
fukasawa e60969
                */
fukasawa e60969
               if (is_combo(current))
fukasawa e60969
               {
fukasawa e60969
                  bad_combo &= current;
fukasawa e60969
                  bad_combo_list |= current;
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               else
fukasawa e60969
                  bad_transforms |= current;
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
         /* Now move to the next transform */
fukasawa e60969
         if (exhaustive) /* all combinations */
fukasawa e60969
         {
fukasawa e60969
            unsigned int next = current;
fukasawa e60969
fukasawa e60969
            do
fukasawa e60969
            {
fukasawa e60969
               if (next == read_transforms) /* Everything tested */
fukasawa e60969
                  goto combo;
fukasawa e60969
fukasawa e60969
               ++next;
fukasawa e60969
            }  /* skip known bad combos if the relevant option is set; skip
fukasawa e60969
                * combos involving known bad single transforms in all cases.
fukasawa e60969
                */
fukasawa e60969
            while (  (next & read_transforms) <= current
fukasawa e60969
                  || (next & active) == 0 /* skip cases that do nothing */
fukasawa e60969
                  || (next & bad_transforms) != 0
fukasawa e60969
                  || skip_transform(dp, next));
fukasawa e60969
fukasawa e60969
            assert((next & read_transforms) == next);
fukasawa e60969
            current = next;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else /* one at a time */
fukasawa e60969
         {
fukasawa e60969
            active &= ~current;
fukasawa e60969
fukasawa e60969
            if (active == 0)
fukasawa e60969
               goto combo;
fukasawa e60969
fukasawa e60969
            current = first_transform(active);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
combo:
fukasawa e60969
      if (dp->options & FIND_BAD_COMBOS)
fukasawa e60969
      {
fukasawa e60969
         /* bad_combos identifies the combos that occur in all failing cases;
fukasawa e60969
          * bad_combo_list identifies transforms that do not prevent the
fukasawa e60969
          * failure.
fukasawa e60969
          */
fukasawa e60969
         if (bad_combo != ~0U)
fukasawa e60969
            printf("%s[0x%x]: PROBLEM: 0x%x[0x%x] ANTIDOTE: 0x%x\n",
fukasawa e60969
               dp->filename, active, bad_combo, bad_combo_list,
fukasawa e60969
               rw_transforms & ~bad_combo_list);
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            printf("%s: no %sbad combos found\n", dp->filename,
fukasawa e60969
               (dp->options & SKIP_BUGS) ? "additional " : "");
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
do_test(struct display *dp, const char *file)
fukasawa e60969
   /* Exists solely to isolate the setjmp clobbers */
fukasawa e60969
{
fukasawa e60969
   int ret = setjmp(dp->error_return);
fukasawa e60969
fukasawa e60969
   if (ret == 0)
fukasawa e60969
   {
fukasawa e60969
      test_one_file(dp, file);
fukasawa e60969
      return 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (ret < ERRORS) /* shouldn't longjmp on warnings */
fukasawa e60969
      display_log(dp, INTERNAL_ERROR, "unexpected return code %d", ret);
fukasawa e60969
fukasawa e60969
   return ret;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
int
fukasawa e60969
main(const int argc, const char * const * const argv)
fukasawa e60969
{
fukasawa e60969
   /* For each file on the command line test it with a range of transforms */
fukasawa e60969
   int option_end, ilog = 0;
fukasawa e60969
   struct display d;
fukasawa e60969
fukasawa e60969
   validate_T();
fukasawa e60969
   display_init(&d);
fukasawa e60969
fukasawa e60969
   for (option_end=1; option_end
fukasawa e60969
   {
fukasawa e60969
      const char *name = argv[option_end];
fukasawa e60969
fukasawa e60969
      if (strcmp(name, "--verbose") == 0)
fukasawa e60969
         d.options = (d.options & ~LEVEL_MASK) | VERBOSE;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--warnings") == 0)
fukasawa e60969
         d.options = (d.options & ~LEVEL_MASK) | WARNINGS;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--errors") == 0)
fukasawa e60969
         d.options = (d.options & ~LEVEL_MASK) | ERRORS;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--quiet") == 0)
fukasawa e60969
         d.options = (d.options & ~LEVEL_MASK) | QUIET;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--exhaustive") == 0)
fukasawa e60969
         d.options |= EXHAUSTIVE;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--fast") == 0)
fukasawa e60969
         d.options &= ~EXHAUSTIVE;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--strict") == 0)
fukasawa e60969
         d.options |= STRICT;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--relaxed") == 0)
fukasawa e60969
         d.options &= ~STRICT;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--log") == 0)
fukasawa e60969
      {
fukasawa e60969
         ilog = option_end; /* prevent display */
fukasawa e60969
         d.options |= LOG;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--nolog") == 0)
fukasawa e60969
         d.options &= ~LOG;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--continue") == 0)
fukasawa e60969
         d.options |= CONTINUE;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--stop") == 0)
fukasawa e60969
         d.options &= ~CONTINUE;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--skip-bugs") == 0)
fukasawa e60969
         d.options |= SKIP_BUGS;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--test-all") == 0)
fukasawa e60969
         d.options &= ~SKIP_BUGS;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--log-skipped") == 0)
fukasawa e60969
         d.options |= LOG_SKIPPED;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--nolog-skipped") == 0)
fukasawa e60969
         d.options &= ~LOG_SKIPPED;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--find-bad-combos") == 0)
fukasawa e60969
         d.options |= FIND_BAD_COMBOS;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--nofind-bad-combos") == 0)
fukasawa e60969
         d.options &= ~FIND_BAD_COMBOS;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--list-combos") == 0)
fukasawa e60969
         d.options |= LIST_COMBOS;
fukasawa e60969
fukasawa e60969
      else if (strcmp(name, "--nolist-combos") == 0)
fukasawa e60969
         d.options &= ~LIST_COMBOS;
fukasawa e60969
fukasawa e60969
      else if (name[0] == '-' && name[1] == '-')
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "pngimage: %s: unknown option\n", name);
fukasawa e60969
         return 99;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         break; /* Not an option */
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   {
fukasawa e60969
      int i;
fukasawa e60969
      int errors = 0;
fukasawa e60969
fukasawa e60969
      for (i=option_end; i
fukasawa e60969
      {
fukasawa e60969
         {
fukasawa e60969
            int ret = do_test(&d, argv[i]);
fukasawa e60969
fukasawa e60969
            if (ret > QUIET) /* abort on user or internal error */
fukasawa e60969
               return 99;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         /* Here on any return, including failures, except user/internal issues
fukasawa e60969
          */
fukasawa e60969
         {
fukasawa e60969
            const int pass = (d.options & STRICT) ?
fukasawa e60969
               RESULT_STRICT(d.results) : RESULT_RELAXED(d.results);
fukasawa e60969
fukasawa e60969
            if (!pass)
fukasawa e60969
               ++errors;
fukasawa e60969
fukasawa e60969
            if (d.options & LOG)
fukasawa e60969
            {
fukasawa e60969
               int j;
fukasawa e60969
fukasawa e60969
               printf("%s: pngimage ", pass ? "PASS" : "FAIL");
fukasawa e60969
fukasawa e60969
               for (j=1; j
fukasawa e60969
                  printf("%s ", argv[j]);
fukasawa e60969
fukasawa e60969
               printf("%s\n", d.filename);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         display_clean(&d);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Release allocated memory */
fukasawa e60969
      display_destroy(&d);
fukasawa e60969
fukasawa e60969
      return errors != 0;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#else /* !INFO_IMAGE || !SEQUENTIAL_READ || !READ_PNG*/
fukasawa e60969
int
fukasawa e60969
main(void)
fukasawa e60969
{
fukasawa e60969
   fprintf(stderr, "pngimage: no support for png_read/write_image\n");
fukasawa e60969
   return 77;
fukasawa e60969
}
fukasawa e60969
#endif