]>
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
)
53 tsize_t TIFFLINKAGEMODE
54 _tiffNullProc(thandle_t
WXUNUSED(handle
),
55 tdata_t
WXUNUSED(buf
),
56 tsize_t
WXUNUSED(size
))
61 tsize_t TIFFLINKAGEMODE
62 _tiffReadProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
64 wxInputStream
*stream
= (wxInputStream
*) handle
;
65 stream
->Read( (void*) buf
, (size_t) size
);
66 return stream
->LastRead();
69 tsize_t TIFFLINKAGEMODE
70 _tiffWriteProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
72 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
73 stream
->Write( (void*) buf
, (size_t) size
);
74 return stream
->LastWrite();
77 toff_t TIFFLINKAGEMODE
78 _tiffSeekIProc(thandle_t handle
, toff_t off
, int whence
)
80 wxInputStream
*stream
= (wxInputStream
*) handle
;
84 case SEEK_SET
: mode
= wxFromStart
; break;
85 case SEEK_CUR
: mode
= wxFromCurrent
; break;
86 case SEEK_END
: mode
= wxFromEnd
; break;
87 default: mode
= wxFromCurrent
; break;
90 return (toff_t
)stream
->SeekI( (off_t
)off
, mode
);
93 toff_t TIFFLINKAGEMODE
94 _tiffSeekOProc(thandle_t handle
, toff_t off
, int whence
)
96 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
100 case SEEK_SET
: mode
= wxFromStart
; break;
101 case SEEK_CUR
: mode
= wxFromCurrent
; break;
102 case SEEK_END
: mode
= wxFromEnd
; break;
103 default: mode
= wxFromCurrent
; break;
106 return (toff_t
)stream
->SeekO( (off_t
)off
, mode
);
110 _tiffCloseProc(thandle_t
WXUNUSED(handle
))
115 toff_t TIFFLINKAGEMODE
116 _tiffSizeProc(thandle_t handle
)
118 wxStreamBase
*stream
= (wxStreamBase
*) handle
;
119 return (toff_t
) stream
->GetSize();
123 _tiffMapProc(thandle_t
WXUNUSED(handle
),
124 tdata_t
* WXUNUSED(pbase
),
125 toff_t
* WXUNUSED(psize
))
131 _tiffUnmapProc(thandle_t
WXUNUSED(handle
),
132 tdata_t
WXUNUSED(base
),
133 toff_t
WXUNUSED(size
))
140 TIFFwxOpen(wxInputStream
&stream
, const char* name
, const char* mode
)
142 TIFF
* tif
= TIFFClientOpen(name
, mode
,
144 _tiffReadProc
, _tiffNullProc
,
145 _tiffSeekIProc
, _tiffCloseProc
, _tiffSizeProc
,
146 _tiffMapProc
, _tiffUnmapProc
);
152 TIFFwxOpen(wxOutputStream
&stream
, const char* name
, const char* mode
)
154 TIFF
* tif
= TIFFClientOpen(name
, mode
,
156 _tiffNullProc
, _tiffWriteProc
,
157 _tiffSeekOProc
, _tiffCloseProc
, _tiffSizeProc
,
158 _tiffMapProc
, _tiffUnmapProc
);
163 bool wxTIFFHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int index
)
170 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
175 wxLogError( _("TIFF: Error loading image.") );
180 if (!TIFFSetDirectory( tif
, (tdir_t
)index
))
183 wxLogError( _("Invalid TIFF image index.") );
194 TIFFGetField( tif
, TIFFTAG_IMAGEWIDTH
, &w
);
195 TIFFGetField( tif
, TIFFTAG_IMAGELENGTH
, &h
);
199 raster
= (uint32
*) _TIFFmalloc( npixels
* sizeof(uint32
) );
204 wxLogError( _("TIFF: Couldn't allocate memory.") );
211 image
->Create( (int)w
, (int)h
);
215 wxLogError( _("TIFF: Couldn't allocate memory.") );
223 if (!TIFFReadRGBAImage( tif
, w
, h
, raster
, 0 ))
226 wxLogError( _("TIFF: Error reading image.") );
235 bool hasmask
= FALSE
;
237 unsigned char *ptr
= image
->GetData();
241 for (uint32 i
= 0; i
< h
; i
++)
243 for (uint32 j
= 0; j
< w
; j
++)
245 unsigned char alpha
= (unsigned char)TIFFGetA(raster
[pos
]);
249 ptr
[0] = image
->GetMaskRed();
251 ptr
[0] = image
->GetMaskGreen();
253 ptr
[0] = image
->GetMaskBlue();
258 ptr
[0] = (unsigned char)TIFFGetR(raster
[pos
]);
260 ptr
[0] = (unsigned char)TIFFGetG(raster
[pos
]);
262 ptr
[0] = (unsigned char)TIFFGetB(raster
[pos
]);
267 ptr
-= 2*w
*3; // subtract line we just added plus one line
274 image
->SetMask( hasmask
);
279 int wxTIFFHandler::GetImageCount( wxInputStream
& stream
)
281 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
286 int dircount
= 0; // according to the libtiff docs, dircount should be set to 1 here???
289 } while (TIFFReadDirectory(tif
));
296 bool wxTIFFHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
298 TIFF
*tif
= TIFFwxOpen( stream
, "image", "w" );
303 wxLogError( _("TIFF: Error saving image.") );
308 TIFFSetField(tif
, TIFFTAG_IMAGEWIDTH
, (uint32
)image
->GetWidth());
309 TIFFSetField(tif
, TIFFTAG_IMAGELENGTH
, (uint32
)image
->GetHeight());
310 TIFFSetField(tif
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
311 TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, 3);
312 TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, 8);
313 TIFFSetField(tif
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
);
314 TIFFSetField(tif
, TIFFTAG_PHOTOMETRIC
, PHOTOMETRIC_RGB
);
315 TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_LZW
);
317 tsize_t linebytes
= (tsize_t
)image
->GetWidth() * 3;
320 if (TIFFScanlineSize(tif
) > linebytes
)
322 buf
= (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif
));
326 wxLogError( _("TIFF: Couldn't allocate memory.") );
338 TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
,
339 TIFFDefaultStripSize(tif
, (uint32
) -1));
341 unsigned char *ptr
= image
->GetData();
342 for (int row
= 0; row
< image
->GetHeight(); row
++)
345 memcpy(buf
, ptr
, image
->GetWidth());
347 if (TIFFWriteScanline(tif
, buf
? buf
: ptr
, (uint32
)row
, 0) < 0)
350 wxLogError( _("TIFF: Error writing image.") );
358 ptr
+= image
->GetWidth()*3;
361 (void) TIFFClose(tif
);
369 bool wxTIFFHandler::DoCanRead( wxInputStream
& stream
)
371 unsigned char hdr
[2];
373 if ( !stream
.Read(&hdr
, WXSIZEOF(hdr
)) )
376 stream
.SeekI(-WXSIZEOF(hdr
), wxFromCurrent
);
378 return (hdr
[0] == 'I' && hdr
[1] == 'I') ||
379 (hdr
[0] == 'M' && hdr
[1] == 'M');