]>
git.saurik.com Git - wxWidgets.git/blob - src/common/imagtiff.cpp
474e12e3e05b5888bf59ffdacae639e92deb0a94
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 if (!*stream
) return 0;
60 _tiffWriteProc(thandle_t handle
, tdata_t buf
, tsize_t size
)
62 wxOutputStream
*stream
= (wxOutputStream
*) handle
;
63 stream
->Write( (void*) buf
, (size_t) size
);
64 if (!*stream
) return 0;
69 _tiffSeekProc(thandle_t handle
, toff_t off
, int whence
)
71 wxInputStream
*stream
= (wxInputStream
*) handle
;
75 case SEEK_SET
: mode
= wxFromStart
; break;
76 case SEEK_CUR
: mode
= wxFromCurrent
; break;
77 case SEEK_END
: mode
= wxFromEnd
; break;
78 default: mode
= wxFromCurrent
; break;
81 return (toff_t
)stream
->SeekI( (off_t
)off
, mode
);
85 _tiffCloseProc(thandle_t
WXUNUSED(handle
))
91 _tiffSizeProc(thandle_t handle
)
93 wxInputStream
*stream
= (wxInputStream
*) handle
;
95 return ((fsize
= stream
->SeekI(0, wxFromEnd
)) < 0 ? 0 : fsize
);
99 _tiffMapProc(thandle_t
WXUNUSED(handle
), tdata_t
* pbase
, toff_t
* psize
)
105 _tiffUnmapProc(thandle_t
WXUNUSED(handle
), tdata_t base
, toff_t size
)
110 TIFFwxOpen(wxInputStream
&stream
, const char* name
, const char* mode
)
112 TIFF
* tif
= TIFFClientOpen(name
, mode
,
114 _tiffReadProc
, _tiffWriteProc
,
115 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
116 _tiffMapProc
, _tiffUnmapProc
);
119 tif
->tif_fd
= (int) &stream
;
125 bool wxTIFFHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
)
129 TIFF
*tif
= TIFFwxOpen( stream
, "horse.tif", "r" );
134 wxLogError( _("Error loading TIFF image.") );
143 TIFFGetField( tif
, TIFFTAG_IMAGEWIDTH
, &w
);
144 TIFFGetField( tif
, TIFFTAG_IMAGELENGTH
, &h
);
148 raster
= (uint32
*) _TIFFmalloc( npixels
* sizeof(uint32
) );
153 wxLogError( _("Not enough memory for loading TIFF image.") );
158 image
->Create( w
, h
);
162 wxLogError( _("Not enough memory for loading TIFF image.") );
169 if (!TIFFReadRGBAImage( tif
, w
, h
, raster
, 0 ))
172 wxLogError( _("Error reading TIFF image.") );
180 bool hasmask
= FALSE
;
182 unsigned char *ptr
= image
->GetData();
185 for (uint32 i
= 0; i
< h
; i
++)
187 for (uint32 j
= 0; w
< h
; j
++)
189 unsigned char alpha
= (unsigned char)(raster
[pos
] >> 24);
193 ptr
[0] = image
->GetMaskRed();
195 ptr
[0] = image
->GetMaskGreen();
197 ptr
[0] = image
->GetMaskBlue();
202 ptr
[0] = (unsigned char)(raster
[pos
] >> 16);
204 ptr
[0] = (unsigned char)(raster
[pos
] >> 8);
206 ptr
[0] = (unsigned char)(raster
[pos
]);
217 image
->SetMask( hasmask
);
224 bool wxTIFFHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
229 bool wxTIFFHandler::DoCanRead( wxInputStream
& stream
)
234 unsigned char hdr[4];
236 stream.Read(&hdr, 4);
237 stream.SeekI(-4, wxFromCurrent);
238 return (hdr[0] == 'T' && hdr[1] == 'I' && hdr[2] == 'F' && hdr[3] == 'F');