pngtest.c 50.4 KB
Newer Older
A
Andreas Dilger 已提交
1

G
Guy Schalnat 已提交
2
/* pngtest.c - a simple test program to test libpng
3
 *
4
 * Last changed in libpng 1.5.3 [(PENDING RELEASE)]
5
 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
6 7
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8
 *
9
 * This code is released under the libpng license.
10
 * For conditions of distribution and use, see the disclaimer
11
 * and license in png.h
12
 *
13 14 15 16 17 18
 * This program reads in a PNG image, writes it out again, and then
 * compares the two files.  If the files are identical, this shows that
 * the basic chunk handling, filtering, and (de)compression code is working
 * properly.  It does not currently test all of the transforms, although
 * it probably should.
 *
19
 * The program will report "FAIL" in certain legitimate cases:
20
 * 1) when the compression level or filter selection method is changed.
21
 * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
22 23
 * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
 *    exist in the input file.
24 25
 * 4) others not listed here...
 * In these cases, it is best to check with another tool such as "pngcheck"
26
 * to see what the differences between the two files are.
27 28 29
 *
 * If a filename is given on the command-line, then this file is used
 * for the input, rather than the default "pngtest.png".  This allows
30 31
 * testing a wide variety of files easily.  You can also test a number
 * of files at once by typing "pngtest -m file1.png file2.png ..."
32
 */
G
Guy Schalnat 已提交
33

34 35
#define _POSIX_SOURCE 1

G
[devel]  
Glenn Randers-Pehrson 已提交
36
#include "zlib.h"
37
#include "png.h"
38 39 40 41
/* Copied from pngpriv.h but only used in error messages below. */
#ifndef PNG_ZBUF_SIZE
#  define PNG_ZBUF_SIZE 8192
#endif
42 43
#  include <stdio.h>
#  include <stdlib.h>
44
#  include <string.h>
45
#  define FCLOSE(file) fclose(file)
46

47
#ifndef PNG_STDIO_SUPPORTED
48
typedef FILE                * png_FILE_p;
49 50
#endif

51
/* Makes pngtest verbose so we can find problems. */
A
Andreas Dilger 已提交
52
#ifndef PNG_DEBUG
53 54 55
#  define PNG_DEBUG 0
#endif

56 57 58 59 60 61 62 63 64
#if PNG_DEBUG > 1
#  define pngtest_debug(m)        ((void)fprintf(stderr, m "\n"))
#  define pngtest_debug1(m,p1)    ((void)fprintf(stderr, m "\n", p1))
#  define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))
#else
#  define pngtest_debug(m)        ((void)0)
#  define pngtest_debug1(m,p1)    ((void)0)
#  define pngtest_debug2(m,p1,p2) ((void)0)
#endif
65

66
#if !PNG_DEBUG
67
#  define SINGLE_ROWBUF_ALLOC  /* Makes buffer overruns easier to nail */
68
#endif
A
Andreas Dilger 已提交
69

G
[devel]  
Glenn Randers-Pehrson 已提交
70 71 72 73 74 75 76 77 78 79 80
/* The code uses memcmp and memcpy on large objects (typically row pointers) so
 * it is necessary to do soemthing special on certain architectures, note that
 * the actual support for this was effectively removed in 1.4, so only the
 * memory remains in this program:
 */
#define CVT_PTR(ptr)         (ptr)
#define CVT_PTR_NOCHECK(ptr) (ptr)
#define png_memcmp  memcmp
#define png_memcpy  memcpy
#define png_memset  memset

81 82 83 84
/* Turn on CPU timing
#define PNGTEST_TIMING
*/

85
#ifndef PNG_FLOATING_POINT_SUPPORTED
86 87 88
#undef PNGTEST_TIMING
#endif

89 90 91 92 93
#ifdef PNGTEST_TIMING
static float t_start, t_stop, t_decode, t_encode, t_misc;
#include <time.h>
#endif

94
#ifdef PNG_TIME_RFC1123_SUPPORTED
95
#define PNG_tIME_STRING_LENGTH 29
96
static int tIME_chunk_present = 0;
97
static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
98 99
#endif

100 101
static int verbose = 0;

102
int test_one_file PNGARG((PNG_CONST char *inname, PNG_CONST char *outname));
103

G
Guy Schalnat 已提交
104 105 106 107
#ifdef __TURBOC__
#include <mem.h>
#endif

108
/* Defined so I can write to a file on gui/windowing platforms */
G
Guy Schalnat 已提交
109
/*  #define STDERR stderr  */
110
#define STDERR stdout   /* For DOS */
G
Guy Schalnat 已提交
111

112 113 114 115 116
/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
#ifndef png_jmpbuf
#  define png_jmpbuf(png_ptr) png_ptr->jmpbuf
#endif

117
/* Example of using row callbacks to make a simple progress meter */
118 119 120
static int status_pass = 1;
static int status_dots_requested = 0;
static int status_dots = 1;
121

122
void PNGCBAPI
123
read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
124
void PNGCBAPI
125
read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
126
{
127 128
   if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
      return;
129

130 131 132 133 134 135
   if (status_pass != pass)
   {
      fprintf(stdout, "\n Pass %d: ", pass);
      status_pass = pass;
      status_dots = 31;
   }
136

137
   status_dots--;
138

139 140 141 142 143
   if (status_dots == 0)
   {
      fprintf(stdout, "\n         ");
      status_dots=30;
   }
144

145
   fprintf(stdout, "r");
146
}
147

148
void PNGCBAPI
149
write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
150
void PNGCBAPI
151
write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
152
{
153 154
   if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
      return;
155

156
   fprintf(stdout, "w");
157 158 159
}


160
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
161
/* Example of using user transform callback (we don't transform anything,
162 163 164
 * but merely examine the row filters.  We set this to 256 rather than
 * 5 in case illegal filter values are present.)
 */
165
static png_uint_32 filters_used[256];
166
void PNGCBAPI
167
count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data);
168
void PNGCBAPI
169 170
count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
{
171
   if (png_ptr != NULL && row_info != NULL)
172
      ++filters_used[*(data - 1)];
173 174 175
}
#endif

176
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
177
/* Example of using user transform callback (we don't transform anything,
178 179
 * but merely count the zero samples)
 */
180

181
static png_uint_32 zero_samples;
182

183
void PNGCBAPI
184
count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data);
185
void PNGCBAPI
186
count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
187 188
{
   png_bytep dp = data;
189 190
   if (png_ptr == NULL)
      return;
191

192
   /* Contents of row_info:
193 194 195 196 197 198 199 200
    *  png_uint_32 width      width of row
    *  png_uint_32 rowbytes   number of bytes in row
    *  png_byte color_type    color type of pixels
    *  png_byte bit_depth     bit depth of samples
    *  png_byte channels      number of channels (1-4)
    *  png_byte pixel_depth   bits per pixel (depth*channels)
    */

201
    /* Counts the number of zero samples (or zero pixels if color_type is 3 */
202

203
    if (row_info->color_type == 0 || row_info->color_type == 3)
204
    {
205
       int pos = 0;
206
       png_uint_32 n, nstop;
207

208
       for (n = 0, nstop=row_info->width; n<nstop; n++)
209
       {
210
          if (row_info->bit_depth == 1)
211
          {
212 213
             if (((*dp << pos++ ) & 0x80) == 0)
                zero_samples++;
214

215
             if (pos == 8)
216
             {
217
                pos = 0;
218 219
                dp++;
             }
220
          }
221

222
          if (row_info->bit_depth == 2)
223
          {
224 225
             if (((*dp << (pos+=2)) & 0xc0) == 0)
                zero_samples++;
226

227
             if (pos == 8)
228
             {
229
                pos = 0;
230 231
                dp++;
             }
232
          }
233

234
          if (row_info->bit_depth == 4)
235
          {
236 237
             if (((*dp << (pos+=4)) & 0xf0) == 0)
                zero_samples++;
238

239
             if (pos == 8)
240
             {
241
                pos = 0;
242 243
                dp++;
             }
244
          }
245

246
          if (row_info->bit_depth == 8)
247 248
             if (*dp++ == 0)
                zero_samples++;
249

250
          if (row_info->bit_depth == 16)
251
          {
252 253
             if ((*dp | *(dp+1)) == 0)
                zero_samples++;
254 255 256 257
             dp+=2;
          }
       }
    }
258
    else /* Other color types */
259
    {
260
       png_uint_32 n, nstop;
261 262
       int channel;
       int color_channels = row_info->channels;
263
       if (row_info->color_type > 3)color_channels--;
264

265
       for (n = 0, nstop=row_info->width; n<nstop; n++)
266 267 268
       {
          for (channel = 0; channel < color_channels; channel++)
          {
269
             if (row_info->bit_depth == 8)
270 271
                if (*dp++ == 0)
                   zero_samples++;
272

273
             if (row_info->bit_depth == 16)
274
             {
275 276
                if ((*dp | *(dp+1)) == 0)
                   zero_samples++;
277

278 279 280
                dp+=2;
             }
          }
281
          if (row_info->color_type > 3)
282 283
          {
             dp++;
284 285
             if (row_info->bit_depth == 16)
                dp++;
286 287 288 289
          }
       }
    }
}
290
#endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
291

