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 | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 12 | #include "SkColorSpaceXform.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 | |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 117 | SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream, |
| 118 | sk_sp<SkColorSpace> colorSpace, Origin origin) |
| 119 | : fEncodedInfo(info) |
msarett | 530c844 | 2016-07-21 11:57:49 -0700 | [diff] [blame] | 120 | , fSrcInfo(info.makeImageInfo(width, height, std::move(colorSpace))) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 121 | , fStream(stream) |
| 122 | , fNeedsRewind(false) |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 123 | , fOrigin(origin) |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 124 | , fDstInfo() |
| 125 | , fOptions() |
| 126 | , fCurrScanline(-1) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 127 | {} |
| 128 | |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 129 | SkCodec::SkCodec(const SkEncodedInfo& info, const SkImageInfo& imageInfo, SkStream* stream, |
| 130 | Origin origin) |
| 131 | : fEncodedInfo(info) |
| 132 | , fSrcInfo(imageInfo) |
| 133 | , fStream(stream) |
| 134 | , fNeedsRewind(false) |
| 135 | , fOrigin(origin) |
| 136 | , fDstInfo() |
| 137 | , fOptions() |
| 138 | , fCurrScanline(-1) |
| 139 | {} |
| 140 | |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 141 | SkCodec::~SkCodec() {} |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 142 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 143 | bool SkCodec::rewindIfNeeded() { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 144 | // Store the value of fNeedsRewind so we can update it. Next read will |
| 145 | // require a rewind. |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 146 | const bool needsRewind = fNeedsRewind; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 147 | fNeedsRewind = true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 148 | if (!needsRewind) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 149 | return true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 150 | } |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 151 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 152 | // startScanlineDecode will need to be called before decoding scanlines. |
| 153 | fCurrScanline = -1; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 154 | // startIncrementalDecode will need to be called before incrementalDecode. |
| 155 | fStartedIncrementalDecode = false; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 156 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 157 | // Some codecs do not have a stream. They may hold onto their own data or another codec. |
| 158 | // They must handle rewinding themselves. |
| 159 | if (fStream && !fStream->rewind()) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | |
| 163 | return this->onRewind(); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 164 | } |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 165 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 166 | #define CHECK_COLOR_TABLE \ |
| 167 | if (kIndex_8_SkColorType == info.colorType()) { \ |
| 168 | if (nullptr == ctable || nullptr == ctableCount) { \ |
| 169 | return SkCodec::kInvalidParameters; \ |
| 170 | } \ |
| 171 | } else { \ |
| 172 | if (ctableCount) { \ |
| 173 | *ctableCount = 0; \ |
| 174 | } \ |
| 175 | ctableCount = nullptr; \ |
| 176 | ctable = nullptr; \ |
| 177 | } |
| 178 | |
| 179 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 180 | SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 181 | const Options* options, SkPMColor ctable[], int* ctableCount) { |
| 182 | if (kUnknown_SkColorType == info.colorType()) { |
| 183 | return kInvalidConversion; |
| 184 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 185 | if (nullptr == pixels) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 186 | return kInvalidParameters; |
| 187 | } |
| 188 | if (rowBytes < info.minRowBytes()) { |
| 189 | return kInvalidParameters; |
| 190 | } |
| 191 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 192 | CHECK_COLOR_TABLE; |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 193 | |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 194 | if (!this->rewindIfNeeded()) { |
| 195 | return kCouldNotRewind; |
| 196 | } |
| 197 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 198 | // Default options. |
| 199 | Options optsStorage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 200 | if (nullptr == options) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 201 | options = &optsStorage; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 202 | } else if (options->fSubset) { |
| 203 | SkIRect subset(*options->fSubset); |
| 204 | if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) { |
| 205 | // FIXME: How to differentiate between not supporting subset at all |
| 206 | // and not supporting this particular subset? |
| 207 | return kUnimplemented; |
| 208 | } |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 209 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 210 | |
| 211 | // FIXME: Support subsets somehow? Note that this works for SkWebpCodec |
| 212 | // because it supports arbitrary scaling/subset combinations. |
| 213 | if (!this->dimensionsSupported(info.dimensions())) { |
| 214 | return kInvalidScale; |
| 215 | } |
| 216 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 217 | fDstInfo = info; |
Leon Scroggins III | 4288657 | 2017-01-27 13:16:28 -0500 | [diff] [blame] | 218 | fOptions = *options; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 219 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 220 | // On an incomplete decode, the subclass will specify the number of scanlines that it decoded |
| 221 | // successfully. |
| 222 | int rowsDecoded = 0; |
| 223 | const Result result = this->onGetPixels(info, pixels, rowBytes, *options, ctable, ctableCount, |
| 224 | &rowsDecoded); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 225 | |
| 226 | if ((kIncompleteInput == result || kSuccess == result) && ctableCount) { |
| 227 | SkASSERT(*ctableCount >= 0 && *ctableCount <= 256); |
| 228 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 229 | |
| 230 | // A return value of kIncompleteInput indicates a truncated image stream. |
| 231 | // In this case, we will fill any uninitialized memory with a default value. |
| 232 | // Some subclasses will take care of filling any uninitialized memory on |
| 233 | // their own. They indicate that all of the memory has been filled by |
| 234 | // setting rowsDecoded equal to the height. |
| 235 | if (kIncompleteInput == result && rowsDecoded != info.height()) { |
Leon Scroggins III | 4288657 | 2017-01-27 13:16:28 -0500 | [diff] [blame] | 236 | // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless |
| 237 | // there is a subset. In that case, it will use the width of the subset. From here, the |
| 238 | // subset will only be non-null in the case of SkWebpCodec, but it treats the subset |
| 239 | // differenty from the other codecs, and it needs to use the width specified by the info. |
| 240 | // Set the subset to null so SkWebpCodec uses the correct width. |
| 241 | fOptions.fSubset = nullptr; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 242 | this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(), |
| 243 | rowsDecoded); |
| 244 | } |
| 245 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 246 | return result; |
| 247 | } |
| 248 | |
| 249 | SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 250 | return this->getPixels(info, pixels, rowBytes, nullptr, nullptr, nullptr); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 251 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 252 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 253 | SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels, |
| 254 | size_t rowBytes, const SkCodec::Options* options, SkPMColor* ctable, int* ctableCount) { |
| 255 | fStartedIncrementalDecode = false; |
| 256 | |
| 257 | if (kUnknown_SkColorType == info.colorType()) { |
| 258 | return kInvalidConversion; |
| 259 | } |
| 260 | if (nullptr == pixels) { |
| 261 | return kInvalidParameters; |
| 262 | } |
| 263 | |
| 264 | // Ensure that valid color ptrs are passed in for kIndex8 color type |
| 265 | CHECK_COLOR_TABLE; |
| 266 | |
| 267 | // FIXME: If the rows come after the rows of a previous incremental decode, |
| 268 | // we might be able to skip the rewind, but only the implementation knows |
| 269 | // that. (e.g. PNG will always need to rewind, since we called longjmp, but |
| 270 | // a bottom-up BMP could skip rewinding if the new rows are above the old |
| 271 | // rows.) |
| 272 | if (!this->rewindIfNeeded()) { |
| 273 | return kCouldNotRewind; |
| 274 | } |
| 275 | |
| 276 | // Set options. |
| 277 | Options optsStorage; |
| 278 | if (nullptr == options) { |
| 279 | options = &optsStorage; |
| 280 | } else if (options->fSubset) { |
| 281 | SkIRect size = SkIRect::MakeSize(info.dimensions()); |
| 282 | if (!size.contains(*options->fSubset)) { |
| 283 | return kInvalidParameters; |
| 284 | } |
| 285 | |
| 286 | const int top = options->fSubset->top(); |
| 287 | const int bottom = options->fSubset->bottom(); |
| 288 | if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) { |
| 289 | return kInvalidParameters; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (!this->dimensionsSupported(info.dimensions())) { |
| 294 | return kInvalidScale; |
| 295 | } |
| 296 | |
| 297 | fDstInfo = info; |
| 298 | fOptions = *options; |
| 299 | |
| 300 | const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, |
| 301 | fOptions, ctable, ctableCount); |
| 302 | if (kSuccess == result) { |
| 303 | fStartedIncrementalDecode = true; |
| 304 | } else if (kUnimplemented == result) { |
| 305 | // FIXME: This is temporarily necessary, until we transition SkCodec |
| 306 | // implementations from scanline decoding to incremental decoding. |
| 307 | // SkAndroidCodec will first attempt to use incremental decoding, but |
| 308 | // will fall back to scanline decoding if incremental returns |
| 309 | // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true |
| 310 | // (after potentially rewinding), but we do not want the next call to |
| 311 | // startScanlineDecode() to do a rewind. |
| 312 | fNeedsRewind = false; |
| 313 | } |
| 314 | return result; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 319 | const SkCodec::Options* options, SkPMColor ctable[], int* ctableCount) { |
| 320 | // Reset fCurrScanline in case of failure. |
| 321 | fCurrScanline = -1; |
| 322 | // Ensure that valid color ptrs are passed in for kIndex8 color type |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 323 | CHECK_COLOR_TABLE; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 324 | |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 325 | if (!this->rewindIfNeeded()) { |
| 326 | return kCouldNotRewind; |
| 327 | } |
| 328 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 329 | // Set options. |
| 330 | Options optsStorage; |
| 331 | if (nullptr == options) { |
| 332 | options = &optsStorage; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 333 | } else if (options->fSubset) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 334 | SkIRect size = SkIRect::MakeSize(info.dimensions()); |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 335 | if (!size.contains(*options->fSubset)) { |
| 336 | return kInvalidInput; |
| 337 | } |
| 338 | |
| 339 | // We only support subsetting in the x-dimension for scanline decoder. |
| 340 | // Subsetting in the y-dimension can be accomplished using skipScanlines(). |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 341 | if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) { |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 342 | return kInvalidInput; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | // FIXME: Support subsets somehow? |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 347 | if (!this->dimensionsSupported(info.dimensions())) { |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 348 | return kInvalidScale; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 349 | } |
| 350 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 351 | const Result result = this->onStartScanlineDecode(info, *options, ctable, ctableCount); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 352 | if (result != SkCodec::kSuccess) { |
| 353 | return result; |
| 354 | } |
| 355 | |
| 356 | fCurrScanline = 0; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 357 | fDstInfo = info; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 358 | fOptions = *options; |
| 359 | return kSuccess; |
| 360 | } |
| 361 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 362 | #undef CHECK_COLOR_TABLE |
| 363 | |
| 364 | SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info) { |
| 365 | return this->startScanlineDecode(info, nullptr, nullptr, nullptr); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 366 | } |
| 367 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 368 | int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 369 | if (fCurrScanline < 0) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 370 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | SkASSERT(!fDstInfo.isEmpty()); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 374 | if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 375 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 376 | } |
| 377 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 378 | const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes); |
| 379 | if (linesDecoded < countLines) { |
| 380 | this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized, |
| 381 | countLines, linesDecoded); |
| 382 | } |
| 383 | fCurrScanline += countLines; |
| 384 | return linesDecoded; |
| 385 | } |
| 386 | |
| 387 | bool SkCodec::skipScanlines(int countLines) { |
| 388 | if (fCurrScanline < 0) { |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | SkASSERT(!fDstInfo.isEmpty()); |
| 393 | if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) { |
| 394 | // Arguably, we could just skip the scanlines which are remaining, |
| 395 | // and return true. We choose to return false so the client |
| 396 | // can catch their bug. |
| 397 | return false; |
| 398 | } |
| 399 | |
| 400 | bool result = this->onSkipScanlines(countLines); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 401 | fCurrScanline += countLines; |
| 402 | return result; |
| 403 | } |
| 404 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 405 | int SkCodec::outputScanline(int inputScanline) const { |
| 406 | SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height()); |
| 407 | return this->onOutputScanline(inputScanline); |
| 408 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 409 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 410 | int SkCodec::onOutputScanline(int inputScanline) const { |
| 411 | switch (this->getScanlineOrder()) { |
| 412 | case kTopDown_SkScanlineOrder: |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 413 | return inputScanline; |
| 414 | case kBottomUp_SkScanlineOrder: |
| 415 | return this->getInfo().height() - inputScanline - 1; |
| 416 | default: |
| 417 | // This case indicates an interlaced gif and is implemented by SkGifCodec. |
| 418 | SkASSERT(false); |
| 419 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 420 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 421 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 422 | |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 423 | uint64_t SkCodec::onGetFillValue(const SkImageInfo& dstInfo) const { |
| 424 | switch (dstInfo.colorType()) { |
| 425 | case kRGBA_F16_SkColorType: { |
| 426 | static constexpr uint64_t transparentColor = 0; |
| 427 | static constexpr uint64_t opaqueColor = ((uint64_t) SK_Half1) << 48; |
| 428 | return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? opaqueColor : transparentColor; |
| 429 | } |
| 430 | default: { |
| 431 | // This not only handles the kN32 case, but also k565, kGray8, kIndex8, since |
| 432 | // the low bits are zeros. |
| 433 | return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? |
| 434 | SK_ColorBLACK : SK_ColorTRANSPARENT; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 439 | static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes, |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 440 | uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 441 | if (sampler) { |
| 442 | sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit); |
| 443 | } else { |
| 444 | SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes, |
| 449 | ZeroInitialized zeroInit, int linesRequested, int linesDecoded) { |
| 450 | |
| 451 | void* fillDst; |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 452 | const uint64_t fillValue = this->getFillValue(info); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 453 | const int linesRemaining = linesRequested - linesDecoded; |
| 454 | SkSampler* sampler = this->getSampler(false); |
| 455 | |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 456 | int fillWidth = info.width(); |
| 457 | if (fOptions.fSubset) { |
| 458 | fillWidth = fOptions.fSubset->width(); |
| 459 | } |
| 460 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 461 | switch (this->getScanlineOrder()) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 462 | case kTopDown_SkScanlineOrder: { |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 463 | const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 464 | fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes); |
| 465 | fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); |
| 466 | break; |
| 467 | } |
| 468 | case kBottomUp_SkScanlineOrder: { |
| 469 | fillDst = dst; |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 470 | const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 471 | fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); |
| 472 | break; |
| 473 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 474 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 475 | } |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 476 | |
| 477 | bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo) { |
| 478 | fColorXform = nullptr; |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 479 | bool needsPremul = needs_premul(dstInfo, fEncodedInfo); |
| 480 | if (needs_color_xform(dstInfo, fSrcInfo, needsPremul)) { |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 481 | fColorXform = SkColorSpaceXform::New(fSrcInfo.colorSpace(), dstInfo.colorSpace()); |
| 482 | if (!fColorXform) { |
| 483 | return false; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | return true; |
| 488 | } |