+ // 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 <= 2)
+ {
+ photometric = PHOTOMETRIC_MINISWHITE;
+ }
+
+ const bool hasAlpha = image->HasAlpha();
+
+ int compression = image->GetOptionInt(wxIMAGE_OPTION_TIFF_COMPRESSION);
+ if ( !compression || (compression == COMPRESSION_JPEG && hasAlpha) )
+ {
+ // 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.
+ // Also JPEG compression for alpha images is not a good idea (viewers
+ // not opening the image properly).
+ compression = COMPRESSION_NONE;
+ }
+
+ if
+ (
+ (photometric == PHOTOMETRIC_RGB && spp == 4)
+ || (photometric <= PHOTOMETRIC_MINISBLACK && spp == 2)
+ )
+ {
+ // Compensate for user passing a SamplesPerPixel that includes
+ // the alpha channel.
+ spp--;
+ }
+
+
+ int extraSamples = hasAlpha ? 1 : 0;
+
+ TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp + extraSamples);
+ TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
+ TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
+ TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
+
+ if (extraSamples)
+ {
+ uint16 extra[] = { EXTRASAMPLE_UNSPECIFIED };
+ TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, (long) 1, &extra);
+ }
+
+ // scanlinesize is determined by spp+extraSamples and bps
+ const tsize_t linebytes =
+ (tsize_t)((imageWidth * (spp + extraSamples) * bps + 7) / 8);
+