292
static int wrote_question = 0;
293

294
#ifndef PNG_STDIO_SUPPORTED
295
/* START of code to validate stdio-free compilation */
296 297 298 299 300 301 302
/* These copies of the default read/write functions come from pngrio.c and
 * pngwio.c.  They allow "don't include stdio" testing of the library.
 * This is the function that does the actual reading of data.  If you are
 * not reading from a standard C stream, you should create a replacement
 * read_data function and use it at run time with png_set_read_fn(), rather
 * than changing the library.
 */
303

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
#ifdef PNG_IO_STATE_SUPPORTED
void
pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
   png_uint_32 io_op);
void
pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
   png_uint_32 io_op)
{
   png_uint_32 io_state = png_get_io_state(png_ptr);
   int err = 0;

   /* Check if the current operation (reading / writing) is as expected. */
   if ((io_state & PNG_IO_MASK_OP) != io_op)
      png_error(png_ptr, "Incorrect operation in I/O state");

   /* Check if the buffer size specific to the current location
    * (file signature / header / data / crc) is as expected.
    */
   switch (io_state & PNG_IO_MASK_LOC)
   {
   case PNG_IO_SIGNATURE:
      if (data_length > 8)
         err = 1;
      break;
   case PNG_IO_CHUNK_HDR:
      if (data_length != 8)
         err = 1;
      break;
   case PNG_IO_CHUNK_DATA:
      break;  /* no restrictions here */
   case PNG_IO_CHUNK_CRC:
      if (data_length != 4)
         err = 1;
      break;
   default:
      err = 1;  /* uninitialized */
   }
   if (err)
      png_error(png_ptr, "Bad I/O state or buffer size");
}
#endif

346
#ifndef USE_FAR_KEYWORD
347
static void PNGCBAPI
348
pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
349
{
350 351
   png_size_t check = 0;
   png_voidp io_ptr;
352 353 354 355

   /* fread() returns 0 on error, so it is OK to store this in a png_size_t
    * instead of an int, which is what fread() actually returns.
    */
356 357 358 359 360
   io_ptr = png_get_io_ptr(png_ptr);
   if (io_ptr != NULL)
   {
      check = fread(data, 1, length, (png_FILE_p)io_ptr);
   }
361

362 363
   if (check != length)
   {
364
      png_error(png_ptr, "Read Error");
365
   }
366 367 368 369

#ifdef PNG_IO_STATE_SUPPORTED
   pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
#endif
370
}
G
Guy Schalnat 已提交
371
#else
372
/* This is the model-independent version. Since the standard I/O library
373 374 375
   can't handle far buffers in the medium and small models, we have to copy
   the data.
*/
376

377 378
#define NEAR_BUF_SIZE 1024
#define MIN(a,b) (a <= b ? a : b)
379

380
static void PNGCBAPI
381
pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
382
{
383
   png_size_t check;
384
   png_byte *n_data;
385
   png_FILE_p io_ptr;
386 387 388

   /* Check if data really is near. If so, use usual code. */
   n_data = (png_byte *)CVT_PTR_NOCHECK(data);
389
   io_ptr = (png_FILE_p)CVT_PTR(png_get_io_ptr(png_ptr));
390 391
   if ((png_bytep)n_data == data)
   {
392
      check = fread(n_data, 1, length, io_ptr);
393 394 395 396 397 398 399
   }
   else
   {
      png_byte buf[NEAR_BUF_SIZE];
      png_size_t read, remaining, err;
      check = 0;
      remaining = length;
400

401 402 403
      do
      {
         read = MIN(NEAR_BUF_SIZE, remaining);
404
         err = fread(buf, 1, 1, io_ptr);
405
         png_memcpy(data, buf, read); /* Copy far buffer to near buffer */
406
         if (err != read)
407 408 409 410 411 412 413 414
            break;
         else
            check += err;
         data += read;
         remaining -= read;
      }
      while (remaining != 0);
   }
415

416
   if (check != length)
417 418 419 420 421
      png_error(png_ptr, "Read Error");

#ifdef PNG_IO_STATE_SUPPORTED
   pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
#endif
422
}
423
#endif /* USE_FAR_KEYWORD */
G
Guy Schalnat 已提交
424

425
#ifdef PNG_WRITE_FLUSH_SUPPORTED
426
static void PNGCBAPI
427
pngtest_flush(png_structp png_ptr)
428
{
429
   /* Do nothing; fflush() is said to be just a waste of energy. */
430
   PNG_UNUSED(png_ptr)   /* Stifle compiler warning */
431 432
}
#endif
G
Guy Schalnat 已提交
433

434
/* This is the function that does the actual writing of data.  If you are
435 436 437 438
 * not writing to a standard C stream, you should create a replacement
 * write_data function and use it at run time with png_set_write_fn(), rather
 * than changing the library.
 */
439
#ifndef USE_FAR_KEYWORD
440
static void PNGCBAPI
441
pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
442
{
443
   png_size_t check;
444

445
   check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
446

447 448 449 450
   if (check != length)
   {
      png_error(png_ptr, "Write Error");
   }
451 452 453 454

#ifdef PNG_IO_STATE_SUPPORTED
   pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
#endif
455 456
}
#else
457
/* This is the model-independent version. Since the standard I/O library
458 459 460 461 462 463 464
   can't handle far buffers in the medium and small models, we have to copy
   the data.
*/

#define NEAR_BUF_SIZE 1024
#define MIN(a,b) (a <= b ? a : b)

465
static void PNGCBAPI
466
pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
467
{
468
   png_size_t check;
469
   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
470
   png_FILE_p io_ptr;
471 472 473

   /* Check if data really is near. If so, use usual code. */
   near_data = (png_byte *)CVT_PTR_NOCHECK(data);
474
   io_ptr = (png_FILE_p)CVT_PTR(png_get_io_ptr(png_ptr));
475

476 477
   if ((png_bytep)near_data == data)
   {
478
      check = fwrite(near_data, 1, length, io_ptr);
479
   }
480

481 482 483 484 485 486
   else
   {
      png_byte buf[NEAR_BUF_SIZE];
      png_size_t written, remaining, err;
      check = 0;
      remaining = length;
487

488 489 490
      do
      {
         written = MIN(NEAR_BUF_SIZE, remaining);
491
         png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
492
         err = fwrite(buf, 1, written, io_ptr);
493 494 495 496 497 498 499 500 501
         if (err != written)
            break;
         else
            check += err;
         data += written;
         remaining -= written;
      }
      while (remaining != 0);
   }
502

503 504 505 506
   if (check != length)
   {
      png_error(png_ptr, "Write Error");
   }
507 508 509 510

#ifdef PNG_IO_STATE_SUPPORTED
   pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
#endif
511
}
512
#endif /* USE_FAR_KEYWORD */
513 514 515 516 517 518

