msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 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 | */ |
Hal Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame] | 7 | #ifndef SkGifCodec_DEFINED |
| 8 | #define SkGifCodec_DEFINED |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 9 | |
| 10 | #include "SkCodec.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 11 | #include "SkCodecAnimation.h" |
msarett | ad8bcfe | 2016-03-07 07:09:03 -0800 | [diff] [blame] | 12 | #include "SkColorSpace.h" |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 13 | #include "SkColorTable.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 14 | #include "SkImageInfo.h" |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 15 | #include "SkSwizzler.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 16 | |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 17 | #include "SkGifImageReader.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * |
| 21 | * This class implements the decoding for gif images |
| 22 | * |
| 23 | */ |
| 24 | class SkGifCodec : public SkCodec { |
| 25 | public: |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 26 | static bool IsGif(const void*, size_t); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Assumes IsGif was called and returned true |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 30 | * Reads enough of the stream to determine the image format |
| 31 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 32 | static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 33 | |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 34 | // Callback for SkGifImageReader when a row is available. |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 35 | bool haveDecodedRow(int frameIndex, const unsigned char* rowBegin, |
| 36 | int rowNumber, int repeatCount, bool writeTransparentPixels); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 37 | protected: |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 38 | /* |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 39 | * Performs the full gif decode |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 40 | */ |
| 41 | Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 42 | int*) override; |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 43 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 44 | SkEncodedImageFormat onGetEncodedFormat() const override { |
| 45 | return SkEncodedImageFormat::kGIF; |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 46 | } |
| 47 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 48 | bool onRewind() override; |
| 49 | |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 50 | uint64_t onGetFillValue(const SkImageInfo&) const override; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 51 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 52 | int onGetFrameCount() override; |
| 53 | bool onGetFrameInfo(int, FrameInfo*) const override; |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 54 | int onGetRepetitionCount() override; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 55 | |
| 56 | Result onStartIncrementalDecode(const SkImageInfo& /*dstInfo*/, void*, size_t, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 57 | const SkCodec::Options&) override; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 58 | |
| 59 | Result onIncrementalDecode(int*) override; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 60 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 61 | const SkFrameHolder* getFrameHolder() const override { |
| 62 | return fReader.get(); |
| 63 | } |
| 64 | |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 65 | private: |
| 66 | |
| 67 | /* |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 68 | * Initializes the color table that we will use for decoding. |
| 69 | * |
| 70 | * @param dstInfo Contains the requested dst color type. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 71 | * @param frameIndex Frame whose color table to use. |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 72 | */ |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 73 | void initializeColorTable(const SkImageInfo& dstInfo, int frameIndex); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 74 | |
| 75 | /* |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 76 | * Does necessary setup, including setting up the color table and swizzler. |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 77 | */ |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 78 | Result prepareToDecode(const SkImageInfo& dstInfo, const Options& opts); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * Initializes the swizzler. |
| 82 | * |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 83 | * @param dstInfo Output image information. Dimensions may have been |
| 84 | * adjusted if the image frame size does not match the size |
| 85 | * indicated in the header. |
| 86 | * @param frameIndex Which frame we are decoding. This determines the frameRect |
| 87 | * to use. |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 88 | */ |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 89 | void initializeSwizzler(const SkImageInfo& dstInfo, int frameIndex); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 90 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 91 | SkSampler* getSampler(bool createIfNecessary) override { |
| 92 | SkASSERT(fSwizzler); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 93 | return fSwizzler.get(); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 94 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 95 | |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 96 | /* |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 97 | * Recursive function to decode a frame. |
msarett | 72261c0 | 2015-11-19 15:29:26 -0800 | [diff] [blame] | 98 | * |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 99 | * @param firstAttempt Whether this is the first call to decodeFrame since |
| 100 | * starting. e.g. true in onGetPixels, and true in the |
| 101 | * first call to onIncrementalDecode after calling |
| 102 | * onStartIncrementalDecode. |
| 103 | * When true, this method may have to initialize the |
| 104 | * frame, for example by filling or decoding the prior |
| 105 | * frame. |
| 106 | * @param opts Options for decoding. May be different from |
| 107 | * this->options() for decoding prior frames. Specifies |
| 108 | * the frame to decode and whether the prior frame has |
| 109 | * already been decoded to fDst. If not, and the frame |
| 110 | * is not independent, this method will recursively |
| 111 | * decode the frame it depends on. |
| 112 | * @param rowsDecoded Out-parameter to report the total number of rows |
| 113 | * that have been decoded (or at least written to, if |
| 114 | * it had to fill), including rows decoded by prior |
| 115 | * calls to onIncrementalDecode. |
| 116 | * @return kSuccess if the frame is complete, kIncompleteInput |
| 117 | * otherwise. |
msarett | 72261c0 | 2015-11-19 15:29:26 -0800 | [diff] [blame] | 118 | */ |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 119 | Result decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 120 | |
| 121 | /* |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 122 | * Swizzles and color xforms (if necessary) into dst. |
| 123 | */ |
| 124 | void applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const; |
| 125 | |
| 126 | /* |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 127 | * Creates an instance of the decoder |
| 128 | * Called only by NewFromStream |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 129 | * Takes ownership of the SkGifImageReader |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 130 | */ |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 131 | SkGifCodec(const SkEncodedInfo&, const SkImageInfo&, SkGifImageReader*); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 132 | |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 133 | std::unique_ptr<SkGifImageReader> fReader; |
| 134 | std::unique_ptr<uint8_t[]> fTmpBuffer; |
| 135 | std::unique_ptr<SkSwizzler> fSwizzler; |
| 136 | sk_sp<SkColorTable> fCurrColorTable; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 137 | // We may create a dummy table if there is not a Map in the input data. In |
| 138 | // that case, we set this value to false, and we can skip a lot of decoding |
| 139 | // work (which would not be meaningful anyway). We create a "fake"/"dummy" |
| 140 | // one in that case, so the client and the swizzler have something to draw. |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 141 | bool fCurrColorTableIsReal; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 142 | // Whether the background was filled. |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 143 | bool fFilledBackground; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 144 | // True on the first call to onIncrementalDecode. This value is passed to |
| 145 | // decodeFrame. |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 146 | bool fFirstCallToIncrementalDecode; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 147 | |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 148 | void* fDst; |
| 149 | size_t fDstRowBytes; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 150 | |
| 151 | // Updated inside haveDecodedRow when rows are decoded, unless we filled |
| 152 | // the background, in which case it is set once and left alone. |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 153 | int fRowsDecoded; |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 154 | std::unique_ptr<uint32_t[]> fXformBuffer; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 155 | |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 156 | typedef SkCodec INHERITED; |
| 157 | }; |
Hal Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame] | 158 | #endif // SkGifCodec_DEFINED |