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