/* This function is called when there is a warning, but the library thinks
 * it can continue anyway.  Replacement functions don't have to do anything
 * here if you don't want to.  In the default configuration, png_ptr is
 * not used, but it is passed in case it may be useful.
 */
519
static void PNGCBAPI
520
pngtest_warning(png_structp png_ptr, png_const_charp message)
521 522
{
   PNG_CONST char *name = "UNKNOWN (ERROR!)";
523 524
   char *test;
   test = png_get_error_ptr(png_ptr);
525

526 527
   if (test == NULL)
     fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
528

529 530
   else
     fprintf(STDERR, "%s: libpng warning: %s\n", test, message);
531 532 533 534 535 536 537
}

/* This is the default error handling function.  Note that replacements for
 * this function MUST NOT RETURN, or the program will likely crash.  This
 * function is used by default, or if the program supplies NULL for the
 * error function pointer in png_set_error_fn().
 */
538
static void PNGCBAPI
539
pngtest_error(png_structp png_ptr, png_const_charp message)
540
{
541
   pngtest_warning(png_ptr, message);
542
   /* We can return because png_error calls the default handler, which is
543 544
    * actually OK in this case.
    */
545
}
546
#endif /* !PNG_STDIO_SUPPORTED */
547 548
/* END of code to validate stdio-free compilation */

549
/* START of code to validate memory allocation and deallocation */
550
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
551 552

/* Allocate memory.  For reasonable files, size should never exceed
553 554 555 556 557 558 559 560
 * 64K.  However, zlib may allocate more then 64K if you don't tell
 * it not to.  See zconf.h and png.h for more information.  zlib does
 * need to allocate exactly 64K, so whatever you call here must
 * have the ability to do that.
 *
 * This piece of code can be compiled to validate max 64K allocations
 * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
 */
561 562
typedef struct memory_information
{
563
   png_alloc_size_t          size;
564
   png_voidp                 pointer;
565 566 567 568 569 570 571
   struct memory_information FAR *next;
} memory_information;
typedef memory_information FAR *memory_infop;

static memory_infop pinformation = NULL;
static int current_allocation = 0;
static int maximum_allocation = 0;
572 573
static int total_allocation = 0;
static int num_allocations = 0;
574

575 576 577
png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,
    png_alloc_size_t size));
void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
578 579

png_voidp
580
PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
581
{
582

583
   /* png_malloc has already tested for NULL; png_create_struct calls
584 585
    * png_debug_malloc directly, with png_ptr == NULL which is OK
    */
586

587
   if (size == 0)
588
      return (NULL);
589 590 591 592

   /* This calls the library allocator twice, once to get the requested
      buffer and once to get a new free list entry. */
   {
593
      /* Disable malloc_fn and free_fn */
594
      memory_infop pinfo;
595
      png_set_mem_fn(png_ptr, NULL, NULL, NULL);
596 597
      pinfo = (memory_infop)png_malloc(png_ptr,
         png_sizeof(*pinfo));
598 599
      pinfo->size = size;
      current_allocation += size;
600 601
      total_allocation += size;
      num_allocations ++;
602

603 604
      if (current_allocation > maximum_allocation)
         maximum_allocation = current_allocation;
605

606
      pinfo->pointer = png_malloc(png_ptr, size);
607
      /* Restore malloc_fn and free_fn */
608

609 610
      png_set_mem_fn(png_ptr,
          NULL, png_debug_malloc, png_debug_free);
611

612 613 614 615
      if (size != 0 && pinfo->pointer == NULL)
      {
         current_allocation -= size;
         total_allocation -= size;
616
         png_error(png_ptr,
617
           "out of memory in pngtest->png_debug_malloc");
618
      }
619

620 621 622 623
      pinfo->next = pinformation;
      pinformation = pinfo;
      /* Make sure the caller isn't assuming zeroed memory. */
      png_memset(pinfo->pointer, 0xdd, pinfo->size);
624

625
      if (verbose)
626
         printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
627
            pinfo->pointer);
628

629
      return (png_voidp)(pinfo->pointer);
630 631 632 633
   }
}

/* Free a pointer.  It is removed from the list at the same time. */
634
void PNGCBAPI
635
png_debug_free(png_structp png_ptr, png_voidp ptr)
636 637
{
   if (png_ptr == NULL)
638
      fprintf(STDERR, "NULL pointer to png_debug_free.\n");
639

640 641
   if (ptr == 0)
   {
642 643 644 645 646 647 648 649 650
#if 0 /* This happens all the time. */
      fprintf(STDERR, "WARNING: freeing NULL pointer\n");
#endif
      return;
   }

   /* Unlink the element from the list. */
   {
      memory_infop FAR *ppinfo = &pinformation;
651

652 653
      for (;;)
      {
654
         memory_infop pinfo = *ppinfo;
655

656 657
         if (pinfo->pointer == ptr)
         {
658 659 660 661 662
            *ppinfo = pinfo->next;
            current_allocation -= pinfo->size;
            if (current_allocation < 0)
               fprintf(STDERR, "Duplicate free of memory\n");
            /* We must free the list element too, but first kill
663
               the memory that is to be freed. */
664
            png_memset(ptr, 0x55, pinfo->size);
665
            png_free_default(png_ptr, pinfo);
666
            pinfo = NULL;
667 668
            break;
         }
669

670 671
         if (pinfo->next == NULL)
         {
672
            fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr);
673 674
            break;
         }
675

676 677 678 679 680
         ppinfo = &pinfo->next;
      }
   }

   /* Finally free the data. */
681
   if (verbose)
682
      printf("Freeing %p\n", ptr);
683

684
   png_free_default(png_ptr, ptr);
685
   ptr = NULL;
686
}
687
#endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */
688 689
/* END of code to test memory allocation/deallocation */

690 691

/* Demonstration of user chunk support of the sTER and vpAg chunks */
692
#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
693

694
/* (sTER is a public chunk not yet known by libpng.  vpAg is a private
695 696 697 698
chunk used in ImageMagick to store "virtual page" size).  */

static png_uint_32 user_chunk_data[4];

699 700 701 702
    /* 0: sTER mode + 1
     * 1: vpAg width
     * 2: vpAg height
     * 3: vpAg units
703 704
     */

705
static int PNGCBAPI read_user_chunk_callback(png_struct *png_ptr,
706 707
   png_unknown_chunkp chunk)
{
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
   png_uint_32
     *my_user_chunk_data;

   /* Return one of the following:
    *    return (-n);  chunk had an error
    *    return (0);  did not recognize
    *    return (n);  success
    *
    * The unknown chunk structure contains the chunk data:
    * png_byte name[5];
    * png_byte *data;
    * png_size_t size;
    *
    * Note that libpng has already taken care of the CRC handling.
    */

   if (chunk->name[0] == 115 && chunk->name[1] ==  84 &&     /* s  T */
       chunk->name[2] ==  69 && chunk->name[3] ==  82)       /* E  R */
      {
         /* Found sTER chunk */
         if (chunk->size != 1)
            return (-1); /* Error return */
730

731 732
         if (chunk->data[0] != 0 && chunk->data[0] != 1)
            return (-1);  /* Invalid mode */
733

734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
         my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr);
         my_user_chunk_data[0]=chunk->data[0]+1;
         return (1);
      }

   if (chunk->name[0] != 118 || chunk->name[1] != 112 ||    /* v  p */
       chunk->name[2] !=  65 || chunk->name[3] != 103)      /* A  g */
      return (0); /* Did not recognize */

   /* Found ImageMagick vpAg chunk */

   if (chunk->size != 9)
      return (-1); /* Error return */

   my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr);

   my_user_chunk_data[1]=png_get_uint_31(png_ptr, chunk->data);
   my_user_chunk_data[2]=png_get_uint_31(png_ptr, chunk->data + 4);
   my_user_chunk_data[3]=(png_uint_32)chunk->data[8];

   return (1);
