fukasawa e60969
fukasawa e60969
/* pngtest.c - a simple test program to test libpng
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.5.25 [December 3, 2015]
fukasawa e60969
 * Copyright (c) 1998-2002,2004,2006-2015 Glenn Randers-Pehrson
fukasawa e60969
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
fukasawa e60969
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
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
 * This program reads in a PNG image, writes it out again, and then
fukasawa e60969
 * compares the two files.  If the files are identical, this shows that
fukasawa e60969
 * the basic chunk handling, filtering, and (de)compression code is working
fukasawa e60969
 * properly.  It does not currently test all of the transforms, although
fukasawa e60969
 * it probably should.
fukasawa e60969
 *
fukasawa e60969
 * The program will report "FAIL" in certain legitimate cases:
fukasawa e60969
 * 1) when the compression level or filter selection method is changed.
fukasawa e60969
 * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
fukasawa e60969
 * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
fukasawa e60969
 *    exist in the input file.
fukasawa e60969
 * 4) others not listed here...
fukasawa e60969
 * In these cases, it is best to check with another tool such as "pngcheck"
fukasawa e60969
 * to see what the differences between the two files are.
fukasawa e60969
 *
fukasawa e60969
 * If a filename is given on the command-line, then this file is used
fukasawa e60969
 * for the input, rather than the default "pngtest.png".  This allows
fukasawa e60969
 * testing a wide variety of files easily.  You can also test a number
fukasawa e60969
 * of files at once by typing "pngtest -m file1.png file2.png ..."
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
#define _POSIX_SOURCE 1
fukasawa e60969
fukasawa e60969
#include <stdio.h></stdio.h>
fukasawa e60969
#include <stdlib.h></stdlib.h>
fukasawa e60969
#include <string.h></string.h>
fukasawa e60969
fukasawa e60969
/* Defined so I can write to a file on gui/windowing platforms */
fukasawa e60969
/*  #define STDERR stderr  */
fukasawa e60969
#define STDERR stdout   /* For DOS */
fukasawa e60969
fukasawa e60969
#include "png.h"
fukasawa e60969
fukasawa e60969
/* Known chunks that exist in pngtest.png must be supported or pngtest will fail
fukasawa e60969
 * simply as a result of re-ordering them.  This may be fixed in 1.7
fukasawa e60969
 *
fukasawa e60969
 * pngtest allocates a single row buffer for each row and overwrites it,
fukasawa e60969
 * therefore if the write side doesn't support the writing of interlaced images
fukasawa e60969
 * nothing can be done for an interlaced image (and the code below will fail
fukasawa e60969
 * horribly trying to write extra data after writing garbage).
fukasawa e60969
 */
fukasawa e60969
#if defined PNG_READ_SUPPORTED && /* else nothing can be done */\
fukasawa e60969
   defined PNG_READ_bKGD_SUPPORTED &&\
fukasawa e60969
   defined PNG_READ_cHRM_SUPPORTED &&\
fukasawa e60969
   defined PNG_READ_gAMA_SUPPORTED &&\
fukasawa e60969
   defined PNG_READ_oFFs_SUPPORTED &&\
fukasawa e60969
   defined PNG_READ_pCAL_SUPPORTED &&\
fukasawa e60969
   defined PNG_READ_pHYs_SUPPORTED &&\
fukasawa e60969
   defined PNG_READ_sBIT_SUPPORTED &&\
fukasawa e60969
   defined PNG_READ_sCAL_SUPPORTED &&\
fukasawa e60969
   defined PNG_READ_sRGB_SUPPORTED &&\
fukasawa e60969
   defined PNG_READ_sPLT_SUPPORTED &&\
fukasawa e60969
   defined PNG_READ_tEXt_SUPPORTED &&\
fukasawa e60969
   defined PNG_READ_tIME_SUPPORTED &&\
fukasawa e60969
   defined PNG_READ_zTXt_SUPPORTED &&\
fukasawa e60969
   (defined PNG_WRITE_INTERLACING_SUPPORTED || PNG_LIBPNG_VER >= 10700)
fukasawa e60969
fukasawa e60969
#ifdef PNG_ZLIB_HEADER
fukasawa e60969
#  include PNG_ZLIB_HEADER /* defined by pnglibconf.h from 1.7 */
fukasawa e60969
#else
fukasawa e60969
#  include "zlib.h"
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Copied from pngpriv.h but only used in error messages below. */
fukasawa e60969
#ifndef PNG_ZBUF_SIZE
fukasawa e60969
#  define PNG_ZBUF_SIZE 8192
fukasawa e60969
#endif
fukasawa e60969
#define FCLOSE(file) fclose(file)
fukasawa e60969
fukasawa e60969
#ifndef PNG_STDIO_SUPPORTED
fukasawa e60969
typedef FILE                * png_FILE_p;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Makes pngtest verbose so we can find problems. */
fukasawa e60969
#ifndef PNG_DEBUG
fukasawa e60969
#  define PNG_DEBUG 0
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#if PNG_DEBUG > 1
fukasawa e60969
#  define pngtest_debug(m)        ((void)fprintf(stderr, m "\n"))
fukasawa e60969
#  define pngtest_debug1(m,p1)    ((void)fprintf(stderr, m "\n", p1))
fukasawa e60969
#  define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))
fukasawa e60969
#else
fukasawa e60969
#  define pngtest_debug(m)        ((void)0)
fukasawa e60969
#  define pngtest_debug1(m,p1)    ((void)0)
fukasawa e60969
#  define pngtest_debug2(m,p1,p2) ((void)0)
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#if !PNG_DEBUG
fukasawa e60969
#  define SINGLE_ROWBUF_ALLOC  /* Makes buffer overruns easier to nail */
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifndef PNG_UNUSED
fukasawa e60969
#  define PNG_UNUSED(param) (void)param;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Turn on CPU timing
fukasawa e60969
#define PNGTEST_TIMING
fukasawa e60969
*/
fukasawa e60969
fukasawa e60969
#ifndef PNG_FLOATING_POINT_SUPPORTED
fukasawa e60969
#undef PNGTEST_TIMING
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNGTEST_TIMING
fukasawa e60969
static float t_start, t_stop, t_decode, t_encode, t_misc;
fukasawa e60969
#include <time.h></time.h>
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_TIME_RFC1123_SUPPORTED
fukasawa e60969
#define PNG_tIME_STRING_LENGTH 29
fukasawa e60969
static int tIME_chunk_present = 0;
fukasawa e60969
static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
fukasawa e60969
fukasawa e60969
#if PNG_LIBPNG_VER < 10619
fukasawa e60969
#define png_convert_to_rfc1123_buffer(ts, t) tIME_to_str(read_ptr, ts, t)
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
tIME_to_str(png_structp png_ptr, png_charp ts, png_const_timep t)
fukasawa e60969
{
fukasawa e60969
    png_const_charp str = png_convert_to_rfc1123(png_ptr, t);
fukasawa e60969
fukasawa e60969
    if (str == NULL)
fukasawa e60969
        return 0;
fukasawa e60969
fukasawa e60969
    strcpy(ts, str);
fukasawa e60969
    return 1;
fukasawa e60969
}
fukasawa e60969
#endif /* older libpng */
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
static int verbose = 0;
fukasawa e60969
static int strict = 0;
fukasawa e60969
static int relaxed = 0;
fukasawa e60969
static int unsupported_chunks = 0; /* chunk unsupported by libpng in input */
fukasawa e60969
static int error_count = 0; /* count calls to png_error */
fukasawa e60969
static int warning_count = 0; /* count calls to png_warning */
fukasawa e60969
fukasawa e60969
/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
fukasawa e60969
#ifndef png_jmpbuf
fukasawa e60969
#  define png_jmpbuf(png_ptr) png_ptr->jmpbuf
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Defines for unknown chunk handling if required. */
fukasawa e60969
#ifndef PNG_HANDLE_CHUNK_ALWAYS
fukasawa e60969
#  define PNG_HANDLE_CHUNK_ALWAYS       3
fukasawa e60969
#endif
fukasawa e60969
#ifndef PNG_HANDLE_CHUNK_IF_SAFE
fukasawa e60969
#  define PNG_HANDLE_CHUNK_IF_SAFE      2
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Utility to save typing/errors, the argument must be a name */
fukasawa e60969
#define MEMZERO(var) ((void)memset(&var, 0, sizeof var))
fukasawa e60969
fukasawa e60969
/* Example of using row callbacks to make a simple progress meter */
fukasawa e60969
static int status_pass = 1;
fukasawa e60969
static int status_dots_requested = 0;
fukasawa e60969
static int status_dots = 1;
fukasawa e60969
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   if (status_pass != pass)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stdout, "\n Pass %d: ", pass);
fukasawa e60969
      status_pass = pass;
fukasawa e60969
      status_dots = 31;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   status_dots--;
fukasawa e60969
fukasawa e60969
   if (status_dots == 0)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stdout, "\n         ");
