scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [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 | |
msarett | b46e5e2 | 2015-07-30 11:36:40 -0700 | [diff] [blame] | 8 | #include "SkBmpCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 9 | #include "SkCodec.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 10 | #include "SkCodecPriv.h" |
msarett | ad8bcfe | 2016-03-07 07:09:03 -0800 | [diff] [blame] | 11 | #include "SkColorSpace.h" |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame] | 12 | #include "SkColorSpaceXform_Base.h" |
msarett | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 13 | #include "SkData.h" |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 14 | #include "SkFrameHolder.h" |
msarett | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 15 | #include "SkGifCodec.h" |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 16 | #include "SkHalf.h" |
msarett | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 17 | #include "SkIcoCodec.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 18 | #include "SkJpegCodec.h" |
msarett | ad3a5c6 | 2016-05-06 07:21:26 -0700 | [diff] [blame] | 19 | #ifdef SK_HAS_PNG_LIBRARY |
msarett | be1d555 | 2016-01-21 09:05:23 -0800 | [diff] [blame] | 20 | #include "SkPngCodec.h" |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 21 | #endif |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 22 | #include "SkRawCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 23 | #include "SkStream.h" |
msarett | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 24 | #include "SkWbmpCodec.h" |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 25 | #include "SkWebpCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 26 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 27 | struct DecoderProc { |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 28 | bool (*IsFormat)(const void*, size_t); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 29 | SkCodec* (*NewFromStream)(SkStream*); |
| 30 | }; |
| 31 | |
| 32 | static const DecoderProc gDecoderProcs[] = { |
msarett | ad3a5c6 | 2016-05-06 07:21:26 -0700 | [diff] [blame] | 33 | #ifdef SK_HAS_JPEG_LIBRARY |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 34 | { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream }, |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 35 | #endif |
msarett | ad3a5c6 | 2016-05-06 07:21:26 -0700 | [diff] [blame] | 36 | #ifdef SK_HAS_WEBP_LIBRARY |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 37 | { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream }, |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 38 | #endif |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 39 | { SkGifCodec::IsGif, SkGifCodec::NewFromStream }, |
msarett | ad3a5c6 | 2016-05-06 07:21:26 -0700 | [diff] [blame] | 40 | #ifdef SK_HAS_PNG_LIBRARY |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 41 | { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 42 | #endif |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 43 | { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }, |
| 44 | { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 47 | size_t SkCodec::MinBufferedBytesNeeded() { |
| 48 | return WEBP_VP8_HEADER_SIZE; |
| 49 | } |
| 50 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 51 | SkCodec* SkCodec::NewFromStream(SkStream* stream, |
| 52 | SkPngChunkReader* chunkReader) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 53 | if (!stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 55 | } |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 56 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 57 | std::unique_ptr<SkStream> streamDeleter(stream); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 58 | |
| 59 | // 14 is enough to read all of the supported types. |
| 60 | const size_t bytesToRead = 14; |
| 61 | SkASSERT(bytesToRead <= MinBufferedBytesNeeded()); |
| 62 | |
| 63 | char buffer[bytesToRead]; |
| 64 | size_t bytesRead = stream->peek(buffer, bytesToRead); |
| 65 | |
| 66 | // It is also possible to have a complete image less than bytesToRead bytes |
| 67 | // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead. |
| 68 | // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter |
| 69 | // than bytesToRead, so pass that directly to the decoder. |
| 70 | // It also is possible the stream uses too small a buffer for peeking, but |
| 71 | // we trust the caller to use a large enough buffer. |
| 72 | |
| 73 | if (0 == bytesRead) { |
scroggo | 3ab9f2e | 2016-01-06 09:53:34 -0800 | [diff] [blame] | 74 | // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this |
| 75 | // printf could be useful to notice failures. |
| 76 | // SkCodecPrintf("Encoded image data failed to peek!\n"); |
| 77 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 78 | // It is possible the stream does not support peeking, but does support |
| 79 | // rewinding. |
| 80 | // Attempt to read() and pass the actual amount read to the decoder. |
| 81 | bytesRead = stream->read(buffer, bytesToRead); |
| 82 | if (!stream->rewind()) { |
scroggo | 3ab9f2e | 2016-01-06 09:53:34 -0800 | [diff] [blame] | 83 | SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n"); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 84 | return nullptr; |
| 85 | } |
| 86 | } |
| 87 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 88 | // PNG is special, since we want to be able to supply an SkPngChunkReader. |
| 89 | // But this code follows the same pattern as the loop. |
msarett | ad3a5c6 | 2016-05-06 07:21:26 -0700 | [diff] [blame] | 90 | #ifdef SK_HAS_PNG_LIBRARY |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 91 | if (SkPngCodec::IsPng(buffer, bytesRead)) { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 92 | return SkPngCodec::NewFromStream(streamDeleter.release(), chunkReader); |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 93 | } else |
| 94 | #endif |
| 95 | { |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 96 | for (DecoderProc proc : gDecoderProcs) { |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 97 | if (proc.IsFormat(buffer, bytesRead)) { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 98 | return proc.NewFromStream(streamDeleter.release()); |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 99 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 100 | } |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 101 | |
| 102 | #ifdef SK_CODEC_DECODES_RAW |
| 103 | // Try to treat the input as RAW if all the other checks failed. |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 104 | return SkRawCodec::NewFromStream(streamDeleter.release()); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 105 | #endif |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 106 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 107 | |
msarett | f44631b | 2016-01-13 10:54:20 -0800 | [diff] [blame] | 108 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 109 | } |
| 110 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 111 | SkCodec* SkCodec::NewFromData(sk_sp<SkData> data, SkPngChunkReader* reader) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 112 | if (!data) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 113 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 114 | } |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 115 | return NewFromStream(new SkMemoryStream(data), reader); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 118 | SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info, |
| 119 | XformFormat srcFormat, SkStream* stream, |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 120 | sk_sp<SkColorSpace> colorSpace, Origin origin) |
| 121 | : fEncodedInfo(info) |
msarett | 530c844 | 2016-07-21 11:57:49 -0700 | [diff] [blame] | 122 | , fSrcInfo(info.makeImageInfo(width, height, std::move(colorSpace))) |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 123 | , fSrcXformFormat(srcFormat) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 124 | , fStream(stream) |
| 125 | , fNeedsRewind(false) |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 126 | , fOrigin(origin) |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 127 | , fDstInfo() |
| 128 | , fOptions() |
| 129 | , fCurrScanline(-1) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 130 | {} |
| 131 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 132 | SkCodec::SkCodec(const SkEncodedInfo& info, const SkImageInfo& imageInfo, |
| 133 | XformFormat srcFormat, SkStream* stream, Origin origin) |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 134 | : fEncodedInfo(info) |
| 135 | , fSrcInfo(imageInfo) |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 136 | , fSrcXformFormat(srcFormat) |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 137 | , fStream(stream) |
| 138 | , fNeedsRewind(false) |
| 139 | , fOrigin(origin) |
| 140 | , fDstInfo() |
| 141 | , fOptions() |
| 142 | , fCurrScanline(-1) |
| 143 | {} |
| 144 | |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 145 | SkCodec::~SkCodec() {} |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 146 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 147 | bool SkCodec::rewindIfNeeded() { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 148 | // Store the value of fNeedsRewind so we can update it. Next read will |
| 149 | // require a rewind. |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 150 | const bool needsRewind = fNeedsRewind; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 151 | fNeedsRewind = true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 152 | if (!needsRewind) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 153 | return true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 154 | } |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 155 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 156 | // startScanlineDecode will need to be called before decoding scanlines. |
| 157 | fCurrScanline = -1; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 158 | // startIncrementalDecode will need to be called before incrementalDecode. |
| 159 | fStartedIncrementalDecode = false; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 160 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 161 | // Some codecs do not have a stream. They may hold onto their own data or another codec. |
| 162 | // They must handle rewinding themselves. |
| 163 | if (fStream && !fStream->rewind()) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | |
| 167 | return this->onRewind(); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 168 | } |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 169 | |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 170 | #define CHECK_COLOR_TABLE \ |
| 171 | if (kIndex_8_SkColorType == info.colorType()) { \ |
| 172 | if (nullptr == ctable || nullptr == ctableCount) { \ |
| 173 | return SkCodec::kInvalidParameters; \ |
| 174 | } \ |
| 175 | } else { \ |
| 176 | if (ctableCount) { \ |
| 177 | *ctableCount = 0; \ |
| 178 | } \ |
| 179 | ctableCount = nullptr; \ |
| 180 | ctable = nullptr; \ |
| 181 | } |
| 182 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 183 | static void zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes, |
| 184 | SkIRect frameRect) { |
| 185 | if (!frameRect.intersect(dstInfo.bounds())) { |
| 186 | return; |
| 187 | } |
| 188 | const auto info = dstInfo.makeWH(frameRect.width(), frameRect.height()); |
| 189 | const size_t bpp = SkColorTypeBytesPerPixel(dstInfo.colorType()); |
| 190 | const size_t offset = frameRect.x() * bpp + frameRect.y() * rowBytes; |
| 191 | auto* eraseDst = SkTAddOffset<void>(pixels, offset); |
| 192 | SkSampler::Fill(info, eraseDst, rowBytes, 0, SkCodec::kNo_ZeroInitialized); |
| 193 | } |
| 194 | |
| 195 | SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 196 | const Options& options) { |
| 197 | const int index = options.fFrameIndex; |
| 198 | if (0 == index) { |
| 199 | return kSuccess; |
| 200 | } |
| 201 | |
| 202 | if (options.fSubset || info.dimensions() != fSrcInfo.dimensions()) { |
| 203 | // If we add support for these, we need to update the code that zeroes |
| 204 | // a kRestoreBGColor frame. |
| 205 | return kInvalidParameters; |
| 206 | } |
| 207 | |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 208 | // index 8 is not supported beyond the first frame. |
| 209 | if (index < 0 || info.colorType() == kIndex_8_SkColorType) { |
| 210 | return kInvalidParameters; |
| 211 | } |
| 212 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 213 | if (index >= this->onGetFrameCount()) { |
| 214 | return kIncompleteInput; |
| 215 | } |
| 216 | |
| 217 | const auto* frameHolder = this->getFrameHolder(); |
| 218 | SkASSERT(frameHolder); |
| 219 | |
| 220 | const auto* frame = frameHolder->getFrame(index); |
| 221 | SkASSERT(frame); |
| 222 | |
| 223 | const int requiredFrame = frame->getRequiredFrame(); |
| 224 | if (requiredFrame == kNone) { |
| 225 | return kSuccess; |
| 226 | } |
| 227 | |
| 228 | if (options.fPriorFrame != kNone) { |
| 229 | // Check for a valid frame as a starting point. Alternatively, we could |
| 230 | // treat an invalid frame as not providing one, but rejecting it will |
| 231 | // make it easier to catch the mistake. |
| 232 | if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) { |
| 233 | return kInvalidParameters; |
| 234 | } |
| 235 | const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame); |
| 236 | switch (prevFrame->getDisposalMethod()) { |
| 237 | case SkCodecAnimation::DisposalMethod::kRestorePrevious: |
| 238 | return kInvalidParameters; |
| 239 | case SkCodecAnimation::DisposalMethod::kRestoreBGColor: |
| 240 | // If a frame after the required frame is provided, there is no |
| 241 | // need to clear, since it must be covered by the desired frame. |
| 242 | if (options.fPriorFrame == requiredFrame) { |
| 243 | zero_rect(info, pixels, rowBytes, prevFrame->frameRect()); |
| 244 | } |
| 245 | break; |
| 246 | default: |
| 247 | break; |
| 248 | } |
| 249 | return kSuccess; |
| 250 | } |
| 251 | |
| 252 | Options prevFrameOptions(options); |
| 253 | prevFrameOptions.fFrameIndex = requiredFrame; |
| 254 | prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized; |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 255 | const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions, |
| 256 | nullptr, nullptr); |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 257 | if (result == kSuccess) { |
| 258 | const auto* prevFrame = frameHolder->getFrame(requiredFrame); |
| 259 | const auto disposalMethod = prevFrame->getDisposalMethod(); |
| 260 | if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) { |
| 261 | zero_rect(info, pixels, rowBytes, prevFrame->frameRect()); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return result; |
| 266 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 267 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 268 | SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 269 | const Options* options, SkPMColor ctable[], int* ctableCount) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 270 | if (kUnknown_SkColorType == info.colorType()) { |
| 271 | return kInvalidConversion; |
| 272 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 273 | if (nullptr == pixels) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 274 | return kInvalidParameters; |
| 275 | } |
| 276 | if (rowBytes < info.minRowBytes()) { |
| 277 | return kInvalidParameters; |
| 278 | } |
| 279 | |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 280 | CHECK_COLOR_TABLE; |
| 281 | |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 282 | if (!this->rewindIfNeeded()) { |
| 283 | return kCouldNotRewind; |
| 284 | } |
| 285 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 286 | // Default options. |
| 287 | Options optsStorage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 288 | if (nullptr == options) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 289 | options = &optsStorage; |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 290 | } else { |
| 291 | const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, *options); |
| 292 | if (frameIndexResult != kSuccess) { |
| 293 | return frameIndexResult; |
| 294 | } |
| 295 | if (options->fSubset) { |
| 296 | SkIRect subset(*options->fSubset); |
| 297 | if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) { |
| 298 | // FIXME: How to differentiate between not supporting subset at all |
| 299 | // and not supporting this particular subset? |
| 300 | return kUnimplemented; |
| 301 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 302 | } |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 303 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 304 | |
| 305 | // FIXME: Support subsets somehow? Note that this works for SkWebpCodec |
| 306 | // because it supports arbitrary scaling/subset combinations. |
| 307 | if (!this->dimensionsSupported(info.dimensions())) { |
| 308 | return kInvalidScale; |
| 309 | } |
| 310 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 311 | fDstInfo = info; |
Leon Scroggins III | 4288657 | 2017-01-27 13:16:28 -0500 | [diff] [blame] | 312 | fOptions = *options; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 313 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 314 | // On an incomplete decode, the subclass will specify the number of scanlines that it decoded |
| 315 | // successfully. |
| 316 | int rowsDecoded = 0; |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 317 | const Result result = this->onGetPixels(info, pixels, rowBytes, *options, ctable, ctableCount, |
| 318 | &rowsDecoded); |
| 319 | |
| 320 | if (ctableCount) { |
| 321 | if (kIncompleteInput == result || kSuccess == result || kErrorInInput == result) { |
| 322 | SkASSERT(*ctableCount >= 0 && *ctableCount <= 256); |
| 323 | } |
| 324 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 325 | |
| 326 | // A return value of kIncompleteInput indicates a truncated image stream. |
| 327 | // In this case, we will fill any uninitialized memory with a default value. |
| 328 | // Some subclasses will take care of filling any uninitialized memory on |
| 329 | // their own. They indicate that all of the memory has been filled by |
| 330 | // setting rowsDecoded equal to the height. |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 331 | if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) { |
Leon Scroggins III | 4288657 | 2017-01-27 13:16:28 -0500 | [diff] [blame] | 332 | // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless |
| 333 | // there is a subset. In that case, it will use the width of the subset. From here, the |
| 334 | // subset will only be non-null in the case of SkWebpCodec, but it treats the subset |
| 335 | // differenty from the other codecs, and it needs to use the width specified by the info. |
| 336 | // Set the subset to null so SkWebpCodec uses the correct width. |
| 337 | fOptions.fSubset = nullptr; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 338 | this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(), |
| 339 | rowsDecoded); |
| 340 | } |
| 341 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 342 | return result; |
| 343 | } |
| 344 | |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 345 | SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 346 | return this->getPixels(info, pixels, rowBytes, nullptr, nullptr, nullptr); |
| 347 | } |
| 348 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 349 | SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels, |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 350 | size_t rowBytes, const SkCodec::Options* options, SkPMColor* ctable, int* ctableCount) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 351 | fStartedIncrementalDecode = false; |
| 352 | |
| 353 | if (kUnknown_SkColorType == info.colorType()) { |
| 354 | return kInvalidConversion; |
| 355 | } |
| 356 | if (nullptr == pixels) { |
| 357 | return kInvalidParameters; |
| 358 | } |
| 359 | |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 360 | // Ensure that valid color ptrs are passed in for kIndex8 color type |
| 361 | CHECK_COLOR_TABLE; |
| 362 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 363 | // FIXME: If the rows come after the rows of a previous incremental decode, |
| 364 | // we might be able to skip the rewind, but only the implementation knows |
| 365 | // that. (e.g. PNG will always need to rewind, since we called longjmp, but |
| 366 | // a bottom-up BMP could skip rewinding if the new rows are above the old |
| 367 | // rows.) |
| 368 | if (!this->rewindIfNeeded()) { |
| 369 | return kCouldNotRewind; |
| 370 | } |
| 371 | |
| 372 | // Set options. |
| 373 | Options optsStorage; |
| 374 | if (nullptr == options) { |
| 375 | options = &optsStorage; |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 376 | } else { |
| 377 | const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, *options); |
| 378 | if (frameIndexResult != kSuccess) { |
| 379 | return frameIndexResult; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 380 | } |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 381 | if (options->fSubset) { |
| 382 | SkIRect size = SkIRect::MakeSize(info.dimensions()); |
| 383 | if (!size.contains(*options->fSubset)) { |
| 384 | return kInvalidParameters; |
| 385 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 386 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 387 | const int top = options->fSubset->top(); |
| 388 | const int bottom = options->fSubset->bottom(); |
| 389 | if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) { |
| 390 | return kInvalidParameters; |
| 391 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | if (!this->dimensionsSupported(info.dimensions())) { |
| 396 | return kInvalidScale; |
| 397 | } |
| 398 | |
| 399 | fDstInfo = info; |
| 400 | fOptions = *options; |
| 401 | |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 402 | const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, |
| 403 | fOptions, ctable, ctableCount); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 404 | if (kSuccess == result) { |
| 405 | fStartedIncrementalDecode = true; |
| 406 | } else if (kUnimplemented == result) { |
| 407 | // FIXME: This is temporarily necessary, until we transition SkCodec |
| 408 | // implementations from scanline decoding to incremental decoding. |
| 409 | // SkAndroidCodec will first attempt to use incremental decoding, but |
| 410 | // will fall back to scanline decoding if incremental returns |
| 411 | // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true |
| 412 | // (after potentially rewinding), but we do not want the next call to |
| 413 | // startScanlineDecode() to do a rewind. |
| 414 | fNeedsRewind = false; |
| 415 | } |
| 416 | return result; |
| 417 | } |
| 418 | |
| 419 | |
| 420 | SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info, |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 421 | const SkCodec::Options* options, SkPMColor ctable[], int* ctableCount) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 422 | // Reset fCurrScanline in case of failure. |
| 423 | fCurrScanline = -1; |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 424 | // Ensure that valid color ptrs are passed in for kIndex8 color type |
| 425 | CHECK_COLOR_TABLE; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 426 | |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 427 | if (!this->rewindIfNeeded()) { |
| 428 | return kCouldNotRewind; |
| 429 | } |
| 430 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 431 | // Set options. |
| 432 | Options optsStorage; |
| 433 | if (nullptr == options) { |
| 434 | options = &optsStorage; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 435 | } else if (options->fSubset) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 436 | SkIRect size = SkIRect::MakeSize(info.dimensions()); |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 437 | if (!size.contains(*options->fSubset)) { |
| 438 | return kInvalidInput; |
| 439 | } |
| 440 | |
| 441 | // We only support subsetting in the x-dimension for scanline decoder. |
| 442 | // Subsetting in the y-dimension can be accomplished using skipScanlines(). |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 443 | if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) { |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 444 | return kInvalidInput; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | // FIXME: Support subsets somehow? |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 449 | if (!this->dimensionsSupported(info.dimensions())) { |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 450 | return kInvalidScale; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 453 | const Result result = this->onStartScanlineDecode(info, *options, ctable, ctableCount); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 454 | if (result != SkCodec::kSuccess) { |
| 455 | return result; |
| 456 | } |
| 457 | |
| 458 | fCurrScanline = 0; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 459 | fDstInfo = info; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 460 | fOptions = *options; |
| 461 | return kSuccess; |
| 462 | } |
| 463 | |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 464 | #undef CHECK_COLOR_TABLE |
| 465 | |
| 466 | SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info) { |
| 467 | return this->startScanlineDecode(info, nullptr, nullptr, nullptr); |
| 468 | } |
| 469 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 470 | int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 471 | if (fCurrScanline < 0) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 472 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | SkASSERT(!fDstInfo.isEmpty()); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 476 | if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 477 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 478 | } |
| 479 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 480 | const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes); |
| 481 | if (linesDecoded < countLines) { |
| 482 | this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized, |
| 483 | countLines, linesDecoded); |
| 484 | } |
| 485 | fCurrScanline += countLines; |
| 486 | return linesDecoded; |
| 487 | } |
| 488 | |
| 489 | bool SkCodec::skipScanlines(int countLines) { |
| 490 | if (fCurrScanline < 0) { |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | SkASSERT(!fDstInfo.isEmpty()); |
| 495 | if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) { |
| 496 | // Arguably, we could just skip the scanlines which are remaining, |
| 497 | // and return true. We choose to return false so the client |
| 498 | // can catch their bug. |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | bool result = this->onSkipScanlines(countLines); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 503 | fCurrScanline += countLines; |
| 504 | return result; |
| 505 | } |
| 506 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 507 | int SkCodec::outputScanline(int inputScanline) const { |
| 508 | SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height()); |
| 509 | return this->onOutputScanline(inputScanline); |
| 510 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 511 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 512 | int SkCodec::onOutputScanline(int inputScanline) const { |
| 513 | switch (this->getScanlineOrder()) { |
| 514 | case kTopDown_SkScanlineOrder: |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 515 | return inputScanline; |
| 516 | case kBottomUp_SkScanlineOrder: |
| 517 | return this->getInfo().height() - inputScanline - 1; |
| 518 | default: |
| 519 | // This case indicates an interlaced gif and is implemented by SkGifCodec. |
| 520 | SkASSERT(false); |
| 521 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 522 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 523 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 524 | |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 525 | uint64_t SkCodec::onGetFillValue(const SkImageInfo& dstInfo) const { |
| 526 | switch (dstInfo.colorType()) { |
| 527 | case kRGBA_F16_SkColorType: { |
| 528 | static constexpr uint64_t transparentColor = 0; |
| 529 | static constexpr uint64_t opaqueColor = ((uint64_t) SK_Half1) << 48; |
| 530 | return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? opaqueColor : transparentColor; |
| 531 | } |
| 532 | default: { |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 533 | // This not only handles the kN32 case, but also k565, kGray8, kIndex8, since |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 534 | // the low bits are zeros. |
| 535 | return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? |
| 536 | SK_ColorBLACK : SK_ColorTRANSPARENT; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 541 | static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes, |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 542 | uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 543 | if (sampler) { |
| 544 | sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit); |
| 545 | } else { |
| 546 | SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes, |
| 551 | ZeroInitialized zeroInit, int linesRequested, int linesDecoded) { |
| 552 | |
| 553 | void* fillDst; |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 554 | const uint64_t fillValue = this->getFillValue(info); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 555 | const int linesRemaining = linesRequested - linesDecoded; |
| 556 | SkSampler* sampler = this->getSampler(false); |
| 557 | |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 558 | int fillWidth = info.width(); |
| 559 | if (fOptions.fSubset) { |
| 560 | fillWidth = fOptions.fSubset->width(); |
| 561 | } |
| 562 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 563 | switch (this->getScanlineOrder()) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 564 | case kTopDown_SkScanlineOrder: { |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 565 | const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 566 | fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes); |
| 567 | fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); |
| 568 | break; |
| 569 | } |
| 570 | case kBottomUp_SkScanlineOrder: { |
| 571 | fillDst = dst; |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 572 | const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 573 | fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); |
| 574 | break; |
| 575 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 576 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 577 | } |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 578 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 579 | static inline SkColorSpaceXform::ColorFormat select_xform_format_ct(SkColorType colorType) { |
| 580 | switch (colorType) { |
| 581 | case kRGBA_8888_SkColorType: |
| 582 | return SkColorSpaceXform::kRGBA_8888_ColorFormat; |
| 583 | case kBGRA_8888_SkColorType: |
| 584 | return SkColorSpaceXform::kBGRA_8888_ColorFormat; |
| 585 | case kRGB_565_SkColorType: |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 586 | case kIndex_8_SkColorType: |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 587 | #ifdef SK_PMCOLOR_IS_RGBA |
| 588 | return SkColorSpaceXform::kRGBA_8888_ColorFormat; |
| 589 | #else |
| 590 | return SkColorSpaceXform::kBGRA_8888_ColorFormat; |
| 591 | #endif |
| 592 | default: |
| 593 | SkASSERT(false); |
| 594 | return SkColorSpaceXform::kRGBA_8888_ColorFormat; |
| 595 | } |
| 596 | } |
| 597 | |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame] | 598 | bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, |
| 599 | SkTransferFunctionBehavior premulBehavior) { |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 600 | fColorXform = nullptr; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 601 | fXformOnDecode = false; |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame] | 602 | bool needsColorCorrectPremul = needs_premul(dstInfo, fEncodedInfo) && |
| 603 | SkTransferFunctionBehavior::kRespect == premulBehavior; |
| 604 | if (needs_color_xform(dstInfo, fSrcInfo, needsColorCorrectPremul)) { |
| 605 | fColorXform = SkColorSpaceXform_Base::New(fSrcInfo.colorSpace(), dstInfo.colorSpace(), |
| 606 | premulBehavior); |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 607 | if (!fColorXform) { |
| 608 | return false; |
| 609 | } |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 610 | |
| 611 | // We will apply the color xform when reading the color table unless F16 is requested. |
| 612 | fXformOnDecode = SkEncodedInfo::kPalette_Color != fEncodedInfo.color() |
| 613 | || kRGBA_F16_SkColorType == dstInfo.colorType(); |
| 614 | if (fXformOnDecode) { |
| 615 | fDstXformFormat = select_xform_format(dstInfo.colorType()); |
| 616 | } else { |
| 617 | fDstXformFormat = select_xform_format_ct(dstInfo.colorType()); |
| 618 | } |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | return true; |
| 622 | } |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 623 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 624 | void SkCodec::applyColorXform(void* dst, const void* src, int count, SkAlphaType at) const { |
| 625 | SkASSERT(fColorXform); |
| 626 | SkAssertResult(fColorXform->apply(fDstXformFormat, dst, |
| 627 | fSrcXformFormat, src, |
| 628 | count, at)); |
| 629 | } |
| 630 | |
| 631 | void SkCodec::applyColorXform(void* dst, const void* src, int count) const { |
| 632 | auto alphaType = select_xform_alpha(fDstInfo.alphaType(), fSrcInfo.alphaType()); |
| 633 | this->applyColorXform(dst, src, count, alphaType); |
| 634 | } |
| 635 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 636 | std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() { |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 637 | const int frameCount = this->getFrameCount(); |
| 638 | SkASSERT(frameCount >= 0); |
| 639 | if (frameCount <= 0) { |
| 640 | return std::vector<FrameInfo>{}; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 641 | } |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 642 | |
| 643 | if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) { |
| 644 | // Not animated. |
| 645 | return std::vector<FrameInfo>{}; |
| 646 | } |
| 647 | |
| 648 | std::vector<FrameInfo> result(frameCount); |
| 649 | for (int i = 0; i < frameCount; ++i) { |
| 650 | SkAssertResult(this->onGetFrameInfo(i, &result[i])); |
| 651 | } |
| 652 | return result; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 653 | } |