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);
}
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)
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.") );
return false;
}
+
ptr += image->GetWidth()*3;
}
(void) TIFFClose(tif);
if (buf)
- _TIFFfree(buf);
+ _TIFFfree(buf);
return true;
}