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

G
Guy Schalnat 已提交
2
/* pngtest.c - a simple test program to test libpng
3
 *
4
 * libpng 1.00
5 6 7
 * For conditions of distribution and use, see copyright notice in png.h
 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
 * Copyright (c) 1996, 1997 Andreas Dilger
8
 * Copyright (c) 1998, Glenn Randers-Pehrson
9
 * March 7, 1998
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * 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.
 *
 * The program will fail in certain legitimate cases:
 * 1) when the compression level or filter selection method is changed.
 * 2) when the chunk size is smaller than 8K.
 * 3) unknown ancillary chunks exist in the input file.
 * 4) others not listed here...
 * In these cases, it is best to check with another tool such as "pngcheck"
 * to see what the differences between the two images are.
 *
 * 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
 * testing a wide variety of files easily.
 */
G
Guy Schalnat 已提交
29 30 31

#include <stdio.h>
#include <stdlib.h>
A
Andreas Dilger 已提交
32 33 34 35

/* Makes pngtest verbose so we can find problems (needs to be before png.h) */
#ifndef PNG_DEBUG
#endif
36
#define PNG_DEBUG 0
A
Andreas Dilger 已提交
37

G
Guy Schalnat 已提交
38 39
#include "png.h"

40 41
int test_one_file(PNG_CONST char *inname, PNG_CONST char *outname);

G
Guy Schalnat 已提交
42 43 44 45 46
#ifdef __TURBOC__
#include <mem.h>
#endif

/* defined so I can write to a file on gui/windowing platforms */
G
Guy Schalnat 已提交
47
/*  #define STDERR stderr  */
G
Guy Schalnat 已提交
48
#define STDERR stdout   /* for DOS */
G
Guy Schalnat 已提交
49

50 51 52 53 54 55 56
/* example of using row callbacks to make a simple progress meter */
static int status_pass=1;
static int status_dots_requested=0;
static int status_dots=1;
void read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
void read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
{
57
    if(png_ptr == NULL || row_number > 0x3fffffffL) return;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    if(status_pass != pass)
    {
       fprintf(stdout,"\n Pass %d: ",pass);
       status_pass = pass;
       status_dots = 30;
    }
    status_dots--;
    if(status_dots == 0)
    {
       fprintf(stdout, "\n         ");
       status_dots=30;
    }
    fprintf(stdout, "r");
}
void write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
void write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
{
75
    if(png_ptr == NULL || row_number > 0x3fffffffL || pass > 7) return;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    fprintf(stdout, "w");
}


#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
/* example of using user transform callback (we don't transform anything,
   but merely count the black pixels) */

static png_uint_32 black_pixels;
void count_black_pixels(png_structp png_ptr, png_row_infop row_info,
   png_bytep data);
void count_black_pixels(png_structp png_ptr, png_row_infop row_info,
   png_bytep data)
{
   png_bytep dp = data;
   if(png_ptr == NULL)return; 

   /* contents of row_info:
    *  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)
    */

    /* counts the number of black pixels (or zero pixels if color_type is 3 */

    if(row_info->color_type == 0 || row_info->color_type == 3)
    {
       int pos=0;
       png_uint_32 n;
109
       for (n=0; n<row_info->width; n++)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
       {
          if(row_info->bit_depth == 1)
             if(((*dp << pos++ )& 0x80) == 0) black_pixels++;
             if(pos == 8)
             {
                pos=0;
                dp++;
             }
          if(row_info->bit_depth == 2)
             if(((*dp << (pos+=2))& 0xc0) == 0) black_pixels++;
             if(pos == 8)
             {
                pos=0;
                dp++;
             }
          if(row_info->bit_depth == 4)
             if(((*dp << (pos+=4))& 0xf0) == 0) black_pixels++;
             if(pos == 8)
             {
                pos=0;
                dp++;
             }
          if(row_info->bit_depth == 8)
             if(*dp++ == 0) black_pixels++;
          if(row_info->bit_depth == 16)
          {
             if((*dp | *(dp+1)) == 0) black_pixels++;
             dp+=2;
          }
       }
    }
    else /* other color types */
    {
       png_uint_32 n;
       int channel;
       int color_channels = row_info->channels;
       if(row_info->color_type > 3)color_channels--;

       for (n=0; n<row_info->width; n++)
       {
          for (channel = 0; channel < color_channels; channel++)
          {
             if(row_info->bit_depth == 8)
                if(*dp++ == 0) black_pixels++;
             if(row_info->bit_depth == 16)
             {
                if((*dp | *(dp+1)) == 0) black_pixels++;
                dp+=2;
             }
          }
          if(row_info->color_type > 3)
          {
             dp++;
             if(row_info->bit_depth == 16)dp++;
          }
       }
    }
}
#endif /* PNG_READ|WRITE_USER_TRANSFORM_SUPPORTED */

