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