]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagtiff.cpp
Don't delete model in views destructor as several
[wxWidgets.git] / src / common / imagtiff.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #include "wx/defs.h"
18
19 #if wxUSE_IMAGE && wxUSE_LIBTIFF
20
21 #include "wx/imagtiff.h"
22 #include "wx/bitmap.h"
23 #include "wx/debug.h"
24 #include "wx/log.h"
25 #include "wx/app.h"
26 extern "C"
27 {
28 #include "tiff.h"
29 #include "tiffio.h"
30 }
31 #include "wx/filefn.h"
32 #include "wx/wfstream.h"
33 #include "wx/intl.h"
34 #include "wx/module.h"
35
36 #ifndef TIFFLINKAGEMODE
37 #if defined(__WATCOMC__) && defined(__WXMGL__)
38 #define TIFFLINKAGEMODE cdecl
39 #else
40 #define TIFFLINKAGEMODE LINKAGEMODE
41 #endif
42 #endif
43
44 //-----------------------------------------------------------------------------
45 // wxTIFFHandler
46 //-----------------------------------------------------------------------------
47
48 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler)
49
50 #if wxUSE_STREAMS
51
52 // helper to translate our, possibly 64 bit, wxFileOffset to TIFF, always 32
53 // bit, toff_t
54 static toff_t wxFileOffsetToTIFF(wxFileOffset ofs)
55 {
56 if ( ofs == wxInvalidOffset )
57 return (toff_t)-1;
58
59 toff_t tofs = wx_truncate_cast(toff_t, ofs);
60 wxCHECK_MSG( (wxFileOffset)tofs == ofs, (toff_t)-1,
61 _T("TIFF library doesn't support large files") );
62
63 return tofs;
64 }
65
66 // another helper to convert standard seek mode to our
67 static wxSeekMode wxSeekModeFromTIFF(int whence)
68 {
69 switch ( whence )
70 {
71 case SEEK_SET:
72 return wxFromStart;
73
74 case SEEK_CUR:
75 return wxFromCurrent;
76
77 case SEEK_END:
78 return wxFromEnd;
79
80 default:
81 return wxFromCurrent;
82 }
83 }
84
85 extern "C"
86 {
87
88 tsize_t TIFFLINKAGEMODE
89 _tiffNullProc(thandle_t WXUNUSED(handle),
90 tdata_t WXUNUSED(buf),
91 tsize_t WXUNUSED(size))
92 {
93 return (tsize_t) -1;
94 }
95
96 tsize_t TIFFLINKAGEMODE
97 _tiffReadProc(thandle_t handle, tdata_t buf, tsize_t size)
98 {
99 wxInputStream *stream = (wxInputStream*) handle;
100 stream->Read( (void*) buf, (size_t) size );
101 return wx_truncate_cast(tsize_t, stream->LastRead());
102 }
103
104 tsize_t TIFFLINKAGEMODE
105 _tiffWriteProc(thandle_t handle, tdata_t buf, tsize_t size)
106 {
107 wxOutputStream *stream = (wxOutputStream*) handle;
108 stream->Write( (void*) buf, (size_t) size );
109 return wx_truncate_cast(tsize_t, stream->LastWrite());
110 }
111
112 toff_t TIFFLINKAGEMODE
113 _tiffSeekIProc(thandle_t handle, toff_t off, int whence)
114 {
115 wxInputStream *stream = (wxInputStream*) handle;
116
117 return wxFileOffsetToTIFF(stream->SeekI((wxFileOffset)off,
118 wxSeekModeFromTIFF(whence)));
119 }
120
121 toff_t TIFFLINKAGEMODE
122 _tiffSeekOProc(thandle_t handle, toff_t off, int whence)
123 {
124 wxOutputStream *stream = (wxOutputStream*) handle;
125
126 return wxFileOffsetToTIFF(stream->SeekO((wxFileOffset)off,
127 wxSeekModeFromTIFF(whence)));
128 }
129
130 int TIFFLINKAGEMODE
131 _tiffCloseProc(thandle_t WXUNUSED(handle))
132 {
133 return 0; // ?
134 }
135
136 toff_t TIFFLINKAGEMODE
137 _tiffSizeProc(thandle_t handle)
138 {
139 wxStreamBase *stream = (wxStreamBase*) handle;
140 return (toff_t) stream->GetSize();
141 }
142
143 int TIFFLINKAGEMODE
144 _tiffMapProc(thandle_t WXUNUSED(handle),
145 tdata_t* WXUNUSED(pbase),
146 toff_t* WXUNUSED(psize))
147 {
148 return 0;
149 }
150
151 void TIFFLINKAGEMODE
152 _tiffUnmapProc(thandle_t WXUNUSED(handle),
153 tdata_t WXUNUSED(base),
154 toff_t WXUNUSED(size))
155 {
156 }
157
158 static void
159 TIFFwxWarningHandler(const char* module,
160 const char* WXUNUSED_IN_UNICODE(fmt),
161 va_list WXUNUSED_IN_UNICODE(ap))
162 {
163 if (module != NULL)
164 wxLogWarning(_("tiff module: %s"), wxString::FromAscii(module).c_str());
165
166 // FIXME: this is not terrible informative but better than crashing!
167 #if wxUSE_UNICODE
168 wxLogWarning(_("TIFF library warning."));
169 #else
170 wxVLogWarning(fmt, ap);
171 #endif
172 }
173
174 static void
175 TIFFwxErrorHandler(const char* module,
176 const char* WXUNUSED_IN_UNICODE(fmt),
177 va_list WXUNUSED_IN_UNICODE(ap))
178 {
179 if (module != NULL)
180 wxLogError(_("tiff module: %s"), wxString::FromAscii(module).c_str());
181
182 // FIXME: as above
183 #if wxUSE_UNICODE
184 wxLogError(_("TIFF library error."));
185 #else
186 wxVLogError(fmt, ap);
187 #endif
188 }
189
190 } // extern "C"
191
192 TIFF*
193 TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode)
194 {
195 TIFF* tif = TIFFClientOpen(name, mode,
196 (thandle_t) &stream,
197 _tiffReadProc, _tiffNullProc,
198 _tiffSeekIProc, _tiffCloseProc, _tiffSizeProc,
199 _tiffMapProc, _tiffUnmapProc);
200
201 return tif;
202 }
203
204 TIFF*
205 TIFFwxOpen(wxOutputStream &stream, const char* name, const char* mode)
206 {
207 TIFF* tif = TIFFClientOpen(name, mode,
208 (thandle_t) &stream,
209 _tiffNullProc, _tiffWriteProc,
210 _tiffSeekOProc, _tiffCloseProc, _tiffSizeProc,
211 _tiffMapProc, _tiffUnmapProc);
212
213 return tif;
214 }
215
216 wxTIFFHandler::wxTIFFHandler()
217 {
218 m_name = wxT("TIFF file");
219 m_extension = wxT("tif");
220 m_type = wxBITMAP_TYPE_TIF;
221 m_mime = wxT("image/tiff");
222 TIFFSetWarningHandler((TIFFErrorHandler) TIFFwxWarningHandler);
223 TIFFSetErrorHandler((TIFFErrorHandler) TIFFwxErrorHandler);
224 }
225
226 bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index )
227 {
228 if (index == -1)
229 index = 0;
230
231 image->Destroy();
232
233 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
234
235 if (!tif)
236 {
237 if (verbose)
238 wxLogError( _("TIFF: Error loading image.") );
239
240 return false;
241 }
242
243 if (!TIFFSetDirectory( tif, (tdir_t)index ))
244 {
245 if (verbose)
246 wxLogError( _("Invalid TIFF image index.") );
247
248 TIFFClose( tif );
249
250 return false;
251 }
252
253 uint32 w, h;
254 uint32 npixels;
255 uint32 *raster;
256
257 TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
258 TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h );
259
260 npixels = w * h;
261
262 raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
263
264 if (!raster)
265 {
266 if (verbose)
267 wxLogError( _("TIFF: Couldn't allocate memory.") );
268
269 TIFFClose( tif );
270
271 return false;
272 }
273
274 image->Create( (int)w, (int)h );
275 if (!image->Ok())
276 {
277 if (verbose)
278 wxLogError( _("TIFF: Couldn't allocate memory.") );
279
280 _TIFFfree( raster );
281 TIFFClose( tif );
282
283 return false;
284 }
285
286 if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
287 {
288 if (verbose)
289 wxLogError( _("TIFF: Error reading image.") );
290
291 _TIFFfree( raster );
292 image->Destroy();
293 TIFFClose( tif );
294
295 return false;
296 }
297
298 bool hasmask = false;
299
300 unsigned char *ptr = image->GetData();
301 ptr += w*3*(h-1);
302 uint32 pos = 0;
303
304 for (uint32 i = 0; i < h; i++)
305 {
306 for (uint32 j = 0; j < w; j++)
307 {
308 unsigned char alpha = (unsigned char)TIFFGetA(raster[pos]);
309 if (alpha < 127)
310 {
311 hasmask = true;
312 ptr[0] = image->GetMaskRed();
313 ptr++;
314 ptr[0] = image->GetMaskGreen();
315 ptr++;
316 ptr[0] = image->GetMaskBlue();
317 ptr++;
318 }
319 else
320 {
321 ptr[0] = (unsigned char)TIFFGetR(raster[pos]);
322 ptr++;
323 ptr[0] = (unsigned char)TIFFGetG(raster[pos]);
324 ptr++;
325 ptr[0] = (unsigned char)TIFFGetB(raster[pos]);
326 ptr++;
327 }
328 pos++;
329 }
330 ptr -= 2*w*3; // subtract line we just added plus one line
331 }
332
333 _TIFFfree( raster );
334
335 TIFFClose( tif );
336
337 image->SetMask( hasmask );
338
339 return true;
340 }
341
342 int wxTIFFHandler::GetImageCount( wxInputStream& stream )
343 {
344 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
345
346 if (!tif)
347 return 0;
348
349 int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here???
350 do {
351 dircount++;
352 } while (TIFFReadDirectory(tif));
353
354 TIFFClose( tif );
355
356 return dircount;
357 }
358
359 bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
360 {
361 TIFF *tif = TIFFwxOpen( stream, "image", "w" );
362
363 if (!tif)
364 {
365 if (verbose)
366 wxLogError( _("TIFF: Error saving image.") );
367
368 return false;
369 }
370
371 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
372 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)image->GetWidth());
373 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight());
374 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
375 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
376
377 if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) &&
378 image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) )
379 {
380 TIFFSetField(tif, TIFFTAG_XRESOLUTION,
381 (float)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX));
382 TIFFSetField(tif, TIFFTAG_YRESOLUTION,
383 (float)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY));
384 }
385
386 int spp = image->GetOptionInt(wxIMAGE_OPTION_SAMPLESPERPIXEL);
387 if ( !spp )
388 spp = 3;
389
390 int bpp = image->GetOptionInt(wxIMAGE_OPTION_BITSPERSAMPLE);
391 if ( !bpp )
392 bpp=8;
393
394 int compression = image->GetOptionInt(wxIMAGE_OPTION_COMPRESSION);
395 if ( !compression )
396 compression=COMPRESSION_LZW;
397
398 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp);
399 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bpp);
400 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, spp*bpp == 1 ? PHOTOMETRIC_MINISBLACK
401 : PHOTOMETRIC_RGB);
402 TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
403
404 // scanlinesize if determined by spp and bpp
405 tsize_t linebytes = (tsize_t)image->GetWidth() * spp * bpp / 8;
406
407 if ( (image->GetWidth() % 8 > 0) && (spp * bpp < 8) )
408 linebytes+=1;
409
410 unsigned char *buf;
411
412 if (TIFFScanlineSize(tif) > linebytes || (spp * bpp < 24))
413 {
414 buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
415 if (!buf)
416 {
417 if (verbose)
418 wxLogError( _("TIFF: Couldn't allocate memory.") );
419
420 TIFFClose( tif );
421
422 return false;
423 }
424 }
425 else
426 {
427 buf = NULL;
428 }
429
430 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,TIFFDefaultStripSize(tif, (uint32) -1));
431
432 unsigned char *ptr = image->GetData();
433 for ( int row = 0; row < image->GetHeight(); row++ )
434 {
435 if ( buf )
436 {
437 if ( spp * bpp > 1 )
438 {
439 // color image
440 memcpy(buf, ptr, image->GetWidth());
441 }
442 else // black and white image
443 {
444 for ( int column = 0; column < linebytes; column++ )
445 {
446 uint8 reverse = 0;
447 for ( int bp = 0; bp < 8; bp++ )
448 {
449 if ( ptr[column*24 + bp*3] > 0 )
450 {
451 // check only red as this is sufficient
452 reverse = (uint8)(reverse | 128 >> bp);
453 }
454 }
455
456 buf[column] = reverse;
457 }
458 }
459 }
460
461 if ( TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0 )
462 {
463 if (verbose)
464 wxLogError( _("TIFF: Error writing image.") );
465
466 TIFFClose( tif );
467 if (buf)
468 _TIFFfree(buf);
469
470 return false;
471 }
472
473 ptr += image->GetWidth()*3;
474 }
475
476 (void) TIFFClose(tif);
477
478 if (buf)
479 _TIFFfree(buf);
480
481 return true;
482 }
483
484 bool wxTIFFHandler::DoCanRead( wxInputStream& stream )
485 {
486 unsigned char hdr[2];
487
488 if ( !stream.Read(&hdr[0], WXSIZEOF(hdr)) )
489 return false;
490
491 return (hdr[0] == 'I' && hdr[1] == 'I') ||
492 (hdr[0] == 'M' && hdr[1] == 'M');
493 }
494
495 #endif // wxUSE_STREAMS
496
497 #endif // wxUSE_LIBTIFF