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