?? pluginpng.cpp
字號:
case PNG_COLOR_TYPE_RGB_ALPHA :
dib = FreeImage_Allocate(width, height, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
break;
case PNG_COLOR_TYPE_PALETTE :
dib = FreeImage_Allocate(width, height, pixel_depth);
png_get_PLTE(png_ptr,info_ptr, &png_palette,&palette_entries);
palette = FreeImage_GetPalette(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_SetTransparencyTable(dib, (BYTE *)trans, num_trans);
break;
case PNG_COLOR_TYPE_GRAY:
dib = FreeImage_AllocateT(image_type, width, height, pixel_depth);
if(pixel_depth <= 8) {
palette = FreeImage_GetPalette(dib);
palette_entries = 1 << pixel_depth;
for (i = 0; i < palette_entries; i++) {
palette[i].rgbRed =
palette[i].rgbGreen =
palette[i].rgbBlue = (i * 255) / (palette_entries - 1);
}
}
// store the transparency table
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
FreeImage_SetTransparencyTable(dib, (BYTE *)trans, num_trans);
break;
default:
throw "PNG format not supported";
}
// store the background color
if(image_background) {
FreeImage_SetBackgroundColor(dib, &rgbBkColor);
}
// get physical resolution
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_pHYs)) {
png_uint_32 res_x, res_y;
// we'll overload this var and use 0 to mean no phys data,
// since if it's not in meters we can't use it anyway
int res_unit_type = 0;
png_get_pHYs(png_ptr,info_ptr,&res_x,&res_y,&res_unit_type);
if (res_unit_type == 1) {
BITMAPINFOHEADER *bih = FreeImage_GetInfoHeader(dib);
bih->biXPelsPerMeter = res_x;
bih->biYPelsPerMeter = res_y;
}
}
// get possible ICC profile
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_iCCP)) {
png_charp profile_name = NULL;
png_charp profile_data = NULL;
png_uint_32 profile_length = 0;
int compression_type;
png_get_iCCP(png_ptr, info_ptr, &profile_name, &compression_type, &profile_data, &profile_length);
// copy ICC profile data (must be done after FreeImage_Allocate)
FreeImage_CreateICCProfile(dib, profile_data, profile_length);
}
// set the individual row_pointers to point at the correct offsets
row_pointers = (png_bytepp)malloc(height * sizeof(png_bytep));
if (!row_pointers) {
if (palette)
png_free(png_ptr, palette);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
FreeImage_Unload(dib);
return NULL;
}
// read in the bitmap bits via the pointer table
for (png_uint_32 k = 0; k < height; k++)
row_pointers[height - 1 - k] = FreeImage_GetScanLine(dib, k);
png_read_image(png_ptr, row_pointers);
// check if the bitmap contains transparency, if so enable it in the header
if (FreeImage_GetBPP(dib) == 32)
if (FreeImage_GetColorType(dib) == FIC_RGBALPHA)
FreeImage_SetTransparent(dib, TRUE);
else
FreeImage_SetTransparent(dib, FALSE);
// cleanup
if (row_pointers) {
free(row_pointers);
row_pointers = NULL;
}
// read the rest of the file, getting any additional chunks in info_ptr
png_read_end(png_ptr, info_ptr);
// get possible metadata (it can be located both before and after the image data)
ReadMetadata(png_ptr, info_ptr, dib);
if (png_ptr) {
// clean up after the read, and free any memory allocated - REQUIRED
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
}
return dib;
} catch (const char *text) {
if (png_ptr)
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
if (row_pointers)
free(row_pointers);
if (dib)
FreeImage_Unload(dib);
FreeImage_OutputMessageProc(s_format_id, text);
return NULL;
}
}
return NULL;
}
static BOOL DLL_CALLCONV
Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data) {
png_structp png_ptr;
png_infop info_ptr;
png_colorp palette = NULL;
png_uint_32 width, height;
BOOL has_alpha_channel = FALSE;
RGBQUAD *pal; // pointer to dib palette
int bit_depth, pixel_depth; // pixel_depth = bit_depth * channels
int palette_entries;
int interlace_type;
s_io = io;
s_handle = handle;
if ((dib) && (handle)) {
try {
// create the chunk manage structure
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)error_handler, error_handler, warning_handler);
if (!png_ptr) {
return FALSE;
}
// allocate/initialize the image information data.
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
return FALSE;
}
// Set error handling. REQUIRED if you aren't supplying your own
// error handling functions in the png_create_write_struct() call.
if (setjmp(png_jmpbuf(png_ptr))) {
// if we get here, we had a problem reading the file
png_destroy_write_struct(&png_ptr, &info_ptr);
return FALSE;
}
// init the IO
png_set_write_fn(png_ptr, info_ptr, _WriteProc, _FlushProc);
// set physical resolution
BITMAPINFOHEADER *bih = FreeImage_GetInfoHeader(dib);
png_uint_32 res_x = bih->biXPelsPerMeter;
png_uint_32 res_y = bih->biYPelsPerMeter;
if ((res_x > 0) && (res_y > 0)) {
png_set_pHYs(png_ptr, info_ptr, res_x, res_y, 1);
}
// Set the image information here. Width and height are up to 2^31,
// bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
// the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
// PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
// or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
// PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
// currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
interlace_type = PNG_INTERLACE_NONE; // Default value
width = FreeImage_GetWidth(dib);
height = FreeImage_GetHeight(dib);
pixel_depth = FreeImage_GetBPP(dib);
if(FreeImage_GetImageType(dib) == FIT_BITMAP) {
// standard image type
bit_depth = (pixel_depth > 8) ? 8 : pixel_depth;
} else {
// 16-bit greyscale
bit_depth = pixel_depth;
}
switch (FreeImage_GetColorType(dib)) {
case FIC_MINISWHITE:
// Invert monochrome files to have 0 as black and 1 as white (no break here)
png_set_invert_mono(png_ptr);
case FIC_MINISBLACK:
png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
PNG_COLOR_TYPE_GRAY, interlace_type,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
break;
case FIC_PALETTE:
{
png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
PNG_COLOR_TYPE_PALETTE, interlace_type,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
// set the palette
palette_entries = 1 << bit_depth;
palette = (png_colorp)png_malloc(png_ptr, palette_entries * sizeof (png_color));
pal = FreeImage_GetPalette(dib);
for (int i = 0; i < palette_entries; i++) {
palette[i].red = pal[i].rgbRed;
palette[i].green = pal[i].rgbGreen;
palette[i].blue = pal[i].rgbBlue;
}
png_set_PLTE(png_ptr, info_ptr, palette, palette_entries);
// You must not free palette here, because png_set_PLTE only makes a link to
// the palette that you malloced. Wait until you are about to destroy
// the png structure.
break;
}
case FIC_RGBALPHA :
has_alpha_channel = TRUE;
png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
PNG_COLOR_TYPE_RGBA, interlace_type,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
#ifndef FREEIMAGE_BIGENDIAN
// flip BGR pixels to RGB
png_set_bgr(png_ptr);
#endif
break;
case FIC_RGB:
png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
PNG_COLOR_TYPE_RGB, interlace_type,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
#ifndef FREEIMAGE_BIGENDIAN
// flip BGR pixels to RGB
png_set_bgr(png_ptr);
#endif
break;
case FIC_CMYK:
break;
}
// write possible ICC profile
FIICCPROFILE *iccProfile = FreeImage_GetICCProfile(dib);
if (iccProfile->size && iccProfile->data) {
png_set_iCCP(png_ptr, info_ptr, "Embedded Profile", 0, (png_charp)iccProfile->data, iccProfile->size);
}
// write metadata
WriteMetadata(png_ptr, info_ptr, dib);
// Optional gamma chunk is strongly suggested if you have any guess
// as to the correct gamma of the image.
// png_set_gAMA(png_ptr, info_ptr, gamma);
// set the transparency table
if ((pixel_depth == 8) && (FreeImage_IsTransparent(dib)) && (FreeImage_GetTransparencyCount(dib) > 0))
png_set_tRNS(png_ptr, info_ptr, FreeImage_GetTransparencyTable(dib), FreeImage_GetTransparencyCount(dib), NULL);
// set the background color
if(FreeImage_HasBackgroundColor(dib)) {
png_color_16 image_background;
RGBQUAD rgbBkColor;
FreeImage_GetBackgroundColor(dib, &rgbBkColor);
memset(&image_background, 0, sizeof(png_color_16));
image_background.blue = rgbBkColor.rgbBlue;
image_background.green = rgbBkColor.rgbGreen;
image_background.red = rgbBkColor.rgbRed;
image_background.index = rgbBkColor.rgbReserved;
png_set_bKGD(png_ptr, info_ptr, &image_background);
}
// Write the file header information.
png_write_info(png_ptr, info_ptr);
// write out the image data
#ifndef FREEIMAGE_BIGENDIAN
if (bit_depth == 16) {
// turn on 16 bit byte swapping
png_set_swap(png_ptr);
}
#endif
if ((pixel_depth == 32) && (!has_alpha_channel)) {
BYTE *buffer = (BYTE *)malloc(width * 3);
// transparent conversion to 24-bit
for (png_uint_32 k = 0; k < height; k++) {
FreeImage_ConvertLine32To24(buffer, FreeImage_GetScanLine(dib, height - k - 1), width);
png_write_row(png_ptr, buffer);
}
free(buffer);
} else {
for (png_uint_32 k = 0; k < height; k++) {
png_write_row(png_ptr, FreeImage_GetScanLine(dib, height - k - 1));
}
}
// It is REQUIRED to call this to finish writing the rest of the file
// Bug with png_flush
png_write_end(png_ptr, info_ptr);
// clean up after the write, and free any memory allocated
if (palette)
png_free(png_ptr, palette);
png_destroy_write_struct(&png_ptr, &info_ptr);
return TRUE;
} catch (const char *text) {
FreeImage_OutputMessageProc(s_format_id, text);
}
}
return FALSE;
}
// ==========================================================
// Init
// ==========================================================
void DLL_CALLCONV
InitPNG(Plugin *plugin, int format_id) {
s_format_id = format_id;
plugin->format_proc = Format;
plugin->description_proc = Description;
plugin->extension_proc = Extension;
plugin->regexpr_proc = RegExpr;
plugin->open_proc = NULL;
plugin->close_proc = NULL;
plugin->pagecount_proc = NULL;
plugin->pagecapability_proc = NULL;
plugin->load_proc = Load;
plugin->save_proc = Save;
plugin->validate_proc = Validate;
plugin->mime_proc = MimeType;
plugin->supports_export_bpp_proc = SupportsExportDepth;
plugin->supports_export_type_proc = SupportsExportType;
plugin->supports_icc_profiles_proc = SupportsICCProfiles;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -