📄 pluginpng.cpp
字号:
// ==========================================================// PNG Loader and Writer//// Design and implementation by// - Floris van den Berg (flvdberg@wxs.nl)// - Herve Drolon (drolon@infonie.fr)//// This file is part of FreeImage 2//// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER// THIS DISCLAIMER.//// Use at your own risk!// ==========================================================#include "thirdparty/common/FreeImage/Source/FreeImage.h"#include "thirdparty/common/FreeImage/Source/Utilities.h"#include <stdlib.h>#include <memory.h> // ----------------------------------------------------------#define PNG_ASSEMBLER_CODE_SUPPORTED#define PNG_BYTES_TO_CHECK 8// ----------------------------------------------------------#include "thirdparty/common/LibPNG/png.h"// ----------------------------------------------------------static FreeImageIO *s_io;static fi_handle s_handle;/////////////////////////////////////////////////////////////////////////////// libpng interface // static void_ReadProc(struct png_struct_def *, unsigned char *data, unsigned int size) { s_io->read_proc(data, size, 1, s_handle);}static void_WriteProc(struct png_struct_def *, unsigned char *data, unsigned int size) { s_io->write_proc(data, size, 1, s_handle);}static void_FlushProc(png_structp png_ptr) { // empty flush implementation}static voiderror_handler(struct png_struct_def *, const char *error) { throw error;}// in FreeImage warnings disabledstatic voidwarning_handler(struct png_struct_def *, const char *warning) {}// ==========================================================// Plugin Interface// ==========================================================static int s_format_id;// ==========================================================// Plugin Implementation// ==========================================================static const char * DLL_CALLCONVFormat() { return "PNG";}static const char * DLL_CALLCONVDescription() { return "Portable Network Graphics";}static const char * DLL_CALLCONVExtension() { return "png";}static const char * DLL_CALLCONVRegExpr() { return "^.PNG\r";}static const char * DLL_CALLCONVMimeType() { return "image/png";}static BOOL DLL_CALLCONVValidate(FreeImageIO &io, fi_handle handle) { BYTE png_signature[8] = { 137, 80, 78, 71, 13, 10, 26, 10 }; BYTE signature[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; io.read_proc(&signature, 1, 8, handle); return (memcmp(png_signature, signature, 8) == 0);}// ----------------------------------------------------------static FIBITMAP * DLL_CALLCONVLoad(FreeImage &freeimage, FreeImageIO &io, fi_handle handle, int page, int flags, void *data) { png_structp png_ptr; png_infop info_ptr; png_uint_32 width, height; png_colorp png_palette; int bpp, color_type, palette_entries; FIBITMAP *dib = NULL; RGBQUAD *palette; // pointer to dib palette png_bytepp row_pointers = NULL; int i; s_io = &io; s_handle = handle; if (handle) { try { // check to see if the file is in fact a PNG file unsigned char png_check[PNG_BYTES_TO_CHECK]; io.read_proc(png_check, 1, PNG_BYTES_TO_CHECK, handle); if (png_sig_cmp(png_check, (png_size_t)0, PNG_BYTES_TO_CHECK) != 0) return NULL; // Bad signature // create the chunk manage structure png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)error_handler, error_handler, warning_handler); if (!png_ptr) return NULL; // create the info structure info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); return NULL; } // init the IO png_set_read_fn(png_ptr, info_ptr, _ReadProc); if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return NULL; } // Because we have already read the signature... png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); // read the IHDR chunk png_read_info(png_ptr, info_ptr); png_get_IHDR(png_ptr, info_ptr, &width, &height, &bpp, &color_type, NULL, NULL, NULL); // DIB's don't support >8 bits per sample // => tell libpng to strip 16 bit/color files down to 8 bits/color if (bpp == 16) { png_set_strip_16(png_ptr); bpp = 8; } // Set some additional flags switch(color_type) { case PNG_COLOR_TYPE_RGB: case PNG_COLOR_TYPE_RGB_ALPHA: // Flip the RGB pixels to BGR (or RGBA to BGRA) png_set_bgr(png_ptr); break; case PNG_COLOR_TYPE_PALETTE: // Expand palette images to the full 8 bits from 2 or 4 bits/pixel if ((bpp == 2) || (bpp == 4)) { png_set_packing(png_ptr); bpp = 8; } break; case PNG_COLOR_TYPE_GRAY: case PNG_COLOR_TYPE_GRAY_ALPHA: // Expand grayscale images to the full 8 bits from 2 or 4 bits/pixel if ((bpp == 2) || (bpp == 4)) { png_set_expand(png_ptr); bpp = 8; } break; default: throw "PNG format not supported"; } // Set the background color to draw transparent and alpha images over. // It is possible to set the red, green, and blue components directly // for paletted images instead of supplying a palette index. Note that // even if the PNG file supplies a background, you are not required to // use it - you should use the (solid) application background if it has one. if (color_type != PNG_COLOR_TYPE_RGB_ALPHA) { png_color_16 my_background= { 0, 255, 255, 255, 0 }; png_color_16 *image_background; if (png_get_bKGD(png_ptr, info_ptr, &image_background)) png_set_background(png_ptr, image_background, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); else png_set_background(png_ptr, &my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); } // if this image has transparency, store the trns values png_bytep trans = NULL; int num_trans = 0; png_color_16p trans_values = NULL; png_uint_32 transparent_value = png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values); // unlike the example in the libpng documentation, we have *no* idea where // this file may have come from--so if it doesn't have a file gamma, don't // do any correction ("do no harm") double gamma = 0; double screen_gamma = 2.2; if (png_get_gAMA(png_ptr, info_ptr, &gamma)) png_set_gamma(png_ptr, screen_gamma, gamma); // All transformations have been registered; now update info_ptr data png_read_update_info(png_ptr, info_ptr); // Create a DIB and write the bitmap header // set up the DIB palette, if needed switch (color_type) { case PNG_COLOR_TYPE_RGB: png_set_invert_alpha(png_ptr); dib = freeimage.allocate_proc(width, height, 24, 0xFF, 0xFF00, 0xFF0000); break; case PNG_COLOR_TYPE_RGB_ALPHA : dib = freeimage.allocate_proc(width, height, 32, 0xFF, 0xFF00, 0xFF0000); break; case PNG_COLOR_TYPE_PALETTE : dib = freeimage.allocate_proc(width, height, bpp, 0, 0, 0); png_get_PLTE(png_ptr,info_ptr, &png_palette,&palette_entries); palette = freeimage.get_palette_proc(dib); // store the palette for (i = 0; i < palette_entries; i++) { palette[i].rgbRed = png_palette[i].red; palette[i].rgbGreen = png_palette[i].green; palette[i].rgbBlue = png_palette[i].blue; } // store the transparency table if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) freeimage.set_transparency_table_proc(dib, (BYTE *)trans, num_trans); break; case PNG_COLOR_TYPE_GRAY: case PNG_COLOR_TYPE_GRAY_ALPHA: dib = freeimage.allocate_proc(width, height, bpp, 0, 0, 0); palette = freeimage.get_palette_proc(dib); palette_entries = 1 << bpp; for (i = 0; i < palette_entries; i++) { palette[i].rgbRed = palette[i].rgbGreen = palette[i].rgbBlue = (i * 255) / (palette_entries - 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -