2 /* pngtest.c - a simple test program to test libpng
4 * libpng 1.2.0 - September 1, 2001
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2001 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
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;
120 read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
122 read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
124 if(png_ptr == NULL || row_number > PNG_MAX_UINT) return;
125 if(status_pass != pass)
127 fprintf(stdout,"\n Pass %d: ",pass);
134 fprintf(stdout, "\n ");
137 fprintf(stdout, "r");
141 write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
143 write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
145 if(png_ptr == NULL || row_number > PNG_MAX_UINT || pass > 7) return;
146 fprintf(stdout, "w");
150 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
151 /* Example of using user transform callback (we don't transform anything,
152 but merely examine the row filters. We set this to 256 rather than
153 5 in case illegal filter values are present.) */
154 static png_uint_32 filters_used[256];
156 count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data);
158 count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
160 if(png_ptr != NULL && row_info != NULL)
161 ++filters_used[*(data-1)];
165 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
166 /* example of using user transform callback (we don't transform anything,
167 but merely count the zero samples) */
169 static png_uint_32 zero_samples;
172 count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data);
174 count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
177 if(png_ptr == NULL)return;
179 /* contents of row_info:
180 * png_uint_32 width width of row
181 * png_uint_32 rowbytes number of bytes in row
182 * png_byte color_type color type of pixels
183 * png_byte bit_depth bit depth of samples
184 * png_byte channels number of channels (1-4)
185 * png_byte pixel_depth bits per pixel (depth*channels)
189 /* counts the number of zero samples (or zero pixels if color_type is 3 */
191 if(row_info->color_type == 0 || row_info->color_type == 3)
194 png_uint_32 n, nstop;
195 for (n=0, nstop=row_info->width; n<nstop; n++)
197 if(row_info->bit_depth == 1)
199 if(((*dp << pos++ ) & 0x80) == 0) zero_samples++;
206 if(row_info->bit_depth == 2)
208 if(((*dp << (pos+=2)) & 0xc0) == 0) zero_samples++;
215 if(row_info->bit_depth == 4)
217 if(((*dp << (pos+=4)) & 0xf0) == 0) zero_samples++;
224 if(row_info->bit_depth == 8)
225 if(*dp++ == 0) zero_samples++;
226 if(row_info->bit_depth == 16)
228 if((*dp | *(dp+1)) == 0) zero_samples++;
233 else /* other color types */
235 png_uint_32 n, nstop;
237 int color_channels = row_info->channels;
238 if(row_info->color_type > 3)color_channels--;
240 for (n=0, nstop=row_info->width; n<nstop; n++)
242 for (channel = 0; channel < color_channels; channel++)
244 if(row_info->bit_depth == 8)
245 if(*dp++ == 0) zero_samples++;
246 if(row_info->bit_depth == 16)
248 if((*dp | *(dp+1)) == 0) zero_samples++;
252 if(row_info->color_type > 3)
255 if(row_info->bit_depth == 16)dp++;
260 #endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
262 static int wrote_question = 0;
264 #if defined(PNG_NO_STDIO)
265 /* START of code to validate stdio-free compilation */
266 /* These copies of the default read/write functions come from pngrio.c and */
267 /* pngwio.c. They allow "don't include stdio" testing of the library. */
268 /* This is the function that does the actual reading of data. If you are
269 not reading from a standard C stream, you should create a replacement
270 read_data function and use it at run time with png_set_read_fn(), rather
271 than changing the library. */
273 #ifndef USE_FAR_KEYWORD
275 pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
279 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
280 * instead of an int, which is what fread() actually returns.
282 READFILE((png_FILE_p)png_ptr->io_ptr, data, length, check);
286 png_error(png_ptr, "Read Error!");
290 /* this is the model-independent version. Since the standard I/O library
291 can't handle far buffers in the medium and small models, we have to copy
295 #define NEAR_BUF_SIZE 1024
296 #define MIN(a,b) (a <= b ? a : b)
299 pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
305 /* Check if data really is near. If so, use usual code. */
306 n_data = (png_byte *)CVT_PTR_NOCHECK(data);
307 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
308 if ((png_bytep)n_data == data)
310 READFILE(io_ptr, n_data, length, check);
314 png_byte buf[NEAR_BUF_SIZE];
315 png_size_t read, remaining, err;
320 read = MIN(NEAR_BUF_SIZE, remaining);
321 READFILE(io_ptr, buf, 1, err);
322 png_memcpy(data, buf, read); /* copy far buffer to near buffer */
330 while (remaining != 0);
334 png_error(png_ptr, "read Error");
337 #endif /* USE_FAR_KEYWORD */
339 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
341 pngtest_flush(png_structp png_ptr)
343 #if !defined(_WIN32_WCE)
345 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
352 /* This is the function that does the actual writing of data. If you are
353 not writing to a standard C stream, you should create a replacement
354 write_data function and use it at run time with png_set_write_fn(), rather
355 than changing the library. */
356 #ifndef USE_FAR_KEYWORD
358 pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
362 WRITEFILE((png_FILE_p)png_ptr->io_ptr, data, length, check);
365 png_error(png_ptr, "Write Error");
369 /* this is the model-independent version. Since the standard I/O library
370 can't handle far buffers in the medium and small models, we have to copy
374 #define NEAR_BUF_SIZE 1024
375 #define MIN(a,b) (a <= b ? a : b)
378 pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
381 png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
384 /* Check if data really is near. If so, use usual code. */
385 near_data = (png_byte *)CVT_PTR_NOCHECK(data);
386 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
387 if ((png_bytep)near_data == data)
389 WRITEFILE(io_ptr, near_data, length, check);
393 png_byte buf[NEAR_BUF_SIZE];
394 png_size_t written, remaining, err;
399 written = MIN(NEAR_BUF_SIZE, remaining);
400 png_memcpy(buf, data, written); /* copy far buffer to near buffer */
401 WRITEFILE(io_ptr, buf, written, err);
407 remaining -= written;
409 while (remaining != 0);
413 png_error(png_ptr, "Write Error");
417 #endif /* USE_FAR_KEYWORD */
419 /* This function is called when there is a warning, but the library thinks
420 * it can continue anyway. Replacement functions don't have to do anything
421 * here if you don't want to. In the default configuration, png_ptr is
422 * not used, but it is passed in case it may be useful.
425 pngtest_warning(png_structp png_ptr, png_const_charp message)
427 PNG_CONST char *name = "UNKNOWN (ERROR!)";
428 if (png_ptr != NULL && png_ptr->error_ptr != NULL)
429 name = png_ptr->error_ptr;
430 fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
433 /* This is the default error handling function. Note that replacements for
434 * this function MUST NOT RETURN, or the program will likely crash. This
435 * function is used by default, or if the program supplies NULL for the
436 * error function pointer in png_set_error_fn().
439 pngtest_error(png_structp png_ptr, png_const_charp message)
441 pngtest_warning(png_ptr, message);
442 /* We can return because png_error calls the default handler, which is
443 * actually OK in this case. */
445 #endif /* PNG_NO_STDIO */
446 /* END of code to validate stdio-free compilation */
448 /* START of code to validate memory allocation and deallocation */
449 #ifdef PNG_USER_MEM_SUPPORTED
451 /* Allocate memory. For reasonable files, size should never exceed
452 64K. However, zlib may allocate more then 64K if you don't tell
453 it not to. See zconf.h and png.h for more information. zlib does
454 need to allocate exactly 64K, so whatever you call here must
455 have the ability to do that.
457 This piece of code can be compiled to validate max 64K allocations
458 by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K. */
459 typedef struct memory_information
463 struct memory_information FAR *next;
464 } memory_information;
465 typedef memory_information FAR *memory_infop;
467 static memory_infop pinformation = NULL;
468 static int current_allocation = 0;
469 static int maximum_allocation = 0;
470 static int total_allocation = 0;
471 static int num_allocations = 0;
473 png_voidp png_debug_malloc PNGARG((png_structp png_ptr, png_uint_32 size));
474 void png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
477 png_debug_malloc(png_structp png_ptr, png_uint_32 size)
480 /* png_malloc has already tested for NULL; png_create_struct calls
481 png_debug_malloc directly, with png_ptr == NULL which is OK */
484 return (png_voidp)(NULL);
486 /* This calls the library allocator twice, once to get the requested
487 buffer and once to get a new free list entry. */
489 memory_infop pinfo = (memory_infop)png_malloc_default(png_ptr,
490 (png_uint_32)sizeof *pinfo);
492 current_allocation += size;
493 total_allocation += size;
495 if (current_allocation > maximum_allocation)
496 maximum_allocation = current_allocation;
497 pinfo->pointer = (png_voidp)png_malloc_default(png_ptr, size);
498 pinfo->next = pinformation;
499 pinformation = pinfo;
500 /* Make sure the caller isn't assuming zeroed memory. */
501 png_memset(pinfo->pointer, 0xdd, pinfo->size);
504 printf("png_malloc %lu bytes at %x\n",size,pinfo->pointer);
506 assert(pinfo->size != 12345678);
507 return (png_voidp)(pinfo->pointer);
511 /* Free a pointer. It is removed from the list at the same time. */
513 png_debug_free(png_structp png_ptr, png_voidp ptr)
516 fprintf(STDERR, "NULL pointer to png_debug_free.\n");
519 #if 0 /* This happens all the time. */
520 fprintf(STDERR, "WARNING: freeing NULL pointer\n");
525 /* Unlink the element from the list. */
527 memory_infop FAR *ppinfo = &pinformation;
530 memory_infop pinfo = *ppinfo;
531 if (pinfo->pointer == ptr)
533 *ppinfo = pinfo->next;
534 current_allocation -= pinfo->size;
535 if (current_allocation < 0)
536 fprintf(STDERR, "Duplicate free of memory\n");
537 /* We must free the list element too, but first kill
538 the memory that is to be freed. */
539 png_memset(ptr, 0x55, pinfo->size);
540 png_free_default(png_ptr, pinfo);
544 if (pinfo->next == NULL)
546 fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr);
549 ppinfo = &pinfo->next;
553 /* Finally free the data. */
556 printf("Freeing %x\n",ptr);
558 png_free_default(png_ptr, ptr);
561 #endif /* PNG_USER_MEM_SUPPORTED */
562 /* END of code to test memory allocation/deallocation */
566 test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
568 static png_FILE_p fpin;
569 static png_FILE_p fpout; /* "static" prevents setjmp corruption */
570 png_structp read_ptr;
571 png_infop read_info_ptr, end_info_ptr;
572 #ifdef PNG_WRITE_SUPPORTED
573 png_structp write_ptr;
574 png_infop write_info_ptr;
575 png_infop write_end_info_ptr;
577 png_structp write_ptr = NULL;
578 png_infop write_info_ptr = NULL;
579 png_infop write_end_info_ptr = NULL;
583 png_uint_32 width, height;
585 int bit_depth, color_type;
586 #ifdef PNG_SETJMP_SUPPORTED
587 #ifdef USE_FAR_KEYWORD
592 #if defined(_WIN32_WCE)
593 TCHAR path[MAX_PATH];
595 char inbuf[256], outbuf[256];
597 row_buf = (png_bytep)NULL;
599 #if defined(_WIN32_WCE)
600 MultiByteToWideChar(CP_ACP, 0, inname, -1, path, MAX_PATH);
601 if ((fpin = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
603 if ((fpin = fopen(inname, "rb")) == NULL)
606 fprintf(STDERR, "Could not find input file %s\n", inname);
610 #if defined(_WIN32_WCE)
611 MultiByteToWideChar(CP_ACP, 0, outname, -1, path, MAX_PATH);
612 if ((fpout = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)) == INVALID_HANDLE_VALUE)
614 if ((fpout = fopen(outname, "wb")) == NULL)
617 fprintf(STDERR, "Could not open output file %s\n", outname);
622 png_debug(0, "Allocating read and write structures\n");
623 #ifdef PNG_USER_MEM_SUPPORTED
624 read_ptr = png_create_read_struct_2(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
625 (png_error_ptr)NULL, (png_error_ptr)NULL, (png_voidp)NULL,
626 (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
628 read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
629 (png_error_ptr)NULL, (png_error_ptr)NULL);
631 #if defined(PNG_NO_STDIO)
632 png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
635 #ifdef PNG_WRITE_SUPPORTED
636 #ifdef PNG_USER_MEM_SUPPORTED
637 write_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
638 (png_error_ptr)NULL, (png_error_ptr)NULL, (png_voidp)NULL,
639 (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
641 write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
642 (png_error_ptr)NULL, (png_error_ptr)NULL);
644 #if defined(PNG_NO_STDIO)
645 png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
649 png_debug(0, "Allocating read_info, write_info and end_info structures\n");
650 read_info_ptr = png_create_info_struct(read_ptr);
651 end_info_ptr = png_create_info_struct(read_ptr);
652 #ifdef PNG_WRITE_SUPPORTED
653 write_info_ptr = png_create_info_struct(write_ptr);
654 write_end_info_ptr = png_create_info_struct(write_ptr);
657 #ifdef PNG_SETJMP_SUPPORTED
658 png_debug(0, "Setting jmpbuf for read struct\n");
659 #ifdef USE_FAR_KEYWORD
662 if (setjmp(png_jmpbuf(read_ptr)))
665 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
666 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
667 #ifdef PNG_WRITE_SUPPORTED
668 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
669 png_destroy_write_struct(&write_ptr, &write_info_ptr);
675 #ifdef USE_FAR_KEYWORD
676 png_memcpy(png_jmpbuf(read_ptr),jmpbuf,sizeof(jmp_buf));
679 #ifdef PNG_WRITE_SUPPORTED
680 png_debug(0, "Setting jmpbuf for write struct\n");
681 #ifdef USE_FAR_KEYWORD
684 if (setjmp(png_jmpbuf(write_ptr)))
687 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
688 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
689 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
690 #ifdef PNG_WRITE_SUPPORTED
691 png_destroy_write_struct(&write_ptr, &write_info_ptr);
697 #ifdef USE_FAR_KEYWORD
698 png_memcpy(png_jmpbuf(write_ptr),jmpbuf,sizeof(jmp_buf));
703 png_debug(0, "Initializing input and output streams\n");
704 #if !defined(PNG_NO_STDIO)
705 png_init_io(read_ptr, fpin);
706 # ifdef PNG_WRITE_SUPPORTED
707 png_init_io(write_ptr, fpout);
710 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
711 # ifdef PNG_WRITE_SUPPORTED
712 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
713 # if defined(PNG_WRITE_FLUSH_SUPPORTED)
720 if(status_dots_requested == 1)
722 #ifdef PNG_WRITE_SUPPORTED
723 png_set_write_status_fn(write_ptr, write_row_callback);
725 png_set_read_status_fn(read_ptr, read_row_callback);
729 #ifdef PNG_WRITE_SUPPORTED
730 png_set_write_status_fn(write_ptr, NULL);
732 png_set_read_status_fn(read_ptr, NULL);
735 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
740 png_set_read_user_transform_fn(read_ptr, count_filters);
743 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
745 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
748 #define HANDLE_CHUNK_IF_SAFE 2
749 #define HANDLE_CHUNK_ALWAYS 3
750 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
751 png_set_keep_unknown_chunks(read_ptr, HANDLE_CHUNK_ALWAYS, NULL, 0);
753 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
754 png_set_keep_unknown_chunks(write_ptr, HANDLE_CHUNK_IF_SAFE, NULL, 0);
757 png_debug(0, "Reading info struct\n");
758 png_read_info(read_ptr, read_info_ptr);
760 png_debug(0, "Transferring info struct\n");
762 int interlace_type, compression_type, filter_type;
764 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
765 &color_type, &interlace_type, &compression_type, &filter_type))
767 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
768 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
769 color_type, interlace_type, compression_type, filter_type);
771 color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
775 #if defined(PNG_FIXED_POINT_SUPPORTED)
776 #if defined(PNG_cHRM_SUPPORTED)
778 png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
780 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
781 &red_y, &green_x, &green_y, &blue_x, &blue_y))
783 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
784 red_y, green_x, green_y, blue_x, blue_y);
788 #if defined(PNG_gAMA_SUPPORTED)
790 png_fixed_point gamma;
792 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
794 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
798 #else /* Use floating point versions */
799 #if defined(PNG_FLOATING_POINT_SUPPORTED)
800 #if defined(PNG_cHRM_SUPPORTED)
802 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
804 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
805 &red_y, &green_x, &green_y, &blue_x, &blue_y))
807 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
808 red_y, green_x, green_y, blue_x, blue_y);
812 #if defined(PNG_gAMA_SUPPORTED)
816 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
818 png_set_gAMA(write_ptr, write_info_ptr, gamma);
822 #endif /* floating point */
823 #endif /* fixed point */
824 #if defined(PNG_iCCP_SUPPORTED)
829 int compression_type;
831 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
834 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
839 #if defined(PNG_sRGB_SUPPORTED)
843 if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
845 png_set_sRGB(write_ptr, write_info_ptr, intent);
853 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
855 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
858 #if defined(PNG_bKGD_SUPPORTED)
860 png_color_16p background;
862 if (png_get_bKGD(read_ptr, read_info_ptr, &background))
864 png_set_bKGD(write_ptr, write_info_ptr, background);
868 #if defined(PNG_hIST_SUPPORTED)
872 if (png_get_hIST(read_ptr, read_info_ptr, &hist))
874 png_set_hIST(write_ptr, write_info_ptr, hist);
878 #if defined(PNG_oFFs_SUPPORTED)
880 png_int_32 offset_x, offset_y;
883 if (png_get_oFFs(read_ptr, read_info_ptr,&offset_x,&offset_y,&unit_type))
885 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
889 #if defined(PNG_pCAL_SUPPORTED)
891 png_charp purpose, units;
896 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
897 &nparams, &units, ¶ms))
899 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
900 nparams, units, params);
904 #if defined(PNG_pHYs_SUPPORTED)
906 png_uint_32 res_x, res_y;
909 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
911 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
915 #if defined(PNG_sBIT_SUPPORTED)
917 png_color_8p sig_bit;
919 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
921 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
925 #if defined(PNG_sCAL_SUPPORTED)
926 #ifdef PNG_FLOATING_POINT_SUPPORTED
929 double scal_width, scal_height;
931 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
934 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
938 #ifdef PNG_FIXED_POINT_SUPPORTED
941 png_charp scal_width, scal_height;
943 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
946 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width, scal_height);
952 #if defined(PNG_TEXT_SUPPORTED)
957 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
959 png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks\n", num_text);
960 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
964 #if defined(PNG_tIME_SUPPORTED)
968 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
970 png_set_tIME(write_ptr, write_info_ptr, mod_time);
971 #if defined(PNG_TIME_RFC1123_SUPPORTED)
972 /* we have to use png_strcpy instead of "=" because the string
973 pointed to by png_convert_to_rfc1123() gets free'ed before
975 png_strcpy(tIME_string,png_convert_to_rfc1123(read_ptr, mod_time));
976 tIME_chunk_present++;
977 #endif /* PNG_TIME_RFC1123_SUPPORTED */
981 #if defined(PNG_tRNS_SUPPORTED)
985 png_color_16p trans_values;
987 if (png_get_tRNS(read_ptr, read_info_ptr, &trans, &num_trans,
990 png_set_tRNS(write_ptr, write_info_ptr, trans, num_trans,
995 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
997 png_unknown_chunkp unknowns;
998 int num_unknowns = (int)png_get_unknown_chunks(read_ptr, read_info_ptr,
1003 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1005 /* copy the locations from the read_info_ptr. The automatically
1006 generated locations in write_info_ptr are wrong because we
1007 haven't written anything yet */
1008 for (i = 0; i < (png_size_t)num_unknowns; i++)
1009 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1010 unknowns[i].location);
1015 #ifdef PNG_WRITE_SUPPORTED
1016 png_debug(0, "\nWriting info struct\n");
1018 /* If we wanted, we could write info in two steps:
1019 png_write_info_before_PLTE(write_ptr, write_info_ptr);
1021 png_write_info(write_ptr, write_info_ptr);
1024 #ifdef SINGLE_ROWBUF_ALLOC
1025 png_debug(0, "\nAllocating row buffer...");
1026 row_buf = (png_bytep)png_malloc(read_ptr,
1027 png_get_rowbytes(read_ptr, read_info_ptr));
1028 png_debug1(0, "0x%08lx\n\n", (unsigned long)row_buf);
1029 #endif /* SINGLE_ROWBUF_ALLOC */
1030 png_debug(0, "Writing row data\n");
1032 #if defined(PNG_READ_INTERLACING_SUPPORTED) || \
1033 defined(PNG_WRITE_INTERLACING_SUPPORTED)
1034 num_pass = png_set_interlace_handling(read_ptr);
1035 # ifdef PNG_WRITE_SUPPORTED
1036 png_set_interlace_handling(write_ptr);
1042 #ifdef PNGTEST_TIMING
1043 t_stop = (float)clock();
1044 t_misc += (t_stop - t_start);
1047 for (pass = 0; pass < num_pass; pass++)
1049 png_debug1(0, "Writing row data for pass %d\n",pass);
1050 for (y = 0; y < height; y++)
1052 #ifndef SINGLE_ROWBUF_ALLOC
1053 png_debug2(0, "\nAllocating row buffer (pass %d, y = %ld)...", pass,y);
1054 row_buf = (png_bytep)png_malloc(read_ptr,
1055 png_get_rowbytes(read_ptr, read_info_ptr));
1056 png_debug2(0, "0x%08lx (%ld bytes)\n", (unsigned long)row_buf,
1057 png_get_rowbytes(read_ptr, read_info_ptr));
1058 #endif /* !SINGLE_ROWBUF_ALLOC */
1059 png_read_rows(read_ptr, (png_bytepp)&row_buf, (png_bytepp)NULL, 1);
1061 #ifdef PNG_WRITE_SUPPORTED
1062 #ifdef PNGTEST_TIMING
1063 t_stop = (float)clock();
1064 t_decode += (t_stop - t_start);
1067 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
1068 #ifdef PNGTEST_TIMING
1069 t_stop = (float)clock();
1070 t_encode += (t_stop - t_start);
1073 #endif /* PNG_WRITE_SUPPORTED */
1075 #ifndef SINGLE_ROWBUF_ALLOC
1076 png_debug2(0, "Freeing row buffer (pass %d, y = %ld)\n\n", pass, y);
1077 png_free(read_ptr, row_buf);
1078 #endif /* !SINGLE_ROWBUF_ALLOC */
1082 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1083 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
1085 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
1086 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
1089 png_debug(0, "Reading and writing end_info data\n");
1091 png_read_end(read_ptr, end_info_ptr);
1092 #if defined(PNG_TEXT_SUPPORTED)
1097 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1099 png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks\n", num_text);
1100 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1104 #if defined(PNG_tIME_SUPPORTED)
1108 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
1110 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
1111 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1112 /* we have to use png_strcpy instead of "=" because the string
1113 pointed to by png_convert_to_rfc1123() gets free'ed before
1115 png_strcpy(tIME_string,png_convert_to_rfc1123(read_ptr, mod_time));
1116 tIME_chunk_present++;
1117 #endif /* PNG_TIME_RFC1123_SUPPORTED */
1121 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
1123 png_unknown_chunkp unknowns;
1125 num_unknowns = (int)png_get_unknown_chunks(read_ptr, end_info_ptr,
1130 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1132 /* copy the locations from the read_info_ptr. The automatically
1133 generated locations in write_end_info_ptr are wrong because we
1134 haven't written the end_info yet */
1135 for (i = 0; i < (png_size_t)num_unknowns; i++)
1136 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1137 unknowns[i].location);
1141 #ifdef PNG_WRITE_SUPPORTED
1142 png_write_end(write_ptr, write_end_info_ptr);
1145 #ifdef PNG_EASY_ACCESS_SUPPORTED
1148 png_uint_32 iwidth, iheight;
1149 iwidth = png_get_image_width(write_ptr, write_info_ptr);
1150 iheight = png_get_image_height(write_ptr, write_info_ptr);
1151 fprintf(STDERR, "Image width = %lu, height = %lu\n",
1156 png_debug(0, "Destroying data structs\n");
1157 #ifdef SINGLE_ROWBUF_ALLOC
1158 png_debug(1, "destroying row_buf for read_ptr\n");
1159 png_free(read_ptr, row_buf);
1161 #endif /* SINGLE_ROWBUF_ALLOC */
1162 png_debug(1, "destroying read_ptr, read_info_ptr, end_info_ptr\n");
1163 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
1164 #ifdef PNG_WRITE_SUPPORTED
1165 png_debug(1, "destroying write_end_info_ptr\n");
1166 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
1167 png_debug(1, "destroying write_ptr, write_info_ptr\n");
1168 png_destroy_write_struct(&write_ptr, &write_info_ptr);
1170 png_debug(0, "Destruction complete.\n");
1175 png_debug(0, "Opening files for comparison\n");
1176 #if defined(_WIN32_WCE)
1177 MultiByteToWideChar(CP_ACP, 0, inname, -1, path, MAX_PATH);
1178 if ((fpin = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
1180 if ((fpin = fopen(inname, "rb")) == NULL)
1183 fprintf(STDERR, "Could not find file %s\n", inname);
1187 #if defined(_WIN32_WCE)
1188 MultiByteToWideChar(CP_ACP, 0, outname, -1, path, MAX_PATH);
1189 if ((fpout = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
1191 if ((fpout = fopen(outname, "rb")) == NULL)
1194 fprintf(STDERR, "Could not find file %s\n", outname);
1201 png_size_t num_in, num_out;
1203 READFILE(fpin, inbuf, 1, num_in);
1204 READFILE(fpout, outbuf, 1, num_out);
1206 if (num_in != num_out)
1208 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
1210 if(wrote_question == 0)
1213 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
1214 inname,PNG_ZBUF_SIZE);
1216 "\n filtering heuristic (libpng default), compression");
1218 " level (zlib default),\n and zlib version (%s)?\n\n",
1230 if (png_memcmp(inbuf, outbuf, num_in))
1232 fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname);
1233 if(wrote_question == 0)
1236 " Was %s written with the same maximum IDAT chunk size (%d bytes),",
1237 inname,PNG_ZBUF_SIZE);
1239 "\n filtering heuristic (libpng default), compression");
1241 " level (zlib default),\n and zlib version (%s)?\n\n",
1257 /* input and output filenames */
1259 static PNG_CONST char *inname = "pngtest/png";
1260 static PNG_CONST char *outname = "pngout/png";
1262 static PNG_CONST char *inname = "pngtest.png";
1263 static PNG_CONST char *outname = "pngout.png";
1267 main(int argc, char *argv[])
1272 fprintf(STDERR, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
1273 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
1274 fprintf(STDERR,"%s",png_get_copyright(NULL));
1275 /* Show the version of libpng used in building the library */
1276 fprintf(STDERR," library (%lu):%s", png_access_version_number(),
1277 png_get_header_version(NULL));
1278 /* Show the version of libpng used in building the application */
1279 fprintf(STDERR," pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
1280 PNG_HEADER_VERSION_STRING);
1281 fprintf(STDERR," sizeof(png_struct)=%ld, sizeof(png_info)=%ld\n",
1282 (long)sizeof(png_struct), (long)sizeof(png_info));
1284 /* Do some consistency checking on the memory allocation settings, I'm
1285 not sure this matters, but it is nice to know, the first of these
1286 tests should be impossible because of the way the macros are set
1288 #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
1289 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
1291 /* I think the following can happen. */
1292 #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
1293 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
1296 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1299 "Warning: versions are different between png.h and png.c\n");
1300 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1301 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1307 if (strcmp(argv[1], "-m") == 0)
1310 status_dots_requested = 0;
1312 else if (strcmp(argv[1], "-mv") == 0 ||
1313 strcmp(argv[1], "-vm") == 0 )
1317 status_dots_requested = 1;
1319 else if (strcmp(argv[1], "-v") == 0)
1322 status_dots_requested = 1;
1328 status_dots_requested = 0;
1332 if (!multiple && argc == 3+verbose)
1333 outname = argv[2+verbose];
1335 if ((!multiple && argc > 3+verbose) || (multiple && argc < 2))
1338 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
1341 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1343 " with -m %s is used as a temporary file\n", outname);
1350 #ifdef PNG_USER_MEM_SUPPORTED
1351 int allocation_now = current_allocation;
1353 for (i=2; i<argc; ++i)
1355 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1359 fprintf(STDERR, "Testing %s:",argv[i]);
1360 kerror = test_one_file(argv[i], outname);
1363 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1364 fprintf(STDERR, "\n PASS (%lu zero samples)\n",zero_samples);
1366 fprintf(STDERR, " PASS\n");
1368 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1369 for (k=0; k<256; k++)
1371 fprintf(STDERR, " Filter %d was used %lu times\n",
1374 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1375 if(tIME_chunk_present != 0)
1376 fprintf(STDERR, " tIME = %s\n",tIME_string);
1377 tIME_chunk_present = 0;
1378 #endif /* PNG_TIME_RFC1123_SUPPORTED */
1382 fprintf(STDERR, " FAIL\n");
1385 #ifdef PNG_USER_MEM_SUPPORTED
1386 if (allocation_now != current_allocation)
1387 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1388 current_allocation-allocation_now);
1389 if (current_allocation != 0)
1391 memory_infop pinfo = pinformation;
1393 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1394 current_allocation);
1395 while (pinfo != NULL)
1397 fprintf(STDERR, " %lu bytes at %x\n", pinfo->size,
1398 (unsigned int) pinfo->pointer);
1399 pinfo = pinfo->next;
1404 #ifdef PNG_USER_MEM_SUPPORTED
1405 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1406 current_allocation);
1407 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
1408 maximum_allocation);
1409 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1411 fprintf(STDERR, " Number of allocations: %10d\n",
1421 #ifdef PNG_USER_MEM_SUPPORTED
1422 int allocation_now = current_allocation;
1424 if (i == 1) status_dots_requested = 1;
1425 else if(verbose == 0)status_dots_requested = 0;
1426 if (i == 0 || verbose == 1 || ierror != 0)
1427 fprintf(STDERR, "Testing %s:",inname);
1428 kerror = test_one_file(inname, outname);
1431 if(verbose == 1 || i == 2)
1433 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1436 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1437 fprintf(STDERR, "\n PASS (%lu zero samples)\n",zero_samples);
1439 fprintf(STDERR, " PASS\n");
1441 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1442 for (k=0; k<256; k++)
1444 fprintf(STDERR, " Filter %d was used %lu times\n",
1447 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1448 if(tIME_chunk_present != 0)
1449 fprintf(STDERR, " tIME = %s\n",tIME_string);
1450 #endif /* PNG_TIME_RFC1123_SUPPORTED */
1455 if(verbose == 0 && i != 2)
1456 fprintf(STDERR, "Testing %s:",inname);
1457 fprintf(STDERR, " FAIL\n");
1460 #ifdef PNG_USER_MEM_SUPPORTED
1461 if (allocation_now != current_allocation)
1462 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1463 current_allocation-allocation_now);
1464 if (current_allocation != 0)
1466 memory_infop pinfo = pinformation;
1468 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1469 current_allocation);
1470 while (pinfo != NULL)
1472 fprintf(STDERR," %lu bytes at %x\n",
1473 pinfo->size, (unsigned int)pinfo->pointer);
1474 pinfo = pinfo->next;
1479 #ifdef PNG_USER_MEM_SUPPORTED
1480 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1481 current_allocation);
1482 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
1483 maximum_allocation);
1484 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1486 fprintf(STDERR, " Number of allocations: %10d\n",
1491 #ifdef PNGTEST_TIMING
1492 t_stop = (float)clock();
1493 t_misc += (t_stop - t_start);
1495 fprintf(STDERR," CPU time used = %.3f seconds",
1496 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
1497 fprintf(STDERR," (decoding %.3f,\n",
1498 t_decode/(float)CLOCKS_PER_SEC);
1499 fprintf(STDERR," encoding %.3f ,",
1500 t_encode/(float)CLOCKS_PER_SEC);
1501 fprintf(STDERR," other %.3f seconds)\n\n",
1502 t_misc/(float)CLOCKS_PER_SEC);
1506 fprintf(STDERR, "libpng passes test\n");
1508 fprintf(STDERR, "libpng FAILS test\n");
1509 return (int)(ierror != 0);
1512 /* Generate a compiler error if there is an old png.h in the search path. */
1513 typedef version_1_2_0 your_png_h_is_not_version_1_2_0;