170
static int verbose = 0;
171
static int wrote_question = 0;
172

173
#if defined(PNG_NO_STDIO)
174 175 176 177 178 179 180 181 182
/* START of code to validate stdio-free compilation */
/* 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 which 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. */
#ifndef USE_FAR_KEYWORD
static void
183 184
png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length);
static void
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
   png_size_t check;

   /* 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.
    */
   check = (png_size_t)fread(data, (png_size_t)1, length,
      (FILE *)png_ptr->io_ptr);

   if (check != length)
   {
      png_error(png_ptr, "Read Error");
   }
}
G
Guy Schalnat 已提交
200
#else
201 202 203 204 205 206 207 208
/* this is the model-independent version. Since the standard I/O library
   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)
 
209 210
static void
png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
static void
png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
   int check;
   png_byte *n_data;
   FILE *io_ptr;

   /* Check if data really is near. If so, use usual code. */
   n_data = (png_byte *)CVT_PTR_NOCHECK(data);
   io_ptr = (FILE *)CVT_PTR(png_ptr->io_ptr);
   if ((png_bytep)n_data == data)
   {
      check = fread(n_data, 1, length, io_ptr);
   }
   else
   {
      png_byte buf[NEAR_BUF_SIZE];
      png_size_t read, remaining, err;
      check = 0;
      remaining = length;
      do
      {
         read = MIN(NEAR_BUF_SIZE, remaining);
         err = fread(buf, (png_size_t)1, read, io_ptr);
         png_memcpy(data, buf, read); /* copy far buffer to near buffer */
         if(err != read)
            break;
         else
            check += err;
         data += read;
         remaining -= read;
      }
      while (remaining != 0);
   }
   if (check != length)
   {
      png_error(png_ptr, "read Error");
   }
}
250
#endif /* USE_FAR_KEYWORD */
G
Guy Schalnat 已提交
251

252 253
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
static void
254 255
png_default_flush(png_structp png_ptr);
static void
256 257 258 259 260 261 262 263
png_default_flush(png_structp png_ptr)
{
   FILE *io_ptr;
   io_ptr = (FILE *)CVT_PTR((png_ptr->io_ptr));
   if (io_ptr != NULL)
      fflush(io_ptr);
}
#endif
G
Guy Schalnat 已提交
264

265 266 267 268 269 270
/* This is the function which does the actual writing of data.  If you are
   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. */
#ifndef USE_FAR_KEYWORD
static void
271 272
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length);
static void
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
   png_uint_32 check;

   check = fwrite(data, 1, length, (FILE *)(png_ptr->io_ptr));
   if (check != length)
   {
      png_error(png_ptr, "Write Error");
   }
}
#else
/* this is the model-independent version. Since the standard I/O library
   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)

292 293
static void
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length);
294 295 296 297 298 299 300 301 302 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
static void
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
   png_uint_32 check;
   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
   FILE *io_ptr;

   /* Check if data really is near. If so, use usual code. */
   near_data = (png_byte *)CVT_PTR_NOCHECK(data);
   io_ptr = (FILE *)CVT_PTR(png_ptr->io_ptr);
   if ((png_bytep)near_data == data)
   {
      check = fwrite(near_data, 1, length, io_ptr);
   }
   else
   {
      png_byte buf[NEAR_BUF_SIZE];
      png_size_t written, remaining, err;
      check = 0;
      remaining = length;
      do
      {
         written = MIN(NEAR_BUF_SIZE, remaining);
         png_memcpy(buf, data, written); /* copy far buffer to near buffer */
         err = fwrite(buf, 1, written, io_ptr);
         if (err != written)
            break;
         else
            check += err;
         data += written;
         remaining -= written;
      }
      while (remaining != 0);
   }
   if (check != length)
   {
      png_error(png_ptr, "Write Error");
   }
}

