blob: 10e7ce66a4115182a217f9fa68d891349d135818 [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 III5dd47e42018-09-27 15:26:48 -040094 auto encodedInfo = SkEncodedInfo::Make(reader->screenWidth(), reader->screenHeight(),
95 SkEncodedInfo::kPalette_Color, alpha, 8);
Leon Scroggins III36f7e322018-08-27 11:55:46 -040096 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).
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400257 fSwizzler = SkSwizzler::Make(this->getEncodedInfo(), fCurrColorTable->readColors(),
258 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
Leon Scroggins III712476e2018-10-03 15:47:00 -0400285 if (dstInfo.dimensions() != this->dimensions()) {
msarett10522ff2015-09-07 08:54:01 -0700286 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.
Leon Scroggins III712476e2018-10-03 15:47:00 -0400344 if (frameContext->frameRect() != this->bounds()
scroggo8bce1172016-10-25 13:08:40 -0700345 || frameContext->interlaced() || !fCurrColorTableIsReal) {
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400346 auto fillInfo = dstInfo.makeWH(fSwizzler->fillWidth(), scaledHeight);
347 SkSampler::Fill(fillInfo, fDst, fDstRowBytes, opts.fZeroInitialized);
scroggo19b91532016-10-24 09:03:26 -0700348 filledBackground = true;
349 }
350 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400351 // Not independent.
352 // SkCodec ensured that the prior frame has been decoded.
scroggo19b91532016-10-24 09:03:26 -0700353 filledBackground = true;
msarett10522ff2015-09-07 08:54:01 -0700354 }
scroggo19b91532016-10-24 09:03:26 -0700355
356 fFilledBackground = filledBackground;
357 if (filledBackground) {
358 // Report the full (scaled) height, since the client will never need to fill.
Leon Scroggins IIIcbf66a22018-02-16 12:03:03 -0500359 fRowsDecoded = scaledHeight;
scroggo19b91532016-10-24 09:03:26 -0700360 } else {
361 // This will be updated by haveDecodedRow.
362 fRowsDecoded = 0;
363 }
msarett10522ff2015-09-07 08:54:01 -0700364 }
msarette6dd0042015-10-09 11:07:34 -0700365
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500366 if (!fCurrColorTableIsReal) {
367 // Nothing to draw this frame.
368 return kSuccess;
369 }
370
scroggo19b91532016-10-24 09:03:26 -0700371 bool frameDecoded = false;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400372 const bool fatalError = !fReader->decode(frameIndex, &frameDecoded);
Leon Scroggins IIIcbf66a22018-02-16 12:03:03 -0500373 if (fatalError || !frameDecoded || fRowsDecoded != scaledHeight) {
scroggo19b91532016-10-24 09:03:26 -0700374 if (rowsDecoded) {
375 *rowsDecoded = fRowsDecoded;
376 }
Leon Scroggins III674a1842017-07-06 12:26:09 -0400377 if (fatalError) {
378 return kErrorInInput;
379 }
scroggo19b91532016-10-24 09:03:26 -0700380 return kIncompleteInput;
381 }
382
383 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700384}
scroggo46c57472015-09-30 08:57:13 -0700385
Matt Sarett61eedeb2016-11-04 13:19:48 -0400386void SkGifCodec::applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400387 if (this->xformOnDecode()) {
388 SkASSERT(this->colorXform());
Matt Sarett61eedeb2016-11-04 13:19:48 -0400389 fSwizzler->swizzle(fXformBuffer.get(), src);
390
Matt Sarett61eedeb2016-11-04 13:19:48 -0400391 const int xformWidth = get_scaled_dimension(dstInfo.width(), fSwizzler->sampleX());
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400392 this->applyColorXform(dst, fXformBuffer.get(), xformWidth);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400393 } else {
394 fSwizzler->swizzle(dst, src);
395 }
396}
397
Leon Scroggins III1f1aa2d2017-09-05 14:17:19 -0400398template <typename T>
399static void blend_line(void* dstAsVoid, const void* srcAsVoid, int width) {
400 T* dst = reinterpret_cast<T*>(dstAsVoid);
401 const T* src = reinterpret_cast<const T*>(srcAsVoid);
402 while (width --> 0) {
403 if (*src != 0) { // GIF pixels are either transparent (== 0) or opaque (!= 0).
404 *dst = *src;
405 }
406 src++;
407 dst++;
408 }
409}
410
Leon Scroggins III223ec292017-08-22 14:13:15 -0400411void SkGifCodec::haveDecodedRow(int frameIndex, const unsigned char* rowBegin,
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400412 int rowNumber, int repeatCount, bool writeTransparentPixels)
scroggo19b91532016-10-24 09:03:26 -0700413{
scroggof9acbe22016-10-25 12:43:21 -0700414 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700415 // The pixel data and coordinates supplied to us are relative to the frame's
416 // origin within the entire image size, i.e.
417 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee
418 // that width == (size().width() - frameContext->xOffset), so
419 // we must ensure we don't run off the end of either the source data or the
420 // row's X-coordinates.
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400421 const int width = frameContext->width();
scroggo19b91532016-10-24 09:03:26 -0700422 const int xBegin = frameContext->xOffset();
423 const int yBegin = frameContext->yOffset() + rowNumber;
Leon Scroggins III712476e2018-10-03 15:47:00 -0400424 const int xEnd = std::min(xBegin + width, this->dimensions().width());
425 const int yEnd = std::min(yBegin + rowNumber + repeatCount, this->dimensions().height());
scroggo19b91532016-10-24 09:03:26 -0700426 // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We could instead do
427 // this once in prepareToDecode.
428 if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin))
Leon Scroggins III223ec292017-08-22 14:13:15 -0400429 return;
scroggo19b91532016-10-24 09:03:26 -0700430
431 // yBegin is the first row in the non-sampled image. dstRow will be the row in the output,
432 // after potentially scaling it.
433 int dstRow = yBegin;
434
435 const int sampleY = fSwizzler->sampleY();
436 if (sampleY > 1) {
437 // Check to see whether this row or one that falls in the repeatCount is needed in the
438 // output.
439 bool foundNecessaryRow = false;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400440 for (int i = 0; i < repeatCount; i++) {
scroggo19b91532016-10-24 09:03:26 -0700441 const int potentialRow = yBegin + i;
442 if (fSwizzler->rowNeeded(potentialRow)) {
443 dstRow = potentialRow / sampleY;
444 const int scaledHeight = get_scaled_dimension(this->dstInfo().height(), sampleY);
445 if (dstRow >= scaledHeight) {
Leon Scroggins III223ec292017-08-22 14:13:15 -0400446 return;
scroggo19b91532016-10-24 09:03:26 -0700447 }
448
449 foundNecessaryRow = true;
450 repeatCount -= i;
451
452 repeatCount = (repeatCount - 1) / sampleY + 1;
453
454 // Make sure the repeatCount does not take us beyond the end of the dst
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400455 if (dstRow + repeatCount > scaledHeight) {
scroggo19b91532016-10-24 09:03:26 -0700456 repeatCount = scaledHeight - dstRow;
457 SkASSERT(repeatCount >= 1);
458 }
459 break;
460 }
461 }
462
463 if (!foundNecessaryRow) {
Leon Scroggins III223ec292017-08-22 14:13:15 -0400464 return;
scroggo19b91532016-10-24 09:03:26 -0700465 }
Matt Sarett8a4e9c52016-10-25 14:24:50 -0400466 } else {
467 // Make sure the repeatCount does not take us beyond the end of the dst
468 SkASSERT(this->dstInfo().height() >= yBegin);
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400469 repeatCount = SkTMin(repeatCount, this->dstInfo().height() - yBegin);
scroggo19b91532016-10-24 09:03:26 -0700470 }
471
472 if (!fFilledBackground) {
473 // At this point, we are definitely going to write the row, so count it towards the number
474 // of rows decoded.
475 // We do not consider the repeatCount, which only happens for interlaced, in which case we
476 // have already set fRowsDecoded to the proper value (reflecting that we have filled the
477 // background).
478 fRowsDecoded++;
479 }
480
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500481 // decodeFrame will early exit if this is false, so this method will not be
482 // called.
483 SkASSERT(fCurrColorTableIsReal);
scroggo19b91532016-10-24 09:03:26 -0700484
485 // The swizzler takes care of offsetting into the dst width-wise.
486 void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes);
487
488 // We may or may not need to write transparent pixels to the buffer.
scroggo1285f412016-10-26 13:48:03 -0700489 // If we're compositing against a previous image, it's wrong, but if
490 // we're decoding an interlaced gif and displaying it "Haeberli"-style,
491 // we must write these for passes beyond the first, or the initial passes
492 // will "show through" the later ones.
scroggo19b91532016-10-24 09:03:26 -0700493 const auto dstInfo = this->dstInfo();
scroggo53f63b62016-10-27 08:29:13 -0700494 if (writeTransparentPixels) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400495 this->applyXformRow(dstInfo, dstLine, rowBegin);
scroggo19b91532016-10-24 09:03:26 -0700496 } else {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400497 this->applyXformRow(dstInfo, fTmpBuffer.get(), rowBegin);
scroggo19b91532016-10-24 09:03:26 -0700498
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400499 size_t offsetBytes = fSwizzler->swizzleOffsetBytes();
500 if (dstInfo.colorType() == kRGBA_F16_SkColorType) {
501 // Account for the fact that post-swizzling we converted to F16,
502 // which is twice as wide.
503 offsetBytes *= 2;
504 }
Leon Scroggins III1f1aa2d2017-09-05 14:17:19 -0400505 const void* src = SkTAddOffset<void>(fTmpBuffer.get(), offsetBytes);
506 void* dst = SkTAddOffset<void>(dstLine, offsetBytes);
Mike Klein45c16fa2017-07-18 18:15:13 -0400507
scroggo19b91532016-10-24 09:03:26 -0700508 switch (dstInfo.colorType()) {
509 case kBGRA_8888_SkColorType:
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400510 case kRGBA_8888_SkColorType:
Leon Scroggins III1f1aa2d2017-09-05 14:17:19 -0400511 blend_line<uint32_t>(dst, src, fSwizzler->swizzleWidth());
scroggo19b91532016-10-24 09:03:26 -0700512 break;
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400513 case kRGBA_F16_SkColorType:
Leon Scroggins III1f1aa2d2017-09-05 14:17:19 -0400514 blend_line<uint64_t>(dst, src, fSwizzler->swizzleWidth());
scroggo19b91532016-10-24 09:03:26 -0700515 break;
scroggo19b91532016-10-24 09:03:26 -0700516 default:
517 SkASSERT(false);
Leon Scroggins III223ec292017-08-22 14:13:15 -0400518 return;
scroggo19b91532016-10-24 09:03:26 -0700519 }
520 }
521
522 // Tell the frame to copy the row data if need be.
523 if (repeatCount > 1) {
Mike Reed7fcfb622018-02-09 13:26:46 -0500524 const size_t bytesPerPixel = this->dstInfo().bytesPerPixel();
scroggo19b91532016-10-24 09:03:26 -0700525 const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel;
526 void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetBytes());
527 void* dst = copiedLine;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400528 for (int i = 1; i < repeatCount; i++) {
scroggo19b91532016-10-24 09:03:26 -0700529 dst = SkTAddOffset<void>(dst, fDstRowBytes);
530 memcpy(dst, copiedLine, bytesToCopy);
msarett72261c02015-11-19 15:29:26 -0800531 }
532 }
msarett72261c02015-11-19 15:29:26 -0800533}