pngtest.c 5.5 KB
Newer Older
A
Andreas Dilger 已提交
1

G
Guy Schalnat 已提交
2
/* pngtest.c - a simple test program to test libpng
G
Guy Schalnat 已提交
3

A
Andreas Dilger 已提交
4
   libpng 1.0 beta 4 - version 0.90
G
Guy Schalnat 已提交
5
   For conditions of distribution and use, see copyright notice in png.h
G
Guy Schalnat 已提交
6
   Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
A
Andreas Dilger 已提交
7
   January 10, 1997
G
Guy Schalnat 已提交
8
*/
G
Guy Schalnat 已提交
9 10 11 12 13 14 15 16 17 18

#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 */
G
Guy Schalnat 已提交
19
/*  #define STDERR stderr  */
G
Guy Schalnat 已提交
20
#define STDERR stdout   /* for DOS */
G
Guy Schalnat 已提交
21 22

/* input and output filenames */
G
Guy Schalnat 已提交
23
#ifdef RISCOS
A
Andreas Dilger 已提交
24
char *inname = "pngtest_png";
G
Guy Schalnat 已提交
25 26 27 28 29
char *outname = "pngout_png";
#else
char *inname = "pngtest.png";
char *outname = "pngout.png";
#endif
G
Guy Schalnat 已提交
30 31 32

char inbuf[256], outbuf[256];

G
Guy Schalnat 已提交
33
int main(int argc, char *argv[])
G
Guy Schalnat 已提交
34
{
G
Guy Schalnat 已提交
35
   FILE *fpin, *fpout;
G
Guy Schalnat 已提交
36 37 38 39
   png_structp read_ptr;
   png_structp write_ptr;
   png_infop info_ptr;
   png_infop end_info;
G
Guy Schalnat 已提交
40
   png_bytep row_buf;
G
Guy Schalnat 已提交
41
   png_byte *near_row_buf;
G
Guy Schalnat 已提交
42
   png_uint_32 rowbytes;
G
Guy Schalnat 已提交
43
   png_uint_32 y;
G
Guy Schalnat 已提交
44
   int channels, num_pass, pass;
A
Andreas Dilger 已提交
45 46 47
#ifdef USE_FAR_KEYWORD
   jmp_buf jmpbuf;
#endif   
G
Guy Schalnat 已提交
48 49
   row_buf = (png_bytep)NULL;
   near_row_buf = (png_byte *)NULL;
G
Guy Schalnat 已提交
50

G
Guy Schalnat 已提交
51 52 53 54 55 56 57 58 59 60
   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);
   }

G
Guy Schalnat 已提交
61 62 63 64 65 66 67 68 69 70 71 72
   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);
   }

G
Guy Schalnat 已提交
73 74 75 76 77 78 79 80 81 82
   fpin = fopen(inname, "rb");
   if (!fpin)
   {
      fprintf(STDERR, "Could not find input file %s\n", inname);
      return 1;
   }

   fpout = fopen(outname, "wb");
   if (!fpout)
   {
G
Guy Schalnat 已提交
83
      fprintf(STDERR, "Could not open output file %s\n", outname);
G
Guy Schalnat 已提交
84 85 86 87
      fclose(fpin);
      return 1;
   }

A
Andreas Dilger 已提交
88
   read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
G
Guy Schalnat 已提交
89
      (png_error_ptr)NULL,  (png_error_ptr)NULL);
A
Andreas Dilger 已提交
90
   write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
G
Guy Schalnat 已提交
91 92 93 94
      (png_error_ptr)NULL, (png_error_ptr)NULL);
   info_ptr = png_create_info_struct(read_ptr);
   end_info = png_create_info_struct(read_ptr);

A
Andreas Dilger 已提交
95 96 97
#ifdef USE_FAR_KEYWORD
   if (setjmp(jmpbuf))
#else
G
Guy Schalnat 已提交
98
   if (setjmp(read_ptr->jmpbuf))
A
Andreas Dilger 已提交
99
#endif
G
Guy Schalnat 已提交
100 101
   {
      fprintf(STDERR, "libpng read error\n");
G
Guy Schalnat 已提交
102 103
      png_destroy_read_struct(&read_ptr, &info_ptr, &end_info);
      png_destroy_write_struct(&write_ptr, (png_infopp)NULL);
G
Guy Schalnat 已提交
104 105 106 107
      fclose(fpin);
      fclose(fpout);
      return 1;
   }
