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