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 | * |
Glenn Randers-Pehrson | f8b008c | 1999-09-18 10:54:36 -0500 | [diff] [blame^] | 4 | * libpng 1.0.4 - September 18, 1999 |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 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 |
Glenn Randers-Pehrson | c944229 | 1999-01-06 21:50:16 -0600 | [diff] [blame] | 8 | * Copyright (c) 1998, 1999 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 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 | * |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 16 | * The program will report "FAIL" in certain legitimate cases: |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 17 | * 1) when the compression level or filter selection method is changed. |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 18 | * 2) when the chunk size is not 8K. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 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 |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 26 | * testing a wide variety of files easily. You can also test a number |
| 27 | * of files at once by typing "pngtest -m file1.png file2.png ..." |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 28 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 32 | |
| 33 | /* Makes pngtest verbose so we can find problems (needs to be before png.h) */ |
| 34 | #ifndef PNG_DEBUG |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 35 | #define PNG_DEBUG 0 |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 36 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 37 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 38 | #include "png.h" |
| 39 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 40 | #if defined(PNG_TIME_RFC1123_SUPPORTED) |
| 41 | static int tIME_chunk_present=0; |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 42 | static char tIME_string[30] = "no tIME chunk present in file"; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 43 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
| 44 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 45 | static int verbose = 0; |
| 46 | |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 47 | int test_one_file PNGARG((PNG_CONST char *inname, PNG_CONST char *outname)); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 48 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 49 | #ifdef __TURBOC__ |
| 50 | #include <mem.h> |
| 51 | #endif |
| 52 | |
| 53 | /* defined so I can write to a file on gui/windowing platforms */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 54 | /* #define STDERR stderr */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 55 | #define STDERR stdout /* for DOS */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 56 | |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 57 | /* example of using row callbacks to make a simple progress meter */ |
| 58 | static int status_pass=1; |
| 59 | static int status_dots_requested=0; |
| 60 | static int status_dots=1; |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 61 | |
Glenn Randers-Pehrson | 7cd899c | 1998-03-07 16:17:42 -0600 | [diff] [blame] | 62 | void |
| 63 | read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 64 | { |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 65 | if(png_ptr == NULL || row_number > PNG_MAX_UINT) return; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 66 | if(status_pass != pass) |
| 67 | { |
| 68 | fprintf(stdout,"\n Pass %d: ",pass); |
| 69 | status_pass = pass; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 70 | status_dots = 31; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 71 | } |
| 72 | status_dots--; |
| 73 | if(status_dots == 0) |
| 74 | { |
| 75 | fprintf(stdout, "\n "); |
| 76 | status_dots=30; |
| 77 | } |
| 78 | fprintf(stdout, "r"); |
| 79 | } |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 80 | |
Glenn Randers-Pehrson | 7cd899c | 1998-03-07 16:17:42 -0600 | [diff] [blame] | 81 | void |
| 82 | write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 83 | { |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 84 | if(png_ptr == NULL || row_number > PNG_MAX_UINT || pass > 7) return; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 85 | fprintf(stdout, "w"); |
| 86 | } |
| 87 | |
| 88 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 89 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) |
| 90 | /* Example of using user transform callback (we don't transform anything, |
| 91 | but merely examine the row filters. We set this to 256 rather than |
| 92 | 5 in case illegal filter values are present.) */ |
| 93 | static png_uint_32 filters_used[256]; |
| 94 | void |
| 95 | count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data) |
| 96 | { |
| 97 | if(png_ptr != NULL && row_info != NULL) |
| 98 | ++filters_used[*(data-1)]; |
| 99 | } |
| 100 | #endif |
| 101 | |
Glenn Randers-Pehrson | 38d73af | 1998-03-07 21:30:44 -0600 | [diff] [blame] | 102 | #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 103 | /* example of using user transform callback (we don't transform anything, |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 104 | but merely count the zero samples) */ |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 105 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 106 | static png_uint_32 zero_samples; |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 107 | |
Glenn Randers-Pehrson | 7cd899c | 1998-03-07 16:17:42 -0600 | [diff] [blame] | 108 | void |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 109 | count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 110 | { |
| 111 | png_bytep dp = data; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 112 | if(png_ptr == NULL)return; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 113 | |
| 114 | /* contents of row_info: |
| 115 | * png_uint_32 width width of row |
| 116 | * png_uint_32 rowbytes number of bytes in row |
| 117 | * png_byte color_type color type of pixels |
| 118 | * png_byte bit_depth bit depth of samples |
| 119 | * png_byte channels number of channels (1-4) |
| 120 | * png_byte pixel_depth bits per pixel (depth*channels) |
| 121 | */ |
| 122 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 123 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 124 | /* counts the number of zero samples (or zero pixels if color_type is 3 */ |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 125 | |
| 126 | if(row_info->color_type == 0 || row_info->color_type == 3) |
| 127 | { |
| 128 | int pos=0; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 129 | png_uint_32 n, nstop; |
| 130 | for (n=0, nstop=row_info->width; n<nstop; n++) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 131 | { |
| 132 | if(row_info->bit_depth == 1) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 133 | { |
| 134 | if(((*dp << pos++ )& 0x80) == 0) zero_samples++; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 135 | if(pos == 8) |
| 136 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 137 | pos = 0; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 138 | dp++; |
| 139 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 140 | } |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 141 | if(row_info->bit_depth == 2) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 142 | { |
| 143 | if(((*dp << (pos+=2))& 0xc0) == 0) zero_samples++; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 144 | if(pos == 8) |
| 145 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 146 | pos = 0; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 147 | dp++; |
| 148 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 149 | } |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 150 | if(row_info->bit_depth == 4) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 151 | { |
| 152 | if(((*dp << (pos+=4))& 0xf0) == 0) zero_samples++; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 153 | if(pos == 8) |
| 154 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 155 | pos = 0; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 156 | dp++; |
| 157 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 158 | } |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 159 | if(row_info->bit_depth == 8) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 160 | if(*dp++ == 0) zero_samples++; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 161 | if(row_info->bit_depth == 16) |
| 162 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 163 | if((*dp | *(dp+1)) == 0) zero_samples++; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 164 | dp+=2; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | else /* other color types */ |
| 169 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 170 | png_uint_32 n, nstop; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 171 | int channel; |
| 172 | int color_channels = row_info->channels; |
| 173 | if(row_info->color_type > 3)color_channels--; |
| 174 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 175 | for (n=0, nstop=row_info->width; n<nstop; n++) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 176 | { |
| 177 | for (channel = 0; channel < color_channels; channel++) |
| 178 | { |
| 179 | if(row_info->bit_depth == 8) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 180 | if(*dp++ == 0) zero_samples++; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 181 | if(row_info->bit_depth == 16) |
| 182 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 183 | if((*dp | *(dp+1)) == 0) zero_samples++; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 184 | dp+=2; |
| 185 | } |
| 186 | } |
| 187 | if(row_info->color_type > 3) |
| 188 | { |
| 189 | dp++; |
| 190 | if(row_info->bit_depth == 16)dp++; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
Glenn Randers-Pehrson | 38d73af | 1998-03-07 21:30:44 -0600 | [diff] [blame] | 195 | #endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */ |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 196 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 197 | static int wrote_question = 0; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 198 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 199 | #if defined(PNG_NO_STDIO) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 200 | /* START of code to validate stdio-free compilation */ |
| 201 | /* These copies of the default read/write functions come from pngrio.c and */ |
| 202 | /* pngwio.c. They allow "don't include stdio" testing of the library. */ |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 203 | /* This is the function that does the actual reading of data. If you are |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 204 | not reading from a standard C stream, you should create a replacement |
| 205 | read_data function and use it at run time with png_set_read_fn(), rather |
| 206 | than changing the library. */ |
| 207 | #ifndef USE_FAR_KEYWORD |
| 208 | static void |
| 209 | png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) |
| 210 | { |
| 211 | png_size_t check; |
| 212 | |
| 213 | /* fread() returns 0 on error, so it is OK to store this in a png_size_t |
| 214 | * instead of an int, which is what fread() actually returns. |
| 215 | */ |
| 216 | check = (png_size_t)fread(data, (png_size_t)1, length, |
| 217 | (FILE *)png_ptr->io_ptr); |
| 218 | |
| 219 | if (check != length) |
| 220 | { |
| 221 | png_error(png_ptr, "Read Error"); |
| 222 | } |
| 223 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 224 | #else |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 225 | /* this is the model-independent version. Since the standard I/O library |
| 226 | can't handle far buffers in the medium and small models, we have to copy |
| 227 | the data. |
| 228 | */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 229 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 230 | #define NEAR_BUF_SIZE 1024 |
| 231 | #define MIN(a,b) (a <= b ? a : b) |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 232 | |
Glenn Randers-Pehrson | 7cd899c | 1998-03-07 16:17:42 -0600 | [diff] [blame] | 233 | static void |
| 234 | png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 235 | { |
| 236 | int check; |
| 237 | png_byte *n_data; |
| 238 | FILE *io_ptr; |
| 239 | |
| 240 | /* Check if data really is near. If so, use usual code. */ |
| 241 | n_data = (png_byte *)CVT_PTR_NOCHECK(data); |
| 242 | io_ptr = (FILE *)CVT_PTR(png_ptr->io_ptr); |
| 243 | if ((png_bytep)n_data == data) |
| 244 | { |
| 245 | check = fread(n_data, 1, length, io_ptr); |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | png_byte buf[NEAR_BUF_SIZE]; |
| 250 | png_size_t read, remaining, err; |
| 251 | check = 0; |
| 252 | remaining = length; |
| 253 | do |
| 254 | { |
| 255 | read = MIN(NEAR_BUF_SIZE, remaining); |
| 256 | err = fread(buf, (png_size_t)1, read, io_ptr); |
| 257 | png_memcpy(data, buf, read); /* copy far buffer to near buffer */ |
| 258 | if(err != read) |
| 259 | break; |
| 260 | else |
| 261 | check += err; |
| 262 | data += read; |
| 263 | remaining -= read; |
| 264 | } |
| 265 | while (remaining != 0); |
| 266 | } |
| 267 | if (check != length) |
| 268 | { |
| 269 | png_error(png_ptr, "read Error"); |
| 270 | } |
| 271 | } |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 272 | #endif /* USE_FAR_KEYWORD */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 273 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 274 | #if defined(PNG_WRITE_FLUSH_SUPPORTED) |
Glenn Randers-Pehrson | 7cd899c | 1998-03-07 16:17:42 -0600 | [diff] [blame] | 275 | static void |
| 276 | png_default_flush(png_structp png_ptr) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 277 | { |
| 278 | FILE *io_ptr; |
| 279 | io_ptr = (FILE *)CVT_PTR((png_ptr->io_ptr)); |
| 280 | if (io_ptr != NULL) |
| 281 | fflush(io_ptr); |
| 282 | } |
| 283 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 284 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 285 | /* This is the function that does the actual writing of data. If you are |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 286 | not writing to a standard C stream, you should create a replacement |
| 287 | write_data function and use it at run time with png_set_write_fn(), rather |
| 288 | than changing the library. */ |
| 289 | #ifndef USE_FAR_KEYWORD |
| 290 | static void |
| 291 | png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) |
| 292 | { |
| 293 | png_uint_32 check; |
| 294 | |
| 295 | check = fwrite(data, 1, length, (FILE *)(png_ptr->io_ptr)); |
| 296 | if (check != length) |
| 297 | { |
| 298 | png_error(png_ptr, "Write Error"); |
| 299 | } |
| 300 | } |
| 301 | #else |
| 302 | /* this is the model-independent version. Since the standard I/O library |
| 303 | can't handle far buffers in the medium and small models, we have to copy |
| 304 | the data. |
| 305 | */ |
| 306 | |
| 307 | #define NEAR_BUF_SIZE 1024 |
| 308 | #define MIN(a,b) (a <= b ? a : b) |
| 309 | |
| 310 | static void |
| 311 | png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) |
| 312 | { |
| 313 | png_uint_32 check; |
| 314 | png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */ |
| 315 | FILE *io_ptr; |
| 316 | |
| 317 | /* Check if data really is near. If so, use usual code. */ |
| 318 | near_data = (png_byte *)CVT_PTR_NOCHECK(data); |
| 319 | io_ptr = (FILE *)CVT_PTR(png_ptr->io_ptr); |
| 320 | if ((png_bytep)near_data == data) |
| 321 | { |
| 322 | check = fwrite(near_data, 1, length, io_ptr); |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | png_byte buf[NEAR_BUF_SIZE]; |
| 327 | png_size_t written, remaining, err; |
| 328 | check = 0; |
| 329 | remaining = length; |
| 330 | do |
| 331 | { |
| 332 | written = MIN(NEAR_BUF_SIZE, remaining); |
| 333 | png_memcpy(buf, data, written); /* copy far buffer to near buffer */ |
| 334 | err = fwrite(buf, 1, written, io_ptr); |
| 335 | if (err != written) |
| 336 | break; |
| 337 | else |
| 338 | check += err; |
| 339 | data += written; |
| 340 | remaining -= written; |
| 341 | } |
| 342 | while (remaining != 0); |
| 343 | } |
| 344 | if (check != length) |
| 345 | { |
| 346 | png_error(png_ptr, "Write Error"); |
| 347 | } |
| 348 | } |
| 349 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 350 | #endif /* USE_FAR_KEYWORD */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 351 | |
| 352 | /* This function is called when there is a warning, but the library thinks |
| 353 | * it can continue anyway. Replacement functions don't have to do anything |
| 354 | * here if you don't want to. In the default configuration, png_ptr is |
| 355 | * not used, but it is passed in case it may be useful. |
| 356 | */ |
| 357 | static void |
| 358 | png_default_warning(png_structp png_ptr, png_const_charp message) |
| 359 | { |
| 360 | PNG_CONST char *name = "UNKNOWN (ERROR!)"; |
| 361 | if (png_ptr != NULL && png_ptr->error_ptr != NULL) |
| 362 | name = png_ptr->error_ptr; |
| 363 | fprintf(STDERR, "%s: libpng warning: %s\n", name, message); |
| 364 | } |
| 365 | |
| 366 | /* This is the default error handling function. Note that replacements for |
| 367 | * this function MUST NOT RETURN, or the program will likely crash. This |
| 368 | * function is used by default, or if the program supplies NULL for the |
| 369 | * error function pointer in png_set_error_fn(). |
| 370 | */ |
| 371 | static void |
| 372 | png_default_error(png_structp png_ptr, png_const_charp message) |
| 373 | { |
| 374 | png_default_warning(png_ptr, message); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 375 | /* We can return because png_error calls the default handler, which is |
| 376 | * actually OK in this case. */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 377 | } |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 378 | #endif /* PNG_NO_STDIO */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 379 | /* END of code to validate stdio-free compilation */ |
| 380 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 381 | /* START of code to validate memory allocation and deallocation */ |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 382 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 383 | |
| 384 | /* Allocate memory. For reasonable files, size should never exceed |
| 385 | 64K. However, zlib may allocate more then 64K if you don't tell |
| 386 | it not to. See zconf.h and png.h for more information. zlib does |
| 387 | need to allocate exactly 64K, so whatever you call here must |
| 388 | have the ability to do that. |
| 389 | |
| 390 | This piece of code can be compiled to validate max 64K allocations |
| 391 | by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K. */ |
| 392 | typedef struct memory_information { |
| 393 | png_uint_32 size; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 394 | png_voidp pointer; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 395 | struct memory_information FAR *next; |
| 396 | } memory_information; |
| 397 | typedef memory_information FAR *memory_infop; |
| 398 | |
| 399 | static memory_infop pinformation = NULL; |
| 400 | static int current_allocation = 0; |
| 401 | static int maximum_allocation = 0; |
| 402 | |
| 403 | extern PNG_EXPORT(png_voidp,png_debug_malloc) PNGARG((png_structp png_ptr, |
| 404 | png_uint_32 size)); |
| 405 | extern PNG_EXPORT(void,png_debug_free) PNGARG((png_structp png_ptr, |
| 406 | png_voidp ptr)); |
| 407 | |
| 408 | png_voidp |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 409 | png_debug_malloc(png_structp png_ptr, png_uint_32 size) { |
| 410 | |
| 411 | /* png_malloc has already tested for NULL; png_create_struct calls |
| 412 | png_debug_malloc directly, with png_ptr == NULL which is OK */ |
| 413 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 414 | if (size == 0) |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 415 | return (png_voidp)(NULL); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 416 | |
| 417 | /* This calls the library allocator twice, once to get the requested |
| 418 | buffer and once to get a new free list entry. */ |
| 419 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 420 | memory_infop pinfo = png_malloc_default(png_ptr, sizeof *pinfo); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 421 | pinfo->size = size; |
| 422 | current_allocation += size; |
| 423 | if (current_allocation > maximum_allocation) |
| 424 | maximum_allocation = current_allocation; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 425 | pinfo->pointer = png_malloc_default(png_ptr, size); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 426 | pinfo->next = pinformation; |
| 427 | pinformation = pinfo; |
| 428 | /* Make sure the caller isn't assuming zeroed memory. */ |
| 429 | png_memset(pinfo->pointer, 0xdd, pinfo->size); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 430 | return (png_voidp)(pinfo->pointer); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | /* Free a pointer. It is removed from the list at the same time. */ |
| 435 | void |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 436 | png_debug_free(png_structp png_ptr, png_voidp ptr) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 437 | { |
| 438 | if (png_ptr == NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 439 | fprintf(STDERR, "NULL pointer to png_debug_free.\n"); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 440 | if (ptr == 0) { |
| 441 | #if 0 /* This happens all the time. */ |
| 442 | fprintf(STDERR, "WARNING: freeing NULL pointer\n"); |
| 443 | #endif |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | /* Unlink the element from the list. */ |
| 448 | { |
| 449 | memory_infop FAR *ppinfo = &pinformation; |
| 450 | for (;;) { |
| 451 | memory_infop pinfo = *ppinfo; |
| 452 | if (pinfo->pointer == ptr) { |
| 453 | *ppinfo = pinfo->next; |
| 454 | current_allocation -= pinfo->size; |
| 455 | if (current_allocation < 0) |
| 456 | fprintf(STDERR, "Duplicate free of memory\n"); |
| 457 | /* We must free the list element too, but first kill |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 458 | the memory that is to be freed. */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 459 | memset(ptr, 0x55, pinfo->size); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 460 | png_free_default(png_ptr, pinfo); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 461 | break; |
| 462 | } |
| 463 | if (pinfo->next == NULL) { |
| 464 | fprintf(STDERR, "Pointer %x not found\n", ptr); |
| 465 | break; |
| 466 | } |
| 467 | ppinfo = &pinfo->next; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /* Finally free the data. */ |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 472 | png_free_default(png_ptr, ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 473 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 474 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 475 | /* END of code to test memory allocation/deallocation */ |
| 476 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 477 | /* Test one file */ |
Glenn Randers-Pehrson | 7cd899c | 1998-03-07 16:17:42 -0600 | [diff] [blame] | 478 | int |
| 479 | test_one_file(PNG_CONST char *inname, PNG_CONST char *outname) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 480 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 481 | static FILE *fpin, *fpout; /* "static" prevents setjmp corruption */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 482 | png_structp read_ptr, write_ptr; |
| 483 | png_infop read_info_ptr, write_info_ptr, end_info_ptr; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 484 | png_bytep row_buf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 485 | png_uint_32 y; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 486 | png_uint_32 width, height; |
| 487 | int num_pass, pass; |
| 488 | int bit_depth, color_type; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 489 | #ifdef USE_FAR_KEYWORD |
| 490 | jmp_buf jmpbuf; |
| 491 | #endif |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 492 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 493 | char inbuf[256], outbuf[256]; |
| 494 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 495 | row_buf = (png_bytep)NULL; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 496 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 497 | if ((fpin = fopen(inname, "rb")) == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 498 | { |
| 499 | fprintf(STDERR, "Could not find input file %s\n", inname); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 500 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 501 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 502 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 503 | if ((fpout = fopen(outname, "wb")) == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 504 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 505 | fprintf(STDERR, "Could not open output file %s\n", outname); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 506 | fclose(fpin); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 507 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 508 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 509 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 510 | png_debug(0, "Allocating read and write structures\n"); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 511 | #ifdef PNG_USER_MEM_SUPPORTED |
| 512 | read_ptr = png_create_read_struct_2(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, |
| 513 | (png_error_ptr)NULL, (png_error_ptr)NULL, (png_voidp)NULL, |
| 514 | (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free); |
| 515 | #else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 516 | 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] | 517 | (png_error_ptr)NULL, (png_error_ptr)NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 518 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 519 | #if defined(PNG_NO_STDIO) |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 520 | png_set_error_fn(read_ptr, (png_voidp)inname, png_default_error, |
| 521 | png_default_warning); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 522 | #endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 523 | #ifdef PNG_USER_MEM_SUPPORTED |
| 524 | write_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, |
| 525 | (png_error_ptr)NULL, (png_error_ptr)NULL, (png_voidp)NULL, |
| 526 | (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free); |
| 527 | #else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 528 | 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] | 529 | (png_error_ptr)NULL, (png_error_ptr)NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 530 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 531 | #if defined(PNG_NO_STDIO) |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 532 | png_set_error_fn(write_ptr, (png_voidp)inname, png_default_error, |
| 533 | png_default_warning); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 534 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 535 | png_debug(0, "Allocating read_info, write_info and end_info structures\n"); |
| 536 | read_info_ptr = png_create_info_struct(read_ptr); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 537 | write_info_ptr = png_create_info_struct(write_ptr); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 538 | end_info_ptr = png_create_info_struct(read_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 539 | #ifdef PNG_USER_MEM_SUPPORTED |
| 540 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 541 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 542 | png_debug(0, "Setting jmpbuf for read struct\n"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 543 | #ifdef USE_FAR_KEYWORD |
| 544 | if (setjmp(jmpbuf)) |
| 545 | #else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 546 | if (setjmp(read_ptr->jmpbuf)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 547 | #endif |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 548 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 549 | fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 550 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
| 551 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 552 | fclose(fpin); |
| 553 | fclose(fpout); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 554 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 555 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 556 | #ifdef USE_FAR_KEYWORD |
| 557 | png_memcpy(read_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf)); |
| 558 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 559 | |
| 560 | png_debug(0, "Setting jmpbuf for write struct\n"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 561 | #ifdef USE_FAR_KEYWORD |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 562 | if (setjmp(jmpbuf)) |
| 563 | #else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 564 | if (setjmp(write_ptr->jmpbuf)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 565 | #endif |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 566 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 567 | fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 568 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
| 569 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 570 | fclose(fpin); |
| 571 | fclose(fpout); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 572 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 573 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 574 | #ifdef USE_FAR_KEYWORD |
| 575 | png_memcpy(write_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf)); |
| 576 | #endif |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 577 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 578 | png_debug(0, "Initializing input and output streams\n"); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 579 | #if !defined(PNG_NO_STDIO) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 580 | png_init_io(read_ptr, fpin); |
| 581 | png_init_io(write_ptr, fpout); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 582 | #else |
| 583 | png_set_read_fn(read_ptr, (png_voidp)fpin, png_default_read_data); |
| 584 | png_set_write_fn(write_ptr, (png_voidp)fpout, png_default_write_data, |
| 585 | #if defined(PNG_WRITE_FLUSH_SUPPORTED) |
| 586 | png_default_flush); |
| 587 | #else |
| 588 | NULL); |
| 589 | #endif |
| 590 | #endif |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 591 | if(status_dots_requested == 1) |
| 592 | { |
| 593 | png_set_write_status_fn(write_ptr, write_row_callback); |
| 594 | png_set_read_status_fn(read_ptr, read_row_callback); |
| 595 | } |
| 596 | else |
| 597 | { |
| 598 | png_set_write_status_fn(write_ptr, NULL); |
| 599 | png_set_read_status_fn(read_ptr, NULL); |
| 600 | } |
| 601 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 602 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) |
| 603 | { |
| 604 | int i; |
| 605 | for(i=0; i<256; i++) |
| 606 | filters_used[i]=0; |
| 607 | png_set_read_user_transform_fn(read_ptr, count_filters); |
| 608 | } |
| 609 | #endif |
| 610 | #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) |
| 611 | zero_samples=0; |
| 612 | png_set_write_user_transform_fn(write_ptr, count_zero_samples); |
| 613 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 614 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 615 | png_debug(0, "Reading info struct\n"); |
| 616 | png_read_info(read_ptr, read_info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 617 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 618 | png_debug(0, "Transferring info struct\n"); |
| 619 | { |
| 620 | int interlace_type, compression_type, filter_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 621 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 622 | if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth, |
| 623 | &color_type, &interlace_type, &compression_type, &filter_type)) |
| 624 | { |
| 625 | png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth, |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 626 | #if defined(PNG_WRITE_INTERLACING_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 627 | color_type, interlace_type, compression_type, filter_type); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 628 | #else |
| 629 | color_type, PNG_INTERLACE_NONE, compression_type, filter_type); |
| 630 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | #if defined(PNG_READ_bKGD_SUPPORTED) && defined(PNG_WRITE_bKGD_SUPPORTED) |
| 634 | { |
| 635 | png_color_16p background; |
| 636 | |
| 637 | if (png_get_bKGD(read_ptr, read_info_ptr, &background)) |
| 638 | { |
| 639 | png_set_bKGD(write_ptr, write_info_ptr, background); |
| 640 | } |
| 641 | } |
| 642 | #endif |
| 643 | #if defined(PNG_READ_cHRM_SUPPORTED) && defined(PNG_WRITE_cHRM_SUPPORTED) |
| 644 | { |
| 645 | double white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y; |
| 646 | |
| 647 | if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x, |
| 648 | &red_y, &green_x, &green_y, &blue_x, &blue_y)) |
| 649 | { |
| 650 | png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x, |
| 651 | red_y, green_x, green_y, blue_x, blue_y); |
| 652 | } |
| 653 | } |
| 654 | #endif |
| 655 | #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_WRITE_gAMA_SUPPORTED) |
| 656 | { |
| 657 | double gamma; |
| 658 | |
| 659 | if (png_get_gAMA(read_ptr, read_info_ptr, &gamma)) |
| 660 | { |
| 661 | png_set_gAMA(write_ptr, write_info_ptr, gamma); |
| 662 | } |
| 663 | } |
| 664 | #endif |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 665 | #if defined(PNG_READ_sRGB_SUPPORTED) && defined(PNG_WRITE_sRGB_SUPPORTED) |
| 666 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 667 | int intent; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 668 | |
| 669 | if (png_get_sRGB(read_ptr, read_info_ptr, &intent)) |
| 670 | { |
| 671 | png_set_sRGB(write_ptr, write_info_ptr, intent); |
| 672 | } |
| 673 | } |
| 674 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 675 | #if defined(PNG_READ_hIST_SUPPORTED) && defined(PNG_WRITE_hIST_SUPPORTED) |
| 676 | { |
| 677 | png_uint_16p hist; |
| 678 | |
| 679 | if (png_get_hIST(read_ptr, read_info_ptr, &hist)) |
| 680 | { |
| 681 | png_set_hIST(write_ptr, write_info_ptr, hist); |
| 682 | } |
| 683 | } |
| 684 | #endif |
| 685 | #if defined(PNG_READ_oFFs_SUPPORTED) && defined(PNG_WRITE_oFFs_SUPPORTED) |
| 686 | { |
| 687 | png_uint_32 offset_x, offset_y; |
| 688 | int unit_type; |
| 689 | |
| 690 | if (png_get_oFFs(read_ptr, read_info_ptr,&offset_x,&offset_y,&unit_type)) |
| 691 | { |
| 692 | png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type); |
| 693 | } |
| 694 | } |
| 695 | #endif |
| 696 | #if defined(PNG_READ_pCAL_SUPPORTED) && defined(PNG_WRITE_pCAL_SUPPORTED) |
| 697 | { |
| 698 | png_charp purpose, units; |
| 699 | png_charpp params; |
| 700 | png_int_32 X0, X1; |
| 701 | int type, nparams; |
| 702 | |
| 703 | if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type, |
| 704 | &nparams, &units, ¶ms)) |
| 705 | { |
| 706 | png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type, |
| 707 | nparams, units, params); |
| 708 | } |
| 709 | } |
| 710 | #endif |
| 711 | #if defined(PNG_READ_pHYs_SUPPORTED) && defined(PNG_WRITE_pHYs_SUPPORTED) |
| 712 | { |
| 713 | png_uint_32 res_x, res_y; |
| 714 | int unit_type; |
| 715 | |
| 716 | if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type)) |
| 717 | { |
| 718 | png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type); |
| 719 | } |
| 720 | } |
| 721 | #endif |
| 722 | { |
| 723 | png_colorp palette; |
| 724 | int num_palette; |
| 725 | |
| 726 | if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette)) |
| 727 | { |
| 728 | png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette); |
| 729 | } |
| 730 | } |
| 731 | #if defined(PNG_READ_sBIT_SUPPORTED) && defined(PNG_WRITE_sBIT_SUPPORTED) |
| 732 | { |
| 733 | png_color_8p sig_bit; |
| 734 | |
| 735 | if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit)) |
| 736 | { |
| 737 | png_set_sBIT(write_ptr, write_info_ptr, sig_bit); |
| 738 | } |
| 739 | } |
| 740 | #endif |
| 741 | #if (defined(PNG_READ_tEXt_SUPPORTED) && defined(PNG_WRITE_tEXt_SUPPORTED)) || \ |
| 742 | (defined(PNG_READ_zTXt_SUPPORTED) && defined(PNG_WRITE_zTXt_SUPPORTED)) |
| 743 | { |
| 744 | png_textp text_ptr; |
| 745 | int num_text; |
| 746 | |
| 747 | if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0) |
| 748 | { |
| 749 | png_debug1(0, "Handling %d tEXt/zTXt chunks\n", num_text); |
| 750 | png_set_text(write_ptr, write_info_ptr, text_ptr, num_text); |
| 751 | } |
| 752 | } |
| 753 | #endif |
| 754 | #if defined(PNG_READ_tIME_SUPPORTED) && defined(PNG_WRITE_tIME_SUPPORTED) |
| 755 | { |
| 756 | png_timep mod_time; |
| 757 | |
| 758 | if (png_get_tIME(read_ptr, read_info_ptr, &mod_time)) |
| 759 | { |
| 760 | png_set_tIME(write_ptr, write_info_ptr, mod_time); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 761 | #if defined(PNG_TIME_RFC1123_SUPPORTED) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 762 | /* we have to use png_strcpy instead of "=" because the string |
| 763 | pointed to by png_convert_to_rfc1123() gets free'ed before |
| 764 | we use it */ |
| 765 | png_strcpy(tIME_string,png_convert_to_rfc1123(read_ptr, mod_time)); |
| 766 | tIME_chunk_present++; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 767 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
Glenn Randers-Pehrson | c944229 | 1999-01-06 21:50:16 -0600 | [diff] [blame] | 768 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 769 | } |
| 770 | #endif |
| 771 | #if defined(PNG_READ_tRNS_SUPPORTED) && defined(PNG_WRITE_tRNS_SUPPORTED) |
| 772 | { |
| 773 | png_bytep trans; |
| 774 | int num_trans; |
| 775 | png_color_16p trans_values; |
| 776 | |
| 777 | if (png_get_tRNS(read_ptr, read_info_ptr, &trans, &num_trans, |
| 778 | &trans_values)) |
| 779 | { |
| 780 | png_set_tRNS(write_ptr, write_info_ptr, trans, num_trans, |
| 781 | trans_values); |
| 782 | } |
| 783 | } |
| 784 | #endif |
| 785 | |
| 786 | png_debug(0, "\nWriting info struct\n"); |
| 787 | png_write_info(write_ptr, write_info_ptr); |
| 788 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 789 | png_debug(0, "\nAllocating row buffer \n"); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 790 | row_buf = (png_bytep)png_malloc(read_ptr, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 791 | png_get_rowbytes(read_ptr, read_info_ptr)); |
| 792 | if (row_buf == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 793 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 794 | fprintf(STDERR, "No memory to allocate row buffer\n"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 795 | png_destroy_read_struct(&read_ptr, &read_info_ptr, (png_infopp)NULL); |
| 796 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 797 | fclose(fpin); |
| 798 | fclose(fpout); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 799 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 800 | } |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 801 | png_debug(0, "Writing row data\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 802 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 803 | num_pass = png_set_interlace_handling(read_ptr); |
| 804 | png_set_interlace_handling(write_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 805 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 806 | for (pass = 0; pass < num_pass; pass++) |
| 807 | { |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 808 | png_debug1(0, "Writing row data for pass %d\n",pass); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 809 | for (y = 0; y < height; y++) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 810 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 811 | 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] | 812 | png_write_rows(write_ptr, (png_bytepp)&row_buf, 1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 813 | } |
| 814 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 815 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 816 | png_debug(0, "Reading and writing end_info data\n"); |
| 817 | png_read_end(read_ptr, end_info_ptr); |
| 818 | png_write_end(write_ptr, end_info_ptr); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 819 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 820 | #ifdef PNG_EASY_ACCESS_SUPPORTED |
| 821 | if(verbose) |
| 822 | { |
| 823 | png_uint_32 iwidth, iheight; |
| 824 | iwidth = png_get_image_width(write_ptr, write_info_ptr); |
| 825 | iheight = png_get_image_height(write_ptr, write_info_ptr); |
| 826 | fprintf(STDERR, "Image width = %lu, height = %lu\n", |
| 827 | iwidth, iheight); |
| 828 | } |
| 829 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 830 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 831 | png_debug(0, "Destroying data structs\n"); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 832 | png_free(read_ptr, row_buf); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 833 | png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); |
| 834 | png_destroy_write_struct(&write_ptr, &write_info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 835 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 836 | fclose(fpin); |
| 837 | fclose(fpout); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 838 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 839 | png_debug(0, "Opening files for comparison\n"); |
| 840 | if ((fpin = fopen(inname, "rb")) == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 841 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 842 | fprintf(STDERR, "Could not find file %s\n", inname); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 843 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 844 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 845 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 846 | if ((fpout = fopen(outname, "rb")) == NULL) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 847 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 848 | fprintf(STDERR, "Could not find file %s\n", outname); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 849 | fclose(fpin); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 850 | return (1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 851 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 852 | |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 853 | for(;;) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 854 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 855 | png_size_t num_in, num_out; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 856 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 857 | num_in = fread(inbuf, 1, 1, fpin); |
| 858 | num_out = fread(outbuf, 1, 1, fpout); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 859 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 860 | if (num_in != num_out) |
| 861 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 862 | fprintf(STDERR, "Files %s and %s are of a different size\n", |
| 863 | inname, outname); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 864 | if(wrote_question == 0) |
| 865 | { |
| 866 | fprintf(STDERR, |
| 867 | " Was %s written with the same chunk size (8k),",inname); |
| 868 | fprintf(STDERR, |
| 869 | " filtering\n heuristic (libpng default), compression"); |
| 870 | fprintf(STDERR, |
| 871 | " level (zlib default)\n and zlib version (%s)?\n\n", |
| 872 | ZLIB_VERSION); |
| 873 | wrote_question=1; |
| 874 | } |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 875 | fclose(fpin); |
| 876 | fclose(fpout); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 877 | return (0); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 878 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 879 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 880 | if (!num_in) |
| 881 | break; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 882 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 883 | if (png_memcmp(inbuf, outbuf, num_in)) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 884 | { |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 885 | fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 886 | if(wrote_question == 0) |
| 887 | { |
| 888 | fprintf(STDERR, |
| 889 | " Was %s written with the same chunk size (8k),",inname); |
| 890 | fprintf(STDERR, |
| 891 | " filtering\n heuristic (libpng default), compression"); |
| 892 | fprintf(STDERR, |
| 893 | " level (zlib default)\n and zlib version (%s)?\n\n", |
| 894 | ZLIB_VERSION); |
| 895 | wrote_question=1; |
| 896 | } |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 897 | fclose(fpin); |
| 898 | fclose(fpout); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 899 | return (0); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
| 903 | fclose(fpin); |
| 904 | fclose(fpout); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 905 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 906 | return (0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 907 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 908 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 909 | /* input and output filenames */ |
| 910 | #ifdef RISCOS |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 911 | static PNG_CONST char *inname = "pngtest/png"; |
| 912 | static PNG_CONST char *outname = "pngout/png"; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 913 | #else |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 914 | static PNG_CONST char *inname = "pngtest.png"; |
| 915 | static PNG_CONST char *outname = "pngout.png"; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 916 | #endif |
| 917 | |
| 918 | int |
| 919 | main(int argc, char *argv[]) |
| 920 | { |
| 921 | int multiple = 0; |
| 922 | int ierror = 0; |
| 923 | |
| 924 | fprintf(STDERR, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 925 | fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 926 | fprintf(STDERR,"%s",png_get_copyright(NULL)); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 927 | |
| 928 | /* Do some consistency checking on the memory allocation settings, I'm |
| 929 | not sure this matters, but it is nice to know, the first of these |
| 930 | tests should be impossible because of the way the macros are set |
| 931 | in pngconf.h */ |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 932 | #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 933 | fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n"); |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 934 | #endif |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 935 | /* I think the following can happen. */ |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 936 | #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 937 | fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n"); |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 938 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 939 | |
| 940 | if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING)) |
| 941 | { |
| 942 | fprintf(STDERR, |
| 943 | "Warning: versions are different between png.h and png.c\n"); |
| 944 | fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING); |
| 945 | fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver); |
| 946 | ++ierror; |
| 947 | } |
| 948 | |
| 949 | if (argc > 1) |
| 950 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 951 | if (strcmp(argv[1], "-m") == 0) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 952 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 953 | multiple = 1; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 954 | status_dots_requested = 0; |
| 955 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 956 | else if (strcmp(argv[1], "-mv") == 0 || |
| 957 | strcmp(argv[1], "-vm") == 0 ) |
| 958 | { |
| 959 | multiple = 1; |
| 960 | verbose = 1; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 961 | status_dots_requested = 1; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 962 | } |
| 963 | else if (strcmp(argv[1], "-v") == 0) |
| 964 | { |
| 965 | verbose = 1; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 966 | status_dots_requested = 1; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 967 | inname = argv[2]; |
| 968 | } |
| 969 | else |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 970 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 971 | inname = argv[1]; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 972 | status_dots_requested = 0; |
| 973 | } |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 974 | } |
| 975 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 976 | if (!multiple && argc == 3+verbose) |
| 977 | outname = argv[2+verbose]; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 978 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 979 | if ((!multiple && argc > 3+verbose) || (multiple && argc < 2)) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 980 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 981 | fprintf(STDERR, |
| 982 | "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n", |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 983 | argv[0], argv[0]); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 984 | fprintf(STDERR, |
| 985 | " reads/writes one PNG file (without -m) or multiple files (-m)\n"); |
| 986 | fprintf(STDERR, |
| 987 | " with -m %s is used as a temporary file\n", outname); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 988 | exit(1); |
| 989 | } |
| 990 | |
| 991 | if (multiple) |
| 992 | { |
| 993 | int i; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 994 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 995 | int allocation_now = current_allocation; |
| 996 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 997 | for (i=2; i<argc; ++i) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 998 | { |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 999 | int k, kerror; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1000 | fprintf(STDERR, "Testing %s:",argv[i]); |
| 1001 | kerror = test_one_file(argv[i], outname); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1002 | if (kerror == 0) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1003 | { |
Glenn Randers-Pehrson | 38d73af | 1998-03-07 21:30:44 -0600 | [diff] [blame] | 1004 | #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1005 | fprintf(STDERR, " PASS (%lu zero samples)\n",zero_samples); |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1006 | #else |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1007 | fprintf(STDERR, " PASS\n"); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1008 | #endif |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1009 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) |
| 1010 | for (k=0; k<256; k++) |
| 1011 | if(filters_used[k]) |
| 1012 | fprintf(STDERR, " Filter %d was used %lu times\n", |
| 1013 | k,filters_used[k]); |
| 1014 | #endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1015 | #if defined(PNG_TIME_RFC1123_SUPPORTED) |
| 1016 | if(tIME_chunk_present != 0) |
| 1017 | fprintf(STDERR, " tIME = %s\n",tIME_string); |
| 1018 | tIME_chunk_present = 0; |
| 1019 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
| 1020 | } |
| 1021 | else |
| 1022 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1023 | fprintf(STDERR, " FAIL\n"); |
| 1024 | ierror += kerror; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1025 | } |
| 1026 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1027 | if (allocation_now != current_allocation) |
| 1028 | fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n", |
| 1029 | current_allocation-allocation_now); |
| 1030 | if (current_allocation != 0) { |
| 1031 | memory_infop pinfo = pinformation; |
| 1032 | |
| 1033 | fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n", |
| 1034 | current_allocation); |
| 1035 | while (pinfo != NULL) { |
| 1036 | fprintf(STDERR, " %d bytes at %x\n", pinfo->size, pinfo->pointer); |
| 1037 | pinfo = pinfo->next; |
| 1038 | } |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 1039 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1040 | #endif |
| 1041 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1042 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1043 | fprintf(STDERR, "Maximum memory allocation: %d bytes\n", |
| 1044 | maximum_allocation); |
| 1045 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1046 | } |
| 1047 | else |
| 1048 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1049 | int i; |
| 1050 | for (i=0; i<3; ++i) { |
| 1051 | int kerror; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1052 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1053 | int allocation_now = current_allocation; |
| 1054 | #endif |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1055 | if (i == 1) status_dots_requested = 1; |
| 1056 | else if(verbose == 0)status_dots_requested = 0; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1057 | if (i == 0 || verbose == 1 || ierror != 0) |
| 1058 | fprintf(STDERR, "Testing %s:",inname); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1059 | kerror = test_one_file(inname, outname); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1060 | if(kerror == 0) |
| 1061 | { |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1062 | if(verbose == 1 || i == 2) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1063 | { |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1064 | int k; |
Glenn Randers-Pehrson | 38d73af | 1998-03-07 21:30:44 -0600 | [diff] [blame] | 1065 | #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1066 | fprintf(STDERR, " PASS (%lu zero samples)\n",zero_samples); |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1067 | #else |
| 1068 | fprintf(STDERR, " PASS\n"); |
| 1069 | #endif |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1070 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) |
| 1071 | for (k=0; k<256; k++) |
| 1072 | if(filters_used[k]) |
| 1073 | fprintf(STDERR, " Filter %d was used %lu times\n", |
| 1074 | k,filters_used[k]); |
| 1075 | #endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1076 | #if defined(PNG_TIME_RFC1123_SUPPORTED) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1077 | if(tIME_chunk_present != 0) |
| 1078 | fprintf(STDERR, " tIME = %s\n",tIME_string); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1079 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
| 1080 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1081 | } |
| 1082 | else |
| 1083 | { |
| 1084 | if(verbose == 0 && i != 2) |
| 1085 | fprintf(STDERR, "Testing %s:",inname); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1086 | fprintf(STDERR, " FAIL\n"); |
| 1087 | ierror += kerror; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1088 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1089 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1090 | if (allocation_now != current_allocation) |
| 1091 | fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n", |
| 1092 | current_allocation-allocation_now); |
| 1093 | if (current_allocation != 0) { |
| 1094 | memory_infop pinfo = pinformation; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1095 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1096 | fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n", |
| 1097 | current_allocation); |
| 1098 | while (pinfo != NULL) { |
| 1099 | fprintf(STDERR, " %d bytes at %x\n", pinfo->size, pinfo->pointer); |
| 1100 | pinfo = pinfo->next; |
| 1101 | } |
| 1102 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1103 | #endif |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1104 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1105 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1106 | fprintf(STDERR, "Maximum memory allocation: %d bytes\n", |
| 1107 | maximum_allocation); |
| 1108 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | if (ierror == 0) |
| 1112 | fprintf(STDERR, "libpng passes test\n"); |
| 1113 | else |
| 1114 | fprintf(STDERR, "libpng FAILS test\n"); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1115 | return (int)(ierror != 0); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1116 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1117 | |