]> git.saurik.com Git - wxWidgets.git/blob - src/common/anidecod.cpp
disable new event code unconditionally for now
[wxWidgets.git] / src / common / anidecod.cpp
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
17 #if wxUSE_STREAMS && wxUSE_ICO_CUR
18
19 #include "wx/anidecod.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/palette.h"
23 #endif
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 // static
29 wxCURHandler wxANIDecoder::sm_handler;
30
31 //---------------------------------------------------------------------------
32 // wxANIFrameInfo
33 //---------------------------------------------------------------------------
34
35 class wxANIFrameInfo
36 {
37 public:
38 wxANIFrameInfo(unsigned int delay = 0, int idx = -1)
39 { m_delay=delay; m_imageIndex=idx; }
40
41 unsigned int m_delay;
42 int m_imageIndex;
43 };
44
45 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
46 WX_DEFINE_OBJARRAY(wxImageArray)
47
48 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
49 WX_DEFINE_OBJARRAY(wxANIFrameInfoArray)
50
51
52 //---------------------------------------------------------------------------
53 // wxANIDecoder
54 //---------------------------------------------------------------------------
55
56 wxANIDecoder::wxANIDecoder()
57 {
58 }
59
60 wxANIDecoder::~wxANIDecoder()
61 {
62 }
63
64 bool wxANIDecoder::ConvertToImage(unsigned int frame, wxImage *image) const
65 {
66 unsigned int idx = m_info[frame].m_imageIndex;
67 *image = m_images[idx]; // copy
68 return image->IsOk();
69 }
70
71
72 //---------------------------------------------------------------------------
73 // Data accessors
74 //---------------------------------------------------------------------------
75
76 wxSize wxANIDecoder::GetFrameSize(unsigned int WXUNUSED(frame)) const
77 {
78 // all frames are of the same size...
79 return m_szAnimation;
80 }
81
82 wxPoint wxANIDecoder::GetFramePosition(unsigned int WXUNUSED(frame)) const
83 {
84 // all frames are of the same size...
85 return wxPoint(0,0);
86 }
87
88 wxAnimationDisposal wxANIDecoder::GetDisposalMethod(unsigned int WXUNUSED(frame)) const
89 {
90 // this disposal is implicit for all frames inside an ANI file
91 return wxANIM_TOBACKGROUND;
92 }
93
94 long wxANIDecoder::GetDelay(unsigned int frame) const
95 {
96 return m_info[frame].m_delay;
97 }
98
99 wxColour wxANIDecoder::GetTransparentColour(unsigned int frame) const
100 {
101 unsigned int idx = m_info[frame].m_imageIndex;
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
111
112 //---------------------------------------------------------------------------
113 // ANI reading and decoding
114 //---------------------------------------------------------------------------
115
116 bool 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
130 if ( stream.SeekI(0) == wxInvalidOffset )
131 return false;
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) ;
147
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 {
158 if ( stream.SeekI(stream.TellI() + datalen) == wxInvalidOffset )
159 return false;
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
174 struct wxANIHeader
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)
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
204 };
205
206 bool wxANIDecoder::Load( wxInputStream& stream )
207 {
208 wxInt32 FCC1, FCC2;
209 wxUint32 datalen;
210 unsigned int globaldelay=0;
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
225 if ( stream.SeekI(0) == wxInvalidOffset)
226 return false;
227 if ( !stream.Read(&FCC1, 4) )
228 return false;
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:
239 while ( stream.IsOk() )
240 {
241 // we always have a data size:
242 stream.Read(&datalen, 4);
243 datalen = wxINT32_SWAP_ON_BE(datalen);
244
245 //data should be padded to make even number of bytes
246 if (datalen % 2 == 1) datalen++;
247
248 // now either data or a FCC:
249 if ( (FCC1 == riff32) || (FCC1 == list32) )
250 {
251 stream.Read(&FCC2, 4);
252 }
253 else if ( FCC1 == anih32 )
254 {
255 if ( datalen != sizeof(wxANIHeader) )
256 return false;
257
258 if (m_nFrames > 0)
259 return false; // already parsed an ani header?
260
261 struct wxANIHeader header;
262 stream.Read(&header, sizeof(wxANIHeader));
263 header.AdjustEndianness();
264
265 // we should have a global frame size
266 m_szAnimation = wxSize(header.cx, header.cy);
267
268 // save interesting info from the header
269 m_nFrames = header.cSteps; // NB: not cFrames!!
270 if ( m_nFrames == 0 )
271 return false;
272
273 globaldelay = header.JifRate * 1000 / 60;
274
275 m_images.Alloc(header.cFrames);
276 m_info.Add(wxANIFrameInfo(), m_nFrames);
277 }
278 else if ( FCC1 == rate32 )
279 {
280 // did we already process the anih32 chunk?
281 if (m_nFrames == 0)
282 return false; // rate chunks should always be placed after anih chunk
283
284 wxASSERT(m_info.GetCount() == m_nFrames);
285 for (unsigned int i=0; i<m_nFrames; i++)
286 {
287 stream.Read(&FCC2, 4);
288 m_info[i].m_delay = wxINT32_SWAP_ON_BE(FCC2) * 1000 / 60;
289 }
290 }
291 else if ( FCC1 == seq32 )
292 {
293 // did we already process the anih32 chunk?
294 if (m_nFrames == 0)
295 return false; // seq chunks should always be placed after anih chunk
296
297 wxASSERT(m_info.GetCount() == m_nFrames);
298 for (unsigned int i=0; i<m_nFrames; i++)
299 {
300 stream.Read(&FCC2, 4);
301 m_info[i].m_imageIndex = wxINT32_SWAP_ON_BE(FCC2);
302 }
303 }
304 else if ( FCC1 == ico32 )
305 {
306 // use DoLoadFile() and not LoadFile()!
307 wxImage image;
308 if (!sm_handler.DoLoadFile(&image, stream, false /* verbose */, -1))
309 return false;
310
311 image.SetType(wxBITMAP_TYPE_ANI);
312 m_images.Add(image);
313 }
314 else
315 {
316 if ( stream.SeekI(stream.TellI() + datalen) == wxInvalidOffset )
317 return false;
318 }
319
320 // try to read next data chunk:
321 if ( !stream.Read(&FCC1, 4) )
322 return false;
323 }
324
325 if (m_nFrames==0)
326 return false;
327
328 if (m_nFrames==m_images.GetCount())
329 {
330 // if no SEQ chunk is available, display the frames in the order
331 // they were loaded
332 for (unsigned int i=0; i<m_nFrames; i++)
333 if (m_info[i].m_imageIndex == -1)
334 m_info[i].m_imageIndex = i;
335 }
336
337 // if some frame has an invalid delay, use the global delay given in the
338 // ANI header
339 for (unsigned int i=0; i<m_nFrames; i++)
340 if (m_info[i].m_delay == 0)
341 m_info[i].m_delay = globaldelay;
342
343 // if the header did not contain a valid frame size, try to grab
344 // it from the size of the first frame (all frames are of the same size)
345 if (m_szAnimation.GetWidth() == 0 ||
346 m_szAnimation.GetHeight() == 0)
347 m_szAnimation = wxSize(m_images[0].GetWidth(), m_images[0].GetHeight());
348
349 return m_szAnimation != wxDefaultSize;
350 }
351
352 #endif // wxUSE_STREAMS && wxUSE_ICO_CUR