334
#endif /* USE_FAR_KEYWORD */
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

/* 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.
 */
static void
png_default_warning(png_structp png_ptr, png_const_charp message)
{
   PNG_CONST char *name = "UNKNOWN (ERROR!)";
   if (png_ptr != NULL && png_ptr->error_ptr != NULL)
      name = png_ptr->error_ptr;
   fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
}

/* 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().
 */
static void
png_default_error(png_structp png_ptr, png_const_charp message)
{
   png_default_warning(png_ptr, message);
   /* We can return because png_error calls the default handler which is
    * actually ok in this case. */
}
362
#endif /* PNG_NO_STDIO */
363 364
/* END of code to validate stdio-free compilation */

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
/* START of code to validate memory allocation and deallocation */
#ifdef PNGTEST_MEMORY_DEBUG
/* Borland DOS special memory handler */
#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
ERROR - memory debugging is not supported on this platform
#else

/* Allocate memory.  For reasonable files, size should never exceed
   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. */
typedef struct memory_information {
   png_uint_32                    size;
   png_voidp                      pointer;
   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;

extern PNG_EXPORT(png_voidp,png_debug_malloc) PNGARG((png_structp png_ptr,
   png_uint_32 size));
extern PNG_EXPORT(void,png_debug_free) PNGARG((png_structp png_ptr,
   png_voidp ptr));

png_voidp
png_malloc(png_structp png_ptr, png_uint_32 size) {
   if (png_ptr == NULL) {
      fprintf(STDERR, "NULL pointer to memory allocator\n");
400
      return (NULL);
401 402
   }
   if (size == 0)
403
      return (png_voidp)(NULL);
404 405 406 407 408 409 410 411 412 413 414 415 416 417

   /* This calls the library allocator twice, once to get the requested
      buffer and once to get a new free list entry. */
   {
      memory_infop pinfo = png_debug_malloc(png_ptr, sizeof *pinfo);
      pinfo->size = size;
      current_allocation += size;
      if (current_allocation > maximum_allocation)
         maximum_allocation = current_allocation;
      pinfo->pointer = png_debug_malloc(png_ptr, size);
      pinfo->next = pinformation;
      pinformation = pinfo;
      /* Make sure the caller isn't assuming zeroed memory. */
      png_memset(pinfo->pointer, 0xdd, pinfo->size);
418
      return (png_voidp)(pinfo->pointer);
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
   }
}

/* Free a pointer.  It is removed from the list at the same time. */
void
png_free(png_structp png_ptr, png_voidp ptr)
{
   if (png_ptr == NULL)
      fprintf(STDERR, "NULL pointer to memory allocator\n");
   if (ptr == 0) {
#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;
      for (;;) {
         memory_infop pinfo = *ppinfo;
         if (pinfo->pointer == ptr) {
            *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
               the memory which is to be freed. */
            memset(ptr, 0x55, pinfo->size);
            png_debug_free(png_ptr, pinfo);
            break;
         }
         if (pinfo->next == NULL) {
            fprintf(STDERR, "Pointer %x not found\n", ptr);
            break;
         }
         ppinfo = &pinfo->next;
      }
   }

   /* Finally free the data. */
   png_debug_free(png_ptr, ptr);
}
#endif /* Not Borland DOS special memory handler */
#endif
/* END of code to test memory allocation/deallocation */

466
/* Test one file */
467
int test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
G
Guy Schalnat 已提交
468
{
469
   static FILE *fpin, *fpout;  /* "static" prevents setjmp corruption */
A
Andreas Dilger 已提交
470 471
   png_structp read_ptr, write_ptr;
   png_infop read_info_ptr, write_info_ptr, end_info_ptr;
G
Guy Schalnat 已提交
472
   png_bytep row_buf;
G
Guy Schalnat 已提交
473
   png_uint_32 y;
A
Andreas Dilger 已提交
474 475 476
   png_uint_32 width, height;
   int num_pass, pass;
   int bit_depth, color_type;
A
Andreas Dilger 已提交
477 478 479
#ifdef USE_FAR_KEYWORD
   jmp_buf jmpbuf;
#endif   
480 481
   
   char inbuf[256], outbuf[256];
G
Guy Schalnat 已提交
482

483
   row_buf = (png_bytep)NULL;
G
Guy Schalnat 已提交
484

A
Andreas Dilger 已提交
485
   if ((fpin = fopen(inname, "rb")) == NULL)
G
Guy Schalnat 已提交
486 487
   {
      fprintf(STDERR, "Could not find input file %s\n", inname);
488
      return (1);
G
Guy Schalnat 已提交
489 490
   }

A
Andreas Dilger 已提交
491
   if ((fpout = fopen(outname, "wb")) == NULL)
G
Guy Schalnat 已提交
492
   {
G
Guy Schalnat 已提交
493
      fprintf(STDERR, "Could not open output file %s\n", outname);
G
Guy Schalnat 已提交
494
      fclose(fpin);
495
      return (1);
G
Guy Schalnat 已提交
496 497
   }

A
Andreas Dilger 已提交
498
   png_debug(0, "Allocating read and write structures\n");
A
Andreas Dilger 已提交
499
   read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
A
Andreas Dilger 已提交
500
      (png_error_ptr)NULL, (png_error_ptr)NULL);
501
#if defined(PNG_NO_STDIO)
502 503
   png_set_error_fn(read_ptr, (png_voidp)inname, png_default_error,
       png_default_warning);
504
#endif
A
Andreas Dilger 已提交
505
   write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
G
Guy Schalnat 已提交
506
      (png_error_ptr)NULL, (png_error_ptr)NULL);
