libpng/pngtest.c

234 lines
5.5 KiB
C
Raw Normal View History

1997-01-17 02:34:35 -05:00
1996-01-16 02:51:56 -05:00
/* pngtest.c - a simple test program to test libpng
1995-07-20 03:43:20 -04:00
1997-01-17 02:34:35 -05:00
libpng 1.0 beta 4 - version 0.90
1995-07-20 03:43:20 -04:00
For conditions of distribution and use, see copyright notice in png.h
1996-01-26 02:38:47 -05:00
Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
1997-01-17 02:34:35 -05:00
January 10, 1997
1996-01-16 02:51:56 -05:00
*/
1995-07-20 03:43:20 -04:00
#include <stdio.h>
#include <stdlib.h>
#include "png.h"
#ifdef __TURBOC__
#include <mem.h>
#endif
/* defined so I can write to a file on gui/windowing platforms */
1995-12-19 04:22:19 -05:00
/* #define STDERR stderr */
1996-01-26 02:38:47 -05:00
#define STDERR stdout /* for DOS */
1995-07-20 03:43:20 -04:00
/* input and output filenames */
1996-06-05 16:50:50 -04:00
#ifdef RISCOS
1997-01-17 02:34:35 -05:00
char *inname = "pngtest_png";
1996-06-05 16:50:50 -04:00
char *outname = "pngout_png";
#else
char *inname = "pngtest.png";
char *outname = "pngout.png";
#endif
1995-07-20 03:43:20 -04:00
char inbuf[256], outbuf[256];
1996-06-05 16:50:50 -04:00
int main(int argc, char *argv[])
1995-07-20 03:43:20 -04:00
{
1995-11-28 12:22:13 -05:00
FILE *fpin, *fpout;
1996-06-05 16:50:50 -04:00
png_structp read_ptr;
png_structp write_ptr;
png_infop info_ptr;
png_infop end_info;
1996-01-26 02:38:47 -05:00
png_bytep row_buf;
1996-06-05 16:50:50 -04:00
png_byte *near_row_buf;
1995-11-28 12:22:13 -05:00
png_uint_32 rowbytes;
1995-07-20 03:43:20 -04:00
png_uint_32 y;
1995-11-28 12:22:13 -05:00
int channels, num_pass, pass;
1997-01-17 02:34:35 -05:00
#ifdef USE_FAR_KEYWORD
jmp_buf jmpbuf;
#endif
1996-06-05 16:50:50 -04:00
row_buf = (png_bytep)NULL;
near_row_buf = (png_byte *)NULL;
1995-07-20 03:43:20 -04:00
1995-09-26 06:22:39 -04:00
fprintf(STDERR, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
{
fprintf(STDERR,
"Warning: versions are different between png.h and png.c\n");
fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
}
1996-06-05 16:50:50 -04:00
if (argc > 1)
inname = argv[1];
if (argc > 2)
outname = argv[2];
if (argc > 3)
{
fprintf(stderr, "usage: %s [infile.png] [outfile.png]\n", argv[0]);
exit(1);
}
1995-11-28 12:22:13 -05:00
fpin = fopen(inname, "rb");
if (!fpin)
{
fprintf(STDERR, "Could not find input file %s\n", inname);
return 1;
}
fpout = fopen(outname, "wb");
if (!fpout)
{
1996-06-05 16:50:50 -04:00
fprintf(STDERR, "Could not open output file %s\n", outname);
1995-11-28 12:22:13 -05:00
fclose(fpin);
return 1;
}
1997-01-17 02:34:35 -05:00
read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
1996-06-05 16:50:50 -04:00
(png_error_ptr)NULL, (png_error_ptr)NULL);
1997-01-17 02:34:35 -05:00
write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
1996-06-05 16:50:50 -04:00
(png_error_ptr)NULL, (png_error_ptr)NULL);
info_ptr = png_create_info_struct(read_ptr);
end_info = png_create_info_struct(read_ptr);
1997-01-17 02:34:35 -05:00
#ifdef USE_FAR_KEYWORD
if (setjmp(jmpbuf))
#else
1996-06-05 16:50:50 -04:00
if (setjmp(read_ptr->jmpbuf))
1997-01-17 02:34:35 -05:00
#endif
1995-11-28 12:22:13 -05:00
{
fprintf(STDERR, "libpng read error\n");
1996-06-05 16:50:50 -04:00
png_destroy_read_struct(&read_ptr, &info_ptr, &end_info);
png_destroy_write_struct(&write_ptr, (png_infopp)NULL);
1995-11-28 12:22:13 -05:00
fclose(fpin);
fclose(fpout);
return 1;
}
1997-01-17 02:34:35 -05:00
#ifdef USE_FAR_KEYWORD
png_memcpy(read_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
if (setjmp(jmpbuf))
#else
1996-06-05 16:50:50 -04:00
if (setjmp(write_ptr->jmpbuf))
1997-01-17 02:34:35 -05:00
#endif
1996-01-26 02:38:47 -05:00
{
1995-11-28 12:22:13 -05:00
fprintf(STDERR, "libpng write error\n");
1996-06-05 16:50:50 -04:00
png_destroy_read_struct(&read_ptr, &info_ptr, &end_info);
png_destroy_write_struct(&write_ptr, (png_infopp)NULL);
1995-11-28 12:22:13 -05:00
fclose(fpin);
fclose(fpout);
return 1;
}
1997-01-17 02:34:35 -05:00
#ifdef USE_FAR_KEYWORD
png_memcpy(write_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
#endif
1996-06-05 16:50:50 -04:00
png_init_io(read_ptr, fpin);
png_init_io(write_ptr, fpout);
1995-11-28 12:22:13 -05:00
1996-06-05 16:50:50 -04:00
png_read_info(read_ptr, info_ptr);
png_write_info(write_ptr, info_ptr);
1995-11-28 12:22:13 -05:00
1996-06-05 16:50:50 -04:00
if ((info_ptr->color_type & PNG_COLOR_TYPE_PALETTE)==PNG_COLOR_TYPE_PALETTE)
1995-11-28 12:22:13 -05:00
channels = 1;
1996-06-05 16:50:50 -04:00
else
channels = 3;
if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1995-11-28 12:22:13 -05:00
channels++;
1996-06-05 16:50:50 -04:00
rowbytes = ((info_ptr->width * info_ptr->bit_depth * channels + 7) >> 3);
1996-01-26 02:38:47 -05:00
near_row_buf = (png_byte *)malloc((size_t)rowbytes);
1996-01-16 02:51:56 -05:00
row_buf = (png_bytep)near_row_buf;
1995-11-28 12:22:13 -05:00
if (!row_buf)
{
1996-06-05 16:50:50 -04:00
fprintf(STDERR, "No memory to allocate row buffer\n");
png_destroy_read_struct(&read_ptr, &info_ptr, &end_info);
png_destroy_write_struct(&write_ptr, (png_infopp)NULL);
1995-11-28 12:22:13 -05:00
fclose(fpin);
fclose(fpout);
return 1;
}
1996-06-05 16:50:50 -04:00
if (info_ptr->interlace_type)
1995-11-28 12:22:13 -05:00
{
1996-06-05 16:50:50 -04:00
num_pass = png_set_interlace_handling(read_ptr);
num_pass = png_set_interlace_handling(write_ptr);
1995-11-28 12:22:13 -05:00
}
else
{
num_pass = 1;
}
for (pass = 0; pass < num_pass; pass++)
{
1996-06-05 16:50:50 -04:00
for (y = 0; y < info_ptr->height; y++)
1995-11-28 12:22:13 -05:00
{
1996-01-16 02:51:56 -05:00
#ifdef TESTING
fprintf(STDERR, "Processing line #%ld\n", y);
#endif
1996-06-05 16:50:50 -04:00
png_read_rows(read_ptr, (png_bytepp)&row_buf, (png_bytepp)0, 1);
png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
1995-11-28 12:22:13 -05:00
}
}
1996-06-05 16:50:50 -04:00
png_read_end(read_ptr, end_info);
png_write_end(write_ptr, end_info);
1995-11-28 12:22:13 -05:00
1996-06-05 16:50:50 -04:00
png_destroy_read_struct(&read_ptr, &info_ptr, &end_info);
png_destroy_write_struct(&write_ptr, (png_infopp)NULL);
1995-11-28 12:22:13 -05:00
fclose(fpin);
fclose(fpout);
1997-01-17 02:34:35 -05:00
free((png_byte *)near_row_buf);
1995-11-28 12:22:13 -05:00
fpin = fopen(inname, "rb");
if (!fpin)
{
1996-06-05 16:50:50 -04:00
fprintf(STDERR, "Could not find file %s\n", inname);
1995-11-28 12:22:13 -05:00
return 1;
}
fpout = fopen(outname, "rb");
if (!fpout)
{
1996-06-05 16:50:50 -04:00
fprintf(STDERR, "Could not find file %s\n", outname);
1995-11-28 12:22:13 -05:00
fclose(fpin);
return 1;
}
while (1)
{
int num_in, num_out;
1997-01-17 02:34:35 -05:00
num_in = fread(inbuf, 1, 1, fpin);
num_out = fread(outbuf, 1, 1, fpout);
1995-11-28 12:22:13 -05:00
if (num_in != num_out)
{
1996-06-05 16:50:50 -04:00
fprintf(STDERR, "Files %s and %s are of a different size\n",
inname, outname);
1995-11-28 12:22:13 -05:00
fclose(fpin);
fclose(fpout);
return 1;
}
if (!num_in)
break;
if (memcmp(inbuf, outbuf, num_in))
{
1996-06-05 16:50:50 -04:00
fprintf(STDERR, "Files %s and %s are different\n", inname, outname);
1995-11-28 12:22:13 -05:00
fclose(fpin);
fclose(fpout);
return 1;
}
}
fclose(fpin);
fclose(fpout);
1995-07-20 03:43:20 -04:00
fprintf(STDERR, "libpng passes test\n");
return 0;
}
1995-09-26 06:22:39 -04:00