]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagtiff.cpp
ed5b06bc76bbca8c01d7b764d5d68b25451a21f1
[wxWidgets.git] / src / common / imagtiff.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: imagjpeg.cpp
3 // Purpose: wxImage JPEG handler
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 /*
11 We don't put pragma implement in this file because it is already present in
12 src/common/image.cpp
13 */
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #include "wx/defs.h"
23
24 #if wxUSE_LIBTIFF
25
26 #include "wx/image.h"
27 #include "wx/bitmap.h"
28 #include "wx/debug.h"
29 #include "wx/log.h"
30 #include "wx/app.h"
31 extern "C"
32 {
33 #include "tiff.h"
34 #include "tiffio.h"
35 #include "tiffiop.h"
36 }
37 #include "wx/filefn.h"
38 #include "wx/wfstream.h"
39 #include "wx/intl.h"
40 #include "wx/module.h"
41
42 //-----------------------------------------------------------------------------
43 // wxTIFFHandler
44 //-----------------------------------------------------------------------------
45
46 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler)
47
48 static tsize_t
49 _tiffReadProc(thandle_t handle, tdata_t buf, tsize_t size)
50 {
51 wxInputStream *stream = (wxInputStream*) handle;
52 stream->Read( (void*) buf, (size_t) size );
53 return stream->LastRead();
54 }
55
56 static tsize_t
57 _tiffWriteProc(thandle_t handle, tdata_t buf, tsize_t size)
58 {
59 wxOutputStream *stream = (wxOutputStream*) handle;
60 stream->Write( (void*) buf, (size_t) size );
61 return stream->LastWrite();
62 }
63
64 static toff_t
65 _tiffSeekProc(thandle_t handle, toff_t off, int whence)
66 {
67 wxInputStream *stream = (wxInputStream*) handle;
68 wxSeekMode mode;
69 switch (whence)
70 {
71 case SEEK_SET: mode = wxFromStart; break;
72 case SEEK_CUR: mode = wxFromCurrent; break;
73 case SEEK_END: mode = wxFromEnd; break;
74 default: mode = wxFromCurrent; break;
75 }
76
77 return (toff_t)stream->SeekI( (off_t)off, mode );
78 }
79
80 static int
81 _tiffCloseProc(thandle_t WXUNUSED(handle))
82 {
83 return 0; // ?
84 }
85
86 static toff_t
87 _tiffSizeProc(thandle_t handle)
88 {
89 wxInputStream *stream = (wxInputStream*) handle;
90 return (toff_t) stream->GetSize();
91 }
92
93 static int
94 _tiffMapProc(thandle_t WXUNUSED(handle), tdata_t* pbase, toff_t* psize)
95 {
96 return 0;
97 }
98
99 static void
100 _tiffUnmapProc(thandle_t WXUNUSED(handle), tdata_t base, toff_t size)
101 {
102 }
103
104 TIFF*
105 TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode)
106 {
107 TIFF* tif = TIFFClientOpen(name, mode,
108 (thandle_t) &stream,
109 _tiffReadProc, _tiffWriteProc,
110 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
111 _tiffMapProc, _tiffUnmapProc);
112
113 if (tif)
114 tif->tif_fd = (int) &stream;
115
116 return tif;
117 }
118
119
120 bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index )
121 {
122 image->Destroy();
123
124 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
125
126 if (!tif)
127 {
128 if (verbose)
129 wxLogError( _("TIFF: Error loading image.") );
130
131 return FALSE;
132 }
133
134 if (!TIFFSetDirectory( tif, (tdir_t)index ))
135 {
136 if (verbose)
137 wxLogError( _("Invalid TIFF image index.") );
138
139 TIFFClose( tif );
140
141 return FALSE;
142 }
143
144 uint32 w, h;
145 size_t npixels;
146 uint32 *raster;
147
148 TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
149 TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h );
150
151 npixels = w * h;
152
153 raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
154
155 if (!raster)
156 {
157 if (verbose)
158 wxLogError( _("TIFF: Couldn't allocate memory.") );
159
160 return FALSE;
161 }
162
163 image->Create( w, h );
164 if (!image->Ok())
165 {
166 if (verbose)
167 wxLogError( _("TIFF: Couldn't allocate memory.") );
168
169 _TIFFfree( raster );
170
171 return FALSE;
172 }
173
174 if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
175 {
176 if (verbose)
177 wxLogError( _("TIFF: Error reading image.") );
178
179 _TIFFfree( raster );
180 image->Destroy();
181
182 return FALSE;
183 }
184
185 bool hasmask = FALSE;
186
187 unsigned char *ptr = image->GetData();
188 uint32 pos = 0;
189
190 for (uint32 i = 0; i < h; i++)
191 {
192 for (uint32 j = 0; w < h; j++)
193 {
194 unsigned char alpha = (unsigned char)(raster[pos] >> 24);
195 if (alpha < 127)
196 {
197 hasmask = TRUE;
198 ptr[0] = image->GetMaskRed();
199 ptr++;
200 ptr[0] = image->GetMaskGreen();
201 ptr++;
202 ptr[0] = image->GetMaskBlue();
203 ptr++;
204 }
205 else
206 {
207 ptr[0] = (unsigned char)(raster[pos] >> 16);
208 ptr++;
209 ptr[0] = (unsigned char)(raster[pos] >> 8);
210 ptr++;
211 ptr[0] = (unsigned char)(raster[pos]);
212 ptr++;
213 }
214 pos++;
215 }
216 }
217
218 _TIFFfree( raster );
219
220 TIFFClose( tif );
221
222 image->SetMask( hasmask );
223
224 return TRUE;
225 }
226
227 int wxTIFFHandler::GetImageCount( wxInputStream& stream )
228 {
229 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
230
231 if (!tif)
232 return 0;
233
234 int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here???
235 do {
236 dircount++;
237 } while (TIFFReadDirectory(tif));
238
239 TIFFClose( tif );
240
241 return dircount;
242 }
243
244 bool wxTIFFHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
245 {
246 return FALSE;
247 }
248
249 bool wxTIFFHandler::DoCanRead( wxInputStream& stream )
250 {
251 unsigned char hdr[2];
252
253 stream.Read(&hdr, 2);
254 stream.SeekI(-2, wxFromCurrent);
255
256 return ((hdr[0] == 0x49 && hdr[1] == 0x49) ||
257 (hdr[0] == 0x4D && hdr[1] == 0x4D));
258 }
259
260
261 #endif
262 // wxUSE_LIBTIFF
263
264
265
266
267
268