scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* ***** BEGIN LICENSE BLOCK ***** |
| 3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 4 | * |
| 5 | * The contents of this file are subject to the Mozilla Public License Version |
| 6 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * http://www.mozilla.org/MPL/ |
| 9 | * |
| 10 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 12 | * for the specific language governing rights and limitations under the |
| 13 | * License. |
| 14 | * |
| 15 | * The Original Code is Mozilla Communicator client code. |
| 16 | * |
| 17 | * The Initial Developer of the Original Code is |
| 18 | * Netscape Communications Corporation. |
| 19 | * Portions created by the Initial Developer are Copyright (C) 1998 |
| 20 | * the Initial Developer. All Rights Reserved. |
| 21 | * |
| 22 | * Contributor(s): |
| 23 | * |
| 24 | * Alternatively, the contents of this file may be used under the terms of |
| 25 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 27 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 28 | * of those above. If you wish to allow use of your version of this file only |
| 29 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 30 | * use your version of this file under the terms of the MPL, indicate your |
| 31 | * decision by deleting the provisions above and replace them with the notice |
| 32 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 33 | * the provisions above, a recipient may use your version of this file under |
| 34 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 35 | * |
| 36 | * ***** END LICENSE BLOCK ***** */ |
| 37 | |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 38 | #ifndef SkGifImageReader_h |
| 39 | #define SkGifImageReader_h |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 40 | |
| 41 | // Define ourselves as the clientPtr. Mozilla just hacked their C++ callback class into this old C decoder, |
| 42 | // so we will too. |
| 43 | class SkGifCodec; |
| 44 | |
| 45 | #include "SkCodec.h" |
| 46 | #include "SkCodecPriv.h" |
| 47 | #include "SkCodecAnimation.h" |
| 48 | #include "SkColorTable.h" |
| 49 | #include "SkData.h" |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 50 | #include "SkFrameHolder.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 51 | #include "SkImageInfo.h" |
| 52 | #include "SkStreamBuffer.h" |
| 53 | #include "../private/SkTArray.h" |
| 54 | #include <memory> |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 55 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 56 | typedef SkTArray<unsigned char, true> SkGIFRow; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 57 | |
| 58 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 59 | #define SK_MAX_DICTIONARY_ENTRY_BITS 12 |
| 60 | #define SK_MAX_DICTIONARY_ENTRIES 4096 // 2^SK_MAX_DICTIONARY_ENTRY_BITS |
| 61 | #define SK_MAX_COLORS 256 |
| 62 | #define SK_BYTES_PER_COLORMAP_ENTRY 3 |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 63 | |
| 64 | // List of possible parsing states. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 65 | enum SkGIFState { |
| 66 | SkGIFType, |
| 67 | SkGIFGlobalHeader, |
| 68 | SkGIFGlobalColormap, |
| 69 | SkGIFImageStart, |
| 70 | SkGIFImageHeader, |
| 71 | SkGIFImageColormap, |
| 72 | SkGIFImageBody, |
| 73 | SkGIFLZWStart, |
| 74 | SkGIFLZW, |
| 75 | SkGIFSubBlock, |
| 76 | SkGIFExtension, |
| 77 | SkGIFControlExtension, |
| 78 | SkGIFConsumeBlock, |
| 79 | SkGIFSkipBlock, |
| 80 | SkGIFDone, |
| 81 | SkGIFCommentExtension, |
| 82 | SkGIFApplicationExtension, |
| 83 | SkGIFNetscapeExtensionBlock, |
| 84 | SkGIFConsumeNetscapeExtension, |
| 85 | SkGIFConsumeComment |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 88 | class SkGIFFrameContext; |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 89 | class SkGIFColorMap; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 90 | |
| 91 | // LZW decoder state machine. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 92 | class SkGIFLZWContext final : public SkNoncopyable { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 93 | public: |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 94 | SkGIFLZWContext(SkGifCodec* client, const SkGIFFrameContext* frameContext) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 95 | : codesize(0) |
| 96 | , codemask(0) |
| 97 | , clearCode(0) |
| 98 | , avail(0) |
| 99 | , oldcode(0) |
| 100 | , firstchar(0) |
| 101 | , bits(0) |
| 102 | , datum(0) |
| 103 | , ipass(0) |
| 104 | , irow(0) |
| 105 | , rowsRemaining(0) |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 106 | , rowIter(nullptr) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 107 | , m_client(client) |
| 108 | , m_frameContext(frameContext) |
| 109 | { } |
| 110 | |
Leon Scroggins III | 45565b6 | 2016-12-05 14:56:30 -0500 | [diff] [blame] | 111 | bool prepareToDecode(); |
Leon Scroggins III | 223ec29 | 2017-08-22 14:13:15 -0400 | [diff] [blame] | 112 | void outputRow(const unsigned char* rowBegin); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 113 | bool doLZW(const unsigned char* block, size_t bytesInBlock); |
Jim Van Verth | 3cfdf6c | 2016-10-26 09:45:23 -0400 | [diff] [blame] | 114 | bool hasRemainingRows() { return SkToBool(rowsRemaining); } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 115 | |
| 116 | private: |
| 117 | // LZW decoding states and output states. |
| 118 | int codesize; |
| 119 | int codemask; |
| 120 | int clearCode; // Codeword used to trigger dictionary reset. |
| 121 | int avail; // Index of next available slot in dictionary. |
| 122 | int oldcode; |
| 123 | unsigned char firstchar; |
| 124 | int bits; // Number of unread bits in "datum". |
| 125 | int datum; // 32-bit input buffer. |
| 126 | int ipass; // Interlace pass; Ranges 1-4 if interlaced. |
| 127 | size_t irow; // Current output row, starting at zero. |
| 128 | size_t rowsRemaining; // Rows remaining to be output. |
| 129 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 130 | unsigned short prefix[SK_MAX_DICTIONARY_ENTRIES]; |
| 131 | unsigned char suffix[SK_MAX_DICTIONARY_ENTRIES]; |
| 132 | unsigned short suffixLength[SK_MAX_DICTIONARY_ENTRIES]; |
| 133 | SkGIFRow rowBuffer; // Single scanline temporary buffer. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 134 | unsigned char* rowIter; |
| 135 | |
| 136 | SkGifCodec* const m_client; |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 137 | const SkGIFFrameContext* m_frameContext; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 140 | struct SkGIFLZWBlock { |
| 141 | public: |
| 142 | SkGIFLZWBlock(size_t position, size_t size) |
| 143 | : blockPosition(position), blockSize(size) {} |
| 144 | |
Chris Blume | d5c5ed5 | 2016-12-20 18:59:39 -0800 | [diff] [blame] | 145 | size_t blockPosition; |
| 146 | size_t blockSize; |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 147 | }; |
| 148 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 149 | class SkGIFColorMap final { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 150 | public: |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 151 | static constexpr int kNotFound = -1; |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 152 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 153 | SkGIFColorMap() |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 154 | : m_isDefined(false) |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 155 | , m_position(0) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 156 | , m_colors(0) |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 157 | , m_transPixel(kNotFound) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 158 | , m_packColorProc(nullptr) |
| 159 | { |
| 160 | } |
| 161 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 162 | void setNumColors(int colors) { |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 163 | SkASSERT(!m_colors); |
| 164 | SkASSERT(!m_position); |
| 165 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 166 | m_colors = colors; |
| 167 | } |
| 168 | |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 169 | void setTablePosition(size_t position) { |
| 170 | SkASSERT(!m_isDefined); |
| 171 | |
| 172 | m_position = position; |
| 173 | m_isDefined = true; |
| 174 | } |
| 175 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 176 | int numColors() const { return m_colors; } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 177 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 178 | bool isDefined() const { return m_isDefined; } |
| 179 | |
| 180 | // Build RGBA table using the data stream. |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 181 | sk_sp<SkColorTable> buildTable(SkStreamBuffer*, SkColorType dstColorType, |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 182 | int transparentPixel) const; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 183 | |
| 184 | private: |
| 185 | bool m_isDefined; |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 186 | size_t m_position; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 187 | int m_colors; |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 188 | // Cached values. If these match on a new request, we can reuse m_table. |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 189 | mutable int m_transPixel; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 190 | mutable PackColorProc m_packColorProc; |
| 191 | mutable sk_sp<SkColorTable> m_table; |
| 192 | }; |
| 193 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 194 | class SkGifImageReader; |
| 195 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 196 | // LocalFrame output state machine. |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 197 | class SkGIFFrameContext : public SkFrame { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 198 | public: |
Chris Blume | 6c08b7b | 2017-09-28 10:23:26 -0700 | [diff] [blame^] | 199 | SkGIFFrameContext(int id) |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 200 | : INHERITED(id) |
Leon Scroggins III | b0b625b | 2016-12-22 16:40:24 -0500 | [diff] [blame] | 201 | , m_transparentPixel(SkGIFColorMap::kNotFound) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 202 | , m_dataSize(0) |
| 203 | , m_progressiveDisplay(false) |
| 204 | , m_interlaced(false) |
| 205 | , m_delayTime(0) |
| 206 | , m_currentLzwBlock(0) |
| 207 | , m_isComplete(false) |
| 208 | , m_isHeaderDefined(false) |
| 209 | , m_isDataSizeDefined(false) |
| 210 | { |
| 211 | } |
| 212 | |
Kevin Lubick | 700a79c | 2017-05-24 08:34:16 -0400 | [diff] [blame] | 213 | ~SkGIFFrameContext() override |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 214 | { |
| 215 | } |
| 216 | |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 217 | void addLzwBlock(size_t position, size_t size) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 218 | { |
Leon Scroggins III | e750391 | 2017-08-30 10:09:42 -0400 | [diff] [blame] | 219 | m_lzwBlocks.emplace_back(position, size); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 222 | bool decode(SkStreamBuffer*, SkGifCodec* client, bool* frameDecoded); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 223 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 224 | int transparentPixel() const { return m_transparentPixel; } |
| 225 | void setTransparentPixel(int pixel) { m_transparentPixel = pixel; } |
Leon Scroggins III | e4ba105 | 2017-01-30 13:55:14 -0500 | [diff] [blame] | 226 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 227 | unsigned delayTime() const { return m_delayTime; } |
| 228 | void setDelayTime(unsigned delay) { m_delayTime = delay; } |
| 229 | bool isComplete() const { return m_isComplete; } |
| 230 | void setComplete() { m_isComplete = true; } |
| 231 | bool isHeaderDefined() const { return m_isHeaderDefined; } |
| 232 | void setHeaderDefined() { m_isHeaderDefined = true; } |
| 233 | bool isDataSizeDefined() const { return m_isDataSizeDefined; } |
| 234 | int dataSize() const { return m_dataSize; } |
| 235 | void setDataSize(int size) |
| 236 | { |
| 237 | m_dataSize = size; |
| 238 | m_isDataSizeDefined = true; |
| 239 | } |
| 240 | bool progressiveDisplay() const { return m_progressiveDisplay; } |
| 241 | void setProgressiveDisplay(bool progressiveDisplay) { m_progressiveDisplay = progressiveDisplay; } |
| 242 | bool interlaced() const { return m_interlaced; } |
| 243 | void setInterlaced(bool interlaced) { m_interlaced = interlaced; } |
| 244 | |
| 245 | void clearDecodeState() { m_lzwContext.reset(); } |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 246 | const SkGIFColorMap& localColorMap() const { return m_localColorMap; } |
| 247 | SkGIFColorMap& localColorMap() { return m_localColorMap; } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 248 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 249 | protected: |
| 250 | bool onReportsAlpha() const override; |
Leon Scroggins III | e4ba105 | 2017-01-30 13:55:14 -0500 | [diff] [blame] | 251 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 252 | private: |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 253 | int m_transparentPixel; // Index of transparent pixel. Value is kNotFound if there is no transparent pixel. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 254 | int m_dataSize; |
| 255 | |
| 256 | bool m_progressiveDisplay; // If true, do Haeberli interlace hack. |
| 257 | bool m_interlaced; // True, if scanlines arrive interlaced order. |
| 258 | |
| 259 | unsigned m_delayTime; // Display time, in milliseconds, for this image in a multi-image GIF. |
| 260 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 261 | std::unique_ptr<SkGIFLZWContext> m_lzwContext; |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 262 | // LZW blocks for this frame. |
Leon Scroggins III | e750391 | 2017-08-30 10:09:42 -0400 | [diff] [blame] | 263 | SkTArray<SkGIFLZWBlock> m_lzwBlocks; |
Leon Scroggins III | 932efed | 2016-12-16 11:39:51 -0500 | [diff] [blame] | 264 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 265 | SkGIFColorMap m_localColorMap; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 266 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 267 | int m_currentLzwBlock; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 268 | bool m_isComplete; |
| 269 | bool m_isHeaderDefined; |
| 270 | bool m_isDataSizeDefined; |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 271 | |
| 272 | typedef SkFrame INHERITED; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 275 | class SkGifImageReader final : public SkFrameHolder { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 276 | public: |
| 277 | // This takes ownership of stream. |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 278 | SkGifImageReader(std::unique_ptr<SkStream> stream) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 279 | : m_client(nullptr) |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 280 | , m_state(SkGIFType) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 281 | , m_bytesToConsume(6) // Number of bytes for GIF type, either "GIF87a" or "GIF89a". |
| 282 | , m_version(0) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 283 | , m_loopCount(cLoopCountNotSeen) |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 284 | , m_streamBuffer(std::move(stream)) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 285 | , m_parseCompleted(false) |
| 286 | , m_firstFrameHasAlpha(false) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 287 | { |
| 288 | } |
| 289 | |
Kevin Lubick | 700a79c | 2017-05-24 08:34:16 -0400 | [diff] [blame] | 290 | ~SkGifImageReader() override |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 291 | { |
| 292 | } |
| 293 | |
| 294 | void setClient(SkGifCodec* client) { m_client = client; } |
| 295 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 296 | // Option to pass to parse(). All enums are negative, because a non-negative value is used to |
| 297 | // indicate that the Reader should parse up to and including the frame indicated. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 298 | enum SkGIFParseQuery { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 299 | // Parse enough to determine the size. Note that this parses the first frame's header, |
| 300 | // since we may decide to expand based on the frame's dimensions. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 301 | SkGIFSizeQuery = -1, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 302 | // Parse to the end, so we know about all frames. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 303 | SkGIFFrameCountQuery = -2, |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 304 | // Parse until we see the loop count. |
| 305 | SkGIFLoopCountQuery = -3, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | // Parse incoming GIF data stream into internal data structures. |
| 309 | // Non-negative values are used to indicate to parse through that frame. |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 310 | SkCodec::Result parse(SkGIFParseQuery); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 311 | |
| 312 | // Decode the frame indicated by frameIndex. |
| 313 | // frameComplete will be set to true if the frame is completely decoded. |
| 314 | // The method returns false if there is an error. |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 315 | bool decode(int frameIndex, bool* frameComplete); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 316 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 317 | int imagesCount() const |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 318 | { |
Leon Scroggins III | e750391 | 2017-08-30 10:09:42 -0400 | [diff] [blame] | 319 | const int frames = m_frames.count(); |
Leon Scroggins III | e726e7c | 2017-07-18 16:22:52 -0400 | [diff] [blame] | 320 | if (!frames) { |
| 321 | return 0; |
Leon Scroggins III | e4ba105 | 2017-01-30 13:55:14 -0500 | [diff] [blame] | 322 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 323 | |
Leon Scroggins III | e4ba105 | 2017-01-30 13:55:14 -0500 | [diff] [blame] | 324 | // This avoids counting an empty frame when the file is truncated (or |
| 325 | // simply not yet complete) after receiving SkGIFControlExtension (and |
| 326 | // possibly SkGIFImageHeader) but before reading the color table. This |
| 327 | // ensures that we do not count a frame before we know its required |
| 328 | // frame. |
Leon Scroggins III | e750391 | 2017-08-30 10:09:42 -0400 | [diff] [blame] | 329 | return m_frames.back()->reachedStartOfData() ? frames : frames - 1; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 330 | } |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 331 | int loopCount() const { |
| 332 | if (cLoopCountNotSeen == m_loopCount) { |
| 333 | return 0; |
| 334 | } |
| 335 | return m_loopCount; |
| 336 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 337 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 338 | const SkGIFColorMap& globalColorMap() const |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 339 | { |
| 340 | return m_globalColorMap; |
| 341 | } |
| 342 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 343 | const SkGIFFrameContext* frameContext(int index) const |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 344 | { |
Leon Scroggins III | e750391 | 2017-08-30 10:09:42 -0400 | [diff] [blame] | 345 | return index >= 0 && index < m_frames.count() |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 346 | ? m_frames[index].get() : nullptr; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void clearDecodeState() { |
Leon Scroggins III | e750391 | 2017-08-30 10:09:42 -0400 | [diff] [blame] | 350 | for (int index = 0; index < m_frames.count(); index++) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 351 | m_frames[index]->clearDecodeState(); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // Return the color table for frame index (which may be the global color table). |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 356 | sk_sp<SkColorTable> getColorTable(SkColorType dstColorType, int index); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 357 | |
| 358 | bool firstFrameHasAlpha() const { return m_firstFrameHasAlpha; } |
| 359 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 360 | protected: |
| 361 | const SkFrame* onGetFrame(int i) const override { |
| 362 | return static_cast<const SkFrame*>(this->frameContext(i)); |
| 363 | } |
| 364 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 365 | private: |
| 366 | // Requires that one byte has been buffered into m_streamBuffer. |
| 367 | unsigned char getOneByte() const { |
| 368 | return reinterpret_cast<const unsigned char*>(m_streamBuffer.get())[0]; |
| 369 | } |
| 370 | |
| 371 | void addFrameIfNecessary(); |
| 372 | bool currentFrameIsFirstFrame() const |
| 373 | { |
Leon Scroggins III | e750391 | 2017-08-30 10:09:42 -0400 | [diff] [blame] | 374 | return m_frames.empty() || (m_frames.count() == 1 && !m_frames[0]->isComplete()); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // Unowned pointer |
| 378 | SkGifCodec* m_client; |
| 379 | |
| 380 | // Parsing state machine. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 381 | SkGIFState m_state; // Current decoder master state. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 382 | size_t m_bytesToConsume; // Number of bytes to consume for next stage of parsing. |
| 383 | |
| 384 | // Global (multi-image) state. |
| 385 | int m_version; // Either 89 for GIF89 or 87 for GIF87. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 386 | SkGIFColorMap m_globalColorMap; |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 387 | |
| 388 | static constexpr int cLoopCountNotSeen = -2; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 389 | int m_loopCount; // Netscape specific extension block to control the number of animation loops a GIF renders. |
| 390 | |
Leon Scroggins III | e750391 | 2017-08-30 10:09:42 -0400 | [diff] [blame] | 391 | SkTArray<std::unique_ptr<SkGIFFrameContext>> m_frames; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 392 | |
| 393 | SkStreamBuffer m_streamBuffer; |
| 394 | bool m_parseCompleted; |
| 395 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 396 | // This value can be computed before we create a SkGIFFrameContext, so we |
| 397 | // store it here instead of on m_frames[0]. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 398 | bool m_firstFrameHasAlpha; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 399 | }; |
| 400 | |
| 401 | #endif |