📄 libpng.3
字号:
After you have called png_read_png(), you can retrieve the image datawith row_pointers = png_get_rows(png_ptr, info_ptr);where row_pointers is an array of pointers to the pixel data for each row: png_bytep row_pointers[height];If you know your image size and pixel size ahead of time, you can allocaterow_pointers prior to calling png_read_png() with if (height > PNG_UINT_32_MAX/png_sizeof(png_byte)) png_error (png_ptr, "Image is too tall to process in memory"); if (width > PNG_UINT_32_MAX/pixel_size) png_error (png_ptr, "Image is too wide to process in memory"); row_pointers = png_malloc(png_ptr, height*png_sizeof(png_bytep)); for (int i=0; i<height, i++) row_pointers[i]=png_malloc(png_ptr, width*pixel_size); png_set_rows(png_ptr, info_ptr, &row_pointers);Alternatively you could allocate your image in one big block and definerow_pointers[i] to point into the proper places in your block.If you use png_set_rows(), the application is responsible for freeingrow_pointers (and row_pointers[i], if they were separately allocated).If you don't allocate row_pointers ahead of time, png_read_png() willdo it, and it'll be free'ed when you call png_destroy_*()..SS The low-level read interfaceIf you are going the low-level route, you are now ready to read allthe file information up to the actual image data. You do this with acall to png_read_info(). png_read_info(png_ptr, info_ptr);This will process all chunks up to but not including the image data..SS Querying the info structureFunctions are used to get the information from the info_ptr once ithas been read. Note that these fields may not be completely filledin until png_read_end() has read the chunk data following the image. png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method); width - holds the width of the image in pixels (up to 2^31). height - holds the height of the image in pixels (up to 2^31). bit_depth - holds the bit depth of one of the image channels. (valid values are 1, 2, 4, 8, 16 and depend also on the color_type. See also significant bits (sBIT) below). color_type - describes which color/alpha channels are present. PNG_COLOR_TYPE_GRAY (bit depths 1, 2, 4, 8, 16) PNG_COLOR_TYPE_GRAY_ALPHA (bit depths 8, 16) PNG_COLOR_TYPE_PALETTE (bit depths 1, 2, 4, 8) PNG_COLOR_TYPE_RGB (bit_depths 8, 16) PNG_COLOR_TYPE_RGB_ALPHA (bit_depths 8, 16) PNG_COLOR_MASK_PALETTE PNG_COLOR_MASK_COLOR PNG_COLOR_MASK_ALPHA filter_method - (must be PNG_FILTER_TYPE_BASE for PNG 1.0, and can also be PNG_INTRAPIXEL_DIFFERENCING if the PNG datastream is embedded in a MNG-1.0 datastream) compression_type - (must be PNG_COMPRESSION_TYPE_BASE for PNG 1.0) interlace_type - (PNG_INTERLACE_NONE or PNG_INTERLACE_ADAM7) Any or all of interlace_type, compression_type, of filter_method can be NULL if you are not interested in their values. channels = png_get_channels(png_ptr, info_ptr); channels - number of channels of info for the color type (valid values are 1 (GRAY, PALETTE), 2 (GRAY_ALPHA), 3 (RGB), 4 (RGB_ALPHA or RGB + filler byte)) rowbytes = png_get_rowbytes(png_ptr, info_ptr); rowbytes - number of bytes needed to hold a row signature = png_get_signature(png_ptr, info_ptr); signature - holds the signature read from the file (if any). The data is kept in the same offset it would be if the whole signature were read (i.e. if an application had already read in 4 bytes of signature before starting libpng, the remaining 4 bytes would be in signature[4] through signature[7] (see png_set_sig_bytes())). width = png_get_image_width(png_ptr, info_ptr); height = png_get_image_height(png_ptr, info_ptr); 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -