]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagtiff.cpp
oops, more fixes needed
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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_IMAGE && 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 }
35 #include "wx/filefn.h"
36 #include "wx/wfstream.h"
37 #include "wx/intl.h"
38 #include "wx/module.h"
39
40 #ifndef TIFFLINKAGEMODE
41 #define TIFFLINKAGEMODE LINKAGEMODE
42 #endif
43
44 //-----------------------------------------------------------------------------
45 // wxTIFFHandler
46 //-----------------------------------------------------------------------------
47
48 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler)
49
50 #if wxUSE_STREAMS
51
52 extern "C"
53 {
54
55 tsize_t TIFFLINKAGEMODE
56 _tiffNullProc(thandle_t WXUNUSED(handle),
57 tdata_t WXUNUSED(buf),
58 tsize_t WXUNUSED(size))
59 {
60 return (tsize_t) -1;
61 }
62
63 tsize_t TIFFLINKAGEMODE
64 _tiffReadProc(thandle_t handle, tdata_t buf, tsize_t size)
65 {
66 wxInputStream *stream = (wxInputStream*) handle;
67 stream->Read( (void*) buf, (size_t) size );
68 return stream->LastRead();
69 }
70
71 tsize_t TIFFLINKAGEMODE
72 _tiffWriteProc(thandle_t handle, tdata_t buf, tsize_t size)
73 {
74 wxOutputStream *stream = (wxOutputStream*) handle;
75 stream->Write( (void*) buf, (size_t) size );
76 return stream->LastWrite();
77 }
78
79 toff_t TIFFLINKAGEMODE
80 _tiffSeekIProc(thandle_t handle, toff_t off, int whence)
81 {
82 wxInputStream *stream = (wxInputStream*) handle;
83 wxSeekMode mode;
84 switch (whence)
85 {
86 case SEEK_SET: mode = wxFromStart; break;
87 case SEEK_CUR: mode = wxFromCurrent; break;
88 case SEEK_END: mode = wxFromEnd; break;
89 default: mode = wxFromCurrent; break;
90 }
91
92 return (toff_t)stream->SeekI( (off_t)off, mode );
93 }
94
95 toff_t TIFFLINKAGEMODE
96 _tiffSeekOProc(thandle_t handle, toff_t off, int whence)
97 {
98 wxOutputStream *stream = (wxOutputStream*) handle;
99 wxSeekMode mode;
100 switch (whence)
101 {
102 case SEEK_SET: mode = wxFromStart; break;
103 case SEEK_CUR: mode = wxFromCurrent; break;
104 case SEEK_END: mode = wxFromEnd; break;
105 default: mode = wxFromCurrent; break;
106 }
107
108 return (toff_t)stream->SeekO( (off_t)off, mode );
109 }
110
111 int TIFFLINKAGEMODE
112 _tiffCloseProc(thandle_t WXUNUSED(handle))
113 {
114 return 0; // ?
115 }
116
117 toff_t TIFFLINKAGEMODE
118 _tiffSizeProc(thandle_t handle)
119 {
120 wxStreamBase *stream = (wxStreamBase*) handle;
121 return (toff_t) stream->GetSize();
122 }
123
124 int TIFFLINKAGEMODE
125 _tiffMapProc(thandle_t WXUNUSED(handle),
126 tdata_t* WXUNUSED(pbase),
127 toff_t* WXUNUSED(psize))
128 {
129 return 0;
130 }
131
132 void TIFFLINKAGEMODE
133 _tiffUnmapProc(thandle_t WXUNUSED(handle),
134 tdata_t WXUNUSED(base),
135 toff_t WXUNUSED(size))
136 {
137 }
138
139 static void
140 TIFFwxWarningHandler(const char* module, const char* fmt, va_list ap)
141 {
142 if (module != NULL)
143 wxLogWarning(_("tiff module: %s"), module);
144 wxLogWarning((wxChar *) fmt, ap);
145 }
146
147 static void
148 TIFFwxErrorHandler(const char* module, const char* fmt, va_list ap)
149 {
150 if (module != NULL)
151 wxLogError(_("tiff module: %s"), module);
152 wxVLogError((wxChar *) fmt, ap);
153 }
154
155 } // extern "C"
156
157 TIFF*
158 TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode)
159 {
160 TIFF* tif = TIFFClientOpen(name, mode,
161 (thandle_t) &stream,
162 _tiffReadProc, _tiffNullProc,
163 _tiffSeekIProc, _tiffCloseProc, _tiffSizeProc,
164 _tiffMapProc, _tiffUnmapProc);
165
166 return tif;
167 }
168
169 TIFF*
170 TIFFwxOpen(wxOutputStream &stream, const char* name, const char* mode)
171 {
172 TIFF* tif = TIFFClientOpen(name, mode,
173 (thandle_t) &stream,
174 _tiffNullProc, _tiffWriteProc,
175 _tiffSeekOProc, _tiffCloseProc, _tiffSizeProc,
176 _tiffMapProc, _tiffUnmapProc);
177
178 return tif;
179 }
180
181 wxTIFFHandler::wxTIFFHandler()
182 {
183 m_name = wxT("TIFF file");
184 m_extension = wxT("tif");
185 m_type = wxBITMAP_TYPE_TIF;
186 m_mime = wxT("image/tiff");
187 TIFFSetWarningHandler((TIFFErrorHandler) TIFFwxWarningHandler);
188 TIFFSetErrorHandler((TIFFErrorHandler) TIFFwxErrorHandler);
189 }
190
191 bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index )
192 {
193 if (index == -1)
194 index = 0;
195
196 image->Destroy();
197
198 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
199
200 if (!tif)
201 {
202 if (verbose)
203 wxLogError( _("TIFF: Error loading image.") );
204
205 return FALSE;
206 }
207
208 if (!TIFFSetDirectory( tif, (tdir_t)index ))
209 {
210 if (verbose)
211 wxLogError( _("Invalid TIFF image index.") );
212
213 TIFFClose( tif );
214
215 return FALSE;
216 }
217
218 uint32 w, h;
219 uint32 npixels;
220 uint32 *raster;
221
222 TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
223 TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h );
224
225 npixels = w * h;
226
227 raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
228
229 if (!raster)
230 {
231 if (verbose)
232 wxLogError( _("TIFF: Couldn't allocate memory.") );
233
234 TIFFClose( tif );
235
236 return FALSE;
237 }
238
239 image->Create( (int)w, (int)h );
240 if (!image->Ok())
241 {
242 if (verbose)
243 wxLogError( _("TIFF: Couldn't allocate memory.") );
244
245 _TIFFfree( raster );
246 TIFFClose( tif );
247
248 return FALSE;
249 }
250
251 if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
252 {
253 if (verbose)
254 wxLogError( _("TIFF: Error reading image.") );
255
256 _TIFFfree( raster );
257 image->Destroy();
258 TIFFClose( tif );
259
260 return FALSE;
261 }
262
263 bool hasmask = FALSE;
264
265 unsigned char *ptr = image->GetData();
266 ptr += w*3*(h-1);
267 uint32 pos = 0;
268
269 for (uint32 i = 0; i < h; i++)
270 {
271 for (uint32 j = 0; j < w; j++)
272 {
273 unsigned char alpha = (unsigned char)TIFFGetA(raster[pos]);
274 if (alpha < 127)
275 {
276 hasmask = TRUE;
277 ptr[0] = image->GetMaskRed();
278 ptr++;
279 ptr[0] = image->GetMaskGreen();
280 ptr++;
281 ptr[0] = image->GetMaskBlue();
282 ptr++;
283 }
284 else
285 {
286 ptr[0] = (unsigned char)TIFFGetR(raster[pos]);
287 ptr++;
288 ptr[0] = (unsigned char)TIFFGetG(raster[pos]);
289 ptr++;
290 ptr[0] = (unsigned char)TIFFGetB(raster[pos]);
291 ptr++;
292 }
293 pos++;
294 }
295 ptr -= 2*w*3; // subtract line we just added plus one line
296 }
297
298 _TIFFfree( raster );
299
300 TIFFClose( tif );
301
302 image->SetMask( hasmask );
303
304 return TRUE;
305 }
306
307 int wxTIFFHandler::GetImageCount( wxInputStream& stream )
308 {
309 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
310
311 if (!tif)
312 return 0;
313
314 int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here???
315 do {
316 dircount++;
317 } while (TIFFReadDirectory(tif));
318
319 TIFFClose( tif );
320
321 return dircount;
322 }
323
324 bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
325 {
326 TIFF *tif = TIFFwxOpen( stream, "image", "w" );
327
328 if (!tif)
329 {
330 if (verbose)
331 wxLogError( _("TIFF: Error saving image.") );
332
333 return FALSE;
334 }
335
336 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)image->GetWidth());
337 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight());
338 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
339 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
340 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
341 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
342 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
343 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
344
345 tsize_t linebytes = (tsize_t)image->GetWidth() * 3;
346 unsigned char *buf;
347
348 if (TIFFScanlineSize(tif) > linebytes)
349 {
350 buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
351 if (!buf)
352 {
353 if (verbose)
354 wxLogError( _("TIFF: Couldn't allocate memory.") );
355
356 TIFFClose( tif );
357
358 return FALSE;
359 }
360 }
361 else
362 {
363 buf = NULL;
364 }
365
366 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
367 TIFFDefaultStripSize(tif, (uint32) -1));
368
369 unsigned char *ptr = image->GetData();
370 for (int row = 0; row < image->GetHeight(); row++)
371 {
372 if (buf)
373 memcpy(buf, ptr, image->GetWidth());
374
375 if (TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0)
376 {
377 if (verbose)
378 wxLogError( _("TIFF: Error writing image.") );
379
380 TIFFClose( tif );
381 if (buf)
382 _TIFFfree(buf);
383
384 return FALSE;
385 }
386 ptr += image->GetWidth()*3;
387 }
388
389 (void) TIFFClose(tif);
390
391 if (buf)
392 _TIFFfree(buf);
393
394 return TRUE;
395 }
396
397 bool wxTIFFHandler::DoCanRead( wxInputStream& stream )
398 {
399 unsigned char hdr[2];
400
401 if ( !stream.Read(&hdr, WXSIZEOF(hdr)) )
402 return FALSE;
403
404 return (hdr[0] == 'I' && hdr[1] == 'I') ||
405 (hdr[0] == 'M' && hdr[1] == 'M');
406 }
407
408 #endif // wxUSE_STREAMS
409
410 #endif // wxUSE_LIBTIFF
411