blob: 9e980abafa4808ba8d5a5a499fcef7823cba3df2 [file] [log] [blame]
msarett8c8f22a2015-04-01 06:58:48 -07001/*
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"
scroggo19b91532016-10-24 09:03:26 -07009#include "SkCodecAnimation.h"
msarettad8bcfe2016-03-07 07:09:03 -080010#include "SkColorSpace.h"
msarett10522ff2015-09-07 08:54:01 -070011#include "SkColorTable.h"
msarett8c8f22a2015-04-01 06:58:48 -070012#include "SkImageInfo.h"
msarett10522ff2015-09-07 08:54:01 -070013#include "SkSwizzler.h"
msarett8c8f22a2015-04-01 06:58:48 -070014
scroggo3d3a65c2016-10-24 12:28:30 -070015#include "SkGifImageReader.h"
msarett8c8f22a2015-04-01 06:58:48 -070016
17/*
18 *
19 * This class implements the decoding for gif images
20 *
21 */
22class SkGifCodec : public SkCodec {
23public:
scroggodb30be22015-12-08 18:54:13 -080024 static bool IsGif(const void*, size_t);
msarett8c8f22a2015-04-01 06:58:48 -070025
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*);
msarett10522ff2015-09-07 08:54:01 -070032
scroggo3d3a65c2016-10-24 12:28:30 -070033 // Callback for SkGifImageReader when a row is available.
scroggo19b91532016-10-24 09:03:26 -070034 bool haveDecodedRow(size_t frameIndex, const unsigned char* rowBegin,
35 size_t rowNumber, unsigned repeatCount, bool writeTransparentPixels);
msarett8c8f22a2015-04-01 06:58:48 -070036protected:
msarett438b2ad2015-04-09 12:43:10 -070037 /*
msarett10522ff2015-09-07 08:54:01 -070038 * Performs the full gif decode
msarett8c8f22a2015-04-01 06:58:48 -070039 */
40 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&,
msarette6dd0042015-10-09 11:07:34 -070041 SkPMColor*, int*, int*) override;
msarett8c8f22a2015-04-01 06:58:48 -070042
Hal Canarydb683012016-11-23 08:55:18 -070043 SkEncodedImageFormat onGetEncodedFormat() const override {
44 return SkEncodedImageFormat::kGIF;
msarett8c8f22a2015-04-01 06:58:48 -070045 }
46
scroggob427db12015-08-12 07:24:13 -070047 bool onRewind() override;
48
msarettf7eb6fc2016-09-13 09:04:11 -070049 uint64_t onGetFillValue(const SkImageInfo&) const override;
msarette6dd0042015-10-09 11:07:34 -070050
scroggo19b91532016-10-24 09:03:26 -070051 std::vector<FrameInfo> onGetFrameInfo() override;
scroggoe71b1a12016-11-01 08:28:28 -070052 int onGetRepetitionCount() override;
scroggo19b91532016-10-24 09:03:26 -070053
54 Result onStartIncrementalDecode(const SkImageInfo& /*dstInfo*/, void*, size_t,
55 const SkCodec::Options&, SkPMColor*, int*) override;
56
57 Result onIncrementalDecode(int*) override;
msarette6dd0042015-10-09 11:07:34 -070058
msarett8c8f22a2015-04-01 06:58:48 -070059private:
60
61 /*
msarett10522ff2015-09-07 08:54:01 -070062 * Initializes the color table that we will use for decoding.
63 *
64 * @param dstInfo Contains the requested dst color type.
scroggo19b91532016-10-24 09:03:26 -070065 * @param frameIndex Frame whose color table to use.
msarett10522ff2015-09-07 08:54:01 -070066 */
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -040067 void initializeColorTable(const SkImageInfo& dstInfo, size_t frameIndex);
msarett10522ff2015-09-07 08:54:01 -070068
69 /*
scroggo19b91532016-10-24 09:03:26 -070070 * Does necessary setup, including setting up the color table and swizzler,
71 * and reports color info to the client.
msarett10522ff2015-09-07 08:54:01 -070072 */
scroggo46c57472015-09-30 08:57:13 -070073 Result prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
msarett10522ff2015-09-07 08:54:01 -070074 int* inputColorCount, const Options& opts);
75
76 /*
77 * Initializes the swizzler.
78 *
scroggo19b91532016-10-24 09:03:26 -070079 * @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.
msarett10522ff2015-09-07 08:54:01 -070084 */
scroggo19b91532016-10-24 09:03:26 -070085 void initializeSwizzler(const SkImageInfo& dstInfo, size_t frameIndex);
msarett10522ff2015-09-07 08:54:01 -070086
msarette6dd0042015-10-09 11:07:34 -070087 SkSampler* getSampler(bool createIfNecessary) override {
88 SkASSERT(fSwizzler);
scroggo19b91532016-10-24 09:03:26 -070089 return fSwizzler.get();
msarette6dd0042015-10-09 11:07:34 -070090 }
scroggoe7fc14b2015-10-02 13:14:46 -070091
msarett10522ff2015-09-07 08:54:01 -070092 /*
scroggo19b91532016-10-24 09:03:26 -070093 * Recursive function to decode a frame.
msarett72261c02015-11-19 15:29:26 -080094 *
scroggo19b91532016-10-24 09:03:26 -070095 * @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.
msarett72261c02015-11-19 15:29:26 -0800114 */
scroggo19b91532016-10-24 09:03:26 -0700115 Result decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded);
msarett8c8f22a2015-04-01 06:58:48 -0700116
117 /*
Matt Sarett61eedeb2016-11-04 13:19:48 -0400118 * Swizzles and color xforms (if necessary) into dst.
119 */
120 void applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const;
121
122 /*
msarett8c8f22a2015-04-01 06:58:48 -0700123 * Creates an instance of the decoder
124 * Called only by NewFromStream
scroggo3d3a65c2016-10-24 12:28:30 -0700125 * Takes ownership of the SkGifImageReader
msarett8c8f22a2015-04-01 06:58:48 -0700126 */
scroggo3d3a65c2016-10-24 12:28:30 -0700127 SkGifCodec(const SkEncodedInfo&, const SkImageInfo&, SkGifImageReader*);
msarett8c8f22a2015-04-01 06:58:48 -0700128
scroggo3d3a65c2016-10-24 12:28:30 -0700129 std::unique_ptr<SkGifImageReader> fReader;
130 std::unique_ptr<uint8_t[]> fTmpBuffer;
131 std::unique_ptr<SkSwizzler> fSwizzler;
132 sk_sp<SkColorTable> fCurrColorTable;
scroggo19b91532016-10-24 09:03:26 -0700133 // 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.
scroggo3d3a65c2016-10-24 12:28:30 -0700137 bool fCurrColorTableIsReal;
scroggo19b91532016-10-24 09:03:26 -0700138 // Whether the background was filled.
scroggo3d3a65c2016-10-24 12:28:30 -0700139 bool fFilledBackground;
scroggo19b91532016-10-24 09:03:26 -0700140 // True on the first call to onIncrementalDecode. This value is passed to
141 // decodeFrame.
scroggo3d3a65c2016-10-24 12:28:30 -0700142 bool fFirstCallToIncrementalDecode;
scroggo19b91532016-10-24 09:03:26 -0700143
scroggo3d3a65c2016-10-24 12:28:30 -0700144 void* fDst;
145 size_t fDstRowBytes;
scroggo19b91532016-10-24 09:03:26 -0700146
147 // Updated inside haveDecodedRow when rows are decoded, unless we filled
148 // the background, in which case it is set once and left alone.
scroggo3d3a65c2016-10-24 12:28:30 -0700149 int fRowsDecoded;
Matt Sarett61eedeb2016-11-04 13:19:48 -0400150 std::unique_ptr<uint32_t[]> fXformBuffer;
151 bool fXformOnDecode;
msarett10522ff2015-09-07 08:54:01 -0700152
msarett8c8f22a2015-04-01 06:58:48 -0700153 typedef SkCodec INHERITED;
154};