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