]>
Commit | Line | Data |
---|---|---|
257bcf28 | 1 | ///////////////////////////////////////////////////////////////////////////// |
38d4b1e4 | 2 | // Name: src/common/imagtiff.cpp |
8f493002 VS |
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 | ||
426272a3 VZ |
10 | // ============================================================================ |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
14 | // ---------------------------------------------------------------------------- | |
15 | // headers | |
16 | // ---------------------------------------------------------------------------- | |
17 | ||
257bcf28 RR |
18 | // For compilers that support precompilation, includes "wx.h". |
19 | #include "wx/wxprec.h" | |
20 | ||
21 | #ifdef __BORLANDC__ | |
8898456d | 22 | #pragma hdrstop |
257bcf28 RR |
23 | #endif |
24 | ||
c96ea657 | 25 | #if wxUSE_IMAGE && wxUSE_LIBTIFF |
257bcf28 | 26 | |
88a7a4e1 | 27 | #include "wx/imagtiff.h" |
ccec9093 | 28 | #include "wx/versioninfo.h" |
88a7a4e1 | 29 | |
8898456d WS |
30 | #ifndef WX_PRECOMP |
31 | #include "wx/log.h" | |
32 | #include "wx/app.h" | |
88a7a4e1 | 33 | #include "wx/intl.h" |
0bca0373 | 34 | #include "wx/bitmap.h" |
02761f6c | 35 | #include "wx/module.h" |
34deb5cd | 36 | #include "wx/wxcrtvararg.h" |
8898456d WS |
37 | #endif |
38 | ||
257bcf28 RR |
39 | extern "C" |
40 | { | |
8df068ca CE |
41 | #ifdef __DMC__ |
42 | #include "tif_config.h" | |
426272a3 | 43 | #endif |
257bcf28 RR |
44 | #include "tiff.h" |
45 | #include "tiffio.h" | |
257bcf28 RR |
46 | } |
47 | #include "wx/filefn.h" | |
48 | #include "wx/wfstream.h" | |
257bcf28 | 49 | |
0729bd19 | 50 | #ifndef TIFFLINKAGEMODE |
0bca0373 WS |
51 | #if defined(__WATCOMC__) && defined(__WXMGL__) |
52 | #define TIFFLINKAGEMODE cdecl | |
53 | #else | |
54 | #define TIFFLINKAGEMODE LINKAGEMODE | |
55 | #endif | |
19193a2c | 56 | #endif |
0729bd19 | 57 | |
426272a3 VZ |
58 | // ============================================================================ |
59 | // implementation | |
60 | // ============================================================================ | |
61 | ||
62 | // ---------------------------------------------------------------------------- | |
63 | // TIFF library error/warning handlers | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
34deb5cd VS |
66 | static wxString |
67 | FormatTiffMessage(const char *module, const char *fmt, va_list ap) | |
68 | { | |
69 | char buf[512]; | |
70 | if ( wxCRT_VsnprintfA(buf, WXSIZEOF(buf), fmt, ap) <= 0 ) | |
71 | { | |
72 | // this isn't supposed to happen, but if it does, it's better | |
73 | // than nothing | |
74 | strcpy(buf, "Incorrectly formatted TIFF message"); | |
75 | } | |
76 | buf[WXSIZEOF(buf)-1] = 0; // make sure it is always NULL-terminated | |
77 | ||
78 | wxString msg(buf); | |
79 | if ( module ) | |
80 | msg += wxString::Format(_(" (in module \"%s\")"), module); | |
81 | ||
82 | return msg; | |
83 | } | |
84 | ||
426272a3 VZ |
85 | extern "C" |
86 | { | |
87 | ||
88 | static void | |
34deb5cd | 89 | TIFFwxWarningHandler(const char* module, const char *fmt, va_list ap) |
426272a3 | 90 | { |
34deb5cd | 91 | wxLogWarning("%s", FormatTiffMessage(module, fmt, ap)); |
426272a3 VZ |
92 | } |
93 | ||
94 | static void | |
34deb5cd | 95 | TIFFwxErrorHandler(const char* module, const char *fmt, va_list ap) |
426272a3 | 96 | { |
34deb5cd | 97 | wxLogError("%s", FormatTiffMessage(module, fmt, ap)); |
426272a3 VZ |
98 | } |
99 | ||
100 | } // extern "C" | |
101 | ||
257bcf28 RR |
102 | //----------------------------------------------------------------------------- |
103 | // wxTIFFHandler | |
104 | //----------------------------------------------------------------------------- | |
105 | ||
257bcf28 | 106 | IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler) |
257bcf28 | 107 | |
426272a3 VZ |
108 | wxTIFFHandler::wxTIFFHandler() |
109 | { | |
110 | m_name = wxT("TIFF file"); | |
111 | m_extension = wxT("tif"); | |
ba4800d3 | 112 | m_altExtensions.Add(wxT("tiff")); |
426272a3 VZ |
113 | m_type = wxBITMAP_TYPE_TIF; |
114 | m_mime = wxT("image/tiff"); | |
115 | TIFFSetWarningHandler((TIFFErrorHandler) TIFFwxWarningHandler); | |
116 | TIFFSetErrorHandler((TIFFErrorHandler) TIFFwxErrorHandler); | |
117 | } | |
118 | ||
e30285ab VZ |
119 | #if wxUSE_STREAMS |
120 | ||
ec524671 VZ |
121 | // helper to translate our, possibly 64 bit, wxFileOffset to TIFF, always 32 |
122 | // bit, toff_t | |
123 | static toff_t wxFileOffsetToTIFF(wxFileOffset ofs) | |
124 | { | |
125 | if ( ofs == wxInvalidOffset ) | |
126 | return (toff_t)-1; | |
127 | ||
128 | toff_t tofs = wx_truncate_cast(toff_t, ofs); | |
38d4b1e4 | 129 | wxCHECK_MSG( (wxFileOffset)tofs == ofs, (toff_t)-1, |
9a83f860 | 130 | wxT("TIFF library doesn't support large files") ); |
ec524671 VZ |
131 | |
132 | return tofs; | |
133 | } | |
134 | ||
135 | // another helper to convert standard seek mode to our | |
136 | static wxSeekMode wxSeekModeFromTIFF(int whence) | |
137 | { | |
138 | switch ( whence ) | |
139 | { | |
140 | case SEEK_SET: | |
141 | return wxFromStart; | |
142 | ||
143 | case SEEK_CUR: | |
144 | return wxFromCurrent; | |
145 | ||
146 | case SEEK_END: | |
147 | return wxFromEnd; | |
148 | ||
149 | default: | |
150 | return wxFromCurrent; | |
151 | } | |
152 | } | |
153 | ||
90350682 VZ |
154 | extern "C" |
155 | { | |
156 | ||
157 | tsize_t TIFFLINKAGEMODE | |
46f36538 | 158 | wxTIFFNullProc(thandle_t WXUNUSED(handle), |
19193a2c KB |
159 | tdata_t WXUNUSED(buf), |
160 | tsize_t WXUNUSED(size)) | |
f6bcfd97 BP |
161 | { |
162 | return (tsize_t) -1; | |
163 | } | |
164 | ||
90350682 | 165 | tsize_t TIFFLINKAGEMODE |
46f36538 | 166 | wxTIFFReadProc(thandle_t handle, tdata_t buf, tsize_t size) |
257bcf28 RR |
167 | { |
168 | wxInputStream *stream = (wxInputStream*) handle; | |
169 | stream->Read( (void*) buf, (size_t) size ); | |
4a10ea8b | 170 | return wx_truncate_cast(tsize_t, stream->LastRead()); |
257bcf28 RR |
171 | } |
172 | ||
90350682 | 173 | tsize_t TIFFLINKAGEMODE |
46f36538 | 174 | wxTIFFWriteProc(thandle_t handle, tdata_t buf, tsize_t size) |
257bcf28 RR |
175 | { |
176 | wxOutputStream *stream = (wxOutputStream*) handle; | |
177 | stream->Write( (void*) buf, (size_t) size ); | |
4a10ea8b | 178 | return wx_truncate_cast(tsize_t, stream->LastWrite()); |
257bcf28 RR |
179 | } |
180 | ||
90350682 | 181 | toff_t TIFFLINKAGEMODE |
46f36538 | 182 | wxTIFFSeekIProc(thandle_t handle, toff_t off, int whence) |
257bcf28 RR |
183 | { |
184 | wxInputStream *stream = (wxInputStream*) handle; | |
13111b2a | 185 | |
ec524671 VZ |
186 | return wxFileOffsetToTIFF(stream->SeekI((wxFileOffset)off, |
187 | wxSeekModeFromTIFF(whence))); | |
257bcf28 RR |
188 | } |
189 | ||
90350682 | 190 | toff_t TIFFLINKAGEMODE |
46f36538 | 191 | wxTIFFSeekOProc(thandle_t handle, toff_t off, int whence) |
f6bcfd97 BP |
192 | { |
193 | wxOutputStream *stream = (wxOutputStream*) handle; | |
f6bcfd97 | 194 | |
ec524671 VZ |
195 | return wxFileOffsetToTIFF(stream->SeekO((wxFileOffset)off, |
196 | wxSeekModeFromTIFF(whence))); | |
f6bcfd97 BP |
197 | } |
198 | ||
90350682 | 199 | int TIFFLINKAGEMODE |
46f36538 | 200 | wxTIFFCloseIProc(thandle_t WXUNUSED(handle)) |
257bcf28 | 201 | { |
be0d315e VZ |
202 | // there is no need to close the input stream |
203 | return 0; | |
204 | } | |
205 | ||
206 | int TIFFLINKAGEMODE | |
46f36538 | 207 | wxTIFFCloseOProc(thandle_t handle) |
be0d315e VZ |
208 | { |
209 | wxOutputStream *stream = (wxOutputStream*) handle; | |
210 | ||
211 | return stream->Close() ? 0 : -1; | |
257bcf28 RR |
212 | } |
213 | ||
90350682 | 214 | toff_t TIFFLINKAGEMODE |
46f36538 | 215 | wxTIFFSizeProc(thandle_t handle) |
257bcf28 | 216 | { |
f6bcfd97 | 217 | wxStreamBase *stream = (wxStreamBase*) handle; |
0b72db08 | 218 | return (toff_t) stream->GetSize(); |
257bcf28 RR |
219 | } |
220 | ||
90350682 | 221 | int TIFFLINKAGEMODE |
46f36538 | 222 | wxTIFFMapProc(thandle_t WXUNUSED(handle), |
13111b2a VZ |
223 | tdata_t* WXUNUSED(pbase), |
224 | toff_t* WXUNUSED(psize)) | |
257bcf28 RR |
225 | { |
226 | return 0; | |
227 | } | |
228 | ||
90350682 | 229 | void TIFFLINKAGEMODE |
46f36538 | 230 | wxTIFFUnmapProc(thandle_t WXUNUSED(handle), |
13111b2a VZ |
231 | tdata_t WXUNUSED(base), |
232 | toff_t WXUNUSED(size)) | |
257bcf28 RR |
233 | { |
234 | } | |
235 | ||
90350682 VZ |
236 | } // extern "C" |
237 | ||
257bcf28 RR |
238 | TIFF* |
239 | TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode) | |
240 | { | |
241 | TIFF* tif = TIFFClientOpen(name, mode, | |
242 | (thandle_t) &stream, | |
46f36538 VZ |
243 | wxTIFFReadProc, wxTIFFNullProc, |
244 | wxTIFFSeekIProc, wxTIFFCloseIProc, wxTIFFSizeProc, | |
245 | wxTIFFMapProc, wxTIFFUnmapProc); | |
257bcf28 | 246 | |
257bcf28 RR |
247 | return tif; |
248 | } | |
249 | ||
f6bcfd97 BP |
250 | TIFF* |
251 | TIFFwxOpen(wxOutputStream &stream, const char* name, const char* mode) | |
252 | { | |
253 | TIFF* tif = TIFFClientOpen(name, mode, | |
254 | (thandle_t) &stream, | |
46f36538 VZ |
255 | wxTIFFNullProc, wxTIFFWriteProc, |
256 | wxTIFFSeekOProc, wxTIFFCloseOProc, wxTIFFSizeProc, | |
257 | wxTIFFMapProc, wxTIFFUnmapProc); | |
f6bcfd97 BP |
258 | |
259 | return tif; | |
260 | } | |
257bcf28 | 261 | |
700ec454 | 262 | bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index ) |
257bcf28 | 263 | { |
60d43ad8 VS |
264 | if (index == -1) |
265 | index = 0; | |
266 | ||
257bcf28 | 267 | image->Destroy(); |
13111b2a | 268 | |
0b72db08 | 269 | TIFF *tif = TIFFwxOpen( stream, "image", "r" ); |
13111b2a | 270 | |
257bcf28 RR |
271 | if (!tif) |
272 | { | |
273 | if (verbose) | |
af588446 | 274 | { |
58c837a4 | 275 | wxLogError( _("TIFF: Error loading image.") ); |
af588446 | 276 | } |
13111b2a | 277 | |
7beb59f3 | 278 | return false; |
257bcf28 | 279 | } |
13111b2a | 280 | |
700ec454 RR |
281 | if (!TIFFSetDirectory( tif, (tdir_t)index )) |
282 | { | |
283 | if (verbose) | |
af588446 | 284 | { |
700ec454 | 285 | wxLogError( _("Invalid TIFF image index.") ); |
af588446 | 286 | } |
13111b2a | 287 | |
700ec454 | 288 | TIFFClose( tif ); |
13111b2a | 289 | |
7beb59f3 | 290 | return false; |
700ec454 | 291 | } |
257bcf28 RR |
292 | |
293 | uint32 w, h; | |
257bcf28 | 294 | uint32 *raster; |
13111b2a | 295 | |
257bcf28 RR |
296 | TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w ); |
297 | TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h ); | |
13111b2a | 298 | |
3d59540f DS |
299 | uint16 photometric; |
300 | uint16 samplesPerPixel; | |
b6ac40dc VS |
301 | uint16 extraSamples; |
302 | uint16* samplesInfo; | |
3d59540f | 303 | TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesPerPixel); |
b6ac40dc VS |
304 | TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, |
305 | &extraSamples, &samplesInfo); | |
3d59540f DS |
306 | if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) |
307 | { | |
308 | photometric = PHOTOMETRIC_MINISWHITE; | |
309 | } | |
310 | const bool hasAlpha = (extraSamples >= 1 | |
311 | && ((samplesInfo[0] == EXTRASAMPLE_UNSPECIFIED && samplesPerPixel > 3) | |
312 | || samplesInfo[0] == EXTRASAMPLE_ASSOCALPHA | |
313 | || samplesInfo[0] == EXTRASAMPLE_UNASSALPHA)) | |
314 | || (extraSamples == 0 && samplesPerPixel == 4 | |
315 | && photometric == PHOTOMETRIC_RGB); | |
b6ac40dc | 316 | |
5beedebb VZ |
317 | // guard against integer overflow during multiplication which could result |
318 | // in allocating a too small buffer and then overflowing it | |
ca588225 | 319 | const double bytesNeeded = (double)w * (double)h * sizeof(uint32); |
5beedebb VZ |
320 | if ( bytesNeeded >= wxUINT32_MAX ) |
321 | { | |
322 | if ( verbose ) | |
af588446 | 323 | { |
5beedebb | 324 | wxLogError( _("TIFF: Image size is abnormally big.") ); |
af588446 | 325 | } |
5beedebb VZ |
326 | |
327 | TIFFClose(tif); | |
328 | ||
329 | return false; | |
330 | } | |
13111b2a | 331 | |
8f2a8de6 | 332 | raster = (uint32*) _TIFFmalloc( (uint32)bytesNeeded ); |
13111b2a | 333 | |
257bcf28 RR |
334 | if (!raster) |
335 | { | |
336 | if (verbose) | |
af588446 | 337 | { |
58c837a4 | 338 | wxLogError( _("TIFF: Couldn't allocate memory.") ); |
af588446 | 339 | } |
38755449 | 340 | |
f6bcfd97 | 341 | TIFFClose( tif ); |
13111b2a | 342 | |
7beb59f3 | 343 | return false; |
257bcf28 RR |
344 | } |
345 | ||
479cd5de | 346 | image->Create( (int)w, (int)h ); |
13111b2a | 347 | if (!image->Ok()) |
257bcf28 RR |
348 | { |
349 | if (verbose) | |
af588446 | 350 | { |
58c837a4 | 351 | wxLogError( _("TIFF: Couldn't allocate memory.") ); |
af588446 | 352 | } |
13111b2a VZ |
353 | |
354 | _TIFFfree( raster ); | |
f6bcfd97 | 355 | TIFFClose( tif ); |
13111b2a | 356 | |
7beb59f3 | 357 | return false; |
257bcf28 | 358 | } |
13111b2a | 359 | |
b6ac40dc VS |
360 | if ( hasAlpha ) |
361 | image->SetAlpha(); | |
362 | ||
257bcf28 RR |
363 | if (!TIFFReadRGBAImage( tif, w, h, raster, 0 )) |
364 | { | |
365 | if (verbose) | |
af588446 | 366 | { |
58c837a4 | 367 | wxLogError( _("TIFF: Error reading image.") ); |
af588446 | 368 | } |
13111b2a VZ |
369 | |
370 | _TIFFfree( raster ); | |
371 | image->Destroy(); | |
f6bcfd97 | 372 | TIFFClose( tif ); |
13111b2a | 373 | |
7beb59f3 | 374 | return false; |
257bcf28 | 375 | } |
13111b2a | 376 | |
257bcf28 | 377 | unsigned char *ptr = image->GetData(); |
5c5ab9eb | 378 | ptr += w*3*(h-1); |
b6ac40dc VS |
379 | |
380 | unsigned char *alpha = hasAlpha ? image->GetAlpha() : NULL; | |
381 | if ( hasAlpha ) | |
382 | alpha += w*(h-1); | |
383 | ||
257bcf28 | 384 | uint32 pos = 0; |
13111b2a | 385 | |
257bcf28 RR |
386 | for (uint32 i = 0; i < h; i++) |
387 | { | |
ff7c6c9c | 388 | for (uint32 j = 0; j < w; j++) |
13111b2a | 389 | { |
b6ac40dc VS |
390 | *(ptr++) = (unsigned char)TIFFGetR(raster[pos]); |
391 | *(ptr++) = (unsigned char)TIFFGetG(raster[pos]); | |
392 | *(ptr++) = (unsigned char)TIFFGetB(raster[pos]); | |
393 | if ( hasAlpha ) | |
394 | *(alpha++) = (unsigned char)TIFFGetA(raster[pos]); | |
395 | ||
13111b2a VZ |
396 | pos++; |
397 | } | |
b6ac40dc VS |
398 | |
399 | // subtract line we just added plus one line: | |
400 | ptr -= 2*w*3; | |
401 | if ( hasAlpha ) | |
402 | alpha -= 2*w; | |
257bcf28 | 403 | } |
13111b2a | 404 | |
37ba70a5 VZ |
405 | // set the image resolution if it's available |
406 | uint16 tiffRes; | |
407 | if ( TIFFGetField(tif, TIFFTAG_RESOLUTIONUNIT, &tiffRes) ) | |
408 | { | |
409 | wxImageResolution res; | |
410 | switch ( tiffRes ) | |
411 | { | |
412 | default: | |
413 | wxLogWarning(_("Unknown TIFF resolution unit %d ignored"), | |
414 | tiffRes); | |
415 | // fall through | |
416 | ||
417 | case RESUNIT_NONE: | |
418 | res = wxIMAGE_RESOLUTION_NONE; | |
419 | break; | |
420 | ||
421 | case RESUNIT_INCH: | |
422 | res = wxIMAGE_RESOLUTION_INCHES; | |
423 | break; | |
424 | ||
425 | case RESUNIT_CENTIMETER: | |
426 | res = wxIMAGE_RESOLUTION_CM; | |
427 | break; | |
428 | } | |
429 | ||
430 | if ( res != wxIMAGE_RESOLUTION_NONE ) | |
431 | { | |
432 | float xres, yres; | |
433 | if ( TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres) ) | |
ad91e1ad | 434 | image->SetOption(wxIMAGE_OPTION_RESOLUTIONX, wxRound(xres)); |
37ba70a5 VZ |
435 | |
436 | if ( TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres) ) | |
ad91e1ad | 437 | image->SetOption(wxIMAGE_OPTION_RESOLUTIONY, wxRound(yres)); |
37ba70a5 VZ |
438 | } |
439 | } | |
440 | ||
441 | ||
257bcf28 | 442 | _TIFFfree( raster ); |
13111b2a | 443 | |
257bcf28 | 444 | TIFFClose( tif ); |
13111b2a | 445 | |
7beb59f3 | 446 | return true; |
257bcf28 RR |
447 | } |
448 | ||
8faef7cc | 449 | int wxTIFFHandler::DoGetImageCount( wxInputStream& stream ) |
700ec454 RR |
450 | { |
451 | TIFF *tif = TIFFwxOpen( stream, "image", "r" ); | |
13111b2a | 452 | |
700ec454 | 453 | if (!tif) |
13111b2a | 454 | return 0; |
257bcf28 | 455 | |
700ec454 RR |
456 | int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here??? |
457 | do { | |
458 | dircount++; | |
459 | } while (TIFFReadDirectory(tif)); | |
13111b2a | 460 | |
700ec454 | 461 | TIFFClose( tif ); |
03647350 | 462 | |
8faef7cc FM |
463 | // NOTE: this function modifies the current stream position but it's ok |
464 | // (see wxImageHandler::GetImageCount) | |
13111b2a | 465 | |
700ec454 RR |
466 | return dircount; |
467 | } | |
257bcf28 | 468 | |
f6bcfd97 | 469 | bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) |
257bcf28 | 470 | { |
f6bcfd97 BP |
471 | TIFF *tif = TIFFwxOpen( stream, "image", "w" ); |
472 | ||
473 | if (!tif) | |
474 | { | |
475 | if (verbose) | |
af588446 | 476 | { |
f6bcfd97 | 477 | wxLogError( _("TIFF: Error saving image.") ); |
af588446 | 478 | } |
f6bcfd97 | 479 | |
7beb59f3 | 480 | return false; |
f6bcfd97 BP |
481 | } |
482 | ||
fe9308c6 | 483 | TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); |
f6bcfd97 BP |
484 | TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)image->GetWidth()); |
485 | TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight()); | |
486 | TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); | |
f6bcfd97 | 487 | TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
38755449 | 488 | |
37ba70a5 VZ |
489 | // save the image resolution if we have it |
490 | int xres, yres; | |
491 | const wxImageResolution res = GetResolutionFromOptions(*image, &xres, &yres); | |
492 | uint16 tiffRes; | |
493 | switch ( res ) | |
fe9308c6 | 494 | { |
37ba70a5 | 495 | default: |
9a83f860 | 496 | wxFAIL_MSG( wxT("unknown image resolution units") ); |
37ba70a5 VZ |
497 | // fall through |
498 | ||
499 | case wxIMAGE_RESOLUTION_NONE: | |
500 | tiffRes = RESUNIT_NONE; | |
501 | break; | |
502 | ||
503 | case wxIMAGE_RESOLUTION_INCHES: | |
504 | tiffRes = RESUNIT_INCH; | |
505 | break; | |
506 | ||
507 | case wxIMAGE_RESOLUTION_CM: | |
508 | tiffRes = RESUNIT_CENTIMETER; | |
509 | break; | |
fe9308c6 VZ |
510 | } |
511 | ||
37ba70a5 VZ |
512 | if ( tiffRes != RESUNIT_NONE ) |
513 | { | |
514 | TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, tiffRes); | |
515 | TIFFSetField(tif, TIFFTAG_XRESOLUTION, (float)xres); | |
516 | TIFFSetField(tif, TIFFTAG_YRESOLUTION, (float)yres); | |
517 | } | |
518 | ||
519 | ||
fe9308c6 VZ |
520 | int spp = image->GetOptionInt(wxIMAGE_OPTION_SAMPLESPERPIXEL); |
521 | if ( !spp ) | |
522 | spp = 3; | |
523 | ||
524 | int bpp = image->GetOptionInt(wxIMAGE_OPTION_BITSPERSAMPLE); | |
525 | if ( !bpp ) | |
37ba70a5 | 526 | bpp = 8; |
fe9308c6 VZ |
527 | |
528 | int compression = image->GetOptionInt(wxIMAGE_OPTION_COMPRESSION); | |
529 | if ( !compression ) | |
f5e20985 VZ |
530 | { |
531 | // we can't use COMPRESSION_LZW because current version of libtiff | |
532 | // doesn't implement it ("no longer implemented due to Unisys patent | |
533 | // enforcement") and other compression methods are lossy so we | |
534 | // shouldn't use them by default -- and the only remaining one is none | |
535 | compression = COMPRESSION_NONE; | |
536 | } | |
fe9308c6 VZ |
537 | |
538 | TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp); | |
539 | TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bpp); | |
540 | TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, spp*bpp == 1 ? PHOTOMETRIC_MINISBLACK | |
541 | : PHOTOMETRIC_RGB); | |
542 | TIFFSetField(tif, TIFFTAG_COMPRESSION, compression); | |
543 | ||
544 | // scanlinesize if determined by spp and bpp | |
545 | tsize_t linebytes = (tsize_t)image->GetWidth() * spp * bpp / 8; | |
546 | ||
547 | if ( (image->GetWidth() % 8 > 0) && (spp * bpp < 8) ) | |
548 | linebytes+=1; | |
549 | ||
f6bcfd97 | 550 | unsigned char *buf; |
38755449 | 551 | |
fe9308c6 | 552 | if (TIFFScanlineSize(tif) > linebytes || (spp * bpp < 24)) |
f6bcfd97 BP |
553 | { |
554 | buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif)); | |
555 | if (!buf) | |
556 | { | |
557 | if (verbose) | |
af588446 | 558 | { |
f6bcfd97 | 559 | wxLogError( _("TIFF: Couldn't allocate memory.") ); |
af588446 | 560 | } |
f6bcfd97 BP |
561 | |
562 | TIFFClose( tif ); | |
563 | ||
7beb59f3 | 564 | return false; |
f6bcfd97 | 565 | } |
38755449 DW |
566 | } |
567 | else | |
f6bcfd97 BP |
568 | { |
569 | buf = NULL; | |
570 | } | |
571 | ||
fe9308c6 VZ |
572 | TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,TIFFDefaultStripSize(tif, (uint32) -1)); |
573 | ||
f6bcfd97 | 574 | unsigned char *ptr = image->GetData(); |
fe9308c6 | 575 | for ( int row = 0; row < image->GetHeight(); row++ ) |
f6bcfd97 | 576 | { |
fe9308c6 VZ |
577 | if ( buf ) |
578 | { | |
579 | if ( spp * bpp > 1 ) | |
580 | { | |
581 | // color image | |
582 | memcpy(buf, ptr, image->GetWidth()); | |
583 | } | |
584 | else // black and white image | |
585 | { | |
586 | for ( int column = 0; column < linebytes; column++ ) | |
587 | { | |
588 | uint8 reverse = 0; | |
fe9308c6 VZ |
589 | for ( int bp = 0; bp < 8; bp++ ) |
590 | { | |
591 | if ( ptr[column*24 + bp*3] > 0 ) | |
592 | { | |
593 | // check only red as this is sufficient | |
38d4b1e4 | 594 | reverse = (uint8)(reverse | 128 >> bp); |
fe9308c6 | 595 | } |
fe9308c6 VZ |
596 | } |
597 | ||
3ba9ea72 | 598 | buf[column] = reverse; |
fe9308c6 VZ |
599 | } |
600 | } | |
601 | } | |
38755449 | 602 | |
fe9308c6 | 603 | if ( TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0 ) |
f6bcfd97 | 604 | { |
19193a2c | 605 | if (verbose) |
af588446 | 606 | { |
19193a2c | 607 | wxLogError( _("TIFF: Error writing image.") ); |
af588446 | 608 | } |
38755449 | 609 | |
f6bcfd97 BP |
610 | TIFFClose( tif ); |
611 | if (buf) | |
612 | _TIFFfree(buf); | |
38755449 | 613 | |
7beb59f3 | 614 | return false; |
f6bcfd97 | 615 | } |
fe9308c6 | 616 | |
f6bcfd97 BP |
617 | ptr += image->GetWidth()*3; |
618 | } | |
619 | ||
620 | (void) TIFFClose(tif); | |
621 | ||
622 | if (buf) | |
fe9308c6 | 623 | _TIFFfree(buf); |
f6bcfd97 | 624 | |
7beb59f3 | 625 | return true; |
257bcf28 RR |
626 | } |
627 | ||
628 | bool wxTIFFHandler::DoCanRead( wxInputStream& stream ) | |
629 | { | |
0b72db08 | 630 | unsigned char hdr[2]; |
257bcf28 | 631 | |
8faef7cc | 632 | if ( !stream.Read(&hdr[0], WXSIZEOF(hdr)) ) // it's ok to modify the stream position here |
7beb59f3 | 633 | return false; |
79fa2374 | 634 | |
79fa2374 VZ |
635 | return (hdr[0] == 'I' && hdr[1] == 'I') || |
636 | (hdr[0] == 'M' && hdr[1] == 'M'); | |
257bcf28 RR |
637 | } |
638 | ||
e30285ab | 639 | #endif // wxUSE_STREAMS |
257bcf28 | 640 | |
ccec9093 VZ |
641 | /*static*/ wxVersionInfo wxTIFFHandler::GetLibraryVersionInfo() |
642 | { | |
643 | int major, | |
644 | minor, | |
645 | micro; | |
646 | ||
647 | const wxString ver(::TIFFGetVersion()); | |
9c1f960f | 648 | if ( wxSscanf(ver, "LIBTIFF, Version %d.%d.%d", &major, &minor, µ) != 3 ) |
ccec9093 VZ |
649 | { |
650 | wxLogDebug("Unrecognized libtiff version string \"%s\"", ver); | |
651 | ||
652 | major = | |
653 | minor = | |
654 | micro = 0; | |
655 | } | |
656 | ||
657 | wxString copyright; | |
658 | const wxString desc = ver.BeforeFirst('\n', ©right); | |
659 | copyright.Replace("\n", ""); | |
660 | ||
661 | return wxVersionInfo("libtiff", major, minor, micro, desc, copyright); | |
662 | } | |
663 | ||
e30285ab | 664 | #endif // wxUSE_LIBTIFF |