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