755 756 757 758 759

}
#endif
/* END of code to demonstrate user chunk support */

760
/* Test one file */
761 762
int
test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
G
Guy Schalnat 已提交
763
{
764 765
   static png_FILE_p fpin;
   static png_FILE_p fpout;  /* "static" prevents setjmp corruption */
766 767 768 769 770 771 772 773 774 775 776
   png_structp read_ptr;
   png_infop read_info_ptr, end_info_ptr;
#ifdef PNG_WRITE_SUPPORTED
   png_structp write_ptr;
   png_infop write_info_ptr;
   png_infop write_end_info_ptr;
#else
   png_structp write_ptr = NULL;
   png_infop write_info_ptr = NULL;
   png_infop write_end_info_ptr = NULL;
#endif
G
Guy Schalnat 已提交
777
   png_bytep row_buf;
G
Guy Schalnat 已提交
778
   png_uint_32 y;
A
Andreas Dilger 已提交
779 780 781
   png_uint_32 width, height;
   int num_pass, pass;
   int bit_depth, color_type;
782
#ifdef PNG_SETJMP_SUPPORTED
A
Andreas Dilger 已提交
783
#ifdef USE_FAR_KEYWORD
784
   jmp_buf tmp_jmpbuf;
785
#endif
786
#endif
787

788
   char inbuf[256], outbuf[256];
G
Guy Schalnat 已提交
789

790
   row_buf = NULL;
G
Guy Schalnat 已提交
791

A
Andreas Dilger 已提交
792
   if ((fpin = fopen(inname, "rb")) == NULL)
G
Guy Schalnat 已提交
793 794
   {
      fprintf(STDERR, "Could not find input file %s\n", inname);
795
      return (1);
G
Guy Schalnat 已提交
796 797
   }

A
Andreas Dilger 已提交
798
   if ((fpout = fopen(outname, "wb")) == NULL)
G
Guy Schalnat 已提交
799
   {
G
Guy Schalnat 已提交
800
      fprintf(STDERR, "Could not open output file %s\n", outname);
801
      FCLOSE(fpin);
802
      return (1);
G
Guy Schalnat 已提交
803 804
   }

805
   pngtest_debug("Allocating read and write structures");
806
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
807 808
   read_ptr =
      png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
809
      NULL, NULL, NULL, png_debug_malloc, png_debug_free);
810
#else
811 812
   read_ptr =
      png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
813
#endif
814
#ifndef PNG_STDIO_SUPPORTED
815 816
   png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
       pngtest_warning);
817
#endif
818

819
#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
820 821 822 823 824 825
   user_chunk_data[0] = 0;
   user_chunk_data[1] = 0;
   user_chunk_data[2] = 0;
   user_chunk_data[3] = 0;
   png_set_read_user_chunk_fn(read_ptr, user_chunk_data,
     read_user_chunk_callback);
826

827
#endif
828
#ifdef PNG_WRITE_SUPPORTED
829
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
830 831 832
   write_ptr =
      png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
      NULL, NULL, NULL, png_debug_malloc, png_debug_free);
833
#else
834 835
   write_ptr =
      png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
836
#endif
837
#ifndef PNG_STDIO_SUPPORTED
838 839
   png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
       pngtest_warning);
840
#endif
841
#endif
842
   pngtest_debug("Allocating read_info, write_info and end_info structures");
A
Andreas Dilger 已提交
843
   read_info_ptr = png_create_info_struct(read_ptr);
844
   end_info_ptr = png_create_info_struct(read_ptr);
845 846
#ifdef PNG_WRITE_SUPPORTED
   write_info_ptr = png_create_info_struct(write_ptr);
847
   write_end_info_ptr = png_create_info_struct(write_ptr);
848
#endif
G
Guy Schalnat 已提交
849

850
#ifdef PNG_SETJMP_SUPPORTED
851
   pngtest_debug("Setting jmpbuf for read struct");
A
Andreas Dilger 已提交
852
#ifdef USE_FAR_KEYWORD
853
   if (setjmp(tmp_jmpbuf))
A
Andreas Dilger 已提交
854
#else
855
   if (setjmp(png_jmpbuf(read_ptr)))
A
Andreas Dilger 已提交
856
#endif
G
Guy Schalnat 已提交
857
   {
858
      fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
859 860
      png_free(read_ptr, row_buf);
      row_buf = NULL;
A
Andreas Dilger 已提交
861
      png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
862
#ifdef PNG_WRITE_SUPPORTED
863
      png_destroy_info_struct(write_ptr, &write_end_info_ptr);
A
Andreas Dilger 已提交
864
      png_destroy_write_struct(&write_ptr, &write_info_ptr);
865
#endif
866 867
      FCLOSE(fpin);
      FCLOSE(fpout);
868
      return (1);
G
Guy Schalnat 已提交
869
   }
870
#ifdef USE_FAR_KEYWORD
871
   png_memcpy(png_jmpbuf(read_ptr), tmp_jmpbuf, png_sizeof(jmp_buf));
872
#endif
A
Andreas Dilger 已提交
873

874
#ifdef PNG_WRITE_SUPPORTED
875
   pngtest_debug("Setting jmpbuf for write struct");
A
Andreas Dilger 已提交
876
#ifdef USE_FAR_KEYWORD
877

878
   if (setjmp(tmp_jmpbuf))
A
Andreas Dilger 已提交
879
#else
880
   if (setjmp(png_jmpbuf(write_ptr)))
A
Andreas Dilger 已提交
881
#endif
G
Guy Schalnat 已提交
882
   {
883
      fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
A
Andreas Dilger 已提交
884
      png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
885
      png_destroy_info_struct(write_ptr, &write_end_info_ptr);
886
#ifdef PNG_WRITE_SUPPORTED
A
Andreas Dilger 已提交
887
      png_destroy_write_struct(&write_ptr, &write_info_ptr);
888
#endif
889 890
      FCLOSE(fpin);
      FCLOSE(fpout);
891
      return (1);
G
Guy Schalnat 已提交
892
   }
893

A
Andreas Dilger 已提交
894
#ifdef USE_FAR_KEYWORD
895
   png_memcpy(png_jmpbuf(write_ptr), tmp_jmpbuf, png_sizeof(jmp_buf));
896
#endif
897
#endif
A
Andreas Dilger 已提交
898
#endif
899

900
   pngtest_debug("Initializing input and output streams");
901
#ifdef PNG_STDIO_SUPPORTED
G
Guy Schalnat 已提交
902
   png_init_io(read_ptr, fpin);
903
#  ifdef PNG_WRITE_SUPPORTED
G
Guy Schalnat 已提交
904
   png_init_io(write_ptr, fpout);
905
#  endif
906
#else
907
   png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
908
#  ifdef PNG_WRITE_SUPPORTED
909
   png_set_write_fn(write_ptr, (png_voidp)fpout,  pngtest_write_data,
910
#    ifdef PNG_WRITE_FLUSH_SUPPORTED
911
      pngtest_flush);
912
#    else
913
      NULL);
914 915
#    endif
#  endif
916
#endif
917

918
#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION
919 920 921 922
   /* Normally one would use Z_DEFAULT_STRATEGY for text compression.
    * This is here just to make pngtest replicate the results from libpng
    * versions prior to 1.5.3, and to test this new API.
    */
923 924 925
   png_set_text_compression_strategy(write_ptr, Z_FILTERED);
#endif

926
   if (status_dots_requested == 1)
927
   {
928
#ifdef PNG_WRITE_SUPPORTED
929
      png_set_write_status_fn(write_ptr, write_row_callback);
930
#endif
931 932
      png_set_read_status_fn(read_ptr, read_row_callback);
   }
