blob: 4bd3917880ff97e049722e58ed029abd95f2d88c [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"
scroggof24f2242015-03-03 08:59:20 -080026
msarett74114382015-03-16 11:55:18 -070027struct DecoderProc {
scroggodb30be22015-12-08 18:54:13 -080028 bool (*IsFormat)(const void*, size_t);
msarett74114382015-03-16 11:55:18 -070029 SkCodec* (*NewFromStream)(SkStream*);
30};
31
32static const DecoderProc gDecoderProcs[] = {
msarettad3a5c62016-05-06 07:21:26 -070033#ifdef SK_HAS_JPEG_LIBRARY
msarette16b04a2015-04-15 07:32:19 -070034 { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080035#endif
msarettad3a5c62016-05-06 07:21:26 -070036#ifdef SK_HAS_WEBP_LIBRARY
scroggo6f5e6192015-06-18 12:53:43 -070037 { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080038#endif
msarett8c8f22a2015-04-01 06:58:48 -070039 { SkGifCodec::IsGif, SkGifCodec::NewFromStream },
msarettad3a5c62016-05-06 07:21:26 -070040#ifdef SK_HAS_PNG_LIBRARY
msarett9bde9182015-03-25 05:27:48 -070041 { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080042#endif
halcanarya096d7a2015-03-27 12:16:53 -070043 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream },
44 { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream }
msarett74114382015-03-16 11:55:18 -070045};
46
scroggodb30be22015-12-08 18:54:13 -080047size_t SkCodec::MinBufferedBytesNeeded() {
48 return WEBP_VP8_HEADER_SIZE;
49}
50
scroggocf98fa92015-11-23 08:14:40 -080051SkCodec* SkCodec::NewFromStream(SkStream* stream,
52 SkPngChunkReader* chunkReader) {
scroggof24f2242015-03-03 08:59:20 -080053 if (!stream) {
halcanary96fcdcc2015-08-27 07:41:13 -070054 return nullptr;
scroggof24f2242015-03-03 08:59:20 -080055 }
scroggo0a7e69c2015-04-03 07:22:22 -070056
Ben Wagner145dbcd2016-11-03 14:40:50 -040057 std::unique_ptr<SkStream> streamDeleter(stream);
scroggodb30be22015-12-08 18:54:13 -080058
59 // 14 is enough to read all of the supported types.
60 const size_t bytesToRead = 14;
61 SkASSERT(bytesToRead <= MinBufferedBytesNeeded());
62
63 char buffer[bytesToRead];
64 size_t bytesRead = stream->peek(buffer, bytesToRead);
65
66 // It is also possible to have a complete image less than bytesToRead bytes
67 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
68 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
69 // than bytesToRead, so pass that directly to the decoder.
70 // It also is possible the stream uses too small a buffer for peeking, but
71 // we trust the caller to use a large enough buffer.
72
73 if (0 == bytesRead) {
scroggo3ab9f2e2016-01-06 09:53:34 -080074 // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
75 // printf could be useful to notice failures.
76 // SkCodecPrintf("Encoded image data failed to peek!\n");
77
scroggodb30be22015-12-08 18:54:13 -080078 // It is possible the stream does not support peeking, but does support
79 // rewinding.
80 // Attempt to read() and pass the actual amount read to the decoder.
81 bytesRead = stream->read(buffer, bytesToRead);
82 if (!stream->rewind()) {
scroggo3ab9f2e2016-01-06 09:53:34 -080083 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
scroggodb30be22015-12-08 18:54:13 -080084 return nullptr;
85 }
86 }
87
scroggocf98fa92015-11-23 08:14:40 -080088 // PNG is special, since we want to be able to supply an SkPngChunkReader.
89 // But this code follows the same pattern as the loop.
msarettad3a5c62016-05-06 07:21:26 -070090#ifdef SK_HAS_PNG_LIBRARY
scroggodb30be22015-12-08 18:54:13 -080091 if (SkPngCodec::IsPng(buffer, bytesRead)) {
mtklein18300a32016-03-16 13:53:35 -070092 return SkPngCodec::NewFromStream(streamDeleter.release(), chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -080093 } else
94#endif
95 {
scroggocf98fa92015-11-23 08:14:40 -080096 for (DecoderProc proc : gDecoderProcs) {
scroggodb30be22015-12-08 18:54:13 -080097 if (proc.IsFormat(buffer, bytesRead)) {
mtklein18300a32016-03-16 13:53:35 -070098 return proc.NewFromStream(streamDeleter.release());
scroggocf98fa92015-11-23 08:14:40 -080099 }
msarett74114382015-03-16 11:55:18 -0700100 }
yujieqin916de9f2016-01-25 08:26:16 -0800101
102#ifdef SK_CODEC_DECODES_RAW
103 // Try to treat the input as RAW if all the other checks failed.
mtklein18300a32016-03-16 13:53:35 -0700104 return SkRawCodec::NewFromStream(streamDeleter.release());
yujieqin916de9f2016-01-25 08:26:16 -0800105#endif
scroggof24f2242015-03-03 08:59:20 -0800106 }
msarett8c8f22a2015-04-01 06:58:48 -0700107
msarettf44631b2016-01-13 10:54:20 -0800108 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800109}
110
reed42943c82016-09-12 12:01:44 -0700111SkCodec* SkCodec::NewFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800112 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700113 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800114 }
scroggocf98fa92015-11-23 08:14:40 -0800115 return NewFromStream(new SkMemoryStream(data), reader);
scroggof24f2242015-03-03 08:59:20 -0800116}
117
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400118SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info,
119 XformFormat srcFormat, SkStream* stream,
msarettc30c4182016-04-20 11:53:35 -0700120 sk_sp<SkColorSpace> colorSpace, Origin origin)
121 : fEncodedInfo(info)
msarett530c8442016-07-21 11:57:49 -0700122 , fSrcInfo(info.makeImageInfo(width, height, std::move(colorSpace)))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400123 , fSrcXformFormat(srcFormat)
scroggof24f2242015-03-03 08:59:20 -0800124 , fStream(stream)
125 , fNeedsRewind(false)
msarett0e6274f2016-03-21 08:04:40 -0700126 , fOrigin(origin)
scroggo46c57472015-09-30 08:57:13 -0700127 , fDstInfo()
128 , fOptions()
129 , fCurrScanline(-1)
scroggof24f2242015-03-03 08:59:20 -0800130{}
131
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400132SkCodec::SkCodec(const SkEncodedInfo& info, const SkImageInfo& imageInfo,
133 XformFormat srcFormat, SkStream* stream, Origin origin)
msarett549ca322016-08-17 08:54:08 -0700134 : fEncodedInfo(info)
135 , fSrcInfo(imageInfo)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400136 , fSrcXformFormat(srcFormat)
msarett549ca322016-08-17 08:54:08 -0700137 , fStream(stream)
138 , fNeedsRewind(false)
139 , fOrigin(origin)
140 , fDstInfo()
141 , fOptions()
142 , fCurrScanline(-1)
143{}
144
scroggo9b2cdbf42015-07-10 12:07:02 -0700145SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700146
scroggob427db12015-08-12 07:24:13 -0700147bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800148 // Store the value of fNeedsRewind so we can update it. Next read will
149 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700150 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800151 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700152 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700153 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700154 }
scroggob427db12015-08-12 07:24:13 -0700155
scroggo46c57472015-09-30 08:57:13 -0700156 // startScanlineDecode will need to be called before decoding scanlines.
157 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700158 // startIncrementalDecode will need to be called before incrementalDecode.
159 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700160
scroggo19b91532016-10-24 09:03:26 -0700161 // Some codecs do not have a stream. They may hold onto their own data or another codec.
162 // They must handle rewinding themselves.
163 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700164 return false;
165 }
166
167 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800168}
scroggo05245902015-03-25 11:11:52 -0700169
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400170static void zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
171 SkIRect frameRect) {
172 if (!frameRect.intersect(dstInfo.bounds())) {
173 return;
174 }
175 const auto info = dstInfo.makeWH(frameRect.width(), frameRect.height());
176 const size_t bpp = SkColorTypeBytesPerPixel(dstInfo.colorType());
177 const size_t offset = frameRect.x() * bpp + frameRect.y() * rowBytes;
178 auto* eraseDst = SkTAddOffset<void>(pixels, offset);
179 SkSampler::Fill(info, eraseDst, rowBytes, 0, SkCodec::kNo_ZeroInitialized);
180}
181
182SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
183 const Options& options) {
184 const int index = options.fFrameIndex;
185 if (0 == index) {
186 return kSuccess;
187 }
188
189 if (options.fSubset || info.dimensions() != fSrcInfo.dimensions()) {
190 // If we add support for these, we need to update the code that zeroes
191 // a kRestoreBGColor frame.
192 return kInvalidParameters;
193 }
194
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400195 if (index >= this->onGetFrameCount()) {
196 return kIncompleteInput;
197 }
198
199 const auto* frameHolder = this->getFrameHolder();
200 SkASSERT(frameHolder);
201
202 const auto* frame = frameHolder->getFrame(index);
203 SkASSERT(frame);
204
205 const int requiredFrame = frame->getRequiredFrame();
206 if (requiredFrame == kNone) {
207 return kSuccess;
208 }
209
210 if (options.fPriorFrame != kNone) {
211 // Check for a valid frame as a starting point. Alternatively, we could
212 // treat an invalid frame as not providing one, but rejecting it will
213 // make it easier to catch the mistake.
214 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
215 return kInvalidParameters;
216 }
217 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
218 switch (prevFrame->getDisposalMethod()) {
219 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
220 return kInvalidParameters;
221 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
222 // If a frame after the required frame is provided, there is no
223 // need to clear, since it must be covered by the desired frame.
224 if (options.fPriorFrame == requiredFrame) {
225 zero_rect(info, pixels, rowBytes, prevFrame->frameRect());
226 }
227 break;
228 default:
229 break;
230 }
231 return kSuccess;
232 }
233
234 Options prevFrameOptions(options);
235 prevFrameOptions.fFrameIndex = requiredFrame;
236 prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000237 const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400238 if (result == kSuccess) {
239 const auto* prevFrame = frameHolder->getFrame(requiredFrame);
240 const auto disposalMethod = prevFrame->getDisposalMethod();
241 if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
242 zero_rect(info, pixels, rowBytes, prevFrame->frameRect());
243 }
244 }
245
246 return result;
247}
scroggo8e6c7ad2016-09-16 08:20:38 -0700248
scroggoeb602a52015-07-09 08:16:03 -0700249SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000250 const Options* options) {
scroggoeb602a52015-07-09 08:16:03 -0700251 if (kUnknown_SkColorType == info.colorType()) {
252 return kInvalidConversion;
253 }
halcanary96fcdcc2015-08-27 07:41:13 -0700254 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700255 return kInvalidParameters;
256 }
257 if (rowBytes < info.minRowBytes()) {
258 return kInvalidParameters;
259 }
260
scroggo3a7701c2015-09-30 09:15:14 -0700261 if (!this->rewindIfNeeded()) {
262 return kCouldNotRewind;
263 }
264
scroggoeb602a52015-07-09 08:16:03 -0700265 // Default options.
266 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700267 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700268 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400269 } else {
270 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, *options);
271 if (frameIndexResult != kSuccess) {
272 return frameIndexResult;
273 }
274 if (options->fSubset) {
275 SkIRect subset(*options->fSubset);
276 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
277 // FIXME: How to differentiate between not supporting subset at all
278 // and not supporting this particular subset?
279 return kUnimplemented;
280 }
scroggoe7fc14b2015-10-02 13:14:46 -0700281 }
scroggoeb602a52015-07-09 08:16:03 -0700282 }
scroggoe7fc14b2015-10-02 13:14:46 -0700283
284 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
285 // because it supports arbitrary scaling/subset combinations.
286 if (!this->dimensionsSupported(info.dimensions())) {
287 return kInvalidScale;
288 }
289
scroggo8e6c7ad2016-09-16 08:20:38 -0700290 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500291 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700292
msarette6dd0042015-10-09 11:07:34 -0700293 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
294 // successfully.
295 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000296 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700297
298 // A return value of kIncompleteInput indicates a truncated image stream.
299 // In this case, we will fill any uninitialized memory with a default value.
300 // Some subclasses will take care of filling any uninitialized memory on
301 // their own. They indicate that all of the memory has been filled by
302 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400303 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500304 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
305 // there is a subset. In that case, it will use the width of the subset. From here, the
306 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
307 // differenty from the other codecs, and it needs to use the width specified by the info.
308 // Set the subset to null so SkWebpCodec uses the correct width.
309 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700310 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
311 rowsDecoded);
312 }
313
scroggoeb602a52015-07-09 08:16:03 -0700314 return result;
315}
316
scroggo8e6c7ad2016-09-16 08:20:38 -0700317SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000318 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700319 fStartedIncrementalDecode = false;
320
321 if (kUnknown_SkColorType == info.colorType()) {
322 return kInvalidConversion;
323 }
324 if (nullptr == pixels) {
325 return kInvalidParameters;
326 }
327
scroggo8e6c7ad2016-09-16 08:20:38 -0700328 // FIXME: If the rows come after the rows of a previous incremental decode,
329 // we might be able to skip the rewind, but only the implementation knows
330 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
331 // a bottom-up BMP could skip rewinding if the new rows are above the old
332 // rows.)
333 if (!this->rewindIfNeeded()) {
334 return kCouldNotRewind;
335 }
336
337 // Set options.
338 Options optsStorage;
339 if (nullptr == options) {
340 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400341 } else {
342 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, *options);
343 if (frameIndexResult != kSuccess) {
344 return frameIndexResult;
scroggo8e6c7ad2016-09-16 08:20:38 -0700345 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400346 if (options->fSubset) {
347 SkIRect size = SkIRect::MakeSize(info.dimensions());
348 if (!size.contains(*options->fSubset)) {
349 return kInvalidParameters;
350 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700351
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400352 const int top = options->fSubset->top();
353 const int bottom = options->fSubset->bottom();
354 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
355 return kInvalidParameters;
356 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700357 }
358 }
359
360 if (!this->dimensionsSupported(info.dimensions())) {
361 return kInvalidScale;
362 }
363
364 fDstInfo = info;
365 fOptions = *options;
366
Leon Scroggins571b30f2017-07-11 17:35:31 +0000367 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700368 if (kSuccess == result) {
369 fStartedIncrementalDecode = true;
370 } else if (kUnimplemented == result) {
371 // FIXME: This is temporarily necessary, until we transition SkCodec
372 // implementations from scanline decoding to incremental decoding.
373 // SkAndroidCodec will first attempt to use incremental decoding, but
374 // will fall back to scanline decoding if incremental returns
375 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
376 // (after potentially rewinding), but we do not want the next call to
377 // startScanlineDecode() to do a rewind.
378 fNeedsRewind = false;
379 }
380 return result;
381}
382
383
384SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000385 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700386 // Reset fCurrScanline in case of failure.
387 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700388
scroggo3a7701c2015-09-30 09:15:14 -0700389 if (!this->rewindIfNeeded()) {
390 return kCouldNotRewind;
391 }
392
scroggo46c57472015-09-30 08:57:13 -0700393 // Set options.
394 Options optsStorage;
395 if (nullptr == options) {
396 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700397 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700398 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700399 if (!size.contains(*options->fSubset)) {
400 return kInvalidInput;
401 }
402
403 // We only support subsetting in the x-dimension for scanline decoder.
404 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700405 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700406 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700407 }
408 }
409
410 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700411 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700412 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700413 }
414
Leon Scroggins571b30f2017-07-11 17:35:31 +0000415 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700416 if (result != SkCodec::kSuccess) {
417 return result;
418 }
419
420 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700421 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700422 fOptions = *options;
423 return kSuccess;
424}
425
msarette6dd0042015-10-09 11:07:34 -0700426int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700427 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700428 return 0;
scroggo46c57472015-09-30 08:57:13 -0700429 }
430
431 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700432 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700433 return 0;
scroggo46c57472015-09-30 08:57:13 -0700434 }
435
msarette6dd0042015-10-09 11:07:34 -0700436 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
437 if (linesDecoded < countLines) {
438 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
439 countLines, linesDecoded);
440 }
441 fCurrScanline += countLines;
442 return linesDecoded;
443}
444
445bool SkCodec::skipScanlines(int countLines) {
446 if (fCurrScanline < 0) {
447 return false;
448 }
449
450 SkASSERT(!fDstInfo.isEmpty());
451 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
452 // Arguably, we could just skip the scanlines which are remaining,
453 // and return true. We choose to return false so the client
454 // can catch their bug.
455 return false;
456 }
457
458 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700459 fCurrScanline += countLines;
460 return result;
461}
462
msarette6dd0042015-10-09 11:07:34 -0700463int SkCodec::outputScanline(int inputScanline) const {
464 SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height());
465 return this->onOutputScanline(inputScanline);
466}
scroggo46c57472015-09-30 08:57:13 -0700467
msarette6dd0042015-10-09 11:07:34 -0700468int SkCodec::onOutputScanline(int inputScanline) const {
469 switch (this->getScanlineOrder()) {
470 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700471 return inputScanline;
472 case kBottomUp_SkScanlineOrder:
473 return this->getInfo().height() - inputScanline - 1;
474 default:
475 // This case indicates an interlaced gif and is implemented by SkGifCodec.
476 SkASSERT(false);
477 return 0;
scroggo46c57472015-09-30 08:57:13 -0700478 }
msarette6dd0042015-10-09 11:07:34 -0700479}
scroggo46c57472015-09-30 08:57:13 -0700480
msarettf7eb6fc2016-09-13 09:04:11 -0700481uint64_t SkCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
482 switch (dstInfo.colorType()) {
483 case kRGBA_F16_SkColorType: {
484 static constexpr uint64_t transparentColor = 0;
485 static constexpr uint64_t opaqueColor = ((uint64_t) SK_Half1) << 48;
486 return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? opaqueColor : transparentColor;
487 }
488 default: {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000489 // This not only handles the kN32 case, but also k565, kGray8, since
msarettf7eb6fc2016-09-13 09:04:11 -0700490 // the low bits are zeros.
491 return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ?
492 SK_ColorBLACK : SK_ColorTRANSPARENT;
493 }
494 }
495}
496
msarette6dd0042015-10-09 11:07:34 -0700497static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes,
msarettf7eb6fc2016-09-13 09:04:11 -0700498 uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) {
msarette6dd0042015-10-09 11:07:34 -0700499 if (sampler) {
500 sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit);
501 } else {
502 SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit);
503 }
504}
505
506void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
507 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
508
509 void* fillDst;
msarettf7eb6fc2016-09-13 09:04:11 -0700510 const uint64_t fillValue = this->getFillValue(info);
msarette6dd0042015-10-09 11:07:34 -0700511 const int linesRemaining = linesRequested - linesDecoded;
512 SkSampler* sampler = this->getSampler(false);
513
msarett91c22b22016-02-22 12:27:46 -0800514 int fillWidth = info.width();
515 if (fOptions.fSubset) {
516 fillWidth = fOptions.fSubset->width();
517 }
518
msarette6dd0042015-10-09 11:07:34 -0700519 switch (this->getScanlineOrder()) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700520 case kTopDown_SkScanlineOrder: {
msarett91c22b22016-02-22 12:27:46 -0800521 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700522 fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes);
523 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
524 break;
525 }
526 case kBottomUp_SkScanlineOrder: {
527 fillDst = dst;
msarett91c22b22016-02-22 12:27:46 -0800528 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700529 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
530 break;
531 }
msarette6dd0042015-10-09 11:07:34 -0700532 }
scroggo46c57472015-09-30 08:57:13 -0700533}
Matt Sarett313c4632016-10-20 12:35:23 -0400534
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400535static inline SkColorSpaceXform::ColorFormat select_xform_format_ct(SkColorType colorType) {
536 switch (colorType) {
537 case kRGBA_8888_SkColorType:
538 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
539 case kBGRA_8888_SkColorType:
540 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
541 case kRGB_565_SkColorType:
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400542#ifdef SK_PMCOLOR_IS_RGBA
543 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
544#else
545 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
546#endif
547 default:
548 SkASSERT(false);
549 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
550 }
551}
552
Matt Sarettcf3f2342017-03-23 15:32:25 -0400553bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo,
554 SkTransferFunctionBehavior premulBehavior) {
Matt Sarett313c4632016-10-20 12:35:23 -0400555 fColorXform = nullptr;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400556 fXformOnDecode = false;
Matt Sarettcf3f2342017-03-23 15:32:25 -0400557 bool needsColorCorrectPremul = needs_premul(dstInfo, fEncodedInfo) &&
558 SkTransferFunctionBehavior::kRespect == premulBehavior;
559 if (needs_color_xform(dstInfo, fSrcInfo, needsColorCorrectPremul)) {
560 fColorXform = SkColorSpaceXform_Base::New(fSrcInfo.colorSpace(), dstInfo.colorSpace(),
561 premulBehavior);
Matt Sarett313c4632016-10-20 12:35:23 -0400562 if (!fColorXform) {
563 return false;
564 }
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400565
566 // We will apply the color xform when reading the color table unless F16 is requested.
567 fXformOnDecode = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
568 || kRGBA_F16_SkColorType == dstInfo.colorType();
569 if (fXformOnDecode) {
570 fDstXformFormat = select_xform_format(dstInfo.colorType());
571 } else {
572 fDstXformFormat = select_xform_format_ct(dstInfo.colorType());
573 }
Matt Sarett313c4632016-10-20 12:35:23 -0400574 }
575
576 return true;
577}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400578
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400579void SkCodec::applyColorXform(void* dst, const void* src, int count, SkAlphaType at) const {
580 SkASSERT(fColorXform);
581 SkAssertResult(fColorXform->apply(fDstXformFormat, dst,
582 fSrcXformFormat, src,
583 count, at));
584}
585
586void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
587 auto alphaType = select_xform_alpha(fDstInfo.alphaType(), fSrcInfo.alphaType());
588 this->applyColorXform(dst, src, count, alphaType);
589}
590
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400591std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400592 const int frameCount = this->getFrameCount();
593 SkASSERT(frameCount >= 0);
594 if (frameCount <= 0) {
595 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400596 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400597
598 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
599 // Not animated.
600 return std::vector<FrameInfo>{};
601 }
602
603 std::vector<FrameInfo> result(frameCount);
604 for (int i = 0; i < frameCount; ++i) {
605 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
606 }
607 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400608}