| | 412 | |
| | 413 | The PNG specification sets no limit on the number of ancillary chunks |
| | 414 | allowed in a PNG datastream. You can impose a limit on the total number |
| | 415 | of sPLT, tEXt, iTXt, zTXt, and unknown chunks that will be stored, with |
| | 416 | |
| | 417 | png_set_chunk_cache_max(png_ptr, user_chunk_cache_max); |
| | 418 | |
| | 419 | where 0x7fffffffL means unlimited. You can retrieve this limit with |
| | 420 | |
| | 421 | chunk_cache_max = png_get_chunk_cache_max(png_ptr); |
| | 422 | |
| | 423 | This limit also applies to the number of buffers that can be allocated |
| | 424 | by png_decompress_chunk() while decompressing iTXt, zTXt, and iCCP chunks. |
| 552 | | channels = png_get_channels(png_ptr, info_ptr); |
| 553 | | channels - number of channels of info for the |
| 554 | | color type (valid values are 1 (GRAY, |
| 555 | | PALETTE), 2 (GRAY_ALPHA), 3 (RGB), |
| 556 | | 4 (RGB_ALPHA or RGB + filler byte)) |
| 557 | | rowbytes = png_get_rowbytes(png_ptr, info_ptr); |
| 558 | | rowbytes - number of bytes needed to hold a row |
| 559 | | |
| 560 | | signature = png_get_signature(png_ptr, info_ptr); |
| 561 | | signature - holds the signature read from the |
| 562 | | file (if any). The data is kept in |
| 563 | | the same offset it would be if the |
| 564 | | whole signature were read (i.e. if an |
| 565 | | application had already read in 4 |
| 566 | | bytes of signature before starting |
| 567 | | libpng, the remaining 4 bytes would |
| 568 | | be in signature[4] through signature[7] |
| 569 | | (see png_set_sig_bytes())). |
| 570 | | |
| | 568 | Note that png_get_IHDR() returns 32-bit data into |
| | 569 | the application's width and height variables. |
| | 570 | This is an unsafe situation if these are 16-bit |
| | 571 | variables. In such situations, the |
| | 572 | png_get_image_width() and png_get_image_height() |
| | 573 | functions described below are safer. |
| | 590 | channels = png_get_channels(png_ptr, info_ptr); |
| | 591 | channels - number of channels of info for the |
| | 592 | color type (valid values are 1 (GRAY, |
| | 593 | PALETTE), 2 (GRAY_ALPHA), 3 (RGB), |
| | 594 | 4 (RGB_ALPHA or RGB + filler byte)) |
| | 595 | rowbytes = png_get_rowbytes(png_ptr, info_ptr); |
| | 596 | rowbytes - number of bytes needed to hold a row |
| | 597 | |
| | 598 | signature = png_get_signature(png_ptr, info_ptr); |
| | 599 | signature - holds the signature read from the |
| | 600 | file (if any). The data is kept in |
| | 601 | the same offset it would be if the |
| | 602 | whole signature were read (i.e. if an |
| | 603 | application had already read in 4 |
| | 604 | bytes of signature before starting |
| | 605 | libpng, the remaining 4 bytes would |
| | 606 | be in signature[4] through signature[7] |
| | 607 | (see png_set_sig_bytes())). |
| | 881 | As of libpng version 1.2.42, not all possible expansions are supported. |
| | 882 | |
| | 883 | In the following table, the 01 means grayscale with depth<8, 31 means |
| | 884 | indexed with depth<8, other numerals represent the color type, "T" means |
| | 885 | the tRNS chunk is present, A means an alpha channel is present, and O |
| | 886 | means tRNS or alpha is present but all pixels in the image are opaque. |
| | 887 | |
| | 888 | FROM 01 31 0 0T 0O 2 2T 2O 3 3T 3O 4A 4O 6A 6O |
| | 889 | TO |
| | 890 | 01 - |
| | 891 | 31 - |
| | 892 | 0 1 - |
| | 893 | 0T - |
| | 894 | 0O - |
| | 895 | 2 GX - |
| | 896 | 2T - |
| | 897 | 2O - |
| | 898 | 3 1 - |
| | 899 | 3T - |
| | 900 | 3O - |
| | 901 | 4A T - |
| | 902 | 4O - |
| | 903 | 6A GX TX TX - |
| | 904 | 6O GX TX - |
| | 905 | |
| | 906 | Within the matrix, |
| | 907 | "-" means the transformation is not supported. |
| | 908 | "X" means the transformation is obtained by png_set_expand(). |
| | 909 | "1" means the transformation is obtained by |
| | 910 | png_set_expand_gray_1_2_4_to_8 |
| | 911 | "G" means the transformation is obtained by |
| | 912 | png_set_gray_to_rgb(). |
| | 913 | "P" means the transformation is obtained by |
| | 914 | png_set_expand_palette_to_rgb(). |
| | 915 | "T" means the transformation is obtained by |
| | 916 | png_set_tRNS_to_alpha(). |
| | 917 | |
| 889 | | higher possible bit depth (e.g. from 5 bits/sample in the range [0,31] to |
| 890 | | 8 bits/sample in the range [0, 255]). However, it is also possible to |
| 891 | | convert the PNG pixel data back to the original bit depth of the image. |
| 892 | | This call reduces the pixels back down to the original bit depth: |
| | 960 | higher possible bit depth (e.g. from 5 bits/sample in the range [0,31] |
| | 961 | to 8 bits/sample in the range [0, 255]). However, it is also possible |
| | 962 | to convert the PNG pixel data back to the original bit depth of the |
| | 963 | image. This call reduces the pixels back down to the original bit depth: |
| 1339 | | by the user and not by libpng, and will in those |
| 1340 | | cases do nothing. The "seq" parameter is ignored if only one item |
| 1341 | | of the selected data type, such as PLTE, is allowed. If "seq" is not |
| 1342 | | -1, and multiple items are allowed for the data type identified in |
| 1343 | | the mask, such as text or sPLT, only the n'th item in the structure |
| 1344 | | is freed, where n is "seq". |
| | 1410 | by the user and not by libpng, and will in those cases do nothing. |
| | 1411 | The "seq" parameter is ignored if only one item of the selected data |
| | 1412 | type, such as PLTE, is allowed. If "seq" is not -1, and multiple items |
| | 1413 | are allowed for the data type identified in the mask, such as text or |
| | 1414 | sPLT, only the n'th item in the structure is freed, where n is "seq". |
| 2109 | | level of opacity. If your data is supplied as a level of |
| 2110 | | transparency, you can invert the alpha channel before you write it, so |
| 2111 | | that 0 is fully transparent and 255 (in 8-bit or paletted images) or |
| 2112 | | 65535 (in 16-bit images) is fully opaque, with |
| | 2183 | level of opacity. If your data is supplied as a level of transparency, |
| | 2184 | you can invert the alpha channel before you write it, so that 0 is |
| | 2185 | fully transparent and 255 (in 8-bit or paletted images) or 65535 |
| | 2186 | (in 16-bit images) is fully opaque, with |
| 2299 | | When the file is interlaced, things can get a good deal more |
| 2300 | | complicated. The only currently (as of the PNG Specification |
| 2301 | | version 1.2, dated July 1999) defined interlacing scheme for PNG files |
| 2302 | | is the "Adam7" interlace scheme, that breaks down an |
| 2303 | | image into seven smaller images of varying size. libpng will build |
| 2304 | | these images for you, or you can do them yourself. If you want to |
| 2305 | | build them yourself, see the PNG specification for details of which |
| 2306 | | pixels to write when. |
| | 2373 | When the file is interlaced, things can get a good deal more complicated. |
| | 2374 | The only currently (as of the PNG Specification version 1.2, dated July |
| | 2375 | 1999) defined interlacing scheme for PNG files is the "Adam7" interlace |
| | 2376 | scheme, that breaks down an image into seven smaller images of varying |
| | 2377 | size. libpng will build these images for you, or you can do them |
| | 2378 | yourself. If you want to build them yourself, see the PNG specification |
| | 2379 | for details of which pixels to write when. |
| 2326 | | As some of these rows are not used, and thus return immediately, |
| 2327 | | you may want to read about interlacing in the PNG specification, |
| 2328 | | and only update the rows that are actually used. |
| | 2399 | As some of these rows are not used, and thus return immediately, you may |
| | 2400 | want to read about interlacing in the PNG specification, and only update |
| | 2401 | the rows that are actually used. |
| 2361 | | by the user and not by libpng, and will in those |
| 2362 | | cases do nothing. The "seq" parameter is ignored if only one item |
| 2363 | | of the selected data type, such as PLTE, is allowed. If "seq" is not |
| 2364 | | -1, and multiple items are allowed for the data type identified in |
| 2365 | | the mask, such as text or sPLT, only the n'th item in the structure |
| 2366 | | is freed, where n is "seq". |
| 2367 | | |
| 2368 | | If you allocated data such as a palette that you passed |
| 2369 | | in to libpng with png_set_*, you must not free it until just before the call to |
| | 2434 | by the user and not by libpng, and will in those cases do nothing. |
| | 2435 | The "seq" parameter is ignored if only one item of the selected data |
| | 2436 | type, such as PLTE, is allowed. If "seq" is not -1, and multiple items |
| | 2437 | are allowed for the data type identified in the mask, such as text or |
| | 2438 | sPLT, only the n'th item in the structure is freed, where n is "seq". |
| | 2439 | |
| | 2440 | If you allocated data such as a palette that you passed in to libpng |
| | 2441 | with png_set_*, you must not free it until just before the call to |
| 2435 | | Memory allocation is done through the functions png_malloc() |
| 2436 | | and png_free(). These currently just call the standard C functions. If |
| 2437 | | your pointers can't access more then 64K at a time, you will want to set |
| 2438 | | MAXSEG_64K in zlib.h. Since it is unlikely that the method of handling |
| 2439 | | memory allocation on a platform will change between applications, these |
| 2440 | | functions must be modified in the library at compile time. If you prefer |
| 2441 | | to use a different method of allocating and freeing data, you can use |
| 2442 | | png_create_read_struct_2() or png_create_write_struct_2() to register |
| 2443 | | your own functions as described above. |
| 2444 | | These functions also provide a void pointer that can be retrieved via |
| | 2507 | Memory allocation is done through the functions png_malloc(), png_calloc(), |
| | 2508 | and png_free(). These currently just call the standard C functions. |
| | 2509 | png_calloc() calls png_malloc() and then png_memset() to clear the newly |
| | 2510 | allocated memory to zero. If your pointers can't access more then 64K |
| | 2511 | at a time, you will want to set MAXSEG_64K in zlib.h. Since it is |
| | 2512 | unlikely that the method of handling memory allocation on a platform |
| | 2513 | will change between applications, these functions must be modified in |
| | 2514 | the library at compile time. If you prefer to use a different method |
| | 2515 | of allocating and freeing data, you can use png_create_read_struct_2() or |
| | 2516 | png_create_write_struct_2() to register your own functions as described |
| | 2517 | above. These functions also provide a void pointer that can be retrieved |
| | 2518 | via |
| 2536 | | after a longjmp, so the user may want to be careful about doing anything after |
| 2537 | | setjmp returns non-zero besides returning itself. Consult your compiler |
| 2538 | | documentation for more details. For an alternative approach, you may wish |
| 2539 | | to use the "cexcept" facility (see http://cexcept.sourceforge.net). |
| | 2610 | after a longjmp, so the user may want to be careful about doing anything |
| | 2611 | after setjmp returns non-zero besides returning itself. Consult your |
| | 2612 | compiler documentation for more details. For an alternative approach, you |
| | 2613 | may wish to use the "cexcept" facility (see http://cexcept.sourceforge.net). |
| 2551 | | specification. Acquire a first level of |
| 2552 | | understanding of how it works. Pay particular attention to the |
| 2553 | | sections that describe chunk names, and look at how other chunks were |
| 2554 | | designed, so you can do things similarly. Second, check out the |
| 2555 | | sections of libpng that read and write chunks. Try to find a chunk |
| 2556 | | that is similar to yours and use it as a template. More details can |
| 2557 | | be found in the comments inside the code. It is best to handle unknown |
| 2558 | | chunks in a generic method, via callback functions, instead of by |
| 2559 | | modifying libpng functions. |
| | 2625 | specification. Acquire a first level of understanding of how it works. |
| | 2626 | Pay particular attention to the sections that describe chunk names, |
| | 2627 | and look at how other chunks were designed, so you can do things |
| | 2628 | similarly. Second, check out the sections of libpng that read and |
| | 2629 | write chunks. Try to find a chunk that is similar to yours and use |
| | 2630 | it as a template. More details can be found in the comments inside |
| | 2631 | the code. It is best to handle unknown chunks in a generic method, |
| | 2632 | via callback functions, instead of by modifying libpng functions. |
| 2736 | | want. The PNG_NO_READ[or WRITE]_TRANSFORMS directives disable |
| 2737 | | the extra transformations but still leave the library fully capable of reading |
| 2738 | | and writing PNG files with all known public chunks |
| 2739 | | Use of the PNG_NO_READ[or WRITE]_ANCILLARY_CHUNKS directive |
| 2740 | | produces a library that is incapable of reading or writing ancillary chunks. |
| 2741 | | If you are not using the progressive reading capability, you can |
| 2742 | | turn that off with PNG_NO_PROGRESSIVE_READ (don't confuse |
| 2743 | | this with the INTERLACING capability, which you'll still have). |
| | 2809 | want. The PNG_NO_READ[or WRITE]_TRANSFORMS directives disable the extra |
| | 2810 | transformations but still leave the library fully capable of reading |
| | 2811 | and writing PNG files with all known public chunks. Use of the |
| | 2812 | PNG_NO_READ[or WRITE]_ANCILLARY_CHUNKS directive produces a library |
| | 2813 | that is incapable of reading or writing ancillary chunks. If you are |
| | 2814 | not using the progressive reading capability, you can turn that off |
| | 2815 | with PNG_NO_PROGRESSIVE_READ (don't confuse this with the INTERLACING |
| | 2816 | capability, which you'll still have). |
| | 3008 | These macros are deprecated: |
| | 3009 | |
| | 3010 | PNG_READ_TRANSFORMS_NOT_SUPPORTED |
| | 3011 | PNG_PROGRESSIVE_READ_NOT_SUPPORTED |
| | 3012 | PNG_NO_SEQUENTIAL_READ_SUPPORTED |
| | 3013 | PNG_WRITE_TRANSFORMS_NOT_SUPPORTED |
| | 3014 | PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED |
| | 3015 | PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED |
| | 3016 | |
| | 3017 | They have been replaced, respectively, by: |
| | 3018 | |
| | 3019 | PNG_NO_READ_TRANSFORMS |
| | 3020 | PNG_NO_PROGRESSIVE_READ |
| | 3021 | PNG_NO_SEQUENTIAL_READ |
| | 3022 | PNG_NO_WRITE_TRANSFORMS |
| | 3023 | PNG_NO_READ_ANCILLARY_CHUNKS |
| | 3024 | PNG_NO_WRITE_ANCILLARY_CHUNKS |
| | 3025 | |
| | 3026 | PNG_MAX_UINT was replaced with PNG_UINT_31_MAX. It has been |
| | 3027 | deprecated since libpng-1.0.16 and libpng-1.2.6. |
| | 3028 | |
| | 3029 | The function |
| | 3030 | png_check_sig(sig, num) |
| | 3031 | was replaced with |
| | 3032 | !png_sig_cmp(sig, 0, num) |
| | 3033 | It has been deprecated since libpng-0.90. |
| | 3034 | |
| | 3035 | The function |
| | 3036 | png_set_gray_1_2_4_to_8() |
| | 3037 | which also expands tRNS to alpha was replaced with |
| | 3038 | png_set_expand_gray_1_2_4_to_8() |
| | 3039 | which does not. It has been deprecated since libpng-1.0.18 and 1.2.9. |
| | 3040 | |