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)) { |
Leon Scroggins III | 4993b95 | 2016-12-08 11:54:04 -0500 | [diff] [blame] | 77 | // Fatal error occurred. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 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 | |
Leon Scroggins III | 4993b95 | 2016-12-08 11:54:04 -0500 | [diff] [blame] | 81 | // If no images are in the data, or the first header is not yet defined, we cannot |
| 82 | // create a codec. In either case, the width and height are not yet known. |
| 83 | if (0 == reader->imagesCount() || !reader->frameContext(0)->isHeaderDefined()) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 84 | return nullptr; |
| 85 | } |
| 86 | |
Leon Scroggins III | 4993b95 | 2016-12-08 11:54:04 -0500 | [diff] [blame] | 87 | // isHeaderDefined() will not return true if the screen size is empty. |
| 88 | SkASSERT(reader->screenHeight() > 0 && reader->screenWidth() > 0); |
| 89 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 90 | const auto alpha = reader->firstFrameHasAlpha() ? SkEncodedInfo::kBinary_Alpha |
| 91 | : SkEncodedInfo::kOpaque_Alpha; |
| 92 | // Use kPalette since Gifs are encoded with a color table. |
| 93 | // FIXME: Gifs can actually be encoded with 4-bits per pixel. Using 8 works, but we could skip |
| 94 | // expanding to 8 bits and take advantage of the SkSwizzler to work from 4. |
| 95 | const auto encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, alpha, 8); |
| 96 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 97 | // The choice of unpremul versus premul is arbitrary, since all colors are either fully |
| 98 | // opaque or fully transparent (i.e. kBinary), but we stored the transparent colors as all |
| 99 | // zeroes, which is arguably premultiplied. |
| 100 | const auto alphaType = reader->firstFrameHasAlpha() ? kUnpremul_SkAlphaType |
| 101 | : kOpaque_SkAlphaType; |
Matt Sarett | 7f650bd | 2016-10-30 21:25:34 -0400 | [diff] [blame] | 102 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 103 | const auto imageInfo = SkImageInfo::Make(reader->screenWidth(), reader->screenHeight(), |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 104 | kN32_SkColorType, alphaType, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 105 | SkColorSpace::MakeSRGB()); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 106 | return new SkGifCodec(encodedInfo, imageInfo, reader.release()); |
| 107 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 108 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 109 | bool SkGifCodec::onRewind() { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 110 | fReader->clearDecodeState(); |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 114 | SkGifCodec::SkGifCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo, |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 115 | SkGifImageReader* reader) |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 116 | : INHERITED(encodedInfo, imageInfo, SkColorSpaceXform::kRGBA_8888_ColorFormat, nullptr) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 117 | , fReader(reader) |
| 118 | , fTmpBuffer(nullptr) |
| 119 | , fSwizzler(nullptr) |
| 120 | , fCurrColorTable(nullptr) |
| 121 | , fCurrColorTableIsReal(false) |
| 122 | , fFilledBackground(false) |
| 123 | , fFirstCallToIncrementalDecode(false) |
| 124 | , fDst(nullptr) |
| 125 | , fDstRowBytes(0) |
| 126 | , fRowsDecoded(0) |
| 127 | { |
| 128 | reader->setClient(this); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 129 | } |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 130 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 131 | int SkGifCodec::onGetFrameCount() { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 132 | fReader->parse(SkGifImageReader::SkGIFFrameCountQuery); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 133 | return fReader->imagesCount(); |
| 134 | } |
| 135 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 136 | bool SkGifCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 137 | if (i >= fReader->imagesCount()) { |
| 138 | return false; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 139 | } |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 140 | |
| 141 | const SkGIFFrameContext* frameContext = fReader->frameContext(i); |
| 142 | if (!frameContext->reachedStartOfData()) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | if (frameInfo) { |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 147 | frameInfo->fDuration = frameContext->getDuration(); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 148 | frameInfo->fRequiredFrame = frameContext->getRequiredFrame(); |
| 149 | frameInfo->fFullyReceived = frameContext->isComplete(); |
| 150 | frameInfo->fAlphaType = frameContext->hasAlpha() ? kUnpremul_SkAlphaType |
| 151 | : kOpaque_SkAlphaType; |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 152 | frameInfo->fDisposalMethod = frameContext->getDisposalMethod(); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 153 | } |
| 154 | return true; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 155 | } |
| 156 | |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 157 | int SkGifCodec::onGetRepetitionCount() { |
| 158 | fReader->parse(SkGifImageReader::SkGIFLoopCountQuery); |
| 159 | return fReader->loopCount(); |
| 160 | } |
| 161 | |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 162 | static const SkColorType kXformSrcColorType = kRGBA_8888_SkColorType; |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 163 | static const SkAlphaType kXformAlphaType = kUnpremul_SkAlphaType; |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 164 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 165 | void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, int frameIndex) { |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 166 | SkColorType colorTableColorType = dstInfo.colorType(); |
| 167 | if (this->colorXform()) { |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 168 | colorTableColorType = kXformSrcColorType; |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | sk_sp<SkColorTable> currColorTable = fReader->getColorTable(colorTableColorType, frameIndex); |
| 172 | fCurrColorTableIsReal = currColorTable; |
| 173 | if (!fCurrColorTableIsReal) { |
Leon Scroggins III | a049ac4 | 2016-10-27 11:16:11 -0400 | [diff] [blame] | 174 | // This is possible for an empty frame. Create a dummy with one value (transparent). |
| 175 | SkPMColor color = SK_ColorTRANSPARENT; |
| 176 | fCurrColorTable.reset(new SkColorTable(&color, 1)); |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 177 | } else if (this->colorXform() && !this->xformOnDecode()) { |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 178 | SkPMColor dstColors[256]; |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 179 | this->applyColorXform(dstColors, currColorTable->readColors(), currColorTable->count(), |
| 180 | kXformAlphaType); |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 181 | fCurrColorTable.reset(new SkColorTable(dstColors, currColorTable->count())); |
| 182 | } else { |
| 183 | fCurrColorTable = std::move(currColorTable); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 184 | } |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 185 | } |
| 186 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 187 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 188 | SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, const Options& opts) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 189 | if (opts.fSubset) { |
| 190 | return gif_error("Subsets not supported.\n", kUnimplemented); |
| 191 | } |
| 192 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 193 | const int frameIndex = opts.fFrameIndex; |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 194 | if (frameIndex > 0 && kRGB_565_SkColorType == dstInfo.colorType()) { |
| 195 | // FIXME: In theory, we might be able to support this, but it's not clear that it |
| 196 | // is necessary (Chromium does not decode to 565, and Android does not decode |
| 197 | // frames beyond the first). Disabling it because it is somewhat difficult: |
| 198 | // - If there is a transparent pixel, and this frame draws on top of another frame |
| 199 | // (if the frame is independent with a transparent pixel, we should not decode to |
| 200 | // 565 anyway, since it is not opaque), we need to skip drawing the transparent |
| 201 | // pixels (see writeTransparentPixels in haveDecodedRow). We currently do this by |
| 202 | // first swizzling into temporary memory, then copying into the destination. (We |
| 203 | // let the swizzler handle it first because it may need to sample.) After |
| 204 | // swizzling to 565, we do not know which pixels in our temporary memory |
| 205 | // correspond to the transparent pixel, so we do not know what to skip. We could |
| 206 | // special case the non-sampled case (no need to swizzle), but as this is |
| 207 | // currently unused we can just not support it. |
| 208 | return gif_error("Cannot decode multiframe gif (except frame 0) as 565.\n", |
| 209 | kInvalidConversion); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 212 | const auto* frame = fReader->frameContext(frameIndex); |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 213 | SkASSERT(frame); |
| 214 | if (0 == frameIndex) { |
| 215 | // SkCodec does not have a way to just parse through frame 0, so we |
| 216 | // have to do so manually, here. |
| 217 | fReader->parse((SkGifImageReader::SkGIFParseQuery) 0); |
| 218 | if (!frame->reachedStartOfData()) { |
| 219 | // We have parsed enough to know that there is a color map, but cannot |
| 220 | // parse the map itself yet. Exit now, so we do not build an incorrect |
| 221 | // table. |
| 222 | return gif_error("color map not available yet\n", kIncompleteInput); |
| 223 | } |
| 224 | } else { |
| 225 | // Parsing happened in SkCodec::getPixels. |
| 226 | SkASSERT(frameIndex < fReader->imagesCount()); |
| 227 | SkASSERT(frame->reachedStartOfData()); |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 228 | } |
| 229 | |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 230 | const auto at = frame->hasAlpha() ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType; |
| 231 | const auto srcInfo = this->getInfo().makeAlphaType(at); |
| 232 | if (!conversion_possible(dstInfo, srcInfo) || |
| 233 | !this->initializeColorXform(dstInfo, opts.fPremulBehavior)) |
| 234 | { |
| 235 | return gif_error("Cannot convert input type to output type.\n", kInvalidConversion); |
| 236 | } |
| 237 | |
| 238 | if (this->xformOnDecode()) { |
| 239 | fXformBuffer.reset(new uint32_t[dstInfo.width()]); |
| 240 | sk_bzero(fXformBuffer.get(), dstInfo.width() * sizeof(uint32_t)); |
| 241 | } |
| 242 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 243 | fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]); |
| 244 | |
Leon Scroggins III | fc49b40 | 2016-10-31 14:08:56 -0400 | [diff] [blame] | 245 | this->initializeColorTable(dstInfo, frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 246 | this->initializeSwizzler(dstInfo, frameIndex); |
Leon Scroggins III | fc49b40 | 2016-10-31 14:08:56 -0400 | [diff] [blame] | 247 | |
| 248 | SkASSERT(fCurrColorTable); |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 249 | return kSuccess; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 252 | void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, int frameIndex) { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 253 | const SkGIFFrameContext* frame = fReader->frameContext(frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 254 | // This is only called by prepareToDecode, which ensures frameIndex is in range. |
| 255 | SkASSERT(frame); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 256 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 257 | const int xBegin = frame->xOffset(); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 258 | const int xEnd = std::min(frame->frameRect().right(), fReader->screenWidth()); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 259 | |
| 260 | // CreateSwizzler only reads left and right of the frame. We cannot use the frame's raw |
| 261 | // frameRect, since it might extend beyond the edge of the frame. |
| 262 | SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0); |
| 263 | |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 264 | SkImageInfo swizzlerInfo = dstInfo; |
| 265 | if (this->colorXform()) { |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 266 | swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType); |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 267 | if (kPremul_SkAlphaType == dstInfo.alphaType()) { |
| 268 | swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType); |
| 269 | } |
| 270 | } |
| 271 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 272 | // The default Options should be fine: |
| 273 | // - we'll ignore if the memory is zero initialized - unless we're the first frame, this won't |
| 274 | // matter anyway. |
| 275 | // - subsets are not supported for gif |
| 276 | // - the swizzler does not need to know about the frame. |
| 277 | // We may not be able to use the real Options anyway, since getPixels does not store it (due to |
| 278 | // a bug). |
| 279 | fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 280 | fCurrColorTable->readColors(), swizzlerInfo, Options(), &swizzleRect)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 281 | SkASSERT(fSwizzler.get()); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Initiates the gif decode |
| 286 | */ |
| 287 | SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 288 | void* pixels, size_t dstRowBytes, |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 289 | const Options& opts, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 290 | int* rowsDecoded) { |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 291 | Result result = this->prepareToDecode(dstInfo, opts); |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 292 | switch (result) { |
| 293 | case kSuccess: |
| 294 | break; |
| 295 | case kIncompleteInput: |
| 296 | // onStartIncrementalDecode treats this as incomplete, since it may |
| 297 | // provide more data later, but in this case, no more data will be |
| 298 | // provided, and there is nothing to draw. We also cannot return |
| 299 | // kIncompleteInput, which will make SkCodec attempt to fill |
| 300 | // remaining rows, but that requires an SkSwizzler, which we have |
| 301 | // not created. |
| 302 | return kInvalidInput; |
| 303 | default: |
| 304 | return result; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 308 | return gif_error("Scaling not supported.\n", kInvalidScale); |
| 309 | } |
| 310 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 311 | fDst = pixels; |
| 312 | fDstRowBytes = dstRowBytes; |
| 313 | |
| 314 | return this->decodeFrame(true, opts, rowsDecoded); |
| 315 | } |
| 316 | |
| 317 | SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo, |
| 318 | void* pixels, size_t dstRowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 319 | const SkCodec::Options& opts) { |
| 320 | Result result = this->prepareToDecode(dstInfo, opts); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 321 | if (result != kSuccess) { |
| 322 | return result; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 323 | } |
| 324 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 325 | fDst = pixels; |
| 326 | fDstRowBytes = dstRowBytes; |
| 327 | |
| 328 | fFirstCallToIncrementalDecode = true; |
| 329 | |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 330 | return kSuccess; |
| 331 | } |
| 332 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 333 | SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) { |
| 334 | // It is possible the client has appended more data. Parse, if needed. |
| 335 | const auto& options = this->options(); |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 336 | const int frameIndex = options.fFrameIndex; |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 337 | fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 338 | |
| 339 | const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode; |
| 340 | fFirstCallToIncrementalDecode = false; |
| 341 | return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 342 | } |
| 343 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 344 | SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded) { |
| 345 | const SkImageInfo& dstInfo = this->dstInfo(); |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 346 | const int frameIndex = opts.fFrameIndex; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 347 | SkASSERT(frameIndex < fReader->imagesCount()); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 348 | const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 349 | if (firstAttempt) { |
| 350 | // rowsDecoded reports how many rows have been initialized, so a layer above |
| 351 | // can fill the rest. In some cases, we fill the background before decoding |
| 352 | // (or it is already filled for us), so we report rowsDecoded to be the full |
| 353 | // height. |
| 354 | bool filledBackground = false; |
| 355 | if (frameContext->getRequiredFrame() == kNone) { |
| 356 | // We may need to clear to transparent for one of the following reasons: |
| 357 | // - The frameRect does not cover the full bounds. haveDecodedRow will |
| 358 | // only draw inside the frameRect, so we need to clear the rest. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 359 | // - The frame is interlaced. There is no obvious way to fill |
| 360 | // afterwards for an incomplete image. (FIXME: Does the first pass |
| 361 | // cover all rows? If so, we do not have to fill here.) |
scroggo | 8bce117 | 2016-10-25 13:08:40 -0700 | [diff] [blame] | 362 | // - There is no color table for this frame. In that case will not |
| 363 | // draw anything, so we need to fill. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 364 | if (frameContext->frameRect() != this->getInfo().bounds() |
scroggo | 8bce117 | 2016-10-25 13:08:40 -0700 | [diff] [blame] | 365 | || frameContext->interlaced() || !fCurrColorTableIsReal) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 366 | // fill ignores the width (replaces it with the actual, scaled width). |
| 367 | // But we need to scale in Y. |
| 368 | const int scaledHeight = get_scaled_dimension(dstInfo.height(), |
| 369 | fSwizzler->sampleY()); |
| 370 | auto fillInfo = dstInfo.makeWH(0, scaledHeight); |
| 371 | fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue(dstInfo), |
| 372 | opts.fZeroInitialized); |
| 373 | filledBackground = true; |
| 374 | } |
| 375 | } else { |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 376 | // Not independent. |
| 377 | // SkCodec ensured that the prior frame has been decoded. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 378 | filledBackground = true; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 379 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 380 | |
| 381 | fFilledBackground = filledBackground; |
| 382 | if (filledBackground) { |
| 383 | // Report the full (scaled) height, since the client will never need to fill. |
| 384 | fRowsDecoded = get_scaled_dimension(dstInfo.height(), fSwizzler->sampleY()); |
| 385 | } else { |
| 386 | // This will be updated by haveDecodedRow. |
| 387 | fRowsDecoded = 0; |
| 388 | } |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 389 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 390 | |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 391 | if (!fCurrColorTableIsReal) { |
| 392 | // Nothing to draw this frame. |
| 393 | return kSuccess; |
| 394 | } |
| 395 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 396 | bool frameDecoded = false; |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 397 | const bool fatalError = !fReader->decode(frameIndex, &frameDecoded); |
| 398 | if (fatalError || !frameDecoded) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 399 | if (rowsDecoded) { |
| 400 | *rowsDecoded = fRowsDecoded; |
| 401 | } |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 402 | if (fatalError) { |
| 403 | return kErrorInInput; |
| 404 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 405 | return kIncompleteInput; |
| 406 | } |
| 407 | |
| 408 | return kSuccess; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 409 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 410 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 411 | uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 412 | // Using transparent as the fill value matches the behavior in Chromium, |
| 413 | // which ignores the background color. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 414 | return SK_ColorTRANSPARENT; |
| 415 | } |
msarett | 72261c0 | 2015-11-19 15:29:26 -0800 | [diff] [blame] | 416 | |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 417 | void SkGifCodec::applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const { |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 418 | if (this->xformOnDecode()) { |
| 419 | SkASSERT(this->colorXform()); |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 420 | fSwizzler->swizzle(fXformBuffer.get(), src); |
| 421 | |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 422 | const int xformWidth = get_scaled_dimension(dstInfo.width(), fSwizzler->sampleX()); |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 423 | this->applyColorXform(dst, fXformBuffer.get(), xformWidth, kXformAlphaType); |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 424 | } else { |
| 425 | fSwizzler->swizzle(dst, src); |
| 426 | } |
| 427 | } |
| 428 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 429 | bool SkGifCodec::haveDecodedRow(int frameIndex, const unsigned char* rowBegin, |
| 430 | int rowNumber, int repeatCount, bool writeTransparentPixels) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 431 | { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 432 | const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 433 | // The pixel data and coordinates supplied to us are relative to the frame's |
| 434 | // origin within the entire image size, i.e. |
| 435 | // (frameContext->xOffset, frameContext->yOffset). There is no guarantee |
| 436 | // that width == (size().width() - frameContext->xOffset), so |
| 437 | // we must ensure we don't run off the end of either the source data or the |
| 438 | // row's X-coordinates. |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 439 | const int width = frameContext->width(); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 440 | const int xBegin = frameContext->xOffset(); |
| 441 | const int yBegin = frameContext->yOffset() + rowNumber; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 442 | const int xEnd = std::min(xBegin + width, this->getInfo().width()); |
| 443 | const int yEnd = std::min(yBegin + rowNumber + repeatCount, this->getInfo().height()); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 444 | // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We could instead do |
| 445 | // this once in prepareToDecode. |
| 446 | if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin)) |
| 447 | return true; |
| 448 | |
| 449 | // yBegin is the first row in the non-sampled image. dstRow will be the row in the output, |
| 450 | // after potentially scaling it. |
| 451 | int dstRow = yBegin; |
| 452 | |
| 453 | const int sampleY = fSwizzler->sampleY(); |
| 454 | if (sampleY > 1) { |
| 455 | // Check to see whether this row or one that falls in the repeatCount is needed in the |
| 456 | // output. |
| 457 | bool foundNecessaryRow = false; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 458 | for (int i = 0; i < repeatCount; i++) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 459 | const int potentialRow = yBegin + i; |
| 460 | if (fSwizzler->rowNeeded(potentialRow)) { |
| 461 | dstRow = potentialRow / sampleY; |
| 462 | const int scaledHeight = get_scaled_dimension(this->dstInfo().height(), sampleY); |
| 463 | if (dstRow >= scaledHeight) { |
| 464 | return true; |
| 465 | } |
| 466 | |
| 467 | foundNecessaryRow = true; |
| 468 | repeatCount -= i; |
| 469 | |
| 470 | repeatCount = (repeatCount - 1) / sampleY + 1; |
| 471 | |
| 472 | // Make sure the repeatCount does not take us beyond the end of the dst |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 473 | if (dstRow + repeatCount > scaledHeight) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 474 | repeatCount = scaledHeight - dstRow; |
| 475 | SkASSERT(repeatCount >= 1); |
| 476 | } |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | if (!foundNecessaryRow) { |
| 482 | return true; |
| 483 | } |
Matt Sarett | 8a4e9c5 | 2016-10-25 14:24:50 -0400 | [diff] [blame] | 484 | } else { |
| 485 | // Make sure the repeatCount does not take us beyond the end of the dst |
| 486 | SkASSERT(this->dstInfo().height() >= yBegin); |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 487 | repeatCount = SkTMin(repeatCount, this->dstInfo().height() - yBegin); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | if (!fFilledBackground) { |
| 491 | // At this point, we are definitely going to write the row, so count it towards the number |
| 492 | // of rows decoded. |
| 493 | // We do not consider the repeatCount, which only happens for interlaced, in which case we |
| 494 | // have already set fRowsDecoded to the proper value (reflecting that we have filled the |
| 495 | // background). |
| 496 | fRowsDecoded++; |
| 497 | } |
| 498 | |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 499 | // decodeFrame will early exit if this is false, so this method will not be |
| 500 | // called. |
| 501 | SkASSERT(fCurrColorTableIsReal); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 502 | |
| 503 | // The swizzler takes care of offsetting into the dst width-wise. |
| 504 | void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes); |
| 505 | |
| 506 | // We may or may not need to write transparent pixels to the buffer. |
scroggo | 1285f41 | 2016-10-26 13:48:03 -0700 | [diff] [blame] | 507 | // If we're compositing against a previous image, it's wrong, but if |
| 508 | // we're decoding an interlaced gif and displaying it "Haeberli"-style, |
| 509 | // we must write these for passes beyond the first, or the initial passes |
| 510 | // will "show through" the later ones. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 511 | const auto dstInfo = this->dstInfo(); |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 512 | if (writeTransparentPixels) { |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 513 | this->applyXformRow(dstInfo, dstLine, rowBegin); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 514 | } else { |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 515 | sk_bzero(fTmpBuffer.get(), dstInfo.minRowBytes()); |
| 516 | this->applyXformRow(dstInfo, fTmpBuffer.get(), rowBegin); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 517 | |
| 518 | const size_t offsetBytes = fSwizzler->swizzleOffsetBytes(); |
| 519 | switch (dstInfo.colorType()) { |
| 520 | case kBGRA_8888_SkColorType: |
| 521 | case kRGBA_8888_SkColorType: { |
| 522 | uint32_t* dstPixel = SkTAddOffset<uint32_t>(dstLine, offsetBytes); |
| 523 | uint32_t* srcPixel = SkTAddOffset<uint32_t>(fTmpBuffer.get(), offsetBytes); |
| 524 | for (int i = 0; i < fSwizzler->swizzleWidth(); i++) { |
| 525 | // Technically SK_ColorTRANSPARENT is an SkPMColor, and srcPixel would have |
| 526 | // the opposite swizzle for the non-native swizzle, but TRANSPARENT is all |
| 527 | // zeroes, which is the same either way. |
| 528 | if (*srcPixel != SK_ColorTRANSPARENT) { |
| 529 | *dstPixel = *srcPixel; |
| 530 | } |
| 531 | dstPixel++; |
| 532 | srcPixel++; |
| 533 | } |
| 534 | break; |
| 535 | } |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 536 | case kRGBA_F16_SkColorType: { |
| 537 | uint64_t* dstPixel = SkTAddOffset<uint64_t>(dstLine, offsetBytes); |
| 538 | uint64_t* srcPixel = SkTAddOffset<uint64_t>(fTmpBuffer.get(), offsetBytes); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 539 | for (int i = 0; i < fSwizzler->swizzleWidth(); i++) { |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 540 | if (*srcPixel != 0) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 541 | *dstPixel = *srcPixel; |
| 542 | } |
| 543 | dstPixel++; |
| 544 | srcPixel++; |
| 545 | } |
| 546 | break; |
| 547 | } |
| 548 | default: |
| 549 | SkASSERT(false); |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | // Tell the frame to copy the row data if need be. |
| 555 | if (repeatCount > 1) { |
| 556 | const size_t bytesPerPixel = SkColorTypeBytesPerPixel(this->dstInfo().colorType()); |
| 557 | const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel; |
| 558 | void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetBytes()); |
| 559 | void* dst = copiedLine; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 560 | for (int i = 1; i < repeatCount; i++) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 561 | dst = SkTAddOffset<void>(dst, fDstRowBytes); |
| 562 | memcpy(dst, copiedLine, bytesToCopy); |
msarett | 72261c0 | 2015-11-19 15:29:26 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | return true; |
| 567 | } |