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 #define TIFFLINKAGEMODE LINKAGEMODE
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler
,wxImageHandler
)
48 // helper to translate our, possibly 64 bit, wxFileOffset to TIFF, always 32
50 static toff_t
wxFileOffsetToTIFF(wxFileOffset ofs
)
52 if ( ofs
== wxInvalidOffset
)
55 toff_t tofs
= wx_truncate_cast(toff_t
, ofs
);
56 wxCHECK_MSG( (wxFileOffset
)tofs
== ofs
, (toff_t
)-1,
57 _T("TIFF library doesn't support large files") );
62 // another helper to convert standard seek mode to our
63 static wxSeekMode
wxSeekModeFromTIFF(int whence
)
84 tsize_t TIFFLINKAGEMODE
85 _tiffNullProc(thandle_t
WXUNUSED(handle
),
86 tdata_t
WXUNUSED(buf
),
87 tsize_t
WXUNUSED(size
))
92 tsize_t TIFFLINKAGEMODE
93 _tiffReadProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
95 wxInputStream
*stream
= (wxInputStream
*) handle
;
96 stream
->Read( (void*) buf
, (size_t) size
);
97 return stream
->LastRead();
100 tsize_t TIFFLINKAGEMODE
101 _tiffWriteProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
103 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
104 stream
->Write( (void*) buf
, (size_t) size
);
105 return stream
->LastWrite();
108 toff_t TIFFLINKAGEMODE
109 _tiffSeekIProc(thandle_t handle
, toff_t off
, int whence
)
111 wxInputStream
*stream
= (wxInputStream
*) handle
;
113 return wxFileOffsetToTIFF(stream
->SeekI((wxFileOffset
)off
,
114 wxSeekModeFromTIFF(whence
)));
117 toff_t TIFFLINKAGEMODE
118 _tiffSeekOProc(thandle_t handle
, toff_t off
, int whence
)
120 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
122 return wxFileOffsetToTIFF(stream
->SeekO((wxFileOffset
)off
,
123 wxSeekModeFromTIFF(whence
)));
127 _tiffCloseProc(thandle_t
WXUNUSED(handle
))
132 toff_t TIFFLINKAGEMODE
133 _tiffSizeProc(thandle_t handle
)
135 wxStreamBase
*stream
= (wxStreamBase
*) handle
;
136 return (toff_t
) stream
->GetSize();
140 _tiffMapProc(thandle_t
WXUNUSED(handle
),
141 tdata_t
* WXUNUSED(pbase
),
142 toff_t
* WXUNUSED(psize
))
148 _tiffUnmapProc(thandle_t
WXUNUSED(handle
),
149 tdata_t
WXUNUSED(base
),
150 toff_t
WXUNUSED(size
))
155 TIFFwxWarningHandler(const char* module,
156 const char* WXUNUSED_IN_UNICODE(fmt
),
157 va_list WXUNUSED_IN_UNICODE(ap
))
160 wxLogWarning(_("tiff module: %s"), wxString::FromAscii(module).c_str());
162 // FIXME: this is not terrible informative but better than crashing!
164 wxLogWarning(_("TIFF library warning."));
166 wxVLogWarning(fmt
, ap
);
171 TIFFwxErrorHandler(const char* module,
172 const char* WXUNUSED_IN_UNICODE(fmt
),
173 va_list WXUNUSED_IN_UNICODE(ap
))
176 wxLogError(_("tiff module: %s"), wxString::FromAscii(module).c_str());
180 wxLogError(_("TIFF library error."));
182 wxVLogError(fmt
, ap
);
189 TIFFwxOpen(wxInputStream
&stream
, const char* name
, const char* mode
)
191 TIFF
* tif
= TIFFClientOpen(name
, mode
,
193 _tiffReadProc
, _tiffNullProc
,
194 _tiffSeekIProc
, _tiffCloseProc
, _tiffSizeProc
,
195 _tiffMapProc
, _tiffUnmapProc
);
201 TIFFwxOpen(wxOutputStream
&stream
, const char* name
, const char* mode
)
203 TIFF
* tif
= TIFFClientOpen(name
, mode
,
205 _tiffNullProc
, _tiffWriteProc
,
206 _tiffSeekOProc
, _tiffCloseProc
, _tiffSizeProc
,
207 _tiffMapProc
, _tiffUnmapProc
);
212 wxTIFFHandler::wxTIFFHandler()
214 m_name
= wxT("TIFF file");
215 m_extension
= wxT("tif");
216 m_type
= wxBITMAP_TYPE_TIF
;
217 m_mime
= wxT("image/tiff");
218 TIFFSetWarningHandler((TIFFErrorHandler
) TIFFwxWarningHandler
);
219 TIFFSetErrorHandler((TIFFErrorHandler
) TIFFwxErrorHandler
);
222 bool wxTIFFHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int index
)
229 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
234 wxLogError( _("TIFF: Error loading image.") );
239 if (!TIFFSetDirectory( tif
, (tdir_t
)index
))
242 wxLogError( _("Invalid TIFF image index.") );
253 TIFFGetField( tif
, TIFFTAG_IMAGEWIDTH
, &w
);
254 TIFFGetField( tif
, TIFFTAG_IMAGELENGTH
, &h
);
258 raster
= (uint32
*) _TIFFmalloc( npixels
* sizeof(uint32
) );
263 wxLogError( _("TIFF: Couldn't allocate memory.") );
270 image
->Create( (int)w
, (int)h
);
274 wxLogError( _("TIFF: Couldn't allocate memory.") );
282 if (!TIFFReadRGBAImage( tif
, w
, h
, raster
, 0 ))
285 wxLogError( _("TIFF: Error reading image.") );
294 bool hasmask
= false;
296 unsigned char *ptr
= image
->GetData();
300 for (uint32 i
= 0; i
< h
; i
++)
302 for (uint32 j
= 0; j
< w
; j
++)
304 unsigned char alpha
= (unsigned char)TIFFGetA(raster
[pos
]);
308 ptr
[0] = image
->GetMaskRed();
310 ptr
[0] = image
->GetMaskGreen();
312 ptr
[0] = image
->GetMaskBlue();
317 ptr
[0] = (unsigned char)TIFFGetR(raster
[pos
]);
319 ptr
[0] = (unsigned char)TIFFGetG(raster
[pos
]);
321 ptr
[0] = (unsigned char)TIFFGetB(raster
[pos
]);
326 ptr
-= 2*w
*3; // subtract line we just added plus one line
333 image
->SetMask( hasmask
);
338 int wxTIFFHandler::GetImageCount( wxInputStream
& stream
)
340 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
345 int dircount
= 0; // according to the libtiff docs, dircount should be set to 1 here???
348 } while (TIFFReadDirectory(tif
));
355 bool wxTIFFHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
357 TIFF
*tif
= TIFFwxOpen( stream
, "image", "w" );
362 wxLogError( _("TIFF: Error saving image.") );
367 TIFFSetField(tif
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
368 TIFFSetField(tif
, TIFFTAG_IMAGEWIDTH
, (uint32
)image
->GetWidth());
369 TIFFSetField(tif
, TIFFTAG_IMAGELENGTH
, (uint32
)image
->GetHeight());
370 TIFFSetField(tif
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
371 TIFFSetField(tif
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
);
373 if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
374 image
->HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
376 TIFFSetField(tif
, TIFFTAG_XRESOLUTION
,
377 (float)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
));
378 TIFFSetField(tif
, TIFFTAG_YRESOLUTION
,
379 (float)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
));
382 int spp
= image
->GetOptionInt(wxIMAGE_OPTION_SAMPLESPERPIXEL
);
386 int bpp
= image
->GetOptionInt(wxIMAGE_OPTION_BITSPERSAMPLE
);
390 int compression
= image
->GetOptionInt(wxIMAGE_OPTION_COMPRESSION
);
392 compression
=COMPRESSION_LZW
;
394 TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, spp
);
395 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, bpp
);
396 TIFFSetField(tif
, TIFFTAG_PHOTOMETRIC
, spp
*bpp
== 1 ? PHOTOMETRIC_MINISBLACK
398 TIFFSetField(tif
, TIFFTAG_COMPRESSION
, compression
);
400 // scanlinesize if determined by spp and bpp
401 tsize_t linebytes
= (tsize_t
)image
->GetWidth() * spp
* bpp
/ 8;
403 if ( (image
->GetWidth() % 8 > 0) && (spp
* bpp
< 8) )
408 if (TIFFScanlineSize(tif
) > linebytes
|| (spp
* bpp
< 24))
410 buf
= (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif
));
414 wxLogError( _("TIFF: Couldn't allocate memory.") );
426 TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
,TIFFDefaultStripSize(tif
, (uint32
) -1));
428 unsigned char *ptr
= image
->GetData();
429 for ( int row
= 0; row
< image
->GetHeight(); row
++ )
436 memcpy(buf
, ptr
, image
->GetWidth());
438 else // black and white image
440 for ( int column
= 0; column
< linebytes
; column
++ )
443 for ( int bp
= 0; bp
< 8; bp
++ )
445 if ( ptr
[column
*24 + bp
*3] > 0 )
447 // check only red as this is sufficient
448 reverse
= (uint8
)(reverse
| 128 >> bp
);
452 buf
[column
] = reverse
;
457 if ( TIFFWriteScanline(tif
, buf
? buf
: ptr
, (uint32
)row
, 0) < 0 )
460 wxLogError( _("TIFF: Error writing image.") );
469 ptr
+= image
->GetWidth()*3;
472 (void) TIFFClose(tif
);
480 bool wxTIFFHandler::DoCanRead( wxInputStream
& stream
)
482 unsigned char hdr
[2];
484 if ( !stream
.Read(&hdr
[0], WXSIZEOF(hdr
)) )
487 return (hdr
[0] == 'I' && hdr
[1] == 'I') ||
488 (hdr
[0] == 'M' && hdr
[1] == 'M');
491 #endif // wxUSE_STREAMS
493 #endif // wxUSE_LIBTIFF