SkGifCodec.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkGifCodec_DEFINED
  8. #define SkGifCodec_DEFINED
  9. #include "include/codec/SkCodec.h"
  10. #include "include/codec/SkCodecAnimation.h"
  11. #include "include/core/SkColorSpace.h"
  12. #include "include/core/SkImageInfo.h"
  13. #include "src/codec/SkColorTable.h"
  14. #include "src/codec/SkSwizzler.h"
  15. #include "third_party/gif/SkGifImageReader.h"
  16. /*
  17. *
  18. * This class implements the decoding for gif images
  19. *
  20. */
  21. class SkGifCodec : public SkCodec {
  22. public:
  23. static bool IsGif(const void*, size_t);
  24. /*
  25. * Assumes IsGif was called and returned true
  26. * Reads enough of the stream to determine the image format
  27. */
  28. static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
  29. // Callback for SkGifImageReader when a row is available.
  30. void haveDecodedRow(int frameIndex, const unsigned char* rowBegin,
  31. int rowNumber, int repeatCount, bool writeTransparentPixels);
  32. protected:
  33. /*
  34. * Performs the full gif decode
  35. */
  36. Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&,
  37. int*) override;
  38. SkEncodedImageFormat onGetEncodedFormat() const override {
  39. return SkEncodedImageFormat::kGIF;
  40. }
  41. bool onRewind() override;
  42. int onGetFrameCount() override;
  43. bool onGetFrameInfo(int, FrameInfo*) const override;
  44. int onGetRepetitionCount() override;
  45. Result onStartIncrementalDecode(const SkImageInfo& /*dstInfo*/, void*, size_t,
  46. const SkCodec::Options&) override;
  47. Result onIncrementalDecode(int*) override;
  48. const SkFrameHolder* getFrameHolder() const override {
  49. return fReader.get();
  50. }
  51. private:
  52. /*
  53. * Initializes the color table that we will use for decoding.
  54. *
  55. * @param dstInfo Contains the requested dst color type.
  56. * @param frameIndex Frame whose color table to use.
  57. */
  58. void initializeColorTable(const SkImageInfo& dstInfo, int frameIndex);
  59. /*
  60. * Does necessary setup, including setting up the color table and swizzler.
  61. */
  62. Result prepareToDecode(const SkImageInfo& dstInfo, const Options& opts);
  63. /*
  64. * Initializes the swizzler.
  65. *
  66. * @param dstInfo Output image information. Dimensions may have been
  67. * adjusted if the image frame size does not match the size
  68. * indicated in the header.
  69. * @param frameIndex Which frame we are decoding. This determines the frameRect
  70. * to use.
  71. */
  72. void initializeSwizzler(const SkImageInfo& dstInfo, int frameIndex);
  73. SkSampler* getSampler(bool createIfNecessary) override {
  74. SkASSERT(fSwizzler);
  75. return fSwizzler.get();
  76. }
  77. /*
  78. * Recursive function to decode a frame.
  79. *
  80. * @param firstAttempt Whether this is the first call to decodeFrame since
  81. * starting. e.g. true in onGetPixels, and true in the
  82. * first call to onIncrementalDecode after calling
  83. * onStartIncrementalDecode.
  84. * When true, this method may have to initialize the
  85. * frame, for example by filling or decoding the prior
  86. * frame.
  87. * @param opts Options for decoding. May be different from
  88. * this->options() for decoding prior frames. Specifies
  89. * the frame to decode and whether the prior frame has
  90. * already been decoded to fDst. If not, and the frame
  91. * is not independent, this method will recursively
  92. * decode the frame it depends on.
  93. * @param rowsDecoded Out-parameter to report the total number of rows
  94. * that have been decoded (or at least written to, if
  95. * it had to fill), including rows decoded by prior
  96. * calls to onIncrementalDecode.
  97. * @return kSuccess if the frame is complete, kIncompleteInput
  98. * otherwise.
  99. */
  100. Result decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded);
  101. /*
  102. * Swizzles and color xforms (if necessary) into dst.
  103. */
  104. void applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const;
  105. /*
  106. * Creates an instance of the decoder
  107. * Called only by NewFromStream
  108. * Takes ownership of the SkGifImageReader
  109. */
  110. SkGifCodec(SkEncodedInfo&&, SkGifImageReader*);
  111. std::unique_ptr<SkGifImageReader> fReader;
  112. std::unique_ptr<uint8_t[]> fTmpBuffer;
  113. std::unique_ptr<SkSwizzler> fSwizzler;
  114. sk_sp<SkColorTable> fCurrColorTable;
  115. // We may create a dummy table if there is not a Map in the input data. In
  116. // that case, we set this value to false, and we can skip a lot of decoding
  117. // work (which would not be meaningful anyway). We create a "fake"/"dummy"
  118. // one in that case, so the client and the swizzler have something to draw.
  119. bool fCurrColorTableIsReal;
  120. // Whether the background was filled.
  121. bool fFilledBackground;
  122. // True on the first call to onIncrementalDecode. This value is passed to
  123. // decodeFrame.
  124. bool fFirstCallToIncrementalDecode;
  125. void* fDst;
  126. size_t fDstRowBytes;
  127. // Updated inside haveDecodedRow when rows are decoded, unless we filled
  128. // the background, in which case it is set once and left alone.
  129. int fRowsDecoded;
  130. std::unique_ptr<uint32_t[]> fXformBuffer;
  131. typedef SkCodec INHERITED;
  132. };
  133. #endif // SkGifCodec_DEFINED