2 /* pngtest.c - a simple test program to test libpng
4 * libpng 1.2.5rc3 - September 18, 2002
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2002 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
10 * This program reads in a PNG image, writes it out again, and then
11 * compares the two files. If the files are identical, this shows that
12 * the basic chunk handling, filtering, and (de)compression code is working
13 * properly. It does not currently test all of the transforms, although
16 * The program will report "FAIL" in certain legitimate cases:
17 * 1) when the compression level or filter selection method is changed.
18 * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
19 * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
20 * exist in the input file.
21 * 4) others not listed here...
22 * In these cases, it is best to check with another tool such as "pngcheck"
23 * to see what the differences between the two files are.
25 * If a filename is given on the command-line, then this file is used
26 * for the input, rather than the default "pngtest.png". This allows
27 * testing a wide variety of files easily. You can also test a number
28 * of files at once by typing "pngtest -m file1.png file2.png ..."
31 #if defined(_WIN32_WCE)
33 __error__ (f
|w
)printf functions are
not supported on old WindowsCE
.;
37 # define READFILE(file, data, length, check) \
38 if (ReadFile(file, data, length, &check,NULL)) check = 0
39 # define WRITEFILE(file, data, length, check)) \
40 if (WriteFile(file, data, length, &check, NULL)) check = 0
41 # define FCLOSE(file) CloseHandle(file)
46 # define READFILE(file, data, length, check) \
47 check=(png_size_t)fread(data,(png_size_t)1,length,file)
48 # define WRITEFILE(file, data, length, check) \
49 check=(png_size_t)fwrite(data,(png_size_t)1, length, file)
50 # define FCLOSE(file) fclose(file)
53 #if defined(PNG_NO_STDIO)
54 # if defined(_WIN32_WCE)
55 typedef HANDLE png_FILE_p
;
57 typedef FILE * png_FILE_p
;
61 /* Makes pngtest verbose so we can find problems (needs to be before png.h) */
67 # define SINGLE_ROWBUF_ALLOC /* makes buffer overruns easier to nail */
71 #define PNGTEST_TIMING
74 #ifdef PNG_NO_FLOATING_POINT_SUPPORTED
79 static float t_start
, t_stop
, t_decode
, t_encode
, t_misc
;
85 /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
87 # define png_jmpbuf(png_ptr) png_ptr->jmpbuf
91 static float t_start
, t_stop
, t_decode
, t_encode
, t_misc
;
92 #if !defined(PNG_tIME_SUPPORTED)
97 #if defined(PNG_TIME_RFC1123_SUPPORTED)
98 static int tIME_chunk_present
=0;
99 static char tIME_string
[30] = "no tIME chunk present in file";
102 static int verbose
= 0;
104 int test_one_file
PNGARG((PNG_CONST
char *inname
, PNG_CONST
char *outname
));
110 /* defined so I can write to a file on gui/windowing platforms */
111 /* #define STDERR stderr */
112 #define STDERR stdout /* for DOS */
114 /* example of using row callbacks to make a simple progress meter */
115 static int status_pass
=1;
116 static int status_dots_requested
=0;
117 static int status_dots
=1;
123 read_row_callback(png_structp png_ptr
, png_uint_32 row_number
, int pass
);
128 read_row_callback(png_structp png_ptr
, png_uint_32 row_number
, int pass
)
130 if(png_ptr
== NULL
|| row_number
> PNG_MAX_UINT
) return;
131 if(status_pass
!= pass
)
133 fprintf(stdout
,"\n Pass %d: ",pass
);
140 fprintf(stdout
, "\n ");
143 fprintf(stdout
, "r");
150 write_row_callback(png_structp png_ptr
, png_uint_32 row_number
, int pass
);
155 write_row_callback(png_structp png_ptr
, png_uint_32 row_number
, int pass
)
157 if(png_ptr
== NULL
|| row_number
> PNG_MAX_UINT
|| pass
> 7) return;
158 fprintf(stdout
, "w");
162 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
163 /* Example of using user transform callback (we don't transform anything,
164 but merely examine the row filters. We set this to 256 rather than
165 5 in case illegal filter values are present.) */
166 static png_uint_32 filters_used
[256];
171 count_filters(png_structp png_ptr
, png_row_infop row_info
, png_bytep data
);
176 count_filters(png_structp png_ptr
, png_row_infop row_info
, png_bytep data
)
178 if(png_ptr
!= NULL
&& row_info
!= NULL
)
179 ++filters_used
[*(data
-1)];
183 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
184 /* example of using user transform callback (we don't transform anything,
185 but merely count the zero samples) */
187 static png_uint_32 zero_samples
;
193 count_zero_samples(png_structp png_ptr
, png_row_infop row_info
, png_bytep data
);
198 count_zero_samples(png_structp png_ptr
, png_row_infop row_info
, png_bytep data
)
201 if(png_ptr
== NULL
)return;
203 /* contents of row_info:
204 * png_uint_32 width width of row
205 * png_uint_32 rowbytes number of bytes in row
206 * png_byte color_type color type of pixels
207 * png_byte bit_depth bit depth of samples
208 * png_byte channels number of channels (1-4)
209 * png_byte pixel_depth bits per pixel (depth*channels)
213 /* counts the number of zero samples (or zero pixels if color_type is 3 */
215 if(row_info
->color_type
== 0 || row_info
->color_type
== 3)
218 png_uint_32 n
, nstop
;
219 for (n
=0, nstop
=row_info
->width
; n
<nstop
; n
++)
221 if(row_info
->bit_depth
== 1)
223 if(((*dp
<< pos
++ ) & 0x80) == 0) zero_samples
++;
230 if(row_info
->bit_depth
== 2)
232 if(((*dp
<< (pos
+=2)) & 0xc0) == 0) zero_samples
++;
239 if(row_info
->bit_depth
== 4)
241 if(((*dp
<< (pos
+=4)) & 0xf0) == 0) zero_samples
++;
248 if(row_info
->bit_depth
== 8)
249 if(*dp
++ == 0) zero_samples
++;
250 if(row_info
->bit_depth
== 16)
252 if((*dp
| *(dp
+1)) == 0) zero_samples
++;
257 else /* other color types */
259 png_uint_32 n
, nstop
;
261 int color_channels
= row_info
->channels
;
262 if(row_info
->color_type
> 3)color_channels
--;
264 for (n
=0, nstop
=row_info
->width
; n
<nstop
; n
++)
266 for (channel
= 0; channel
< color_channels
; channel
++)
268 if(row_info
->bit_depth
== 8)
269 if(*dp
++ == 0) zero_samples
++;
270 if(row_info
->bit_depth
== 16)
272 if((*dp
| *(dp
+1)) == 0) zero_samples
++;
276 if(row_info
->color_type
> 3)
279 if(row_info
->bit_depth
== 16)dp
++;
284 #endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
286 static int wrote_question
= 0;
288 #if defined(PNG_NO_STDIO)
289 /* START of code to validate stdio-free compilation */
290 /* These copies of the default read/write functions come from pngrio.c and */
291 /* pngwio.c. They allow "don't include stdio" testing of the library. */
292 /* This is the function that does the actual reading of data. If you are
293 not reading from a standard C stream, you should create a replacement
294 read_data function and use it at run time with png_set_read_fn(), rather
295 than changing the library. */
297 #ifndef USE_FAR_KEYWORD
299 pngtest_read_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
303 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
304 * instead of an int, which is what fread() actually returns.
306 READFILE((png_FILE_p
)png_ptr
->io_ptr
, data
, length
, check
);
310 png_error(png_ptr
, "Read Error!");
314 /* this is the model-independent version. Since the standard I/O library
315 can't handle far buffers in the medium and small models, we have to copy
319 #define NEAR_BUF_SIZE 1024
320 #define MIN(a,b) (a <= b ? a : b)
323 pngtest_read_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
329 /* Check if data really is near. If so, use usual code. */
330 n_data
= (png_byte
*)CVT_PTR_NOCHECK(data
);
331 io_ptr
= (png_FILE_p
)CVT_PTR(png_ptr
->io_ptr
);
332 if ((png_bytep
)n_data
== data
)
334 READFILE(io_ptr
, n_data
, length
, check
);
338 png_byte buf
[NEAR_BUF_SIZE
];
339 png_size_t read
, remaining
, err
;
344 read
= MIN(NEAR_BUF_SIZE
, remaining
);
345 READFILE(io_ptr
, buf
, 1, err
);
346 png_memcpy(data
, buf
, read
); /* copy far buffer to near buffer */
354 while (remaining
!= 0);
358 png_error(png_ptr
, "read Error");
361 #endif /* USE_FAR_KEYWORD */
363 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
365 pngtest_flush(png_structp png_ptr
)
367 #if !defined(_WIN32_WCE)
369 io_ptr
= (png_FILE_p
)CVT_PTR((png_ptr
->io_ptr
));
376 /* This is the function that does the actual writing of data. If you are
377 not writing to a standard C stream, you should create a replacement
378 write_data function and use it at run time with png_set_write_fn(), rather
379 than changing the library. */
380 #ifndef USE_FAR_KEYWORD
382 pngtest_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
386 WRITEFILE((png_FILE_p
)png_ptr
->io_ptr
, data
, length
, check
);
389 png_error(png_ptr
, "Write Error");
393 /* this is the model-independent version. Since the standard I/O library
394 can't handle far buffers in the medium and small models, we have to copy
398 #define NEAR_BUF_SIZE 1024
399 #define MIN(a,b) (a <= b ? a : b)
402 pngtest_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
405 png_byte
*near_data
; /* Needs to be "png_byte *" instead of "png_bytep" */
408 /* Check if data really is near. If so, use usual code. */
409 near_data
= (png_byte
*)CVT_PTR_NOCHECK(data
);
410 io_ptr
= (png_FILE_p
)CVT_PTR(png_ptr
->io_ptr
);
411 if ((png_bytep
)near_data
== data
)
413 WRITEFILE(io_ptr
, near_data
, length
, check
);
417 png_byte buf
[NEAR_BUF_SIZE
];
418 png_size_t written
, remaining
, err
;
423 written
= MIN(NEAR_BUF_SIZE
, remaining
);
424 png_memcpy(buf
, data
, written
); /* copy far buffer to near buffer */
425 WRITEFILE(io_ptr
, buf
, written
, err
);
431 remaining
-= written
;
433 while (remaining
!= 0);
437 png_error(png_ptr
, "Write Error");
441 #endif /* USE_FAR_KEYWORD */
443 /* This function is called when there is a warning, but the library thinks
444 * it can continue anyway. Replacement functions don't have to do anything
445 * here if you don't want to. In the default configuration, png_ptr is
446 * not used, but it is passed in case it may be useful.
449 pngtest_warning(png_structp png_ptr
, png_const_charp message
)
451 PNG_CONST
char *name
= "UNKNOWN (ERROR!)";
452 if (png_ptr
!= NULL
&& png_ptr
->error_ptr
!= NULL
)
453 name
= png_ptr
->error_ptr
;
454 fprintf(STDERR
, "%s: libpng warning: %s\n", name
, message
);
457 /* This is the default error handling function. Note that replacements for
458 * this function MUST NOT RETURN, or the program will likely crash. This
459 * function is used by default, or if the program supplies NULL for the
460 * error function pointer in png_set_error_fn().
463 pngtest_error(png_structp png_ptr
, png_const_charp message
)
465 pngtest_warning(png_ptr
, message
);
466 /* We can return because png_error calls the default handler, which is
467 * actually OK in this case. */
469 #endif /* PNG_NO_STDIO */
470 /* END of code to validate stdio-free compilation */
472 /* START of code to validate memory allocation and deallocation */
473 #ifdef PNG_USER_MEM_SUPPORTED
475 /* Allocate memory. For reasonable files, size should never exceed
476 64K. However, zlib may allocate more then 64K if you don't tell
477 it not to. See zconf.h and png.h for more information. zlib does
478 need to allocate exactly 64K, so whatever you call here must
479 have the ability to do that.
481 This piece of code can be compiled to validate max 64K allocations
482 by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K. */
483 typedef struct memory_information
487 struct memory_information FAR
*next
;
488 } memory_information
;
489 typedef memory_information FAR
*memory_infop
;
491 static memory_infop pinformation
= NULL
;
492 static int current_allocation
= 0;
493 static int maximum_allocation
= 0;
494 static int total_allocation
= 0;
495 static int num_allocations
= 0;
497 png_voidp png_debug_malloc
PNGARG((png_structp png_ptr
, png_uint_32 size
));
498 void png_debug_free
PNGARG((png_structp png_ptr
, png_voidp ptr
));
501 png_debug_malloc(png_structp png_ptr
, png_uint_32 size
)
504 /* png_malloc has already tested for NULL; png_create_struct calls
505 png_debug_malloc directly, with png_ptr == NULL which is OK */
510 /* This calls the library allocator twice, once to get the requested
511 buffer and once to get a new free list entry. */
513 memory_infop pinfo
= (memory_infop
)png_malloc_default(png_ptr
,
514 (png_uint_32
)sizeof *pinfo
);
516 current_allocation
+= size
;
517 total_allocation
+= size
;
519 if (current_allocation
> maximum_allocation
)
520 maximum_allocation
= current_allocation
;
521 pinfo
->pointer
= (png_voidp
)png_malloc_default(png_ptr
, size
);
522 pinfo
->next
= pinformation
;
523 pinformation
= pinfo
;
524 /* Make sure the caller isn't assuming zeroed memory. */
525 png_memset(pinfo
->pointer
, 0xdd, pinfo
->size
);
528 printf("png_malloc %lu bytes at %x\n",size
,pinfo
->pointer
);
530 assert(pinfo
->size
!= 12345678);
531 return (png_voidp
)(pinfo
->pointer
);
535 /* Free a pointer. It is removed from the list at the same time. */
537 png_debug_free(png_structp png_ptr
, png_voidp ptr
)
540 fprintf(STDERR
, "NULL pointer to png_debug_free.\n");
543 #if 0 /* This happens all the time. */
544 fprintf(STDERR
, "WARNING: freeing NULL pointer\n");
549 /* Unlink the element from the list. */
551 memory_infop FAR
*ppinfo
= &pinformation
;
554 memory_infop pinfo
= *ppinfo
;
555 if (pinfo
->pointer
== ptr
)
557 *ppinfo
= pinfo
->next
;
558 current_allocation
-= pinfo
->size
;
559 if (current_allocation
< 0)
560 fprintf(STDERR
, "Duplicate free of memory\n");
561 /* We must free the list element too, but first kill
562 the memory that is to be freed. */
563 png_memset(ptr
, 0x55, pinfo
->size
);
564 png_free_default(png_ptr
, pinfo
);
568 if (pinfo
->next
== NULL
)
570 fprintf(STDERR
, "Pointer %x not found\n", (unsigned int)ptr
);
573 ppinfo
= &pinfo
->next
;
577 /* Finally free the data. */
580 printf("Freeing %x\n",ptr
);
582 png_free_default(png_ptr
, ptr
);
585 #endif /* PNG_USER_MEM_SUPPORTED */
586 /* END of code to test memory allocation/deallocation */
590 test_one_file(PNG_CONST
char *inname
, PNG_CONST
char *outname
)
592 static png_FILE_p fpin
;
593 static png_FILE_p fpout
; /* "static" prevents setjmp corruption */
594 png_structp read_ptr
;
595 png_infop read_info_ptr
, end_info_ptr
;
596 #ifdef PNG_WRITE_SUPPORTED
597 png_structp write_ptr
;
598 png_infop write_info_ptr
;
599 png_infop write_end_info_ptr
;
601 png_structp write_ptr
= NULL
;
602 png_infop write_info_ptr
= NULL
;
603 png_infop write_end_info_ptr
= NULL
;
607 png_uint_32 width
, height
;
609 int bit_depth
, color_type
;
610 #ifdef PNG_SETJMP_SUPPORTED
611 #ifdef USE_FAR_KEYWORD
616 #if defined(_WIN32_WCE)
617 TCHAR path
[MAX_PATH
];
619 char inbuf
[256], outbuf
[256];
623 #if defined(_WIN32_WCE)
624 MultiByteToWideChar(CP_ACP
, 0, inname
, -1, path
, MAX_PATH
);
625 if ((fpin
= CreateFile(path
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
)) == INVALID_HANDLE_VALUE
)
627 if ((fpin
= fopen(inname
, "rb")) == NULL
)
630 fprintf(STDERR
, "Could not find input file %s\n", inname
);
634 #if defined(_WIN32_WCE)
635 MultiByteToWideChar(CP_ACP
, 0, outname
, -1, path
, MAX_PATH
);
636 if ((fpout
= CreateFile(path
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
)) == INVALID_HANDLE_VALUE
)
638 if ((fpout
= fopen(outname
, "wb")) == NULL
)
641 fprintf(STDERR
, "Could not open output file %s\n", outname
);
646 png_debug(0, "Allocating read and write structures\n");
647 #ifdef PNG_USER_MEM_SUPPORTED
648 read_ptr
= png_create_read_struct_2(PNG_LIBPNG_VER_STRING
, png_voidp_NULL
,
649 png_error_ptr_NULL
, png_error_ptr_NULL
, png_voidp_NULL
,
650 (png_malloc_ptr
)png_debug_malloc
, (png_free_ptr
)png_debug_free
);
652 read_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, png_voidp_NULL
,
653 png_error_ptr_NULL
, png_error_ptr_NULL
);
655 #if defined(PNG_NO_STDIO)
656 png_set_error_fn(read_ptr
, (png_voidp
)inname
, pngtest_error
,
659 #ifdef PNG_WRITE_SUPPORTED
660 #ifdef PNG_USER_MEM_SUPPORTED
661 write_ptr
= png_create_write_struct_2(PNG_LIBPNG_VER_STRING
, png_voidp_NULL
,
662 png_error_ptr_NULL
, png_error_ptr_NULL
, png_voidp_NULL
,
663 (png_malloc_ptr
)png_debug_malloc
, (png_free_ptr
)png_debug_free
);
665 write_ptr
= png_create_write_struct(PNG_LIBPNG_VER_STRING
, png_voidp_NULL
,
666 png_error_ptr_NULL
, png_error_ptr_NULL
);
668 #if defined(PNG_NO_STDIO)
669 png_set_error_fn(write_ptr
, (png_voidp
)inname
, pngtest_error
,
673 png_debug(0, "Allocating read_info, write_info and end_info structures\n");
674 read_info_ptr
= png_create_info_struct(read_ptr
);
675 end_info_ptr
= png_create_info_struct(read_ptr
);
676 #ifdef PNG_WRITE_SUPPORTED
677 write_info_ptr
= png_create_info_struct(write_ptr
);
678 write_end_info_ptr
= png_create_info_struct(write_ptr
);
681 #ifdef PNG_SETJMP_SUPPORTED
682 png_debug(0, "Setting jmpbuf for read struct\n");
683 #ifdef USE_FAR_KEYWORD
686 if (setjmp(png_jmpbuf(read_ptr
)))
689 fprintf(STDERR
, "%s -> %s: libpng read error\n", inname
, outname
);
691 png_free(read_ptr
, row_buf
);
692 png_destroy_read_struct(&read_ptr
, &read_info_ptr
, &end_info_ptr
);
693 #ifdef PNG_WRITE_SUPPORTED
694 png_destroy_info_struct(write_ptr
, &write_end_info_ptr
);
695 png_destroy_write_struct(&write_ptr
, &write_info_ptr
);
701 #ifdef USE_FAR_KEYWORD
702 png_memcpy(png_jmpbuf(read_ptr
),jmpbuf
,sizeof(jmp_buf));
705 #ifdef PNG_WRITE_SUPPORTED
706 png_debug(0, "Setting jmpbuf for write struct\n");
707 #ifdef USE_FAR_KEYWORD
710 if (setjmp(png_jmpbuf(write_ptr
)))
713 fprintf(STDERR
, "%s -> %s: libpng write error\n", inname
, outname
);
714 png_destroy_read_struct(&read_ptr
, &read_info_ptr
, &end_info_ptr
);
715 png_destroy_info_struct(write_ptr
, &write_end_info_ptr
);
716 #ifdef PNG_WRITE_SUPPORTED
717 png_destroy_write_struct(&write_ptr
, &write_info_ptr
);
723 #ifdef USE_FAR_KEYWORD
724 png_memcpy(png_jmpbuf(write_ptr
),jmpbuf
,sizeof(jmp_buf));
729 png_debug(0, "Initializing input and output streams\n");
730 #if !defined(PNG_NO_STDIO)
731 png_init_io(read_ptr
, fpin
);
732 # ifdef PNG_WRITE_SUPPORTED
733 png_init_io(write_ptr
, fpout
);
736 png_set_read_fn(read_ptr
, (png_voidp
)fpin
, pngtest_read_data
);
737 # ifdef PNG_WRITE_SUPPORTED
738 png_set_write_fn(write_ptr
, (png_voidp
)fpout
, pngtest_write_data
,
739 # if defined(PNG_WRITE_FLUSH_SUPPORTED)
746 if(status_dots_requested
== 1)
748 #ifdef PNG_WRITE_SUPPORTED
749 png_set_write_status_fn(write_ptr
, write_row_callback
);
751 png_set_read_status_fn(read_ptr
, read_row_callback
);
755 #ifdef PNG_WRITE_SUPPORTED
756 png_set_write_status_fn(write_ptr
, png_write_status_ptr_NULL
);
758 png_set_read_status_fn(read_ptr
, png_read_status_ptr_NULL
);
761 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
766 png_set_read_user_transform_fn(read_ptr
, count_filters
);
769 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
771 png_set_write_user_transform_fn(write_ptr
, count_zero_samples
);
774 #define HANDLE_CHUNK_IF_SAFE 2
775 #define HANDLE_CHUNK_ALWAYS 3
776 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
777 png_set_keep_unknown_chunks(read_ptr
, HANDLE_CHUNK_ALWAYS
,
780 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
781 png_set_keep_unknown_chunks(write_ptr
, HANDLE_CHUNK_IF_SAFE
,
785 png_debug(0, "Reading info struct\n");
786 png_read_info(read_ptr
, read_info_ptr
);
788 png_debug(0, "Transferring info struct\n");
790 int interlace_type
, compression_type
, filter_type
;
792 if (png_get_IHDR(read_ptr
, read_info_ptr
, &width
, &height
, &bit_depth
,
793 &color_type
, &interlace_type
, &compression_type
, &filter_type
))
795 png_set_IHDR(write_ptr
, write_info_ptr
, width
, height
, bit_depth
,
796 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
797 color_type
, interlace_type
, compression_type
, filter_type
);
799 color_type
, PNG_INTERLACE_NONE
, compression_type
, filter_type
);
803 #if defined(PNG_FIXED_POINT_SUPPORTED)
804 #if defined(PNG_cHRM_SUPPORTED)
806 png_fixed_point white_x
, white_y
, red_x
, red_y
, green_x
, green_y
, blue_x
,
808 if (png_get_cHRM_fixed(read_ptr
, read_info_ptr
, &white_x
, &white_y
, &red_x
,
809 &red_y
, &green_x
, &green_y
, &blue_x
, &blue_y
))
811 png_set_cHRM_fixed(write_ptr
, write_info_ptr
, white_x
, white_y
, red_x
,
812 red_y
, green_x
, green_y
, blue_x
, blue_y
);
816 #if defined(PNG_gAMA_SUPPORTED)
818 png_fixed_point gamma
;
820 if (png_get_gAMA_fixed(read_ptr
, read_info_ptr
, &gamma
))
822 png_set_gAMA_fixed(write_ptr
, write_info_ptr
, gamma
);
826 #else /* Use floating point versions */
827 #if defined(PNG_FLOATING_POINT_SUPPORTED)
828 #if defined(PNG_cHRM_SUPPORTED)
830 double white_x
, white_y
, red_x
, red_y
, green_x
, green_y
, blue_x
,
832 if (png_get_cHRM(read_ptr
, read_info_ptr
, &white_x
, &white_y
, &red_x
,
833 &red_y
, &green_x
, &green_y
, &blue_x
, &blue_y
))
835 png_set_cHRM(write_ptr
, write_info_ptr
, white_x
, white_y
, red_x
,
836 red_y
, green_x
, green_y
, blue_x
, blue_y
);
840 #if defined(PNG_gAMA_SUPPORTED)
844 if (png_get_gAMA(read_ptr
, read_info_ptr
, &gamma
))
846 png_set_gAMA(write_ptr
, write_info_ptr
, gamma
);
850 #endif /* floating point */
851 #endif /* fixed point */
852 #if defined(PNG_iCCP_SUPPORTED)
857 int compression_type
;
859 if (png_get_iCCP(read_ptr
, read_info_ptr
, &name
, &compression_type
,
862 png_set_iCCP(write_ptr
, write_info_ptr
, name
, compression_type
,
867 #if defined(PNG_sRGB_SUPPORTED)
871 if (png_get_sRGB(read_ptr
, read_info_ptr
, &intent
))
873 png_set_sRGB(write_ptr
, write_info_ptr
, intent
);
881 if (png_get_PLTE(read_ptr
, read_info_ptr
, &palette
, &num_palette
))
883 png_set_PLTE(write_ptr
, write_info_ptr
, palette
, num_palette
);
886 #if defined(PNG_bKGD_SUPPORTED)
888 png_color_16p background
;
890 if (png_get_bKGD(read_ptr
, read_info_ptr
, &background
))
892 png_set_bKGD(write_ptr
, write_info_ptr
, background
);
896 #if defined(PNG_hIST_SUPPORTED)
900 if (png_get_hIST(read_ptr
, read_info_ptr
, &hist
))
902 png_set_hIST(write_ptr
, write_info_ptr
, hist
);
906 #if defined(PNG_oFFs_SUPPORTED)
908 png_int_32 offset_x
, offset_y
;
911 if (png_get_oFFs(read_ptr
, read_info_ptr
,&offset_x
,&offset_y
,&unit_type
))
913 png_set_oFFs(write_ptr
, write_info_ptr
, offset_x
, offset_y
, unit_type
);
917 #if defined(PNG_pCAL_SUPPORTED)
919 png_charp purpose
, units
;
924 if (png_get_pCAL(read_ptr
, read_info_ptr
, &purpose
, &X0
, &X1
, &type
,
925 &nparams
, &units
, ¶ms
))
927 png_set_pCAL(write_ptr
, write_info_ptr
, purpose
, X0
, X1
, type
,
928 nparams
, units
, params
);
932 #if defined(PNG_pHYs_SUPPORTED)
934 png_uint_32 res_x
, res_y
;
937 if (png_get_pHYs(read_ptr
, read_info_ptr
, &res_x
, &res_y
, &unit_type
))
939 png_set_pHYs(write_ptr
, write_info_ptr
, res_x
, res_y
, unit_type
);
943 #if defined(PNG_sBIT_SUPPORTED)
945 png_color_8p sig_bit
;
947 if (png_get_sBIT(read_ptr
, read_info_ptr
, &sig_bit
))
949 png_set_sBIT(write_ptr
, write_info_ptr
, sig_bit
);
953 #if defined(PNG_sCAL_SUPPORTED)
954 #ifdef PNG_FLOATING_POINT_SUPPORTED
957 double scal_width
, scal_height
;
959 if (png_get_sCAL(read_ptr
, read_info_ptr
, &unit
, &scal_width
,
962 png_set_sCAL(write_ptr
, write_info_ptr
, unit
, scal_width
, scal_height
);
966 #ifdef PNG_FIXED_POINT_SUPPORTED
969 png_charp scal_width
, scal_height
;
971 if (png_get_sCAL_s(read_ptr
, read_info_ptr
, &unit
, &scal_width
,
974 png_set_sCAL_s(write_ptr
, write_info_ptr
, unit
, scal_width
, scal_height
);
980 #if defined(PNG_TEXT_SUPPORTED)
985 if (png_get_text(read_ptr
, read_info_ptr
, &text_ptr
, &num_text
) > 0)
987 png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks\n", num_text
);
988 png_set_text(write_ptr
, write_info_ptr
, text_ptr
, num_text
);
992 #if defined(PNG_tIME_SUPPORTED)
996 if (png_get_tIME(read_ptr
, read_info_ptr
, &mod_time
))
998 png_set_tIME(write_ptr
, write_info_ptr
, mod_time
);
999 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1000 /* we have to use png_strcpy instead of "=" because the string
1001 pointed to by png_convert_to_rfc1123() gets free'ed before
1003 png_strcpy(tIME_string
,png_convert_to_rfc1123(read_ptr
, mod_time
));
1004 tIME_chunk_present
++;
1005 #endif /* PNG_TIME_RFC1123_SUPPORTED */
1009 #if defined(PNG_tRNS_SUPPORTED)
1013 png_color_16p trans_values
;
1015 if (png_get_tRNS(read_ptr
, read_info_ptr
, &trans
, &num_trans
,
1018 png_set_tRNS(write_ptr
, write_info_ptr
, trans
, num_trans
,
1023 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
1025 png_unknown_chunkp unknowns
;
1026 int num_unknowns
= (int)png_get_unknown_chunks(read_ptr
, read_info_ptr
,
1031 png_set_unknown_chunks(write_ptr
, write_info_ptr
, unknowns
,
1033 /* copy the locations from the read_info_ptr. The automatically
1034 generated locations in write_info_ptr are wrong because we
1035 haven't written anything yet */
1036 for (i
= 0; i
< (png_size_t
)num_unknowns
; i
++)
1037 png_set_unknown_chunk_location(write_ptr
, write_info_ptr
, i
,
1038 unknowns
[i
].location
);
1043 #ifdef PNG_WRITE_SUPPORTED
1044 png_debug(0, "\nWriting info struct\n");
1046 /* If we wanted, we could write info in two steps:
1047 png_write_info_before_PLTE(write_ptr, write_info_ptr);
1049 png_write_info(write_ptr
, write_info_ptr
);
1052 #ifdef SINGLE_ROWBUF_ALLOC
1053 png_debug(0, "\nAllocating row buffer...");
1054 row_buf
= (png_bytep
)png_malloc(read_ptr
,
1055 png_get_rowbytes(read_ptr
, read_info_ptr
));
1056 png_debug1(0, "0x%08lx\n\n", (unsigned long)row_buf
);
1057 #endif /* SINGLE_ROWBUF_ALLOC */
1058 png_debug(0, "Writing row data\n");
1060 #if defined(PNG_READ_INTERLACING_SUPPORTED) || \
1061 defined(PNG_WRITE_INTERLACING_SUPPORTED)
1062 num_pass
= png_set_interlace_handling(read_ptr
);
1063 # ifdef PNG_WRITE_SUPPORTED
1064 png_set_interlace_handling(write_ptr
);
1070 #ifdef PNGTEST_TIMING
1071 t_stop
= (float)clock();
1072 t_misc
+= (t_stop
- t_start
);
1075 for (pass
= 0; pass
< num_pass
; pass
++)
1077 png_debug1(0, "Writing row data for pass %d\n",pass
);
1078 for (y
= 0; y
< height
; y
++)
1080 #ifndef SINGLE_ROWBUF_ALLOC
1081 png_debug2(0, "\nAllocating row buffer (pass %d, y = %ld)...", pass
,y
);
1082 row_buf
= (png_bytep
)png_malloc(read_ptr
,
1083 png_get_rowbytes(read_ptr
, read_info_ptr
));
1084 png_debug2(0, "0x%08lx (%ld bytes)\n", (unsigned long)row_buf
,
1085 png_get_rowbytes(read_ptr
, read_info_ptr
));
1086 #endif /* !SINGLE_ROWBUF_ALLOC */
1087 png_read_rows(read_ptr
, (png_bytepp
)&row_buf
, png_bytepp_NULL
, 1);
1089 #ifdef PNG_WRITE_SUPPORTED
1090 #ifdef PNGTEST_TIMING
1091 t_stop
= (float)clock();
1092 t_decode
+= (t_stop
- t_start
);
1095 png_write_rows(write_ptr
, (png_bytepp
)&row_buf
, 1);
1096 #ifdef PNGTEST_TIMING
1097 t_stop
= (float)clock();
1098 t_encode
+= (t_stop
- t_start
);
1101 #endif /* PNG_WRITE_SUPPORTED */
1103 #ifndef SINGLE_ROWBUF_ALLOC
1104 png_debug2(0, "Freeing row buffer (pass %d, y = %ld)\n\n", pass
, y
);
1105 png_free(read_ptr
, row_buf
);
1106 #endif /* !SINGLE_ROWBUF_ALLOC */
1110 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1111 png_free_data(read_ptr
, read_info_ptr
, PNG_FREE_UNKN
, -1);
1113 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
1114 png_free_data(write_ptr
, write_info_ptr
, PNG_FREE_UNKN
, -1);
1117 png_debug(0, "Reading and writing end_info data\n");
1119 png_read_end(read_ptr
, end_info_ptr
);
1120 #if defined(PNG_TEXT_SUPPORTED)
1125 if (png_get_text(read_ptr
, end_info_ptr
, &text_ptr
, &num_text
) > 0)
1127 png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks\n", num_text
);
1128 png_set_text(write_ptr
, write_end_info_ptr
, text_ptr
, num_text
);
1132 #if defined(PNG_tIME_SUPPORTED)
1136 if (png_get_tIME(read_ptr
, end_info_ptr
, &mod_time
))
1138 png_set_tIME(write_ptr
, write_end_info_ptr
, mod_time
);
1139 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1140 /* we have to use png_strcpy instead of "=" because the string
1141 pointed to by png_convert_to_rfc1123() gets free'ed before
1143 png_strcpy(tIME_string
,png_convert_to_rfc1123(read_ptr
, mod_time
));
1144 tIME_chunk_present
++;
1145 #endif /* PNG_TIME_RFC1123_SUPPORTED */
1149 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
1151 png_unknown_chunkp unknowns
;
1153 num_unknowns
= (int)png_get_unknown_chunks(read_ptr
, end_info_ptr
,
1158 png_set_unknown_chunks(write_ptr
, write_end_info_ptr
, unknowns
,
1160 /* copy the locations from the read_info_ptr. The automatically
1161 generated locations in write_end_info_ptr are wrong because we
1162 haven't written the end_info yet */
1163 for (i
= 0; i
< (png_size_t
)num_unknowns
; i
++)
1164 png_set_unknown_chunk_location(write_ptr
, write_end_info_ptr
, i
,
1165 unknowns
[i
].location
);
1169 #ifdef PNG_WRITE_SUPPORTED
1170 png_write_end(write_ptr
, write_end_info_ptr
);
1173 #ifdef PNG_EASY_ACCESS_SUPPORTED
1176 png_uint_32 iwidth
, iheight
;
1177 iwidth
= png_get_image_width(write_ptr
, write_info_ptr
);
1178 iheight
= png_get_image_height(write_ptr
, write_info_ptr
);
1179 fprintf(STDERR
, "Image width = %lu, height = %lu\n",
1184 png_debug(0, "Destroying data structs\n");
1185 #ifdef SINGLE_ROWBUF_ALLOC
1186 png_debug(1, "destroying row_buf for read_ptr\n");
1187 png_free(read_ptr
, row_buf
);
1189 #endif /* SINGLE_ROWBUF_ALLOC */
1190 png_debug(1, "destroying read_ptr, read_info_ptr, end_info_ptr\n");
1191 png_destroy_read_struct(&read_ptr
, &read_info_ptr
, &end_info_ptr
);
1192 #ifdef PNG_WRITE_SUPPORTED
1193 png_debug(1, "destroying write_end_info_ptr\n");
1194 png_destroy_info_struct(write_ptr
, &write_end_info_ptr
);
1195 png_debug(1, "destroying write_ptr, write_info_ptr\n");
1196 png_destroy_write_struct(&write_ptr
, &write_info_ptr
);
1198 png_debug(0, "Destruction complete.\n");
1203 png_debug(0, "Opening files for comparison\n");
1204 #if defined(_WIN32_WCE)
1205 MultiByteToWideChar(CP_ACP
, 0, inname
, -1, path
, MAX_PATH
);
1206 if ((fpin
= CreateFile(path
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
)) == INVALID_HANDLE_VALUE
)
1208 if ((fpin
= fopen(inname
, "rb")) == NULL
)
1211 fprintf(STDERR
, "Could not find file %s\n", inname
);
1215 #if defined(_WIN32_WCE)
1216 MultiByteToWideChar(CP_ACP
, 0, outname
, -1, path
, MAX_PATH
);
1217 if ((fpout
= CreateFile(path
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
)) == INVALID_HANDLE_VALUE
)
1219 if ((fpout
= fopen(outname
, "rb")) == NULL
)
1222 fprintf(STDERR
, "Could not find file %s\n", outname
);
1229 png_size_t num_in
, num_out
;
1231 READFILE(fpin
, inbuf
, 1, num_in
);
1232 READFILE(fpout
, outbuf
, 1, num_out
);
1234 if (num_in
!= num_out
)
1236 fprintf(STDERR
, "\nFiles %s and %s are of a different size\n",
1238 if(wrote_question
== 0)
1241 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
1242 inname
,PNG_ZBUF_SIZE
);
1244 "\n filtering heuristic (libpng default), compression");
1246 " level (zlib default),\n and zlib version (%s)?\n\n",
1258 if (png_memcmp(inbuf
, outbuf
, num_in
))
1260 fprintf(STDERR
, "\nFiles %s and %s are different\n", inname
, outname
);
1261 if(wrote_question
== 0)
1264 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
1265 inname
,PNG_ZBUF_SIZE
);
1267 "\n filtering heuristic (libpng default), compression");
1269 " level (zlib default),\n and zlib version (%s)?\n\n",
1285 /* input and output filenames */
1287 static PNG_CONST
char *inname
= "pngtest/png";
1288 static PNG_CONST
char *outname
= "pngout/png";
1290 static PNG_CONST
char *inname
= "pngtest.png";
1291 static PNG_CONST
char *outname
= "pngout.png";
1295 main(int argc
, char *argv
[])
1300 fprintf(STDERR
, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING
);
1301 fprintf(STDERR
, " with zlib version %s\n", ZLIB_VERSION
);
1302 fprintf(STDERR
,"%s",png_get_copyright(NULL
));
1303 /* Show the version of libpng used in building the library */
1304 fprintf(STDERR
," library (%lu):%s", png_access_version_number(),
1305 png_get_header_version(NULL
));
1306 /* Show the version of libpng used in building the application */
1307 fprintf(STDERR
," pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER
,
1308 PNG_HEADER_VERSION_STRING
);
1309 fprintf(STDERR
," sizeof(png_struct)=%ld, sizeof(png_info)=%ld\n",
1310 (long)sizeof(png_struct
), (long)sizeof(png_info
));
1312 /* Do some consistency checking on the memory allocation settings, I'm
1313 not sure this matters, but it is nice to know, the first of these
1314 tests should be impossible because of the way the macros are set
1316 #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
1317 fprintf(STDERR
, " NOTE: Zlib compiled for max 64k, libpng not\n");
1319 /* I think the following can happen. */
1320 #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
1321 fprintf(STDERR
, " NOTE: libpng compiled for max 64k, zlib not\n");
1324 if (strcmp(png_libpng_ver
, PNG_LIBPNG_VER_STRING
))
1327 "Warning: versions are different between png.h and png.c\n");
1328 fprintf(STDERR
, " png.h version: %s\n", PNG_LIBPNG_VER_STRING
);
1329 fprintf(STDERR
, " png.c version: %s\n\n", png_libpng_ver
);
1335 if (strcmp(argv
[1], "-m") == 0)
1338 status_dots_requested
= 0;
1340 else if (strcmp(argv
[1], "-mv") == 0 ||
1341 strcmp(argv
[1], "-vm") == 0 )
1345 status_dots_requested
= 1;
1347 else if (strcmp(argv
[1], "-v") == 0)
1350 status_dots_requested
= 1;
1356 status_dots_requested
= 0;
1360 if (!multiple
&& argc
== 3+verbose
)
1361 outname
= argv
[2+verbose
];
1363 if ((!multiple
&& argc
> 3+verbose
) || (multiple
&& argc
< 2))
1366 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
1369 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1371 " with -m %s is used as a temporary file\n", outname
);
1378 #ifdef PNG_USER_MEM_SUPPORTED
1379 int allocation_now
= current_allocation
;
1381 for (i
=2; i
<argc
; ++i
)
1383 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1387 fprintf(STDERR
, "Testing %s:",argv
[i
]);
1388 kerror
= test_one_file(argv
[i
], outname
);
1391 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1392 fprintf(STDERR
, "\n PASS (%lu zero samples)\n",zero_samples
);
1394 fprintf(STDERR
, " PASS\n");
1396 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1397 for (k
=0; k
<256; k
++)
1399 fprintf(STDERR
, " Filter %d was used %lu times\n",
1402 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1403 if(tIME_chunk_present
!= 0)
1404 fprintf(STDERR
, " tIME = %s\n",tIME_string
);
1405 tIME_chunk_present
= 0;
1406 #endif /* PNG_TIME_RFC1123_SUPPORTED */
1410 fprintf(STDERR
, " FAIL\n");
1413 #ifdef PNG_USER_MEM_SUPPORTED
1414 if (allocation_now
!= current_allocation
)
1415 fprintf(STDERR
, "MEMORY ERROR: %d bytes lost\n",
1416 current_allocation
-allocation_now
);
1417 if (current_allocation
!= 0)
1419 memory_infop pinfo
= pinformation
;
1421 fprintf(STDERR
, "MEMORY ERROR: %d bytes still allocated\n",
1422 current_allocation
);
1423 while (pinfo
!= NULL
)
1425 fprintf(STDERR
, " %lu bytes at %x\n", pinfo
->size
,
1426 (unsigned int) pinfo
->pointer
);
1427 pinfo
= pinfo
->next
;
1432 #ifdef PNG_USER_MEM_SUPPORTED
1433 fprintf(STDERR
, " Current memory allocation: %10d bytes\n",
1434 current_allocation
);
1435 fprintf(STDERR
, " Maximum memory allocation: %10d bytes\n",
1436 maximum_allocation
);
1437 fprintf(STDERR
, " Total memory allocation: %10d bytes\n",
1439 fprintf(STDERR
, " Number of allocations: %10d\n",
1449 #ifdef PNG_USER_MEM_SUPPORTED
1450 int allocation_now
= current_allocation
;
1452 if (i
== 1) status_dots_requested
= 1;
1453 else if(verbose
== 0)status_dots_requested
= 0;
1454 if (i
== 0 || verbose
== 1 || ierror
!= 0)
1455 fprintf(STDERR
, "Testing %s:",inname
);
1456 kerror
= test_one_file(inname
, outname
);
1459 if(verbose
== 1 || i
== 2)
1461 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1464 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1465 fprintf(STDERR
, "\n PASS (%lu zero samples)\n",zero_samples
);
1467 fprintf(STDERR
, " PASS\n");
1469 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1470 for (k
=0; k
<256; k
++)
1472 fprintf(STDERR
, " Filter %d was used %lu times\n",
1475 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1476 if(tIME_chunk_present
!= 0)
1477 fprintf(STDERR
, " tIME = %s\n",tIME_string
);
1478 #endif /* PNG_TIME_RFC1123_SUPPORTED */
1483 if(verbose
== 0 && i
!= 2)
1484 fprintf(STDERR
, "Testing %s:",inname
);
1485 fprintf(STDERR
, " FAIL\n");
1488 #ifdef PNG_USER_MEM_SUPPORTED
1489 if (allocation_now
!= current_allocation
)
1490 fprintf(STDERR
, "MEMORY ERROR: %d bytes lost\n",
1491 current_allocation
-allocation_now
);
1492 if (current_allocation
!= 0)
1494 memory_infop pinfo
= pinformation
;
1496 fprintf(STDERR
, "MEMORY ERROR: %d bytes still allocated\n",
1497 current_allocation
);
1498 while (pinfo
!= NULL
)
1500 fprintf(STDERR
," %lu bytes at %x\n",
1501 pinfo
->size
, (unsigned int)pinfo
->pointer
);
1502 pinfo
= pinfo
->next
;
1507 #ifdef PNG_USER_MEM_SUPPORTED
1508 fprintf(STDERR
, " Current memory allocation: %10d bytes\n",
1509 current_allocation
);
1510 fprintf(STDERR
, " Maximum memory allocation: %10d bytes\n",
1511 maximum_allocation
);
1512 fprintf(STDERR
, " Total memory allocation: %10d bytes\n",
1514 fprintf(STDERR
, " Number of allocations: %10d\n",
1519 #ifdef PNGTEST_TIMING
1520 t_stop
= (float)clock();
1521 t_misc
+= (t_stop
- t_start
);
1523 fprintf(STDERR
," CPU time used = %.3f seconds",
1524 (t_misc
+t_decode
+t_encode
)/(float)CLOCKS_PER_SEC
);
1525 fprintf(STDERR
," (decoding %.3f,\n",
1526 t_decode
/(float)CLOCKS_PER_SEC
);
1527 fprintf(STDERR
," encoding %.3f ,",
1528 t_encode
/(float)CLOCKS_PER_SEC
);
1529 fprintf(STDERR
," other %.3f seconds)\n\n",
1530 t_misc
/(float)CLOCKS_PER_SEC
);
1534 fprintf(STDERR
, "libpng passes test\n");
1536 fprintf(STDERR
, "libpng FAILS test\n");
1537 return (int)(ierror
!= 0);
1540 /* Generate a compiler error if there is an old png.h in the search path. */
1541 typedef version_1_2_5rc3 your_png_h_is_not_version_1_2_5rc3
;