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