]>
Commit | Line | Data |
---|---|---|
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 | #if wxUSE_IMAGE && wxUSE_LIBTIFF | |
18 | ||
19 | #include "wx/imagtiff.h" | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/log.h" | |
23 | #include "wx/app.h" | |
24 | #include "wx/intl.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/bitmap.h" | |
28 | ||
29 | extern "C" | |
30 | { | |
31 | #include "tiff.h" | |
32 | #include "tiffio.h" | |
33 | } | |
34 | #include "wx/filefn.h" | |
35 | #include "wx/wfstream.h" | |
36 | #include "wx/module.h" | |
37 | ||
38 | #ifndef TIFFLINKAGEMODE | |
39 | #if defined(__WATCOMC__) && defined(__WXMGL__) | |
40 | #define TIFFLINKAGEMODE cdecl | |
41 | #else | |
42 | #define TIFFLINKAGEMODE LINKAGEMODE | |
43 | #endif | |
44 | #endif | |
45 | ||
46 | //----------------------------------------------------------------------------- | |
47 | // wxTIFFHandler | |
48 | //----------------------------------------------------------------------------- | |
49 | ||
50 | IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler) | |
51 | ||
52 | #if wxUSE_STREAMS | |
53 | ||
54 | // helper to translate our, possibly 64 bit, wxFileOffset to TIFF, always 32 | |
55 | // bit, toff_t | |
56 | static 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); | |
62 | wxCHECK_MSG( (wxFileOffset)tofs == ofs, (toff_t)-1, | |
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 | |
69 | static 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 | ||
87 | extern "C" | |
88 | { | |
89 | ||
90 | tsize_t TIFFLINKAGEMODE | |
91 | _tiffNullProc(thandle_t WXUNUSED(handle), | |
92 | tdata_t WXUNUSED(buf), | |
93 | tsize_t WXUNUSED(size)) | |
94 | { | |
95 | return (tsize_t) -1; | |
96 | } | |
97 | ||
98 | tsize_t TIFFLINKAGEMODE | |
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 ); | |
103 | return wx_truncate_cast(tsize_t, stream->LastRead()); | |
104 | } | |
105 | ||
106 | tsize_t TIFFLINKAGEMODE | |
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 ); | |
111 | return wx_truncate_cast(tsize_t, stream->LastWrite()); | |
112 | } | |
113 | ||
114 | toff_t TIFFLINKAGEMODE | |
115 | _tiffSeekIProc(thandle_t handle, toff_t off, int whence) | |
116 | { | |
117 | wxInputStream *stream = (wxInputStream*) handle; | |
118 | ||
119 | return wxFileOffsetToTIFF(stream->SeekI((wxFileOffset)off, | |
120 | wxSeekModeFromTIFF(whence))); | |
121 | } | |
122 | ||
123 | toff_t TIFFLINKAGEMODE | |
124 | _tiffSeekOProc(thandle_t handle, toff_t off, int whence) | |
125 | { | |
126 | wxOutputStream *stream = (wxOutputStream*) handle; | |
127 | ||
128 | return wxFileOffsetToTIFF(stream->SeekO((wxFileOffset)off, | |
129 | wxSeekModeFromTIFF(whence))); | |
130 | } | |
131 | ||
132 | int TIFFLINKAGEMODE | |
133 | _tiffCloseProc(thandle_t WXUNUSED(handle)) | |
134 | { | |
135 | return 0; // ? | |
136 | } | |
137 | ||
138 | toff_t TIFFLINKAGEMODE | |
139 | _tiffSizeProc(thandle_t handle) | |
140 | { | |
141 | wxStreamBase *stream = (wxStreamBase*) handle; | |
142 | return (toff_t) stream->GetSize(); | |
143 | } | |
144 | ||
145 | int TIFFLINKAGEMODE | |
146 | _tiffMapProc(thandle_t WXUNUSED(handle), | |
147 | tdata_t* WXUNUSED(pbase), | |
148 | toff_t* WXUNUSED(psize)) | |
149 | { | |
150 | return 0; | |
151 | } | |
152 | ||
153 | void TIFFLINKAGEMODE | |
154 | _tiffUnmapProc(thandle_t WXUNUSED(handle), | |
155 | tdata_t WXUNUSED(base), | |
156 | toff_t WXUNUSED(size)) | |
157 | { | |
158 | } | |
159 | ||
160 | static void | |
161 | TIFFwxWarningHandler(const char* module, | |
162 | const char* WXUNUSED_IN_UNICODE(fmt), | |
163 | va_list WXUNUSED_IN_UNICODE(ap)) | |
164 | { | |
165 | if (module != NULL) | |
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 | |
174 | } | |
175 | ||
176 | static void | |
177 | TIFFwxErrorHandler(const char* module, | |
178 | const char* WXUNUSED_IN_UNICODE(fmt), | |
179 | va_list WXUNUSED_IN_UNICODE(ap)) | |
180 | { | |
181 | if (module != NULL) | |
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 | |
190 | } | |
191 | ||
192 | } // extern "C" | |
193 | ||
194 | TIFF* | |
195 | TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode) | |
196 | { | |
197 | TIFF* tif = TIFFClientOpen(name, mode, | |
198 | (thandle_t) &stream, | |
199 | _tiffReadProc, _tiffNullProc, | |
200 | _tiffSeekIProc, _tiffCloseProc, _tiffSizeProc, | |
201 | _tiffMapProc, _tiffUnmapProc); | |
202 | ||
203 | return tif; | |
204 | } | |
205 | ||
206 | TIFF* | |
207 | TIFFwxOpen(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 | } | |
217 | ||
218 | wxTIFFHandler::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 | ||
228 | bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index ) | |
229 | { | |
230 | if (index == -1) | |
231 | index = 0; | |
232 | ||
233 | image->Destroy(); | |
234 | ||
235 | TIFF *tif = TIFFwxOpen( stream, "image", "r" ); | |
236 | ||
237 | if (!tif) | |
238 | { | |
239 | if (verbose) | |
240 | wxLogError( _("TIFF: Error loading image.") ); | |
241 | ||
242 | return false; | |
243 | } | |
244 | ||
245 | if (!TIFFSetDirectory( tif, (tdir_t)index )) | |
246 | { | |
247 | if (verbose) | |
248 | wxLogError( _("Invalid TIFF image index.") ); | |
249 | ||
250 | TIFFClose( tif ); | |
251 | ||
252 | return false; | |
253 | } | |
254 | ||
255 | uint32 w, h; | |
256 | uint32 npixels; | |
257 | uint32 *raster; | |
258 | ||
259 | TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w ); | |
260 | TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h ); | |
261 | ||
262 | npixels = w * h; | |
263 | ||
264 | raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) ); | |
265 | ||
266 | if (!raster) | |
267 | { | |
268 | if (verbose) | |
269 | wxLogError( _("TIFF: Couldn't allocate memory.") ); | |
270 | ||
271 | TIFFClose( tif ); | |
272 | ||
273 | return false; | |
274 | } | |
275 | ||
276 | image->Create( (int)w, (int)h ); | |
277 | if (!image->Ok()) | |
278 | { | |
279 | if (verbose) | |
280 | wxLogError( _("TIFF: Couldn't allocate memory.") ); | |
281 | ||
282 | _TIFFfree( raster ); | |
283 | TIFFClose( tif ); | |
284 | ||
285 | return false; | |
286 | } | |
287 | ||
288 | if (!TIFFReadRGBAImage( tif, w, h, raster, 0 )) | |
289 | { | |
290 | if (verbose) | |
291 | wxLogError( _("TIFF: Error reading image.") ); | |
292 | ||
293 | _TIFFfree( raster ); | |
294 | image->Destroy(); | |
295 | TIFFClose( tif ); | |
296 | ||
297 | return false; | |
298 | } | |
299 | ||
300 | bool hasmask = false; | |
301 | ||
302 | unsigned char *ptr = image->GetData(); | |
303 | ptr += w*3*(h-1); | |
304 | uint32 pos = 0; | |
305 | ||
306 | for (uint32 i = 0; i < h; i++) | |
307 | { | |
308 | for (uint32 j = 0; j < w; j++) | |
309 | { | |
310 | unsigned char alpha = (unsigned char)TIFFGetA(raster[pos]); | |
311 | if (alpha < 127) | |
312 | { | |
313 | hasmask = true; | |
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 | { | |
323 | ptr[0] = (unsigned char)TIFFGetR(raster[pos]); | |
324 | ptr++; | |
325 | ptr[0] = (unsigned char)TIFFGetG(raster[pos]); | |
326 | ptr++; | |
327 | ptr[0] = (unsigned char)TIFFGetB(raster[pos]); | |
328 | ptr++; | |
329 | } | |
330 | pos++; | |
331 | } | |
332 | ptr -= 2*w*3; // subtract line we just added plus one line | |
333 | } | |
334 | ||
335 | _TIFFfree( raster ); | |
336 | ||
337 | TIFFClose( tif ); | |
338 | ||
339 | image->SetMask( hasmask ); | |
340 | ||
341 | return true; | |
342 | } | |
343 | ||
344 | int wxTIFFHandler::GetImageCount( wxInputStream& stream ) | |
345 | { | |
346 | TIFF *tif = TIFFwxOpen( stream, "image", "r" ); | |
347 | ||
348 | if (!tif) | |
349 | return 0; | |
350 | ||
351 | int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here??? | |
352 | do { | |
353 | dircount++; | |
354 | } while (TIFFReadDirectory(tif)); | |
355 | ||
356 | TIFFClose( tif ); | |
357 | ||
358 | return dircount; | |
359 | } | |
360 | ||
361 | bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) | |
362 | { | |
363 | TIFF *tif = TIFFwxOpen( stream, "image", "w" ); | |
364 | ||
365 | if (!tif) | |
366 | { | |
367 | if (verbose) | |
368 | wxLogError( _("TIFF: Error saving image.") ); | |
369 | ||
370 | return false; | |
371 | } | |
372 | ||
373 | TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); | |
374 | TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)image->GetWidth()); | |
375 | TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight()); | |
376 | TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); | |
377 | TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); | |
378 | ||
379 | if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) && | |
380 | image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) ) | |
381 | { | |
382 | TIFFSetField(tif, TIFFTAG_XRESOLUTION, | |
383 | (float)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX)); | |
384 | TIFFSetField(tif, TIFFTAG_YRESOLUTION, | |
385 | (float)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY)); | |
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 ) | |
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 | } | |
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 | ||
418 | unsigned char *buf; | |
419 | ||
420 | if (TIFFScanlineSize(tif) > linebytes || (spp * bpp < 24)) | |
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 | ||
430 | return false; | |
431 | } | |
432 | } | |
433 | else | |
434 | { | |
435 | buf = NULL; | |
436 | } | |
437 | ||
438 | TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,TIFFDefaultStripSize(tif, (uint32) -1)); | |
439 | ||
440 | unsigned char *ptr = image->GetData(); | |
441 | for ( int row = 0; row < image->GetHeight(); row++ ) | |
442 | { | |
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; | |
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 | |
460 | reverse = (uint8)(reverse | 128 >> bp); | |
461 | } | |
462 | } | |
463 | ||
464 | buf[column] = reverse; | |
465 | } | |
466 | } | |
467 | } | |
468 | ||
469 | if ( TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0 ) | |
470 | { | |
471 | if (verbose) | |
472 | wxLogError( _("TIFF: Error writing image.") ); | |
473 | ||
474 | TIFFClose( tif ); | |
475 | if (buf) | |
476 | _TIFFfree(buf); | |
477 | ||
478 | return false; | |
479 | } | |
480 | ||
481 | ptr += image->GetWidth()*3; | |
482 | } | |
483 | ||
484 | (void) TIFFClose(tif); | |
485 | ||
486 | if (buf) | |
487 | _TIFFfree(buf); | |
488 | ||
489 | return true; | |
490 | } | |
491 | ||
492 | bool wxTIFFHandler::DoCanRead( wxInputStream& stream ) | |
493 | { | |
494 | unsigned char hdr[2]; | |
495 | ||
496 | if ( !stream.Read(&hdr[0], WXSIZEOF(hdr)) ) | |
497 | return false; | |
498 | ||
499 | return (hdr[0] == 'I' && hdr[1] == 'I') || | |
500 | (hdr[0] == 'M' && hdr[1] == 'M'); | |
501 | } | |
502 | ||
503 | #endif // wxUSE_STREAMS | |
504 | ||
505 | #endif // wxUSE_LIBTIFF |