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