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