+ // 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_TIFF_SAMPLESPERPIXEL);
+ if ( !spp )
+ spp = 3;
+
+ int bps = image->GetOptionInt(wxIMAGE_OPTION_TIFF_BITSPERSAMPLE);
+ if ( !bps )
+ {
+ bps = 8;
+ }
+ else if (bps == 1)
+ {
+ // One bit per sample combined with 3 samples per pixel is
+ // not allowed and crashes libtiff.
+ spp = 1;
+ }
+
+ int photometric = PHOTOMETRIC_RGB;
+
+ if ( image->HasOption(wxIMAGE_OPTION_TIFF_PHOTOMETRIC) )
+ {
+ photometric = image->GetOptionInt(wxIMAGE_OPTION_TIFF_PHOTOMETRIC);
+ if (photometric == PHOTOMETRIC_MINISWHITE
+ || photometric == PHOTOMETRIC_MINISBLACK)
+ {
+ // either b/w or greyscale
+ spp = 1;
+ }
+ }
+ else if (spp == 1)
+ {
+ photometric = PHOTOMETRIC_MINISWHITE;
+ }
+
+ const bool isColouredImage = (spp > 1)
+ && (photometric != PHOTOMETRIC_MINISWHITE)
+ && (photometric != PHOTOMETRIC_MINISBLACK);
+
+ int compression = image->GetOptionInt(wxIMAGE_OPTION_TIFF_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, bps);
+ TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
+ TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
+
+ // scanlinesize if determined by spp and bps
+ tsize_t linebytes = (tsize_t)image->GetWidth() * spp * bps / 8;
+
+ if ( (image->GetWidth() % 8 > 0) && (spp * bps < 8) )
+ linebytes+=1;
+