VirtualBox

source: vbox/trunk/src/libs/libpng-1.2.54/pngtest.c@ 68390

Last change on this file since 68390 was 58796, checked in by vboxsync, 9 years ago

libpng 1.2.54 unmodified

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette