1 /////////////////////////////////////////////////////////////////////////////
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
)
51 tsize_t TIFFLINKAGEMODE
52 _tiffNullProc(thandle_t
WXUNUSED(handle
),
53 tdata_t
WXUNUSED(buf
),
54 tsize_t
WXUNUSED(size
))
59 tsize_t TIFFLINKAGEMODE
60 _tiffReadProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
62 wxInputStream
*stream
= (wxInputStream
*) handle
;
63 stream
->Read( (void*) buf
, (size_t) size
);
64 return stream
->LastRead();
67 tsize_t TIFFLINKAGEMODE
68 _tiffWriteProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
70 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
71 stream
->Write( (void*) buf
, (size_t) size
);
72 return stream
->LastWrite();
75 toff_t TIFFLINKAGEMODE
76 _tiffSeekIProc(thandle_t handle
, toff_t off
, int whence
)
78 wxInputStream
*stream
= (wxInputStream
*) handle
;
82 case SEEK_SET
: mode
= wxFromStart
; break;
83 case SEEK_CUR
: mode
= wxFromCurrent
; break;
84 case SEEK_END
: mode
= wxFromEnd
; break;
85 default: mode
= wxFromCurrent
; break;
88 return (toff_t
)stream
->SeekI( (wxFileOffset
)off
, mode
);
91 toff_t TIFFLINKAGEMODE
92 _tiffSeekOProc(thandle_t handle
, toff_t off
, int whence
)
94 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
98 case SEEK_SET
: mode
= wxFromStart
; break;
99 case SEEK_CUR
: mode
= wxFromCurrent
; break;
100 case SEEK_END
: mode
= wxFromEnd
; break;
101 default: mode
= wxFromCurrent
; break;
104 return (toff_t
)stream
->SeekO( (wxFileOffset
)off
, mode
);
108 _tiffCloseProc(thandle_t
WXUNUSED(handle
))
113 toff_t TIFFLINKAGEMODE
114 _tiffSizeProc(thandle_t handle
)
116 wxStreamBase
*stream
= (wxStreamBase
*) handle
;
117 return (toff_t
) stream
->GetSize();
121 _tiffMapProc(thandle_t
WXUNUSED(handle
),
122 tdata_t
* WXUNUSED(pbase
),
123 toff_t
* WXUNUSED(psize
))
129 _tiffUnmapProc(thandle_t
WXUNUSED(handle
),
130 tdata_t
WXUNUSED(base
),
131 toff_t
WXUNUSED(size
))
136 TIFFwxWarningHandler(const char* module,
137 const char* WXUNUSED_IN_UNICODE(fmt
),
138 va_list WXUNUSED_IN_UNICODE(ap
))
141 wxLogWarning(_("tiff module: %s"), wxString::FromAscii(module).c_str());
143 // FIXME: this is not terrible informative but better than crashing!
145 wxLogWarning(_("TIFF library warning."));
147 wxVLogWarning(fmt
, ap
);
152 TIFFwxErrorHandler(const char* module,
153 const char* WXUNUSED_IN_UNICODE(fmt
),
154 va_list WXUNUSED_IN_UNICODE(ap
))
157 wxLogError(_("tiff module: %s"), wxString::FromAscii(module).c_str());
161 wxLogError(_("TIFF library error."));
163 wxVLogError(fmt
, ap
);
170 TIFFwxOpen(wxInputStream
&stream
, const char* name
, const char* mode
)
172 TIFF
* tif
= TIFFClientOpen(name
, mode
,
174 _tiffReadProc
, _tiffNullProc
,
175 _tiffSeekIProc
, _tiffCloseProc
, _tiffSizeProc
,
176 _tiffMapProc
, _tiffUnmapProc
);
182 TIFFwxOpen(wxOutputStream
&stream
, const char* name
, const char* mode
)
184 TIFF
* tif
= TIFFClientOpen(name
, mode
,
186 _tiffNullProc
, _tiffWriteProc
,
187 _tiffSeekOProc
, _tiffCloseProc
, _tiffSizeProc
,
188 _tiffMapProc
, _tiffUnmapProc
);
193 wxTIFFHandler::wxTIFFHandler()
195 m_name
= wxT("TIFF file");
196 m_extension
= wxT("tif");
197 m_type
= wxBITMAP_TYPE_TIF
;
198 m_mime
= wxT("image/tiff");
199 TIFFSetWarningHandler((TIFFErrorHandler
) TIFFwxWarningHandler
);
200 TIFFSetErrorHandler((TIFFErrorHandler
) TIFFwxErrorHandler
);
203 bool wxTIFFHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int index
)
210 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
215 wxLogError( _("TIFF: Error loading image.") );
220 if (!TIFFSetDirectory( tif
, (tdir_t
)index
))
223 wxLogError( _("Invalid TIFF image index.") );
234 TIFFGetField( tif
, TIFFTAG_IMAGEWIDTH
, &w
);
235 TIFFGetField( tif
, TIFFTAG_IMAGELENGTH
, &h
);
239 raster
= (uint32
*) _TIFFmalloc( npixels
* sizeof(uint32
) );
244 wxLogError( _("TIFF: Couldn't allocate memory.") );
251 image
->Create( (int)w
, (int)h
);
255 wxLogError( _("TIFF: Couldn't allocate memory.") );
263 if (!TIFFReadRGBAImage( tif
, w
, h
, raster
, 0 ))
266 wxLogError( _("TIFF: Error reading image.") );
275 bool hasmask
= false;
277 unsigned char *ptr
= image
->GetData();
281 for (uint32 i
= 0; i
< h
; i
++)
283 for (uint32 j
= 0; j
< w
; j
++)
285 unsigned char alpha
= (unsigned char)TIFFGetA(raster
[pos
]);
289 ptr
[0] = image
->GetMaskRed();
291 ptr
[0] = image
->GetMaskGreen();
293 ptr
[0] = image
->GetMaskBlue();
298 ptr
[0] = (unsigned char)TIFFGetR(raster
[pos
]);
300 ptr
[0] = (unsigned char)TIFFGetG(raster
[pos
]);
302 ptr
[0] = (unsigned char)TIFFGetB(raster
[pos
]);
307 ptr
-= 2*w
*3; // subtract line we just added plus one line
314 image
->SetMask( hasmask
);
319 int wxTIFFHandler::GetImageCount( wxInputStream
& stream
)
321 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
326 int dircount
= 0; // according to the libtiff docs, dircount should be set to 1 here???
329 } while (TIFFReadDirectory(tif
));
336 bool wxTIFFHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
338 TIFF
*tif
= TIFFwxOpen( stream
, "image", "w" );
343 wxLogError( _("TIFF: Error saving image.") );
348 TIFFSetField(tif
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
349 TIFFSetField(tif
, TIFFTAG_IMAGEWIDTH
, (uint32
)image
->GetWidth());
350 TIFFSetField(tif
, TIFFTAG_IMAGELENGTH
, (uint32
)image
->GetHeight());
351 TIFFSetField(tif
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
352 TIFFSetField(tif
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
);
354 if ( image
->HasOption(wxIMAGE_OPTION_RESOLUTIONX
) &&
355 image
->HasOption(wxIMAGE_OPTION_RESOLUTIONY
) )
357 TIFFSetField(tif
, TIFFTAG_XRESOLUTION
,
358 (float)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
));
359 TIFFSetField(tif
, TIFFTAG_YRESOLUTION
,
360 (float)image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
));
363 int spp
= image
->GetOptionInt(wxIMAGE_OPTION_SAMPLESPERPIXEL
);
367 int bpp
= image
->GetOptionInt(wxIMAGE_OPTION_BITSPERSAMPLE
);
371 int compression
= image
->GetOptionInt(wxIMAGE_OPTION_COMPRESSION
);
373 compression
=COMPRESSION_LZW
;
375 TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, spp
);
376 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, bpp
);
377 TIFFSetField(tif
, TIFFTAG_PHOTOMETRIC
, spp
*bpp
== 1 ? PHOTOMETRIC_MINISBLACK
379 TIFFSetField(tif
, TIFFTAG_COMPRESSION
, compression
);
381 // scanlinesize if determined by spp and bpp
382 tsize_t linebytes
= (tsize_t
)image
->GetWidth() * spp
* bpp
/ 8;
384 if ( (image
->GetWidth() % 8 > 0) && (spp
* bpp
< 8) )
389 if (TIFFScanlineSize(tif
) > linebytes
|| (spp
* bpp
< 24))
391 buf
= (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif
));
395 wxLogError( _("TIFF: Couldn't allocate memory.") );
407 TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
,TIFFDefaultStripSize(tif
, (uint32
) -1));
409 unsigned char *ptr
= image
->GetData();
410 for ( int row
= 0; row
< image
->GetHeight(); row
++ )
417 memcpy(buf
, ptr
, image
->GetWidth());
419 else // black and white image
421 for ( int column
= 0; column
< linebytes
; column
++ )
424 for ( int bp
= 0; bp
< 8; bp
++ )
426 if ( ptr
[column
*24 + bp
*3] > 0 )
428 // check only red as this is sufficient
429 reverse
= reverse
| 128 >> bp
;
433 buf
[column
] = reverse
;
438 if ( TIFFWriteScanline(tif
, buf
? buf
: ptr
, (uint32
)row
, 0) < 0 )
441 wxLogError( _("TIFF: Error writing image.") );
450 ptr
+= image
->GetWidth()*3;
453 (void) TIFFClose(tif
);
461 bool wxTIFFHandler::DoCanRead( wxInputStream
& stream
)
463 unsigned char hdr
[2];
465 if ( !stream
.Read(&hdr
[0], WXSIZEOF(hdr
)) )
468 return (hdr
[0] == 'I' && hdr
[1] == 'I') ||
469 (hdr
[0] == 'M' && hdr
[1] == 'M');
472 #endif // wxUSE_STREAMS
474 #endif // wxUSE_LIBTIFF