]>
git.saurik.com Git - wxWidgets.git/blob - src/common/imagtiff.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage TIFF handler
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "imagtiff.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
23 #if wxUSE_IMAGE && wxUSE_LIBTIFF
25 #include "wx/imagtiff.h"
26 #include "wx/bitmap.h"
35 #include "wx/filefn.h"
36 #include "wx/wfstream.h"
38 #include "wx/module.h"
40 #ifndef TIFFLINKAGEMODE
41 #define TIFFLINKAGEMODE LINKAGEMODE
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler
,wxImageHandler
)
55 tsize_t TIFFLINKAGEMODE
56 _tiffNullProc(thandle_t
WXUNUSED(handle
),
57 tdata_t
WXUNUSED(buf
),
58 tsize_t
WXUNUSED(size
))
63 tsize_t TIFFLINKAGEMODE
64 _tiffReadProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
66 wxInputStream
*stream
= (wxInputStream
*) handle
;
67 stream
->Read( (void*) buf
, (size_t) size
);
68 return stream
->LastRead();
71 tsize_t TIFFLINKAGEMODE
72 _tiffWriteProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
74 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
75 stream
->Write( (void*) buf
, (size_t) size
);
76 return stream
->LastWrite();
79 toff_t TIFFLINKAGEMODE
80 _tiffSeekIProc(thandle_t handle
, toff_t off
, int whence
)
82 wxInputStream
*stream
= (wxInputStream
*) handle
;
86 case SEEK_SET
: mode
= wxFromStart
; break;
87 case SEEK_CUR
: mode
= wxFromCurrent
; break;
88 case SEEK_END
: mode
= wxFromEnd
; break;
89 default: mode
= wxFromCurrent
; break;
92 return (toff_t
)stream
->SeekI( (off_t
)off
, mode
);
95 toff_t TIFFLINKAGEMODE
96 _tiffSeekOProc(thandle_t handle
, toff_t off
, int whence
)
98 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
102 case SEEK_SET
: mode
= wxFromStart
; break;
103 case SEEK_CUR
: mode
= wxFromCurrent
; break;
104 case SEEK_END
: mode
= wxFromEnd
; break;
105 default: mode
= wxFromCurrent
; break;
108 return (toff_t
)stream
->SeekO( (off_t
)off
, mode
);
112 _tiffCloseProc(thandle_t
WXUNUSED(handle
))
117 toff_t TIFFLINKAGEMODE
118 _tiffSizeProc(thandle_t handle
)
120 wxStreamBase
*stream
= (wxStreamBase
*) handle
;
121 return (toff_t
) stream
->GetSize();
125 _tiffMapProc(thandle_t
WXUNUSED(handle
),
126 tdata_t
* WXUNUSED(pbase
),
127 toff_t
* WXUNUSED(psize
))
133 _tiffUnmapProc(thandle_t
WXUNUSED(handle
),
134 tdata_t
WXUNUSED(base
),
135 toff_t
WXUNUSED(size
))
142 TIFFwxOpen(wxInputStream
&stream
, const char* name
, const char* mode
)
144 TIFF
* tif
= TIFFClientOpen(name
, mode
,
146 _tiffReadProc
, _tiffNullProc
,
147 _tiffSeekIProc
, _tiffCloseProc
, _tiffSizeProc
,
148 _tiffMapProc
, _tiffUnmapProc
);
154 TIFFwxOpen(wxOutputStream
&stream
, const char* name
, const char* mode
)
156 TIFF
* tif
= TIFFClientOpen(name
, mode
,
158 _tiffNullProc
, _tiffWriteProc
,
159 _tiffSeekOProc
, _tiffCloseProc
, _tiffSizeProc
,
160 _tiffMapProc
, _tiffUnmapProc
);
165 bool wxTIFFHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int index
)
172 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
177 wxLogError( _("TIFF: Error loading image.") );
182 if (!TIFFSetDirectory( tif
, (tdir_t
)index
))
185 wxLogError( _("Invalid TIFF image index.") );
196 TIFFGetField( tif
, TIFFTAG_IMAGEWIDTH
, &w
);
197 TIFFGetField( tif
, TIFFTAG_IMAGELENGTH
, &h
);
201 raster
= (uint32
*) _TIFFmalloc( npixels
* sizeof(uint32
) );
206 wxLogError( _("TIFF: Couldn't allocate memory.") );
213 image
->Create( (int)w
, (int)h
);
217 wxLogError( _("TIFF: Couldn't allocate memory.") );
225 if (!TIFFReadRGBAImage( tif
, w
, h
, raster
, 0 ))
228 wxLogError( _("TIFF: Error reading image.") );
237 bool hasmask
= FALSE
;
239 unsigned char *ptr
= image
->GetData();
243 for (uint32 i
= 0; i
< h
; i
++)
245 for (uint32 j
= 0; j
< w
; j
++)
247 unsigned char alpha
= (unsigned char)TIFFGetA(raster
[pos
]);
251 ptr
[0] = image
->GetMaskRed();
253 ptr
[0] = image
->GetMaskGreen();
255 ptr
[0] = image
->GetMaskBlue();
260 ptr
[0] = (unsigned char)TIFFGetR(raster
[pos
]);
262 ptr
[0] = (unsigned char)TIFFGetG(raster
[pos
]);
264 ptr
[0] = (unsigned char)TIFFGetB(raster
[pos
]);
269 ptr
-= 2*w
*3; // subtract line we just added plus one line
276 image
->SetMask( hasmask
);
281 int wxTIFFHandler::GetImageCount( wxInputStream
& stream
)
283 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
288 int dircount
= 0; // according to the libtiff docs, dircount should be set to 1 here???
291 } while (TIFFReadDirectory(tif
));
298 bool wxTIFFHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
300 TIFF
*tif
= TIFFwxOpen( stream
, "image", "w" );
305 wxLogError( _("TIFF: Error saving image.") );
310 TIFFSetField(tif
, TIFFTAG_IMAGEWIDTH
, (uint32
)image
->GetWidth());
311 TIFFSetField(tif
, TIFFTAG_IMAGELENGTH
, (uint32
)image
->GetHeight());
312 TIFFSetField(tif
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
313 TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, 3);
314 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, 8);
315 TIFFSetField(tif
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
);
316 TIFFSetField(tif
, TIFFTAG_PHOTOMETRIC
, PHOTOMETRIC_RGB
);
317 TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_LZW
);
319 tsize_t linebytes
= (tsize_t
)image
->GetWidth() * 3;
322 if (TIFFScanlineSize(tif
) > linebytes
)
324 buf
= (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif
));
328 wxLogError( _("TIFF: Couldn't allocate memory.") );
340 TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
,
341 TIFFDefaultStripSize(tif
, (uint32
) -1));
343 unsigned char *ptr
= image
->GetData();
344 for (int row
= 0; row
< image
->GetHeight(); row
++)
347 memcpy(buf
, ptr
, image
->GetWidth());
349 if (TIFFWriteScanline(tif
, buf
? buf
: ptr
, (uint32
)row
, 0) < 0)
352 wxLogError( _("TIFF: Error writing image.") );
360 ptr
+= image
->GetWidth()*3;
363 (void) TIFFClose(tif
);
371 bool wxTIFFHandler::DoCanRead( wxInputStream
& stream
)
373 unsigned char hdr
[2];
375 if ( !stream
.Read(&hdr
, WXSIZEOF(hdr
)) )
378 return (hdr
[0] == 'I' && hdr
[1] == 'I') ||
379 (hdr
[0] == 'M' && hdr
[1] == 'M');
382 #endif // wxUSE_STREAMS
384 #endif // wxUSE_LIBTIFF