OLD | NEW |
1 | 1 |
2 /* pngread.c - read a PNG file | 2 /* pngread.c - read a PNG file |
3 * | 3 * |
4 * Last changed in libpng 1.2.44 [June 26, 2010] | 4 * Last changed in libpng 1.2.52 [November 20, 2014] |
5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson | 5 * Copyright (c) 1998-2014 Glenn Randers-Pehrson |
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) | 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) | 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
8 * | 8 * |
9 * This code is released under the libpng license. | 9 * This code is released under the libpng license. |
10 * For conditions of distribution and use, see the disclaimer | 10 * For conditions of distribution and use, see the disclaimer |
11 * and license in png.h | 11 * and license in png.h |
12 * | 12 * |
13 * This file contains routines that an application calls directly to | 13 * This file contains routines that an application calls directly to |
14 * read a PNG file or stream. | 14 * read a PNG file or stream. |
15 */ | 15 */ |
16 | 16 |
17 #define PNG_INTERNAL | 17 #define PNG_INTERNAL |
18 #define PNG_NO_PEDANTIC_WARNINGS | 18 #define PNG_NO_PEDANTIC_WARNINGS |
19 #include "png.h" | 19 #include "png.h" |
20 #ifdef PNG_READ_SUPPORTED | 20 #ifdef PNG_READ_SUPPORTED |
21 | 21 |
22 | |
23 /* Create a PNG structure for reading, and allocate any memory needed. */ | 22 /* Create a PNG structure for reading, and allocate any memory needed. */ |
24 png_structp PNGAPI | 23 png_structp PNGAPI |
25 png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr, | 24 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) | 25 png_error_ptr error_fn, png_error_ptr warn_fn) |
27 { | 26 { |
28 | 27 |
29 #ifdef PNG_USER_MEM_SUPPORTED | 28 #ifdef PNG_USER_MEM_SUPPORTED |
30 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn, | 29 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn, |
31 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL)); | 30 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL)); |
32 } | 31 } |
(...skipping 29 matching lines...) Expand all Loading... |
62 #else | 61 #else |
63 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); | 62 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); |
64 #endif | 63 #endif |
65 if (png_ptr == NULL) | 64 if (png_ptr == NULL) |
66 return (NULL); | 65 return (NULL); |
67 | 66 |
68 /* Added at libpng-1.2.6 */ | 67 /* Added at libpng-1.2.6 */ |
69 #ifdef PNG_USER_LIMITS_SUPPORTED | 68 #ifdef PNG_USER_LIMITS_SUPPORTED |
70 png_ptr->user_width_max = PNG_USER_WIDTH_MAX; | 69 png_ptr->user_width_max = PNG_USER_WIDTH_MAX; |
71 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX; | 70 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX; |
72 # ifdef PNG_USER_CHUNK_CACHE_MAX | |
73 /* Added at libpng-1.2.43 and 1.4.0 */ | 71 /* Added at libpng-1.2.43 and 1.4.0 */ |
74 png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX; | 72 png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX; |
75 # endif | |
76 # ifdef PNG_SET_USER_CHUNK_MALLOC_MAX | |
77 /* Added at libpng-1.2.43 and 1.4.1 */ | |
78 png_ptr->user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX; | |
79 # endif | |
80 #endif | 73 #endif |
81 | 74 |
82 #ifdef PNG_SETJMP_SUPPORTED | 75 #ifdef PNG_SETJMP_SUPPORTED |
83 #ifdef USE_FAR_KEYWORD | 76 #ifdef USE_FAR_KEYWORD |
84 if (setjmp(jmpbuf)) | 77 if (setjmp(jmpbuf)) |
85 #else | 78 #else |
86 if (setjmp(png_ptr->jmpbuf)) | 79 if (setjmp(png_ptr->jmpbuf)) |
87 #endif | 80 #endif |
88 { | 81 { |
89 png_free(png_ptr, png_ptr->zbuf); | 82 png_free(png_ptr, png_ptr->zbuf); |
(...skipping 10 matching lines...) Expand all Loading... |
100 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf)); | 93 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf)); |
101 #endif | 94 #endif |
102 #endif /* PNG_SETJMP_SUPPORTED */ | 95 #endif /* PNG_SETJMP_SUPPORTED */ |
103 | 96 |
104 #ifdef PNG_USER_MEM_SUPPORTED | 97 #ifdef PNG_USER_MEM_SUPPORTED |
105 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn); | 98 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn); |
106 #endif | 99 #endif |
107 | 100 |
108 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); | 101 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); |
109 | 102 |
110 if (user_png_ver) | 103 if (user_png_ver != NULL) |
111 { | 104 { |
112 i = 0; | 105 int found_dots = 0; |
| 106 i = -1; |
| 107 |
113 do | 108 do |
114 { | 109 { |
115 if (user_png_ver[i] != png_libpng_ver[i]) | 110 i++; |
| 111 if (user_png_ver[i] != PNG_LIBPNG_VER_STRING[i]) |
116 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; | 112 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; |
117 } while (png_libpng_ver[i++]); | 113 if (user_png_ver[i] == '.') |
118 } | 114 found_dots++; |
119 else | 115 } while (found_dots < 2 && user_png_ver[i] != 0 && |
| 116 PNG_LIBPNG_VER_STRING[i] != 0); |
| 117 } |
| 118 else |
120 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; | 119 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; |
121 | 120 |
122 | 121 |
123 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) | 122 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) |
124 { | 123 { |
125 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so | 124 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so |
126 * we must recompile any applications that use any older library version. | 125 * we must recompile any applications that use any older library version. |
127 * For versions after libpng 1.0, we will be compatible, so we need | 126 * For versions after libpng 1.0, we will be compatible, so we need |
128 * only check the first digit. | 127 * only check the first digit. |
129 */ | 128 */ |
(...skipping 1170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1300 #endif | 1299 #endif |
1301 #ifdef PNG_TIME_RFC1123_SUPPORTED | 1300 #ifdef PNG_TIME_RFC1123_SUPPORTED |
1302 png_free(png_ptr, png_ptr->time_buffer); | 1301 png_free(png_ptr, png_ptr->time_buffer); |
1303 #endif | 1302 #endif |
1304 | 1303 |
1305 inflateEnd(&png_ptr->zstream); | 1304 inflateEnd(&png_ptr->zstream); |
1306 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED | 1305 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED |
1307 png_free(png_ptr, png_ptr->save_buffer); | 1306 png_free(png_ptr, png_ptr->save_buffer); |
1308 #endif | 1307 #endif |
1309 | 1308 |
1310 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED | |
1311 #ifdef PNG_TEXT_SUPPORTED | |
1312 png_free(png_ptr, png_ptr->current_text); | |
1313 #endif /* PNG_TEXT_SUPPORTED */ | |
1314 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ | |
1315 | |
1316 /* Save the important info out of the png_struct, in case it is | 1309 /* Save the important info out of the png_struct, in case it is |
1317 * being used again. | 1310 * being used again. |
1318 */ | 1311 */ |
1319 #ifdef PNG_SETJMP_SUPPORTED | 1312 #ifdef PNG_SETJMP_SUPPORTED |
1320 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf)); | 1313 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf)); |
1321 #endif | 1314 #endif |
1322 | 1315 |
1323 error_fn = png_ptr->error_fn; | 1316 error_fn = png_ptr->error_fn; |
1324 warning_fn = png_ptr->warning_fn; | 1317 warning_fn = png_ptr->warning_fn; |
1325 error_ptr = png_ptr->error_ptr; | 1318 error_ptr = png_ptr->error_ptr; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1411 | 1404 |
1412 #ifdef PNG_READ_EXPAND_SUPPORTED | 1405 #ifdef PNG_READ_EXPAND_SUPPORTED |
1413 /* Expand paletted colors into true RGB triplets | 1406 /* Expand paletted colors into true RGB triplets |
1414 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel | 1407 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel |
1415 * Expand paletted or RGB images with transparency to full alpha | 1408 * Expand paletted or RGB images with transparency to full alpha |
1416 * channels so the data will be available as RGBA quartets. | 1409 * channels so the data will be available as RGBA quartets. |
1417 */ | 1410 */ |
1418 if (transforms & PNG_TRANSFORM_EXPAND) | 1411 if (transforms & PNG_TRANSFORM_EXPAND) |
1419 if ((png_ptr->bit_depth < 8) || | 1412 if ((png_ptr->bit_depth < 8) || |
1420 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) || | 1413 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) || |
1421 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))) | 1414 (info_ptr->valid & PNG_INFO_tRNS)) |
1422 png_set_expand(png_ptr); | 1415 png_set_expand(png_ptr); |
1423 #endif | 1416 #endif |
1424 | 1417 |
1425 /* We don't handle background color or gamma transformation or dithering. | 1418 /* We don't handle background color or gamma transformation or dithering. |
1426 */ | 1419 */ |
1427 | 1420 |
1428 #ifdef PNG_READ_INVERT_SUPPORTED | 1421 #ifdef PNG_READ_INVERT_SUPPORTED |
1429 /* Invert monochrome files to have 0 as white and 1 as black | 1422 /* Invert monochrome files to have 0 as white and 1 as black |
1430 */ | 1423 */ |
1431 if (transforms & PNG_TRANSFORM_INVERT_MONO) | 1424 if (transforms & PNG_TRANSFORM_INVERT_MONO) |
1432 png_set_invert_mono(png_ptr); | 1425 png_set_invert_mono(png_ptr); |
1433 #endif | 1426 #endif |
1434 | 1427 |
1435 #ifdef PNG_READ_SHIFT_SUPPORTED | 1428 #ifdef PNG_READ_SHIFT_SUPPORTED |
1436 /* If you want to shift the pixel values from the range [0,255] or | 1429 /* If you want to shift the pixel values from the range [0,255] or |
1437 * [0,65535] to the original [0,7] or [0,31], or whatever range the | 1430 * [0,65535] to the original [0,7] or [0,31], or whatever range the |
1438 * colors were originally in: | 1431 * colors were originally in: |
1439 */ | 1432 */ |
1440 if ((transforms & PNG_TRANSFORM_SHIFT) | 1433 if ((transforms & PNG_TRANSFORM_SHIFT) && (info_ptr->valid & PNG_INFO_sBIT)) |
1441 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) | 1434 png_set_shift(png_ptr, &info_ptr->sig_bit); |
1442 { | |
1443 png_color_8p sig_bit; | |
1444 | |
1445 png_get_sBIT(png_ptr, info_ptr, &sig_bit); | |
1446 png_set_shift(png_ptr, sig_bit); | |
1447 } | |
1448 #endif | 1435 #endif |
1449 | 1436 |
1450 #ifdef PNG_READ_BGR_SUPPORTED | 1437 #ifdef PNG_READ_BGR_SUPPORTED |
1451 /* Flip the RGB pixels to BGR (or RGBA to BGRA) | 1438 /* Flip the RGB pixels to BGR (or RGBA to BGRA) |
1452 */ | 1439 */ |
1453 if (transforms & PNG_TRANSFORM_BGR) | 1440 if (transforms & PNG_TRANSFORM_BGR) |
1454 png_set_bgr(png_ptr); | 1441 png_set_bgr(png_ptr); |
1455 #endif | 1442 #endif |
1456 | 1443 |
1457 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED | 1444 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1512 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr, | 1499 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr, |
1513 png_get_rowbytes(png_ptr, info_ptr)); | 1500 png_get_rowbytes(png_ptr, info_ptr)); |
1514 } | 1501 } |
1515 | 1502 |
1516 png_read_image(png_ptr, info_ptr->row_pointers); | 1503 png_read_image(png_ptr, info_ptr->row_pointers); |
1517 info_ptr->valid |= PNG_INFO_IDAT; | 1504 info_ptr->valid |= PNG_INFO_IDAT; |
1518 | 1505 |
1519 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */ | 1506 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */ |
1520 png_read_end(png_ptr, info_ptr); | 1507 png_read_end(png_ptr, info_ptr); |
1521 | 1508 |
1522 transforms = transforms; /* Quiet compiler warnings */ | 1509 PNG_UNUSED(transforms) /* Quiet compiler warnings */ |
1523 params = params; | 1510 PNG_UNUSED(params) |
1524 | 1511 |
1525 } | 1512 } |
1526 #endif /* PNG_INFO_IMAGE_SUPPORTED */ | 1513 #endif /* PNG_INFO_IMAGE_SUPPORTED */ |
1527 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ | 1514 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
1528 #endif /* PNG_READ_SUPPORTED */ | 1515 #endif /* PNG_READ_SUPPORTED */ |
OLD | NEW |