507
#if defined(PNG_NO_STDIO)
508 509
   png_set_error_fn(write_ptr, (png_voidp)inname, png_default_error,
       png_default_warning);
510
#endif
A
Andreas Dilger 已提交
511 512
   png_debug(0, "Allocating read_info, write_info and end_info structures\n");
   read_info_ptr = png_create_info_struct(read_ptr);
513
   write_info_ptr = png_create_info_struct(write_ptr);
A
Andreas Dilger 已提交
514
   end_info_ptr = png_create_info_struct(read_ptr);
G
Guy Schalnat 已提交
515

A
Andreas Dilger 已提交
516
   png_debug(0, "Setting jmpbuf for read struct\n");
A
Andreas Dilger 已提交
517 518 519
#ifdef USE_FAR_KEYWORD
   if (setjmp(jmpbuf))
#else
G
Guy Schalnat 已提交
520
   if (setjmp(read_ptr->jmpbuf))
A
Andreas Dilger 已提交
521
#endif
G
Guy Schalnat 已提交
522
   {
523
      fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
A
Andreas Dilger 已提交
524 525
      png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
      png_destroy_write_struct(&write_ptr, &write_info_ptr);
G
Guy Schalnat 已提交
526 527
      fclose(fpin);
      fclose(fpout);
528
      return (1);
G
Guy Schalnat 已提交
529
   }
A
Andreas Dilger 已提交
530 531

   png_debug(0, "Setting jmpbuf for write struct\n");
