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