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 | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 11 | #include "SkData.h" |
| 12 | #include "SkGifCodec.h" |
| 13 | #include "SkIcoCodec.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 14 | #include "SkJpegCodec.h" |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 15 | #ifdef SK_CODEC_DECODES_PNG |
msarett | be1d555 | 2016-01-21 09:05:23 -0800 | [diff] [blame] | 16 | #include "SkPngCodec.h" |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 17 | #endif |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 18 | #include "SkRawCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 19 | #include "SkStream.h" |
msarett | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 20 | #include "SkWbmpCodec.h" |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 21 | #include "SkWebpCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 22 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 23 | struct DecoderProc { |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 24 | bool (*IsFormat)(const void*, size_t); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 25 | SkCodec* (*NewFromStream)(SkStream*); |
| 26 | }; |
| 27 | |
| 28 | static const DecoderProc gDecoderProcs[] = { |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 29 | #ifdef SK_CODEC_DECODES_JPEG |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 30 | { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream }, |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 31 | #endif |
| 32 | #ifdef SK_CODEC_DECODES_WEBP |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 33 | { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream }, |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 34 | #endif |
| 35 | #ifdef SK_CODEC_DECODES_GIF |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 36 | { SkGifCodec::IsGif, SkGifCodec::NewFromStream }, |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 37 | #endif |
| 38 | #ifdef SK_CODEC_DECODES_PNG |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 39 | { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 40 | #endif |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 41 | { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }, |
| 42 | { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 45 | size_t SkCodec::MinBufferedBytesNeeded() { |
| 46 | return WEBP_VP8_HEADER_SIZE; |
| 47 | } |
| 48 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 49 | SkCodec* SkCodec::NewFromStream(SkStream* stream, |
| 50 | SkPngChunkReader* chunkReader) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 51 | if (!stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 52 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 53 | } |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 54 | |
| 55 | SkAutoTDelete<SkStream> streamDeleter(stream); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 56 | |
| 57 | // 14 is enough to read all of the supported types. |
| 58 | const size_t bytesToRead = 14; |
| 59 | SkASSERT(bytesToRead <= MinBufferedBytesNeeded()); |
| 60 | |
| 61 | char buffer[bytesToRead]; |
| 62 | size_t bytesRead = stream->peek(buffer, bytesToRead); |
| 63 | |
| 64 | // It is also possible to have a complete image less than bytesToRead bytes |
| 65 | // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead. |
| 66 | // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter |
| 67 | // than bytesToRead, so pass that directly to the decoder. |
| 68 | // It also is possible the stream uses too small a buffer for peeking, but |
| 69 | // we trust the caller to use a large enough buffer. |
| 70 | |
| 71 | if (0 == bytesRead) { |
scroggo | 3ab9f2e | 2016-01-06 09:53:34 -0800 | [diff] [blame] | 72 | // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this |
| 73 | // printf could be useful to notice failures. |
| 74 | // SkCodecPrintf("Encoded image data failed to peek!\n"); |
| 75 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 76 | // It is possible the stream does not support peeking, but does support |
| 77 | // rewinding. |
| 78 | // Attempt to read() and pass the actual amount read to the decoder. |
| 79 | bytesRead = stream->read(buffer, bytesToRead); |
| 80 | if (!stream->rewind()) { |
scroggo | 3ab9f2e | 2016-01-06 09:53:34 -0800 | [diff] [blame] | 81 | SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n"); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 82 | return nullptr; |
| 83 | } |
| 84 | } |
| 85 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 86 | // PNG is special, since we want to be able to supply an SkPngChunkReader. |
| 87 | // But this code follows the same pattern as the loop. |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 88 | #ifdef SK_CODEC_DECODES_PNG |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 89 | if (SkPngCodec::IsPng(buffer, bytesRead)) { |
msarett | f44631b | 2016-01-13 10:54:20 -0800 | [diff] [blame] | 90 | return SkPngCodec::NewFromStream(streamDeleter.detach(), chunkReader); |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 91 | } else |
| 92 | #endif |
| 93 | { |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 94 | for (DecoderProc proc : gDecoderProcs) { |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 95 | if (proc.IsFormat(buffer, bytesRead)) { |
msarett | f44631b | 2016-01-13 10:54:20 -0800 | [diff] [blame] | 96 | return proc.NewFromStream(streamDeleter.detach()); |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 97 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 98 | } |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 99 | |
| 100 | #ifdef SK_CODEC_DECODES_RAW |
| 101 | // Try to treat the input as RAW if all the other checks failed. |
| 102 | return SkRawCodec::NewFromStream(streamDeleter.detach()); |
| 103 | #endif |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 104 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 105 | |
msarett | f44631b | 2016-01-13 10:54:20 -0800 | [diff] [blame] | 106 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 107 | } |
| 108 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 109 | SkCodec* SkCodec::NewFromData(SkData* data, SkPngChunkReader* reader) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 110 | if (!data) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 111 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 112 | } |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 113 | return NewFromStream(new SkMemoryStream(data), reader); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream) |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 117 | : fSrcInfo(info) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 118 | , fStream(stream) |
| 119 | , fNeedsRewind(false) |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 120 | , fDstInfo() |
| 121 | , fOptions() |
| 122 | , fCurrScanline(-1) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 123 | {} |
| 124 | |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 125 | SkCodec::~SkCodec() {} |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 126 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 127 | bool SkCodec::rewindIfNeeded() { |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 128 | if (!fStream) { |
| 129 | // Some codecs do not have a stream, but they hold others that do. They |
| 130 | // must handle rewinding themselves. |
| 131 | return true; |
| 132 | } |
| 133 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 134 | // Store the value of fNeedsRewind so we can update it. Next read will |
| 135 | // require a rewind. |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 136 | const bool needsRewind = fNeedsRewind; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 137 | fNeedsRewind = true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 138 | if (!needsRewind) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 139 | return true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 140 | } |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 141 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 142 | // startScanlineDecode will need to be called before decoding scanlines. |
| 143 | fCurrScanline = -1; |
| 144 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 145 | if (!fStream->rewind()) { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | return this->onRewind(); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 150 | } |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 151 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 152 | SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 153 | const Options* options, SkPMColor ctable[], int* ctableCount) { |
| 154 | if (kUnknown_SkColorType == info.colorType()) { |
| 155 | return kInvalidConversion; |
| 156 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 157 | if (nullptr == pixels) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 158 | return kInvalidParameters; |
| 159 | } |
| 160 | if (rowBytes < info.minRowBytes()) { |
| 161 | return kInvalidParameters; |
| 162 | } |
| 163 | |
| 164 | if (kIndex_8_SkColorType == info.colorType()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 165 | if (nullptr == ctable || nullptr == ctableCount) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 166 | return kInvalidParameters; |
| 167 | } |
| 168 | } else { |
| 169 | if (ctableCount) { |
| 170 | *ctableCount = 0; |
| 171 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 172 | ctableCount = nullptr; |
| 173 | ctable = nullptr; |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 174 | } |
| 175 | |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 176 | if (!this->rewindIfNeeded()) { |
| 177 | return kCouldNotRewind; |
| 178 | } |
| 179 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 180 | // Default options. |
| 181 | Options optsStorage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 182 | if (nullptr == options) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 183 | options = &optsStorage; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 184 | } else if (options->fSubset) { |
| 185 | SkIRect subset(*options->fSubset); |
| 186 | if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) { |
| 187 | // FIXME: How to differentiate between not supporting subset at all |
| 188 | // and not supporting this particular subset? |
| 189 | return kUnimplemented; |
| 190 | } |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 191 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 192 | |
| 193 | // FIXME: Support subsets somehow? Note that this works for SkWebpCodec |
| 194 | // because it supports arbitrary scaling/subset combinations. |
| 195 | if (!this->dimensionsSupported(info.dimensions())) { |
| 196 | return kInvalidScale; |
| 197 | } |
| 198 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 199 | // On an incomplete decode, the subclass will specify the number of scanlines that it decoded |
| 200 | // successfully. |
| 201 | int rowsDecoded = 0; |
| 202 | const Result result = this->onGetPixels(info, pixels, rowBytes, *options, ctable, ctableCount, |
| 203 | &rowsDecoded); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 204 | |
| 205 | if ((kIncompleteInput == result || kSuccess == result) && ctableCount) { |
| 206 | SkASSERT(*ctableCount >= 0 && *ctableCount <= 256); |
| 207 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 208 | |
| 209 | // A return value of kIncompleteInput indicates a truncated image stream. |
| 210 | // In this case, we will fill any uninitialized memory with a default value. |
| 211 | // Some subclasses will take care of filling any uninitialized memory on |
| 212 | // their own. They indicate that all of the memory has been filled by |
| 213 | // setting rowsDecoded equal to the height. |
| 214 | if (kIncompleteInput == result && rowsDecoded != info.height()) { |
| 215 | this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(), |
| 216 | rowsDecoded); |
| 217 | } |
| 218 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 219 | return result; |
| 220 | } |
| 221 | |
| 222 | SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 223 | return this->getPixels(info, pixels, rowBytes, nullptr, nullptr, nullptr); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 224 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 225 | |
| 226 | SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& dstInfo, |
| 227 | const SkCodec::Options* options, SkPMColor ctable[], int* ctableCount) { |
| 228 | // Reset fCurrScanline in case of failure. |
| 229 | fCurrScanline = -1; |
| 230 | // Ensure that valid color ptrs are passed in for kIndex8 color type |
| 231 | if (kIndex_8_SkColorType == dstInfo.colorType()) { |
| 232 | if (nullptr == ctable || nullptr == ctableCount) { |
| 233 | return SkCodec::kInvalidParameters; |
| 234 | } |
| 235 | } else { |
| 236 | if (ctableCount) { |
| 237 | *ctableCount = 0; |
| 238 | } |
| 239 | ctableCount = nullptr; |
| 240 | ctable = nullptr; |
| 241 | } |
| 242 | |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 243 | if (!this->rewindIfNeeded()) { |
| 244 | return kCouldNotRewind; |
| 245 | } |
| 246 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 247 | // Set options. |
| 248 | Options optsStorage; |
| 249 | if (nullptr == options) { |
| 250 | options = &optsStorage; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 251 | } else if (options->fSubset) { |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 252 | SkIRect size = SkIRect::MakeSize(dstInfo.dimensions()); |
| 253 | if (!size.contains(*options->fSubset)) { |
| 254 | return kInvalidInput; |
| 255 | } |
| 256 | |
| 257 | // We only support subsetting in the x-dimension for scanline decoder. |
| 258 | // Subsetting in the y-dimension can be accomplished using skipScanlines(). |
| 259 | if (options->fSubset->top() != 0 || options->fSubset->height() != dstInfo.height()) { |
| 260 | return kInvalidInput; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | // FIXME: Support subsets somehow? |
| 265 | if (!this->dimensionsSupported(dstInfo.dimensions())) { |
| 266 | return kInvalidScale; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | const Result result = this->onStartScanlineDecode(dstInfo, *options, ctable, ctableCount); |
| 270 | if (result != SkCodec::kSuccess) { |
| 271 | return result; |
| 272 | } |
| 273 | |
| 274 | fCurrScanline = 0; |
| 275 | fDstInfo = dstInfo; |
| 276 | fOptions = *options; |
| 277 | return kSuccess; |
| 278 | } |
| 279 | |
| 280 | SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& dstInfo) { |
| 281 | return this->startScanlineDecode(dstInfo, nullptr, nullptr, nullptr); |
| 282 | } |
| 283 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 284 | int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 285 | if (fCurrScanline < 0) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 286 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | SkASSERT(!fDstInfo.isEmpty()); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 290 | if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 291 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 292 | } |
| 293 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 294 | const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes); |
| 295 | if (linesDecoded < countLines) { |
| 296 | this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized, |
| 297 | countLines, linesDecoded); |
| 298 | } |
| 299 | fCurrScanline += countLines; |
| 300 | return linesDecoded; |
| 301 | } |
| 302 | |
| 303 | bool SkCodec::skipScanlines(int countLines) { |
| 304 | if (fCurrScanline < 0) { |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | SkASSERT(!fDstInfo.isEmpty()); |
| 309 | if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) { |
| 310 | // Arguably, we could just skip the scanlines which are remaining, |
| 311 | // and return true. We choose to return false so the client |
| 312 | // can catch their bug. |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | bool result = this->onSkipScanlines(countLines); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 317 | fCurrScanline += countLines; |
| 318 | return result; |
| 319 | } |
| 320 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 321 | int SkCodec::outputScanline(int inputScanline) const { |
| 322 | SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height()); |
| 323 | return this->onOutputScanline(inputScanline); |
| 324 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 325 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 326 | int SkCodec::onOutputScanline(int inputScanline) const { |
| 327 | switch (this->getScanlineOrder()) { |
| 328 | case kTopDown_SkScanlineOrder: |
| 329 | case kNone_SkScanlineOrder: |
| 330 | return inputScanline; |
| 331 | case kBottomUp_SkScanlineOrder: |
| 332 | return this->getInfo().height() - inputScanline - 1; |
| 333 | default: |
| 334 | // This case indicates an interlaced gif and is implemented by SkGifCodec. |
| 335 | SkASSERT(false); |
| 336 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 337 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 338 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 339 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 340 | static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes, |
| 341 | uint32_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) { |
| 342 | if (sampler) { |
| 343 | sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit); |
| 344 | } else { |
| 345 | SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes, |
| 350 | ZeroInitialized zeroInit, int linesRequested, int linesDecoded) { |
| 351 | |
| 352 | void* fillDst; |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 353 | const uint32_t fillValue = this->getFillValue(info.colorType()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 354 | const int linesRemaining = linesRequested - linesDecoded; |
| 355 | SkSampler* sampler = this->getSampler(false); |
| 356 | |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 357 | int fillWidth = info.width(); |
| 358 | if (fOptions.fSubset) { |
| 359 | fillWidth = fOptions.fSubset->width(); |
| 360 | } |
| 361 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 362 | switch (this->getScanlineOrder()) { |
| 363 | case kTopDown_SkScanlineOrder: |
| 364 | case kNone_SkScanlineOrder: { |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 365 | const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 366 | fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes); |
| 367 | fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); |
| 368 | break; |
| 369 | } |
| 370 | case kBottomUp_SkScanlineOrder: { |
| 371 | fillDst = dst; |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 372 | const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 373 | fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); |
| 374 | break; |
| 375 | } |
| 376 | case kOutOfOrder_SkScanlineOrder: { |
| 377 | SkASSERT(1 == linesRequested || this->getInfo().height() == linesRequested); |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 378 | const SkImageInfo fillInfo = info.makeWH(fillWidth, 1); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 379 | for (int srcY = linesDecoded; srcY < linesRequested; srcY++) { |
| 380 | fillDst = SkTAddOffset<void>(dst, this->outputScanline(srcY) * rowBytes); |
| 381 | fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); |
| 382 | } |
| 383 | break; |
| 384 | } |
| 385 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 386 | } |