A
Andreas Dilger 已提交
108 109 110 111
#ifdef USE_FAR_KEYWORD
   png_memcpy(read_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
   if (setjmp(jmpbuf))
#else
G
Guy Schalnat 已提交
112
   if (setjmp(write_ptr->jmpbuf))
A
Andreas Dilger 已提交
113
#endif
G
Guy Schalnat 已提交
114
   {
G
Guy Schalnat 已提交
115
      fprintf(STDERR, "libpng write error\n");
G
Guy Schalnat 已提交
116 117
      png_destroy_read_struct(&read_ptr, &info_ptr, &end_info);
      png_destroy_write_struct(&write_ptr, (png_infopp)NULL);
G
Guy Schalnat 已提交
118 119 120 121
      fclose(fpin);
      fclose(fpout);
      return 1;
   }
A
Andreas Dilger 已提交
122 123 124
#ifdef USE_FAR_KEYWORD
   png_memcpy(write_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
#endif
G
Guy Schalnat 已提交
125 126
   png_init_io(read_ptr, fpin);
   png_init_io(write_ptr, fpout);
G
Guy Schalnat 已提交
127

G
Guy Schalnat 已提交
128 129
   png_read_info(read_ptr, info_ptr);
   png_write_info(write_ptr, info_ptr);
G
Guy Schalnat 已提交
130

G
Guy Schalnat 已提交
131
   if ((info_ptr->color_type & PNG_COLOR_TYPE_PALETTE)==PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
132
      channels = 1;
G
Guy Schalnat 已提交
133 134 135
   else
      channels = 3;
   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
G
Guy Schalnat 已提交
136 137
      channels++;

G
Guy Schalnat 已提交
138
   rowbytes = ((info_ptr->width * info_ptr->bit_depth * channels + 7) >> 3);
G
Guy Schalnat 已提交
139
   near_row_buf = (png_byte *)malloc((size_t)rowbytes);
G
Guy Schalnat 已提交
140
   row_buf = (png_bytep)near_row_buf;
G
Guy Schalnat 已提交
141 142
   if (!row_buf)
   {
G
Guy Schalnat 已提交
143 144 145
      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);
G
Guy Schalnat 已提交
146 147 148 149 150
      fclose(fpin);
      fclose(fpout);
      return 1;
   }

G
Guy Schalnat 已提交
151
   if (info_ptr->interlace_type)
G
Guy Schalnat 已提交
152
   {
G
Guy Schalnat 已提交
153 154
      num_pass = png_set_interlace_handling(read_ptr);
      num_pass = png_set_interlace_handling(write_ptr);
G
Guy Schalnat 已提交
155 156 157 158 159 160 161 162
   }
   else
   {
      num_pass = 1;
   }

   for (pass = 0; pass < num_pass; pass++)
   {
G
Guy Schalnat 已提交
163
      for (y = 0; y < info_ptr->height; y++)
G
Guy Schalnat 已提交
164
      {
G
Guy Schalnat 已提交
165 166 167
#ifdef TESTING
         fprintf(STDERR, "Processing line #%ld\n", y);
#endif
G
Guy Schalnat 已提交
168 169
         png_read_rows(read_ptr, (png_bytepp)&row_buf, (png_bytepp)0, 1);
         png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
G
Guy Schalnat 已提交
170 171 172
      }
   }

G
Guy Schalnat 已提交
173 174
   png_read_end(read_ptr, end_info);
   png_write_end(write_ptr, end_info);
G
Guy Schalnat 已提交
175

G
Guy Schalnat 已提交
176 177
   png_destroy_read_struct(&read_ptr, &info_ptr, &end_info);
   png_destroy_write_struct(&write_ptr, (png_infopp)NULL);
G
Guy Schalnat 已提交
178 179 180 181

   fclose(fpin);
   fclose(fpout);

A
Andreas Dilger 已提交
182
   free((png_byte *)near_row_buf);
G
Guy Schalnat 已提交
183 184 185 186 187

   fpin = fopen(inname, "rb");

   if (!fpin)
   {
G
Guy Schalnat 已提交
188
      fprintf(STDERR, "Could not find file %s\n", inname);
G
Guy Schalnat 已提交
189 190 191 192 193 194
      return 1;
   }

   fpout = fopen(outname, "rb");
   if (!fpout)
   {
G
Guy Schalnat 已提交
195
      fprintf(STDERR, "Could not find file %s\n", outname);
G
Guy Schalnat 已提交
196 197 198 199 200 201 202
      fclose(fpin);
      return 1;
   }
   while (1)
   {
      int num_in, num_out;

A
Andreas Dilger 已提交
203 204
      num_in = fread(inbuf, 1, 1, fpin);
      num_out = fread(outbuf, 1, 1, fpout);
G
Guy Schalnat 已提交
205 206 207

      if (num_in != num_out)
      {
G
Guy Schalnat 已提交
208 209
         fprintf(STDERR, "Files %s and %s are of a different size\n",
                 inname, outname);
G
Guy Schalnat 已提交
210 211 212 213 214 215 216 217 218 219
         fclose(fpin);
         fclose(fpout);
         return 1;
      }

      if (!num_in)
         break;

      if (memcmp(inbuf, outbuf, num_in))
      {
G
Guy Schalnat 已提交
220
         fprintf(STDERR, "Files %s and %s are different\n", inname, outname);
G
Guy Schalnat 已提交
221 222 223 224 225 226 227 228
         fclose(fpin);
         fclose(fpout);
         return 1;
      }
   }

   fclose(fpin);
   fclose(fpout);
G
Guy Schalnat 已提交
229 230 231 232
   fprintf(STDERR, "libpng passes test\n");

   return 0;
}
G
Guy Schalnat 已提交
233