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