]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagtiff.cpp
added default ctor for wxDirDialog
[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 #if !USE_SHARED_LIBRARIES
47 IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler)
48 #endif
49
50 static tsize_t
51 _tiffReadProc(thandle_t handle, tdata_t buf, tsize_t size)
52 {
53 wxInputStream *stream = (wxInputStream*) handle;
54 stream->Read( (void*) buf, (size_t) size );
55 return stream->LastRead();
56 }
57
58 static tsize_t
59 _tiffWriteProc(thandle_t handle, tdata_t buf, tsize_t size)
60 {
61 wxOutputStream *stream = (wxOutputStream*) handle;
62 stream->Write( (void*) buf, (size_t) size );
63 return stream->LastWrite();
64 }
65
66 static toff_t
67 _tiffSeekProc(thandle_t handle, toff_t off, int whence)
68 {
69 wxInputStream *stream = (wxInputStream*) handle;
70 wxSeekMode mode;
71 switch (whence)
72 {
73 case SEEK_SET: mode = wxFromStart; break;
74 case SEEK_CUR: mode = wxFromCurrent; break;
75 case SEEK_END: mode = wxFromEnd; break;
76 default: mode = wxFromCurrent; break;
77 }
78
79 return (toff_t)stream->SeekI( (off_t)off, mode );
80 }
81
82 static int
83 _tiffCloseProc(thandle_t WXUNUSED(handle))
84 {
85 return 0; // ?
86 }
87
88 static toff_t
89 _tiffSizeProc(thandle_t handle)
90 {
91 wxInputStream *stream = (wxInputStream*) handle;
92 return (toff_t) stream->GetSize();
93 }
94
95 static int
96 _tiffMapProc(thandle_t WXUNUSED(handle), tdata_t* pbase, toff_t* psize)
97 {
98 return 0;
99 }
100
101 static void
102 _tiffUnmapProc(thandle_t WXUNUSED(handle), tdata_t base, toff_t size)
103 {
104 }
105
106 TIFF*
107 TIFFwxOpen(wxInputStream &stream, const char* name, const char* mode)
108 {
109 TIFF* tif = TIFFClientOpen(name, mode,
110 (thandle_t) &stream,
111 _tiffReadProc, _tiffWriteProc,
112 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
113 _tiffMapProc, _tiffUnmapProc);
114
115 if (tif)
116 tif->tif_fd = (int) &stream;
117
118 return tif;
119 }
120
121
122 bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index )
123 {
124 image->Destroy();
125
126 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
127
128 if (!tif)
129 {
130 if (verbose)
131 wxLogError( _("TIFF: Error loading image.") );
132
133 return FALSE;
134 }
135
136 if (!TIFFSetDirectory( tif, (tdir_t)index ))
137 {
138 if (verbose)
139 wxLogError( _("Invalid TIFF image index.") );
140
141 TIFFClose( tif );
142
143 return FALSE;
144 }
145
146 uint32 w, h;
147 size_t npixels;
148 uint32 *raster;
149
150 TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
151 TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h );
152
153 npixels = w * h;
154
155 raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
156
157 if (!raster)
158 {
159 if (verbose)
160 wxLogError( _("TIFF: Couldn't allocate memory.") );
161
162 return FALSE;
163 }
164
165 image->Create( w, h );
166 if (!image->Ok())
167 {
168 if (verbose)
169 wxLogError( _("TIFF: Couldn't allocate memory.") );
170
171 _TIFFfree( raster );
172
173 return FALSE;
174 }
175
176 if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
177 {
178 if (verbose)
179 wxLogError( _("TIFF: Error reading image.") );
180
181 _TIFFfree( raster );
182 image->Destroy();
183
184 return FALSE;
185 }
186
187 bool hasmask = FALSE;
188
189 unsigned char *ptr = image->GetData();
190 uint32 pos = 0;
191
192 for (uint32 i = 0; i < h; i++)
193 {
194 for (uint32 j = 0; w < h; j++)
195 {
196 unsigned char alpha = (unsigned char)(raster[pos] >> 24);
197 if (alpha < 127)
198 {
199 hasmask = TRUE;
200 ptr[0] = image->GetMaskRed();
201 ptr++;
202 ptr[0] = image->GetMaskGreen();
203 ptr++;
204 ptr[0] = image->GetMaskBlue();
205 ptr++;
206 }
207 else
208 {
209 ptr[0] = (unsigned char)(raster[pos] >> 16);
210 ptr++;
211 ptr[0] = (unsigned char)(raster[pos] >> 8);
212 ptr++;
213 ptr[0] = (unsigned char)(raster[pos]);
214 ptr++;
215 }
216 pos++;
217 }
218 }
219
220 _TIFFfree( raster );
221
222 TIFFClose( tif );
223
224 image->SetMask( hasmask );
225
226 return TRUE;
227 }
228
229 int wxTIFFHandler::GetImageCount( wxInputStream& stream )
230 {
231 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
232
233 if (!tif)
234 return 0;
235
236 int dircount = 0; // according to the libtiff docs, dircount should be set to 1 here???
237 do {
238 dircount++;
239 } while (TIFFReadDirectory(tif));
240
241 TIFFClose( tif );
242
243 return dircount;
244 }
245
246 bool wxTIFFHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
247 {
248 return FALSE;
249 }
250
251 bool wxTIFFHandler::DoCanRead( wxInputStream& stream )
252 {
253 unsigned char hdr[2];
254
255 stream.Read(&hdr, 2);
256 stream.SeekI(-2, wxFromCurrent);
257
258 return ((hdr[0] == 0x49 && hdr[1] == 0x49) ||
259 (hdr[0] == 0x4D && hdr[1] == 0x4D));
260 }
261
262
263 #endif
264 // wxUSE_LIBTIFF
265
266
267
268
269
270