]>
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"
35 #include "wx/filefn.h"
36 #include "wx/wfstream.h"
38 #include "wx/module.h"
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler
,wxImageHandler
)
47 _tiffReadProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
49 wxInputStream
*stream
= (wxInputStream
*) handle
;
50 stream
->Read( (void*) buf
, (size_t) size
);
51 return stream
->LastRead();
55 _tiffWriteProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
57 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
58 stream
->Write( (void*) buf
, (size_t) size
);
59 return stream
->LastWrite();
63 _tiffSeekProc(thandle_t handle
, toff_t off
, int whence
)
65 wxInputStream
*stream
= (wxInputStream
*) handle
;
69 case SEEK_SET
: mode
= wxFromStart
; break;
70 case SEEK_CUR
: mode
= wxFromCurrent
; break;
71 case SEEK_END
: mode
= wxFromEnd
; break;
72 default: mode
= wxFromCurrent
; break;
75 return (toff_t
)stream
->SeekI( (off_t
)off
, mode
);
79 _tiffCloseProc(thandle_t
WXUNUSED(handle
))
85 _tiffSizeProc(thandle_t handle
)
87 wxInputStream
*stream
= (wxInputStream
*) handle
;
88 return (toff_t
) stream
->GetSize();
92 _tiffMapProc(thandle_t
WXUNUSED(handle
),
93 tdata_t
* WXUNUSED(pbase
),
94 toff_t
* WXUNUSED(psize
))
100 _tiffUnmapProc(thandle_t
WXUNUSED(handle
),
101 tdata_t
WXUNUSED(base
),
102 toff_t
WXUNUSED(size
))
107 TIFFwxOpen(wxInputStream
&stream
, const char* name
, const char* mode
)
109 TIFF
* tif
= TIFFClientOpen(name
, mode
,
111 _tiffReadProc
, _tiffWriteProc
,
112 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
113 _tiffMapProc
, _tiffUnmapProc
);
119 bool wxTIFFHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int index
)
123 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
128 wxLogError( _("TIFF: Error loading image.") );
133 if (!TIFFSetDirectory( tif
, (tdir_t
)index
))
136 wxLogError( _("Invalid TIFF image index.") );
147 TIFFGetField( tif
, TIFFTAG_IMAGEWIDTH
, &w
);
148 TIFFGetField( tif
, TIFFTAG_IMAGELENGTH
, &h
);
152 raster
= (uint32
*) _TIFFmalloc( npixels
* sizeof(uint32
) );
157 wxLogError( _("TIFF: Couldn't allocate memory.") );
162 image
->Create( (int)w
, (int)h
);
166 wxLogError( _("TIFF: Couldn't allocate memory.") );
173 if (!TIFFReadRGBAImage( tif
, w
, h
, raster
, 0 ))
176 wxLogError( _("TIFF: Error reading image.") );
184 bool hasmask
= FALSE
;
186 unsigned char *ptr
= image
->GetData();
190 for (uint32 i
= 0; i
< h
; i
++)
192 for (uint32 j
= 0; j
< w
; j
++)
194 unsigned char alpha
= (unsigned char)TIFFGetA(raster
[pos
]);
198 ptr
[0] = image
->GetMaskRed();
200 ptr
[0] = image
->GetMaskGreen();
202 ptr
[0] = image
->GetMaskBlue();
207 ptr
[0] = (unsigned char)TIFFGetR(raster
[pos
]);
209 ptr
[0] = (unsigned char)TIFFGetG(raster
[pos
]);
211 ptr
[0] = (unsigned char)TIFFGetB(raster
[pos
]);
216 ptr
-= 2*w
*3; // subtract line we just added plus one line
223 image
->SetMask( hasmask
);
228 int wxTIFFHandler::GetImageCount( wxInputStream
& stream
)
230 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
235 int dircount
= 0; // according to the libtiff docs, dircount should be set to 1 here???
238 } while (TIFFReadDirectory(tif
));
245 bool wxTIFFHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
250 bool wxTIFFHandler::DoCanRead( wxInputStream
& stream
)
252 unsigned char hdr
[2];
254 stream
.Read(&hdr
, 2);
255 stream
.SeekI(-2, wxFromCurrent
);
257 return ((hdr
[0] == 0x49 && hdr
[1] == 0x49) ||
258 (hdr
[0] == 0x4D && hdr
[1] == 0x4D));