]>
git.saurik.com Git - wxWidgets.git/blob - src/common/imagtiff.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage JPEG handler
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 We don't put pragma implement in this file because it is already present in
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
27 #include "wx/bitmap.h"
37 #include "wx/filefn.h"
38 #include "wx/wfstream.h"
40 #include "wx/module.h"
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler
,wxImageHandler
)
49 _tiffReadProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
51 wxInputStream
*stream
= (wxInputStream
*) handle
;
52 stream
->Read( (void*) buf
, (size_t) size
);
53 return stream
->LastRead();
57 _tiffWriteProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
59 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
60 stream
->Write( (void*) buf
, (size_t) size
);
61 return stream
->LastWrite();
65 _tiffSeekProc(thandle_t handle
, toff_t off
, int whence
)
67 wxInputStream
*stream
= (wxInputStream
*) handle
;
71 case SEEK_SET
: mode
= wxFromStart
; break;
72 case SEEK_CUR
: mode
= wxFromCurrent
; break;
73 case SEEK_END
: mode
= wxFromEnd
; break;
74 default: mode
= wxFromCurrent
; break;
77 return (toff_t
)stream
->SeekI( (off_t
)off
, mode
);
81 _tiffCloseProc(thandle_t
WXUNUSED(handle
))
87 _tiffSizeProc(thandle_t handle
)
89 wxInputStream
*stream
= (wxInputStream
*) handle
;
90 return (toff_t
) stream
->GetSize();
94 _tiffMapProc(thandle_t
WXUNUSED(handle
), tdata_t
* pbase
, toff_t
* psize
)
100 _tiffUnmapProc(thandle_t
WXUNUSED(handle
), tdata_t base
, toff_t size
)
105 TIFFwxOpen(wxInputStream
&stream
, const char* name
, const char* mode
)
107 TIFF
* tif
= TIFFClientOpen(name
, mode
,
109 _tiffReadProc
, _tiffWriteProc
,
110 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
111 _tiffMapProc
, _tiffUnmapProc
);
114 tif
->tif_fd
= (int) &stream
;
120 bool wxTIFFHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int index
)
124 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
129 wxLogError( _("TIFF: Error loading image.") );
134 if (!TIFFSetDirectory( tif
, (tdir_t
)index
))
137 wxLogError( _("Invalid TIFF image index.") );
148 TIFFGetField( tif
, TIFFTAG_IMAGEWIDTH
, &w
);
149 TIFFGetField( tif
, TIFFTAG_IMAGELENGTH
, &h
);
153 raster
= (uint32
*) _TIFFmalloc( npixels
* sizeof(uint32
) );
158 wxLogError( _("TIFF: Couldn't allocate memory.") );
163 image
->Create( w
, h
);
167 wxLogError( _("TIFF: Couldn't allocate memory.") );
174 if (!TIFFReadRGBAImage( tif
, w
, h
, raster
, 0 ))
177 wxLogError( _("TIFF: Error reading image.") );
185 bool hasmask
= FALSE
;
187 unsigned char *ptr
= image
->GetData();
190 for (uint32 i
= 0; i
< h
; i
++)
192 for (uint32 j
= 0; w
< h
; j
++)
194 unsigned char alpha
= (unsigned char)(raster
[pos
] >> 24);
198 ptr
[0] = image
->GetMaskRed();
200 ptr
[0] = image
->GetMaskGreen();
202 ptr
[0] = image
->GetMaskBlue();
207 ptr
[0] = (unsigned char)(raster
[pos
] >> 16);
209 ptr
[0] = (unsigned char)(raster
[pos
] >> 8);
211 ptr
[0] = (unsigned char)(raster
[pos
]);
222 image
->SetMask( hasmask
);
227 int wxTIFFHandler::GetImageCount( wxInputStream
& stream
)
229 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
234 int dircount
= 0; // according to the libtiff docs, dircount should be set to 1 here???
237 } while (TIFFReadDirectory(tif
));
244 bool wxTIFFHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
249 bool wxTIFFHandler::DoCanRead( wxInputStream
& stream
)
251 unsigned char hdr
[2];
253 stream
.Read(&hdr
, 2);
254 stream
.SeekI(-2, wxFromCurrent
);
256 return ((hdr
[0] == 0x49 && hdr
[1] == 0x49) ||
257 (hdr
[0] == 0x4D && hdr
[1] == 0x4D));