]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagtiff.cpp
fix warnings about empty controlling statement in release build
[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
426272a3
VZ
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
257bcf28
RR
18// For compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
21#ifdef __BORLANDC__
8898456d 22 #pragma hdrstop
257bcf28
RR
23#endif
24
c96ea657 25#if wxUSE_IMAGE && wxUSE_LIBTIFF
257bcf28 26
88a7a4e1
WS
27#include "wx/imagtiff.h"
28
8898456d
WS
29#ifndef WX_PRECOMP
30 #include "wx/log.h"
31 #include "wx/app.h"
88a7a4e1 32 #include "wx/intl.h"
0bca0373 33 #include "wx/bitmap.h"
02761f6c 34 #include "wx/module.h"
34deb5cd 35 #include "wx/wxcrtvararg.h"
8898456d
WS
36#endif
37
257bcf28
RR
38extern "C"
39{
8df068ca
CE
40#ifdef __DMC__
41 #include "tif_config.h"
426272a3 42#endif
257bcf28
RR
43 #include "tiff.h"
44 #include "tiffio.h"
257bcf28
RR
45}
46#include "wx/filefn.h"
47#include "wx/wfstream.h"
257bcf28 48
0729bd19 49#ifndef TIFFLINKAGEMODE
0bca0373
WS
50 #if defined(__WATCOMC__) && defined(__WXMGL__)
51 #define TIFFLINKAGEMODE cdecl
52 #else
53 #define TIFFLINKAGEMODE LINKAGEMODE
54 #endif
19193a2c 55#endif
0729bd19 56
426272a3
VZ
57// ============================================================================
58// implementation
59// ============================================================================
60
61// ----------------------------------------------------------------------------
62// TIFF library error/warning handlers
63// ----------------------------------------------------------------------------
64
34deb5cd
VS
65static wxString
66FormatTiffMessage(const char *module, const char *fmt, va_list ap)
67{
68 char buf[512];
69 if ( wxCRT_VsnprintfA(buf, WXSIZEOF(buf), fmt, ap) <= 0 )
70 {
71 // this isn't supposed to happen, but if it does, it's better
72 // than nothing
73 strcpy(buf, "Incorrectly formatted TIFF message");
74 }
75 buf[WXSIZEOF(buf)-1] = 0; // make sure it is always NULL-terminated
76
77 wxString msg(buf);
78 if ( module )
79 msg += wxString::Format(_(" (in module \"%s\")"), module);
80
81 return msg;
82}
83
426272a3
VZ
84extern "C"
85{
86
87static void
34deb5cd 88TIFFwxWarningHandler(const char* module, const char *fmt, va_list ap)
426272a3 89{
34deb5cd 90 wxLogWarning("%s", FormatTiffMessage(module, fmt, ap));
426272a3
VZ
91}
92
93static void
34deb5cd 94TIFFwxErrorHandler(const char* module, const char *fmt, va_list ap)
426272a3 95{
34deb5cd 96 wxLogError("%s", FormatTiffMessage(module, fmt, ap));
426272a3
VZ
97}
98
99} // extern "C"
100
257bcf28
RR
101//-----------------------------------------------------------------------------
102// wxTIFFHandler
103//-----------------------------------------------------------------------------
104
257bcf28 105IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler)
257bcf28 106
426272a3
VZ
107wxTIFFHandler::wxTIFFHandler()
108{
109 m_name = wxT("TIFF file");
110 m_extension = wxT("tif");
ba4800d3 111 m_altExtensions.Add(wxT("tiff"));
426272a3
VZ
112 m_type = wxBITMAP_TYPE_TIF;
113 m_mime = wxT("image/tiff");
114 TIFFSetWarningHandler((TIFFErrorHandler) TIFFwxWarningHandler);
115 TIFFSetErrorHandler((TIFFErrorHandler) TIFFwxErrorHandler);
116}
117
e30285ab
VZ
118#if wxUSE_STREAMS
119
ec524671
VZ
120// helper to translate our, possibly 64 bit, wxFileOffset to TIFF, always 32
121// bit, toff_t
122static toff_t wxFileOffsetToTIFF(wxFileOffset ofs)
123{
124 if ( ofs == wxInvalidOffset )
125 return (toff_t)-1;
126
127 toff_t tofs = wx_truncate_cast(toff_t, ofs);
38d4b1e4 128 wxCHECK_MSG( (wxFileOffset)tofs == ofs, (toff_t)-1,
ec524671
VZ
129 _T("TIFF library doesn't support large files") );
130
131 return tofs;
132}
133
134// another helper to convert standard seek mode to our
135static wxSeekMode wxSeekModeFromTIFF(int whence)
136{
137 switch ( whence )
138 {
139 case SEEK_SET:
140 return wxFromStart;
141
142 case SEEK_CUR:
143 return wxFromCurrent;
144
145 case SEEK_END:
146 return wxFromEnd;
147
148 default:
149 return wxFromCurrent;
150 }
151}
152
90350682
VZ
153extern "C"
154{
155
156tsize_t TIFFLINKAGEMODE
46f36538 157wxTIFFNullProc(thandle_t WXUNUSED(handle),
19193a2c
KB
158 tdata_t WXUNUSED(buf),
159 tsize_t WXUNUSED(size))
f6bcfd97
BP
160{
161 return (tsize_t) -1;
162}
163
90350682 164tsize_t TIFFLINKAGEMODE
46f36538 165wxTIFFReadProc(thandle_t handle, tdata_t buf, tsize_t size)
257bcf28
RR
166{
167 wxInputStream *stream = (wxInputStream*) handle;
168 stream->Read( (void*) buf, (size_t) size );
4a10ea8b 169 return wx_truncate_cast(tsize_t, stream->LastRead());
257bcf28
RR
170}
171
90350682 172tsize_t TIFFLINKAGEMODE
46f36538 173wxTIFFWriteProc(thandle_t handle, tdata_t buf, tsize_t size)
257bcf28
RR
174{
175 wxOutputStream *stream = (wxOutputStream*) handle;
176 stream->Write( (void*) buf, (size_t) size );
4a10ea8b 177 return wx_truncate_cast(tsize_t, stream->LastWrite());
257bcf28
RR
178}
179
90350682 180toff_t TIFFLINKAGEMODE
46f36538 181wxTIFFSeekIProc(thandle_t handle, toff_t off, int whence)
257bcf28
RR
182{
183 wxInputStream *stream = (wxInputStream*) handle;
13111b2a 184
ec524671
VZ
185 return wxFileOffsetToTIFF(stream->SeekI((wxFileOffset)off,
186 wxSeekModeFromTIFF(whence)));
257bcf28
RR
187}
188
90350682 189toff_t TIFFLINKAGEMODE
46f36538 190wxTIFFSeekOProc(thandle_t handle, toff_t off, int whence)
f6bcfd97
BP
191{
192 wxOutputStream *stream = (wxOutputStream*) handle;
f6bcfd97 193
ec524671
VZ
194 return wxFileOffsetToTIFF(stream->SeekO((wxFileOffset)off,
195 wxSeekModeFromTIFF(whence)));
f6bcfd97
BP
196}
197
90350682 198int TIFFLINKAGEMODE
46f36538 199wxTIFFCloseIProc(thandle_t WXUNUSED(handle))
257bcf28 200{
be0d315e
VZ
201 // there is no need to close the input stream
202 return 0;
203}
204
205int TIFFLINKAGEMODE
46f36538 206wxTIFFCloseOProc(thandle_t handle)
be0d315e
VZ
207{
208 wxOutputStream *stream = (wxOutputStream*) handle;
209
210 return stream->Close() ? 0 : -1;
257bcf28
RR
211}
212
90350682 213toff_t TIFFLINKAGEMODE
46f36538 214wxTIFFSizeProc(thandle_t handle)
257bcf28 215{
f6bcfd97 216 wxStreamBase *stream = (wxStreamBase*) handle;
0b72db08 217 return (toff_t) stream->GetSize();
257bcf28
RR
218}
219
90350682 220int TIFFLINKAGEMODE
46f36538 221wxTIFFMapProc(thandle_t WXUNUSED(handle),
13111b2a
VZ
222 tdata_t* WXUNUSED(pbase),
223 toff_t* WXUNUSED(psize))
257bcf28
RR
224{
225 return 0;
226}
227
90350682 228void TIFFLINKAGEMODE
46f36538 229wxTIFFUnmapProc(thandle_t WXUNUSED(handle),
13111b2a
VZ
230 tdata_t WXUNUSED(base),
231 toff_t WXUNUSED(size))
257bcf28
RR
232{
233}
234
90350682
VZ
235} // extern "C"
236
257bcf28
RR
237TIFF*
238TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode)
239{
240 TIFF* tif = TIFFClientOpen(name, mode,
241 (thandle_t) &stream,
46f36538
VZ
242 wxTIFFReadProc, wxTIFFNullProc,
243 wxTIFFSeekIProc, wxTIFFCloseIProc, wxTIFFSizeProc,
244 wxTIFFMapProc, wxTIFFUnmapProc);
257bcf28 245
257bcf28
RR
246 return tif;
247}
248
f6bcfd97
BP
249TIFF*
250TIFFwxOpen(wxOutputStream &stream, const char* name, const char* mode)
251{
252 TIFF* tif = TIFFClientOpen(name, mode,
253 (thandle_t) &stream,
46f36538
VZ
254 wxTIFFNullProc, wxTIFFWriteProc,
255 wxTIFFSeekOProc, wxTIFFCloseOProc, wxTIFFSizeProc,
256 wxTIFFMapProc, wxTIFFUnmapProc);
f6bcfd97
BP
257
258 return tif;
259}
257bcf28 260
700ec454 261bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index )
257bcf28 262{
60d43ad8
VS
263 if (index == -1)
264 index = 0;
265
257bcf28 266 image->Destroy();
13111b2a 267
0b72db08 268 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
13111b2a 269
257bcf28
RR
270 if (!tif)
271 {
272 if (verbose)
58c837a4 273 wxLogError( _("TIFF: Error loading image.") );
13111b2a 274
7beb59f3 275 return false;
257bcf28 276 }
13111b2a 277
700ec454
RR
278 if (!TIFFSetDirectory( tif, (tdir_t)index ))
279 {
280 if (verbose)
281 wxLogError( _("Invalid TIFF image index.") );
13111b2a 282
700ec454 283 TIFFClose( tif );
13111b2a 284
7beb59f3 285 return false;
700ec454 286 }
257bcf28
RR
287
288 uint32 w, h;
13111b2a 289 uint32 npixels;
257bcf28 290 uint32 *raster;
13111b2a 291
257bcf28
RR
292 TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
293 TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h );
13111b2a 294
b6ac40dc
VS
295 uint16 extraSamples;
296 uint16* samplesInfo;
297 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
298 &extraSamples, &samplesInfo);
299 const bool hasAlpha = (extraSamples == 1 &&
300 (samplesInfo[0] == EXTRASAMPLE_ASSOCALPHA ||
301 samplesInfo[0] == EXTRASAMPLE_UNASSALPHA));
302
257bcf28 303 npixels = w * h;
13111b2a 304
257bcf28 305 raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
13111b2a 306
257bcf28
RR
307 if (!raster)
308 {
309 if (verbose)
58c837a4 310 wxLogError( _("TIFF: Couldn't allocate memory.") );
38755449 311
f6bcfd97 312 TIFFClose( tif );
13111b2a 313
7beb59f3 314 return false;
257bcf28
RR
315 }
316
479cd5de 317 image->Create( (int)w, (int)h );
13111b2a 318 if (!image->Ok())
257bcf28
RR
319 {
320 if (verbose)
58c837a4 321 wxLogError( _("TIFF: Couldn't allocate memory.") );
13111b2a
VZ
322
323 _TIFFfree( raster );
f6bcfd97 324 TIFFClose( tif );
13111b2a 325
7beb59f3 326 return false;
257bcf28 327 }
13111b2a 328
b6ac40dc
VS
329 if ( hasAlpha )
330 image->SetAlpha();
331
257bcf28
RR
332 if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
333 {
334 if (verbose)
58c837a4 335 wxLogError( _("TIFF: Error reading image.") );
13111b2a
VZ
336
337 _TIFFfree( raster );
338 image->Destroy();
f6bcfd97 339 TIFFClose( tif );
13111b2a 340
7beb59f3 341 return false;
257bcf28 342 }
13111b2a 343
257bcf28 344 unsigned char *ptr = image->GetData();
5c5ab9eb 345 ptr += w*3*(h-1);
b6ac40dc
VS
346
347 unsigned char *alpha = hasAlpha ? image->GetAlpha() : NULL;
348 if ( hasAlpha )
349 alpha += w*(h-1);
350
257bcf28 351 uint32 pos = 0;
13111b2a 352
257bcf28
RR
353 for (uint32 i = 0; i < h; i++)
354 {
ff7c6c9c 355 for (uint32 j = 0; j < w; j++)
13111b2a 356 {
b6ac40dc
VS
357 *(ptr++) = (unsigned char)TIFFGetR(raster[pos]);
358 *(ptr++) = (unsigned char)TIFFGetG(raster[pos]);
359 *(ptr++) = (unsigned char)TIFFGetB(raster[pos]);
360 if ( hasAlpha )
361 *(alpha++) = (unsigned char)TIFFGetA(raster[pos]);
362
13111b2a
VZ
363 pos++;
364 }
b6ac40dc
VS
365
366 // subtract line we just added plus one line:
367 ptr -= 2*w*3;
368 if ( hasAlpha )
369 alpha -= 2*w;
257bcf28 370 }
13111b2a 371
37ba70a5
VZ
372 // set the image resolution if it's available
373 uint16 tiffRes;
374 if ( TIFFGetField(tif, TIFFTAG_RESOLUTIONUNIT, &tiffRes) )
375 {
376 wxImageResolution res;
377 switch ( tiffRes )
378 {
379 default:
380 wxLogWarning(_("Unknown TIFF resolution unit %d ignored"),
381 tiffRes);
382 // fall through
383
384 case RESUNIT_NONE:
385 res = wxIMAGE_RESOLUTION_NONE;
386 break;
387
388 case RESUNIT_INCH:
389 res = wxIMAGE_RESOLUTION_INCHES;
390 break;
391
392 case RESUNIT_CENTIMETER:
393 res = wxIMAGE_RESOLUTION_CM;
394 break;
395 }
396
397 if ( res != wxIMAGE_RESOLUTION_NONE )
398 {
399 float xres, yres;
400 if ( TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres) )
ad91e1ad 401 image->SetOption(wxIMAGE_OPTION_RESOLUTIONX, wxRound(xres));
37ba70a5
VZ
402
403 if ( TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres) )
ad91e1ad 404 image->SetOption(wxIMAGE_OPTION_RESOLUTIONY, wxRound(yres));
37ba70a5
VZ
405 }
406 }
407
408
257bcf28 409 _TIFFfree( raster );
13111b2a 410
257bcf28 411 TIFFClose( tif );
13111b2a 412
7beb59f3 413 return true;
257bcf28
RR
414}
415
649d13e8 416int wxTIFFHandler::GetImageCount( wxInputStream& stream )
700ec454
RR
417{
418 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
13111b2a 419
700ec454 420 if (!tif)
13111b2a 421 return 0;
257bcf28 422
700ec454
RR
423 int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here???
424 do {
425 dircount++;
426 } while (TIFFReadDirectory(tif));
13111b2a 427
700ec454 428 TIFFClose( tif );
13111b2a 429
700ec454
RR
430 return dircount;
431}
257bcf28 432
f6bcfd97 433bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
257bcf28 434{
f6bcfd97
BP
435 TIFF *tif = TIFFwxOpen( stream, "image", "w" );
436
437 if (!tif)
438 {
439 if (verbose)
440 wxLogError( _("TIFF: Error saving image.") );
441
7beb59f3 442 return false;
f6bcfd97
BP
443 }
444
fe9308c6 445 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
f6bcfd97
BP
446 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)image->GetWidth());
447 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight());
448 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
f6bcfd97 449 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
38755449 450
37ba70a5
VZ
451 // save the image resolution if we have it
452 int xres, yres;
453 const wxImageResolution res = GetResolutionFromOptions(*image, &xres, &yres);
454 uint16 tiffRes;
455 switch ( res )
fe9308c6 456 {
37ba70a5
VZ
457 default:
458 wxFAIL_MSG( _T("unknown image resolution units") );
459 // fall through
460
461 case wxIMAGE_RESOLUTION_NONE:
462 tiffRes = RESUNIT_NONE;
463 break;
464
465 case wxIMAGE_RESOLUTION_INCHES:
466 tiffRes = RESUNIT_INCH;
467 break;
468
469 case wxIMAGE_RESOLUTION_CM:
470 tiffRes = RESUNIT_CENTIMETER;
471 break;
fe9308c6
VZ
472 }
473
37ba70a5
VZ
474 if ( tiffRes != RESUNIT_NONE )
475 {
476 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, tiffRes);
477 TIFFSetField(tif, TIFFTAG_XRESOLUTION, (float)xres);
478 TIFFSetField(tif, TIFFTAG_YRESOLUTION, (float)yres);
479 }
480
481
fe9308c6
VZ
482 int spp = image->GetOptionInt(wxIMAGE_OPTION_SAMPLESPERPIXEL);
483 if ( !spp )
484 spp = 3;
485
486 int bpp = image->GetOptionInt(wxIMAGE_OPTION_BITSPERSAMPLE);
487 if ( !bpp )
37ba70a5 488 bpp = 8;
fe9308c6
VZ
489
490 int compression = image->GetOptionInt(wxIMAGE_OPTION_COMPRESSION);
491 if ( !compression )
f5e20985
VZ
492 {
493 // we can't use COMPRESSION_LZW because current version of libtiff
494 // doesn't implement it ("no longer implemented due to Unisys patent
495 // enforcement") and other compression methods are lossy so we
496 // shouldn't use them by default -- and the only remaining one is none
497 compression = COMPRESSION_NONE;
498 }
fe9308c6
VZ
499
500 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp);
501 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bpp);
502 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, spp*bpp == 1 ? PHOTOMETRIC_MINISBLACK
503 : PHOTOMETRIC_RGB);
504 TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
505
506 // scanlinesize if determined by spp and bpp
507 tsize_t linebytes = (tsize_t)image->GetWidth() * spp * bpp / 8;
508
509 if ( (image->GetWidth() % 8 > 0) && (spp * bpp < 8) )
510 linebytes+=1;
511
f6bcfd97 512 unsigned char *buf;
38755449 513
fe9308c6 514 if (TIFFScanlineSize(tif) > linebytes || (spp * bpp < 24))
f6bcfd97
BP
515 {
516 buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
517 if (!buf)
518 {
519 if (verbose)
520 wxLogError( _("TIFF: Couldn't allocate memory.") );
521
522 TIFFClose( tif );
523
7beb59f3 524 return false;
f6bcfd97 525 }
38755449
DW
526 }
527 else
f6bcfd97
BP
528 {
529 buf = NULL;
530 }
531
fe9308c6
VZ
532 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,TIFFDefaultStripSize(tif, (uint32) -1));
533
f6bcfd97 534 unsigned char *ptr = image->GetData();
fe9308c6 535 for ( int row = 0; row < image->GetHeight(); row++ )
f6bcfd97 536 {
fe9308c6
VZ
537 if ( buf )
538 {
539 if ( spp * bpp > 1 )
540 {
541 // color image
542 memcpy(buf, ptr, image->GetWidth());
543 }
544 else // black and white image
545 {
546 for ( int column = 0; column < linebytes; column++ )
547 {
548 uint8 reverse = 0;
fe9308c6
VZ
549 for ( int bp = 0; bp < 8; bp++ )
550 {
551 if ( ptr[column*24 + bp*3] > 0 )
552 {
553 // check only red as this is sufficient
38d4b1e4 554 reverse = (uint8)(reverse | 128 >> bp);
fe9308c6 555 }
fe9308c6
VZ
556 }
557
3ba9ea72 558 buf[column] = reverse;
fe9308c6
VZ
559 }
560 }
561 }
38755449 562
fe9308c6 563 if ( TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0 )
f6bcfd97 564 {
19193a2c
KB
565 if (verbose)
566 wxLogError( _("TIFF: Error writing image.") );
38755449 567
f6bcfd97
BP
568 TIFFClose( tif );
569 if (buf)
570 _TIFFfree(buf);
38755449 571
7beb59f3 572 return false;
f6bcfd97 573 }
fe9308c6 574
f6bcfd97
BP
575 ptr += image->GetWidth()*3;
576 }
577
578 (void) TIFFClose(tif);
579
580 if (buf)
fe9308c6 581 _TIFFfree(buf);
f6bcfd97 582
7beb59f3 583 return true;
257bcf28
RR
584}
585
586bool wxTIFFHandler::DoCanRead( wxInputStream& stream )
587{
0b72db08 588 unsigned char hdr[2];
257bcf28 589
7ac31c42 590 if ( !stream.Read(&hdr[0], WXSIZEOF(hdr)) )
7beb59f3 591 return false;
79fa2374 592
79fa2374
VZ
593 return (hdr[0] == 'I' && hdr[1] == 'I') ||
594 (hdr[0] == 'M' && hdr[1] == 'M');
257bcf28
RR
595}
596
e30285ab 597#endif // wxUSE_STREAMS
257bcf28 598
e30285ab 599#endif // wxUSE_LIBTIFF