]>
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(unsigned int 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(unsigned int frame
, wxImage
*image
) const
66 unsigned int idx
= m_info
[frame
].m_imageIndex
;
67 *image
= m_images
[idx
]; // copy
72 //---------------------------------------------------------------------------
74 //---------------------------------------------------------------------------
76 wxSize
wxANIDecoder::GetFrameSize(unsigned int WXUNUSED(frame
)) const
78 // all frames are of the same size...
82 wxPoint
wxANIDecoder::GetFramePosition(unsigned int WXUNUSED(frame
)) const
84 // all frames are of the same size...
88 wxAnimationDisposal
wxANIDecoder::GetDisposalMethod(unsigned int WXUNUSED(frame
)) const
90 // this disposal is implicit for all frames inside an ANI file
91 return wxANIM_TOBACKGROUND
;
94 long wxANIDecoder::GetDelay(unsigned int frame
) const
96 return m_info
[frame
].m_delay
;
99 wxColour
wxANIDecoder::GetTransparentColour(unsigned int frame
) const
101 unsigned int idx
= m_info
[frame
].m_imageIndex
;
103 if (!m_images
[idx
].HasMask())
106 return wxColour(m_images
[idx
].GetMaskRed(),
107 m_images
[idx
].GetMaskGreen(),
108 m_images
[idx
].GetMaskBlue());
112 //---------------------------------------------------------------------------
113 // ANI reading and decoding
114 //---------------------------------------------------------------------------
116 bool wxANIDecoder::DoCanRead(wxInputStream
& stream
) const
122 memcpy( &riff32
, "RIFF", 4 );
124 memcpy( &list32
, "LIST", 4 );
126 memcpy( &ico32
, "icon", 4 );
128 memcpy( &anih32
, "anih", 4 );
130 if ( !stream
.Read(&FCC1
, 4) )
133 if ( FCC1
!= riff32
)
136 // we have a riff file:
137 while ( stream
.IsOk() )
139 if ( FCC1
== anih32
)
140 return true; // found the ANIH chunk - this should be an ANI file
142 // we always have a data size:
143 stream
.Read(&datalen
, 4);
144 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
146 // data should be padded to make even number of bytes
147 if (datalen
% 2 == 1) datalen
++ ;
149 // now either data or a FCC:
150 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
152 stream
.Read(&FCC2
, 4);
156 if ( stream
.SeekI(stream
.TellI() + datalen
) == wxInvalidOffset
)
160 // try to read next data chunk:
161 if ( !stream
.Read(&FCC1
, 4) )
163 // reading failed -- either EOF or IO error, bail out anyhow
171 // the "anih" RIFF chunk
174 wxInt32 cbSizeOf
; // Num bytes in AniHeader (36 bytes)
175 wxInt32 cFrames
; // Number of unique Icons in this cursor
176 wxInt32 cSteps
; // Number of Blits before the animation cycles
177 wxInt32 cx
; // width of the frames
178 wxInt32 cy
; // height of the frames
179 wxInt32 cBitCount
; // bit depth
180 wxInt32 cPlanes
; // 1
181 wxInt32 JifRate
; // Default Jiffies (1/60th of a second) if rate chunk not present.
182 wxInt32 flags
; // Animation Flag (see AF_ constants)
184 // ANI files are always little endian so we need to swap bytes on big
185 // endian architectures
186 #ifdef WORDS_BIGENDIAN
187 void AdjustEndianness()
189 // this works because all our fields are wxInt32 and they must be
190 // packed without holes between them (if they're not, they wouldn't map
191 // to the file header!)
192 wxInt32
* const start
= (wxInt32
*)this;
193 wxInt32
* const end
= start
+ sizeof(wxANIHeader
)/sizeof(wxInt32
);
194 for ( wxInt32
*p
= start
; p
!= end
; p
++ )
196 *p
= wxINT32_SWAP_ALWAYS(*p
);
200 void AdjustEndianness() { }
204 bool wxANIDecoder::Load( wxInputStream
& stream
)
208 unsigned int globaldelay
=0;
211 memcpy( &riff32
, "RIFF", 4 );
213 memcpy( &list32
, "LIST", 4 );
215 memcpy( &ico32
, "icon", 4 );
217 memcpy( &anih32
, "anih", 4 );
219 memcpy( &rate32
, "rate", 4 );
221 memcpy( &seq32
, "seq ", 4 );
223 if ( !stream
.Read(&FCC1
, 4) )
225 if ( FCC1
!= riff32
)
229 m_szAnimation
= wxDefaultSize
;
234 // we have a riff file:
235 while ( !stream
.Eof() )
237 // we always have a data size:
238 if (!stream
.Read(&datalen
, 4))
241 datalen
= wxINT32_SWAP_ON_BE(datalen
);
243 //data should be padded to make even number of bytes
244 if (datalen
% 2 == 1) datalen
++;
246 // now either data or a FCC:
247 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
249 if (!stream
.Read(&FCC2
, 4))
252 else if ( FCC1
== anih32
)
254 if ( datalen
!= sizeof(wxANIHeader
) )
258 return false; // already parsed an ani header?
260 struct wxANIHeader header
;
261 if (!stream
.Read(&header
, sizeof(wxANIHeader
)))
263 header
.AdjustEndianness();
265 // we should have a global frame size
266 m_szAnimation
= wxSize(header
.cx
, header
.cy
);
268 // save interesting info from the header
269 m_nFrames
= header
.cSteps
; // NB: not cFrames!!
270 if ( m_nFrames
== 0 )
273 globaldelay
= header
.JifRate
* 1000 / 60;
275 m_images
.Alloc(header
.cFrames
);
276 m_info
.Add(wxANIFrameInfo(), m_nFrames
);
278 else if ( FCC1
== rate32
)
280 // did we already process the anih32 chunk?
282 return false; // rate chunks should always be placed after anih chunk
284 wxASSERT(m_info
.GetCount() == m_nFrames
);
285 for (unsigned int i
=0; i
<m_nFrames
; i
++)
287 if (!stream
.Read(&FCC2
, 4))
289 m_info
[i
].m_delay
= wxINT32_SWAP_ON_BE(FCC2
) * 1000 / 60;
292 else if ( FCC1
== seq32
)
294 // did we already process the anih32 chunk?
296 return false; // seq chunks should always be placed after anih chunk
298 wxASSERT(m_info
.GetCount() == m_nFrames
);
299 for (unsigned int i
=0; i
<m_nFrames
; i
++)
301 if (!stream
.Read(&FCC2
, 4))
303 m_info
[i
].m_imageIndex
= wxINT32_SWAP_ON_BE(FCC2
);
306 else if ( FCC1
== ico32
)
308 // use DoLoadFile() and not LoadFile()!
310 if (!sm_handler
.DoLoadFile(&image
, stream
, false /* verbose */, -1))
313 image
.SetType(wxBITMAP_TYPE_ANI
);
318 if ( stream
.SeekI(stream
.TellI() + datalen
) == wxInvalidOffset
)
322 // try to read next data chunk:
323 if ( !stream
.Read(&FCC1
, 4) && !stream
.Eof())
325 // we didn't reach the EOF! An other kind of error has occurred...
328 //else: proceed with the parsing of the next header block or
329 // exiting this loop (if stream.Eof() == true)
335 if (m_nFrames
==m_images
.GetCount())
337 // if no SEQ chunk is available, display the frames in the order
339 for (unsigned int i
=0; i
<m_nFrames
; i
++)
340 if (m_info
[i
].m_imageIndex
== -1)
341 m_info
[i
].m_imageIndex
= i
;
344 // if some frame has an invalid delay, use the global delay given in the
346 for (unsigned int i
=0; i
<m_nFrames
; i
++)
347 if (m_info
[i
].m_delay
== 0)
348 m_info
[i
].m_delay
= globaldelay
;
350 // if the header did not contain a valid frame size, try to grab
351 // it from the size of the first frame (all frames are of the same size)
352 if (m_szAnimation
.GetWidth() == 0 ||
353 m_szAnimation
.GetHeight() == 0)
354 m_szAnimation
= wxSize(m_images
[0].GetWidth(), m_images
[0].GetHeight());
356 return m_szAnimation
!= wxDefaultSize
;
359 #endif // wxUSE_STREAMS && wxUSE_ICO_CUR