]>
git.saurik.com Git - wxWidgets.git/blob - src/common/anidecod.cpp
76d2f9c0ba8261e7b9c2cfd45123a60c05b3399f
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_GIF
20 #include "wx/palette.h"
25 #include "wx/anidecod.h"
28 wxCURHandler
wxANIDecoder::sm_handler
;
32 //---------------------------------------------------------------------------
34 //---------------------------------------------------------------------------
39 wxANIFrameInfo(size_t delay
= 0, int idx
= -1)
40 { m_delay
=delay
; m_imageIndex
=idx
; }
46 #include <wx/arrimpl.cpp> // this is a magic incantation which must be done!
47 WX_DEFINE_OBJARRAY(wxImageArray
);
49 #include <wx/arrimpl.cpp> // this is a magic incantation which must be done!
50 WX_DEFINE_OBJARRAY(wxANIFrameInfoArray
);
55 //---------------------------------------------------------------------------
57 //---------------------------------------------------------------------------
59 wxANIDecoder::wxANIDecoder()
63 wxANIDecoder::~wxANIDecoder()
67 bool wxANIDecoder::ConvertToImage(size_t frame
, wxImage
*image
) const
69 size_t idx
= m_info
[frame
].m_imageIndex
;
70 *image
= m_images
[idx
]; // copy
75 //---------------------------------------------------------------------------
77 //---------------------------------------------------------------------------
79 wxSize
wxANIDecoder::GetFrameSize(size_t WXUNUSED(frame
)) const
81 // all frames are of the same size...
85 wxPoint
wxANIDecoder::GetFramePosition(size_t WXUNUSED(frame
)) const
87 // all frames are of the same size...
91 wxAnimationDisposal
wxANIDecoder::GetDisposalMethod(size_t WXUNUSED(frame
)) const
93 // this disposal is implicit for all frames inside an ANI file
94 return wxANIM_TOBACKGROUND
;
97 long wxANIDecoder::GetDelay(size_t frame
) const
99 return m_info
[frame
].m_delay
;
103 //---------------------------------------------------------------------------
104 // ANI reading and decoding
105 //---------------------------------------------------------------------------
107 bool wxANIDecoder::CanRead(wxInputStream
& stream
) const
113 memcpy( &riff32
, "RIFF", 4 );
115 memcpy( &list32
, "LIST", 4 );
117 memcpy( &ico32
, "icon", 4 );
119 memcpy( &anih32
, "anih", 4 );
122 if ( !stream
.Read(&FCC1
, 4) )
125 if ( FCC1
!= riff32
)
128 // we have a riff file:
129 while ( stream
.IsOk() )
131 if ( FCC1
== anih32
)
132 return true; // found the ANIH chunk - this should be an ANI file
134 // we always have a data size:
135 stream
.Read(&datalen
, 4);
136 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
138 // data should be padded to make even number of bytes
139 if (datalen
% 2 == 1) datalen
++ ;
141 // now either data or a FCC:
142 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
144 stream
.Read(&FCC2
, 4);
148 stream
.SeekI(stream
.TellI() + datalen
);
151 // try to read next data chunk:
152 if ( !stream
.Read(&FCC1
, 4) )
154 // reading failed -- either EOF or IO error, bail out anyhow
162 // the "anih" RIFF chunk
165 wxInt32 cbSizeOf
; // Num bytes in AniHeader (36 bytes)
166 wxInt32 cFrames
; // Number of unique Icons in this cursor
167 wxInt32 cSteps
; // Number of Blits before the animation cycles
168 wxInt32 cx
; // width of the frames
169 wxInt32 cy
; // height of the frames
170 wxInt32 cBitCount
; // bit depth
171 wxInt32 cPlanes
; // 1
172 wxInt32 JifRate
; // Default Jiffies (1/60th of a second) if rate chunk not present.
173 wxInt32 flags
; // Animation Flag (see AF_ constants)
175 // ANI files are always little endian so we need to swap bytes on big
176 // endian architectures
177 #ifdef WORDS_BIGENDIAN
178 void AdjustEndianness()
180 // this works because all our fields are wxInt32 and they must be
181 // packed without holes between them (if they're not, they wouldn't map
182 // to the file header!)
183 wxInt32
* const start
= (wxInt32
*)this;
184 wxInt32
* const end
= start
+ sizeof(wxANIHeader
)/sizeof(wxInt32
);
185 for ( wxInt32
*p
= start
; p
!= end
; p
++ )
187 *p
= wxINT32_SWAP_ALWAYS(*p
);
191 void AdjustEndianness() { }
195 bool wxANIDecoder::Load( wxInputStream
& stream
)
199 size_t globaldelay
=0;
202 memcpy( &riff32
, "RIFF", 4 );
204 memcpy( &list32
, "LIST", 4 );
206 memcpy( &ico32
, "icon", 4 );
208 memcpy( &anih32
, "anih", 4 );
210 memcpy( &rate32
, "rate", 4 );
212 memcpy( &seq32
, "seq ", 4 );
215 stream
.Read(&FCC1
, 4);
216 if ( FCC1
!= riff32
)
220 m_szAnimation
= wxDefaultSize
;
225 // we have a riff file:
226 while ( stream
.IsOk() )
228 // we always have a data size:
229 stream
.Read(&datalen
, 4);
230 datalen
= wxINT32_SWAP_ON_BE(datalen
);
232 //data should be padded to make even number of bytes
233 if (datalen
% 2 == 1) datalen
++;
235 // now either data or a FCC:
236 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
238 stream
.Read(&FCC2
, 4);
240 else if ( FCC1
== anih32
)
242 if ( datalen
!= sizeof(wxANIHeader
) )
246 return false; // already parsed an ani header?
248 struct wxANIHeader header
;
249 stream
.Read(&header
, sizeof(wxANIHeader
));
250 header
.AdjustEndianness();
252 // we should have a global frame size
253 m_szAnimation
= wxSize(header
.cx
, header
.cy
);
255 // save interesting info from the header
256 m_nFrames
= header
.cSteps
; // NB: not cFrames!!
257 if ( m_nFrames
== 0 )
260 globaldelay
= header
.JifRate
* 1000 / 60;
262 m_images
.Alloc(header
.cFrames
);
263 m_info
.Add(wxANIFrameInfo(), m_nFrames
);
265 else if ( FCC1
== rate32
)
267 // did we already process the anih32 chunk?
269 return false; // rate chunks should always be placed after anih chunk
271 wxASSERT(m_info
.GetCount() == m_nFrames
);
272 for (size_t i
=0; i
<m_nFrames
; i
++)
274 stream
.Read(&FCC2
, 4);
275 m_info
[i
].m_delay
= wxINT32_SWAP_ON_BE(FCC2
) * 1000 / 60;
278 else if ( FCC1
== seq32
)
280 // did we already process the anih32 chunk?
282 return false; // seq chunks should always be placed after anih chunk
284 wxASSERT(m_info
.GetCount() == m_nFrames
);
285 for (size_t i
=0; i
<m_nFrames
; i
++)
287 stream
.Read(&FCC2
, 4);
288 m_info
[i
].m_imageIndex
= wxINT32_SWAP_ON_BE(FCC2
);
291 else if ( FCC1
== ico32
)
293 // use DoLoadFile() and not LoadFile()!
295 if (!sm_handler
.DoLoadFile(&image
, stream
, false /* verbose */, -1))
302 stream
.SeekI(stream
.TellI() + datalen
);
305 // try to read next data chunk:
306 stream
.Read(&FCC1
, 4);
312 if (m_nFrames
==m_images
.GetCount())
314 // if no SEQ chunk is available, display the frames in the order
316 for (size_t i
=0; i
<m_nFrames
; i
++)
317 if (m_info
[i
].m_imageIndex
== -1)
318 m_info
[i
].m_imageIndex
= i
;
321 // if some frame has an invalid delay, use the global delay given in the
323 for (size_t i
=0; i
<m_nFrames
; i
++)
324 if (m_info
[i
].m_delay
== 0)
325 m_info
[i
].m_delay
= globaldelay
;
327 // if the header did not contain a valid frame size, try to grab
328 // it from the size of the first frame (all frames are of the same size)
329 if (m_szAnimation
.GetWidth() == 0 ||
330 m_szAnimation
.GetHeight() == 0)
331 m_szAnimation
= wxSize(m_images
[0].GetWidth(), m_images
[0].GetHeight());
333 return m_szAnimation
!=wxDefaultSize
;
336 #endif // wxUSE_STREAMS && wxUSE_GIF