A
Andreas Dilger 已提交
532 533 534 535
#ifdef USE_FAR_KEYWORD
   png_memcpy(read_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
   if (setjmp(jmpbuf))
#else
G
Guy Schalnat 已提交
536
   if (setjmp(write_ptr->jmpbuf))
A
Andreas Dilger 已提交
537
#endif
G
Guy Schalnat 已提交
538
   {
539
      fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
A
Andreas Dilger 已提交
540 541
      png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
      png_destroy_write_struct(&write_ptr, &write_info_ptr);
G
Guy Schalnat 已提交
542 543
      fclose(fpin);
      fclose(fpout);
544
      return (1);
G
Guy Schalnat 已提交
545
   }
A
Andreas Dilger 已提交
546

A
Andreas Dilger 已提交
547 548 549
#ifdef USE_FAR_KEYWORD
   png_memcpy(write_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
#endif
A
Andreas Dilger 已提交
550
   png_debug(0, "Initializing input and output streams\n");
551
#if !defined(PNG_NO_STDIO)
G
Guy Schalnat 已提交
552 553
   png_init_io(read_ptr, fpin);
   png_init_io(write_ptr, fpout);
554 555 556 557 558 559 560 561
#else
   png_set_read_fn(read_ptr, (png_voidp)fpin, png_default_read_data);
   png_set_write_fn(write_ptr, (png_voidp)fpout,  png_default_write_data,
#if defined(PNG_WRITE_FLUSH_SUPPORTED)
      png_default_flush);
#else
      NULL);
#endif
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
#endif
   if(status_dots_requested == 1)
   {
      png_set_write_status_fn(write_ptr, write_row_callback);
      png_set_read_status_fn(read_ptr, read_row_callback);
   }
   else
   {
      png_set_write_status_fn(write_ptr, NULL);
      png_set_read_status_fn(read_ptr, NULL);
   }

#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
   black_pixels=0;
   png_set_write_user_transform_fn(write_ptr, count_black_pixels);
578
#endif
G
Guy Schalnat 已提交
579

A
Andreas Dilger 已提交
580 581
   png_debug(0, "Reading info struct\n");
   png_read_info(read_ptr, read_info_ptr);
G
Guy Schalnat 已提交
582

A
Andreas Dilger 已提交
583 584 585
   png_debug(0, "Transferring info struct\n");
   {
      int interlace_type, compression_type, filter_type;
G
Guy Schalnat 已提交
586

A
Andreas Dilger 已提交
587 588 589 590
      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,
591
#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
A
Andreas Dilger 已提交
592
            color_type, interlace_type, compression_type, filter_type);
593 594 595
#else
            color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
#endif
A
Andreas Dilger 已提交
596 597 598
      }
   }
#if defined(PNG_READ_bKGD_SUPPORTED) && defined(PNG_WRITE_bKGD_SUPPORTED)
G
Guy Schalnat 已提交
599
   {
A
Andreas Dilger 已提交
600 601 602 603 604 605
      png_color_16p background;

      if (png_get_bKGD(read_ptr, read_info_ptr, &background))
      {
         png_set_bKGD(write_ptr, write_info_ptr, background);
      }
G
Guy Schalnat 已提交
606
   }
A
Andreas Dilger 已提交
607 608 609 610
#endif
#if defined(PNG_READ_cHRM_SUPPORTED) && defined(PNG_WRITE_cHRM_SUPPORTED)
   {
      double white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
G
Guy Schalnat 已提交
611

A
Andreas Dilger 已提交
612 613 614 615 616 617 618 619 620
      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
#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_WRITE_gAMA_SUPPORTED)
G
Guy Schalnat 已提交
621
   {
A
Andreas Dilger 已提交
622 623 624 625 626 627
      double gamma;

      if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
      {
         png_set_gAMA(write_ptr, write_info_ptr, gamma);
      }
G
Guy Schalnat 已提交
628
   }
629 630 631
#endif
#if defined(PNG_READ_sRGB_SUPPORTED) && defined(PNG_WRITE_sRGB_SUPPORTED)
   {
632
      int intent;
633 634 635 636 637 638

      if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
      {
         png_set_sRGB(write_ptr, write_info_ptr, intent);
      }
   }
A
Andreas Dilger 已提交
639 640
#endif
#if defined(PNG_READ_hIST_SUPPORTED) && defined(PNG_WRITE_hIST_SUPPORTED)
G
Guy Schalnat 已提交
641
   {
A
Andreas Dilger 已提交
642 643 644 645 646 647
      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 已提交
648
   }
A
Andreas Dilger 已提交
649 650 651 652 653
#endif
#if defined(PNG_READ_oFFs_SUPPORTED) && defined(PNG_WRITE_oFFs_SUPPORTED)
   {
      png_uint_32 offset_x, offset_y;
      int unit_type;
G
Guy Schalnat 已提交
654

A
Andreas Dilger 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
      if (png_get_oFFs(read_ptr, read_info_ptr,&offset_x,&offset_y,&unit_type))
      {
         png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
      }
   }
#endif
#if defined(PNG_READ_pCAL_SUPPORTED) && defined(PNG_WRITE_pCAL_SUPPORTED)
   {
      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
#if defined(PNG_READ_pHYs_SUPPORTED) && defined(PNG_WRITE_pHYs_SUPPORTED)
   {
      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
   {
      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);
      }
   }
#if defined(PNG_READ_sBIT_SUPPORTED) && defined(PNG_WRITE_sBIT_SUPPORTED)
G
Guy Schalnat 已提交
697
   {
A
Andreas Dilger 已提交
698 699 700
      png_color_8p sig_bit;

      if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
G
Guy Schalnat 已提交
701
      {
A
Andreas Dilger 已提交
702 703 704
         png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
      }
   }
G
Guy Schalnat 已提交
705
#endif
A
Andreas Dilger 已提交
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
#if (defined(PNG_READ_tEXt_SUPPORTED) && defined(PNG_WRITE_tEXt_SUPPORTED)) || \
    (defined(PNG_READ_zTXt_SUPPORTED) && defined(PNG_WRITE_zTXt_SUPPORTED))
   {
      png_textp text_ptr;
      int num_text;

      if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
      {
         png_debug1(0, "Handling %d tEXt/zTXt chunks\n", num_text);
         png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
      }
   }
#endif
#if defined(PNG_READ_tIME_SUPPORTED) && defined(PNG_WRITE_tIME_SUPPORTED)
   {
      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);
      }
   }
