blob: cea4c8eda659f69c939520f50f3361905ab9cff6 [file] [log] [blame]
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001
Guy Schalnat4ee97b01996-01-16 01:51:56 -06002/* pngtest.c - a simple test program to test libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
4 * libpng 1.00.97
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
8 * May 28, 1997
9 *
10 * This program reads in a PNG image, writes it out again, and then
11 * compares the two files. If the files are identical, this shows that
12 * the basic chunk handling, filtering, and (de)compression code is working
13 * properly. It does not currently test all of the transforms, although
14 * it probably should.
15 *
16 * The program will fail in certain legitimate cases:
17 * 1) when the compression level or filter selection method is changed.
18 * 2) when the chunk size is smaller than 8K.
19 * 3) unknown ancillary chunks exist in the input file.
20 * 4) others not listed here...
21 * In these cases, it is best to check with another tool such as "pngcheck"
22 * to see what the differences between the two images are.
23 *
24 * If a filename is given on the command-line, then this file is used
25 * for the input, rather than the default "pngtest.png". This allows
26 * testing a wide variety of files easily.
27 */
Guy Schalnat0d580581995-07-20 02:43:20 -050028
29#include <stdio.h>
30#include <stdlib.h>
Andreas Dilger47a0c421997-05-16 02:46:07 -050031
32/* Makes pngtest verbose so we can find problems (needs to be before png.h) */
33#ifndef PNG_DEBUG
34#define PNG_DEBUG 0
35#endif
36
Guy Schalnat0d580581995-07-20 02:43:20 -050037#include "png.h"
38
39#ifdef __TURBOC__
40#include <mem.h>
41#endif
42
43/* defined so I can write to a file on gui/windowing platforms */
Guy Schalnat6d764711995-12-19 03:22:19 -060044/* #define STDERR stderr */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060045#define STDERR stdout /* for DOS */
Guy Schalnat0d580581995-07-20 02:43:20 -050046
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -060047/* START of code to validate stdio-free compilation */
48/* These copies of the default read/write functions come from pngrio.c and */
49/* pngwio.c. They allow "don't include stdio" testing of the library. */
50#if defined(PNG_NO_STDIO)
51/* This is the function which does the actual reading of data. If you are
52 not reading from a standard C stream, you should create a replacement
53 read_data function and use it at run time with png_set_read_fn(), rather
54 than changing the library. */
55#ifndef USE_FAR_KEYWORD
56static void
57png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
58{
59 png_size_t check;
60
61 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
62 * instead of an int, which is what fread() actually returns.
63 */
64 check = (png_size_t)fread(data, (png_size_t)1, length,
65 (FILE *)png_ptr->io_ptr);
66
67 if (check != length)
68 {
69 png_error(png_ptr, "Read Error");
70 }
71}
Guy Schalnate5a37791996-06-05 15:50:50 -050072#else
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -060073/* this is the model-independent version. Since the standard I/O library
74 can't handle far buffers in the medium and small models, we have to copy
75 the data.
76*/
77
78#define NEAR_BUF_SIZE 1024
79#define MIN(a,b) (a <= b ? a : b)
80
81static void
82png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
83{
84 int check;
85 png_byte *n_data;
86 FILE *io_ptr;
87
88 /* Check if data really is near. If so, use usual code. */
89 n_data = (png_byte *)CVT_PTR_NOCHECK(data);
90 io_ptr = (FILE *)CVT_PTR(png_ptr->io_ptr);
91 if ((png_bytep)n_data == data)
92 {
93 check = fread(n_data, 1, length, io_ptr);
94 }
95 else
96 {
97 png_byte buf[NEAR_BUF_SIZE];
98 png_size_t read, remaining, err;
99 check = 0;
100 remaining = length;
101 do
102 {
103 read = MIN(NEAR_BUF_SIZE, remaining);
104 err = fread(buf, (png_size_t)1, read, io_ptr);
105 png_memcpy(data, buf, read); /* copy far buffer to near buffer */
106 if(err != read)
107 break;
108 else
109 check += err;
110 data += read;
111 remaining -= read;
112 }
113 while (remaining != 0);
114 }
115 if (check != length)
116 {
117 png_error(png_ptr, "read Error");
118 }
119}
Guy Schalnate5a37791996-06-05 15:50:50 -0500120#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500121
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600122#if defined(PNG_WRITE_FLUSH_SUPPORTED)
123static void
124png_default_flush(png_structp png_ptr)
125{
126 FILE *io_ptr;
127 io_ptr = (FILE *)CVT_PTR((png_ptr->io_ptr));
128 if (io_ptr != NULL)
129 fflush(io_ptr);
130}
131#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500132
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600133/* This is the function which does the actual writing of data. If you are
134 not writing to a standard C stream, you should create a replacement
135 write_data function and use it at run time with png_set_write_fn(), rather
136 than changing the library. */
137#ifndef USE_FAR_KEYWORD
138static void
139png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
140{
141 png_uint_32 check;
142
143 check = fwrite(data, 1, length, (FILE *)(png_ptr->io_ptr));
144 if (check != length)
145 {
146 png_error(png_ptr, "Write Error");
147 }
148}
149#else
150/* this is the model-independent version. Since the standard I/O library
151 can't handle far buffers in the medium and small models, we have to copy
152 the data.
153*/
154
155#define NEAR_BUF_SIZE 1024
156#define MIN(a,b) (a <= b ? a : b)
157
158static void
159png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
160{
161 png_uint_32 check;
162 png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
163 FILE *io_ptr;
164
165 /* Check if data really is near. If so, use usual code. */
166 near_data = (png_byte *)CVT_PTR_NOCHECK(data);
167 io_ptr = (FILE *)CVT_PTR(png_ptr->io_ptr);
168 if ((png_bytep)near_data == data)
169 {
170 check = fwrite(near_data, 1, length, io_ptr);
171 }
172 else
173 {
174 png_byte buf[NEAR_BUF_SIZE];
175 png_size_t written, remaining, err;
176 check = 0;
177 remaining = length;
178 do
179 {
180 written = MIN(NEAR_BUF_SIZE, remaining);
181 png_memcpy(buf, data, written); /* copy far buffer to near buffer */
182 err = fwrite(buf, 1, written, io_ptr);
183 if (err != written)
184 break;
185 else
186 check += err;
187 data += written;
188 remaining -= written;
189 }
190 while (remaining != 0);
191 }
192 if (check != length)
193 {
194 png_error(png_ptr, "Write Error");
195 }
196}
197
198#endif
199
200/* This function is called when there is a warning, but the library thinks
201 * it can continue anyway. Replacement functions don't have to do anything
202 * here if you don't want to. In the default configuration, png_ptr is
203 * not used, but it is passed in case it may be useful.
204 */
205static void
206png_default_warning(png_structp png_ptr, png_const_charp message)
207{
208 PNG_CONST char *name = "UNKNOWN (ERROR!)";
209 if (png_ptr != NULL && png_ptr->error_ptr != NULL)
210 name = png_ptr->error_ptr;
211 fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
212}
213
214/* This is the default error handling function. Note that replacements for
215 * this function MUST NOT RETURN, or the program will likely crash. This
216 * function is used by default, or if the program supplies NULL for the
217 * error function pointer in png_set_error_fn().
218 */
219static void
220png_default_error(png_structp png_ptr, png_const_charp message)
221{
222 png_default_warning(png_ptr, message);
223 /* We can return because png_error calls the default handler which is
224 * actually ok in this case. */
225}
226#endif
227/* END of code to validate stdio-free compilation */
228
229/* Test one file */
230int test(PNG_CONST char *inname, PNG_CONST char *outname)
Guy Schalnat0d580581995-07-20 02:43:20 -0500231{
Guy Schalnat0f716451995-11-28 11:22:13 -0600232 FILE *fpin, *fpout;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500233 png_structp read_ptr, write_ptr;
234 png_infop read_info_ptr, write_info_ptr, end_info_ptr;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600235 png_bytep row_buf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500236 png_uint_32 y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500237 png_uint_32 width, height;
238 int num_pass, pass;
239 int bit_depth, color_type;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600240#ifdef USE_FAR_KEYWORD
241 jmp_buf jmpbuf;
242#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600243
244 char inbuf[256], outbuf[256];
245
Guy Schalnate5a37791996-06-05 15:50:50 -0500246 row_buf = (png_bytep)NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500247
Andreas Dilger47a0c421997-05-16 02:46:07 -0500248 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600249 {
250 fprintf(STDERR, "Could not find input file %s\n", inname);
251 return 1;
252 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500253
Andreas Dilger47a0c421997-05-16 02:46:07 -0500254 if ((fpout = fopen(outname, "wb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600255 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500256 fprintf(STDERR, "Could not open output file %s\n", outname);
Guy Schalnat0f716451995-11-28 11:22:13 -0600257 fclose(fpin);
258 return 1;
259 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500260
Andreas Dilger47a0c421997-05-16 02:46:07 -0500261 png_debug(0, "Allocating read and write structures\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600262 read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500263 (png_error_ptr)NULL, (png_error_ptr)NULL);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600264#if defined(PNG_NO_STDIO)
265 png_set_error_fn(read_ptr, (png_voidp)inname, png_default_error, png_default_warning);
266#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600267 write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
Guy Schalnate5a37791996-06-05 15:50:50 -0500268 (png_error_ptr)NULL, (png_error_ptr)NULL);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600269#if defined(PNG_NO_STDIO)
270 png_set_error_fn(write_ptr, (png_voidp)inname, png_default_error, png_default_warning);
271#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500272 png_debug(0, "Allocating read_info, write_info and end_info structures\n");
273 read_info_ptr = png_create_info_struct(read_ptr);
274 write_info_ptr = png_create_info_struct(read_ptr);
275 end_info_ptr = png_create_info_struct(read_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500276
Andreas Dilger47a0c421997-05-16 02:46:07 -0500277 png_debug(0, "Setting jmpbuf for read struct\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600278#ifdef USE_FAR_KEYWORD
279 if (setjmp(jmpbuf))
280#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500281 if (setjmp(read_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600282#endif
Guy Schalnat0f716451995-11-28 11:22:13 -0600283 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600284 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500285 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
286 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600287 fclose(fpin);
288 fclose(fpout);
289 return 1;
290 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500291
292 png_debug(0, "Setting jmpbuf for write struct\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600293#ifdef USE_FAR_KEYWORD
294 png_memcpy(read_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
295 if (setjmp(jmpbuf))
296#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500297 if (setjmp(write_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600298#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600299 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600300 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500301 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
302 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600303 fclose(fpin);
304 fclose(fpout);
305 return 1;
306 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500307
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600308#ifdef USE_FAR_KEYWORD
309 png_memcpy(write_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
310#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500311 png_debug(0, "Initializing input and output streams\n");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600312#if !defined(PNG_NO_STDIO)
Guy Schalnate5a37791996-06-05 15:50:50 -0500313 png_init_io(read_ptr, fpin);
314 png_init_io(write_ptr, fpout);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600315#else
316 png_set_read_fn(read_ptr, (png_voidp)fpin, png_default_read_data);
317 png_set_write_fn(write_ptr, (png_voidp)fpout, png_default_write_data,
318#if defined(PNG_WRITE_FLUSH_SUPPORTED)
319 png_default_flush);
320#else
321 NULL);
322#endif
323#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500324
Andreas Dilger47a0c421997-05-16 02:46:07 -0500325 png_debug(0, "Reading info struct\n");
326 png_read_info(read_ptr, read_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500327
Andreas Dilger47a0c421997-05-16 02:46:07 -0500328 png_debug(0, "Transferring info struct\n");
329 {
330 int interlace_type, compression_type, filter_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500331
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
333 &color_type, &interlace_type, &compression_type, &filter_type))
334 {
335 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
336 color_type, interlace_type, compression_type, filter_type);
337 }
338 }
339#if defined(PNG_READ_bKGD_SUPPORTED) && defined(PNG_WRITE_bKGD_SUPPORTED)
340 {
341 png_color_16p background;
342
343 if (png_get_bKGD(read_ptr, read_info_ptr, &background))
344 {
345 png_set_bKGD(write_ptr, write_info_ptr, background);
346 }
347 }
348#endif
349#if defined(PNG_READ_cHRM_SUPPORTED) && defined(PNG_WRITE_cHRM_SUPPORTED)
350 {
351 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
352
353 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
354 &red_y, &green_x, &green_y, &blue_x, &blue_y))
355 {
356 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
357 red_y, green_x, green_y, blue_x, blue_y);
358 }
359 }
360#endif
361#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_WRITE_gAMA_SUPPORTED)
362 {
363 double gamma;
364
365 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
366 {
367 png_set_gAMA(write_ptr, write_info_ptr, gamma);
368 }
369 }
370#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600371#if defined(PNG_READ_sRGB_SUPPORTED) && defined(PNG_WRITE_sRGB_SUPPORTED)
372 {
373 png_byte intent;
374
375 if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
376 {
377 png_set_sRGB(write_ptr, write_info_ptr, intent);
378 }
379 }
380#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500381#if defined(PNG_READ_hIST_SUPPORTED) && defined(PNG_WRITE_hIST_SUPPORTED)
382 {
383 png_uint_16p hist;
384
385 if (png_get_hIST(read_ptr, read_info_ptr, &hist))
386 {
387 png_set_hIST(write_ptr, write_info_ptr, hist);
388 }
389 }
390#endif
391#if defined(PNG_READ_oFFs_SUPPORTED) && defined(PNG_WRITE_oFFs_SUPPORTED)
392 {
393 png_uint_32 offset_x, offset_y;
394 int unit_type;
395
396 if (png_get_oFFs(read_ptr, read_info_ptr,&offset_x,&offset_y,&unit_type))
397 {
398 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
399 }
400 }
401#endif
402#if defined(PNG_READ_pCAL_SUPPORTED) && defined(PNG_WRITE_pCAL_SUPPORTED)
403 {
404 png_charp purpose, units;
405 png_charpp params;
406 png_int_32 X0, X1;
407 int type, nparams;
408
409 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
410 &nparams, &units, &params))
411 {
412 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
413 nparams, units, params);
414 }
415 }
416#endif
417#if defined(PNG_READ_pHYs_SUPPORTED) && defined(PNG_WRITE_pHYs_SUPPORTED)
418 {
419 png_uint_32 res_x, res_y;
420 int unit_type;
421
422 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
423 {
424 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
425 }
426 }
427#endif
428 {
429 png_colorp palette;
430 int num_palette;
431
432 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
433 {
434 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
435 }
436 }
437#if defined(PNG_READ_sBIT_SUPPORTED) && defined(PNG_WRITE_sBIT_SUPPORTED)
438 {
439 png_color_8p sig_bit;
440
441 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
442 {
443 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
444 }
445 }
446#endif
447#if (defined(PNG_READ_tEXt_SUPPORTED) && defined(PNG_WRITE_tEXt_SUPPORTED)) || \
448 (defined(PNG_READ_zTXt_SUPPORTED) && defined(PNG_WRITE_zTXt_SUPPORTED))
449 {
450 png_textp text_ptr;
451 int num_text;
452
453 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
454 {
455 png_debug1(0, "Handling %d tEXt/zTXt chunks\n", num_text);
456 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
457 }
458 }
459#endif
460#if defined(PNG_READ_tIME_SUPPORTED) && defined(PNG_WRITE_tIME_SUPPORTED)
461 {
462 png_timep mod_time;
463
464 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
465 {
466 png_set_tIME(write_ptr, write_info_ptr, mod_time);
467 }
468 }
469#endif
470#if defined(PNG_READ_tRNS_SUPPORTED) && defined(PNG_WRITE_tRNS_SUPPORTED)
471 {
472 png_bytep trans;
473 int num_trans;
474 png_color_16p trans_values;
475
476 if (png_get_tRNS(read_ptr, read_info_ptr, &trans, &num_trans,
477 &trans_values))
478 {
479 png_set_tRNS(write_ptr, write_info_ptr, trans, num_trans,
480 trans_values);
481 }
482 }
483#endif
484
485 png_debug(0, "\nWriting info struct\n");
486 png_write_info(write_ptr, write_info_ptr);
487
488 row_buf = (png_bytep)png_malloc(read_ptr,
489 png_get_rowbytes(read_ptr, read_info_ptr));
490 if (row_buf == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600491 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500492 fprintf(STDERR, "No memory to allocate row buffer\n");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500493 png_destroy_read_struct(&read_ptr, &read_info_ptr, (png_infopp)NULL);
494 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600495 fclose(fpin);
496 fclose(fpout);
497 return 1;
498 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500499
Andreas Dilger47a0c421997-05-16 02:46:07 -0500500 num_pass = png_set_interlace_handling(read_ptr);
501 png_set_interlace_handling(write_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500502
Guy Schalnat0f716451995-11-28 11:22:13 -0600503 for (pass = 0; pass < num_pass; pass++)
504 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500505 for (y = 0; y < height; y++)
Guy Schalnat0f716451995-11-28 11:22:13 -0600506 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500507 png_read_rows(read_ptr, (png_bytepp)&row_buf, (png_bytepp)NULL, 1);
Guy Schalnate5a37791996-06-05 15:50:50 -0500508 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600509 }
510 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500511
Andreas Dilger47a0c421997-05-16 02:46:07 -0500512 png_debug(0, "Reading and writing end_info data\n");
513 png_read_end(read_ptr, end_info_ptr);
514 png_write_end(write_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500515
Andreas Dilger47a0c421997-05-16 02:46:07 -0500516 png_debug(0, "Destroying data structs\n");
517 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
518 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500519
Guy Schalnat0f716451995-11-28 11:22:13 -0600520 fclose(fpin);
521 fclose(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -0500522
Andreas Dilger47a0c421997-05-16 02:46:07 -0500523 png_free(read_ptr, row_buf);
Guy Schalnat0d580581995-07-20 02:43:20 -0500524
Andreas Dilger47a0c421997-05-16 02:46:07 -0500525 png_debug(0, "Opening files for comparison\n");
526 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600527 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500528 fprintf(STDERR, "Could not find file %s\n", inname);
Guy Schalnat0f716451995-11-28 11:22:13 -0600529 return 1;
530 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500531
Andreas Dilger47a0c421997-05-16 02:46:07 -0500532 if ((fpout = fopen(outname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600533 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500534 fprintf(STDERR, "Could not find file %s\n", outname);
Guy Schalnat0f716451995-11-28 11:22:13 -0600535 fclose(fpin);
536 return 1;
537 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500538
Guy Schalnat0f716451995-11-28 11:22:13 -0600539 while (1)
540 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500541 png_size_t num_in, num_out;
Guy Schalnat0d580581995-07-20 02:43:20 -0500542
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600543 num_in = fread(inbuf, 1, 1, fpin);
544 num_out = fread(outbuf, 1, 1, fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -0500545
Guy Schalnat0f716451995-11-28 11:22:13 -0600546 if (num_in != num_out)
547 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500548 fprintf(STDERR, "Files %s and %s are of a different size\n",
549 inname, outname);
Guy Schalnat0f716451995-11-28 11:22:13 -0600550 fclose(fpin);
551 fclose(fpout);
552 return 1;
553 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500554
Guy Schalnat0f716451995-11-28 11:22:13 -0600555 if (!num_in)
556 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500557
Andreas Dilger47a0c421997-05-16 02:46:07 -0500558 if (png_memcmp(inbuf, outbuf, num_in))
Guy Schalnat0f716451995-11-28 11:22:13 -0600559 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500560 fprintf(STDERR, "Files %s and %s are different\n", inname, outname);
Guy Schalnat0f716451995-11-28 11:22:13 -0600561 fclose(fpin);
562 fclose(fpout);
563 return 1;
564 }
565 }
566
567 fclose(fpin);
568 fclose(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -0500569
570 return 0;
571}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500572
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600573/* input and output filenames */
574#ifdef RISCOS
575PNG_CONST char *inname = "pngtest_png";
576PNG_CONST char *outname = "pngout_png";
577#else
578PNG_CONST char *inname = "pngtest.png";
579PNG_CONST char *outname = "pngout.png";
580#endif
581
582int
583main(int argc, char *argv[])
584{
585 int multiple = 0;
586 int ierror = 0;
587
588 fprintf(STDERR, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
589
590 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
591 {
592 fprintf(STDERR,
593 "Warning: versions are different between png.h and png.c\n");
594 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
595 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
596 ++ierror;
597 }
598
599 if (argc > 1)
600 {
601 if (strcmp(argv[1], "-m") == 0)
602 multiple = 1;
603 else
604 inname = argv[1];
605 }
606
607 if (!multiple && argc == 3)
608 outname = argv[2];
609
610 if (!multiple && argc > 3 || multiple && argc < 2)
611 {
612 fprintf(STDERR, "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
613 argv[0], argv[0]);
614 fprintf(STDERR, " reads/writes one PNG file (without -m) or multiple files (-m)\n");
615 fprintf(STDERR, " with -m %s is used as a temporary file\n", outname);
616 exit(1);
617 }
618
619 if (multiple)
620 {
621 int i;
622 for (i=2; i<argc; ++i)
623 ierror += test(argv[i], outname);
624 }
625 else
626 {
627 ierror += test(inname, outname);
628 }
629
630 if (ierror == 0)
631 fprintf(STDERR, "libpng passes test\n");
632 else
633 fprintf(STDERR, "libpng FAILS test\n");
634 return ierror != 0;
635}