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
This commit is contained in:
Vadim Zeitlin
2005-02-19 16:32:29 +00:00
parent a4622f29d8
commit fe9308c639
5 changed files with 119 additions and 31 deletions

View File

@@ -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
//-----------------------------------------------------------------------------

View File

@@ -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_

View File

@@ -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

View File

@@ -359,11 +359,30 @@ 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

View File

@@ -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,14 +392,47 @@ 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++ )
{
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 )
{
@@ -383,6 +445,7 @@ bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbo
return false;
}
ptr += image->GetWidth()*3;
}