CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/ext/libpng17/pngtest.c
Views: 1401
1
2
/* pngtest.c - a simple test program to test libpng
3
*
4
* Last changed in libpng 1.6.26 [(PENDING RELEASE)]
5
* Copyright (c) 1998-2002,2004,2006-2016 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 _POSIX_SOURCE 1
35
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
40
/* Defined so I can write to a file on gui/windowing platforms */
41
/* #define STDERR stderr */
42
#define STDERR stdout /* For DOS */
43
44
#include "png.h"
45
46
/* Known chunks that exist in pngtest.png must be supported or pngtest will fail
47
* simply as a result of re-ordering them. This may be fixed in 1.7
48
*
49
* pngtest allocates a single row buffer for each row and overwrites it,
50
* therefore if the write side doesn't support the writing of interlaced images
51
* nothing can be done for an interlaced image (and the code below will fail
52
* horribly trying to write extra data after writing garbage).
53
*/
54
#if defined PNG_READ_SUPPORTED && /* else nothing can be done */\
55
defined PNG_READ_bKGD_SUPPORTED &&\
56
defined PNG_READ_cHRM_SUPPORTED &&\
57
defined PNG_READ_gAMA_SUPPORTED &&\
58
defined PNG_READ_oFFs_SUPPORTED &&\
59
defined PNG_READ_pCAL_SUPPORTED &&\
60
defined PNG_READ_pHYs_SUPPORTED &&\
61
defined PNG_READ_sBIT_SUPPORTED &&\
62
defined PNG_READ_sCAL_SUPPORTED &&\
63
defined PNG_READ_sRGB_SUPPORTED &&\
64
defined PNG_READ_sPLT_SUPPORTED &&\
65
defined PNG_READ_tEXt_SUPPORTED &&\
66
defined PNG_READ_tIME_SUPPORTED &&\
67
defined PNG_READ_zTXt_SUPPORTED &&\
68
(defined PNG_WRITE_INTERLACING_SUPPORTED || PNG_LIBPNG_VER >= 10700)
69
70
#ifdef PNG_ZLIB_HEADER
71
# include PNG_ZLIB_HEADER /* defined by pnglibconf.h from 1.7 */
72
#else
73
# include "zlib.h"
74
#endif
75
76
/* Copied from pngpriv.h but only used in error messages below. */
77
#ifndef PNG_ZBUF_SIZE
78
# define PNG_ZBUF_SIZE 8192
79
#endif
80
#define FCLOSE(file) fclose(file)
81
82
#ifndef PNG_STDIO_SUPPORTED
83
typedef FILE * png_FILE_p;
84
#endif
85
86
/* Makes pngtest verbose so we can find problems. */
87
#ifndef PNG_DEBUG
88
# define PNG_DEBUG 0
89
#endif
90
91
#if PNG_DEBUG > 1
92
# define pngtest_debug(m) ((void)fprintf(stderr, m "\n"))
93
# define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1))
94
# define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))
95
#else
96
# define pngtest_debug(m) ((void)0)
97
# define pngtest_debug1(m,p1) ((void)0)
98
# define pngtest_debug2(m,p1,p2) ((void)0)
99
#endif
100
101
#if !PNG_DEBUG
102
# define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */
103
#endif
104
105
#ifndef PNG_UNUSED
106
# define PNG_UNUSED(param) (void)param;
107
#endif
108
109
/* Turn on CPU timing
110
#define PNGTEST_TIMING
111
*/
112
113
#ifndef PNG_FLOATING_POINT_SUPPORTED
114
#undef PNGTEST_TIMING
115
#endif
116
117
#ifdef PNGTEST_TIMING
118
static float t_start, t_stop, t_decode, t_encode, t_misc;
119
#include <time.h>
120
#endif
121
122
#ifdef PNG_TIME_RFC1123_SUPPORTED
123
#define PNG_tIME_STRING_LENGTH 29
124
static int tIME_chunk_present = 0;
125
static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
126
127
#if PNG_LIBPNG_VER < 10619
128
#define png_convert_to_rfc1123_buffer(ts, t) tIME_to_str(read_ptr, ts, t)
129
130
static int
131
tIME_to_str(png_structp png_ptr, png_charp ts, png_const_timep t)
132
{
133
png_const_charp str = png_convert_to_rfc1123(png_ptr, t);
134
135
if (str == NULL)
136
return 0;
137
138
strcpy(ts, str);
139
return 1;
140
}
141
#endif /* older libpng */
142
#endif
143
144
static int verbose = 0;
145
static int strict = 0;
146
static int relaxed = 0;
147
static int unsupported_chunks = 0; /* chunk unsupported by libpng in input */
148
static int error_count = 0; /* count calls to png_error */
149
static int warning_count = 0; /* count calls to png_warning */
150
151
/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
152
#ifndef png_jmpbuf
153
# define png_jmpbuf(png_ptr) png_ptr->jmpbuf
154
#endif
155
156
/* Defines for unknown chunk handling if required. */
157
#ifndef PNG_HANDLE_CHUNK_ALWAYS
158
# define PNG_HANDLE_CHUNK_ALWAYS 3
159
#endif
160
#ifndef PNG_HANDLE_CHUNK_IF_SAFE
161
# define PNG_HANDLE_CHUNK_IF_SAFE 2
162
#endif
163
164
/* Utility to save typing/errors, the argument must be a name */
165
#define MEMZERO(var) ((void)memset(&var, 0, sizeof var))
166
167
/* Example of using row callbacks to make a simple progress meter */
168
static int status_pass = 1;
169
static int status_dots_requested = 0;
170
static int status_dots = 1;
171
172
static void PNGCBAPI
173
read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
174
{
175
if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
176
return;
177
178
if (status_pass != pass)
179
{
180
fprintf(stdout, "\n Pass %d: ", pass);
181
status_pass = pass;
182
status_dots = 31;
183
}
184
185
status_dots--;
186
187
if (status_dots == 0)
188
{
189
fprintf(stdout, "\n ");
190
status_dots=30;
191
}
192
193
fprintf(stdout, "r");
194
}
195
196
#ifdef PNG_WRITE_SUPPORTED
197
static void PNGCBAPI
198
write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
199
{
200
if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
201
return;
202
203
fprintf(stdout, "w");
204
}
205
#endif
206
207
208
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
209
/* Example of using a user transform callback (doesn't do anything at present).
210
*/
211
static void PNGCBAPI
212
read_user_callback(png_structp png_ptr, png_row_infop row_info, png_bytep data)
213
{
214
PNG_UNUSED(png_ptr)
215
PNG_UNUSED(row_info)
216
PNG_UNUSED(data)
217
}
218
#endif
219
220
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
221
/* Example of using user transform callback (we don't transform anything,
222
* but merely count the zero samples)
223
*/
224
225
static png_uint_32 zero_samples;
226
227
static void PNGCBAPI
228
count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
229
{
230
png_bytep dp = data;
231
if (png_ptr == NULL)
232
return;
233
234
/* Contents of row_info:
235
* png_uint_32 width width of row
236
* png_uint_32 rowbytes number of bytes in row
237
* png_byte color_type color type of pixels
238
* png_byte bit_depth bit depth of samples
239
* png_byte channels number of channels (1-4)
240
* png_byte pixel_depth bits per pixel (depth*channels)
241
*/
242
243
/* Counts the number of zero samples (or zero pixels if color_type is 3 */
244
245
if (row_info->color_type == 0 || row_info->color_type == 3)
246
{
247
int pos = 0;
248
png_uint_32 n, nstop;
249
250
for (n = 0, nstop=row_info->width; n<nstop; n++)
251
{
252
if (row_info->bit_depth == 1)
253
{
254
if (((*dp << pos++ ) & 0x80) == 0)
255
zero_samples++;
256
257
if (pos == 8)
258
{
259
pos = 0;
260
dp++;
261
}
262
}
263
264
if (row_info->bit_depth == 2)
265
{
266
if (((*dp << (pos+=2)) & 0xc0) == 0)
267
zero_samples++;
268
269
if (pos == 8)
270
{
271
pos = 0;
272
dp++;
273
}
274
}
275
276
if (row_info->bit_depth == 4)
277
{
278
if (((*dp << (pos+=4)) & 0xf0) == 0)
279
zero_samples++;
280
281
if (pos == 8)
282
{
283
pos = 0;
284
dp++;
285
}
286
}
287
288
if (row_info->bit_depth == 8)
289
if (*dp++ == 0)
290
zero_samples++;
291
292
if (row_info->bit_depth == 16)
293
{
294
if ((*dp | *(dp+1)) == 0)
295
zero_samples++;
296
dp+=2;
297
}
298
}
299
}
300
else /* Other color types */
301
{
302
png_uint_32 n, nstop;
303
int channel;
304
int color_channels = row_info->channels;
305
if (row_info->color_type > 3)
306
color_channels--;
307
308
for (n = 0, nstop=row_info->width; n<nstop; n++)
309
{
310
for (channel = 0; channel < color_channels; channel++)
311
{
312
if (row_info->bit_depth == 8)
313
if (*dp++ == 0)
314
zero_samples++;
315
316
if (row_info->bit_depth == 16)
317
{
318
if ((*dp | *(dp+1)) == 0)
319
zero_samples++;
320
321
dp+=2;
322
}
323
}
324
if (row_info->color_type > 3)
325
{
326
dp++;
327
if (row_info->bit_depth == 16)
328
dp++;
329
}
330
}
331
}
332
}
333
#endif /* WRITE_USER_TRANSFORM */
334
335
#ifndef PNG_STDIO_SUPPORTED
336
/* START of code to validate stdio-free compilation */
337
/* These copies of the default read/write functions come from pngrio.c and
338
* pngwio.c. They allow "don't include stdio" testing of the library.
339
* This is the function that does the actual reading of data. If you are
340
* not reading from a standard C stream, you should create a replacement
341
* read_data function and use it at run time with png_set_read_fn(), rather
342
* than changing the library.
343
*/
344
345
#ifdef PNG_IO_STATE_SUPPORTED
346
void
347
pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
348
png_uint_32 io_op);
349
void
350
pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
351
png_uint_32 io_op)
352
{
353
png_uint_32 io_state = png_get_io_state(png_ptr);
354
int err = 0;
355
356
/* Check if the current operation (reading / writing) is as expected. */
357
if ((io_state & PNG_IO_MASK_OP) != io_op)
358
png_error(png_ptr, "Incorrect operation in I/O state");
359
360
/* Check if the buffer size specific to the current location
361
* (file signature / header / data / crc) is as expected.
362
*/
363
switch (io_state & PNG_IO_MASK_LOC)
364
{
365
case PNG_IO_SIGNATURE:
366
if (data_length > 8)
367
err = 1;
368
break;
369
case PNG_IO_CHUNK_HDR:
370
if (data_length != 8)
371
err = 1;
372
break;
373
case PNG_IO_CHUNK_DATA:
374
break; /* no restrictions here */
375
case PNG_IO_CHUNK_CRC:
376
if (data_length != 4)
377
err = 1;
378
break;
379
default:
380
err = 1; /* uninitialized */
381
}
382
if (err != 0)
383
png_error(png_ptr, "Bad I/O state or buffer size");
384
}
385
#endif
386
387
static void PNGCBAPI
388
pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
389
{
390
png_size_t check = 0;
391
png_voidp io_ptr;
392
393
/* fread() returns 0 on error, so it is OK to store this in a png_size_t
394
* instead of an int, which is what fread() actually returns.
395
*/
396
io_ptr = png_get_io_ptr(png_ptr);
397
if (io_ptr != NULL)
398
{
399
check = fread(data, 1, length, (png_FILE_p)io_ptr);
400
}
401
402
if (check != length)
403
{
404
png_error(png_ptr, "Read Error");
405
}
406
407
#ifdef PNG_IO_STATE_SUPPORTED
408
pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
409
#endif
410
}
411
412
#ifdef PNG_WRITE_FLUSH_SUPPORTED
413
static void PNGCBAPI
414
pngtest_flush(png_structp png_ptr)
415
{
416
/* Do nothing; fflush() is said to be just a waste of energy. */
417
PNG_UNUSED(png_ptr) /* Stifle compiler warning */
418
}
419
#endif
420
421
/* This is the function that does the actual writing of data. If you are
422
* not writing to a standard C stream, you should create a replacement
423
* write_data function and use it at run time with png_set_write_fn(), rather
424
* than changing the library.
425
*/
426
static void PNGCBAPI
427
pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
428
{
429
png_size_t check;
430
431
check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
432
433
if (check != length)
434
{
435
png_error(png_ptr, "Write Error");
436
}
437
438
#ifdef PNG_IO_STATE_SUPPORTED
439
pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
440
#endif
441
}
442
#endif /* !STDIO */
443
444
/* This function is called when there is a warning, but the library thinks
445
* it can continue anyway. Replacement functions don't have to do anything
446
* here if you don't want to. In the default configuration, png_ptr is
447
* not used, but it is passed in case it may be useful.
448
*/
449
typedef struct
450
{
451
PNG_CONST char *file_name;
452
} pngtest_error_parameters;
453
454
static void PNGCBAPI
455
pngtest_warning(png_structp png_ptr, png_const_charp message)
456
{
457
PNG_CONST char *name = "UNKNOWN (ERROR!)";
458
pngtest_error_parameters *test =
459
(pngtest_error_parameters*)png_get_error_ptr(png_ptr);
460
461
++warning_count;
462
463
if (test != NULL && test->file_name != NULL)
464
name = test->file_name;
465
466
fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
467
}
468
469
/* This is the default error handling function. Note that replacements for
470
* this function MUST NOT RETURN, or the program will likely crash. This
471
* function is used by default, or if the program supplies NULL for the
472
* error function pointer in png_set_error_fn().
473
*/
474
static void PNGCBAPI
475
pngtest_error(png_structp png_ptr, png_const_charp message)
476
{
477
++error_count;
478
479
pngtest_warning(png_ptr, message);
480
/* We can return because png_error calls the default handler, which is
481
* actually OK in this case.
482
*/
483
}
484
485
/* END of code to validate stdio-free compilation */
486
487
/* START of code to validate memory allocation and deallocation */
488
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
489
490
/* Allocate memory. For reasonable files, size should never exceed
491
* 64K. However, zlib may allocate more than 64K if you don't tell
492
* it not to. See zconf.h and png.h for more information. zlib does
493
* need to allocate exactly 64K, so whatever you call here must
494
* have the ability to do that.
495
*
496
* This piece of code can be compiled to validate max 64K allocations
497
* by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
498
*/
499
typedef struct memory_information
500
{
501
png_alloc_size_t size;
502
png_voidp pointer;
503
struct memory_information *next;
504
} memory_information;
505
typedef memory_information *memory_infop;
506
507
static memory_infop pinformation = NULL;
508
static int current_allocation = 0;
509
static int maximum_allocation = 0;
510
static int total_allocation = 0;
511
static int num_allocations = 0;
512
513
png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,
514
png_alloc_size_t size));
515
void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
516
517
png_voidp
518
PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
519
{
520
521
/* png_malloc has already tested for NULL; png_create_struct calls
522
* png_debug_malloc directly, with png_ptr == NULL which is OK
523
*/
524
525
if (size == 0)
526
return (NULL);
527
528
/* This calls the library allocator twice, once to get the requested
529
buffer and once to get a new free list entry. */
530
{
531
/* Disable malloc_fn and free_fn */
532
memory_infop pinfo;
533
png_set_mem_fn(png_ptr, NULL, NULL, NULL);
534
pinfo = (memory_infop)png_malloc(png_ptr,
535
(sizeof *pinfo));
536
pinfo->size = size;
537
current_allocation += size;
538
total_allocation += size;
539
num_allocations ++;
540
541
if (current_allocation > maximum_allocation)
542
maximum_allocation = current_allocation;
543
544
pinfo->pointer = png_malloc(png_ptr, size);
545
/* Restore malloc_fn and free_fn */
546
547
png_set_mem_fn(png_ptr,
548
NULL, png_debug_malloc, png_debug_free);
549
550
if (size != 0 && pinfo->pointer == NULL)
551
{
552
current_allocation -= size;
553
total_allocation -= size;
554
png_error(png_ptr,
555
"out of memory in pngtest->png_debug_malloc");
556
}
557
558
pinfo->next = pinformation;
559
pinformation = pinfo;
560
/* Make sure the caller isn't assuming zeroed memory. */
561
memset(pinfo->pointer, 0xdd, pinfo->size);
562
563
if (verbose != 0)
564
printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
565
pinfo->pointer);
566
567
return (png_voidp)(pinfo->pointer);
568
}
569
}
570
571
/* Free a pointer. It is removed from the list at the same time. */
572
void PNGCBAPI
573
png_debug_free(png_structp png_ptr, png_voidp ptr)
574
{
575
if (png_ptr == NULL)
576
fprintf(STDERR, "NULL pointer to png_debug_free.\n");
577
578
if (ptr == 0)
579
{
580
#if 0 /* This happens all the time. */
581
fprintf(STDERR, "WARNING: freeing NULL pointer\n");
582
#endif
583
return;
584
}
585
586
/* Unlink the element from the list. */
587
if (pinformation != NULL)
588
{
589
memory_infop *ppinfo = &pinformation;
590
591
for (;;)
592
{
593
memory_infop pinfo = *ppinfo;
594
595
if (pinfo->pointer == ptr)
596
{
597
*ppinfo = pinfo->next;
598
current_allocation -= pinfo->size;
599
if (current_allocation < 0)
600
fprintf(STDERR, "Duplicate free of memory\n");
601
/* We must free the list element too, but first kill
602
the memory that is to be freed. */
603
memset(ptr, 0x55, pinfo->size);
604
free(pinfo);
605
pinfo = NULL;
606
break;
607
}
608
609
if (pinfo->next == NULL)
610
{
611
fprintf(STDERR, "Pointer %p not found\n", ptr);
612
break;
613
}
614
615
ppinfo = &pinfo->next;
616
}
617
}
618
619
/* Finally free the data. */
620
if (verbose != 0)
621
printf("Freeing %p\n", ptr);
622
623
if (ptr != NULL)
624
free(ptr);
625
ptr = NULL;
626
}
627
#endif /* USER_MEM && DEBUG */
628
/* END of code to test memory allocation/deallocation */
629
630
631
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
632
/* Demonstration of user chunk support of the sTER and vpAg chunks */
633
634
/* (sTER is a public chunk not yet known by libpng. vpAg is a private
635
chunk used in ImageMagick to store "virtual page" size). */
636
637
static struct user_chunk_data
638
{
639
png_const_infop info_ptr;
640
png_uint_32 vpAg_width, vpAg_height;
641
png_byte vpAg_units;
642
png_byte sTER_mode;
643
int location[2];
644
}
645
user_chunk_data;
646
647
/* Used for location and order; zero means nothing. */
648
#define have_sTER 0x01
649
#define have_vpAg 0x02
650
#define before_PLTE 0x10
651
#define before_IDAT 0x20
652
#define after_IDAT 0x40
653
654
static void
655
init_callback_info(png_const_infop info_ptr)
656
{
657
MEMZERO(user_chunk_data);
658
user_chunk_data.info_ptr = info_ptr;
659
}
660
661
static int
662
set_location(png_structp png_ptr, struct user_chunk_data *data, int what)
663
{
664
int location;
665
666
if ((data->location[0] & what) != 0 || (data->location[1] & what) != 0)
667
return 0; /* already have one of these */
668
669
/* Find where we are (the code below zeroes info_ptr to indicate that the
670
* chunks before the first IDAT have been read.)
671
*/
672
if (data->info_ptr == NULL) /* after IDAT */
673
location = what | after_IDAT;
674
675
else if (png_get_valid(png_ptr, data->info_ptr, PNG_INFO_PLTE) != 0)
676
location = what | before_IDAT;
677
678
else
679
location = what | before_PLTE;
680
681
if (data->location[0] == 0)
682
data->location[0] = location;
683
684
else
685
data->location[1] = location;
686
687
return 1; /* handled */
688
}
689
690
static int PNGCBAPI
691
read_user_chunk_callback(png_struct *png_ptr, png_unknown_chunkp chunk)
692
{
693
struct user_chunk_data *my_user_chunk_data =
694
(struct user_chunk_data*)png_get_user_chunk_ptr(png_ptr);
695
696
if (my_user_chunk_data == NULL)
697
png_error(png_ptr, "lost user chunk pointer");
698
699
/* Return one of the following:
700
* return (-n); chunk had an error
701
* return (0); did not recognize
702
* return (n); success
703
*
704
* The unknown chunk structure contains the chunk data:
705
* png_byte name[5];
706
* png_byte *data;
707
* png_size_t size;
708
*
709
* Note that libpng has already taken care of the CRC handling.
710
*/
711
712
if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */
713
chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */
714
{
715
/* Found sTER chunk */
716
if (chunk->size != 1)
717
return (-1); /* Error return */
718
719
if (chunk->data[0] != 0 && chunk->data[0] != 1)
720
return (-1); /* Invalid mode */
721
722
if (set_location(png_ptr, my_user_chunk_data, have_sTER) != 0)
723
{
724
my_user_chunk_data->sTER_mode=chunk->data[0];
725
return (1);
726
}
727
728
else
729
return (0); /* duplicate sTER - give it to libpng */
730
}
731
732
if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */
733
chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */
734
return (0); /* Did not recognize */
735
736
/* Found ImageMagick vpAg chunk */
737
738
if (chunk->size != 9)
739
return (-1); /* Error return */
740
741
if (set_location(png_ptr, my_user_chunk_data, have_vpAg) == 0)
742
return (0); /* duplicate vpAg */
743
744
my_user_chunk_data->vpAg_width = png_get_uint_31(png_ptr, chunk->data);
745
my_user_chunk_data->vpAg_height = png_get_uint_31(png_ptr, chunk->data + 4);
746
my_user_chunk_data->vpAg_units = chunk->data[8];
747
748
return (1);
749
}
750
751
#ifdef PNG_WRITE_SUPPORTED
752
static void
753
write_sTER_chunk(png_structp write_ptr)
754
{
755
png_byte sTER[5] = {115, 84, 69, 82, '\0'};
756
757
if (verbose != 0)
758
fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode);
759
760
png_write_chunk(write_ptr, sTER, &user_chunk_data.sTER_mode, 1);
761
}
762
763
static void
764
write_vpAg_chunk(png_structp write_ptr)
765
{
766
png_byte vpAg[5] = {118, 112, 65, 103, '\0'};
767
768
png_byte vpag_chunk_data[9];
769
770
if (verbose != 0)
771
fprintf(STDERR, " vpAg = %lu x %lu, units = %d\n",
772
(unsigned long)user_chunk_data.vpAg_width,
773
(unsigned long)user_chunk_data.vpAg_height,
774
user_chunk_data.vpAg_units);
775
776
png_save_uint_32(vpag_chunk_data, user_chunk_data.vpAg_width);
777
png_save_uint_32(vpag_chunk_data + 4, user_chunk_data.vpAg_height);
778
vpag_chunk_data[8] = user_chunk_data.vpAg_units;
779
png_write_chunk(write_ptr, vpAg, vpag_chunk_data, 9);
780
}
781
782
static void
783
write_chunks(png_structp write_ptr, int location)
784
{
785
int i;
786
787
/* Notice that this preserves the original chunk order, however chunks
788
* intercepted by the callback will be written *after* chunks passed to
789
* libpng. This will actually reverse a pair of sTER chunks or a pair of
790
* vpAg chunks, resulting in an error later. This is not worth worrying
791
* about - the chunks should not be duplicated!
792
*/
793
for (i=0; i<2; ++i)
794
{
795
if (user_chunk_data.location[i] == (location | have_sTER))
796
write_sTER_chunk(write_ptr);
797
798
else if (user_chunk_data.location[i] == (location | have_vpAg))
799
write_vpAg_chunk(write_ptr);
800
}
801
}
802
#endif /* WRITE */
803
#else /* !READ_USER_CHUNKS */
804
# define write_chunks(pp,loc) ((void)0)
805
#endif
806
/* END of code to demonstrate user chunk support */
807
808
/* START of code to check that libpng has the required text support; this only
809
* checks for the write support because if read support is missing the chunk
810
* will simply not be reported back to pngtest.
811
*/
812
#ifdef PNG_TEXT_SUPPORTED
813
static void
814
pngtest_check_text_support(png_structp png_ptr, png_textp text_ptr,
815
int num_text)
816
{
817
while (num_text > 0)
818
{
819
switch (text_ptr[--num_text].compression)
820
{
821
case PNG_TEXT_COMPRESSION_NONE:
822
break;
823
824
case PNG_TEXT_COMPRESSION_zTXt:
825
# ifndef PNG_WRITE_zTXt_SUPPORTED
826
++unsupported_chunks;
827
/* In libpng 1.7 this now does an app-error, so stop it: */
828
text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
829
# endif
830
break;
831
832
case PNG_ITXT_COMPRESSION_NONE:
833
case PNG_ITXT_COMPRESSION_zTXt:
834
# ifndef PNG_WRITE_iTXt_SUPPORTED
835
++unsupported_chunks;
836
text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
837
# endif
838
break;
839
840
default:
841
/* This is an error */
842
png_error(png_ptr, "invalid text chunk compression field");
843
break;
844
}
845
}
846
}
847
#endif
848
/* END of code to check that libpng has the required text support */
849
850
/* Test one file */
851
static int
852
test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
853
{
854
static png_FILE_p fpin;
855
static png_FILE_p fpout; /* "static" prevents setjmp corruption */
856
pngtest_error_parameters error_parameters;
857
png_structp read_ptr;
858
png_infop read_info_ptr, end_info_ptr;
859
#ifdef PNG_WRITE_SUPPORTED
860
png_structp write_ptr;
861
png_infop write_info_ptr;
862
png_infop write_end_info_ptr;
863
#ifdef PNG_WRITE_FILTER_SUPPORTED
864
int interlace_preserved = 1;
865
#endif /* WRITE_FILTER */
866
#else /* !WRITE */
867
png_structp write_ptr = NULL;
868
png_infop write_info_ptr = NULL;
869
png_infop write_end_info_ptr = NULL;
870
#endif /* !WRITE */
871
png_bytep row_buf;
872
png_uint_32 y;
873
png_uint_32 width, height;
874
volatile int num_passes;
875
int pass;
876
int bit_depth, color_type;
877
878
row_buf = NULL;
879
error_parameters.file_name = inname;
880
881
if ((fpin = fopen(inname, "rb")) == NULL)
882
{
883
fprintf(STDERR, "Could not find input file %s\n", inname);
884
return (1);
885
}
886
887
if ((fpout = fopen(outname, "wb")) == NULL)
888
{
889
fprintf(STDERR, "Could not open output file %s\n", outname);
890
FCLOSE(fpin);
891
return (1);
892
}
893
894
pngtest_debug("Allocating read and write structures");
895
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
896
read_ptr =
897
png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
898
NULL, NULL, NULL, png_debug_malloc, png_debug_free);
899
#else
900
read_ptr =
901
png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
902
#endif
903
png_set_error_fn(read_ptr, &error_parameters, pngtest_error,
904
pngtest_warning);
905
906
#ifdef PNG_WRITE_SUPPORTED
907
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
908
write_ptr =
909
png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
910
NULL, NULL, NULL, png_debug_malloc, png_debug_free);
911
#else
912
write_ptr =
913
png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
914
#endif
915
png_set_error_fn(write_ptr, &error_parameters, pngtest_error,
916
pngtest_warning);
917
#endif
918
pngtest_debug("Allocating read_info, write_info and end_info structures");
919
read_info_ptr = png_create_info_struct(read_ptr);
920
end_info_ptr = png_create_info_struct(read_ptr);
921
#ifdef PNG_WRITE_SUPPORTED
922
write_info_ptr = png_create_info_struct(write_ptr);
923
write_end_info_ptr = png_create_info_struct(write_ptr);
924
#endif
925
926
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
927
init_callback_info(read_info_ptr);
928
png_set_read_user_chunk_fn(read_ptr, &user_chunk_data,
929
read_user_chunk_callback);
930
#endif
931
932
#ifdef PNG_SETJMP_SUPPORTED
933
pngtest_debug("Setting jmpbuf for read struct");
934
if (setjmp(png_jmpbuf(read_ptr)))
935
{
936
fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
937
png_free(read_ptr, row_buf);
938
row_buf = NULL;
939
png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
940
#ifdef PNG_WRITE_SUPPORTED
941
png_destroy_info_struct(write_ptr, &write_end_info_ptr);
942
png_destroy_write_struct(&write_ptr, &write_info_ptr);
943
#endif
944
FCLOSE(fpin);
945
FCLOSE(fpout);
946
return (1);
947
}
948
949
#ifdef PNG_WRITE_SUPPORTED
950
pngtest_debug("Setting jmpbuf for write struct");
951
952
if (setjmp(png_jmpbuf(write_ptr)))
953
{
954
fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
955
png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
956
png_destroy_info_struct(write_ptr, &write_end_info_ptr);
957
#ifdef PNG_WRITE_SUPPORTED
958
png_destroy_write_struct(&write_ptr, &write_info_ptr);
959
#endif
960
FCLOSE(fpin);
961
FCLOSE(fpout);
962
return (1);
963
}
964
#endif
965
#endif
966
967
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
968
if (strict != 0)
969
{
970
/* Treat png_benign_error() as errors on read */
971
png_set_benign_errors(read_ptr, 0);
972
973
# ifdef PNG_WRITE_SUPPORTED
974
/* Treat them as errors on write */
975
png_set_benign_errors(write_ptr, 0);
976
# endif
977
978
/* if strict is not set, then app warnings and errors are treated as
979
* warnings in release builds, but not in unstable builds; this can be
980
* changed with '--relaxed'.
981
*/
982
}
983
984
else if (relaxed != 0)
985
{
986
/* Allow application (pngtest) errors and warnings to pass */
987
png_set_benign_errors(read_ptr, 1);
988
989
/* Turn off CRC and ADLER32 checking while reading */
990
png_set_crc_action(read_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE);
991
992
# ifdef PNG_WRITE_SUPPORTED
993
png_set_benign_errors(write_ptr, 1);
994
# endif
995
996
}
997
#endif /* BENIGN_ERRORS */
998
999
pngtest_debug("Initializing input and output streams");
1000
#ifdef PNG_STDIO_SUPPORTED
1001
png_init_io(read_ptr, fpin);
1002
# ifdef PNG_WRITE_SUPPORTED
1003
png_init_io(write_ptr, fpout);
1004
# endif
1005
#else
1006
png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
1007
# ifdef PNG_WRITE_SUPPORTED
1008
png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
1009
# ifdef PNG_WRITE_FLUSH_SUPPORTED
1010
pngtest_flush);
1011
# else
1012
NULL);
1013
# endif
1014
# endif
1015
#endif
1016
1017
if (status_dots_requested == 1)
1018
{
1019
#ifdef PNG_WRITE_SUPPORTED
1020
png_set_write_status_fn(write_ptr, write_row_callback);
1021
#endif
1022
png_set_read_status_fn(read_ptr, read_row_callback);
1023
}
1024
1025
else
1026
{
1027
#ifdef PNG_WRITE_SUPPORTED
1028
png_set_write_status_fn(write_ptr, NULL);
1029
#endif
1030
png_set_read_status_fn(read_ptr, NULL);
1031
}
1032
1033
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1034
png_set_read_user_transform_fn(read_ptr, read_user_callback);
1035
#endif
1036
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1037
zero_samples = 0;
1038
png_set_write_user_transform_fn(write_ptr, count_zero_samples);
1039
#endif
1040
1041
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
1042
/* Preserve all the unknown chunks, if possible. If this is disabled then,
1043
* even if the png_{get,set}_unknown_chunks stuff is enabled, we can't use
1044
* libpng to *save* the unknown chunks on read (because we can't switch the
1045
* save option on!)
1046
*
1047
* Notice that if SET_UNKNOWN_CHUNKS is *not* supported read will discard all
1048
* unknown chunks and write will write them all.
1049
*/
1050
#ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
1051
png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
1052
NULL, 0);
1053
#endif
1054
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1055
png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,
1056
NULL, 0);
1057
#endif
1058
#endif
1059
1060
pngtest_debug("Reading info struct");
1061
png_read_info(read_ptr, read_info_ptr);
1062
1063
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1064
/* This is a bit of a hack; there is no obvious way in the callback function
1065
* to determine that the chunks before the first IDAT have been read, so
1066
* remove the info_ptr (which is only used to determine position relative to
1067
* PLTE) here to indicate that we are after the IDAT.
1068
*/
1069
user_chunk_data.info_ptr = NULL;
1070
#endif
1071
1072
pngtest_debug("Transferring info struct");
1073
{
1074
int interlace_type, compression_type, filter_type;
1075
1076
if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
1077
&color_type, &interlace_type, &compression_type, &filter_type) != 0)
1078
{
1079
png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
1080
color_type, interlace_type, compression_type, filter_type);
1081
/* num_passes may not be available below if interlace support is not
1082
* provided by libpng for both read and write.
1083
*/
1084
switch (interlace_type)
1085
{
1086
case PNG_INTERLACE_NONE:
1087
num_passes = 1;
1088
break;
1089
1090
case PNG_INTERLACE_ADAM7:
1091
num_passes = 7;
1092
break;
1093
1094
default:
1095
png_error(read_ptr, "invalid interlace type");
1096
/*NOT REACHED*/
1097
}
1098
}
1099
1100
else
1101
png_error(read_ptr, "png_get_IHDR failed");
1102
}
1103
#ifdef PNG_FIXED_POINT_SUPPORTED
1104
#ifdef PNG_cHRM_SUPPORTED
1105
{
1106
png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1107
blue_y;
1108
1109
if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
1110
&red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
1111
{
1112
png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
1113
red_y, green_x, green_y, blue_x, blue_y);
1114
}
1115
}
1116
#endif
1117
#ifdef PNG_gAMA_SUPPORTED
1118
{
1119
png_fixed_point gamma;
1120
1121
if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma) != 0)
1122
png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
1123
}
1124
#endif
1125
#else /* Use floating point versions */
1126
#ifdef PNG_FLOATING_POINT_SUPPORTED
1127
#ifdef PNG_cHRM_SUPPORTED
1128
{
1129
double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1130
blue_y;
1131
1132
if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
1133
&red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
1134
{
1135
png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
1136
red_y, green_x, green_y, blue_x, blue_y);
1137
}
1138
}
1139
#endif
1140
#ifdef PNG_gAMA_SUPPORTED
1141
{
1142
double gamma;
1143
1144
if (png_get_gAMA(read_ptr, read_info_ptr, &gamma) != 0)
1145
png_set_gAMA(write_ptr, write_info_ptr, gamma);
1146
}
1147
#endif
1148
#endif /* Floating point */
1149
#endif /* Fixed point */
1150
#ifdef PNG_iCCP_SUPPORTED
1151
{
1152
png_charp name;
1153
png_bytep profile;
1154
png_uint_32 proflen;
1155
int compression_type;
1156
1157
if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
1158
&profile, &proflen) != 0)
1159
{
1160
png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
1161
profile, proflen);
1162
}
1163
}
1164
#endif
1165
#ifdef PNG_sRGB_SUPPORTED
1166
{
1167
int intent;
1168
1169
if (png_get_sRGB(read_ptr, read_info_ptr, &intent) != 0)
1170
png_set_sRGB(write_ptr, write_info_ptr, intent);
1171
}
1172
#endif
1173
{
1174
png_colorp palette;
1175
int num_palette;
1176
1177
if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette) != 0)
1178
png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
1179
}
1180
#ifdef PNG_bKGD_SUPPORTED
1181
{
1182
png_color_16p background;
1183
1184
if (png_get_bKGD(read_ptr, read_info_ptr, &background) != 0)
1185
{
1186
png_set_bKGD(write_ptr, write_info_ptr, background);
1187
}
1188
}
1189
#endif
1190
#ifdef PNG_hIST_SUPPORTED
1191
{
1192
png_uint_16p hist;
1193
1194
if (png_get_hIST(read_ptr, read_info_ptr, &hist) != 0)
1195
png_set_hIST(write_ptr, write_info_ptr, hist);
1196
}
1197
#endif
1198
#ifdef PNG_oFFs_SUPPORTED
1199
{
1200
png_int_32 offset_x, offset_y;
1201
int unit_type;
1202
1203
if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
1204
&unit_type) != 0)
1205
{
1206
png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
1207
}
1208
}
1209
#endif
1210
#ifdef PNG_pCAL_SUPPORTED
1211
{
1212
png_charp purpose, units;
1213
png_charpp params;
1214
png_int_32 X0, X1;
1215
int type, nparams;
1216
1217
if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
1218
&nparams, &units, &params) != 0)
1219
{
1220
png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
1221
nparams, units, params);
1222
}
1223
}
1224
#endif
1225
#ifdef PNG_pHYs_SUPPORTED
1226
{
1227
png_uint_32 res_x, res_y;
1228
int unit_type;
1229
1230
if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y,
1231
&unit_type) != 0)
1232
png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
1233
}
1234
#endif
1235
#ifdef PNG_sBIT_SUPPORTED
1236
{
1237
png_color_8p sig_bit;
1238
1239
if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit) != 0)
1240
png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
1241
}
1242
#endif
1243
#ifdef PNG_sCAL_SUPPORTED
1244
#if defined(PNG_FLOATING_POINT_SUPPORTED) && \
1245
defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)
1246
{
1247
int unit;
1248
double scal_width, scal_height;
1249
1250
if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
1251
&scal_height) != 0)
1252
{
1253
png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
1254
}
1255
}
1256
#else
1257
#ifdef PNG_FIXED_POINT_SUPPORTED
1258
{
1259
int unit;
1260
png_charp scal_width, scal_height;
1261
1262
if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
1263
&scal_height) != 0)
1264
{
1265
png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
1266
scal_height);
1267
}
1268
}
1269
#endif
1270
#endif
1271
#endif
1272
1273
#ifdef PNG_sPLT_SUPPORTED
1274
{
1275
png_sPLT_tp entries;
1276
1277
int num_entries = (int) png_get_sPLT(read_ptr, read_info_ptr, &entries);
1278
if (num_entries)
1279
{
1280
png_set_sPLT(write_ptr, write_info_ptr, entries, num_entries);
1281
}
1282
}
1283
#endif
1284
1285
#ifdef PNG_TEXT_SUPPORTED
1286
{
1287
png_textp text_ptr;
1288
int num_text;
1289
1290
if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1291
{
1292
pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1293
1294
pngtest_check_text_support(read_ptr, text_ptr, num_text);
1295
1296
if (verbose != 0)
1297
{
1298
int i;
1299
1300
printf("\n");
1301
for (i=0; i<num_text; i++)
1302
{
1303
printf(" Text compression[%d]=%d\n",
1304
i, text_ptr[i].compression);
1305
}
1306
}
1307
1308
png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1309
}
1310
}
1311
#endif
1312
#ifdef PNG_tIME_SUPPORTED
1313
{
1314
png_timep mod_time;
1315
1316
if (png_get_tIME(read_ptr, read_info_ptr, &mod_time) != 0)
1317
{
1318
png_set_tIME(write_ptr, write_info_ptr, mod_time);
1319
#ifdef PNG_TIME_RFC1123_SUPPORTED
1320
if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
1321
tIME_string[(sizeof tIME_string) - 1] = '\0';
1322
1323
else
1324
{
1325
strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string));
1326
tIME_string[(sizeof tIME_string) - 1] = '\0';
1327
}
1328
1329
tIME_chunk_present++;
1330
#endif /* TIME_RFC1123 */
1331
}
1332
}
1333
#endif
1334
#ifdef PNG_tRNS_SUPPORTED
1335
{
1336
png_bytep trans_alpha;
1337
int num_trans;
1338
png_color_16p trans_color;
1339
1340
if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
1341
&trans_color) != 0)
1342
{
1343
int sample_max = (1 << bit_depth);
1344
/* libpng doesn't reject a tRNS chunk with out-of-range samples */
1345
if (!((color_type == PNG_COLOR_TYPE_GRAY &&
1346
(int)trans_color->gray > sample_max) ||
1347
(color_type == PNG_COLOR_TYPE_RGB &&
1348
((int)trans_color->red > sample_max ||
1349
(int)trans_color->green > sample_max ||
1350
(int)trans_color->blue > sample_max))))
1351
png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
1352
trans_color);
1353
}
1354
}
1355
#endif
1356
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1357
{
1358
png_unknown_chunkp unknowns;
1359
int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
1360
&unknowns);
1361
1362
if (num_unknowns != 0)
1363
{
1364
png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1365
num_unknowns);
1366
#if PNG_LIBPNG_VER < 10600
1367
/* Copy the locations from the read_info_ptr. The automatically
1368
* generated locations in write_end_info_ptr are wrong prior to 1.6.0
1369
* because they are reset from the write pointer (removed in 1.6.0).
1370
*/
1371
{
1372
int i;
1373
for (i = 0; i < num_unknowns; i++)
1374
png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1375
unknowns[i].location);
1376
}
1377
#endif
1378
}
1379
}
1380
#endif
1381
1382
#ifdef PNG_WRITE_SUPPORTED
1383
pngtest_debug("Writing info struct");
1384
1385
/* Write the info in two steps so that if we write the 'unknown' chunks here
1386
* they go to the correct place.
1387
*/
1388
png_write_info_before_PLTE(write_ptr, write_info_ptr);
1389
1390
write_chunks(write_ptr, before_PLTE); /* before PLTE */
1391
1392
png_write_info(write_ptr, write_info_ptr);
1393
1394
write_chunks(write_ptr, before_IDAT); /* after PLTE */
1395
1396
#ifdef PNG_COMPRESSION_COMPAT
1397
/* Test the 'compatibility' setting here, if it is available. */
1398
png_set_compression(write_ptr, PNG_COMPRESSION_COMPAT);
1399
#endif
1400
#endif
1401
1402
#ifdef SINGLE_ROWBUF_ALLOC
1403
pngtest_debug("Allocating row buffer...");
1404
row_buf = (png_bytep)png_malloc(read_ptr,
1405
png_get_rowbytes(read_ptr, read_info_ptr));
1406
1407
pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
1408
#endif /* SINGLE_ROWBUF_ALLOC */
1409
pngtest_debug("Writing row data");
1410
1411
#if defined(PNG_READ_INTERLACING_SUPPORTED) &&\
1412
defined(PNG_WRITE_INTERLACING_SUPPORTED)
1413
/* Both must be defined for libpng to be able to handle the interlace,
1414
* otherwise it gets handled below by simply reading and writing the passes
1415
* directly.
1416
*/
1417
if (png_set_interlace_handling(read_ptr) != num_passes)
1418
png_error(write_ptr,
1419
"png_set_interlace_handling(read): wrong pass count ");
1420
if (png_set_interlace_handling(write_ptr) != num_passes)
1421
png_error(write_ptr,
1422
"png_set_interlace_handling(write): wrong pass count ");
1423
#else /* png_set_interlace_handling not called on either read or write */
1424
# define calc_pass_height
1425
#endif /* not using libpng interlace handling */
1426
1427
#ifdef PNGTEST_TIMING
1428
t_stop = (float)clock();
1429
t_misc += (t_stop - t_start);
1430
t_start = t_stop;
1431
#endif
1432
for (pass = 0; pass < num_passes; pass++)
1433
{
1434
# ifdef calc_pass_height
1435
png_uint_32 pass_height;
1436
1437
if (num_passes == 7) /* interlaced */
1438
{
1439
if (PNG_PASS_COLS(width, pass) > 0)
1440
pass_height = PNG_PASS_ROWS(height, pass);
1441
1442
else
1443
pass_height = 0;
1444
}
1445
1446
else /* not interlaced */
1447
pass_height = height;
1448
# else
1449
# define pass_height height
1450
# endif
1451
1452
pngtest_debug1("Writing row data for pass %d", pass);
1453
for (y = 0; y < pass_height; y++)
1454
{
1455
#ifndef SINGLE_ROWBUF_ALLOC
1456
pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
1457
1458
row_buf = (png_bytep)png_malloc(read_ptr,
1459
png_get_rowbytes(read_ptr, read_info_ptr));
1460
1461
pngtest_debug2("\t0x%08lx (%lu bytes)", (unsigned long)row_buf,
1462
(unsigned long)png_get_rowbytes(read_ptr, read_info_ptr));
1463
1464
#endif /* !SINGLE_ROWBUF_ALLOC */
1465
png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
1466
1467
#ifdef PNG_WRITE_SUPPORTED
1468
#ifdef PNGTEST_TIMING
1469
t_stop = (float)clock();
1470
t_decode += (t_stop - t_start);
1471
t_start = t_stop;
1472
#endif
1473
png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
1474
#ifdef PNGTEST_TIMING
1475
t_stop = (float)clock();
1476
t_encode += (t_stop - t_start);
1477
t_start = t_stop;
1478
#endif
1479
#endif /* WRITE */
1480
1481
#ifndef SINGLE_ROWBUF_ALLOC
1482
pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
1483
png_free(read_ptr, row_buf);
1484
row_buf = NULL;
1485
#endif /* !SINGLE_ROWBUF_ALLOC */
1486
}
1487
}
1488
1489
#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1490
# ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1491
png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
1492
# endif
1493
# ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1494
png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
1495
# endif
1496
#endif
1497
1498
pngtest_debug("Reading and writing end_info data");
1499
1500
png_read_end(read_ptr, end_info_ptr);
1501
#ifdef PNG_TEXT_SUPPORTED
1502
{
1503
png_textp text_ptr;
1504
int num_text;
1505
1506
if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1507
{
1508
pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1509
1510
pngtest_check_text_support(read_ptr, text_ptr, num_text);
1511
1512
if (verbose != 0)
1513
{
1514
int i;
1515
1516
printf("\n");
1517
for (i=0; i<num_text; i++)
1518
{
1519
printf(" Text compression[%d]=%d\n",
1520
i, text_ptr[i].compression);
1521
}
1522
}
1523
1524
png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1525
}
1526
}
1527
#endif
1528
#ifdef PNG_tIME_SUPPORTED
1529
{
1530
png_timep mod_time;
1531
1532
if (png_get_tIME(read_ptr, end_info_ptr, &mod_time) != 0)
1533
{
1534
png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
1535
#ifdef PNG_TIME_RFC1123_SUPPORTED
1536
if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
1537
tIME_string[(sizeof tIME_string) - 1] = '\0';
1538
1539
else
1540
{
1541
strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string);
1542
tIME_string[(sizeof tIME_string)-1] = '\0';
1543
}
1544
1545
tIME_chunk_present++;
1546
#endif /* TIME_RFC1123 */
1547
}
1548
}
1549
#endif
1550
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1551
{
1552
png_unknown_chunkp unknowns;
1553
int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
1554
&unknowns);
1555
1556
if (num_unknowns != 0)
1557
{
1558
png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1559
num_unknowns);
1560
#if PNG_LIBPNG_VER < 10600
1561
/* Copy the locations from the read_info_ptr. The automatically
1562
* generated locations in write_end_info_ptr are wrong prior to 1.6.0
1563
* because they are reset from the write pointer (removed in 1.6.0).
1564
*/
1565
{
1566
int i;
1567
for (i = 0; i < num_unknowns; i++)
1568
png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1569
unknowns[i].location);
1570
}
1571
#endif
1572
}
1573
}
1574
#endif
1575
1576
#ifdef PNG_WRITE_SUPPORTED
1577
#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
1578
/* Normally one would use Z_DEFAULT_STRATEGY for text compression.
1579
* This is here just to make pngtest replicate the results from libpng
1580
* versions prior to 1.5.4, and to test this new API.
1581
*/
1582
png_set_text_compression_strategy(write_ptr, Z_FILTERED);
1583
#endif
1584
1585
/* When the unknown vpAg/sTER chunks are written by pngtest the only way to
1586
* do it is to write them *before* calling png_write_end. When unknown
1587
* chunks are written by libpng, however, they are written just before IEND.
1588
* There seems to be no way round this, however vpAg/sTER are not expected
1589
* after IDAT.
1590
*/
1591
write_chunks(write_ptr, after_IDAT);
1592
1593
png_write_end(write_ptr, write_end_info_ptr);
1594
#endif
1595
1596
#ifdef PNG_EASY_ACCESS_SUPPORTED
1597
if (verbose != 0)
1598
{
1599
png_uint_32 iwidth, iheight;
1600
iwidth = png_get_image_width(write_ptr, write_info_ptr);
1601
iheight = png_get_image_height(write_ptr, write_info_ptr);
1602
fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
1603
(unsigned long)iwidth, (unsigned long)iheight);
1604
}
1605
#endif
1606
1607
pngtest_debug("Destroying data structs");
1608
#ifdef SINGLE_ROWBUF_ALLOC
1609
pngtest_debug("destroying row_buf for read_ptr");
1610
png_free(read_ptr, row_buf);
1611
row_buf = NULL;
1612
#endif /* SINGLE_ROWBUF_ALLOC */
1613
pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
1614
png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
1615
#ifdef PNG_WRITE_SUPPORTED
1616
pngtest_debug("destroying write_end_info_ptr");
1617
png_destroy_info_struct(write_ptr, &write_end_info_ptr);
1618
pngtest_debug("destroying write_ptr, write_info_ptr");
1619
png_destroy_write_struct(&write_ptr, &write_info_ptr);
1620
#endif
1621
pngtest_debug("Destruction complete.");
1622
1623
FCLOSE(fpin);
1624
FCLOSE(fpout);
1625
1626
/* Summarize any warnings or errors and in 'strict' mode fail the test.
1627
* Unsupported chunks can result in warnings, in that case ignore the strict
1628
* setting, otherwise fail the test on warnings as well as errors.
1629
*/
1630
if (error_count > 0)
1631
{
1632
/* We don't really expect to get here because of the setjmp handling
1633
* above, but this is safe.
1634
*/
1635
fprintf(STDERR, "\n %s: %d libpng errors found (%d warnings)",
1636
inname, error_count, warning_count);
1637
1638
if (strict != 0)
1639
return (1);
1640
}
1641
1642
# ifdef PNG_WRITE_SUPPORTED
1643
/* If there is no write support nothing was written! */
1644
else if (unsupported_chunks > 0)
1645
{
1646
fprintf(STDERR, "\n %s: unsupported chunks (%d)%s",
1647
inname, unsupported_chunks, strict ? ": IGNORED --strict!" : "");
1648
}
1649
# endif
1650
1651
else if (warning_count > 0)
1652
{
1653
fprintf(STDERR, "\n %s: %d libpng warnings found",
1654
inname, warning_count);
1655
1656
if (strict != 0)
1657
return (1);
1658
}
1659
1660
pngtest_debug("Opening files for comparison");
1661
if ((fpin = fopen(inname, "rb")) == NULL)
1662
{
1663
fprintf(STDERR, "Could not find file %s\n", inname);
1664
return (1);
1665
}
1666
1667
if ((fpout = fopen(outname, "rb")) == NULL)
1668
{
1669
fprintf(STDERR, "Could not find file %s\n", outname);
1670
FCLOSE(fpin);
1671
return (1);
1672
}
1673
1674
#if defined (PNG_WRITE_SUPPORTED) /* else nothing was written */ &&\
1675
defined (PNG_WRITE_FILTER_SUPPORTED)
1676
if (interlace_preserved != 0) /* else the files will be changed */
1677
{
1678
for (;;)
1679
{
1680
static int wrote_question = 0;
1681
png_size_t num_in, num_out;
1682
char inbuf[256], outbuf[256];
1683
1684
num_in = fread(inbuf, 1, sizeof inbuf, fpin);
1685
num_out = fread(outbuf, 1, sizeof outbuf, fpout);
1686
1687
if (num_in != num_out)
1688
{
1689
fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
1690
inname, outname);
1691
1692
if (wrote_question == 0 && unsupported_chunks == 0)
1693
{
1694
fprintf(STDERR,
1695
" Was %s written with the same maximum IDAT"
1696
" chunk size (%d bytes),",
1697
inname, PNG_ZBUF_SIZE);
1698
fprintf(STDERR,
1699
"\n filtering heuristic (libpng default), compression");
1700
fprintf(STDERR,
1701
" level (zlib default),\n and zlib version (%s)?\n\n",
1702
ZLIB_VERSION);
1703
wrote_question = 1;
1704
}
1705
1706
FCLOSE(fpin);
1707
FCLOSE(fpout);
1708
1709
if (strict != 0 && unsupported_chunks == 0)
1710
return (1);
1711
1712
else
1713
return (0);
1714
}
1715
1716
if (num_in == 0)
1717
break;
1718
1719
if (memcmp(inbuf, outbuf, num_in))
1720
{
1721
fprintf(STDERR, "\nFiles %s and %s are different\n", inname,
1722
outname);
1723
1724
if (wrote_question == 0 && unsupported_chunks == 0)
1725
{
1726
fprintf(STDERR,
1727
" Was %s written with the same maximum"
1728
" IDAT chunk size (%d bytes),",
1729
inname, PNG_ZBUF_SIZE);
1730
fprintf(STDERR,
1731
"\n filtering heuristic (libpng default), compression");
1732
fprintf(STDERR,
1733
" level (zlib default),\n and zlib version (%s)?\n\n",
1734
ZLIB_VERSION);
1735
wrote_question = 1;
1736
}
1737
1738
FCLOSE(fpin);
1739
FCLOSE(fpout);
1740
1741
/* NOTE: the unsupported_chunks escape is permitted here because
1742
* unsupported text chunk compression will result in the compression
1743
* mode being changed (to NONE) yet, in the test case, the result
1744
* can be exactly the same size!
1745
*/
1746
if (strict != 0 && unsupported_chunks == 0)
1747
return (1);
1748
1749
else
1750
return (0);
1751
}
1752
}
1753
}
1754
#endif /* WRITE && WRITE_FILTER */
1755
1756
FCLOSE(fpin);
1757
FCLOSE(fpout);
1758
1759
return (0);
1760
}
1761
1762
/* Input and output filenames */
1763
#ifdef RISCOS
1764
static PNG_CONST char *inname = "pngtest/png";
1765
static PNG_CONST char *outname = "pngout/png";
1766
#else
1767
static PNG_CONST char *inname = "pngtest.png";
1768
static PNG_CONST char *outname = "pngout.png";
1769
#endif
1770
1771
int
1772
main(int argc, char *argv[])
1773
{
1774
int multiple = 0;
1775
int ierror = 0;
1776
1777
png_structp dummy_ptr;
1778
1779
fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
1780
fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
1781
fprintf(STDERR, "%s", png_get_copyright(NULL));
1782
/* Show the version of libpng used in building the library */
1783
fprintf(STDERR, " library (%lu):%s",
1784
(unsigned long)png_access_version_number(),
1785
png_get_header_version(NULL));
1786
1787
/* Show the version of libpng used in building the application */
1788
fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
1789
PNG_HEADER_VERSION_STRING);
1790
1791
/* Do some consistency checking on the memory allocation settings, I'm
1792
* not sure this matters, but it is nice to know, the first of these
1793
* tests should be impossible because of the way the macros are set
1794
* in pngconf.h
1795
*/
1796
#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
1797
fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
1798
#endif
1799
/* I think the following can happen. */
1800
#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
1801
fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
1802
#endif
1803
1804
if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1805
{
1806
fprintf(STDERR,
1807
"Warning: versions are different between png.h and png.c\n");
1808
fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1809
fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1810
++ierror;
1811
}
1812
1813
if (argc > 1)
1814
{
1815
if (strcmp(argv[1], "-m") == 0)
1816
{
1817
multiple = 1;
1818
status_dots_requested = 0;
1819
}
1820
1821
else if (strcmp(argv[1], "-mv") == 0 ||
1822
strcmp(argv[1], "-vm") == 0 )
1823
{
1824
multiple = 1;
1825
verbose = 1;
1826
status_dots_requested = 1;
1827
}
1828
1829
else if (strcmp(argv[1], "-v") == 0)
1830
{
1831
verbose = 1;
1832
status_dots_requested = 1;
1833
inname = argv[2];
1834
}
1835
1836
else if (strcmp(argv[1], "--strict") == 0)
1837
{
1838
status_dots_requested = 0;
1839
verbose = 1;
1840
inname = argv[2];
1841
strict++;
1842
relaxed = 0;
1843
}
1844
1845
else if (strcmp(argv[1], "--relaxed") == 0)
1846
{
1847
status_dots_requested = 0;
1848
verbose = 1;
1849
inname = argv[2];
1850
strict = 0;
1851
relaxed++;
1852
}
1853
1854
else
1855
{
1856
inname = argv[1];
1857
status_dots_requested = 0;
1858
}
1859
}
1860
1861
if (multiple == 0 && argc == 3 + verbose)
1862
outname = argv[2 + verbose];
1863
1864
if ((multiple == 0 && argc > 3 + verbose) ||
1865
(multiple != 0 && argc < 2))
1866
{
1867
fprintf(STDERR,
1868
"usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
1869
argv[0], argv[0]);
1870
fprintf(STDERR,
1871
" reads/writes one PNG file (without -m) or multiple files (-m)\n");
1872
fprintf(STDERR,
1873
" with -m %s is used as a temporary file\n", outname);
1874
exit(1);
1875
}
1876
1877
if (multiple != 0)
1878
{
1879
int i;
1880
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1881
int allocation_now = current_allocation;
1882
#endif
1883
for (i=2; i<argc; ++i)
1884
{
1885
int kerror;
1886
fprintf(STDERR, "\n Testing %s:", argv[i]);
1887
#if PNG_DEBUG > 0
1888
fprintf(STDERR, "\n");
1889
#endif
1890
kerror = test_one_file(argv[i], outname);
1891
if (kerror == 0)
1892
{
1893
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1894
fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1895
(unsigned long)zero_samples);
1896
#else
1897
fprintf(STDERR, " PASS\n");
1898
#endif
1899
#ifdef PNG_TIME_RFC1123_SUPPORTED
1900
if (tIME_chunk_present != 0)
1901
fprintf(STDERR, " tIME = %s\n", tIME_string);
1902
1903
tIME_chunk_present = 0;
1904
#endif /* TIME_RFC1123 */
1905
}
1906
1907
else
1908
{
1909
fprintf(STDERR, " FAIL\n");
1910
ierror += kerror;
1911
}
1912
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1913
if (allocation_now != current_allocation)
1914
fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1915
current_allocation - allocation_now);
1916
1917
if (current_allocation != 0)
1918
{
1919
memory_infop pinfo = pinformation;
1920
1921
fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1922
current_allocation);
1923
1924
while (pinfo != NULL)
1925
{
1926
fprintf(STDERR, " %lu bytes at %p\n",
1927
(unsigned long)pinfo->size,
1928
pinfo->pointer);
1929
pinfo = pinfo->next;
1930
}
1931
}
1932
#endif
1933
}
1934
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1935
fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1936
current_allocation);
1937
fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
1938
maximum_allocation);
1939
fprintf(STDERR, " Total memory allocation: %10d bytes\n",
1940
total_allocation);
1941
fprintf(STDERR, " Number of allocations: %10d\n",
1942
num_allocations);
1943
#endif
1944
}
1945
1946
else
1947
{
1948
int i;
1949
for (i = 0; i<3; ++i)
1950
{
1951
int kerror;
1952
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1953
int allocation_now = current_allocation;
1954
#endif
1955
if (i == 1)
1956
status_dots_requested = 1;
1957
1958
else if (verbose == 0)
1959
status_dots_requested = 0;
1960
1961
if (i == 0 || verbose == 1 || ierror != 0)
1962
{
1963
fprintf(STDERR, "\n Testing %s:", inname);
1964
#if PNG_DEBUG > 0
1965
fprintf(STDERR, "\n");
1966
#endif
1967
}
1968
1969
kerror = test_one_file(inname, outname);
1970
1971
if (kerror == 0)
1972
{
1973
if (verbose == 1 || i == 2)
1974
{
1975
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1976
fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1977
(unsigned long)zero_samples);
1978
#else
1979
fprintf(STDERR, " PASS\n");
1980
#endif
1981
#ifdef PNG_TIME_RFC1123_SUPPORTED
1982
if (tIME_chunk_present != 0)
1983
fprintf(STDERR, " tIME = %s\n", tIME_string);
1984
#endif /* TIME_RFC1123 */
1985
}
1986
}
1987
1988
else
1989
{
1990
if (verbose == 0 && i != 2)
1991
{
1992
fprintf(STDERR, "\n Testing %s:", inname);
1993
#if PNG_DEBUG > 0
1994
fprintf(STDERR, "\n");
1995
#endif
1996
}
1997
1998
fprintf(STDERR, " FAIL\n");
1999
ierror += kerror;
2000
}
2001
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
2002
if (allocation_now != current_allocation)
2003
fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
2004
current_allocation - allocation_now);
2005
2006
if (current_allocation != 0)
2007
{
2008
memory_infop pinfo = pinformation;
2009
2010
fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
2011
current_allocation);
2012
2013
while (pinfo != NULL)
2014
{
2015
fprintf(STDERR, " %lu bytes at %p\n",
2016
(unsigned long)pinfo->size, pinfo->pointer);
2017
pinfo = pinfo->next;
2018
}
2019
}
2020
#endif
2021
}
2022
#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
2023
fprintf(STDERR, " Current memory allocation: %10d bytes\n",
2024
current_allocation);
2025
fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
2026
maximum_allocation);
2027
fprintf(STDERR, " Total memory allocation: %10d bytes\n",
2028
total_allocation);
2029
fprintf(STDERR, " Number of allocations: %10d\n",
2030
num_allocations);
2031
#endif
2032
}
2033
2034
#ifdef PNGTEST_TIMING
2035
t_stop = (float)clock();
2036
t_misc += (t_stop - t_start);
2037
t_start = t_stop;
2038
fprintf(STDERR, " CPU time used = %.3f seconds",
2039
(t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
2040
fprintf(STDERR, " (decoding %.3f,\n",
2041
t_decode/(float)CLOCKS_PER_SEC);
2042
fprintf(STDERR, " encoding %.3f ,",
2043
t_encode/(float)CLOCKS_PER_SEC);
2044
fprintf(STDERR, " other %.3f seconds)\n\n",
2045
t_misc/(float)CLOCKS_PER_SEC);
2046
#endif
2047
2048
if (ierror == 0)
2049
fprintf(STDERR, " libpng passes test\n");
2050
2051
else
2052
fprintf(STDERR, " libpng FAILS test\n");
2053
2054
dummy_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2055
fprintf(STDERR, " Default limits:\n");
2056
fprintf(STDERR, " width_max = %lu\n",
2057
(unsigned long) png_get_user_width_max(dummy_ptr));
2058
fprintf(STDERR, " height_max = %lu\n",
2059
(unsigned long) png_get_user_height_max(dummy_ptr));
2060
if (png_get_chunk_cache_max(dummy_ptr) == 0)
2061
fprintf(STDERR, " cache_max = unlimited\n");
2062
else
2063
fprintf(STDERR, " cache_max = %lu\n",
2064
(unsigned long) png_get_chunk_cache_max(dummy_ptr));
2065
if (png_get_chunk_malloc_max(dummy_ptr) == 0)
2066
fprintf(STDERR, " malloc_max = unlimited\n");
2067
else
2068
fprintf(STDERR, " malloc_max = %lu\n",
2069
(unsigned long) png_get_chunk_malloc_max(dummy_ptr));
2070
png_destroy_read_struct(&dummy_ptr, NULL, NULL);
2071
2072
return (int)(ierror != 0);
2073
}
2074
#else
2075
int
2076
main(void)
2077
{
2078
fprintf(STDERR,
2079
" test ignored because libpng was not built with read support\n");
2080
/* And skip this test */
2081
return PNG_LIBPNG_VER < 10600 ? 0 : 77;
2082
}
2083
#endif
2084
2085
/* Generate a compiler error if there is an old png.h in the search path. */
2086
typedef png_libpng_version_1_7_0beta90 Your_png_h_is_not_version_1_7_0beta90;
2087
2088