#endif
#if defined(PNG_READ_tRNS_SUPPORTED) && defined(PNG_WRITE_tRNS_SUPPORTED)
   {
      png_bytep trans;
      int num_trans;
      png_color_16p trans_values;

      if (png_get_tRNS(read_ptr, read_info_ptr, &trans, &num_trans,
         &trans_values))
      {
         png_set_tRNS(write_ptr, write_info_ptr, trans, num_trans,
            trans_values);
      }
   }
#endif

   png_debug(0, "\nWriting info struct\n");
   png_write_info(write_ptr, write_info_ptr);

747
   png_debug(0, "\nAllocating row buffer \n");
A
Andreas Dilger 已提交
748 749 750 751 752 753 754 755 756
   row_buf = (png_bytep)png_malloc(read_ptr, 
      png_get_rowbytes(read_ptr, read_info_ptr));
   if (row_buf == NULL)
   {
      fprintf(STDERR, "No memory to allocate row buffer\n");
      png_destroy_read_struct(&read_ptr, &read_info_ptr, (png_infopp)NULL);
      png_destroy_write_struct(&write_ptr, &write_info_ptr);
      fclose(fpin);
      fclose(fpout);
757
      return (1);
A
Andreas Dilger 已提交
758
   }
759
   png_debug(0, "Writing row data\n");
A
Andreas Dilger 已提交
760 761 762 763 764 765

   num_pass = png_set_interlace_handling(read_ptr);
   png_set_interlace_handling(write_ptr);

   for (pass = 0; pass < num_pass; pass++)
   {
766
      png_debug1(0, "Writing row data for pass %d\n",pass);
A
Andreas Dilger 已提交
767 768 769
      for (y = 0; y < height; y++)
      {
         png_read_rows(read_ptr, (png_bytepp)&row_buf, (png_bytepp)NULL, 1);
G
Guy Schalnat 已提交
770
         png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
G
Guy Schalnat 已提交
771 772 773
      }
   }

A
Andreas Dilger 已提交
774 775 776
   png_debug(0, "Reading and writing end_info data\n");
   png_read_end(read_ptr, end_info_ptr);
   png_write_end(write_ptr, end_info_ptr);
777 778 779 780 781 782 783 784 785 786 787
 
#ifdef PNG_EASY_ACCESS_SUPPORTED
   if(verbose)
   {
      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);
      fprintf(STDERR, "Image width = %lu, height = %lu\n",
         iwidth, iheight);
   }
#endif
G
Guy Schalnat 已提交
788

A
Andreas Dilger 已提交
789
   png_debug(0, "Destroying data structs\n");
790
   png_free(read_ptr, row_buf);
A
Andreas Dilger 已提交
791 792
   png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
   png_destroy_write_struct(&write_ptr, &write_info_ptr);
G
Guy Schalnat 已提交
793 794 795 796

   fclose(fpin);
   fclose(fpout);

A
Andreas Dilger 已提交
797 798
   png_debug(0, "Opening files for comparison\n");
   if ((fpin = fopen(inname, "rb")) == NULL)
