Initial revision
[rrdtool.git] / libraries / libpng-1.0.9 / pngtest.c
1
2 /* pngtest.c - a simple test program to test libpng
3  *
4  * libpng 1.0.9 - January 31, 2001
5  * For conditions of distribution and use, see copyright notice in png.h
6  * Copyright (c) 1998-2001 Glenn Randers-Pehrson
7  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9  *
10  * This program reads in a PNG image, writes it out again, and then
11  * compares the two files.  If the files are identical, this shows that
12  * the basic chunk handling, filtering, and (de)compression code is working
13  * properly.  It does not currently test all of the transforms, although
14  * it probably should.
15  *
16  * The program will report "FAIL" in certain legitimate cases:
17  * 1) when the compression level or filter selection method is changed.
18  * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
19  * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
20  *    exist in the input file.
21  * 4) others not listed here...
22  * In these cases, it is best to check with another tool such as "pngcheck"
23  * to see what the differences between the two files are.
24  *
25  * If a filename is given on the command-line, then this file is used
26  * for the input, rather than the default "pngtest.png".  This allows
27  * testing a wide variety of files easily.  You can also test a number
28  * of files at once by typing "pngtest -m file1.png file2.png ..."
29  */
30
31 #if defined(_WIN32_WCE)
32 #  if _WIN32_WCE < 211
33      __error__ (f|w)printf functions are not supported on old WindowsCE.;
34 #  endif
35 #  include <windows.h>
36 #  include <stdlib.h>
37 #  define READFILE(file, data, length, check) \
38      if (ReadFile(file, data, length, &check,NULL)) check = 0
39 #  define WRITEFILE(file, data, length, check)) \
40      if (WriteFile(file, data, length, &check, NULL)) check = 0
41 #  define FCLOSE(file) CloseHandle(file)
42 #else
43 #  include <stdio.h>
44 #  include <stdlib.h>
45 #  include <assert.h>
46 #  define READFILE(file, data, length, check) \
47      check=(png_size_t)fread(data,(png_size_t)1,length,file)
48 #  define WRITEFILE(file, data, length, check) \
49      check=(png_size_t)fwrite(data,(png_size_t)1, length, file)
50 #  define FCLOSE(file) fclose(file)
51 #endif
52
53 #if defined(PNG_NO_STDIO)
54 #if defined(_WIN32_WCE)
55 typedef HANDLE                png_FILE_p;
56 #else
57 typedef FILE                * png_FILE_p;
58 #endif
59 #endif
60
61 /* Makes pngtest verbose so we can find problems (needs to be before png.h) */
62 #ifndef PNG_DEBUG
63 #define PNG_DEBUG 0
64 #endif
65
66 /* Turn on CPU timing
67 #define PNGTEST_TIMING
68 */
69
70 #ifdef PNG_NO_FLOATING_POINT_SUPPORTED
71 #undef PNGTEST_TIMING
72 #endif
73
74 #ifdef PNGTEST_TIMING
75 static float t_start, t_stop, t_decode, t_encode, t_misc;
76 #include <time.h>
77 #endif
78
79 #include "png.h"
80
81 /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
82 #ifndef png_jmpbuf
83 #  define png_jmpbuf(png_ptr) png_ptr->jmpbuf
84 #endif
85
86 #ifdef PNGTEST_TIMING
87 static float t_start, t_stop, t_decode, t_encode, t_misc;
88 #if !defined(PNG_tIME_SUPPORTED)
89 #include <time.h>
90 #endif
91 #endif
92
93 #if defined(PNG_TIME_RFC1123_SUPPORTED)
94 static int tIME_chunk_present=0;
95 static char tIME_string[30] = "no tIME chunk present in file";
96 #endif
97
98 static int verbose = 0;
99
100 int test_one_file PNGARG((PNG_CONST char *inname, PNG_CONST char *outname));
101
102 #ifdef __TURBOC__
103 #include <mem.h>
104 #endif
105
106 /* defined so I can write to a file on gui/windowing platforms */
107 /*  #define STDERR stderr  */
108 #define STDERR stdout   /* for DOS */
109
110 /* example of using row callbacks to make a simple progress meter */
111 static int status_pass=1;
112 static int status_dots_requested=0;
113 static int status_dots=1;
114
115 void
116 read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
117 void
118 read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
119 {
120     if(png_ptr == NULL || row_number > PNG_MAX_UINT) return;
121     if(status_pass != pass)
122     {
123        fprintf(stdout,"\n Pass %d: ",pass);
124        status_pass = pass;
125        status_dots = 31;
126     }
127     status_dots--;
128     if(status_dots == 0)
129     {
130        fprintf(stdout, "\n         ");
131        status_dots=30;
132     }
133     fprintf(stdout, "r");
134 }
135
136 void
137 write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
138 void
139 write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
140 {
141     if(png_ptr == NULL || row_number > PNG_MAX_UINT || pass > 7) return;
142     fprintf(stdout, "w");
143 }
144
145
146 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
147 /* Example of using user transform callback (we don't transform anything,
148    but merely examine the row filters.  We set this to 256 rather than
149    5 in case illegal filter values are present.) */
150 static png_uint_32 filters_used[256];
151 void
152 count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data);
153 void
154 count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
155 {
156     if(png_ptr != NULL && row_info != NULL)
157       ++filters_used[*(data-1)];
158 }
159 #endif
160
161 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
162 /* example of using user transform callback (we don't transform anything,
163    but merely count the zero samples) */
164
165 static png_uint_32 zero_samples;
166
167 void
168 count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data);
169 void
170 count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
171 {
172    png_bytep dp = data;
173    if(png_ptr == NULL)return;
174
175    /* contents of row_info:
176     *  png_uint_32 width      width of row
177     *  png_uint_32 rowbytes   number of bytes in row
178     *  png_byte color_type    color type of pixels
179     *  png_byte bit_depth     bit depth of samples
180     *  png_byte channels      number of channels (1-4)
181     *  png_byte pixel_depth   bits per pixel (depth*channels)
182     */
183
184
185     /* counts the number of zero samples (or zero pixels if color_type is 3 */
186
187     if(row_info->color_type == 0 || row_info->color_type == 3)
188     {
189        int pos=0;
190        png_uint_32 n, nstop;
191        for (n=0, nstop=row_info->width; n<nstop; n++)
192        {
193           if(row_info->bit_depth == 1)
194           {
195              if(((*dp << pos++ ) & 0x80) == 0) zero_samples++;
196              if(pos == 8)
197              {
198                 pos = 0;
199                 dp++;
200              }
201           }
202           if(row_info->bit_depth == 2)
203           {
204              if(((*dp << (pos+=2)) & 0xc0) == 0) zero_samples++;
205              if(pos == 8)
206              {
207                 pos = 0;
208                 dp++;
209              }
210           }
211           if(row_info->bit_depth == 4)
212           {
213              if(((*dp << (pos+=4)) & 0xf0) == 0) zero_samples++;
214              if(pos == 8)
215              {
216                 pos = 0;
217                 dp++;
218              }
219           }
220           if(row_info->bit_depth == 8)
221              if(*dp++ == 0) zero_samples++;
222           if(row_info->bit_depth == 16)
223           {
224              if((*dp | *(dp+1)) == 0) zero_samples++;
225              dp+=2;
226           }
227        }
228     }
229     else /* other color types */
230     {
231        png_uint_32 n, nstop;
232        int channel;
233        int color_channels = row_info->channels;
234        if(row_info->color_type > 3)color_channels--;
235
236        for (n=0, nstop=row_info->width; n<nstop; n++)
237        {
238           for (channel = 0; channel < color_channels; channel++)
239           {
240              if(row_info->bit_depth == 8)
241                 if(*dp++ == 0) zero_samples++;
242              if(row_info->bit_depth == 16)
243              {
244                 if((*dp | *(dp+1)) == 0) zero_samples++;
245                 dp+=2;
246              }
247           }
248           if(row_info->color_type > 3)
249           {
250              dp++;
251              if(row_info->bit_depth == 16)dp++;
252           }
253        }
254     }
255 }
256 #endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
257
258 static int wrote_question = 0;
259
260 #if defined(PNG_NO_STDIO)
261 /* START of code to validate stdio-free compilation */
262 /* These copies of the default read/write functions come from pngrio.c and */
263 /* pngwio.c.  They allow "don't include stdio" testing of the library. */
264 /* This is the function that does the actual reading of data.  If you are
265    not reading from a standard C stream, you should create a replacement
266    read_data function and use it at run time with png_set_read_fn(), rather
267    than changing the library. */
268
269 #ifndef USE_FAR_KEYWORD
270 static void
271 pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
272 {
273    png_size_t check;
274
275    /* fread() returns 0 on error, so it is OK to store this in a png_size_t
276     * instead of an int, which is what fread() actually returns.
277     */
278    READFILE((png_FILE_p)png_ptr->io_ptr, data, length, check);
279
280    if (check != length)
281    {
282       png_error(png_ptr, "Read Error");
283    }
284 }
285 #else
286 /* this is the model-independent version. Since the standard I/O library
287    can't handle far buffers in the medium and small models, we have to copy
288    the data.
289 */
290
291 #define NEAR_BUF_SIZE 1024
292 #define MIN(a,b) (a <= b ? a : b)
293
294 static void
295 pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
296 {
297    int check;
298    png_byte *n_data;
299    png_FILE_p io_ptr;
300
301    /* Check if data really is near. If so, use usual code. */
302    n_data = (png_byte *)CVT_PTR_NOCHECK(data);
303    io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
304    if ((png_bytep)n_data == data)
305    {
306       READFILE(io_ptr, n_data, length, check);
307    }
308    else
309    {
310       png_byte buf[NEAR_BUF_SIZE];
311       png_size_t read, remaining, err;
312       check = 0;
313       remaining = length;
314       do
315       {
316          read = MIN(NEAR_BUF_SIZE, remaining);
317          READFILE(io_ptr, buf, 1, err);
318          png_memcpy(data, buf, read); /* copy far buffer to near buffer */
319          if(err != read)
320             break;
321          else
322             check += err;
323          data += read;
324          remaining -= read;
325       }
326       while (remaining != 0);
327    }
328    if (check != length)
329    {
330       png_error(png_ptr, "read Error");
331    }
332 }
333 #endif /* USE_FAR_KEYWORD */
334
335 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
336 static void
337 pngtest_flush(png_structp png_ptr)
338 {
339 #if !defined(_WIN32_WCE)
340    png_FILE_p io_ptr;
341    io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
342    if (io_ptr != NULL)
343       fflush(io_ptr);
344 #endif
345 }
346 #endif
347
348 /* This is the function that does the actual writing of data.  If you are
349    not writing to a standard C stream, you should create a replacement
350    write_data function and use it at run time with png_set_write_fn(), rather
351    than changing the library. */
352 #ifndef USE_FAR_KEYWORD
353 static void
354 pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
355 {
356    png_uint_32 check;
357
358    WRITEFILE((png_FILE_p)png_ptr->io_ptr,  data, length, check);
359    if (check != length)
360    {
361       png_error(png_ptr, "Write Error");
362    }
363 }
364 #else
365 /* this is the model-independent version. Since the standard I/O library
366    can't handle far buffers in the medium and small models, we have to copy
367    the data.
368 */
369
370 #define NEAR_BUF_SIZE 1024
371 #define MIN(a,b) (a <= b ? a : b)
372
373 static void
374 pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
375 {
376    png_uint_32 check;
377    png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
378    png_FILE_p io_ptr;
379
380    /* Check if data really is near. If so, use usual code. */
381    near_data = (png_byte *)CVT_PTR_NOCHECK(data);
382    io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
383    if ((png_bytep)near_data == data)
384    {
385       WRITEFILE(io_ptr, near_data, length, check);
386    }
387    else
388    {
389       png_byte buf[NEAR_BUF_SIZE];
390       png_size_t written, remaining, err;
391       check = 0;
392       remaining = length;
393       do
394       {
395          written = MIN(NEAR_BUF_SIZE, remaining);
396          png_memcpy(buf, data, written); /* copy far buffer to near buffer */
397          WRITEFILE(io_ptr, buf, written, err);
398          if (err != written)
399             break;
400          else
401             check += err;
402          data += written;
403          remaining -= written;
404       }
405       while (remaining != 0);
406    }
407    if (check != length)
408    {
409       png_error(png_ptr, "Write Error");
410    }
411 }
412
413 #endif /* USE_FAR_KEYWORD */
414
415 /* This function is called when there is a warning, but the library thinks
416  * it can continue anyway.  Replacement functions don't have to do anything
417  * here if you don't want to.  In the default configuration, png_ptr is
418  * not used, but it is passed in case it may be useful.
419  */
420 static void
421 pngtest_warning(png_structp png_ptr, png_const_charp message)
422 {
423    PNG_CONST char *name = "UNKNOWN (ERROR!)";
424    if (png_ptr != NULL && png_ptr->error_ptr != NULL)
425       name = png_ptr->error_ptr;
426    fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
427 }
428
429 /* This is the default error handling function.  Note that replacements for
430  * this function MUST NOT RETURN, or the program will likely crash.  This
431  * function is used by default, or if the program supplies NULL for the
432  * error function pointer in png_set_error_fn().
433  */
434 static void
435 pngtest_error(png_structp png_ptr, png_const_charp message)
436 {
437    pngtest_warning(png_ptr, message);
438    /* We can return because png_error calls the default handler, which is
439     * actually OK in this case. */
440 }
441 #endif /* PNG_NO_STDIO */
442 /* END of code to validate stdio-free compilation */
443
444 /* START of code to validate memory allocation and deallocation */
445 #ifdef PNG_USER_MEM_SUPPORTED
446
447 /* Allocate memory.  For reasonable files, size should never exceed
448    64K.  However, zlib may allocate more then 64K if you don't tell
449    it not to.  See zconf.h and png.h for more information.  zlib does
450    need to allocate exactly 64K, so whatever you call here must
451    have the ability to do that.
452
453    This piece of code can be compiled to validate max 64K allocations
454    by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K. */
455 typedef struct memory_information
456 {
457    png_uint_32               size;
458    png_voidp                 pointer;
459    struct memory_information FAR *next;
460 } memory_information;
461 typedef memory_information FAR *memory_infop;
462
463 static memory_infop pinformation = NULL;
464 static int current_allocation = 0;
465 static int maximum_allocation = 0;
466 static int total_allocation = 0;
467 static int num_allocations = 0;
468
469 extern PNG_EXPORT(png_voidp,png_debug_malloc) PNGARG((png_structp png_ptr,
470    png_uint_32 size));
471 extern PNG_EXPORT(void,png_debug_free) PNGARG((png_structp png_ptr,
472    png_voidp ptr));
473
474 png_voidp
475 png_debug_malloc(png_structp png_ptr, png_uint_32 size)
476 {
477
478    /* png_malloc has already tested for NULL; png_create_struct calls
479       png_debug_malloc directly, with png_ptr == NULL which is OK */
480
481    if (size == 0)
482       return (png_voidp)(NULL);
483
484    /* This calls the library allocator twice, once to get the requested
485       buffer and once to get a new free list entry. */
486    {
487       memory_infop pinfo = png_malloc_default(png_ptr, sizeof *pinfo);
488       pinfo->size = size;
489       current_allocation += size;
490       total_allocation += size;
491       num_allocations ++;
492       if (current_allocation > maximum_allocation)
493          maximum_allocation = current_allocation;
494       pinfo->pointer = png_malloc_default(png_ptr, size);
495       pinfo->next = pinformation;
496       pinformation = pinfo;
497       /* Make sure the caller isn't assuming zeroed memory. */
498       png_memset(pinfo->pointer, 0xdd, pinfo->size);
499 #if PNG_DEBUG
500       if(verbose)
501          printf("png_malloc %d bytes at %x\n",size,pinfo->pointer);
502 #endif
503       assert(pinfo->size != 12345678);
504       return (png_voidp)(pinfo->pointer);
505    }
506 }
507
508 /* Free a pointer.  It is removed from the list at the same time. */
509 void
510 png_debug_free(png_structp png_ptr, png_voidp ptr)
511 {
512    if (png_ptr == NULL)
513       fprintf(STDERR, "NULL pointer to png_debug_free.\n");
514    if (ptr == 0)
515    {
516 #if 0 /* This happens all the time. */
517       fprintf(STDERR, "WARNING: freeing NULL pointer\n");
518 #endif
519       return;
520    }
521
522    /* Unlink the element from the list. */
523    {
524       memory_infop FAR *ppinfo = &pinformation;
525       for (;;)
526       {
527          memory_infop pinfo = *ppinfo;
528          if (pinfo->pointer == ptr)
529          {
530             *ppinfo = pinfo->next;
531             current_allocation -= pinfo->size;
532             if (current_allocation < 0)
533                fprintf(STDERR, "Duplicate free of memory\n");
534             /* We must free the list element too, but first kill
535                the memory that is to be freed. */
536             memset(ptr, 0x55, pinfo->size);
537             png_free_default(png_ptr, pinfo);
538             pinfo=NULL;
539             break;
540          }
541          if (pinfo->next == NULL)
542          {
543             fprintf(STDERR, "Pointer %x not found\n", ptr);
544             break;
545          }
546          ppinfo = &pinfo->next;
547       }
548    }
549
550    /* Finally free the data. */
551 #if PNG_DEBUG
552    if(verbose)
553       printf("Freeing %x\n",ptr);
554 #endif
555    png_free_default(png_ptr, ptr);
556    ptr=NULL;
557 }
558 #endif /* PNG_USER_MEM_SUPPORTED */
559 /* END of code to test memory allocation/deallocation */
560
561 /* Test one file */
562 int
563 test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
564 {
565    static png_FILE_p fpin;
566    static png_FILE_p fpout;  /* "static" prevents setjmp corruption */
567    png_structp read_ptr, write_ptr;
568    png_infop read_info_ptr, write_info_ptr, end_info_ptr, write_end_info_ptr;
569    png_bytep row_buf;
570    png_uint_32 y;
571    png_uint_32 width, height;
572    int num_pass, pass;
573    int bit_depth, color_type;
574 #ifdef PNG_SETJMP_SUPPORTED
575 #ifdef USE_FAR_KEYWORD
576    jmp_buf jmpbuf;
577 #endif
578 #endif
579
580 #if defined(_WIN32_WCE)
581    TCHAR path[MAX_PATH];
582 #endif
583    char inbuf[256], outbuf[256];
584
585    row_buf = (png_bytep)NULL;
586
587 #if defined(_WIN32_WCE)
588    MultiByteToWideChar(CP_ACP, 0, inname, -1, path, MAX_PATH);
589    if ((fpin = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
590 #else
591    if ((fpin = fopen(inname, "rb")) == NULL)
592 #endif
593    {
594       fprintf(STDERR, "Could not find input file %s\n", inname);
595       return (1);
596    }
597
598 #if defined(_WIN32_WCE)
599    MultiByteToWideChar(CP_ACP, 0, outname, -1, path, MAX_PATH);
600    if ((fpout = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)) == INVALID_HANDLE_VALUE)
601 #else
602    if ((fpout = fopen(outname, "wb")) == NULL)
603 #endif
604    {
605       fprintf(STDERR, "Could not open output file %s\n", outname);
606       FCLOSE(fpin);
607       return (1);
608    }
609
610    png_debug(0, "Allocating read and write structures\n");
611 #ifdef PNG_USER_MEM_SUPPORTED
612    read_ptr = png_create_read_struct_2(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
613       (png_error_ptr)NULL, (png_error_ptr)NULL, (png_voidp)NULL,
614       (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
615 #else
616    read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
617       (png_error_ptr)NULL, (png_error_ptr)NULL);
618 #endif
619 #if defined(PNG_NO_STDIO)
620    png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
621        pngtest_warning);
622 #endif
623 #ifdef PNG_USER_MEM_SUPPORTED
624    write_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
625       (png_error_ptr)NULL, (png_error_ptr)NULL, (png_voidp)NULL,
626       (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
627 #else
628    write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
629       (png_error_ptr)NULL, (png_error_ptr)NULL);
630 #endif
631 #if defined(PNG_NO_STDIO)
632    png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
633        pngtest_warning);
634 #endif
635    png_debug(0, "Allocating read_info, write_info and end_info structures\n");
636    read_info_ptr = png_create_info_struct(read_ptr);
637    write_info_ptr = png_create_info_struct(write_ptr);
638    end_info_ptr = png_create_info_struct(read_ptr);
639    write_end_info_ptr = png_create_info_struct(write_ptr);
640 #ifdef PNG_USER_MEM_SUPPORTED
641 #endif
642
643 #ifdef PNG_SETJMP_SUPPORTED
644    png_debug(0, "Setting jmpbuf for read struct\n");
645 #ifdef USE_FAR_KEYWORD
646    if (setjmp(jmpbuf))
647 #else
648    if (setjmp(png_jmpbuf(read_ptr)))
649 #endif
650    {
651       fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
652       png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
653       png_destroy_info_struct(write_ptr, &write_end_info_ptr);
654       png_destroy_write_struct(&write_ptr, &write_info_ptr);
655       FCLOSE(fpin);
656       FCLOSE(fpout);
657       return (1);
658    }
659 #ifdef USE_FAR_KEYWORD
660    png_memcpy(png_jmpbuf(read_ptr),jmpbuf,sizeof(jmp_buf));
661 #endif
662
663    png_debug(0, "Setting jmpbuf for write struct\n");
664 #ifdef USE_FAR_KEYWORD
665    if (setjmp(jmpbuf))
666 #else
667    if (setjmp(png_jmpbuf(write_ptr)))
668 #endif
669    {
670       fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
671       png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
672       png_destroy_info_struct(write_ptr, &write_end_info_ptr);
673       png_destroy_write_struct(&write_ptr, &write_info_ptr);
674       FCLOSE(fpin);
675       FCLOSE(fpout);
676       return (1);
677    }
678 #ifdef USE_FAR_KEYWORD
679    png_memcpy(png_jmpbuf(write_ptr),jmpbuf,sizeof(jmp_buf));
680 #endif
681 #endif
682
683    png_debug(0, "Initializing input and output streams\n");
684 #if !defined(PNG_NO_STDIO)
685    png_init_io(read_ptr, fpin);
686    png_init_io(write_ptr, fpout);
687 #else
688    png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
689    png_set_write_fn(write_ptr, (png_voidp)fpout,  pngtest_write_data,
690 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
691       pngtest_flush);
692 #else
693       NULL);
694 #endif
695 #endif
696    if(status_dots_requested == 1)
697    {
698       png_set_write_status_fn(write_ptr, write_row_callback);
699       png_set_read_status_fn(read_ptr, read_row_callback);
700    }
701    else
702    {
703       png_set_write_status_fn(write_ptr, NULL);
704       png_set_read_status_fn(read_ptr, NULL);
705    }
706
707 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
708    {
709      int i;
710      for(i=0; i<256; i++)
711         filters_used[i]=0;
712      png_set_read_user_transform_fn(read_ptr, count_filters);
713    }
714 #endif
715 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
716    zero_samples=0;
717    png_set_write_user_transform_fn(write_ptr, count_zero_samples);
718 #endif
719
720 #define HANDLE_CHUNK_IF_SAFE      2
721 #define HANDLE_CHUNK_ALWAYS       3
722 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
723    png_set_keep_unknown_chunks(read_ptr, HANDLE_CHUNK_ALWAYS, NULL, 0);
724 #endif
725 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
726    png_set_keep_unknown_chunks(write_ptr, HANDLE_CHUNK_IF_SAFE, NULL, 0);
727 #endif
728
729    png_debug(0, "Reading info struct\n");
730    png_read_info(read_ptr, read_info_ptr);
731
732    png_debug(0, "Transferring info struct\n");
733    {
734       int interlace_type, compression_type, filter_type;
735
736       if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
737           &color_type, &interlace_type, &compression_type, &filter_type))
738       {
739          png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
740 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
741             color_type, interlace_type, compression_type, filter_type);
742 #else
743             color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
744 #endif
745       }
746    }
747 #if defined(PNG_FIXED_POINT_SUPPORTED)
748 #if defined(PNG_cHRM_SUPPORTED)
749    {
750       png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
751          blue_y;
752       if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
753          &red_y, &green_x, &green_y, &blue_x, &blue_y))
754       {
755          png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
756             red_y, green_x, green_y, blue_x, blue_y);
757       }
758    }
759 #endif
760 #if defined(PNG_gAMA_SUPPORTED)
761    {
762       png_fixed_point gamma;
763
764       if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
765       {
766          png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
767       }
768    }
769 #endif
770 #else /* Use floating point versions */
771 #if defined(PNG_FLOATING_POINT_SUPPORTED)
772 #if defined(PNG_cHRM_SUPPORTED)
773    {
774       double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
775          blue_y;
776       if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
777          &red_y, &green_x, &green_y, &blue_x, &blue_y))
778       {
779          png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
780             red_y, green_x, green_y, blue_x, blue_y);
781       }
782    }
783 #endif
784 #if defined(PNG_gAMA_SUPPORTED)
785    {
786       double gamma;
787
788       if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
789       {
790          png_set_gAMA(write_ptr, write_info_ptr, gamma);
791       }
792    }
793 #endif
794 #endif /* floating point */
795 #endif /* fixed point */
796 #if defined(PNG_iCCP_SUPPORTED)
797    {
798       png_charp name;
799       png_charp profile;
800       png_uint_32 proflen;
801       int compression_type;
802
803       if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
804                       &profile, &proflen))
805       {
806          png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
807                       profile, proflen);
808       }
809    }
810 #endif
811 #if defined(PNG_sRGB_SUPPORTED)
812    {
813       int intent;
814
815       if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
816       {
817          png_set_sRGB(write_ptr, write_info_ptr, intent);
818       }
819    }
820 #endif
821    {
822       png_colorp palette;
823       int num_palette;
824
825       if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
826       {
827          png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
828       }
829    }
830 #if defined(PNG_bKGD_SUPPORTED)
831    {
832       png_color_16p background;
833
834       if (png_get_bKGD(read_ptr, read_info_ptr, &background))
835       {
836          png_set_bKGD(write_ptr, write_info_ptr, background);
837       }
838    }
839 #endif
840 #if defined(PNG_hIST_SUPPORTED)
841    {
842       png_uint_16p hist;
843
844       if (png_get_hIST(read_ptr, read_info_ptr, &hist))
845       {
846          png_set_hIST(write_ptr, write_info_ptr, hist);
847       }
848    }
849 #endif
850 #if defined(PNG_oFFs_SUPPORTED)
851    {
852       png_int_32 offset_x, offset_y;
853       int unit_type;
854
855       if (png_get_oFFs(read_ptr, read_info_ptr,&offset_x,&offset_y,&unit_type))
856       {
857          png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
858       }
859    }
860 #endif
861 #if defined(PNG_pCAL_SUPPORTED)
862    {
863       png_charp purpose, units;
864       png_charpp params;
865       png_int_32 X0, X1;
866       int type, nparams;
867
868       if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
869          &nparams, &units, &params))
870       {
871          png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
872             nparams, units, params);
873       }
874    }
875 #endif
876 #if defined(PNG_pHYs_SUPPORTED)
877    {
878       png_uint_32 res_x, res_y;
879       int unit_type;
880
881       if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
882       {
883          png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
884       }
885    }
886 #endif
887 #if defined(PNG_sBIT_SUPPORTED)
888    {
889       png_color_8p sig_bit;
890
891       if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
892       {
893          png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
894       }
895    }
896 #endif
897 #if defined(PNG_sCAL_SUPPORTED)
898 #ifdef PNG_FLOATING_POINT_SUPPORTED
899    {
900       int unit;
901       double scal_width, scal_height;
902
903       if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
904          &scal_height))
905       {
906          png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
907       }
908    }
909 #else
910 #ifdef PNG_FIXED_POINT_SUPPORTED
911    {
912       int unit;
913       png_charp scal_width, scal_height;
914
915       if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
916           &scal_height))
917       {
918          png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width, scal_height);
919       }
920    }
921 #endif
922 #endif
923 #endif
924 #if defined(PNG_TEXT_SUPPORTED)
925    {
926       png_textp text_ptr;
927       int num_text;
928
929       if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
930       {
931          png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks\n", num_text);
932          png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
933       }
934    }
935 #endif
936 #if defined(PNG_tIME_SUPPORTED)
937    {
938       png_timep mod_time;
939
940       if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
941       {
942          png_set_tIME(write_ptr, write_info_ptr, mod_time);
943 #if defined(PNG_TIME_RFC1123_SUPPORTED)
944          /* we have to use png_strcpy instead of "=" because the string
945             pointed to by png_convert_to_rfc1123() gets free'ed before
946             we use it */
947          png_strcpy(tIME_string,png_convert_to_rfc1123(read_ptr, mod_time));
948          tIME_chunk_present++;
949 #endif /* PNG_TIME_RFC1123_SUPPORTED */
950       }
951    }
952 #endif
953 #if defined(PNG_tRNS_SUPPORTED)
954    {
955       png_bytep trans;
956       int num_trans;
957       png_color_16p trans_values;
958
959       if (png_get_tRNS(read_ptr, read_info_ptr, &trans, &num_trans,
960          &trans_values))
961       {
962          png_set_tRNS(write_ptr, write_info_ptr, trans, num_trans,
963             trans_values);
964       }
965    }
966 #endif
967 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
968    {
969       png_unknown_chunkp unknowns;
970       int num_unknowns = (int)png_get_unknown_chunks(read_ptr, read_info_ptr,
971          &unknowns);
972       if (num_unknowns)
973       {
974          png_size_t i;
975          png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
976            num_unknowns);
977          /* copy the locations from the read_info_ptr.  The automatically
978             generated locations in write_info_ptr are wrong because we
979             haven't written anything yet */
980          for (i = 0; i < (png_size_t)num_unknowns; i++)
981            png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
982              unknowns[i].location);
983       }
984    }
985 #endif
986
987    png_debug(0, "\nWriting info struct\n");
988
989 /* If we wanted, we could write info in two steps:
990    png_write_info_before_PLTE(write_ptr, write_info_ptr);
991  */
992    png_write_info(write_ptr, write_info_ptr);
993
994    png_debug(0, "\nAllocating row buffer \n");
995    row_buf = (png_bytep)png_malloc(read_ptr,
996       png_get_rowbytes(read_ptr, read_info_ptr));
997    if (row_buf == NULL)
998    {
999       fprintf(STDERR, "No memory to allocate row buffer\n");
1000       png_destroy_read_struct(&read_ptr, &read_info_ptr, (png_infopp)NULL);
1001       png_destroy_info_struct(write_ptr, &write_end_info_ptr);
1002       png_destroy_write_struct(&write_ptr, &write_info_ptr);
1003       FCLOSE(fpin);
1004       FCLOSE(fpout);
1005       return (1);
1006    }
1007    png_debug(0, "Writing row data\n");
1008
1009 #if defined(PNG_READ_INTERLACING_SUPPORTED) || \
1010   defined(PNG_WRITE_INTERLACING_SUPPORTED)
1011    num_pass = png_set_interlace_handling(read_ptr);
1012    png_set_interlace_handling(write_ptr);
1013 #else
1014    num_pass=1;
1015 #endif
1016
1017 #ifdef PNGTEST_TIMING
1018    t_stop = (float)clock();
1019    t_misc += (t_stop - t_start);
1020    t_start = t_stop;
1021 #endif
1022    for (pass = 0; pass < num_pass; pass++)
1023    {
1024       png_debug1(0, "Writing row data for pass %d\n",pass);
1025       for (y = 0; y < height; y++)
1026       {
1027          png_read_rows(read_ptr, (png_bytepp)&row_buf, (png_bytepp)NULL, 1);
1028 #ifdef PNGTEST_TIMING
1029          t_stop = (float)clock();
1030          t_decode += (t_stop - t_start);
1031          t_start = t_stop;
1032 #endif
1033          png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
1034 #ifdef PNGTEST_TIMING
1035          t_stop = (float)clock();
1036          t_encode += (t_stop - t_start);
1037          t_start = t_stop;
1038 #endif
1039       }
1040    }
1041
1042 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1043    png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
1044 #endif
1045 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
1046    png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
1047 #endif
1048
1049    png_debug(0, "Reading and writing end_info data\n");
1050
1051    png_read_end(read_ptr, end_info_ptr);
1052 #if defined(PNG_TEXT_SUPPORTED)
1053    {
1054       png_textp text_ptr;
1055       int num_text;
1056
1057       if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1058       {
1059          png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks\n", num_text);
1060          png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1061       }
1062    }
1063 #endif
1064 #if defined(PNG_tIME_SUPPORTED)
1065    {
1066       png_timep mod_time;
1067
1068       if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
1069       {
1070          png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
1071 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1072          /* we have to use png_strcpy instead of "=" because the string
1073             pointed to by png_convert_to_rfc1123() gets free'ed before
1074             we use it */
1075          png_strcpy(tIME_string,png_convert_to_rfc1123(read_ptr, mod_time));
1076          tIME_chunk_present++;
1077 #endif /* PNG_TIME_RFC1123_SUPPORTED */
1078       }
1079    }
1080 #endif
1081 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
1082    {
1083       png_unknown_chunkp unknowns;
1084       int num_unknowns;
1085       num_unknowns = (int)png_get_unknown_chunks(read_ptr, end_info_ptr,
1086          &unknowns);
1087       if (num_unknowns)
1088       {
1089          png_size_t i;
1090          png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1091            num_unknowns);
1092          /* copy the locations from the read_info_ptr.  The automatically
1093             generated locations in write_end_info_ptr are wrong because we
1094             haven't written the end_info yet */
1095          for (i = 0; i < (png_size_t)num_unknowns; i++)
1096            png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1097              unknowns[i].location);
1098       }
1099    }
1100 #endif
1101    png_write_end(write_ptr, write_end_info_ptr);
1102
1103 #ifdef PNG_EASY_ACCESS_SUPPORTED
1104    if(verbose)
1105    {
1106       png_uint_32 iwidth, iheight;
1107       iwidth = png_get_image_width(write_ptr, write_info_ptr);
1108       iheight = png_get_image_height(write_ptr, write_info_ptr);
1109       fprintf(STDERR, "Image width = %lu, height = %lu\n",
1110          iwidth, iheight);
1111    }
1112 #endif
1113
1114    png_debug(0, "Destroying data structs\n");
1115    png_free(read_ptr, row_buf);
1116    row_buf=NULL;
1117    png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
1118    png_destroy_info_struct(write_ptr, &write_end_info_ptr);
1119    png_destroy_write_struct(&write_ptr, &write_info_ptr);
1120
1121    FCLOSE(fpin);
1122    FCLOSE(fpout);
1123
1124    png_debug(0, "Opening files for comparison\n");
1125 #if defined(_WIN32_WCE)
1126    MultiByteToWideChar(CP_ACP, 0, inname, -1, path, MAX_PATH);
1127    if ((fpin = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
1128 #else
1129    if ((fpin = fopen(inname, "rb")) == NULL)
1130 #endif
1131    {
1132       fprintf(STDERR, "Could not find file %s\n", inname);
1133       return (1);
1134    }
1135
1136 #if defined(_WIN32_WCE)
1137    MultiByteToWideChar(CP_ACP, 0, outname, -1, path, MAX_PATH);
1138    if ((fpout = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
1139 #else
1140    if ((fpout = fopen(outname, "rb")) == NULL)
1141 #endif
1142    {
1143       fprintf(STDERR, "Could not find file %s\n", outname);
1144       FCLOSE(fpin);
1145       return (1);
1146    }
1147
1148    for(;;)
1149    {
1150       png_size_t num_in, num_out;
1151
1152       READFILE(fpin, inbuf, 1, num_in);
1153       READFILE(fpout, outbuf, 1, num_out);
1154
1155       if (num_in != num_out)
1156       {
1157          fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
1158                  inname, outname);
1159          if(wrote_question == 0)
1160          {
1161             fprintf(STDERR,
1162          "   Was %s written with the same maximum IDAT chunk size (%d bytes),",
1163               inname,PNG_ZBUF_SIZE);
1164             fprintf(STDERR,
1165               "\n   filtering heuristic (libpng default), compression");
1166             fprintf(STDERR,
1167               " level (zlib default),\n   and zlib version (%s)?\n\n",
1168               ZLIB_VERSION);
1169             wrote_question=1;
1170          }
1171          FCLOSE(fpin);
1172          FCLOSE(fpout);
1173          return (0);
1174       }
1175
1176       if (!num_in)
1177          break;
1178
1179       if (png_memcmp(inbuf, outbuf, num_in))
1180       {
1181          fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname);
1182          if(wrote_question == 0)
1183          {
1184             fprintf(STDERR,
1185          "   Was %s written with the same maximum IDAT chunk size (%d bytes),",
1186                  inname,PNG_ZBUF_SIZE);
1187             fprintf(STDERR,
1188               "\n   filtering heuristic (libpng default), compression");
1189             fprintf(STDERR,
1190               " level (zlib default),\n   and zlib version (%s)?\n\n",
1191               ZLIB_VERSION);
1192             wrote_question=1;
1193          }
1194          FCLOSE(fpin);
1195          FCLOSE(fpout);
1196          return (0);
1197       }
1198    }
1199
1200    FCLOSE(fpin);
1201    FCLOSE(fpout);
1202
1203    return (0);
1204 }
1205
1206 /* input and output filenames */
1207 #ifdef RISCOS
1208 static PNG_CONST char *inname = "pngtest/png";
1209 static PNG_CONST char *outname = "pngout/png";
1210 #else
1211 static PNG_CONST char *inname = "pngtest.png";
1212 static PNG_CONST char *outname = "pngout.png";
1213 #endif
1214
1215 int
1216 main(int argc, char *argv[])
1217 {
1218    int multiple = 0;
1219    int ierror = 0;
1220
1221    fprintf(STDERR, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
1222    fprintf(STDERR, "   with zlib   version %s\n", ZLIB_VERSION);
1223    fprintf(STDERR,"%s",png_get_copyright(NULL));
1224    /* Show the version of libpng used in building the library */
1225    fprintf(STDERR," library (%lu):%s", png_access_version_number(),
1226       png_get_header_version(NULL));
1227    /* Show the version of libpng used in building the application */
1228    fprintf(STDERR," pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
1229       PNG_HEADER_VERSION_STRING);
1230    fprintf(STDERR," sizeof(png_struct)=%d, sizeof(png_info)=%d\n",
1231                     sizeof(png_struct), sizeof(png_info));
1232
1233    /* Do some consistency checking on the memory allocation settings, I'm
1234       not sure this matters, but it is nice to know, the first of these
1235       tests should be impossible because of the way the macros are set
1236       in pngconf.h */
1237 #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
1238       fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
1239 #endif
1240    /* I think the following can happen. */
1241 #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
1242       fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
1243 #endif
1244
1245    if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1246    {
1247       fprintf(STDERR,
1248          "Warning: versions are different between png.h and png.c\n");
1249       fprintf(STDERR, "  png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1250       fprintf(STDERR, "  png.c version: %s\n\n", png_libpng_ver);
1251       ++ierror;
1252    }
1253
1254    if (argc > 1)
1255    {
1256       if (strcmp(argv[1], "-m") == 0)
1257       {
1258          multiple = 1;
1259          status_dots_requested = 0;
1260       }
1261       else if (strcmp(argv[1], "-mv") == 0 ||
1262                strcmp(argv[1], "-vm") == 0 )
1263       {
1264          multiple = 1;
1265          verbose = 1;
1266          status_dots_requested = 1;
1267       }
1268       else if (strcmp(argv[1], "-v") == 0)
1269       {
1270          verbose = 1;
1271          status_dots_requested = 1;
1272          inname = argv[2];
1273       }
1274       else
1275       {
1276          inname = argv[1];
1277          status_dots_requested = 0;
1278       }
1279    }
1280
1281    if (!multiple && argc == 3+verbose)
1282      outname = argv[2+verbose];
1283
1284    if ((!multiple && argc > 3+verbose) || (multiple && argc < 2))
1285    {
1286      fprintf(STDERR,
1287        "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
1288         argv[0], argv[0]);
1289      fprintf(STDERR,
1290        "  reads/writes one PNG file (without -m) or multiple files (-m)\n");
1291      fprintf(STDERR,
1292        "  with -m %s is used as a temporary file\n", outname);
1293      exit(1);
1294    }
1295
1296    if (multiple)
1297    {
1298       int i;
1299 #ifdef PNG_USER_MEM_SUPPORTED
1300       int allocation_now = current_allocation;
1301 #endif
1302       for (i=2; i<argc; ++i)
1303       {
1304 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1305          int k;
1306 #endif
1307          int kerror;
1308          fprintf(STDERR, "Testing %s:",argv[i]);
1309          kerror = test_one_file(argv[i], outname);
1310          if (kerror == 0)
1311          {
1312 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1313             fprintf(STDERR, "\n PASS (%lu zero samples)\n",zero_samples);
1314 #else
1315             fprintf(STDERR, " PASS\n");
1316 #endif
1317 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1318             for (k=0; k<256; k++)
1319                if(filters_used[k])
1320                   fprintf(STDERR, " Filter %d was used %lu times\n",
1321                      k,filters_used[k]);
1322 #endif
1323 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1324          if(tIME_chunk_present != 0)
1325             fprintf(STDERR, " tIME = %s\n",tIME_string);
1326          tIME_chunk_present = 0;
1327 #endif /* PNG_TIME_RFC1123_SUPPORTED */
1328          }
1329          else
1330          {
1331             fprintf(STDERR, " FAIL\n");
1332             ierror += kerror;
1333          }
1334 #ifdef PNG_USER_MEM_SUPPORTED
1335          if (allocation_now != current_allocation)
1336             fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1337                current_allocation-allocation_now);
1338          if (current_allocation != 0)
1339          {
1340             memory_infop pinfo = pinformation;
1341
1342             fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1343                current_allocation);
1344             while (pinfo != NULL)
1345             {
1346                fprintf(STDERR, " %d bytes at %x\n", pinfo->size, pinfo->pointer);
1347                pinfo = pinfo->next;
1348             }
1349          }
1350 #endif
1351       }
1352 #ifdef PNG_USER_MEM_SUPPORTED
1353          fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1354             current_allocation);
1355          fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
1356             maximum_allocation);
1357          fprintf(STDERR, " Total   memory allocation: %10d bytes\n",
1358             total_allocation);
1359          fprintf(STDERR, "     Number of allocations: %10d\n",
1360             num_allocations);
1361 #endif
1362    }
1363    else
1364    {
1365       int i;
1366       for (i=0; i<3; ++i)
1367       {
1368          int kerror;
1369 #ifdef PNG_USER_MEM_SUPPORTED
1370          int allocation_now = current_allocation;
1371 #endif
1372          if (i == 1) status_dots_requested = 1;
1373          else if(verbose == 0)status_dots_requested = 0;
1374          if (i == 0 || verbose == 1 || ierror != 0)
1375             fprintf(STDERR, "Testing %s:",inname);
1376          kerror = test_one_file(inname, outname);
1377          if(kerror == 0)
1378          {
1379             if(verbose == 1 || i == 2)
1380             {
1381 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1382                 int k;
1383 #endif
1384 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1385                 fprintf(STDERR, "\n PASS (%lu zero samples)\n",zero_samples);
1386 #else
1387                 fprintf(STDERR, " PASS\n");
1388 #endif
1389 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1390                 for (k=0; k<256; k++)
1391                    if(filters_used[k])
1392                       fprintf(STDERR, " Filter %d was used %lu times\n",
1393                          k,filters_used[k]);
1394 #endif
1395 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1396              if(tIME_chunk_present != 0)
1397                 fprintf(STDERR, " tIME = %s\n",tIME_string);
1398 #endif /* PNG_TIME_RFC1123_SUPPORTED */
1399             }
1400          }
1401          else
1402          {
1403             if(verbose == 0 && i != 2)
1404                fprintf(STDERR, "Testing %s:",inname);
1405             fprintf(STDERR, " FAIL\n");
1406             ierror += kerror;
1407          }
1408 #ifdef PNG_USER_MEM_SUPPORTED
1409          if (allocation_now != current_allocation)
1410              fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1411                current_allocation-allocation_now);
1412          if (current_allocation != 0)
1413          {
1414              memory_infop pinfo = pinformation;
1415
1416              fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1417                 current_allocation);
1418              while (pinfo != NULL)
1419              {
1420                 fprintf(STDERR," %d bytes at %x\n",
1421                    pinfo->size, pinfo->pointer);
1422                 pinfo = pinfo->next;
1423              }
1424           }
1425 #endif
1426        }
1427 #ifdef PNG_USER_MEM_SUPPORTED
1428        fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1429           current_allocation);
1430        fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
1431           maximum_allocation);
1432        fprintf(STDERR, " Total   memory allocation: %10d bytes\n",
1433           total_allocation);
1434        fprintf(STDERR, "     Number of allocations: %10d\n",
1435             num_allocations);
1436 #endif
1437    }
1438
1439 #ifdef PNGTEST_TIMING
1440    t_stop = (float)clock();
1441    t_misc += (t_stop - t_start);
1442    t_start = t_stop;
1443    fprintf(STDERR," CPU time used = %.3f seconds",
1444       (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
1445    fprintf(STDERR," (decoding %.3f,\n",
1446       t_decode/(float)CLOCKS_PER_SEC);
1447    fprintf(STDERR,"        encoding %.3f ,",
1448       t_encode/(float)CLOCKS_PER_SEC);
1449    fprintf(STDERR," other %.3f seconds)\n\n",
1450       t_misc/(float)CLOCKS_PER_SEC);
1451 #endif
1452
1453    if (ierror == 0)
1454       fprintf(STDERR, "libpng passes test\n");
1455    else
1456       fprintf(STDERR, "libpng FAILS test\n");
1457    return (int)(ierror != 0);
1458 }
1459
1460 /* Generate a compiler error if there is an old png.h in the search path. */
1461 typedef version_1_0_9 your_png_h_is_not_version_1_0_9;