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