blob: 739c9cd6730a3becd9b20ab94127cb571cd4b1af [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;
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
msarette6dd0042015-10-09 11:07:34 -0700222 // 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);
scroggoeb602a52015-07-09 08:16:03 -0700227
228 if ((kIncompleteInput == result || kSuccess == result) && ctableCount) {
229 SkASSERT(*ctableCount >= 0 && *ctableCount <= 256);
230 }
msarette6dd0042015-10-09 11:07:34 -0700231
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
scroggoeb602a52015-07-09 08:16:03 -0700242 return result;
243}
244
245SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
halcanary96fcdcc2015-08-27 07:41:13 -0700246 return this->getPixels(info, pixels, rowBytes, nullptr, nullptr, nullptr);
scroggoeb602a52015-07-09 08:16:03 -0700247}
scroggo46c57472015-09-30 08:57:13 -0700248
scroggo8e6c7ad2016-09-16 08:20:38 -0700249SkCodec::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
314SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
scroggo46c57472015-09-30 08:57:13 -0700315 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
scroggo8e6c7ad2016-09-16 08:20:38 -0700319 CHECK_COLOR_TABLE;
scroggo46c57472015-09-30 08:57:13 -0700320
scroggo3a7701c2015-09-30 09:15:14 -0700321 if (!this->rewindIfNeeded()) {
322 return kCouldNotRewind;
323 }
324
scroggo46c57472015-09-30 08:57:13 -0700325 // Set options.
326 Options optsStorage;
327 if (nullptr == options) {
328 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700329 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700330 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700331 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().
scroggo8e6c7ad2016-09-16 08:20:38 -0700337 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700338 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700339 }
340 }
341
342 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700343 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700344 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700345 }
346
scroggo8e6c7ad2016-09-16 08:20:38 -0700347 const Result result = this->onStartScanlineDecode(info, *options, ctable, ctableCount);
scroggo46c57472015-09-30 08:57:13 -0700348 if (result != SkCodec::kSuccess) {
349 return result;
350 }
351
352 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700353 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700354 fOptions = *options;
355 return kSuccess;
356}
357
scroggo8e6c7ad2016-09-16 08:20:38 -0700358#undef CHECK_COLOR_TABLE
359
360SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info) {
361 return this->startScanlineDecode(info, nullptr, nullptr, nullptr);
scroggo46c57472015-09-30 08:57:13 -0700362}
363
msarette6dd0042015-10-09 11:07:34 -0700364int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700365 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700366 return 0;
scroggo46c57472015-09-30 08:57:13 -0700367 }
368
369 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700370 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700371 return 0;
scroggo46c57472015-09-30 08:57:13 -0700372 }
373
msarette6dd0042015-10-09 11:07:34 -0700374 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
383bool 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);
scroggo46c57472015-09-30 08:57:13 -0700397 fCurrScanline += countLines;
398 return result;
399}
400
msarette6dd0042015-10-09 11:07:34 -0700401int SkCodec::outputScanline(int inputScanline) const {
402 SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height());
403 return this->onOutputScanline(inputScanline);
404}
scroggo46c57472015-09-30 08:57:13 -0700405
msarette6dd0042015-10-09 11:07:34 -0700406int SkCodec::onOutputScanline(int inputScanline) const {
407 switch (this->getScanlineOrder()) {
408 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700409 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;
scroggo46c57472015-09-30 08:57:13 -0700416 }
msarette6dd0042015-10-09 11:07:34 -0700417}
scroggo46c57472015-09-30 08:57:13 -0700418
msarettf7eb6fc2016-09-13 09:04:11 -0700419uint64_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
msarette6dd0042015-10-09 11:07:34 -0700435static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes,
msarettf7eb6fc2016-09-13 09:04:11 -0700436 uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) {
msarette6dd0042015-10-09 11:07:34 -0700437 if (sampler) {
438 sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit);
439 } else {
440 SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit);
441 }
442}
443
444void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
445 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
446
447 void* fillDst;
msarettf7eb6fc2016-09-13 09:04:11 -0700448 const uint64_t fillValue = this->getFillValue(info);
msarette6dd0042015-10-09 11:07:34 -0700449 const int linesRemaining = linesRequested - linesDecoded;
450 SkSampler* sampler = this->getSampler(false);
451
msarett91c22b22016-02-22 12:27:46 -0800452 int fillWidth = info.width();
453 if (fOptions.fSubset) {
454 fillWidth = fOptions.fSubset->width();
455 }
456
msarette6dd0042015-10-09 11:07:34 -0700457 switch (this->getScanlineOrder()) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700458 case kTopDown_SkScanlineOrder: {
msarett91c22b22016-02-22 12:27:46 -0800459 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700460 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;
msarett91c22b22016-02-22 12:27:46 -0800466 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700467 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
468 break;
469 }
msarette6dd0042015-10-09 11:07:34 -0700470 }
scroggo46c57472015-09-30 08:57:13 -0700471}
Matt Sarett313c4632016-10-20 12:35:23 -0400472
473bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo) {
474 fColorXform = nullptr;
Matt Sarett61eedeb2016-11-04 13:19:48 -0400475 bool needsPremul = needs_premul(dstInfo, fEncodedInfo);
476 if (needs_color_xform(dstInfo, fSrcInfo, needsPremul)) {
Matt Sarett313c4632016-10-20 12:35:23 -0400477 fColorXform = SkColorSpaceXform::New(fSrcInfo.colorSpace(), dstInfo.colorSpace());
478 if (!fColorXform) {
479 return false;
480 }
481 }
482
483 return true;
484}