1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagtiff.cpp
3 // Purpose: wxImage TIFF handler
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 #if wxUSE_IMAGE && wxUSE_LIBTIFF
19 #include "wx/imagtiff.h"
25 #include "wx/bitmap.h"
26 #include "wx/module.h"
34 #include "wx/filefn.h"
35 #include "wx/wfstream.h"
37 #ifndef TIFFLINKAGEMODE
38 #if defined(__WATCOMC__) && defined(__WXMGL__)
39 #define TIFFLINKAGEMODE cdecl
41 #define TIFFLINKAGEMODE LINKAGEMODE
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler
,wxImageHandler
)
53 // helper to translate our, possibly 64 bit, wxFileOffset to TIFF, always 32
55 static toff_t
wxFileOffsetToTIFF(wxFileOffset ofs
)
57 if ( ofs
== wxInvalidOffset
)
60 toff_t tofs
= wx_truncate_cast(toff_t
, ofs
);
61 wxCHECK_MSG( (wxFileOffset
)tofs
== ofs
, (toff_t
)-1,
62 _T("TIFF library doesn't support large files") );
67 // another helper to convert standard seek mode to our
68 static wxSeekMode
wxSeekModeFromTIFF(int whence
)
89 tsize_t TIFFLINKAGEMODE
90 wxTIFFNullProc(thandle_t
WXUNUSED(handle
),
91 tdata_t
WXUNUSED(buf
),
92 tsize_t
WXUNUSED(size
))
97 tsize_t TIFFLINKAGEMODE
98 wxTIFFReadProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
100 wxInputStream
*stream
= (wxInputStream
*) handle
;
101 stream
->Read( (void*) buf
, (size_t) size
);
102 return wx_truncate_cast(tsize_t
, stream
->LastRead());
105 tsize_t TIFFLINKAGEMODE
106 wxTIFFWriteProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
108 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
109 stream
->Write( (void*) buf
, (size_t) size
);
110 return wx_truncate_cast(tsize_t
, stream
->LastWrite());
113 toff_t TIFFLINKAGEMODE
114 wxTIFFSeekIProc(thandle_t handle
, toff_t off
, int whence
)
116 wxInputStream
*stream
= (wxInputStream
*) handle
;
118 return wxFileOffsetToTIFF(stream
->SeekI((wxFileOffset
)off
,
119 wxSeekModeFromTIFF(whence
)));
122 toff_t TIFFLINKAGEMODE
123 wxTIFFSeekOProc(thandle_t handle
, toff_t off
, int whence
)
125 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
127 return wxFileOffsetToTIFF(stream
->SeekO((wxFileOffset
)off
,
128 wxSeekModeFromTIFF(whence
)));
132 wxTIFFCloseIProc(thandle_t
WXUNUSED(handle
))
134 // there is no need to close the input stream
139 wxTIFFCloseOProc(thandle_t handle
)
141 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
143 return stream
->Close() ? 0 : -1;
146 toff_t TIFFLINKAGEMODE
147 wxTIFFSizeProc(thandle_t handle
)
149 wxStreamBase
*stream
= (wxStreamBase
*) handle
;
150 return (toff_t
) stream
->GetSize();
154 wxTIFFMapProc(thandle_t
WXUNUSED(handle
),
155 tdata_t
* WXUNUSED(pbase
),
156 toff_t
* WXUNUSED(psize
))
162 wxTIFFUnmapProc(thandle_t
WXUNUSED(handle
),
163 tdata_t
WXUNUSED(base
),
164 toff_t
WXUNUSED(size
))
169 TIFFwxWarningHandler(const char* module,
170 const char* WXUNUSED_IN_UNICODE(fmt
),
171 va_list WXUNUSED_IN_UNICODE(ap
))
174 wxLogWarning(_("tiff module: %s"), wxString::FromAscii(module).c_str());
176 // FIXME: this is not terrible informative but better than crashing!
178 wxLogWarning(_("TIFF library warning."));
180 wxVLogWarning(fmt
, ap
);
185 TIFFwxErrorHandler(const char* module,
186 const char* WXUNUSED_IN_UNICODE(fmt
),
187 va_list WXUNUSED_IN_UNICODE(ap
))
190 wxLogError(_("tiff module: %s"), wxString::FromAscii(module).c_str());
194 wxLogError(_("TIFF library error."));
196 wxVLogError(fmt
, ap
);
203 TIFFwxOpen(wxInputStream
&stream
, const char* name
, const char* mode
)
205 TIFF
* tif
= TIFFClientOpen(name
, mode
,
207 wxTIFFReadProc
, wxTIFFNullProc
,
208 wxTIFFSeekIProc
, wxTIFFCloseIProc
, wxTIFFSizeProc
,
209 wxTIFFMapProc
, wxTIFFUnmapProc
);
215 TIFFwxOpen(wxOutputStream
&stream
, const char* name
, const char* mode
)
217 TIFF
* tif
= TIFFClientOpen(name
, mode
,
219 wxTIFFNullProc
, wxTIFFWriteProc
,
220 wxTIFFSeekOProc
, wxTIFFCloseOProc
, wxTIFFSizeProc
,
221 wxTIFFMapProc
, wxTIFFUnmapProc
);
226 wxTIFFHandler::wxTIFFHandler()
228 m_name
= wxT("TIFF file");
229 m_extension
= wxT("tif");
230 m_type
= wxBITMAP_TYPE_TIF
;
231 m_mime
= wxT("image/tiff");
232 TIFFSetWarningHandler((TIFFErrorHandler
) TIFFwxWarningHandler
);
233 TIFFSetErrorHandler((TIFFErrorHandler
) TIFFwxErrorHandler
);
236 bool wxTIFFHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int index
)
243 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
248 wxLogError( _("TIFF: Error loading image.") );
253 if (!TIFFSetDirectory( tif
, (tdir_t
)index
))
256 wxLogError( _("Invalid TIFF image index.") );
267 TIFFGetField( tif
, TIFFTAG_IMAGEWIDTH
, &w
);
268 TIFFGetField( tif
, TIFFTAG_IMAGELENGTH
, &h
);
272 TIFFGetFieldDefaulted(tif
, TIFFTAG_EXTRASAMPLES
,
273 &extraSamples
, &samplesInfo
);
274 const bool hasAlpha
= (extraSamples
== 1 &&
275 (samplesInfo
[0] == EXTRASAMPLE_ASSOCALPHA
||
276 samplesInfo
[0] == EXTRASAMPLE_UNASSALPHA
));
280 raster
= (uint32
*) _TIFFmalloc( npixels
* sizeof(uint32
) );
285 wxLogError( _("TIFF: Couldn't allocate memory.") );
292 image
->Create( (int)w
, (int)h
);
296 wxLogError( _("TIFF: Couldn't allocate memory.") );
307 if (!TIFFReadRGBAImage( tif
, w
, h
, raster
, 0 ))
310 wxLogError( _("TIFF: Error reading image.") );
319 unsigned char *ptr
= image
->GetData();
322 unsigned char *alpha
= hasAlpha
? image
->GetAlpha() : NULL
;
328 for (uint32 i
= 0; i
< h
; i
++)
330 for (uint32 j
= 0; j
< w
; j
++)
332 *(ptr
++) = (unsigned char)TIFFGetR(raster
[pos
]);
333 *(ptr
++) = (unsigned char)TIFFGetG(raster
[pos
]);
334 *(ptr
++) = (unsigned char)TIFFGetB(raster
[pos
]);
336 *(alpha
++) = (unsigned char)TIFFGetA(raster
[pos
]);
341 // subtract line we just added plus one line:
347 // set the image resolution if it's available
349 if ( TIFFGetField(tif
, TIFFTAG_RESOLUTIONUNIT
, &tiffRes
) )
351 wxImageResolution res
;
355 wxLogWarning(_("Unknown TIFF resolution unit %d ignored"),
360 res
= wxIMAGE_RESOLUTION_NONE
;
364 res
= wxIMAGE_RESOLUTION_INCHES
;
367 case RESUNIT_CENTIMETER
:
368 res
= wxIMAGE_RESOLUTION_CM
;
372 if ( res
!= wxIMAGE_RESOLUTION_NONE
)
375 if ( TIFFGetField(tif
, TIFFTAG_XRESOLUTION
, &xres
) )
376 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONX
, wxRound(xres
));
378 if ( TIFFGetField(tif
, TIFFTAG_YRESOLUTION
, &yres
) )
379 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONY
, wxRound(yres
));
391 int wxTIFFHandler::GetImageCount( wxInputStream
& stream
)
393 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
398 int dircount
= 0; // according to the libtiff docs, dircount should be set to 1 here???
401 } while (TIFFReadDirectory(tif
));
408 bool wxTIFFHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
410 TIFF
*tif
= TIFFwxOpen( stream
, "image", "w" );
415 wxLogError( _("TIFF: Error saving image.") );
420 TIFFSetField(tif
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
421 TIFFSetField(tif
, TIFFTAG_IMAGEWIDTH
, (uint32
)image
->GetWidth());
422 TIFFSetField(tif
, TIFFTAG_IMAGELENGTH
, (uint32
)image
->GetHeight());
423 TIFFSetField(tif
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
424 TIFFSetField(tif
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
);
426 // save the image resolution if we have it
428 const wxImageResolution res
= GetResolutionFromOptions(*image
, &xres
, &yres
);
433 wxFAIL_MSG( _T("unknown image resolution units") );
436 case wxIMAGE_RESOLUTION_NONE
:
437 tiffRes
= RESUNIT_NONE
;
440 case wxIMAGE_RESOLUTION_INCHES
:
441 tiffRes
= RESUNIT_INCH
;
444 case wxIMAGE_RESOLUTION_CM
:
445 tiffRes
= RESUNIT_CENTIMETER
;
449 if ( tiffRes
!= RESUNIT_NONE
)
451 TIFFSetField(tif
, TIFFTAG_RESOLUTIONUNIT
, tiffRes
);
452 TIFFSetField(tif
, TIFFTAG_XRESOLUTION
, (float)xres
);
453 TIFFSetField(tif
, TIFFTAG_YRESOLUTION
, (float)yres
);
457 int spp
= image
->GetOptionInt(wxIMAGE_OPTION_SAMPLESPERPIXEL
);
461 int bpp
= image
->GetOptionInt(wxIMAGE_OPTION_BITSPERSAMPLE
);
465 int compression
= image
->GetOptionInt(wxIMAGE_OPTION_COMPRESSION
);
468 // we can't use COMPRESSION_LZW because current version of libtiff
469 // doesn't implement it ("no longer implemented due to Unisys patent
470 // enforcement") and other compression methods are lossy so we
471 // shouldn't use them by default -- and the only remaining one is none
472 compression
= COMPRESSION_NONE
;
475 TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, spp
);
476 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, bpp
);
477 TIFFSetField(tif
, TIFFTAG_PHOTOMETRIC
, spp
*bpp
== 1 ? PHOTOMETRIC_MINISBLACK
479 TIFFSetField(tif
, TIFFTAG_COMPRESSION
, compression
);
481 // scanlinesize if determined by spp and bpp
482 tsize_t linebytes
= (tsize_t
)image
->GetWidth() * spp
* bpp
/ 8;
484 if ( (image
->GetWidth() % 8 > 0) && (spp
* bpp
< 8) )
489 if (TIFFScanlineSize(tif
) > linebytes
|| (spp
* bpp
< 24))
491 buf
= (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif
));
495 wxLogError( _("TIFF: Couldn't allocate memory.") );
507 TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
,TIFFDefaultStripSize(tif
, (uint32
) -1));
509 unsigned char *ptr
= image
->GetData();
510 for ( int row
= 0; row
< image
->GetHeight(); row
++ )
517 memcpy(buf
, ptr
, image
->GetWidth());
519 else // black and white image
521 for ( int column
= 0; column
< linebytes
; column
++ )
524 for ( int bp
= 0; bp
< 8; bp
++ )
526 if ( ptr
[column
*24 + bp
*3] > 0 )
528 // check only red as this is sufficient
529 reverse
= (uint8
)(reverse
| 128 >> bp
);
533 buf
[column
] = reverse
;
538 if ( TIFFWriteScanline(tif
, buf
? buf
: ptr
, (uint32
)row
, 0) < 0 )
541 wxLogError( _("TIFF: Error writing image.") );
550 ptr
+= image
->GetWidth()*3;
553 (void) TIFFClose(tif
);
561 bool wxTIFFHandler::DoCanRead( wxInputStream
& stream
)
563 unsigned char hdr
[2];
565 if ( !stream
.Read(&hdr
[0], WXSIZEOF(hdr
)) )
568 return (hdr
[0] == 'I' && hdr
[1] == 'I') ||
569 (hdr
[0] == 'M' && hdr
[1] == 'M');
572 #endif // wxUSE_STREAMS
574 #endif // wxUSE_LIBTIFF