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