]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagtiff.cpp
Added ComputeHistogram
[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
257bcf28 46IMPLEMENT_DYNAMIC_CLASS(wxTIFFHandler,wxImageHandler)
257bcf28
RR
47
48static 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 );
0b72db08 53 return stream->LastRead();
257bcf28
RR
54}
55
56static 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 );
0b72db08 61 return stream->LastWrite();
257bcf28
RR
62}
63
64static 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
80static int
81_tiffCloseProc(thandle_t WXUNUSED(handle))
82{
83 return 0; // ?
84}
85
86static toff_t
87_tiffSizeProc(thandle_t handle)
88{
89 wxInputStream *stream = (wxInputStream*) handle;
0b72db08 90 return (toff_t) stream->GetSize();
257bcf28
RR
91}
92
93static int
94_tiffMapProc(thandle_t WXUNUSED(handle), tdata_t* pbase, toff_t* psize)
95{
96 return 0;
97}
98
99static void
100_tiffUnmapProc(thandle_t WXUNUSED(handle), tdata_t base, toff_t size)
101{
102}
103
104TIFF*
105TIFFwxOpen(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
700ec454 120bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index )
257bcf28
RR
121{
122 image->Destroy();
123
0b72db08 124 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
257bcf28
RR
125
126 if (!tif)
127 {
128 if (verbose)
58c837a4 129 wxLogError( _("TIFF: Error loading image.") );
257bcf28
RR
130
131 return FALSE;
132 }
700ec454
RR
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 }
257bcf28
RR
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)
58c837a4 158 wxLogError( _("TIFF: Couldn't allocate memory.") );
257bcf28
RR
159
160 return FALSE;
161 }
162
163 image->Create( w, h );
164 if (!image->Ok())
165 {
166 if (verbose)
58c837a4 167 wxLogError( _("TIFF: Couldn't allocate memory.") );
257bcf28
RR
168
169 _TIFFfree( raster );
170
171 return FALSE;
172 }
173
174 if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
175 {
176 if (verbose)
58c837a4 177 wxLogError( _("TIFF: Error reading image.") );
257bcf28
RR
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
700ec454
RR
227int wxTIFFHandler::GetImageCount( wxInputStream& stream )
228{
229 TIFF *tif = TIFFwxOpen( stream, "image", "r" );
230
231 if (!tif)
232 return 0;
257bcf28 233
700ec454
RR
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}
257bcf28 243
700ec454 244bool wxTIFFHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
257bcf28
RR
245{
246 return FALSE;
247}
248
249bool wxTIFFHandler::DoCanRead( wxInputStream& stream )
250{
0b72db08 251 unsigned char hdr[2];
257bcf28 252
0b72db08
RR
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));
257bcf28
RR
258}
259
260
261#endif
262 // wxUSE_LIBTIFF
263
264
265
266
267
268