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 | |
| 56 | SkAutoTDelete<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; |
| 218 | // FIXME: fOptions should be updated to options here, since fillIncompleteImage (called below |
| 219 | // in this method) accesses it. Without updating, it uses the old value. |
| 220 | //fOptions = *options; |
| 221 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 222 | // On an incomplete decode, the subclass will specify the number of scanlines that it decoded |
| 223 | // successfully. |
| 224 | int rowsDecoded = 0; |
| 225 | const Result result = this->onGetPixels(info, pixels, rowBytes, *options, ctable, ctableCount, |
| 226 | &rowsDecoded); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 227 | |
| 228 | if ((kIncompleteInput == result || kSuccess == result) && ctableCount) { |
| 229 | SkASSERT(*ctableCount >= 0 && *ctableCount <= 256); |
| 230 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 231 | |
| 232 | // A return value of kIncompleteInput indicates a truncated image stream. |
| 233 | // In this case, we will fill any uninitialized memory with a default value. |
| 234 | // Some subclasses will take care of filling any uninitialized memory on |
| 235 | // their own. They indicate that all of the memory has been filled by |
| 236 | // setting rowsDecoded equal to the height. |
| 237 | if (kIncompleteInput == result && rowsDecoded != info.height()) { |
| 238 | this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(), |
| 239 | rowsDecoded); |
| 240 | } |
| 241 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 242 | return result; |
| 243 | } |
| 244 | |
| 245 | SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 246 | return this->getPixels(info, pixels, rowBytes, nullptr, nullptr, nullptr); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 247 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 248 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 249 | SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels, |
| 250 | size_t rowBytes, const SkCodec::Options* options, SkPMColor* ctable, int* ctableCount) { |
| 251 | fStartedIncrementalDecode = false; |
| 252 | |
| 253 | if (kUnknown_SkColorType == info.colorType()) { |
| 254 | return kInvalidConversion; |
| 255 | } |
| 256 | if (nullptr == pixels) { |
| 257 | return kInvalidParameters; |
| 258 | } |
| 259 | |
| 260 | // Ensure that valid color ptrs are passed in for kIndex8 color type |
| 261 | CHECK_COLOR_TABLE; |
| 262 | |
| 263 | // FIXME: If the rows come after the rows of a previous incremental decode, |
| 264 | // we might be able to skip the rewind, but only the implementation knows |
| 265 | // that. (e.g. PNG will always need to rewind, since we called longjmp, but |
| 266 | // a bottom-up BMP could skip rewinding if the new rows are above the old |
| 267 | // rows.) |
| 268 | if (!this->rewindIfNeeded()) { |
| 269 | return kCouldNotRewind; |
| 270 | } |
| 271 | |
| 272 | // Set options. |
| 273 | Options optsStorage; |
| 274 | if (nullptr == options) { |
| 275 | options = &optsStorage; |
| 276 | } else if (options->fSubset) { |
| 277 | SkIRect size = SkIRect::MakeSize(info.dimensions()); |
| 278 | if (!size.contains(*options->fSubset)) { |
| 279 | return kInvalidParameters; |
| 280 | } |
| 281 | |
| 282 | const int top = options->fSubset->top(); |
| 283 | const int bottom = options->fSubset->bottom(); |
| 284 | if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) { |
| 285 | return kInvalidParameters; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (!this->dimensionsSupported(info.dimensions())) { |
| 290 | return kInvalidScale; |
| 291 | } |
| 292 | |
| 293 | fDstInfo = info; |
| 294 | fOptions = *options; |
| 295 | |
| 296 | const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, |
| 297 | fOptions, ctable, ctableCount); |
| 298 | if (kSuccess == result) { |
| 299 | fStartedIncrementalDecode = true; |
| 300 | } else if (kUnimplemented == result) { |
| 301 | // FIXME: This is temporarily necessary, until we transition SkCodec |
| 302 | // implementations from scanline decoding to incremental decoding. |
| 303 | // SkAndroidCodec will first attempt to use incremental decoding, but |
| 304 | // will fall back to scanline decoding if incremental returns |
| 305 | // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true |
| 306 | // (after potentially rewinding), but we do not want the next call to |
| 307 | // startScanlineDecode() to do a rewind. |
| 308 | fNeedsRewind = false; |
| 309 | } |
| 310 | return result; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 315 | const SkCodec::Options* options, SkPMColor ctable[], int* ctableCount) { |
| 316 | // Reset fCurrScanline in case of failure. |
| 317 | fCurrScanline = -1; |
| 318 | // Ensure that valid color ptrs are passed in for kIndex8 color type |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 319 | CHECK_COLOR_TABLE; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 320 | |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 321 | if (!this->rewindIfNeeded()) { |
| 322 | return kCouldNotRewind; |
| 323 | } |
| 324 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 325 | // Set options. |
| 326 | Options optsStorage; |
| 327 | if (nullptr == options) { |
| 328 | options = &optsStorage; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 329 | } else if (options->fSubset) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 330 | SkIRect size = SkIRect::MakeSize(info.dimensions()); |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 331 | if (!size.contains(*options->fSubset)) { |
| 332 | return kInvalidInput; |
| 333 | } |
| 334 | |
| 335 | // We only support subsetting in the x-dimension for scanline decoder. |
| 336 | // Subsetting in the y-dimension can be accomplished using skipScanlines(). |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 337 | if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) { |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 338 | return kInvalidInput; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | // FIXME: Support subsets somehow? |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 343 | if (!this->dimensionsSupported(info.dimensions())) { |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 344 | return kInvalidScale; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 345 | } |
| 346 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 347 | const Result result = this->onStartScanlineDecode(info, *options, ctable, ctableCount); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 348 | if (result != SkCodec::kSuccess) { |
| 349 | return result; |
| 350 | } |
| 351 | |
| 352 | fCurrScanline = 0; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 353 | fDstInfo = info; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 354 | fOptions = *options; |
| 355 | return kSuccess; |
| 356 | } |
| 357 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 358 | #undef CHECK_COLOR_TABLE |
| 359 | |
| 360 | SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info) { |
| 361 | return this->startScanlineDecode(info, nullptr, nullptr, nullptr); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 362 | } |
| 363 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 364 | int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 365 | if (fCurrScanline < 0) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 366 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | SkASSERT(!fDstInfo.isEmpty()); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 370 | if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 371 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 372 | } |
| 373 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 374 | const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes); |
| 375 | if (linesDecoded < countLines) { |
| 376 | this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized, |
| 377 | countLines, linesDecoded); |
| 378 | } |
| 379 | fCurrScanline += countLines; |
| 380 | return linesDecoded; |
| 381 | } |
| 382 | |
| 383 | bool SkCodec::skipScanlines(int countLines) { |
| 384 | if (fCurrScanline < 0) { |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | SkASSERT(!fDstInfo.isEmpty()); |
| 389 | if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) { |
| 390 | // Arguably, we could just skip the scanlines which are remaining, |
| 391 | // and return true. We choose to return false so the client |
| 392 | // can catch their bug. |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | bool result = this->onSkipScanlines(countLines); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 397 | fCurrScanline += countLines; |
| 398 | return result; |
| 399 | } |
| 400 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 401 | int SkCodec::outputScanline(int inputScanline) const { |
| 402 | SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height()); |
| 403 | return this->onOutputScanline(inputScanline); |
| 404 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 405 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 406 | int SkCodec::onOutputScanline(int inputScanline) const { |
| 407 | switch (this->getScanlineOrder()) { |
| 408 | case kTopDown_SkScanlineOrder: |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 409 | return inputScanline; |
| 410 | case kBottomUp_SkScanlineOrder: |
| 411 | return this->getInfo().height() - inputScanline - 1; |
| 412 | default: |
| 413 | // This case indicates an interlaced gif and is implemented by SkGifCodec. |
| 414 | SkASSERT(false); |
| 415 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 416 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 417 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 418 | |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 419 | uint64_t SkCodec::onGetFillValue(const SkImageInfo& dstInfo) const { |
| 420 | switch (dstInfo.colorType()) { |
| 421 | case kRGBA_F16_SkColorType: { |
| 422 | static constexpr uint64_t transparentColor = 0; |
| 423 | static constexpr uint64_t opaqueColor = ((uint64_t) SK_Half1) << 48; |
| 424 | return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? opaqueColor : transparentColor; |
| 425 | } |
| 426 | default: { |
| 427 | // This not only handles the kN32 case, but also k565, kGray8, kIndex8, since |
| 428 | // the low bits are zeros. |
| 429 | return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? |
| 430 | SK_ColorBLACK : SK_ColorTRANSPARENT; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 435 | static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes, |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 436 | uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 437 | if (sampler) { |
| 438 | sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit); |
| 439 | } else { |
| 440 | SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes, |
| 445 | ZeroInitialized zeroInit, int linesRequested, int linesDecoded) { |
| 446 | |
| 447 | void* fillDst; |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 448 | const uint64_t fillValue = this->getFillValue(info); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 449 | const int linesRemaining = linesRequested - linesDecoded; |
| 450 | SkSampler* sampler = this->getSampler(false); |
| 451 | |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 452 | int fillWidth = info.width(); |
| 453 | if (fOptions.fSubset) { |
| 454 | fillWidth = fOptions.fSubset->width(); |
| 455 | } |
| 456 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 457 | switch (this->getScanlineOrder()) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 458 | case kTopDown_SkScanlineOrder: { |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 459 | const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 460 | fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes); |
| 461 | fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); |
| 462 | break; |
| 463 | } |
| 464 | case kBottomUp_SkScanlineOrder: { |
| 465 | fillDst = dst; |
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 | fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); |
| 468 | break; |
| 469 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 470 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 471 | } |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 472 | |
| 473 | bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo) { |
| 474 | fColorXform = nullptr; |
| 475 | if (needs_color_xform(dstInfo, fSrcInfo)) { |
| 476 | fColorXform = SkColorSpaceXform::New(fSrcInfo.colorSpace(), dstInfo.colorSpace()); |
| 477 | if (!fColorXform) { |
| 478 | return false; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return true; |
| 483 | } |