G
Guy Schalnat 已提交
799
   {
G
Guy Schalnat 已提交
800
      fprintf(STDERR, "Could not find file %s\n", inname);
801
      return (1);
G
Guy Schalnat 已提交
802 803
   }

A
Andreas Dilger 已提交
804
   if ((fpout = fopen(outname, "rb")) == NULL)
G
Guy Schalnat 已提交
805
   {
G
Guy Schalnat 已提交
806
      fprintf(STDERR, "Could not find file %s\n", outname);
G
Guy Schalnat 已提交
807
      fclose(fpin);
808
      return (1);
G
Guy Schalnat 已提交
809
   }
A
Andreas Dilger 已提交
810

G
Guy Schalnat 已提交
811 812
   while (1)
   {
A
Andreas Dilger 已提交
813
      png_size_t num_in, num_out;
G
Guy Schalnat 已提交
814

A
Andreas Dilger 已提交
815 816
      num_in = fread(inbuf, 1, 1, fpin);
      num_out = fread(outbuf, 1, 1, fpout);
G
Guy Schalnat 已提交
817 818 819

      if (num_in != num_out)
      {
G
Guy Schalnat 已提交
820 821
         fprintf(STDERR, "Files %s and %s are of a different size\n",
                 inname, outname);
822 823 824 825 826 827 828 829 830 831 832
         if(wrote_question == 0)
         {
            fprintf(STDERR,
              "   Was %s written with the same chunk size (8k),",inname);
            fprintf(STDERR,
              " filtering\n   heuristic (libpng default), compression");
            fprintf(STDERR,
              " level (zlib default)\n   and zlib version (%s)?\n\n",
              ZLIB_VERSION);
            wrote_question=1;
         }
G
Guy Schalnat 已提交
833 834
         fclose(fpin);
         fclose(fpout);
835
         return (0);
G
Guy Schalnat 已提交
836 837 838 839 840
      }

      if (!num_in)
         break;

A
Andreas Dilger 已提交
841
      if (png_memcmp(inbuf, outbuf, num_in))
G
Guy Schalnat 已提交
842
      {
G
Guy Schalnat 已提交
843
         fprintf(STDERR, "Files %s and %s are different\n", inname, outname);
844 845 846 847 848 849 850 851 852 853 854
         if(wrote_question == 0)
         {
            fprintf(STDERR,
              "   Was %s written with the same chunk size (8k),",inname);
            fprintf(STDERR,
              " filtering\n   heuristic (libpng default), compression");
            fprintf(STDERR,
              " level (zlib default)\n   and zlib version (%s)?\n\n",
              ZLIB_VERSION);
            wrote_question=1;
         }
G
Guy Schalnat 已提交
855 856
         fclose(fpin);
         fclose(fpout);
857
         return (0);
G
Guy Schalnat 已提交
858 859 860 861 862
      }
   }

   fclose(fpin);
   fclose(fpout);
G
Guy Schalnat 已提交
863

864
   return (0);
G
Guy Schalnat 已提交
865
}
G
Guy Schalnat 已提交
866

867 868
/* input and output filenames */
#ifdef RISCOS
869 870
PNG_CONST char *inname = "pngtest/png";
PNG_CONST char *outname = "pngout/png";
871
#else
872 873
static PNG_CONST char *inname = "pngtest.png";
static PNG_CONST char *outname = "pngout.png";
874 875 876 877 878 879 880 881 882
#endif

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

   fprintf(STDERR, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
883 884 885 886 887 888 889 890 891 892 893 894 895
   fprintf(STDERR, "   with zlib   version %s\n", ZLIB_VERSION);

   /* Do some consistency checking on the memory allocation settings, I'm
      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 */
   #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
      fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
   #endif
   /* I think the following can happen. */
   #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
      fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
   #endif
896 897 898 899 900 901 902 903 904 905 906 907

   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)
   {
908
      if (strcmp(argv[1], "-m") == 0)
909
      {
910
         multiple = 1;
911 912
         status_dots_requested = 0;
      }
913 914 915 916 917
      else if (strcmp(argv[1], "-mv") == 0 ||
               strcmp(argv[1], "-vm") == 0 )
      {
         multiple = 1;
         verbose = 1;
918
         status_dots_requested = 1;
919 920 921 922
      }
      else if (strcmp(argv[1], "-v") == 0)
      {
         verbose = 1;
923
         status_dots_requested = 1;
924 925 926
         inname = argv[2];
      }
      else
927
      {
928
         inname = argv[1];
929 930
         status_dots_requested = 0;
      }
931 932
   }

