Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2 | /* pngread.c - read a PNG file |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3 | * |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame^] | 4 | * 1.0.1d |
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 | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 8 | * Copyright (c) 1998, Glenn Randers-Pehrson |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame^] | 9 | * May 21, 1998 |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 10 | * |
| 11 | * This file contains routines that an application calls directly to |
| 12 | * read a PNG file or stream. |
| 13 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 14 | |
| 15 | #define PNG_INTERNAL |
| 16 | #include "png.h" |
| 17 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 18 | /* Create a PNG structure for reading, and allocate any memory needed. */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 19 | png_structp |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 20 | png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr, |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 21 | png_error_ptr error_fn, png_error_ptr warn_fn) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 22 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 23 | png_structp png_ptr; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 24 | #ifdef USE_FAR_KEYWORD |
| 25 | jmp_buf jmpbuf; |
| 26 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 27 | png_debug(1, "in png_create_read_struct\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 28 | if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL) |
| 29 | { |
| 30 | return (png_structp)NULL; |
| 31 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 32 | #ifdef USE_FAR_KEYWORD |
| 33 | if (setjmp(jmpbuf)) |
| 34 | #else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 35 | if (setjmp(png_ptr->jmpbuf)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 36 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 37 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 38 | png_free(png_ptr, png_ptr->zbuf); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 39 | png_destroy_struct(png_ptr); |
| 40 | return (png_structp)NULL; |
| 41 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 42 | #ifdef USE_FAR_KEYWORD |
| 43 | png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf)); |
| 44 | #endif |
| 45 | png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 46 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 47 | /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so |
| 48 | * we must recompile any applications that use any older library version. |
| 49 | * For versions after libpng 1.0, we will be compatible, so we need |
| 50 | * only check the first digit. |
| 51 | */ |
| 52 | if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] || |
| 53 | (user_png_ver[0] == '0' && user_png_ver[2] < '9')) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 54 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 55 | png_error(png_ptr, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 56 | "Incompatible libpng version in application and library"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* initialize zbuf - compression buffer */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 60 | png_ptr->zbuf_size = PNG_ZBUF_SIZE; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 61 | png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, |
| 62 | (png_uint_32)png_ptr->zbuf_size); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 63 | png_ptr->zstream.zalloc = png_zalloc; |
| 64 | png_ptr->zstream.zfree = png_zfree; |
| 65 | png_ptr->zstream.opaque = (voidpf)png_ptr; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 66 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 67 | switch (inflateInit(&png_ptr->zstream)) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 68 | { |
| 69 | case Z_OK: /* Do nothing */ break; |
| 70 | case Z_MEM_ERROR: |
| 71 | case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break; |
| 72 | case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break; |
| 73 | default: png_error(png_ptr, "Unknown zlib error"); |
| 74 | } |
| 75 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 76 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 77 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 78 | |
| 79 | png_set_read_fn(png_ptr, NULL, NULL); |
| 80 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 81 | return (png_ptr); |
| 82 | } |
| 83 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 84 | /* Initialize PNG structure for reading, and allocate any memory needed. |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 85 | This interface is deprecated in favour of the png_create_read_struct(), |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 86 | and it will eventually disappear. */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 87 | void |
| 88 | png_read_init(png_structp png_ptr) |
| 89 | { |
| 90 | jmp_buf tmp_jmp; /* to save current jump buffer */ |
| 91 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 92 | png_debug(1, "in png_read_init\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 93 | /* save jump buffer and error functions */ |
| 94 | png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf)); |
| 95 | |
| 96 | /* reset all variables to 0 */ |
| 97 | png_memset(png_ptr, 0, sizeof (png_struct)); |
| 98 | |
| 99 | /* restore jump buffer */ |
| 100 | png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf)); |
| 101 | |
| 102 | /* initialize zbuf - compression buffer */ |
| 103 | png_ptr->zbuf_size = PNG_ZBUF_SIZE; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 104 | png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, |
| 105 | (png_uint_32)png_ptr->zbuf_size); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 106 | png_ptr->zstream.zalloc = png_zalloc; |
| 107 | png_ptr->zstream.zfree = png_zfree; |
| 108 | png_ptr->zstream.opaque = (voidpf)png_ptr; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 109 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 110 | switch (inflateInit(&png_ptr->zstream)) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 111 | { |
| 112 | case Z_OK: /* Do nothing */ break; |
| 113 | case Z_MEM_ERROR: |
| 114 | case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break; |
| 115 | case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break; |
| 116 | default: png_error(png_ptr, "Unknown zlib error"); |
| 117 | } |
| 118 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 119 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 120 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 121 | |
| 122 | png_set_read_fn(png_ptr, NULL, NULL); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 125 | /* Read the information before the actual image data. This has been |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 126 | * changed in v0.90 to allow reading a file that already has the magic |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 127 | * bytes read from the stream. You can tell libpng how many bytes have |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 128 | * been read from the beginning of the stream (up to the maximum of 8) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 129 | * via png_set_sig_bytes(), and we will only check the remaining bytes |
| 130 | * here. The application can then have access to the signature bytes we |
| 131 | * read if it is determined that this isn't a valid PNG file. |
| 132 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 133 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 134 | png_read_info(png_structp png_ptr, png_infop info_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 135 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 136 | png_debug(1, "in png_read_info\n"); |
| 137 | /* save jump buffer and error functions */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 138 | /* If we haven't checked all of the PNG signature bytes, do so now. */ |
| 139 | if (png_ptr->sig_bytes < 8) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 140 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 141 | png_size_t num_checked = png_ptr->sig_bytes, |
| 142 | num_to_check = 8 - num_checked; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 143 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 144 | png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 145 | png_ptr->sig_bytes = 8; |
| 146 | |
| 147 | if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) |
| 148 | { |
| 149 | if (num_checked < 4 && |
| 150 | png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) |
| 151 | png_error(png_ptr, "Not a PNG file"); |
| 152 | else |
| 153 | png_error(png_ptr, "PNG file corrupted by ASCII conversion"); |
| 154 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 155 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 156 | |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 157 | for(;;) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 158 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 159 | png_byte chunk_length[4]; |
| 160 | png_uint_32 length; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 161 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 162 | png_read_data(png_ptr, chunk_length, 4); |
| 163 | length = png_get_uint_32(chunk_length); |
| 164 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 165 | png_reset_crc(png_ptr); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 166 | png_crc_read(png_ptr, png_ptr->chunk_name, 4); |
| 167 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 168 | png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name); |
| 169 | |
| 170 | /* This should be a binary subdivision search or a hash for |
| 171 | * matching the chunk name rather than a linear search. |
| 172 | */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 173 | if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) |
| 174 | png_handle_IHDR(png_ptr, info_ptr, length); |
| 175 | else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) |
| 176 | png_handle_PLTE(png_ptr, info_ptr, length); |
| 177 | else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) |
| 178 | png_handle_IEND(png_ptr, info_ptr, length); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 179 | else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 180 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 181 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 182 | png_error(png_ptr, "Missing IHDR before IDAT"); |
| 183 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 184 | !(png_ptr->mode & PNG_HAVE_PLTE)) |
| 185 | png_error(png_ptr, "Missing PLTE before IDAT"); |
| 186 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 187 | png_ptr->idat_size = length; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 188 | png_ptr->mode |= PNG_HAVE_IDAT; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 189 | break; |
| 190 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 191 | #if defined(PNG_READ_bKGD_SUPPORTED) |
| 192 | else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) |
| 193 | png_handle_bKGD(png_ptr, info_ptr, length); |
| 194 | #endif |
| 195 | #if defined(PNG_READ_cHRM_SUPPORTED) |
| 196 | else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) |
| 197 | png_handle_cHRM(png_ptr, info_ptr, length); |
| 198 | #endif |
| 199 | #if defined(PNG_READ_gAMA_SUPPORTED) |
| 200 | else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) |
| 201 | png_handle_gAMA(png_ptr, info_ptr, length); |
| 202 | #endif |
| 203 | #if defined(PNG_READ_hIST_SUPPORTED) |
| 204 | else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) |
| 205 | png_handle_hIST(png_ptr, info_ptr, length); |
| 206 | #endif |
| 207 | #if defined(PNG_READ_oFFs_SUPPORTED) |
| 208 | else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) |
| 209 | png_handle_oFFs(png_ptr, info_ptr, length); |
| 210 | #endif |
| 211 | #if defined(PNG_READ_pCAL_SUPPORTED) |
| 212 | else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4)) |
| 213 | png_handle_pCAL(png_ptr, info_ptr, length); |
| 214 | #endif |
| 215 | #if defined(PNG_READ_pHYs_SUPPORTED) |
| 216 | else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) |
| 217 | png_handle_pHYs(png_ptr, info_ptr, length); |
| 218 | #endif |
| 219 | #if defined(PNG_READ_sBIT_SUPPORTED) |
| 220 | else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) |
| 221 | png_handle_sBIT(png_ptr, info_ptr, length); |
| 222 | #endif |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 223 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 224 | else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4)) |
| 225 | png_handle_sRGB(png_ptr, info_ptr, length); |
| 226 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 227 | #if defined(PNG_READ_tEXt_SUPPORTED) |
| 228 | else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) |
| 229 | png_handle_tEXt(png_ptr, info_ptr, length); |
| 230 | #endif |
| 231 | #if defined(PNG_READ_tIME_SUPPORTED) |
| 232 | else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) |
| 233 | png_handle_tIME(png_ptr, info_ptr, length); |
| 234 | #endif |
| 235 | #if defined(PNG_READ_tRNS_SUPPORTED) |
| 236 | else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) |
| 237 | png_handle_tRNS(png_ptr, info_ptr, length); |
| 238 | #endif |
| 239 | #if defined(PNG_READ_zTXt_SUPPORTED) |
| 240 | else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) |
| 241 | png_handle_zTXt(png_ptr, info_ptr, length); |
| 242 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 243 | else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 244 | png_handle_unknown(png_ptr, info_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 248 | /* optional call to update the users info_ptr structure */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 249 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 250 | png_read_update_info(png_structp png_ptr, png_infop info_ptr) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 251 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 252 | png_debug(1, "in png_read_update_info\n"); |
| 253 | /* save jump buffer and error functions */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 254 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 255 | png_read_start_row(png_ptr); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 256 | png_read_transform_info(png_ptr, info_ptr); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 257 | } |
| 258 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 259 | /* Initialize palette, background, etc, after transformations |
| 260 | * are set, but before any reading takes place. This allows |
| 261 | * the user to obtail a gamma corrected palette, for example. |
| 262 | * If the user doesn't call this, we will do it ourselves. |
| 263 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 264 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 265 | png_start_read_image(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 266 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 267 | png_debug(1, "in png_start_read_image\n"); |
| 268 | /* save jump buffer and error functions */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 269 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 270 | png_read_start_row(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 274 | png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 275 | { |
| 276 | int ret; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 277 | png_debug2(1, "in png_read_row (row %d, pass %d)\n", |
| 278 | png_ptr->row_number, png_ptr->pass); |
| 279 | /* save jump buffer and error functions */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 280 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 281 | png_read_start_row(png_ptr); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 282 | if (png_ptr->row_number == 0 && png_ptr->pass == 0) |
| 283 | { |
| 284 | /* check for transforms that have been set but were defined out */ |
| 285 | #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) |
| 286 | if (png_ptr->transformations & PNG_INVERT_MONO) |
| 287 | png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined."); |
| 288 | #endif |
| 289 | #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) |
| 290 | if (png_ptr->transformations & PNG_FILLER) |
| 291 | png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined."); |
| 292 | #endif |
| 293 | #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 294 | if (png_ptr->transformations & PNG_PACKSWAP) |
| 295 | png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined."); |
| 296 | #endif |
| 297 | #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) |
| 298 | if (png_ptr->transformations & PNG_PACK) |
| 299 | png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined."); |
| 300 | #endif |
| 301 | #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) |
| 302 | if (png_ptr->transformations & PNG_SHIFT) |
| 303 | png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined."); |
| 304 | #endif |
| 305 | #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) |
| 306 | if (png_ptr->transformations & PNG_BGR) |
| 307 | png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined."); |
| 308 | #endif |
| 309 | #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) |
| 310 | if (png_ptr->transformations & PNG_SWAP_BYTES) |
| 311 | png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined."); |
| 312 | #endif |
| 313 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 314 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 315 | #if defined(PNG_READ_INTERLACING_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 316 | /* if interlaced and we do not need a new row, combine row and return */ |
| 317 | if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) |
| 318 | { |
| 319 | switch (png_ptr->pass) |
| 320 | { |
| 321 | case 0: |
| 322 | if (png_ptr->row_number & 7) |
| 323 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 324 | if (dsp_row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 325 | png_combine_row(png_ptr, dsp_row, |
| 326 | png_pass_dsp_mask[png_ptr->pass]); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 327 | png_read_finish_row(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 328 | return; |
| 329 | } |
| 330 | break; |
| 331 | case 1: |
| 332 | if ((png_ptr->row_number & 7) || png_ptr->width < 5) |
| 333 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 334 | if (dsp_row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 335 | png_combine_row(png_ptr, dsp_row, |
| 336 | png_pass_dsp_mask[png_ptr->pass]); |
| 337 | png_read_finish_row(png_ptr); |
| 338 | return; |
| 339 | } |
| 340 | break; |
| 341 | case 2: |
| 342 | if ((png_ptr->row_number & 7) != 4) |
| 343 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 344 | if (dsp_row != NULL && (png_ptr->row_number & 4)) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 345 | png_combine_row(png_ptr, dsp_row, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 346 | png_pass_dsp_mask[png_ptr->pass]); |
| 347 | png_read_finish_row(png_ptr); |
| 348 | return; |
| 349 | } |
| 350 | break; |
| 351 | case 3: |
| 352 | if ((png_ptr->row_number & 3) || png_ptr->width < 3) |
| 353 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 354 | if (dsp_row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 355 | png_combine_row(png_ptr, dsp_row, |
| 356 | png_pass_dsp_mask[png_ptr->pass]); |
| 357 | png_read_finish_row(png_ptr); |
| 358 | return; |
| 359 | } |
| 360 | break; |
| 361 | case 4: |
| 362 | if ((png_ptr->row_number & 3) != 2) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 363 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 364 | if (dsp_row != NULL && (png_ptr->row_number & 2)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 365 | png_combine_row(png_ptr, dsp_row, |
| 366 | png_pass_dsp_mask[png_ptr->pass]); |
| 367 | png_read_finish_row(png_ptr); |
| 368 | return; |
| 369 | } |
| 370 | break; |
| 371 | case 5: |
| 372 | if ((png_ptr->row_number & 1) || png_ptr->width < 2) |
| 373 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 374 | if (dsp_row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 375 | png_combine_row(png_ptr, dsp_row, |
| 376 | png_pass_dsp_mask[png_ptr->pass]); |
| 377 | png_read_finish_row(png_ptr); |
| 378 | return; |
| 379 | } |
| 380 | break; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 381 | case 6: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 382 | if (!(png_ptr->row_number & 1)) |
| 383 | { |
| 384 | png_read_finish_row(png_ptr); |
| 385 | return; |
| 386 | } |
| 387 | break; |
| 388 | } |
| 389 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 390 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 391 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 392 | if (!(png_ptr->mode & PNG_HAVE_IDAT)) |
| 393 | png_error(png_ptr, "Invalid attempt to read row data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 394 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 395 | png_ptr->zstream.next_out = png_ptr->row_buf; |
| 396 | png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 397 | do |
| 398 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 399 | if (!(png_ptr->zstream.avail_in)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 400 | { |
| 401 | while (!png_ptr->idat_size) |
| 402 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 403 | png_byte chunk_length[4]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 404 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 405 | png_crc_finish(png_ptr, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 406 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 407 | png_read_data(png_ptr, chunk_length, 4); |
| 408 | png_ptr->idat_size = png_get_uint_32(chunk_length); |
| 409 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 410 | png_reset_crc(png_ptr); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 411 | png_crc_read(png_ptr, png_ptr->chunk_name, 4); |
| 412 | if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) |
| 413 | png_error(png_ptr, "Not enough image data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 414 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 415 | png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; |
| 416 | png_ptr->zstream.next_in = png_ptr->zbuf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 417 | if (png_ptr->zbuf_size > png_ptr->idat_size) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 418 | png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 419 | png_crc_read(png_ptr, png_ptr->zbuf, |
| 420 | (png_size_t)png_ptr->zstream.avail_in); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 421 | png_ptr->idat_size -= png_ptr->zstream.avail_in; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 422 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 423 | ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 424 | if (ret == Z_STREAM_END) |
| 425 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 426 | if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in || |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 427 | png_ptr->idat_size) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 428 | png_error(png_ptr, "Extra compressed data"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 429 | png_ptr->mode |= PNG_AFTER_IDAT; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 430 | png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 431 | break; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 432 | } |
| 433 | if (ret != Z_OK) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 434 | png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 435 | "Decompression error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 436 | |
| 437 | } while (png_ptr->zstream.avail_out); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 438 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 439 | png_ptr->row_info.color_type = png_ptr->color_type; |
| 440 | png_ptr->row_info.width = png_ptr->iwidth; |
| 441 | png_ptr->row_info.channels = png_ptr->channels; |
| 442 | png_ptr->row_info.bit_depth = png_ptr->bit_depth; |
| 443 | png_ptr->row_info.pixel_depth = png_ptr->pixel_depth; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 444 | { |
| 445 | png_ptr->row_info.rowbytes = ((png_ptr->row_info.width * |
| 446 | (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3); |
| 447 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 448 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 449 | png_read_filter_row(png_ptr, &(png_ptr->row_info), |
| 450 | png_ptr->row_buf + 1, png_ptr->prev_row + 1, |
| 451 | (int)(png_ptr->row_buf[0])); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 452 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 453 | png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 454 | png_ptr->rowbytes + 1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 455 | |
| 456 | if (png_ptr->transformations) |
| 457 | png_do_read_transformations(png_ptr); |
| 458 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 459 | #if defined(PNG_READ_INTERLACING_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 460 | /* blow up interlaced rows to full size */ |
| 461 | if (png_ptr->interlaced && |
| 462 | (png_ptr->transformations & PNG_INTERLACE)) |
| 463 | { |
| 464 | if (png_ptr->pass < 6) |
| 465 | png_do_read_interlace(&(png_ptr->row_info), |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 466 | png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 467 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 468 | if (dsp_row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 469 | png_combine_row(png_ptr, dsp_row, |
| 470 | png_pass_dsp_mask[png_ptr->pass]); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 471 | if (row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 472 | png_combine_row(png_ptr, row, |
| 473 | png_pass_mask[png_ptr->pass]); |
| 474 | } |
| 475 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 476 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 477 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 478 | if (row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 479 | png_combine_row(png_ptr, row, 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 480 | if (dsp_row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 481 | png_combine_row(png_ptr, dsp_row, 0xff); |
| 482 | } |
| 483 | png_read_finish_row(png_ptr); |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 484 | |
| 485 | if (png_ptr->read_row_fn != NULL) |
| 486 | (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 487 | } |
| 488 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 489 | /* Read one or more rows of image data. If the image is interlaced, |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 490 | * and png_set_interlace_handling() has been called, the rows need to |
| 491 | * contain the contents of the rows from the previous pass. If the |
| 492 | * image has alpha or transparency, and png_handle_alpha() has been |
| 493 | * called, the rows contents must be initialized to the contents of the |
| 494 | * screen. |
| 495 | * |
| 496 | * "row" holds the actual image, and pixels are placed in it |
| 497 | * as they arrive. If the image is displayed after each pass, it will |
| 498 | * appear to "sparkle" in. "display_row" can be used to display a |
| 499 | * "chunky" progressive image, with finer detail added as it becomes |
| 500 | * available. If you do not want this "chunky" display, you may pass |
| 501 | * NULL for display_row. If you do not want the sparkle display, and |
| 502 | * you have not called png_handle_alpha(), you may pass NULL for rows. |
| 503 | * If you have called png_handle_alpha(), and the image has either an |
| 504 | * alpha channel or a transparency chunk, you must provide a buffer for |
| 505 | * rows. In this case, you do not have to provide a display_row buffer |
| 506 | * also, but you may. If the image is not interlaced, or if you have |
| 507 | * not called png_set_interlace_handling(), the display_row buffer will |
| 508 | * be ignored, so pass NULL to it. |
| 509 | */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 510 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 511 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 512 | png_read_rows(png_structp png_ptr, png_bytepp row, |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 513 | png_bytepp display_row, png_uint_32 num_rows) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 514 | { |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 515 | png_uint_32 i; |
| 516 | png_bytepp rp; |
| 517 | png_bytepp dp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 518 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 519 | png_debug(1, "in png_read_rows\n"); |
| 520 | /* save jump buffer and error functions */ |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 521 | rp = row; |
| 522 | dp = display_row; |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame^] | 523 | if (rp != NULL && dp != NULL) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 524 | for (i = 0; i < num_rows; i++) |
| 525 | { |
| 526 | png_bytep rptr = *rp++; |
| 527 | png_bytep dptr = *dp++; |
| 528 | |
| 529 | png_read_row(png_ptr, rptr, dptr); |
| 530 | } |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame^] | 531 | else if(rp != NULL) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 532 | for (i = 0; i < num_rows; i++) |
| 533 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame^] | 534 | png_bytep rptr = *rp; |
| 535 | png_read_row(png_ptr, rptr, NULL); |
| 536 | rp++; |
| 537 | } |
| 538 | else if(dp != NULL) |
| 539 | for (i = 0; i < num_rows; i++) |
| 540 | { |
| 541 | png_bytep dptr = *dp; |
| 542 | png_read_row(png_ptr, NULL, dptr); |
| 543 | dp++; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 544 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 545 | } |
| 546 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 547 | /* Read the entire image. If the image has an alpha channel or a tRNS |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 548 | * chunk, and you have called png_handle_alpha(), you will need to |
| 549 | * initialize the image to the current image that PNG will be overlaying. |
| 550 | * We set the num_rows again here, in case it was incorrectly set in |
| 551 | * png_read_start_row() by a call to png_read_update_info() or |
| 552 | * png_start_read_image() if png_set_interlace_handling() wasn't called |
| 553 | * prior to either of these functions like it should have been. You can |
| 554 | * only call this function once. If you desire to have an image for |
| 555 | * each pass of a interlaced image, use png_read_rows() instead. |
| 556 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 557 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 558 | png_read_image(png_structp png_ptr, png_bytepp image) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 559 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 560 | png_uint_32 i,image_height; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 561 | int pass, j; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 562 | png_bytepp rp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 563 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 564 | png_debug(1, "in png_read_image\n"); |
| 565 | /* save jump buffer and error functions */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 566 | pass = png_set_interlace_handling(png_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 567 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 568 | image_height=png_ptr->height; |
| 569 | png_ptr->num_rows = image_height; /* Make sure this is set correctly */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 570 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 571 | for (j = 0; j < pass; j++) |
| 572 | { |
| 573 | rp = image; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 574 | for (i = 0; i < image_height; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 575 | { |
| 576 | png_read_row(png_ptr, *rp, NULL); |
| 577 | rp++; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 582 | /* Read the end of the PNG file. Will not read past the end of the |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 583 | * file, will verify the end is accurate, and will read any comments |
| 584 | * or time information at the end of the file, if info is not NULL. |
| 585 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 586 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 587 | png_read_end(png_structp png_ptr, png_infop info_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 588 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 589 | png_byte chunk_length[4]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 590 | png_uint_32 length; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 591 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 592 | png_debug(1, "in png_read_end\n"); |
| 593 | /* save jump buffer and error functions */ |
| 594 | png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 595 | |
| 596 | do |
| 597 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 598 | png_read_data(png_ptr, chunk_length, 4); |
| 599 | length = png_get_uint_32(chunk_length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 600 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 601 | png_reset_crc(png_ptr); |
| 602 | png_crc_read(png_ptr, png_ptr->chunk_name, 4); |
| 603 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 604 | png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name); |
| 605 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 606 | if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) |
| 607 | png_handle_IHDR(png_ptr, info_ptr, length); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 608 | else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 609 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 610 | /* Zero length IDATs are legal after the last IDAT has been |
| 611 | * read, but not after other chunks have been read. |
| 612 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 613 | if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT) |
| 614 | png_error(png_ptr, "Too many IDAT's found"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 615 | else |
| 616 | png_crc_finish(png_ptr, 0); |
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 | else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) |
| 619 | png_handle_PLTE(png_ptr, info_ptr, length); |
| 620 | else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) |
| 621 | png_handle_IEND(png_ptr, info_ptr, length); |
| 622 | #if defined(PNG_READ_bKGD_SUPPORTED) |
| 623 | else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) |
| 624 | png_handle_bKGD(png_ptr, info_ptr, length); |
| 625 | #endif |
| 626 | #if defined(PNG_READ_cHRM_SUPPORTED) |
| 627 | else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) |
| 628 | png_handle_cHRM(png_ptr, info_ptr, length); |
| 629 | #endif |
| 630 | #if defined(PNG_READ_gAMA_SUPPORTED) |
| 631 | else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) |
| 632 | png_handle_gAMA(png_ptr, info_ptr, length); |
| 633 | #endif |
| 634 | #if defined(PNG_READ_hIST_SUPPORTED) |
| 635 | else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) |
| 636 | png_handle_hIST(png_ptr, info_ptr, length); |
| 637 | #endif |
| 638 | #if defined(PNG_READ_oFFs_SUPPORTED) |
| 639 | else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) |
| 640 | png_handle_oFFs(png_ptr, info_ptr, length); |
| 641 | #endif |
| 642 | #if defined(PNG_READ_pCAL_SUPPORTED) |
| 643 | else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4)) |
| 644 | png_handle_pCAL(png_ptr, info_ptr, length); |
| 645 | #endif |
| 646 | #if defined(PNG_READ_pHYs_SUPPORTED) |
| 647 | else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) |
| 648 | png_handle_pHYs(png_ptr, info_ptr, length); |
| 649 | #endif |
| 650 | #if defined(PNG_READ_sBIT_SUPPORTED) |
| 651 | else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) |
| 652 | png_handle_sBIT(png_ptr, info_ptr, length); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 653 | #endif |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 654 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 655 | else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4)) |
| 656 | png_handle_sRGB(png_ptr, info_ptr, length); |
| 657 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 658 | #if defined(PNG_READ_tEXt_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 659 | else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) |
| 660 | png_handle_tEXt(png_ptr, info_ptr, length); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 661 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 662 | #if defined(PNG_READ_tIME_SUPPORTED) |
| 663 | else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) |
| 664 | png_handle_tIME(png_ptr, info_ptr, length); |
| 665 | #endif |
| 666 | #if defined(PNG_READ_tRNS_SUPPORTED) |
| 667 | else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) |
| 668 | png_handle_tRNS(png_ptr, info_ptr, length); |
| 669 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 670 | #if defined(PNG_READ_zTXt_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 671 | else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) |
| 672 | png_handle_zTXt(png_ptr, info_ptr, length); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 673 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 674 | else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 675 | png_handle_unknown(png_ptr, info_ptr, length); |
| 676 | } while (!(png_ptr->mode & PNG_HAVE_IEND)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /* free all memory used by the read */ |
| 680 | void |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 681 | png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 682 | png_infopp end_info_ptr_ptr) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 683 | { |
| 684 | png_structp png_ptr = NULL; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 685 | png_infop info_ptr = NULL, end_info_ptr = NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 686 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 687 | png_debug(1, "in png_destroy_read_struct\n"); |
| 688 | /* save jump buffer and error functions */ |
| 689 | if (png_ptr_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 690 | png_ptr = *png_ptr_ptr; |
| 691 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 692 | if (info_ptr_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 693 | info_ptr = *info_ptr_ptr; |
| 694 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 695 | if (end_info_ptr_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 696 | end_info_ptr = *end_info_ptr_ptr; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 697 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 698 | png_read_destroy(png_ptr, info_ptr, end_info_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 699 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 700 | if (info_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 701 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 702 | #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED) |
| 703 | png_free(png_ptr, info_ptr->text); |
| 704 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 705 | png_destroy_struct((png_voidp)info_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 706 | *info_ptr_ptr = (png_infop)NULL; |
| 707 | } |
| 708 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 709 | if (end_info_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 710 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 711 | #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 712 | png_free(png_ptr, end_info_ptr->text); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 713 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 714 | png_destroy_struct((png_voidp)end_info_ptr); |
| 715 | *end_info_ptr_ptr = (png_infop)NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 716 | } |
| 717 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 718 | if (png_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 719 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 720 | png_destroy_struct((png_voidp)png_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 721 | *png_ptr_ptr = (png_structp)NULL; |
| 722 | } |
| 723 | } |
| 724 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 725 | /* free all memory used by the read (old method) */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 726 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 727 | png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 728 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 729 | jmp_buf tmp_jmp; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 730 | png_error_ptr error_fn; |
| 731 | png_error_ptr warning_fn; |
| 732 | png_voidp error_ptr; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 733 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 734 | png_debug(1, "in png_read_destroy\n"); |
| 735 | /* save jump buffer and error functions */ |
| 736 | if (info_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 737 | png_info_destroy(png_ptr, info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 738 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 739 | if (end_info_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 740 | png_info_destroy(png_ptr, end_info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 741 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 742 | png_free(png_ptr, png_ptr->zbuf); |
| 743 | png_free(png_ptr, png_ptr->row_buf); |
| 744 | png_free(png_ptr, png_ptr->prev_row); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 745 | #if defined(PNG_READ_DITHER_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 746 | png_free(png_ptr, png_ptr->palette_lookup); |
| 747 | png_free(png_ptr, png_ptr->dither_index); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 748 | #endif |
| 749 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 750 | png_free(png_ptr, png_ptr->gamma_table); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 751 | #endif |
| 752 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 753 | png_free(png_ptr, png_ptr->gamma_from_1); |
| 754 | png_free(png_ptr, png_ptr->gamma_to_1); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 755 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 756 | if (png_ptr->flags & PNG_FLAG_FREE_PALETTE) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 757 | png_zfree(png_ptr, png_ptr->palette); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 758 | if (png_ptr->flags & PNG_FLAG_FREE_TRANS) |
| 759 | png_free(png_ptr, png_ptr->trans); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 760 | #if defined(PNG_READ_hIST_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 761 | if (png_ptr->flags & PNG_FLAG_FREE_HIST) |
| 762 | png_free(png_ptr, png_ptr->hist); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 763 | #endif |
| 764 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 765 | if (png_ptr->gamma_16_table != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 766 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 767 | int i; |
| 768 | int istop = (1 << (8 - png_ptr->gamma_shift)); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 769 | for (i = 0; i < istop; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 770 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 771 | png_free(png_ptr, png_ptr->gamma_16_table[i]); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 772 | } |
| 773 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 774 | #endif |
| 775 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 776 | png_free(png_ptr, png_ptr->gamma_16_table); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 777 | if (png_ptr->gamma_16_from_1 != NULL) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 778 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 779 | int i; |
| 780 | int istop = (1 << (8 - png_ptr->gamma_shift)); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 781 | for (i = 0; i < istop; i++) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 782 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 783 | png_free(png_ptr, png_ptr->gamma_16_from_1[i]); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 784 | } |
| 785 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 786 | png_free(png_ptr, png_ptr->gamma_16_from_1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 787 | if (png_ptr->gamma_16_to_1 != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 788 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 789 | int i; |
| 790 | int istop = (1 << (8 - png_ptr->gamma_shift)); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 791 | for (i = 0; i < istop; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 792 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 793 | png_free(png_ptr, png_ptr->gamma_16_to_1[i]); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 794 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 795 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 796 | png_free(png_ptr, png_ptr->gamma_16_to_1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 797 | #endif |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 798 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 799 | inflateEnd(&png_ptr->zstream); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 800 | #ifdef PNG_PROGRESSIVE_READ_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 801 | png_free(png_ptr, png_ptr->save_buffer); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 802 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 803 | |
| 804 | /* Save the important info out of the png_struct, in case it is |
| 805 | * being used again. |
| 806 | */ |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 807 | png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 808 | |
| 809 | error_fn = png_ptr->error_fn; |
| 810 | warning_fn = png_ptr->warning_fn; |
| 811 | error_ptr = png_ptr->error_ptr; |
| 812 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 813 | png_memset(png_ptr, 0, sizeof (png_struct)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 814 | |
| 815 | png_ptr->error_fn = error_fn; |
| 816 | png_ptr->warning_fn = warning_fn; |
| 817 | png_ptr->error_ptr = error_ptr; |
| 818 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 819 | png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 820 | } |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 821 | |
| 822 | void |
| 823 | png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn) |
| 824 | { |
| 825 | png_ptr->read_row_fn = read_row_fn; |
| 826 | } |