]>
Commit | Line | Data |
---|---|---|
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" | |
30 | extern "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 | 48 | IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler) |
257bcf28 | 49 | |
e30285ab VZ |
50 | #if wxUSE_STREAMS |
51 | ||
90350682 VZ |
52 | extern "C" |
53 | { | |
54 | ||
55 | tsize_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 | 63 | tsize_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 | 71 | tsize_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 | 79 | toff_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 | 95 | toff_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 | 111 | int TIFFLINKAGEMODE |
257bcf28 RR |
112 | _tiffCloseProc(thandle_t WXUNUSED(handle)) |
113 | { | |
114 | return 0; // ? | |
115 | } | |
116 | ||
90350682 | 117 | toff_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 | 124 | int 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 | 132 | void 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 JS |
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 | } | |
7beb59f3 | 146 | |
0fc7f695 JS |
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 | ||
90350682 VZ |
155 | } // extern "C" |
156 | ||
257bcf28 RR |
157 | TIFF* |
158 | TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode) | |
159 | { | |
160 | TIFF* tif = TIFFClientOpen(name, mode, | |
161 | (thandle_t) &stream, | |
f6bcfd97 BP |
162 | _tiffReadProc, _tiffNullProc, |
163 | _tiffSeekIProc, _tiffCloseProc, _tiffSizeProc, | |
257bcf28 RR |
164 | _tiffMapProc, _tiffUnmapProc); |
165 | ||
257bcf28 RR |
166 | return tif; |
167 | } | |
168 | ||
f6bcfd97 BP |
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 | } | |
257bcf28 | 180 | |
0fc7f695 JS |
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 | ||
700ec454 | 191 | bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index ) |
257bcf28 | 192 | { |
60d43ad8 VS |
193 | if (index == -1) |
194 | index = 0; | |
195 | ||
257bcf28 | 196 | image->Destroy(); |
13111b2a | 197 | |
0b72db08 | 198 | TIFF *tif = TIFFwxOpen( stream, "image", "r" ); |
13111b2a | 199 | |
257bcf28 RR |
200 | if (!tif) |
201 | { | |
202 | if (verbose) | |
58c837a4 | 203 | wxLogError( _("TIFF: Error loading image.") ); |
13111b2a | 204 | |
7beb59f3 | 205 | return false; |
257bcf28 | 206 | } |
13111b2a | 207 | |
700ec454 RR |
208 | if (!TIFFSetDirectory( tif, (tdir_t)index )) |
209 | { | |
210 | if (verbose) | |
211 | wxLogError( _("Invalid TIFF image index.") ); | |
13111b2a | 212 | |
700ec454 | 213 | TIFFClose( tif ); |
13111b2a | 214 | |
7beb59f3 | 215 | return false; |
700ec454 | 216 | } |
257bcf28 RR |
217 | |
218 | uint32 w, h; | |
13111b2a | 219 | uint32 npixels; |
257bcf28 | 220 | uint32 *raster; |
13111b2a | 221 | |
257bcf28 RR |
222 | TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w ); |
223 | TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h ); | |
13111b2a | 224 | |
257bcf28 | 225 | npixels = w * h; |
13111b2a | 226 | |
257bcf28 | 227 | raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) ); |
13111b2a | 228 | |
257bcf28 RR |
229 | if (!raster) |
230 | { | |
231 | if (verbose) | |
58c837a4 | 232 | wxLogError( _("TIFF: Couldn't allocate memory.") ); |
38755449 | 233 | |
f6bcfd97 | 234 | TIFFClose( tif ); |
13111b2a | 235 | |
7beb59f3 | 236 | return false; |
257bcf28 RR |
237 | } |
238 | ||
479cd5de | 239 | image->Create( (int)w, (int)h ); |
13111b2a | 240 | if (!image->Ok()) |
257bcf28 RR |
241 | { |
242 | if (verbose) | |
58c837a4 | 243 | wxLogError( _("TIFF: Couldn't allocate memory.") ); |
13111b2a VZ |
244 | |
245 | _TIFFfree( raster ); | |
f6bcfd97 | 246 | TIFFClose( tif ); |
13111b2a | 247 | |
7beb59f3 | 248 | return false; |
257bcf28 | 249 | } |
13111b2a | 250 | |
257bcf28 RR |
251 | if (!TIFFReadRGBAImage( tif, w, h, raster, 0 )) |
252 | { | |
253 | if (verbose) | |
58c837a4 | 254 | wxLogError( _("TIFF: Error reading image.") ); |
13111b2a VZ |
255 | |
256 | _TIFFfree( raster ); | |
257 | image->Destroy(); | |
f6bcfd97 | 258 | TIFFClose( tif ); |
13111b2a | 259 | |
7beb59f3 | 260 | return false; |
257bcf28 | 261 | } |
13111b2a | 262 | |
7beb59f3 | 263 | bool hasmask = false; |
13111b2a | 264 | |
257bcf28 | 265 | unsigned char *ptr = image->GetData(); |
5c5ab9eb | 266 | ptr += w*3*(h-1); |
257bcf28 | 267 | uint32 pos = 0; |
13111b2a | 268 | |
257bcf28 RR |
269 | for (uint32 i = 0; i < h; i++) |
270 | { | |
ff7c6c9c | 271 | for (uint32 j = 0; j < w; j++) |
13111b2a | 272 | { |
5c5ab9eb | 273 | unsigned char alpha = (unsigned char)TIFFGetA(raster[pos]); |
13111b2a VZ |
274 | if (alpha < 127) |
275 | { | |
7beb59f3 | 276 | hasmask = true; |
13111b2a VZ |
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 | { | |
5c5ab9eb | 286 | ptr[0] = (unsigned char)TIFFGetR(raster[pos]); |
13111b2a | 287 | ptr++; |
5c5ab9eb | 288 | ptr[0] = (unsigned char)TIFFGetG(raster[pos]); |
13111b2a | 289 | ptr++; |
5c5ab9eb | 290 | ptr[0] = (unsigned char)TIFFGetB(raster[pos]); |
13111b2a VZ |
291 | ptr++; |
292 | } | |
293 | pos++; | |
294 | } | |
5c5ab9eb | 295 | ptr -= 2*w*3; // subtract line we just added plus one line |
257bcf28 | 296 | } |
13111b2a | 297 | |
257bcf28 | 298 | _TIFFfree( raster ); |
13111b2a | 299 | |
257bcf28 | 300 | TIFFClose( tif ); |
13111b2a | 301 | |
257bcf28 | 302 | image->SetMask( hasmask ); |
13111b2a | 303 | |
7beb59f3 | 304 | return true; |
257bcf28 RR |
305 | } |
306 | ||
649d13e8 | 307 | int wxTIFFHandler::GetImageCount( wxInputStream& stream ) |
700ec454 RR |
308 | { |
309 | TIFF *tif = TIFFwxOpen( stream, "image", "r" ); | |
13111b2a | 310 | |
700ec454 | 311 | if (!tif) |
13111b2a | 312 | return 0; |
257bcf28 | 313 | |
700ec454 RR |
314 | int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here??? |
315 | do { | |
316 | dircount++; | |
317 | } while (TIFFReadDirectory(tif)); | |
13111b2a | 318 | |
700ec454 | 319 | TIFFClose( tif ); |
13111b2a | 320 | |
700ec454 RR |
321 | return dircount; |
322 | } | |
257bcf28 | 323 | |
f6bcfd97 | 324 | bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) |
257bcf28 | 325 | { |
f6bcfd97 BP |
326 | TIFF *tif = TIFFwxOpen( stream, "image", "w" ); |
327 | ||
328 | if (!tif) | |
329 | { | |
330 | if (verbose) | |
331 | wxLogError( _("TIFF: Error saving image.") ); | |
332 | ||
7beb59f3 | 333 | return false; |
f6bcfd97 BP |
334 | } |
335 | ||
fe9308c6 | 336 | TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); |
f6bcfd97 BP |
337 | TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)image->GetWidth()); |
338 | TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight()); | |
339 | TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); | |
f6bcfd97 | 340 | TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
38755449 | 341 | |
fe9308c6 VZ |
342 | if ( image->HasOption(wxIMAGE_OPTION_RESOLUTIONX) && |
343 | image->HasOption(wxIMAGE_OPTION_RESOLUTIONY) ) | |
344 | { | |
345 | TIFFSetField(tif, TIFFTAG_XRESOLUTION, | |
f78d506b | 346 | (float)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX)); |
fe9308c6 | 347 | TIFFSetField(tif, TIFFTAG_YRESOLUTION, |
f78d506b | 348 | (float)image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY)); |
fe9308c6 VZ |
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 | ||
f6bcfd97 | 375 | unsigned char *buf; |
38755449 | 376 | |
fe9308c6 | 377 | if (TIFFScanlineSize(tif) > linebytes || (spp * bpp < 24)) |
f6bcfd97 BP |
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 | ||
7beb59f3 | 387 | return false; |
f6bcfd97 | 388 | } |
38755449 DW |
389 | } |
390 | else | |
f6bcfd97 BP |
391 | { |
392 | buf = NULL; | |
393 | } | |
394 | ||
fe9308c6 VZ |
395 | TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,TIFFDefaultStripSize(tif, (uint32) -1)); |
396 | ||
f6bcfd97 | 397 | unsigned char *ptr = image->GetData(); |
fe9308c6 | 398 | for ( int row = 0; row < image->GetHeight(); row++ ) |
f6bcfd97 | 399 | { |
fe9308c6 VZ |
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; | |
fe9308c6 VZ |
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 | } | |
fe9308c6 VZ |
419 | } |
420 | ||
3ba9ea72 | 421 | buf[column] = reverse; |
fe9308c6 VZ |
422 | } |
423 | } | |
424 | } | |
38755449 | 425 | |
fe9308c6 | 426 | if ( TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0 ) |
f6bcfd97 | 427 | { |
19193a2c KB |
428 | if (verbose) |
429 | wxLogError( _("TIFF: Error writing image.") ); | |
38755449 | 430 | |
f6bcfd97 BP |
431 | TIFFClose( tif ); |
432 | if (buf) | |
433 | _TIFFfree(buf); | |
38755449 | 434 | |
7beb59f3 | 435 | return false; |
f6bcfd97 | 436 | } |
fe9308c6 | 437 | |
f6bcfd97 BP |
438 | ptr += image->GetWidth()*3; |
439 | } | |
440 | ||
441 | (void) TIFFClose(tif); | |
442 | ||
443 | if (buf) | |
fe9308c6 | 444 | _TIFFfree(buf); |
f6bcfd97 | 445 | |
7beb59f3 | 446 | return true; |
257bcf28 RR |
447 | } |
448 | ||
449 | bool wxTIFFHandler::DoCanRead( wxInputStream& stream ) | |
450 | { | |
0b72db08 | 451 | unsigned char hdr[2]; |
257bcf28 | 452 | |
7ac31c42 | 453 | if ( !stream.Read(&hdr[0], WXSIZEOF(hdr)) ) |
7beb59f3 | 454 | return false; |
79fa2374 | 455 | |
79fa2374 VZ |
456 | return (hdr[0] == 'I' && hdr[1] == 'I') || |
457 | (hdr[0] == 'M' && hdr[1] == 'M'); | |
257bcf28 RR |
458 | } |
459 | ||
e30285ab | 460 | #endif // wxUSE_STREAMS |
257bcf28 | 461 | |
e30285ab | 462 | #endif // wxUSE_LIBTIFF |
257bcf28 | 463 |