]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagtiff.cpp
Stupid timeout bug fixed
[wxWidgets.git] / src / common / imagtiff.cpp
CommitLineData
257bcf28 1/////////////////////////////////////////////////////////////////////////////
8f493002
VS
2// Name: imagtiff.cpp
3// Purpose: wxImage TIFF handler
4// Author: Robert Roebling
257bcf28 5// RCS-ID: $Id$
8f493002 6// Copyright: (c) Robert Roebling
257bcf28
RR
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
8f493002
VS
10#ifdef __GNUG__
11#pragma implementation "imagtiff.h"
12#endif
257bcf28
RR
13
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18#pragma hdrstop
19#endif
20
21#include "wx/defs.h"
22
23#if wxUSE_LIBTIFF
24
8f493002 25#include "wx/imagtiff.h"
257bcf28
RR
26#include "wx/bitmap.h"
27#include "wx/debug.h"
28#include "wx/log.h"
29#include "wx/app.h"
30extern "C"
31{
32 #include "tiff.h"
33 #include "tiffio.h"
34 #include "tiffiop.h"
35}
36#include "wx/filefn.h"
37#include "wx/wfstream.h"
38#include "wx/intl.h"
39#include "wx/module.h"
40
41//-----------------------------------------------------------------------------
42// wxTIFFHandler
43//-----------------------------------------------------------------------------
44
257bcf28 45IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler)
257bcf28
RR
46
47static tsize_t
48_tiffReadProc(thandle_t handle, tdata_t buf, tsize_t size)
49{
50 wxInputStream *stream = (wxInputStream*) handle;
51 stream->Read( (void*) buf, (size_t) size );
0b72db08 52 return stream->LastRead();
257bcf28
RR
53}
54
55static tsize_t
56_tiffWriteProc(thandle_t handle, tdata_t buf, tsize_t size)
57{
58 wxOutputStream *stream = (wxOutputStream*) handle;
59 stream->Write( (void*) buf, (size_t) size );
0b72db08 60 return stream->LastWrite();
257bcf28
RR
61}
62
63static toff_t
64_tiffSeekProc(thandle_t handle, toff_t off, int whence)
65{
66 wxInputStream *stream = (wxInputStream*) handle;
67 wxSeekMode mode;
68 switch (whence)
69 {
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;
74 }
75
76 return (toff_t)stream->SeekI( (off_t)off, mode );
77}
78
79static int
80_tiffCloseProc(thandle_t WXUNUSED(handle))
81{
82 return 0; // ?
83}
84
85static toff_t
86_tiffSizeProc(thandle_t handle)
87{
88 wxInputStream *stream = (wxInputStream*) handle;
0b72db08 89 return (toff_t) stream->GetSize();
257bcf28
RR
90}
91
92static int
93_tiffMapProc(thandle_t WXUNUSED(handle), tdata_t* pbase, toff_t* psize)
94{
95 return 0;
96}
97
98static void
99_tiffUnmapProc(thandle_t WXUNUSED(handle), tdata_t base, toff_t size)
100{
101}
102
103TIFF*
104TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode)
105{
106 TIFF* tif = TIFFClientOpen(name, mode,
107 (thandle_t) &stream,
108 _tiffReadProc, _tiffWriteProc,
109 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
110 _tiffMapProc, _tiffUnmapProc);
111
112 if (tif)
113 tif->tif_fd = (int) &stream;
114
115 return tif;
116}
117
118
700ec454 119bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index )
257bcf28
RR
120{
121 image->Destroy();
122
0b72db08 123 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
257bcf28
RR
124
125 if (!tif)
126 {
127 if (verbose)
58c837a4 128 wxLogError( _("TIFF: Error loading image.") );
257bcf28
RR
129
130 return FALSE;
131 }
700ec454
RR
132
133 if (!TIFFSetDirectory( tif, (tdir_t)index ))
134 {
135 if (verbose)
136 wxLogError( _("Invalid TIFF image index.") );
137
138 TIFFClose( tif );
139
140 return FALSE;
141 }
257bcf28
RR
142
143 uint32 w, h;
144 size_t npixels;
145 uint32 *raster;
146
147 TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
148 TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h );
149
150 npixels = w * h;
151
152 raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
153
154 if (!raster)
155 {
156 if (verbose)
58c837a4 157 wxLogError( _("TIFF: Couldn't allocate memory.") );
257bcf28
RR
158
159 return FALSE;
160 }
161
162 image->Create( w, h );
163 if (!image->Ok())
164 {
165 if (verbose)
58c837a4 166 wxLogError( _("TIFF: Couldn't allocate memory.") );
257bcf28
RR
167
168 _TIFFfree( raster );
169
170 return FALSE;
171 }
172
173 if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
174 {
175 if (verbose)
58c837a4 176 wxLogError( _("TIFF: Error reading image.") );
257bcf28
RR
177
178 _TIFFfree( raster );
179 image->Destroy();
180
181 return FALSE;
182 }
183
184 bool hasmask = FALSE;
185
186 unsigned char *ptr = image->GetData();
187 uint32 pos = 0;
188
189 for (uint32 i = 0; i < h; i++)
190 {
191 for (uint32 j = 0; w < h; j++)
192 {
193 unsigned char alpha = (unsigned char)(raster[pos] >> 24);
194 if (alpha < 127)
195 {
196 hasmask = TRUE;
197 ptr[0] = image->GetMaskRed();
198 ptr++;
199 ptr[0] = image->GetMaskGreen();
200 ptr++;
201 ptr[0] = image->GetMaskBlue();
202 ptr++;
203 }
204 else
205 {
206 ptr[0] = (unsigned char)(raster[pos] >> 16);
207 ptr++;
208 ptr[0] = (unsigned char)(raster[pos] >> 8);
209 ptr++;
210 ptr[0] = (unsigned char)(raster[pos]);
211 ptr++;
212 }
213 pos++;
214 }
215 }
216
217 _TIFFfree( raster );
218
219 TIFFClose( tif );
220
221 image->SetMask( hasmask );
222
223 return TRUE;
224}
225
700ec454
RR
226int wxTIFFHandler::GetImageCount( wxInputStream& stream )
227{
228 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
229
230 if (!tif)
231 return 0;
257bcf28 232
700ec454
RR
233 int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here???
234 do {
235 dircount++;
236 } while (TIFFReadDirectory(tif));
237
238 TIFFClose( tif );
239
240 return dircount;
241}
257bcf28 242
700ec454 243bool wxTIFFHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
257bcf28
RR
244{
245 return FALSE;
246}
247
248bool wxTIFFHandler::DoCanRead( wxInputStream& stream )
249{
0b72db08 250 unsigned char hdr[2];
257bcf28 251
0b72db08
RR
252 stream.Read(&hdr, 2);
253 stream.SeekI(-2, wxFromCurrent);
254
255 return ((hdr[0] == 0x49 && hdr[1] == 0x49) ||
256 (hdr[0] == 0x4D && hdr[1] == 0x4D));
257bcf28
RR
257}
258
259
260#endif
261 // wxUSE_LIBTIFF
262
263
264
265
266
267