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