]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagtiff.cpp
removed obsolete docs
[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
8f493002
VS
10#ifdef __GNUG__
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
0729bd19 50static tsize_t TIFFLINKAGEMODE
f6bcfd97 51_tiffNullProc(thandle_t WXUNUSED(handle),
19193a2c
KB
52 tdata_t WXUNUSED(buf),
53 tsize_t WXUNUSED(size))
f6bcfd97
BP
54{
55 return (tsize_t) -1;
56}
57
0729bd19 58static tsize_t TIFFLINKAGEMODE
257bcf28
RR
59_tiffReadProc(thandle_t handle, tdata_t buf, tsize_t size)
60{
61 wxInputStream *stream = (wxInputStream*) handle;
62 stream->Read( (void*) buf, (size_t) size );
0b72db08 63 return stream->LastRead();
257bcf28
RR
64}
65
0729bd19 66static tsize_t TIFFLINKAGEMODE
257bcf28
RR
67_tiffWriteProc(thandle_t handle, tdata_t buf, tsize_t size)
68{
69 wxOutputStream *stream = (wxOutputStream*) handle;
70 stream->Write( (void*) buf, (size_t) size );
0b72db08 71 return stream->LastWrite();
257bcf28
RR
72}
73
0729bd19 74static toff_t TIFFLINKAGEMODE
f6bcfd97 75_tiffSeekIProc(thandle_t handle, toff_t off, int whence)
257bcf28
RR
76{
77 wxInputStream *stream = (wxInputStream*) handle;
78 wxSeekMode mode;
79 switch (whence)
80 {
81 case SEEK_SET: mode = wxFromStart; break;
82 case SEEK_CUR: mode = wxFromCurrent; break;
83 case SEEK_END: mode = wxFromEnd; break;
84 default: mode = wxFromCurrent; break;
85 }
13111b2a 86
257bcf28
RR
87 return (toff_t)stream->SeekI( (off_t)off, mode );
88}
89
0729bd19 90static toff_t TIFFLINKAGEMODE
f6bcfd97
BP
91_tiffSeekOProc(thandle_t handle, toff_t off, int whence)
92{
93 wxOutputStream *stream = (wxOutputStream*) handle;
94 wxSeekMode mode;
95 switch (whence)
96 {
97 case SEEK_SET: mode = wxFromStart; break;
98 case SEEK_CUR: mode = wxFromCurrent; break;
99 case SEEK_END: mode = wxFromEnd; break;
100 default: mode = wxFromCurrent; break;
101 }
102
103 return (toff_t)stream->SeekO( (off_t)off, mode );
104}
105
0729bd19 106static int TIFFLINKAGEMODE
257bcf28
RR
107_tiffCloseProc(thandle_t WXUNUSED(handle))
108{
109 return 0; // ?
110}
111
0729bd19 112static toff_t TIFFLINKAGEMODE
257bcf28
RR
113_tiffSizeProc(thandle_t handle)
114{
f6bcfd97 115 wxStreamBase *stream = (wxStreamBase*) handle;
0b72db08 116 return (toff_t) stream->GetSize();
257bcf28
RR
117}
118
0729bd19 119static int TIFFLINKAGEMODE
13111b2a
VZ
120_tiffMapProc(thandle_t WXUNUSED(handle),
121 tdata_t* WXUNUSED(pbase),
122 toff_t* WXUNUSED(psize))
257bcf28
RR
123{
124 return 0;
125}
126
0729bd19 127static void TIFFLINKAGEMODE
13111b2a
VZ
128_tiffUnmapProc(thandle_t WXUNUSED(handle),
129 tdata_t WXUNUSED(base),
130 toff_t WXUNUSED(size))
257bcf28
RR
131{
132}
133
134TIFF*
135TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode)
136{
137 TIFF* tif = TIFFClientOpen(name, mode,
138 (thandle_t) &stream,
f6bcfd97
BP
139 _tiffReadProc, _tiffNullProc,
140 _tiffSeekIProc, _tiffCloseProc, _tiffSizeProc,
257bcf28
RR
141 _tiffMapProc, _tiffUnmapProc);
142
257bcf28
RR
143 return tif;
144}
145
f6bcfd97
BP
146TIFF*
147TIFFwxOpen(wxOutputStream &stream, const char* name, const char* mode)
148{
149 TIFF* tif = TIFFClientOpen(name, mode,
150 (thandle_t) &stream,
151 _tiffNullProc, _tiffWriteProc,
152 _tiffSeekOProc, _tiffCloseProc, _tiffSizeProc,
153 _tiffMapProc, _tiffUnmapProc);
154
155 return tif;
156}
257bcf28 157
700ec454 158bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index )
257bcf28
RR
159{
160 image->Destroy();
13111b2a 161
0b72db08 162 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
13111b2a 163
257bcf28
RR
164 if (!tif)
165 {
166 if (verbose)
58c837a4 167 wxLogError( _("TIFF: Error loading image.") );
13111b2a
VZ
168
169 return FALSE;
257bcf28 170 }
13111b2a 171
700ec454
RR
172 if (!TIFFSetDirectory( tif, (tdir_t)index ))
173 {
174 if (verbose)
175 wxLogError( _("Invalid TIFF image index.") );
13111b2a 176
700ec454 177 TIFFClose( tif );
13111b2a
VZ
178
179 return FALSE;
700ec454 180 }
257bcf28
RR
181
182 uint32 w, h;
13111b2a 183 uint32 npixels;
257bcf28 184 uint32 *raster;
13111b2a 185
257bcf28
RR
186 TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
187 TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h );
13111b2a 188
257bcf28 189 npixels = w * h;
13111b2a 190
257bcf28 191 raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
13111b2a 192
257bcf28
RR
193 if (!raster)
194 {
195 if (verbose)
58c837a4 196 wxLogError( _("TIFF: Couldn't allocate memory.") );
38755449 197
f6bcfd97 198 TIFFClose( tif );
13111b2a
VZ
199
200 return FALSE;
257bcf28
RR
201 }
202
479cd5de 203 image->Create( (int)w, (int)h );
13111b2a 204 if (!image->Ok())
257bcf28
RR
205 {
206 if (verbose)
58c837a4 207 wxLogError( _("TIFF: Couldn't allocate memory.") );
13111b2a
VZ
208
209 _TIFFfree( raster );
f6bcfd97 210 TIFFClose( tif );
13111b2a 211
257bcf28
RR
212 return FALSE;
213 }
13111b2a 214
257bcf28
RR
215 if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
216 {
217 if (verbose)
58c837a4 218 wxLogError( _("TIFF: Error reading image.") );
13111b2a
VZ
219
220 _TIFFfree( raster );
221 image->Destroy();
f6bcfd97 222 TIFFClose( tif );
13111b2a
VZ
223
224 return FALSE;
257bcf28 225 }
13111b2a 226
257bcf28 227 bool hasmask = FALSE;
13111b2a 228
257bcf28 229 unsigned char *ptr = image->GetData();
5c5ab9eb 230 ptr += w*3*(h-1);
257bcf28 231 uint32 pos = 0;
13111b2a 232
257bcf28
RR
233 for (uint32 i = 0; i < h; i++)
234 {
ff7c6c9c 235 for (uint32 j = 0; j < w; j++)
13111b2a 236 {
5c5ab9eb 237 unsigned char alpha = (unsigned char)TIFFGetA(raster[pos]);
13111b2a
VZ
238 if (alpha < 127)
239 {
240 hasmask = TRUE;
241 ptr[0] = image->GetMaskRed();
242 ptr++;
243 ptr[0] = image->GetMaskGreen();
244 ptr++;
245 ptr[0] = image->GetMaskBlue();
246 ptr++;
247 }
248 else
249 {
5c5ab9eb 250 ptr[0] = (unsigned char)TIFFGetR(raster[pos]);
13111b2a 251 ptr++;
5c5ab9eb 252 ptr[0] = (unsigned char)TIFFGetG(raster[pos]);
13111b2a 253 ptr++;
5c5ab9eb 254 ptr[0] = (unsigned char)TIFFGetB(raster[pos]);
13111b2a
VZ
255 ptr++;
256 }
257 pos++;
258 }
5c5ab9eb 259 ptr -= 2*w*3; // subtract line we just added plus one line
257bcf28 260 }
13111b2a 261
257bcf28 262 _TIFFfree( raster );
13111b2a 263
257bcf28 264 TIFFClose( tif );
13111b2a 265
257bcf28 266 image->SetMask( hasmask );
13111b2a 267
257bcf28
RR
268 return TRUE;
269}
270
700ec454
RR
271int wxTIFFHandler::GetImageCount( wxInputStream& stream )
272{
273 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
13111b2a 274
700ec454 275 if (!tif)
13111b2a 276 return 0;
257bcf28 277
700ec454
RR
278 int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here???
279 do {
280 dircount++;
281 } while (TIFFReadDirectory(tif));
13111b2a 282
700ec454 283 TIFFClose( tif );
13111b2a 284
700ec454
RR
285 return dircount;
286}
257bcf28 287
f6bcfd97 288bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
257bcf28 289{
f6bcfd97
BP
290 TIFF *tif = TIFFwxOpen( stream, "image", "w" );
291
292 if (!tif)
293 {
294 if (verbose)
295 wxLogError( _("TIFF: Error saving image.") );
296
297 return FALSE;
298 }
299
300 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)image->GetWidth());
301 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight());
302 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
303 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
304 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
305 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
306 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
307 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
38755449 308
f6bcfd97
BP
309 tsize_t linebytes = (tsize_t)image->GetWidth() * 3;
310 unsigned char *buf;
38755449
DW
311
312 if (TIFFScanlineSize(tif) > linebytes)
f6bcfd97
BP
313 {
314 buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
315 if (!buf)
316 {
317 if (verbose)
318 wxLogError( _("TIFF: Couldn't allocate memory.") );
319
320 TIFFClose( tif );
321
322 return FALSE;
323 }
38755449
DW
324 }
325 else
f6bcfd97
BP
326 {
327 buf = NULL;
328 }
329
330 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
331 TIFFDefaultStripSize(tif, (uint32) -1));
38755449 332
f6bcfd97 333 unsigned char *ptr = image->GetData();
38755449 334 for (int row = 0; row < image->GetHeight(); row++)
f6bcfd97 335 {
19193a2c
KB
336 if (buf)
337 memcpy(buf, ptr, image->GetWidth());
38755449 338
19193a2c 339 if (TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0)
f6bcfd97 340 {
19193a2c
KB
341 if (verbose)
342 wxLogError( _("TIFF: Error writing image.") );
38755449 343
f6bcfd97
BP
344 TIFFClose( tif );
345 if (buf)
346 _TIFFfree(buf);
38755449 347
f6bcfd97
BP
348 return FALSE;
349 }
350 ptr += image->GetWidth()*3;
351 }
352
353 (void) TIFFClose(tif);
354
355 if (buf)
356 _TIFFfree(buf);
357
358 return TRUE;
257bcf28
RR
359}
360
361bool wxTIFFHandler::DoCanRead( wxInputStream& stream )
362{
0b72db08 363 unsigned char hdr[2];
257bcf28 364
0b72db08
RR
365 stream.Read(&hdr, 2);
366 stream.SeekI(-2, wxFromCurrent);
13111b2a 367
0b72db08
RR
368 return ((hdr[0] == 0x49 && hdr[1] == 0x49) ||
369 (hdr[0] == 0x4D && hdr[1] == 0x4D));
257bcf28
RR
370}
371
372
373#endif
374 // wxUSE_LIBTIFF
375
376