fukasawa e60969
      status_dots=30;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   fprintf(stdout, "r");
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   fprintf(stdout, "w");
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
fukasawa e60969
/* Example of using a user transform callback (doesn't do anything at present).
fukasawa e60969
 */
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
read_user_callback(png_structp png_ptr, png_row_infop row_info, png_bytep data)
fukasawa e60969
{
fukasawa e60969
   PNG_UNUSED(png_ptr)
fukasawa e60969
   PNG_UNUSED(row_info)
fukasawa e60969
   PNG_UNUSED(data)
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
fukasawa e60969
/* Example of using user transform callback (we don't transform anything,
fukasawa e60969
 * but merely count the zero samples)
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
static png_uint_32 zero_samples;
fukasawa e60969
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
fukasawa e60969
{
fukasawa e60969
   png_bytep dp = data;
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   /* Contents of row_info:
fukasawa e60969
    *  png_uint_32 width      width of row
fukasawa e60969
    *  png_uint_32 rowbytes   number of bytes in row
fukasawa e60969
    *  png_byte color_type    color type of pixels
fukasawa e60969
    *  png_byte bit_depth     bit depth of samples
fukasawa e60969
    *  png_byte channels      number of channels (1-4)
fukasawa e60969
    *  png_byte pixel_depth   bits per pixel (depth*channels)
fukasawa e60969
    */
fukasawa e60969
fukasawa e60969
    /* Counts the number of zero samples (or zero pixels if color_type is 3 */
fukasawa e60969
fukasawa e60969
    if (row_info->color_type == 0 || row_info->color_type == 3)
fukasawa e60969
    {
fukasawa e60969
       int pos = 0;
fukasawa e60969
       png_uint_32 n, nstop;
fukasawa e60969
fukasawa e60969
       for (n = 0, nstop=row_info->width; n
fukasawa e60969
       {
fukasawa e60969
          if (row_info->bit_depth == 1)
fukasawa e60969
          {
fukasawa e60969
             if (((*dp << pos++ ) & 0x80) == 0)
fukasawa e60969
                zero_samples++;
fukasawa e60969
fukasawa e60969
             if (pos == 8)
fukasawa e60969
             {
fukasawa e60969
                pos = 0;
fukasawa e60969
                dp++;
fukasawa e60969
             }
fukasawa e60969
          }
fukasawa e60969
fukasawa e60969
          if (row_info->bit_depth == 2)
fukasawa e60969
          {
fukasawa e60969
             if (((*dp << (pos+=2)) & 0xc0) == 0)
fukasawa e60969
                zero_samples++;
fukasawa e60969
fukasawa e60969
             if (pos == 8)
fukasawa e60969
             {
fukasawa e60969
                pos = 0;
fukasawa e60969
                dp++;
fukasawa e60969
             }
fukasawa e60969
          }
fukasawa e60969
fukasawa e60969
          if (row_info->bit_depth == 4)
fukasawa e60969
          {
fukasawa e60969
             if (((*dp << (pos+=4)) & 0xf0) == 0)
fukasawa e60969
                zero_samples++;
fukasawa e60969
fukasawa e60969
             if (pos == 8)
fukasawa e60969
             {
fukasawa e60969
                pos = 0;
fukasawa e60969
                dp++;
fukasawa e60969
             }
fukasawa e60969
          }
fukasawa e60969
fukasawa e60969
          if (row_info->bit_depth == 8)
fukasawa e60969
             if (*dp++ == 0)
fukasawa e60969
                zero_samples++;
fukasawa e60969
fukasawa e60969
          if (row_info->bit_depth == 16)
fukasawa e60969
          {
fukasawa e60969
             if ((*dp | *(dp+1)) == 0)
fukasawa e60969
                zero_samples++;
fukasawa e60969
             dp+=2;
fukasawa e60969
          }
fukasawa e60969
       }
fukasawa e60969
    }
fukasawa e60969
    else /* Other color types */
fukasawa e60969
    {
fukasawa e60969
       png_uint_32 n, nstop;
fukasawa e60969
       int channel;
fukasawa e60969
       int color_channels = row_info->channels;
fukasawa e60969
       if (row_info->color_type > 3)
fukasawa e60969
          color_channels--;
fukasawa e60969
fukasawa e60969
       for (n = 0, nstop=row_info->width; n
fukasawa e60969
       {
fukasawa e60969
          for (channel = 0; channel < color_channels; channel++)
fukasawa e60969
          {
fukasawa e60969
             if (row_info->bit_depth == 8)
fukasawa e60969
                if (*dp++ == 0)
fukasawa e60969
                   zero_samples++;
fukasawa e60969
fukasawa e60969
             if (row_info->bit_depth == 16)
fukasawa e60969
             {
fukasawa e60969
                if ((*dp | *(dp+1)) == 0)
fukasawa e60969
                   zero_samples++;
fukasawa e60969
fukasawa e60969
                dp+=2;
fukasawa e60969
             }
fukasawa e60969
          }
fukasawa e60969
          if (row_info->color_type > 3)
fukasawa e60969
          {
fukasawa e60969
             dp++;
fukasawa e60969
             if (row_info->bit_depth == 16)
fukasawa e60969
                dp++;
fukasawa e60969
          }
fukasawa e60969
       }
fukasawa e60969
    }
fukasawa e60969
}
fukasawa e60969
#endif /* WRITE_USER_TRANSFORM */
fukasawa e60969
fukasawa e60969
#ifndef PNG_STDIO_SUPPORTED
fukasawa e60969
/* START of code to validate stdio-free compilation */
fukasawa e60969
/* These copies of the default read/write functions come from pngrio.c and
fukasawa e60969
 * pngwio.c.  They allow "don't include stdio" testing of the library.
fukasawa e60969
 * This is the function that does the actual reading of data.  If you are
fukasawa e60969
 * not reading from a standard C stream, you should create a replacement
fukasawa e60969
 * read_data function and use it at run time with png_set_read_fn(), rather
fukasawa e60969
 * than changing the library.
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
#ifdef PNG_IO_STATE_SUPPORTED
fukasawa e60969
void
fukasawa e60969
pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
fukasawa e60969
   png_uint_32 io_op);
fukasawa e60969
void
fukasawa e60969
pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
fukasawa e60969
   png_uint_32 io_op)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 io_state = png_get_io_state(png_ptr);
fukasawa e60969
   int err = 0;
fukasawa e60969
fukasawa e60969
   /* Check if the current operation (reading / writing) is as expected. */
fukasawa e60969
   if ((io_state & PNG_IO_MASK_OP) != io_op)
fukasawa e60969
      png_error(png_ptr, "Incorrect operation in I/O state");
fukasawa e60969
fukasawa e60969
   /* Check if the buffer size specific to the current location
fukasawa e60969
    * (file signature / header / data / crc) is as expected.
fukasawa e60969
    */
fukasawa e60969
   switch (io_state & PNG_IO_MASK_LOC)
fukasawa e60969
   {
fukasawa e60969
   case PNG_IO_SIGNATURE:
fukasawa e60969
      if (data_length > 8)
fukasawa e60969
         err = 1;
fukasawa e60969
      break;
fukasawa e60969
   case PNG_IO_CHUNK_HDR:
fukasawa e60969
      if (data_length != 8)
fukasawa e60969
         err = 1;
fukasawa e60969
      break;
fukasawa e60969
   case PNG_IO_CHUNK_DATA:
fukasawa e60969
      break;  /* no restrictions here */
fukasawa e60969
   case PNG_IO_CHUNK_CRC:
fukasawa e60969
      if (data_length != 4)
fukasawa e60969
         err = 1;
fukasawa e60969
      break;
fukasawa e60969
   default:
fukasawa e60969
      err = 1;  /* uninitialized */
fukasawa e60969
   }
fukasawa e60969
   if (err != 0)
fukasawa e60969
      png_error(png_ptr, "Bad I/O state or buffer size");
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
fukasawa e60969
{
fukasawa e60969
   png_size_t check = 0;
fukasawa e60969
   png_voidp io_ptr;
fukasawa e60969
fukasawa e60969
   /* fread() returns 0 on error, so it is OK to store this in a png_size_t
fukasawa e60969
    * instead of an int, which is what fread() actually returns.
fukasawa e60969
    */
fukasawa e60969
   io_ptr = png_get_io_ptr(png_ptr);
fukasawa e60969
   if (io_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
      check = fread(data, 1, length, (png_FILE_p)io_ptr);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (check != length)
fukasawa e60969
   {
fukasawa e60969
      png_error(png_ptr, "Read Error");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#ifdef PNG_IO_STATE_SUPPORTED
fukasawa e60969
   pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
fukasawa e60969
#endif
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_FLUSH_SUPPORTED
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
pngtest_flush(png_structp png_ptr)
fukasawa e60969
{
fukasawa e60969
   /* Do nothing; fflush() is said to be just a waste of energy. */
fukasawa e60969
   PNG_UNUSED(png_ptr)   /* Stifle compiler warning */
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* This is the function that does the actual writing of data.  If you are
fukasawa e60969
 * not writing to a standard C stream, you should create a replacement
fukasawa e60969
 * write_data function and use it at run time with png_set_write_fn(), rather
fukasawa e60969
 * than changing the library.
fukasawa e60969
 */
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
fukasawa e60969
{
fukasawa e60969
   png_size_t check;
fukasawa e60969
fukasawa e60969
   check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
fukasawa e60969
fukasawa e60969
   if (check != length)
fukasawa e60969
   {
fukasawa e60969
      png_error(png_ptr, "Write Error");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#ifdef PNG_IO_STATE_SUPPORTED
fukasawa e60969
   pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
fukasawa e60969
#endif
fukasawa e60969
}
fukasawa e60969
#endif /* !STDIO */
fukasawa e60969
fukasawa e60969
/* This function is called when there is a warning, but the library thinks
fukasawa e60969
 * it can continue anyway.  Replacement functions don't have to do anything
fukasawa e60969
 * here if you don't want to.  In the default configuration, png_ptr is
fukasawa e60969
 * not used, but it is passed in case it may be useful.
fukasawa e60969
 */
fukasawa e60969
typedef struct
fukasawa e60969
{
fukasawa e60969
   PNG_CONST char *file_name;
fukasawa e60969
}  pngtest_error_parameters;
fukasawa e60969
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
pngtest_warning(png_structp png_ptr, png_const_charp message)
fukasawa e60969
{
fukasawa e60969
   PNG_CONST char *name = "UNKNOWN (ERROR!)";
fukasawa e60969
   pngtest_error_parameters *test =
fukasawa e60969
      (pngtest_error_parameters*)png_get_error_ptr(png_ptr);
fukasawa e60969
fukasawa e60969
   ++warning_count;
fukasawa e60969
fukasawa e60969
   if (test != NULL && test->file_name != NULL)
fukasawa e60969
      name = test->file_name;
fukasawa e60969
fukasawa e60969
   fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* This is the default error handling function.  Note that replacements for
fukasawa e60969
 * this function MUST NOT RETURN, or the program will likely crash.  This
fukasawa e60969
 * function is used by default, or if the program supplies NULL for the
fukasawa e60969
 * error function pointer in png_set_error_fn().
fukasawa e60969
 */
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
pngtest_error(png_structp png_ptr, png_const_charp message)
fukasawa e60969
{
fukasawa e60969
   ++error_count;
fukasawa e60969
fukasawa e60969
   pngtest_warning(png_ptr, message);
fukasawa e60969
   /* We can return because png_error calls the default handler, which is
fukasawa e60969
    * actually OK in this case.
fukasawa e60969
    */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* END of code to validate stdio-free compilation */
fukasawa e60969
fukasawa e60969
/* START of code to validate memory allocation and deallocation */
fukasawa e60969
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
fukasawa e60969
fukasawa e60969
/* Allocate memory.  For reasonable files, size should never exceed
fukasawa e60969
 * 64K.  However, zlib may allocate more than 64K if you don't tell
fukasawa e60969
 * it not to.  See zconf.h and png.h for more information.  zlib does
fukasawa e60969
 * need to allocate exactly 64K, so whatever you call here must
fukasawa e60969
 * have the ability to do that.
fukasawa e60969
 *
fukasawa e60969
 * This piece of code can be compiled to validate max 64K allocations
fukasawa e60969
 * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
fukasawa e60969
 */
fukasawa e60969
typedef struct memory_information
fukasawa e60969
{
fukasawa e60969
   png_alloc_size_t          size;
fukasawa e60969
   png_voidp                 pointer;
fukasawa e60969
   struct memory_information *next;
fukasawa e60969
} memory_information;
fukasawa e60969
typedef memory_information *memory_infop;
fukasawa e60969
fukasawa e60969
static memory_infop pinformation = NULL;
fukasawa e60969
static int current_allocation = 0;
fukasawa e60969
static int maximum_allocation = 0;
fukasawa e60969
static int total_allocation = 0;
fukasawa e60969
static int num_allocations = 0;
fukasawa e60969
fukasawa e60969
png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,
fukasawa e60969
    png_alloc_size_t size));
fukasawa e60969
void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
fukasawa e60969
fukasawa e60969
png_voidp
fukasawa e60969
PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
fukasawa e60969
{
fukasawa e60969
fukasawa e60969
   /* png_malloc has already tested for NULL; png_create_struct calls
fukasawa e60969
    * png_debug_malloc directly, with png_ptr == NULL which is OK
fukasawa e60969
    */
fukasawa e60969
fukasawa e60969
   if (size == 0)
fukasawa e60969
      return (NULL);
fukasawa e60969
fukasawa e60969
   /* This calls the library allocator twice, once to get the requested
fukasawa e60969
      buffer and once to get a new free list entry. */
fukasawa e60969
   {
fukasawa e60969
      /* Disable malloc_fn and free_fn */
fukasawa e60969
      memory_infop pinfo;
fukasawa e60969
      png_set_mem_fn(png_ptr, NULL, NULL, NULL);
fukasawa e60969
      pinfo = (memory_infop)png_malloc(png_ptr,
fukasawa e60969
         (sizeof *pinfo));
fukasawa e60969
      pinfo->size = size;
fukasawa e60969
      current_allocation += size;
fukasawa e60969
      total_allocation += size;
fukasawa e60969
      num_allocations ++;
fukasawa e60969
fukasawa e60969
      if (current_allocation > maximum_allocation)
fukasawa e60969
         maximum_allocation = current_allocation;
fukasawa e60969
fukasawa e60969
      pinfo->pointer = png_malloc(png_ptr, size);
fukasawa e60969
      /* Restore malloc_fn and free_fn */
fukasawa e60969
fukasawa e60969
      png_set_mem_fn(png_ptr,
fukasawa e60969
          NULL, png_debug_malloc, png_debug_free);
fukasawa e60969
fukasawa e60969
      if (size != 0 && pinfo->pointer == NULL)
fukasawa e60969
      {
fukasawa e60969
         current_allocation -= size;
fukasawa e60969
         total_allocation -= size;
fukasawa e60969
         png_error(png_ptr,
fukasawa e60969
           "out of memory in pngtest->png_debug_malloc");
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      pinfo->next = pinformation;
fukasawa e60969
      pinformation = pinfo;
fukasawa e60969
      /* Make sure the caller isn't assuming zeroed memory. */
fukasawa e60969
      memset(pinfo->pointer, 0xdd, pinfo->size);
fukasawa e60969
fukasawa e60969
      if (verbose != 0)
fukasawa e60969
         printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
fukasawa e60969
            pinfo->pointer);
fukasawa e60969
fukasawa e60969
      return (png_voidp)(pinfo->pointer);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Free a pointer.  It is removed from the list at the same time. */
fukasawa e60969
void PNGCBAPI
fukasawa e60969
png_debug_free(png_structp png_ptr, png_voidp ptr)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      fprintf(STDERR, "NULL pointer to png_debug_free.\n");
fukasawa e60969
fukasawa e60969
   if (ptr == 0)
fukasawa e60969
   {
fukasawa e60969
#if 0 /* This happens all the time. */
fukasawa e60969
      fprintf(STDERR, "WARNING: freeing NULL pointer\n");
fukasawa e60969
#endif
fukasawa e60969
      return;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Unlink the element from the list. */
fukasawa e60969
   if (pinformation != NULL)
fukasawa e60969
   {
fukasawa e60969
      memory_infop *ppinfo = &pinformation;
fukasawa e60969
fukasawa e60969
      for (;;)
fukasawa e60969
      {
fukasawa e60969
         memory_infop pinfo = *ppinfo;
fukasawa e60969
fukasawa e60969
         if (pinfo->pointer == ptr)
fukasawa e60969
         {
fukasawa e60969
            *ppinfo = pinfo->next;
fukasawa e60969
            current_allocation -= pinfo->size;
fukasawa e60969
            if (current_allocation < 0)
fukasawa e60969
               fprintf(STDERR, "Duplicate free of memory\n");
fukasawa e60969
            /* We must free the list element too, but first kill
fukasawa e60969
               the memory that is to be freed. */
fukasawa e60969
            memset(ptr, 0x55, pinfo->size);
fukasawa e60969
            free(pinfo);
fukasawa e60969
            pinfo = NULL;
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         if (pinfo->next == NULL)
fukasawa e60969
         {
fukasawa e60969
            fprintf(STDERR, "Pointer %p not found\n", ptr);
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         ppinfo = &pinfo->next;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Finally free the data. */
fukasawa e60969
   if (verbose != 0)
fukasawa e60969
      printf("Freeing %p\n", ptr);
fukasawa e60969
fukasawa e60969
   if (ptr != NULL)
fukasawa e60969
      free(ptr);
fukasawa e60969
   ptr = NULL;
fukasawa e60969
}
fukasawa e60969
#endif /* USER_MEM && DEBUG */
fukasawa e60969
/* END of code to test memory allocation/deallocation */
fukasawa e60969
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
fukasawa e60969
/* Demonstration of user chunk support of the sTER and vpAg chunks */
fukasawa e60969
fukasawa e60969
/* (sTER is a public chunk not yet known by libpng.  vpAg is a private
fukasawa e60969
chunk used in ImageMagick to store "virtual page" size).  */
fukasawa e60969
fukasawa e60969
static struct user_chunk_data
fukasawa e60969
{
fukasawa e60969
   png_const_infop info_ptr;
fukasawa e60969
   png_uint_32     vpAg_width, vpAg_height;
fukasawa e60969
   png_byte        vpAg_units;
fukasawa e60969
   png_byte        sTER_mode;
fukasawa e60969
   int             location[2];
fukasawa e60969
}
fukasawa e60969
user_chunk_data;
fukasawa e60969
fukasawa e60969
/* Used for location and order; zero means nothing. */
fukasawa e60969
#define have_sTER   0x01
fukasawa e60969
#define have_vpAg   0x02
fukasawa e60969
#define before_PLTE 0x10
fukasawa e60969
#define before_IDAT 0x20
fukasawa e60969
#define after_IDAT  0x40
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
init_callback_info(png_const_infop info_ptr)
fukasawa e60969
{
fukasawa e60969
   MEMZERO(user_chunk_data);
fukasawa e60969
   user_chunk_data.info_ptr = info_ptr;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
set_location(png_structp png_ptr, struct user_chunk_data *data, int what)
fukasawa e60969
{
fukasawa e60969
   int location;
fukasawa e60969
fukasawa e60969
   if ((data->location[0] & what) != 0 || (data->location[1] & what) != 0)
fukasawa e60969
      return 0; /* already have one of these */
fukasawa e60969
fukasawa e60969
   /* Find where we are (the code below zeroes info_ptr to indicate that the
fukasawa e60969
    * chunks before the first IDAT have been read.)
fukasawa e60969
    */
fukasawa e60969
   if (data->info_ptr == NULL) /* after IDAT */
fukasawa e60969
      location = what | after_IDAT;
fukasawa e60969
fukasawa e60969
   else if (png_get_valid(png_ptr, data->info_ptr, PNG_INFO_PLTE) != 0)
fukasawa e60969
      location = what | before_IDAT;
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      location = what | before_PLTE;
fukasawa e60969
fukasawa e60969
   if (data->location[0] == 0)
fukasawa e60969
      data->location[0] = location;
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      data->location[1] = location;
fukasawa e60969
fukasawa e60969
   return 1; /* handled */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int PNGCBAPI
fukasawa e60969
read_user_chunk_callback(png_struct *png_ptr, png_unknown_chunkp chunk)
fukasawa e60969
{
fukasawa e60969
   struct user_chunk_data *my_user_chunk_data =
fukasawa e60969
      (struct user_chunk_data*)png_get_user_chunk_ptr(png_ptr);
fukasawa e60969
fukasawa e60969
   if (my_user_chunk_data == NULL)
fukasawa e60969
      png_error(png_ptr, "lost user chunk pointer");
fukasawa e60969
fukasawa e60969
   /* Return one of the following:
fukasawa e60969
    *    return (-n);  chunk had an error
fukasawa e60969
    *    return (0);  did not recognize
fukasawa e60969
    *    return (n);  success
fukasawa e60969
    *
fukasawa e60969
    * The unknown chunk structure contains the chunk data:
fukasawa e60969
    * png_byte name[5];
fukasawa e60969
    * png_byte *data;
fukasawa e60969
    * png_size_t size;
fukasawa e60969
    *
fukasawa e60969
    * Note that libpng has already taken care of the CRC handling.
fukasawa e60969
    */
fukasawa e60969
fukasawa e60969
   if (chunk->name[0] == 115 && chunk->name[1] ==  84 &&     /* s  T */
fukasawa e60969
       chunk->name[2] ==  69 && chunk->name[3] ==  82)       /* E  R */
fukasawa e60969
      {
fukasawa e60969
         /* Found sTER chunk */
fukasawa e60969
         if (chunk->size != 1)
fukasawa e60969
            return (-1); /* Error return */
fukasawa e60969
fukasawa e60969
         if (chunk->data[0] != 0 && chunk->data[0] != 1)
fukasawa e60969
            return (-1);  /* Invalid mode */
fukasawa e60969
fukasawa e60969
         if (set_location(png_ptr, my_user_chunk_data, have_sTER) != 0)
fukasawa e60969
         {
fukasawa e60969
            my_user_chunk_data->sTER_mode=chunk->data[0];
fukasawa e60969
            return (1);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            return (0); /* duplicate sTER - give it to libpng */
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
   if (chunk->name[0] != 118 || chunk->name[1] != 112 ||    /* v  p */
fukasawa e60969
       chunk->name[2] !=  65 || chunk->name[3] != 103)      /* A  g */
fukasawa e60969
      return (0); /* Did not recognize */
fukasawa e60969
fukasawa e60969
   /* Found ImageMagick vpAg chunk */
fukasawa e60969
fukasawa e60969
   if (chunk->size != 9)
fukasawa e60969
      return (-1); /* Error return */
fukasawa e60969
fukasawa e60969
   if (set_location(png_ptr, my_user_chunk_data, have_vpAg) == 0)
fukasawa e60969
      return (0);  /* duplicate vpAg */
fukasawa e60969
fukasawa e60969
   my_user_chunk_data->vpAg_width = png_get_uint_31(png_ptr, chunk->data);
fukasawa e60969
   my_user_chunk_data->vpAg_height = png_get_uint_31(png_ptr, chunk->data + 4);
fukasawa e60969
   my_user_chunk_data->vpAg_units = chunk->data[8];
fukasawa e60969
fukasawa e60969
   return (1);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
static void
fukasawa e60969
write_sTER_chunk(png_structp write_ptr)
fukasawa e60969
{
fukasawa e60969
   png_byte sTER[5] = {115,  84,  69,  82, '\0'};
fukasawa e60969
fukasawa e60969
   if (verbose != 0)
fukasawa e60969
      fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode);
fukasawa e60969
fukasawa e60969
   png_write_chunk(write_ptr, sTER, &user_chunk_data.sTER_mode, 1);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
write_vpAg_chunk(png_structp write_ptr)
fukasawa e60969
{
fukasawa e60969
   png_byte vpAg[5] = {118, 112,  65, 103, '\0'};
fukasawa e60969
fukasawa e60969
   png_byte vpag_chunk_data[9];
fukasawa e60969
fukasawa e60969
   if (verbose != 0)
fukasawa e60969
      fprintf(STDERR, " vpAg = %lu x %lu, units = %d\n",
fukasawa e60969
        (unsigned long)user_chunk_data.vpAg_width,
fukasawa e60969
        (unsigned long)user_chunk_data.vpAg_height,
fukasawa e60969
        user_chunk_data.vpAg_units);
fukasawa e60969
fukasawa e60969
   png_save_uint_32(vpag_chunk_data, user_chunk_data.vpAg_width);
fukasawa e60969
   png_save_uint_32(vpag_chunk_data + 4, user_chunk_data.vpAg_height);
fukasawa e60969
   vpag_chunk_data[8] = user_chunk_data.vpAg_units;
fukasawa e60969
   png_write_chunk(write_ptr, vpAg, vpag_chunk_data, 9);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
write_chunks(png_structp write_ptr, int location)
fukasawa e60969
{
fukasawa e60969
   int i;
fukasawa e60969
fukasawa e60969
   /* Notice that this preserves the original chunk order, however chunks
fukasawa e60969
    * intercepted by the callback will be written *after* chunks passed to
fukasawa e60969
    * libpng.  This will actually reverse a pair of sTER chunks or a pair of
fukasawa e60969
    * vpAg chunks, resulting in an error later.  This is not worth worrying
fukasawa e60969
    * about - the chunks should not be duplicated!
fukasawa e60969
    */
fukasawa e60969
   for (i=0; i<2; ++i)
fukasawa e60969
   {
fukasawa e60969
      if (user_chunk_data.location[i] == (location | have_sTER))
fukasawa e60969
         write_sTER_chunk(write_ptr);
fukasawa e60969
fukasawa e60969
      else if (user_chunk_data.location[i] == (location | have_vpAg))
fukasawa e60969
         write_vpAg_chunk(write_ptr);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif /* WRITE */
fukasawa e60969
#else /* !READ_USER_CHUNKS */
fukasawa e60969
#  define write_chunks(pp,loc) ((void)0)
fukasawa e60969
#endif
fukasawa e60969
/* END of code to demonstrate user chunk support */
fukasawa e60969
fukasawa e60969
/* START of code to check that libpng has the required text support; this only
fukasawa e60969
 * checks for the write support because if read support is missing the chunk
fukasawa e60969
 * will simply not be reported back to pngtest.
fukasawa e60969
 */
fukasawa e60969
#ifdef PNG_TEXT_SUPPORTED
fukasawa e60969
static void
fukasawa e60969
pngtest_check_text_support(png_structp png_ptr, png_textp text_ptr,
fukasawa e60969
   int num_text)
fukasawa e60969
{
fukasawa e60969
   while (num_text > 0)
fukasawa e60969
   {
fukasawa e60969
      switch (text_ptr[--num_text].compression)
fukasawa e60969
      {
fukasawa e60969
         case PNG_TEXT_COMPRESSION_NONE:
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case PNG_TEXT_COMPRESSION_zTXt:
fukasawa e60969
#           ifndef PNG_WRITE_zTXt_SUPPORTED
fukasawa e60969
               ++unsupported_chunks;
fukasawa e60969
               /* In libpng 1.7 this now does an app-error, so stop it: */
fukasawa e60969
               text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
fukasawa e60969
#           endif
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case PNG_ITXT_COMPRESSION_NONE:
fukasawa e60969
         case PNG_ITXT_COMPRESSION_zTXt:
fukasawa e60969
#           ifndef PNG_WRITE_iTXt_SUPPORTED
fukasawa e60969
               ++unsupported_chunks;
fukasawa e60969
               text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
fukasawa e60969
#           endif
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         default:
fukasawa e60969
            /* This is an error */
fukasawa e60969
            png_error(png_ptr, "invalid text chunk compression field");
fukasawa e60969
            break;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
/* END of code to check that libpng has the required text support */
fukasawa e60969
fukasawa e60969
/* Test one file */
fukasawa e60969
static int
fukasawa e60969
test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
fukasawa e60969
{
fukasawa e60969
   static png_FILE_p fpin;
fukasawa e60969
   static png_FILE_p fpout;  /* "static" prevents setjmp corruption */
fukasawa e60969
   pngtest_error_parameters error_parameters;
fukasawa e60969
   png_structp read_ptr;
fukasawa e60969
   png_infop read_info_ptr, end_info_ptr;
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
   png_structp write_ptr;
fukasawa e60969
   png_infop write_info_ptr;
fukasawa e60969
   png_infop write_end_info_ptr;
fukasawa e60969
#ifdef PNG_WRITE_FILTER_SUPPORTED
fukasawa e60969
   int interlace_preserved = 1;
fukasawa e60969
#endif /* WRITE_FILTER */
fukasawa e60969
#else /* !WRITE */
fukasawa e60969
   png_structp write_ptr = NULL;
fukasawa e60969
   png_infop write_info_ptr = NULL;
fukasawa e60969
   png_infop write_end_info_ptr = NULL;
fukasawa e60969
#endif /* !WRITE */
fukasawa e60969
   png_bytep row_buf;
fukasawa e60969
   png_uint_32 y;
fukasawa e60969
   png_uint_32 width, height;
fukasawa e60969
   volatile int num_passes;
fukasawa e60969
   int pass;
fukasawa e60969
   int bit_depth, color_type;
fukasawa e60969
fukasawa e60969
   row_buf = NULL;
fukasawa e60969
   error_parameters.file_name = inname;
fukasawa e60969
fukasawa e60969
   if ((fpin = fopen(inname, "rb")) == NULL)
fukasawa e60969
   {
fukasawa e60969
      fprintf(STDERR, "Could not find input file %s\n", inname);
fukasawa e60969
      return (1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if ((fpout = fopen(outname, "wb")) == NULL)
fukasawa e60969
   {
fukasawa e60969
      fprintf(STDERR, "Could not open output file %s\n", outname);
fukasawa e60969
      FCLOSE(fpin);
fukasawa e60969
      return (1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   pngtest_debug("Allocating read and write structures");
fukasawa e60969
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
fukasawa e60969
   read_ptr =
fukasawa e60969
      png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
fukasawa e60969
      NULL, NULL, NULL, png_debug_malloc, png_debug_free);
fukasawa e60969
#else
fukasawa e60969
   read_ptr =
fukasawa e60969
      png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
fukasawa e60969
#endif
fukasawa e60969
   png_set_error_fn(read_ptr, &error_parameters, pngtest_error,
fukasawa e60969
      pngtest_warning);
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
fukasawa e60969
   write_ptr =
fukasawa e60969
      png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
fukasawa e60969
      NULL, NULL, NULL, png_debug_malloc, png_debug_free);
fukasawa e60969
#else
fukasawa e60969
   write_ptr =
fukasawa e60969
      png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
fukasawa e60969
#endif
fukasawa e60969
   png_set_error_fn(write_ptr, &error_parameters, pngtest_error,
fukasawa e60969
      pngtest_warning);
fukasawa e60969
#endif
fukasawa e60969
   pngtest_debug("Allocating read_info, write_info and end_info structures");
fukasawa e60969
   read_info_ptr = png_create_info_struct(read_ptr);
fukasawa e60969
   end_info_ptr = png_create_info_struct(read_ptr);
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
   write_info_ptr = png_create_info_struct(write_ptr);
fukasawa e60969
   write_end_info_ptr = png_create_info_struct(write_ptr);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
fukasawa e60969
   init_callback_info(read_info_ptr);
fukasawa e60969
   png_set_read_user_chunk_fn(read_ptr, &user_chunk_data,
fukasawa e60969
     read_user_chunk_callback);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_SETJMP_SUPPORTED
fukasawa e60969
   pngtest_debug("Setting jmpbuf for read struct");
fukasawa e60969
   if (setjmp(png_jmpbuf(read_ptr)))
fukasawa e60969
   {
fukasawa e60969
      fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
fukasawa e60969
      png_free(read_ptr, row_buf);
fukasawa e60969
      row_buf = NULL;
fukasawa e60969
      png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
      png_destroy_info_struct(write_ptr, &write_end_info_ptr);
fukasawa e60969
      png_destroy_write_struct(&write_ptr, &write_info_ptr);
fukasawa e60969
#endif
fukasawa e60969
      FCLOSE(fpin);
fukasawa e60969
      FCLOSE(fpout);
fukasawa e60969
      return (1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
   pngtest_debug("Setting jmpbuf for write struct");
fukasawa e60969
fukasawa e60969
   if (setjmp(png_jmpbuf(write_ptr)))
fukasawa e60969
   {
fukasawa e60969
      fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
fukasawa e60969
      png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
fukasawa e60969
      png_destroy_info_struct(write_ptr, &write_end_info_ptr);
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
      png_destroy_write_struct(&write_ptr, &write_info_ptr);
fukasawa e60969
#endif
fukasawa e60969
      FCLOSE(fpin);
fukasawa e60969
      FCLOSE(fpout);
fukasawa e60969
      return (1);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   if (strict != 0)
fukasawa e60969
   {
fukasawa e60969
      /* Treat png_benign_error() as errors on read */
fukasawa e60969
      png_set_benign_errors(read_ptr, 0);
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
      /* Treat them as errors on write */
fukasawa e60969
      png_set_benign_errors(write_ptr, 0);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
      /* if strict is not set, then app warnings and errors are treated as
fukasawa e60969
       * warnings in release builds, but not in unstable builds; this can be
fukasawa e60969
       * changed with '--relaxed'.
fukasawa e60969
       */
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (relaxed != 0)
fukasawa e60969
   {
fukasawa e60969
      /* Allow application (pngtest) errors and warnings to pass */
fukasawa e60969
      png_set_benign_errors(read_ptr, 1);
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
      png_set_benign_errors(write_ptr, 1);
fukasawa e60969
#endif
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   pngtest_debug("Initializing input and output streams");
fukasawa e60969
#ifdef PNG_STDIO_SUPPORTED
fukasawa e60969
   png_init_io(read_ptr, fpin);
fukasawa e60969
#  ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
   png_init_io(write_ptr, fpout);
fukasawa e60969
#  endif
fukasawa e60969
#else
fukasawa e60969
   png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
fukasawa e60969
#  ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
   png_set_write_fn(write_ptr, (png_voidp)fpout,  pngtest_write_data,
fukasawa e60969
#    ifdef PNG_WRITE_FLUSH_SUPPORTED
fukasawa e60969
      pngtest_flush);
fukasawa e60969
#    else
fukasawa e60969
      NULL);
fukasawa e60969
#    endif
fukasawa e60969
#  endif
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   if (status_dots_requested == 1)
fukasawa e60969
   {
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
      png_set_write_status_fn(write_ptr, write_row_callback);
fukasawa e60969
#endif
fukasawa e60969
      png_set_read_status_fn(read_ptr, read_row_callback);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
      png_set_write_status_fn(write_ptr, NULL);
fukasawa e60969
#endif
fukasawa e60969
      png_set_read_status_fn(read_ptr, NULL);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
fukasawa e60969
   png_set_read_user_transform_fn(read_ptr, read_user_callback);
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
fukasawa e60969
   zero_samples = 0;
fukasawa e60969
   png_set_write_user_transform_fn(write_ptr, count_zero_samples);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
   /* Preserve all the unknown chunks, if possible.  If this is disabled then,
fukasawa e60969
    * even if the png_{get,set}_unknown_chunks stuff is enabled, we can't use
fukasawa e60969
    * libpng to *save* the unknown chunks on read (because we can't switch the
fukasawa e60969
    * save option on!)
fukasawa e60969
    *
fukasawa e60969
    * Notice that if SET_UNKNOWN_CHUNKS is *not* supported read will discard all
fukasawa e60969
    * unknown chunks and write will write them all.
fukasawa e60969
    */
fukasawa e60969
#ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
   png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
fukasawa e60969
      NULL, 0);
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
   png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,
fukasawa e60969
      NULL, 0);
fukasawa e60969
#endif
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   pngtest_debug("Reading info struct");
fukasawa e60969
   png_read_info(read_ptr, read_info_ptr);
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
fukasawa e60969
   /* This is a bit of a hack; there is no obvious way in the callback function
fukasawa e60969
    * to determine that the chunks before the first IDAT have been read, so
fukasawa e60969
    * remove the info_ptr (which is only used to determine position relative to
fukasawa e60969
    * PLTE) here to indicate that we are after the IDAT.
fukasawa e60969
    */
fukasawa e60969
   user_chunk_data.info_ptr = NULL;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   pngtest_debug("Transferring info struct");
fukasawa e60969
   {
fukasawa e60969
      int interlace_type, compression_type, filter_type;
fukasawa e60969
fukasawa e60969
      if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
fukasawa e60969
          &color_type, &interlace_type, &compression_type, &filter_type) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
fukasawa e60969
            color_type, interlace_type, compression_type, filter_type);
fukasawa e60969
         /* num_passes may not be available below if interlace support is not
fukasawa e60969
          * provided by libpng for both read and write.
fukasawa e60969
          */
fukasawa e60969
         switch (interlace_type)
fukasawa e60969
         {
fukasawa e60969
            case PNG_INTERLACE_NONE:
fukasawa e60969
               num_passes = 1;
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case PNG_INTERLACE_ADAM7:
fukasawa e60969
               num_passes = 7;
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            default:
fukasawa e60969
               png_error(read_ptr, "invalid interlace type");
fukasawa e60969
               /*NOT REACHED*/
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         png_error(read_ptr, "png_get_IHDR failed");
fukasawa e60969
   }
fukasawa e60969
#ifdef PNG_FIXED_POINT_SUPPORTED
fukasawa e60969
#ifdef PNG_cHRM_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
fukasawa e60969
         blue_y;
fukasawa e60969
fukasawa e60969
      if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
fukasawa e60969
         &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
fukasawa e60969
            red_y, green_x, green_y, blue_x, blue_y);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_gAMA_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_fixed_point gamma;
fukasawa e60969
fukasawa e60969
      if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma) != 0)
fukasawa e60969
         png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#else /* Use floating point versions */
fukasawa e60969
#ifdef PNG_FLOATING_POINT_SUPPORTED
fukasawa e60969
#ifdef PNG_cHRM_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
fukasawa e60969
         blue_y;
fukasawa e60969
fukasawa e60969
      if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
fukasawa e60969
         &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
fukasawa e60969
            red_y, green_x, green_y, blue_x, blue_y);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_gAMA_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      double gamma;
fukasawa e60969
fukasawa e60969
      if (png_get_gAMA(read_ptr, read_info_ptr, &gamma) != 0)
fukasawa e60969
         png_set_gAMA(write_ptr, write_info_ptr, gamma);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#endif /* Floating point */
fukasawa e60969
#endif /* Fixed point */
fukasawa e60969
#ifdef PNG_iCCP_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_charp name;
fukasawa e60969
      png_bytep profile;
fukasawa e60969
      png_uint_32 proflen;
fukasawa e60969
      int compression_type;
fukasawa e60969
fukasawa e60969
      if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
fukasawa e60969
                      &profile, &proflen) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
fukasawa e60969
                      profile, proflen);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_sRGB_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      int intent;
fukasawa e60969
fukasawa e60969
      if (png_get_sRGB(read_ptr, read_info_ptr, &intent) != 0)
fukasawa e60969
         png_set_sRGB(write_ptr, write_info_ptr, intent);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
   {
fukasawa e60969
      png_colorp palette;
fukasawa e60969
      int num_palette;
fukasawa e60969
fukasawa e60969
      if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette) != 0)
fukasawa e60969
         png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
fukasawa e60969
   }
fukasawa e60969
#ifdef PNG_bKGD_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_color_16p background;
fukasawa e60969
fukasawa e60969
      if (png_get_bKGD(read_ptr, read_info_ptr, &background) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_bKGD(write_ptr, write_info_ptr, background);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_hIST_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_uint_16p hist;
fukasawa e60969
fukasawa e60969
      if (png_get_hIST(read_ptr, read_info_ptr, &hist) != 0)
fukasawa e60969
         png_set_hIST(write_ptr, write_info_ptr, hist);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_oFFs_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_int_32 offset_x, offset_y;
fukasawa e60969
      int unit_type;
fukasawa e60969
fukasawa e60969
      if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
fukasawa e60969
          &unit_type) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_pCAL_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_charp purpose, units;
fukasawa e60969
      png_charpp params;
fukasawa e60969
      png_int_32 X0, X1;
fukasawa e60969
      int type, nparams;
fukasawa e60969
fukasawa e60969
      if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
fukasawa e60969
         &nparams, &units, ¶ms) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
fukasawa e60969
            nparams, units, params);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_pHYs_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_uint_32 res_x, res_y;
fukasawa e60969
      int unit_type;
fukasawa e60969
fukasawa e60969
      if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y,
fukasawa e60969
          &unit_type) != 0)
fukasawa e60969
         png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_sBIT_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_color_8p sig_bit;
fukasawa e60969
fukasawa e60969
      if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit) != 0)
fukasawa e60969
         png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_sCAL_SUPPORTED
fukasawa e60969
#if defined(PNG_FLOATING_POINT_SUPPORTED) && \
fukasawa e60969
   defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)
fukasawa e60969
   {
fukasawa e60969
      int unit;
fukasawa e60969
      double scal_width, scal_height;
fukasawa e60969
fukasawa e60969
      if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
fukasawa e60969
         &scal_height) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#else
fukasawa e60969
#ifdef PNG_FIXED_POINT_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      int unit;
fukasawa e60969
      png_charp scal_width, scal_height;
fukasawa e60969
fukasawa e60969
      if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
fukasawa e60969
          &scal_height) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
fukasawa e60969
             scal_height);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#endif
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_sPLT_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
       png_sPLT_tp entries;
fukasawa e60969
fukasawa e60969
       int num_entries = (int) png_get_sPLT(read_ptr, read_info_ptr, &entries);
fukasawa e60969
       if (num_entries)
fukasawa e60969
       {
fukasawa e60969
           png_set_sPLT(write_ptr, write_info_ptr, entries, num_entries);
fukasawa e60969
       }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_TEXT_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_textp text_ptr;
fukasawa e60969
      int num_text;
fukasawa e60969
fukasawa e60969
      if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
fukasawa e60969
      {
fukasawa e60969
         pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
fukasawa e60969
fukasawa e60969
         pngtest_check_text_support(read_ptr, text_ptr, num_text);
fukasawa e60969
fukasawa e60969
         if (verbose != 0)
fukasawa e60969
         {
fukasawa e60969
            int i;
fukasawa e60969
fukasawa e60969
            printf("\n");
fukasawa e60969
            for (i=0; i
fukasawa e60969
            {
fukasawa e60969
               printf("   Text compression[%d]=%d\n",
fukasawa e60969
                     i, text_ptr[i].compression);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_tIME_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_timep mod_time;
fukasawa e60969
fukasawa e60969
      if (png_get_tIME(read_ptr, read_info_ptr, &mod_time) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_tIME(write_ptr, write_info_ptr, mod_time);
fukasawa e60969
#ifdef PNG_TIME_RFC1123_SUPPORTED
fukasawa e60969
         if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
fukasawa e60969
            tIME_string[(sizeof tIME_string) - 1] = '\0';
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
         {
fukasawa e60969
            strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string));
fukasawa e60969
            tIME_string[(sizeof tIME_string) - 1] = '\0';
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         tIME_chunk_present++;
fukasawa e60969
#endif /* TIME_RFC1123 */
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_tRNS_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_bytep trans_alpha;
fukasawa e60969
      int num_trans;
fukasawa e60969
      png_color_16p trans_color;
fukasawa e60969
fukasawa e60969
      if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
fukasawa e60969
         &trans_color) != 0)
fukasawa e60969
      {
fukasawa e60969
         int sample_max = (1 << bit_depth);
fukasawa e60969
         /* libpng doesn't reject a tRNS chunk with out-of-range samples */
fukasawa e60969
         if (!((color_type == PNG_COLOR_TYPE_GRAY &&
fukasawa e60969
             (int)trans_color->gray > sample_max) ||
fukasawa e60969
             (color_type == PNG_COLOR_TYPE_RGB &&
fukasawa e60969
             ((int)trans_color->red > sample_max ||
fukasawa e60969
             (int)trans_color->green > sample_max ||
fukasawa e60969
             (int)trans_color->blue > sample_max))))
fukasawa e60969
            png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
fukasawa e60969
               trans_color);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_unknown_chunkp unknowns;
fukasawa e60969
      int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
fukasawa e60969
         &unknowns);
fukasawa e60969
fukasawa e60969
      if (num_unknowns != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
fukasawa e60969
           num_unknowns);
fukasawa e60969
#if PNG_LIBPNG_VER < 10600
fukasawa e60969
         /* Copy the locations from the read_info_ptr.  The automatically
fukasawa e60969
          * generated locations in write_end_info_ptr are wrong prior to 1.6.0
fukasawa e60969
          * because they are reset from the write pointer (removed in 1.6.0).
fukasawa e60969
          */
fukasawa e60969
         {
fukasawa e60969
            int i;
fukasawa e60969
            for (i = 0; i < num_unknowns; i++)
fukasawa e60969
              png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
fukasawa e60969
                unknowns[i].location);
fukasawa e60969
         }
fukasawa e60969
#endif
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
   pngtest_debug("Writing info struct");
fukasawa e60969
fukasawa e60969
   /* Write the info in two steps so that if we write the 'unknown' chunks here
fukasawa e60969
    * they go to the correct place.
fukasawa e60969
    */
fukasawa e60969
   png_write_info_before_PLTE(write_ptr, write_info_ptr);
fukasawa e60969
fukasawa e60969
   write_chunks(write_ptr, before_PLTE); /* before PLTE */
fukasawa e60969
fukasawa e60969
   png_write_info(write_ptr, write_info_ptr);
fukasawa e60969
fukasawa e60969
   write_chunks(write_ptr, before_IDAT); /* after PLTE */
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef SINGLE_ROWBUF_ALLOC
fukasawa e60969
   pngtest_debug("Allocating row buffer...");
fukasawa e60969
   row_buf = (png_bytep)png_malloc(read_ptr,
fukasawa e60969
      png_get_rowbytes(read_ptr, read_info_ptr));
fukasawa e60969
fukasawa e60969
   pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
fukasawa e60969
#endif /* SINGLE_ROWBUF_ALLOC */
fukasawa e60969
   pngtest_debug("Writing row data");
fukasawa e60969
fukasawa e60969
#if defined(PNG_READ_INTERLACING_SUPPORTED) &&\
fukasawa e60969
   defined(PNG_WRITE_INTERLACING_SUPPORTED)
fukasawa e60969
   /* Both must be defined for libpng to be able to handle the interlace,
fukasawa e60969
    * otherwise it gets handled below by simply reading and writing the passes
fukasawa e60969
    * directly.
fukasawa e60969
    */
fukasawa e60969
   if (png_set_interlace_handling(read_ptr) != num_passes)
fukasawa e60969
      png_error(write_ptr,
fukasawa e60969
            "png_set_interlace_handling(read): wrong pass count ");
fukasawa e60969
   if (png_set_interlace_handling(write_ptr) != num_passes)
fukasawa e60969
      png_error(write_ptr,
fukasawa e60969
            "png_set_interlace_handling(write): wrong pass count ");
fukasawa e60969
#else /* png_set_interlace_handling not called on either read or write */
fukasawa e60969
#  define calc_pass_height
fukasawa e60969
#endif /* not using libpng interlace handling */
fukasawa e60969
fukasawa e60969
#ifdef PNGTEST_TIMING
fukasawa e60969
   t_stop = (float)clock();
fukasawa e60969
   t_misc += (t_stop - t_start);
fukasawa e60969
   t_start = t_stop;
fukasawa e60969
#endif
fukasawa e60969
   for (pass = 0; pass < num_passes; pass++)
fukasawa e60969
   {
fukasawa e60969
#     ifdef calc_pass_height
fukasawa e60969
         png_uint_32 pass_height;
fukasawa e60969
fukasawa e60969
         if (num_passes == 7) /* interlaced */
fukasawa e60969
         {
fukasawa e60969
            if (PNG_PASS_COLS(width, pass) > 0)
fukasawa e60969
               pass_height = PNG_PASS_ROWS(height, pass);
fukasawa e60969
fukasawa e60969
            else
fukasawa e60969
               pass_height = 0;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else /* not interlaced */
fukasawa e60969
            pass_height = height;
fukasawa e60969
#     else
fukasawa e60969
#        define pass_height height
fukasawa e60969
#     endif
fukasawa e60969
fukasawa e60969
      pngtest_debug1("Writing row data for pass %d", pass);
fukasawa e60969
      for (y = 0; y < pass_height; y++)
fukasawa e60969
      {
fukasawa e60969
#ifndef SINGLE_ROWBUF_ALLOC
fukasawa e60969
         pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
fukasawa e60969
fukasawa e60969
         row_buf = (png_bytep)png_malloc(read_ptr,
fukasawa e60969
            png_get_rowbytes(read_ptr, read_info_ptr));
fukasawa e60969
fukasawa e60969
         pngtest_debug2("\t0x%08lx (%lu bytes)", (unsigned long)row_buf,
fukasawa e60969
            (unsigned long)png_get_rowbytes(read_ptr, read_info_ptr));
fukasawa e60969
fukasawa e60969
#endif /* !SINGLE_ROWBUF_ALLOC */
fukasawa e60969
         png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
#ifdef PNGTEST_TIMING
fukasawa e60969
         t_stop = (float)clock();
fukasawa e60969
         t_decode += (t_stop - t_start);
fukasawa e60969
         t_start = t_stop;
fukasawa e60969
#endif
fukasawa e60969
         png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
fukasawa e60969
#ifdef PNGTEST_TIMING
fukasawa e60969
         t_stop = (float)clock();
fukasawa e60969
         t_encode += (t_stop - t_start);
fukasawa e60969
         t_start = t_stop;
fukasawa e60969
#endif
fukasawa e60969
#endif /* WRITE */
fukasawa e60969
fukasawa e60969
#ifndef SINGLE_ROWBUF_ALLOC
fukasawa e60969
         pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
fukasawa e60969
         png_free(read_ptr, row_buf);
fukasawa e60969
         row_buf = NULL;
fukasawa e60969
#endif /* !SINGLE_ROWBUF_ALLOC */
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
#  ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
      png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
fukasawa e60969
#  endif
fukasawa e60969
#  ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
      png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
fukasawa e60969
#  endif
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   pngtest_debug("Reading and writing end_info data");
fukasawa e60969
fukasawa e60969
   png_read_end(read_ptr, end_info_ptr);
fukasawa e60969
#ifdef PNG_TEXT_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_textp text_ptr;
fukasawa e60969
      int num_text;
fukasawa e60969
fukasawa e60969
      if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
fukasawa e60969
      {
fukasawa e60969
         pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
fukasawa e60969
fukasawa e60969
         pngtest_check_text_support(read_ptr, text_ptr, num_text);
fukasawa e60969
fukasawa e60969
         if (verbose != 0)
fukasawa e60969
         {
fukasawa e60969
            int i;
fukasawa e60969
fukasawa e60969
            printf("\n");
fukasawa e60969
            for (i=0; i
fukasawa e60969
            {
fukasawa e60969
               printf("   Text compression[%d]=%d\n",
fukasawa e60969
                     i, text_ptr[i].compression);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_tIME_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_timep mod_time;
fukasawa e60969
fukasawa e60969
      if (png_get_tIME(read_ptr, end_info_ptr, &mod_time) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
fukasawa e60969
#ifdef PNG_TIME_RFC1123_SUPPORTED
fukasawa e60969
         if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
fukasawa e60969
            tIME_string[(sizeof tIME_string) - 1] = '\0';
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
         {
fukasawa e60969
            strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string);
fukasawa e60969
            tIME_string[(sizeof tIME_string)-1] = '\0';
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         tIME_chunk_present++;
fukasawa e60969
#endif /* TIME_RFC1123 */
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
   {
fukasawa e60969
      png_unknown_chunkp unknowns;
fukasawa e60969
      int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
fukasawa e60969
         &unknowns);
fukasawa e60969
fukasawa e60969
      if (num_unknowns != 0)
fukasawa e60969
      {
fukasawa e60969
         png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
fukasawa e60969
           num_unknowns);
fukasawa e60969
#if PNG_LIBPNG_VER < 10600
fukasawa e60969
         /* Copy the locations from the read_info_ptr.  The automatically
fukasawa e60969
          * generated locations in write_end_info_ptr are wrong prior to 1.6.0
fukasawa e60969
          * because they are reset from the write pointer (removed in 1.6.0).
fukasawa e60969
          */
fukasawa e60969
         {
fukasawa e60969
            int i;
fukasawa e60969
            for (i = 0; i < num_unknowns; i++)
fukasawa e60969
              png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
fukasawa e60969
                unknowns[i].location);
fukasawa e60969
         }
fukasawa e60969
#endif
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
fukasawa e60969
   /* Normally one would use Z_DEFAULT_STRATEGY for text compression.
fukasawa e60969
    * This is here just to make pngtest replicate the results from libpng
fukasawa e60969
    * versions prior to 1.5.4, and to test this new API.
fukasawa e60969
    */
fukasawa e60969
   png_set_text_compression_strategy(write_ptr, Z_FILTERED);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* When the unknown vpAg/sTER chunks are written by pngtest the only way to
fukasawa e60969
    * do it is to write them *before* calling png_write_end.  When unknown
fukasawa e60969
    * chunks are written by libpng, however, they are written just before IEND.
fukasawa e60969
    * There seems to be no way round this, however vpAg/sTER are not expected
fukasawa e60969
    * after IDAT.
fukasawa e60969
    */
fukasawa e60969
   write_chunks(write_ptr, after_IDAT);
fukasawa e60969
fukasawa e60969
   png_write_end(write_ptr, write_end_info_ptr);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_EASY_ACCESS_SUPPORTED
fukasawa e60969
   if (verbose != 0)
fukasawa e60969
   {
fukasawa e60969
      png_uint_32 iwidth, iheight;
fukasawa e60969
      iwidth = png_get_image_width(write_ptr, write_info_ptr);
fukasawa e60969
      iheight = png_get_image_height(write_ptr, write_info_ptr);
fukasawa e60969
      fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
fukasawa e60969
         (unsigned long)iwidth, (unsigned long)iheight);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   pngtest_debug("Destroying data structs");
fukasawa e60969
#ifdef SINGLE_ROWBUF_ALLOC
fukasawa e60969
   pngtest_debug("destroying row_buf for read_ptr");
fukasawa e60969
   png_free(read_ptr, row_buf);
fukasawa e60969
   row_buf = NULL;
fukasawa e60969
#endif /* SINGLE_ROWBUF_ALLOC */
fukasawa e60969
   pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
fukasawa e60969
   png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
   pngtest_debug("destroying write_end_info_ptr");
fukasawa e60969
   png_destroy_info_struct(write_ptr, &write_end_info_ptr);
fukasawa e60969
   pngtest_debug("destroying write_ptr, write_info_ptr");
fukasawa e60969
   png_destroy_write_struct(&write_ptr, &write_info_ptr);
fukasawa e60969
#endif
fukasawa e60969
   pngtest_debug("Destruction complete.");
fukasawa e60969
fukasawa e60969
   FCLOSE(fpin);
fukasawa e60969
   FCLOSE(fpout);
fukasawa e60969
fukasawa e60969
   /* Summarize any warnings or errors and in 'strict' mode fail the test.
fukasawa e60969
    * Unsupported chunks can result in warnings, in that case ignore the strict
fukasawa e60969
    * setting, otherwise fail the test on warnings as well as errors.
fukasawa e60969
    */
fukasawa e60969
   if (error_count > 0)
fukasawa e60969
   {
fukasawa e60969
      /* We don't really expect to get here because of the setjmp handling
fukasawa e60969
       * above, but this is safe.
fukasawa e60969
       */
fukasawa e60969
      fprintf(STDERR, "\n  %s: %d libpng errors found (%d warnings)",
fukasawa e60969
         inname, error_count, warning_count);
fukasawa e60969
fukasawa e60969
      if (strict != 0)
fukasawa e60969
         return (1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#  ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
      /* If there is no write support nothing was written! */
fukasawa e60969
      else if (unsupported_chunks > 0)
fukasawa e60969
      {
fukasawa e60969
         fprintf(STDERR, "\n  %s: unsupported chunks (%d)%s",
fukasawa e60969
            inname, unsupported_chunks, strict ? ": IGNORED --strict!" : "");
fukasawa e60969
      }
fukasawa e60969
#  endif
fukasawa e60969
fukasawa e60969
   else if (warning_count > 0)
fukasawa e60969
   {
fukasawa e60969
      fprintf(STDERR, "\n  %s: %d libpng warnings found",
fukasawa e60969
         inname, warning_count);
fukasawa e60969
fukasawa e60969
      if (strict != 0)
fukasawa e60969
         return (1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   pngtest_debug("Opening files for comparison");
fukasawa e60969
   if ((fpin = fopen(inname, "rb")) == NULL)
fukasawa e60969
   {
fukasawa e60969
      fprintf(STDERR, "Could not find file %s\n", inname);
fukasawa e60969
      return (1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if ((fpout = fopen(outname, "rb")) == NULL)
fukasawa e60969
   {
fukasawa e60969
      fprintf(STDERR, "Could not find file %s\n", outname);
fukasawa e60969
      FCLOSE(fpin);
fukasawa e60969
      return (1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#if defined (PNG_WRITE_SUPPORTED) /* else nothing was written */ &&\
fukasawa e60969
    defined (PNG_WRITE_FILTER_SUPPORTED)
fukasawa e60969
   if (interlace_preserved != 0) /* else the files will be changed */
fukasawa e60969
   {
fukasawa e60969
      for (;;)
fukasawa e60969
      {
fukasawa e60969
         static int wrote_question = 0;
fukasawa e60969
         png_size_t num_in, num_out;
fukasawa e60969
         char inbuf[256], outbuf[256];
fukasawa e60969
fukasawa e60969
         num_in = fread(inbuf, 1, sizeof inbuf, fpin);
fukasawa e60969
         num_out = fread(outbuf, 1, sizeof outbuf, fpout);
fukasawa e60969
fukasawa e60969
         if (num_in != num_out)
fukasawa e60969
         {
fukasawa e60969
            fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
fukasawa e60969
                    inname, outname);
fukasawa e60969
fukasawa e60969
            if (wrote_question == 0 && unsupported_chunks == 0)
fukasawa e60969
            {
fukasawa e60969
               fprintf(STDERR,
fukasawa e60969
         "   Was %s written with the same maximum IDAT chunk size (%d bytes),",
fukasawa e60969
                 inname, PNG_ZBUF_SIZE);
fukasawa e60969
               fprintf(STDERR,
fukasawa e60969
                 "\n   filtering heuristic (libpng default), compression");
fukasawa e60969
               fprintf(STDERR,
fukasawa e60969
                 " level (zlib default),\n   and zlib version (%s)?\n\n",
fukasawa e60969
                 ZLIB_VERSION);
fukasawa e60969
               wrote_question = 1;
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            FCLOSE(fpin);
fukasawa e60969
            FCLOSE(fpout);
fukasawa e60969
fukasawa e60969
            if (strict != 0 && unsupported_chunks == 0)
fukasawa e60969
              return (1);
fukasawa e60969
fukasawa e60969
            else
fukasawa e60969
              return (0);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         if (num_in == 0)
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         if (memcmp(inbuf, outbuf, num_in))
fukasawa e60969
         {
fukasawa e60969
            fprintf(STDERR, "\nFiles %s and %s are different\n", inname,
fukasawa e60969
               outname);
fukasawa e60969
fukasawa e60969
            if (wrote_question == 0 && unsupported_chunks == 0)
fukasawa e60969
            {
fukasawa e60969
               fprintf(STDERR,
fukasawa e60969
         "   Was %s written with the same maximum IDAT chunk size (%d bytes),",
fukasawa e60969
                    inname, PNG_ZBUF_SIZE);
fukasawa e60969
               fprintf(STDERR,
fukasawa e60969
                 "\n   filtering heuristic (libpng default), compression");
fukasawa e60969
               fprintf(STDERR,
fukasawa e60969
                 " level (zlib default),\n   and zlib version (%s)?\n\n",
fukasawa e60969
                 ZLIB_VERSION);
fukasawa e60969
               wrote_question = 1;
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            FCLOSE(fpin);
fukasawa e60969
            FCLOSE(fpout);
fukasawa e60969
fukasawa e60969
            /* NOTE: the unsupported_chunks escape is permitted here because
fukasawa e60969
             * unsupported text chunk compression will result in the compression
fukasawa e60969
             * mode being changed (to NONE) yet, in the test case, the result
fukasawa e60969
             * can be exactly the same size!
fukasawa e60969
             */
fukasawa e60969
            if (strict != 0 && unsupported_chunks == 0)
fukasawa e60969
              return (1);
fukasawa e60969
fukasawa e60969
            else
fukasawa e60969
              return (0);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif /* WRITE && WRITE_FILTER */
fukasawa e60969
fukasawa e60969
   FCLOSE(fpin);
fukasawa e60969
   FCLOSE(fpout);
fukasawa e60969
fukasawa e60969
   return (0);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Input and output filenames */
fukasawa e60969
#ifdef RISCOS
fukasawa e60969
static PNG_CONST char *inname = "pngtest/png";
fukasawa e60969
static PNG_CONST char *outname = "pngout/png";
fukasawa e60969
#else
fukasawa e60969
static PNG_CONST char *inname = "pngtest.png";
fukasawa e60969
static PNG_CONST char *outname = "pngout.png";
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
int
fukasawa e60969
main(int argc, char *argv[])
fukasawa e60969
{
fukasawa e60969
   int multiple = 0;
fukasawa e60969
   int ierror = 0;
fukasawa e60969
fukasawa e60969
   png_structp dummy_ptr;
fukasawa e60969
fukasawa e60969
   fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
fukasawa e60969
   fprintf(STDERR, "   with zlib   version %s\n", ZLIB_VERSION);
fukasawa e60969
   fprintf(STDERR, "%s", png_get_copyright(NULL));
fukasawa e60969
   /* Show the version of libpng used in building the library */
fukasawa e60969
   fprintf(STDERR, " library (%lu):%s",
fukasawa e60969
      (unsigned long)png_access_version_number(),
fukasawa e60969
      png_get_header_version(NULL));
fukasawa e60969
fukasawa e60969
   /* Show the version of libpng used in building the application */
fukasawa e60969
   fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
fukasawa e60969
      PNG_HEADER_VERSION_STRING);
fukasawa e60969
fukasawa e60969
   /* Do some consistency checking on the memory allocation settings, I'm
fukasawa e60969
    * not sure this matters, but it is nice to know, the first of these
fukasawa e60969
    * tests should be impossible because of the way the macros are set
fukasawa e60969
    * in pngconf.h
fukasawa e60969
    */
fukasawa e60969
#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
fukasawa e60969
      fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
fukasawa e60969
#endif
fukasawa e60969
   /* I think the following can happen. */
fukasawa e60969
#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
fukasawa e60969
      fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
fukasawa e60969
   {
fukasawa e60969
      fprintf(STDERR,
fukasawa e60969
         "Warning: versions are different between png.h and png.c\n");
fukasawa e60969
      fprintf(STDERR, "  png.h version: %s\n", PNG_LIBPNG_VER_STRING);
fukasawa e60969
      fprintf(STDERR, "  png.c version: %s\n\n", png_libpng_ver);
fukasawa e60969
      ++ierror;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (argc > 1)
fukasawa e60969
   {
fukasawa e60969
      if (strcmp(argv[1], "-m") == 0)
fukasawa e60969
      {
fukasawa e60969
         multiple = 1;
fukasawa e60969
         status_dots_requested = 0;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (strcmp(argv[1], "-mv") == 0 ||
fukasawa e60969
               strcmp(argv[1], "-vm") == 0 )
fukasawa e60969
      {
fukasawa e60969
         multiple = 1;
fukasawa e60969
         verbose = 1;
fukasawa e60969
         status_dots_requested = 1;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (strcmp(argv[1], "-v") == 0)
fukasawa e60969
      {
fukasawa e60969
         verbose = 1;
fukasawa e60969
         status_dots_requested = 1;
fukasawa e60969
         inname = argv[2];
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (strcmp(argv[1], "--strict") == 0)
fukasawa e60969
      {
fukasawa e60969
         status_dots_requested = 0;
fukasawa e60969
         verbose = 1;
fukasawa e60969
         inname = argv[2];
fukasawa e60969
         strict++;
fukasawa e60969
         relaxed = 0;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (strcmp(argv[1], "--relaxed") == 0)
fukasawa e60969
      {
fukasawa e60969
         status_dots_requested = 0;
fukasawa e60969
         verbose = 1;
fukasawa e60969
         inname = argv[2];
fukasawa e60969
         strict = 0;
fukasawa e60969
         relaxed++;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         inname = argv[1];
fukasawa e60969
         status_dots_requested = 0;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (multiple == 0 && argc == 3 + verbose)
fukasawa e60969
     outname = argv[2 + verbose];
fukasawa e60969
fukasawa e60969
   if ((multiple == 0 && argc > 3 + verbose) ||
fukasawa e60969
       (multiple != 0 && argc < 2))
fukasawa e60969
   {
fukasawa e60969
     fprintf(STDERR,
fukasawa e60969
       "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
fukasawa e60969
        argv[0], argv[0]);
fukasawa e60969
     fprintf(STDERR,
fukasawa e60969
       "  reads/writes one PNG file (without -m) or multiple files (-m)\n");
fukasawa e60969
     fprintf(STDERR,
fukasawa e60969
       "  with -m %s is used as a temporary file\n", outname);
fukasawa e60969
     exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (multiple != 0)
fukasawa e60969
   {
fukasawa e60969
      int i;
fukasawa e60969
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
fukasawa e60969
      int allocation_now = current_allocation;
fukasawa e60969
#endif
fukasawa e60969
      for (i=2; i
fukasawa e60969
      {
fukasawa e60969
         int kerror;
fukasawa e60969
         fprintf(STDERR, "\n Testing %s:", argv[i]);
fukasawa e60969
#if PNG_DEBUG > 0
fukasawa e60969
         fprintf(STDERR, "\n");
fukasawa e60969
#endif
fukasawa e60969
         kerror = test_one_file(argv[i], outname);
fukasawa e60969
         if (kerror == 0)
fukasawa e60969
         {
fukasawa e60969
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
fukasawa e60969
            fprintf(STDERR, "\n PASS (%lu zero samples)\n",
fukasawa e60969
               (unsigned long)zero_samples);
fukasawa e60969
#else
fukasawa e60969
            fprintf(STDERR, " PASS\n");
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_TIME_RFC1123_SUPPORTED
fukasawa e60969
            if (tIME_chunk_present != 0)
fukasawa e60969
               fprintf(STDERR, " tIME = %s\n", tIME_string);
fukasawa e60969
fukasawa e60969
            tIME_chunk_present = 0;
fukasawa e60969
#endif /* TIME_RFC1123 */
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
         {
fukasawa e60969
            fprintf(STDERR, " FAIL\n");
fukasawa e60969
            ierror += kerror;
fukasawa e60969
         }
fukasawa e60969
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
fukasawa e60969
         if (allocation_now != current_allocation)
fukasawa e60969
            fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
fukasawa e60969
               current_allocation - allocation_now);
fukasawa e60969
fukasawa e60969
         if (current_allocation != 0)
fukasawa e60969
         {
fukasawa e60969
            memory_infop pinfo = pinformation;
fukasawa e60969
fukasawa e60969
            fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
fukasawa e60969
               current_allocation);
fukasawa e60969
fukasawa e60969
            while (pinfo != NULL)
fukasawa e60969
            {
fukasawa e60969
               fprintf(STDERR, " %lu bytes at %p\n",
fukasawa e60969
                 (unsigned long)pinfo->size,
fukasawa e60969
                 pinfo->pointer);
fukasawa e60969
               pinfo = pinfo->next;
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
#endif
fukasawa e60969
      }
fukasawa e60969
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
fukasawa e60969
         fprintf(STDERR, " Current memory allocation: %10d bytes\n",
fukasawa e60969
            current_allocation);
fukasawa e60969
         fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
fukasawa e60969
            maximum_allocation);
fukasawa e60969
         fprintf(STDERR, " Total   memory allocation: %10d bytes\n",
fukasawa e60969
            total_allocation);
fukasawa e60969
         fprintf(STDERR, "     Number of allocations: %10d\n",
fukasawa e60969
            num_allocations);
fukasawa e60969
#endif
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      int i;
fukasawa e60969
      for (i = 0; i<3; ++i)
fukasawa e60969
      {
fukasawa e60969
         int kerror;
fukasawa e60969
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
fukasawa e60969
         int allocation_now = current_allocation;
fukasawa e60969
#endif
fukasawa e60969
         if (i == 1)
fukasawa e60969
            status_dots_requested = 1;
fukasawa e60969
fukasawa e60969
         else if (verbose == 0)
fukasawa e60969
            status_dots_requested = 0;
fukasawa e60969
fukasawa e60969
         if (i == 0 || verbose == 1 || ierror != 0)
fukasawa e60969
         {
fukasawa e60969
            fprintf(STDERR, "\n Testing %s:", inname);
fukasawa e60969
#if PNG_DEBUG > 0
fukasawa e60969
            fprintf(STDERR, "\n");
fukasawa e60969
#endif
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         kerror = test_one_file(inname, outname);
fukasawa e60969
fukasawa e60969
         if (kerror == 0)
fukasawa e60969
         {
fukasawa e60969
            if (verbose == 1 || i == 2)
fukasawa e60969
            {
fukasawa e60969
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
fukasawa e60969
                fprintf(STDERR, "\n PASS (%lu zero samples)\n",
fukasawa e60969
                   (unsigned long)zero_samples);
fukasawa e60969
#else
fukasawa e60969
                fprintf(STDERR, " PASS\n");
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_TIME_RFC1123_SUPPORTED
fukasawa e60969
             if (tIME_chunk_present != 0)
fukasawa e60969
                fprintf(STDERR, " tIME = %s\n", tIME_string);
fukasawa e60969
#endif /* TIME_RFC1123 */
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
         {
fukasawa e60969
            if (verbose == 0 && i != 2)
fukasawa e60969
            {
fukasawa e60969
               fprintf(STDERR, "\n Testing %s:", inname);
fukasawa e60969
#if PNG_DEBUG > 0
fukasawa e60969
               fprintf(STDERR, "\n");
fukasawa e60969
#endif
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            fprintf(STDERR, " FAIL\n");
fukasawa e60969
            ierror += kerror;
fukasawa e60969
         }
fukasawa e60969
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
fukasawa e60969
         if (allocation_now != current_allocation)
fukasawa e60969
             fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
fukasawa e60969
               current_allocation - allocation_now);
fukasawa e60969
fukasawa e60969
         if (current_allocation != 0)
fukasawa e60969
         {
fukasawa e60969
             memory_infop pinfo = pinformation;
fukasawa e60969
fukasawa e60969
             fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
fukasawa e60969
                current_allocation);
fukasawa e60969
fukasawa e60969
             while (pinfo != NULL)
fukasawa e60969
             {
fukasawa e60969
                fprintf(STDERR, " %lu bytes at %p\n",
fukasawa e60969
                   (unsigned long)pinfo->size, pinfo->pointer);
fukasawa e60969
                pinfo = pinfo->next;
fukasawa e60969
             }
fukasawa e60969
          }
fukasawa e60969
#endif
fukasawa e60969
       }
fukasawa e60969
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
fukasawa e60969
       fprintf(STDERR, " Current memory allocation: %10d bytes\n",
fukasawa e60969
          current_allocation);
fukasawa e60969
       fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
fukasawa e60969
          maximum_allocation);
fukasawa e60969
       fprintf(STDERR, " Total   memory allocation: %10d bytes\n",
fukasawa e60969
          total_allocation);
fukasawa e60969
       fprintf(STDERR, "     Number of allocations: %10d\n",
fukasawa e60969
            num_allocations);
fukasawa e60969
#endif
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#ifdef PNGTEST_TIMING
fukasawa e60969
   t_stop = (float)clock();
fukasawa e60969
   t_misc += (t_stop - t_start);
fukasawa e60969
   t_start = t_stop;
fukasawa e60969
   fprintf(STDERR, " CPU time used = %.3f seconds",
fukasawa e60969
      (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
fukasawa e60969
   fprintf(STDERR, " (decoding %.3f,\n",
fukasawa e60969
      t_decode/(float)CLOCKS_PER_SEC);
fukasawa e60969
   fprintf(STDERR, "        encoding %.3f ,",
fukasawa e60969
      t_encode/(float)CLOCKS_PER_SEC);
fukasawa e60969
   fprintf(STDERR, " other %.3f seconds)\n\n",
fukasawa e60969
      t_misc/(float)CLOCKS_PER_SEC);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   if (ierror == 0)
fukasawa e60969
      fprintf(STDERR, " libpng passes test\n");
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      fprintf(STDERR, " libpng FAILS test\n");
fukasawa e60969
fukasawa e60969
   dummy_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
fukasawa e60969
   fprintf(STDERR, " Default limits:\n");
fukasawa e60969
   fprintf(STDERR, "  width_max  = %lu\n",
fukasawa e60969
      (unsigned long) png_get_user_width_max(dummy_ptr));
fukasawa e60969
   fprintf(STDERR, "  height_max = %lu\n",
fukasawa e60969
      (unsigned long) png_get_user_height_max(dummy_ptr));
fukasawa e60969
   if (png_get_chunk_cache_max(dummy_ptr) == 0)
fukasawa e60969
      fprintf(STDERR, "  cache_max  = unlimited\n");
fukasawa e60969
   else
fukasawa e60969
      fprintf(STDERR, "  cache_max  = %lu\n",
fukasawa e60969
         (unsigned long) png_get_chunk_cache_max(dummy_ptr));
fukasawa e60969
   if (png_get_chunk_malloc_max(dummy_ptr) == 0)
fukasawa e60969
      fprintf(STDERR, "  malloc_max = unlimited\n");
fukasawa e60969
   else
fukasawa e60969
      fprintf(STDERR, "  malloc_max = %lu\n",
fukasawa e60969
         (unsigned long) png_get_chunk_malloc_max(dummy_ptr));
fukasawa e60969
   png_destroy_read_struct(&dummy_ptr, NULL, NULL);
fukasawa e60969
fukasawa e60969
   return (int)(ierror != 0);
fukasawa e60969
}
fukasawa e60969
#else
fukasawa e60969
int
fukasawa e60969
main(void)
fukasawa e60969
{
fukasawa e60969
   fprintf(STDERR,
fukasawa e60969
      " test ignored because libpng was not built with read support\n");
fukasawa e60969
   /* And skip this test */
fukasawa e60969
   return PNG_LIBPNG_VER < 10600 ? 0 : 77;
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Generate a compiler error if there is an old png.h in the search path. */
fukasawa e60969
typedef png_libpng_version_1_6_21 Your_png_h_is_not_version_1_6_21;