From fe9308c639e9738796028b56c40c58f2e8a648de Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 19 Feb 2005 16:32:29 +0000 Subject: [PATCH] more options for JPEG and TIFF handlers (slightly modified patch 1044470) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@32189 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/image.h | 13 ++++++ include/wx/imagjpeg.h | 14 +------ include/wx/imagtiff.h | 5 +++ src/common/imagjpeg.cpp | 29 +++++++++++--- src/common/imagtiff.cpp | 89 +++++++++++++++++++++++++++++++++++------ 5 files changed, 119 insertions(+), 31 deletions(-) diff --git a/include/wx/image.h b/include/wx/image.h index 6fb884955b..7a9e084fb2 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -32,6 +32,19 @@ #define wxIMAGE_OPTION_FILENAME wxString(_T("FileName")) +#define wxIMAGE_OPTION_RESOLUTION wxString(_T("Resolution")) +#define wxIMAGE_OPTION_RESOLUTIONX wxString(_T("ResolutionX")) +#define wxIMAGE_OPTION_RESOLUTIONY wxString(_T("ResolutionY")) + +#define wxIMAGE_OPTION_RESOLUTIONUNIT wxString(_T("ResolutionUnit")) + +// constants used with wxIMAGE_OPTION_RESOLUTIONUNIT +enum +{ + wxIMAGE_RESOLUTION_INCHES = 1, + wxIMAGE_RESOLUTION_CM = 2 +}; + //----------------------------------------------------------------------------- // classes //----------------------------------------------------------------------------- diff --git a/include/wx/imagjpeg.h b/include/wx/imagjpeg.h index bf7594fae1..e51491e8f7 100644 --- a/include/wx/imagjpeg.h +++ b/include/wx/imagjpeg.h @@ -16,15 +16,6 @@ #include "wx/image.h" -// defines for wxImage::SetOption -#define wxIMAGE_OPTION_RESOLUTION wxString(_T("Resolution")) -#define wxIMAGE_OPTION_RESOLUTIONUNIT wxString(_T("ResolutionUnit")) - -enum -{ - wxIMAGE_RESOLUTION_INCHES = 1, - wxIMAGE_RESOLUTION_CM = 2 -}; //----------------------------------------------------------------------------- // wxJPEGHandler @@ -53,8 +44,5 @@ private: }; #endif - - -#endif - // _WX_IMAGJPEG_H_ +#endif // _WX_IMAGJPEG_H_ diff --git a/include/wx/imagtiff.h b/include/wx/imagtiff.h index 4607162bf0..44e8c6fa64 100644 --- a/include/wx/imagtiff.h +++ b/include/wx/imagtiff.h @@ -16,6 +16,11 @@ #include "wx/image.h" +// defines for wxImage::SetOption +#define wxIMAGE_OPTION_BITSPERSAMPLE wxString(_T("BitsPerSample")) +#define wxIMAGE_OPTION_SAMPLESPERPIXEL wxString(_T("SamplesPerPixel")) +#define wxIMAGE_OPTION_COMPRESSION wxString(_T("Compression")) +#define wxIMAGE_OPTION_IMAGEDESCRIPTOR wxString(_T("ImageDescriptor")) //----------------------------------------------------------------------------- // wxTIFFHandler diff --git a/src/common/imagjpeg.cpp b/src/common/imagjpeg.cpp index d7cd54a73c..42b995aae9 100644 --- a/src/common/imagjpeg.cpp +++ b/src/common/imagjpeg.cpp @@ -359,17 +359,36 @@ bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbo if (image->HasOption(wxT("quality"))) jpeg_set_quality(&cinfo, image->GetOptionInt(wxT("quality")), TRUE); - // sets the resolution fields in the output file - if (image->HasOption(wxIMAGE_OPTION_RESOLUTION)) + // set the resolution fields in the output file + UINT16 resX, + resY; + if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) && + image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) ) { - cinfo.X_density = - cinfo.Y_density = (UINT16)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION); + resX = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX); + resY = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY); + } + else if ( image->HasOption(wxIMAGE_OPTION_RESOLUTION) ) + { + resX = + resY = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION); + } + else + { + resX = + resY = 0; + } + + if ( resX && resY ) + { + cinfo.X_density = resX; + cinfo.Y_density = resY; } // sets the resolution unit field in the output file // wxIMAGE_RESOLUTION_INCHES for inches // wxIMAGE_RESOLUTION_CM for centimeters - if (image->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT)) + if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONUNIT) ) { cinfo.density_unit = (UINT8)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT); } diff --git a/src/common/imagtiff.cpp b/src/common/imagtiff.cpp index 3cd681f757..63e7b60c78 100644 --- a/src/common/imagtiff.cpp +++ b/src/common/imagtiff.cpp @@ -333,19 +333,48 @@ bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbo 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_SAMPLESPERPIXEL, 3); - TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); - TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); - TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW); - tsize_t linebytes = (tsize_t)image->GetWidth() * 3; + if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) && + image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) ) + { + TIFFSetField(tif, TIFFTAG_XRESOLUTION, + image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX)); + TIFFSetField(tif, TIFFTAG_YRESOLUTION, + image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY)); + } + + 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 ) + compression=COMPRESSION_LZW; + + 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) + if (TIFFScanlineSize(tif) > linebytes || (spp * bpp < 24)) { buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif)); if (!buf) @@ -363,16 +392,49 @@ bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbo buf = NULL; } - TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, - TIFFDefaultStripSize(tif, (uint32) -1)); + TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,TIFFDefaultStripSize(tif, (uint32) -1)); + + uint8 bitmask; unsigned char *ptr = image->GetData(); - for (int row = 0; row < image->GetHeight(); row++) + for ( int row = 0; row < image->GetHeight(); row++ ) { - if (buf) - memcpy(buf, ptr, image->GetWidth()); + 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; + bitmask = 1; + for ( int bp = 0; bp < 8; bp++ ) + { + if ( ptr[column*24 + bp*3] > 0 ) + { + // check only red as this is sufficient + reverse = reverse | 128 >> bp; + } + + bitmask <<= 1; + } + + // FIXME: what's this?? +#ifdef __WXGTK__ + buf[column]=~reverse; + +#else + buf[column]=reverse; +#endif + } + } + } - if (TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0) + if ( TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0 ) { if (verbose) wxLogError( _("TIFF: Error writing image.") ); @@ -383,13 +445,14 @@ bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbo return false; } + ptr += image->GetWidth()*3; } (void) TIFFClose(tif); if (buf) - _TIFFfree(buf); + _TIFFfree(buf); return true; } -- 2.47.2