msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 8 | /* |
| 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" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 34 | #include "SkCodecPriv.h" |
| 35 | #include "SkColorPriv.h" |
| 36 | #include "SkColorTable.h" |
msarett | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 37 | #include "SkGifCodec.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 38 | #include "SkStream.h" |
| 39 | #include "SkSwizzler.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 40 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 41 | #include <algorithm> |
| 42 | |
| 43 | #define GIF87_STAMP "GIF87a" |
| 44 | #define GIF89_STAMP "GIF89a" |
| 45 | #define GIF_STAMP_LEN 6 |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 46 | |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Checks the start of the stream to see if the image is a gif |
| 49 | */ |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 50 | bool SkGifCodec::IsGif(const void* buf, size_t bytesRead) { |
| 51 | if (bytesRead >= GIF_STAMP_LEN) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 52 | if (memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 || |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 53 | memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) |
| 54 | { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | /* |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 62 | * Error function |
| 63 | */ |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 64 | static SkCodec::Result gif_error(const char* msg, SkCodec::Result result = SkCodec::kInvalidInput) { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 65 | SkCodecPrintf("Gif Error: %s\n", msg); |
| 66 | return result; |
| 67 | } |
| 68 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 69 | /* |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 70 | * Assumes IsGif was called and returned true |
| 71 | * Creates a gif decoder |
| 72 | * Reads enough of the stream to determine the image format |
| 73 | */ |
| 74 | SkCodec* SkGifCodec::NewFromStream(SkStream* stream) { |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 75 | std::unique_ptr<SkGifImageReader> reader(new SkGifImageReader(stream)); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 76 | if (!reader->parse(SkGifImageReader::SkGIFSizeQuery)) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 77 | // Not enough data to determine the size. |
| 78 | return nullptr; |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 79 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 80 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 81 | if (0 == reader->screenWidth() || 0 == reader->screenHeight()) { |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | const auto alpha = reader->firstFrameHasAlpha() ? SkEncodedInfo::kBinary_Alpha |
| 86 | : SkEncodedInfo::kOpaque_Alpha; |
| 87 | // Use kPalette since Gifs are encoded with a color table. |
| 88 | // FIXME: Gifs can actually be encoded with 4-bits per pixel. Using 8 works, but we could skip |
| 89 | // expanding to 8 bits and take advantage of the SkSwizzler to work from 4. |
| 90 | const auto encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, alpha, 8); |
| 91 | |
| 92 | // Although the encodedInfo is always kPalette_Color, it is possible that kIndex_8 is |
| 93 | // unsupported if the frame is subset and there is no transparent pixel. |
| 94 | const auto colorType = reader->firstFrameSupportsIndex8() ? kIndex_8_SkColorType |
| 95 | : kN32_SkColorType; |
| 96 | // 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 Sarett | 7f650bd | 2016-10-30 21:25:34 -0400 | [diff] [blame] | 101 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 102 | const auto imageInfo = SkImageInfo::Make(reader->screenWidth(), reader->screenHeight(), |
Matt Sarett | 7f650bd | 2016-10-30 21:25:34 -0400 | [diff] [blame] | 103 | colorType, alphaType, |
| 104 | SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 105 | return new SkGifCodec(encodedInfo, imageInfo, reader.release()); |
| 106 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 107 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 108 | bool SkGifCodec::onRewind() { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 109 | fReader->clearDecodeState(); |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 113 | SkGifCodec::SkGifCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo, |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 114 | SkGifImageReader* reader) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 115 | : INHERITED(encodedInfo, imageInfo, nullptr) |
| 116 | , 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); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 128 | } |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 129 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 130 | std::vector<SkCodec::FrameInfo> SkGifCodec::onGetFrameInfo() { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 131 | fReader->parse(SkGifImageReader::SkGIFFrameCountQuery); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 132 | const size_t size = fReader->imagesCount(); |
| 133 | std::vector<FrameInfo> result(size); |
| 134 | for (size_t i = 0; i < size; i++) { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 135 | const SkGIFFrameContext* frameContext = fReader->frameContext(i); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 136 | result[i].fDuration = frameContext->delayTime(); |
| 137 | result[i].fRequiredFrame = frameContext->getRequiredFrame(); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 138 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 139 | return result; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 140 | } |
| 141 | |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame^] | 142 | int SkGifCodec::onGetRepetitionCount() { |
| 143 | fReader->parse(SkGifImageReader::SkGIFLoopCountQuery); |
| 144 | return fReader->loopCount(); |
| 145 | } |
| 146 | |
Leon Scroggins III | fc49b40 | 2016-10-31 14:08:56 -0400 | [diff] [blame] | 147 | void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, size_t frameIndex) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 148 | fCurrColorTable = fReader->getColorTable(dstInfo.colorType(), frameIndex); |
| 149 | fCurrColorTableIsReal = fCurrColorTable; |
| 150 | if (!fCurrColorTable) { |
Leon Scroggins III | a049ac4 | 2016-10-27 11:16:11 -0400 | [diff] [blame] | 151 | // This is possible for an empty frame. Create a dummy with one value (transparent). |
| 152 | SkPMColor color = SK_ColorTRANSPARENT; |
| 153 | fCurrColorTable.reset(new SkColorTable(&color, 1)); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 154 | } |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 155 | } |
| 156 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 157 | |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 158 | SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr, |
| 159 | int* inputColorCount, const Options& opts) { |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 160 | // Check for valid input parameters |
msarett | 2ecc35f | 2016-09-08 11:55:16 -0700 | [diff] [blame] | 161 | if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) { |
| 162 | return gif_error("Cannot convert input type to output type.\n", kInvalidConversion); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 163 | } |
| 164 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 165 | if (dstInfo.colorType() == kRGBA_F16_SkColorType) { |
| 166 | // FIXME: This should be supported. |
| 167 | return gif_error("GIF does not yet support F16.\n", kInvalidConversion); |
| 168 | } |
msarett | 5af4e0b | 2015-11-17 11:18:03 -0800 | [diff] [blame] | 169 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 170 | if (opts.fSubset) { |
| 171 | return gif_error("Subsets not supported.\n", kUnimplemented); |
| 172 | } |
| 173 | |
| 174 | const size_t frameIndex = opts.fFrameIndex; |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 175 | if (frameIndex > 0) { |
| 176 | switch (dstInfo.colorType()) { |
| 177 | case kIndex_8_SkColorType: |
| 178 | // FIXME: It is possible that a later frame can be decoded to index8, if it does one |
| 179 | // of the following: |
| 180 | // - Covers the entire previous frame |
| 181 | // - Shares a color table (and transparent index) with any prior frames that are |
| 182 | // showing. |
| 183 | // We must support index8 for the first frame to be backwards compatible on Android, |
| 184 | // but we do not (currently) need to support later frames as index8. |
| 185 | return gif_error("Cannot decode multiframe gif (except frame 0) as index 8.\n", |
| 186 | kInvalidConversion); |
| 187 | case kRGB_565_SkColorType: |
| 188 | // FIXME: In theory, we might be able to support this, but it's not clear that it |
| 189 | // is necessary (Chromium does not decode to 565, and Android does not decode |
| 190 | // frames beyond the first). Disabling it because it is somewhat difficult: |
| 191 | // - If there is a transparent pixel, and this frame draws on top of another frame |
| 192 | // (if the frame is independent with a transparent pixel, we should not decode to |
| 193 | // 565 anyway, since it is not opaque), we need to skip drawing the transparent |
| 194 | // pixels (see writeTransparentPixels in haveDecodedRow). We currently do this by |
| 195 | // first swizzling into temporary memory, then copying into the destination. (We |
| 196 | // let the swizzler handle it first because it may need to sample.) After |
| 197 | // swizzling to 565, we do not know which pixels in our temporary memory |
| 198 | // correspond to the transparent pixel, so we do not know what to skip. We could |
| 199 | // special case the non-sampled case (no need to swizzle), but as this is |
| 200 | // currently unused we can just not support it. |
| 201 | return gif_error("Cannot decode multiframe gif (except frame 0) as 565.\n", |
| 202 | kInvalidConversion); |
| 203 | default: |
| 204 | break; |
| 205 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 206 | } |
| 207 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 208 | fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 209 | |
| 210 | if (frameIndex >= fReader->imagesCount()) { |
| 211 | return gif_error("frame index out of range!\n", kIncompleteInput); |
| 212 | } |
| 213 | |
| 214 | fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]); |
| 215 | |
Leon Scroggins III | fc49b40 | 2016-10-31 14:08:56 -0400 | [diff] [blame] | 216 | this->initializeColorTable(dstInfo, frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 217 | this->initializeSwizzler(dstInfo, frameIndex); |
Leon Scroggins III | fc49b40 | 2016-10-31 14:08:56 -0400 | [diff] [blame] | 218 | |
| 219 | SkASSERT(fCurrColorTable); |
| 220 | if (inputColorCount) { |
| 221 | *inputColorCount = fCurrColorTable->count(); |
| 222 | } |
| 223 | copy_color_table(dstInfo, fCurrColorTable.get(), inputColorPtr, inputColorCount); |
| 224 | |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 225 | return kSuccess; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 226 | } |
| 227 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 228 | void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, size_t frameIndex) { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 229 | const SkGIFFrameContext* frame = fReader->frameContext(frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 230 | // This is only called by prepareToDecode, which ensures frameIndex is in range. |
| 231 | SkASSERT(frame); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 232 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 233 | const int xBegin = frame->xOffset(); |
| 234 | const int xEnd = std::min(static_cast<int>(frame->xOffset() + frame->width()), |
| 235 | static_cast<int>(fReader->screenWidth())); |
| 236 | |
| 237 | // CreateSwizzler only reads left and right of the frame. We cannot use the frame's raw |
| 238 | // frameRect, since it might extend beyond the edge of the frame. |
| 239 | SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0); |
| 240 | |
| 241 | // The default Options should be fine: |
| 242 | // - we'll ignore if the memory is zero initialized - unless we're the first frame, this won't |
| 243 | // matter anyway. |
| 244 | // - subsets are not supported for gif |
| 245 | // - the swizzler does not need to know about the frame. |
| 246 | // We may not be able to use the real Options anyway, since getPixels does not store it (due to |
| 247 | // a bug). |
| 248 | fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), |
| 249 | fCurrColorTable->readColors(), dstInfo, Options(), &swizzleRect)); |
| 250 | SkASSERT(fSwizzler.get()); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Initiates the gif decode |
| 255 | */ |
| 256 | SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 257 | void* pixels, size_t dstRowBytes, |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 258 | const Options& opts, |
| 259 | SkPMColor* inputColorPtr, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 260 | int* inputColorCount, |
| 261 | int* rowsDecoded) { |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 262 | Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts); |
| 263 | if (kSuccess != result) { |
| 264 | return result; |
| 265 | } |
| 266 | |
| 267 | if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 268 | return gif_error("Scaling not supported.\n", kInvalidScale); |
| 269 | } |
| 270 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 271 | fDst = pixels; |
| 272 | fDstRowBytes = dstRowBytes; |
| 273 | |
| 274 | return this->decodeFrame(true, opts, rowsDecoded); |
| 275 | } |
| 276 | |
| 277 | SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo, |
| 278 | void* pixels, size_t dstRowBytes, |
| 279 | const SkCodec::Options& opts, |
| 280 | SkPMColor* inputColorPtr, |
| 281 | int* inputColorCount) { |
| 282 | Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts); |
| 283 | if (result != kSuccess) { |
| 284 | return result; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 285 | } |
| 286 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 287 | fDst = pixels; |
| 288 | fDstRowBytes = dstRowBytes; |
| 289 | |
| 290 | fFirstCallToIncrementalDecode = true; |
| 291 | |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 292 | return kSuccess; |
| 293 | } |
| 294 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 295 | SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) { |
| 296 | // It is possible the client has appended more data. Parse, if needed. |
| 297 | const auto& options = this->options(); |
| 298 | const size_t frameIndex = options.fFrameIndex; |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 299 | fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 300 | |
| 301 | const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode; |
| 302 | fFirstCallToIncrementalDecode = false; |
| 303 | return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 304 | } |
| 305 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 306 | SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded) { |
| 307 | const SkImageInfo& dstInfo = this->dstInfo(); |
| 308 | const size_t frameIndex = opts.fFrameIndex; |
| 309 | SkASSERT(frameIndex < fReader->imagesCount()); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 310 | const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 311 | if (firstAttempt) { |
| 312 | // rowsDecoded reports how many rows have been initialized, so a layer above |
| 313 | // can fill the rest. In some cases, we fill the background before decoding |
| 314 | // (or it is already filled for us), so we report rowsDecoded to be the full |
| 315 | // height. |
| 316 | bool filledBackground = false; |
| 317 | if (frameContext->getRequiredFrame() == kNone) { |
| 318 | // We may need to clear to transparent for one of the following reasons: |
| 319 | // - The frameRect does not cover the full bounds. haveDecodedRow will |
| 320 | // only draw inside the frameRect, so we need to clear the rest. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 321 | // - The frame is interlaced. There is no obvious way to fill |
| 322 | // afterwards for an incomplete image. (FIXME: Does the first pass |
| 323 | // cover all rows? If so, we do not have to fill here.) |
scroggo | 8bce117 | 2016-10-25 13:08:40 -0700 | [diff] [blame] | 324 | // - There is no color table for this frame. In that case will not |
| 325 | // draw anything, so we need to fill. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 326 | if (frameContext->frameRect() != this->getInfo().bounds() |
scroggo | 8bce117 | 2016-10-25 13:08:40 -0700 | [diff] [blame] | 327 | || frameContext->interlaced() || !fCurrColorTableIsReal) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 328 | // fill ignores the width (replaces it with the actual, scaled width). |
| 329 | // But we need to scale in Y. |
| 330 | const int scaledHeight = get_scaled_dimension(dstInfo.height(), |
| 331 | fSwizzler->sampleY()); |
| 332 | auto fillInfo = dstInfo.makeWH(0, scaledHeight); |
| 333 | fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue(dstInfo), |
| 334 | opts.fZeroInitialized); |
| 335 | filledBackground = true; |
| 336 | } |
| 337 | } else { |
| 338 | // Not independent |
| 339 | if (!opts.fHasPriorFrame) { |
| 340 | // Decode that frame into pixels. |
| 341 | Options prevFrameOpts(opts); |
| 342 | prevFrameOpts.fFrameIndex = frameContext->getRequiredFrame(); |
| 343 | prevFrameOpts.fHasPriorFrame = false; |
Leon Scroggins III | fc49b40 | 2016-10-31 14:08:56 -0400 | [diff] [blame] | 344 | // The prior frame may have a different color table, so update it and the |
| 345 | // swizzler. |
| 346 | this->initializeColorTable(dstInfo, prevFrameOpts.fFrameIndex); |
| 347 | this->initializeSwizzler(dstInfo, prevFrameOpts.fFrameIndex); |
| 348 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 349 | const Result prevResult = this->decodeFrame(true, prevFrameOpts, nullptr); |
| 350 | switch (prevResult) { |
| 351 | case kSuccess: |
| 352 | // Prior frame succeeded. Carry on. |
| 353 | break; |
| 354 | case kIncompleteInput: |
| 355 | // Prior frame was incomplete. So this frame cannot be decoded. |
| 356 | return kInvalidInput; |
| 357 | default: |
| 358 | return prevResult; |
| 359 | } |
Leon Scroggins III | fc49b40 | 2016-10-31 14:08:56 -0400 | [diff] [blame] | 360 | |
| 361 | // Go back to using the correct color table for this frame. |
| 362 | this->initializeColorTable(dstInfo, frameIndex); |
| 363 | this->initializeSwizzler(dstInfo, frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 364 | } |
| 365 | const auto* prevFrame = fReader->frameContext(frameContext->getRequiredFrame()); |
| 366 | if (prevFrame->getDisposalMethod() == SkCodecAnimation::RestoreBGColor_DisposalMethod) { |
| 367 | const SkIRect prevRect = prevFrame->frameRect(); |
| 368 | auto left = get_scaled_dimension(prevRect.fLeft, fSwizzler->sampleX()); |
| 369 | auto top = get_scaled_dimension(prevRect.fTop, fSwizzler->sampleY()); |
| 370 | void* const eraseDst = SkTAddOffset<void>(fDst, top * fDstRowBytes |
| 371 | + left * SkColorTypeBytesPerPixel(dstInfo.colorType())); |
| 372 | auto width = get_scaled_dimension(prevRect.width(), fSwizzler->sampleX()); |
| 373 | auto height = get_scaled_dimension(prevRect.height(), fSwizzler->sampleY()); |
| 374 | // fSwizzler->fill() would fill to the scaled width of the frame, but we want to |
| 375 | // fill to the scaled with of the width of the PRIOR frame, so we do all the scaling |
| 376 | // ourselves and call the static version. |
| 377 | SkSampler::Fill(dstInfo.makeWH(width, height), eraseDst, |
| 378 | fDstRowBytes, this->getFillValue(dstInfo), kNo_ZeroInitialized); |
| 379 | } |
| 380 | filledBackground = true; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 381 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 382 | |
| 383 | fFilledBackground = filledBackground; |
| 384 | if (filledBackground) { |
| 385 | // Report the full (scaled) height, since the client will never need to fill. |
| 386 | fRowsDecoded = get_scaled_dimension(dstInfo.height(), fSwizzler->sampleY()); |
| 387 | } else { |
| 388 | // This will be updated by haveDecodedRow. |
| 389 | fRowsDecoded = 0; |
| 390 | } |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 391 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 392 | |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 393 | // Note: there is a difference between the following call to SkGifImageReader::decode |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 394 | // returning false and leaving frameDecoded false: |
| 395 | // - If the method returns false, there was an error in the stream. We still treat this as |
| 396 | // incomplete, since we have already decoded some rows. |
| 397 | // - If frameDecoded is false, that just means that we do not have enough data. If more data |
| 398 | // is supplied, we may be able to continue decoding this frame. We also treat this as |
| 399 | // incomplete. |
| 400 | // FIXME: Ensure that we do not attempt to continue decoding if the method returns false and |
| 401 | // more data is supplied. |
| 402 | bool frameDecoded = false; |
| 403 | if (!fReader->decode(frameIndex, &frameDecoded) || !frameDecoded) { |
| 404 | if (rowsDecoded) { |
| 405 | *rowsDecoded = fRowsDecoded; |
| 406 | } |
| 407 | return kIncompleteInput; |
| 408 | } |
| 409 | |
| 410 | return kSuccess; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 411 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 412 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 413 | uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const { |
| 414 | // Note: Using fCurrColorTable relies on having called initializeColorTable already. |
| 415 | // This is (currently) safe because this method is only called when filling, after |
| 416 | // initializeColorTable has been called. |
| 417 | // FIXME: Is there a way to make this less fragile? |
| 418 | if (dstInfo.colorType() == kIndex_8_SkColorType && fCurrColorTableIsReal) { |
| 419 | // We only support index 8 for the first frame, for backwards |
| 420 | // compatibity on Android, so we are using the color table for the first frame. |
| 421 | SkASSERT(this->options().fFrameIndex == 0); |
| 422 | // Use the transparent index for the first frame. |
| 423 | const size_t transPixel = fReader->frameContext(0)->transparentPixel(); |
| 424 | if (transPixel < (size_t) fCurrColorTable->count()) { |
| 425 | return transPixel; |
| 426 | } |
| 427 | // Fall through to return SK_ColorTRANSPARENT (i.e. 0). This choice is arbitrary, |
| 428 | // but we have to pick something inside the color table, and this one is as good |
| 429 | // as any. |
| 430 | } |
| 431 | // Using transparent as the fill value matches the behavior in Chromium, |
| 432 | // which ignores the background color. |
| 433 | // If the colorType is kIndex_8, and there was no color table (i.e. |
| 434 | // fCurrColorTableIsReal is false), this value (zero) corresponds to the |
| 435 | // only entry in the dummy color table provided to the client. |
| 436 | return SK_ColorTRANSPARENT; |
| 437 | } |
msarett | 72261c0 | 2015-11-19 15:29:26 -0800 | [diff] [blame] | 438 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 439 | bool SkGifCodec::haveDecodedRow(size_t frameIndex, const unsigned char* rowBegin, |
| 440 | size_t rowNumber, unsigned repeatCount, bool writeTransparentPixels) |
| 441 | { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 442 | const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 443 | // The pixel data and coordinates supplied to us are relative to the frame's |
| 444 | // origin within the entire image size, i.e. |
| 445 | // (frameContext->xOffset, frameContext->yOffset). There is no guarantee |
| 446 | // that width == (size().width() - frameContext->xOffset), so |
| 447 | // we must ensure we don't run off the end of either the source data or the |
| 448 | // row's X-coordinates. |
| 449 | const size_t width = frameContext->width(); |
| 450 | const int xBegin = frameContext->xOffset(); |
| 451 | const int yBegin = frameContext->yOffset() + rowNumber; |
| 452 | const int xEnd = std::min(static_cast<int>(frameContext->xOffset() + width), |
| 453 | this->getInfo().width()); |
| 454 | const int yEnd = std::min(static_cast<int>(frameContext->yOffset() + rowNumber + repeatCount), |
| 455 | this->getInfo().height()); |
| 456 | // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We could instead do |
| 457 | // this once in prepareToDecode. |
| 458 | if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin)) |
| 459 | return true; |
| 460 | |
| 461 | // yBegin is the first row in the non-sampled image. dstRow will be the row in the output, |
| 462 | // after potentially scaling it. |
| 463 | int dstRow = yBegin; |
| 464 | |
| 465 | const int sampleY = fSwizzler->sampleY(); |
| 466 | if (sampleY > 1) { |
| 467 | // Check to see whether this row or one that falls in the repeatCount is needed in the |
| 468 | // output. |
| 469 | bool foundNecessaryRow = false; |
| 470 | for (unsigned i = 0; i < repeatCount; i++) { |
| 471 | const int potentialRow = yBegin + i; |
| 472 | if (fSwizzler->rowNeeded(potentialRow)) { |
| 473 | dstRow = potentialRow / sampleY; |
| 474 | const int scaledHeight = get_scaled_dimension(this->dstInfo().height(), sampleY); |
| 475 | if (dstRow >= scaledHeight) { |
| 476 | return true; |
| 477 | } |
| 478 | |
| 479 | foundNecessaryRow = true; |
| 480 | repeatCount -= i; |
| 481 | |
| 482 | repeatCount = (repeatCount - 1) / sampleY + 1; |
| 483 | |
| 484 | // Make sure the repeatCount does not take us beyond the end of the dst |
| 485 | if (dstRow + (int) repeatCount > scaledHeight) { |
| 486 | repeatCount = scaledHeight - dstRow; |
| 487 | SkASSERT(repeatCount >= 1); |
| 488 | } |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if (!foundNecessaryRow) { |
| 494 | return true; |
| 495 | } |
Matt Sarett | 8a4e9c5 | 2016-10-25 14:24:50 -0400 | [diff] [blame] | 496 | } else { |
| 497 | // Make sure the repeatCount does not take us beyond the end of the dst |
| 498 | SkASSERT(this->dstInfo().height() >= yBegin); |
| 499 | repeatCount = SkTMin(repeatCount, (unsigned) (this->dstInfo().height() - yBegin)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | if (!fFilledBackground) { |
| 503 | // At this point, we are definitely going to write the row, so count it towards the number |
| 504 | // of rows decoded. |
| 505 | // We do not consider the repeatCount, which only happens for interlaced, in which case we |
| 506 | // have already set fRowsDecoded to the proper value (reflecting that we have filled the |
| 507 | // background). |
| 508 | fRowsDecoded++; |
| 509 | } |
| 510 | |
| 511 | if (!fCurrColorTableIsReal) { |
| 512 | // No color table, so nothing to draw this frame. |
| 513 | // FIXME: We can abort even earlier - no need to decode this frame. |
| 514 | return true; |
| 515 | } |
| 516 | |
| 517 | // The swizzler takes care of offsetting into the dst width-wise. |
| 518 | void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes); |
| 519 | |
| 520 | // We may or may not need to write transparent pixels to the buffer. |
scroggo | 1285f41 | 2016-10-26 13:48:03 -0700 | [diff] [blame] | 521 | // If we're compositing against a previous image, it's wrong, but if |
| 522 | // we're decoding an interlaced gif and displaying it "Haeberli"-style, |
| 523 | // we must write these for passes beyond the first, or the initial passes |
| 524 | // will "show through" the later ones. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 525 | const auto dstInfo = this->dstInfo(); |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 526 | if (writeTransparentPixels) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 527 | fSwizzler->swizzle(dstLine, rowBegin); |
| 528 | } else { |
| 529 | // We cannot swizzle directly into the dst, since that will write the transparent pixels. |
| 530 | // Instead, swizzle into a temporary buffer, and copy that into the dst. |
| 531 | { |
| 532 | void* const memsetDst = fTmpBuffer.get(); |
| 533 | // Although onGetFillValue returns a uint64_t, we only use the low eight bits. The |
| 534 | // return value is either an 8 bit index (for index8) or SK_ColorTRANSPARENT, which is |
| 535 | // all zeroes. |
| 536 | const int fillValue = (uint8_t) this->onGetFillValue(dstInfo); |
| 537 | const size_t rb = dstInfo.minRowBytes(); |
| 538 | if (fillValue == 0) { |
| 539 | // FIXME: This special case should be unnecessary, and in fact sk_bzero just calls |
| 540 | // memset. But without it, the compiler thinks this is trying to pass a zero length |
| 541 | // to memset, causing an error. |
| 542 | sk_bzero(memsetDst, rb); |
| 543 | } else { |
| 544 | memset(memsetDst, fillValue, rb); |
| 545 | } |
| 546 | } |
| 547 | fSwizzler->swizzle(fTmpBuffer.get(), rowBegin); |
| 548 | |
| 549 | const size_t offsetBytes = fSwizzler->swizzleOffsetBytes(); |
| 550 | switch (dstInfo.colorType()) { |
| 551 | case kBGRA_8888_SkColorType: |
| 552 | case kRGBA_8888_SkColorType: { |
| 553 | uint32_t* dstPixel = SkTAddOffset<uint32_t>(dstLine, offsetBytes); |
| 554 | uint32_t* srcPixel = SkTAddOffset<uint32_t>(fTmpBuffer.get(), offsetBytes); |
| 555 | for (int i = 0; i < fSwizzler->swizzleWidth(); i++) { |
| 556 | // Technically SK_ColorTRANSPARENT is an SkPMColor, and srcPixel would have |
| 557 | // the opposite swizzle for the non-native swizzle, but TRANSPARENT is all |
| 558 | // zeroes, which is the same either way. |
| 559 | if (*srcPixel != SK_ColorTRANSPARENT) { |
| 560 | *dstPixel = *srcPixel; |
| 561 | } |
| 562 | dstPixel++; |
| 563 | srcPixel++; |
| 564 | } |
| 565 | break; |
| 566 | } |
| 567 | case kIndex_8_SkColorType: { |
| 568 | uint8_t* dstPixel = SkTAddOffset<uint8_t>(dstLine, offsetBytes); |
| 569 | uint8_t* srcPixel = SkTAddOffset<uint8_t>(fTmpBuffer.get(), offsetBytes); |
| 570 | for (int i = 0; i < fSwizzler->swizzleWidth(); i++) { |
| 571 | if (*srcPixel != frameContext->transparentPixel()) { |
| 572 | *dstPixel = *srcPixel; |
| 573 | } |
| 574 | dstPixel++; |
| 575 | srcPixel++; |
| 576 | } |
| 577 | break; |
| 578 | } |
| 579 | default: |
| 580 | SkASSERT(false); |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | // Tell the frame to copy the row data if need be. |
| 586 | if (repeatCount > 1) { |
| 587 | const size_t bytesPerPixel = SkColorTypeBytesPerPixel(this->dstInfo().colorType()); |
| 588 | const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel; |
| 589 | void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetBytes()); |
| 590 | void* dst = copiedLine; |
| 591 | for (unsigned i = 1; i < repeatCount; i++) { |
| 592 | dst = SkTAddOffset<void>(dst, fDstRowBytes); |
| 593 | memcpy(dst, copiedLine, bytesToCopy); |
msarett | 72261c0 | 2015-11-19 15:29:26 -0800 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
| 597 | return true; |
| 598 | } |