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