933

934 935
   else
   {
936
#ifdef PNG_WRITE_SUPPORTED
937
      png_set_write_status_fn(write_ptr, NULL);
938
#endif
939
      png_set_read_status_fn(read_ptr, NULL);
940 941
   }

942
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
943
   {
944
      int i;
945

946 947
      for (i = 0; i<256; i++)
         filters_used[i] = 0;
948

949
      png_set_read_user_transform_fn(read_ptr, count_filters);
950 951
   }
#endif
952
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
953
   zero_samples = 0;
954 955
   png_set_write_user_transform_fn(write_ptr, count_zero_samples);
#endif
G
Guy Schalnat 已提交
956

957
#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
958 959 960
#  ifndef PNG_HANDLE_CHUNK_ALWAYS
#    define PNG_HANDLE_CHUNK_ALWAYS       3
#  endif
961 962
   png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
      NULL, 0);
963
#endif
964
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
965 966 967
#  ifndef PNG_HANDLE_CHUNK_IF_SAFE
#    define PNG_HANDLE_CHUNK_IF_SAFE      2
#  endif
968 969
   png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE,
      NULL, 0);
970 971
#endif

972
   pngtest_debug("Reading info struct");
A
Andreas Dilger 已提交
973
   png_read_info(read_ptr, read_info_ptr);
G
Guy Schalnat 已提交
974

975
   pngtest_debug("Transferring info struct");
A
Andreas Dilger 已提交
976 977
   {
      int interlace_type, compression_type, filter_type;
G
Guy Schalnat 已提交
978

A
Andreas Dilger 已提交
979 980 981 982
      if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
          &color_type, &interlace_type, &compression_type, &filter_type))
      {
         png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
983
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
A
Andreas Dilger 已提交
984
            color_type, interlace_type, compression_type, filter_type);
985 986 987
#else
            color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
#endif
A
Andreas Dilger 已提交
988 989
      }
   }
990 991
#ifdef PNG_FIXED_POINT_SUPPORTED
#ifdef PNG_cHRM_SUPPORTED
G
Guy Schalnat 已提交
992
   {
993
      png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
994
         blue_y;
995

996 997
      if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
         &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y))
A
Andreas Dilger 已提交
998
      {
999 1000
         png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
            red_y, green_x, green_y, blue_x, blue_y);
A
Andreas Dilger 已提交
1001
      }
G
Guy Schalnat 已提交
1002
   }
A
Andreas Dilger 已提交
1003
#endif
1004
#ifdef PNG_gAMA_SUPPORTED
A
Andreas Dilger 已提交
1005
   {
1006
      png_fixed_point gamma;
G
Guy Schalnat 已提交
1007

1008 1009
      if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
         png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
A
Andreas Dilger 已提交
1010 1011
   }
#endif
1012
#else /* Use floating point versions */
1013 1014
#ifdef PNG_FLOATING_POINT_SUPPORTED
#ifdef PNG_cHRM_SUPPORTED
1015 1016 1017
   {
      double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
         blue_y;
1018

1019 1020 1021 1022 1023 1024 1025 1026
      if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
         &red_y, &green_x, &green_y, &blue_x, &blue_y))
      {
         png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
            red_y, green_x, green_y, blue_x, blue_y);
      }
   }
#endif
1027
#ifdef PNG_gAMA_SUPPORTED
1028 1029 1030 1031 1032 1033 1034
   {
      double gamma;

      if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
         png_set_gAMA(write_ptr, write_info_ptr, gamma);
   }
#endif
1035 1036
#endif /* Floating point */
#endif /* Fixed point */
1037
#ifdef PNG_iCCP_SUPPORTED
G
Guy Schalnat 已提交
1038
   {
1039
      png_charp name;
G
[devel]  
Glenn Randers-Pehrson 已提交
1040
      png_bytep profile;
1041
      png_uint_32 proflen;
1042
      int compression_type;
A
Andreas Dilger 已提交
1043

1044 1045
      if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
                      &profile, &proflen))
A
Andreas Dilger 已提交
1046
      {
1047 1048
         png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
                      profile, proflen);
A
Andreas Dilger 已提交
1049
      }
G
Guy Schalnat 已提交
1050
   }
1051
#endif
1052
#ifdef PNG_sRGB_SUPPORTED
1053
   {
1054
      int intent;
1055 1056 1057 1058

      if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
         png_set_sRGB(write_ptr, write_info_ptr, intent);
   }
A
Andreas Dilger 已提交
1059
#endif
1060 1061 1062 1063 1064 1065 1066
   {
      png_colorp palette;
      int num_palette;

      if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
         png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
   }
1067
#ifdef PNG_bKGD_SUPPORTED
1068 1069 1070 1071 1072 1073 1074 1075 1076
   {
      png_color_16p background;

      if (png_get_bKGD(read_ptr, read_info_ptr, &background))
      {
         png_set_bKGD(write_ptr, write_info_ptr, background);
      }
   }
#endif
1077
#ifdef PNG_hIST_SUPPORTED
G
Guy Schalnat 已提交
1078
   {
A
Andreas Dilger 已提交
1079 1080 1081 1082
      png_uint_16p hist;

      if (png_get_hIST(read_ptr, read_info_ptr, &hist))
         png_set_hIST(write_ptr, write_info_ptr, hist);
G
Guy Schalnat 已提交
1083
   }
A
Andreas Dilger 已提交
1084
#endif
1085
#ifdef PNG_oFFs_SUPPORTED
A
Andreas Dilger 已提交
1086
   {
1087
      png_int_32 offset_x, offset_y;
A
Andreas Dilger 已提交
1088
      int unit_type;
G
Guy Schalnat 已提交
1089

1090
      if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
1091
          &unit_type))
A
Andreas Dilger 已提交
1092 1093 1094 1095 1096
      {
         png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
      }
   }
#endif
1097
#ifdef PNG_pCAL_SUPPORTED
A
Andreas Dilger 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
   {
      png_charp purpose, units;
      png_charpp params;
      png_int_32 X0, X1;
      int type, nparams;

      if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
         &nparams, &units, &params))
      {
         png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
            nparams, units, params);
      }
   }
#endif
1112
#ifdef PNG_pHYs_SUPPORTED
A
Andreas Dilger 已提交
1113 1114 1115 1116 1117 1118 1119 1120
   {
      png_uint_32 res_x, res_y;
      int unit_type;

      if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
         png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
   }
#endif
1121
#ifdef PNG_sBIT_SUPPORTED
A
Andreas Dilger 已提交
1122
   {
1123
      png_color_8p sig_bit;
A
Andreas Dilger 已提交
1124

1125 1126
      if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
         png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
A
Andreas Dilger 已提交
1127
   }
1128
#endif
1129
#ifdef PNG_sCAL_SUPPORTED
1130
#ifdef PNG_FLOATING_POINT_SUPPORTED
G
Guy Schalnat 已提交
1131
   {
1132
      int unit;
1133
      double scal_width, scal_height;
A
Andreas Dilger 已提交
1134

1135 1136
      if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
         &scal_height))
G
Guy Schalnat 已提交
1137
      {
1138
         png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
1139 1140 1141
      }
   }
#else
1142
#ifdef PNG_FIXED_POINT_SUPPORTED
1143
   {
1144
      int unit;
1145
      png_charp scal_width, scal_height;
1146

1147 1148
      if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
          &scal_height))
1149
      {
1150 1151
         png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
             scal_height);
A
Andreas Dilger 已提交
1152 1153
      }
   }
G
Guy Schalnat 已提交
1154
#endif
1155 1156
#endif
#endif
1157
#ifdef PNG_TEXT_SUPPORTED
A
Andreas Dilger 已提交
1158 1159 1160 1161 1162 1163
   {
      png_textp text_ptr;
      int num_text;

      if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
      {
1164
         pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
A
Andreas Dilger 已提交
1165 1166 1167 1168
         png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
      }
   }
