]>
git.saurik.com Git - wxWidgets.git/blob - src/common/anidecod.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/anidecod.cpp
3 // Purpose: wxANIDecoder, ANI reader for wxImage and wxAnimation
4 // Author: Francesco Montorsi
6 // Copyright: (c) Francesco Montorsi
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 #if wxUSE_STREAMS && wxUSE_ICO_CUR
19 #include "wx/anidecod.h"
22 #include "wx/palette.h"
29 wxCURHandler
wxANIDecoder::sm_handler
;
31 //---------------------------------------------------------------------------
33 //---------------------------------------------------------------------------
38 wxANIFrameInfo(size_t delay
= 0, int idx
= -1)
39 { m_delay
=delay
; m_imageIndex
=idx
; }
45 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
46 WX_DEFINE_OBJARRAY(wxImageArray
)
48 #include "wx/arrimpl.cpp" // this is a magic incantation which must be done!
49 WX_DEFINE_OBJARRAY(wxANIFrameInfoArray
)
52 //---------------------------------------------------------------------------
54 //---------------------------------------------------------------------------
56 wxANIDecoder::wxANIDecoder()
60 wxANIDecoder::~wxANIDecoder()
64 bool wxANIDecoder::ConvertToImage(size_t frame
, wxImage
*image
) const
66 size_t idx
= m_info
[frame
].m_imageIndex
;
67 *image
= m_images
[idx
]; // copy
72 //---------------------------------------------------------------------------
74 //---------------------------------------------------------------------------
76 wxSize
wxANIDecoder::GetFrameSize(size_t WXUNUSED(frame
)) const
78 // all frames are of the same size...
82 wxPoint
wxANIDecoder::GetFramePosition(size_t WXUNUSED(frame
)) const
84 // all frames are of the same size...
88 wxAnimationDisposal
wxANIDecoder::GetDisposalMethod(size_t WXUNUSED(frame
)) const
90 // this disposal is implicit for all frames inside an ANI file
91 return wxANIM_TOBACKGROUND
;
94 long wxANIDecoder::GetDelay(size_t frame
) const
96 return m_info
[frame
].m_delay
;
100 //---------------------------------------------------------------------------
101 // ANI reading and decoding
102 //---------------------------------------------------------------------------
104 bool wxANIDecoder::CanRead(wxInputStream
& stream
) const
110 memcpy( &riff32
, "RIFF", 4 );
112 memcpy( &list32
, "LIST", 4 );
114 memcpy( &ico32
, "icon", 4 );
116 memcpy( &anih32
, "anih", 4 );
119 if ( !stream
.Read(&FCC1
, 4) )
122 if ( FCC1
!= riff32
)
125 // we have a riff file:
126 while ( stream
.IsOk() )
128 if ( FCC1
== anih32
)
129 return true; // found the ANIH chunk - this should be an ANI file
131 // we always have a data size:
132 stream
.Read(&datalen
, 4);
133 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
135 // data should be padded to make even number of bytes
136 if (datalen
% 2 == 1) datalen
++ ;
138 // now either data or a FCC:
139 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
141 stream
.Read(&FCC2
, 4);
145 stream
.SeekI(stream
.TellI() + datalen
);
148 // try to read next data chunk:
149 if ( !stream
.Read(&FCC1
, 4) )
151 // reading failed -- either EOF or IO error, bail out anyhow
159 // the "anih" RIFF chunk
162 wxInt32 cbSizeOf
; // Num bytes in AniHeader (36 bytes)
163 wxInt32 cFrames
; // Number of unique Icons in this cursor
164 wxInt32 cSteps
; // Number of Blits before the animation cycles
165 wxInt32 cx
; // width of the frames
166 wxInt32 cy
; // height of the frames
167 wxInt32 cBitCount
; // bit depth
168 wxInt32 cPlanes
; // 1
169 wxInt32 JifRate
; // Default Jiffies (1/60th of a second) if rate chunk not present.
170 wxInt32 flags
; // Animation Flag (see AF_ constants)
172 // ANI files are always little endian so we need to swap bytes on big
173 // endian architectures
174 #ifdef WORDS_BIGENDIAN
175 void AdjustEndianness()
177 // this works because all our fields are wxInt32 and they must be
178 // packed without holes between them (if they're not, they wouldn't map
179 // to the file header!)
180 wxInt32
* const start
= (wxInt32
*)this;
181 wxInt32
* const end
= start
+ sizeof(wxANIHeader
)/sizeof(wxInt32
);
182 for ( wxInt32
*p
= start
; p
!= end
; p
++ )
184 *p
= wxINT32_SWAP_ALWAYS(*p
);
188 void AdjustEndianness() { }
192 bool wxANIDecoder::Load( wxInputStream
& stream
)
196 size_t globaldelay
=0;
199 memcpy( &riff32
, "RIFF", 4 );
201 memcpy( &list32
, "LIST", 4 );
203 memcpy( &ico32
, "icon", 4 );
205 memcpy( &anih32
, "anih", 4 );
207 memcpy( &rate32
, "rate", 4 );
209 memcpy( &seq32
, "seq ", 4 );
212 stream
.Read(&FCC1
, 4);
213 if ( FCC1
!= riff32
)
217 m_szAnimation
= wxDefaultSize
;
222 // we have a riff file:
223 while ( stream
.IsOk() )
225 // we always have a data size:
226 stream
.Read(&datalen
, 4);
227 datalen
= wxINT32_SWAP_ON_BE(datalen
);
229 //data should be padded to make even number of bytes
230 if (datalen
% 2 == 1) datalen
++;
232 // now either data or a FCC:
233 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
235 stream
.Read(&FCC2
, 4);
237 else if ( FCC1
== anih32
)
239 if ( datalen
!= sizeof(wxANIHeader
) )
243 return false; // already parsed an ani header?
245 struct wxANIHeader header
;
246 stream
.Read(&header
, sizeof(wxANIHeader
));
247 header
.AdjustEndianness();
249 // we should have a global frame size
250 m_szAnimation
= wxSize(header
.cx
, header
.cy
);
252 // save interesting info from the header
253 m_nFrames
= header
.cSteps
; // NB: not cFrames!!
254 if ( m_nFrames
== 0 )
257 globaldelay
= header
.JifRate
* 1000 / 60;
259 m_images
.Alloc(header
.cFrames
);
260 m_info
.Add(wxANIFrameInfo(), m_nFrames
);
262 else if ( FCC1
== rate32
)
264 // did we already process the anih32 chunk?
266 return false; // rate chunks should always be placed after anih chunk
268 wxASSERT(m_info
.GetCount() == m_nFrames
);
269 for (size_t i
=0; i
<m_nFrames
; i
++)
271 stream
.Read(&FCC2
, 4);
272 m_info
[i
].m_delay
= wxINT32_SWAP_ON_BE(FCC2
) * 1000 / 60;
275 else if ( FCC1
== seq32
)
277 // did we already process the anih32 chunk?
279 return false; // seq chunks should always be placed after anih chunk
281 wxASSERT(m_info
.GetCount() == m_nFrames
);
282 for (size_t i
=0; i
<m_nFrames
; i
++)
284 stream
.Read(&FCC2
, 4);
285 m_info
[i
].m_imageIndex
= wxINT32_SWAP_ON_BE(FCC2
);
288 else if ( FCC1
== ico32
)
290 // use DoLoadFile() and not LoadFile()!
292 if (!sm_handler
.DoLoadFile(&image
, stream
, false /* verbose */, -1))
299 stream
.SeekI(stream
.TellI() + datalen
);
302 // try to read next data chunk:
303 stream
.Read(&FCC1
, 4);
309 if (m_nFrames
==m_images
.GetCount())
311 // if no SEQ chunk is available, display the frames in the order
313 for (size_t i
=0; i
<m_nFrames
; i
++)
314 if (m_info
[i
].m_imageIndex
== -1)
315 m_info
[i
].m_imageIndex
= i
;
318 // if some frame has an invalid delay, use the global delay given in the
320 for (size_t i
=0; i
<m_nFrames
; i
++)
321 if (m_info
[i
].m_delay
== 0)
322 m_info
[i
].m_delay
= globaldelay
;
324 // if the header did not contain a valid frame size, try to grab
325 // it from the size of the first frame (all frames are of the same size)
326 if (m_szAnimation
.GetWidth() == 0 ||
327 m_szAnimation
.GetHeight() == 0)
328 m_szAnimation
= wxSize(m_images
[0].GetWidth(), m_images
[0].GetHeight());
330 return m_szAnimation
!= wxDefaultSize
;
333 #endif // wxUSE_STREAMS && wxUSE_ICO_CUR