GifAnimation.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #pragma once
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <memory>
  5. #include <vector>
  6. namespace WallpaperEngine::Render {
  7. /**
  8. * Animated GIF player for user-picked .gif image properties, a port of WE 2.8.42's resourceutil64 GIF exports
  9. * (PrepareGIF/OpenGIF/AdvanceGIF/FreeGIF). Their decoder is an older stb_image variant: frames come out at the
  10. * logical screen size, disposal 2 fills with a palette colour instead of the previous background, and the
  11. * stream rewinds to the first frame after the trailer.
  12. */
  13. class GifAnimation {
  14. public:
  15. /** nullptr when the data isn't a GIF (PrepareGIF's stbi__gif_test) */
  16. static std::unique_ptr<GifAnimation> open (const void* data, size_t size);
  17. /**
  18. * AdvanceGIF: steps the frame timer by dt seconds and decodes the next frame once it runs out. The first call
  19. * always decodes. Returns true when pixels () holds a new frame
  20. */
  21. bool advance (float dt);
  22. [[nodiscard]] const uint8_t* pixels () const { return m_out.data (); }
  23. [[nodiscard]] int width () const { return m_width; }
  24. [[nodiscard]] int height () const { return m_height; }
  25. /** Logical screen size from the GIF header, without decoding anything */
  26. static bool canvasSize (const void* data, size_t size, int& width, int& height);
  27. private:
  28. struct Code {
  29. int16_t prefix;
  30. uint8_t first;
  31. uint8_t suffix;
  32. };
  33. enum class Result { Frame, Trailer, Error };
  34. GifAnimation () = default;
  35. uint8_t get8 ();
  36. int get16le ();
  37. void skip (int count);
  38. bool readHeader ();
  39. void readColorTable (uint8_t (*palette)[4], int count, int transparent);
  40. Result loadNext (const uint8_t* twoBack, bool restart);
  41. bool decodeRaster ();
  42. void outputCode (uint16_t code);
  43. std::vector<uint8_t> m_data;
  44. size_t m_cursor = 0;
  45. int m_width = 0;
  46. int m_height = 0;
  47. std::vector<uint8_t> m_out;
  48. std::vector<uint8_t> m_history;
  49. int m_flags = 0;
  50. int m_bgIndex = -1;
  51. int m_transparent = -1;
  52. int m_eflags = 0;
  53. uint8_t m_palette[256][4] {};
  54. uint8_t m_localPalette[256][4] {};
  55. Code m_codes[8192] {};
  56. const uint8_t* m_colorTable = nullptr;
  57. int m_parse = 0;
  58. int m_step = 0;
  59. int m_startX = 0;
  60. int m_startY = 0;
  61. int m_maxX = 0;
  62. int m_maxY = 0;
  63. int m_curX = 0;
  64. int m_curY = 0;
  65. int m_lineSize = 0;
  66. int m_delay = 0;
  67. /** the last frame whose disposal keeps it (0 or 1), what disposal 3 restores to */
  68. std::vector<uint8_t> m_twoBack;
  69. float m_timer = 0.0f;
  70. bool m_first = true;
  71. int m_lastWidth = 0;
  72. int m_lastHeight = 0;
  73. };
  74. } // namespace WallpaperEngine::Render