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