]> git.saurik.com Git - wxWidgets.git/blame - src/common/anidecod.cpp
special casing mac code
[wxWidgets.git] / src / common / anidecod.cpp
CommitLineData
72045d57
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/common/anidecod.cpp
3// Purpose: wxANIDecoder, ANI reader for wxImage and wxAnimation
4// Author: Francesco Montorsi
5// RCS-ID: $Id$
6// Copyright: (c) Francesco Montorsi
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
14 #pragma hdrstop
15#endif
16
d18868bb 17#if wxUSE_STREAMS && wxUSE_ICO_CUR
72045d57 18
14c0d834
PC
19#include "wx/anidecod.h"
20
72045d57
VZ
21#ifndef WX_PRECOMP
22 #include "wx/palette.h"
23#endif
24
25#include <stdlib.h>
26#include <string.h>
72045d57
VZ
27
28// static
29wxCURHandler wxANIDecoder::sm_handler;
30
72045d57
VZ
31//---------------------------------------------------------------------------
32// wxANIFrameInfo
33//---------------------------------------------------------------------------
34
35class wxANIFrameInfo
36{
37public:
870cf35c 38 wxANIFrameInfo(unsigned int delay = 0, int idx = -1)
72045d57
VZ
39 { m_delay=delay; m_imageIndex=idx; }
40
870cf35c 41 unsigned int m_delay;
72045d57
VZ
42 int m_imageIndex;
43};
44
14c0d834
PC
45#include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
46WX_DEFINE_OBJARRAY(wxImageArray)
72045d57 47
14c0d834
PC
48#include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
49WX_DEFINE_OBJARRAY(wxANIFrameInfoArray)
72045d57
VZ
50
51
52//---------------------------------------------------------------------------
53// wxANIDecoder
54//---------------------------------------------------------------------------
55
56wxANIDecoder::wxANIDecoder()
57{
58}
59
60wxANIDecoder::~wxANIDecoder()
61{
62}
63
870cf35c 64bool wxANIDecoder::ConvertToImage(unsigned int frame, wxImage *image) const
72045d57 65{
870cf35c 66 unsigned int idx = m_info[frame].m_imageIndex;
72045d57 67 *image = m_images[idx]; // copy
9d9e3858 68 return image->IsOk();
72045d57
VZ
69}
70
71
72//---------------------------------------------------------------------------
73// Data accessors
74//---------------------------------------------------------------------------
75
870cf35c 76wxSize wxANIDecoder::GetFrameSize(unsigned int WXUNUSED(frame)) const
72045d57
VZ
77{
78 // all frames are of the same size...
79 return m_szAnimation;
80}
81
870cf35c 82wxPoint wxANIDecoder::GetFramePosition(unsigned int WXUNUSED(frame)) const
72045d57
VZ
83{
84 // all frames are of the same size...
85 return wxPoint(0,0);
86}
87
870cf35c 88wxAnimationDisposal wxANIDecoder::GetDisposalMethod(unsigned int WXUNUSED(frame)) const
72045d57
VZ
89{
90 // this disposal is implicit for all frames inside an ANI file
91 return wxANIM_TOBACKGROUND;
92}
93
870cf35c 94long wxANIDecoder::GetDelay(unsigned int frame) const
72045d57
VZ
95{
96 return m_info[frame].m_delay;
97}
98
870cf35c 99wxColour wxANIDecoder::GetTransparentColour(unsigned int frame) const
05a98b6d 100{
870cf35c 101 unsigned int idx = m_info[frame].m_imageIndex;
05a98b6d
RR
102
103 if (!m_images[idx].HasMask())
104 return wxNullColour;
105
106 return wxColour(m_images[idx].GetMaskRed(),
107 m_images[idx].GetMaskGreen(),
108 m_images[idx].GetMaskBlue());
109}
110
72045d57
VZ
111
112//---------------------------------------------------------------------------
113// ANI reading and decoding
114//---------------------------------------------------------------------------
115
116bool wxANIDecoder::CanRead(wxInputStream& stream) const
117{
118 wxInt32 FCC1, FCC2;
119 wxUint32 datalen ;
120
121 wxInt32 riff32;
122 memcpy( &riff32, "RIFF", 4 );
123 wxInt32 list32;
124 memcpy( &list32, "LIST", 4 );
125 wxInt32 ico32;
126 memcpy( &ico32, "icon", 4 );
127 wxInt32 anih32;
128 memcpy( &anih32, "anih", 4 );
129
b81e4506
FM
130 if ( stream.SeekI(0) == wxInvalidOffset )
131 return false;
72045d57
VZ
132 if ( !stream.Read(&FCC1, 4) )
133 return false;
134
135 if ( FCC1 != riff32 )
136 return false;
137
138 // we have a riff file:
139 while ( stream.IsOk() )
140 {
141 if ( FCC1 == anih32 )
142 return true; // found the ANIH chunk - this should be an ANI file
143
144 // we always have a data size:
145 stream.Read(&datalen, 4);
146 datalen = wxINT32_SWAP_ON_BE(datalen) ;
9d9e3858 147
72045d57
VZ
148 // data should be padded to make even number of bytes
149 if (datalen % 2 == 1) datalen ++ ;
150
151 // now either data or a FCC:
152 if ( (FCC1 == riff32) || (FCC1 == list32) )
153 {
154 stream.Read(&FCC2, 4);
155 }
156 else
157 {
b81e4506
FM
158 if ( stream.SeekI(stream.TellI() + datalen) == wxInvalidOffset )
159 return false;
72045d57
VZ
160 }
161
162 // try to read next data chunk:
163 if ( !stream.Read(&FCC1, 4) )
164 {
165 // reading failed -- either EOF or IO error, bail out anyhow
166 return false;
167 }
168 }
169
170 return false;
171}
172
173// the "anih" RIFF chunk
9d9e3858 174struct wxANIHeader
72045d57
VZ
175{
176 wxInt32 cbSizeOf; // Num bytes in AniHeader (36 bytes)
177 wxInt32 cFrames; // Number of unique Icons in this cursor
178 wxInt32 cSteps; // Number of Blits before the animation cycles
179 wxInt32 cx; // width of the frames
180 wxInt32 cy; // height of the frames
181 wxInt32 cBitCount; // bit depth
182 wxInt32 cPlanes; // 1
183 wxInt32 JifRate; // Default Jiffies (1/60th of a second) if rate chunk not present.
184 wxInt32 flags; // Animation Flag (see AF_ constants)
ce7efe2d
VZ
185
186 // ANI files are always little endian so we need to swap bytes on big
187 // endian architectures
188#ifdef WORDS_BIGENDIAN
189 void AdjustEndianness()
190 {
191 // this works because all our fields are wxInt32 and they must be
192 // packed without holes between them (if they're not, they wouldn't map
193 // to the file header!)
194 wxInt32 * const start = (wxInt32 *)this;
195 wxInt32 * const end = start + sizeof(wxANIHeader)/sizeof(wxInt32);
196 for ( wxInt32 *p = start; p != end; p++ )
197 {
198 *p = wxINT32_SWAP_ALWAYS(*p);
199 }
200 }
201#else
202 void AdjustEndianness() { }
203#endif
72045d57
VZ
204};
205
206bool wxANIDecoder::Load( wxInputStream& stream )
207{
208 wxInt32 FCC1, FCC2;
209 wxUint32 datalen;
870cf35c 210 unsigned int globaldelay=0;
72045d57
VZ
211
212 wxInt32 riff32;
213 memcpy( &riff32, "RIFF", 4 );
214 wxInt32 list32;
215 memcpy( &list32, "LIST", 4 );
216 wxInt32 ico32;
217 memcpy( &ico32, "icon", 4 );
218 wxInt32 anih32;
219 memcpy( &anih32, "anih", 4 );
220 wxInt32 rate32;
221 memcpy( &rate32, "rate", 4 );
222 wxInt32 seq32;
223 memcpy( &seq32, "seq ", 4 );
224
b81e4506
FM
225 if ( stream.SeekI(0) == wxInvalidOffset)
226 return false;
227 if ( !stream.Read(&FCC1, 4) )
228 return false;
72045d57
VZ
229 if ( FCC1 != riff32 )
230 return false;
231
232 m_nFrames = 0;
233 m_szAnimation = wxDefaultSize;
234
235 m_images.Clear();
236 m_info.Clear();
237
238 // we have a riff file:
604898dc 239 while ( !stream.Eof() )
72045d57
VZ
240 {
241 // we always have a data size:
604898dc
FM
242 if (!stream.Read(&datalen, 4))
243 return false;
244
72045d57
VZ
245 datalen = wxINT32_SWAP_ON_BE(datalen);
246
247 //data should be padded to make even number of bytes
248 if (datalen % 2 == 1) datalen++;
249
250 // now either data or a FCC:
251 if ( (FCC1 == riff32) || (FCC1 == list32) )
252 {
604898dc
FM
253 if (!stream.Read(&FCC2, 4))
254 return false;
72045d57
VZ
255 }
256 else if ( FCC1 == anih32 )
257 {
258 if ( datalen != sizeof(wxANIHeader) )
259 return false;
260
261 if (m_nFrames > 0)
262 return false; // already parsed an ani header?
263
264 struct wxANIHeader header;
604898dc
FM
265 if (!stream.Read(&header, sizeof(wxANIHeader)))
266 return false;
ce7efe2d 267 header.AdjustEndianness();
72045d57
VZ
268
269 // we should have a global frame size
270 m_szAnimation = wxSize(header.cx, header.cy);
271
272 // save interesting info from the header
273 m_nFrames = header.cSteps; // NB: not cFrames!!
9d9e3858 274 if ( m_nFrames == 0 )
72045d57
VZ
275 return false;
276
ce7efe2d 277 globaldelay = header.JifRate * 1000 / 60;
72045d57
VZ
278
279 m_images.Alloc(header.cFrames);
280 m_info.Add(wxANIFrameInfo(), m_nFrames);
281 }
282 else if ( FCC1 == rate32 )
283 {
284 // did we already process the anih32 chunk?
285 if (m_nFrames == 0)
286 return false; // rate chunks should always be placed after anih chunk
9d9e3858 287
72045d57 288 wxASSERT(m_info.GetCount() == m_nFrames);
870cf35c 289 for (unsigned int i=0; i<m_nFrames; i++)
72045d57 290 {
604898dc
FM
291 if (!stream.Read(&FCC2, 4))
292 return false;
72045d57
VZ
293 m_info[i].m_delay = wxINT32_SWAP_ON_BE(FCC2) * 1000 / 60;
294 }
295 }
296 else if ( FCC1 == seq32 )
297 {
298 // did we already process the anih32 chunk?
299 if (m_nFrames == 0)
300 return false; // seq chunks should always be placed after anih chunk
301
302 wxASSERT(m_info.GetCount() == m_nFrames);
870cf35c 303 for (unsigned int i=0; i<m_nFrames; i++)
72045d57 304 {
604898dc
FM
305 if (!stream.Read(&FCC2, 4))
306 return false;
72045d57
VZ
307 m_info[i].m_imageIndex = wxINT32_SWAP_ON_BE(FCC2);
308 }
309 }
310 else if ( FCC1 == ico32 )
311 {
312 // use DoLoadFile() and not LoadFile()!
313 wxImage image;
314 if (!sm_handler.DoLoadFile(&image, stream, false /* verbose */, -1))
315 return false;
316
9d1c7e84 317 image.SetType(wxBITMAP_TYPE_ANI);
72045d57
VZ
318 m_images.Add(image);
319 }
320 else
ce7efe2d 321 {
b81e4506
FM
322 if ( stream.SeekI(stream.TellI() + datalen) == wxInvalidOffset )
323 return false;
ce7efe2d 324 }
72045d57
VZ
325
326 // try to read next data chunk:
604898dc
FM
327 if ( !stream.Read(&FCC1, 4) && !stream.Eof())
328 {
329 // we didn't reach the EOF! An other kind of error has occurred...
b81e4506 330 return false;
604898dc
FM
331 }
332 //else: proceed with the parsing of the next header block or
333 // exiting this loop (if stream.Eof() == true)
72045d57
VZ
334 }
335
336 if (m_nFrames==0)
337 return false;
338
339 if (m_nFrames==m_images.GetCount())
340 {
341 // if no SEQ chunk is available, display the frames in the order
342 // they were loaded
870cf35c 343 for (unsigned int i=0; i<m_nFrames; i++)
72045d57
VZ
344 if (m_info[i].m_imageIndex == -1)
345 m_info[i].m_imageIndex = i;
346 }
347
348 // if some frame has an invalid delay, use the global delay given in the
349 // ANI header
870cf35c 350 for (unsigned int i=0; i<m_nFrames; i++)
72045d57
VZ
351 if (m_info[i].m_delay == 0)
352 m_info[i].m_delay = globaldelay;
353
354 // if the header did not contain a valid frame size, try to grab
355 // it from the size of the first frame (all frames are of the same size)
356 if (m_szAnimation.GetWidth() == 0 ||
357 m_szAnimation.GetHeight() == 0)
358 m_szAnimation = wxSize(m_images[0].GetWidth(), m_images[0].GetHeight());
359
14c0d834 360 return m_szAnimation != wxDefaultSize;
72045d57
VZ
361}
362
d18868bb 363#endif // wxUSE_STREAMS && wxUSE_ICO_CUR