]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagtiff.cpp
Remove all extensions before inport, not just .py
[wxWidgets.git] / src / common / imagtiff.cpp
CommitLineData
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
257bcf28
RR
7// Licence: wxWindows licence
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"
30extern "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 48IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler)
257bcf28 49
e30285ab
VZ
50#if wxUSE_STREAMS
51
90350682
VZ
52extern "C"
53{
54
55tsize_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 63tsize_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 71tsize_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 79toff_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
257bcf28
RR
92 return (toff_t)stream->SeekI( (off_t)off, mode );
93}
94
90350682 95toff_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
108 return (toff_t)stream->SeekO( (off_t)off, mode );
109}
110
90350682 111int TIFFLINKAGEMODE
257bcf28
RR
112_tiffCloseProc(thandle_t WXUNUSED(handle))
113{
114 return 0; // ?
115}
116
90350682 117toff_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 124int 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 132void 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
139static void
140TIFFwxWarningHandler(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}
146
147static void
148TIFFwxErrorHandler(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
157TIFF*
158TIFFwxOpen(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
169TIFF*
170TIFFwxOpen(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
181wxTIFFHandler::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 191bool 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
VZ
204
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
VZ
214
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
VZ
235
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
257bcf28
RR
248 return FALSE;
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
VZ
259
260 return FALSE;
257bcf28 261 }
13111b2a 262
257bcf28 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 {
276 hasmask = TRUE;
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
257bcf28
RR
304 return TRUE;
305}
306
649d13e8 307int 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 324bool 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
333 return FALSE;
334 }
335
336 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)image->GetWidth());
337 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight());
338 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
339 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
340 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
341 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
342 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
343 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
38755449 344
f6bcfd97
BP
345 tsize_t linebytes = (tsize_t)image->GetWidth() * 3;
346 unsigned char *buf;
38755449
DW
347
348 if (TIFFScanlineSize(tif) > linebytes)
f6bcfd97
BP
349 {
350 buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
351 if (!buf)
352 {
353 if (verbose)
354 wxLogError( _("TIFF: Couldn't allocate memory.") );
355
356 TIFFClose( tif );
357
358 return FALSE;
359 }
38755449
DW
360 }
361 else
f6bcfd97
BP
362 {
363 buf = NULL;
364 }
365
366 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
367 TIFFDefaultStripSize(tif, (uint32) -1));
38755449 368
f6bcfd97 369 unsigned char *ptr = image->GetData();
38755449 370 for (int row = 0; row < image->GetHeight(); row++)
f6bcfd97 371 {
19193a2c
KB
372 if (buf)
373 memcpy(buf, ptr, image->GetWidth());
38755449 374
19193a2c 375 if (TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0)
f6bcfd97 376 {
19193a2c
KB
377 if (verbose)
378 wxLogError( _("TIFF: Error writing image.") );
38755449 379
f6bcfd97
BP
380 TIFFClose( tif );
381 if (buf)
382 _TIFFfree(buf);
38755449 383
f6bcfd97
BP
384 return FALSE;
385 }
386 ptr += image->GetWidth()*3;
387 }
388
389 (void) TIFFClose(tif);
390
391 if (buf)
392 _TIFFfree(buf);
393
394 return TRUE;
257bcf28
RR
395}
396
397bool wxTIFFHandler::DoCanRead( wxInputStream& stream )
398{
0b72db08 399 unsigned char hdr[2];
257bcf28 400
79fa2374
VZ
401 if ( !stream.Read(&hdr, WXSIZEOF(hdr)) )
402 return FALSE;
403
79fa2374
VZ
404 return (hdr[0] == 'I' && hdr[1] == 'I') ||
405 (hdr[0] == 'M' && hdr[1] == 'M');
257bcf28
RR
406}
407
e30285ab 408#endif // wxUSE_STREAMS
257bcf28 409
e30285ab 410#endif // wxUSE_LIBTIFF
257bcf28 411