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