Debian Codegraph @

PNG/libpng/pngtest.c

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