]>
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 #if !USE_SHARED_LIBRARIES
47 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler
,wxImageHandler
)
51 _tiffReadProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
53 wxInputStream
*stream
= (wxInputStream
*) handle
;
54 stream
->Read( (void*) buf
, (size_t) size
);
55 return stream
->LastRead();
59 _tiffWriteProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
61 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
62 stream
->Write( (void*) buf
, (size_t) size
);
63 return stream
->LastWrite();
67 _tiffSeekProc(thandle_t handle
, toff_t off
, int whence
)
69 wxInputStream
*stream
= (wxInputStream
*) handle
;
73 case SEEK_SET
: mode
= wxFromStart
; break;
74 case SEEK_CUR
: mode
= wxFromCurrent
; break;
75 case SEEK_END
: mode
= wxFromEnd
; break;
76 default: mode
= wxFromCurrent
; break;
79 return (toff_t
)stream
->SeekI( (off_t
)off
, mode
);
83 _tiffCloseProc(thandle_t
WXUNUSED(handle
))
89 _tiffSizeProc(thandle_t handle
)
91 wxInputStream
*stream
= (wxInputStream
*) handle
;
92 return (toff_t
) stream
->GetSize();
96 _tiffMapProc(thandle_t
WXUNUSED(handle
), tdata_t
* pbase
, toff_t
* psize
)
102 _tiffUnmapProc(thandle_t
WXUNUSED(handle
), tdata_t base
, toff_t 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
);
116 tif
->tif_fd
= (int) &stream
;
122 bool wxTIFFHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int index
)
126 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
131 wxLogError( _("Error loading TIFF image.") );
136 if (!TIFFSetDirectory( tif
, (tdir_t
)index
))
139 wxLogError( _("Invalid TIFF image index.") );
150 TIFFGetField( tif
, TIFFTAG_IMAGEWIDTH
, &w
);
151 TIFFGetField( tif
, TIFFTAG_IMAGELENGTH
, &h
);
155 raster
= (uint32
*) _TIFFmalloc( npixels
* sizeof(uint32
) );
160 wxLogError( _("Not enough memory for loading TIFF image.") );
165 image
->Create( w
, h
);
169 wxLogError( _("Not enough memory for loading TIFF image.") );
176 if (!TIFFReadRGBAImage( tif
, w
, h
, raster
, 0 ))
179 wxLogError( _("Error reading TIFF image.") );
187 bool hasmask
= FALSE
;
189 unsigned char *ptr
= image
->GetData();
192 for (uint32 i
= 0; i
< h
; i
++)
194 for (uint32 j
= 0; w
< h
; j
++)
196 unsigned char alpha
= (unsigned char)(raster
[pos
] >> 24);
200 ptr
[0] = image
->GetMaskRed();
202 ptr
[0] = image
->GetMaskGreen();
204 ptr
[0] = image
->GetMaskBlue();
209 ptr
[0] = (unsigned char)(raster
[pos
] >> 16);
211 ptr
[0] = (unsigned char)(raster
[pos
] >> 8);
213 ptr
[0] = (unsigned char)(raster
[pos
]);
224 image
->SetMask( hasmask
);
229 int wxTIFFHandler::GetImageCount( wxInputStream
& stream
)
231 TIFF
*tif
= TIFFwxOpen( stream
, "image", "r" );
236 int dircount
= 0; // according to the libtiff docs, dircount should be set to 1 here???
239 } while (TIFFReadDirectory(tif
));
246 bool wxTIFFHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
251 bool wxTIFFHandler::DoCanRead( wxInputStream
& stream
)
253 unsigned char hdr
[2];
255 stream
.Read(&hdr
, 2);
256 stream
.SeekI(-2, wxFromCurrent
);
258 return ((hdr
[0] == 0x49 && hdr
[1] == 0x49) ||
259 (hdr
[0] == 0x4D && hdr
[1] == 0x4D));