1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        wx/gifdecod.h
3 // Purpose:     wxGIFDecoder, GIF reader for wxImage and wxAnimation
4 // Author:      Guillermo Rodriguez Garcia <guille@iies.es>
5 // Version:     3.02
6 // Copyright:   (c) 1999 Guillermo Rodriguez Garcia
7 // Licence:     wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9 
10 #ifndef _WX_GIFDECOD_H_
11 #define _WX_GIFDECOD_H_
12 
13 #include "wx/defs.h"
14 
15 #if wxUSE_STREAMS && wxUSE_GIF
16 
17 #include "wx/stream.h"
18 #include "wx/image.h"
19 #include "wx/animdecod.h"
20 #include "wx/dynarray.h"
21 
22 // internal utility used to store a frame in 8bit-per-pixel format
23 class GIFImage;
24 
25 
26 // --------------------------------------------------------------------------
27 // Constants
28 // --------------------------------------------------------------------------
29 
30 // Error codes:
31 //  Note that the error code wxGIF_TRUNCATED means that the image itself
32 //  is most probably OK, but the decoder didn't reach the end of the data
33 //  stream; this means that if it was not reading directly from file,
34 //  the stream will not be correctly positioned.
35 //
36 enum wxGIFErrorCode
37 {
38     wxGIF_OK = 0,                   // everything was OK
39     wxGIF_INVFORMAT,                // error in GIF header
40     wxGIF_MEMERR,                   // error allocating memory
41     wxGIF_TRUNCATED                 // file appears to be truncated
42 };
43 
44 // --------------------------------------------------------------------------
45 // wxGIFDecoder class
46 // --------------------------------------------------------------------------
47 
48 class WXDLLIMPEXP_CORE wxGIFDecoder : public wxAnimationDecoder
49 {
50 public:
51     // constructor, destructor, etc.
52     wxGIFDecoder();
53     ~wxGIFDecoder();
54 
55     // get data of current frame
56     unsigned char* GetData(unsigned int frame) const;
57     unsigned char* GetPalette(unsigned int frame) const;
58     unsigned int GetNcolours(unsigned int frame) const;
59     int GetTransparentColourIndex(unsigned int frame) const;
60     wxColour GetTransparentColour(unsigned int frame) const wxOVERRIDE;
61 
62     virtual wxSize GetFrameSize(unsigned int frame) const wxOVERRIDE;
63     virtual wxPoint GetFramePosition(unsigned int frame) const wxOVERRIDE;
64     virtual wxAnimationDisposal GetDisposalMethod(unsigned int frame) const wxOVERRIDE;
65     virtual long GetDelay(unsigned int frame) const wxOVERRIDE;
66 
67     // GIFs can contain both static images and animations
IsAnimation()68     bool IsAnimation() const
69         { return m_nFrames > 1; }
70 
71     // load function which returns more info than just Load():
72     wxGIFErrorCode LoadGIF( wxInputStream& stream );
73 
74     // free all internal frames
75     void Destroy();
76 
77     // implementation of wxAnimationDecoder's pure virtuals
Load(wxInputStream & stream)78     virtual bool Load( wxInputStream& stream ) wxOVERRIDE
79         { return LoadGIF(stream) == wxGIF_OK; }
80 
81     bool ConvertToImage(unsigned int frame, wxImage *image) const wxOVERRIDE;
82 
Clone()83     wxAnimationDecoder *Clone() const wxOVERRIDE
84         { return new wxGIFDecoder; }
GetType()85     wxAnimationType GetType() const wxOVERRIDE
86         { return wxANIMATION_TYPE_GIF; }
87 
88 protected:
89     // wxAnimationDecoder pure virtual
90     virtual bool DoCanRead( wxInputStream& stream ) const wxOVERRIDE;
91         // modifies current stream position (see wxAnimationDecoder::CanRead)
92 
93 private:
94     int getcode(wxInputStream& stream, int bits, int abfin);
95     wxGIFErrorCode dgif(wxInputStream& stream,
96                         GIFImage *img, int interl, int bits);
97 
98 
99     // array of all frames
100     wxArrayPtrVoid m_frames;
101 
102     // decoder state vars
103     int           m_restbits;       // remaining valid bits
104     unsigned int  m_restbyte;       // remaining bytes in this block
105     unsigned int  m_lastbyte;       // last byte read
106     unsigned char m_buffer[256];    // buffer for reading
107     unsigned char *m_bufp;          // pointer to next byte in buffer
108 
109     wxDECLARE_NO_COPY_CLASS(wxGIFDecoder);
110 };
111 
112 #endif // wxUSE_STREAMS && wxUSE_GIF
113 
114 #endif // _WX_GIFDECOD_H_
115