#endif
1169
#ifdef PNG_tIME_SUPPORTED
A
Andreas Dilger 已提交
1170 1171 1172 1173 1174 1175
   {
      png_timep mod_time;

      if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
      {
         png_set_tIME(write_ptr, write_info_ptr, mod_time);
1176
#ifdef PNG_TIME_RFC1123_SUPPORTED
1177
         /* We have to use png_memcpy instead of "=" because the string
1178 1179 1180
          * pointed to by png_convert_to_rfc1123() gets free'ed before
          * we use it.
          */
1181 1182 1183
         png_memcpy(tIME_string,
                    png_convert_to_rfc1123(read_ptr, mod_time),
                    png_sizeof(tIME_string));
1184

1185
         tIME_string[png_sizeof(tIME_string) - 1] = '\0';
1186 1187
         tIME_chunk_present++;
#endif /* PNG_TIME_RFC1123_SUPPORTED */
1188
      }
A
Andreas Dilger 已提交
1189 1190
   }
#endif
1191
#ifdef PNG_tRNS_SUPPORTED
A
Andreas Dilger 已提交
1192
   {
1193
      png_bytep trans_alpha;
A
Andreas Dilger 已提交
1194
      int num_trans;
1195
      png_color_16p trans_color;
A
Andreas Dilger 已提交
1196

1197
      if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
1198
         &trans_color))
A
Andreas Dilger 已提交
1199
      {
1200
         int sample_max = (1 << bit_depth);
1201
         /* libpng doesn't reject a tRNS chunk with out-of-range samples */
1202
         if (!((color_type == PNG_COLOR_TYPE_GRAY &&
1203
             (int)trans_color->gray > sample_max) ||
1204
             (color_type == PNG_COLOR_TYPE_RGB &&
1205 1206 1207
             ((int)trans_color->red > sample_max ||
             (int)trans_color->green > sample_max ||
             (int)trans_color->blue > sample_max))))
1208
            png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
1209
               trans_color);
A
Andreas Dilger 已提交
1210 1211 1212
      }
   }
#endif
1213
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1214 1215
   {
      png_unknown_chunkp unknowns;
1216
      int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
1217
         &unknowns);
1218

1219 1220
      if (num_unknowns)
      {
1221
         int i;
1222 1223
         png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
           num_unknowns);
1224
         /* Copy the locations from the read_info_ptr.  The automatically
1225 1226 1227
          * generated locations in write_info_ptr are wrong because we
          * haven't written anything yet.
          */
1228
         for (i = 0; i < num_unknowns; i++)
1229 1230
           png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
             unknowns[i].location);
1231 1232 1233
      }
   }
#endif
A
Andreas Dilger 已提交
1234

1235
#ifdef PNG_WRITE_SUPPORTED
1236
   pngtest_debug("Writing info struct");
1237 1238

/* If we wanted, we could write info in two steps:
1239
 * png_write_info_before_PLTE(write_ptr, write_info_ptr);
1240
 */
A
Andreas Dilger 已提交
1241
   png_write_info(write_ptr, write_info_ptr);
1242

1243
#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
1244 1245
   if (user_chunk_data[0] != 0)
   {
1246
      png_byte png_sTER[5] = {115,  84,  69,  82, '\0'};
1247

1248 1249
      unsigned char
        ster_chunk_data[1];
1250

1251 1252 1253
      if (verbose)
         fprintf(STDERR, "\n stereo mode = %lu\n",
           (unsigned long)(user_chunk_data[0] - 1));
1254

1255 1256
      ster_chunk_data[0]=(unsigned char)(user_chunk_data[0] - 1);
      png_write_chunk(write_ptr, png_sTER, ster_chunk_data, 1);
1257
   }
1258

1259 1260
   if (user_chunk_data[1] != 0 || user_chunk_data[2] != 0)
   {
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
      png_byte png_vpAg[5] = {118, 112,  65, 103, '\0'};

      unsigned char
        vpag_chunk_data[9];

      if (verbose)
         fprintf(STDERR, " vpAg = %lu x %lu, units = %lu\n",
           (unsigned long)user_chunk_data[1],
           (unsigned long)user_chunk_data[2],
           (unsigned long)user_chunk_data[3]);
1271

1272 1273 1274 1275
      png_save_uint_32(vpag_chunk_data, user_chunk_data[1]);
      png_save_uint_32(vpag_chunk_data + 4, user_chunk_data[2]);
      vpag_chunk_data[8] = (unsigned char)(user_chunk_data[3] & 0xff);
      png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9);
1276 1277 1278
   }

#endif
1279
#endif
A
Andreas Dilger 已提交
1280

1281
#ifdef SINGLE_ROWBUF_ALLOC
1282
   pngtest_debug("Allocating row buffer...");
1283
   row_buf = (png_bytep)png_malloc(read_ptr,
A
Andreas Dilger 已提交
1284
      png_get_rowbytes(read_ptr, read_info_ptr));
1285

1286
   pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
1287
#endif /* SINGLE_ROWBUF_ALLOC */
1288
   pngtest_debug("Writing row data");
A
Andreas Dilger 已提交
1289

1290 1291
#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
  defined(PNG_WRITE_INTERLACING_SUPPORTED)
A
Andreas Dilger 已提交
1292
   num_pass = png_set_interlace_handling(read_ptr);
1293
#  ifdef PNG_WRITE_SUPPORTED
A
Andreas Dilger 已提交
1294
   png_set_interlace_handling(write_ptr);
1295
#  endif
1296
#else
1297
   num_pass = 1;
1298
#endif
A
Andreas Dilger 已提交
1299

1300 1301 1302 1303 1304
#ifdef PNGTEST_TIMING
   t_stop = (float)clock();
   t_misc += (t_stop - t_start);
   t_start = t_stop;
#endif
A
Andreas Dilger 已提交
1305 1306
   for (pass = 0; pass < num_pass; pass++)
   {
1307
      pngtest_debug1("Writing row data for pass %d", pass);
A
Andreas Dilger 已提交
1308 1309
      for (y = 0; y < height; y++)
      {
1310
#ifndef SINGLE_ROWBUF_ALLOC
1311
         pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
1312 1313
         row_buf = (png_bytep)png_malloc(read_ptr,
            png_get_rowbytes(read_ptr, read_info_ptr));
1314

1315
         pngtest_debug2("\t0x%08lx (%u bytes)", (unsigned long)row_buf,
1316
            png_get_rowbytes(read_ptr, read_info_ptr));
1317

1318
#endif /* !SINGLE_ROWBUF_ALLOC */
1319
         png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
1320 1321

#ifdef PNG_WRITE_SUPPORTED
1322 1323 1324 1325 1326
#ifdef PNGTEST_TIMING
         t_stop = (float)clock();
         t_decode += (t_stop - t_start);
         t_start = t_stop;
#endif
G
Guy Schalnat 已提交
1327
         png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
1328 1329 1330 1331 1332
#ifdef PNGTEST_TIMING
         t_stop = (float)clock();
         t_encode += (t_stop - t_start);
         t_start = t_stop;
#endif
1333 1334 1335
#endif /* PNG_WRITE_SUPPORTED */

#ifndef SINGLE_ROWBUF_ALLOC
1336
         pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
1337
         png_free(read_ptr, row_buf);
1338
         row_buf = NULL;
1339
#endif /* !SINGLE_ROWBUF_ALLOC */
G
Guy Schalnat 已提交
1340 1341 1342
      }
   }

1343
#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1344
   png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
1345
#endif
1346
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1347
   png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
1348 1349
#endif

1350
   pngtest_debug("Reading and writing end_info data");
1351

A
Andreas Dilger 已提交
1352
   png_read_end(read_ptr, end_info_ptr);
