blob: 970cc980baf548a1dbf8138107bd223499c5aaee [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
scroggo19b91532016-10-24 09:03:26 -07008/*
9 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "SkCodecAnimation.h"
msarett8c8f22a2015-04-01 06:58:48 -070034#include "SkCodecPriv.h"
Cary Clarka4083c92017-09-15 11:59:23 -040035#include "SkColorData.h"
msarett8c8f22a2015-04-01 06:58:48 -070036#include "SkColorTable.h"
msarett1a464672016-01-07 13:17:19 -080037#include "SkGifCodec.h"
Mike Reedede7bac2017-07-23 15:30:02 -040038#include "SkMakeUnique.h"
msarett8c8f22a2015-04-01 06:58:48 -070039#include "SkStream.h"
40#include "SkSwizzler.h"
msarett8c8f22a2015-04-01 06:58:48 -070041
scroggo19b91532016-10-24 09:03:26 -070042#include <algorithm>
43
44#define GIF87_STAMP "GIF87a"
45#define GIF89_STAMP "GIF89a"
46#define GIF_STAMP_LEN 6
msarett39b2d5a2016-02-17 08:26:31 -080047
msarett8c8f22a2015-04-01 06:58:48 -070048/*
49 * Checks the start of the stream to see if the image is a gif
50 */
scroggodb30be22015-12-08 18:54:13 -080051bool SkGifCodec::IsGif(const void* buf, size_t bytesRead) {
52 if (bytesRead >= GIF_STAMP_LEN) {
scroggo19b91532016-10-24 09:03:26 -070053 if (memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
bungeman0153dea2015-08-27 16:43:42 -070054 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0)
55 {
msarett8c8f22a2015-04-01 06:58:48 -070056 return true;
57 }
58 }
59 return false;
60}
61
62/*
msarett8c8f22a2015-04-01 06:58:48 -070063 * Error function
64 */
bungeman0153dea2015-08-27 16:43:42 -070065static SkCodec::Result gif_error(const char* msg, SkCodec::Result result = SkCodec::kInvalidInput) {
msarett8c8f22a2015-04-01 06:58:48 -070066 SkCodecPrintf("Gif Error: %s\n", msg);
67 return result;
68}
69
Mike Reedede7bac2017-07-23 15:30:02 -040070std::unique_ptr<SkCodec> SkGifCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
71 Result* result) {
72 std::unique_ptr<SkGifImageReader> reader(new SkGifImageReader(std::move(stream)));
Leon Scroggins III588fb042017-07-14 16:32:31 -040073 *result = reader->parse(SkGifImageReader::SkGIFSizeQuery);
74 if (*result != kSuccess) {
scroggo19b91532016-10-24 09:03:26 -070075 return nullptr;
msarett8c8f22a2015-04-01 06:58:48 -070076 }
msarett8c8f22a2015-04-01 06:58:48 -070077
Leon Scroggins III4993b952016-12-08 11:54:04 -050078 // If no images are in the data, or the first header is not yet defined, we cannot
79 // create a codec. In either case, the width and height are not yet known.
Leon Scroggins IIIe726e7c2017-07-18 16:22:52 -040080 auto* frame = reader->frameContext(0);
81 if (!frame || !frame->isHeaderDefined()) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040082 *result = kInvalidInput;
scroggo19b91532016-10-24 09:03:26 -070083 return nullptr;
84 }
85
Leon Scroggins III4993b952016-12-08 11:54:04 -050086 // isHeaderDefined() will not return true if the screen size is empty.
87 SkASSERT(reader->screenHeight() > 0 && reader->screenWidth() > 0);
88
scroggo19b91532016-10-24 09:03:26 -070089 const auto alpha = reader->firstFrameHasAlpha() ? SkEncodedInfo::kBinary_Alpha
90 : SkEncodedInfo::kOpaque_Alpha;
91 // Use kPalette since Gifs are encoded with a color table.
92 // FIXME: Gifs can actually be encoded with 4-bits per pixel. Using 8 works, but we could skip
93 // expanding to 8 bits and take advantage of the SkSwizzler to work from 4.
94 const auto encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, alpha, 8);
95
scroggo19b91532016-10-24 09:03:26 -070096 // The choice of unpremul versus premul is arbitrary, since all colors are either fully
97 // opaque or fully transparent (i.e. kBinary), but we stored the transparent colors as all
98 // zeroes, which is arguably premultiplied.
99 const auto alphaType = reader->firstFrameHasAlpha() ? kUnpremul_SkAlphaType
100 : kOpaque_SkAlphaType;
Matt Sarett7f650bd2016-10-30 21:25:34 -0400101
scroggo19b91532016-10-24 09:03:26 -0700102 const auto imageInfo = SkImageInfo::Make(reader->screenWidth(), reader->screenHeight(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000103 kN32_SkColorType, alphaType,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500104 SkColorSpace::MakeSRGB());
Mike Reedede7bac2017-07-23 15:30:02 -0400105 return std::unique_ptr<SkCodec>(new SkGifCodec(encodedInfo, imageInfo, reader.release()));
scroggo19b91532016-10-24 09:03:26 -0700106}
msarett8c8f22a2015-04-01 06:58:48 -0700107
scroggob427db12015-08-12 07:24:13 -0700108bool SkGifCodec::onRewind() {
scroggo19b91532016-10-24 09:03:26 -0700109 fReader->clearDecodeState();
scroggob427db12015-08-12 07:24:13 -0700110 return true;
111}
112
scroggo19b91532016-10-24 09:03:26 -0700113SkGifCodec::SkGifCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo,
scroggo3d3a65c2016-10-24 12:28:30 -0700114 SkGifImageReader* reader)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400115 : INHERITED(encodedInfo, imageInfo, SkColorSpaceXform::kRGBA_8888_ColorFormat, nullptr)
scroggo19b91532016-10-24 09:03:26 -0700116 , fReader(reader)
117 , fTmpBuffer(nullptr)
118 , fSwizzler(nullptr)
119 , fCurrColorTable(nullptr)
120 , fCurrColorTableIsReal(false)
121 , fFilledBackground(false)
122 , fFirstCallToIncrementalDecode(false)
123 , fDst(nullptr)
124 , fDstRowBytes(0)
125 , fRowsDecoded(0)
126{
127 reader->setClient(this);
msarett8c8f22a2015-04-01 06:58:48 -0700128}
msarett10522ff2015-09-07 08:54:01 -0700129
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400130int SkGifCodec::onGetFrameCount() {
scroggof9acbe22016-10-25 12:43:21 -0700131 fReader->parse(SkGifImageReader::SkGIFFrameCountQuery);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400132 return fReader->imagesCount();
133}
134
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400135bool SkGifCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400136 if (i >= fReader->imagesCount()) {
137 return false;
msarett10522ff2015-09-07 08:54:01 -0700138 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400139
140 const SkGIFFrameContext* frameContext = fReader->frameContext(i);
Leon Scroggins IIIe726e7c2017-07-18 16:22:52 -0400141 SkASSERT(frameContext->reachedStartOfData());
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400142
143 if (frameInfo) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400144 frameInfo->fDuration = frameContext->getDuration();
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400145 frameInfo->fRequiredFrame = frameContext->getRequiredFrame();
146 frameInfo->fFullyReceived = frameContext->isComplete();
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400147 frameInfo->fAlpha = frameContext->hasAlpha() ? SkEncodedInfo::kBinary_Alpha
148 : SkEncodedInfo::kOpaque_Alpha;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400149 frameInfo->fDisposalMethod = frameContext->getDisposalMethod();
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400150 }
151 return true;
msarett10522ff2015-09-07 08:54:01 -0700152}
153
scroggoe71b1a12016-11-01 08:28:28 -0700154int SkGifCodec::onGetRepetitionCount() {
155 fReader->parse(SkGifImageReader::SkGIFLoopCountQuery);
156 return fReader->loopCount();
157}
158
Matt Sarett562e6812016-11-08 16:13:43 -0500159static const SkColorType kXformSrcColorType = kRGBA_8888_SkColorType;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400160static const SkAlphaType kXformAlphaType = kUnpremul_SkAlphaType;
Matt Sarett562e6812016-11-08 16:13:43 -0500161
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400162void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, int frameIndex) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400163 SkColorType colorTableColorType = dstInfo.colorType();
164 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500165 colorTableColorType = kXformSrcColorType;
Matt Sarett61eedeb2016-11-04 13:19:48 -0400166 }
167
168 sk_sp<SkColorTable> currColorTable = fReader->getColorTable(colorTableColorType, frameIndex);
Hal Canary9a0e3902017-07-12 11:59:17 -0400169 fCurrColorTableIsReal = static_cast<bool>(currColorTable);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400170 if (!fCurrColorTableIsReal) {
Leon Scroggins IIIa049ac42016-10-27 11:16:11 -0400171 // This is possible for an empty frame. Create a dummy with one value (transparent).
172 SkPMColor color = SK_ColorTRANSPARENT;
173 fCurrColorTable.reset(new SkColorTable(&color, 1));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400174 } else if (this->colorXform() && !this->xformOnDecode()) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400175 SkPMColor dstColors[256];
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400176 this->applyColorXform(dstColors, currColorTable->readColors(), currColorTable->count(),
177 kXformAlphaType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400178 fCurrColorTable.reset(new SkColorTable(dstColors, currColorTable->count()));
179 } else {
180 fCurrColorTable = std::move(currColorTable);
msarett10522ff2015-09-07 08:54:01 -0700181 }
msarett10522ff2015-09-07 08:54:01 -0700182}
183
scroggo19b91532016-10-24 09:03:26 -0700184
Leon Scroggins571b30f2017-07-11 17:35:31 +0000185SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, const Options& opts) {
scroggo19b91532016-10-24 09:03:26 -0700186 if (opts.fSubset) {
187 return gif_error("Subsets not supported.\n", kUnimplemented);
188 }
189
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400190 const int frameIndex = opts.fFrameIndex;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400191 if (frameIndex > 0 && kRGB_565_SkColorType == dstInfo.colorType()) {
192 // FIXME: In theory, we might be able to support this, but it's not clear that it
193 // is necessary (Chromium does not decode to 565, and Android does not decode
194 // frames beyond the first). Disabling it because it is somewhat difficult:
195 // - If there is a transparent pixel, and this frame draws on top of another frame
196 // (if the frame is independent with a transparent pixel, we should not decode to
197 // 565 anyway, since it is not opaque), we need to skip drawing the transparent
198 // pixels (see writeTransparentPixels in haveDecodedRow). We currently do this by
199 // first swizzling into temporary memory, then copying into the destination. (We
200 // let the swizzler handle it first because it may need to sample.) After
201 // swizzling to 565, we do not know which pixels in our temporary memory
202 // correspond to the transparent pixel, so we do not know what to skip. We could
203 // special case the non-sampled case (no need to swizzle), but as this is
204 // currently unused we can just not support it.
205 return gif_error("Cannot decode multiframe gif (except frame 0) as 565.\n",
206 kInvalidConversion);
scroggo19b91532016-10-24 09:03:26 -0700207 }
208
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400209 const auto* frame = fReader->frameContext(frameIndex);
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400210 SkASSERT(frame);
211 if (0 == frameIndex) {
212 // SkCodec does not have a way to just parse through frame 0, so we
213 // have to do so manually, here.
214 fReader->parse((SkGifImageReader::SkGIFParseQuery) 0);
215 if (!frame->reachedStartOfData()) {
216 // We have parsed enough to know that there is a color map, but cannot
217 // parse the map itself yet. Exit now, so we do not build an incorrect
218 // table.
219 return gif_error("color map not available yet\n", kIncompleteInput);
220 }
221 } else {
222 // Parsing happened in SkCodec::getPixels.
223 SkASSERT(frameIndex < fReader->imagesCount());
224 SkASSERT(frame->reachedStartOfData());
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500225 }
226
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400227 if (this->xformOnDecode()) {
228 fXformBuffer.reset(new uint32_t[dstInfo.width()]);
229 sk_bzero(fXformBuffer.get(), dstInfo.width() * sizeof(uint32_t));
230 }
231
scroggo19b91532016-10-24 09:03:26 -0700232 fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]);
233
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -0400234 this->initializeColorTable(dstInfo, frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700235 this->initializeSwizzler(dstInfo, frameIndex);
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -0400236
237 SkASSERT(fCurrColorTable);
msarettb30d6982016-02-15 10:18:45 -0800238 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700239}
240
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400241void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, int frameIndex) {
scroggof9acbe22016-10-25 12:43:21 -0700242 const SkGIFFrameContext* frame = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700243 // This is only called by prepareToDecode, which ensures frameIndex is in range.
244 SkASSERT(frame);
msarett10522ff2015-09-07 08:54:01 -0700245
scroggo19b91532016-10-24 09:03:26 -0700246 const int xBegin = frame->xOffset();
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400247 const int xEnd = std::min(frame->frameRect().right(), fReader->screenWidth());
scroggo19b91532016-10-24 09:03:26 -0700248
249 // CreateSwizzler only reads left and right of the frame. We cannot use the frame's raw
250 // frameRect, since it might extend beyond the edge of the frame.
251 SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0);
252
Matt Sarett61eedeb2016-11-04 13:19:48 -0400253 SkImageInfo swizzlerInfo = dstInfo;
254 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500255 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400256 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
257 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
258 }
259 }
260
scroggo19b91532016-10-24 09:03:26 -0700261 // The default Options should be fine:
262 // - we'll ignore if the memory is zero initialized - unless we're the first frame, this won't
263 // matter anyway.
264 // - subsets are not supported for gif
265 // - the swizzler does not need to know about the frame.
266 // We may not be able to use the real Options anyway, since getPixels does not store it (due to
267 // a bug).
268 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(),
Matt Sarett61eedeb2016-11-04 13:19:48 -0400269 fCurrColorTable->readColors(), swizzlerInfo, Options(), &swizzleRect));
scroggo19b91532016-10-24 09:03:26 -0700270 SkASSERT(fSwizzler.get());
msarett10522ff2015-09-07 08:54:01 -0700271}
272
273/*
274 * Initiates the gif decode
275 */
276SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo,
scroggo19b91532016-10-24 09:03:26 -0700277 void* pixels, size_t dstRowBytes,
msarett10522ff2015-09-07 08:54:01 -0700278 const Options& opts,
msarette6dd0042015-10-09 11:07:34 -0700279 int* rowsDecoded) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000280 Result result = this->prepareToDecode(dstInfo, opts);
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500281 switch (result) {
282 case kSuccess:
283 break;
284 case kIncompleteInput:
285 // onStartIncrementalDecode treats this as incomplete, since it may
286 // provide more data later, but in this case, no more data will be
287 // provided, and there is nothing to draw. We also cannot return
288 // kIncompleteInput, which will make SkCodec attempt to fill
289 // remaining rows, but that requires an SkSwizzler, which we have
290 // not created.
291 return kInvalidInput;
292 default:
293 return result;
msarett10522ff2015-09-07 08:54:01 -0700294 }
295
296 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
297 return gif_error("Scaling not supported.\n", kInvalidScale);
298 }
299
scroggo19b91532016-10-24 09:03:26 -0700300 fDst = pixels;
301 fDstRowBytes = dstRowBytes;
302
303 return this->decodeFrame(true, opts, rowsDecoded);
304}
305
306SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
307 void* pixels, size_t dstRowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000308 const SkCodec::Options& opts) {
309 Result result = this->prepareToDecode(dstInfo, opts);
scroggo19b91532016-10-24 09:03:26 -0700310 if (result != kSuccess) {
311 return result;
msarett10522ff2015-09-07 08:54:01 -0700312 }
313
scroggo19b91532016-10-24 09:03:26 -0700314 fDst = pixels;
315 fDstRowBytes = dstRowBytes;
316
317 fFirstCallToIncrementalDecode = true;
318
msarett10522ff2015-09-07 08:54:01 -0700319 return kSuccess;
320}
321
scroggo19b91532016-10-24 09:03:26 -0700322SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) {
323 // It is possible the client has appended more data. Parse, if needed.
324 const auto& options = this->options();
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400325 const int frameIndex = options.fFrameIndex;
scroggof9acbe22016-10-25 12:43:21 -0700326 fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700327
328 const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode;
329 fFirstCallToIncrementalDecode = false;
330 return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700331}
332
scroggo19b91532016-10-24 09:03:26 -0700333SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded) {
334 const SkImageInfo& dstInfo = this->dstInfo();
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400335 const int frameIndex = opts.fFrameIndex;
scroggo19b91532016-10-24 09:03:26 -0700336 SkASSERT(frameIndex < fReader->imagesCount());
scroggof9acbe22016-10-25 12:43:21 -0700337 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700338 if (firstAttempt) {
339 // rowsDecoded reports how many rows have been initialized, so a layer above
340 // can fill the rest. In some cases, we fill the background before decoding
341 // (or it is already filled for us), so we report rowsDecoded to be the full
342 // height.
343 bool filledBackground = false;
344 if (frameContext->getRequiredFrame() == kNone) {
345 // We may need to clear to transparent for one of the following reasons:
346 // - The frameRect does not cover the full bounds. haveDecodedRow will
347 // only draw inside the frameRect, so we need to clear the rest.
scroggo19b91532016-10-24 09:03:26 -0700348 // - The frame is interlaced. There is no obvious way to fill
349 // afterwards for an incomplete image. (FIXME: Does the first pass
350 // cover all rows? If so, we do not have to fill here.)
scroggo8bce1172016-10-25 13:08:40 -0700351 // - There is no color table for this frame. In that case will not
352 // draw anything, so we need to fill.
scroggo19b91532016-10-24 09:03:26 -0700353 if (frameContext->frameRect() != this->getInfo().bounds()
scroggo8bce1172016-10-25 13:08:40 -0700354 || frameContext->interlaced() || !fCurrColorTableIsReal) {
scroggo19b91532016-10-24 09:03:26 -0700355 // fill ignores the width (replaces it with the actual, scaled width).
356 // But we need to scale in Y.
357 const int scaledHeight = get_scaled_dimension(dstInfo.height(),
358 fSwizzler->sampleY());
359 auto fillInfo = dstInfo.makeWH(0, scaledHeight);
360 fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue(dstInfo),
361 opts.fZeroInitialized);
362 filledBackground = true;
363 }
364 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400365 // Not independent.
366 // SkCodec ensured that the prior frame has been decoded.
scroggo19b91532016-10-24 09:03:26 -0700367 filledBackground = true;
msarett10522ff2015-09-07 08:54:01 -0700368 }
scroggo19b91532016-10-24 09:03:26 -0700369
370 fFilledBackground = filledBackground;
371 if (filledBackground) {
372 // Report the full (scaled) height, since the client will never need to fill.
373 fRowsDecoded = get_scaled_dimension(dstInfo.height(), fSwizzler->sampleY());
374 } else {
375 // This will be updated by haveDecodedRow.
376 fRowsDecoded = 0;
377 }
msarett10522ff2015-09-07 08:54:01 -0700378 }
msarette6dd0042015-10-09 11:07:34 -0700379
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500380 if (!fCurrColorTableIsReal) {
381 // Nothing to draw this frame.
382 return kSuccess;
383 }
384
scroggo19b91532016-10-24 09:03:26 -0700385 bool frameDecoded = false;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400386 const bool fatalError = !fReader->decode(frameIndex, &frameDecoded);
387 if (fatalError || !frameDecoded) {
scroggo19b91532016-10-24 09:03:26 -0700388 if (rowsDecoded) {
389 *rowsDecoded = fRowsDecoded;
390 }
Leon Scroggins III674a1842017-07-06 12:26:09 -0400391 if (fatalError) {
392 return kErrorInInput;
393 }
scroggo19b91532016-10-24 09:03:26 -0700394 return kIncompleteInput;
395 }
396
397 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700398}
scroggo46c57472015-09-30 08:57:13 -0700399
scroggo19b91532016-10-24 09:03:26 -0700400uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
scroggo19b91532016-10-24 09:03:26 -0700401 // Using transparent as the fill value matches the behavior in Chromium,
402 // which ignores the background color.
scroggo19b91532016-10-24 09:03:26 -0700403 return SK_ColorTRANSPARENT;
404}
msarett72261c02015-11-19 15:29:26 -0800405
Matt Sarett61eedeb2016-11-04 13:19:48 -0400406void SkGifCodec::applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400407 if (this->xformOnDecode()) {
408 SkASSERT(this->colorXform());
Matt Sarett61eedeb2016-11-04 13:19:48 -0400409 fSwizzler->swizzle(fXformBuffer.get(), src);
410
Matt Sarett61eedeb2016-11-04 13:19:48 -0400411 const int xformWidth = get_scaled_dimension(dstInfo.width(), fSwizzler->sampleX());
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400412 this->applyColorXform(dst, fXformBuffer.get(), xformWidth, kXformAlphaType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400413 } else {
414 fSwizzler->swizzle(dst, src);
415 }
416}
417
Leon Scroggins III1f1aa2d2017-09-05 14:17:19 -0400418template <typename T>
419static void blend_line(void* dstAsVoid, const void* srcAsVoid, int width) {
420 T* dst = reinterpret_cast<T*>(dstAsVoid);
421 const T* src = reinterpret_cast<const T*>(srcAsVoid);
422 while (width --> 0) {
423 if (*src != 0) { // GIF pixels are either transparent (== 0) or opaque (!= 0).
424 *dst = *src;
425 }
426 src++;
427 dst++;
428 }
429}
430
Leon Scroggins III223ec292017-08-22 14:13:15 -0400431void SkGifCodec::haveDecodedRow(int frameIndex, const unsigned char* rowBegin,
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400432 int rowNumber, int repeatCount, bool writeTransparentPixels)
scroggo19b91532016-10-24 09:03:26 -0700433{
scroggof9acbe22016-10-25 12:43:21 -0700434 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700435 // The pixel data and coordinates supplied to us are relative to the frame's
436 // origin within the entire image size, i.e.
437 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee
438 // that width == (size().width() - frameContext->xOffset), so
439 // we must ensure we don't run off the end of either the source data or the
440 // row's X-coordinates.
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400441 const int width = frameContext->width();
scroggo19b91532016-10-24 09:03:26 -0700442 const int xBegin = frameContext->xOffset();
443 const int yBegin = frameContext->yOffset() + rowNumber;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400444 const int xEnd = std::min(xBegin + width, this->getInfo().width());
445 const int yEnd = std::min(yBegin + rowNumber + repeatCount, this->getInfo().height());
scroggo19b91532016-10-24 09:03:26 -0700446 // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We could instead do
447 // this once in prepareToDecode.
448 if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin))
Leon Scroggins III223ec292017-08-22 14:13:15 -0400449 return;
scroggo19b91532016-10-24 09:03:26 -0700450
451 // yBegin is the first row in the non-sampled image. dstRow will be the row in the output,
452 // after potentially scaling it.
453 int dstRow = yBegin;
454
455 const int sampleY = fSwizzler->sampleY();
456 if (sampleY > 1) {
457 // Check to see whether this row or one that falls in the repeatCount is needed in the
458 // output.
459 bool foundNecessaryRow = false;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400460 for (int i = 0; i < repeatCount; i++) {
scroggo19b91532016-10-24 09:03:26 -0700461 const int potentialRow = yBegin + i;
462 if (fSwizzler->rowNeeded(potentialRow)) {
463 dstRow = potentialRow / sampleY;
464 const int scaledHeight = get_scaled_dimension(this->dstInfo().height(), sampleY);
465 if (dstRow >= scaledHeight) {
Leon Scroggins III223ec292017-08-22 14:13:15 -0400466 return;
scroggo19b91532016-10-24 09:03:26 -0700467 }
468
469 foundNecessaryRow = true;
470 repeatCount -= i;
471
472 repeatCount = (repeatCount - 1) / sampleY + 1;
473
474 // Make sure the repeatCount does not take us beyond the end of the dst
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400475 if (dstRow + repeatCount > scaledHeight) {
scroggo19b91532016-10-24 09:03:26 -0700476 repeatCount = scaledHeight - dstRow;
477 SkASSERT(repeatCount >= 1);
478 }
479 break;
480 }
481 }
482
483 if (!foundNecessaryRow) {
Leon Scroggins III223ec292017-08-22 14:13:15 -0400484 return;
scroggo19b91532016-10-24 09:03:26 -0700485 }
Matt Sarett8a4e9c52016-10-25 14:24:50 -0400486 } else {
487 // Make sure the repeatCount does not take us beyond the end of the dst
488 SkASSERT(this->dstInfo().height() >= yBegin);
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400489 repeatCount = SkTMin(repeatCount, this->dstInfo().height() - yBegin);
scroggo19b91532016-10-24 09:03:26 -0700490 }
491
492 if (!fFilledBackground) {
493 // At this point, we are definitely going to write the row, so count it towards the number
494 // of rows decoded.
495 // We do not consider the repeatCount, which only happens for interlaced, in which case we
496 // have already set fRowsDecoded to the proper value (reflecting that we have filled the
497 // background).
498 fRowsDecoded++;
499 }
500
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500501 // decodeFrame will early exit if this is false, so this method will not be
502 // called.
503 SkASSERT(fCurrColorTableIsReal);
scroggo19b91532016-10-24 09:03:26 -0700504
505 // The swizzler takes care of offsetting into the dst width-wise.
506 void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes);
507
508 // We may or may not need to write transparent pixels to the buffer.
scroggo1285f412016-10-26 13:48:03 -0700509 // If we're compositing against a previous image, it's wrong, but if
510 // we're decoding an interlaced gif and displaying it "Haeberli"-style,
511 // we must write these for passes beyond the first, or the initial passes
512 // will "show through" the later ones.
scroggo19b91532016-10-24 09:03:26 -0700513 const auto dstInfo = this->dstInfo();
scroggo53f63b62016-10-27 08:29:13 -0700514 if (writeTransparentPixels) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400515 this->applyXformRow(dstInfo, dstLine, rowBegin);
scroggo19b91532016-10-24 09:03:26 -0700516 } else {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400517 this->applyXformRow(dstInfo, fTmpBuffer.get(), rowBegin);
scroggo19b91532016-10-24 09:03:26 -0700518
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400519 size_t offsetBytes = fSwizzler->swizzleOffsetBytes();
520 if (dstInfo.colorType() == kRGBA_F16_SkColorType) {
521 // Account for the fact that post-swizzling we converted to F16,
522 // which is twice as wide.
523 offsetBytes *= 2;
524 }
Leon Scroggins III1f1aa2d2017-09-05 14:17:19 -0400525 const void* src = SkTAddOffset<void>(fTmpBuffer.get(), offsetBytes);
526 void* dst = SkTAddOffset<void>(dstLine, offsetBytes);
Mike Klein45c16fa2017-07-18 18:15:13 -0400527
scroggo19b91532016-10-24 09:03:26 -0700528 switch (dstInfo.colorType()) {
529 case kBGRA_8888_SkColorType:
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400530 case kRGBA_8888_SkColorType:
Leon Scroggins III1f1aa2d2017-09-05 14:17:19 -0400531 blend_line<uint32_t>(dst, src, fSwizzler->swizzleWidth());
scroggo19b91532016-10-24 09:03:26 -0700532 break;
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400533 case kRGBA_F16_SkColorType:
Leon Scroggins III1f1aa2d2017-09-05 14:17:19 -0400534 blend_line<uint64_t>(dst, src, fSwizzler->swizzleWidth());
scroggo19b91532016-10-24 09:03:26 -0700535 break;
scroggo19b91532016-10-24 09:03:26 -0700536 default:
537 SkASSERT(false);
Leon Scroggins III223ec292017-08-22 14:13:15 -0400538 return;
scroggo19b91532016-10-24 09:03:26 -0700539 }
540 }
541
542 // Tell the frame to copy the row data if need be.
543 if (repeatCount > 1) {
544 const size_t bytesPerPixel = SkColorTypeBytesPerPixel(this->dstInfo().colorType());
545 const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel;
546 void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetBytes());
547 void* dst = copiedLine;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400548 for (int i = 1; i < repeatCount; i++) {
scroggo19b91532016-10-24 09:03:26 -0700549 dst = SkTAddOffset<void>(dst, fDstRowBytes);
550 memcpy(dst, copiedLine, bytesToCopy);
msarett72261c02015-11-19 15:29:26 -0800551 }
552 }
msarett72261c02015-11-19 15:29:26 -0800553}