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