blob: 2cae6db1df468cd953bcf5692326ce0c1b720f9c [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 Sarettcf3f2342017-03-23 15:32:25 -040012#include "SkColorSpaceXform_Base.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
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400117SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info,
118 XformFormat srcFormat, SkStream* stream,
msarettc30c4182016-04-20 11:53:35 -0700119 sk_sp<SkColorSpace> colorSpace, Origin origin)
120 : fEncodedInfo(info)
msarett530c8442016-07-21 11:57:49 -0700121 , fSrcInfo(info.makeImageInfo(width, height, std::move(colorSpace)))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400122 , fSrcXformFormat(srcFormat)
scroggof24f2242015-03-03 08:59:20 -0800123 , fStream(stream)
124 , fNeedsRewind(false)
msarett0e6274f2016-03-21 08:04:40 -0700125 , fOrigin(origin)
scroggo46c57472015-09-30 08:57:13 -0700126 , fDstInfo()
127 , fOptions()
128 , fCurrScanline(-1)
scroggof24f2242015-03-03 08:59:20 -0800129{}
130
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400131SkCodec::SkCodec(const SkEncodedInfo& info, const SkImageInfo& imageInfo,
132 XformFormat srcFormat, SkStream* stream, Origin origin)
msarett549ca322016-08-17 08:54:08 -0700133 : fEncodedInfo(info)
134 , fSrcInfo(imageInfo)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400135 , fSrcXformFormat(srcFormat)
msarett549ca322016-08-17 08:54:08 -0700136 , fStream(stream)
137 , fNeedsRewind(false)
138 , fOrigin(origin)
139 , fDstInfo()
140 , fOptions()
141 , fCurrScanline(-1)
142{}
143
scroggo9b2cdbf42015-07-10 12:07:02 -0700144SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700145
scroggob427db12015-08-12 07:24:13 -0700146bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800147 // Store the value of fNeedsRewind so we can update it. Next read will
148 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700149 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800150 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700151 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700152 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700153 }
scroggob427db12015-08-12 07:24:13 -0700154
scroggo46c57472015-09-30 08:57:13 -0700155 // startScanlineDecode will need to be called before decoding scanlines.
156 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700157 // startIncrementalDecode will need to be called before incrementalDecode.
158 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700159
scroggo19b91532016-10-24 09:03:26 -0700160 // Some codecs do not have a stream. They may hold onto their own data or another codec.
161 // They must handle rewinding themselves.
162 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700163 return false;
164 }
165
166 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800167}
scroggo05245902015-03-25 11:11:52 -0700168
scroggo8e6c7ad2016-09-16 08:20:38 -0700169#define CHECK_COLOR_TABLE \
170 if (kIndex_8_SkColorType == info.colorType()) { \
171 if (nullptr == ctable || nullptr == ctableCount) { \
172 return SkCodec::kInvalidParameters; \
173 } \
174 } else { \
175 if (ctableCount) { \
176 *ctableCount = 0; \
177 } \
178 ctableCount = nullptr; \
179 ctable = nullptr; \
180 }
181
182
scroggoeb602a52015-07-09 08:16:03 -0700183SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
184 const Options* options, SkPMColor ctable[], int* ctableCount) {
185 if (kUnknown_SkColorType == info.colorType()) {
186 return kInvalidConversion;
187 }
halcanary96fcdcc2015-08-27 07:41:13 -0700188 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700189 return kInvalidParameters;
190 }
191 if (rowBytes < info.minRowBytes()) {
192 return kInvalidParameters;
193 }
194
scroggo8e6c7ad2016-09-16 08:20:38 -0700195 CHECK_COLOR_TABLE;
scroggoeb602a52015-07-09 08:16:03 -0700196
scroggo3a7701c2015-09-30 09:15:14 -0700197 if (!this->rewindIfNeeded()) {
198 return kCouldNotRewind;
199 }
200
scroggoeb602a52015-07-09 08:16:03 -0700201 // Default options.
202 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700203 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700204 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700205 } else if (options->fSubset) {
206 SkIRect subset(*options->fSubset);
207 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
208 // FIXME: How to differentiate between not supporting subset at all
209 // and not supporting this particular subset?
210 return kUnimplemented;
211 }
scroggoeb602a52015-07-09 08:16:03 -0700212 }
scroggoe7fc14b2015-10-02 13:14:46 -0700213
214 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
215 // because it supports arbitrary scaling/subset combinations.
216 if (!this->dimensionsSupported(info.dimensions())) {
217 return kInvalidScale;
218 }
219
scroggo8e6c7ad2016-09-16 08:20:38 -0700220 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500221 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700222
msarette6dd0042015-10-09 11:07:34 -0700223 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
224 // successfully.
225 int rowsDecoded = 0;
226 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, ctable, ctableCount,
227 &rowsDecoded);
scroggoeb602a52015-07-09 08:16:03 -0700228
229 if ((kIncompleteInput == result || kSuccess == result) && ctableCount) {
230 SkASSERT(*ctableCount >= 0 && *ctableCount <= 256);
231 }
msarette6dd0042015-10-09 11:07:34 -0700232
233 // A return value of kIncompleteInput indicates a truncated image stream.
234 // In this case, we will fill any uninitialized memory with a default value.
235 // Some subclasses will take care of filling any uninitialized memory on
236 // their own. They indicate that all of the memory has been filled by
237 // setting rowsDecoded equal to the height.
238 if (kIncompleteInput == result && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500239 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
240 // there is a subset. In that case, it will use the width of the subset. From here, the
241 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
242 // differenty from the other codecs, and it needs to use the width specified by the info.
243 // Set the subset to null so SkWebpCodec uses the correct width.
244 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700245 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
246 rowsDecoded);
247 }
248
scroggoeb602a52015-07-09 08:16:03 -0700249 return result;
250}
251
252SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
halcanary96fcdcc2015-08-27 07:41:13 -0700253 return this->getPixels(info, pixels, rowBytes, nullptr, nullptr, nullptr);
scroggoeb602a52015-07-09 08:16:03 -0700254}
scroggo46c57472015-09-30 08:57:13 -0700255
scroggo8e6c7ad2016-09-16 08:20:38 -0700256SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
257 size_t rowBytes, const SkCodec::Options* options, SkPMColor* ctable, int* ctableCount) {
258 fStartedIncrementalDecode = false;
259
260 if (kUnknown_SkColorType == info.colorType()) {
261 return kInvalidConversion;
262 }
263 if (nullptr == pixels) {
264 return kInvalidParameters;
265 }
266
267 // Ensure that valid color ptrs are passed in for kIndex8 color type
268 CHECK_COLOR_TABLE;
269
270 // FIXME: If the rows come after the rows of a previous incremental decode,
271 // we might be able to skip the rewind, but only the implementation knows
272 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
273 // a bottom-up BMP could skip rewinding if the new rows are above the old
274 // rows.)
275 if (!this->rewindIfNeeded()) {
276 return kCouldNotRewind;
277 }
278
279 // Set options.
280 Options optsStorage;
281 if (nullptr == options) {
282 options = &optsStorage;
283 } else if (options->fSubset) {
284 SkIRect size = SkIRect::MakeSize(info.dimensions());
285 if (!size.contains(*options->fSubset)) {
286 return kInvalidParameters;
287 }
288
289 const int top = options->fSubset->top();
290 const int bottom = options->fSubset->bottom();
291 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
292 return kInvalidParameters;
293 }
294 }
295
296 if (!this->dimensionsSupported(info.dimensions())) {
297 return kInvalidScale;
298 }
299
300 fDstInfo = info;
301 fOptions = *options;
302
303 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes,
304 fOptions, ctable, ctableCount);
305 if (kSuccess == result) {
306 fStartedIncrementalDecode = true;
307 } else if (kUnimplemented == result) {
308 // FIXME: This is temporarily necessary, until we transition SkCodec
309 // implementations from scanline decoding to incremental decoding.
310 // SkAndroidCodec will first attempt to use incremental decoding, but
311 // will fall back to scanline decoding if incremental returns
312 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
313 // (after potentially rewinding), but we do not want the next call to
314 // startScanlineDecode() to do a rewind.
315 fNeedsRewind = false;
316 }
317 return result;
318}
319
320
321SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
scroggo46c57472015-09-30 08:57:13 -0700322 const SkCodec::Options* options, SkPMColor ctable[], int* ctableCount) {
323 // Reset fCurrScanline in case of failure.
324 fCurrScanline = -1;
325 // Ensure that valid color ptrs are passed in for kIndex8 color type
scroggo8e6c7ad2016-09-16 08:20:38 -0700326 CHECK_COLOR_TABLE;
scroggo46c57472015-09-30 08:57:13 -0700327
scroggo3a7701c2015-09-30 09:15:14 -0700328 if (!this->rewindIfNeeded()) {
329 return kCouldNotRewind;
330 }
331
scroggo46c57472015-09-30 08:57:13 -0700332 // Set options.
333 Options optsStorage;
334 if (nullptr == options) {
335 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700336 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700337 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700338 if (!size.contains(*options->fSubset)) {
339 return kInvalidInput;
340 }
341
342 // We only support subsetting in the x-dimension for scanline decoder.
343 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700344 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700345 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700346 }
347 }
348
349 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700350 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700351 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700352 }
353
scroggo8e6c7ad2016-09-16 08:20:38 -0700354 const Result result = this->onStartScanlineDecode(info, *options, ctable, ctableCount);
scroggo46c57472015-09-30 08:57:13 -0700355 if (result != SkCodec::kSuccess) {
356 return result;
357 }
358
359 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700360 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700361 fOptions = *options;
362 return kSuccess;
363}
364
scroggo8e6c7ad2016-09-16 08:20:38 -0700365#undef CHECK_COLOR_TABLE
366
367SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info) {
368 return this->startScanlineDecode(info, nullptr, nullptr, nullptr);
scroggo46c57472015-09-30 08:57:13 -0700369}
370
msarette6dd0042015-10-09 11:07:34 -0700371int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700372 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700373 return 0;
scroggo46c57472015-09-30 08:57:13 -0700374 }
375
376 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700377 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700378 return 0;
scroggo46c57472015-09-30 08:57:13 -0700379 }
380
msarette6dd0042015-10-09 11:07:34 -0700381 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
382 if (linesDecoded < countLines) {
383 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
384 countLines, linesDecoded);
385 }
386 fCurrScanline += countLines;
387 return linesDecoded;
388}
389
390bool SkCodec::skipScanlines(int countLines) {
391 if (fCurrScanline < 0) {
392 return false;
393 }
394
395 SkASSERT(!fDstInfo.isEmpty());
396 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
397 // Arguably, we could just skip the scanlines which are remaining,
398 // and return true. We choose to return false so the client
399 // can catch their bug.
400 return false;
401 }
402
403 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700404 fCurrScanline += countLines;
405 return result;
406}
407
msarette6dd0042015-10-09 11:07:34 -0700408int SkCodec::outputScanline(int inputScanline) const {
409 SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height());
410 return this->onOutputScanline(inputScanline);
411}
scroggo46c57472015-09-30 08:57:13 -0700412
msarette6dd0042015-10-09 11:07:34 -0700413int SkCodec::onOutputScanline(int inputScanline) const {
414 switch (this->getScanlineOrder()) {
415 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700416 return inputScanline;
417 case kBottomUp_SkScanlineOrder:
418 return this->getInfo().height() - inputScanline - 1;
419 default:
420 // This case indicates an interlaced gif and is implemented by SkGifCodec.
421 SkASSERT(false);
422 return 0;
scroggo46c57472015-09-30 08:57:13 -0700423 }
msarette6dd0042015-10-09 11:07:34 -0700424}
scroggo46c57472015-09-30 08:57:13 -0700425
msarettf7eb6fc2016-09-13 09:04:11 -0700426uint64_t SkCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
427 switch (dstInfo.colorType()) {
428 case kRGBA_F16_SkColorType: {
429 static constexpr uint64_t transparentColor = 0;
430 static constexpr uint64_t opaqueColor = ((uint64_t) SK_Half1) << 48;
431 return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? opaqueColor : transparentColor;
432 }
433 default: {
434 // This not only handles the kN32 case, but also k565, kGray8, kIndex8, since
435 // the low bits are zeros.
436 return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ?
437 SK_ColorBLACK : SK_ColorTRANSPARENT;
438 }
439 }
440}
441
msarette6dd0042015-10-09 11:07:34 -0700442static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes,
msarettf7eb6fc2016-09-13 09:04:11 -0700443 uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) {
msarette6dd0042015-10-09 11:07:34 -0700444 if (sampler) {
445 sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit);
446 } else {
447 SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit);
448 }
449}
450
451void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
452 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
453
454 void* fillDst;
msarettf7eb6fc2016-09-13 09:04:11 -0700455 const uint64_t fillValue = this->getFillValue(info);
msarette6dd0042015-10-09 11:07:34 -0700456 const int linesRemaining = linesRequested - linesDecoded;
457 SkSampler* sampler = this->getSampler(false);
458
msarett91c22b22016-02-22 12:27:46 -0800459 int fillWidth = info.width();
460 if (fOptions.fSubset) {
461 fillWidth = fOptions.fSubset->width();
462 }
463
msarette6dd0042015-10-09 11:07:34 -0700464 switch (this->getScanlineOrder()) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700465 case kTopDown_SkScanlineOrder: {
msarett91c22b22016-02-22 12:27:46 -0800466 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700467 fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes);
468 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
469 break;
470 }
471 case kBottomUp_SkScanlineOrder: {
472 fillDst = dst;
msarett91c22b22016-02-22 12:27:46 -0800473 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700474 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
475 break;
476 }
msarette6dd0042015-10-09 11:07:34 -0700477 }
scroggo46c57472015-09-30 08:57:13 -0700478}
Matt Sarett313c4632016-10-20 12:35:23 -0400479
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400480static inline SkColorSpaceXform::ColorFormat select_xform_format_ct(SkColorType colorType) {
481 switch (colorType) {
482 case kRGBA_8888_SkColorType:
483 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
484 case kBGRA_8888_SkColorType:
485 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
486 case kRGB_565_SkColorType:
487 case kIndex_8_SkColorType:
488#ifdef SK_PMCOLOR_IS_RGBA
489 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
490#else
491 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
492#endif
493 default:
494 SkASSERT(false);
495 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
496 }
497}
498
Matt Sarettcf3f2342017-03-23 15:32:25 -0400499bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo,
500 SkTransferFunctionBehavior premulBehavior) {
Matt Sarett313c4632016-10-20 12:35:23 -0400501 fColorXform = nullptr;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400502 fXformOnDecode = false;
Matt Sarettcf3f2342017-03-23 15:32:25 -0400503 bool needsColorCorrectPremul = needs_premul(dstInfo, fEncodedInfo) &&
504 SkTransferFunctionBehavior::kRespect == premulBehavior;
505 if (needs_color_xform(dstInfo, fSrcInfo, needsColorCorrectPremul)) {
506 fColorXform = SkColorSpaceXform_Base::New(fSrcInfo.colorSpace(), dstInfo.colorSpace(),
507 premulBehavior);
Matt Sarett313c4632016-10-20 12:35:23 -0400508 if (!fColorXform) {
509 return false;
510 }
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400511
512 // We will apply the color xform when reading the color table unless F16 is requested.
513 fXformOnDecode = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
514 || kRGBA_F16_SkColorType == dstInfo.colorType();
515 if (fXformOnDecode) {
516 fDstXformFormat = select_xform_format(dstInfo.colorType());
517 } else {
518 fDstXformFormat = select_xform_format_ct(dstInfo.colorType());
519 }
Matt Sarett313c4632016-10-20 12:35:23 -0400520 }
521
522 return true;
523}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400524
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400525void SkCodec::applyColorXform(void* dst, const void* src, int count, SkAlphaType at) const {
526 SkASSERT(fColorXform);
527 SkAssertResult(fColorXform->apply(fDstXformFormat, dst,
528 fSrcXformFormat, src,
529 count, at));
530}
531
532void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
533 auto alphaType = select_xform_alpha(fDstInfo.alphaType(), fSrcInfo.alphaType());
534 this->applyColorXform(dst, src, count, alphaType);
535}
536
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400537std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400538 const int frameCount = this->getFrameCount();
539 SkASSERT(frameCount >= 0);
540 if (frameCount <= 0) {
541 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400542 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400543
544 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
545 // Not animated.
546 return std::vector<FrameInfo>{};
547 }
548
549 std::vector<FrameInfo> result(frameCount);
550 for (int i = 0; i < frameCount; ++i) {
551 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
552 }
553 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400554}