]>
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"
25 #include "wx/imagtiff.h"
26 #include "wx/bitmap.h"
36 #include "wx/filefn.h"
37 #include "wx/wfstream.h"
39 #include "wx/module.h"
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler
,wxImageHandler
)
48 _tiffReadProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
50 wxInputStream
*stream
= (wxInputStream
*) handle
;
51 stream
->Read( (void*) buf
, (size_t) size
);
52 return stream
->LastRead();
56 _tiffWriteProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
58 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
59 stream
->Write( (void*) buf
, (size_t) size
);
60 return stream
->LastWrite();
64 _tiffSeekProc(thandle_t handle
, toff_t off
, int whence
)
66 wxInputStream
*stream
= (wxInputStream
*) handle
;
70 case SEEK_SET
: mode
= wxFromStart
; break;
71 case SEEK_CUR
: mode
= wxFromCurrent
; break;
72 case SEEK_END
: mode
= wxFromEnd
; break;
73 default: mode
= wxFromCurrent
; break;
76 return (toff_t
)stream
->SeekI( (off_t
)off
, mode
);
80 _tiffCloseProc(thandle_t
WXUNUSED(handle
))
86 _tiffSizeProc(thandle_t handle
)
88 wxInputStream
*stream
= (wxInputStream
*) handle
;
89 return (toff_t
) stream
->GetSize();
93 _tiffMapProc(thandle_t
WXUNUSED(handle
),
94 tdata_t
* WXUNUSED(pbase
),
95 toff_t
* WXUNUSED(psize
))
101 _tiffUnmapProc(thandle_t
WXUNUSED(handle
),
102 tdata_t
WXUNUSED(base
),
103 toff_t
WXUNUSED(size
))
108 TIFFwxOpen(wxInputStream
&stream
, const char* name
, const char* mode
)
110 TIFF
* tif
= TIFFClientOpen(name
, mode
,
112 _tiffReadProc
, _tiffWriteProc
,
113 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
114 _tiffMapProc
, _tiffUnmapProc
);
117 tif
->tif_fd
= (int) &stream
;
123 bool wxTIFFHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int index
)
127 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
132 wxLogError( _("TIFF: Error loading image.") );
137 if (!TIFFSetDirectory( tif
, (tdir_t
)index
))
140 wxLogError( _("Invalid TIFF image index.") );
151 TIFFGetField( tif
, TIFFTAG_IMAGEWIDTH
, &w
);
152 TIFFGetField( tif
, TIFFTAG_IMAGELENGTH
, &h
);
156 raster
= (uint32
*) _TIFFmalloc( npixels
* sizeof(uint32
) );
161 wxLogError( _("TIFF: Couldn't allocate memory.") );
166 image
->Create( (int)w
, (int)h
);
170 wxLogError( _("TIFF: Couldn't allocate memory.") );
177 if (!TIFFReadRGBAImage( tif
, w
, h
, raster
, 0 ))
180 wxLogError( _("TIFF: Error reading image.") );
188 bool hasmask
= FALSE
;
190 unsigned char *ptr
= image
->GetData();
193 for (uint32 i
= 0; i
< h
; i
++)
195 for (uint32 j
= 0; w
< h
; j
++)
197 unsigned char alpha
= (unsigned char)(raster
[pos
] >> 24);
201 ptr
[0] = image
->GetMaskRed();
203 ptr
[0] = image
->GetMaskGreen();
205 ptr
[0] = image
->GetMaskBlue();
210 ptr
[0] = (unsigned char)(raster
[pos
] >> 16);
212 ptr
[0] = (unsigned char)(raster
[pos
] >> 8);
214 ptr
[0] = (unsigned char)(raster
[pos
]);
225 image
->SetMask( hasmask
);
230 int wxTIFFHandler::GetImageCount( wxInputStream
& stream
)
232 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
237 int dircount
= 0; // according to the libtiff docs, dircount should be set to 1 here???
240 } while (TIFFReadDirectory(tif
));
247 bool wxTIFFHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
252 bool wxTIFFHandler::DoCanRead( wxInputStream
& stream
)
254 unsigned char hdr
[2];
256 stream
.Read(&hdr
, 2);
257 stream
.SeekI(-2, wxFromCurrent
);
259 return ((hdr
[0] == 0x49 && hdr
[1] == 0x49) ||
260 (hdr
[0] == 0x4D && hdr
[1] == 0x4D));