blob: 77160cb523f1820411a636f31bdae3428217f1c4 [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.
Leon Scroggins III36f7e322018-08-27 11:55:46 -040094 auto encodedInfo = SkEncodedInfo::MakeSRGB(reader->screenWidth(), reader->screenHeight(),
95 SkEncodedInfo::kPalette_Color, alpha, 8);
96 return std::unique_ptr<SkCodec>(new SkGifCodec(std::move(encodedInfo), reader.release()));
scroggo19b91532016-10-24 09:03:26 -070097}
msarett8c8f22a2015-04-01 06:58:48 -070098
scroggob427db12015-08-12 07:24:13 -070099bool SkGifCodec::onRewind() {
scroggo19b91532016-10-24 09:03:26 -0700100 fReader->clearDecodeState();
scroggob427db12015-08-12 07:24:13 -0700101 return true;
102}
103
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400104SkGifCodec::SkGifCodec(SkEncodedInfo&& encodedInfo, SkGifImageReader* reader)
105 : INHERITED(std::move(encodedInfo), skcms_PixelFormat_RGBA_8888, nullptr)
scroggo19b91532016-10-24 09:03:26 -0700106 , fReader(reader)
107 , fTmpBuffer(nullptr)
108 , fSwizzler(nullptr)
109 , fCurrColorTable(nullptr)
110 , fCurrColorTableIsReal(false)
111 , fFilledBackground(false)
112 , fFirstCallToIncrementalDecode(false)
113 , fDst(nullptr)
114 , fDstRowBytes(0)
115 , fRowsDecoded(0)
116{
117 reader->setClient(this);
msarett8c8f22a2015-04-01 06:58:48 -0700118}
msarett10522ff2015-09-07 08:54:01 -0700119
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400120int SkGifCodec::onGetFrameCount() {
scroggof9acbe22016-10-25 12:43:21 -0700121 fReader->parse(SkGifImageReader::SkGIFFrameCountQuery);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400122 return fReader->imagesCount();
123}
124
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400125bool SkGifCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400126 if (i >= fReader->imagesCount()) {
127 return false;
msarett10522ff2015-09-07 08:54:01 -0700128 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400129
130 const SkGIFFrameContext* frameContext = fReader->frameContext(i);
Leon Scroggins IIIe726e7c2017-07-18 16:22:52 -0400131 SkASSERT(frameContext->reachedStartOfData());
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400132
133 if (frameInfo) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400134 frameInfo->fDuration = frameContext->getDuration();
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400135 frameInfo->fRequiredFrame = frameContext->getRequiredFrame();
136 frameInfo->fFullyReceived = frameContext->isComplete();
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500137 frameInfo->fAlphaType = frameContext->hasAlpha() ? kUnpremul_SkAlphaType
138 : kOpaque_SkAlphaType;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400139 frameInfo->fDisposalMethod = frameContext->getDisposalMethod();
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400140 }
141 return true;
msarett10522ff2015-09-07 08:54:01 -0700142}
143
scroggoe71b1a12016-11-01 08:28:28 -0700144int SkGifCodec::onGetRepetitionCount() {
145 fReader->parse(SkGifImageReader::SkGIFLoopCountQuery);
146 return fReader->loopCount();
147}
148
Leon Scroggins III862c1962017-10-02 16:28:49 -0400149static constexpr SkColorType kXformSrcColorType = kRGBA_8888_SkColorType;
Matt Sarett562e6812016-11-08 16:13:43 -0500150
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400151void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, int frameIndex) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400152 SkColorType colorTableColorType = dstInfo.colorType();
153 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500154 colorTableColorType = kXformSrcColorType;
Matt Sarett61eedeb2016-11-04 13:19:48 -0400155 }
156
157 sk_sp<SkColorTable> currColorTable = fReader->getColorTable(colorTableColorType, frameIndex);
Hal Canary9a0e3902017-07-12 11:59:17 -0400158 fCurrColorTableIsReal = static_cast<bool>(currColorTable);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400159 if (!fCurrColorTableIsReal) {
Leon Scroggins IIIa049ac42016-10-27 11:16:11 -0400160 // This is possible for an empty frame. Create a dummy with one value (transparent).
161 SkPMColor color = SK_ColorTRANSPARENT;
162 fCurrColorTable.reset(new SkColorTable(&color, 1));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400163 } else if (this->colorXform() && !this->xformOnDecode()) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400164 SkPMColor dstColors[256];
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400165 this->applyColorXform(dstColors, currColorTable->readColors(),
166 currColorTable->count());
Matt Sarett61eedeb2016-11-04 13:19:48 -0400167 fCurrColorTable.reset(new SkColorTable(dstColors, currColorTable->count()));
168 } else {
169 fCurrColorTable = std::move(currColorTable);
msarett10522ff2015-09-07 08:54:01 -0700170 }
msarett10522ff2015-09-07 08:54:01 -0700171}
172
scroggo19b91532016-10-24 09:03:26 -0700173
Leon Scroggins571b30f2017-07-11 17:35:31 +0000174SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, const Options& opts) {
scroggo19b91532016-10-24 09:03:26 -0700175 if (opts.fSubset) {
176 return gif_error("Subsets not supported.\n", kUnimplemented);
177 }
178
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400179 const int frameIndex = opts.fFrameIndex;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400180 if (frameIndex > 0 && kRGB_565_SkColorType == dstInfo.colorType()) {
181 // FIXME: In theory, we might be able to support this, but it's not clear that it
182 // is necessary (Chromium does not decode to 565, and Android does not decode
183 // frames beyond the first). Disabling it because it is somewhat difficult:
184 // - If there is a transparent pixel, and this frame draws on top of another frame
185 // (if the frame is independent with a transparent pixel, we should not decode to
186 // 565 anyway, since it is not opaque), we need to skip drawing the transparent
187 // pixels (see writeTransparentPixels in haveDecodedRow). We currently do this by
188 // first swizzling into temporary memory, then copying into the destination. (We
189 // let the swizzler handle it first because it may need to sample.) After
190 // swizzling to 565, we do not know which pixels in our temporary memory
191 // correspond to the transparent pixel, so we do not know what to skip. We could
192 // special case the non-sampled case (no need to swizzle), but as this is
193 // currently unused we can just not support it.
194 return gif_error("Cannot decode multiframe gif (except frame 0) as 565.\n",
195 kInvalidConversion);
scroggo19b91532016-10-24 09:03:26 -0700196 }
197
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400198 const auto* frame = fReader->frameContext(frameIndex);
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400199 SkASSERT(frame);
200 if (0 == frameIndex) {
201 // SkCodec does not have a way to just parse through frame 0, so we
202 // have to do so manually, here.
203 fReader->parse((SkGifImageReader::SkGIFParseQuery) 0);
204 if (!frame->reachedStartOfData()) {
205 // We have parsed enough to know that there is a color map, but cannot
206 // parse the map itself yet. Exit now, so we do not build an incorrect
207 // table.
208 return gif_error("color map not available yet\n", kIncompleteInput);
209 }
210 } else {
211 // Parsing happened in SkCodec::getPixels.
212 SkASSERT(frameIndex < fReader->imagesCount());
213 SkASSERT(frame->reachedStartOfData());
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500214 }
215
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400216 if (this->xformOnDecode()) {
217 fXformBuffer.reset(new uint32_t[dstInfo.width()]);
218 sk_bzero(fXformBuffer.get(), dstInfo.width() * sizeof(uint32_t));
219 }
220
scroggo19b91532016-10-24 09:03:26 -0700221 fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]);
222
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -0400223 this->initializeColorTable(dstInfo, frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700224 this->initializeSwizzler(dstInfo, frameIndex);
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -0400225
226 SkASSERT(fCurrColorTable);
msarettb30d6982016-02-15 10:18:45 -0800227 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700228}
229
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400230void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, int frameIndex) {
scroggof9acbe22016-10-25 12:43:21 -0700231 const SkGIFFrameContext* frame = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700232 // This is only called by prepareToDecode, which ensures frameIndex is in range.
233 SkASSERT(frame);
msarett10522ff2015-09-07 08:54:01 -0700234
scroggo19b91532016-10-24 09:03:26 -0700235 const int xBegin = frame->xOffset();
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400236 const int xEnd = std::min(frame->frameRect().right(), fReader->screenWidth());
scroggo19b91532016-10-24 09:03:26 -0700237
238 // CreateSwizzler only reads left and right of the frame. We cannot use the frame's raw
239 // frameRect, since it might extend beyond the edge of the frame.
240 SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0);
241
Matt Sarett61eedeb2016-11-04 13:19:48 -0400242 SkImageInfo swizzlerInfo = dstInfo;
243 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500244 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400245 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
246 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
247 }
248 }
249
scroggo19b91532016-10-24 09:03:26 -0700250 // The default Options should be fine:
251 // - we'll ignore if the memory is zero initialized - unless we're the first frame, this won't
252 // matter anyway.
253 // - subsets are not supported for gif
254 // - the swizzler does not need to know about the frame.
255 // We may not be able to use the real Options anyway, since getPixels does not store it (due to
256 // a bug).
257 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(),
Matt Sarett61eedeb2016-11-04 13:19:48 -0400258 fCurrColorTable->readColors(), swizzlerInfo, Options(), &swizzleRect));
scroggo19b91532016-10-24 09:03:26 -0700259 SkASSERT(fSwizzler.get());
msarett10522ff2015-09-07 08:54:01 -0700260}
261
262/*
263 * Initiates the gif decode
264 */
265SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo,
scroggo19b91532016-10-24 09:03:26 -0700266 void* pixels, size_t dstRowBytes,
msarett10522ff2015-09-07 08:54:01 -0700267 const Options& opts,
msarette6dd0042015-10-09 11:07:34 -0700268 int* rowsDecoded) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000269 Result result = this->prepareToDecode(dstInfo, opts);
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500270 switch (result) {
271 case kSuccess:
272 break;
273 case kIncompleteInput:
274 // onStartIncrementalDecode treats this as incomplete, since it may
275 // provide more data later, but in this case, no more data will be
276 // provided, and there is nothing to draw. We also cannot return
277 // kIncompleteInput, which will make SkCodec attempt to fill
278 // remaining rows, but that requires an SkSwizzler, which we have
279 // not created.
280 return kInvalidInput;
281 default:
282 return result;
msarett10522ff2015-09-07 08:54:01 -0700283 }
284
285 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
286 return gif_error("Scaling not supported.\n", kInvalidScale);
287 }
288
scroggo19b91532016-10-24 09:03:26 -0700289 fDst = pixels;
290 fDstRowBytes = dstRowBytes;
291
292 return this->decodeFrame(true, opts, rowsDecoded);
293}
294
295SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
296 void* pixels, size_t dstRowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000297 const SkCodec::Options& opts) {
298 Result result = this->prepareToDecode(dstInfo, opts);
scroggo19b91532016-10-24 09:03:26 -0700299 if (result != kSuccess) {
300 return result;
msarett10522ff2015-09-07 08:54:01 -0700301 }
302
scroggo19b91532016-10-24 09:03:26 -0700303 fDst = pixels;
304 fDstRowBytes = dstRowBytes;
305
306 fFirstCallToIncrementalDecode = true;
307
msarett10522ff2015-09-07 08:54:01 -0700308 return kSuccess;
309}
310
scroggo19b91532016-10-24 09:03:26 -0700311SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) {
312 // It is possible the client has appended more data. Parse, if needed.
313 const auto& options = this->options();
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400314 const int frameIndex = options.fFrameIndex;
scroggof9acbe22016-10-25 12:43:21 -0700315 fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700316
317 const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode;
318 fFirstCallToIncrementalDecode = false;
319 return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700320}
321
scroggo19b91532016-10-24 09:03:26 -0700322SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded) {
323 const SkImageInfo& dstInfo = this->dstInfo();
Leon Scroggins IIIcbf66a22018-02-16 12:03:03 -0500324 const int scaledHeight = get_scaled_dimension(dstInfo.height(), fSwizzler->sampleY());
325
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400326 const int frameIndex = opts.fFrameIndex;
scroggo19b91532016-10-24 09:03:26 -0700327 SkASSERT(frameIndex < fReader->imagesCount());
scroggof9acbe22016-10-25 12:43:21 -0700328 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700329 if (firstAttempt) {
330 // rowsDecoded reports how many rows have been initialized, so a layer above
331 // can fill the rest. In some cases, we fill the background before decoding
332 // (or it is already filled for us), so we report rowsDecoded to be the full
333 // height.
334 bool filledBackground = false;
Nigel Tao66bc5242018-08-22 10:56:03 +1000335 if (frameContext->getRequiredFrame() == kNoFrame) {
scroggo19b91532016-10-24 09:03:26 -0700336 // We may need to clear to transparent for one of the following reasons:
337 // - The frameRect does not cover the full bounds. haveDecodedRow will
338 // only draw inside the frameRect, so we need to clear the rest.
scroggo19b91532016-10-24 09:03:26 -0700339 // - The frame is interlaced. There is no obvious way to fill
340 // afterwards for an incomplete image. (FIXME: Does the first pass
341 // cover all rows? If so, we do not have to fill here.)
scroggo8bce1172016-10-25 13:08:40 -0700342 // - There is no color table for this frame. In that case will not
343 // draw anything, so we need to fill.
scroggo19b91532016-10-24 09:03:26 -0700344 if (frameContext->frameRect() != this->getInfo().bounds()
scroggo8bce1172016-10-25 13:08:40 -0700345 || frameContext->interlaced() || !fCurrColorTableIsReal) {
scroggo19b91532016-10-24 09:03:26 -0700346 // fill ignores the width (replaces it with the actual, scaled width).
347 // But we need to scale in Y.
scroggo19b91532016-10-24 09:03:26 -0700348 auto fillInfo = dstInfo.makeWH(0, scaledHeight);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400349 fSwizzler->fill(fillInfo, fDst, fDstRowBytes, opts.fZeroInitialized);
scroggo19b91532016-10-24 09:03:26 -0700350 filledBackground = true;
351 }
352 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400353 // Not independent.
354 // SkCodec ensured that the prior frame has been decoded.
scroggo19b91532016-10-24 09:03:26 -0700355 filledBackground = true;
msarett10522ff2015-09-07 08:54:01 -0700356 }
scroggo19b91532016-10-24 09:03:26 -0700357
358 fFilledBackground = filledBackground;
359 if (filledBackground) {
360 // Report the full (scaled) height, since the client will never need to fill.
Leon Scroggins IIIcbf66a22018-02-16 12:03:03 -0500361 fRowsDecoded = scaledHeight;
scroggo19b91532016-10-24 09:03:26 -0700362 } else {
363 // This will be updated by haveDecodedRow.
364 fRowsDecoded = 0;
365 }
msarett10522ff2015-09-07 08:54:01 -0700366 }
msarette6dd0042015-10-09 11:07:34 -0700367
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500368 if (!fCurrColorTableIsReal) {
369 // Nothing to draw this frame.
370 return kSuccess;
371 }
372
scroggo19b91532016-10-24 09:03:26 -0700373 bool frameDecoded = false;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400374 const bool fatalError = !fReader->decode(frameIndex, &frameDecoded);
Leon Scroggins IIIcbf66a22018-02-16 12:03:03 -0500375 if (fatalError || !frameDecoded || fRowsDecoded != scaledHeight) {
scroggo19b91532016-10-24 09:03:26 -0700376 if (rowsDecoded) {
377 *rowsDecoded = fRowsDecoded;
378 }
Leon Scroggins III674a1842017-07-06 12:26:09 -0400379 if (fatalError) {
380 return kErrorInInput;
381 }
scroggo19b91532016-10-24 09:03:26 -0700382 return kIncompleteInput;
383 }
384
385 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700386}
scroggo46c57472015-09-30 08:57:13 -0700387
Matt Sarett61eedeb2016-11-04 13:19:48 -0400388void SkGifCodec::applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400389 if (this->xformOnDecode()) {
390 SkASSERT(this->colorXform());
Matt Sarett61eedeb2016-11-04 13:19:48 -0400391 fSwizzler->swizzle(fXformBuffer.get(), src);
392
Matt Sarett61eedeb2016-11-04 13:19:48 -0400393 const int xformWidth = get_scaled_dimension(dstInfo.width(), fSwizzler->sampleX());
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400394 this->applyColorXform(dst, fXformBuffer.get(), xformWidth);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400395 } else {
396 fSwizzler->swizzle(dst, src);
397 }
398}
399
Leon Scroggins III1f1aa2d2017-09-05 14:17:19 -0400400template <typename T>
401static void blend_line(void* dstAsVoid, const void* srcAsVoid, int width) {
402 T* dst = reinterpret_cast<T*>(dstAsVoid);
403 const T* src = reinterpret_cast<const T*>(srcAsVoid);
404 while (width --> 0) {
405 if (*src != 0) { // GIF pixels are either transparent (== 0) or opaque (!= 0).
406 *dst = *src;
407 }
408 src++;
409 dst++;
410 }
411}
412
Leon Scroggins III223ec292017-08-22 14:13:15 -0400413void SkGifCodec::haveDecodedRow(int frameIndex, const unsigned char* rowBegin,
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400414 int rowNumber, int repeatCount, bool writeTransparentPixels)
scroggo19b91532016-10-24 09:03:26 -0700415{
scroggof9acbe22016-10-25 12:43:21 -0700416 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700417 // The pixel data and coordinates supplied to us are relative to the frame's
418 // origin within the entire image size, i.e.
419 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee
420 // that width == (size().width() - frameContext->xOffset), so
421 // we must ensure we don't run off the end of either the source data or the
422 // row's X-coordinates.
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400423 const int width = frameContext->width();
scroggo19b91532016-10-24 09:03:26 -0700424 const int xBegin = frameContext->xOffset();
425 const int yBegin = frameContext->yOffset() + rowNumber;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400426 const int xEnd = std::min(xBegin + width, this->getInfo().width());
427 const int yEnd = std::min(yBegin + rowNumber + repeatCount, this->getInfo().height());
scroggo19b91532016-10-24 09:03:26 -0700428 // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We could instead do
429 // this once in prepareToDecode.
430 if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin))
Leon Scroggins III223ec292017-08-22 14:13:15 -0400431 return;
scroggo19b91532016-10-24 09:03:26 -0700432
433 // yBegin is the first row in the non-sampled image. dstRow will be the row in the output,
434 // after potentially scaling it.
435 int dstRow = yBegin;
436
437 const int sampleY = fSwizzler->sampleY();
438 if (sampleY > 1) {
439 // Check to see whether this row or one that falls in the repeatCount is needed in the
440 // output.
441 bool foundNecessaryRow = false;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400442 for (int i = 0; i < repeatCount; i++) {
scroggo19b91532016-10-24 09:03:26 -0700443 const int potentialRow = yBegin + i;
444 if (fSwizzler->rowNeeded(potentialRow)) {
445 dstRow = potentialRow / sampleY;
446 const int scaledHeight = get_scaled_dimension(this->dstInfo().height(), sampleY);
447 if (dstRow >= scaledHeight) {
Leon Scroggins III223ec292017-08-22 14:13:15 -0400448 return;
scroggo19b91532016-10-24 09:03:26 -0700449 }
450
451 foundNecessaryRow = true;
452 repeatCount -= i;
453
454 repeatCount = (repeatCount - 1) / sampleY + 1;
455
456 // Make sure the repeatCount does not take us beyond the end of the dst
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400457 if (dstRow + repeatCount > scaledHeight) {
scroggo19b91532016-10-24 09:03:26 -0700458 repeatCount = scaledHeight - dstRow;
459 SkASSERT(repeatCount >= 1);
460 }
461 break;
462 }
463 }
464
465 if (!foundNecessaryRow) {
Leon Scroggins III223ec292017-08-22 14:13:15 -0400466 return;
scroggo19b91532016-10-24 09:03:26 -0700467 }
Matt Sarett8a4e9c52016-10-25 14:24:50 -0400468 } else {
469 // Make sure the repeatCount does not take us beyond the end of the dst
470 SkASSERT(this->dstInfo().height() >= yBegin);
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400471 repeatCount = SkTMin(repeatCount, this->dstInfo().height() - yBegin);
scroggo19b91532016-10-24 09:03:26 -0700472 }
473
474 if (!fFilledBackground) {
475 // At this point, we are definitely going to write the row, so count it towards the number
476 // of rows decoded.
477 // We do not consider the repeatCount, which only happens for interlaced, in which case we
478 // have already set fRowsDecoded to the proper value (reflecting that we have filled the
479 // background).
480 fRowsDecoded++;
481 }
482
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500483 // decodeFrame will early exit if this is false, so this method will not be
484 // called.
485 SkASSERT(fCurrColorTableIsReal);
scroggo19b91532016-10-24 09:03:26 -0700486
487 // The swizzler takes care of offsetting into the dst width-wise.
488 void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes);
489
490 // We may or may not need to write transparent pixels to the buffer.
scroggo1285f412016-10-26 13:48:03 -0700491 // If we're compositing against a previous image, it's wrong, but if
492 // we're decoding an interlaced gif and displaying it "Haeberli"-style,
493 // we must write these for passes beyond the first, or the initial passes
494 // will "show through" the later ones.
scroggo19b91532016-10-24 09:03:26 -0700495 const auto dstInfo = this->dstInfo();
scroggo53f63b62016-10-27 08:29:13 -0700496 if (writeTransparentPixels) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400497 this->applyXformRow(dstInfo, dstLine, rowBegin);
scroggo19b91532016-10-24 09:03:26 -0700498 } else {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400499 this->applyXformRow(dstInfo, fTmpBuffer.get(), rowBegin);
scroggo19b91532016-10-24 09:03:26 -0700500
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400501 size_t offsetBytes = fSwizzler->swizzleOffsetBytes();
502 if (dstInfo.colorType() == kRGBA_F16_SkColorType) {
503 // Account for the fact that post-swizzling we converted to F16,
504 // which is twice as wide.
505 offsetBytes *= 2;
506 }
Leon Scroggins III1f1aa2d2017-09-05 14:17:19 -0400507 const void* src = SkTAddOffset<void>(fTmpBuffer.get(), offsetBytes);
508 void* dst = SkTAddOffset<void>(dstLine, offsetBytes);
Mike Klein45c16fa2017-07-18 18:15:13 -0400509
scroggo19b91532016-10-24 09:03:26 -0700510 switch (dstInfo.colorType()) {
511 case kBGRA_8888_SkColorType:
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400512 case kRGBA_8888_SkColorType:
Leon Scroggins III1f1aa2d2017-09-05 14:17:19 -0400513 blend_line<uint32_t>(dst, src, fSwizzler->swizzleWidth());
scroggo19b91532016-10-24 09:03:26 -0700514 break;
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400515 case kRGBA_F16_SkColorType:
Leon Scroggins III1f1aa2d2017-09-05 14:17:19 -0400516 blend_line<uint64_t>(dst, src, fSwizzler->swizzleWidth());
scroggo19b91532016-10-24 09:03:26 -0700517 break;
scroggo19b91532016-10-24 09:03:26 -0700518 default:
519 SkASSERT(false);
Leon Scroggins III223ec292017-08-22 14:13:15 -0400520 return;
scroggo19b91532016-10-24 09:03:26 -0700521 }
522 }
523
524 // Tell the frame to copy the row data if need be.
525 if (repeatCount > 1) {
Mike Reed7fcfb622018-02-09 13:26:46 -0500526 const size_t bytesPerPixel = this->dstInfo().bytesPerPixel();
scroggo19b91532016-10-24 09:03:26 -0700527 const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel;
528 void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetBytes());
529 void* dst = copiedLine;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400530 for (int i = 1; i < repeatCount; i++) {
scroggo19b91532016-10-24 09:03:26 -0700531 dst = SkTAddOffset<void>(dst, fDstRowBytes);
532 memcpy(dst, copiedLine, bytesToCopy);
msarett72261c02015-11-19 15:29:26 -0800533 }
534 }
msarett72261c02015-11-19 15:29:26 -0800535}