more fixes to compilation warnings from HP-UX build log. About 30% more to go
[wxWidgets.git] / src / common / imagtiff.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: imagtiff.cpp
3 // Purpose: wxImage TIFF handler
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "imagtiff.h"
12 #endif
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
25 #include "wx/imagtiff.h"
26 #include "wx/bitmap.h"
27 #include "wx/debug.h"
28 #include "wx/log.h"
29 #include "wx/app.h"
30 extern "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
45 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler)
46
47 static 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 );
52 return stream->LastRead();
53 }
54
55 static 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 );
60 return stream->LastWrite();
61 }
62
63 static 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
79 static int
80 _tiffCloseProc(thandle_t WXUNUSED(handle))
81 {
82 return 0; // ?
83 }
84
85 static toff_t
86 _tiffSizeProc(thandle_t handle)
87 {
88 wxInputStream *stream = (wxInputStream*) handle;
89 return (toff_t) stream->GetSize();
90 }
91
92 static int
93 _tiffMapProc(thandle_t WXUNUSED(handle),
94 tdata_t* WXUNUSED(pbase),
95 toff_t* WXUNUSED(psize))
96 {
97 return 0;
98 }
99
100 static void
101 _tiffUnmapProc(thandle_t WXUNUSED(handle),
102 tdata_t WXUNUSED(base),
103 toff_t WXUNUSED(size))
104 {
105 }
106
107 TIFF*
108 TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode)
109 {
110 TIFF* tif = TIFFClientOpen(name, mode,
111 (thandle_t) &stream,
112 _tiffReadProc, _tiffWriteProc,
113 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
114 _tiffMapProc, _tiffUnmapProc);
115
116 if (tif)
117 tif->tif_fd = (int) &stream;
118
119 return tif;
120 }
121
122
123 bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index )
124 {
125 image->Destroy();
126
127 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
128
129 if (!tif)
130 {
131 if (verbose)
132 wxLogError( _("TIFF: Error loading image.") );
133
134 return FALSE;
135 }
136
137 if (!TIFFSetDirectory( tif, (tdir_t)index ))
138 {
139 if (verbose)
140 wxLogError( _("Invalid TIFF image index.") );
141
142 TIFFClose( tif );
143
144 return FALSE;
145 }
146
147 uint32 w, h;
148 uint32 npixels;
149 uint32 *raster;
150
151 TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
152 TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h );
153
154 npixels = w * h;
155
156 raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
157
158 if (!raster)
159 {
160 if (verbose)
161 wxLogError( _("TIFF: Couldn't allocate memory.") );
162
163 return FALSE;
164 }
165
166 image->Create( (int)w, (int)h );
167 if (!image->Ok())
168 {
169 if (verbose)
170 wxLogError( _("TIFF: Couldn't allocate memory.") );
171
172 _TIFFfree( raster );
173
174 return FALSE;
175 }
176
177 if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
178 {
179 if (verbose)
180 wxLogError( _("TIFF: Error reading image.") );
181
182 _TIFFfree( raster );
183 image->Destroy();
184
185 return FALSE;
186 }
187
188 bool hasmask = FALSE;
189
190 unsigned char *ptr = image->GetData();
191 uint32 pos = 0;
192
193 for (uint32 i = 0; i < h; i++)
194 {
195 for (uint32 j = 0; w < h; j++)
196 {
197 unsigned char alpha = (unsigned char)(raster[pos] >> 24);
198 if (alpha < 127)
199 {
200 hasmask = TRUE;
201 ptr[0] = image->GetMaskRed();
202 ptr++;
203 ptr[0] = image->GetMaskGreen();
204 ptr++;
205 ptr[0] = image->GetMaskBlue();
206 ptr++;
207 }
208 else
209 {
210 ptr[0] = (unsigned char)(raster[pos] >> 16);
211 ptr++;
212 ptr[0] = (unsigned char)(raster[pos] >> 8);
213 ptr++;
214 ptr[0] = (unsigned char)(raster[pos]);
215 ptr++;
216 }
217 pos++;
218 }
219 }
220
221 _TIFFfree( raster );
222
223 TIFFClose( tif );
224
225 image->SetMask( hasmask );
226
227 return TRUE;
228 }
229
230 int wxTIFFHandler::GetImageCount( wxInputStream& stream )
231 {
232 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
233
234 if (!tif)
235 return 0;
236
237 int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here???
238 do {
239 dircount++;
240 } while (TIFFReadDirectory(tif));
241
242 TIFFClose( tif );
243
244 return dircount;
245 }
246
247 bool wxTIFFHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
248 {
249 return FALSE;
250 }
251
252 bool wxTIFFHandler::DoCanRead( wxInputStream& stream )
253 {
254 unsigned char hdr[2];
255
256 stream.Read(&hdr, 2);
257 stream.SeekI(-2, wxFromCurrent);
258
259 return ((hdr[0] == 0x49 && hdr[1] == 0x49) ||
260 (hdr[0] == 0x4D && hdr[1] == 0x4D));
261 }
262
263
264 #endif
265 // wxUSE_LIBTIFF
266
267
268
269
270
271