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