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