More TIFF things,
[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 )
123 {
124 image->Destroy();
125
126 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
127
128 if (!tif)
129 {
130 if (verbose)
131 wxLogError( _("Error loading TIFF image.") );
132
133 return FALSE;
134 }
135
136 uint32 w, h;
137 size_t npixels;
138 uint32 *raster;
139
140 TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
141 TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h );
142
143 npixels = w * h;
144
145 raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
146
147 if (!raster)
148 {
149 if (verbose)
150 wxLogError( _("Not enough memory for loading TIFF image.") );
151
152 return FALSE;
153 }
154
155 image->Create( w, h );
156 if (!image->Ok())
157 {
158 if (verbose)
159 wxLogError( _("Not enough memory for loading TIFF image.") );
160
161 _TIFFfree( raster );
162
163 return FALSE;
164 }
165
166 if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
167 {
168 if (verbose)
169 wxLogError( _("Error reading TIFF image.") );
170
171 _TIFFfree( raster );
172 image->Destroy();
173
174 return FALSE;
175 }
176
177 bool hasmask = FALSE;
178
179 unsigned char *ptr = image->GetData();
180 uint32 pos = 0;
181
182 for (uint32 i = 0; i < h; i++)
183 {
184 for (uint32 j = 0; w < h; j++)
185 {
186 unsigned char alpha = (unsigned char)(raster[pos] >> 24);
187 if (alpha < 127)
188 {
189 hasmask = TRUE;
190 ptr[0] = image->GetMaskRed();
191 ptr++;
192 ptr[0] = image->GetMaskGreen();
193 ptr++;
194 ptr[0] = image->GetMaskBlue();
195 ptr++;
196 }
197 else
198 {
199 ptr[0] = (unsigned char)(raster[pos] >> 16);
200 ptr++;
201 ptr[0] = (unsigned char)(raster[pos] >> 8);
202 ptr++;
203 ptr[0] = (unsigned char)(raster[pos]);
204 ptr++;
205 }
206 pos++;
207 }
208 }
209
210 _TIFFfree( raster );
211
212 TIFFClose( tif );
213
214 image->SetMask( hasmask );
215
216 return TRUE;
217 }
218
219
220
221 bool wxTIFFHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
222 {
223 return FALSE;
224 }
225
226 bool wxTIFFHandler::DoCanRead( wxInputStream& stream )
227 {
228 unsigned char hdr[2];
229
230 stream.Read(&hdr, 2);
231 stream.SeekI(-2, wxFromCurrent);
232
233 return ((hdr[0] == 0x49 && hdr[1] == 0x49) ||
234 (hdr[0] == 0x4D && hdr[1] == 0x4D));
235 }
236
237
238 #endif
239 // wxUSE_LIBTIFF
240
241
242
243
244
245