+ TIFF *tif = TIFFwxOpen( stream, "image", "w" );
+
+ if (!tif)
+ {
+ if (verbose)
+ wxLogError( _("TIFF: Error saving image.") );
+
+ return FALSE;
+ }
+
+ 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;
+ unsigned char *buf;
+
+ if (TIFFScanlineSize(tif) > linebytes)
+ {
+ buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
+ if (!buf)
+ {
+ if (verbose)
+ wxLogError( _("TIFF: Couldn't allocate memory.") );
+
+ TIFFClose( tif );
+
+ return FALSE;
+ }
+ }
+ else
+ {
+ buf = NULL;
+ }
+
+ TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
+ TIFFDefaultStripSize(tif, (uint32) -1));
+
+ unsigned char *ptr = image->GetData();
+ for (int row = 0; row < image->GetHeight(); row++)
+ {
+ if (buf)
+ memcpy(buf, ptr, image->GetWidth());
+
+ if (TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0)
+ {
+ if (verbose)
+ wxLogError( _("TIFF: Error writing image.") );
+
+ TIFFClose( tif );
+ if (buf)
+ _TIFFfree(buf);
+
+ return FALSE;
+ }
+ ptr += image->GetWidth()*3;
+ }
+
+ (void) TIFFClose(tif);
+
+ if (buf)
+ _TIFFfree(buf);
+
+ return TRUE;