]>
Commit | Line | Data |
---|---|---|
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, | |
141 | const char* WXUNUSED_IN_UNICODE(fmt), | |
142 | va_list WXUNUSED_IN_UNICODE(ap)) | |
143 | { | |
144 | if (module != NULL) | |
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 | |
153 | } | |
154 | ||
155 | static void | |
156 | TIFFwxErrorHandler(const char* module, | |
157 | const char* WXUNUSED_IN_UNICODE(fmt), | |
158 | va_list WXUNUSED_IN_UNICODE(ap)) | |
159 | { | |
160 | if (module != NULL) | |
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 | |
169 | } | |
170 | ||
171 | } // extern "C" | |
172 | ||
173 | TIFF* | |
174 | TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode) | |
175 | { | |
176 | TIFF* tif = TIFFClientOpen(name, mode, | |
177 | (thandle_t) &stream, | |
178 | _tiffReadProc, _tiffNullProc, | |
179 | _tiffSeekIProc, _tiffCloseProc, _tiffSizeProc, | |
180 | _tiffMapProc, _tiffUnmapProc); | |
181 | ||
182 | return tif; | |
183 | } | |
184 | ||
185 | TIFF* | |
186 | TIFFwxOpen(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 | } | |
196 | ||
197 | wxTIFFHandler::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 | ||
207 | bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index ) | |
208 | { | |
209 | if (index == -1) | |
210 | index = 0; | |
211 | ||
212 | image->Destroy(); | |
213 | ||
214 | TIFF *tif = TIFFwxOpen( stream, "image", "r" ); | |
215 | ||
216 | if (!tif) | |
217 | { | |
218 | if (verbose) | |
219 | wxLogError( _("TIFF: Error loading image.") ); | |
220 | ||
221 | return false; | |
222 | } | |
223 | ||
224 | if (!TIFFSetDirectory( tif, (tdir_t)index )) | |
225 | { | |
226 | if (verbose) | |
227 | wxLogError( _("Invalid TIFF image index.") ); | |
228 | ||
229 | TIFFClose( tif ); | |
230 | ||
231 | return false; | |
232 | } | |
233 | ||
234 | uint32 w, h; | |
235 | uint32 npixels; | |
236 | uint32 *raster; | |
237 | ||
238 | TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w ); | |
239 | TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h ); | |
240 | ||
241 | npixels = w * h; | |
242 | ||
243 | raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) ); | |
244 | ||
245 | if (!raster) | |
246 | { | |
247 | if (verbose) | |
248 | wxLogError( _("TIFF: Couldn't allocate memory.") ); | |
249 | ||
250 | TIFFClose( tif ); | |
251 | ||
252 | return false; | |
253 | } | |
254 | ||
255 | image->Create( (int)w, (int)h ); | |
256 | if (!image->Ok()) | |
257 | { | |
258 | if (verbose) | |
259 | wxLogError( _("TIFF: Couldn't allocate memory.") ); | |
260 | ||
261 | _TIFFfree( raster ); | |
262 | TIFFClose( tif ); | |
263 | ||
264 | return false; | |
265 | } | |
266 | ||
267 | if (!TIFFReadRGBAImage( tif, w, h, raster, 0 )) | |
268 | { | |
269 | if (verbose) | |
270 | wxLogError( _("TIFF: Error reading image.") ); | |
271 | ||
272 | _TIFFfree( raster ); | |
273 | image->Destroy(); | |
274 | TIFFClose( tif ); | |
275 | ||
276 | return false; | |
277 | } | |
278 | ||
279 | bool hasmask = false; | |
280 | ||
281 | unsigned char *ptr = image->GetData(); | |
282 | ptr += w*3*(h-1); | |
283 | uint32 pos = 0; | |
284 | ||
285 | for (uint32 i = 0; i < h; i++) | |
286 | { | |
287 | for (uint32 j = 0; j < w; j++) | |
288 | { | |
289 | unsigned char alpha = (unsigned char)TIFFGetA(raster[pos]); | |
290 | if (alpha < 127) | |
291 | { | |
292 | hasmask = true; | |
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 | { | |
302 | ptr[0] = (unsigned char)TIFFGetR(raster[pos]); | |
303 | ptr++; | |
304 | ptr[0] = (unsigned char)TIFFGetG(raster[pos]); | |
305 | ptr++; | |
306 | ptr[0] = (unsigned char)TIFFGetB(raster[pos]); | |
307 | ptr++; | |
308 | } | |
309 | pos++; | |
310 | } | |
311 | ptr -= 2*w*3; // subtract line we just added plus one line | |
312 | } | |
313 | ||
314 | _TIFFfree( raster ); | |
315 | ||
316 | TIFFClose( tif ); | |
317 | ||
318 | image->SetMask( hasmask ); | |
319 | ||
320 | return true; | |
321 | } | |
322 | ||
323 | int wxTIFFHandler::GetImageCount( wxInputStream& stream ) | |
324 | { | |
325 | TIFF *tif = TIFFwxOpen( stream, "image", "r" ); | |
326 | ||
327 | if (!tif) | |
328 | return 0; | |
329 | ||
330 | int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here??? | |
331 | do { | |
332 | dircount++; | |
333 | } while (TIFFReadDirectory(tif)); | |
334 | ||
335 | TIFFClose( tif ); | |
336 | ||
337 | return dircount; | |
338 | } | |
339 | ||
340 | bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) | |
341 | { | |
342 | TIFF *tif = TIFFwxOpen( stream, "image", "w" ); | |
343 | ||
344 | if (!tif) | |
345 | { | |
346 | if (verbose) | |
347 | wxLogError( _("TIFF: Error saving image.") ); | |
348 | ||
349 | return false; | |
350 | } | |
351 | ||
352 | TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); | |
353 | TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)image->GetWidth()); | |
354 | TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight()); | |
355 | TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); | |
356 | TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); | |
357 | ||
358 | if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) && | |
359 | image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) ) | |
360 | { | |
361 | TIFFSetField(tif, TIFFTAG_XRESOLUTION, | |
362 | (float)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX)); | |
363 | TIFFSetField(tif, TIFFTAG_YRESOLUTION, | |
364 | (float)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY)); | |
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 | ||
391 | unsigned char *buf; | |
392 | ||
393 | if (TIFFScanlineSize(tif) > linebytes || (spp * bpp < 24)) | |
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 | ||
403 | return false; | |
404 | } | |
405 | } | |
406 | else | |
407 | { | |
408 | buf = NULL; | |
409 | } | |
410 | ||
411 | TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,TIFFDefaultStripSize(tif, (uint32) -1)); | |
412 | ||
413 | unsigned char *ptr = image->GetData(); | |
414 | for ( int row = 0; row < image->GetHeight(); row++ ) | |
415 | { | |
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; | |
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 | } | |
435 | } | |
436 | ||
437 | buf[column] = reverse; | |
438 | } | |
439 | } | |
440 | } | |
441 | ||
442 | if ( TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0 ) | |
443 | { | |
444 | if (verbose) | |
445 | wxLogError( _("TIFF: Error writing image.") ); | |
446 | ||
447 | TIFFClose( tif ); | |
448 | if (buf) | |
449 | _TIFFfree(buf); | |
450 | ||
451 | return false; | |
452 | } | |
453 | ||
454 | ptr += image->GetWidth()*3; | |
455 | } | |
456 | ||
457 | (void) TIFFClose(tif); | |
458 | ||
459 | if (buf) | |
460 | _TIFFfree(buf); | |
461 | ||
462 | return true; | |
463 | } | |
464 | ||
465 | bool wxTIFFHandler::DoCanRead( wxInputStream& stream ) | |
466 | { | |
467 | unsigned char hdr[2]; | |
468 | ||
469 | if ( !stream.Read(&hdr[0], WXSIZEOF(hdr)) ) | |
470 | return false; | |
471 | ||
472 | return (hdr[0] == 'I' && hdr[1] == 'I') || | |
473 | (hdr[0] == 'M' && hdr[1] == 'M'); | |
474 | } | |
475 | ||
476 | #endif // wxUSE_STREAMS | |
477 | ||
478 | #endif // wxUSE_LIBTIFF | |
479 |