blob: 7dc10854d295029695781b70dc7c439bc8b73574 [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"
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -040014#include "SkFrameHolder.h"
msarett1a464672016-01-07 13:17:19 -080015#include "SkGifCodec.h"
msarettf7eb6fc2016-09-13 09:04:11 -070016#include "SkHalf.h"
msarett1a464672016-01-07 13:17:19 -080017#include "SkIcoCodec.h"
msarette16b04a2015-04-15 07:32:19 -070018#include "SkJpegCodec.h"
msarettad3a5c62016-05-06 07:21:26 -070019#ifdef SK_HAS_PNG_LIBRARY
msarettbe1d5552016-01-21 09:05:23 -080020#include "SkPngCodec.h"
yujieqin916de9f2016-01-25 08:26:16 -080021#endif
msarett39b2d5a2016-02-17 08:26:31 -080022#include "SkRawCodec.h"
scroggof24f2242015-03-03 08:59:20 -080023#include "SkStream.h"
msarett1a464672016-01-07 13:17:19 -080024#include "SkWbmpCodec.h"
scroggo6f5e6192015-06-18 12:53:43 -070025#include "SkWebpCodec.h"
Chong Zhangc2a95422017-08-16 13:03:47 -070026#include "SkHeifCodec.h"
scroggof24f2242015-03-03 08:59:20 -080027
msarett74114382015-03-16 11:55:18 -070028struct DecoderProc {
scroggodb30be22015-12-08 18:54:13 -080029 bool (*IsFormat)(const void*, size_t);
Mike Reedede7bac2017-07-23 15:30:02 -040030 std::unique_ptr<SkCodec> (*MakeFromStream)(std::unique_ptr<SkStream>, SkCodec::Result*);
msarett74114382015-03-16 11:55:18 -070031};
32
33static const DecoderProc gDecoderProcs[] = {
msarettad3a5c62016-05-06 07:21:26 -070034#ifdef SK_HAS_JPEG_LIBRARY
Mike Reedede7bac2017-07-23 15:30:02 -040035 { SkJpegCodec::IsJpeg, SkJpegCodec::MakeFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080036#endif
msarettad3a5c62016-05-06 07:21:26 -070037#ifdef SK_HAS_WEBP_LIBRARY
Mike Reedede7bac2017-07-23 15:30:02 -040038 { SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080039#endif
Mike Reedede7bac2017-07-23 15:30:02 -040040 { SkGifCodec::IsGif, SkGifCodec::MakeFromStream },
msarettad3a5c62016-05-06 07:21:26 -070041#ifdef SK_HAS_PNG_LIBRARY
Mike Reedede7bac2017-07-23 15:30:02 -040042 { SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080043#endif
Mike Reedede7bac2017-07-23 15:30:02 -040044 { SkBmpCodec::IsBmp, SkBmpCodec::MakeFromStream },
Chong Zhangc2a95422017-08-16 13:03:47 -070045 { SkWbmpCodec::IsWbmp, SkWbmpCodec::MakeFromStream },
46#ifdef SK_HAS_HEIF_LIBRARY
47 { SkHeifCodec::IsHeif, SkHeifCodec::MakeFromStream },
48#endif
msarett74114382015-03-16 11:55:18 -070049};
50
Mike Reedede7bac2017-07-23 15:30:02 -040051std::unique_ptr<SkCodec> SkCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
52 Result* outResult, SkPngChunkReader* chunkReader) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040053 Result resultStorage;
54 if (!outResult) {
55 outResult = &resultStorage;
56 }
57
scroggof24f2242015-03-03 08:59:20 -080058 if (!stream) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040059 *outResult = kInvalidInput;
halcanary96fcdcc2015-08-27 07:41:13 -070060 return nullptr;
scroggof24f2242015-03-03 08:59:20 -080061 }
scroggo0a7e69c2015-04-03 07:22:22 -070062
Chong Zhangc2a95422017-08-16 13:03:47 -070063 constexpr size_t bytesToRead = MinBufferedBytesNeeded();
scroggodb30be22015-12-08 18:54:13 -080064
65 char buffer[bytesToRead];
66 size_t bytesRead = stream->peek(buffer, bytesToRead);
67
68 // It is also possible to have a complete image less than bytesToRead bytes
69 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
70 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
71 // than bytesToRead, so pass that directly to the decoder.
72 // It also is possible the stream uses too small a buffer for peeking, but
73 // we trust the caller to use a large enough buffer.
74
75 if (0 == bytesRead) {
scroggo3ab9f2e2016-01-06 09:53:34 -080076 // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
77 // printf could be useful to notice failures.
78 // SkCodecPrintf("Encoded image data failed to peek!\n");
79
scroggodb30be22015-12-08 18:54:13 -080080 // It is possible the stream does not support peeking, but does support
81 // rewinding.
82 // Attempt to read() and pass the actual amount read to the decoder.
83 bytesRead = stream->read(buffer, bytesToRead);
84 if (!stream->rewind()) {
scroggo3ab9f2e2016-01-06 09:53:34 -080085 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -040086 *outResult = kCouldNotRewind;
scroggodb30be22015-12-08 18:54:13 -080087 return nullptr;
88 }
89 }
90
scroggocf98fa92015-11-23 08:14:40 -080091 // PNG is special, since we want to be able to supply an SkPngChunkReader.
92 // But this code follows the same pattern as the loop.
msarettad3a5c62016-05-06 07:21:26 -070093#ifdef SK_HAS_PNG_LIBRARY
scroggodb30be22015-12-08 18:54:13 -080094 if (SkPngCodec::IsPng(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -040095 return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -080096 } else
97#endif
98 {
scroggocf98fa92015-11-23 08:14:40 -080099 for (DecoderProc proc : gDecoderProcs) {
scroggodb30be22015-12-08 18:54:13 -0800100 if (proc.IsFormat(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400101 return proc.MakeFromStream(std::move(stream), outResult);
scroggocf98fa92015-11-23 08:14:40 -0800102 }
msarett74114382015-03-16 11:55:18 -0700103 }
yujieqin916de9f2016-01-25 08:26:16 -0800104
105#ifdef SK_CODEC_DECODES_RAW
106 // Try to treat the input as RAW if all the other checks failed.
Mike Reedede7bac2017-07-23 15:30:02 -0400107 return SkRawCodec::MakeFromStream(std::move(stream), outResult);
yujieqin916de9f2016-01-25 08:26:16 -0800108#endif
scroggof24f2242015-03-03 08:59:20 -0800109 }
msarett8c8f22a2015-04-01 06:58:48 -0700110
Leon Scroggins III588fb042017-07-14 16:32:31 -0400111 if (bytesRead < bytesToRead) {
112 *outResult = kIncompleteInput;
113 } else {
114 *outResult = kUnimplemented;
115 }
116
msarettf44631b2016-01-13 10:54:20 -0800117 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800118}
119
Mike Reedede7bac2017-07-23 15:30:02 -0400120std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800121 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700122 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800123 }
Mike Reed847068c2017-07-26 11:35:53 -0400124 return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
scroggof24f2242015-03-03 08:59:20 -0800125}
126
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400127SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info,
Mike Reedede7bac2017-07-23 15:30:02 -0400128 XformFormat srcFormat, std::unique_ptr<SkStream> stream,
msarettc30c4182016-04-20 11:53:35 -0700129 sk_sp<SkColorSpace> colorSpace, Origin origin)
130 : fEncodedInfo(info)
msarett530c8442016-07-21 11:57:49 -0700131 , fSrcInfo(info.makeImageInfo(width, height, std::move(colorSpace)))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400132 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400133 , fStream(std::move(stream))
scroggof24f2242015-03-03 08:59:20 -0800134 , fNeedsRewind(false)
msarett0e6274f2016-03-21 08:04:40 -0700135 , fOrigin(origin)
scroggo46c57472015-09-30 08:57:13 -0700136 , fDstInfo()
137 , fOptions()
138 , fCurrScanline(-1)
scroggof24f2242015-03-03 08:59:20 -0800139{}
140
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400141SkCodec::SkCodec(const SkEncodedInfo& info, const SkImageInfo& imageInfo,
Mike Reedede7bac2017-07-23 15:30:02 -0400142 XformFormat srcFormat, std::unique_ptr<SkStream> stream, Origin origin)
msarett549ca322016-08-17 08:54:08 -0700143 : fEncodedInfo(info)
144 , fSrcInfo(imageInfo)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400145 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400146 , fStream(std::move(stream))
msarett549ca322016-08-17 08:54:08 -0700147 , fNeedsRewind(false)
148 , fOrigin(origin)
149 , fDstInfo()
150 , fOptions()
151 , fCurrScanline(-1)
152{}
153
scroggo9b2cdbf42015-07-10 12:07:02 -0700154SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700155
scroggob427db12015-08-12 07:24:13 -0700156bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800157 // Store the value of fNeedsRewind so we can update it. Next read will
158 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700159 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800160 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700161 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700162 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700163 }
scroggob427db12015-08-12 07:24:13 -0700164
scroggo46c57472015-09-30 08:57:13 -0700165 // startScanlineDecode will need to be called before decoding scanlines.
166 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700167 // startIncrementalDecode will need to be called before incrementalDecode.
168 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700169
scroggo19b91532016-10-24 09:03:26 -0700170 // Some codecs do not have a stream. They may hold onto their own data or another codec.
171 // They must handle rewinding themselves.
172 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700173 return false;
174 }
175
176 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800177}
scroggo05245902015-03-25 11:11:52 -0700178
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400179static void zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
180 SkIRect frameRect) {
181 if (!frameRect.intersect(dstInfo.bounds())) {
182 return;
183 }
184 const auto info = dstInfo.makeWH(frameRect.width(), frameRect.height());
185 const size_t bpp = SkColorTypeBytesPerPixel(dstInfo.colorType());
186 const size_t offset = frameRect.x() * bpp + frameRect.y() * rowBytes;
187 auto* eraseDst = SkTAddOffset<void>(pixels, offset);
188 SkSampler::Fill(info, eraseDst, rowBytes, 0, SkCodec::kNo_ZeroInitialized);
189}
190
191SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
192 const Options& options) {
193 const int index = options.fFrameIndex;
194 if (0 == index) {
195 return kSuccess;
196 }
197
198 if (options.fSubset || info.dimensions() != fSrcInfo.dimensions()) {
199 // If we add support for these, we need to update the code that zeroes
200 // a kRestoreBGColor frame.
201 return kInvalidParameters;
202 }
203
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400204 if (index >= this->onGetFrameCount()) {
205 return kIncompleteInput;
206 }
207
208 const auto* frameHolder = this->getFrameHolder();
209 SkASSERT(frameHolder);
210
211 const auto* frame = frameHolder->getFrame(index);
212 SkASSERT(frame);
213
214 const int requiredFrame = frame->getRequiredFrame();
215 if (requiredFrame == kNone) {
216 return kSuccess;
217 }
218
219 if (options.fPriorFrame != kNone) {
220 // Check for a valid frame as a starting point. Alternatively, we could
221 // treat an invalid frame as not providing one, but rejecting it will
222 // make it easier to catch the mistake.
223 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
224 return kInvalidParameters;
225 }
226 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
227 switch (prevFrame->getDisposalMethod()) {
228 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
229 return kInvalidParameters;
230 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
231 // If a frame after the required frame is provided, there is no
232 // need to clear, since it must be covered by the desired frame.
233 if (options.fPriorFrame == requiredFrame) {
234 zero_rect(info, pixels, rowBytes, prevFrame->frameRect());
235 }
236 break;
237 default:
238 break;
239 }
240 return kSuccess;
241 }
242
243 Options prevFrameOptions(options);
244 prevFrameOptions.fFrameIndex = requiredFrame;
245 prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000246 const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400247 if (result == kSuccess) {
248 const auto* prevFrame = frameHolder->getFrame(requiredFrame);
249 const auto disposalMethod = prevFrame->getDisposalMethod();
250 if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
251 zero_rect(info, pixels, rowBytes, prevFrame->frameRect());
252 }
253 }
254
255 return result;
256}
scroggo8e6c7ad2016-09-16 08:20:38 -0700257
scroggoeb602a52015-07-09 08:16:03 -0700258SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000259 const Options* options) {
scroggoeb602a52015-07-09 08:16:03 -0700260 if (kUnknown_SkColorType == info.colorType()) {
261 return kInvalidConversion;
262 }
halcanary96fcdcc2015-08-27 07:41:13 -0700263 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700264 return kInvalidParameters;
265 }
266 if (rowBytes < info.minRowBytes()) {
267 return kInvalidParameters;
268 }
269
scroggo3a7701c2015-09-30 09:15:14 -0700270 if (!this->rewindIfNeeded()) {
271 return kCouldNotRewind;
272 }
273
scroggoeb602a52015-07-09 08:16:03 -0700274 // Default options.
275 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700276 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700277 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400278 } else {
279 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, *options);
280 if (frameIndexResult != kSuccess) {
281 return frameIndexResult;
282 }
283 if (options->fSubset) {
284 SkIRect subset(*options->fSubset);
285 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
286 // FIXME: How to differentiate between not supporting subset at all
287 // and not supporting this particular subset?
288 return kUnimplemented;
289 }
scroggoe7fc14b2015-10-02 13:14:46 -0700290 }
scroggoeb602a52015-07-09 08:16:03 -0700291 }
scroggoe7fc14b2015-10-02 13:14:46 -0700292
293 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
294 // because it supports arbitrary scaling/subset combinations.
295 if (!this->dimensionsSupported(info.dimensions())) {
296 return kInvalidScale;
297 }
298
scroggo8e6c7ad2016-09-16 08:20:38 -0700299 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500300 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700301
msarette6dd0042015-10-09 11:07:34 -0700302 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
303 // successfully.
304 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000305 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700306
307 // A return value of kIncompleteInput indicates a truncated image stream.
308 // In this case, we will fill any uninitialized memory with a default value.
309 // Some subclasses will take care of filling any uninitialized memory on
310 // their own. They indicate that all of the memory has been filled by
311 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400312 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500313 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
314 // there is a subset. In that case, it will use the width of the subset. From here, the
315 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
316 // differenty from the other codecs, and it needs to use the width specified by the info.
317 // Set the subset to null so SkWebpCodec uses the correct width.
318 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700319 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
320 rowsDecoded);
321 }
322
scroggoeb602a52015-07-09 08:16:03 -0700323 return result;
324}
325
scroggo8e6c7ad2016-09-16 08:20:38 -0700326SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000327 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700328 fStartedIncrementalDecode = false;
329
330 if (kUnknown_SkColorType == info.colorType()) {
331 return kInvalidConversion;
332 }
333 if (nullptr == pixels) {
334 return kInvalidParameters;
335 }
336
scroggo8e6c7ad2016-09-16 08:20:38 -0700337 // FIXME: If the rows come after the rows of a previous incremental decode,
338 // we might be able to skip the rewind, but only the implementation knows
339 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
340 // a bottom-up BMP could skip rewinding if the new rows are above the old
341 // rows.)
342 if (!this->rewindIfNeeded()) {
343 return kCouldNotRewind;
344 }
345
346 // Set options.
347 Options optsStorage;
348 if (nullptr == options) {
349 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400350 } else {
351 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, *options);
352 if (frameIndexResult != kSuccess) {
353 return frameIndexResult;
scroggo8e6c7ad2016-09-16 08:20:38 -0700354 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400355 if (options->fSubset) {
356 SkIRect size = SkIRect::MakeSize(info.dimensions());
357 if (!size.contains(*options->fSubset)) {
358 return kInvalidParameters;
359 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700360
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400361 const int top = options->fSubset->top();
362 const int bottom = options->fSubset->bottom();
363 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
364 return kInvalidParameters;
365 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700366 }
367 }
368
369 if (!this->dimensionsSupported(info.dimensions())) {
370 return kInvalidScale;
371 }
372
373 fDstInfo = info;
374 fOptions = *options;
375
Leon Scroggins571b30f2017-07-11 17:35:31 +0000376 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700377 if (kSuccess == result) {
378 fStartedIncrementalDecode = true;
379 } else if (kUnimplemented == result) {
380 // FIXME: This is temporarily necessary, until we transition SkCodec
381 // implementations from scanline decoding to incremental decoding.
382 // SkAndroidCodec will first attempt to use incremental decoding, but
383 // will fall back to scanline decoding if incremental returns
384 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
385 // (after potentially rewinding), but we do not want the next call to
386 // startScanlineDecode() to do a rewind.
387 fNeedsRewind = false;
388 }
389 return result;
390}
391
392
393SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000394 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700395 // Reset fCurrScanline in case of failure.
396 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700397
scroggo3a7701c2015-09-30 09:15:14 -0700398 if (!this->rewindIfNeeded()) {
399 return kCouldNotRewind;
400 }
401
scroggo46c57472015-09-30 08:57:13 -0700402 // Set options.
403 Options optsStorage;
404 if (nullptr == options) {
405 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700406 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700407 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700408 if (!size.contains(*options->fSubset)) {
409 return kInvalidInput;
410 }
411
412 // We only support subsetting in the x-dimension for scanline decoder.
413 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700414 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700415 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700416 }
417 }
418
419 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700420 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700421 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700422 }
423
Leon Scroggins571b30f2017-07-11 17:35:31 +0000424 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700425 if (result != SkCodec::kSuccess) {
426 return result;
427 }
428
429 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700430 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700431 fOptions = *options;
432 return kSuccess;
433}
434
msarette6dd0042015-10-09 11:07:34 -0700435int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700436 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700437 return 0;
scroggo46c57472015-09-30 08:57:13 -0700438 }
439
440 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700441 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700442 return 0;
scroggo46c57472015-09-30 08:57:13 -0700443 }
444
msarette6dd0042015-10-09 11:07:34 -0700445 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
446 if (linesDecoded < countLines) {
447 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
448 countLines, linesDecoded);
449 }
450 fCurrScanline += countLines;
451 return linesDecoded;
452}
453
454bool SkCodec::skipScanlines(int countLines) {
455 if (fCurrScanline < 0) {
456 return false;
457 }
458
459 SkASSERT(!fDstInfo.isEmpty());
460 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
461 // Arguably, we could just skip the scanlines which are remaining,
462 // and return true. We choose to return false so the client
463 // can catch their bug.
464 return false;
465 }
466
467 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700468 fCurrScanline += countLines;
469 return result;
470}
471
msarette6dd0042015-10-09 11:07:34 -0700472int SkCodec::outputScanline(int inputScanline) const {
473 SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height());
474 return this->onOutputScanline(inputScanline);
475}
scroggo46c57472015-09-30 08:57:13 -0700476
msarette6dd0042015-10-09 11:07:34 -0700477int SkCodec::onOutputScanline(int inputScanline) const {
478 switch (this->getScanlineOrder()) {
479 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700480 return inputScanline;
481 case kBottomUp_SkScanlineOrder:
482 return this->getInfo().height() - inputScanline - 1;
483 default:
484 // This case indicates an interlaced gif and is implemented by SkGifCodec.
485 SkASSERT(false);
486 return 0;
scroggo46c57472015-09-30 08:57:13 -0700487 }
msarette6dd0042015-10-09 11:07:34 -0700488}
scroggo46c57472015-09-30 08:57:13 -0700489
msarettf7eb6fc2016-09-13 09:04:11 -0700490uint64_t SkCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
491 switch (dstInfo.colorType()) {
492 case kRGBA_F16_SkColorType: {
493 static constexpr uint64_t transparentColor = 0;
494 static constexpr uint64_t opaqueColor = ((uint64_t) SK_Half1) << 48;
495 return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? opaqueColor : transparentColor;
496 }
497 default: {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000498 // This not only handles the kN32 case, but also k565, kGray8, since
msarettf7eb6fc2016-09-13 09:04:11 -0700499 // the low bits are zeros.
500 return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ?
501 SK_ColorBLACK : SK_ColorTRANSPARENT;
502 }
503 }
504}
505
msarette6dd0042015-10-09 11:07:34 -0700506static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes,
msarettf7eb6fc2016-09-13 09:04:11 -0700507 uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) {
msarette6dd0042015-10-09 11:07:34 -0700508 if (sampler) {
509 sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit);
510 } else {
511 SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit);
512 }
513}
514
515void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
516 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
517
518 void* fillDst;
msarettf7eb6fc2016-09-13 09:04:11 -0700519 const uint64_t fillValue = this->getFillValue(info);
msarette6dd0042015-10-09 11:07:34 -0700520 const int linesRemaining = linesRequested - linesDecoded;
521 SkSampler* sampler = this->getSampler(false);
522
msarett91c22b22016-02-22 12:27:46 -0800523 int fillWidth = info.width();
524 if (fOptions.fSubset) {
525 fillWidth = fOptions.fSubset->width();
526 }
527
msarette6dd0042015-10-09 11:07:34 -0700528 switch (this->getScanlineOrder()) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700529 case kTopDown_SkScanlineOrder: {
msarett91c22b22016-02-22 12:27:46 -0800530 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700531 fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes);
532 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
533 break;
534 }
535 case kBottomUp_SkScanlineOrder: {
536 fillDst = dst;
msarett91c22b22016-02-22 12:27:46 -0800537 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700538 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
539 break;
540 }
msarette6dd0042015-10-09 11:07:34 -0700541 }
scroggo46c57472015-09-30 08:57:13 -0700542}
Matt Sarett313c4632016-10-20 12:35:23 -0400543
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400544static inline SkColorSpaceXform::ColorFormat select_xform_format_ct(SkColorType colorType) {
545 switch (colorType) {
546 case kRGBA_8888_SkColorType:
547 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
548 case kBGRA_8888_SkColorType:
549 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
550 case kRGB_565_SkColorType:
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400551#ifdef SK_PMCOLOR_IS_RGBA
552 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
553#else
554 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
555#endif
556 default:
557 SkASSERT(false);
558 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
559 }
560}
561
Matt Sarettcf3f2342017-03-23 15:32:25 -0400562bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo,
563 SkTransferFunctionBehavior premulBehavior) {
Matt Sarett313c4632016-10-20 12:35:23 -0400564 fColorXform = nullptr;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400565 fXformOnDecode = false;
Matt Sarettcf3f2342017-03-23 15:32:25 -0400566 bool needsColorCorrectPremul = needs_premul(dstInfo, fEncodedInfo) &&
567 SkTransferFunctionBehavior::kRespect == premulBehavior;
568 if (needs_color_xform(dstInfo, fSrcInfo, needsColorCorrectPremul)) {
569 fColorXform = SkColorSpaceXform_Base::New(fSrcInfo.colorSpace(), dstInfo.colorSpace(),
570 premulBehavior);
Matt Sarett313c4632016-10-20 12:35:23 -0400571 if (!fColorXform) {
572 return false;
573 }
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400574
575 // We will apply the color xform when reading the color table unless F16 is requested.
576 fXformOnDecode = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
577 || kRGBA_F16_SkColorType == dstInfo.colorType();
578 if (fXformOnDecode) {
579 fDstXformFormat = select_xform_format(dstInfo.colorType());
580 } else {
581 fDstXformFormat = select_xform_format_ct(dstInfo.colorType());
582 }
Matt Sarett313c4632016-10-20 12:35:23 -0400583 }
584
585 return true;
586}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400587
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400588void SkCodec::applyColorXform(void* dst, const void* src, int count, SkAlphaType at) const {
589 SkASSERT(fColorXform);
590 SkAssertResult(fColorXform->apply(fDstXformFormat, dst,
591 fSrcXformFormat, src,
592 count, at));
593}
594
595void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
596 auto alphaType = select_xform_alpha(fDstInfo.alphaType(), fSrcInfo.alphaType());
597 this->applyColorXform(dst, src, count, alphaType);
598}
599
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400600std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400601 const int frameCount = this->getFrameCount();
602 SkASSERT(frameCount >= 0);
603 if (frameCount <= 0) {
604 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400605 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400606
607 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
608 // Not animated.
609 return std::vector<FrameInfo>{};
610 }
611
612 std::vector<FrameInfo> result(frameCount);
613 for (int i = 0; i < frameCount; ++i) {
614 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
615 }
616 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400617}