1353
#ifdef PNG_TEXT_SUPPORTED
1354 1355 1356 1357 1358 1359
   {
      png_textp text_ptr;
      int num_text;

      if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
      {
1360
         pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1361 1362 1363 1364
         png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
      }
   }
#endif
1365
#ifdef PNG_tIME_SUPPORTED
1366 1367 1368 1369 1370 1371
   {
      png_timep mod_time;

      if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
      {
         png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
1372
#ifdef PNG_TIME_RFC1123_SUPPORTED
1373
         /* We have to use png_memcpy instead of "=" because the string
1374 1375
            pointed to by png_convert_to_rfc1123() gets free'ed before
            we use it */
1376 1377 1378
         png_memcpy(tIME_string,
                    png_convert_to_rfc1123(read_ptr, mod_time),
                    png_sizeof(tIME_string));
1379

1380
         tIME_string[png_sizeof(tIME_string) - 1] = '\0';
1381 1382 1383 1384
         tIME_chunk_present++;
#endif /* PNG_TIME_RFC1123_SUPPORTED */
      }
   }
1385
#endif
1386
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1387 1388
   {
      png_unknown_chunkp unknowns;
1389
      int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
1390
         &unknowns);
1391

1392 1393
      if (num_unknowns)
      {
1394
         int i;
1395 1396
         png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
           num_unknowns);
1397
         /* Copy the locations from the read_info_ptr.  The automatically
1398 1399 1400
          * generated locations in write_end_info_ptr are wrong because we
          * haven't written the end_info yet.
          */
1401
         for (i = 0; i < num_unknowns; i++)
1402 1403
           png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
             unknowns[i].location);
1404 1405
      }
   }
1406
#endif
1407
#ifdef PNG_WRITE_SUPPORTED
1408
   png_write_end(write_ptr, write_end_info_ptr);
1409
#endif
1410

1411
#ifdef PNG_EASY_ACCESS_SUPPORTED
1412
   if (verbose)
1413 1414 1415 1416
   {
      png_uint_32 iwidth, iheight;
      iwidth = png_get_image_width(write_ptr, write_info_ptr);
      iheight = png_get_image_height(write_ptr, write_info_ptr);
1417
      fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
1418
         (unsigned long)iwidth, (unsigned long)iheight);
1419 1420
   }
#endif
G
Guy Schalnat 已提交
1421

1422
   pngtest_debug("Destroying data structs");
1423
#ifdef SINGLE_ROWBUF_ALLOC
1424
   pngtest_debug("destroying row_buf for read_ptr");
1425
   png_free(read_ptr, row_buf);
1426
   row_buf = NULL;
1427
#endif /* SINGLE_ROWBUF_ALLOC */
1428
   pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
A
Andreas Dilger 已提交
1429
   png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
1430
#ifdef PNG_WRITE_SUPPORTED
1431
   pngtest_debug("destroying write_end_info_ptr");
1432
   png_destroy_info_struct(write_ptr, &write_end_info_ptr);
1433
   pngtest_debug("destroying write_ptr, write_info_ptr");
A
Andreas Dilger 已提交
1434
   png_destroy_write_struct(&write_ptr, &write_info_ptr);
1435
#endif
1436
   pngtest_debug("Destruction complete.");
G
Guy Schalnat 已提交
1437

1438 1439
   FCLOSE(fpin);
   FCLOSE(fpout);
G
Guy Schalnat 已提交
1440

1441
   pngtest_debug("Opening files for comparison");
A
Andreas Dilger 已提交
1442
   if ((fpin = fopen(inname, "rb")) == NULL)
G
Guy Schalnat 已提交
1443
   {
G
Guy Schalnat 已提交
1444
      fprintf(STDERR, "Could not find file %s\n", inname);
1445
      return (1);
G
Guy Schalnat 已提交
1446 1447
   }

A
Andreas Dilger 已提交
1448
   if ((fpout = fopen(outname, "rb")) == NULL)
G
Guy Schalnat 已提交
1449
   {
G
Guy Schalnat 已提交
1450
      fprintf(STDERR, "Could not find file %s\n", outname);
1451
      FCLOSE(fpin);
1452
      return (1);
G
Guy Schalnat 已提交
1453
   }
A
Andreas Dilger 已提交
1454

1455
   for (;;)
G
Guy Schalnat 已提交
1456
   {
A
Andreas Dilger 已提交
1457
      png_size_t num_in, num_out;
G
Guy Schalnat 已提交
1458

1459 1460
         num_in = fread(inbuf, 1, 1, fpin);
         num_out = fread(outbuf, 1, 1, fpout);
G
Guy Schalnat 已提交
1461 1462 1463

      if (num_in != num_out)
      {
1464
         fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
G
Guy Schalnat 已提交
1465
                 inname, outname);
1466

1467
         if (wrote_question == 0)
1468 1469
         {
            fprintf(STDERR,
1470
         "   Was %s written with the same maximum IDAT chunk size (%d bytes),",
1471
              inname, PNG_ZBUF_SIZE);
1472
            fprintf(STDERR,
1473
              "\n   filtering heuristic (libpng default), compression");
1474
            fprintf(STDERR,
1475
              " level (zlib default),\n   and zlib version (%s)?\n\n",
1476
              ZLIB_VERSION);
1477
            wrote_question = 1;
1478
         }
1479

1480 1481
         FCLOSE(fpin);
         FCLOSE(fpout);
1482
         return (0);
G
Guy Schalnat 已提交
1483 1484 1485 1486 1487
      }

      if (!num_in)
         break;

A
Andreas Dilger 已提交
1488
      if (png_memcmp(inbuf, outbuf, num_in))
G
Guy Schalnat 已提交
1489
      {
1490
         fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname);
1491

1492
         if (wrote_question == 0)
1493 1494
         {
            fprintf(STDERR,
1495
         "   Was %s written with the same maximum IDAT chunk size (%d bytes),",
1496
                 inname, PNG_ZBUF_SIZE);
1497
            fprintf(STDERR,
1498
              "\n   filtering heuristic (libpng default), compression");
1499
            fprintf(STDERR,
1500
              " level (zlib default),\n   and zlib version (%s)?\n\n",
1501
              ZLIB_VERSION);
1502
            wrote_question = 1;
1503
         }
1504

1505 1506
         FCLOSE(fpin);
         FCLOSE(fpout);
1507
         return (0);
G
Guy Schalnat 已提交
1508 1509 1510
      }
   }

1511 1512
   FCLOSE(fpin);
   FCLOSE(fpout);
G
Guy Schalnat 已提交
1513

1514
   return (0);
G
Guy Schalnat 已提交
1515
}
G
Guy Schalnat 已提交
1516

1517
/* Input and output filenames */
1518
#ifdef RISCOS
1519 1520
static PNG_CONST char *inname = "pngtest/png";
static PNG_CONST char *outname = "pngout/png";
1521
#else
1522 1523
static PNG_CONST char *inname = "pngtest.png";
static PNG_CONST char *outname = "pngout.png";
1524 1525 1526 1527 1528 1529 1530 1531
#endif

int
main(int argc, char *argv[])
{
   int multiple = 0;
   int ierror = 0;

1532
   fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
1533
   fprintf(STDERR, "   with zlib   version %s\n", ZLIB_VERSION);
1534
   fprintf(STDERR, "%s", png_get_copyright(NULL));
1535
   /* Show the version of libpng used in building the library */
1536 1537
   fprintf(STDERR, " library (%lu):%s",
      (unsigned long)png_access_version_number(),
1538
      png_get_header_version(NULL));
1539

1540
   /* Show the version of libpng used in building the application */
1541
   fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
1542
      PNG_HEADER_VERSION_STRING);
1543 1544

   /* Do some consistency checking on the memory allocation settings, I'm
1545 1546 1547 1548
    * not sure this matters, but it is nice to know, the first of these
    * tests should be impossible because of the way the macros are set
    * in pngconf.h
    */
1549
#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
1550
      fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
1551
#endif
1552
   /* I think the following can happen. */
