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