bool wxBMPHandler::SaveDib(wxImage *image, wxOutputStream& stream, bool verbose, bool IsBmp, bool IsMask) { wxCHECK_MSG( image, false, wxT("invalid pointer in wxBMPHandler::SaveFile") ); if ( !image->Ok() ) { if ( verbose ) { wxLogError(_("BMP: Couldn't save invalid image.")); } return false; } // get the format of the BMP file to save, else use 24bpp unsigned format = wxBMP_24BPP; if ( image->HasOption(wxIMAGE_OPTION_BMP_FORMAT) ) format = image->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT); wxUint16 bpp; // # of bits per pixel int palette_size; // # of color map entries, ie. 2^bpp colors // set the bpp and appropriate palette_size, and do additional checks if ( (format == wxBMP_1BPP) || (format == wxBMP_1BPP_BW) ) { bpp = 1; palette_size = 2; } else if ( format == wxBMP_4BPP ) { bpp = 4; palette_size = 16; } else if ( (format == wxBMP_8BPP) || (format == wxBMP_8BPP_GREY) || (format == wxBMP_8BPP_RED) || (format == wxBMP_8BPP_PALETTE) ) { // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE? if ((format == wxBMP_8BPP_PALETTE) #if wxUSE_PALETTE && !image->HasPalette() #endif // wxUSE_PALETTE ) { if ( verbose ) { wxLogError(_("BMP: wxImage doesn't have own wxPalette.")); } return false; } bpp = 8; palette_size = 256; } else // you get 24bpp { format = wxBMP_24BPP; bpp = 24; palette_size = 0; } unsigned width = image->GetWidth(); unsigned row_padding = (4 - int(width*bpp/8.0) % 4) % 4; // # bytes to pad to dword unsigned row_width = int(width * bpp/8.0) + row_padding; // # of bytes per row struct { // BitmapHeader: wxUint16 magic; // format magic, always 'BM' wxUint32 filesize; // total file size, inc. headers wxUint32 reserved; // for future use wxUint32 data_offset; // image data offset in the file // BitmapInfoHeader: wxUint32 bih_size; // 2nd part's size wxUint32 width, height; // bitmap's dimensions wxUint16 planes; // num of planes wxUint16 bpp; // bits per pixel wxUint32 compression; // compression method wxUint32 size_of_bmp; // size of the bitmap wxUint32 h_res, v_res; // image resolution in pixels-per-meter wxUint32 num_clrs; // number of colors used wxUint32 num_signif_clrs;// number of significant colors } hdr; wxUint32 hdr_size = 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/; hdr.magic = wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/); hdr.filesize = wxUINT32_SWAP_ON_BE( hdr_size + palette_size*4 + row_width * image->GetHeight() ); hdr.reserved = 0; hdr.data_offset = wxUINT32_SWAP_ON_BE(hdr_size + palette_size*4); hdr.bih_size = wxUINT32_SWAP_ON_BE(hdr_size - 14); hdr.width = wxUINT32_SWAP_ON_BE(image->GetWidth()); if ( IsBmp ) { hdr.height = wxUINT32_SWAP_ON_BE(image->GetHeight()); } else { hdr.height = wxUINT32_SWAP_ON_BE(2 * image->GetHeight()); } hdr.planes = wxUINT16_SWAP_ON_BE(1); // always 1 plane hdr.bpp = wxUINT16_SWAP_ON_BE(bpp); hdr.compression = 0; // RGB uncompressed hdr.size_of_bmp = wxUINT32_SWAP_ON_BE(row_width * image->GetHeight()); // get the resolution from the image options or fall back to 72dpi standard // for the BMP format if not specified int hres, vres; switch ( GetResolutionFromOptions(*image, &hres, &vres) ) { default: wxFAIL_MSG( wxT("unexpected image resolution units") ); // fall through case wxIMAGE_RESOLUTION_NONE: hres = vres = 72; // fall through to convert it to correct units case wxIMAGE_RESOLUTION_INCHES: // convert resolution in inches to resolution in centimeters hres = (int)(10*mm2inches*hres); vres = (int)(10*mm2inches*vres); // fall through to convert it to resolution in meters case wxIMAGE_RESOLUTION_CM: // convert resolution in centimeters to resolution in meters hres *= 100; vres *= 100; break; } hdr.h_res = wxUINT32_SWAP_ON_BE(hres); hdr.v_res = wxUINT32_SWAP_ON_BE(vres); hdr.num_clrs = wxUINT32_SWAP_ON_BE(palette_size); // # colors in colormap hdr.num_signif_clrs = 0; // all colors are significant if ( IsBmp ) { if (// VS: looks ugly but compilers tend to do ugly things with structs, // like aligning hdr.filesize's ofset to dword :( // VZ: we should add padding then... !stream.Write(&hdr.magic, 2) || !stream.Write(&hdr.filesize, 4) || !stream.Write(&hdr.reserved, 4) || !stream.Write(&hdr.data_offset, 4) ) { if (verbose) { wxLogError(_("BMP: Couldn't write the file (Bitmap) header.")); } return false; } } if ( !IsMask ) { if ( !stream.Write(&hdr.bih_size, 4) || !stream.Write(&hdr.width, 4) || !stream.Write(&hdr.height, 4) || !stream.Write(&hdr.planes, 2) || !stream.Write(&hdr.bpp, 2) || !stream.Write(&hdr.compression, 4) || !stream.Write(&hdr.size_of_bmp, 4) || !stream.Write(&hdr.h_res, 4) || !stream.Write(&hdr.v_res, 4) || !stream.Write(&hdr.num_clrs, 4) || !stream.Write(&hdr.num_signif_clrs, 4) ) { if (verbose) { wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header.")); } return false; } } wxPalette *palette = NULL; // entries for quantized images wxUint8 *rgbquad = NULL; // for the RGBQUAD bytes for the colormap wxImage *q_image = NULL; // destination for quantized image // if <24bpp use quantization to reduce colors for *some* of the formats if ( (format == wxBMP_1BPP) || (format == wxBMP_4BPP) || (format == wxBMP_8BPP) || (format == wxBMP_8BPP_PALETTE) ) { // make a new palette and quantize the image if (format != wxBMP_8BPP_PALETTE) { q_image = new wxImage(); // I get a delete error using Quantize when desired colors > 236 int quantize = ((palette_size > 236) ? 236 : palette_size); // fill the destination too, it gives much nicer 4bpp images wxQuantize::Quantize( *image, *q_image, &palette, quantize, 0, wxQUANTIZE_FILL_DESTINATION_IMAGE ); } else { #if wxUSE_PALETTE palette = new wxPalette(image->GetPalette()); #endif // wxUSE_PALETTE } int i; unsigned char r, g, b; rgbquad = new wxUint8 [palette_size*4]; for (i = 0; i < palette_size; i++) { #if wxUSE_PALETTE if ( !palette->GetRGB(i, &r, &g, &b) ) #endif // wxUSE_PALETTE r = g = b = 0; rgbquad[i*4] = b; rgbquad[i*4+1] = g; rgbquad[i*4+2] = r; rgbquad[i*4+3] = 0; } } // make a 256 entry greyscale colormap or 2 entry black & white else if ( (format == wxBMP_8BPP_GREY) || (format == wxBMP_8BPP_RED) || (format == wxBMP_1BPP_BW) ) { rgbquad = new wxUint8 [palette_size*4]; for ( int i = 0; i < palette_size; i++ ) { // if 1BPP_BW then the value should be either 0 or 255 wxUint8 c = (wxUint8)((i > 0) && (format == wxBMP_1BPP_BW) ? 255 : i); rgbquad[i*4] = rgbquad[i*4+1] = rgbquad[i*4+2] = c; rgbquad[i*4+3] = 0; } } // if the colormap was made, then it needs to be written if (rgbquad) { if ( !IsMask ) { if ( !stream.Write(rgbquad, palette_size*4) ) { if (verbose) { wxLogError(_("BMP: Couldn't write RGB color map.")); } delete[] rgbquad; #if wxUSE_PALETTE delete palette; #endif // wxUSE_PALETTE delete q_image; return false; } } delete []rgbquad; } // pointer to the image data, use quantized if available wxUint8 *data = (wxUint8*) image->GetData(); if (q_image) if (q_image->Ok()) data = (wxUint8*) q_image->GetData(); wxUint8 *buffer = new wxUint8[row_width]; memset(buffer, 0, row_width); int y; unsigned x; long int pixel; for (y = image->GetHeight() -1; y >= 0; y--) { if ( format == wxBMP_24BPP ) // 3 bytes per pixel red,green,blue { for ( x = 0; x < width; x++ ) { pixel = 3*(y*width + x); buffer[3*x ] = data[pixel+2]; buffer[3*x + 1] = data[pixel+1]; buffer[3*x + 2] = data[pixel]; } } else if ((format == wxBMP_8BPP) || // 1 byte per pixel in color (format == wxBMP_8BPP_PALETTE)) { for (x = 0; x < width; x++) { pixel = 3*(y*width + x); #if wxUSE_PALETTE buffer[x] = (wxUint8)palette->GetPixel( data[pixel], data[pixel+1], data[pixel+2] ); #else // FIXME: what should this be? use some std palette maybe? buffer[x] = 0; #endif // wxUSE_PALETTE } } else if ( format == wxBMP_8BPP_GREY ) // 1 byte per pix, rgb ave to grey { for (x = 0; x < width; x++) { pixel = 3*(y*width + x); buffer[x] = (wxUint8)(.299*data[pixel] + .587*data[pixel+1] + .114*data[pixel+2]); } } else if ( format == wxBMP_8BPP_RED ) // 1 byte per pixel, red as greys { for (x = 0; x < width; x++) { buffer[x] = (wxUint8)data[3*(y*width + x)]; } } else if ( format == wxBMP_4BPP ) // 4 bpp in color { for (x = 0; x < width; x+=2) { pixel = 3*(y*width + x); // fill buffer, ignore if > width #if wxUSE_PALETTE buffer[x/2] = (wxUint8)( ((wxUint8)palette->GetPixel(data[pixel], data[pixel+1], data[pixel+2]) << 4) | (((x+1) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+3], data[pixel+4], data[pixel+5]) )) ); #else // FIXME: what should this be? use some std palette maybe? buffer[x/2] = 0; #endif // wxUSE_PALETTE } } else if ( format == wxBMP_1BPP ) // 1 bpp in "color" { for (x = 0; x < width; x+=8) { pixel = 3*(y*width + x); #if wxUSE_PALETTE buffer[x/8] = (wxUint8)( ((wxUint8)palette->GetPixel(data[pixel], data[pixel+1], data[pixel+2]) << 7) | (((x+1) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+3], data[pixel+4], data[pixel+5]) << 6)) | (((x+2) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+6], data[pixel+7], data[pixel+8]) << 5)) | (((x+3) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+9], data[pixel+10], data[pixel+11]) << 4)) | (((x+4) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+12], data[pixel+13], data[pixel+14]) << 3)) | (((x+5) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+15], data[pixel+16], data[pixel+17]) << 2)) | (((x+6) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+18], data[pixel+19], data[pixel+20]) << 1)) | (((x+7) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+21], data[pixel+22], data[pixel+23]) )) ); #else // FIXME: what should this be? use some std palette maybe? buffer[x/8] = 0; #endif // wxUSE_PALETTE } } else if ( format == wxBMP_1BPP_BW ) // 1 bpp B&W colormap from red color ONLY { for (x = 0; x < width; x+=8) { pixel = 3*(y*width + x); buffer[x/8] = (wxUint8)( (((wxUint8)(data[pixel] /128.)) << 7) | (((x+1) > width) ? 0 : (((wxUint8)(data[pixel+3] /128.)) << 6)) | (((x+2) > width) ? 0 : (((wxUint8)(data[pixel+6] /128.)) << 5)) | (((x+3) > width) ? 0 : (((wxUint8)(data[pixel+9] /128.)) << 4)) | (((x+4) > width) ? 0 : (((wxUint8)(data[pixel+12]/128.)) << 3)) | (((x+5) > width) ? 0 : (((wxUint8)(data[pixel+15]/128.)) << 2)) | (((x+6) > width) ? 0 : (((wxUint8)(data[pixel+18]/128.)) << 1)) | (((x+7) > width) ? 0 : (((wxUint8)(data[pixel+21]/128.)) )) ); } } if ( !stream.Write(buffer, row_width) ) { if (verbose) { wxLogError(_("BMP: Couldn't write data.")); } delete[] buffer; #if wxUSE_PALETTE delete palette; #endif // wxUSE_PALETTE delete q_image; return false; } } delete[] buffer; #if wxUSE_PALETTE delete palette; #endif // wxUSE_PALETTE delete q_image; return true; }
bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) { struct jpeg_compress_struct cinfo; wx_error_mgr jerr; JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ JSAMPLE *image_buffer; int stride; /* physical row width in image buffer */ cinfo.err = jpeg_std_error(&jerr); jerr.error_exit = wx_error_exit; if (!verbose) cinfo.err->output_message = wx_ignore_message; /* Establish the setjmp return context for wx_error_exit to use. */ if (setjmp(jerr.setjmp_buffer)) { /* If we get here, the JPEG code has signaled an error. * We need to clean up the JPEG object, close the input file, and return. */ if (verbose) { wxLogError(_("JPEG: Couldn't save image.")); } jpeg_destroy_compress(&cinfo); return false; } jpeg_create_compress(&cinfo); wx_jpeg_io_dest(&cinfo, stream); cinfo.image_width = image->GetWidth(); cinfo.image_height = image->GetHeight(); cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); // TODO: 3rd parameter is force_baseline, what value should this be? // Code says: "If force_baseline is TRUE, the computed quantization table entries // are limited to 1..255 for JPEG baseline compatibility." // 'Quality' is a number between 0 (terrible) and 100 (very good). // The default (in jcparam.c, jpeg_set_defaults) is 75, // and force_baseline is TRUE. if (image->HasOption(wxIMAGE_OPTION_QUALITY)) jpeg_set_quality(&cinfo, image->GetOptionInt(wxIMAGE_OPTION_QUALITY), TRUE); // set the resolution fields in the output file int resX, resY; wxImageResolution res = GetResolutionFromOptions(*image, &resX, &resY); if ( res != wxIMAGE_RESOLUTION_NONE ) { cinfo.X_density = resX; cinfo.Y_density = resY; // it so happens that wxIMAGE_RESOLUTION_INCHES/CM values are the same // ones as used by libjpeg, so we can assign them directly cinfo.density_unit = res; } jpeg_start_compress(&cinfo, TRUE); stride = cinfo.image_width * 3; /* JSAMPLEs per row in image_buffer */ image_buffer = image->GetData(); while (cinfo.next_scanline < cinfo.image_height) { row_pointer[0] = &image_buffer[cinfo.next_scanline * stride]; jpeg_write_scanlines( &cinfo, row_pointer, 1 ); } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); return true; }
bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) { wxPNGInfoStruct wxinfo; wxinfo.verbose = verbose; wxinfo.stream.out = &stream; png_structp png_ptr = png_create_write_struct ( PNG_LIBPNG_VER_STRING, NULL, wx_png_error, wx_png_warning ); if (!png_ptr) { if (verbose) { wxLogError(_("Couldn't save PNG image.")); } return false; } png_infop info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); if (verbose) { wxLogError(_("Couldn't save PNG image.")); } return false; } if (setjmp(wxinfo.jmpbuf)) { png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); if (verbose) { wxLogError(_("Couldn't save PNG image.")); } return false; } // NB: please see the comment near wxPNGInfoStruct declaration for // explanation why this line is mandatory png_set_write_fn( png_ptr, &wxinfo, wx_PNG_stream_writer, NULL); const int iColorType = image->HasOption(wxIMAGE_OPTION_PNG_FORMAT) ? image->GetOptionInt(wxIMAGE_OPTION_PNG_FORMAT) : wxPNG_TYPE_COLOUR; const int iBitDepth = image->HasOption(wxIMAGE_OPTION_PNG_BITDEPTH) ? image->GetOptionInt(wxIMAGE_OPTION_PNG_BITDEPTH) : 8; bool bHasAlpha = image->HasAlpha(); bool bHasMask = image->HasMask(); bool bUseAlpha = bHasAlpha || bHasMask; int iPngColorType; if ( iColorType==wxPNG_TYPE_COLOUR ) { iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB; } else { iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_GRAY_ALPHA : PNG_COLOR_TYPE_GRAY; } if (image->HasOption(wxIMAGE_OPTION_PNG_FILTER)) png_set_filter( png_ptr, PNG_FILTER_TYPE_BASE, image->GetOptionInt(wxIMAGE_OPTION_PNG_FILTER) ); if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL)) png_set_compression_level( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL) ); if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL)) png_set_compression_mem_level( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL) ); if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY)) png_set_compression_strategy( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY) ); if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE)) png_set_compression_buffer_size( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE) ); png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), iBitDepth, iPngColorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); int iElements; png_color_8 sig_bit; if ( iPngColorType & PNG_COLOR_MASK_COLOR ) { sig_bit.red = sig_bit.green = sig_bit.blue = (png_byte)iBitDepth; iElements = 3; } else // grey { sig_bit.gray = (png_byte)iBitDepth; iElements = 1; } if ( iPngColorType & PNG_COLOR_MASK_ALPHA ) { sig_bit.alpha = (png_byte)iBitDepth; iElements++; } if ( iBitDepth == 16 ) iElements *= 2; // save the image resolution if we have it int resX, resY; switch ( GetResolutionFromOptions(*image, &resX, &resY) ) { case wxIMAGE_RESOLUTION_INCHES: { const double INCHES_IN_METER = 10000.0 / 254; resX = int(resX * INCHES_IN_METER); resY = int(resY * INCHES_IN_METER); } break; case wxIMAGE_RESOLUTION_CM: resX *= 100; resY *= 100; break; case wxIMAGE_RESOLUTION_NONE: break; default: wxFAIL_MSG( wxT("unsupported image resolution units") ); } if ( resX && resY ) png_set_pHYs( png_ptr, info_ptr, resX, resY, PNG_RESOLUTION_METER ); png_set_sBIT( png_ptr, info_ptr, &sig_bit ); png_write_info( png_ptr, info_ptr ); png_set_shift( png_ptr, &sig_bit ); png_set_packing( png_ptr ); unsigned char * data = (unsigned char *)malloc( image->GetWidth() * iElements ); if ( !data ) { png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); return false; } unsigned char * pAlpha = (unsigned char *)(bHasAlpha ? image->GetAlpha() : NULL); int iHeight = image->GetHeight(); int iWidth = image->GetWidth(); unsigned char uchMaskRed = 0, uchMaskGreen = 0, uchMaskBlue = 0; if ( bHasMask ) { uchMaskRed = image->GetMaskRed(); uchMaskGreen = image->GetMaskGreen(); uchMaskBlue = image->GetMaskBlue(); } unsigned char *pColors = image->GetData(); for (int y = 0; y != iHeight; ++y) { unsigned char *pData = data; for (int x = 0; x != iWidth; x++) { unsigned char uchRed = *pColors++; unsigned char uchGreen = *pColors++; unsigned char uchBlue = *pColors++; switch ( iColorType ) { default: wxFAIL_MSG( wxT("unknown wxPNG_TYPE_XXX") ); // fall through case wxPNG_TYPE_COLOUR: *pData++ = uchRed; if ( iBitDepth == 16 ) *pData++ = 0; *pData++ = uchGreen; if ( iBitDepth == 16 ) *pData++ = 0; *pData++ = uchBlue; if ( iBitDepth == 16 ) *pData++ = 0; break; case wxPNG_TYPE_GREY: { // where do these coefficients come from? maybe we // should have image options for them as well? unsigned uiColor = (unsigned) (76.544*(unsigned)uchRed + 150.272*(unsigned)uchGreen + 36.864*(unsigned)uchBlue); *pData++ = (unsigned char)((uiColor >> 8) & 0xFF); if ( iBitDepth == 16 ) *pData++ = (unsigned char)(uiColor & 0xFF); } break; case wxPNG_TYPE_GREY_RED: *pData++ = uchRed; if ( iBitDepth == 16 ) *pData++ = 0; break; } if ( bUseAlpha ) { unsigned char uchAlpha = 255; if ( bHasAlpha ) uchAlpha = *pAlpha++; if ( bHasMask ) { if ( (uchRed == uchMaskRed) && (uchGreen == uchMaskGreen) && (uchBlue == uchMaskBlue) ) uchAlpha = 0; } *pData++ = uchAlpha; if ( iBitDepth == 16 ) *pData++ = 0; } } png_bytep row_ptr = data; png_write_rows( png_ptr, &row_ptr, 1 ); } free(data); png_write_end( png_ptr, info_ptr ); png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr ); return true; }
bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) { wxPNGInfoStruct wxinfo; wxinfo.verbose = verbose; wxinfo.stream.out = &stream; png_structp png_ptr = png_create_write_struct ( PNG_LIBPNG_VER_STRING, NULL, wx_PNG_error, wx_PNG_warning ); if (!png_ptr) { if (verbose) { wxLogError(_("Couldn't save PNG image.")); } return false; } png_infop info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); if (verbose) { wxLogError(_("Couldn't save PNG image.")); } return false; } if (setjmp(wxinfo.jmpbuf)) { png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); if (verbose) { wxLogError(_("Couldn't save PNG image.")); } return false; } // NB: please see the comment near wxPNGInfoStruct declaration for // explanation why this line is mandatory png_set_write_fn( png_ptr, &wxinfo, wx_PNG_stream_writer, NULL); const int iHeight = image->GetHeight(); const int iWidth = image->GetWidth(); const bool bHasPngFormatOption = image->HasOption(wxIMAGE_OPTION_PNG_FORMAT); int iColorType = bHasPngFormatOption ? image->GetOptionInt(wxIMAGE_OPTION_PNG_FORMAT) : wxPNG_TYPE_COLOUR; bool bHasAlpha = image->HasAlpha(); bool bHasMask = image->HasMask(); bool bUsePalette = iColorType == wxPNG_TYPE_PALETTE #if wxUSE_PALETTE || (!bHasPngFormatOption && image->HasPalette() ) #endif ; png_color_8 mask = { 0, 0, 0, 0, 0 }; if (bHasMask) { mask.red = image->GetMaskRed(); mask.green = image->GetMaskGreen(); mask.blue = image->GetMaskBlue(); } PaletteMap palette; if (bUsePalette) { png_color png_rgb [PNG_MAX_PALETTE_LENGTH]; png_byte png_trans[PNG_MAX_PALETTE_LENGTH]; const unsigned char *pColors = image->GetData(); const unsigned char* pAlpha = image->GetAlpha(); if (bHasMask && !pAlpha) { // Mask must be first PaletteAdd(&palette, mask); } for (int y = 0; y < iHeight; y++) { for (int x = 0; x < iWidth; x++) { png_color_8 rgba; rgba.red = *pColors++; rgba.green = *pColors++; rgba.blue = *pColors++; rgba.gray = 0; rgba.alpha = (pAlpha && !bHasMask) ? *pAlpha++ : 0; // save in our palette long index = PaletteAdd(&palette, rgba); if (index < PNG_MAX_PALETTE_LENGTH) { // save in libpng's palette png_rgb[index].red = rgba.red; png_rgb[index].green = rgba.green; png_rgb[index].blue = rgba.blue; png_trans[index] = rgba.alpha; } else { bUsePalette = false; break; } } } if (bUsePalette) { png_set_PLTE(png_ptr, info_ptr, png_rgb, palette.size()); if (bHasMask && !pAlpha) { wxASSERT(PaletteFind(palette, mask) == 0); png_trans[0] = 0; png_set_tRNS(png_ptr, info_ptr, png_trans, 1, NULL); } else if (pAlpha && !bHasMask) { png_set_tRNS(png_ptr, info_ptr, png_trans, palette.size(), NULL); } } } /* If saving palettised was requested but it was decided we can't use a palette then reset the colour type to RGB. */ if (!bUsePalette && iColorType == wxPNG_TYPE_PALETTE) { iColorType = wxPNG_TYPE_COLOUR; } bool bUseAlpha = !bUsePalette && (bHasAlpha || bHasMask); int iPngColorType; if (bUsePalette) { iPngColorType = PNG_COLOR_TYPE_PALETTE; iColorType = wxPNG_TYPE_PALETTE; } else if ( iColorType==wxPNG_TYPE_COLOUR ) { iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB; } else { iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_GRAY_ALPHA : PNG_COLOR_TYPE_GRAY; } if (image->HasOption(wxIMAGE_OPTION_PNG_FILTER)) png_set_filter( png_ptr, PNG_FILTER_TYPE_BASE, image->GetOptionInt(wxIMAGE_OPTION_PNG_FILTER) ); if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL)) png_set_compression_level( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL) ); if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL)) png_set_compression_mem_level( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL) ); if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY)) png_set_compression_strategy( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY) ); if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE)) png_set_compression_buffer_size( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE) ); int iBitDepth = !bUsePalette && image->HasOption(wxIMAGE_OPTION_PNG_BITDEPTH) ? image->GetOptionInt(wxIMAGE_OPTION_PNG_BITDEPTH) : 8; png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), iBitDepth, iPngColorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); int iElements; png_color_8 sig_bit; if ( iPngColorType & PNG_COLOR_MASK_COLOR ) { sig_bit.red = sig_bit.green = sig_bit.blue = (png_byte)iBitDepth; iElements = 3; } else // grey { sig_bit.gray = (png_byte)iBitDepth; iElements = 1; } if ( bUseAlpha ) { sig_bit.alpha = (png_byte)iBitDepth; iElements++; } if ( iBitDepth == 16 ) iElements *= 2; // save the image resolution if we have it int resX, resY; switch ( GetResolutionFromOptions(*image, &resX, &resY) ) { case wxIMAGE_RESOLUTION_INCHES: { const double INCHES_IN_METER = 10000.0 / 254; resX = int(resX * INCHES_IN_METER); resY = int(resY * INCHES_IN_METER); } break; case wxIMAGE_RESOLUTION_CM: resX *= 100; resY *= 100; break; case wxIMAGE_RESOLUTION_NONE: break; default: wxFAIL_MSG( wxT("unsupported image resolution units") ); } if ( resX && resY ) png_set_pHYs( png_ptr, info_ptr, resX, resY, PNG_RESOLUTION_METER ); png_set_sBIT( png_ptr, info_ptr, &sig_bit ); png_write_info( png_ptr, info_ptr ); png_set_shift( png_ptr, &sig_bit ); png_set_packing( png_ptr ); unsigned char * data = (unsigned char *)malloc( image->GetWidth() * iElements ); if ( !data ) { png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); return false; } const unsigned char * pAlpha = (const unsigned char *)(bHasAlpha ? image->GetAlpha() : NULL); const unsigned char *pColors = image->GetData(); for (int y = 0; y != iHeight; ++y) { unsigned char *pData = data; for (int x = 0; x != iWidth; x++) { png_color_8 clr; clr.red = *pColors++; clr.green = *pColors++; clr.blue = *pColors++; clr.gray = 0; clr.alpha = (bUsePalette && pAlpha) ? *pAlpha++ : 0; // use with wxPNG_TYPE_PALETTE only switch ( iColorType ) { default: wxFAIL_MSG( wxT("unknown wxPNG_TYPE_XXX") ); // fall through case wxPNG_TYPE_COLOUR: *pData++ = clr.red; if ( iBitDepth == 16 ) *pData++ = 0; *pData++ = clr.green; if ( iBitDepth == 16 ) *pData++ = 0; *pData++ = clr.blue; if ( iBitDepth == 16 ) *pData++ = 0; break; case wxPNG_TYPE_GREY: { // where do these coefficients come from? maybe we // should have image options for them as well? unsigned uiColor = (unsigned) (76.544*(unsigned)clr.red + 150.272*(unsigned)clr.green + 36.864*(unsigned)clr.blue); *pData++ = (unsigned char)((uiColor >> 8) & 0xFF); if ( iBitDepth == 16 ) *pData++ = (unsigned char)(uiColor & 0xFF); } break; case wxPNG_TYPE_GREY_RED: *pData++ = clr.red; if ( iBitDepth == 16 ) *pData++ = 0; break; case wxPNG_TYPE_PALETTE: *pData++ = (unsigned char) PaletteFind(palette, clr); break; } if ( bUseAlpha ) { unsigned char uchAlpha = 255; if ( bHasAlpha ) uchAlpha = *pAlpha++; if ( bHasMask ) { if ( (clr.red == mask.red) && (clr.green == mask.green) && (clr.blue == mask.blue) ) uchAlpha = 0; } *pData++ = uchAlpha; if ( iBitDepth == 16 ) *pData++ = 0; } } png_bytep row_ptr = data; png_write_rows( png_ptr, &row_ptr, 1 ); } free(data); png_write_end( png_ptr, info_ptr ); png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr ); return true; }
bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) { TIFF *tif = TIFFwxOpen( stream, "image", "w" ); if (!tif) { if (verbose) { wxLogError( _("TIFF: Error saving image.") ); } return false; } TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)image->GetWidth()); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight()); TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); // save the image resolution if we have it int xres, yres; const wxImageResolution res = GetResolutionFromOptions(*image, &xres, &yres); uint16 tiffRes; switch ( res ) { default: wxFAIL_MSG( wxT("unknown image resolution units") ); // fall through case wxIMAGE_RESOLUTION_NONE: tiffRes = RESUNIT_NONE; break; case wxIMAGE_RESOLUTION_INCHES: tiffRes = RESUNIT_INCH; break; case wxIMAGE_RESOLUTION_CM: tiffRes = RESUNIT_CENTIMETER; break; } if ( tiffRes != RESUNIT_NONE ) { TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, tiffRes); TIFFSetField(tif, TIFFTAG_XRESOLUTION, (float)xres); TIFFSetField(tif, TIFFTAG_YRESOLUTION, (float)yres); } int spp = image->GetOptionInt(wxIMAGE_OPTION_SAMPLESPERPIXEL); if ( !spp ) spp = 3; int bpp = image->GetOptionInt(wxIMAGE_OPTION_BITSPERSAMPLE); if ( !bpp ) bpp = 8; int compression = image->GetOptionInt(wxIMAGE_OPTION_COMPRESSION); if ( !compression ) { // we can't use COMPRESSION_LZW because current version of libtiff // doesn't implement it ("no longer implemented due to Unisys patent // enforcement") and other compression methods are lossy so we // shouldn't use them by default -- and the only remaining one is none compression = COMPRESSION_NONE; } TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bpp); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, spp*bpp == 1 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB); TIFFSetField(tif, TIFFTAG_COMPRESSION, compression); // scanlinesize if determined by spp and bpp tsize_t linebytes = (tsize_t)image->GetWidth() * spp * bpp / 8; if ( (image->GetWidth() % 8 > 0) && (spp * bpp < 8) ) linebytes+=1; unsigned char *buf; if (TIFFScanlineSize(tif) > linebytes || (spp * bpp < 24)) { buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif)); if (!buf) { if (verbose) { wxLogError( _("TIFF: Couldn't allocate memory.") ); } TIFFClose( tif ); return false; } } else { buf = NULL; } TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,TIFFDefaultStripSize(tif, (uint32) -1)); unsigned char *ptr = image->GetData(); for ( int row = 0; row < image->GetHeight(); row++ ) { if ( buf ) { if ( spp * bpp > 1 ) { // color image memcpy(buf, ptr, image->GetWidth()); } else // black and white image { for ( int column = 0; column < linebytes; column++ ) { uint8 reverse = 0; for ( int bp = 0; bp < 8; bp++ ) { if ( ptr[column*24 + bp*3] > 0 ) { // check only red as this is sufficient reverse = (uint8)(reverse | 128 >> bp); } } buf[column] = reverse; } } } if ( TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0 ) { if (verbose) { wxLogError( _("TIFF: Error writing image.") ); } TIFFClose( tif ); if (buf) _TIFFfree(buf); return false; } ptr += image->GetWidth()*3; }