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 | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame^] | 4 | * Last changed in libpng 1.4.0 [February 14, 2009] |
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 |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame^] | 6 | * Copyright (c) 1998-2009 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 7 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 8 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
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 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 14 | #include "png.h" |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 15 | #if defined(PNG_READ_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 16 | #include "pngpriv.h" |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 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. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 19 | png_structp PNGAPI |
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 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 23 | |
| 24 | #ifdef PNG_USER_MEM_SUPPORTED |
| 25 | return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn, |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 26 | warn_fn, NULL, NULL, NULL)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /* Alternate create PNG structure for reading, and allocate any memory needed. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 30 | png_structp PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 31 | png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr, |
| 32 | png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, |
| 33 | png_malloc_ptr malloc_fn, png_free_ptr free_fn) |
| 34 | { |
| 35 | #endif /* PNG_USER_MEM_SUPPORTED */ |
| 36 | |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame^] | 37 | #ifdef PNG_SETJMP_SUPPORTED |
| 38 | volatile |
| 39 | #endif |
| 40 | png_structp png_ptr; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 41 | |
| 42 | #ifdef PNG_SETJMP_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 43 | #ifdef USE_FAR_KEYWORD |
| 44 | jmp_buf jmpbuf; |
| 45 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 46 | #endif |
| 47 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 48 | int i; |
| 49 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 50 | png_debug(1, "in png_create_read_struct"); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 51 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 52 | png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG, |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 53 | malloc_fn, mem_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 54 | #else |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 55 | png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 56 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 57 | if (png_ptr == NULL) |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 58 | return (NULL); |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 59 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 60 | /* added at libpng-1.2.6 */ |
| 61 | #ifdef PNG_SET_USER_LIMITS_SUPPORTED |
| 62 | png_ptr->user_width_max=PNG_USER_WIDTH_MAX; |
| 63 | png_ptr->user_height_max=PNG_USER_HEIGHT_MAX; |
| 64 | #endif |
| 65 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 66 | #ifdef PNG_SETJMP_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 67 | #ifdef USE_FAR_KEYWORD |
| 68 | if (setjmp(jmpbuf)) |
| 69 | #else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 70 | if (setjmp(png_ptr->jmpbuf)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 71 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 72 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 73 | png_free(png_ptr, png_ptr->zbuf); |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 74 | png_ptr->zbuf = NULL; |
Glenn Randers-Pehrson | 25228ab | 2002-03-31 07:33:55 -0600 | [diff] [blame] | 75 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | e647462 | 2006-03-04 16:50:47 -0600 | [diff] [blame] | 76 | png_destroy_struct_2((png_voidp)png_ptr, |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 77 | (png_free_ptr)free_fn, (png_voidp)mem_ptr); |
Glenn Randers-Pehrson | 25228ab | 2002-03-31 07:33:55 -0600 | [diff] [blame] | 78 | #else |
| 79 | png_destroy_struct((png_voidp)png_ptr); |
| 80 | #endif |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 81 | return (NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 82 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 83 | #ifdef USE_FAR_KEYWORD |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 84 | png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf)); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 85 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 86 | #endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 87 | |
| 88 | #ifdef PNG_USER_MEM_SUPPORTED |
| 89 | png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 90 | #endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 91 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 92 | png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 93 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 94 | if (user_png_ver) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 95 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 96 | i = 0; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 97 | do |
| 98 | { |
| 99 | if (user_png_ver[i] != png_libpng_ver[i]) |
| 100 | png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; |
| 101 | } while (png_libpng_ver[i++]); |
| 102 | } |
| 103 | else |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 104 | png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 105 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 106 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 107 | if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) |
Glenn Randers-Pehrson | 98c9d73 | 2000-05-03 21:06:11 -0500 | [diff] [blame] | 108 | { |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 109 | /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so |
| 110 | * we must recompile any applications that use any older library version. |
| 111 | * For versions after libpng 1.0, we will be compatible, so we need |
| 112 | * only check the first digit. |
| 113 | */ |
| 114 | if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] || |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 115 | (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) || |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 116 | (user_png_ver[0] == '0' && user_png_ver[2] < '9')) |
| 117 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 118 | #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 119 | char msg[80]; |
| 120 | if (user_png_ver) |
| 121 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 122 | png_snprintf(msg, 80, |
| 123 | "Application was compiled with png.h from libpng-%.20s", |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 124 | user_png_ver); |
| 125 | png_warning(png_ptr, msg); |
| 126 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 127 | png_snprintf(msg, 80, |
| 128 | "Application is running with png.c from libpng-%.20s", |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 129 | png_libpng_ver); |
| 130 | png_warning(png_ptr, msg); |
| 131 | #endif |
| 132 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 133 | png_ptr->flags = 0; |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 134 | #endif |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 135 | png_error(png_ptr, |
| 136 | "Incompatible libpng version in application and library"); |
| 137 | } |
Glenn Randers-Pehrson | 98c9d73 | 2000-05-03 21:06:11 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 140 | /* initialize zbuf - compression buffer */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 141 | png_ptr->zbuf_size = PNG_ZBUF_SIZE; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 142 | png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, |
| 143 | png_ptr->zbuf_size); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 144 | png_ptr->zstream.zalloc = png_zalloc; |
| 145 | png_ptr->zstream.zfree = png_zfree; |
| 146 | png_ptr->zstream.opaque = (voidpf)png_ptr; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 147 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 148 | switch (inflateInit(&png_ptr->zstream)) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 149 | { |
| 150 | case Z_OK: /* Do nothing */ break; |
| 151 | case Z_MEM_ERROR: |
| 152 | case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break; |
| 153 | case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break; |
| 154 | default: png_error(png_ptr, "Unknown zlib error"); |
| 155 | } |
| 156 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 157 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 158 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 159 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 160 | png_set_read_fn(png_ptr, NULL, NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 161 | |
Glenn Randers-Pehrson | 03008a0 | 2002-04-27 10:11:25 -0500 | [diff] [blame] | 162 | #ifdef PNG_SETJMP_SUPPORTED |
| 163 | /* Applications that neglect to set up their own setjmp() and then encounter |
| 164 | a png_error() will longjmp here. Since the jmpbuf is then meaningless we |
| 165 | abort instead of returning. */ |
| 166 | #ifdef USE_FAR_KEYWORD |
| 167 | if (setjmp(jmpbuf)) |
| 168 | PNG_ABORT(); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 169 | png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf)); |
Glenn Randers-Pehrson | 03008a0 | 2002-04-27 10:11:25 -0500 | [diff] [blame] | 170 | #else |
| 171 | if (setjmp(png_ptr->jmpbuf)) |
| 172 | PNG_ABORT(); |
| 173 | #endif |
| 174 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 175 | return (png_ptr); |
| 176 | } |
| 177 | |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 178 | |
| 179 | void PNGAPI |
| 180 | png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver, |
| 181 | png_size_t png_struct_size) |
| 182 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 183 | #ifdef PNG_SETJMP_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 184 | jmp_buf tmp_jmp; /* to save current jump buffer */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 185 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 186 | |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 187 | int i = 0; |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 188 | |
| 189 | png_structp png_ptr=*ptr_ptr; |
| 190 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 191 | if (png_ptr == NULL) return; |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame] | 192 | |
Glenn Randers-Pehrson | 98c9d73 | 2000-05-03 21:06:11 -0500 | [diff] [blame] | 193 | do |
| 194 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 195 | if (user_png_ver[i] != png_libpng_ver[i]) |
Glenn Randers-Pehrson | 98c9d73 | 2000-05-03 21:06:11 -0500 | [diff] [blame] | 196 | { |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 197 | #ifdef PNG_LEGACY_SUPPORTED |
| 198 | png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; |
| 199 | #else |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 200 | png_ptr->warning_fn = NULL; |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 201 | png_warning(png_ptr, |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 202 | "Application uses deprecated png_read_init() and should be recompiled"); |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 203 | break; |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 204 | #endif |
Glenn Randers-Pehrson | 98c9d73 | 2000-05-03 21:06:11 -0500 | [diff] [blame] | 205 | } |
| 206 | } while (png_libpng_ver[i++]); |
Glenn Randers-Pehrson | 98c9d73 | 2000-05-03 21:06:11 -0500 | [diff] [blame] | 207 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 208 | png_debug(1, "in png_read_init_3"); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 209 | |
| 210 | #ifdef PNG_SETJMP_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 211 | /* save jump buffer and error functions */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 212 | png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 213 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 214 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 215 | if (png_sizeof(png_struct) > png_struct_size) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 216 | { |
| 217 | png_destroy_struct(png_ptr); |
| 218 | *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); |
| 219 | png_ptr = *ptr_ptr; |
| 220 | } |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 221 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 222 | /* reset all variables to 0 */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 223 | png_memset(png_ptr, 0, png_sizeof(png_struct)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 224 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 225 | #ifdef PNG_SETJMP_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 226 | /* restore jump buffer */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 227 | png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 228 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 229 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 230 | /* added at libpng-1.2.6 */ |
| 231 | #ifdef PNG_SET_USER_LIMITS_SUPPORTED |
| 232 | png_ptr->user_width_max=PNG_USER_WIDTH_MAX; |
| 233 | png_ptr->user_height_max=PNG_USER_HEIGHT_MAX; |
| 234 | #endif |
| 235 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 236 | /* initialize zbuf - compression buffer */ |
| 237 | png_ptr->zbuf_size = PNG_ZBUF_SIZE; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 238 | png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, |
| 239 | png_ptr->zbuf_size); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 240 | png_ptr->zstream.zalloc = png_zalloc; |
| 241 | png_ptr->zstream.zfree = png_zfree; |
| 242 | png_ptr->zstream.opaque = (voidpf)png_ptr; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 243 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 244 | switch (inflateInit(&png_ptr->zstream)) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 245 | { |
| 246 | case Z_OK: /* Do nothing */ break; |
| 247 | case Z_MEM_ERROR: |
| 248 | case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break; |
| 249 | case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break; |
| 250 | default: png_error(png_ptr, "Unknown zlib error"); |
| 251 | } |
| 252 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 253 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 254 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 255 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 256 | png_set_read_fn(png_ptr, NULL, NULL); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 257 | } |
| 258 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 259 | #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 260 | /* Read the information before the actual image data. This has been |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 261 | * 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] | 262 | * 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] | 263 | * 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] | 264 | * via png_set_sig_bytes(), and we will only check the remaining bytes |
| 265 | * here. The application can then have access to the signature bytes we |
| 266 | * read if it is determined that this isn't a valid PNG file. |
| 267 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 268 | void PNGAPI |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 269 | png_read_info(png_structp png_ptr, png_infop info_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 270 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 271 | if (png_ptr == NULL || info_ptr == NULL) return; |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 272 | png_debug(1, "in png_read_info"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 273 | /* If we haven't checked all of the PNG signature bytes, do so now. */ |
| 274 | if (png_ptr->sig_bytes < 8) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 275 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 276 | png_size_t num_checked = png_ptr->sig_bytes, |
| 277 | num_to_check = 8 - num_checked; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 278 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 279 | #ifdef PNG_IO_STATE_SUPPORTED |
| 280 | png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE; |
| 281 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 282 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 283 | 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] | 284 | png_ptr->sig_bytes = 8; |
| 285 | |
| 286 | if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) |
| 287 | { |
| 288 | if (num_checked < 4 && |
| 289 | png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) |
| 290 | png_error(png_ptr, "Not a PNG file"); |
| 291 | else |
| 292 | png_error(png_ptr, "PNG file corrupted by ASCII conversion"); |
| 293 | } |
Glenn Randers-Pehrson | 408b421 | 2000-12-18 09:33:57 -0600 | [diff] [blame] | 294 | if (num_checked < 3) |
| 295 | png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 296 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 297 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 298 | for (;;) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 299 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 300 | #ifdef PNG_USE_LOCAL_ARRAYS |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 301 | PNG_CONST PNG_IHDR; |
| 302 | PNG_CONST PNG_IDAT; |
| 303 | PNG_CONST PNG_IEND; |
| 304 | PNG_CONST PNG_PLTE; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 305 | #if defined(PNG_READ_bKGD_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 306 | PNG_CONST PNG_bKGD; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 307 | #endif |
| 308 | #if defined(PNG_READ_cHRM_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 309 | PNG_CONST PNG_cHRM; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 310 | #endif |
| 311 | #if defined(PNG_READ_gAMA_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 312 | PNG_CONST PNG_gAMA; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 313 | #endif |
| 314 | #if defined(PNG_READ_hIST_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 315 | PNG_CONST PNG_hIST; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 316 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 317 | #if defined(PNG_READ_iCCP_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 318 | PNG_CONST PNG_iCCP; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 319 | #endif |
| 320 | #if defined(PNG_READ_iTXt_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 321 | PNG_CONST PNG_iTXt; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 322 | #endif |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 323 | #if defined(PNG_READ_oFFs_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 324 | PNG_CONST PNG_oFFs; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 325 | #endif |
| 326 | #if defined(PNG_READ_pCAL_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 327 | PNG_CONST PNG_pCAL; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 328 | #endif |
| 329 | #if defined(PNG_READ_pHYs_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 330 | PNG_CONST PNG_pHYs; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 331 | #endif |
| 332 | #if defined(PNG_READ_sBIT_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 333 | PNG_CONST PNG_sBIT; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 334 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 335 | #if defined(PNG_READ_sCAL_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 336 | PNG_CONST PNG_sCAL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 337 | #endif |
| 338 | #if defined(PNG_READ_sPLT_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 339 | PNG_CONST PNG_sPLT; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 340 | #endif |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 341 | #if defined(PNG_READ_sRGB_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 342 | PNG_CONST PNG_sRGB; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 343 | #endif |
| 344 | #if defined(PNG_READ_tEXt_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 345 | PNG_CONST PNG_tEXt; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 346 | #endif |
| 347 | #if defined(PNG_READ_tIME_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 348 | PNG_CONST PNG_tIME; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 349 | #endif |
| 350 | #if defined(PNG_READ_tRNS_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 351 | PNG_CONST PNG_tRNS; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 352 | #endif |
| 353 | #if defined(PNG_READ_zTXt_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 354 | PNG_CONST PNG_zTXt; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 355 | #endif |
Glenn Randers-Pehrson | 16e1166 | 2004-11-01 14:13:40 -0600 | [diff] [blame] | 356 | #endif /* PNG_USE_LOCAL_ARRAYS */ |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 357 | png_uint_32 length = png_read_chunk_header(png_ptr); |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 358 | PNG_CONST png_bytep chunk_name = png_ptr->chunk_name; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 359 | |
| 360 | /* This should be a binary subdivision search or a hash for |
| 361 | * matching the chunk name rather than a linear search. |
| 362 | */ |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 363 | if (!png_memcmp(chunk_name, png_IDAT, 4)) |
| 364 | if (png_ptr->mode & PNG_AFTER_IDAT) |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 365 | png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; |
| 366 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 367 | if (!png_memcmp(chunk_name, png_IHDR, 4)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 368 | png_handle_IHDR(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 369 | else if (!png_memcmp(chunk_name, png_IEND, 4)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 370 | png_handle_IEND(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 371 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 372 | else if (png_handle_as_unknown(png_ptr, chunk_name)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 373 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 374 | if (!png_memcmp(chunk_name, png_IDAT, 4)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 375 | png_ptr->mode |= PNG_HAVE_IDAT; |
| 376 | png_handle_unknown(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 377 | if (!png_memcmp(chunk_name, png_PLTE, 4)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 378 | png_ptr->mode |= PNG_HAVE_PLTE; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 379 | else if (!png_memcmp(chunk_name, png_IDAT, 4)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 380 | { |
| 381 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 382 | png_error(png_ptr, "Missing IHDR before IDAT"); |
| 383 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 384 | !(png_ptr->mode & PNG_HAVE_PLTE)) |
| 385 | png_error(png_ptr, "Missing PLTE before IDAT"); |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | #endif |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 390 | else if (!png_memcmp(chunk_name, png_PLTE, 4)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 391 | png_handle_PLTE(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 392 | else if (!png_memcmp(chunk_name, png_IDAT, 4)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 393 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 394 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 395 | png_error(png_ptr, "Missing IHDR before IDAT"); |
| 396 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 397 | !(png_ptr->mode & PNG_HAVE_PLTE)) |
| 398 | png_error(png_ptr, "Missing PLTE before IDAT"); |
| 399 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 400 | png_ptr->idat_size = length; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 401 | png_ptr->mode |= PNG_HAVE_IDAT; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 402 | break; |
| 403 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 404 | #if defined(PNG_READ_bKGD_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 405 | else if (!png_memcmp(chunk_name, png_bKGD, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 406 | png_handle_bKGD(png_ptr, info_ptr, length); |
| 407 | #endif |
| 408 | #if defined(PNG_READ_cHRM_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 409 | else if (!png_memcmp(chunk_name, png_cHRM, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 410 | png_handle_cHRM(png_ptr, info_ptr, length); |
| 411 | #endif |
| 412 | #if defined(PNG_READ_gAMA_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 413 | else if (!png_memcmp(chunk_name, png_gAMA, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 414 | png_handle_gAMA(png_ptr, info_ptr, length); |
| 415 | #endif |
| 416 | #if defined(PNG_READ_hIST_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 417 | else if (!png_memcmp(chunk_name, png_hIST, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 418 | png_handle_hIST(png_ptr, info_ptr, length); |
| 419 | #endif |
| 420 | #if defined(PNG_READ_oFFs_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 421 | else if (!png_memcmp(chunk_name, png_oFFs, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 422 | png_handle_oFFs(png_ptr, info_ptr, length); |
| 423 | #endif |
| 424 | #if defined(PNG_READ_pCAL_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 425 | else if (!png_memcmp(chunk_name, png_pCAL, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 426 | png_handle_pCAL(png_ptr, info_ptr, length); |
| 427 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 428 | #if defined(PNG_READ_sCAL_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 429 | else if (!png_memcmp(chunk_name, png_sCAL, 4)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 430 | png_handle_sCAL(png_ptr, info_ptr, length); |
| 431 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 432 | #if defined(PNG_READ_pHYs_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 433 | else if (!png_memcmp(chunk_name, png_pHYs, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 434 | png_handle_pHYs(png_ptr, info_ptr, length); |
| 435 | #endif |
| 436 | #if defined(PNG_READ_sBIT_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 437 | else if (!png_memcmp(chunk_name, png_sBIT, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 438 | png_handle_sBIT(png_ptr, info_ptr, length); |
| 439 | #endif |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 440 | #if defined(PNG_READ_sRGB_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 441 | else if (!png_memcmp(chunk_name, png_sRGB, 4)) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 442 | png_handle_sRGB(png_ptr, info_ptr, length); |
| 443 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 444 | #if defined(PNG_READ_iCCP_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 445 | else if (!png_memcmp(chunk_name, png_iCCP, 4)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 446 | png_handle_iCCP(png_ptr, info_ptr, length); |
| 447 | #endif |
| 448 | #if defined(PNG_READ_sPLT_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 449 | else if (!png_memcmp(chunk_name, png_sPLT, 4)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 450 | png_handle_sPLT(png_ptr, info_ptr, length); |
| 451 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 452 | #if defined(PNG_READ_tEXt_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 453 | else if (!png_memcmp(chunk_name, png_tEXt, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 454 | png_handle_tEXt(png_ptr, info_ptr, length); |
| 455 | #endif |
| 456 | #if defined(PNG_READ_tIME_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 457 | else if (!png_memcmp(chunk_name, png_tIME, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 458 | png_handle_tIME(png_ptr, info_ptr, length); |
| 459 | #endif |
| 460 | #if defined(PNG_READ_tRNS_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 461 | else if (!png_memcmp(chunk_name, png_tRNS, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 462 | png_handle_tRNS(png_ptr, info_ptr, length); |
| 463 | #endif |
| 464 | #if defined(PNG_READ_zTXt_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 465 | else if (!png_memcmp(chunk_name, png_zTXt, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 466 | png_handle_zTXt(png_ptr, info_ptr, length); |
| 467 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 468 | #if defined(PNG_READ_iTXt_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 469 | else if (!png_memcmp(chunk_name, png_iTXt, 4)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 470 | png_handle_iTXt(png_ptr, info_ptr, length); |
| 471 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 472 | else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 473 | png_handle_unknown(png_ptr, info_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 474 | } |
| 475 | } |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 476 | #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 477 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 478 | /* optional call to update the users info_ptr structure */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 479 | void PNGAPI |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 480 | png_read_update_info(png_structp png_ptr, png_infop info_ptr) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 481 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 482 | png_debug(1, "in png_read_update_info"); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 483 | if (png_ptr == NULL) return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 484 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 485 | png_read_start_row(png_ptr); |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 486 | else |
| 487 | png_warning(png_ptr, |
| 488 | "Ignoring extra png_read_update_info() call; row buffer not reallocated"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 489 | png_read_transform_info(png_ptr, info_ptr); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 490 | } |
| 491 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 492 | #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 493 | /* Initialize palette, background, etc, after transformations |
| 494 | * are set, but before any reading takes place. This allows |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 495 | * the user to obtain a gamma-corrected palette, for example. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 496 | * If the user doesn't call this, we will do it ourselves. |
| 497 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 498 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 499 | png_start_read_image(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 500 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 501 | png_debug(1, "in png_start_read_image"); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 502 | if (png_ptr == NULL) return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 503 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 504 | png_read_start_row(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 505 | } |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 506 | #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 507 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 508 | #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 509 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 510 | 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] | 511 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 512 | #ifdef PNG_USE_LOCAL_ARRAYS |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 513 | PNG_CONST PNG_IDAT; |
| 514 | PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, |
| 515 | 0xff}; |
| 516 | PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 517 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 518 | int ret; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 519 | if (png_ptr == NULL) return; |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 520 | png_debug2(1, "in png_read_row (row %lu, pass %d)", |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 521 | (unsigned long) png_ptr->row_number, png_ptr->pass); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 522 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 523 | png_read_start_row(png_ptr); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 524 | if (png_ptr->row_number == 0 && png_ptr->pass == 0) |
| 525 | { |
| 526 | /* check for transforms that have been set but were defined out */ |
| 527 | #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) |
| 528 | if (png_ptr->transformations & PNG_INVERT_MONO) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 529 | png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 530 | #endif |
| 531 | #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) |
| 532 | if (png_ptr->transformations & PNG_FILLER) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 533 | png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 534 | #endif |
| 535 | #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 536 | if (png_ptr->transformations & PNG_PACKSWAP) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 537 | png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 538 | #endif |
| 539 | #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) |
| 540 | if (png_ptr->transformations & PNG_PACK) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 541 | png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 542 | #endif |
| 543 | #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) |
| 544 | if (png_ptr->transformations & PNG_SHIFT) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 545 | png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 546 | #endif |
| 547 | #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) |
| 548 | if (png_ptr->transformations & PNG_BGR) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 549 | png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 550 | #endif |
| 551 | #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) |
| 552 | if (png_ptr->transformations & PNG_SWAP_BYTES) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 553 | png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 554 | #endif |
| 555 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 556 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 557 | #if defined(PNG_READ_INTERLACING_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 558 | /* if interlaced and we do not need a new row, combine row and return */ |
| 559 | if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) |
| 560 | { |
| 561 | switch (png_ptr->pass) |
| 562 | { |
| 563 | case 0: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 564 | if (png_ptr->row_number & 0x07) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 565 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 566 | if (dsp_row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 567 | png_combine_row(png_ptr, dsp_row, |
| 568 | png_pass_dsp_mask[png_ptr->pass]); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 569 | png_read_finish_row(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 570 | return; |
| 571 | } |
| 572 | break; |
| 573 | case 1: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 574 | if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 575 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 576 | if (dsp_row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 577 | png_combine_row(png_ptr, dsp_row, |
| 578 | png_pass_dsp_mask[png_ptr->pass]); |
| 579 | png_read_finish_row(png_ptr); |
| 580 | return; |
| 581 | } |
| 582 | break; |
| 583 | case 2: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 584 | if ((png_ptr->row_number & 0x07) != 4) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 585 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 586 | if (dsp_row != NULL && (png_ptr->row_number & 4)) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 587 | png_combine_row(png_ptr, dsp_row, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 588 | png_pass_dsp_mask[png_ptr->pass]); |
| 589 | png_read_finish_row(png_ptr); |
| 590 | return; |
| 591 | } |
| 592 | break; |
| 593 | case 3: |
| 594 | if ((png_ptr->row_number & 3) || png_ptr->width < 3) |
| 595 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 596 | if (dsp_row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 597 | png_combine_row(png_ptr, dsp_row, |
| 598 | png_pass_dsp_mask[png_ptr->pass]); |
| 599 | png_read_finish_row(png_ptr); |
| 600 | return; |
| 601 | } |
| 602 | break; |
| 603 | case 4: |
| 604 | if ((png_ptr->row_number & 3) != 2) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 605 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 606 | if (dsp_row != NULL && (png_ptr->row_number & 2)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 607 | png_combine_row(png_ptr, dsp_row, |
| 608 | png_pass_dsp_mask[png_ptr->pass]); |
| 609 | png_read_finish_row(png_ptr); |
| 610 | return; |
| 611 | } |
| 612 | break; |
| 613 | case 5: |
| 614 | if ((png_ptr->row_number & 1) || png_ptr->width < 2) |
| 615 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 616 | if (dsp_row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 617 | png_combine_row(png_ptr, dsp_row, |
| 618 | png_pass_dsp_mask[png_ptr->pass]); |
| 619 | png_read_finish_row(png_ptr); |
| 620 | return; |
| 621 | } |
| 622 | break; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 623 | case 6: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 624 | if (!(png_ptr->row_number & 1)) |
| 625 | { |
| 626 | png_read_finish_row(png_ptr); |
| 627 | return; |
| 628 | } |
| 629 | break; |
| 630 | } |
| 631 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 632 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 633 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 634 | if (!(png_ptr->mode & PNG_HAVE_IDAT)) |
| 635 | png_error(png_ptr, "Invalid attempt to read row data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 636 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 637 | png_ptr->zstream.next_out = png_ptr->row_buf; |
| 638 | png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 639 | do |
| 640 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 641 | if (!(png_ptr->zstream.avail_in)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 642 | { |
| 643 | while (!png_ptr->idat_size) |
| 644 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 645 | png_crc_finish(png_ptr, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 646 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 647 | png_ptr->idat_size = png_read_chunk_header(png_ptr); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 648 | if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) |
| 649 | png_error(png_ptr, "Not enough image data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 650 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 651 | png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; |
| 652 | png_ptr->zstream.next_in = png_ptr->zbuf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 653 | if (png_ptr->zbuf_size > png_ptr->idat_size) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 654 | png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 655 | png_crc_read(png_ptr, png_ptr->zbuf, |
| 656 | (png_size_t)png_ptr->zstream.avail_in); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 657 | png_ptr->idat_size -= png_ptr->zstream.avail_in; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 658 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 659 | ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 660 | if (ret == Z_STREAM_END) |
| 661 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 662 | if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in || |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 663 | png_ptr->idat_size) |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 664 | png_benign_error(png_ptr, "Extra compressed data"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 665 | png_ptr->mode |= PNG_AFTER_IDAT; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 666 | png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 667 | break; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 668 | } |
| 669 | if (ret != Z_OK) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 670 | png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 671 | "Decompression error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 672 | |
| 673 | } while (png_ptr->zstream.avail_out); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 674 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 675 | png_ptr->row_info.color_type = png_ptr->color_type; |
| 676 | png_ptr->row_info.width = png_ptr->iwidth; |
| 677 | png_ptr->row_info.channels = png_ptr->channels; |
| 678 | png_ptr->row_info.bit_depth = png_ptr->bit_depth; |
| 679 | png_ptr->row_info.pixel_depth = png_ptr->pixel_depth; |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 680 | png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, |
| 681 | png_ptr->row_info.width); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 682 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 683 | if (png_ptr->row_buf[0]) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 684 | png_read_filter_row(png_ptr, &(png_ptr->row_info), |
| 685 | png_ptr->row_buf + 1, png_ptr->prev_row + 1, |
| 686 | (int)(png_ptr->row_buf[0])); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 687 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 688 | png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1); |
Glenn Randers-Pehrson | e647462 | 2006-03-04 16:50:47 -0600 | [diff] [blame] | 689 | |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 690 | #if defined(PNG_MNG_FEATURES_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 691 | if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 692 | (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) |
| 693 | { |
| 694 | /* Intrapixel differencing */ |
| 695 | png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 696 | } |
| 697 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 698 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 699 | |
Glenn Randers-Pehrson | 73b029f | 2004-11-26 17:28:09 -0600 | [diff] [blame] | 700 | if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 701 | png_do_read_transformations(png_ptr); |
| 702 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 703 | #if defined(PNG_READ_INTERLACING_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 704 | /* blow up interlaced rows to full size */ |
| 705 | if (png_ptr->interlaced && |
| 706 | (png_ptr->transformations & PNG_INTERLACE)) |
| 707 | { |
| 708 | if (png_ptr->pass < 6) |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 709 | /* old interface (pre-1.0.9): |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 710 | png_do_read_interlace(&(png_ptr->row_info), |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 711 | png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations); |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 712 | */ |
| 713 | png_do_read_interlace(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 714 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 715 | if (dsp_row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 716 | png_combine_row(png_ptr, dsp_row, |
| 717 | png_pass_dsp_mask[png_ptr->pass]); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 718 | if (row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 719 | png_combine_row(png_ptr, row, |
| 720 | png_pass_mask[png_ptr->pass]); |
| 721 | } |
| 722 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 723 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 724 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 725 | if (row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 726 | png_combine_row(png_ptr, row, 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 727 | if (dsp_row != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 728 | png_combine_row(png_ptr, dsp_row, 0xff); |
| 729 | } |
| 730 | png_read_finish_row(png_ptr); |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 731 | |
| 732 | if (png_ptr->read_row_fn != NULL) |
| 733 | (*(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] | 734 | } |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 735 | #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 736 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 737 | #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 738 | /* 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] | 739 | * and png_set_interlace_handling() has been called, the rows need to |
| 740 | * 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] | 741 | * image has alpha or transparency, and png_handle_alpha()[*] has been |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 742 | * called, the rows contents must be initialized to the contents of the |
| 743 | * screen. |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 744 | * |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 745 | * "row" holds the actual image, and pixels are placed in it |
| 746 | * as they arrive. If the image is displayed after each pass, it will |
| 747 | * appear to "sparkle" in. "display_row" can be used to display a |
| 748 | * "chunky" progressive image, with finer detail added as it becomes |
| 749 | * available. If you do not want this "chunky" display, you may pass |
| 750 | * NULL for display_row. If you do not want the sparkle display, and |
| 751 | * you have not called png_handle_alpha(), you may pass NULL for rows. |
| 752 | * If you have called png_handle_alpha(), and the image has either an |
| 753 | * alpha channel or a transparency chunk, you must provide a buffer for |
| 754 | * rows. In this case, you do not have to provide a display_row buffer |
| 755 | * also, but you may. If the image is not interlaced, or if you have |
| 756 | * not called png_set_interlace_handling(), the display_row buffer will |
| 757 | * be ignored, so pass NULL to it. |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 758 | * |
Glenn Randers-Pehrson | c3d51c1 | 2006-03-02 07:23:18 -0600 | [diff] [blame] | 759 | * [*] png_handle_alpha() does not exist yet, as of this version of libpng |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 760 | */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 761 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 762 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 763 | png_read_rows(png_structp png_ptr, png_bytepp row, |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 764 | png_bytepp display_row, png_uint_32 num_rows) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 765 | { |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 766 | png_uint_32 i; |
| 767 | png_bytepp rp; |
| 768 | png_bytepp dp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 769 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 770 | png_debug(1, "in png_read_rows"); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 771 | if (png_ptr == NULL) return; |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 772 | rp = row; |
| 773 | dp = display_row; |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 774 | if (rp != NULL && dp != NULL) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 775 | for (i = 0; i < num_rows; i++) |
| 776 | { |
| 777 | png_bytep rptr = *rp++; |
| 778 | png_bytep dptr = *dp++; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 779 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 780 | png_read_row(png_ptr, rptr, dptr); |
| 781 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 782 | else if (rp != NULL) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 783 | for (i = 0; i < num_rows; i++) |
| 784 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 785 | png_bytep rptr = *rp; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 786 | png_read_row(png_ptr, rptr, NULL); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 787 | rp++; |
| 788 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 789 | else if (dp != NULL) |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 790 | for (i = 0; i < num_rows; i++) |
| 791 | { |
| 792 | png_bytep dptr = *dp; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 793 | png_read_row(png_ptr, NULL, dptr); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 794 | dp++; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 795 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 796 | } |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 797 | #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 798 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 799 | #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 800 | /* 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] | 801 | * 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] | 802 | * initialize the image to the current image that PNG will be overlaying. |
| 803 | * We set the num_rows again here, in case it was incorrectly set in |
| 804 | * png_read_start_row() by a call to png_read_update_info() or |
| 805 | * png_start_read_image() if png_set_interlace_handling() wasn't called |
| 806 | * prior to either of these functions like it should have been. You can |
| 807 | * only call this function once. If you desire to have an image for |
| 808 | * each pass of a interlaced image, use png_read_rows() instead. |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 809 | * |
Glenn Randers-Pehrson | c3d51c1 | 2006-03-02 07:23:18 -0600 | [diff] [blame] | 810 | * [*] png_handle_alpha() does not exist yet, as of this version of libpng |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 811 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 812 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 813 | png_read_image(png_structp png_ptr, png_bytepp image) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 814 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 815 | png_uint_32 i, image_height; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 816 | int pass, j; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 817 | png_bytepp rp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 818 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 819 | png_debug(1, "in png_read_image"); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 820 | if (png_ptr == NULL) return; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 821 | |
| 822 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 823 | pass = png_set_interlace_handling(png_ptr); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 824 | #else |
| 825 | if (png_ptr->interlaced) |
| 826 | png_error(png_ptr, |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 827 | "Cannot read interlaced image -- interlace handler disabled"); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 828 | pass = 1; |
| 829 | #endif |
| 830 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 831 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 832 | image_height=png_ptr->height; |
| 833 | png_ptr->num_rows = image_height; /* Make sure this is set correctly */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 834 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 835 | for (j = 0; j < pass; j++) |
| 836 | { |
| 837 | rp = image; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 838 | for (i = 0; i < image_height; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 839 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 840 | png_read_row(png_ptr, *rp, NULL); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 841 | rp++; |
| 842 | } |
| 843 | } |
| 844 | } |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 845 | #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 846 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 847 | #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 848 | /* 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] | 849 | * file, will verify the end is accurate, and will read any comments |
| 850 | * or time information at the end of the file, if info is not NULL. |
| 851 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 852 | void PNGAPI |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 853 | png_read_end(png_structp png_ptr, png_infop info_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 854 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 855 | png_debug(1, "in png_read_end"); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 856 | if (png_ptr == NULL) return; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 857 | 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] | 858 | |
| 859 | do |
| 860 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 861 | #ifdef PNG_USE_LOCAL_ARRAYS |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 862 | PNG_CONST PNG_IHDR; |
| 863 | PNG_CONST PNG_IDAT; |
| 864 | PNG_CONST PNG_IEND; |
| 865 | PNG_CONST PNG_PLTE; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 866 | #if defined(PNG_READ_bKGD_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 867 | PNG_CONST PNG_bKGD; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 868 | #endif |
| 869 | #if defined(PNG_READ_cHRM_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 870 | PNG_CONST PNG_cHRM; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 871 | #endif |
| 872 | #if defined(PNG_READ_gAMA_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 873 | PNG_CONST PNG_gAMA; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 874 | #endif |
| 875 | #if defined(PNG_READ_hIST_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 876 | PNG_CONST PNG_hIST; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 877 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 878 | #if defined(PNG_READ_iCCP_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 879 | PNG_CONST PNG_iCCP; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 880 | #endif |
| 881 | #if defined(PNG_READ_iTXt_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 882 | PNG_CONST PNG_iTXt; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 883 | #endif |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 884 | #if defined(PNG_READ_oFFs_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 885 | PNG_CONST PNG_oFFs; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 886 | #endif |
| 887 | #if defined(PNG_READ_pCAL_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 888 | PNG_CONST PNG_pCAL; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 889 | #endif |
| 890 | #if defined(PNG_READ_pHYs_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 891 | PNG_CONST PNG_pHYs; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 892 | #endif |
| 893 | #if defined(PNG_READ_sBIT_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 894 | PNG_CONST PNG_sBIT; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 895 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 896 | #if defined(PNG_READ_sCAL_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 897 | PNG_CONST PNG_sCAL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 898 | #endif |
| 899 | #if defined(PNG_READ_sPLT_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 900 | PNG_CONST PNG_sPLT; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 901 | #endif |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 902 | #if defined(PNG_READ_sRGB_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 903 | PNG_CONST PNG_sRGB; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 904 | #endif |
| 905 | #if defined(PNG_READ_tEXt_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 906 | PNG_CONST PNG_tEXt; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 907 | #endif |
| 908 | #if defined(PNG_READ_tIME_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 909 | PNG_CONST PNG_tIME; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 910 | #endif |
| 911 | #if defined(PNG_READ_tRNS_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 912 | PNG_CONST PNG_tRNS; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 913 | #endif |
| 914 | #if defined(PNG_READ_zTXt_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 915 | PNG_CONST PNG_zTXt; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 916 | #endif |
Glenn Randers-Pehrson | 16e1166 | 2004-11-01 14:13:40 -0600 | [diff] [blame] | 917 | #endif /* PNG_USE_LOCAL_ARRAYS */ |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 918 | png_uint_32 length = png_read_chunk_header(png_ptr); |
| 919 | PNG_CONST png_bytep chunk_name = png_ptr->chunk_name; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 920 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 921 | if (!png_memcmp(chunk_name, png_IHDR, 4)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 922 | png_handle_IHDR(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 923 | else if (!png_memcmp(chunk_name, png_IEND, 4)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 924 | png_handle_IEND(png_ptr, info_ptr, length); |
| 925 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 926 | else if (png_handle_as_unknown(png_ptr, chunk_name)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 927 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 928 | if (!png_memcmp(chunk_name, png_IDAT, 4)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 929 | { |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 930 | if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 931 | png_benign_error(png_ptr, "Too many IDATs found"); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 932 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 933 | png_handle_unknown(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 934 | if (!png_memcmp(chunk_name, png_PLTE, 4)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 935 | png_ptr->mode |= PNG_HAVE_PLTE; |
| 936 | } |
| 937 | #endif |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 938 | else if (!png_memcmp(chunk_name, png_IDAT, 4)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 939 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 940 | /* Zero length IDATs are legal after the last IDAT has been |
| 941 | * read, but not after other chunks have been read. |
| 942 | */ |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 943 | if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 944 | png_benign_error(png_ptr, "Too many IDATs found"); |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 945 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 946 | } |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 947 | else if (!png_memcmp(chunk_name, png_PLTE, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 948 | png_handle_PLTE(png_ptr, info_ptr, length); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 949 | #if defined(PNG_READ_bKGD_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 950 | else if (!png_memcmp(chunk_name, png_bKGD, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 951 | png_handle_bKGD(png_ptr, info_ptr, length); |
| 952 | #endif |
| 953 | #if defined(PNG_READ_cHRM_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 954 | else if (!png_memcmp(chunk_name, png_cHRM, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 955 | png_handle_cHRM(png_ptr, info_ptr, length); |
| 956 | #endif |
| 957 | #if defined(PNG_READ_gAMA_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 958 | else if (!png_memcmp(chunk_name, png_gAMA, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 959 | png_handle_gAMA(png_ptr, info_ptr, length); |
| 960 | #endif |
| 961 | #if defined(PNG_READ_hIST_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 962 | else if (!png_memcmp(chunk_name, png_hIST, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 963 | png_handle_hIST(png_ptr, info_ptr, length); |
| 964 | #endif |
| 965 | #if defined(PNG_READ_oFFs_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 966 | else if (!png_memcmp(chunk_name, png_oFFs, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 967 | png_handle_oFFs(png_ptr, info_ptr, length); |
| 968 | #endif |
| 969 | #if defined(PNG_READ_pCAL_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 970 | else if (!png_memcmp(chunk_name, png_pCAL, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 971 | png_handle_pCAL(png_ptr, info_ptr, length); |
| 972 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 973 | #if defined(PNG_READ_sCAL_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 974 | else if (!png_memcmp(chunk_name, png_sCAL, 4)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 975 | png_handle_sCAL(png_ptr, info_ptr, length); |
| 976 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 977 | #if defined(PNG_READ_pHYs_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 978 | else if (!png_memcmp(chunk_name, png_pHYs, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 979 | png_handle_pHYs(png_ptr, info_ptr, length); |
| 980 | #endif |
| 981 | #if defined(PNG_READ_sBIT_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 982 | else if (!png_memcmp(chunk_name, png_sBIT, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 983 | png_handle_sBIT(png_ptr, info_ptr, length); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 984 | #endif |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 985 | #if defined(PNG_READ_sRGB_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 986 | else if (!png_memcmp(chunk_name, png_sRGB, 4)) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 987 | png_handle_sRGB(png_ptr, info_ptr, length); |
| 988 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 989 | #if defined(PNG_READ_iCCP_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 990 | else if (!png_memcmp(chunk_name, png_iCCP, 4)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 991 | png_handle_iCCP(png_ptr, info_ptr, length); |
| 992 | #endif |
| 993 | #if defined(PNG_READ_sPLT_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 994 | else if (!png_memcmp(chunk_name, png_sPLT, 4)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 995 | png_handle_sPLT(png_ptr, info_ptr, length); |
| 996 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 997 | #if defined(PNG_READ_tEXt_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 998 | else if (!png_memcmp(chunk_name, png_tEXt, 4)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 999 | png_handle_tEXt(png_ptr, info_ptr, length); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1000 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1001 | #if defined(PNG_READ_tIME_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1002 | else if (!png_memcmp(chunk_name, png_tIME, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1003 | png_handle_tIME(png_ptr, info_ptr, length); |
| 1004 | #endif |
| 1005 | #if defined(PNG_READ_tRNS_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1006 | else if (!png_memcmp(chunk_name, png_tRNS, 4)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1007 | png_handle_tRNS(png_ptr, info_ptr, length); |
| 1008 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1009 | #if defined(PNG_READ_zTXt_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1010 | else if (!png_memcmp(chunk_name, png_zTXt, 4)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1011 | png_handle_zTXt(png_ptr, info_ptr, length); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1012 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1013 | #if defined(PNG_READ_iTXt_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1014 | else if (!png_memcmp(chunk_name, png_iTXt, 4)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1015 | png_handle_iTXt(png_ptr, info_ptr, length); |
| 1016 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1017 | else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1018 | png_handle_unknown(png_ptr, info_ptr, length); |
| 1019 | } while (!(png_ptr->mode & PNG_HAVE_IEND)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1020 | } |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1021 | #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1022 | |
| 1023 | /* free all memory used by the read */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1024 | void PNGAPI |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1025 | 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] | 1026 | png_infopp end_info_ptr_ptr) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1027 | { |
| 1028 | png_structp png_ptr = NULL; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1029 | png_infop info_ptr = NULL, end_info_ptr = NULL; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1030 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1031 | png_free_ptr free_fn = NULL; |
| 1032 | png_voidp mem_ptr = NULL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1033 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1034 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1035 | png_debug(1, "in png_destroy_read_struct"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1036 | if (png_ptr_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1037 | png_ptr = *png_ptr_ptr; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1038 | if (png_ptr == NULL) |
| 1039 | return; |
| 1040 | |
| 1041 | #ifdef PNG_USER_MEM_SUPPORTED |
| 1042 | free_fn = png_ptr->free_fn; |
| 1043 | mem_ptr = png_ptr->mem_ptr; |
| 1044 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1045 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1046 | if (info_ptr_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1047 | info_ptr = *info_ptr_ptr; |
| 1048 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1049 | if (end_info_ptr_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1050 | end_info_ptr = *end_info_ptr_ptr; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1051 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1052 | png_read_destroy(png_ptr, info_ptr, end_info_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1053 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1054 | if (info_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1055 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1056 | #if defined(PNG_TEXT_SUPPORTED) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1057 | png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1058 | #endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1059 | |
| 1060 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 1061 | png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn, |
| 1062 | (png_voidp)mem_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1063 | #else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1064 | png_destroy_struct((png_voidp)info_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1065 | #endif |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 1066 | *info_ptr_ptr = NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1067 | } |
| 1068 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1069 | if (end_info_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1070 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1071 | #if defined(PNG_READ_TEXT_SUPPORTED) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1072 | png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1073 | #endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1074 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 1075 | png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn, |
| 1076 | (png_voidp)mem_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1077 | #else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1078 | png_destroy_struct((png_voidp)end_info_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1079 | #endif |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 1080 | *end_info_ptr_ptr = NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1081 | } |
| 1082 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1083 | if (png_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1084 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1085 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 1086 | png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, |
| 1087 | (png_voidp)mem_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1088 | #else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1089 | png_destroy_struct((png_voidp)png_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1090 | #endif |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 1091 | *png_ptr_ptr = NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1095 | /* free all memory used by the read (old method) */ |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1096 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1097 | 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] | 1098 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1099 | #ifdef PNG_SETJMP_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1100 | jmp_buf tmp_jmp; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1101 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1102 | png_error_ptr error_fn; |
| 1103 | png_error_ptr warning_fn; |
| 1104 | png_voidp error_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1105 | #ifdef PNG_USER_MEM_SUPPORTED |
| 1106 | png_free_ptr free_fn; |
| 1107 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1108 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1109 | png_debug(1, "in png_read_destroy"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1110 | if (info_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1111 | png_info_destroy(png_ptr, info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1112 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1113 | if (end_info_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1114 | png_info_destroy(png_ptr, end_info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1115 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1116 | png_free(png_ptr, png_ptr->zbuf); |
Glenn Randers-Pehrson | 1b8e567 | 2001-08-25 06:46:06 -0500 | [diff] [blame] | 1117 | png_free(png_ptr, png_ptr->big_row_buf); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1118 | png_free(png_ptr, png_ptr->prev_row); |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 1119 | png_free(png_ptr, png_ptr->chunkdata); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1120 | #if defined(PNG_READ_DITHER_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1121 | png_free(png_ptr, png_ptr->palette_lookup); |
| 1122 | png_free(png_ptr, png_ptr->dither_index); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1123 | #endif |
| 1124 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1125 | png_free(png_ptr, png_ptr->gamma_table); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1126 | #endif |
| 1127 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1128 | png_free(png_ptr, png_ptr->gamma_from_1); |
| 1129 | png_free(png_ptr, png_ptr->gamma_to_1); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 1130 | #endif |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1131 | #ifdef PNG_FREE_ME_SUPPORTED |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1132 | if (png_ptr->free_me & PNG_FREE_PLTE) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1133 | png_zfree(png_ptr, png_ptr->palette); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1134 | png_ptr->free_me &= ~PNG_FREE_PLTE; |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1135 | #else |
| 1136 | if (png_ptr->flags & PNG_FLAG_FREE_PLTE) |
| 1137 | png_zfree(png_ptr, png_ptr->palette); |
| 1138 | png_ptr->flags &= ~PNG_FLAG_FREE_PLTE; |
| 1139 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1140 | #if defined(PNG_tRNS_SUPPORTED) || \ |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1141 | defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1142 | #ifdef PNG_FREE_ME_SUPPORTED |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1143 | if (png_ptr->free_me & PNG_FREE_TRNS) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1144 | png_free(png_ptr, png_ptr->trans); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1145 | png_ptr->free_me &= ~PNG_FREE_TRNS; |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1146 | #else |
| 1147 | if (png_ptr->flags & PNG_FLAG_FREE_TRNS) |
| 1148 | png_free(png_ptr, png_ptr->trans); |
| 1149 | png_ptr->flags &= ~PNG_FLAG_FREE_TRNS; |
| 1150 | #endif |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1151 | #endif |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 1152 | #if defined(PNG_READ_hIST_SUPPORTED) |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1153 | #ifdef PNG_FREE_ME_SUPPORTED |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1154 | if (png_ptr->free_me & PNG_FREE_HIST) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1155 | png_free(png_ptr, png_ptr->hist); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1156 | png_ptr->free_me &= ~PNG_FREE_HIST; |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1157 | #else |
| 1158 | if (png_ptr->flags & PNG_FLAG_FREE_HIST) |
| 1159 | png_free(png_ptr, png_ptr->hist); |
| 1160 | png_ptr->flags &= ~PNG_FLAG_FREE_HIST; |
| 1161 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1162 | #endif |
| 1163 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1164 | if (png_ptr->gamma_16_table != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1165 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1166 | int i; |
| 1167 | int istop = (1 << (8 - png_ptr->gamma_shift)); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1168 | for (i = 0; i < istop; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1169 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1170 | png_free(png_ptr, png_ptr->gamma_16_table[i]); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1171 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1172 | png_free(png_ptr, png_ptr->gamma_16_table); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1173 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 1174 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1175 | if (png_ptr->gamma_16_from_1 != NULL) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 1176 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1177 | int i; |
| 1178 | int istop = (1 << (8 - png_ptr->gamma_shift)); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1179 | for (i = 0; i < istop; i++) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 1180 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1181 | png_free(png_ptr, png_ptr->gamma_16_from_1[i]); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 1182 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1183 | png_free(png_ptr, png_ptr->gamma_16_from_1); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1184 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1185 | if (png_ptr->gamma_16_to_1 != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1186 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1187 | int i; |
| 1188 | int istop = (1 << (8 - png_ptr->gamma_shift)); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1189 | for (i = 0; i < istop; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1190 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1191 | png_free(png_ptr, png_ptr->gamma_16_to_1[i]); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1192 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1193 | png_free(png_ptr, png_ptr->gamma_16_to_1); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1194 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1195 | #endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1196 | #endif |
| 1197 | #if defined(PNG_TIME_RFC1123_SUPPORTED) |
| 1198 | png_free(png_ptr, png_ptr->time_buffer); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1199 | #endif |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1200 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1201 | inflateEnd(&png_ptr->zstream); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1202 | #ifdef PNG_PROGRESSIVE_READ_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1203 | png_free(png_ptr, png_ptr->save_buffer); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1204 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1205 | |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 1206 | #ifdef PNG_PROGRESSIVE_READ_SUPPORTED |
| 1207 | #ifdef PNG_TEXT_SUPPORTED |
| 1208 | png_free(png_ptr, png_ptr->current_text); |
| 1209 | #endif /* PNG_TEXT_SUPPORTED */ |
| 1210 | #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ |
| 1211 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1212 | /* Save the important info out of the png_struct, in case it is |
| 1213 | * being used again. |
| 1214 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1215 | #ifdef PNG_SETJMP_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1216 | png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1217 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1218 | |
| 1219 | error_fn = png_ptr->error_fn; |
| 1220 | warning_fn = png_ptr->warning_fn; |
| 1221 | error_ptr = png_ptr->error_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1222 | #ifdef PNG_USER_MEM_SUPPORTED |
| 1223 | free_fn = png_ptr->free_fn; |
| 1224 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1225 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1226 | png_memset(png_ptr, 0, png_sizeof(png_struct)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1227 | |
| 1228 | png_ptr->error_fn = error_fn; |
| 1229 | png_ptr->warning_fn = warning_fn; |
| 1230 | png_ptr->error_ptr = error_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1231 | #ifdef PNG_USER_MEM_SUPPORTED |
| 1232 | png_ptr->free_fn = free_fn; |
| 1233 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1234 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1235 | #ifdef PNG_SETJMP_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1236 | png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1237 | #endif |
| 1238 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1239 | } |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1240 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1241 | void PNGAPI |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1242 | png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn) |
| 1243 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1244 | if (png_ptr == NULL) return; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1245 | png_ptr->read_row_fn = read_row_fn; |
| 1246 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1247 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1248 | |
| 1249 | #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1250 | #if defined(PNG_INFO_IMAGE_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1251 | void PNGAPI |
| 1252 | png_read_png(png_structp png_ptr, png_infop info_ptr, |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1253 | int transforms, |
| 1254 | voidp params) |
| 1255 | { |
| 1256 | int row; |
| 1257 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1258 | if (png_ptr == NULL) return; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1259 | #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1260 | /* invert the alpha channel from opacity to transparency |
| 1261 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1262 | if (transforms & PNG_TRANSFORM_INVERT_ALPHA) |
| 1263 | png_set_invert_alpha(png_ptr); |
| 1264 | #endif |
| 1265 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1266 | /* png_read_info() gives us all of the information from the |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1267 | * PNG file before the first IDAT (image data chunk). |
| 1268 | */ |
| 1269 | png_read_info(png_ptr, info_ptr); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1270 | if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep)) |
| 1271 | png_error(png_ptr, "Image is too high to process with png_read_png()"); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1272 | |
| 1273 | /* -------------- image transformations start here ------------------- */ |
| 1274 | |
| 1275 | #if defined(PNG_READ_16_TO_8_SUPPORTED) |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1276 | /* tell libpng to strip 16 bit/color files down to 8 bits per color |
| 1277 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1278 | if (transforms & PNG_TRANSFORM_STRIP_16) |
| 1279 | png_set_strip_16(png_ptr); |
| 1280 | #endif |
| 1281 | |
| 1282 | #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1283 | /* Strip alpha bytes from the input data without combining with |
| 1284 | * the background (not recommended). |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1285 | */ |
| 1286 | if (transforms & PNG_TRANSFORM_STRIP_ALPHA) |
| 1287 | png_set_strip_alpha(png_ptr); |
| 1288 | #endif |
| 1289 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1290 | #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED) |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1291 | /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1292 | * byte into separate bytes (useful for paletted and grayscale images). |
| 1293 | */ |
| 1294 | if (transforms & PNG_TRANSFORM_PACKING) |
| 1295 | png_set_packing(png_ptr); |
| 1296 | #endif |
| 1297 | |
| 1298 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 1299 | /* Change the order of packed pixels to least significant bit first |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1300 | * (not useful if you are using png_set_packing). |
| 1301 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1302 | if (transforms & PNG_TRANSFORM_PACKSWAP) |
| 1303 | png_set_packswap(png_ptr); |
| 1304 | #endif |
| 1305 | |
| 1306 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
| 1307 | /* Expand paletted colors into true RGB triplets |
| 1308 | * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel |
| 1309 | * Expand paletted or RGB images with transparency to full alpha |
| 1310 | * channels so the data will be available as RGBA quartets. |
| 1311 | */ |
| 1312 | if (transforms & PNG_TRANSFORM_EXPAND) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1313 | if ((png_ptr->bit_depth < 8) || |
| 1314 | (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) || |
| 1315 | (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))) |
| 1316 | png_set_expand(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1317 | #endif |
| 1318 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1319 | /* We don't handle background color or gamma transformation or dithering. |
| 1320 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1321 | |
| 1322 | #if defined(PNG_READ_INVERT_SUPPORTED) |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1323 | /* invert monochrome files to have 0 as white and 1 as black |
| 1324 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1325 | if (transforms & PNG_TRANSFORM_INVERT_MONO) |
| 1326 | png_set_invert_mono(png_ptr); |
| 1327 | #endif |
| 1328 | |
| 1329 | #if defined(PNG_READ_SHIFT_SUPPORTED) |
| 1330 | /* If you want to shift the pixel values from the range [0,255] or |
| 1331 | * [0,65535] to the original [0,7] or [0,31], or whatever range the |
| 1332 | * colors were originally in: |
| 1333 | */ |
| 1334 | if ((transforms & PNG_TRANSFORM_SHIFT) |
| 1335 | && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) |
| 1336 | { |
| 1337 | png_color_8p sig_bit; |
| 1338 | |
| 1339 | png_get_sBIT(png_ptr, info_ptr, &sig_bit); |
| 1340 | png_set_shift(png_ptr, sig_bit); |
| 1341 | } |
| 1342 | #endif |
| 1343 | |
| 1344 | #if defined(PNG_READ_BGR_SUPPORTED) |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1345 | /* flip the RGB pixels to BGR (or RGBA to BGRA) |
| 1346 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1347 | if (transforms & PNG_TRANSFORM_BGR) |
| 1348 | png_set_bgr(png_ptr); |
| 1349 | #endif |
| 1350 | |
| 1351 | #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1352 | /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) |
| 1353 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1354 | if (transforms & PNG_TRANSFORM_SWAP_ALPHA) |
| 1355 | png_set_swap_alpha(png_ptr); |
| 1356 | #endif |
| 1357 | |
| 1358 | #if defined(PNG_READ_SWAP_SUPPORTED) |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1359 | /* swap bytes of 16 bit files to least significant byte first |
| 1360 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1361 | if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) |
| 1362 | png_set_swap(png_ptr); |
| 1363 | #endif |
| 1364 | |
| 1365 | /* We don't handle adding filler bytes */ |
| 1366 | |
| 1367 | /* Optional call to gamma correct and add the background to the palette |
| 1368 | * and update info structure. REQUIRED if you are expecting libpng to |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1369 | * update the palette for you (i.e., you selected such a transform above). |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1370 | */ |
| 1371 | png_read_update_info(png_ptr, info_ptr); |
| 1372 | |
| 1373 | /* -------------- image transformations end here ------------------- */ |
| 1374 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1375 | #ifdef PNG_FREE_ME_SUPPORTED |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 1376 | png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1377 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1378 | if (info_ptr->row_pointers == NULL) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1379 | { |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame^] | 1380 | info_ptr->row_pointers = (png_bytepp)png_calloc(png_ptr, |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1381 | info_ptr->height * png_sizeof(png_bytep)); |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1382 | #ifdef PNG_FREE_ME_SUPPORTED |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1383 | info_ptr->free_me |= PNG_FREE_ROWS; |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1384 | #endif |
| 1385 | for (row = 0; row < (int)info_ptr->height; row++) |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1386 | { |
| 1387 | info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1388 | png_get_rowbytes(png_ptr, info_ptr)); |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1389 | } |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1390 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1391 | |
| 1392 | png_read_image(png_ptr, info_ptr->row_pointers); |
| 1393 | info_ptr->valid |= PNG_INFO_IDAT; |
| 1394 | |
| 1395 | /* read rest of file, and get additional chunks in info_ptr - REQUIRED */ |
| 1396 | png_read_end(png_ptr, info_ptr); |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1397 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1398 | transforms = transforms; /* quiet compiler warnings */ |
| 1399 | params = params; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1400 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1401 | } |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 1402 | #endif /* PNG_INFO_IMAGE_SUPPORTED */ |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1403 | #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 1404 | #endif /* PNG_READ_SUPPORTED */ |