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