annotate lib/libpng/pngread.c @ 2553:48708da03b7f

Party Creation UI logic separated from MainMenu
author a.parshin
date Wed, 13 May 2015 02:20:05 +0200
parents 6e178010fc29
children
rev   line source
2296
Ritor1
parents:
diff changeset
1
Ritor1
parents:
diff changeset
2 /* pngread.c - read a PNG file
Ritor1
parents:
diff changeset
3 *
Ritor1
parents:
diff changeset
4 * Last changed in libpng 1.6.10 [March 6, 2014]
Ritor1
parents:
diff changeset
5 * Copyright (c) 1998-2014 Glenn Randers-Pehrson
Ritor1
parents:
diff changeset
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
Ritor1
parents:
diff changeset
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Ritor1
parents:
diff changeset
8 *
Ritor1
parents:
diff changeset
9 * This code is released under the libpng license.
Ritor1
parents:
diff changeset
10 * For conditions of distribution and use, see the disclaimer
Ritor1
parents:
diff changeset
11 * and license in png.h
Ritor1
parents:
diff changeset
12 *
Ritor1
parents:
diff changeset
13 * This file contains routines that an application calls directly to
Ritor1
parents:
diff changeset
14 * read a PNG file or stream.
Ritor1
parents:
diff changeset
15 */
Ritor1
parents:
diff changeset
16
Ritor1
parents:
diff changeset
17 #include "pngpriv.h"
Ritor1
parents:
diff changeset
18 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
Ritor1
parents:
diff changeset
19 # include <errno.h>
Ritor1
parents:
diff changeset
20 #endif
Ritor1
parents:
diff changeset
21
Ritor1
parents:
diff changeset
22 #ifdef PNG_READ_SUPPORTED
Ritor1
parents:
diff changeset
23
Ritor1
parents:
diff changeset
24 /* Create a PNG structure for reading, and allocate any memory needed. */
Ritor1
parents:
diff changeset
25 PNG_FUNCTION(png_structp,PNGAPI
Ritor1
parents:
diff changeset
26 png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
Ritor1
parents:
diff changeset
27 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
Ritor1
parents:
diff changeset
28 {
Ritor1
parents:
diff changeset
29 #ifndef PNG_USER_MEM_SUPPORTED
Ritor1
parents:
diff changeset
30 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
Ritor1
parents:
diff changeset
31 error_fn, warn_fn, NULL, NULL, NULL);
Ritor1
parents:
diff changeset
32 #else
Ritor1
parents:
diff changeset
33 return png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
Ritor1
parents:
diff changeset
34 warn_fn, NULL, NULL, NULL);
Ritor1
parents:
diff changeset
35 }
Ritor1
parents:
diff changeset
36
Ritor1
parents:
diff changeset
37 /* Alternate create PNG structure for reading, and allocate any memory
Ritor1
parents:
diff changeset
38 * needed.
Ritor1
parents:
diff changeset
39 */
Ritor1
parents:
diff changeset
40 PNG_FUNCTION(png_structp,PNGAPI
Ritor1
parents:
diff changeset
41 png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Ritor1
parents:
diff changeset
42 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Ritor1
parents:
diff changeset
43 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Ritor1
parents:
diff changeset
44 {
Ritor1
parents:
diff changeset
45 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
Ritor1
parents:
diff changeset
46 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
Ritor1
parents:
diff changeset
47 #endif /* PNG_USER_MEM_SUPPORTED */
Ritor1
parents:
diff changeset
48
Ritor1
parents:
diff changeset
49 if (png_ptr != NULL)
Ritor1
parents:
diff changeset
50 {
Ritor1
parents:
diff changeset
51 png_ptr->mode = PNG_IS_READ_STRUCT;
Ritor1
parents:
diff changeset
52
Ritor1
parents:
diff changeset
53 /* Added in libpng-1.6.0; this can be used to detect a read structure if
Ritor1
parents:
diff changeset
54 * required (it will be zero in a write structure.)
Ritor1
parents:
diff changeset
55 */
Ritor1
parents:
diff changeset
56 # ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Ritor1
parents:
diff changeset
57 png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE;
Ritor1
parents:
diff changeset
58 # endif
Ritor1
parents:
diff changeset
59
Ritor1
parents:
diff changeset
60 # ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
Ritor1
parents:
diff changeset
61 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
Ritor1
parents:
diff changeset
62
Ritor1
parents:
diff changeset
63 /* In stable builds only warn if an application error can be completely
Ritor1
parents:
diff changeset
64 * handled.
Ritor1
parents:
diff changeset
65 */
Ritor1
parents:
diff changeset
66 # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
Ritor1
parents:
diff changeset
67 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
Ritor1
parents:
diff changeset
68 # endif
Ritor1
parents:
diff changeset
69 # endif
Ritor1
parents:
diff changeset
70
Ritor1
parents:
diff changeset
71 /* TODO: delay this, it can be done in png_init_io (if the app doesn't
Ritor1
parents:
diff changeset
72 * do it itself) avoiding setting the default function if it is not
Ritor1
parents:
diff changeset
73 * required.
Ritor1
parents:
diff changeset
74 */
Ritor1
parents:
diff changeset
75 png_set_read_fn(png_ptr, NULL, NULL);
Ritor1
parents:
diff changeset
76 }
Ritor1
parents:
diff changeset
77
Ritor1
parents:
diff changeset
78 return png_ptr;
Ritor1
parents:
diff changeset
79 }
Ritor1
parents:
diff changeset
80
Ritor1
parents:
diff changeset
81
Ritor1
parents:
diff changeset
82 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Ritor1
parents:
diff changeset
83 /* Read the information before the actual image data. This has been
Ritor1
parents:
diff changeset
84 * changed in v0.90 to allow reading a file that already has the magic
Ritor1
parents:
diff changeset
85 * bytes read from the stream. You can tell libpng how many bytes have
Ritor1
parents:
diff changeset
86 * been read from the beginning of the stream (up to the maximum of 8)
Ritor1
parents:
diff changeset
87 * via png_set_sig_bytes(), and we will only check the remaining bytes
Ritor1
parents:
diff changeset
88 * here. The application can then have access to the signature bytes we
Ritor1
parents:
diff changeset
89 * read if it is determined that this isn't a valid PNG file.
Ritor1
parents:
diff changeset
90 */
Ritor1
parents:
diff changeset
91 void PNGAPI
Ritor1
parents:
diff changeset
92 png_read_info(png_structrp png_ptr, png_inforp info_ptr)
Ritor1
parents:
diff changeset
93 {
Ritor1
parents:
diff changeset
94 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Ritor1
parents:
diff changeset
95 int keep;
Ritor1
parents:
diff changeset
96 #endif
Ritor1
parents:
diff changeset
97
Ritor1
parents:
diff changeset
98 png_debug(1, "in png_read_info");
Ritor1
parents:
diff changeset
99
Ritor1
parents:
diff changeset
100 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
101 return;
Ritor1
parents:
diff changeset
102
Ritor1
parents:
diff changeset
103 /* Read and check the PNG file signature. */
Ritor1
parents:
diff changeset
104 png_read_sig(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
105
Ritor1
parents:
diff changeset
106 for (;;)
Ritor1
parents:
diff changeset
107 {
Ritor1
parents:
diff changeset
108 png_uint_32 length = png_read_chunk_header(png_ptr);
Ritor1
parents:
diff changeset
109 png_uint_32 chunk_name = png_ptr->chunk_name;
Ritor1
parents:
diff changeset
110
Ritor1
parents:
diff changeset
111 /* IDAT logic needs to happen here to simplify getting the two flags
Ritor1
parents:
diff changeset
112 * right.
Ritor1
parents:
diff changeset
113 */
Ritor1
parents:
diff changeset
114 if (chunk_name == png_IDAT)
Ritor1
parents:
diff changeset
115 {
Ritor1
parents:
diff changeset
116 if (!(png_ptr->mode & PNG_HAVE_IHDR))
Ritor1
parents:
diff changeset
117 png_chunk_error(png_ptr, "Missing IHDR before IDAT");
Ritor1
parents:
diff changeset
118
Ritor1
parents:
diff changeset
119 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Ritor1
parents:
diff changeset
120 !(png_ptr->mode & PNG_HAVE_PLTE))
Ritor1
parents:
diff changeset
121 png_chunk_error(png_ptr, "Missing PLTE before IDAT");
Ritor1
parents:
diff changeset
122
Ritor1
parents:
diff changeset
123 else if (png_ptr->mode & PNG_AFTER_IDAT)
Ritor1
parents:
diff changeset
124 png_chunk_benign_error(png_ptr, "Too many IDATs found");
Ritor1
parents:
diff changeset
125
Ritor1
parents:
diff changeset
126 png_ptr->mode |= PNG_HAVE_IDAT;
Ritor1
parents:
diff changeset
127 }
Ritor1
parents:
diff changeset
128
Ritor1
parents:
diff changeset
129 else if (png_ptr->mode & PNG_HAVE_IDAT)
Ritor1
parents:
diff changeset
130 png_ptr->mode |= PNG_AFTER_IDAT;
Ritor1
parents:
diff changeset
131
Ritor1
parents:
diff changeset
132 /* This should be a binary subdivision search or a hash for
Ritor1
parents:
diff changeset
133 * matching the chunk name rather than a linear search.
Ritor1
parents:
diff changeset
134 */
Ritor1
parents:
diff changeset
135 if (chunk_name == png_IHDR)
Ritor1
parents:
diff changeset
136 png_handle_IHDR(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
137
Ritor1
parents:
diff changeset
138 else if (chunk_name == png_IEND)
Ritor1
parents:
diff changeset
139 png_handle_IEND(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
140
Ritor1
parents:
diff changeset
141 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Ritor1
parents:
diff changeset
142 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
Ritor1
parents:
diff changeset
143 {
Ritor1
parents:
diff changeset
144 png_handle_unknown(png_ptr, info_ptr, length, keep);
Ritor1
parents:
diff changeset
145
Ritor1
parents:
diff changeset
146 if (chunk_name == png_PLTE)
Ritor1
parents:
diff changeset
147 png_ptr->mode |= PNG_HAVE_PLTE;
Ritor1
parents:
diff changeset
148
Ritor1
parents:
diff changeset
149 else if (chunk_name == png_IDAT)
Ritor1
parents:
diff changeset
150 {
Ritor1
parents:
diff changeset
151 png_ptr->idat_size = 0; /* It has been consumed */
Ritor1
parents:
diff changeset
152 break;
Ritor1
parents:
diff changeset
153 }
Ritor1
parents:
diff changeset
154 }
Ritor1
parents:
diff changeset
155 #endif
Ritor1
parents:
diff changeset
156 else if (chunk_name == png_PLTE)
Ritor1
parents:
diff changeset
157 png_handle_PLTE(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
158
Ritor1
parents:
diff changeset
159 else if (chunk_name == png_IDAT)
Ritor1
parents:
diff changeset
160 {
Ritor1
parents:
diff changeset
161 png_ptr->idat_size = length;
Ritor1
parents:
diff changeset
162 break;
Ritor1
parents:
diff changeset
163 }
Ritor1
parents:
diff changeset
164
Ritor1
parents:
diff changeset
165 #ifdef PNG_READ_bKGD_SUPPORTED
Ritor1
parents:
diff changeset
166 else if (chunk_name == png_bKGD)
Ritor1
parents:
diff changeset
167 png_handle_bKGD(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
168 #endif
Ritor1
parents:
diff changeset
169
Ritor1
parents:
diff changeset
170 #ifdef PNG_READ_cHRM_SUPPORTED
Ritor1
parents:
diff changeset
171 else if (chunk_name == png_cHRM)
Ritor1
parents:
diff changeset
172 png_handle_cHRM(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
173 #endif
Ritor1
parents:
diff changeset
174
Ritor1
parents:
diff changeset
175 #ifdef PNG_READ_gAMA_SUPPORTED
Ritor1
parents:
diff changeset
176 else if (chunk_name == png_gAMA)
Ritor1
parents:
diff changeset
177 png_handle_gAMA(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
178 #endif
Ritor1
parents:
diff changeset
179
Ritor1
parents:
diff changeset
180 #ifdef PNG_READ_hIST_SUPPORTED
Ritor1
parents:
diff changeset
181 else if (chunk_name == png_hIST)
Ritor1
parents:
diff changeset
182 png_handle_hIST(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
183 #endif
Ritor1
parents:
diff changeset
184
Ritor1
parents:
diff changeset
185 #ifdef PNG_READ_oFFs_SUPPORTED
Ritor1
parents:
diff changeset
186 else if (chunk_name == png_oFFs)
Ritor1
parents:
diff changeset
187 png_handle_oFFs(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
188 #endif
Ritor1
parents:
diff changeset
189
Ritor1
parents:
diff changeset
190 #ifdef PNG_READ_pCAL_SUPPORTED
Ritor1
parents:
diff changeset
191 else if (chunk_name == png_pCAL)
Ritor1
parents:
diff changeset
192 png_handle_pCAL(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
193 #endif
Ritor1
parents:
diff changeset
194
Ritor1
parents:
diff changeset
195 #ifdef PNG_READ_sCAL_SUPPORTED
Ritor1
parents:
diff changeset
196 else if (chunk_name == png_sCAL)
Ritor1
parents:
diff changeset
197 png_handle_sCAL(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
198 #endif
Ritor1
parents:
diff changeset
199
Ritor1
parents:
diff changeset
200 #ifdef PNG_READ_pHYs_SUPPORTED
Ritor1
parents:
diff changeset
201 else if (chunk_name == png_pHYs)
Ritor1
parents:
diff changeset
202 png_handle_pHYs(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
203 #endif
Ritor1
parents:
diff changeset
204
Ritor1
parents:
diff changeset
205 #ifdef PNG_READ_sBIT_SUPPORTED
Ritor1
parents:
diff changeset
206 else if (chunk_name == png_sBIT)
Ritor1
parents:
diff changeset
207 png_handle_sBIT(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
208 #endif
Ritor1
parents:
diff changeset
209
Ritor1
parents:
diff changeset
210 #ifdef PNG_READ_sRGB_SUPPORTED
Ritor1
parents:
diff changeset
211 else if (chunk_name == png_sRGB)
Ritor1
parents:
diff changeset
212 png_handle_sRGB(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
213 #endif
Ritor1
parents:
diff changeset
214
Ritor1
parents:
diff changeset
215 #ifdef PNG_READ_iCCP_SUPPORTED
Ritor1
parents:
diff changeset
216 else if (chunk_name == png_iCCP)
Ritor1
parents:
diff changeset
217 png_handle_iCCP(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
218 #endif
Ritor1
parents:
diff changeset
219
Ritor1
parents:
diff changeset
220 #ifdef PNG_READ_sPLT_SUPPORTED
Ritor1
parents:
diff changeset
221 else if (chunk_name == png_sPLT)
Ritor1
parents:
diff changeset
222 png_handle_sPLT(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
223 #endif
Ritor1
parents:
diff changeset
224
Ritor1
parents:
diff changeset
225 #ifdef PNG_READ_tEXt_SUPPORTED
Ritor1
parents:
diff changeset
226 else if (chunk_name == png_tEXt)
Ritor1
parents:
diff changeset
227 png_handle_tEXt(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
228 #endif
Ritor1
parents:
diff changeset
229
Ritor1
parents:
diff changeset
230 #ifdef PNG_READ_tIME_SUPPORTED
Ritor1
parents:
diff changeset
231 else if (chunk_name == png_tIME)
Ritor1
parents:
diff changeset
232 png_handle_tIME(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
233 #endif
Ritor1
parents:
diff changeset
234
Ritor1
parents:
diff changeset
235 #ifdef PNG_READ_tRNS_SUPPORTED
Ritor1
parents:
diff changeset
236 else if (chunk_name == png_tRNS)
Ritor1
parents:
diff changeset
237 png_handle_tRNS(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
238 #endif
Ritor1
parents:
diff changeset
239
Ritor1
parents:
diff changeset
240 #ifdef PNG_READ_zTXt_SUPPORTED
Ritor1
parents:
diff changeset
241 else if (chunk_name == png_zTXt)
Ritor1
parents:
diff changeset
242 png_handle_zTXt(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
243 #endif
Ritor1
parents:
diff changeset
244
Ritor1
parents:
diff changeset
245 #ifdef PNG_READ_iTXt_SUPPORTED
Ritor1
parents:
diff changeset
246 else if (chunk_name == png_iTXt)
Ritor1
parents:
diff changeset
247 png_handle_iTXt(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
248 #endif
Ritor1
parents:
diff changeset
249
Ritor1
parents:
diff changeset
250 else
Ritor1
parents:
diff changeset
251 png_handle_unknown(png_ptr, info_ptr, length,
Ritor1
parents:
diff changeset
252 PNG_HANDLE_CHUNK_AS_DEFAULT);
Ritor1
parents:
diff changeset
253 }
Ritor1
parents:
diff changeset
254 }
Ritor1
parents:
diff changeset
255 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Ritor1
parents:
diff changeset
256
Ritor1
parents:
diff changeset
257 /* Optional call to update the users info_ptr structure */
Ritor1
parents:
diff changeset
258 void PNGAPI
Ritor1
parents:
diff changeset
259 png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
Ritor1
parents:
diff changeset
260 {
Ritor1
parents:
diff changeset
261 png_debug(1, "in png_read_update_info");
Ritor1
parents:
diff changeset
262
Ritor1
parents:
diff changeset
263 if (png_ptr != NULL)
Ritor1
parents:
diff changeset
264 {
Ritor1
parents:
diff changeset
265 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
Ritor1
parents:
diff changeset
266 {
Ritor1
parents:
diff changeset
267 png_read_start_row(png_ptr);
Ritor1
parents:
diff changeset
268
Ritor1
parents:
diff changeset
269 # ifdef PNG_READ_TRANSFORMS_SUPPORTED
Ritor1
parents:
diff changeset
270 png_read_transform_info(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
271 # else
Ritor1
parents:
diff changeset
272 PNG_UNUSED(info_ptr)
Ritor1
parents:
diff changeset
273 # endif
Ritor1
parents:
diff changeset
274 }
Ritor1
parents:
diff changeset
275
Ritor1
parents:
diff changeset
276 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
Ritor1
parents:
diff changeset
277 else
Ritor1
parents:
diff changeset
278 png_app_error(png_ptr,
Ritor1
parents:
diff changeset
279 "png_read_update_info/png_start_read_image: duplicate call");
Ritor1
parents:
diff changeset
280 }
Ritor1
parents:
diff changeset
281 }
Ritor1
parents:
diff changeset
282
Ritor1
parents:
diff changeset
283 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Ritor1
parents:
diff changeset
284 /* Initialize palette, background, etc, after transformations
Ritor1
parents:
diff changeset
285 * are set, but before any reading takes place. This allows
Ritor1
parents:
diff changeset
286 * the user to obtain a gamma-corrected palette, for example.
Ritor1
parents:
diff changeset
287 * If the user doesn't call this, we will do it ourselves.
Ritor1
parents:
diff changeset
288 */
Ritor1
parents:
diff changeset
289 void PNGAPI
Ritor1
parents:
diff changeset
290 png_start_read_image(png_structrp png_ptr)
Ritor1
parents:
diff changeset
291 {
Ritor1
parents:
diff changeset
292 png_debug(1, "in png_start_read_image");
Ritor1
parents:
diff changeset
293
Ritor1
parents:
diff changeset
294 if (png_ptr != NULL)
Ritor1
parents:
diff changeset
295 {
Ritor1
parents:
diff changeset
296 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
Ritor1
parents:
diff changeset
297 png_read_start_row(png_ptr);
Ritor1
parents:
diff changeset
298
Ritor1
parents:
diff changeset
299 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
Ritor1
parents:
diff changeset
300 else
Ritor1
parents:
diff changeset
301 png_app_error(png_ptr,
Ritor1
parents:
diff changeset
302 "png_start_read_image/png_read_update_info: duplicate call");
Ritor1
parents:
diff changeset
303 }
Ritor1
parents:
diff changeset
304 }
Ritor1
parents:
diff changeset
305 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Ritor1
parents:
diff changeset
306
Ritor1
parents:
diff changeset
307 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Ritor1
parents:
diff changeset
308 #ifdef PNG_MNG_FEATURES_SUPPORTED
Ritor1
parents:
diff changeset
309 /* Undoes intrapixel differencing,
Ritor1
parents:
diff changeset
310 * NOTE: this is apparently only supported in the 'sequential' reader.
Ritor1
parents:
diff changeset
311 */
Ritor1
parents:
diff changeset
312 static void
Ritor1
parents:
diff changeset
313 png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
Ritor1
parents:
diff changeset
314 {
Ritor1
parents:
diff changeset
315 png_debug(1, "in png_do_read_intrapixel");
Ritor1
parents:
diff changeset
316
Ritor1
parents:
diff changeset
317 if (
Ritor1
parents:
diff changeset
318 (row_info->color_type & PNG_COLOR_MASK_COLOR))
Ritor1
parents:
diff changeset
319 {
Ritor1
parents:
diff changeset
320 int bytes_per_pixel;
Ritor1
parents:
diff changeset
321 png_uint_32 row_width = row_info->width;
Ritor1
parents:
diff changeset
322
Ritor1
parents:
diff changeset
323 if (row_info->bit_depth == 8)
Ritor1
parents:
diff changeset
324 {
Ritor1
parents:
diff changeset
325 png_bytep rp;
Ritor1
parents:
diff changeset
326 png_uint_32 i;
Ritor1
parents:
diff changeset
327
Ritor1
parents:
diff changeset
328 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
Ritor1
parents:
diff changeset
329 bytes_per_pixel = 3;
Ritor1
parents:
diff changeset
330
Ritor1
parents:
diff changeset
331 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Ritor1
parents:
diff changeset
332 bytes_per_pixel = 4;
Ritor1
parents:
diff changeset
333
Ritor1
parents:
diff changeset
334 else
Ritor1
parents:
diff changeset
335 return;
Ritor1
parents:
diff changeset
336
Ritor1
parents:
diff changeset
337 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
Ritor1
parents:
diff changeset
338 {
Ritor1
parents:
diff changeset
339 *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
Ritor1
parents:
diff changeset
340 *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
Ritor1
parents:
diff changeset
341 }
Ritor1
parents:
diff changeset
342 }
Ritor1
parents:
diff changeset
343 else if (row_info->bit_depth == 16)
Ritor1
parents:
diff changeset
344 {
Ritor1
parents:
diff changeset
345 png_bytep rp;
Ritor1
parents:
diff changeset
346 png_uint_32 i;
Ritor1
parents:
diff changeset
347
Ritor1
parents:
diff changeset
348 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
Ritor1
parents:
diff changeset
349 bytes_per_pixel = 6;
Ritor1
parents:
diff changeset
350
Ritor1
parents:
diff changeset
351 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Ritor1
parents:
diff changeset
352 bytes_per_pixel = 8;
Ritor1
parents:
diff changeset
353
Ritor1
parents:
diff changeset
354 else
Ritor1
parents:
diff changeset
355 return;
Ritor1
parents:
diff changeset
356
Ritor1
parents:
diff changeset
357 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
Ritor1
parents:
diff changeset
358 {
Ritor1
parents:
diff changeset
359 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
Ritor1
parents:
diff changeset
360 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
Ritor1
parents:
diff changeset
361 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
Ritor1
parents:
diff changeset
362 png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
Ritor1
parents:
diff changeset
363 png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
Ritor1
parents:
diff changeset
364 *(rp ) = (png_byte)((red >> 8) & 0xff);
Ritor1
parents:
diff changeset
365 *(rp + 1) = (png_byte)(red & 0xff);
Ritor1
parents:
diff changeset
366 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
Ritor1
parents:
diff changeset
367 *(rp + 5) = (png_byte)(blue & 0xff);
Ritor1
parents:
diff changeset
368 }
Ritor1
parents:
diff changeset
369 }
Ritor1
parents:
diff changeset
370 }
Ritor1
parents:
diff changeset
371 }
Ritor1
parents:
diff changeset
372 #endif /* PNG_MNG_FEATURES_SUPPORTED */
Ritor1
parents:
diff changeset
373
Ritor1
parents:
diff changeset
374 void PNGAPI
Ritor1
parents:
diff changeset
375 png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
Ritor1
parents:
diff changeset
376 {
Ritor1
parents:
diff changeset
377 png_row_info row_info;
Ritor1
parents:
diff changeset
378
Ritor1
parents:
diff changeset
379 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
380 return;
Ritor1
parents:
diff changeset
381
Ritor1
parents:
diff changeset
382 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Ritor1
parents:
diff changeset
383 (unsigned long)png_ptr->row_number, png_ptr->pass);
Ritor1
parents:
diff changeset
384
Ritor1
parents:
diff changeset
385 /* png_read_start_row sets the information (in particular iwidth) for this
Ritor1
parents:
diff changeset
386 * interlace pass.
Ritor1
parents:
diff changeset
387 */
Ritor1
parents:
diff changeset
388 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Ritor1
parents:
diff changeset
389 png_read_start_row(png_ptr);
Ritor1
parents:
diff changeset
390
Ritor1
parents:
diff changeset
391 /* 1.5.6: row_info moved out of png_struct to a local here. */
Ritor1
parents:
diff changeset
392 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
Ritor1
parents:
diff changeset
393 row_info.color_type = png_ptr->color_type;
Ritor1
parents:
diff changeset
394 row_info.bit_depth = png_ptr->bit_depth;
Ritor1
parents:
diff changeset
395 row_info.channels = png_ptr->channels;
Ritor1
parents:
diff changeset
396 row_info.pixel_depth = png_ptr->pixel_depth;
Ritor1
parents:
diff changeset
397 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
Ritor1
parents:
diff changeset
398
Ritor1
parents:
diff changeset
399 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Ritor1
parents:
diff changeset
400 {
Ritor1
parents:
diff changeset
401 /* Check for transforms that have been set but were defined out */
Ritor1
parents:
diff changeset
402 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
Ritor1
parents:
diff changeset
403 if (png_ptr->transformations & PNG_INVERT_MONO)
Ritor1
parents:
diff changeset
404 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Ritor1
parents:
diff changeset
405 #endif
Ritor1
parents:
diff changeset
406
Ritor1
parents:
diff changeset
407 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
Ritor1
parents:
diff changeset
408 if (png_ptr->transformations & PNG_FILLER)
Ritor1
parents:
diff changeset
409 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Ritor1
parents:
diff changeset
410 #endif
Ritor1
parents:
diff changeset
411
Ritor1
parents:
diff changeset
412 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
Ritor1
parents:
diff changeset
413 !defined(PNG_READ_PACKSWAP_SUPPORTED)
Ritor1
parents:
diff changeset
414 if (png_ptr->transformations & PNG_PACKSWAP)
Ritor1
parents:
diff changeset
415 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Ritor1
parents:
diff changeset
416 #endif
Ritor1
parents:
diff changeset
417
Ritor1
parents:
diff changeset
418 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
Ritor1
parents:
diff changeset
419 if (png_ptr->transformations & PNG_PACK)
Ritor1
parents:
diff changeset
420 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Ritor1
parents:
diff changeset
421 #endif
Ritor1
parents:
diff changeset
422
Ritor1
parents:
diff changeset
423 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
Ritor1
parents:
diff changeset
424 if (png_ptr->transformations & PNG_SHIFT)
Ritor1
parents:
diff changeset
425 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Ritor1
parents:
diff changeset
426 #endif
Ritor1
parents:
diff changeset
427
Ritor1
parents:
diff changeset
428 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
Ritor1
parents:
diff changeset
429 if (png_ptr->transformations & PNG_BGR)
Ritor1
parents:
diff changeset
430 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Ritor1
parents:
diff changeset
431 #endif
Ritor1
parents:
diff changeset
432
Ritor1
parents:
diff changeset
433 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
Ritor1
parents:
diff changeset
434 if (png_ptr->transformations & PNG_SWAP_BYTES)
Ritor1
parents:
diff changeset
435 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Ritor1
parents:
diff changeset
436 #endif
Ritor1
parents:
diff changeset
437 }
Ritor1
parents:
diff changeset
438
Ritor1
parents:
diff changeset
439 #ifdef PNG_READ_INTERLACING_SUPPORTED
Ritor1
parents:
diff changeset
440 /* If interlaced and we do not need a new row, combine row and return.
Ritor1
parents:
diff changeset
441 * Notice that the pixels we have from previous rows have been transformed
Ritor1
parents:
diff changeset
442 * already; we can only combine like with like (transformed or
Ritor1
parents:
diff changeset
443 * untransformed) and, because of the libpng API for interlaced images, this
Ritor1
parents:
diff changeset
444 * means we must transform before de-interlacing.
Ritor1
parents:
diff changeset
445 */
Ritor1
parents:
diff changeset
446 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
Ritor1
parents:
diff changeset
447 {
Ritor1
parents:
diff changeset
448 switch (png_ptr->pass)
Ritor1
parents:
diff changeset
449 {
Ritor1
parents:
diff changeset
450 case 0:
Ritor1
parents:
diff changeset
451 if (png_ptr->row_number & 0x07)
Ritor1
parents:
diff changeset
452 {
Ritor1
parents:
diff changeset
453 if (dsp_row != NULL)
Ritor1
parents:
diff changeset
454 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Ritor1
parents:
diff changeset
455 png_read_finish_row(png_ptr);
Ritor1
parents:
diff changeset
456 return;
Ritor1
parents:
diff changeset
457 }
Ritor1
parents:
diff changeset
458 break;
Ritor1
parents:
diff changeset
459
Ritor1
parents:
diff changeset
460 case 1:
Ritor1
parents:
diff changeset
461 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Ritor1
parents:
diff changeset
462 {
Ritor1
parents:
diff changeset
463 if (dsp_row != NULL)
Ritor1
parents:
diff changeset
464 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Ritor1
parents:
diff changeset
465
Ritor1
parents:
diff changeset
466 png_read_finish_row(png_ptr);
Ritor1
parents:
diff changeset
467 return;
Ritor1
parents:
diff changeset
468 }
Ritor1
parents:
diff changeset
469 break;
Ritor1
parents:
diff changeset
470
Ritor1
parents:
diff changeset
471 case 2:
Ritor1
parents:
diff changeset
472 if ((png_ptr->row_number & 0x07) != 4)
Ritor1
parents:
diff changeset
473 {
Ritor1
parents:
diff changeset
474 if (dsp_row != NULL && (png_ptr->row_number & 4))
Ritor1
parents:
diff changeset
475 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Ritor1
parents:
diff changeset
476
Ritor1
parents:
diff changeset
477 png_read_finish_row(png_ptr);
Ritor1
parents:
diff changeset
478 return;
Ritor1
parents:
diff changeset
479 }
Ritor1
parents:
diff changeset
480 break;
Ritor1
parents:
diff changeset
481
Ritor1
parents:
diff changeset
482 case 3:
Ritor1
parents:
diff changeset
483 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
Ritor1
parents:
diff changeset
484 {
Ritor1
parents:
diff changeset
485 if (dsp_row != NULL)
Ritor1
parents:
diff changeset
486 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Ritor1
parents:
diff changeset
487
Ritor1
parents:
diff changeset
488 png_read_finish_row(png_ptr);
Ritor1
parents:
diff changeset
489 return;
Ritor1
parents:
diff changeset
490 }
Ritor1
parents:
diff changeset
491 break;
Ritor1
parents:
diff changeset
492
Ritor1
parents:
diff changeset
493 case 4:
Ritor1
parents:
diff changeset
494 if ((png_ptr->row_number & 3) != 2)
Ritor1
parents:
diff changeset
495 {
Ritor1
parents:
diff changeset
496 if (dsp_row != NULL && (png_ptr->row_number & 2))
Ritor1
parents:
diff changeset
497 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Ritor1
parents:
diff changeset
498
Ritor1
parents:
diff changeset
499 png_read_finish_row(png_ptr);
Ritor1
parents:
diff changeset
500 return;
Ritor1
parents:
diff changeset
501 }
Ritor1
parents:
diff changeset
502 break;
Ritor1
parents:
diff changeset
503
Ritor1
parents:
diff changeset
504 case 5:
Ritor1
parents:
diff changeset
505 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
Ritor1
parents:
diff changeset
506 {
Ritor1
parents:
diff changeset
507 if (dsp_row != NULL)
Ritor1
parents:
diff changeset
508 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Ritor1
parents:
diff changeset
509
Ritor1
parents:
diff changeset
510 png_read_finish_row(png_ptr);
Ritor1
parents:
diff changeset
511 return;
Ritor1
parents:
diff changeset
512 }
Ritor1
parents:
diff changeset
513 break;
Ritor1
parents:
diff changeset
514
Ritor1
parents:
diff changeset
515 default:
Ritor1
parents:
diff changeset
516 case 6:
Ritor1
parents:
diff changeset
517 if (!(png_ptr->row_number & 1))
Ritor1
parents:
diff changeset
518 {
Ritor1
parents:
diff changeset
519 png_read_finish_row(png_ptr);
Ritor1
parents:
diff changeset
520 return;
Ritor1
parents:
diff changeset
521 }
Ritor1
parents:
diff changeset
522 break;
Ritor1
parents:
diff changeset
523 }
Ritor1
parents:
diff changeset
524 }
Ritor1
parents:
diff changeset
525 #endif
Ritor1
parents:
diff changeset
526
Ritor1
parents:
diff changeset
527 if (!(png_ptr->mode & PNG_HAVE_IDAT))
Ritor1
parents:
diff changeset
528 png_error(png_ptr, "Invalid attempt to read row data");
Ritor1
parents:
diff changeset
529
Ritor1
parents:
diff changeset
530 /* Fill the row with IDAT data: */
Ritor1
parents:
diff changeset
531 png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
Ritor1
parents:
diff changeset
532
Ritor1
parents:
diff changeset
533 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
Ritor1
parents:
diff changeset
534 {
Ritor1
parents:
diff changeset
535 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
Ritor1
parents:
diff changeset
536 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
Ritor1
parents:
diff changeset
537 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
Ritor1
parents:
diff changeset
538 else
Ritor1
parents:
diff changeset
539 png_error(png_ptr, "bad adaptive filter value");
Ritor1
parents:
diff changeset
540 }
Ritor1
parents:
diff changeset
541
Ritor1
parents:
diff changeset
542 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
Ritor1
parents:
diff changeset
543 * 1.5.6, while the buffer really is this big in current versions of libpng
Ritor1
parents:
diff changeset
544 * it may not be in the future, so this was changed just to copy the
Ritor1
parents:
diff changeset
545 * interlaced count:
Ritor1
parents:
diff changeset
546 */
Ritor1
parents:
diff changeset
547 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
Ritor1
parents:
diff changeset
548
Ritor1
parents:
diff changeset
549 #ifdef PNG_MNG_FEATURES_SUPPORTED
Ritor1
parents:
diff changeset
550 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Ritor1
parents:
diff changeset
551 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Ritor1
parents:
diff changeset
552 {
Ritor1
parents:
diff changeset
553 /* Intrapixel differencing */
Ritor1
parents:
diff changeset
554 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
Ritor1
parents:
diff changeset
555 }
Ritor1
parents:
diff changeset
556 #endif
Ritor1
parents:
diff changeset
557
Ritor1
parents:
diff changeset
558 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
Ritor1
parents:
diff changeset
559 if (png_ptr->transformations)
Ritor1
parents:
diff changeset
560 png_do_read_transformations(png_ptr, &row_info);
Ritor1
parents:
diff changeset
561 #endif
Ritor1
parents:
diff changeset
562
Ritor1
parents:
diff changeset
563 /* The transformed pixel depth should match the depth now in row_info. */
Ritor1
parents:
diff changeset
564 if (png_ptr->transformed_pixel_depth == 0)
Ritor1
parents:
diff changeset
565 {
Ritor1
parents:
diff changeset
566 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
Ritor1
parents:
diff changeset
567 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
Ritor1
parents:
diff changeset
568 png_error(png_ptr, "sequential row overflow");
Ritor1
parents:
diff changeset
569 }
Ritor1
parents:
diff changeset
570
Ritor1
parents:
diff changeset
571 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
Ritor1
parents:
diff changeset
572 png_error(png_ptr, "internal sequential row size calculation error");
Ritor1
parents:
diff changeset
573
Ritor1
parents:
diff changeset
574 #ifdef PNG_READ_INTERLACING_SUPPORTED
Ritor1
parents:
diff changeset
575 /* Blow up interlaced rows to full size */
Ritor1
parents:
diff changeset
576 if (png_ptr->interlaced &&
Ritor1
parents:
diff changeset
577 (png_ptr->transformations & PNG_INTERLACE))
Ritor1
parents:
diff changeset
578 {
Ritor1
parents:
diff changeset
579 if (png_ptr->pass < 6)
Ritor1
parents:
diff changeset
580 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
Ritor1
parents:
diff changeset
581 png_ptr->transformations);
Ritor1
parents:
diff changeset
582
Ritor1
parents:
diff changeset
583 if (dsp_row != NULL)
Ritor1
parents:
diff changeset
584 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Ritor1
parents:
diff changeset
585
Ritor1
parents:
diff changeset
586 if (row != NULL)
Ritor1
parents:
diff changeset
587 png_combine_row(png_ptr, row, 0/*row*/);
Ritor1
parents:
diff changeset
588 }
Ritor1
parents:
diff changeset
589
Ritor1
parents:
diff changeset
590 else
Ritor1
parents:
diff changeset
591 #endif
Ritor1
parents:
diff changeset
592 {
Ritor1
parents:
diff changeset
593 if (row != NULL)
Ritor1
parents:
diff changeset
594 png_combine_row(png_ptr, row, -1/*ignored*/);
Ritor1
parents:
diff changeset
595
Ritor1
parents:
diff changeset
596 if (dsp_row != NULL)
Ritor1
parents:
diff changeset
597 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
Ritor1
parents:
diff changeset
598 }
Ritor1
parents:
diff changeset
599 png_read_finish_row(png_ptr);
Ritor1
parents:
diff changeset
600
Ritor1
parents:
diff changeset
601 if (png_ptr->read_row_fn != NULL)
Ritor1
parents:
diff changeset
602 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Ritor1
parents:
diff changeset
603
Ritor1
parents:
diff changeset
604 }
Ritor1
parents:
diff changeset
605 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Ritor1
parents:
diff changeset
606
Ritor1
parents:
diff changeset
607 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Ritor1
parents:
diff changeset
608 /* Read one or more rows of image data. If the image is interlaced,
Ritor1
parents:
diff changeset
609 * and png_set_interlace_handling() has been called, the rows need to
Ritor1
parents:
diff changeset
610 * contain the contents of the rows from the previous pass. If the
Ritor1
parents:
diff changeset
611 * image has alpha or transparency, and png_handle_alpha()[*] has been
Ritor1
parents:
diff changeset
612 * called, the rows contents must be initialized to the contents of the
Ritor1
parents:
diff changeset
613 * screen.
Ritor1
parents:
diff changeset
614 *
Ritor1
parents:
diff changeset
615 * "row" holds the actual image, and pixels are placed in it
Ritor1
parents:
diff changeset
616 * as they arrive. If the image is displayed after each pass, it will
Ritor1
parents:
diff changeset
617 * appear to "sparkle" in. "display_row" can be used to display a
Ritor1
parents:
diff changeset
618 * "chunky" progressive image, with finer detail added as it becomes
Ritor1
parents:
diff changeset
619 * available. If you do not want this "chunky" display, you may pass
Ritor1
parents:
diff changeset
620 * NULL for display_row. If you do not want the sparkle display, and
Ritor1
parents:
diff changeset
621 * you have not called png_handle_alpha(), you may pass NULL for rows.
Ritor1
parents:
diff changeset
622 * If you have called png_handle_alpha(), and the image has either an
Ritor1
parents:
diff changeset
623 * alpha channel or a transparency chunk, you must provide a buffer for
Ritor1
parents:
diff changeset
624 * rows. In this case, you do not have to provide a display_row buffer
Ritor1
parents:
diff changeset
625 * also, but you may. If the image is not interlaced, or if you have
Ritor1
parents:
diff changeset
626 * not called png_set_interlace_handling(), the display_row buffer will
Ritor1
parents:
diff changeset
627 * be ignored, so pass NULL to it.
Ritor1
parents:
diff changeset
628 *
Ritor1
parents:
diff changeset
629 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Ritor1
parents:
diff changeset
630 */
Ritor1
parents:
diff changeset
631
Ritor1
parents:
diff changeset
632 void PNGAPI
Ritor1
parents:
diff changeset
633 png_read_rows(png_structrp png_ptr, png_bytepp row,
Ritor1
parents:
diff changeset
634 png_bytepp display_row, png_uint_32 num_rows)
Ritor1
parents:
diff changeset
635 {
Ritor1
parents:
diff changeset
636 png_uint_32 i;
Ritor1
parents:
diff changeset
637 png_bytepp rp;
Ritor1
parents:
diff changeset
638 png_bytepp dp;
Ritor1
parents:
diff changeset
639
Ritor1
parents:
diff changeset
640 png_debug(1, "in png_read_rows");
Ritor1
parents:
diff changeset
641
Ritor1
parents:
diff changeset
642 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
643 return;
Ritor1
parents:
diff changeset
644
Ritor1
parents:
diff changeset
645 rp = row;
Ritor1
parents:
diff changeset
646 dp = display_row;
Ritor1
parents:
diff changeset
647 if (rp != NULL && dp != NULL)
Ritor1
parents:
diff changeset
648 for (i = 0; i < num_rows; i++)
Ritor1
parents:
diff changeset
649 {
Ritor1
parents:
diff changeset
650 png_bytep rptr = *rp++;
Ritor1
parents:
diff changeset
651 png_bytep dptr = *dp++;
Ritor1
parents:
diff changeset
652
Ritor1
parents:
diff changeset
653 png_read_row(png_ptr, rptr, dptr);
Ritor1
parents:
diff changeset
654 }
Ritor1
parents:
diff changeset
655
Ritor1
parents:
diff changeset
656 else if (rp != NULL)
Ritor1
parents:
diff changeset
657 for (i = 0; i < num_rows; i++)
Ritor1
parents:
diff changeset
658 {
Ritor1
parents:
diff changeset
659 png_bytep rptr = *rp;
Ritor1
parents:
diff changeset
660 png_read_row(png_ptr, rptr, NULL);
Ritor1
parents:
diff changeset
661 rp++;
Ritor1
parents:
diff changeset
662 }
Ritor1
parents:
diff changeset
663
Ritor1
parents:
diff changeset
664 else if (dp != NULL)
Ritor1
parents:
diff changeset
665 for (i = 0; i < num_rows; i++)
Ritor1
parents:
diff changeset
666 {
Ritor1
parents:
diff changeset
667 png_bytep dptr = *dp;
Ritor1
parents:
diff changeset
668 png_read_row(png_ptr, NULL, dptr);
Ritor1
parents:
diff changeset
669 dp++;
Ritor1
parents:
diff changeset
670 }
Ritor1
parents:
diff changeset
671 }
Ritor1
parents:
diff changeset
672 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Ritor1
parents:
diff changeset
673
Ritor1
parents:
diff changeset
674 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Ritor1
parents:
diff changeset
675 /* Read the entire image. If the image has an alpha channel or a tRNS
Ritor1
parents:
diff changeset
676 * chunk, and you have called png_handle_alpha()[*], you will need to
Ritor1
parents:
diff changeset
677 * initialize the image to the current image that PNG will be overlaying.
Ritor1
parents:
diff changeset
678 * We set the num_rows again here, in case it was incorrectly set in
Ritor1
parents:
diff changeset
679 * png_read_start_row() by a call to png_read_update_info() or
Ritor1
parents:
diff changeset
680 * png_start_read_image() if png_set_interlace_handling() wasn't called
Ritor1
parents:
diff changeset
681 * prior to either of these functions like it should have been. You can
Ritor1
parents:
diff changeset
682 * only call this function once. If you desire to have an image for
Ritor1
parents:
diff changeset
683 * each pass of a interlaced image, use png_read_rows() instead.
Ritor1
parents:
diff changeset
684 *
Ritor1
parents:
diff changeset
685 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Ritor1
parents:
diff changeset
686 */
Ritor1
parents:
diff changeset
687 void PNGAPI
Ritor1
parents:
diff changeset
688 png_read_image(png_structrp png_ptr, png_bytepp image)
Ritor1
parents:
diff changeset
689 {
Ritor1
parents:
diff changeset
690 png_uint_32 i, image_height;
Ritor1
parents:
diff changeset
691 int pass, j;
Ritor1
parents:
diff changeset
692 png_bytepp rp;
Ritor1
parents:
diff changeset
693
Ritor1
parents:
diff changeset
694 png_debug(1, "in png_read_image");
Ritor1
parents:
diff changeset
695
Ritor1
parents:
diff changeset
696 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
697 return;
Ritor1
parents:
diff changeset
698
Ritor1
parents:
diff changeset
699 #ifdef PNG_READ_INTERLACING_SUPPORTED
Ritor1
parents:
diff changeset
700 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Ritor1
parents:
diff changeset
701 {
Ritor1
parents:
diff changeset
702 pass = png_set_interlace_handling(png_ptr);
Ritor1
parents:
diff changeset
703 /* And make sure transforms are initialized. */
Ritor1
parents:
diff changeset
704 png_start_read_image(png_ptr);
Ritor1
parents:
diff changeset
705 }
Ritor1
parents:
diff changeset
706 else
Ritor1
parents:
diff changeset
707 {
Ritor1
parents:
diff changeset
708 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
Ritor1
parents:
diff changeset
709 {
Ritor1
parents:
diff changeset
710 /* Caller called png_start_read_image or png_read_update_info without
Ritor1
parents:
diff changeset
711 * first turning on the PNG_INTERLACE transform. We can fix this here,
Ritor1
parents:
diff changeset
712 * but the caller should do it!
Ritor1
parents:
diff changeset
713 */
Ritor1
parents:
diff changeset
714 png_warning(png_ptr, "Interlace handling should be turned on when "
Ritor1
parents:
diff changeset
715 "using png_read_image");
Ritor1
parents:
diff changeset
716 /* Make sure this is set correctly */
Ritor1
parents:
diff changeset
717 png_ptr->num_rows = png_ptr->height;
Ritor1
parents:
diff changeset
718 }
Ritor1
parents:
diff changeset
719
Ritor1
parents:
diff changeset
720 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
Ritor1
parents:
diff changeset
721 * the above error case.
Ritor1
parents:
diff changeset
722 */
Ritor1
parents:
diff changeset
723 pass = png_set_interlace_handling(png_ptr);
Ritor1
parents:
diff changeset
724 }
Ritor1
parents:
diff changeset
725 #else
Ritor1
parents:
diff changeset
726 if (png_ptr->interlaced)
Ritor1
parents:
diff changeset
727 png_error(png_ptr,
Ritor1
parents:
diff changeset
728 "Cannot read interlaced image -- interlace handler disabled");
Ritor1
parents:
diff changeset
729
Ritor1
parents:
diff changeset
730 pass = 1;
Ritor1
parents:
diff changeset
731 #endif
Ritor1
parents:
diff changeset
732
Ritor1
parents:
diff changeset
733 image_height=png_ptr->height;
Ritor1
parents:
diff changeset
734
Ritor1
parents:
diff changeset
735 for (j = 0; j < pass; j++)
Ritor1
parents:
diff changeset
736 {
Ritor1
parents:
diff changeset
737 rp = image;
Ritor1
parents:
diff changeset
738 for (i = 0; i < image_height; i++)
Ritor1
parents:
diff changeset
739 {
Ritor1
parents:
diff changeset
740 png_read_row(png_ptr, *rp, NULL);
Ritor1
parents:
diff changeset
741 rp++;
Ritor1
parents:
diff changeset
742 }
Ritor1
parents:
diff changeset
743 }
Ritor1
parents:
diff changeset
744 }
Ritor1
parents:
diff changeset
745 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Ritor1
parents:
diff changeset
746
Ritor1
parents:
diff changeset
747 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Ritor1
parents:
diff changeset
748 /* Read the end of the PNG file. Will not read past the end of the
Ritor1
parents:
diff changeset
749 * file, will verify the end is accurate, and will read any comments
Ritor1
parents:
diff changeset
750 * or time information at the end of the file, if info is not NULL.
Ritor1
parents:
diff changeset
751 */
Ritor1
parents:
diff changeset
752 void PNGAPI
Ritor1
parents:
diff changeset
753 png_read_end(png_structrp png_ptr, png_inforp info_ptr)
Ritor1
parents:
diff changeset
754 {
Ritor1
parents:
diff changeset
755 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Ritor1
parents:
diff changeset
756 int keep;
Ritor1
parents:
diff changeset
757 #endif
Ritor1
parents:
diff changeset
758
Ritor1
parents:
diff changeset
759 png_debug(1, "in png_read_end");
Ritor1
parents:
diff changeset
760
Ritor1
parents:
diff changeset
761 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
762 return;
Ritor1
parents:
diff changeset
763
Ritor1
parents:
diff changeset
764 /* If png_read_end is called in the middle of reading the rows there may
Ritor1
parents:
diff changeset
765 * still be pending IDAT data and an owned zstream. Deal with this here.
Ritor1
parents:
diff changeset
766 */
Ritor1
parents:
diff changeset
767 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Ritor1
parents:
diff changeset
768 if (!png_chunk_unknown_handling(png_ptr, png_IDAT))
Ritor1
parents:
diff changeset
769 #endif
Ritor1
parents:
diff changeset
770 png_read_finish_IDAT(png_ptr);
Ritor1
parents:
diff changeset
771
Ritor1
parents:
diff changeset
772 #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
Ritor1
parents:
diff changeset
773 /* Report invalid palette index; added at libng-1.5.10 */
Ritor1
parents:
diff changeset
774 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Ritor1
parents:
diff changeset
775 png_ptr->num_palette_max > png_ptr->num_palette)
Ritor1
parents:
diff changeset
776 png_benign_error(png_ptr, "Read palette index exceeding num_palette");
Ritor1
parents:
diff changeset
777 #endif
Ritor1
parents:
diff changeset
778
Ritor1
parents:
diff changeset
779 do
Ritor1
parents:
diff changeset
780 {
Ritor1
parents:
diff changeset
781 png_uint_32 length = png_read_chunk_header(png_ptr);
Ritor1
parents:
diff changeset
782 png_uint_32 chunk_name = png_ptr->chunk_name;
Ritor1
parents:
diff changeset
783
Ritor1
parents:
diff changeset
784 if (chunk_name == png_IEND)
Ritor1
parents:
diff changeset
785 png_handle_IEND(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
786
Ritor1
parents:
diff changeset
787 else if (chunk_name == png_IHDR)
Ritor1
parents:
diff changeset
788 png_handle_IHDR(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
789
Ritor1
parents:
diff changeset
790 else if (info_ptr == NULL)
Ritor1
parents:
diff changeset
791 png_crc_finish(png_ptr, length);
Ritor1
parents:
diff changeset
792
Ritor1
parents:
diff changeset
793 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Ritor1
parents:
diff changeset
794 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
Ritor1
parents:
diff changeset
795 {
Ritor1
parents:
diff changeset
796 if (chunk_name == png_IDAT)
Ritor1
parents:
diff changeset
797 {
Ritor1
parents:
diff changeset
798 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Ritor1
parents:
diff changeset
799 png_benign_error(png_ptr, "Too many IDATs found");
Ritor1
parents:
diff changeset
800 }
Ritor1
parents:
diff changeset
801 png_handle_unknown(png_ptr, info_ptr, length, keep);
Ritor1
parents:
diff changeset
802 if (chunk_name == png_PLTE)
Ritor1
parents:
diff changeset
803 png_ptr->mode |= PNG_HAVE_PLTE;
Ritor1
parents:
diff changeset
804 }
Ritor1
parents:
diff changeset
805 #endif
Ritor1
parents:
diff changeset
806
Ritor1
parents:
diff changeset
807 else if (chunk_name == png_IDAT)
Ritor1
parents:
diff changeset
808 {
Ritor1
parents:
diff changeset
809 /* Zero length IDATs are legal after the last IDAT has been
Ritor1
parents:
diff changeset
810 * read, but not after other chunks have been read.
Ritor1
parents:
diff changeset
811 */
Ritor1
parents:
diff changeset
812 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Ritor1
parents:
diff changeset
813 png_benign_error(png_ptr, "Too many IDATs found");
Ritor1
parents:
diff changeset
814
Ritor1
parents:
diff changeset
815 png_crc_finish(png_ptr, length);
Ritor1
parents:
diff changeset
816 }
Ritor1
parents:
diff changeset
817 else if (chunk_name == png_PLTE)
Ritor1
parents:
diff changeset
818 png_handle_PLTE(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
819
Ritor1
parents:
diff changeset
820 #ifdef PNG_READ_bKGD_SUPPORTED
Ritor1
parents:
diff changeset
821 else if (chunk_name == png_bKGD)
Ritor1
parents:
diff changeset
822 png_handle_bKGD(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
823 #endif
Ritor1
parents:
diff changeset
824
Ritor1
parents:
diff changeset
825 #ifdef PNG_READ_cHRM_SUPPORTED
Ritor1
parents:
diff changeset
826 else if (chunk_name == png_cHRM)
Ritor1
parents:
diff changeset
827 png_handle_cHRM(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
828 #endif
Ritor1
parents:
diff changeset
829
Ritor1
parents:
diff changeset
830 #ifdef PNG_READ_gAMA_SUPPORTED
Ritor1
parents:
diff changeset
831 else if (chunk_name == png_gAMA)
Ritor1
parents:
diff changeset
832 png_handle_gAMA(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
833 #endif
Ritor1
parents:
diff changeset
834
Ritor1
parents:
diff changeset
835 #ifdef PNG_READ_hIST_SUPPORTED
Ritor1
parents:
diff changeset
836 else if (chunk_name == png_hIST)
Ritor1
parents:
diff changeset
837 png_handle_hIST(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
838 #endif
Ritor1
parents:
diff changeset
839
Ritor1
parents:
diff changeset
840 #ifdef PNG_READ_oFFs_SUPPORTED
Ritor1
parents:
diff changeset
841 else if (chunk_name == png_oFFs)
Ritor1
parents:
diff changeset
842 png_handle_oFFs(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
843 #endif
Ritor1
parents:
diff changeset
844
Ritor1
parents:
diff changeset
845 #ifdef PNG_READ_pCAL_SUPPORTED
Ritor1
parents:
diff changeset
846 else if (chunk_name == png_pCAL)
Ritor1
parents:
diff changeset
847 png_handle_pCAL(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
848 #endif
Ritor1
parents:
diff changeset
849
Ritor1
parents:
diff changeset
850 #ifdef PNG_READ_sCAL_SUPPORTED
Ritor1
parents:
diff changeset
851 else if (chunk_name == png_sCAL)
Ritor1
parents:
diff changeset
852 png_handle_sCAL(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
853 #endif
Ritor1
parents:
diff changeset
854
Ritor1
parents:
diff changeset
855 #ifdef PNG_READ_pHYs_SUPPORTED
Ritor1
parents:
diff changeset
856 else if (chunk_name == png_pHYs)
Ritor1
parents:
diff changeset
857 png_handle_pHYs(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
858 #endif
Ritor1
parents:
diff changeset
859
Ritor1
parents:
diff changeset
860 #ifdef PNG_READ_sBIT_SUPPORTED
Ritor1
parents:
diff changeset
861 else if (chunk_name == png_sBIT)
Ritor1
parents:
diff changeset
862 png_handle_sBIT(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
863 #endif
Ritor1
parents:
diff changeset
864
Ritor1
parents:
diff changeset
865 #ifdef PNG_READ_sRGB_SUPPORTED
Ritor1
parents:
diff changeset
866 else if (chunk_name == png_sRGB)
Ritor1
parents:
diff changeset
867 png_handle_sRGB(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
868 #endif
Ritor1
parents:
diff changeset
869
Ritor1
parents:
diff changeset
870 #ifdef PNG_READ_iCCP_SUPPORTED
Ritor1
parents:
diff changeset
871 else if (chunk_name == png_iCCP)
Ritor1
parents:
diff changeset
872 png_handle_iCCP(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
873 #endif
Ritor1
parents:
diff changeset
874
Ritor1
parents:
diff changeset
875 #ifdef PNG_READ_sPLT_SUPPORTED
Ritor1
parents:
diff changeset
876 else if (chunk_name == png_sPLT)
Ritor1
parents:
diff changeset
877 png_handle_sPLT(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
878 #endif
Ritor1
parents:
diff changeset
879
Ritor1
parents:
diff changeset
880 #ifdef PNG_READ_tEXt_SUPPORTED
Ritor1
parents:
diff changeset
881 else if (chunk_name == png_tEXt)
Ritor1
parents:
diff changeset
882 png_handle_tEXt(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
883 #endif
Ritor1
parents:
diff changeset
884
Ritor1
parents:
diff changeset
885 #ifdef PNG_READ_tIME_SUPPORTED
Ritor1
parents:
diff changeset
886 else if (chunk_name == png_tIME)
Ritor1
parents:
diff changeset
887 png_handle_tIME(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
888 #endif
Ritor1
parents:
diff changeset
889
Ritor1
parents:
diff changeset
890 #ifdef PNG_READ_tRNS_SUPPORTED
Ritor1
parents:
diff changeset
891 else if (chunk_name == png_tRNS)
Ritor1
parents:
diff changeset
892 png_handle_tRNS(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
893 #endif
Ritor1
parents:
diff changeset
894
Ritor1
parents:
diff changeset
895 #ifdef PNG_READ_zTXt_SUPPORTED
Ritor1
parents:
diff changeset
896 else if (chunk_name == png_zTXt)
Ritor1
parents:
diff changeset
897 png_handle_zTXt(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
898 #endif
Ritor1
parents:
diff changeset
899
Ritor1
parents:
diff changeset
900 #ifdef PNG_READ_iTXt_SUPPORTED
Ritor1
parents:
diff changeset
901 else if (chunk_name == png_iTXt)
Ritor1
parents:
diff changeset
902 png_handle_iTXt(png_ptr, info_ptr, length);
Ritor1
parents:
diff changeset
903 #endif
Ritor1
parents:
diff changeset
904
Ritor1
parents:
diff changeset
905 else
Ritor1
parents:
diff changeset
906 png_handle_unknown(png_ptr, info_ptr, length,
Ritor1
parents:
diff changeset
907 PNG_HANDLE_CHUNK_AS_DEFAULT);
Ritor1
parents:
diff changeset
908 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Ritor1
parents:
diff changeset
909 }
Ritor1
parents:
diff changeset
910 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Ritor1
parents:
diff changeset
911
Ritor1
parents:
diff changeset
912 /* Free all memory used in the read struct */
Ritor1
parents:
diff changeset
913 static void
Ritor1
parents:
diff changeset
914 png_read_destroy(png_structrp png_ptr)
Ritor1
parents:
diff changeset
915 {
Ritor1
parents:
diff changeset
916 png_debug(1, "in png_read_destroy");
Ritor1
parents:
diff changeset
917
Ritor1
parents:
diff changeset
918 #ifdef PNG_READ_GAMMA_SUPPORTED
Ritor1
parents:
diff changeset
919 png_destroy_gamma_table(png_ptr);
Ritor1
parents:
diff changeset
920 #endif
Ritor1
parents:
diff changeset
921
Ritor1
parents:
diff changeset
922 png_free(png_ptr, png_ptr->big_row_buf);
Ritor1
parents:
diff changeset
923 png_free(png_ptr, png_ptr->big_prev_row);
Ritor1
parents:
diff changeset
924 png_free(png_ptr, png_ptr->read_buffer);
Ritor1
parents:
diff changeset
925
Ritor1
parents:
diff changeset
926 #ifdef PNG_READ_QUANTIZE_SUPPORTED
Ritor1
parents:
diff changeset
927 png_free(png_ptr, png_ptr->palette_lookup);
Ritor1
parents:
diff changeset
928 png_free(png_ptr, png_ptr->quantize_index);
Ritor1
parents:
diff changeset
929 #endif
Ritor1
parents:
diff changeset
930
Ritor1
parents:
diff changeset
931 if (png_ptr->free_me & PNG_FREE_PLTE)
Ritor1
parents:
diff changeset
932 png_zfree(png_ptr, png_ptr->palette);
Ritor1
parents:
diff changeset
933 png_ptr->free_me &= ~PNG_FREE_PLTE;
Ritor1
parents:
diff changeset
934
Ritor1
parents:
diff changeset
935 #if defined(PNG_tRNS_SUPPORTED) || \
Ritor1
parents:
diff changeset
936 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Ritor1
parents:
diff changeset
937 if (png_ptr->free_me & PNG_FREE_TRNS)
Ritor1
parents:
diff changeset
938 png_free(png_ptr, png_ptr->trans_alpha);
Ritor1
parents:
diff changeset
939 png_ptr->free_me &= ~PNG_FREE_TRNS;
Ritor1
parents:
diff changeset
940 #endif
Ritor1
parents:
diff changeset
941
Ritor1
parents:
diff changeset
942 inflateEnd(&png_ptr->zstream);
Ritor1
parents:
diff changeset
943
Ritor1
parents:
diff changeset
944 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Ritor1
parents:
diff changeset
945 png_free(png_ptr, png_ptr->save_buffer);
Ritor1
parents:
diff changeset
946 #endif
Ritor1
parents:
diff changeset
947
Ritor1
parents:
diff changeset
948 #if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) &&\
Ritor1
parents:
diff changeset
949 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
Ritor1
parents:
diff changeset
950 png_free(png_ptr, png_ptr->unknown_chunk.data);
Ritor1
parents:
diff changeset
951 #endif
Ritor1
parents:
diff changeset
952
Ritor1
parents:
diff changeset
953 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
Ritor1
parents:
diff changeset
954 png_free(png_ptr, png_ptr->chunk_list);
Ritor1
parents:
diff changeset
955 #endif
Ritor1
parents:
diff changeset
956
Ritor1
parents:
diff changeset
957 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
Ritor1
parents:
diff changeset
958 * callbacks are still set at this point. They are required to complete the
Ritor1
parents:
diff changeset
959 * destruction of the png_struct itself.
Ritor1
parents:
diff changeset
960 */
Ritor1
parents:
diff changeset
961 }
Ritor1
parents:
diff changeset
962
Ritor1
parents:
diff changeset
963 /* Free all memory used by the read */
Ritor1
parents:
diff changeset
964 void PNGAPI
Ritor1
parents:
diff changeset
965 png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Ritor1
parents:
diff changeset
966 png_infopp end_info_ptr_ptr)
Ritor1
parents:
diff changeset
967 {
Ritor1
parents:
diff changeset
968 png_structrp png_ptr = NULL;
Ritor1
parents:
diff changeset
969
Ritor1
parents:
diff changeset
970 png_debug(1, "in png_destroy_read_struct");
Ritor1
parents:
diff changeset
971
Ritor1
parents:
diff changeset
972 if (png_ptr_ptr != NULL)
Ritor1
parents:
diff changeset
973 png_ptr = *png_ptr_ptr;
Ritor1
parents:
diff changeset
974
Ritor1
parents:
diff changeset
975 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
976 return;
Ritor1
parents:
diff changeset
977
Ritor1
parents:
diff changeset
978 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
Ritor1
parents:
diff changeset
979 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
Ritor1
parents:
diff changeset
980 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
Ritor1
parents:
diff changeset
981 */
Ritor1
parents:
diff changeset
982 png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
Ritor1
parents:
diff changeset
983 png_destroy_info_struct(png_ptr, info_ptr_ptr);
Ritor1
parents:
diff changeset
984
Ritor1
parents:
diff changeset
985 *png_ptr_ptr = NULL;
Ritor1
parents:
diff changeset
986 png_read_destroy(png_ptr);
Ritor1
parents:
diff changeset
987 png_destroy_png_struct(png_ptr);
Ritor1
parents:
diff changeset
988 }
Ritor1
parents:
diff changeset
989
Ritor1
parents:
diff changeset
990 void PNGAPI
Ritor1
parents:
diff changeset
991 png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
Ritor1
parents:
diff changeset
992 {
Ritor1
parents:
diff changeset
993 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
994 return;
Ritor1
parents:
diff changeset
995
Ritor1
parents:
diff changeset
996 png_ptr->read_row_fn = read_row_fn;
Ritor1
parents:
diff changeset
997 }
Ritor1
parents:
diff changeset
998
Ritor1
parents:
diff changeset
999
Ritor1
parents:
diff changeset
1000 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Ritor1
parents:
diff changeset
1001 #ifdef PNG_INFO_IMAGE_SUPPORTED
Ritor1
parents:
diff changeset
1002 void PNGAPI
Ritor1
parents:
diff changeset
1003 png_read_png(png_structrp png_ptr, png_inforp info_ptr,
Ritor1
parents:
diff changeset
1004 int transforms,
Ritor1
parents:
diff changeset
1005 voidp params)
Ritor1
parents:
diff changeset
1006 {
Ritor1
parents:
diff changeset
1007 if (png_ptr == NULL || info_ptr == NULL)
Ritor1
parents:
diff changeset
1008 return;
Ritor1
parents:
diff changeset
1009
Ritor1
parents:
diff changeset
1010 /* png_read_info() gives us all of the information from the
Ritor1
parents:
diff changeset
1011 * PNG file before the first IDAT (image data chunk).
Ritor1
parents:
diff changeset
1012 */
Ritor1
parents:
diff changeset
1013 png_read_info(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
1014 if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
Ritor1
parents:
diff changeset
1015 png_error(png_ptr, "Image is too high to process with png_read_png()");
Ritor1
parents:
diff changeset
1016
Ritor1
parents:
diff changeset
1017 /* -------------- image transformations start here ------------------- */
Ritor1
parents:
diff changeset
1018 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
Ritor1
parents:
diff changeset
1019 * is not implemented. This will only happen in de-configured (non-default)
Ritor1
parents:
diff changeset
1020 * libpng builds. The results can be unexpected - png_read_png may return
Ritor1
parents:
diff changeset
1021 * short or mal-formed rows because the transform is skipped.
Ritor1
parents:
diff changeset
1022 */
Ritor1
parents:
diff changeset
1023
Ritor1
parents:
diff changeset
1024 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
Ritor1
parents:
diff changeset
1025 */
Ritor1
parents:
diff changeset
1026 if (transforms & PNG_TRANSFORM_SCALE_16)
Ritor1
parents:
diff changeset
1027 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
Ritor1
parents:
diff changeset
1028 * did in earlier versions, while "scale_16" is now more accurate.
Ritor1
parents:
diff changeset
1029 */
Ritor1
parents:
diff changeset
1030 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Ritor1
parents:
diff changeset
1031 png_set_scale_16(png_ptr);
Ritor1
parents:
diff changeset
1032 #else
Ritor1
parents:
diff changeset
1033 png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
Ritor1
parents:
diff changeset
1034 #endif
Ritor1
parents:
diff changeset
1035
Ritor1
parents:
diff changeset
1036 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
Ritor1
parents:
diff changeset
1037 * latter by doing SCALE first. This is ok and allows apps not to check for
Ritor1
parents:
diff changeset
1038 * which is supported to get the right answer.
Ritor1
parents:
diff changeset
1039 */
Ritor1
parents:
diff changeset
1040 if (transforms & PNG_TRANSFORM_STRIP_16)
Ritor1
parents:
diff changeset
1041 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
Ritor1
parents:
diff changeset
1042 png_set_strip_16(png_ptr);
Ritor1
parents:
diff changeset
1043 #else
Ritor1
parents:
diff changeset
1044 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
Ritor1
parents:
diff changeset
1045 #endif
Ritor1
parents:
diff changeset
1046
Ritor1
parents:
diff changeset
1047 /* Strip alpha bytes from the input data without combining with
Ritor1
parents:
diff changeset
1048 * the background (not recommended).
Ritor1
parents:
diff changeset
1049 */
Ritor1
parents:
diff changeset
1050 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Ritor1
parents:
diff changeset
1051 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Ritor1
parents:
diff changeset
1052 png_set_strip_alpha(png_ptr);
Ritor1
parents:
diff changeset
1053 #else
Ritor1
parents:
diff changeset
1054 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
Ritor1
parents:
diff changeset
1055 #endif
Ritor1
parents:
diff changeset
1056
Ritor1
parents:
diff changeset
1057 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Ritor1
parents:
diff changeset
1058 * byte into separate bytes (useful for paletted and grayscale images).
Ritor1
parents:
diff changeset
1059 */
Ritor1
parents:
diff changeset
1060 if (transforms & PNG_TRANSFORM_PACKING)
Ritor1
parents:
diff changeset
1061 #ifdef PNG_READ_PACK_SUPPORTED
Ritor1
parents:
diff changeset
1062 png_set_packing(png_ptr);
Ritor1
parents:
diff changeset
1063 #else
Ritor1
parents:
diff changeset
1064 png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
Ritor1
parents:
diff changeset
1065 #endif
Ritor1
parents:
diff changeset
1066
Ritor1
parents:
diff changeset
1067 /* Change the order of packed pixels to least significant bit first
Ritor1
parents:
diff changeset
1068 * (not useful if you are using png_set_packing).
Ritor1
parents:
diff changeset
1069 */
Ritor1
parents:
diff changeset
1070 if (transforms & PNG_TRANSFORM_PACKSWAP)
Ritor1
parents:
diff changeset
1071 #ifdef PNG_READ_PACKSWAP_SUPPORTED
Ritor1
parents:
diff changeset
1072 png_set_packswap(png_ptr);
Ritor1
parents:
diff changeset
1073 #else
Ritor1
parents:
diff changeset
1074 png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
Ritor1
parents:
diff changeset
1075 #endif
Ritor1
parents:
diff changeset
1076
Ritor1
parents:
diff changeset
1077 /* Expand paletted colors into true RGB triplets
Ritor1
parents:
diff changeset
1078 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
Ritor1
parents:
diff changeset
1079 * Expand paletted or RGB images with transparency to full alpha
Ritor1
parents:
diff changeset
1080 * channels so the data will be available as RGBA quartets.
Ritor1
parents:
diff changeset
1081 */
Ritor1
parents:
diff changeset
1082 if (transforms & PNG_TRANSFORM_EXPAND)
Ritor1
parents:
diff changeset
1083 #ifdef PNG_READ_EXPAND_SUPPORTED
Ritor1
parents:
diff changeset
1084 png_set_expand(png_ptr);
Ritor1
parents:
diff changeset
1085 #else
Ritor1
parents:
diff changeset
1086 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
Ritor1
parents:
diff changeset
1087 #endif
Ritor1
parents:
diff changeset
1088
Ritor1
parents:
diff changeset
1089 /* We don't handle background color or gamma transformation or quantizing.
Ritor1
parents:
diff changeset
1090 */
Ritor1
parents:
diff changeset
1091
Ritor1
parents:
diff changeset
1092 /* Invert monochrome files to have 0 as white and 1 as black
Ritor1
parents:
diff changeset
1093 */
Ritor1
parents:
diff changeset
1094 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Ritor1
parents:
diff changeset
1095 #ifdef PNG_READ_INVERT_SUPPORTED
Ritor1
parents:
diff changeset
1096 png_set_invert_mono(png_ptr);
Ritor1
parents:
diff changeset
1097 #else
Ritor1
parents:
diff changeset
1098 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
Ritor1
parents:
diff changeset
1099 #endif
Ritor1
parents:
diff changeset
1100
Ritor1
parents:
diff changeset
1101 /* If you want to shift the pixel values from the range [0,255] or
Ritor1
parents:
diff changeset
1102 * [0,65535] to the original [0,7] or [0,31], or whatever range the
Ritor1
parents:
diff changeset
1103 * colors were originally in:
Ritor1
parents:
diff changeset
1104 */
Ritor1
parents:
diff changeset
1105 if (transforms & PNG_TRANSFORM_SHIFT)
Ritor1
parents:
diff changeset
1106 #ifdef PNG_READ_SHIFT_SUPPORTED
Ritor1
parents:
diff changeset
1107 if (info_ptr->valid & PNG_INFO_sBIT)
Ritor1
parents:
diff changeset
1108 png_set_shift(png_ptr, &info_ptr->sig_bit);
Ritor1
parents:
diff changeset
1109 #else
Ritor1
parents:
diff changeset
1110 png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
Ritor1
parents:
diff changeset
1111 #endif
Ritor1
parents:
diff changeset
1112
Ritor1
parents:
diff changeset
1113 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Ritor1
parents:
diff changeset
1114 if (transforms & PNG_TRANSFORM_BGR)
Ritor1
parents:
diff changeset
1115 #ifdef PNG_READ_BGR_SUPPORTED
Ritor1
parents:
diff changeset
1116 png_set_bgr(png_ptr);
Ritor1
parents:
diff changeset
1117 #else
Ritor1
parents:
diff changeset
1118 png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
Ritor1
parents:
diff changeset
1119 #endif
Ritor1
parents:
diff changeset
1120
Ritor1
parents:
diff changeset
1121 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Ritor1
parents:
diff changeset
1122 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Ritor1
parents:
diff changeset
1123 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Ritor1
parents:
diff changeset
1124 png_set_swap_alpha(png_ptr);
Ritor1
parents:
diff changeset
1125 #else
Ritor1
parents:
diff changeset
1126 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
Ritor1
parents:
diff changeset
1127 #endif
Ritor1
parents:
diff changeset
1128
Ritor1
parents:
diff changeset
1129 /* Swap bytes of 16-bit files to least significant byte first */
Ritor1
parents:
diff changeset
1130 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Ritor1
parents:
diff changeset
1131 #ifdef PNG_READ_SWAP_SUPPORTED
Ritor1
parents:
diff changeset
1132 png_set_swap(png_ptr);
Ritor1
parents:
diff changeset
1133 #else
Ritor1
parents:
diff changeset
1134 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
Ritor1
parents:
diff changeset
1135 #endif
Ritor1
parents:
diff changeset
1136
Ritor1
parents:
diff changeset
1137 /* Added at libpng-1.2.41 */
Ritor1
parents:
diff changeset
1138 /* Invert the alpha channel from opacity to transparency */
Ritor1
parents:
diff changeset
1139 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Ritor1
parents:
diff changeset
1140 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Ritor1
parents:
diff changeset
1141 png_set_invert_alpha(png_ptr);
Ritor1
parents:
diff changeset
1142 #else
Ritor1
parents:
diff changeset
1143 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
Ritor1
parents:
diff changeset
1144 #endif
Ritor1
parents:
diff changeset
1145
Ritor1
parents:
diff changeset
1146 /* Added at libpng-1.2.41 */
Ritor1
parents:
diff changeset
1147 /* Expand grayscale image to RGB */
Ritor1
parents:
diff changeset
1148 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
Ritor1
parents:
diff changeset
1149 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Ritor1
parents:
diff changeset
1150 png_set_gray_to_rgb(png_ptr);
Ritor1
parents:
diff changeset
1151 #else
Ritor1
parents:
diff changeset
1152 png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
Ritor1
parents:
diff changeset
1153 #endif
Ritor1
parents:
diff changeset
1154
Ritor1
parents:
diff changeset
1155 /* Added at libpng-1.5.4 */
Ritor1
parents:
diff changeset
1156 if (transforms & PNG_TRANSFORM_EXPAND_16)
Ritor1
parents:
diff changeset
1157 #ifdef PNG_READ_EXPAND_16_SUPPORTED
Ritor1
parents:
diff changeset
1158 png_set_expand_16(png_ptr);
Ritor1
parents:
diff changeset
1159 #else
Ritor1
parents:
diff changeset
1160 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
Ritor1
parents:
diff changeset
1161 #endif
Ritor1
parents:
diff changeset
1162
Ritor1
parents:
diff changeset
1163 /* We don't handle adding filler bytes */
Ritor1
parents:
diff changeset
1164
Ritor1
parents:
diff changeset
1165 /* We use png_read_image and rely on that for interlace handling, but we also
Ritor1
parents:
diff changeset
1166 * call png_read_update_info therefore must turn on interlace handling now:
Ritor1
parents:
diff changeset
1167 */
Ritor1
parents:
diff changeset
1168 (void)png_set_interlace_handling(png_ptr);
Ritor1
parents:
diff changeset
1169
Ritor1
parents:
diff changeset
1170 /* Optional call to gamma correct and add the background to the palette
Ritor1
parents:
diff changeset
1171 * and update info structure. REQUIRED if you are expecting libpng to
Ritor1
parents:
diff changeset
1172 * update the palette for you (i.e., you selected such a transform above).
Ritor1
parents:
diff changeset
1173 */
Ritor1
parents:
diff changeset
1174 png_read_update_info(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
1175
Ritor1
parents:
diff changeset
1176 /* -------------- image transformations end here ------------------- */
Ritor1
parents:
diff changeset
1177
Ritor1
parents:
diff changeset
1178 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Ritor1
parents:
diff changeset
1179 if (info_ptr->row_pointers == NULL)
Ritor1
parents:
diff changeset
1180 {
Ritor1
parents:
diff changeset
1181 png_uint_32 iptr;
Ritor1
parents:
diff changeset
1182
Ritor1
parents:
diff changeset
1183 info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
Ritor1
parents:
diff changeset
1184 info_ptr->height * (sizeof (png_bytep))));
Ritor1
parents:
diff changeset
1185
Ritor1
parents:
diff changeset
1186 for (iptr=0; iptr<info_ptr->height; iptr++)
Ritor1
parents:
diff changeset
1187 info_ptr->row_pointers[iptr] = NULL;
Ritor1
parents:
diff changeset
1188
Ritor1
parents:
diff changeset
1189 info_ptr->free_me |= PNG_FREE_ROWS;
Ritor1
parents:
diff changeset
1190
Ritor1
parents:
diff changeset
1191 for (iptr = 0; iptr < info_ptr->height; iptr++)
Ritor1
parents:
diff changeset
1192 info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
Ritor1
parents:
diff changeset
1193 png_malloc(png_ptr, info_ptr->rowbytes));
Ritor1
parents:
diff changeset
1194 }
Ritor1
parents:
diff changeset
1195
Ritor1
parents:
diff changeset
1196 png_read_image(png_ptr, info_ptr->row_pointers);
Ritor1
parents:
diff changeset
1197 info_ptr->valid |= PNG_INFO_IDAT;
Ritor1
parents:
diff changeset
1198
Ritor1
parents:
diff changeset
1199 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Ritor1
parents:
diff changeset
1200 png_read_end(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
1201
Ritor1
parents:
diff changeset
1202 PNG_UNUSED(params)
Ritor1
parents:
diff changeset
1203 }
Ritor1
parents:
diff changeset
1204 #endif /* PNG_INFO_IMAGE_SUPPORTED */
Ritor1
parents:
diff changeset
1205 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Ritor1
parents:
diff changeset
1206
Ritor1
parents:
diff changeset
1207 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED
Ritor1
parents:
diff changeset
1208 /* SIMPLIFIED READ
Ritor1
parents:
diff changeset
1209 *
Ritor1
parents:
diff changeset
1210 * This code currently relies on the sequential reader, though it could easily
Ritor1
parents:
diff changeset
1211 * be made to work with the progressive one.
Ritor1
parents:
diff changeset
1212 */
Ritor1
parents:
diff changeset
1213 /* Arguments to png_image_finish_read: */
Ritor1
parents:
diff changeset
1214
Ritor1
parents:
diff changeset
1215 /* Encoding of PNG data (used by the color-map code) */
Ritor1
parents:
diff changeset
1216 # define P_NOTSET 0 /* File encoding not yet known */
Ritor1
parents:
diff changeset
1217 # define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
Ritor1
parents:
diff changeset
1218 # define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
Ritor1
parents:
diff changeset
1219 # define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
Ritor1
parents:
diff changeset
1220 # define P_LINEAR8 4 /* 8-bit linear: only from a file value */
Ritor1
parents:
diff changeset
1221
Ritor1
parents:
diff changeset
1222 /* Color-map processing: after libpng has run on the PNG image further
Ritor1
parents:
diff changeset
1223 * processing may be needed to conver the data to color-map indicies.
Ritor1
parents:
diff changeset
1224 */
Ritor1
parents:
diff changeset
1225 #define PNG_CMAP_NONE 0
Ritor1
parents:
diff changeset
1226 #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
Ritor1
parents:
diff changeset
1227 #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
Ritor1
parents:
diff changeset
1228 #define PNG_CMAP_RGB 3 /* Process RGB data */
Ritor1
parents:
diff changeset
1229 #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
Ritor1
parents:
diff changeset
1230
Ritor1
parents:
diff changeset
1231 /* The following document where the background is for each processing case. */
Ritor1
parents:
diff changeset
1232 #define PNG_CMAP_NONE_BACKGROUND 256
Ritor1
parents:
diff changeset
1233 #define PNG_CMAP_GA_BACKGROUND 231
Ritor1
parents:
diff changeset
1234 #define PNG_CMAP_TRANS_BACKGROUND 254
Ritor1
parents:
diff changeset
1235 #define PNG_CMAP_RGB_BACKGROUND 256
Ritor1
parents:
diff changeset
1236 #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
Ritor1
parents:
diff changeset
1237
Ritor1
parents:
diff changeset
1238 typedef struct
Ritor1
parents:
diff changeset
1239 {
Ritor1
parents:
diff changeset
1240 /* Arguments: */
Ritor1
parents:
diff changeset
1241 png_imagep image;
Ritor1
parents:
diff changeset
1242 png_voidp buffer;
Ritor1
parents:
diff changeset
1243 png_int_32 row_stride;
Ritor1
parents:
diff changeset
1244 png_voidp colormap;
Ritor1
parents:
diff changeset
1245 png_const_colorp background;
Ritor1
parents:
diff changeset
1246 /* Local variables: */
Ritor1
parents:
diff changeset
1247 png_voidp local_row;
Ritor1
parents:
diff changeset
1248 png_voidp first_row;
Ritor1
parents:
diff changeset
1249 ptrdiff_t row_bytes; /* step between rows */
Ritor1
parents:
diff changeset
1250 int file_encoding; /* E_ values above */
Ritor1
parents:
diff changeset
1251 png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
Ritor1
parents:
diff changeset
1252 int colormap_processing; /* PNG_CMAP_ values above */
Ritor1
parents:
diff changeset
1253 } png_image_read_control;
Ritor1
parents:
diff changeset
1254
Ritor1
parents:
diff changeset
1255 /* Do all the *safe* initialization - 'safe' means that png_error won't be
Ritor1
parents:
diff changeset
1256 * called, so setting up the jmp_buf is not required. This means that anything
Ritor1
parents:
diff changeset
1257 * called from here must *not* call png_malloc - it has to call png_malloc_warn
Ritor1
parents:
diff changeset
1258 * instead so that control is returned safely back to this routine.
Ritor1
parents:
diff changeset
1259 */
Ritor1
parents:
diff changeset
1260 static int
Ritor1
parents:
diff changeset
1261 png_image_read_init(png_imagep image)
Ritor1
parents:
diff changeset
1262 {
Ritor1
parents:
diff changeset
1263 if (image->opaque == NULL)
Ritor1
parents:
diff changeset
1264 {
Ritor1
parents:
diff changeset
1265 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
Ritor1
parents:
diff changeset
1266 png_safe_error, png_safe_warning);
Ritor1
parents:
diff changeset
1267
Ritor1
parents:
diff changeset
1268 /* And set the rest of the structure to NULL to ensure that the various
Ritor1
parents:
diff changeset
1269 * fields are consistent.
Ritor1
parents:
diff changeset
1270 */
Ritor1
parents:
diff changeset
1271 memset(image, 0, (sizeof *image));
Ritor1
parents:
diff changeset
1272 image->version = PNG_IMAGE_VERSION;
Ritor1
parents:
diff changeset
1273
Ritor1
parents:
diff changeset
1274 if (png_ptr != NULL)
Ritor1
parents:
diff changeset
1275 {
Ritor1
parents:
diff changeset
1276 png_infop info_ptr = png_create_info_struct(png_ptr);
Ritor1
parents:
diff changeset
1277
Ritor1
parents:
diff changeset
1278 if (info_ptr != NULL)
Ritor1
parents:
diff changeset
1279 {
Ritor1
parents:
diff changeset
1280 png_controlp control = png_voidcast(png_controlp,
Ritor1
parents:
diff changeset
1281 png_malloc_warn(png_ptr, (sizeof *control)));
Ritor1
parents:
diff changeset
1282
Ritor1
parents:
diff changeset
1283 if (control != NULL)
Ritor1
parents:
diff changeset
1284 {
Ritor1
parents:
diff changeset
1285 memset(control, 0, (sizeof *control));
Ritor1
parents:
diff changeset
1286
Ritor1
parents:
diff changeset
1287 control->png_ptr = png_ptr;
Ritor1
parents:
diff changeset
1288 control->info_ptr = info_ptr;
Ritor1
parents:
diff changeset
1289 control->for_write = 0;
Ritor1
parents:
diff changeset
1290
Ritor1
parents:
diff changeset
1291 image->opaque = control;
Ritor1
parents:
diff changeset
1292 return 1;
Ritor1
parents:
diff changeset
1293 }
Ritor1
parents:
diff changeset
1294
Ritor1
parents:
diff changeset
1295 /* Error clean up */
Ritor1
parents:
diff changeset
1296 png_destroy_info_struct(png_ptr, &info_ptr);
Ritor1
parents:
diff changeset
1297 }
Ritor1
parents:
diff changeset
1298
Ritor1
parents:
diff changeset
1299 png_destroy_read_struct(&png_ptr, NULL, NULL);
Ritor1
parents:
diff changeset
1300 }
Ritor1
parents:
diff changeset
1301
Ritor1
parents:
diff changeset
1302 return png_image_error(image, "png_image_read: out of memory");
Ritor1
parents:
diff changeset
1303 }
Ritor1
parents:
diff changeset
1304
Ritor1
parents:
diff changeset
1305 return png_image_error(image, "png_image_read: opaque pointer not NULL");
Ritor1
parents:
diff changeset
1306 }
Ritor1
parents:
diff changeset
1307
Ritor1
parents:
diff changeset
1308 /* Utility to find the base format of a PNG file from a png_struct. */
Ritor1
parents:
diff changeset
1309 static png_uint_32
Ritor1
parents:
diff changeset
1310 png_image_format(png_structrp png_ptr)
Ritor1
parents:
diff changeset
1311 {
Ritor1
parents:
diff changeset
1312 png_uint_32 format = 0;
Ritor1
parents:
diff changeset
1313
Ritor1
parents:
diff changeset
1314 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
Ritor1
parents:
diff changeset
1315 format |= PNG_FORMAT_FLAG_COLOR;
Ritor1
parents:
diff changeset
1316
Ritor1
parents:
diff changeset
1317 if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
Ritor1
parents:
diff changeset
1318 format |= PNG_FORMAT_FLAG_ALPHA;
Ritor1
parents:
diff changeset
1319
Ritor1
parents:
diff changeset
1320 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
Ritor1
parents:
diff changeset
1321 * sets the png_struct fields; that's all we are interested in here. The
Ritor1
parents:
diff changeset
1322 * precise interaction with an app call to png_set_tRNS and PNG file reading
Ritor1
parents:
diff changeset
1323 * is unclear.
Ritor1
parents:
diff changeset
1324 */
Ritor1
parents:
diff changeset
1325 else if (png_ptr->num_trans > 0)
Ritor1
parents:
diff changeset
1326 format |= PNG_FORMAT_FLAG_ALPHA;
Ritor1
parents:
diff changeset
1327
Ritor1
parents:
diff changeset
1328 if (png_ptr->bit_depth == 16)
Ritor1
parents:
diff changeset
1329 format |= PNG_FORMAT_FLAG_LINEAR;
Ritor1
parents:
diff changeset
1330
Ritor1
parents:
diff changeset
1331 if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE)
Ritor1
parents:
diff changeset
1332 format |= PNG_FORMAT_FLAG_COLORMAP;
Ritor1
parents:
diff changeset
1333
Ritor1
parents:
diff changeset
1334 return format;
Ritor1
parents:
diff changeset
1335 }
Ritor1
parents:
diff changeset
1336
Ritor1
parents:
diff changeset
1337 /* Is the given gamma significantly different from sRGB? The test is the same
Ritor1
parents:
diff changeset
1338 * one used in pngrtran.c when deciding whether to do gamma correction. The
Ritor1
parents:
diff changeset
1339 * arithmetic optimizes the division by using the fact that the inverse of the
Ritor1
parents:
diff changeset
1340 * file sRGB gamma is 2.2
Ritor1
parents:
diff changeset
1341 */
Ritor1
parents:
diff changeset
1342 static int
Ritor1
parents:
diff changeset
1343 png_gamma_not_sRGB(png_fixed_point g)
Ritor1
parents:
diff changeset
1344 {
Ritor1
parents:
diff changeset
1345 if (g < PNG_FP_1)
Ritor1
parents:
diff changeset
1346 {
Ritor1
parents:
diff changeset
1347 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
Ritor1
parents:
diff changeset
1348 if (g == 0)
Ritor1
parents:
diff changeset
1349 return 0;
Ritor1
parents:
diff changeset
1350
Ritor1
parents:
diff changeset
1351 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
Ritor1
parents:
diff changeset
1352 }
Ritor1
parents:
diff changeset
1353
Ritor1
parents:
diff changeset
1354 return 1;
Ritor1
parents:
diff changeset
1355 }
Ritor1
parents:
diff changeset
1356
Ritor1
parents:
diff changeset
1357 /* Do the main body of a 'png_image_begin_read' function; read the PNG file
Ritor1
parents:
diff changeset
1358 * header and fill in all the information. This is executed in a safe context,
Ritor1
parents:
diff changeset
1359 * unlike the init routine above.
Ritor1
parents:
diff changeset
1360 */
Ritor1
parents:
diff changeset
1361 static int
Ritor1
parents:
diff changeset
1362 png_image_read_header(png_voidp argument)
Ritor1
parents:
diff changeset
1363 {
Ritor1
parents:
diff changeset
1364 png_imagep image = png_voidcast(png_imagep, argument);
Ritor1
parents:
diff changeset
1365 png_structrp png_ptr = image->opaque->png_ptr;
Ritor1
parents:
diff changeset
1366 png_inforp info_ptr = image->opaque->info_ptr;
Ritor1
parents:
diff changeset
1367
Ritor1
parents:
diff changeset
1368 png_set_benign_errors(png_ptr, 1/*warn*/);
Ritor1
parents:
diff changeset
1369 png_read_info(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
1370
Ritor1
parents:
diff changeset
1371 /* Do this the fast way; just read directly out of png_struct. */
Ritor1
parents:
diff changeset
1372 image->width = png_ptr->width;
Ritor1
parents:
diff changeset
1373 image->height = png_ptr->height;
Ritor1
parents:
diff changeset
1374
Ritor1
parents:
diff changeset
1375 {
Ritor1
parents:
diff changeset
1376 png_uint_32 format = png_image_format(png_ptr);
Ritor1
parents:
diff changeset
1377
Ritor1
parents:
diff changeset
1378 image->format = format;
Ritor1
parents:
diff changeset
1379
Ritor1
parents:
diff changeset
1380 #ifdef PNG_COLORSPACE_SUPPORTED
Ritor1
parents:
diff changeset
1381 /* Does the colorspace match sRGB? If there is no color endpoint
Ritor1
parents:
diff changeset
1382 * (colorant) information assume yes, otherwise require the
Ritor1
parents:
diff changeset
1383 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
Ritor1
parents:
diff changeset
1384 * colorspace has been determined to be invalid ignore it.
Ritor1
parents:
diff changeset
1385 */
Ritor1
parents:
diff changeset
1386 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
Ritor1
parents:
diff changeset
1387 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
Ritor1
parents:
diff changeset
1388 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
Ritor1
parents:
diff changeset
1389 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
Ritor1
parents:
diff changeset
1390 #endif
Ritor1
parents:
diff changeset
1391 }
Ritor1
parents:
diff changeset
1392
Ritor1
parents:
diff changeset
1393 /* We need the maximum number of entries regardless of the format the
Ritor1
parents:
diff changeset
1394 * application sets here.
Ritor1
parents:
diff changeset
1395 */
Ritor1
parents:
diff changeset
1396 {
Ritor1
parents:
diff changeset
1397 png_uint_32 cmap_entries;
Ritor1
parents:
diff changeset
1398
Ritor1
parents:
diff changeset
1399 switch (png_ptr->color_type)
Ritor1
parents:
diff changeset
1400 {
Ritor1
parents:
diff changeset
1401 case PNG_COLOR_TYPE_GRAY:
Ritor1
parents:
diff changeset
1402 cmap_entries = 1U << png_ptr->bit_depth;
Ritor1
parents:
diff changeset
1403 break;
Ritor1
parents:
diff changeset
1404
Ritor1
parents:
diff changeset
1405 case PNG_COLOR_TYPE_PALETTE:
Ritor1
parents:
diff changeset
1406 cmap_entries = png_ptr->num_palette;
Ritor1
parents:
diff changeset
1407 break;
Ritor1
parents:
diff changeset
1408
Ritor1
parents:
diff changeset
1409 default:
Ritor1
parents:
diff changeset
1410 cmap_entries = 256;
Ritor1
parents:
diff changeset
1411 break;
Ritor1
parents:
diff changeset
1412 }
Ritor1
parents:
diff changeset
1413
Ritor1
parents:
diff changeset
1414 if (cmap_entries > 256)
Ritor1
parents:
diff changeset
1415 cmap_entries = 256;
Ritor1
parents:
diff changeset
1416
Ritor1
parents:
diff changeset
1417 image->colormap_entries = cmap_entries;
Ritor1
parents:
diff changeset
1418 }
Ritor1
parents:
diff changeset
1419
Ritor1
parents:
diff changeset
1420 return 1;
Ritor1
parents:
diff changeset
1421 }
Ritor1
parents:
diff changeset
1422
Ritor1
parents:
diff changeset
1423 #ifdef PNG_STDIO_SUPPORTED
Ritor1
parents:
diff changeset
1424 int PNGAPI
Ritor1
parents:
diff changeset
1425 png_image_begin_read_from_stdio(png_imagep image, FILE* file)
Ritor1
parents:
diff changeset
1426 {
Ritor1
parents:
diff changeset
1427 if (image != NULL && image->version == PNG_IMAGE_VERSION)
Ritor1
parents:
diff changeset
1428 {
Ritor1
parents:
diff changeset
1429 if (file != NULL)
Ritor1
parents:
diff changeset
1430 {
Ritor1
parents:
diff changeset
1431 if (png_image_read_init(image))
Ritor1
parents:
diff changeset
1432 {
Ritor1
parents:
diff changeset
1433 /* This is slightly evil, but png_init_io doesn't do anything other
Ritor1
parents:
diff changeset
1434 * than this and we haven't changed the standard IO functions so
Ritor1
parents:
diff changeset
1435 * this saves a 'safe' function.
Ritor1
parents:
diff changeset
1436 */
Ritor1
parents:
diff changeset
1437 image->opaque->png_ptr->io_ptr = file;
Ritor1
parents:
diff changeset
1438 return png_safe_execute(image, png_image_read_header, image);
Ritor1
parents:
diff changeset
1439 }
Ritor1
parents:
diff changeset
1440 }
Ritor1
parents:
diff changeset
1441
Ritor1
parents:
diff changeset
1442 else
Ritor1
parents:
diff changeset
1443 return png_image_error(image,
Ritor1
parents:
diff changeset
1444 "png_image_begin_read_from_stdio: invalid argument");
Ritor1
parents:
diff changeset
1445 }
Ritor1
parents:
diff changeset
1446
Ritor1
parents:
diff changeset
1447 else if (image != NULL)
Ritor1
parents:
diff changeset
1448 return png_image_error(image,
Ritor1
parents:
diff changeset
1449 "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
Ritor1
parents:
diff changeset
1450
Ritor1
parents:
diff changeset
1451 return 0;
Ritor1
parents:
diff changeset
1452 }
Ritor1
parents:
diff changeset
1453
Ritor1
parents:
diff changeset
1454 int PNGAPI
Ritor1
parents:
diff changeset
1455 png_image_begin_read_from_file(png_imagep image, const char *file_name)
Ritor1
parents:
diff changeset
1456 {
Ritor1
parents:
diff changeset
1457 if (image != NULL && image->version == PNG_IMAGE_VERSION)
Ritor1
parents:
diff changeset
1458 {
Ritor1
parents:
diff changeset
1459 if (file_name != NULL)
Ritor1
parents:
diff changeset
1460 {
Ritor1
parents:
diff changeset
1461 FILE *fp = fopen(file_name, "rb");
Ritor1
parents:
diff changeset
1462
Ritor1
parents:
diff changeset
1463 if (fp != NULL)
Ritor1
parents:
diff changeset
1464 {
Ritor1
parents:
diff changeset
1465 if (png_image_read_init(image))
Ritor1
parents:
diff changeset
1466 {
Ritor1
parents:
diff changeset
1467 image->opaque->png_ptr->io_ptr = fp;
Ritor1
parents:
diff changeset
1468 image->opaque->owned_file = 1;
Ritor1
parents:
diff changeset
1469 return png_safe_execute(image, png_image_read_header, image);
Ritor1
parents:
diff changeset
1470 }
Ritor1
parents:
diff changeset
1471
Ritor1
parents:
diff changeset
1472 /* Clean up: just the opened file. */
Ritor1
parents:
diff changeset
1473 (void)fclose(fp);
Ritor1
parents:
diff changeset
1474 }
Ritor1
parents:
diff changeset
1475
Ritor1
parents:
diff changeset
1476 else
Ritor1
parents:
diff changeset
1477 return png_image_error(image, strerror(errno));
Ritor1
parents:
diff changeset
1478 }
Ritor1
parents:
diff changeset
1479
Ritor1
parents:
diff changeset
1480 else
Ritor1
parents:
diff changeset
1481 return png_image_error(image,
Ritor1
parents:
diff changeset
1482 "png_image_begin_read_from_file: invalid argument");
Ritor1
parents:
diff changeset
1483 }
Ritor1
parents:
diff changeset
1484
Ritor1
parents:
diff changeset
1485 else if (image != NULL)
Ritor1
parents:
diff changeset
1486 return png_image_error(image,
Ritor1
parents:
diff changeset
1487 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
Ritor1
parents:
diff changeset
1488
Ritor1
parents:
diff changeset
1489 return 0;
Ritor1
parents:
diff changeset
1490 }
Ritor1
parents:
diff changeset
1491 #endif /* PNG_STDIO_SUPPORTED */
Ritor1
parents:
diff changeset
1492
Ritor1
parents:
diff changeset
1493 static void PNGCBAPI
Ritor1
parents:
diff changeset
1494 png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
Ritor1
parents:
diff changeset
1495 {
Ritor1
parents:
diff changeset
1496 if (png_ptr != NULL)
Ritor1
parents:
diff changeset
1497 {
Ritor1
parents:
diff changeset
1498 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
Ritor1
parents:
diff changeset
1499 if (image != NULL)
Ritor1
parents:
diff changeset
1500 {
Ritor1
parents:
diff changeset
1501 png_controlp cp = image->opaque;
Ritor1
parents:
diff changeset
1502 if (cp != NULL)
Ritor1
parents:
diff changeset
1503 {
Ritor1
parents:
diff changeset
1504 png_const_bytep memory = cp->memory;
Ritor1
parents:
diff changeset
1505 png_size_t size = cp->size;
Ritor1
parents:
diff changeset
1506
Ritor1
parents:
diff changeset
1507 if (memory != NULL && size >= need)
Ritor1
parents:
diff changeset
1508 {
Ritor1
parents:
diff changeset
1509 memcpy(out, memory, need);
Ritor1
parents:
diff changeset
1510 cp->memory = memory + need;
Ritor1
parents:
diff changeset
1511 cp->size = size - need;
Ritor1
parents:
diff changeset
1512 return;
Ritor1
parents:
diff changeset
1513 }
Ritor1
parents:
diff changeset
1514
Ritor1
parents:
diff changeset
1515 png_error(png_ptr, "read beyond end of data");
Ritor1
parents:
diff changeset
1516 }
Ritor1
parents:
diff changeset
1517 }
Ritor1
parents:
diff changeset
1518
Ritor1
parents:
diff changeset
1519 png_error(png_ptr, "invalid memory read");
Ritor1
parents:
diff changeset
1520 }
Ritor1
parents:
diff changeset
1521 }
Ritor1
parents:
diff changeset
1522
Ritor1
parents:
diff changeset
1523 int PNGAPI png_image_begin_read_from_memory(png_imagep image,
Ritor1
parents:
diff changeset
1524 png_const_voidp memory, png_size_t size)
Ritor1
parents:
diff changeset
1525 {
Ritor1
parents:
diff changeset
1526 if (image != NULL && image->version == PNG_IMAGE_VERSION)
Ritor1
parents:
diff changeset
1527 {
Ritor1
parents:
diff changeset
1528 if (memory != NULL && size > 0)
Ritor1
parents:
diff changeset
1529 {
Ritor1
parents:
diff changeset
1530 if (png_image_read_init(image))
Ritor1
parents:
diff changeset
1531 {
Ritor1
parents:
diff changeset
1532 /* Now set the IO functions to read from the memory buffer and
Ritor1
parents:
diff changeset
1533 * store it into io_ptr. Again do this in-place to avoid calling a
Ritor1
parents:
diff changeset
1534 * libpng function that requires error handling.
Ritor1
parents:
diff changeset
1535 */
Ritor1
parents:
diff changeset
1536 image->opaque->memory = png_voidcast(png_const_bytep, memory);
Ritor1
parents:
diff changeset
1537 image->opaque->size = size;
Ritor1
parents:
diff changeset
1538 image->opaque->png_ptr->io_ptr = image;
Ritor1
parents:
diff changeset
1539 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
Ritor1
parents:
diff changeset
1540
Ritor1
parents:
diff changeset
1541 return png_safe_execute(image, png_image_read_header, image);
Ritor1
parents:
diff changeset
1542 }
Ritor1
parents:
diff changeset
1543 }
Ritor1
parents:
diff changeset
1544
Ritor1
parents:
diff changeset
1545 else
Ritor1
parents:
diff changeset
1546 return png_image_error(image,
Ritor1
parents:
diff changeset
1547 "png_image_begin_read_from_memory: invalid argument");
Ritor1
parents:
diff changeset
1548 }
Ritor1
parents:
diff changeset
1549
Ritor1
parents:
diff changeset
1550 else if (image != NULL)
Ritor1
parents:
diff changeset
1551 return png_image_error(image,
Ritor1
parents:
diff changeset
1552 "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
Ritor1
parents:
diff changeset
1553
Ritor1
parents:
diff changeset
1554 return 0;
Ritor1
parents:
diff changeset
1555 }
Ritor1
parents:
diff changeset
1556
Ritor1
parents:
diff changeset
1557 /* Utility function to skip chunks that are not used by the simplified image
Ritor1
parents:
diff changeset
1558 * read functions and an appropriate macro to call it.
Ritor1
parents:
diff changeset
1559 */
Ritor1
parents:
diff changeset
1560 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Ritor1
parents:
diff changeset
1561 static void
Ritor1
parents:
diff changeset
1562 png_image_skip_unused_chunks(png_structrp png_ptr)
Ritor1
parents:
diff changeset
1563 {
Ritor1
parents:
diff changeset
1564 /* Prepare the reader to ignore all recognized chunks whose data will not
Ritor1
parents:
diff changeset
1565 * be used, i.e., all chunks recognized by libpng except for those
Ritor1
parents:
diff changeset
1566 * involved in basic image reading:
Ritor1
parents:
diff changeset
1567 *
Ritor1
parents:
diff changeset
1568 * IHDR, PLTE, IDAT, IEND
Ritor1
parents:
diff changeset
1569 *
Ritor1
parents:
diff changeset
1570 * Or image data handling:
Ritor1
parents:
diff changeset
1571 *
Ritor1
parents:
diff changeset
1572 * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
Ritor1
parents:
diff changeset
1573 *
Ritor1
parents:
diff changeset
1574 * This provides a small performance improvement and eliminates any
Ritor1
parents:
diff changeset
1575 * potential vulnerability to security problems in the unused chunks.
Ritor1
parents:
diff changeset
1576 *
Ritor1
parents:
diff changeset
1577 * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
Ritor1
parents:
diff changeset
1578 * too. This allows the simplified API to be compiled without iCCP support,
Ritor1
parents:
diff changeset
1579 * however if the support is there the chunk is still checked to detect
Ritor1
parents:
diff changeset
1580 * errors (which are unfortunately quite common.)
Ritor1
parents:
diff changeset
1581 */
Ritor1
parents:
diff changeset
1582 {
Ritor1
parents:
diff changeset
1583 static PNG_CONST png_byte chunks_to_process[] = {
Ritor1
parents:
diff changeset
1584 98, 75, 71, 68, '\0', /* bKGD */
Ritor1
parents:
diff changeset
1585 99, 72, 82, 77, '\0', /* cHRM */
Ritor1
parents:
diff changeset
1586 103, 65, 77, 65, '\0', /* gAMA */
Ritor1
parents:
diff changeset
1587 # ifdef PNG_READ_iCCP_SUPPORTED
Ritor1
parents:
diff changeset
1588 105, 67, 67, 80, '\0', /* iCCP */
Ritor1
parents:
diff changeset
1589 # endif
Ritor1
parents:
diff changeset
1590 115, 66, 73, 84, '\0', /* sBIT */
Ritor1
parents:
diff changeset
1591 115, 82, 71, 66, '\0', /* sRGB */
Ritor1
parents:
diff changeset
1592 };
Ritor1
parents:
diff changeset
1593
Ritor1
parents:
diff changeset
1594 /* Ignore unknown chunks and all other chunks except for the
Ritor1
parents:
diff changeset
1595 * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
Ritor1
parents:
diff changeset
1596 */
Ritor1
parents:
diff changeset
1597 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
Ritor1
parents:
diff changeset
1598 NULL, -1);
Ritor1
parents:
diff changeset
1599
Ritor1
parents:
diff changeset
1600 /* But do not ignore image data handling chunks */
Ritor1
parents:
diff changeset
1601 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
Ritor1
parents:
diff changeset
1602 chunks_to_process, (sizeof chunks_to_process)/5);
Ritor1
parents:
diff changeset
1603 }
Ritor1
parents:
diff changeset
1604 }
Ritor1
parents:
diff changeset
1605
Ritor1
parents:
diff changeset
1606 # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
Ritor1
parents:
diff changeset
1607 #else
Ritor1
parents:
diff changeset
1608 # define PNG_SKIP_CHUNKS(p) ((void)0)
Ritor1
parents:
diff changeset
1609 #endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */
Ritor1
parents:
diff changeset
1610
Ritor1
parents:
diff changeset
1611 /* The following macro gives the exact rounded answer for all values in the
Ritor1
parents:
diff changeset
1612 * range 0..255 (it actually divides by 51.2, but the rounding still generates
Ritor1
parents:
diff changeset
1613 * the correct numbers 0..5
Ritor1
parents:
diff changeset
1614 */
Ritor1
parents:
diff changeset
1615 #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
Ritor1
parents:
diff changeset
1616
Ritor1
parents:
diff changeset
1617 /* Utility functions to make particular color-maps */
Ritor1
parents:
diff changeset
1618 static void
Ritor1
parents:
diff changeset
1619 set_file_encoding(png_image_read_control *display)
Ritor1
parents:
diff changeset
1620 {
Ritor1
parents:
diff changeset
1621 png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma;
Ritor1
parents:
diff changeset
1622 if (png_gamma_significant(g))
Ritor1
parents:
diff changeset
1623 {
Ritor1
parents:
diff changeset
1624 if (png_gamma_not_sRGB(g))
Ritor1
parents:
diff changeset
1625 {
Ritor1
parents:
diff changeset
1626 display->file_encoding = P_FILE;
Ritor1
parents:
diff changeset
1627 display->gamma_to_linear = png_reciprocal(g);
Ritor1
parents:
diff changeset
1628 }
Ritor1
parents:
diff changeset
1629
Ritor1
parents:
diff changeset
1630 else
Ritor1
parents:
diff changeset
1631 display->file_encoding = P_sRGB;
Ritor1
parents:
diff changeset
1632 }
Ritor1
parents:
diff changeset
1633
Ritor1
parents:
diff changeset
1634 else
Ritor1
parents:
diff changeset
1635 display->file_encoding = P_LINEAR8;
Ritor1
parents:
diff changeset
1636 }
Ritor1
parents:
diff changeset
1637
Ritor1
parents:
diff changeset
1638 static unsigned int
Ritor1
parents:
diff changeset
1639 decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
Ritor1
parents:
diff changeset
1640 {
Ritor1
parents:
diff changeset
1641 if (encoding == P_FILE) /* double check */
Ritor1
parents:
diff changeset
1642 encoding = display->file_encoding;
Ritor1
parents:
diff changeset
1643
Ritor1
parents:
diff changeset
1644 if (encoding == P_NOTSET) /* must be the file encoding */
Ritor1
parents:
diff changeset
1645 {
Ritor1
parents:
diff changeset
1646 set_file_encoding(display);
Ritor1
parents:
diff changeset
1647 encoding = display->file_encoding;
Ritor1
parents:
diff changeset
1648 }
Ritor1
parents:
diff changeset
1649
Ritor1
parents:
diff changeset
1650 switch (encoding)
Ritor1
parents:
diff changeset
1651 {
Ritor1
parents:
diff changeset
1652 case P_FILE:
Ritor1
parents:
diff changeset
1653 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
Ritor1
parents:
diff changeset
1654 break;
Ritor1
parents:
diff changeset
1655
Ritor1
parents:
diff changeset
1656 case P_sRGB:
Ritor1
parents:
diff changeset
1657 value = png_sRGB_table[value];
Ritor1
parents:
diff changeset
1658 break;
Ritor1
parents:
diff changeset
1659
Ritor1
parents:
diff changeset
1660 case P_LINEAR:
Ritor1
parents:
diff changeset
1661 break;
Ritor1
parents:
diff changeset
1662
Ritor1
parents:
diff changeset
1663 case P_LINEAR8:
Ritor1
parents:
diff changeset
1664 value *= 257;
Ritor1
parents:
diff changeset
1665 break;
Ritor1
parents:
diff changeset
1666
Ritor1
parents:
diff changeset
1667 default:
Ritor1
parents:
diff changeset
1668 png_error(display->image->opaque->png_ptr,
Ritor1
parents:
diff changeset
1669 "unexpected encoding (internal error)");
Ritor1
parents:
diff changeset
1670 break;
Ritor1
parents:
diff changeset
1671 }
Ritor1
parents:
diff changeset
1672
Ritor1
parents:
diff changeset
1673 return value;
Ritor1
parents:
diff changeset
1674 }
Ritor1
parents:
diff changeset
1675
Ritor1
parents:
diff changeset
1676 static png_uint_32
Ritor1
parents:
diff changeset
1677 png_colormap_compose(png_image_read_control *display,
Ritor1
parents:
diff changeset
1678 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
Ritor1
parents:
diff changeset
1679 png_uint_32 background, int encoding)
Ritor1
parents:
diff changeset
1680 {
Ritor1
parents:
diff changeset
1681 /* The file value is composed on the background, the background has the given
Ritor1
parents:
diff changeset
1682 * encoding and so does the result, the file is encoded with P_FILE and the
Ritor1
parents:
diff changeset
1683 * file and alpha are 8-bit values. The (output) encoding will always be
Ritor1
parents:
diff changeset
1684 * P_LINEAR or P_sRGB.
Ritor1
parents:
diff changeset
1685 */
Ritor1
parents:
diff changeset
1686 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
Ritor1
parents:
diff changeset
1687 png_uint_32 b = decode_gamma(display, background, encoding);
Ritor1
parents:
diff changeset
1688
Ritor1
parents:
diff changeset
1689 /* The alpha is always an 8-bit value (it comes from the palette), the value
Ritor1
parents:
diff changeset
1690 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
Ritor1
parents:
diff changeset
1691 */
Ritor1
parents:
diff changeset
1692 f = f * alpha + b * (255-alpha);
Ritor1
parents:
diff changeset
1693
Ritor1
parents:
diff changeset
1694 if (encoding == P_LINEAR)
Ritor1
parents:
diff changeset
1695 {
Ritor1
parents:
diff changeset
1696 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
Ritor1
parents:
diff changeset
1697 * accurate, it divides by 255.00000005937181414556, with no overflow.)
Ritor1
parents:
diff changeset
1698 */
Ritor1
parents:
diff changeset
1699 f *= 257; /* Now scaled by 65535 */
Ritor1
parents:
diff changeset
1700 f += f >> 16;
Ritor1
parents:
diff changeset
1701 f = (f+32768) >> 16;
Ritor1
parents:
diff changeset
1702 }
Ritor1
parents:
diff changeset
1703
Ritor1
parents:
diff changeset
1704 else /* P_sRGB */
Ritor1
parents:
diff changeset
1705 f = PNG_sRGB_FROM_LINEAR(f);
Ritor1
parents:
diff changeset
1706
Ritor1
parents:
diff changeset
1707 return f;
Ritor1
parents:
diff changeset
1708 }
Ritor1
parents:
diff changeset
1709
Ritor1
parents:
diff changeset
1710 /* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
Ritor1
parents:
diff changeset
1711 * be 8-bit.
Ritor1
parents:
diff changeset
1712 */
Ritor1
parents:
diff changeset
1713 static void
Ritor1
parents:
diff changeset
1714 png_create_colormap_entry(png_image_read_control *display,
Ritor1
parents:
diff changeset
1715 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
Ritor1
parents:
diff changeset
1716 png_uint_32 alpha, int encoding)
Ritor1
parents:
diff changeset
1717 {
Ritor1
parents:
diff changeset
1718 png_imagep image = display->image;
Ritor1
parents:
diff changeset
1719 const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ?
Ritor1
parents:
diff changeset
1720 P_LINEAR : P_sRGB;
Ritor1
parents:
diff changeset
1721 const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
Ritor1
parents:
diff changeset
1722 (red != green || green != blue);
Ritor1
parents:
diff changeset
1723
Ritor1
parents:
diff changeset
1724 if (ip > 255)
Ritor1
parents:
diff changeset
1725 png_error(image->opaque->png_ptr, "color-map index out of range");
Ritor1
parents:
diff changeset
1726
Ritor1
parents:
diff changeset
1727 /* Update the cache with whether the file gamma is significantly different
Ritor1
parents:
diff changeset
1728 * from sRGB.
Ritor1
parents:
diff changeset
1729 */
Ritor1
parents:
diff changeset
1730 if (encoding == P_FILE)
Ritor1
parents:
diff changeset
1731 {
Ritor1
parents:
diff changeset
1732 if (display->file_encoding == P_NOTSET)
Ritor1
parents:
diff changeset
1733 set_file_encoding(display);
Ritor1
parents:
diff changeset
1734
Ritor1
parents:
diff changeset
1735 /* Note that the cached value may be P_FILE too, but if it is then the
Ritor1
parents:
diff changeset
1736 * gamma_to_linear member has been set.
Ritor1
parents:
diff changeset
1737 */
Ritor1
parents:
diff changeset
1738 encoding = display->file_encoding;
Ritor1
parents:
diff changeset
1739 }
Ritor1
parents:
diff changeset
1740
Ritor1
parents:
diff changeset
1741 if (encoding == P_FILE)
Ritor1
parents:
diff changeset
1742 {
Ritor1
parents:
diff changeset
1743 png_fixed_point g = display->gamma_to_linear;
Ritor1
parents:
diff changeset
1744
Ritor1
parents:
diff changeset
1745 red = png_gamma_16bit_correct(red*257, g);
Ritor1
parents:
diff changeset
1746 green = png_gamma_16bit_correct(green*257, g);
Ritor1
parents:
diff changeset
1747 blue = png_gamma_16bit_correct(blue*257, g);
Ritor1
parents:
diff changeset
1748
Ritor1
parents:
diff changeset
1749 if (convert_to_Y || output_encoding == P_LINEAR)
Ritor1
parents:
diff changeset
1750 {
Ritor1
parents:
diff changeset
1751 alpha *= 257;
Ritor1
parents:
diff changeset
1752 encoding = P_LINEAR;
Ritor1
parents:
diff changeset
1753 }
Ritor1
parents:
diff changeset
1754
Ritor1
parents:
diff changeset
1755 else
Ritor1
parents:
diff changeset
1756 {
Ritor1
parents:
diff changeset
1757 red = PNG_sRGB_FROM_LINEAR(red * 255);
Ritor1
parents:
diff changeset
1758 green = PNG_sRGB_FROM_LINEAR(green * 255);
Ritor1
parents:
diff changeset
1759 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
Ritor1
parents:
diff changeset
1760 encoding = P_sRGB;
Ritor1
parents:
diff changeset
1761 }
Ritor1
parents:
diff changeset
1762 }
Ritor1
parents:
diff changeset
1763
Ritor1
parents:
diff changeset
1764 else if (encoding == P_LINEAR8)
Ritor1
parents:
diff changeset
1765 {
Ritor1
parents:
diff changeset
1766 /* This encoding occurs quite frequently in test cases because PngSuite
Ritor1
parents:
diff changeset
1767 * includes a gAMA 1.0 chunk with most images.
Ritor1
parents:
diff changeset
1768 */
Ritor1
parents:
diff changeset
1769 red *= 257;
Ritor1
parents:
diff changeset
1770 green *= 257;
Ritor1
parents:
diff changeset
1771 blue *= 257;
Ritor1
parents:
diff changeset
1772 alpha *= 257;
Ritor1
parents:
diff changeset
1773 encoding = P_LINEAR;
Ritor1
parents:
diff changeset
1774 }
Ritor1
parents:
diff changeset
1775
Ritor1
parents:
diff changeset
1776 else if (encoding == P_sRGB && (convert_to_Y || output_encoding == P_LINEAR))
Ritor1
parents:
diff changeset
1777 {
Ritor1
parents:
diff changeset
1778 /* The values are 8-bit sRGB values, but must be converted to 16-bit
Ritor1
parents:
diff changeset
1779 * linear.
Ritor1
parents:
diff changeset
1780 */
Ritor1
parents:
diff changeset
1781 red = png_sRGB_table[red];
Ritor1
parents:
diff changeset
1782 green = png_sRGB_table[green];
Ritor1
parents:
diff changeset
1783 blue = png_sRGB_table[blue];
Ritor1
parents:
diff changeset
1784 alpha *= 257;
Ritor1
parents:
diff changeset
1785 encoding = P_LINEAR;
Ritor1
parents:
diff changeset
1786 }
Ritor1
parents:
diff changeset
1787
Ritor1
parents:
diff changeset
1788 /* This is set if the color isn't gray but the output is. */
Ritor1
parents:
diff changeset
1789 if (encoding == P_LINEAR)
Ritor1
parents:
diff changeset
1790 {
Ritor1
parents:
diff changeset
1791 if (convert_to_Y)
Ritor1
parents:
diff changeset
1792 {
Ritor1
parents:
diff changeset
1793 /* NOTE: these values are copied from png_do_rgb_to_gray */
Ritor1
parents:
diff changeset
1794 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
Ritor1
parents:
diff changeset
1795 (png_uint_32)2366 * blue;
Ritor1
parents:
diff changeset
1796
Ritor1
parents:
diff changeset
1797 if (output_encoding == P_LINEAR)
Ritor1
parents:
diff changeset
1798 y = (y + 16384) >> 15;
Ritor1
parents:
diff changeset
1799
Ritor1
parents:
diff changeset
1800 else
Ritor1
parents:
diff changeset
1801 {
Ritor1
parents:
diff changeset
1802 /* y is scaled by 32768, we need it scaled by 255: */
Ritor1
parents:
diff changeset
1803 y = (y + 128) >> 8;
Ritor1
parents:
diff changeset
1804 y *= 255;
Ritor1
parents:
diff changeset
1805 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
Ritor1
parents:
diff changeset
1806 encoding = P_sRGB;
Ritor1
parents:
diff changeset
1807 }
Ritor1
parents:
diff changeset
1808
Ritor1
parents:
diff changeset
1809 blue = red = green = y;
Ritor1
parents:
diff changeset
1810 }
Ritor1
parents:
diff changeset
1811
Ritor1
parents:
diff changeset
1812 else if (output_encoding == P_sRGB)
Ritor1
parents:
diff changeset
1813 {
Ritor1
parents:
diff changeset
1814 red = PNG_sRGB_FROM_LINEAR(red * 255);
Ritor1
parents:
diff changeset
1815 green = PNG_sRGB_FROM_LINEAR(green * 255);
Ritor1
parents:
diff changeset
1816 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
Ritor1
parents:
diff changeset
1817 alpha = PNG_DIV257(alpha);
Ritor1
parents:
diff changeset
1818 encoding = P_sRGB;
Ritor1
parents:
diff changeset
1819 }
Ritor1
parents:
diff changeset
1820 }
Ritor1
parents:
diff changeset
1821
Ritor1
parents:
diff changeset
1822 if (encoding != output_encoding)
Ritor1
parents:
diff changeset
1823 png_error(image->opaque->png_ptr, "bad encoding (internal error)");
Ritor1
parents:
diff changeset
1824
Ritor1
parents:
diff changeset
1825 /* Store the value. */
Ritor1
parents:
diff changeset
1826 {
Ritor1
parents:
diff changeset
1827 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
Ritor1
parents:
diff changeset
1828 const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
Ritor1
parents:
diff changeset
1829 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
Ritor1
parents:
diff changeset
1830 # else
Ritor1
parents:
diff changeset
1831 # define afirst 0
Ritor1
parents:
diff changeset
1832 # endif
Ritor1
parents:
diff changeset
1833 # ifdef PNG_FORMAT_BGR_SUPPORTED
Ritor1
parents:
diff changeset
1834 const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
Ritor1
parents:
diff changeset
1835 # else
Ritor1
parents:
diff changeset
1836 # define bgr 0
Ritor1
parents:
diff changeset
1837 # endif
Ritor1
parents:
diff changeset
1838
Ritor1
parents:
diff changeset
1839 if (output_encoding == P_LINEAR)
Ritor1
parents:
diff changeset
1840 {
Ritor1
parents:
diff changeset
1841 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
Ritor1
parents:
diff changeset
1842
Ritor1
parents:
diff changeset
1843 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
Ritor1
parents:
diff changeset
1844
Ritor1
parents:
diff changeset
1845 /* The linear 16-bit values must be pre-multiplied by the alpha channel
Ritor1
parents:
diff changeset
1846 * value, if less than 65535 (this is, effectively, composite on black
Ritor1
parents:
diff changeset
1847 * if the alpha channel is removed.)
Ritor1
parents:
diff changeset
1848 */
Ritor1
parents:
diff changeset
1849 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
Ritor1
parents:
diff changeset
1850 {
Ritor1
parents:
diff changeset
1851 case 4:
Ritor1
parents:
diff changeset
1852 entry[afirst ? 0 : 3] = (png_uint_16)alpha;
Ritor1
parents:
diff changeset
1853 /* FALL THROUGH */
Ritor1
parents:
diff changeset
1854
Ritor1
parents:
diff changeset
1855 case 3:
Ritor1
parents:
diff changeset
1856 if (alpha < 65535)
Ritor1
parents:
diff changeset
1857 {
Ritor1
parents:
diff changeset
1858 if (alpha > 0)
Ritor1
parents:
diff changeset
1859 {
Ritor1
parents:
diff changeset
1860 blue = (blue * alpha + 32767U)/65535U;
Ritor1
parents:
diff changeset
1861 green = (green * alpha + 32767U)/65535U;
Ritor1
parents:
diff changeset
1862 red = (red * alpha + 32767U)/65535U;
Ritor1
parents:
diff changeset
1863 }
Ritor1
parents:
diff changeset
1864
Ritor1
parents:
diff changeset
1865 else
Ritor1
parents:
diff changeset
1866 red = green = blue = 0;
Ritor1
parents:
diff changeset
1867 }
Ritor1
parents:
diff changeset
1868 entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
Ritor1
parents:
diff changeset
1869 entry[afirst + 1] = (png_uint_16)green;
Ritor1
parents:
diff changeset
1870 entry[afirst + bgr] = (png_uint_16)red;
Ritor1
parents:
diff changeset
1871 break;
Ritor1
parents:
diff changeset
1872
Ritor1
parents:
diff changeset
1873 case 2:
Ritor1
parents:
diff changeset
1874 entry[1 ^ afirst] = (png_uint_16)alpha;
Ritor1
parents:
diff changeset
1875 /* FALL THROUGH */
Ritor1
parents:
diff changeset
1876
Ritor1
parents:
diff changeset
1877 case 1:
Ritor1
parents:
diff changeset
1878 if (alpha < 65535)
Ritor1
parents:
diff changeset
1879 {
Ritor1
parents:
diff changeset
1880 if (alpha > 0)
Ritor1
parents:
diff changeset
1881 green = (green * alpha + 32767U)/65535U;
Ritor1
parents:
diff changeset
1882
Ritor1
parents:
diff changeset
1883 else
Ritor1
parents:
diff changeset
1884 green = 0;
Ritor1
parents:
diff changeset
1885 }
Ritor1
parents:
diff changeset
1886 entry[afirst] = (png_uint_16)green;
Ritor1
parents:
diff changeset
1887 break;
Ritor1
parents:
diff changeset
1888
Ritor1
parents:
diff changeset
1889 default:
Ritor1
parents:
diff changeset
1890 break;
Ritor1
parents:
diff changeset
1891 }
Ritor1
parents:
diff changeset
1892 }
Ritor1
parents:
diff changeset
1893
Ritor1
parents:
diff changeset
1894 else /* output encoding is P_sRGB */
Ritor1
parents:
diff changeset
1895 {
Ritor1
parents:
diff changeset
1896 png_bytep entry = png_voidcast(png_bytep, display->colormap);
Ritor1
parents:
diff changeset
1897
Ritor1
parents:
diff changeset
1898 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
Ritor1
parents:
diff changeset
1899
Ritor1
parents:
diff changeset
1900 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
Ritor1
parents:
diff changeset
1901 {
Ritor1
parents:
diff changeset
1902 case 4:
Ritor1
parents:
diff changeset
1903 entry[afirst ? 0 : 3] = (png_byte)alpha;
Ritor1
parents:
diff changeset
1904 case 3:
Ritor1
parents:
diff changeset
1905 entry[afirst + (2 ^ bgr)] = (png_byte)blue;
Ritor1
parents:
diff changeset
1906 entry[afirst + 1] = (png_byte)green;
Ritor1
parents:
diff changeset
1907 entry[afirst + bgr] = (png_byte)red;
Ritor1
parents:
diff changeset
1908 break;
Ritor1
parents:
diff changeset
1909
Ritor1
parents:
diff changeset
1910 case 2:
Ritor1
parents:
diff changeset
1911 entry[1 ^ afirst] = (png_byte)alpha;
Ritor1
parents:
diff changeset
1912 case 1:
Ritor1
parents:
diff changeset
1913 entry[afirst] = (png_byte)green;
Ritor1
parents:
diff changeset
1914 break;
Ritor1
parents:
diff changeset
1915
Ritor1
parents:
diff changeset
1916 default:
Ritor1
parents:
diff changeset
1917 break;
Ritor1
parents:
diff changeset
1918 }
Ritor1
parents:
diff changeset
1919 }
Ritor1
parents:
diff changeset
1920
Ritor1
parents:
diff changeset
1921 # ifdef afirst
Ritor1
parents:
diff changeset
1922 # undef afirst
Ritor1
parents:
diff changeset
1923 # endif
Ritor1
parents:
diff changeset
1924 # ifdef bgr
Ritor1
parents:
diff changeset
1925 # undef bgr
Ritor1
parents:
diff changeset
1926 # endif
Ritor1
parents:
diff changeset
1927 }
Ritor1
parents:
diff changeset
1928 }
Ritor1
parents:
diff changeset
1929
Ritor1
parents:
diff changeset
1930 static int
Ritor1
parents:
diff changeset
1931 make_gray_file_colormap(png_image_read_control *display)
Ritor1
parents:
diff changeset
1932 {
Ritor1
parents:
diff changeset
1933 unsigned int i;
Ritor1
parents:
diff changeset
1934
Ritor1
parents:
diff changeset
1935 for (i=0; i<256; ++i)
Ritor1
parents:
diff changeset
1936 png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
Ritor1
parents:
diff changeset
1937
Ritor1
parents:
diff changeset
1938 return i;
Ritor1
parents:
diff changeset
1939 }
Ritor1
parents:
diff changeset
1940
Ritor1
parents:
diff changeset
1941 static int
Ritor1
parents:
diff changeset
1942 make_gray_colormap(png_image_read_control *display)
Ritor1
parents:
diff changeset
1943 {
Ritor1
parents:
diff changeset
1944 unsigned int i;
Ritor1
parents:
diff changeset
1945
Ritor1
parents:
diff changeset
1946 for (i=0; i<256; ++i)
Ritor1
parents:
diff changeset
1947 png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
Ritor1
parents:
diff changeset
1948
Ritor1
parents:
diff changeset
1949 return i;
Ritor1
parents:
diff changeset
1950 }
Ritor1
parents:
diff changeset
1951 #define PNG_GRAY_COLORMAP_ENTRIES 256
Ritor1
parents:
diff changeset
1952
Ritor1
parents:
diff changeset
1953 static int
Ritor1
parents:
diff changeset
1954 make_ga_colormap(png_image_read_control *display)
Ritor1
parents:
diff changeset
1955 {
Ritor1
parents:
diff changeset
1956 unsigned int i, a;
Ritor1
parents:
diff changeset
1957
Ritor1
parents:
diff changeset
1958 /* Alpha is retained, the output will be a color-map with entries
Ritor1
parents:
diff changeset
1959 * selected by six levels of alpha. One transparent entry, 6 gray
Ritor1
parents:
diff changeset
1960 * levels for all the intermediate alpha values, leaving 230 entries
Ritor1
parents:
diff changeset
1961 * for the opaque grays. The color-map entries are the six values
Ritor1
parents:
diff changeset
1962 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
Ritor1
parents:
diff changeset
1963 * relevant entry.
Ritor1
parents:
diff changeset
1964 *
Ritor1
parents:
diff changeset
1965 * if (alpha > 229) // opaque
Ritor1
parents:
diff changeset
1966 * {
Ritor1
parents:
diff changeset
1967 * // The 231 entries are selected to make the math below work:
Ritor1
parents:
diff changeset
1968 * base = 0;
Ritor1
parents:
diff changeset
1969 * entry = (231 * gray + 128) >> 8;
Ritor1
parents:
diff changeset
1970 * }
Ritor1
parents:
diff changeset
1971 * else if (alpha < 26) // transparent
Ritor1
parents:
diff changeset
1972 * {
Ritor1
parents:
diff changeset
1973 * base = 231;
Ritor1
parents:
diff changeset
1974 * entry = 0;
Ritor1
parents:
diff changeset
1975 * }
Ritor1
parents:
diff changeset
1976 * else // partially opaque
Ritor1
parents:
diff changeset
1977 * {
Ritor1
parents:
diff changeset
1978 * base = 226 + 6 * PNG_DIV51(alpha);
Ritor1
parents:
diff changeset
1979 * entry = PNG_DIV51(gray);
Ritor1
parents:
diff changeset
1980 * }
Ritor1
parents:
diff changeset
1981 */
Ritor1
parents:
diff changeset
1982 i = 0;
Ritor1
parents:
diff changeset
1983 while (i < 231)
Ritor1
parents:
diff changeset
1984 {
Ritor1
parents:
diff changeset
1985 unsigned int gray = (i * 256 + 115) / 231;
Ritor1
parents:
diff changeset
1986 png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
Ritor1
parents:
diff changeset
1987 }
Ritor1
parents:
diff changeset
1988
Ritor1
parents:
diff changeset
1989 /* 255 is used here for the component values for consistency with the code
Ritor1
parents:
diff changeset
1990 * that undoes premultiplication in pngwrite.c.
Ritor1
parents:
diff changeset
1991 */
Ritor1
parents:
diff changeset
1992 png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
Ritor1
parents:
diff changeset
1993
Ritor1
parents:
diff changeset
1994 for (a=1; a<5; ++a)
Ritor1
parents:
diff changeset
1995 {
Ritor1
parents:
diff changeset
1996 unsigned int g;
Ritor1
parents:
diff changeset
1997
Ritor1
parents:
diff changeset
1998 for (g=0; g<6; ++g)
Ritor1
parents:
diff changeset
1999 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
Ritor1
parents:
diff changeset
2000 P_sRGB);
Ritor1
parents:
diff changeset
2001 }
Ritor1
parents:
diff changeset
2002
Ritor1
parents:
diff changeset
2003 return i;
Ritor1
parents:
diff changeset
2004 }
Ritor1
parents:
diff changeset
2005
Ritor1
parents:
diff changeset
2006 #define PNG_GA_COLORMAP_ENTRIES 256
Ritor1
parents:
diff changeset
2007
Ritor1
parents:
diff changeset
2008 static int
Ritor1
parents:
diff changeset
2009 make_rgb_colormap(png_image_read_control *display)
Ritor1
parents:
diff changeset
2010 {
Ritor1
parents:
diff changeset
2011 unsigned int i, r;
Ritor1
parents:
diff changeset
2012
Ritor1
parents:
diff changeset
2013 /* Build a 6x6x6 opaque RGB cube */
Ritor1
parents:
diff changeset
2014 for (i=r=0; r<6; ++r)
Ritor1
parents:
diff changeset
2015 {
Ritor1
parents:
diff changeset
2016 unsigned int g;
Ritor1
parents:
diff changeset
2017
Ritor1
parents:
diff changeset
2018 for (g=0; g<6; ++g)
Ritor1
parents:
diff changeset
2019 {
Ritor1
parents:
diff changeset
2020 unsigned int b;
Ritor1
parents:
diff changeset
2021
Ritor1
parents:
diff changeset
2022 for (b=0; b<6; ++b)
Ritor1
parents:
diff changeset
2023 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
Ritor1
parents:
diff changeset
2024 P_sRGB);
Ritor1
parents:
diff changeset
2025 }
Ritor1
parents:
diff changeset
2026 }
Ritor1
parents:
diff changeset
2027
Ritor1
parents:
diff changeset
2028 return i;
Ritor1
parents:
diff changeset
2029 }
Ritor1
parents:
diff changeset
2030
Ritor1
parents:
diff changeset
2031 #define PNG_RGB_COLORMAP_ENTRIES 216
Ritor1
parents:
diff changeset
2032
Ritor1
parents:
diff changeset
2033 /* Return a palette index to the above palette given three 8-bit sRGB values. */
Ritor1
parents:
diff changeset
2034 #define PNG_RGB_INDEX(r,g,b) \
Ritor1
parents:
diff changeset
2035 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
Ritor1
parents:
diff changeset
2036
Ritor1
parents:
diff changeset
2037 static int
Ritor1
parents:
diff changeset
2038 png_image_read_colormap(png_voidp argument)
Ritor1
parents:
diff changeset
2039 {
Ritor1
parents:
diff changeset
2040 png_image_read_control *display =
Ritor1
parents:
diff changeset
2041 png_voidcast(png_image_read_control*, argument);
Ritor1
parents:
diff changeset
2042 const png_imagep image = display->image;
Ritor1
parents:
diff changeset
2043
Ritor1
parents:
diff changeset
2044 const png_structrp png_ptr = image->opaque->png_ptr;
Ritor1
parents:
diff changeset
2045 const png_uint_32 output_format = image->format;
Ritor1
parents:
diff changeset
2046 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ?
Ritor1
parents:
diff changeset
2047 P_LINEAR : P_sRGB;
Ritor1
parents:
diff changeset
2048
Ritor1
parents:
diff changeset
2049 unsigned int cmap_entries;
Ritor1
parents:
diff changeset
2050 unsigned int output_processing; /* Output processing option */
Ritor1
parents:
diff changeset
2051 unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
Ritor1
parents:
diff changeset
2052
Ritor1
parents:
diff changeset
2053 /* Background information; the background color and the index of this color
Ritor1
parents:
diff changeset
2054 * in the color-map if it exists (else 256).
Ritor1
parents:
diff changeset
2055 */
Ritor1
parents:
diff changeset
2056 unsigned int background_index = 256;
Ritor1
parents:
diff changeset
2057 png_uint_32 back_r, back_g, back_b;
Ritor1
parents:
diff changeset
2058
Ritor1
parents:
diff changeset
2059 /* Flags to accumulate things that need to be done to the input. */
Ritor1
parents:
diff changeset
2060 int expand_tRNS = 0;
Ritor1
parents:
diff changeset
2061
Ritor1
parents:
diff changeset
2062 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
Ritor1
parents:
diff changeset
2063 * very difficult to do, the results look awful, and it is difficult to see
Ritor1
parents:
diff changeset
2064 * what possible use it is because the application can't control the
Ritor1
parents:
diff changeset
2065 * color-map.
Ritor1
parents:
diff changeset
2066 */
Ritor1
parents:
diff changeset
2067 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
Ritor1
parents:
diff changeset
2068 png_ptr->num_trans > 0) /* alpha in input */ &&
Ritor1
parents:
diff changeset
2069 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
Ritor1
parents:
diff changeset
2070 {
Ritor1
parents:
diff changeset
2071 if (output_encoding == P_LINEAR) /* compose on black */
Ritor1
parents:
diff changeset
2072 back_b = back_g = back_r = 0;
Ritor1
parents:
diff changeset
2073
Ritor1
parents:
diff changeset
2074 else if (display->background == NULL /* no way to remove it */)
Ritor1
parents:
diff changeset
2075 png_error(png_ptr,
Ritor1
parents:
diff changeset
2076 "a background color must be supplied to remove alpha/transparency");
Ritor1
parents:
diff changeset
2077
Ritor1
parents:
diff changeset
2078 /* Get a copy of the background color (this avoids repeating the checks
Ritor1
parents:
diff changeset
2079 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
Ritor1
parents:
diff changeset
2080 * output format.
Ritor1
parents:
diff changeset
2081 */
Ritor1
parents:
diff changeset
2082 else
Ritor1
parents:
diff changeset
2083 {
Ritor1
parents:
diff changeset
2084 back_g = display->background->green;
Ritor1
parents:
diff changeset
2085 if (output_format & PNG_FORMAT_FLAG_COLOR)
Ritor1
parents:
diff changeset
2086 {
Ritor1
parents:
diff changeset
2087 back_r = display->background->red;
Ritor1
parents:
diff changeset
2088 back_b = display->background->blue;
Ritor1
parents:
diff changeset
2089 }
Ritor1
parents:
diff changeset
2090 else
Ritor1
parents:
diff changeset
2091 back_b = back_r = back_g;
Ritor1
parents:
diff changeset
2092 }
Ritor1
parents:
diff changeset
2093 }
Ritor1
parents:
diff changeset
2094
Ritor1
parents:
diff changeset
2095 else if (output_encoding == P_LINEAR)
Ritor1
parents:
diff changeset
2096 back_b = back_r = back_g = 65535;
Ritor1
parents:
diff changeset
2097
Ritor1
parents:
diff changeset
2098 else
Ritor1
parents:
diff changeset
2099 back_b = back_r = back_g = 255;
Ritor1
parents:
diff changeset
2100
Ritor1
parents:
diff changeset
2101 /* Default the input file gamma if required - this is necessary because
Ritor1
parents:
diff changeset
2102 * libpng assumes that if no gamma information is present the data is in the
Ritor1
parents:
diff changeset
2103 * output format, but the simplified API deduces the gamma from the input
Ritor1
parents:
diff changeset
2104 * format.
Ritor1
parents:
diff changeset
2105 */
Ritor1
parents:
diff changeset
2106 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0)
Ritor1
parents:
diff changeset
2107 {
Ritor1
parents:
diff changeset
2108 /* Do this directly, not using the png_colorspace functions, to ensure
Ritor1
parents:
diff changeset
2109 * that it happens even if the colorspace is invalid (though probably if
Ritor1
parents:
diff changeset
2110 * it is the setting will be ignored) Note that the same thing can be
Ritor1
parents:
diff changeset
2111 * achieved at the application interface with png_set_gAMA.
Ritor1
parents:
diff changeset
2112 */
Ritor1
parents:
diff changeset
2113 if (png_ptr->bit_depth == 16 &&
Ritor1
parents:
diff changeset
2114 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
Ritor1
parents:
diff changeset
2115 png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR;
Ritor1
parents:
diff changeset
2116
Ritor1
parents:
diff changeset
2117 else
Ritor1
parents:
diff changeset
2118 png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE;
Ritor1
parents:
diff changeset
2119
Ritor1
parents:
diff changeset
2120 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
Ritor1
parents:
diff changeset
2121 }
Ritor1
parents:
diff changeset
2122
Ritor1
parents:
diff changeset
2123 /* Decide what to do based on the PNG color type of the input data. The
Ritor1
parents:
diff changeset
2124 * utility function png_create_colormap_entry deals with most aspects of the
Ritor1
parents:
diff changeset
2125 * output transformations; this code works out how to produce bytes of
Ritor1
parents:
diff changeset
2126 * color-map entries from the original format.
Ritor1
parents:
diff changeset
2127 */
Ritor1
parents:
diff changeset
2128 switch (png_ptr->color_type)
Ritor1
parents:
diff changeset
2129 {
Ritor1
parents:
diff changeset
2130 case PNG_COLOR_TYPE_GRAY:
Ritor1
parents:
diff changeset
2131 if (png_ptr->bit_depth <= 8)
Ritor1
parents:
diff changeset
2132 {
Ritor1
parents:
diff changeset
2133 /* There at most 256 colors in the output, regardless of
Ritor1
parents:
diff changeset
2134 * transparency.
Ritor1
parents:
diff changeset
2135 */
Ritor1
parents:
diff changeset
2136 unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
Ritor1
parents:
diff changeset
2137
Ritor1
parents:
diff changeset
2138 cmap_entries = 1U << png_ptr->bit_depth;
Ritor1
parents:
diff changeset
2139 if (cmap_entries > image->colormap_entries)
Ritor1
parents:
diff changeset
2140 png_error(png_ptr, "gray[8] color-map: too few entries");
Ritor1
parents:
diff changeset
2141
Ritor1
parents:
diff changeset
2142 step = 255 / (cmap_entries - 1);
Ritor1
parents:
diff changeset
2143 output_processing = PNG_CMAP_NONE;
Ritor1
parents:
diff changeset
2144
Ritor1
parents:
diff changeset
2145 /* If there is a tRNS chunk then this either selects a transparent
Ritor1
parents:
diff changeset
2146 * value or, if the output has no alpha, the background color.
Ritor1
parents:
diff changeset
2147 */
Ritor1
parents:
diff changeset
2148 if (png_ptr->num_trans > 0)
Ritor1
parents:
diff changeset
2149 {
Ritor1
parents:
diff changeset
2150 trans = png_ptr->trans_color.gray;
Ritor1
parents:
diff changeset
2151
Ritor1
parents:
diff changeset
2152 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
Ritor1
parents:
diff changeset
2153 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
Ritor1
parents:
diff changeset
2154 }
Ritor1
parents:
diff changeset
2155
Ritor1
parents:
diff changeset
2156 /* png_create_colormap_entry just takes an RGBA and writes the
Ritor1
parents:
diff changeset
2157 * corresponding color-map entry using the format from 'image',
Ritor1
parents:
diff changeset
2158 * including the required conversion to sRGB or linear as
Ritor1
parents:
diff changeset
2159 * appropriate. The input values are always either sRGB (if the
Ritor1
parents:
diff changeset
2160 * gamma correction flag is 0) or 0..255 scaled file encoded values
Ritor1
parents:
diff changeset
2161 * (if the function must gamma correct them).
Ritor1
parents:
diff changeset
2162 */
Ritor1
parents:
diff changeset
2163 for (i=val=0; i<cmap_entries; ++i, val += step)
Ritor1
parents:
diff changeset
2164 {
Ritor1
parents:
diff changeset
2165 /* 'i' is a file value. While this will result in duplicated
Ritor1
parents:
diff changeset
2166 * entries for 8-bit non-sRGB encoded files it is necessary to
Ritor1
parents:
diff changeset
2167 * have non-gamma corrected values to do tRNS handling.
Ritor1
parents:
diff changeset
2168 */
Ritor1
parents:
diff changeset
2169 if (i != trans)
Ritor1
parents:
diff changeset
2170 png_create_colormap_entry(display, i, val, val, val, 255,
Ritor1
parents:
diff changeset
2171 P_FILE/*8-bit with file gamma*/);
Ritor1
parents:
diff changeset
2172
Ritor1
parents:
diff changeset
2173 /* Else this entry is transparent. The colors don't matter if
Ritor1
parents:
diff changeset
2174 * there is an alpha channel (back_alpha == 0), but it does no
Ritor1
parents:
diff changeset
2175 * harm to pass them in; the values are not set above so this
Ritor1
parents:
diff changeset
2176 * passes in white.
Ritor1
parents:
diff changeset
2177 *
Ritor1
parents:
diff changeset
2178 * NOTE: this preserves the full precision of the application
Ritor1
parents:
diff changeset
2179 * supplied background color when it is used.
Ritor1
parents:
diff changeset
2180 */
Ritor1
parents:
diff changeset
2181 else
Ritor1
parents:
diff changeset
2182 png_create_colormap_entry(display, i, back_r, back_g, back_b,
Ritor1
parents:
diff changeset
2183 back_alpha, output_encoding);
Ritor1
parents:
diff changeset
2184 }
Ritor1
parents:
diff changeset
2185
Ritor1
parents:
diff changeset
2186 /* We need libpng to preserve the original encoding. */
Ritor1
parents:
diff changeset
2187 data_encoding = P_FILE;
Ritor1
parents:
diff changeset
2188
Ritor1
parents:
diff changeset
2189 /* The rows from libpng, while technically gray values, are now also
Ritor1
parents:
diff changeset
2190 * color-map indicies; however, they may need to be expanded to 1
Ritor1
parents:
diff changeset
2191 * byte per pixel. This is what png_set_packing does (i.e., it
Ritor1
parents:
diff changeset
2192 * unpacks the bit values into bytes.)
Ritor1
parents:
diff changeset
2193 */
Ritor1
parents:
diff changeset
2194 if (png_ptr->bit_depth < 8)
Ritor1
parents:
diff changeset
2195 png_set_packing(png_ptr);
Ritor1
parents:
diff changeset
2196 }
Ritor1
parents:
diff changeset
2197
Ritor1
parents:
diff changeset
2198 else /* bit depth is 16 */
Ritor1
parents:
diff changeset
2199 {
Ritor1
parents:
diff changeset
2200 /* The 16-bit input values can be converted directly to 8-bit gamma
Ritor1
parents:
diff changeset
2201 * encoded values; however, if a tRNS chunk is present 257 color-map
Ritor1
parents:
diff changeset
2202 * entries are required. This means that the extra entry requires
Ritor1
parents:
diff changeset
2203 * special processing; add an alpha channel, sacrifice gray level
Ritor1
parents:
diff changeset
2204 * 254 and convert transparent (alpha==0) entries to that.
Ritor1
parents:
diff changeset
2205 *
Ritor1
parents:
diff changeset
2206 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
Ritor1
parents:
diff changeset
2207 * same time to minimize quality loss. If a tRNS chunk is present
Ritor1
parents:
diff changeset
2208 * this means libpng must handle it too; otherwise it is impossible
Ritor1
parents:
diff changeset
2209 * to do the exact match on the 16-bit value.
Ritor1
parents:
diff changeset
2210 *
Ritor1
parents:
diff changeset
2211 * If the output has no alpha channel *and* the background color is
Ritor1
parents:
diff changeset
2212 * gray then it is possible to let libpng handle the substitution by
Ritor1
parents:
diff changeset
2213 * ensuring that the corresponding gray level matches the background
Ritor1
parents:
diff changeset
2214 * color exactly.
Ritor1
parents:
diff changeset
2215 */
Ritor1
parents:
diff changeset
2216 data_encoding = P_sRGB;
Ritor1
parents:
diff changeset
2217
Ritor1
parents:
diff changeset
2218 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
Ritor1
parents:
diff changeset
2219 png_error(png_ptr, "gray[16] color-map: too few entries");
Ritor1
parents:
diff changeset
2220
Ritor1
parents:
diff changeset
2221 cmap_entries = make_gray_colormap(display);
Ritor1
parents:
diff changeset
2222
Ritor1
parents:
diff changeset
2223 if (png_ptr->num_trans > 0)
Ritor1
parents:
diff changeset
2224 {
Ritor1
parents:
diff changeset
2225 unsigned int back_alpha;
Ritor1
parents:
diff changeset
2226
Ritor1
parents:
diff changeset
2227 if (output_format & PNG_FORMAT_FLAG_ALPHA)
Ritor1
parents:
diff changeset
2228 back_alpha = 0;
Ritor1
parents:
diff changeset
2229
Ritor1
parents:
diff changeset
2230 else
Ritor1
parents:
diff changeset
2231 {
Ritor1
parents:
diff changeset
2232 if (back_r == back_g && back_g == back_b)
Ritor1
parents:
diff changeset
2233 {
Ritor1
parents:
diff changeset
2234 /* Background is gray; no special processing will be
Ritor1
parents:
diff changeset
2235 * required.
Ritor1
parents:
diff changeset
2236 */
Ritor1
parents:
diff changeset
2237 png_color_16 c;
Ritor1
parents:
diff changeset
2238 png_uint_32 gray = back_g;
Ritor1
parents:
diff changeset
2239
Ritor1
parents:
diff changeset
2240 if (output_encoding == P_LINEAR)
Ritor1
parents:
diff changeset
2241 {
Ritor1
parents:
diff changeset
2242 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
Ritor1
parents:
diff changeset
2243
Ritor1
parents:
diff changeset
2244 /* And make sure the corresponding palette entry
Ritor1
parents:
diff changeset
2245 * matches.
Ritor1
parents:
diff changeset
2246 */
Ritor1
parents:
diff changeset
2247 png_create_colormap_entry(display, gray, back_g, back_g,
Ritor1
parents:
diff changeset
2248 back_g, 65535, P_LINEAR);
Ritor1
parents:
diff changeset
2249 }
Ritor1
parents:
diff changeset
2250
Ritor1
parents:
diff changeset
2251 /* The background passed to libpng, however, must be the
Ritor1
parents:
diff changeset
2252 * sRGB value.
Ritor1
parents:
diff changeset
2253 */
Ritor1
parents:
diff changeset
2254 c.index = 0; /*unused*/
Ritor1
parents:
diff changeset
2255 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
Ritor1
parents:
diff changeset
2256
Ritor1
parents:
diff changeset
2257 /* NOTE: does this work without expanding tRNS to alpha?
Ritor1
parents:
diff changeset
2258 * It should be the color->gray case below apparently
Ritor1
parents:
diff changeset
2259 * doesn't.
Ritor1
parents:
diff changeset
2260 */
Ritor1
parents:
diff changeset
2261 png_set_background_fixed(png_ptr, &c,
Ritor1
parents:
diff changeset
2262 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
Ritor1
parents:
diff changeset
2263 0/*gamma: not used*/);
Ritor1
parents:
diff changeset
2264
Ritor1
parents:
diff changeset
2265 output_processing = PNG_CMAP_NONE;
Ritor1
parents:
diff changeset
2266 break;
Ritor1
parents:
diff changeset
2267 }
Ritor1
parents:
diff changeset
2268
Ritor1
parents:
diff changeset
2269 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
Ritor1
parents:
diff changeset
2270 }
Ritor1
parents:
diff changeset
2271
Ritor1
parents:
diff changeset
2272 /* output_processing means that the libpng-processed row will be
Ritor1
parents:
diff changeset
2273 * 8-bit GA and it has to be processing to single byte color-map
Ritor1
parents:
diff changeset
2274 * values. Entry 254 is replaced by either a completely
Ritor1
parents:
diff changeset
2275 * transparent entry or by the background color at full
Ritor1
parents:
diff changeset
2276 * precision (and the background color is not a simple gray leve
Ritor1
parents:
diff changeset
2277 * in this case.)
Ritor1
parents:
diff changeset
2278 */
Ritor1
parents:
diff changeset
2279 expand_tRNS = 1;
Ritor1
parents:
diff changeset
2280 output_processing = PNG_CMAP_TRANS;
Ritor1
parents:
diff changeset
2281 background_index = 254;
Ritor1
parents:
diff changeset
2282
Ritor1
parents:
diff changeset
2283 /* And set (overwrite) color-map entry 254 to the actual
Ritor1
parents:
diff changeset
2284 * background color at full precision.
Ritor1
parents:
diff changeset
2285 */
Ritor1
parents:
diff changeset
2286 png_create_colormap_entry(display, 254, back_r, back_g, back_b,
Ritor1
parents:
diff changeset
2287 back_alpha, output_encoding);
Ritor1
parents:
diff changeset
2288 }
Ritor1
parents:
diff changeset
2289
Ritor1
parents:
diff changeset
2290 else
Ritor1
parents:
diff changeset
2291 output_processing = PNG_CMAP_NONE;
Ritor1
parents:
diff changeset
2292 }
Ritor1
parents:
diff changeset
2293 break;
Ritor1
parents:
diff changeset
2294
Ritor1
parents:
diff changeset
2295 case PNG_COLOR_TYPE_GRAY_ALPHA:
Ritor1
parents:
diff changeset
2296 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
Ritor1
parents:
diff changeset
2297 * of 65536 combinations. If, however, the alpha channel is to be
Ritor1
parents:
diff changeset
2298 * removed there are only 256 possibilities if the background is gray.
Ritor1
parents:
diff changeset
2299 * (Otherwise there is a subset of the 65536 possibilities defined by
Ritor1
parents:
diff changeset
2300 * the triangle between black, white and the background color.)
Ritor1
parents:
diff changeset
2301 *
Ritor1
parents:
diff changeset
2302 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
Ritor1
parents:
diff changeset
2303 * worry about tRNS matching - tRNS is ignored if there is an alpha
Ritor1
parents:
diff changeset
2304 * channel.
Ritor1
parents:
diff changeset
2305 */
Ritor1
parents:
diff changeset
2306 data_encoding = P_sRGB;
Ritor1
parents:
diff changeset
2307
Ritor1
parents:
diff changeset
2308 if (output_format & PNG_FORMAT_FLAG_ALPHA)
Ritor1
parents:
diff changeset
2309 {
Ritor1
parents:
diff changeset
2310 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
Ritor1
parents:
diff changeset
2311 png_error(png_ptr, "gray+alpha color-map: too few entries");
Ritor1
parents:
diff changeset
2312
Ritor1
parents:
diff changeset
2313 cmap_entries = make_ga_colormap(display);
Ritor1
parents:
diff changeset
2314
Ritor1
parents:
diff changeset
2315 background_index = PNG_CMAP_GA_BACKGROUND;
Ritor1
parents:
diff changeset
2316 output_processing = PNG_CMAP_GA;
Ritor1
parents:
diff changeset
2317 }
Ritor1
parents:
diff changeset
2318
Ritor1
parents:
diff changeset
2319 else /* alpha is removed */
Ritor1
parents:
diff changeset
2320 {
Ritor1
parents:
diff changeset
2321 /* Alpha must be removed as the PNG data is processed when the
Ritor1
parents:
diff changeset
2322 * background is a color because the G and A channels are
Ritor1
parents:
diff changeset
2323 * independent and the vector addition (non-parallel vectors) is a
Ritor1
parents:
diff changeset
2324 * 2-D problem.
Ritor1
parents:
diff changeset
2325 *
Ritor1
parents:
diff changeset
2326 * This can be reduced to the same algorithm as above by making a
Ritor1
parents:
diff changeset
2327 * colormap containing gray levels (for the opaque grays), a
Ritor1
parents:
diff changeset
2328 * background entry (for a transparent pixel) and a set of four six
Ritor1
parents:
diff changeset
2329 * level color values, one set for each intermediate alpha value.
Ritor1
parents:
diff changeset
2330 * See the comments in make_ga_colormap for how this works in the
Ritor1
parents:
diff changeset
2331 * per-pixel processing.
Ritor1
parents:
diff changeset
2332 *
Ritor1
parents:
diff changeset
2333 * If the background is gray, however, we only need a 256 entry gray
Ritor1
parents:
diff changeset
2334 * level color map. It is sufficient to make the entry generated
Ritor1
parents:
diff changeset
2335 * for the background color be exactly the color specified.
Ritor1
parents:
diff changeset
2336 */
Ritor1
parents:
diff changeset
2337 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
Ritor1
parents:
diff changeset
2338 (back_r == back_g && back_g == back_b))
Ritor1
parents:
diff changeset
2339 {
Ritor1
parents:
diff changeset
2340 /* Background is gray; no special processing will be required. */
Ritor1
parents:
diff changeset
2341 png_color_16 c;
Ritor1
parents:
diff changeset
2342 png_uint_32 gray = back_g;
Ritor1
parents:
diff changeset
2343
Ritor1
parents:
diff changeset
2344 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
Ritor1
parents:
diff changeset
2345 png_error(png_ptr, "gray-alpha color-map: too few entries");
Ritor1
parents:
diff changeset
2346
Ritor1
parents:
diff changeset
2347 cmap_entries = make_gray_colormap(display);
Ritor1
parents:
diff changeset
2348
Ritor1
parents:
diff changeset
2349 if (output_encoding == P_LINEAR)
Ritor1
parents:
diff changeset
2350 {
Ritor1
parents:
diff changeset
2351 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
Ritor1
parents:
diff changeset
2352
Ritor1
parents:
diff changeset
2353 /* And make sure the corresponding palette entry matches. */
Ritor1
parents:
diff changeset
2354 png_create_colormap_entry(display, gray, back_g, back_g,
Ritor1
parents:
diff changeset
2355 back_g, 65535, P_LINEAR);
Ritor1
parents:
diff changeset
2356 }
Ritor1
parents:
diff changeset
2357
Ritor1
parents:
diff changeset
2358 /* The background passed to libpng, however, must be the sRGB
Ritor1
parents:
diff changeset
2359 * value.
Ritor1
parents:
diff changeset
2360 */
Ritor1
parents:
diff changeset
2361 c.index = 0; /*unused*/
Ritor1
parents:
diff changeset
2362 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
Ritor1
parents:
diff changeset
2363
Ritor1
parents:
diff changeset
2364 png_set_background_fixed(png_ptr, &c,
Ritor1
parents:
diff changeset
2365 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
Ritor1
parents:
diff changeset
2366 0/*gamma: not used*/);
Ritor1
parents:
diff changeset
2367
Ritor1
parents:
diff changeset
2368 output_processing = PNG_CMAP_NONE;
Ritor1
parents:
diff changeset
2369 }
Ritor1
parents:
diff changeset
2370
Ritor1
parents:
diff changeset
2371 else
Ritor1
parents:
diff changeset
2372 {
Ritor1
parents:
diff changeset
2373 png_uint_32 i, a;
Ritor1
parents:
diff changeset
2374
Ritor1
parents:
diff changeset
2375 /* This is the same as png_make_ga_colormap, above, except that
Ritor1
parents:
diff changeset
2376 * the entries are all opaque.
Ritor1
parents:
diff changeset
2377 */
Ritor1
parents:
diff changeset
2378 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
Ritor1
parents:
diff changeset
2379 png_error(png_ptr, "ga-alpha color-map: too few entries");
Ritor1
parents:
diff changeset
2380
Ritor1
parents:
diff changeset
2381 i = 0;
Ritor1
parents:
diff changeset
2382 while (i < 231)
Ritor1
parents:
diff changeset
2383 {
Ritor1
parents:
diff changeset
2384 png_uint_32 gray = (i * 256 + 115) / 231;
Ritor1
parents:
diff changeset
2385 png_create_colormap_entry(display, i++, gray, gray, gray,
Ritor1
parents:
diff changeset
2386 255, P_sRGB);
Ritor1
parents:
diff changeset
2387 }
Ritor1
parents:
diff changeset
2388
Ritor1
parents:
diff changeset
2389 /* NOTE: this preserves the full precision of the application
Ritor1
parents:
diff changeset
2390 * background color.
Ritor1
parents:
diff changeset
2391 */
Ritor1
parents:
diff changeset
2392 background_index = i;
Ritor1
parents:
diff changeset
2393 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
Ritor1
parents:
diff changeset
2394 output_encoding == P_LINEAR ? 65535U : 255U, output_encoding);
Ritor1
parents:
diff changeset
2395
Ritor1
parents:
diff changeset
2396 /* For non-opaque input composite on the sRGB background - this
Ritor1
parents:
diff changeset
2397 * requires inverting the encoding for each component. The input
Ritor1
parents:
diff changeset
2398 * is still converted to the sRGB encoding because this is a
Ritor1
parents:
diff changeset
2399 * reasonable approximate to the logarithmic curve of human
Ritor1
parents:
diff changeset
2400 * visual sensitivity, at least over the narrow range which PNG
Ritor1
parents:
diff changeset
2401 * represents. Consequently 'G' is always sRGB encoded, while
Ritor1
parents:
diff changeset
2402 * 'A' is linear. We need the linear background colors.
Ritor1
parents:
diff changeset
2403 */
Ritor1
parents:
diff changeset
2404 if (output_encoding == P_sRGB) /* else already linear */
Ritor1
parents:
diff changeset
2405 {
Ritor1
parents:
diff changeset
2406 /* This may produce a value not exactly matching the
Ritor1
parents:
diff changeset
2407 * background, but that's ok because these numbers are only
Ritor1
parents:
diff changeset
2408 * used when alpha != 0
Ritor1
parents:
diff changeset
2409 */
Ritor1
parents:
diff changeset
2410 back_r = png_sRGB_table[back_r];
Ritor1
parents:
diff changeset
2411 back_g = png_sRGB_table[back_g];
Ritor1
parents:
diff changeset
2412 back_b = png_sRGB_table[back_b];
Ritor1
parents:
diff changeset
2413 }
Ritor1
parents:
diff changeset
2414
Ritor1
parents:
diff changeset
2415 for (a=1; a<5; ++a)
Ritor1
parents:
diff changeset
2416 {
Ritor1
parents:
diff changeset
2417 unsigned int g;
Ritor1
parents:
diff changeset
2418
Ritor1
parents:
diff changeset
2419 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
Ritor1
parents:
diff changeset
2420 * by an 8-bit alpha value (0..255).
Ritor1
parents:
diff changeset
2421 */
Ritor1
parents:
diff changeset
2422 png_uint_32 alpha = 51 * a;
Ritor1
parents:
diff changeset
2423 png_uint_32 back_rx = (255-alpha) * back_r;
Ritor1
parents:
diff changeset
2424 png_uint_32 back_gx = (255-alpha) * back_g;
Ritor1
parents:
diff changeset
2425 png_uint_32 back_bx = (255-alpha) * back_b;
Ritor1
parents:
diff changeset
2426
Ritor1
parents:
diff changeset
2427 for (g=0; g<6; ++g)
Ritor1
parents:
diff changeset
2428 {
Ritor1
parents:
diff changeset
2429 png_uint_32 gray = png_sRGB_table[g*51] * alpha;
Ritor1
parents:
diff changeset
2430
Ritor1
parents:
diff changeset
2431 png_create_colormap_entry(display, i++,
Ritor1
parents:
diff changeset
2432 PNG_sRGB_FROM_LINEAR(gray + back_rx),
Ritor1
parents:
diff changeset
2433 PNG_sRGB_FROM_LINEAR(gray + back_gx),
Ritor1
parents:
diff changeset
2434 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
Ritor1
parents:
diff changeset
2435 }
Ritor1
parents:
diff changeset
2436 }
Ritor1
parents:
diff changeset
2437
Ritor1
parents:
diff changeset
2438 cmap_entries = i;
Ritor1
parents:
diff changeset
2439 output_processing = PNG_CMAP_GA;
Ritor1
parents:
diff changeset
2440 }
Ritor1
parents:
diff changeset
2441 }
Ritor1
parents:
diff changeset
2442 break;
Ritor1
parents:
diff changeset
2443
Ritor1
parents:
diff changeset
2444 case PNG_COLOR_TYPE_RGB:
Ritor1
parents:
diff changeset
2445 case PNG_COLOR_TYPE_RGB_ALPHA:
Ritor1
parents:
diff changeset
2446 /* Exclude the case where the output is gray; we can always handle this
Ritor1
parents:
diff changeset
2447 * with the cases above.
Ritor1
parents:
diff changeset
2448 */
Ritor1
parents:
diff changeset
2449 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
Ritor1
parents:
diff changeset
2450 {
Ritor1
parents:
diff changeset
2451 /* The color-map will be grayscale, so we may as well convert the
Ritor1
parents:
diff changeset
2452 * input RGB values to a simple grayscale and use the grayscale
Ritor1
parents:
diff changeset
2453 * code above.
Ritor1
parents:
diff changeset
2454 *
Ritor1
parents:
diff changeset
2455 * NOTE: calling this apparently damages the recognition of the
Ritor1
parents:
diff changeset
2456 * transparent color in background color handling; call
Ritor1
parents:
diff changeset
2457 * png_set_tRNS_to_alpha before png_set_background_fixed.
Ritor1
parents:
diff changeset
2458 */
Ritor1
parents:
diff changeset
2459 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
Ritor1
parents:
diff changeset
2460 -1);
Ritor1
parents:
diff changeset
2461 data_encoding = P_sRGB;
Ritor1
parents:
diff changeset
2462
Ritor1
parents:
diff changeset
2463 /* The output will now be one or two 8-bit gray or gray+alpha
Ritor1
parents:
diff changeset
2464 * channels. The more complex case arises when the input has alpha.
Ritor1
parents:
diff changeset
2465 */
Ritor1
parents:
diff changeset
2466 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
Ritor1
parents:
diff changeset
2467 png_ptr->num_trans > 0) &&
Ritor1
parents:
diff changeset
2468 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
Ritor1
parents:
diff changeset
2469 {
Ritor1
parents:
diff changeset
2470 /* Both input and output have an alpha channel, so no background
Ritor1
parents:
diff changeset
2471 * processing is required; just map the GA bytes to the right
Ritor1
parents:
diff changeset
2472 * color-map entry.
Ritor1
parents:
diff changeset
2473 */
Ritor1
parents:
diff changeset
2474 expand_tRNS = 1;
Ritor1
parents:
diff changeset
2475
Ritor1
parents:
diff changeset
2476 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
Ritor1
parents:
diff changeset
2477 png_error(png_ptr, "rgb[ga] color-map: too few entries");
Ritor1
parents:
diff changeset
2478
Ritor1
parents:
diff changeset
2479 cmap_entries = make_ga_colormap(display);
Ritor1
parents:
diff changeset
2480 background_index = PNG_CMAP_GA_BACKGROUND;
Ritor1
parents:
diff changeset
2481 output_processing = PNG_CMAP_GA;
Ritor1
parents:
diff changeset
2482 }
Ritor1
parents:
diff changeset
2483
Ritor1
parents:
diff changeset
2484 else
Ritor1
parents:
diff changeset
2485 {
Ritor1
parents:
diff changeset
2486 /* Either the input or the output has no alpha channel, so there
Ritor1
parents:
diff changeset
2487 * will be no non-opaque pixels in the color-map; it will just be
Ritor1
parents:
diff changeset
2488 * grayscale.
Ritor1
parents:
diff changeset
2489 */
Ritor1
parents:
diff changeset
2490 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
Ritor1
parents:
diff changeset
2491 png_error(png_ptr, "rgb[gray] color-map: too few entries");
Ritor1
parents:
diff changeset
2492
Ritor1
parents:
diff changeset
2493 /* Ideally this code would use libpng to do the gamma correction,
Ritor1
parents:
diff changeset
2494 * but if an input alpha channel is to be removed we will hit the
Ritor1
parents:
diff changeset
2495 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
Ritor1
parents:
diff changeset
2496 * correction bug). Fix this by dropping the gamma correction in
Ritor1
parents:
diff changeset
2497 * this case and doing it in the palette; this will result in
Ritor1
parents:
diff changeset
2498 * duplicate palette entries, but that's better than the
Ritor1
parents:
diff changeset
2499 * alternative of double gamma correction.
Ritor1
parents:
diff changeset
2500 */
Ritor1
parents:
diff changeset
2501 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
Ritor1
parents:
diff changeset
2502 png_ptr->num_trans > 0) &&
Ritor1
parents:
diff changeset
2503 png_gamma_not_sRGB(png_ptr->colorspace.gamma))
Ritor1
parents:
diff changeset
2504 {
Ritor1
parents:
diff changeset
2505 cmap_entries = make_gray_file_colormap(display);
Ritor1
parents:
diff changeset
2506 data_encoding = P_FILE;
Ritor1
parents:
diff changeset
2507 }
Ritor1
parents:
diff changeset
2508
Ritor1
parents:
diff changeset
2509 else
Ritor1
parents:
diff changeset
2510 cmap_entries = make_gray_colormap(display);
Ritor1
parents:
diff changeset
2511
Ritor1
parents:
diff changeset
2512 /* But if the input has alpha or transparency it must be removed
Ritor1
parents:
diff changeset
2513 */
Ritor1
parents:
diff changeset
2514 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
Ritor1
parents:
diff changeset
2515 png_ptr->num_trans > 0)
Ritor1
parents:
diff changeset
2516 {
Ritor1
parents:
diff changeset
2517 png_color_16 c;
Ritor1
parents:
diff changeset
2518 png_uint_32 gray = back_g;
Ritor1
parents:
diff changeset
2519
Ritor1
parents:
diff changeset
2520 /* We need to ensure that the application background exists in
Ritor1
parents:
diff changeset
2521 * the colormap and that completely transparent pixels map to
Ritor1
parents:
diff changeset
2522 * it. Achieve this simply by ensuring that the entry
Ritor1
parents:
diff changeset
2523 * selected for the background really is the background color.
Ritor1
parents:
diff changeset
2524 */
Ritor1
parents:
diff changeset
2525 if (data_encoding == P_FILE) /* from the fixup above */
Ritor1
parents:
diff changeset
2526 {
Ritor1
parents:
diff changeset
2527 /* The app supplied a gray which is in output_encoding, we
Ritor1
parents:
diff changeset
2528 * need to convert it to a value of the input (P_FILE)
Ritor1
parents:
diff changeset
2529 * encoding then set this palette entry to the required
Ritor1
parents:
diff changeset
2530 * output encoding.
Ritor1
parents:
diff changeset
2531 */
Ritor1
parents:
diff changeset
2532 if (output_encoding == P_sRGB)
Ritor1
parents:
diff changeset
2533 gray = png_sRGB_table[gray]; /* now P_LINEAR */
Ritor1
parents:
diff changeset
2534
Ritor1
parents:
diff changeset
2535 gray = PNG_DIV257(png_gamma_16bit_correct(gray,
Ritor1
parents:
diff changeset
2536 png_ptr->colorspace.gamma)); /* now P_FILE */
Ritor1
parents:
diff changeset
2537
Ritor1
parents:
diff changeset
2538 /* And make sure the corresponding palette entry contains
Ritor1
parents:
diff changeset
2539 * exactly the required sRGB value.
Ritor1
parents:
diff changeset
2540 */
Ritor1
parents:
diff changeset
2541 png_create_colormap_entry(display, gray, back_g, back_g,
Ritor1
parents:
diff changeset
2542 back_g, 0/*unused*/, output_encoding);
Ritor1
parents:
diff changeset
2543 }
Ritor1
parents:
diff changeset
2544
Ritor1
parents:
diff changeset
2545 else if (output_encoding == P_LINEAR)
Ritor1
parents:
diff changeset
2546 {
Ritor1
parents:
diff changeset
2547 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
Ritor1
parents:
diff changeset
2548
Ritor1
parents:
diff changeset
2549 /* And make sure the corresponding palette entry matches.
Ritor1
parents:
diff changeset
2550 */
Ritor1
parents:
diff changeset
2551 png_create_colormap_entry(display, gray, back_g, back_g,
Ritor1
parents:
diff changeset
2552 back_g, 0/*unused*/, P_LINEAR);
Ritor1
parents:
diff changeset
2553 }
Ritor1
parents:
diff changeset
2554
Ritor1
parents:
diff changeset
2555 /* The background passed to libpng, however, must be the
Ritor1
parents:
diff changeset
2556 * output (normally sRGB) value.
Ritor1
parents:
diff changeset
2557 */
Ritor1
parents:
diff changeset
2558 c.index = 0; /*unused*/
Ritor1
parents:
diff changeset
2559 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
Ritor1
parents:
diff changeset
2560
Ritor1
parents:
diff changeset
2561 /* NOTE: the following is apparently a bug in libpng. Without
Ritor1
parents:
diff changeset
2562 * it the transparent color recognition in
Ritor1
parents:
diff changeset
2563 * png_set_background_fixed seems to go wrong.
Ritor1
parents:
diff changeset
2564 */
Ritor1
parents:
diff changeset
2565 expand_tRNS = 1;
Ritor1
parents:
diff changeset
2566 png_set_background_fixed(png_ptr, &c,
Ritor1
parents:
diff changeset
2567 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
Ritor1
parents:
diff changeset
2568 0/*gamma: not used*/);
Ritor1
parents:
diff changeset
2569 }
Ritor1
parents:
diff changeset
2570
Ritor1
parents:
diff changeset
2571 output_processing = PNG_CMAP_NONE;
Ritor1
parents:
diff changeset
2572 }
Ritor1
parents:
diff changeset
2573 }
Ritor1
parents:
diff changeset
2574
Ritor1
parents:
diff changeset
2575 else /* output is color */
Ritor1
parents:
diff changeset
2576 {
Ritor1
parents:
diff changeset
2577 /* We could use png_quantize here so long as there is no transparent
Ritor1
parents:
diff changeset
2578 * color or alpha; png_quantize ignores alpha. Easier overall just
Ritor1
parents:
diff changeset
2579 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
Ritor1
parents:
diff changeset
2580 * Consequently we always want libpng to produce sRGB data.
Ritor1
parents:
diff changeset
2581 */
Ritor1
parents:
diff changeset
2582 data_encoding = P_sRGB;
Ritor1
parents:
diff changeset
2583
Ritor1
parents:
diff changeset
2584 /* Is there any transparency or alpha? */
Ritor1
parents:
diff changeset
2585 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
Ritor1
parents:
diff changeset
2586 png_ptr->num_trans > 0)
Ritor1
parents:
diff changeset
2587 {
Ritor1
parents:
diff changeset
2588 /* Is there alpha in the output too? If so all four channels are
Ritor1
parents:
diff changeset
2589 * processed into a special RGB cube with alpha support.
Ritor1
parents:
diff changeset
2590 */
Ritor1
parents:
diff changeset
2591 if (output_format & PNG_FORMAT_FLAG_ALPHA)
Ritor1
parents:
diff changeset
2592 {
Ritor1
parents:
diff changeset
2593 png_uint_32 r;
Ritor1
parents:
diff changeset
2594
Ritor1
parents:
diff changeset
2595 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
Ritor1
parents:
diff changeset
2596 png_error(png_ptr, "rgb+alpha color-map: too few entries");
Ritor1
parents:
diff changeset
2597
Ritor1
parents:
diff changeset
2598 cmap_entries = make_rgb_colormap(display);
Ritor1
parents:
diff changeset
2599
Ritor1
parents:
diff changeset
2600 /* Add a transparent entry. */
Ritor1
parents:
diff changeset
2601 png_create_colormap_entry(display, cmap_entries, 255, 255,
Ritor1
parents:
diff changeset
2602 255, 0, P_sRGB);
Ritor1
parents:
diff changeset
2603
Ritor1
parents:
diff changeset
2604 /* This is stored as the background index for the processing
Ritor1
parents:
diff changeset
2605 * algorithm.
Ritor1
parents:
diff changeset
2606 */
Ritor1
parents:
diff changeset
2607 background_index = cmap_entries++;
Ritor1
parents:
diff changeset
2608
Ritor1
parents:
diff changeset
2609 /* Add 27 r,g,b entries each with alpha 0.5. */
Ritor1
parents:
diff changeset
2610 for (r=0; r<256; r = (r << 1) | 0x7f)
Ritor1
parents:
diff changeset
2611 {
Ritor1
parents:
diff changeset
2612 png_uint_32 g;
Ritor1
parents:
diff changeset
2613
Ritor1
parents:
diff changeset
2614 for (g=0; g<256; g = (g << 1) | 0x7f)
Ritor1
parents:
diff changeset
2615 {
Ritor1
parents:
diff changeset
2616 png_uint_32 b;
Ritor1
parents:
diff changeset
2617
Ritor1
parents:
diff changeset
2618 /* This generates components with the values 0, 127 and
Ritor1
parents:
diff changeset
2619 * 255
Ritor1
parents:
diff changeset
2620 */
Ritor1
parents:
diff changeset
2621 for (b=0; b<256; b = (b << 1) | 0x7f)
Ritor1
parents:
diff changeset
2622 png_create_colormap_entry(display, cmap_entries++,
Ritor1
parents:
diff changeset
2623 r, g, b, 128, P_sRGB);
Ritor1
parents:
diff changeset
2624 }
Ritor1
parents:
diff changeset
2625 }
Ritor1
parents:
diff changeset
2626
Ritor1
parents:
diff changeset
2627 expand_tRNS = 1;
Ritor1
parents:
diff changeset
2628 output_processing = PNG_CMAP_RGB_ALPHA;
Ritor1
parents:
diff changeset
2629 }
Ritor1
parents:
diff changeset
2630
Ritor1
parents:
diff changeset
2631 else
Ritor1
parents:
diff changeset
2632 {
Ritor1
parents:
diff changeset
2633 /* Alpha/transparency must be removed. The background must
Ritor1
parents:
diff changeset
2634 * exist in the color map (achieved by setting adding it after
Ritor1
parents:
diff changeset
2635 * the 666 color-map). If the standard processing code will
Ritor1
parents:
diff changeset
2636 * pick up this entry automatically that's all that is
Ritor1
parents:
diff changeset
2637 * required; libpng can be called to do the background
Ritor1
parents:
diff changeset
2638 * processing.
Ritor1
parents:
diff changeset
2639 */
Ritor1
parents:
diff changeset
2640 unsigned int sample_size =
Ritor1
parents:
diff changeset
2641 PNG_IMAGE_SAMPLE_SIZE(output_format);
Ritor1
parents:
diff changeset
2642 png_uint_32 r, g, b; /* sRGB background */
Ritor1
parents:
diff changeset
2643
Ritor1
parents:
diff changeset
2644 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
Ritor1
parents:
diff changeset
2645 png_error(png_ptr, "rgb-alpha color-map: too few entries");
Ritor1
parents:
diff changeset
2646
Ritor1
parents:
diff changeset
2647 cmap_entries = make_rgb_colormap(display);
Ritor1
parents:
diff changeset
2648
Ritor1
parents:
diff changeset
2649 png_create_colormap_entry(display, cmap_entries, back_r,
Ritor1
parents:
diff changeset
2650 back_g, back_b, 0/*unused*/, output_encoding);
Ritor1
parents:
diff changeset
2651
Ritor1
parents:
diff changeset
2652 if (output_encoding == P_LINEAR)
Ritor1
parents:
diff changeset
2653 {
Ritor1
parents:
diff changeset
2654 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
Ritor1
parents:
diff changeset
2655 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
Ritor1
parents:
diff changeset
2656 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
Ritor1
parents:
diff changeset
2657 }
Ritor1
parents:
diff changeset
2658
Ritor1
parents:
diff changeset
2659 else
Ritor1
parents:
diff changeset
2660 {
Ritor1
parents:
diff changeset
2661 r = back_r;
Ritor1
parents:
diff changeset
2662 g = back_g;
Ritor1
parents:
diff changeset
2663 b = back_g;
Ritor1
parents:
diff changeset
2664 }
Ritor1
parents:
diff changeset
2665
Ritor1
parents:
diff changeset
2666 /* Compare the newly-created color-map entry with the one the
Ritor1
parents:
diff changeset
2667 * PNG_CMAP_RGB algorithm will use. If the two entries don't
Ritor1
parents:
diff changeset
2668 * match, add the new one and set this as the background
Ritor1
parents:
diff changeset
2669 * index.
Ritor1
parents:
diff changeset
2670 */
Ritor1
parents:
diff changeset
2671 if (memcmp((png_const_bytep)display->colormap +
Ritor1
parents:
diff changeset
2672 sample_size * cmap_entries,
Ritor1
parents:
diff changeset
2673 (png_const_bytep)display->colormap +
Ritor1
parents:
diff changeset
2674 sample_size * PNG_RGB_INDEX(r,g,b),
Ritor1
parents:
diff changeset
2675 sample_size) != 0)
Ritor1
parents:
diff changeset
2676 {
Ritor1
parents:
diff changeset
2677 /* The background color must be added. */
Ritor1
parents:
diff changeset
2678 background_index = cmap_entries++;
Ritor1
parents:
diff changeset
2679
Ritor1
parents:
diff changeset
2680 /* Add 27 r,g,b entries each with created by composing with
Ritor1
parents:
diff changeset
2681 * the background at alpha 0.5.
Ritor1
parents:
diff changeset
2682 */
Ritor1
parents:
diff changeset
2683 for (r=0; r<256; r = (r << 1) | 0x7f)
Ritor1
parents:
diff changeset
2684 {
Ritor1
parents:
diff changeset
2685 for (g=0; g<256; g = (g << 1) | 0x7f)
Ritor1
parents:
diff changeset
2686 {
Ritor1
parents:
diff changeset
2687 /* This generates components with the values 0, 127
Ritor1
parents:
diff changeset
2688 * and 255
Ritor1
parents:
diff changeset
2689 */
Ritor1
parents:
diff changeset
2690 for (b=0; b<256; b = (b << 1) | 0x7f)
Ritor1
parents:
diff changeset
2691 png_create_colormap_entry(display, cmap_entries++,
Ritor1
parents:
diff changeset
2692 png_colormap_compose(display, r, P_sRGB, 128,
Ritor1
parents:
diff changeset
2693 back_r, output_encoding),
Ritor1
parents:
diff changeset
2694 png_colormap_compose(display, g, P_sRGB, 128,
Ritor1
parents:
diff changeset
2695 back_g, output_encoding),
Ritor1
parents:
diff changeset
2696 png_colormap_compose(display, b, P_sRGB, 128,
Ritor1
parents:
diff changeset
2697 back_b, output_encoding),
Ritor1
parents:
diff changeset
2698 0/*unused*/, output_encoding);
Ritor1
parents:
diff changeset
2699 }
Ritor1
parents:
diff changeset
2700 }
Ritor1
parents:
diff changeset
2701
Ritor1
parents:
diff changeset
2702 expand_tRNS = 1;
Ritor1
parents:
diff changeset
2703 output_processing = PNG_CMAP_RGB_ALPHA;
Ritor1
parents:
diff changeset
2704 }
Ritor1
parents:
diff changeset
2705
Ritor1
parents:
diff changeset
2706 else /* background color is in the standard color-map */
Ritor1
parents:
diff changeset
2707 {
Ritor1
parents:
diff changeset
2708 png_color_16 c;
Ritor1
parents:
diff changeset
2709
Ritor1
parents:
diff changeset
2710 c.index = 0; /*unused*/
Ritor1
parents:
diff changeset
2711 c.red = (png_uint_16)back_r;
Ritor1
parents:
diff changeset
2712 c.gray = c.green = (png_uint_16)back_g;
Ritor1
parents:
diff changeset
2713 c.blue = (png_uint_16)back_b;
Ritor1
parents:
diff changeset
2714
Ritor1
parents:
diff changeset
2715 png_set_background_fixed(png_ptr, &c,
Ritor1
parents:
diff changeset
2716 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
Ritor1
parents:
diff changeset
2717 0/*gamma: not used*/);
Ritor1
parents:
diff changeset
2718
Ritor1
parents:
diff changeset
2719 output_processing = PNG_CMAP_RGB;
Ritor1
parents:
diff changeset
2720 }
Ritor1
parents:
diff changeset
2721 }
Ritor1
parents:
diff changeset
2722 }
Ritor1
parents:
diff changeset
2723
Ritor1
parents:
diff changeset
2724 else /* no alpha or transparency in the input */
Ritor1
parents:
diff changeset
2725 {
Ritor1
parents:
diff changeset
2726 /* Alpha in the output is irrelevant, simply map the opaque input
Ritor1
parents:
diff changeset
2727 * pixels to the 6x6x6 color-map.
Ritor1
parents:
diff changeset
2728 */
Ritor1
parents:
diff changeset
2729 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
Ritor1
parents:
diff changeset
2730 png_error(png_ptr, "rgb color-map: too few entries");
Ritor1
parents:
diff changeset
2731
Ritor1
parents:
diff changeset
2732 cmap_entries = make_rgb_colormap(display);
Ritor1
parents:
diff changeset
2733 output_processing = PNG_CMAP_RGB;
Ritor1
parents:
diff changeset
2734 }
Ritor1
parents:
diff changeset
2735 }
Ritor1
parents:
diff changeset
2736 break;
Ritor1
parents:
diff changeset
2737
Ritor1
parents:
diff changeset
2738 case PNG_COLOR_TYPE_PALETTE:
Ritor1
parents:
diff changeset
2739 /* It's already got a color-map. It may be necessary to eliminate the
Ritor1
parents:
diff changeset
2740 * tRNS entries though.
Ritor1
parents:
diff changeset
2741 */
Ritor1
parents:
diff changeset
2742 {
Ritor1
parents:
diff changeset
2743 unsigned int num_trans = png_ptr->num_trans;
Ritor1
parents:
diff changeset
2744 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
Ritor1
parents:
diff changeset
2745 png_const_colorp colormap = png_ptr->palette;
Ritor1
parents:
diff changeset
2746 const int do_background = trans != NULL &&
Ritor1
parents:
diff changeset
2747 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
Ritor1
parents:
diff changeset
2748 unsigned int i;
Ritor1
parents:
diff changeset
2749
Ritor1
parents:
diff changeset
2750 /* Just in case: */
Ritor1
parents:
diff changeset
2751 if (trans == NULL)
Ritor1
parents:
diff changeset
2752 num_trans = 0;
Ritor1
parents:
diff changeset
2753
Ritor1
parents:
diff changeset
2754 output_processing = PNG_CMAP_NONE;
Ritor1
parents:
diff changeset
2755 data_encoding = P_FILE; /* Don't change from color-map indicies */
Ritor1
parents:
diff changeset
2756 cmap_entries = png_ptr->num_palette;
Ritor1
parents:
diff changeset
2757 if (cmap_entries > 256)
Ritor1
parents:
diff changeset
2758 cmap_entries = 256;
Ritor1
parents:
diff changeset
2759
Ritor1
parents:
diff changeset
2760 if (cmap_entries > image->colormap_entries)
Ritor1
parents:
diff changeset
2761 png_error(png_ptr, "palette color-map: too few entries");
Ritor1
parents:
diff changeset
2762
Ritor1
parents:
diff changeset
2763 for (i=0; i < cmap_entries; ++i)
Ritor1
parents:
diff changeset
2764 {
Ritor1
parents:
diff changeset
2765 if (do_background && i < num_trans && trans[i] < 255)
Ritor1
parents:
diff changeset
2766 {
Ritor1
parents:
diff changeset
2767 if (trans[i] == 0)
Ritor1
parents:
diff changeset
2768 png_create_colormap_entry(display, i, back_r, back_g,
Ritor1
parents:
diff changeset
2769 back_b, 0, output_encoding);
Ritor1
parents:
diff changeset
2770
Ritor1
parents:
diff changeset
2771 else
Ritor1
parents:
diff changeset
2772 {
Ritor1
parents:
diff changeset
2773 /* Must compose the PNG file color in the color-map entry
Ritor1
parents:
diff changeset
2774 * on the sRGB color in 'back'.
Ritor1
parents:
diff changeset
2775 */
Ritor1
parents:
diff changeset
2776 png_create_colormap_entry(display, i,
Ritor1
parents:
diff changeset
2777 png_colormap_compose(display, colormap[i].red, P_FILE,
Ritor1
parents:
diff changeset
2778 trans[i], back_r, output_encoding),
Ritor1
parents:
diff changeset
2779 png_colormap_compose(display, colormap[i].green, P_FILE,
Ritor1
parents:
diff changeset
2780 trans[i], back_g, output_encoding),
Ritor1
parents:
diff changeset
2781 png_colormap_compose(display, colormap[i].blue, P_FILE,
Ritor1
parents:
diff changeset
2782 trans[i], back_b, output_encoding),
Ritor1
parents:
diff changeset
2783 output_encoding == P_LINEAR ? trans[i] * 257U :
Ritor1
parents:
diff changeset
2784 trans[i],
Ritor1
parents:
diff changeset
2785 output_encoding);
Ritor1
parents:
diff changeset
2786 }
Ritor1
parents:
diff changeset
2787 }
Ritor1
parents:
diff changeset
2788
Ritor1
parents:
diff changeset
2789 else
Ritor1
parents:
diff changeset
2790 png_create_colormap_entry(display, i, colormap[i].red,
Ritor1
parents:
diff changeset
2791 colormap[i].green, colormap[i].blue,
Ritor1
parents:
diff changeset
2792 i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
Ritor1
parents:
diff changeset
2793 }
Ritor1
parents:
diff changeset
2794
Ritor1
parents:
diff changeset
2795 /* The PNG data may have indicies packed in fewer than 8 bits, it
Ritor1
parents:
diff changeset
2796 * must be expanded if so.
Ritor1
parents:
diff changeset
2797 */
Ritor1
parents:
diff changeset
2798 if (png_ptr->bit_depth < 8)
Ritor1
parents:
diff changeset
2799 png_set_packing(png_ptr);
Ritor1
parents:
diff changeset
2800 }
Ritor1
parents:
diff changeset
2801 break;
Ritor1
parents:
diff changeset
2802
Ritor1
parents:
diff changeset
2803 default:
Ritor1
parents:
diff changeset
2804 png_error(png_ptr, "invalid PNG color type");
Ritor1
parents:
diff changeset
2805 /*NOT REACHED*/
Ritor1
parents:
diff changeset
2806 break;
Ritor1
parents:
diff changeset
2807 }
Ritor1
parents:
diff changeset
2808
Ritor1
parents:
diff changeset
2809 /* Now deal with the output processing */
Ritor1
parents:
diff changeset
2810 if (expand_tRNS && png_ptr->num_trans > 0 &&
Ritor1
parents:
diff changeset
2811 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
Ritor1
parents:
diff changeset
2812 png_set_tRNS_to_alpha(png_ptr);
Ritor1
parents:
diff changeset
2813
Ritor1
parents:
diff changeset
2814 switch (data_encoding)
Ritor1
parents:
diff changeset
2815 {
Ritor1
parents:
diff changeset
2816 default:
Ritor1
parents:
diff changeset
2817 png_error(png_ptr, "bad data option (internal error)");
Ritor1
parents:
diff changeset
2818 break;
Ritor1
parents:
diff changeset
2819
Ritor1
parents:
diff changeset
2820 case P_sRGB:
Ritor1
parents:
diff changeset
2821 /* Change to 8-bit sRGB */
Ritor1
parents:
diff changeset
2822 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
Ritor1
parents:
diff changeset
2823 /* FALL THROUGH */
Ritor1
parents:
diff changeset
2824
Ritor1
parents:
diff changeset
2825 case P_FILE:
Ritor1
parents:
diff changeset
2826 if (png_ptr->bit_depth > 8)
Ritor1
parents:
diff changeset
2827 png_set_scale_16(png_ptr);
Ritor1
parents:
diff changeset
2828 break;
Ritor1
parents:
diff changeset
2829 }
Ritor1
parents:
diff changeset
2830
Ritor1
parents:
diff changeset
2831 if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
Ritor1
parents:
diff changeset
2832 png_error(png_ptr, "color map overflow (BAD internal error)");
Ritor1
parents:
diff changeset
2833
Ritor1
parents:
diff changeset
2834 image->colormap_entries = cmap_entries;
Ritor1
parents:
diff changeset
2835
Ritor1
parents:
diff changeset
2836 /* Double check using the recorded background index */
Ritor1
parents:
diff changeset
2837 switch (output_processing)
Ritor1
parents:
diff changeset
2838 {
Ritor1
parents:
diff changeset
2839 case PNG_CMAP_NONE:
Ritor1
parents:
diff changeset
2840 if (background_index != PNG_CMAP_NONE_BACKGROUND)
Ritor1
parents:
diff changeset
2841 goto bad_background;
Ritor1
parents:
diff changeset
2842 break;
Ritor1
parents:
diff changeset
2843
Ritor1
parents:
diff changeset
2844 case PNG_CMAP_GA:
Ritor1
parents:
diff changeset
2845 if (background_index != PNG_CMAP_GA_BACKGROUND)
Ritor1
parents:
diff changeset
2846 goto bad_background;
Ritor1
parents:
diff changeset
2847 break;
Ritor1
parents:
diff changeset
2848
Ritor1
parents:
diff changeset
2849 case PNG_CMAP_TRANS:
Ritor1
parents:
diff changeset
2850 if (background_index >= cmap_entries ||
Ritor1
parents:
diff changeset
2851 background_index != PNG_CMAP_TRANS_BACKGROUND)
Ritor1
parents:
diff changeset
2852 goto bad_background;
Ritor1
parents:
diff changeset
2853 break;
Ritor1
parents:
diff changeset
2854
Ritor1
parents:
diff changeset
2855 case PNG_CMAP_RGB:
Ritor1
parents:
diff changeset
2856 if (background_index != PNG_CMAP_RGB_BACKGROUND)
Ritor1
parents:
diff changeset
2857 goto bad_background;
Ritor1
parents:
diff changeset
2858 break;
Ritor1
parents:
diff changeset
2859
Ritor1
parents:
diff changeset
2860 case PNG_CMAP_RGB_ALPHA:
Ritor1
parents:
diff changeset
2861 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
Ritor1
parents:
diff changeset
2862 goto bad_background;
Ritor1
parents:
diff changeset
2863 break;
Ritor1
parents:
diff changeset
2864
Ritor1
parents:
diff changeset
2865 default:
Ritor1
parents:
diff changeset
2866 png_error(png_ptr, "bad processing option (internal error)");
Ritor1
parents:
diff changeset
2867
Ritor1
parents:
diff changeset
2868 bad_background:
Ritor1
parents:
diff changeset
2869 png_error(png_ptr, "bad background index (internal error)");
Ritor1
parents:
diff changeset
2870 }
Ritor1
parents:
diff changeset
2871
Ritor1
parents:
diff changeset
2872 display->colormap_processing = output_processing;
Ritor1
parents:
diff changeset
2873
Ritor1
parents:
diff changeset
2874 return 1/*ok*/;
Ritor1
parents:
diff changeset
2875 }
Ritor1
parents:
diff changeset
2876
Ritor1
parents:
diff changeset
2877 /* The final part of the color-map read called from png_image_finish_read. */
Ritor1
parents:
diff changeset
2878 static int
Ritor1
parents:
diff changeset
2879 png_image_read_and_map(png_voidp argument)
Ritor1
parents:
diff changeset
2880 {
Ritor1
parents:
diff changeset
2881 png_image_read_control *display = png_voidcast(png_image_read_control*,
Ritor1
parents:
diff changeset
2882 argument);
Ritor1
parents:
diff changeset
2883 png_imagep image = display->image;
Ritor1
parents:
diff changeset
2884 png_structrp png_ptr = image->opaque->png_ptr;
Ritor1
parents:
diff changeset
2885 int passes;
Ritor1
parents:
diff changeset
2886
Ritor1
parents:
diff changeset
2887 /* Called when the libpng data must be transformed into the color-mapped
Ritor1
parents:
diff changeset
2888 * form. There is a local row buffer in display->local and this routine must
Ritor1
parents:
diff changeset
2889 * do the interlace handling.
Ritor1
parents:
diff changeset
2890 */
Ritor1
parents:
diff changeset
2891 switch (png_ptr->interlaced)
Ritor1
parents:
diff changeset
2892 {
Ritor1
parents:
diff changeset
2893 case PNG_INTERLACE_NONE:
Ritor1
parents:
diff changeset
2894 passes = 1;
Ritor1
parents:
diff changeset
2895 break;
Ritor1
parents:
diff changeset
2896
Ritor1
parents:
diff changeset
2897 case PNG_INTERLACE_ADAM7:
Ritor1
parents:
diff changeset
2898 passes = PNG_INTERLACE_ADAM7_PASSES;
Ritor1
parents:
diff changeset
2899 break;
Ritor1
parents:
diff changeset
2900
Ritor1
parents:
diff changeset
2901 default:
Ritor1
parents:
diff changeset
2902 png_error(png_ptr, "unknown interlace type");
Ritor1
parents:
diff changeset
2903 }
Ritor1
parents:
diff changeset
2904
Ritor1
parents:
diff changeset
2905 {
Ritor1
parents:
diff changeset
2906 png_uint_32 height = image->height;
Ritor1
parents:
diff changeset
2907 png_uint_32 width = image->width;
Ritor1
parents:
diff changeset
2908 int proc = display->colormap_processing;
Ritor1
parents:
diff changeset
2909 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
Ritor1
parents:
diff changeset
2910 ptrdiff_t step_row = display->row_bytes;
Ritor1
parents:
diff changeset
2911 int pass;
Ritor1
parents:
diff changeset
2912
Ritor1
parents:
diff changeset
2913 for (pass = 0; pass < passes; ++pass)
Ritor1
parents:
diff changeset
2914 {
Ritor1
parents:
diff changeset
2915 unsigned int startx, stepx, stepy;
Ritor1
parents:
diff changeset
2916 png_uint_32 y;
Ritor1
parents:
diff changeset
2917
Ritor1
parents:
diff changeset
2918 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
Ritor1
parents:
diff changeset
2919 {
Ritor1
parents:
diff changeset
2920 /* The row may be empty for a short image: */
Ritor1
parents:
diff changeset
2921 if (PNG_PASS_COLS(width, pass) == 0)
Ritor1
parents:
diff changeset
2922 continue;
Ritor1
parents:
diff changeset
2923
Ritor1
parents:
diff changeset
2924 startx = PNG_PASS_START_COL(pass);
Ritor1
parents:
diff changeset
2925 stepx = PNG_PASS_COL_OFFSET(pass);
Ritor1
parents:
diff changeset
2926 y = PNG_PASS_START_ROW(pass);
Ritor1
parents:
diff changeset
2927 stepy = PNG_PASS_ROW_OFFSET(pass);
Ritor1
parents:
diff changeset
2928 }
Ritor1
parents:
diff changeset
2929
Ritor1
parents:
diff changeset
2930 else
Ritor1
parents:
diff changeset
2931 {
Ritor1
parents:
diff changeset
2932 y = 0;
Ritor1
parents:
diff changeset
2933 startx = 0;
Ritor1
parents:
diff changeset
2934 stepx = stepy = 1;
Ritor1
parents:
diff changeset
2935 }
Ritor1
parents:
diff changeset
2936
Ritor1
parents:
diff changeset
2937 for (; y<height; y += stepy)
Ritor1
parents:
diff changeset
2938 {
Ritor1
parents:
diff changeset
2939 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
Ritor1
parents:
diff changeset
2940 png_bytep outrow = first_row + y * step_row;
Ritor1
parents:
diff changeset
2941 png_const_bytep end_row = outrow + width;
Ritor1
parents:
diff changeset
2942
Ritor1
parents:
diff changeset
2943 /* Read read the libpng data into the temporary buffer. */
Ritor1
parents:
diff changeset
2944 png_read_row(png_ptr, inrow, NULL);
Ritor1
parents:
diff changeset
2945
Ritor1
parents:
diff changeset
2946 /* Now process the row according to the processing option, note
Ritor1
parents:
diff changeset
2947 * that the caller verifies that the format of the libpng output
Ritor1
parents:
diff changeset
2948 * data is as required.
Ritor1
parents:
diff changeset
2949 */
Ritor1
parents:
diff changeset
2950 outrow += startx;
Ritor1
parents:
diff changeset
2951 switch (proc)
Ritor1
parents:
diff changeset
2952 {
Ritor1
parents:
diff changeset
2953 case PNG_CMAP_GA:
Ritor1
parents:
diff changeset
2954 for (; outrow < end_row; outrow += stepx)
Ritor1
parents:
diff changeset
2955 {
Ritor1
parents:
diff changeset
2956 /* The data is always in the PNG order */
Ritor1
parents:
diff changeset
2957 unsigned int gray = *inrow++;
Ritor1
parents:
diff changeset
2958 unsigned int alpha = *inrow++;
Ritor1
parents:
diff changeset
2959 unsigned int entry;
Ritor1
parents:
diff changeset
2960
Ritor1
parents:
diff changeset
2961 /* NOTE: this code is copied as a comment in
Ritor1
parents:
diff changeset
2962 * make_ga_colormap above. Please update the
Ritor1
parents:
diff changeset
2963 * comment if you change this code!
Ritor1
parents:
diff changeset
2964 */
Ritor1
parents:
diff changeset
2965 if (alpha > 229) /* opaque */
Ritor1
parents:
diff changeset
2966 {
Ritor1
parents:
diff changeset
2967 entry = (231 * gray + 128) >> 8;
Ritor1
parents:
diff changeset
2968 }
Ritor1
parents:
diff changeset
2969 else if (alpha < 26) /* transparent */
Ritor1
parents:
diff changeset
2970 {
Ritor1
parents:
diff changeset
2971 entry = 231;
Ritor1
parents:
diff changeset
2972 }
Ritor1
parents:
diff changeset
2973 else /* partially opaque */
Ritor1
parents:
diff changeset
2974 {
Ritor1
parents:
diff changeset
2975 entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
Ritor1
parents:
diff changeset
2976 }
Ritor1
parents:
diff changeset
2977
Ritor1
parents:
diff changeset
2978 *outrow = (png_byte)entry;
Ritor1
parents:
diff changeset
2979 }
Ritor1
parents:
diff changeset
2980 break;
Ritor1
parents:
diff changeset
2981
Ritor1
parents:
diff changeset
2982 case PNG_CMAP_TRANS:
Ritor1
parents:
diff changeset
2983 for (; outrow < end_row; outrow += stepx)
Ritor1
parents:
diff changeset
2984 {
Ritor1
parents:
diff changeset
2985 png_byte gray = *inrow++;
Ritor1
parents:
diff changeset
2986 png_byte alpha = *inrow++;
Ritor1
parents:
diff changeset
2987
Ritor1
parents:
diff changeset
2988 if (alpha == 0)
Ritor1
parents:
diff changeset
2989 *outrow = PNG_CMAP_TRANS_BACKGROUND;
Ritor1
parents:
diff changeset
2990
Ritor1
parents:
diff changeset
2991 else if (gray != PNG_CMAP_TRANS_BACKGROUND)
Ritor1
parents:
diff changeset
2992 *outrow = gray;
Ritor1
parents:
diff changeset
2993
Ritor1
parents:
diff changeset
2994 else
Ritor1
parents:
diff changeset
2995 *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
Ritor1
parents:
diff changeset
2996 }
Ritor1
parents:
diff changeset
2997 break;
Ritor1
parents:
diff changeset
2998
Ritor1
parents:
diff changeset
2999 case PNG_CMAP_RGB:
Ritor1
parents:
diff changeset
3000 for (; outrow < end_row; outrow += stepx)
Ritor1
parents:
diff changeset
3001 {
Ritor1
parents:
diff changeset
3002 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
Ritor1
parents:
diff changeset
3003 inrow += 3;
Ritor1
parents:
diff changeset
3004 }
Ritor1
parents:
diff changeset
3005 break;
Ritor1
parents:
diff changeset
3006
Ritor1
parents:
diff changeset
3007 case PNG_CMAP_RGB_ALPHA:
Ritor1
parents:
diff changeset
3008 for (; outrow < end_row; outrow += stepx)
Ritor1
parents:
diff changeset
3009 {
Ritor1
parents:
diff changeset
3010 unsigned int alpha = inrow[3];
Ritor1
parents:
diff changeset
3011
Ritor1
parents:
diff changeset
3012 /* Because the alpha entries only hold alpha==0.5 values
Ritor1
parents:
diff changeset
3013 * split the processing at alpha==0.25 (64) and 0.75
Ritor1
parents:
diff changeset
3014 * (196).
Ritor1
parents:
diff changeset
3015 */
Ritor1
parents:
diff changeset
3016
Ritor1
parents:
diff changeset
3017 if (alpha >= 196)
Ritor1
parents:
diff changeset
3018 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
Ritor1
parents:
diff changeset
3019 inrow[2]);
Ritor1
parents:
diff changeset
3020
Ritor1
parents:
diff changeset
3021 else if (alpha < 64)
Ritor1
parents:
diff changeset
3022 *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
Ritor1
parents:
diff changeset
3023
Ritor1
parents:
diff changeset
3024 else
Ritor1
parents:
diff changeset
3025 {
Ritor1
parents:
diff changeset
3026 /* Likewise there are three entries for each of r, g
Ritor1
parents:
diff changeset
3027 * and b. We could select the entry by popcount on
Ritor1
parents:
diff changeset
3028 * the top two bits on those architectures that
Ritor1
parents:
diff changeset
3029 * support it, this is what the code below does,
Ritor1
parents:
diff changeset
3030 * crudely.
Ritor1
parents:
diff changeset
3031 */
Ritor1
parents:
diff changeset
3032 unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
Ritor1
parents:
diff changeset
3033
Ritor1
parents:
diff changeset
3034 /* Here are how the values map:
Ritor1
parents:
diff changeset
3035 *
Ritor1
parents:
diff changeset
3036 * 0x00 .. 0x3f -> 0
Ritor1
parents:
diff changeset
3037 * 0x40 .. 0xbf -> 1
Ritor1
parents:
diff changeset
3038 * 0xc0 .. 0xff -> 2
Ritor1
parents:
diff changeset
3039 *
Ritor1
parents:
diff changeset
3040 * So, as above with the explicit alpha checks, the
Ritor1
parents:
diff changeset
3041 * breakpoints are at 64 and 196.
Ritor1
parents:
diff changeset
3042 */
Ritor1
parents:
diff changeset
3043 if (inrow[0] & 0x80) back_i += 9; /* red */
Ritor1
parents:
diff changeset
3044 if (inrow[0] & 0x40) back_i += 9;
Ritor1
parents:
diff changeset
3045 if (inrow[0] & 0x80) back_i += 3; /* green */
Ritor1
parents:
diff changeset
3046 if (inrow[0] & 0x40) back_i += 3;
Ritor1
parents:
diff changeset
3047 if (inrow[0] & 0x80) back_i += 1; /* blue */
Ritor1
parents:
diff changeset
3048 if (inrow[0] & 0x40) back_i += 1;
Ritor1
parents:
diff changeset
3049
Ritor1
parents:
diff changeset
3050 *outrow = (png_byte)back_i;
Ritor1
parents:
diff changeset
3051 }
Ritor1
parents:
diff changeset
3052
Ritor1
parents:
diff changeset
3053 inrow += 4;
Ritor1
parents:
diff changeset
3054 }
Ritor1
parents:
diff changeset
3055 break;
Ritor1
parents:
diff changeset
3056
Ritor1
parents:
diff changeset
3057 default:
Ritor1
parents:
diff changeset
3058 break;
Ritor1
parents:
diff changeset
3059 }
Ritor1
parents:
diff changeset
3060 }
Ritor1
parents:
diff changeset
3061 }
Ritor1
parents:
diff changeset
3062 }
Ritor1
parents:
diff changeset
3063
Ritor1
parents:
diff changeset
3064 return 1;
Ritor1
parents:
diff changeset
3065 }
Ritor1
parents:
diff changeset
3066
Ritor1
parents:
diff changeset
3067 static int
Ritor1
parents:
diff changeset
3068 png_image_read_colormapped(png_voidp argument)
Ritor1
parents:
diff changeset
3069 {
Ritor1
parents:
diff changeset
3070 png_image_read_control *display = png_voidcast(png_image_read_control*,
Ritor1
parents:
diff changeset
3071 argument);
Ritor1
parents:
diff changeset
3072 png_imagep image = display->image;
Ritor1
parents:
diff changeset
3073 png_controlp control = image->opaque;
Ritor1
parents:
diff changeset
3074 png_structrp png_ptr = control->png_ptr;
Ritor1
parents:
diff changeset
3075 png_inforp info_ptr = control->info_ptr;
Ritor1
parents:
diff changeset
3076
Ritor1
parents:
diff changeset
3077 int passes = 0; /* As a flag */
Ritor1
parents:
diff changeset
3078
Ritor1
parents:
diff changeset
3079 PNG_SKIP_CHUNKS(png_ptr);
Ritor1
parents:
diff changeset
3080
Ritor1
parents:
diff changeset
3081 /* Update the 'info' structure and make sure the result is as required; first
Ritor1
parents:
diff changeset
3082 * make sure to turn on the interlace handling if it will be required
Ritor1
parents:
diff changeset
3083 * (because it can't be turned on *after* the call to png_read_update_info!)
Ritor1
parents:
diff changeset
3084 */
Ritor1
parents:
diff changeset
3085 if (display->colormap_processing == PNG_CMAP_NONE)
Ritor1
parents:
diff changeset
3086 passes = png_set_interlace_handling(png_ptr);
Ritor1
parents:
diff changeset
3087
Ritor1
parents:
diff changeset
3088 png_read_update_info(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
3089
Ritor1
parents:
diff changeset
3090 /* The expected output can be deduced from the colormap_processing option. */
Ritor1
parents:
diff changeset
3091 switch (display->colormap_processing)
Ritor1
parents:
diff changeset
3092 {
Ritor1
parents:
diff changeset
3093 case PNG_CMAP_NONE:
Ritor1
parents:
diff changeset
3094 /* Output must be one channel and one byte per pixel, the output
Ritor1
parents:
diff changeset
3095 * encoding can be anything.
Ritor1
parents:
diff changeset
3096 */
Ritor1
parents:
diff changeset
3097 if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
Ritor1
parents:
diff changeset
3098 info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
Ritor1
parents:
diff changeset
3099 info_ptr->bit_depth == 8)
Ritor1
parents:
diff changeset
3100 break;
Ritor1
parents:
diff changeset
3101
Ritor1
parents:
diff changeset
3102 goto bad_output;
Ritor1
parents:
diff changeset
3103
Ritor1
parents:
diff changeset
3104 case PNG_CMAP_TRANS:
Ritor1
parents:
diff changeset
3105 case PNG_CMAP_GA:
Ritor1
parents:
diff changeset
3106 /* Output must be two channels and the 'G' one must be sRGB, the latter
Ritor1
parents:
diff changeset
3107 * can be checked with an exact number because it should have been set
Ritor1
parents:
diff changeset
3108 * to this number above!
Ritor1
parents:
diff changeset
3109 */
Ritor1
parents:
diff changeset
3110 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
Ritor1
parents:
diff changeset
3111 info_ptr->bit_depth == 8 &&
Ritor1
parents:
diff changeset
3112 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
Ritor1
parents:
diff changeset
3113 image->colormap_entries == 256)
Ritor1
parents:
diff changeset
3114 break;
Ritor1
parents:
diff changeset
3115
Ritor1
parents:
diff changeset
3116 goto bad_output;
Ritor1
parents:
diff changeset
3117
Ritor1
parents:
diff changeset
3118 case PNG_CMAP_RGB:
Ritor1
parents:
diff changeset
3119 /* Output must be 8-bit sRGB encoded RGB */
Ritor1
parents:
diff changeset
3120 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
Ritor1
parents:
diff changeset
3121 info_ptr->bit_depth == 8 &&
Ritor1
parents:
diff changeset
3122 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
Ritor1
parents:
diff changeset
3123 image->colormap_entries == 216)
Ritor1
parents:
diff changeset
3124 break;
Ritor1
parents:
diff changeset
3125
Ritor1
parents:
diff changeset
3126 goto bad_output;
Ritor1
parents:
diff changeset
3127
Ritor1
parents:
diff changeset
3128 case PNG_CMAP_RGB_ALPHA:
Ritor1
parents:
diff changeset
3129 /* Output must be 8-bit sRGB encoded RGBA */
Ritor1
parents:
diff changeset
3130 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
Ritor1
parents:
diff changeset
3131 info_ptr->bit_depth == 8 &&
Ritor1
parents:
diff changeset
3132 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
Ritor1
parents:
diff changeset
3133 image->colormap_entries == 244 /* 216 + 1 + 27 */)
Ritor1
parents:
diff changeset
3134 break;
Ritor1
parents:
diff changeset
3135
Ritor1
parents:
diff changeset
3136 /* goto bad_output; */
Ritor1
parents:
diff changeset
3137 /* FALL THROUGH */
Ritor1
parents:
diff changeset
3138
Ritor1
parents:
diff changeset
3139 default:
Ritor1
parents:
diff changeset
3140 bad_output:
Ritor1
parents:
diff changeset
3141 png_error(png_ptr, "bad color-map processing (internal error)");
Ritor1
parents:
diff changeset
3142 }
Ritor1
parents:
diff changeset
3143
Ritor1
parents:
diff changeset
3144 /* Now read the rows. Do this here if it is possible to read directly into
Ritor1
parents:
diff changeset
3145 * the output buffer, otherwise allocate a local row buffer of the maximum
Ritor1
parents:
diff changeset
3146 * size libpng requires and call the relevant processing routine safely.
Ritor1
parents:
diff changeset
3147 */
Ritor1
parents:
diff changeset
3148 {
Ritor1
parents:
diff changeset
3149 png_voidp first_row = display->buffer;
Ritor1
parents:
diff changeset
3150 ptrdiff_t row_bytes = display->row_stride;
Ritor1
parents:
diff changeset
3151
Ritor1
parents:
diff changeset
3152 /* The following expression is designed to work correctly whether it gives
Ritor1
parents:
diff changeset
3153 * a signed or an unsigned result.
Ritor1
parents:
diff changeset
3154 */
Ritor1
parents:
diff changeset
3155 if (row_bytes < 0)
Ritor1
parents:
diff changeset
3156 {
Ritor1
parents:
diff changeset
3157 char *ptr = png_voidcast(char*, first_row);
Ritor1
parents:
diff changeset
3158 ptr += (image->height-1) * (-row_bytes);
Ritor1
parents:
diff changeset
3159 first_row = png_voidcast(png_voidp, ptr);
Ritor1
parents:
diff changeset
3160 }
Ritor1
parents:
diff changeset
3161
Ritor1
parents:
diff changeset
3162 display->first_row = first_row;
Ritor1
parents:
diff changeset
3163 display->row_bytes = row_bytes;
Ritor1
parents:
diff changeset
3164 }
Ritor1
parents:
diff changeset
3165
Ritor1
parents:
diff changeset
3166 if (passes == 0)
Ritor1
parents:
diff changeset
3167 {
Ritor1
parents:
diff changeset
3168 int result;
Ritor1
parents:
diff changeset
3169 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
Ritor1
parents:
diff changeset
3170
Ritor1
parents:
diff changeset
3171 display->local_row = row;
Ritor1
parents:
diff changeset
3172 result = png_safe_execute(image, png_image_read_and_map, display);
Ritor1
parents:
diff changeset
3173 display->local_row = NULL;
Ritor1
parents:
diff changeset
3174 png_free(png_ptr, row);
Ritor1
parents:
diff changeset
3175
Ritor1
parents:
diff changeset
3176 return result;
Ritor1
parents:
diff changeset
3177 }
Ritor1
parents:
diff changeset
3178
Ritor1
parents:
diff changeset
3179 else
Ritor1
parents:
diff changeset
3180 {
Ritor1
parents:
diff changeset
3181 png_alloc_size_t row_bytes = display->row_bytes;
Ritor1
parents:
diff changeset
3182
Ritor1
parents:
diff changeset
3183 while (--passes >= 0)
Ritor1
parents:
diff changeset
3184 {
Ritor1
parents:
diff changeset
3185 png_uint_32 y = image->height;
Ritor1
parents:
diff changeset
3186 png_bytep row = png_voidcast(png_bytep, display->first_row);
Ritor1
parents:
diff changeset
3187
Ritor1
parents:
diff changeset
3188 while (y-- > 0)
Ritor1
parents:
diff changeset
3189 {
Ritor1
parents:
diff changeset
3190 png_read_row(png_ptr, row, NULL);
Ritor1
parents:
diff changeset
3191 row += row_bytes;
Ritor1
parents:
diff changeset
3192 }
Ritor1
parents:
diff changeset
3193 }
Ritor1
parents:
diff changeset
3194
Ritor1
parents:
diff changeset
3195 return 1;
Ritor1
parents:
diff changeset
3196 }
Ritor1
parents:
diff changeset
3197 }
Ritor1
parents:
diff changeset
3198
Ritor1
parents:
diff changeset
3199 /* Just the row reading part of png_image_read. */
Ritor1
parents:
diff changeset
3200 static int
Ritor1
parents:
diff changeset
3201 png_image_read_composite(png_voidp argument)
Ritor1
parents:
diff changeset
3202 {
Ritor1
parents:
diff changeset
3203 png_image_read_control *display = png_voidcast(png_image_read_control*,
Ritor1
parents:
diff changeset
3204 argument);
Ritor1
parents:
diff changeset
3205 png_imagep image = display->image;
Ritor1
parents:
diff changeset
3206 png_structrp png_ptr = image->opaque->png_ptr;
Ritor1
parents:
diff changeset
3207 int passes;
Ritor1
parents:
diff changeset
3208
Ritor1
parents:
diff changeset
3209 switch (png_ptr->interlaced)
Ritor1
parents:
diff changeset
3210 {
Ritor1
parents:
diff changeset
3211 case PNG_INTERLACE_NONE:
Ritor1
parents:
diff changeset
3212 passes = 1;
Ritor1
parents:
diff changeset
3213 break;
Ritor1
parents:
diff changeset
3214
Ritor1
parents:
diff changeset
3215 case PNG_INTERLACE_ADAM7:
Ritor1
parents:
diff changeset
3216 passes = PNG_INTERLACE_ADAM7_PASSES;
Ritor1
parents:
diff changeset
3217 break;
Ritor1
parents:
diff changeset
3218
Ritor1
parents:
diff changeset
3219 default:
Ritor1
parents:
diff changeset
3220 png_error(png_ptr, "unknown interlace type");
Ritor1
parents:
diff changeset
3221 }
Ritor1
parents:
diff changeset
3222
Ritor1
parents:
diff changeset
3223 {
Ritor1
parents:
diff changeset
3224 png_uint_32 height = image->height;
Ritor1
parents:
diff changeset
3225 png_uint_32 width = image->width;
Ritor1
parents:
diff changeset
3226 ptrdiff_t step_row = display->row_bytes;
Ritor1
parents:
diff changeset
3227 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
Ritor1
parents:
diff changeset
3228 int pass;
Ritor1
parents:
diff changeset
3229
Ritor1
parents:
diff changeset
3230 for (pass = 0; pass < passes; ++pass)
Ritor1
parents:
diff changeset
3231 {
Ritor1
parents:
diff changeset
3232 unsigned int startx, stepx, stepy;
Ritor1
parents:
diff changeset
3233 png_uint_32 y;
Ritor1
parents:
diff changeset
3234
Ritor1
parents:
diff changeset
3235 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
Ritor1
parents:
diff changeset
3236 {
Ritor1
parents:
diff changeset
3237 /* The row may be empty for a short image: */
Ritor1
parents:
diff changeset
3238 if (PNG_PASS_COLS(width, pass) == 0)
Ritor1
parents:
diff changeset
3239 continue;
Ritor1
parents:
diff changeset
3240
Ritor1
parents:
diff changeset
3241 startx = PNG_PASS_START_COL(pass) * channels;
Ritor1
parents:
diff changeset
3242 stepx = PNG_PASS_COL_OFFSET(pass) * channels;
Ritor1
parents:
diff changeset
3243 y = PNG_PASS_START_ROW(pass);
Ritor1
parents:
diff changeset
3244 stepy = PNG_PASS_ROW_OFFSET(pass);
Ritor1
parents:
diff changeset
3245 }
Ritor1
parents:
diff changeset
3246
Ritor1
parents:
diff changeset
3247 else
Ritor1
parents:
diff changeset
3248 {
Ritor1
parents:
diff changeset
3249 y = 0;
Ritor1
parents:
diff changeset
3250 startx = 0;
Ritor1
parents:
diff changeset
3251 stepx = channels;
Ritor1
parents:
diff changeset
3252 stepy = 1;
Ritor1
parents:
diff changeset
3253 }
Ritor1
parents:
diff changeset
3254
Ritor1
parents:
diff changeset
3255 for (; y<height; y += stepy)
Ritor1
parents:
diff changeset
3256 {
Ritor1
parents:
diff changeset
3257 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
Ritor1
parents:
diff changeset
3258 png_bytep outrow;
Ritor1
parents:
diff changeset
3259 png_const_bytep end_row;
Ritor1
parents:
diff changeset
3260
Ritor1
parents:
diff changeset
3261 /* Read the row, which is packed: */
Ritor1
parents:
diff changeset
3262 png_read_row(png_ptr, inrow, NULL);
Ritor1
parents:
diff changeset
3263
Ritor1
parents:
diff changeset
3264 outrow = png_voidcast(png_bytep, display->first_row);
Ritor1
parents:
diff changeset
3265 outrow += y * step_row;
Ritor1
parents:
diff changeset
3266 end_row = outrow + width * channels;
Ritor1
parents:
diff changeset
3267
Ritor1
parents:
diff changeset
3268 /* Now do the composition on each pixel in this row. */
Ritor1
parents:
diff changeset
3269 outrow += startx;
Ritor1
parents:
diff changeset
3270 for (; outrow < end_row; outrow += stepx)
Ritor1
parents:
diff changeset
3271 {
Ritor1
parents:
diff changeset
3272 png_byte alpha = inrow[channels];
Ritor1
parents:
diff changeset
3273
Ritor1
parents:
diff changeset
3274 if (alpha > 0) /* else no change to the output */
Ritor1
parents:
diff changeset
3275 {
Ritor1
parents:
diff changeset
3276 unsigned int c;
Ritor1
parents:
diff changeset
3277
Ritor1
parents:
diff changeset
3278 for (c=0; c<channels; ++c)
Ritor1
parents:
diff changeset
3279 {
Ritor1
parents:
diff changeset
3280 png_uint_32 component = inrow[c];
Ritor1
parents:
diff changeset
3281
Ritor1
parents:
diff changeset
3282 if (alpha < 255) /* else just use component */
Ritor1
parents:
diff changeset
3283 {
Ritor1
parents:
diff changeset
3284 /* This is PNG_OPTIMIZED_ALPHA, the component value
Ritor1
parents:
diff changeset
3285 * is a linear 8-bit value. Combine this with the
Ritor1
parents:
diff changeset
3286 * current outrow[c] value which is sRGB encoded.
Ritor1
parents:
diff changeset
3287 * Arithmetic here is 16-bits to preserve the output
Ritor1
parents:
diff changeset
3288 * values correctly.
Ritor1
parents:
diff changeset
3289 */
Ritor1
parents:
diff changeset
3290 component *= 257*255; /* =65535 */
Ritor1
parents:
diff changeset
3291 component += (255-alpha)*png_sRGB_table[outrow[c]];
Ritor1
parents:
diff changeset
3292
Ritor1
parents:
diff changeset
3293 /* So 'component' is scaled by 255*65535 and is
Ritor1
parents:
diff changeset
3294 * therefore appropriate for the sRGB to linear
Ritor1
parents:
diff changeset
3295 * conversion table.
Ritor1
parents:
diff changeset
3296 */
Ritor1
parents:
diff changeset
3297 component = PNG_sRGB_FROM_LINEAR(component);
Ritor1
parents:
diff changeset
3298 }
Ritor1
parents:
diff changeset
3299
Ritor1
parents:
diff changeset
3300 outrow[c] = (png_byte)component;
Ritor1
parents:
diff changeset
3301 }
Ritor1
parents:
diff changeset
3302 }
Ritor1
parents:
diff changeset
3303
Ritor1
parents:
diff changeset
3304 inrow += channels+1; /* components and alpha channel */
Ritor1
parents:
diff changeset
3305 }
Ritor1
parents:
diff changeset
3306 }
Ritor1
parents:
diff changeset
3307 }
Ritor1
parents:
diff changeset
3308 }
Ritor1
parents:
diff changeset
3309
Ritor1
parents:
diff changeset
3310 return 1;
Ritor1
parents:
diff changeset
3311 }
Ritor1
parents:
diff changeset
3312
Ritor1
parents:
diff changeset
3313 /* The do_local_background case; called when all the following transforms are to
Ritor1
parents:
diff changeset
3314 * be done:
Ritor1
parents:
diff changeset
3315 *
Ritor1
parents:
diff changeset
3316 * PNG_RGB_TO_GRAY
Ritor1
parents:
diff changeset
3317 * PNG_COMPOSITE
Ritor1
parents:
diff changeset
3318 * PNG_GAMMA
Ritor1
parents:
diff changeset
3319 *
Ritor1
parents:
diff changeset
3320 * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and
Ritor1
parents:
diff changeset
3321 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
Ritor1
parents:
diff changeset
3322 * correction. The fix-up is to prevent the PNG_COMPOSITE operation happening
Ritor1
parents:
diff changeset
3323 * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles
Ritor1
parents:
diff changeset
3324 * the removal or pre-multiplication of the alpha channel.
Ritor1
parents:
diff changeset
3325 */
Ritor1
parents:
diff changeset
3326 static int
Ritor1
parents:
diff changeset
3327 png_image_read_background(png_voidp argument)
Ritor1
parents:
diff changeset
3328 {
Ritor1
parents:
diff changeset
3329 png_image_read_control *display = png_voidcast(png_image_read_control*,
Ritor1
parents:
diff changeset
3330 argument);
Ritor1
parents:
diff changeset
3331 png_imagep image = display->image;
Ritor1
parents:
diff changeset
3332 png_structrp png_ptr = image->opaque->png_ptr;
Ritor1
parents:
diff changeset
3333 png_inforp info_ptr = image->opaque->info_ptr;
Ritor1
parents:
diff changeset
3334 png_uint_32 height = image->height;
Ritor1
parents:
diff changeset
3335 png_uint_32 width = image->width;
Ritor1
parents:
diff changeset
3336 int pass, passes;
Ritor1
parents:
diff changeset
3337
Ritor1
parents:
diff changeset
3338 /* Double check the convoluted logic below. We expect to get here with
Ritor1
parents:
diff changeset
3339 * libpng doing rgb to gray and gamma correction but background processing
Ritor1
parents:
diff changeset
3340 * left to the png_image_read_background function. The rows libpng produce
Ritor1
parents:
diff changeset
3341 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
Ritor1
parents:
diff changeset
3342 */
Ritor1
parents:
diff changeset
3343 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
Ritor1
parents:
diff changeset
3344 png_error(png_ptr, "lost rgb to gray");
Ritor1
parents:
diff changeset
3345
Ritor1
parents:
diff changeset
3346 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
Ritor1
parents:
diff changeset
3347 png_error(png_ptr, "unexpected compose");
Ritor1
parents:
diff changeset
3348
Ritor1
parents:
diff changeset
3349 if (png_get_channels(png_ptr, info_ptr) != 2)
Ritor1
parents:
diff changeset
3350 png_error(png_ptr, "lost/gained channels");
Ritor1
parents:
diff changeset
3351
Ritor1
parents:
diff changeset
3352 /* Expect the 8-bit case to always remove the alpha channel */
Ritor1
parents:
diff changeset
3353 if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
Ritor1
parents:
diff changeset
3354 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
Ritor1
parents:
diff changeset
3355 png_error(png_ptr, "unexpected 8-bit transformation");
Ritor1
parents:
diff changeset
3356
Ritor1
parents:
diff changeset
3357 switch (png_ptr->interlaced)
Ritor1
parents:
diff changeset
3358 {
Ritor1
parents:
diff changeset
3359 case PNG_INTERLACE_NONE:
Ritor1
parents:
diff changeset
3360 passes = 1;
Ritor1
parents:
diff changeset
3361 break;
Ritor1
parents:
diff changeset
3362
Ritor1
parents:
diff changeset
3363 case PNG_INTERLACE_ADAM7:
Ritor1
parents:
diff changeset
3364 passes = PNG_INTERLACE_ADAM7_PASSES;
Ritor1
parents:
diff changeset
3365 break;
Ritor1
parents:
diff changeset
3366
Ritor1
parents:
diff changeset
3367 default:
Ritor1
parents:
diff changeset
3368 png_error(png_ptr, "unknown interlace type");
Ritor1
parents:
diff changeset
3369 }
Ritor1
parents:
diff changeset
3370
Ritor1
parents:
diff changeset
3371 /* Use direct access to info_ptr here because otherwise the simplified API
Ritor1
parents:
diff changeset
3372 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
Ritor1
parents:
diff changeset
3373 * checking the value after libpng expansions, not the original value in the
Ritor1
parents:
diff changeset
3374 * PNG.
Ritor1
parents:
diff changeset
3375 */
Ritor1
parents:
diff changeset
3376 switch (info_ptr->bit_depth)
Ritor1
parents:
diff changeset
3377 {
Ritor1
parents:
diff changeset
3378 default:
Ritor1
parents:
diff changeset
3379 png_error(png_ptr, "unexpected bit depth");
Ritor1
parents:
diff changeset
3380 break;
Ritor1
parents:
diff changeset
3381
Ritor1
parents:
diff changeset
3382 case 8:
Ritor1
parents:
diff changeset
3383 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
Ritor1
parents:
diff changeset
3384 * to be removed by composing on a background: either the row if
Ritor1
parents:
diff changeset
3385 * display->background is NULL or display->background->green if not.
Ritor1
parents:
diff changeset
3386 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
Ritor1
parents:
diff changeset
3387 */
Ritor1
parents:
diff changeset
3388 {
Ritor1
parents:
diff changeset
3389 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
Ritor1
parents:
diff changeset
3390 ptrdiff_t step_row = display->row_bytes;
Ritor1
parents:
diff changeset
3391
Ritor1
parents:
diff changeset
3392 for (pass = 0; pass < passes; ++pass)
Ritor1
parents:
diff changeset
3393 {
Ritor1
parents:
diff changeset
3394 png_bytep row = png_voidcast(png_bytep,
Ritor1
parents:
diff changeset
3395 display->first_row);
Ritor1
parents:
diff changeset
3396 unsigned int startx, stepx, stepy;
Ritor1
parents:
diff changeset
3397 png_uint_32 y;
Ritor1
parents:
diff changeset
3398
Ritor1
parents:
diff changeset
3399 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
Ritor1
parents:
diff changeset
3400 {
Ritor1
parents:
diff changeset
3401 /* The row may be empty for a short image: */
Ritor1
parents:
diff changeset
3402 if (PNG_PASS_COLS(width, pass) == 0)
Ritor1
parents:
diff changeset
3403 continue;
Ritor1
parents:
diff changeset
3404
Ritor1
parents:
diff changeset
3405 startx = PNG_PASS_START_COL(pass);
Ritor1
parents:
diff changeset
3406 stepx = PNG_PASS_COL_OFFSET(pass);
Ritor1
parents:
diff changeset
3407 y = PNG_PASS_START_ROW(pass);
Ritor1
parents:
diff changeset
3408 stepy = PNG_PASS_ROW_OFFSET(pass);
Ritor1
parents:
diff changeset
3409 }
Ritor1
parents:
diff changeset
3410
Ritor1
parents:
diff changeset
3411 else
Ritor1
parents:
diff changeset
3412 {
Ritor1
parents:
diff changeset
3413 y = 0;
Ritor1
parents:
diff changeset
3414 startx = 0;
Ritor1
parents:
diff changeset
3415 stepx = stepy = 1;
Ritor1
parents:
diff changeset
3416 }
Ritor1
parents:
diff changeset
3417
Ritor1
parents:
diff changeset
3418 if (display->background == NULL)
Ritor1
parents:
diff changeset
3419 {
Ritor1
parents:
diff changeset
3420 for (; y<height; y += stepy)
Ritor1
parents:
diff changeset
3421 {
Ritor1
parents:
diff changeset
3422 png_bytep inrow = png_voidcast(png_bytep,
Ritor1
parents:
diff changeset
3423 display->local_row);
Ritor1
parents:
diff changeset
3424 png_bytep outrow = first_row + y * step_row;
Ritor1
parents:
diff changeset
3425 png_const_bytep end_row = outrow + width;
Ritor1
parents:
diff changeset
3426
Ritor1
parents:
diff changeset
3427 /* Read the row, which is packed: */
Ritor1
parents:
diff changeset
3428 png_read_row(png_ptr, inrow, NULL);
Ritor1
parents:
diff changeset
3429
Ritor1
parents:
diff changeset
3430 /* Now do the composition on each pixel in this row. */
Ritor1
parents:
diff changeset
3431 outrow += startx;
Ritor1
parents:
diff changeset
3432 for (; outrow < end_row; outrow += stepx)
Ritor1
parents:
diff changeset
3433 {
Ritor1
parents:
diff changeset
3434 png_byte alpha = inrow[1];
Ritor1
parents:
diff changeset
3435
Ritor1
parents:
diff changeset
3436 if (alpha > 0) /* else no change to the output */
Ritor1
parents:
diff changeset
3437 {
Ritor1
parents:
diff changeset
3438 png_uint_32 component = inrow[0];
Ritor1
parents:
diff changeset
3439
Ritor1
parents:
diff changeset
3440 if (alpha < 255) /* else just use component */
Ritor1
parents:
diff changeset
3441 {
Ritor1
parents:
diff changeset
3442 /* Since PNG_OPTIMIZED_ALPHA was not set it is
Ritor1
parents:
diff changeset
3443 * necessary to invert the sRGB transfer
Ritor1
parents:
diff changeset
3444 * function and multiply the alpha out.
Ritor1
parents:
diff changeset
3445 */
Ritor1
parents:
diff changeset
3446 component = png_sRGB_table[component] * alpha;
Ritor1
parents:
diff changeset
3447 component += png_sRGB_table[outrow[0]] *
Ritor1
parents:
diff changeset
3448 (255-alpha);
Ritor1
parents:
diff changeset
3449 component = PNG_sRGB_FROM_LINEAR(component);
Ritor1
parents:
diff changeset
3450 }
Ritor1
parents:
diff changeset
3451
Ritor1
parents:
diff changeset
3452 outrow[0] = (png_byte)component;
Ritor1
parents:
diff changeset
3453 }
Ritor1
parents:
diff changeset
3454
Ritor1
parents:
diff changeset
3455 inrow += 2; /* gray and alpha channel */
Ritor1
parents:
diff changeset
3456 }
Ritor1
parents:
diff changeset
3457 }
Ritor1
parents:
diff changeset
3458 }
Ritor1
parents:
diff changeset
3459
Ritor1
parents:
diff changeset
3460 else /* constant background value */
Ritor1
parents:
diff changeset
3461 {
Ritor1
parents:
diff changeset
3462 png_byte background8 = display->background->green;
Ritor1
parents:
diff changeset
3463 png_uint_16 background = png_sRGB_table[background8];
Ritor1
parents:
diff changeset
3464
Ritor1
parents:
diff changeset
3465 for (; y<height; y += stepy)
Ritor1
parents:
diff changeset
3466 {
Ritor1
parents:
diff changeset
3467 png_bytep inrow = png_voidcast(png_bytep,
Ritor1
parents:
diff changeset
3468 display->local_row);
Ritor1
parents:
diff changeset
3469 png_bytep outrow = first_row + y * step_row;
Ritor1
parents:
diff changeset
3470 png_const_bytep end_row = outrow + width;
Ritor1
parents:
diff changeset
3471
Ritor1
parents:
diff changeset
3472 /* Read the row, which is packed: */
Ritor1
parents:
diff changeset
3473 png_read_row(png_ptr, inrow, NULL);
Ritor1
parents:
diff changeset
3474
Ritor1
parents:
diff changeset
3475 /* Now do the composition on each pixel in this row. */
Ritor1
parents:
diff changeset
3476 outrow += startx;
Ritor1
parents:
diff changeset
3477 for (; outrow < end_row; outrow += stepx)
Ritor1
parents:
diff changeset
3478 {
Ritor1
parents:
diff changeset
3479 png_byte alpha = inrow[1];
Ritor1
parents:
diff changeset
3480
Ritor1
parents:
diff changeset
3481 if (alpha > 0) /* else use background */
Ritor1
parents:
diff changeset
3482 {
Ritor1
parents:
diff changeset
3483 png_uint_32 component = inrow[0];
Ritor1
parents:
diff changeset
3484
Ritor1
parents:
diff changeset
3485 if (alpha < 255) /* else just use component */
Ritor1
parents:
diff changeset
3486 {
Ritor1
parents:
diff changeset
3487 component = png_sRGB_table[component] * alpha;
Ritor1
parents:
diff changeset
3488 component += background * (255-alpha);
Ritor1
parents:
diff changeset
3489 component = PNG_sRGB_FROM_LINEAR(component);
Ritor1
parents:
diff changeset
3490 }
Ritor1
parents:
diff changeset
3491
Ritor1
parents:
diff changeset
3492 outrow[0] = (png_byte)component;
Ritor1
parents:
diff changeset
3493 }
Ritor1
parents:
diff changeset
3494
Ritor1
parents:
diff changeset
3495 else
Ritor1
parents:
diff changeset
3496 outrow[0] = background8;
Ritor1
parents:
diff changeset
3497
Ritor1
parents:
diff changeset
3498 inrow += 2; /* gray and alpha channel */
Ritor1
parents:
diff changeset
3499 }
Ritor1
parents:
diff changeset
3500
Ritor1
parents:
diff changeset
3501 row += display->row_bytes;
Ritor1
parents:
diff changeset
3502 }
Ritor1
parents:
diff changeset
3503 }
Ritor1
parents:
diff changeset
3504 }
Ritor1
parents:
diff changeset
3505 }
Ritor1
parents:
diff changeset
3506 break;
Ritor1
parents:
diff changeset
3507
Ritor1
parents:
diff changeset
3508 case 16:
Ritor1
parents:
diff changeset
3509 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
Ritor1
parents:
diff changeset
3510 * still be done and, maybe, the alpha channel removed. This code also
Ritor1
parents:
diff changeset
3511 * handles the alpha-first option.
Ritor1
parents:
diff changeset
3512 */
Ritor1
parents:
diff changeset
3513 {
Ritor1
parents:
diff changeset
3514 png_uint_16p first_row = png_voidcast(png_uint_16p,
Ritor1
parents:
diff changeset
3515 display->first_row);
Ritor1
parents:
diff changeset
3516 /* The division by two is safe because the caller passed in a
Ritor1
parents:
diff changeset
3517 * stride which was multiplied by 2 (below) to get row_bytes.
Ritor1
parents:
diff changeset
3518 */
Ritor1
parents:
diff changeset
3519 ptrdiff_t step_row = display->row_bytes / 2;
Ritor1
parents:
diff changeset
3520 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
Ritor1
parents:
diff changeset
3521 unsigned int outchannels = 1+preserve_alpha;
Ritor1
parents:
diff changeset
3522 int swap_alpha = 0;
Ritor1
parents:
diff changeset
3523
Ritor1
parents:
diff changeset
3524 # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
Ritor1
parents:
diff changeset
3525 if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST))
Ritor1
parents:
diff changeset
3526 swap_alpha = 1;
Ritor1
parents:
diff changeset
3527 # endif
Ritor1
parents:
diff changeset
3528
Ritor1
parents:
diff changeset
3529 for (pass = 0; pass < passes; ++pass)
Ritor1
parents:
diff changeset
3530 {
Ritor1
parents:
diff changeset
3531 unsigned int startx, stepx, stepy;
Ritor1
parents:
diff changeset
3532 png_uint_32 y;
Ritor1
parents:
diff changeset
3533
Ritor1
parents:
diff changeset
3534 /* The 'x' start and step are adjusted to output components here.
Ritor1
parents:
diff changeset
3535 */
Ritor1
parents:
diff changeset
3536 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
Ritor1
parents:
diff changeset
3537 {
Ritor1
parents:
diff changeset
3538 /* The row may be empty for a short image: */
Ritor1
parents:
diff changeset
3539 if (PNG_PASS_COLS(width, pass) == 0)
Ritor1
parents:
diff changeset
3540 continue;
Ritor1
parents:
diff changeset
3541
Ritor1
parents:
diff changeset
3542 startx = PNG_PASS_START_COL(pass) * outchannels;
Ritor1
parents:
diff changeset
3543 stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
Ritor1
parents:
diff changeset
3544 y = PNG_PASS_START_ROW(pass);
Ritor1
parents:
diff changeset
3545 stepy = PNG_PASS_ROW_OFFSET(pass);
Ritor1
parents:
diff changeset
3546 }
Ritor1
parents:
diff changeset
3547
Ritor1
parents:
diff changeset
3548 else
Ritor1
parents:
diff changeset
3549 {
Ritor1
parents:
diff changeset
3550 y = 0;
Ritor1
parents:
diff changeset
3551 startx = 0;
Ritor1
parents:
diff changeset
3552 stepx = outchannels;
Ritor1
parents:
diff changeset
3553 stepy = 1;
Ritor1
parents:
diff changeset
3554 }
Ritor1
parents:
diff changeset
3555
Ritor1
parents:
diff changeset
3556 for (; y<height; y += stepy)
Ritor1
parents:
diff changeset
3557 {
Ritor1
parents:
diff changeset
3558 png_const_uint_16p inrow;
Ritor1
parents:
diff changeset
3559 png_uint_16p outrow = first_row + y*step_row;
Ritor1
parents:
diff changeset
3560 png_uint_16p end_row = outrow + width * outchannels;
Ritor1
parents:
diff changeset
3561
Ritor1
parents:
diff changeset
3562 /* Read the row, which is packed: */
Ritor1
parents:
diff changeset
3563 png_read_row(png_ptr, png_voidcast(png_bytep,
Ritor1
parents:
diff changeset
3564 display->local_row), NULL);
Ritor1
parents:
diff changeset
3565 inrow = png_voidcast(png_const_uint_16p, display->local_row);
Ritor1
parents:
diff changeset
3566
Ritor1
parents:
diff changeset
3567 /* Now do the pre-multiplication on each pixel in this row.
Ritor1
parents:
diff changeset
3568 */
Ritor1
parents:
diff changeset
3569 outrow += startx;
Ritor1
parents:
diff changeset
3570 for (; outrow < end_row; outrow += stepx)
Ritor1
parents:
diff changeset
3571 {
Ritor1
parents:
diff changeset
3572 png_uint_32 component = inrow[0];
Ritor1
parents:
diff changeset
3573 png_uint_16 alpha = inrow[1];
Ritor1
parents:
diff changeset
3574
Ritor1
parents:
diff changeset
3575 if (alpha > 0) /* else 0 */
Ritor1
parents:
diff changeset
3576 {
Ritor1
parents:
diff changeset
3577 if (alpha < 65535) /* else just use component */
Ritor1
parents:
diff changeset
3578 {
Ritor1
parents:
diff changeset
3579 component *= alpha;
Ritor1
parents:
diff changeset
3580 component += 32767;
Ritor1
parents:
diff changeset
3581 component /= 65535;
Ritor1
parents:
diff changeset
3582 }
Ritor1
parents:
diff changeset
3583 }
Ritor1
parents:
diff changeset
3584
Ritor1
parents:
diff changeset
3585 else
Ritor1
parents:
diff changeset
3586 component = 0;
Ritor1
parents:
diff changeset
3587
Ritor1
parents:
diff changeset
3588 outrow[swap_alpha] = (png_uint_16)component;
Ritor1
parents:
diff changeset
3589 if (preserve_alpha)
Ritor1
parents:
diff changeset
3590 outrow[1 ^ swap_alpha] = alpha;
Ritor1
parents:
diff changeset
3591
Ritor1
parents:
diff changeset
3592 inrow += 2; /* components and alpha channel */
Ritor1
parents:
diff changeset
3593 }
Ritor1
parents:
diff changeset
3594 }
Ritor1
parents:
diff changeset
3595 }
Ritor1
parents:
diff changeset
3596 }
Ritor1
parents:
diff changeset
3597 break;
Ritor1
parents:
diff changeset
3598 }
Ritor1
parents:
diff changeset
3599
Ritor1
parents:
diff changeset
3600 return 1;
Ritor1
parents:
diff changeset
3601 }
Ritor1
parents:
diff changeset
3602
Ritor1
parents:
diff changeset
3603 /* The guts of png_image_finish_read as a png_safe_execute callback. */
Ritor1
parents:
diff changeset
3604 static int
Ritor1
parents:
diff changeset
3605 png_image_read_direct(png_voidp argument)
Ritor1
parents:
diff changeset
3606 {
Ritor1
parents:
diff changeset
3607 png_image_read_control *display = png_voidcast(png_image_read_control*,
Ritor1
parents:
diff changeset
3608 argument);
Ritor1
parents:
diff changeset
3609 png_imagep image = display->image;
Ritor1
parents:
diff changeset
3610 png_structrp png_ptr = image->opaque->png_ptr;
Ritor1
parents:
diff changeset
3611 png_inforp info_ptr = image->opaque->info_ptr;
Ritor1
parents:
diff changeset
3612
Ritor1
parents:
diff changeset
3613 png_uint_32 format = image->format;
Ritor1
parents:
diff changeset
3614 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
Ritor1
parents:
diff changeset
3615 int do_local_compose = 0;
Ritor1
parents:
diff changeset
3616 int do_local_background = 0; /* to avoid double gamma correction bug */
Ritor1
parents:
diff changeset
3617 int passes = 0;
Ritor1
parents:
diff changeset
3618
Ritor1
parents:
diff changeset
3619 /* Add transforms to ensure the correct output format is produced then check
Ritor1
parents:
diff changeset
3620 * that the required implementation support is there. Always expand; always
Ritor1
parents:
diff changeset
3621 * need 8 bits minimum, no palette and expanded tRNS.
Ritor1
parents:
diff changeset
3622 */
Ritor1
parents:
diff changeset
3623 png_set_expand(png_ptr);
Ritor1
parents:
diff changeset
3624
Ritor1
parents:
diff changeset
3625 /* Now check the format to see if it was modified. */
Ritor1
parents:
diff changeset
3626 {
Ritor1
parents:
diff changeset
3627 png_uint_32 base_format = png_image_format(png_ptr) &
Ritor1
parents:
diff changeset
3628 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
Ritor1
parents:
diff changeset
3629 png_uint_32 change = format ^ base_format;
Ritor1
parents:
diff changeset
3630 png_fixed_point output_gamma;
Ritor1
parents:
diff changeset
3631 int mode; /* alpha mode */
Ritor1
parents:
diff changeset
3632
Ritor1
parents:
diff changeset
3633 /* Do this first so that we have a record if rgb to gray is happening. */
Ritor1
parents:
diff changeset
3634 if (change & PNG_FORMAT_FLAG_COLOR)
Ritor1
parents:
diff changeset
3635 {
Ritor1
parents:
diff changeset
3636 /* gray<->color transformation required. */
Ritor1
parents:
diff changeset
3637 if (format & PNG_FORMAT_FLAG_COLOR)
Ritor1
parents:
diff changeset
3638 png_set_gray_to_rgb(png_ptr);
Ritor1
parents:
diff changeset
3639
Ritor1
parents:
diff changeset
3640 else
Ritor1
parents:
diff changeset
3641 {
Ritor1
parents:
diff changeset
3642 /* libpng can't do both rgb to gray and
Ritor1
parents:
diff changeset
3643 * background/pre-multiplication if there is also significant gamma
Ritor1
parents:
diff changeset
3644 * correction, because both operations require linear colors and
Ritor1
parents:
diff changeset
3645 * the code only supports one transform doing the gamma correction.
Ritor1
parents:
diff changeset
3646 * Handle this by doing the pre-multiplication or background
Ritor1
parents:
diff changeset
3647 * operation in this code, if necessary.
Ritor1
parents:
diff changeset
3648 *
Ritor1
parents:
diff changeset
3649 * TODO: fix this by rewriting pngrtran.c (!)
Ritor1
parents:
diff changeset
3650 *
Ritor1
parents:
diff changeset
3651 * For the moment (given that fixing this in pngrtran.c is an
Ritor1
parents:
diff changeset
3652 * enormous change) 'do_local_background' is used to indicate that
Ritor1
parents:
diff changeset
3653 * the problem exists.
Ritor1
parents:
diff changeset
3654 */
Ritor1
parents:
diff changeset
3655 if (base_format & PNG_FORMAT_FLAG_ALPHA)
Ritor1
parents:
diff changeset
3656 do_local_background = 1/*maybe*/;
Ritor1
parents:
diff changeset
3657
Ritor1
parents:
diff changeset
3658 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
Ritor1
parents:
diff changeset
3659 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
Ritor1
parents:
diff changeset
3660 }
Ritor1
parents:
diff changeset
3661
Ritor1
parents:
diff changeset
3662 change &= ~PNG_FORMAT_FLAG_COLOR;
Ritor1
parents:
diff changeset
3663 }
Ritor1
parents:
diff changeset
3664
Ritor1
parents:
diff changeset
3665 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
Ritor1
parents:
diff changeset
3666 */
Ritor1
parents:
diff changeset
3667 {
Ritor1
parents:
diff changeset
3668 png_fixed_point input_gamma_default;
Ritor1
parents:
diff changeset
3669
Ritor1
parents:
diff changeset
3670 if ((base_format & PNG_FORMAT_FLAG_LINEAR) &&
Ritor1
parents:
diff changeset
3671 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
Ritor1
parents:
diff changeset
3672 input_gamma_default = PNG_GAMMA_LINEAR;
Ritor1
parents:
diff changeset
3673 else
Ritor1
parents:
diff changeset
3674 input_gamma_default = PNG_DEFAULT_sRGB;
Ritor1
parents:
diff changeset
3675
Ritor1
parents:
diff changeset
3676 /* Call png_set_alpha_mode to set the default for the input gamma; the
Ritor1
parents:
diff changeset
3677 * output gamma is set by a second call below.
Ritor1
parents:
diff changeset
3678 */
Ritor1
parents:
diff changeset
3679 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
Ritor1
parents:
diff changeset
3680 }
Ritor1
parents:
diff changeset
3681
Ritor1
parents:
diff changeset
3682 if (linear)
Ritor1
parents:
diff changeset
3683 {
Ritor1
parents:
diff changeset
3684 /* If there *is* an alpha channel in the input it must be multiplied
Ritor1
parents:
diff changeset
3685 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
Ritor1
parents:
diff changeset
3686 */
Ritor1
parents:
diff changeset
3687 if (base_format & PNG_FORMAT_FLAG_ALPHA)
Ritor1
parents:
diff changeset
3688 mode = PNG_ALPHA_STANDARD; /* associated alpha */
Ritor1
parents:
diff changeset
3689
Ritor1
parents:
diff changeset
3690 else
Ritor1
parents:
diff changeset
3691 mode = PNG_ALPHA_PNG;
Ritor1
parents:
diff changeset
3692
Ritor1
parents:
diff changeset
3693 output_gamma = PNG_GAMMA_LINEAR;
Ritor1
parents:
diff changeset
3694 }
Ritor1
parents:
diff changeset
3695
Ritor1
parents:
diff changeset
3696 else
Ritor1
parents:
diff changeset
3697 {
Ritor1
parents:
diff changeset
3698 mode = PNG_ALPHA_PNG;
Ritor1
parents:
diff changeset
3699 output_gamma = PNG_DEFAULT_sRGB;
Ritor1
parents:
diff changeset
3700 }
Ritor1
parents:
diff changeset
3701
Ritor1
parents:
diff changeset
3702 /* If 'do_local_background' is set check for the presence of gamma
Ritor1
parents:
diff changeset
3703 * correction; this is part of the work-round for the libpng bug
Ritor1
parents:
diff changeset
3704 * described above.
Ritor1
parents:
diff changeset
3705 *
Ritor1
parents:
diff changeset
3706 * TODO: fix libpng and remove this.
Ritor1
parents:
diff changeset
3707 */
Ritor1
parents:
diff changeset
3708 if (do_local_background)
Ritor1
parents:
diff changeset
3709 {
Ritor1
parents:
diff changeset
3710 png_fixed_point gtest;
Ritor1
parents:
diff changeset
3711
Ritor1
parents:
diff changeset
3712 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
Ritor1
parents:
diff changeset
3713 * gamma correction, the screen gamma hasn't been set on png_struct
Ritor1
parents:
diff changeset
3714 * yet; it's set below. png_struct::gamma, however, is set to the
Ritor1
parents:
diff changeset
3715 * final value.
Ritor1
parents:
diff changeset
3716 */
Ritor1
parents:
diff changeset
3717 if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
Ritor1
parents:
diff changeset
3718 PNG_FP_1) && !png_gamma_significant(gtest))
Ritor1
parents:
diff changeset
3719 do_local_background = 0;
Ritor1
parents:
diff changeset
3720
Ritor1
parents:
diff changeset
3721 else if (mode == PNG_ALPHA_STANDARD)
Ritor1
parents:
diff changeset
3722 {
Ritor1
parents:
diff changeset
3723 do_local_background = 2/*required*/;
Ritor1
parents:
diff changeset
3724 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
Ritor1
parents:
diff changeset
3725 }
Ritor1
parents:
diff changeset
3726
Ritor1
parents:
diff changeset
3727 /* else leave as 1 for the checks below */
Ritor1
parents:
diff changeset
3728 }
Ritor1
parents:
diff changeset
3729
Ritor1
parents:
diff changeset
3730 /* If the bit-depth changes then handle that here. */
Ritor1
parents:
diff changeset
3731 if (change & PNG_FORMAT_FLAG_LINEAR)
Ritor1
parents:
diff changeset
3732 {
Ritor1
parents:
diff changeset
3733 if (linear /*16-bit output*/)
Ritor1
parents:
diff changeset
3734 png_set_expand_16(png_ptr);
Ritor1
parents:
diff changeset
3735
Ritor1
parents:
diff changeset
3736 else /* 8-bit output */
Ritor1
parents:
diff changeset
3737 png_set_scale_16(png_ptr);
Ritor1
parents:
diff changeset
3738
Ritor1
parents:
diff changeset
3739 change &= ~PNG_FORMAT_FLAG_LINEAR;
Ritor1
parents:
diff changeset
3740 }
Ritor1
parents:
diff changeset
3741
Ritor1
parents:
diff changeset
3742 /* Now the background/alpha channel changes. */
Ritor1
parents:
diff changeset
3743 if (change & PNG_FORMAT_FLAG_ALPHA)
Ritor1
parents:
diff changeset
3744 {
Ritor1
parents:
diff changeset
3745 /* Removing an alpha channel requires composition for the 8-bit
Ritor1
parents:
diff changeset
3746 * formats; for the 16-bit it is already done, above, by the
Ritor1
parents:
diff changeset
3747 * pre-multiplication and the channel just needs to be stripped.
Ritor1
parents:
diff changeset
3748 */
Ritor1
parents:
diff changeset
3749 if (base_format & PNG_FORMAT_FLAG_ALPHA)
Ritor1
parents:
diff changeset
3750 {
Ritor1
parents:
diff changeset
3751 /* If RGB->gray is happening the alpha channel must be left and the
Ritor1
parents:
diff changeset
3752 * operation completed locally.
Ritor1
parents:
diff changeset
3753 *
Ritor1
parents:
diff changeset
3754 * TODO: fix libpng and remove this.
Ritor1
parents:
diff changeset
3755 */
Ritor1
parents:
diff changeset
3756 if (do_local_background)
Ritor1
parents:
diff changeset
3757 do_local_background = 2/*required*/;
Ritor1
parents:
diff changeset
3758
Ritor1
parents:
diff changeset
3759 /* 16-bit output: just remove the channel */
Ritor1
parents:
diff changeset
3760 else if (linear) /* compose on black (well, pre-multiply) */
Ritor1
parents:
diff changeset
3761 png_set_strip_alpha(png_ptr);
Ritor1
parents:
diff changeset
3762
Ritor1
parents:
diff changeset
3763 /* 8-bit output: do an appropriate compose */
Ritor1
parents:
diff changeset
3764 else if (display->background != NULL)
Ritor1
parents:
diff changeset
3765 {
Ritor1
parents:
diff changeset
3766 png_color_16 c;
Ritor1
parents:
diff changeset
3767
Ritor1
parents:
diff changeset
3768 c.index = 0; /*unused*/
Ritor1
parents:
diff changeset
3769 c.red = display->background->red;
Ritor1
parents:
diff changeset
3770 c.green = display->background->green;
Ritor1
parents:
diff changeset
3771 c.blue = display->background->blue;
Ritor1
parents:
diff changeset
3772 c.gray = display->background->green;
Ritor1
parents:
diff changeset
3773
Ritor1
parents:
diff changeset
3774 /* This is always an 8-bit sRGB value, using the 'green' channel
Ritor1
parents:
diff changeset
3775 * for gray is much better than calculating the luminance here;
Ritor1
parents:
diff changeset
3776 * we can get off-by-one errors in that calculation relative to
Ritor1
parents:
diff changeset
3777 * the app expectations and that will show up in transparent
Ritor1
parents:
diff changeset
3778 * pixels.
Ritor1
parents:
diff changeset
3779 */
Ritor1
parents:
diff changeset
3780 png_set_background_fixed(png_ptr, &c,
Ritor1
parents:
diff changeset
3781 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
Ritor1
parents:
diff changeset
3782 0/*gamma: not used*/);
Ritor1
parents:
diff changeset
3783 }
Ritor1
parents:
diff changeset
3784
Ritor1
parents:
diff changeset
3785 else /* compose on row: implemented below. */
Ritor1
parents:
diff changeset
3786 {
Ritor1
parents:
diff changeset
3787 do_local_compose = 1;
Ritor1
parents:
diff changeset
3788 /* This leaves the alpha channel in the output, so it has to be
Ritor1
parents:
diff changeset
3789 * removed by the code below. Set the encoding to the 'OPTIMIZE'
Ritor1
parents:
diff changeset
3790 * one so the code only has to hack on the pixels that require
Ritor1
parents:
diff changeset
3791 * composition.
Ritor1
parents:
diff changeset
3792 */
Ritor1
parents:
diff changeset
3793 mode = PNG_ALPHA_OPTIMIZED;
Ritor1
parents:
diff changeset
3794 }
Ritor1
parents:
diff changeset
3795 }
Ritor1
parents:
diff changeset
3796
Ritor1
parents:
diff changeset
3797 else /* output needs an alpha channel */
Ritor1
parents:
diff changeset
3798 {
Ritor1
parents:
diff changeset
3799 /* This is tricky because it happens before the swap operation has
Ritor1
parents:
diff changeset
3800 * been accomplished; however, the swap does *not* swap the added
Ritor1
parents:
diff changeset
3801 * alpha channel (weird API), so it must be added in the correct
Ritor1
parents:
diff changeset
3802 * place.
Ritor1
parents:
diff changeset
3803 */
Ritor1
parents:
diff changeset
3804 png_uint_32 filler; /* opaque filler */
Ritor1
parents:
diff changeset
3805 int where;
Ritor1
parents:
diff changeset
3806
Ritor1
parents:
diff changeset
3807 if (linear)
Ritor1
parents:
diff changeset
3808 filler = 65535;
Ritor1
parents:
diff changeset
3809
Ritor1
parents:
diff changeset
3810 else
Ritor1
parents:
diff changeset
3811 filler = 255;
Ritor1
parents:
diff changeset
3812
Ritor1
parents:
diff changeset
3813 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
Ritor1
parents:
diff changeset
3814 if (format & PNG_FORMAT_FLAG_AFIRST)
Ritor1
parents:
diff changeset
3815 {
Ritor1
parents:
diff changeset
3816 where = PNG_FILLER_BEFORE;
Ritor1
parents:
diff changeset
3817 change &= ~PNG_FORMAT_FLAG_AFIRST;
Ritor1
parents:
diff changeset
3818 }
Ritor1
parents:
diff changeset
3819
Ritor1
parents:
diff changeset
3820 else
Ritor1
parents:
diff changeset
3821 # endif
Ritor1
parents:
diff changeset
3822 where = PNG_FILLER_AFTER;
Ritor1
parents:
diff changeset
3823
Ritor1
parents:
diff changeset
3824 png_set_add_alpha(png_ptr, filler, where);
Ritor1
parents:
diff changeset
3825 }
Ritor1
parents:
diff changeset
3826
Ritor1
parents:
diff changeset
3827 /* This stops the (irrelevant) call to swap_alpha below. */
Ritor1
parents:
diff changeset
3828 change &= ~PNG_FORMAT_FLAG_ALPHA;
Ritor1
parents:
diff changeset
3829 }
Ritor1
parents:
diff changeset
3830
Ritor1
parents:
diff changeset
3831 /* Now set the alpha mode correctly; this is always done, even if there is
Ritor1
parents:
diff changeset
3832 * no alpha channel in either the input or the output because it correctly
Ritor1
parents:
diff changeset
3833 * sets the output gamma.
Ritor1
parents:
diff changeset
3834 */
Ritor1
parents:
diff changeset
3835 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
Ritor1
parents:
diff changeset
3836
Ritor1
parents:
diff changeset
3837 # ifdef PNG_FORMAT_BGR_SUPPORTED
Ritor1
parents:
diff changeset
3838 if (change & PNG_FORMAT_FLAG_BGR)
Ritor1
parents:
diff changeset
3839 {
Ritor1
parents:
diff changeset
3840 /* Check only the output format; PNG is never BGR; don't do this if
Ritor1
parents:
diff changeset
3841 * the output is gray, but fix up the 'format' value in that case.
Ritor1
parents:
diff changeset
3842 */
Ritor1
parents:
diff changeset
3843 if (format & PNG_FORMAT_FLAG_COLOR)
Ritor1
parents:
diff changeset
3844 png_set_bgr(png_ptr);
Ritor1
parents:
diff changeset
3845
Ritor1
parents:
diff changeset
3846 else
Ritor1
parents:
diff changeset
3847 format &= ~PNG_FORMAT_FLAG_BGR;
Ritor1
parents:
diff changeset
3848
Ritor1
parents:
diff changeset
3849 change &= ~PNG_FORMAT_FLAG_BGR;
Ritor1
parents:
diff changeset
3850 }
Ritor1
parents:
diff changeset
3851 # endif
Ritor1
parents:
diff changeset
3852
Ritor1
parents:
diff changeset
3853 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
Ritor1
parents:
diff changeset
3854 if (change & PNG_FORMAT_FLAG_AFIRST)
Ritor1
parents:
diff changeset
3855 {
Ritor1
parents:
diff changeset
3856 /* Only relevant if there is an alpha channel - it's particularly
Ritor1
parents:
diff changeset
3857 * important to handle this correctly because do_local_compose may
Ritor1
parents:
diff changeset
3858 * be set above and then libpng will keep the alpha channel for this
Ritor1
parents:
diff changeset
3859 * code to remove.
Ritor1
parents:
diff changeset
3860 */
Ritor1
parents:
diff changeset
3861 if (format & PNG_FORMAT_FLAG_ALPHA)
Ritor1
parents:
diff changeset
3862 {
Ritor1
parents:
diff changeset
3863 /* Disable this if doing a local background,
Ritor1
parents:
diff changeset
3864 * TODO: remove this when local background is no longer required.
Ritor1
parents:
diff changeset
3865 */
Ritor1
parents:
diff changeset
3866 if (do_local_background != 2)
Ritor1
parents:
diff changeset
3867 png_set_swap_alpha(png_ptr);
Ritor1
parents:
diff changeset
3868 }
Ritor1
parents:
diff changeset
3869
Ritor1
parents:
diff changeset
3870 else
Ritor1
parents:
diff changeset
3871 format &= ~PNG_FORMAT_FLAG_AFIRST;
Ritor1
parents:
diff changeset
3872
Ritor1
parents:
diff changeset
3873 change &= ~PNG_FORMAT_FLAG_AFIRST;
Ritor1
parents:
diff changeset
3874 }
Ritor1
parents:
diff changeset
3875 # endif
Ritor1
parents:
diff changeset
3876
Ritor1
parents:
diff changeset
3877 /* If the *output* is 16-bit then we need to check for a byte-swap on this
Ritor1
parents:
diff changeset
3878 * architecture.
Ritor1
parents:
diff changeset
3879 */
Ritor1
parents:
diff changeset
3880 if (linear)
Ritor1
parents:
diff changeset
3881 {
Ritor1
parents:
diff changeset
3882 PNG_CONST png_uint_16 le = 0x0001;
Ritor1
parents:
diff changeset
3883
Ritor1
parents:
diff changeset
3884 if (*(png_const_bytep)&le)
Ritor1
parents:
diff changeset
3885 png_set_swap(png_ptr);
Ritor1
parents:
diff changeset
3886 }
Ritor1
parents:
diff changeset
3887
Ritor1
parents:
diff changeset
3888 /* If change is not now 0 some transformation is missing - error out. */
Ritor1
parents:
diff changeset
3889 if (change)
Ritor1
parents:
diff changeset
3890 png_error(png_ptr, "png_read_image: unsupported transformation");
Ritor1
parents:
diff changeset
3891 }
Ritor1
parents:
diff changeset
3892
Ritor1
parents:
diff changeset
3893 PNG_SKIP_CHUNKS(png_ptr);
Ritor1
parents:
diff changeset
3894
Ritor1
parents:
diff changeset
3895 /* Update the 'info' structure and make sure the result is as required; first
Ritor1
parents:
diff changeset
3896 * make sure to turn on the interlace handling if it will be required
Ritor1
parents:
diff changeset
3897 * (because it can't be turned on *after* the call to png_read_update_info!)
Ritor1
parents:
diff changeset
3898 *
Ritor1
parents:
diff changeset
3899 * TODO: remove the do_local_background fixup below.
Ritor1
parents:
diff changeset
3900 */
Ritor1
parents:
diff changeset
3901 if (!do_local_compose && do_local_background != 2)
Ritor1
parents:
diff changeset
3902 passes = png_set_interlace_handling(png_ptr);
Ritor1
parents:
diff changeset
3903
Ritor1
parents:
diff changeset
3904 png_read_update_info(png_ptr, info_ptr);
Ritor1
parents:
diff changeset
3905
Ritor1
parents:
diff changeset
3906 {
Ritor1
parents:
diff changeset
3907 png_uint_32 info_format = 0;
Ritor1
parents:
diff changeset
3908
Ritor1
parents:
diff changeset
3909 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
Ritor1
parents:
diff changeset
3910 info_format |= PNG_FORMAT_FLAG_COLOR;
Ritor1
parents:
diff changeset
3911
Ritor1
parents:
diff changeset
3912 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
Ritor1
parents:
diff changeset
3913 {
Ritor1
parents:
diff changeset
3914 /* do_local_compose removes this channel below. */
Ritor1
parents:
diff changeset
3915 if (!do_local_compose)
Ritor1
parents:
diff changeset
3916 {
Ritor1
parents:
diff changeset
3917 /* do_local_background does the same if required. */
Ritor1
parents:
diff changeset
3918 if (do_local_background != 2 ||
Ritor1
parents:
diff changeset
3919 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
Ritor1
parents:
diff changeset
3920 info_format |= PNG_FORMAT_FLAG_ALPHA;
Ritor1
parents:
diff changeset
3921 }
Ritor1
parents:
diff changeset
3922 }
Ritor1
parents:
diff changeset
3923
Ritor1
parents:
diff changeset
3924 else if (do_local_compose) /* internal error */
Ritor1
parents:
diff changeset
3925 png_error(png_ptr, "png_image_read: alpha channel lost");
Ritor1
parents:
diff changeset
3926
Ritor1
parents:
diff changeset
3927 if (info_ptr->bit_depth == 16)
Ritor1
parents:
diff changeset
3928 info_format |= PNG_FORMAT_FLAG_LINEAR;
Ritor1
parents:
diff changeset
3929
Ritor1
parents:
diff changeset
3930 # ifdef PNG_FORMAT_BGR_SUPPORTED
Ritor1
parents:
diff changeset
3931 if (png_ptr->transformations & PNG_BGR)
Ritor1
parents:
diff changeset
3932 info_format |= PNG_FORMAT_FLAG_BGR;
Ritor1
parents:
diff changeset
3933 # endif
Ritor1
parents:
diff changeset
3934
Ritor1
parents:
diff changeset
3935 # ifdef PNG_FORMAT_AFIRST_SUPPORTED
Ritor1
parents:
diff changeset
3936 if (do_local_background == 2)
Ritor1
parents:
diff changeset
3937 {
Ritor1
parents:
diff changeset
3938 if (format & PNG_FORMAT_FLAG_AFIRST)
Ritor1
parents:
diff changeset
3939 info_format |= PNG_FORMAT_FLAG_AFIRST;
Ritor1
parents:
diff changeset
3940 }
Ritor1
parents:
diff changeset
3941
Ritor1
parents:
diff changeset
3942 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
Ritor1
parents:
diff changeset
3943 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
Ritor1
parents:
diff changeset
3944 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
Ritor1
parents:
diff changeset
3945 {
Ritor1
parents:
diff changeset
3946 if (do_local_background == 2)
Ritor1
parents:
diff changeset
3947 png_error(png_ptr, "unexpected alpha swap transformation");
Ritor1
parents:
diff changeset
3948
Ritor1
parents:
diff changeset
3949 info_format |= PNG_FORMAT_FLAG_AFIRST;
Ritor1
parents:
diff changeset
3950 }
Ritor1
parents:
diff changeset
3951 # endif
Ritor1
parents:
diff changeset
3952
Ritor1
parents:
diff changeset
3953 /* This is actually an internal error. */
Ritor1
parents:
diff changeset
3954 if (info_format != format)
Ritor1
parents:
diff changeset
3955 png_error(png_ptr, "png_read_image: invalid transformations");
Ritor1
parents:
diff changeset
3956 }
Ritor1
parents:
diff changeset
3957
Ritor1
parents:
diff changeset
3958 /* Now read the rows. If do_local_compose is set then it is necessary to use
Ritor1
parents:
diff changeset
3959 * a local row buffer. The output will be GA, RGBA or BGRA and must be
Ritor1
parents:
diff changeset
3960 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
Ritor1
parents:
diff changeset
3961 * display acts as a flag.
Ritor1
parents:
diff changeset
3962 */
Ritor1
parents:
diff changeset
3963 {
Ritor1
parents:
diff changeset
3964 png_voidp first_row = display->buffer;
Ritor1
parents:
diff changeset
3965 ptrdiff_t row_bytes = display->row_stride;
Ritor1
parents:
diff changeset
3966
Ritor1
parents:
diff changeset
3967 if (linear)
Ritor1
parents:
diff changeset
3968 row_bytes *= 2;
Ritor1
parents:
diff changeset
3969
Ritor1
parents:
diff changeset
3970 /* The following expression is designed to work correctly whether it gives
Ritor1
parents:
diff changeset
3971 * a signed or an unsigned result.
Ritor1
parents:
diff changeset
3972 */
Ritor1
parents:
diff changeset
3973 if (row_bytes < 0)
Ritor1
parents:
diff changeset
3974 {
Ritor1
parents:
diff changeset
3975 char *ptr = png_voidcast(char*, first_row);
Ritor1
parents:
diff changeset
3976 ptr += (image->height-1) * (-row_bytes);
Ritor1
parents:
diff changeset
3977 first_row = png_voidcast(png_voidp, ptr);
Ritor1
parents:
diff changeset
3978 }
Ritor1
parents:
diff changeset
3979
Ritor1
parents:
diff changeset
3980 display->first_row = first_row;
Ritor1
parents:
diff changeset
3981 display->row_bytes = row_bytes;
Ritor1
parents:
diff changeset
3982 }
Ritor1
parents:
diff changeset
3983
Ritor1
parents:
diff changeset
3984 if (do_local_compose)
Ritor1
parents:
diff changeset
3985 {
Ritor1
parents:
diff changeset
3986 int result;
Ritor1
parents:
diff changeset
3987 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
Ritor1
parents:
diff changeset
3988
Ritor1
parents:
diff changeset
3989 display->local_row = row;
Ritor1
parents:
diff changeset
3990 result = png_safe_execute(image, png_image_read_composite, display);
Ritor1
parents:
diff changeset
3991 display->local_row = NULL;
Ritor1
parents:
diff changeset
3992 png_free(png_ptr, row);
Ritor1
parents:
diff changeset
3993
Ritor1
parents:
diff changeset
3994 return result;
Ritor1
parents:
diff changeset
3995 }
Ritor1
parents:
diff changeset
3996
Ritor1
parents:
diff changeset
3997 else if (do_local_background == 2)
Ritor1
parents:
diff changeset
3998 {
Ritor1
parents:
diff changeset
3999 int result;
Ritor1
parents:
diff changeset
4000 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
Ritor1
parents:
diff changeset
4001
Ritor1
parents:
diff changeset
4002 display->local_row = row;
Ritor1
parents:
diff changeset
4003 result = png_safe_execute(image, png_image_read_background, display);
Ritor1
parents:
diff changeset
4004 display->local_row = NULL;
Ritor1
parents:
diff changeset
4005 png_free(png_ptr, row);
Ritor1
parents:
diff changeset
4006
Ritor1
parents:
diff changeset
4007 return result;
Ritor1
parents:
diff changeset
4008 }
Ritor1
parents:
diff changeset
4009
Ritor1
parents:
diff changeset
4010 else
Ritor1
parents:
diff changeset
4011 {
Ritor1
parents:
diff changeset
4012 png_alloc_size_t row_bytes = display->row_bytes;
Ritor1
parents:
diff changeset
4013
Ritor1
parents:
diff changeset
4014 while (--passes >= 0)
Ritor1
parents:
diff changeset
4015 {
Ritor1
parents:
diff changeset
4016 png_uint_32 y = image->height;
Ritor1
parents:
diff changeset
4017 png_bytep row = png_voidcast(png_bytep, display->first_row);
Ritor1
parents:
diff changeset
4018
Ritor1
parents:
diff changeset
4019 while (y-- > 0)
Ritor1
parents:
diff changeset
4020 {
Ritor1
parents:
diff changeset
4021 png_read_row(png_ptr, row, NULL);
Ritor1
parents:
diff changeset
4022 row += row_bytes;
Ritor1
parents:
diff changeset
4023 }
Ritor1
parents:
diff changeset
4024 }
Ritor1
parents:
diff changeset
4025
Ritor1
parents:
diff changeset
4026 return 1;
Ritor1
parents:
diff changeset
4027 }
Ritor1
parents:
diff changeset
4028 }
Ritor1
parents:
diff changeset
4029
Ritor1
parents:
diff changeset
4030 int PNGAPI
Ritor1
parents:
diff changeset
4031 png_image_finish_read(png_imagep image, png_const_colorp background,
Ritor1
parents:
diff changeset
4032 void *buffer, png_int_32 row_stride, void *colormap)
Ritor1
parents:
diff changeset
4033 {
Ritor1
parents:
diff changeset
4034 if (image != NULL && image->version == PNG_IMAGE_VERSION)
Ritor1
parents:
diff changeset
4035 {
Ritor1
parents:
diff changeset
4036 png_uint_32 check;
Ritor1
parents:
diff changeset
4037
Ritor1
parents:
diff changeset
4038 if (row_stride == 0)
Ritor1
parents:
diff changeset
4039 row_stride = PNG_IMAGE_ROW_STRIDE(*image);
Ritor1
parents:
diff changeset
4040
Ritor1
parents:
diff changeset
4041 if (row_stride < 0)
Ritor1
parents:
diff changeset
4042 check = -row_stride;
Ritor1
parents:
diff changeset
4043
Ritor1
parents:
diff changeset
4044 else
Ritor1
parents:
diff changeset
4045 check = row_stride;
Ritor1
parents:
diff changeset
4046
Ritor1
parents:
diff changeset
4047 if (image->opaque != NULL && buffer != NULL &&
Ritor1
parents:
diff changeset
4048 check >= PNG_IMAGE_ROW_STRIDE(*image))
Ritor1
parents:
diff changeset
4049 {
Ritor1
parents:
diff changeset
4050 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
Ritor1
parents:
diff changeset
4051 (image->colormap_entries > 0 && colormap != NULL))
Ritor1
parents:
diff changeset
4052 {
Ritor1
parents:
diff changeset
4053 int result;
Ritor1
parents:
diff changeset
4054 png_image_read_control display;
Ritor1
parents:
diff changeset
4055
Ritor1
parents:
diff changeset
4056 memset(&display, 0, (sizeof display));
Ritor1
parents:
diff changeset
4057 display.image = image;
Ritor1
parents:
diff changeset
4058 display.buffer = buffer;
Ritor1
parents:
diff changeset
4059 display.row_stride = row_stride;
Ritor1
parents:
diff changeset
4060 display.colormap = colormap;
Ritor1
parents:
diff changeset
4061 display.background = background;
Ritor1
parents:
diff changeset
4062 display.local_row = NULL;
Ritor1
parents:
diff changeset
4063
Ritor1
parents:
diff changeset
4064 /* Choose the correct 'end' routine; for the color-map case all the
Ritor1
parents:
diff changeset
4065 * setup has already been done.
Ritor1
parents:
diff changeset
4066 */
Ritor1
parents:
diff changeset
4067 if (image->format & PNG_FORMAT_FLAG_COLORMAP)
Ritor1
parents:
diff changeset
4068 result =
Ritor1
parents:
diff changeset
4069 png_safe_execute(image, png_image_read_colormap, &display) &&
Ritor1
parents:
diff changeset
4070 png_safe_execute(image, png_image_read_colormapped, &display);
Ritor1
parents:
diff changeset
4071
Ritor1
parents:
diff changeset
4072 else
Ritor1
parents:
diff changeset
4073 result =
Ritor1
parents:
diff changeset
4074 png_safe_execute(image, png_image_read_direct, &display);
Ritor1
parents:
diff changeset
4075
Ritor1
parents:
diff changeset
4076 png_image_free(image);
Ritor1
parents:
diff changeset
4077 return result;
Ritor1
parents:
diff changeset
4078 }
Ritor1
parents:
diff changeset
4079
Ritor1
parents:
diff changeset
4080 else
Ritor1
parents:
diff changeset
4081 return png_image_error(image,
Ritor1
parents:
diff changeset
4082 "png_image_finish_read[color-map]: no color-map");
Ritor1
parents:
diff changeset
4083 }
Ritor1
parents:
diff changeset
4084
Ritor1
parents:
diff changeset
4085 else
Ritor1
parents:
diff changeset
4086 return png_image_error(image,
Ritor1
parents:
diff changeset
4087 "png_image_finish_read: invalid argument");
Ritor1
parents:
diff changeset
4088 }
Ritor1
parents:
diff changeset
4089
Ritor1
parents:
diff changeset
4090 else if (image != NULL)
Ritor1
parents:
diff changeset
4091 return png_image_error(image,
Ritor1
parents:
diff changeset
4092 "png_image_finish_read: damaged PNG_IMAGE_VERSION");
Ritor1
parents:
diff changeset
4093
Ritor1
parents:
diff changeset
4094 return 0;
Ritor1
parents:
diff changeset
4095 }
Ritor1
parents:
diff changeset
4096
Ritor1
parents:
diff changeset
4097 #endif /* PNG_SIMPLIFIED_READ_SUPPORTED */
Ritor1
parents:
diff changeset
4098 #endif /* PNG_READ_SUPPORTED */