933 934
   if (!multiple && argc == 3+verbose)
     outname = argv[2+verbose];
935

936
   if ((!multiple && argc > 3+verbose) || (multiple && argc < 2))
937
   {
938 939
     fprintf(STDERR,
       "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
940
        argv[0], argv[0]);
941 942 943 944
     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);
945 946 947 948 949 950
     exit(1);
   }

   if (multiple)
   {
      int i;
951 952 953
#ifdef PNGTEST_MEMORY_DEBUG
      int allocation_now = current_allocation;
#endif
954
      for (i=2; i<argc; ++i)
955
      {
956 957 958
         int kerror;
         fprintf(STDERR, "Testing %s:",argv[i]);
         kerror = test_one_file(argv[i], outname);
959
         if (kerror == 0) 
960 961 962 963
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
            fprintf(STDERR, " PASS (%lu black pixels)\n",black_pixels);
#else
964
            fprintf(STDERR, " PASS\n");
965
#endif
966 967 968 969
         else {
            fprintf(STDERR, " FAIL\n");
            ierror += kerror;
            }
970 971 972 973 974 975 976 977 978 979 980 981 982
#ifdef PNGTEST_MEMORY_DEBUG
         if (allocation_now != current_allocation)
            fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
               current_allocation-allocation_now);
         if (current_allocation != 0) {
            memory_infop pinfo = pinformation;

            fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
               current_allocation);
            while (pinfo != NULL) {
               fprintf(STDERR, " %d bytes at %x\n", pinfo->size, pinfo->pointer);
               pinfo = pinfo->next;
               }
983
         }
984 985 986 987 988 989
#endif
      }
#ifdef PNGTEST_MEMORY_DEBUG
         fprintf(STDERR, "Maximum memory allocation: %d bytes\n",
            maximum_allocation);
#endif
990 991 992
   }
   else
   {
993 994 995
      int i;
      for (i=0; i<3; ++i) {
         int kerror;
996 997 998
#ifdef PNGTEST_MEMORY_DEBUG
         int allocation_now = current_allocation;
#endif
999 1000
         if (i == 1) status_dots_requested = 1;
         else if(verbose == 0)status_dots_requested = 0;
1001 1002
         if (i == 0 || verbose == 1 || ierror != 0)
            fprintf(STDERR, "Testing %s:",inname);
1003
         kerror = test_one_file(inname, outname);
1004 1005
         if(kerror == 0)
         {
1006 1007 1008 1009 1010 1011 1012
            if(verbose == 1 || i == 2)
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
                fprintf(STDERR, " PASS (%lu black pixels)\n",black_pixels);
#else
                fprintf(STDERR, " PASS\n");
#endif
1013 1014 1015 1016 1017
         }
         else
         {
            if(verbose == 0 && i != 2)
               fprintf(STDERR, "Testing %s:",inname);
1018 1019
            fprintf(STDERR, " FAIL\n");
            ierror += kerror;
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
         }
#ifdef PNGTEST_MEMORY_DEBUG
         if (allocation_now != current_allocation)
             fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
               current_allocation-allocation_now);
         if (current_allocation != 0) {
             memory_infop pinfo = pinformation;
   
             fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
                current_allocation);
             while (pinfo != NULL) {
                fprintf(STDERR, " %d bytes at %x\n", pinfo->size, pinfo->pointer);
                pinfo = pinfo->next;
             }
          }
#endif
1036
       }
1037 1038 1039 1040
#ifdef PNGTEST_MEMORY_DEBUG
       fprintf(STDERR, "Maximum memory allocation: %d bytes\n",
          maximum_allocation);
#endif
1041 1042 1043 1044 1045 1046
   }

   if (ierror == 0)
      fprintf(STDERR, "libpng passes test\n");
   else
      fprintf(STDERR, "libpng FAILS test\n");
1047
   return (int)(ierror != 0);
1048
}