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