animdecod.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/animdecod.h
  3. // Purpose: wxAnimationDecoder
  4. // Author: Francesco Montorsi
  5. // Copyright: (c) 2006 Francesco Montorsi
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. #ifndef _WX_ANIMDECOD_H
  9. #define _WX_ANIMDECOD_H
  10. #include "wx/defs.h"
  11. #if wxUSE_STREAMS
  12. #include "wx/colour.h"
  13. #include "wx/gdicmn.h"
  14. #include "wx/log.h"
  15. #include "wx/stream.h"
  16. class WXDLLIMPEXP_FWD_CORE wxImage;
  17. /*
  18. Differences between a wxAnimationDecoder and a wxImageHandler:
  19. 1) wxImageHandlers always load an input stream directly into a given wxImage
  20. object converting from the format-specific data representation to the
  21. wxImage native format (RGB24).
  22. wxAnimationDecoders always load an input stream using some optimized format
  23. to store it which is format-depedent. This allows to store a (possibly big)
  24. animation using a format which is a good compromise between required memory
  25. and time required to blit it on the screen.
  26. 2) wxAnimationDecoders contain the animation data in some internal variable.
  27. That's why they derive from wxObjectRefData: they are data which can be shared.
  28. 3) wxAnimationDecoders can be used by a wxImageHandler to retrieve a frame
  29. in wxImage format; the viceversa cannot be done.
  30. 4) wxAnimationDecoders are decoders only, thus they do not support save features.
  31. 5) wxAnimationDecoders are directly used by wxAnimation (generic implementation)
  32. as wxObjectRefData while they need to be 'wrapped' by a wxImageHandler for
  33. wxImage uses.
  34. */
  35. // --------------------------------------------------------------------------
  36. // Constants
  37. // --------------------------------------------------------------------------
  38. // NB: the values of these enum items are not casual but coincide with the
  39. // GIF disposal codes. Do not change them !!
  40. enum wxAnimationDisposal
  41. {
  42. // No disposal specified. The decoder is not required to take any action.
  43. wxANIM_UNSPECIFIED = -1,
  44. // Do not dispose. The graphic is to be left in place.
  45. wxANIM_DONOTREMOVE = 0,
  46. // Restore to background color. The area used by the graphic must be
  47. // restored to the background color.
  48. wxANIM_TOBACKGROUND = 1,
  49. // Restore to previous. The decoder is required to restore the area
  50. // overwritten by the graphic with what was there prior to rendering the graphic.
  51. wxANIM_TOPREVIOUS = 2
  52. };
  53. enum wxAnimationType
  54. {
  55. wxANIMATION_TYPE_INVALID,
  56. wxANIMATION_TYPE_GIF,
  57. wxANIMATION_TYPE_ANI,
  58. wxANIMATION_TYPE_ANY
  59. };
  60. // --------------------------------------------------------------------------
  61. // wxAnimationDecoder class
  62. // --------------------------------------------------------------------------
  63. class WXDLLIMPEXP_CORE wxAnimationDecoder : public wxObjectRefData
  64. {
  65. public:
  66. wxAnimationDecoder()
  67. {
  68. m_nFrames = 0;
  69. }
  70. virtual bool Load( wxInputStream& stream ) = 0;
  71. bool CanRead( wxInputStream& stream ) const
  72. {
  73. // NOTE: this code is the same of wxImageHandler::CallDoCanRead
  74. if ( !stream.IsSeekable() )
  75. return false; // can't test unseekable stream
  76. wxFileOffset posOld = stream.TellI();
  77. bool ok = DoCanRead(stream);
  78. // restore the old position to be able to test other formats and so on
  79. if ( stream.SeekI(posOld) == wxInvalidOffset )
  80. {
  81. wxLogDebug(wxT("Failed to rewind the stream in wxAnimationDecoder!"));
  82. // reading would fail anyhow as we're not at the right position
  83. return false;
  84. }
  85. return ok;
  86. }
  87. virtual wxAnimationDecoder *Clone() const = 0;
  88. virtual wxAnimationType GetType() const = 0;
  89. // convert given frame to wxImage
  90. virtual bool ConvertToImage(unsigned int frame, wxImage *image) const = 0;
  91. // frame specific data getters
  92. // not all frames may be of the same size; e.g. GIF allows to
  93. // specify that between two frames only a smaller portion of the
  94. // entire animation has changed.
  95. virtual wxSize GetFrameSize(unsigned int frame) const = 0;
  96. // the position of this frame in case it's not as big as m_szAnimation
  97. // or wxPoint(0,0) otherwise.
  98. virtual wxPoint GetFramePosition(unsigned int frame) const = 0;
  99. // what should be done after displaying this frame.
  100. virtual wxAnimationDisposal GetDisposalMethod(unsigned int frame) const = 0;
  101. // the number of milliseconds this frame should be displayed.
  102. // if returns -1 then the frame must be displayed forever.
  103. virtual long GetDelay(unsigned int frame) const = 0;
  104. // the transparent colour for this frame if any or wxNullColour.
  105. virtual wxColour GetTransparentColour(unsigned int frame) const = 0;
  106. // get global data
  107. wxSize GetAnimationSize() const { return m_szAnimation; }
  108. wxColour GetBackgroundColour() const { return m_background; }
  109. unsigned int GetFrameCount() const { return m_nFrames; }
  110. protected:
  111. // checks the signature of the data in the given stream and returns true if it
  112. // appears to be a valid animation format recognized by the animation decoder;
  113. // this function should modify the stream current position without taking care
  114. // of restoring it since CanRead() will do it.
  115. virtual bool DoCanRead(wxInputStream& stream) const = 0;
  116. wxSize m_szAnimation;
  117. unsigned int m_nFrames;
  118. // this is the colour to use for the wxANIM_TOBACKGROUND disposal.
  119. // if not specified by the animation, it's set to wxNullColour
  120. wxColour m_background;
  121. };
  122. #endif // wxUSE_STREAMS
  123. #endif // _WX_ANIMDECOD_H