BOOL DLL_CALLCONV FreeImage_GetBackgroundColor(FIBITMAP *dib, RGBQUAD *bkcolor) { if(dib && bkcolor) { if(FreeImage_HasBackgroundColor(dib)) { // get the background color RGBQUAD *bkgnd_color = &((FREEIMAGEHEADER *)dib->data)->bkgnd_color; memcpy(bkcolor, bkgnd_color, sizeof(RGBQUAD)); // get the background index if(FreeImage_GetBPP(dib) == 8) { RGBQUAD *pal = FreeImage_GetPalette(dib); for(unsigned i = 0; i < FreeImage_GetColorsUsed(dib); i++) { if(bkgnd_color->rgbRed == pal[i].rgbRed) { if(bkgnd_color->rgbGreen == pal[i].rgbGreen) { if(bkgnd_color->rgbBlue == pal[i].rgbBlue) { bkcolor->rgbReserved = i; return TRUE; } } } } } bkcolor->rgbReserved = 0; return TRUE; } } return FALSE; }
/** @brief Composite a foreground image against a background color or a background image. The equation for computing a composited sample value is:<br> output = alpha * foreground + (1-alpha) * background<br> where alpha and the input and output sample values are expressed as fractions in the range 0 to 1. For colour images, the computation is done separately for R, G, and B samples. @param fg Foreground image @param useFileBkg If TRUE and a file background is present, use it as the background color @param appBkColor If not equal to NULL, and useFileBkg is FALSE, use this color as the background color @param bg If not equal to NULL and useFileBkg is FALSE and appBkColor is NULL, use this as the background image @return Returns the composite image if successful, returns NULL otherwise @see FreeImage_IsTransparent, FreeImage_HasBackgroundColor */ FIBITMAP * DLL_CALLCONV FreeImage_Composite(FIBITMAP *fg, BOOL useFileBkg, RGBQUAD *appBkColor, FIBITMAP *bg) { if(!FreeImage_HasPixels(fg)) return NULL; int width = FreeImage_GetWidth(fg); int height = FreeImage_GetHeight(fg); int bpp = FreeImage_GetBPP(fg); if((bpp != 8) && (bpp != 32)) return NULL; if(bg) { int bg_width = FreeImage_GetWidth(bg); int bg_height = FreeImage_GetHeight(bg); int bg_bpp = FreeImage_GetBPP(bg); if((bg_width != width) || (bg_height != height) || (bg_bpp != 24)) return NULL; } int bytespp = (bpp == 8) ? 1 : 4; int x, y, c; BYTE alpha = 0, not_alpha; BYTE index; RGBQUAD fgc; // foreground color RGBQUAD bkc; // background color memset(&fgc, 0, sizeof(RGBQUAD)); memset(&bkc, 0, sizeof(RGBQUAD)); // allocate the composite image FIBITMAP *composite = FreeImage_Allocate(width, height, 24, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK); if(!composite) return NULL; // get the palette RGBQUAD *pal = FreeImage_GetPalette(fg); // retrieve the alpha table from the foreground image BOOL bIsTransparent = FreeImage_IsTransparent(fg); BYTE *trns = FreeImage_GetTransparencyTable(fg); // retrieve the background color from the foreground image BOOL bHasBkColor = FALSE; if(useFileBkg && FreeImage_HasBackgroundColor(fg)) { FreeImage_GetBackgroundColor(fg, &bkc); bHasBkColor = TRUE; } else { // no file background color // use application background color ? if(appBkColor) { memcpy(&bkc, appBkColor, sizeof(RGBQUAD)); bHasBkColor = TRUE; } // use background image ? else if(bg) { bHasBkColor = FALSE; } } for(y = 0; y < height; y++) { // foreground BYTE *fg_bits = FreeImage_GetScanLine(fg, y); // background BYTE *bg_bits = FreeImage_GetScanLine(bg, y); // composite image BYTE *cp_bits = FreeImage_GetScanLine(composite, y); for(x = 0; x < width; x++) { // foreground color + alpha if(bpp == 8) { // get the foreground color index = fg_bits[0]; memcpy(&fgc, &pal[index], sizeof(RGBQUAD)); // get the alpha if(bIsTransparent) { alpha = trns[index]; } else { alpha = 255; } } else if(bpp == 32) { // get the foreground color fgc.rgbBlue = fg_bits[FI_RGBA_BLUE]; fgc.rgbGreen = fg_bits[FI_RGBA_GREEN]; fgc.rgbRed = fg_bits[FI_RGBA_RED]; // get the alpha alpha = fg_bits[FI_RGBA_ALPHA]; } // background color if(!bHasBkColor) { if(bg) { // get the background color from the background image bkc.rgbBlue = bg_bits[FI_RGBA_BLUE]; bkc.rgbGreen = bg_bits[FI_RGBA_GREEN]; bkc.rgbRed = bg_bits[FI_RGBA_RED]; } else { // use a checkerboard pattern c = (((y & 0x8) == 0) ^ ((x & 0x8) == 0)) * 192; c = c ? c : 255; bkc.rgbBlue = (BYTE)c; bkc.rgbGreen = (BYTE)c; bkc.rgbRed = (BYTE)c; } } // composition if(alpha == 0) { // output = background cp_bits[FI_RGBA_BLUE] = bkc.rgbBlue; cp_bits[FI_RGBA_GREEN] = bkc.rgbGreen; cp_bits[FI_RGBA_RED] = bkc.rgbRed; } else if(alpha == 255) { // output = foreground cp_bits[FI_RGBA_BLUE] = fgc.rgbBlue; cp_bits[FI_RGBA_GREEN] = fgc.rgbGreen; cp_bits[FI_RGBA_RED] = fgc.rgbRed; } else { // output = alpha * foreground + (1-alpha) * background not_alpha = (BYTE)~alpha; cp_bits[FI_RGBA_BLUE] = (BYTE)((alpha * (WORD)fgc.rgbBlue + not_alpha * (WORD)bkc.rgbBlue) >> 8); cp_bits[FI_RGBA_GREEN] = (BYTE)((alpha * (WORD)fgc.rgbGreen + not_alpha * (WORD)bkc.rgbGreen) >> 8); cp_bits[FI_RGBA_RED] = (BYTE)((alpha * (WORD)fgc.rgbRed + not_alpha * (WORD)bkc.rgbRed) >> 8); } fg_bits += bytespp; bg_bits += 3; cp_bits += 3; } } // copy metadata from src to dst FreeImage_CloneMetadata(composite, fg); return composite; }
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; fi_ioStructure fio; fio.s_handle = handle; fio.s_io = io; if ((dib) && (handle)) { try { // create the chunk manage structure png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, 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, &fio, _WriteProc, _FlushProc); // set physical resolution png_uint_32 res_x = (png_uint_32)FreeImage_GetDotsPerMeterX(dib); png_uint_32 res_y = (png_uint_32)FreeImage_GetDotsPerMeterY(dib); if ((res_x > 0) && (res_y > 0)) { png_set_pHYs(png_ptr, info_ptr, res_x, res_y, PNG_RESOLUTION_METER); } // 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 width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); pixel_depth = FreeImage_GetBPP(dib); BOOL bInterlaced = FALSE; if( (flags & PNG_INTERLACED) == PNG_INTERLACED) { interlace_type = PNG_INTERLACE_ADAM7; bInterlaced = TRUE; } else { interlace_type = PNG_INTERLACE_NONE; } // set the ZLIB compression level or default to PNG default compression level (ZLIB level = 6) int zlib_level = flags & 0x0F; if((zlib_level >= 1) && (zlib_level <= 9)) { png_set_compression_level(png_ptr, zlib_level); } else if((flags & PNG_Z_NO_COMPRESSION) == PNG_Z_NO_COMPRESSION) { png_set_compression_level(png_ptr, Z_NO_COMPRESSION); } // filtered strategy works better for high color images if(pixel_depth >= 16){ png_set_compression_strategy(png_ptr, Z_FILTERED); png_set_filter(png_ptr, 0, PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_PAETH); } else { png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY); } FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib); if(image_type == FIT_BITMAP) { // standard image type bit_depth = (pixel_depth > 8) ? 8 : pixel_depth; } else { // 16-bit greyscale or 16-bit RGB(A) bit_depth = 16; } 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); #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR // flip BGR pixels to RGB if(image_type == FIT_BITMAP) { 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); #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR // flip BGR pixels to RGB if(image_type == FIT_BITMAP) { 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_const_bytep)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 (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 int number_passes = 1; if (bInterlaced) { number_passes = png_set_interlace_handling(png_ptr); } if ((pixel_depth == 32) && (!has_alpha_channel)) { BYTE *buffer = (BYTE *)malloc(width * 3); // transparent conversion to 24-bit // the number of passes is either 1 for non-interlaced images, or 7 for interlaced images for (int pass = 0; pass < number_passes; pass++) { 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 { // the number of passes is either 1 for non-interlaced images, or 7 for interlaced images for (int pass = 0; pass < number_passes; pass++) { 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; }
BOOL fipImage::hasFileBkColor() const { return FreeImage_HasBackgroundColor(_dib); }
void fipWinImage::drawEx(HDC hDC, RECT& rcDest, BOOL useFileBkg, RGBQUAD *appBkColor, FIBITMAP *bg) const { // Convert to a standard bitmap if needed if(_bHasChanged) { if(_bDeleteMe) { FreeImage_Unload(_display_dib); _display_dib = NULL; _bDeleteMe = FALSE; } FREE_IMAGE_TYPE image_type = getImageType(); if(image_type == FIT_BITMAP) { BOOL bHasBackground = FreeImage_HasBackgroundColor(_dib); BOOL bIsTransparent = FreeImage_IsTransparent(_dib); if(!bIsTransparent && (!bHasBackground || !useFileBkg)) { // Copy pointer _display_dib = _dib; } else { // Create the transparent / alpha blended image _display_dib = FreeImage_Composite(_dib, useFileBkg, appBkColor, bg); if(_display_dib) { // Remember to delete _display_dib _bDeleteMe = TRUE; } else { // Something failed: copy pointers _display_dib = _dib; } } } else { // Convert to a standard dib for display if(image_type == FIT_COMPLEX) { // Convert to type FIT_DOUBLE FIBITMAP *dib_double = FreeImage_GetComplexChannel(_dib, FICC_MAG); // Convert to a standard bitmap (linear scaling) _display_dib = FreeImage_ConvertToStandardType(dib_double, TRUE); // Free image of type FIT_DOUBLE FreeImage_Unload(dib_double); } else if((image_type == FIT_RGBF) || (image_type == FIT_RGB16)) { // Apply a tone mapping algorithm and convert to 24-bit _display_dib = FreeImage_ToneMapping(_dib, _tmo, _tmo_param_1, _tmo_param_2); } else if(image_type == FIT_RGBA16) { // Convert to 32-bit FIBITMAP *dib32 = FreeImage_ConvertTo32Bits(_dib); if(dib32) { // Create the transparent / alpha blended image _display_dib = FreeImage_Composite(dib32, useFileBkg, appBkColor, bg); FreeImage_Unload(dib32); } } else { // Other cases: convert to a standard bitmap (linear scaling) _display_dib = FreeImage_ConvertToStandardType(_dib, TRUE); } // Remember to delete _display_dib _bDeleteMe = TRUE; } _bHasChanged = FALSE; } // Draw the dib SetStretchBltMode(hDC, COLORONCOLOR); StretchDIBits(hDC, rcDest.left, rcDest.top, rcDest.right-rcDest.left, rcDest.bottom-rcDest.top, 0, 0, FreeImage_GetWidth(_display_dib), FreeImage_GetHeight(_display_dib), FreeImage_GetBits(_display_dib), FreeImage_GetInfo(_display_dib), DIB_RGB_COLORS, SRCCOPY); }
/** * @return if the picture has an background color, else false */ bool fipImage::hasFileBkColor() const{ return FreeImage_HasBackgroundColor( pImageData ); }