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