1553
#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
1554
      fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
1555
#endif
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567

   if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
   {
      fprintf(STDERR,
         "Warning: versions are different between png.h and png.c\n");
      fprintf(STDERR, "  png.h version: %s\n", PNG_LIBPNG_VER_STRING);
      fprintf(STDERR, "  png.c version: %s\n\n", png_libpng_ver);
      ++ierror;
   }

   if (argc > 1)
   {
1568
      if (strcmp(argv[1], "-m") == 0)
1569
      {
1570
         multiple = 1;
1571 1572
         status_dots_requested = 0;
      }
1573

1574 1575 1576 1577 1578
      else if (strcmp(argv[1], "-mv") == 0 ||
               strcmp(argv[1], "-vm") == 0 )
      {
         multiple = 1;
         verbose = 1;
1579
         status_dots_requested = 1;
1580
      }
1581

1582 1583 1584
      else if (strcmp(argv[1], "-v") == 0)
      {
         verbose = 1;
1585
         status_dots_requested = 1;
1586 1587
         inname = argv[2];
      }
1588

1589
      else
1590
      {
1591
         inname = argv[1];
1592 1593
         status_dots_requested = 0;
      }
1594 1595
   }

1596 1597
   if (!multiple && argc == 3 + verbose)
     outname = argv[2 + verbose];
1598

1599
   if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2))
1600
   {
1601 1602
     fprintf(STDERR,
       "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
1603
        argv[0], argv[0]);
1604 1605 1606 1607
     fprintf(STDERR,
       "  reads/writes one PNG file (without -m) or multiple files (-m)\n");
     fprintf(STDERR,
       "  with -m %s is used as a temporary file\n", outname);
1608 1609 1610 1611 1612 1613
     exit(1);
   }

   if (multiple)
   {
      int i;
1614
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1615 1616
      int allocation_now = current_allocation;
#endif
1617
      for (i=2; i<argc; ++i)
1618
      {
1619
         int kerror;
1620
         fprintf(STDERR, "\n Testing %s:", argv[i]);
1621
         kerror = test_one_file(argv[i], outname);
1622
         if (kerror == 0)
1623
         {
1624 1625 1626
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
            int k;
#endif
1627
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1628
            fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1629
               (unsigned long)zero_samples);
1630
#else
1631
            fprintf(STDERR, " PASS\n");
1632
#endif
1633
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1634 1635
            for (k = 0; k<256; k++)
               if (filters_used[k])
1636
                  fprintf(STDERR, " Filter %d was used %lu times\n",
1637
                     k, (unsigned long)filters_used[k]);
1638
#endif
1639
#ifdef PNG_TIME_RFC1123_SUPPORTED
1640 1641
         if (tIME_chunk_present != 0)
            fprintf(STDERR, " tIME = %s\n", tIME_string);
1642

1643 1644 1645
         tIME_chunk_present = 0;
#endif /* PNG_TIME_RFC1123_SUPPORTED */
         }
1646

1647 1648
         else
         {
1649 1650
            fprintf(STDERR, " FAIL\n");
            ierror += kerror;
1651
         }
1652
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1653 1654
         if (allocation_now != current_allocation)
            fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1655
               current_allocation - allocation_now);
1656

1657 1658
         if (current_allocation != 0)
         {
1659 1660 1661 1662
            memory_infop pinfo = pinformation;

            fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
               current_allocation);
1663

1664 1665
            while (pinfo != NULL)
            {
1666
               fprintf(STDERR, " %lu bytes at %x\n",
1667
                 (unsigned long)pinfo->size,
1668
                 (unsigned int)pinfo->pointer);
1669
               pinfo = pinfo->next;
1670
            }
1671
         }
1672 1673
#endif
      }
1674
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1675
         fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1676
            current_allocation);
1677
         fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
1678
            maximum_allocation);
1679 1680 1681 1682
         fprintf(STDERR, " Total   memory allocation: %10d bytes\n",
            total_allocation);
         fprintf(STDERR, "     Number of allocations: %10d\n",
            num_allocations);
1683
#endif
1684
   }
1685

1686 1687
   else
   {
1688
      int i;
1689
      for (i = 0; i<3; ++i)
1690
      {
1691
         int kerror;
1692
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1693 1694
         int allocation_now = current_allocation;
#endif
1695 1696 1697 1698 1699 1700
         if (i == 1)
            status_dots_requested = 1;

         else if (verbose == 0)
            status_dots_requested = 0;

1701
         if (i == 0 || verbose == 1 || ierror != 0)
1702
            fprintf(STDERR, "\n Testing %s:", inname);
1703

1704
         kerror = test_one_file(inname, outname);
1705

1706
         if (kerror == 0)
1707
         {
1708
            if (verbose == 1 || i == 2)
1709
            {
1710
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1711
                int k;
1712
#endif
1713
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1714
                fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1715
                   (unsigned long)zero_samples);
1716 1717 1718
#else
                fprintf(STDERR, " PASS\n");
#endif
1719
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1720 1721
                for (k = 0; k<256; k++)
                   if (filters_used[k])
1722
                      fprintf(STDERR, " Filter %d was used %lu times\n",
1723
                         k, (unsigned long)filters_used[k]);
1724
#endif
1725
#ifdef PNG_TIME_RFC1123_SUPPORTED
1726 1727
             if (tIME_chunk_present != 0)
                fprintf(STDERR, " tIME = %s\n", tIME_string);
1728 1729
#endif /* PNG_TIME_RFC1123_SUPPORTED */
            }
1730
         }
1731

1732 1733
         else
         {
1734
            if (verbose == 0 && i != 2)
1735
               fprintf(STDERR, "\n Testing %s:", inname);
1736

1737 1738
            fprintf(STDERR, " FAIL\n");
            ierror += kerror;
1739
         }
1740
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1741 1742
         if (allocation_now != current_allocation)
             fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1743
               current_allocation - allocation_now);
1744

1745 1746
         if (current_allocation != 0)
         {
1747
             memory_infop pinfo = pinformation;
1748

1749 1750
             fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
                current_allocation);
1751

1752 1753
             while (pinfo != NULL)
             {
1754 1755
                fprintf(STDERR, " %lu bytes at %x\n",
                   (unsigned long)pinfo->size, (unsigned int)pinfo->pointer);
1756 1757 1758 1759
                pinfo = pinfo->next;
             }
          }
#endif
1760
       }
1761
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1762
       fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1763
          current_allocation);
1764
       fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
1765
          maximum_allocation);
1766 1767 1768 1769
       fprintf(STDERR, " Total   memory allocation: %10d bytes\n",
          total_allocation);
       fprintf(STDERR, "     Number of allocations: %10d\n",
            num_allocations);
1770
#endif
1771 1772
   }

1773 1774 1775 1776
#ifdef PNGTEST_TIMING
   t_stop = (float)clock();
   t_misc += (t_stop - t_start);
   t_start = t_stop;
1777
   fprintf(STDERR, " CPU time used = %.3f seconds",
1778
      (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
1779
   fprintf(STDERR, " (decoding %.3f,\n",
1780
      t_decode/(float)CLOCKS_PER_SEC);
1781
   fprintf(STDERR, "        encoding %.3f ,",
1782
      t_encode/(float)CLOCKS_PER_SEC);
1783
   fprintf(STDERR, " other %.3f seconds)\n\n",
1784 1785 1786
      t_misc/(float)CLOCKS_PER_SEC);
#endif

1787
   if (ierror == 0)
1788
      fprintf(STDERR, " libpng passes test\n");
1789

1790
   else
1791
      fprintf(STDERR, " libpng FAILS test\n");
1792

1793
   return (int)(ierror != 0);
1794
}
1795

1796
/* Generate a compiler error if there is an old png.h in the search path. */
1797
typedef png_libpng_version_1_5_3beta11 Your_png_h_is_not_version_1_5_3beta11;