Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 2 | /* pngtest.c - a simple test program to test libpng |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3 | * |
| 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 Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 31 | |
| 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 Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 37 | #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 Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 44 | /* #define STDERR stderr */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 45 | #define STDERR stdout /* for DOS */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 46 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame^] | 47 | /* 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 |
| 56 | static void |
| 57 | png_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 Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 72 | #else |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame^] | 73 | /* 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 | |
| 81 | static void |
| 82 | png_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 Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 120 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 121 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame^] | 122 | #if defined(PNG_WRITE_FLUSH_SUPPORTED) |
| 123 | static void |
| 124 | png_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 Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 132 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame^] | 133 | /* 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 |
| 138 | static void |
| 139 | png_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 | |
| 158 | static void |
| 159 | png_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 | */ |
| 205 | static void |
| 206 | png_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 | */ |
| 219 | static void |
| 220 | png_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 */ |
| 230 | int test(PNG_CONST char *inname, PNG_CONST char *outname) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 231 | { |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 232 | FILE *fpin, *fpout; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 233 | png_structp read_ptr, write_ptr; |
| 234 | png_infop read_info_ptr, write_info_ptr, end_info_ptr; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 235 | png_bytep row_buf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 236 | png_uint_32 y; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 237 | png_uint_32 width, height; |
| 238 | int num_pass, pass; |
| 239 | int bit_depth, color_type; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 240 | #ifdef USE_FAR_KEYWORD |
| 241 | jmp_buf jmpbuf; |
| 242 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame^] | 243 | |
| 244 | char inbuf[256], outbuf[256]; |
| 245 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 246 | row_buf = (png_bytep)NULL; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 247 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 248 | if ((fpin = fopen(inname, "rb")) == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 249 | { |
| 250 | fprintf(STDERR, "Could not find input file %s\n", inname); |
| 251 | return 1; |
| 252 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 253 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 254 | if ((fpout = fopen(outname, "wb")) == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 255 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 256 | fprintf(STDERR, "Could not open output file %s\n", outname); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 257 | fclose(fpin); |
| 258 | return 1; |
| 259 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 260 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 261 | png_debug(0, "Allocating read and write structures\n"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 262 | read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 263 | (png_error_ptr)NULL, (png_error_ptr)NULL); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame^] | 264 | #if defined(PNG_NO_STDIO) |
| 265 | png_set_error_fn(read_ptr, (png_voidp)inname, png_default_error, png_default_warning); |
| 266 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 267 | write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 268 | (png_error_ptr)NULL, (png_error_ptr)NULL); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame^] | 269 | #if defined(PNG_NO_STDIO) |
| 270 | png_set_error_fn(write_ptr, (png_voidp)inname, png_default_error, png_default_warning); |
| 271 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 272 | 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 Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 276 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 277 | png_debug(0, "Setting jmpbuf for read struct\n"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 278 | #ifdef USE_FAR_KEYWORD |
| 279 | if (setjmp(jmpbuf)) |
| 280 | #else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 281 | if (setjmp(read_ptr->jmpbuf)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 282 | #endif |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 283 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame^] | 284 | fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 285 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
| 286 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 287 | fclose(fpin); |
| 288 | fclose(fpout); |
| 289 | return 1; |
| 290 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 291 | |
| 292 | png_debug(0, "Setting jmpbuf for write struct\n"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 293 | #ifdef USE_FAR_KEYWORD |
| 294 | png_memcpy(read_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf)); |
| 295 | if (setjmp(jmpbuf)) |
| 296 | #else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 297 | if (setjmp(write_ptr->jmpbuf)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 298 | #endif |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 299 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame^] | 300 | fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 301 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
| 302 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 303 | fclose(fpin); |
| 304 | fclose(fpout); |
| 305 | return 1; |
| 306 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 307 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 308 | #ifdef USE_FAR_KEYWORD |
| 309 | png_memcpy(write_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf)); |
| 310 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 311 | png_debug(0, "Initializing input and output streams\n"); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame^] | 312 | #if !defined(PNG_NO_STDIO) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 313 | png_init_io(read_ptr, fpin); |
| 314 | png_init_io(write_ptr, fpout); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame^] | 315 | #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 Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 324 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 325 | png_debug(0, "Reading info struct\n"); |
| 326 | png_read_info(read_ptr, read_info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 327 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 328 | png_debug(0, "Transferring info struct\n"); |
| 329 | { |
| 330 | int interlace_type, compression_type, filter_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 331 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 332 | 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-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 371 | #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 Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 381 | #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, ¶ms)) |
| 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 Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 491 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 492 | fprintf(STDERR, "No memory to allocate row buffer\n"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 493 | png_destroy_read_struct(&read_ptr, &read_info_ptr, (png_infopp)NULL); |
| 494 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 495 | fclose(fpin); |
| 496 | fclose(fpout); |
| 497 | return 1; |
| 498 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 499 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 500 | num_pass = png_set_interlace_handling(read_ptr); |
| 501 | png_set_interlace_handling(write_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 502 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 503 | for (pass = 0; pass < num_pass; pass++) |
| 504 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 505 | for (y = 0; y < height; y++) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 506 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 507 | png_read_rows(read_ptr, (png_bytepp)&row_buf, (png_bytepp)NULL, 1); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 508 | png_write_rows(write_ptr, (png_bytepp)&row_buf, 1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 509 | } |
| 510 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 511 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 512 | 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 Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 515 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 516 | 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 Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 519 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 520 | fclose(fpin); |
| 521 | fclose(fpout); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 522 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 523 | png_free(read_ptr, row_buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 524 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 525 | png_debug(0, "Opening files for comparison\n"); |
| 526 | if ((fpin = fopen(inname, "rb")) == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 527 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 528 | fprintf(STDERR, "Could not find file %s\n", inname); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 529 | return 1; |
| 530 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 531 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 532 | if ((fpout = fopen(outname, "rb")) == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 533 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 534 | fprintf(STDERR, "Could not find file %s\n", outname); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 535 | fclose(fpin); |
| 536 | return 1; |
| 537 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 538 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 539 | while (1) |
| 540 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 541 | png_size_t num_in, num_out; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 542 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 543 | num_in = fread(inbuf, 1, 1, fpin); |
| 544 | num_out = fread(outbuf, 1, 1, fpout); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 545 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 546 | if (num_in != num_out) |
| 547 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 548 | fprintf(STDERR, "Files %s and %s are of a different size\n", |
| 549 | inname, outname); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 550 | fclose(fpin); |
| 551 | fclose(fpout); |
| 552 | return 1; |
| 553 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 554 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 555 | if (!num_in) |
| 556 | break; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 557 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 558 | if (png_memcmp(inbuf, outbuf, num_in)) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 559 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 560 | fprintf(STDERR, "Files %s and %s are different\n", inname, outname); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 561 | fclose(fpin); |
| 562 | fclose(fpout); |
| 563 | return 1; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | fclose(fpin); |
| 568 | fclose(fpout); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 569 | |
| 570 | return 0; |
| 571 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 572 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame^] | 573 | /* input and output filenames */ |
| 574 | #ifdef RISCOS |
| 575 | PNG_CONST char *inname = "pngtest_png"; |
| 576 | PNG_CONST char *outname = "pngout_png"; |
| 577 | #else |
| 578 | PNG_CONST char *inname = "pngtest.png"; |
| 579 | PNG_CONST char *outname = "pngout.png"; |
| 580 | #endif |
| 581 | |
| 582 | int |
| 583 | main(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 | } |