]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/imagtiff.cpp
OS/2 NativeFontInfo support
[wxWidgets.git] / src / common / imagtiff.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: imagtiff.cpp
3// Purpose: wxImage TIFF handler
4// Author: Robert Roebling
5// RCS-ID: $Id$
6// Copyright: (c) Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "imagtiff.h"
12#endif
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
23#if wxUSE_IMAGE && wxUSE_LIBTIFF
24
25#include "wx/imagtiff.h"
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"
34}
35#include "wx/filefn.h"
36#include "wx/wfstream.h"
37#include "wx/intl.h"
38#include "wx/module.h"
39
40#ifndef TIFFLINKAGEMODE
41 #define TIFFLINKAGEMODE LINKAGEMODE
42#endif
43
44//-----------------------------------------------------------------------------
45// wxTIFFHandler
46//-----------------------------------------------------------------------------
47
48IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler)
49
50static tsize_t TIFFLINKAGEMODE
51_tiffNullProc(thandle_t WXUNUSED(handle),
52 tdata_t WXUNUSED(buf),
53 tsize_t WXUNUSED(size))
54{
55 return (tsize_t) -1;
56}
57
58static tsize_t TIFFLINKAGEMODE
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 );
63 return stream->LastRead();
64}
65
66static tsize_t TIFFLINKAGEMODE
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 );
71 return stream->LastWrite();
72}
73
74static toff_t TIFFLINKAGEMODE
75_tiffSeekIProc(thandle_t handle, toff_t off, int whence)
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 }
86
87 return (toff_t)stream->SeekI( (off_t)off, mode );
88}
89
90static toff_t TIFFLINKAGEMODE
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
106static int TIFFLINKAGEMODE
107_tiffCloseProc(thandle_t WXUNUSED(handle))
108{
109 return 0; // ?
110}
111
112static toff_t TIFFLINKAGEMODE
113_tiffSizeProc(thandle_t handle)
114{
115 wxStreamBase *stream = (wxStreamBase*) handle;
116 return (toff_t) stream->GetSize();
117}
118
119static int TIFFLINKAGEMODE
120_tiffMapProc(thandle_t WXUNUSED(handle),
121 tdata_t* WXUNUSED(pbase),
122 toff_t* WXUNUSED(psize))
123{
124 return 0;
125}
126
127static void TIFFLINKAGEMODE
128_tiffUnmapProc(thandle_t WXUNUSED(handle),
129 tdata_t WXUNUSED(base),
130 toff_t WXUNUSED(size))
131{
132}
133
134TIFF*
135TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode)
136{
137 TIFF* tif = TIFFClientOpen(name, mode,
138 (thandle_t) &stream,
139 _tiffReadProc, _tiffNullProc,
140 _tiffSeekIProc, _tiffCloseProc, _tiffSizeProc,
141 _tiffMapProc, _tiffUnmapProc);
142
143 return tif;
144}
145
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}
157
158bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index )
159{
160 if (index == -1)
161 index = 0;
162
163 image->Destroy();
164
165 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
166
167 if (!tif)
168 {
169 if (verbose)
170 wxLogError( _("TIFF: Error loading image.") );
171
172 return FALSE;
173 }
174
175 if (!TIFFSetDirectory( tif, (tdir_t)index ))
176 {
177 if (verbose)
178 wxLogError( _("Invalid TIFF image index.") );
179
180 TIFFClose( tif );
181
182 return FALSE;
183 }
184
185 uint32 w, h;
186 uint32 npixels;
187 uint32 *raster;
188
189 TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
190 TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h );
191
192 npixels = w * h;
193
194 raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
195
196 if (!raster)
197 {
198 if (verbose)
199 wxLogError( _("TIFF: Couldn't allocate memory.") );
200
201 TIFFClose( tif );
202
203 return FALSE;
204 }
205
206 image->Create( (int)w, (int)h );
207 if (!image->Ok())
208 {
209 if (verbose)
210 wxLogError( _("TIFF: Couldn't allocate memory.") );
211
212 _TIFFfree( raster );
213 TIFFClose( tif );
214
215 return FALSE;
216 }
217
218 if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
219 {
220 if (verbose)
221 wxLogError( _("TIFF: Error reading image.") );
222
223 _TIFFfree( raster );
224 image->Destroy();
225 TIFFClose( tif );
226
227 return FALSE;
228 }
229
230 bool hasmask = FALSE;
231
232 unsigned char *ptr = image->GetData();
233 ptr += w*3*(h-1);
234 uint32 pos = 0;
235
236 for (uint32 i = 0; i < h; i++)
237 {
238 for (uint32 j = 0; j < w; j++)
239 {
240 unsigned char alpha = (unsigned char)TIFFGetA(raster[pos]);
241 if (alpha < 127)
242 {
243 hasmask = TRUE;
244 ptr[0] = image->GetMaskRed();
245 ptr++;
246 ptr[0] = image->GetMaskGreen();
247 ptr++;
248 ptr[0] = image->GetMaskBlue();
249 ptr++;
250 }
251 else
252 {
253 ptr[0] = (unsigned char)TIFFGetR(raster[pos]);
254 ptr++;
255 ptr[0] = (unsigned char)TIFFGetG(raster[pos]);
256 ptr++;
257 ptr[0] = (unsigned char)TIFFGetB(raster[pos]);
258 ptr++;
259 }
260 pos++;
261 }
262 ptr -= 2*w*3; // subtract line we just added plus one line
263 }
264
265 _TIFFfree( raster );
266
267 TIFFClose( tif );
268
269 image->SetMask( hasmask );
270
271 return TRUE;
272}
273
274int wxTIFFHandler::GetImageCount( wxInputStream& stream )
275{
276 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
277
278 if (!tif)
279 return 0;
280
281 int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here???
282 do {
283 dircount++;
284 } while (TIFFReadDirectory(tif));
285
286 TIFFClose( tif );
287
288 return dircount;
289}
290
291bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
292{
293 TIFF *tif = TIFFwxOpen( stream, "image", "w" );
294
295 if (!tif)
296 {
297 if (verbose)
298 wxLogError( _("TIFF: Error saving image.") );
299
300 return FALSE;
301 }
302
303 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32)image->GetWidth());
304 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32)image->GetHeight());
305 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
306 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
307 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
308 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
309 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
310 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
311
312 tsize_t linebytes = (tsize_t)image->GetWidth() * 3;
313 unsigned char *buf;
314
315 if (TIFFScanlineSize(tif) > linebytes)
316 {
317 buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
318 if (!buf)
319 {
320 if (verbose)
321 wxLogError( _("TIFF: Couldn't allocate memory.") );
322
323 TIFFClose( tif );
324
325 return FALSE;
326 }
327 }
328 else
329 {
330 buf = NULL;
331 }
332
333 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
334 TIFFDefaultStripSize(tif, (uint32) -1));
335
336 unsigned char *ptr = image->GetData();
337 for (int row = 0; row < image->GetHeight(); row++)
338 {
339 if (buf)
340 memcpy(buf, ptr, image->GetWidth());
341
342 if (TIFFWriteScanline(tif, buf ? buf : ptr, (uint32)row, 0) < 0)
343 {
344 if (verbose)
345 wxLogError( _("TIFF: Error writing image.") );
346
347 TIFFClose( tif );
348 if (buf)
349 _TIFFfree(buf);
350
351 return FALSE;
352 }
353 ptr += image->GetWidth()*3;
354 }
355
356 (void) TIFFClose(tif);
357
358 if (buf)
359 _TIFFfree(buf);
360
361 return TRUE;
362}
363
364bool wxTIFFHandler::DoCanRead( wxInputStream& stream )
365{
366 unsigned char hdr[2];
367
368 stream.Read(&hdr, 2);
369 stream.SeekI(-2, wxFromCurrent);
370
371 return ((hdr[0] == 0x49 && hdr[1] == 0x49) ||
372 (hdr[0] == 0x4D && hdr[1] == 0x4D));
373}
374
375
376#endif
377 // wxUSE_LIBTIFF
378
379