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"
19 #if wxUSE_IMAGE && wxUSE_LIBTIFF
21 #include "wx/imagtiff.h"
22 #include "wx/bitmap.h"
31 #include "wx/filefn.h"
32 #include "wx/wfstream.h"
34 #include "wx/module.h"
36 #ifndef TIFFLINKAGEMODE
37 #if defined(__WATCOMC__) && defined(__WXMGL__)
38 #define TIFFLINKAGEMODE cdecl
40 #define TIFFLINKAGEMODE LINKAGEMODE
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler
,wxImageHandler
)
52 // helper to translate our, possibly 64 bit, wxFileOffset to TIFF, always 32
54 static toff_t
wxFileOffsetToTIFF(wxFileOffset ofs
)
56 if ( ofs
== wxInvalidOffset
)
59 toff_t tofs
= wx_truncate_cast(toff_t
, ofs
);
60 wxCHECK_MSG( (wxFileOffset
)tofs
== ofs
, (toff_t
)-1,
61 _T("TIFF library doesn't support large files") );
66 // another helper to convert standard seek mode to our
67 static wxSeekMode
wxSeekModeFromTIFF(int whence
)
88 tsize_t TIFFLINKAGEMODE
89 _tiffNullProc(thandle_t
WXUNUSED(handle
),
90 tdata_t
WXUNUSED(buf
),
91 tsize_t
WXUNUSED(size
))
96 tsize_t TIFFLINKAGEMODE
97 _tiffReadProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
99 wxInputStream
*stream
= (wxInputStream
*) handle
;
100 stream
->Read( (void*) buf
, (size_t) size
);
101 return wx_truncate_cast(tsize_t
, stream
->LastRead());
104 tsize_t TIFFLINKAGEMODE
105 _tiffWriteProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
107 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
108 stream
->Write( (void*) buf
, (size_t) size
);
109 return wx_truncate_cast(tsize_t
, stream
->LastWrite());
112 toff_t TIFFLINKAGEMODE
113 _tiffSeekIProc(thandle_t handle
, toff_t off
, int whence
)
115 wxInputStream
*stream
= (wxInputStream
*) handle
;
117 return wxFileOffsetToTIFF(stream
->SeekI((wxFileOffset
)off
,
118 wxSeekModeFromTIFF(whence
)));
121 toff_t TIFFLINKAGEMODE
122 _tiffSeekOProc(thandle_t handle
, toff_t off
, int whence
)
124 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
126 return wxFileOffsetToTIFF(stream
->SeekO((wxFileOffset
)off
,
127 wxSeekModeFromTIFF(whence
)));
131 _tiffCloseProc(thandle_t
WXUNUSED(handle
))
136 toff_t TIFFLINKAGEMODE
137 _tiffSizeProc(thandle_t handle
)
139 wxStreamBase
*stream
= (wxStreamBase
*) handle
;
140 return (toff_t
) stream
->GetSize();
144 _tiffMapProc(thandle_t
WXUNUSED(handle
),
145 tdata_t
* WXUNUSED(pbase
),
146 toff_t
* WXUNUSED(psize
))
152 _tiffUnmapProc(thandle_t
WXUNUSED(handle
),
153 tdata_t
WXUNUSED(base
),
154 toff_t
WXUNUSED(size
))
159 TIFFwxWarningHandler(const char* module,
160 const char* WXUNUSED_IN_UNICODE(fmt
),
161 va_list WXUNUSED_IN_UNICODE(ap
))
164 wxLogWarning(_("tiff module: %s"), wxString::FromAscii(module).c_str());
166 // FIXME: this is not terrible informative but better than crashing!
168 wxLogWarning(_("TIFF library warning."));
170 wxVLogWarning(fmt
, ap
);
175 TIFFwxErrorHandler(const char* module,
176 const char* WXUNUSED_IN_UNICODE(fmt
),
177 va_list WXUNUSED_IN_UNICODE(ap
))
180 wxLogError(_("tiff module: %s"), wxString::FromAscii(module).c_str());
184 wxLogError(_("TIFF library error."));
186 wxVLogError(fmt
, ap
);
193 TIFFwxOpen(wxInputStream
&stream
, const char* name
, const char* mode
)
195 TIFF
* tif
= TIFFClientOpen(name
, mode
,
197 _tiffReadProc
, _tiffNullProc
,
198 _tiffSeekIProc
, _tiffCloseProc
, _tiffSizeProc
,
199 _tiffMapProc
, _tiffUnmapProc
);
205 TIFFwxOpen(wxOutputStream
&stream
, const char* name
, const char* mode
)
207 TIFF
* tif
= TIFFClientOpen(name
, mode
,
209 _tiffNullProc
, _tiffWriteProc
,
210 _tiffSeekOProc
, _tiffCloseProc
, _tiffSizeProc
,
211 _tiffMapProc
, _tiffUnmapProc
);
216 wxTIFFHandler::wxTIFFHandler()
218 m_name
= wxT("TIFF file");
219 m_extension
= wxT("tif");
220 m_type
= wxBITMAP_TYPE_TIF
;
221 m_mime
= wxT("image/tiff");
222 TIFFSetWarningHandler((TIFFErrorHandler
) TIFFwxWarningHandler
);
223 TIFFSetErrorHandler((TIFFErrorHandler
) TIFFwxErrorHandler
);
226 bool wxTIFFHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int index
)
233 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
238 wxLogError( _("TIFF: Error loading image.") );
243 if (!TIFFSetDirectory( tif
, (tdir_t
)index
))
246 wxLogError( _("Invalid TIFF image index.") );
257 TIFFGetField( tif
, TIFFTAG_IMAGEWIDTH
, &w
);
258 TIFFGetField( tif
, TIFFTAG_IMAGELENGTH
, &h
);
262 raster
= (uint32
*) _TIFFmalloc( npixels
* sizeof(uint32
) );
267 wxLogError( _("TIFF: Couldn't allocate memory.") );
274 image
->Create( (int)w
, (int)h
);
278 wxLogError( _("TIFF: Couldn't allocate memory.") );
286 if (!TIFFReadRGBAImage( tif
, w
, h
, raster
, 0 ))
289 wxLogError( _("TIFF: Error reading image.") );
298 bool hasmask
= false;
300 unsigned char *ptr
= image
->GetData();
304 for (uint32 i
= 0; i
< h
; i
++)
306 for (uint32 j
= 0; j
< w
; j
++)
308 unsigned char alpha
= (unsigned char)TIFFGetA(raster
[pos
]);
312 ptr
[0] = image
->GetMaskRed();
314 ptr
[0] = image
->GetMaskGreen();
316 ptr
[0] = image
->GetMaskBlue();
321 ptr
[0] = (unsigned char)TIFFGetR(raster
[pos
]);
323 ptr
[0] = (unsigned char)TIFFGetG(raster
[pos
]);
325 ptr
[0] = (unsigned char)TIFFGetB(raster
[pos
]);
330 ptr
-= 2*w
*3; // subtract line we just added plus one line
337 image
->SetMask( hasmask
);
342 int wxTIFFHandler::GetImageCount( wxInputStream
& stream
)
344 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
349 int dircount
= 0; // according to the libtiff docs, dircount should be set to 1 here???
352 } while (TIFFReadDirectory(tif
));
359 bool wxTIFFHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
361 TIFF
*tif
= TIFFwxOpen( stream
, "image", "w" );
366 wxLogError( _("TIFF: Error saving image.") );
371 TIFFSetField(tif
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
372 TIFFSetField(tif
, TIFFTAG_IMAGEWIDTH
, (uint32
)image
->GetWidth());
373 TIFFSetField(tif
, TIFFTAG_IMAGELENGTH
, (uint32
)image
->GetHeight());
374 TIFFSetField(tif
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
375 TIFFSetField(tif
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
);
377 if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
378 image
->HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
380 TIFFSetField(tif
, TIFFTAG_XRESOLUTION
,
381 (float)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
));
382 TIFFSetField(tif
, TIFFTAG_YRESOLUTION
,
383 (float)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
));
386 int spp
= image
->GetOptionInt(wxIMAGE_OPTION_SAMPLESPERPIXEL
);
390 int bpp
= image
->GetOptionInt(wxIMAGE_OPTION_BITSPERSAMPLE
);
394 int compression
= image
->GetOptionInt(wxIMAGE_OPTION_COMPRESSION
);
396 compression
=COMPRESSION_LZW
;
398 TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, spp
);
399 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, bpp
);
400 TIFFSetField(tif
, TIFFTAG_PHOTOMETRIC
, spp
*bpp
== 1 ? PHOTOMETRIC_MINISBLACK
402 TIFFSetField(tif
, TIFFTAG_COMPRESSION
, compression
);
404 // scanlinesize if determined by spp and bpp
405 tsize_t linebytes
= (tsize_t
)image
->GetWidth() * spp
* bpp
/ 8;
407 if ( (image
->GetWidth() % 8 > 0) && (spp
* bpp
< 8) )
412 if (TIFFScanlineSize(tif
) > linebytes
|| (spp
* bpp
< 24))
414 buf
= (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif
));
418 wxLogError( _("TIFF: Couldn't allocate memory.") );
430 TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
,TIFFDefaultStripSize(tif
, (uint32
) -1));
432 unsigned char *ptr
= image
->GetData();
433 for ( int row
= 0; row
< image
->GetHeight(); row
++ )
440 memcpy(buf
, ptr
, image
->GetWidth());
442 else // black and white image
444 for ( int column
= 0; column
< linebytes
; column
++ )
447 for ( int bp
= 0; bp
< 8; bp
++ )
449 if ( ptr
[column
*24 + bp
*3] > 0 )
451 // check only red as this is sufficient
452 reverse
= (uint8
)(reverse
| 128 >> bp
);
456 buf
[column
] = reverse
;
461 if ( TIFFWriteScanline(tif
, buf
? buf
: ptr
, (uint32
)row
, 0) < 0 )
464 wxLogError( _("TIFF: Error writing image.") );
473 ptr
+= image
->GetWidth()*3;
476 (void) TIFFClose(tif
);
484 bool wxTIFFHandler::DoCanRead( wxInputStream
& stream
)
486 unsigned char hdr
[2];
488 if ( !stream
.Read(&hdr
[0], WXSIZEOF(hdr
)) )
491 return (hdr
[0] == 'I' && hdr
[1] == 'I') ||
492 (hdr
[0] == 'M' && hdr
[1] == 'M');
495 #endif // wxUSE_STREAMS
497 #endif // wxUSE_LIBTIFF