📄 libpng.3
字号:
bit_depth = png_get_bit_depth(png_ptr, info_ptr); color_type = png_get_color_type(png_ptr, info_ptr); filter_method = png_get_filter_type(png_ptr, info_ptr); compression_type = png_get_compression_type(png_ptr, info_ptr); interlace_type = png_get_interlace_type(png_ptr, info_ptr);These are also important, but their validity depends on whether the chunkhas been read. The png_get_valid(png_ptr, info_ptr, PNG_INFO_<chunk>) andpng_get_<chunk>(png_ptr, info_ptr, ...) functions return non-zero if thedata has been read, or zero if it is missing. The parameters to thepng_get_<chunk> are set directly if they are simple data types, or a pointerinto the info_ptr is returned for any complex types. png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette); palette - the palette for the file (array of png_color) num_palette - number of entries in the palette png_get_gAMA(png_ptr, info_ptr, &gamma); gamma - the gamma the file is written at (PNG_INFO_gAMA) png_get_sRGB(png_ptr, info_ptr, &srgb_intent); srgb_intent - the rendering intent (PNG_INFO_sRGB) The presence of the sRGB chunk means that the pixel data is in the sRGB color space. This chunk also implies specific values of gAMA and cHRM. png_get_iCCP(png_ptr, info_ptr, &name, &compression_type, &profile, &proflen); name - The profile name. compression - The compression type; always PNG_COMPRESSION_TYPE_BASE for PNG 1.0. You may give NULL to this argument to ignore it. profile - International Color Consortium color profile data. May contain NULs. proflen - length of profile data in bytes. png_get_sBIT(png_ptr, info_ptr, &sig_bit); sig_bit - the number of significant bits for (PNG_INFO_sBIT) each of the gray, red, green, and blue channels, whichever are appropriate for the given color type (png_color_16) png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values); trans - array of transparent entries for palette (PNG_INFO_tRNS) trans_values - graylevel or color sample values of the single transparent color for non-paletted images (PNG_INFO_tRNS) num_trans - number of transparent entries (PNG_INFO_tRNS) png_get_hIST(png_ptr, info_ptr, &hist); (PNG_INFO_hIST) hist - histogram of palette (array of png_uint_16) png_get_tIME(png_ptr, info_ptr, &mod_time); mod_time - time image was last modified (PNG_VALID_tIME) png_get_bKGD(png_ptr, info_ptr, &background); background - background color (PNG_VALID_bKGD) valid 16-bit red, green and blue values, regardless of color_type num_comments = png_get_text(png_ptr, info_ptr, &text_ptr, &num_text); num_comments - number of comments text_ptr - array of png_text holding image comments text_ptr[i].compression - type of compression used on "text" PNG_TEXT_COMPRESSION_NONE PNG_TEXT_COMPRESSION_zTXt PNG_ITXT_COMPRESSION_NONE PNG_ITXT_COMPRESSION_zTXt text_ptr[i].key - keyword for comment. Must contain 1-79 characters. text_ptr[i].text - text comments for current keyword. Can be empty. text_ptr[i].text_length - length of text string, after decompression, 0 for iTXt text_ptr[i].itxt_length - length of itxt string, after decompression, 0 for tEXt/zTXt text_ptr[i].lang - language of comment (empty string for unknown). text_ptr[i].lang_key - keyword in UTF-8 (empty string for unknown). num_text - number of comments (same as num_comments; you can put NULL here to avoid the duplication) Note while png_set_text() will accept text, language, and translated keywords that can be NULL pointers, the structure returned by png_get_text will always contain regular zero-terminated C strings. They might be empty strings but they will never be NULL pointers. num_spalettes = png_get_sPLT(png_ptr, info_ptr, &palette_ptr); palette_ptr - array of palette structures holding contents of one or more sPLT chunks read. num_spalettes - number of sPLT chunks read. png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y, &unit_type); offset_x - positive offset from the left edge of the screen offset_y - positive offset from the top edge of the screen unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y, &unit_type); res_x - pixels/unit physical resolution in x direction res_y - pixels/unit physical resolution in x direction unit_type - PNG_RESOLUTION_UNKNOWN, PNG_RESOLUTION_METER png_get_sCAL(png_ptr, info_ptr, &unit, &width, &height) unit - physical scale units (an integer) width - width of a pixel in physical scale units height - height of a pixel in physical scale units (width and height are doubles) png_get_sCAL_s(png_ptr, info_ptr, &unit, &width, &height) unit - physical scale units (an integer) width - width of a pixel in physical scale units height - height of a pixel in physical scale units (width and height are strings like "2.54") num_unknown_chunks = png_get_unknown_chunks(png_ptr, info_ptr, &unknowns) unknowns - array of png_unknown_chunk structures holding unknown chunks unknowns[i].name - name of unknown chunk unknowns[i].data - data of unknown chunk unknowns[i].size - size of unknown chunk's data unknowns[i].location - position of chunk in file The value of "i" corresponds to the order in which the chunks were read from the PNG file or inserted with the png_set_unknown_chunks() function.The data from the pHYs chunk can be retrieved in several convenientforms: res_x = png_get_x_pixels_per_meter(png_ptr, info_ptr) res_y = png_get_y_pixels_per_meter(png_ptr, info_ptr) res_x_and_y = png_get_pixels_per_meter(png_ptr, info_ptr) res_x = png_get_x_pixels_per_inch(png_ptr, info_ptr) res_y = png_get_y_pixels_per_inch(png_ptr, info_ptr) res_x_and_y = png_get_pixels_per_inch(png_ptr, info_ptr) aspect_ratio = png_get_pixel_aspect_ratio(png_ptr, info_ptr) (Each of these returns 0 [signifying "unknown"] if the data is not present or if res_x is 0; res_x_and_y is 0 if res_x != res_y)The data from the oFFs chunk can be retrieved in several convenientforms: x_offset = png_get_x_offset_microns(png_ptr, info_ptr); y_offset = png_get_y_offset_microns(png_ptr, info_ptr); x_offset = png_get_x_offset_inches(png_ptr, info_ptr); y_offset = png_get_y_offset_inches(png_ptr, info_ptr); (Each of these returns 0 [signifying "unknown" if both x and y are 0] if the data is not present or if the chunk is present but the unit is the pixel)For more information, see the png_info definition in png.h and thePNG specification for chunk contents. Be careful with trustingrowbytes, as some of the transformations could increase the spaceneeded to hold a row (expand, filler, gray_to_rgb, etc.).See png_read_update_info(), below.A quick word about text_ptr and num_text. PNG stores comments inkeyword/text pairs, one pair per chunk, with no limit on the numberof text chunks, and a 2^31 byte limit on their size. While there aresuggested keywords, there is no requirement to restrict the use to thesestrings. It is strongly suggested that keywords and text be sensibleto humans (that's the point), so don't use abbreviations. Non-printingsymbols are not allowed. See the PNG specification for more details.There is also no requirement to have text after the keyword.Keywords should be limited to 79 Latin-1 characters without leading ortrailing spaces, but non-consecutive spaces are allowed within thekeyword. It is possible to have the same keyword any number of times.The text_ptr is an array of png_text structures, each holding apointer to a language string, a pointer to a keyword and a pointer toa text string. The text string, language code, and translatedkeyword may be empty or NULL pointers. The keyword/textpairs are put into the array in the order that they are received.However, some or all of the text chunks may be after the image, so, tomake sure you have read all the text chunks, don't mess with theseuntil after you read the stuff after the image. This will bementioned again below in the discussion that goes with png_read_end()..SS Input transformationsAfter you've read the header information, you can set up the libraryto handle any special transformations of the image data. The variousways to transform the data will be described in the order that theyshould occur. This is important, as some of these change the colortype and/or bit depth of the data, and some others only work oncertain color types and bit depths. Even though each transformationchecks to see if it has data that it can do something with, you shouldmake sure to only enable a transformation if it will be valid for thedata. For example, don't swap red and blue on grayscale data.The colors used for the background and transparency values should besupplied in the same format/depth as the current image data. Theyare stored in the same format/depth as the image data in a bKGD or tRNSchunk, so this is what libpng expects for this data. The colors aretransformed to keep in sync with the image data when an applicationcalls the png_read_update_info() routine (see below).Data will be decoded into the supplied row buffers packed into bytesunless the library has been told to transform it into another format.For example, 4 bit/pixel paletted or grayscale data will be returned2 pixels/byte with the leftmost pixel in the high-order bits of thebyte, unless png_set_packing() is called. 8-bit RGB data will be storedin RGB RGB RGB format unless png_set_filler() or png_set_add_alpha()is called to insert filler bytes, either before or after each RGB triplet.16-bit RGB data will be returned RRGGBB RRGGBB, with the most significantbyte of the color value first, unless png_set_strip_16() is called totransform it to regular RGB RGB triplets, or png_set_filler() orpng_set_add alpha() is called to insert filler bytes, either before orafter each RRGGBB triplet. Similarly, 8-bit or 16-bit grayscale data canbe modified withpng_set_filler(), png_set_add_alpha(), or png_set_strip_16().The following code transforms grayscale images of less than 8 to 8 bits,changes paletted images to RGB, and adds a full alpha channel if there istransparency information in a tRNS chunk. This is most useful ongrayscale images with bit depths of 2 or 4 or if there is a multiple-imageviewing application that wishes to treat all images in the same way. if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png_ptr); if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr); if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);These three functions are actually aliases for png_set_expand(), addedin libpng version 1.0.4, with the function names expanded to improve codereadability. In some future version they may actually do differentthings.As of libpng version 1.2.9, png_set_expand_gray_1_2_4_to_8() wasadded. It expands the sample depth without changing tRNS to alpha.At the same time, png_set_gray_1_2_4_to_8() was deprecated, and itwill be removed from a future version.PNG can have files with 16 bits per channel. If you only can handle8 bits per channel, this will strip the pixels down to 8 bit. if (bit_depth == 16) png_set_strip_16(png_ptr);If, for some reason, you don't need the alpha channel on an image,and you want to remove it rather than combining it with the background(but the image author certainly had in mind that you *would* combineit with the background, so that's what you should probably do): if (color_type & PNG_COLOR_MASK_ALPHA) png_set_strip_alpha(png_ptr);In PNG files, the alpha channel in an imageis the level of opacity. If you need the alpha channel in an image tobe the level of transparency instead of opacity, you can invert thealpha channel (or the tRNS chunk data) after it's read, so that 0 isfully opaque and 255 (in 8-bit or paletted images) or 65535 (in 16-bitimages) is fully transparent, with png_set_invert_alpha(png_ptr);PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small asthey can, resulting in, for example, 8 pixels per byte for 1 bitfiles. This code expands to 1 pixel per byte without changing thevalues of the pixels: if (bit_depth < 8) png_set_packing(png_ptr);PNG files have possible bit depths of 1, 2, 4, 8, and 16. All pixelsstored in a PNG image have been "scaled" or "shifted" up to the nexthigher possible bit depth (e.g. from 5 bits/sample in the range [0,31] to8 bits/sample in the range [0, 255]). However, it is also possible toconvert the PNG pixel data back to the original bit depth of the image.This call reduces the pixels back down to the original bit depth: png_color_8p sig_bit; if (png_get_sBIT(png_ptr, info_ptr, &sig_bit)) png_set_shift(png_ptr, sig_bit);PNG files store 3-color pixels in red, green, blue order. This codechanges the storage of the pixels to blue, green, red: if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_RGB_ALPHA) png_set_bgr(png_ptr);PNG files store RGB pixels packed into 3 or 6 bytes. This code expands theminto 4 or 8 bytes for windowing systems that need them in this format: if (color_type == PNG_COLOR_TYPE_RGB) png_set_filler(png_ptr, filler, PNG_FILLER_BEFORE);where "filler" is the 8 or 16-bit number to fill with, and the location iseither PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whetheryou want the filler before the RGB or after. This transformationdoes not affect images that already have full alpha channels. To add anopaque alpha channel, use
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -