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