blob: e9eeb4529a9d7fa6a43e52dad90daa545f15946e [file] [log] [blame]
scroggof24f2242015-03-03 08:59:20 -08001/*
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
msarettb46e5e22015-07-30 11:36:40 -07008#include "SkBmpCodec.h"
scroggof24f2242015-03-03 08:59:20 -08009#include "SkCodec.h"
msarett8c8f22a2015-04-01 06:58:48 -070010#include "SkCodecPriv.h"
msarettad8bcfe2016-03-07 07:09:03 -080011#include "SkColorSpace.h"
Matt Sarett313c4632016-10-20 12:35:23 -040012#include "SkColorSpaceXform.h"
msarett1a464672016-01-07 13:17:19 -080013#include "SkData.h"
14#include "SkGifCodec.h"
msarettf7eb6fc2016-09-13 09:04:11 -070015#include "SkHalf.h"
msarett1a464672016-01-07 13:17:19 -080016#include "SkIcoCodec.h"
msarette16b04a2015-04-15 07:32:19 -070017#include "SkJpegCodec.h"
msarettad3a5c62016-05-06 07:21:26 -070018#ifdef SK_HAS_PNG_LIBRARY
msarettbe1d5552016-01-21 09:05:23 -080019#include "SkPngCodec.h"
yujieqin916de9f2016-01-25 08:26:16 -080020#endif
msarett39b2d5a2016-02-17 08:26:31 -080021#include "SkRawCodec.h"
scroggof24f2242015-03-03 08:59:20 -080022#include "SkStream.h"
msarett1a464672016-01-07 13:17:19 -080023#include "SkWbmpCodec.h"
scroggo6f5e6192015-06-18 12:53:43 -070024#include "SkWebpCodec.h"
scroggof24f2242015-03-03 08:59:20 -080025
msarett74114382015-03-16 11:55:18 -070026struct DecoderProc {
scroggodb30be22015-12-08 18:54:13 -080027 bool (*IsFormat)(const void*, size_t);
msarett74114382015-03-16 11:55:18 -070028 SkCodec* (*NewFromStream)(SkStream*);
29};
30
31static const DecoderProc gDecoderProcs[] = {
msarettad3a5c62016-05-06 07:21:26 -070032#ifdef SK_HAS_JPEG_LIBRARY
msarette16b04a2015-04-15 07:32:19 -070033 { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080034#endif
msarettad3a5c62016-05-06 07:21:26 -070035#ifdef SK_HAS_WEBP_LIBRARY
scroggo6f5e6192015-06-18 12:53:43 -070036 { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080037#endif
msarett8c8f22a2015-04-01 06:58:48 -070038 { SkGifCodec::IsGif, SkGifCodec::NewFromStream },
msarettad3a5c62016-05-06 07:21:26 -070039#ifdef SK_HAS_PNG_LIBRARY
msarett9bde9182015-03-25 05:27:48 -070040 { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080041#endif
halcanarya096d7a2015-03-27 12:16:53 -070042 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream },
43 { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream }
msarett74114382015-03-16 11:55:18 -070044};
45
scroggodb30be22015-12-08 18:54:13 -080046size_t SkCodec::MinBufferedBytesNeeded() {
47 return WEBP_VP8_HEADER_SIZE;
48}
49
scroggocf98fa92015-11-23 08:14:40 -080050SkCodec* SkCodec::NewFromStream(SkStream* stream,
51 SkPngChunkReader* chunkReader) {
scroggof24f2242015-03-03 08:59:20 -080052 if (!stream) {
halcanary96fcdcc2015-08-27 07:41:13 -070053 return nullptr;
scroggof24f2242015-03-03 08:59:20 -080054 }
scroggo0a7e69c2015-04-03 07:22:22 -070055
Ben Wagner145dbcd2016-11-03 14:40:50 -040056 std::unique_ptr<SkStream> streamDeleter(stream);
scroggodb30be22015-12-08 18:54:13 -080057
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) {
scroggo3ab9f2e2016-01-06 09:53:34 -080073 // 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
scroggodb30be22015-12-08 18:54:13 -080077 // 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()) {
scroggo3ab9f2e2016-01-06 09:53:34 -080082 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
scroggodb30be22015-12-08 18:54:13 -080083 return nullptr;
84 }
85 }
86
scroggocf98fa92015-11-23 08:14:40 -080087 // PNG is special, since we want to be able to supply an SkPngChunkReader.
88 // But this code follows the same pattern as the loop.
msarettad3a5c62016-05-06 07:21:26 -070089#ifdef SK_HAS_PNG_LIBRARY
scroggodb30be22015-12-08 18:54:13 -080090 if (SkPngCodec::IsPng(buffer, bytesRead)) {
mtklein18300a32016-03-16 13:53:35 -070091 return SkPngCodec::NewFromStream(streamDeleter.release(), chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -080092 } else
93#endif
94 {
scroggocf98fa92015-11-23 08:14:40 -080095 for (DecoderProc proc : gDecoderProcs) {
scroggodb30be22015-12-08 18:54:13 -080096 if (proc.IsFormat(buffer, bytesRead)) {
mtklein18300a32016-03-16 13:53:35 -070097 return proc.NewFromStream(streamDeleter.release());
scroggocf98fa92015-11-23 08:14:40 -080098 }
msarett74114382015-03-16 11:55:18 -070099 }
yujieqin916de9f2016-01-25 08:26:16 -0800100
101#ifdef SK_CODEC_DECODES_RAW
102 // Try to treat the input as RAW if all the other checks failed.
mtklein18300a32016-03-16 13:53:35 -0700103 return SkRawCodec::NewFromStream(streamDeleter.release());
yujieqin916de9f2016-01-25 08:26:16 -0800104#endif
scroggof24f2242015-03-03 08:59:20 -0800105 }
msarett8c8f22a2015-04-01 06:58:48 -0700106
msarettf44631b2016-01-13 10:54:20 -0800107 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800108}
109
reed42943c82016-09-12 12:01:44 -0700110SkCodec* SkCodec::NewFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800111 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700112 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800113 }
scroggocf98fa92015-11-23 08:14:40 -0800114 return NewFromStream(new SkMemoryStream(data), reader);
scroggof24f2242015-03-03 08:59:20 -0800115}
116
msarettc30c4182016-04-20 11:53:35 -0700117SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
118 sk_sp<SkColorSpace> colorSpace, Origin origin)
119 : fEncodedInfo(info)
msarett530c8442016-07-21 11:57:49 -0700120 , fSrcInfo(info.makeImageInfo(width, height, std::move(colorSpace)))
scroggof24f2242015-03-03 08:59:20 -0800121 , fStream(stream)
122 , fNeedsRewind(false)
msarett0e6274f2016-03-21 08:04:40 -0700123 , fOrigin(origin)
scroggo46c57472015-09-30 08:57:13 -0700124 , fDstInfo()
125 , fOptions()
126 , fCurrScanline(-1)
scroggof24f2242015-03-03 08:59:20 -0800127{}
128
msarett549ca322016-08-17 08:54:08 -0700129SkCodec::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
scroggo9b2cdbf42015-07-10 12:07:02 -0700141SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700142
scroggob427db12015-08-12 07:24:13 -0700143bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800144 // Store the value of fNeedsRewind so we can update it. Next read will
145 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700146 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800147 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700148 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700149 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700150 }
scroggob427db12015-08-12 07:24:13 -0700151
scroggo46c57472015-09-30 08:57:13 -0700152 // startScanlineDecode will need to be called before decoding scanlines.
153 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700154 // startIncrementalDecode will need to be called before incrementalDecode.
155 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700156
scroggo19b91532016-10-24 09:03:26 -0700157 // 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()) {
scroggob427db12015-08-12 07:24:13 -0700160 return false;
161 }
162
163 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800164}
scroggo05245902015-03-25 11:11:52 -0700165
scroggo8e6c7ad2016-09-16 08:20:38 -0700166#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
scroggoeb602a52015-07-09 08:16:03 -0700180SkCodec::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 }
halcanary96fcdcc2015-08-27 07:41:13 -0700185 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700186 return kInvalidParameters;
187 }
188 if (rowBytes < info.minRowBytes()) {
189 return kInvalidParameters;
190 }
191
scroggo8e6c7ad2016-09-16 08:20:38 -0700192 CHECK_COLOR_TABLE;
scroggoeb602a52015-07-09 08:16:03 -0700193
scroggo3a7701c2015-09-30 09:15:14 -0700194 if (!this->rewindIfNeeded()) {
195 return kCouldNotRewind;
196 }
197
scroggoeb602a52015-07-09 08:16:03 -0700198 // Default options.
199 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700200 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700201 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700202 } 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 }
scroggoeb602a52015-07-09 08:16:03 -0700209 }
scroggoe7fc14b2015-10-02 13:14:46 -0700210
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
scroggo8e6c7ad2016-09-16 08:20:38 -0700217 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500218 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700219
msarette6dd0042015-10-09 11:07:34 -0700220 // 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);
scroggoeb602a52015-07-09 08:16:03 -0700225
226 if ((kIncompleteInput == result || kSuccess == result) && ctableCount) {
227 SkASSERT(*ctableCount >= 0 && *ctableCount <= 256);
228 }
msarette6dd0042015-10-09 11:07:34 -0700229
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 III42886572017-01-27 13:16:28 -0500236 // 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;
msarette6dd0042015-10-09 11:07:34 -0700242 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
243 rowsDecoded);
244 }
245
scroggoeb602a52015-07-09 08:16:03 -0700246 return result;
247}
248
249SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
halcanary96fcdcc2015-08-27 07:41:13 -0700250 return this->getPixels(info, pixels, rowBytes, nullptr, nullptr, nullptr);
scroggoeb602a52015-07-09 08:16:03 -0700251}
scroggo46c57472015-09-30 08:57:13 -0700252
scroggo8e6c7ad2016-09-16 08:20:38 -0700253SkCodec::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
318SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
scroggo46c57472015-09-30 08:57:13 -0700319 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
scroggo8e6c7ad2016-09-16 08:20:38 -0700323 CHECK_COLOR_TABLE;
scroggo46c57472015-09-30 08:57:13 -0700324
scroggo3a7701c2015-09-30 09:15:14 -0700325 if (!this->rewindIfNeeded()) {
326 return kCouldNotRewind;
327 }
328
scroggo46c57472015-09-30 08:57:13 -0700329 // Set options.
330 Options optsStorage;
331 if (nullptr == options) {
332 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700333 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700334 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700335 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().
scroggo8e6c7ad2016-09-16 08:20:38 -0700341 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700342 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700343 }
344 }
345
346 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700347 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700348 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700349 }
350
scroggo8e6c7ad2016-09-16 08:20:38 -0700351 const Result result = this->onStartScanlineDecode(info, *options, ctable, ctableCount);
scroggo46c57472015-09-30 08:57:13 -0700352 if (result != SkCodec::kSuccess) {
353 return result;
354 }
355
356 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700357 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700358 fOptions = *options;
359 return kSuccess;
360}
361
scroggo8e6c7ad2016-09-16 08:20:38 -0700362#undef CHECK_COLOR_TABLE
363
364SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info) {
365 return this->startScanlineDecode(info, nullptr, nullptr, nullptr);
scroggo46c57472015-09-30 08:57:13 -0700366}
367
msarette6dd0042015-10-09 11:07:34 -0700368int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700369 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700370 return 0;
scroggo46c57472015-09-30 08:57:13 -0700371 }
372
373 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700374 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700375 return 0;
scroggo46c57472015-09-30 08:57:13 -0700376 }
377
msarette6dd0042015-10-09 11:07:34 -0700378 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
387bool 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);
scroggo46c57472015-09-30 08:57:13 -0700401 fCurrScanline += countLines;
402 return result;
403}
404
msarette6dd0042015-10-09 11:07:34 -0700405int SkCodec::outputScanline(int inputScanline) const {
406 SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height());
407 return this->onOutputScanline(inputScanline);
408}
scroggo46c57472015-09-30 08:57:13 -0700409
msarette6dd0042015-10-09 11:07:34 -0700410int SkCodec::onOutputScanline(int inputScanline) const {
411 switch (this->getScanlineOrder()) {
412 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700413 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;
scroggo46c57472015-09-30 08:57:13 -0700420 }
msarette6dd0042015-10-09 11:07:34 -0700421}
scroggo46c57472015-09-30 08:57:13 -0700422
msarettf7eb6fc2016-09-13 09:04:11 -0700423uint64_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
msarette6dd0042015-10-09 11:07:34 -0700439static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes,
msarettf7eb6fc2016-09-13 09:04:11 -0700440 uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) {
msarette6dd0042015-10-09 11:07:34 -0700441 if (sampler) {
442 sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit);
443 } else {
444 SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit);
445 }
446}
447
448void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
449 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
450
451 void* fillDst;
msarettf7eb6fc2016-09-13 09:04:11 -0700452 const uint64_t fillValue = this->getFillValue(info);
msarette6dd0042015-10-09 11:07:34 -0700453 const int linesRemaining = linesRequested - linesDecoded;
454 SkSampler* sampler = this->getSampler(false);
455
msarett91c22b22016-02-22 12:27:46 -0800456 int fillWidth = info.width();
457 if (fOptions.fSubset) {
458 fillWidth = fOptions.fSubset->width();
459 }
460
msarette6dd0042015-10-09 11:07:34 -0700461 switch (this->getScanlineOrder()) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700462 case kTopDown_SkScanlineOrder: {
msarett91c22b22016-02-22 12:27:46 -0800463 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700464 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;
msarett91c22b22016-02-22 12:27:46 -0800470 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700471 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
472 break;
473 }
msarette6dd0042015-10-09 11:07:34 -0700474 }
scroggo46c57472015-09-30 08:57:13 -0700475}
Matt Sarett313c4632016-10-20 12:35:23 -0400476
477bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo) {
478 fColorXform = nullptr;
Matt Sarett61eedeb2016-11-04 13:19:48 -0400479 bool needsPremul = needs_premul(dstInfo, fEncodedInfo);
480 if (needs_color_xform(dstInfo, fSrcInfo, needsPremul)) {
Matt Sarett313c4632016-10-20 12:35:23 -0400481 fColorXform = SkColorSpaceXform::New(fSrcInfo.colorSpace(), dstInfo.colorSpace());
482 if (!fColorXform) {
483 return false;
484 }
485 }
486
487 return true;
488}