blob: 872fe2b6893277a819e73cf35179927f9a63faab [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);
Leon Scroggins III588fb042017-07-14 16:32:31 -040029 SkCodec* (*NewFromStream)(SkStream*, SkCodec::Result*);
msarett74114382015-03-16 11:55:18 -070030};
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,
Leon Scroggins III588fb042017-07-14 16:32:31 -040052 Result* outResult, SkPngChunkReader* chunkReader) {
53 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
Ben Wagner145dbcd2016-11-03 14:40:50 -040063 std::unique_ptr<SkStream> streamDeleter(stream);
scroggodb30be22015-12-08 18:54:13 -080064
65 // 14 is enough to read all of the supported types.
66 const size_t bytesToRead = 14;
67 SkASSERT(bytesToRead <= MinBufferedBytesNeeded());
68
69 char buffer[bytesToRead];
70 size_t bytesRead = stream->peek(buffer, bytesToRead);
71
72 // It is also possible to have a complete image less than bytesToRead bytes
73 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
74 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
75 // than bytesToRead, so pass that directly to the decoder.
76 // It also is possible the stream uses too small a buffer for peeking, but
77 // we trust the caller to use a large enough buffer.
78
79 if (0 == bytesRead) {
scroggo3ab9f2e2016-01-06 09:53:34 -080080 // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
81 // printf could be useful to notice failures.
82 // SkCodecPrintf("Encoded image data failed to peek!\n");
83
scroggodb30be22015-12-08 18:54:13 -080084 // It is possible the stream does not support peeking, but does support
85 // rewinding.
86 // Attempt to read() and pass the actual amount read to the decoder.
87 bytesRead = stream->read(buffer, bytesToRead);
88 if (!stream->rewind()) {
scroggo3ab9f2e2016-01-06 09:53:34 -080089 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -040090 *outResult = kCouldNotRewind;
scroggodb30be22015-12-08 18:54:13 -080091 return nullptr;
92 }
93 }
94
scroggocf98fa92015-11-23 08:14:40 -080095 // PNG is special, since we want to be able to supply an SkPngChunkReader.
96 // But this code follows the same pattern as the loop.
msarettad3a5c62016-05-06 07:21:26 -070097#ifdef SK_HAS_PNG_LIBRARY
scroggodb30be22015-12-08 18:54:13 -080098 if (SkPngCodec::IsPng(buffer, bytesRead)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040099 return SkPngCodec::NewFromStream(streamDeleter.release(), outResult, chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -0800100 } else
101#endif
102 {
scroggocf98fa92015-11-23 08:14:40 -0800103 for (DecoderProc proc : gDecoderProcs) {
scroggodb30be22015-12-08 18:54:13 -0800104 if (proc.IsFormat(buffer, bytesRead)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400105 return proc.NewFromStream(streamDeleter.release(), outResult);
scroggocf98fa92015-11-23 08:14:40 -0800106 }
msarett74114382015-03-16 11:55:18 -0700107 }
yujieqin916de9f2016-01-25 08:26:16 -0800108
109#ifdef SK_CODEC_DECODES_RAW
110 // Try to treat the input as RAW if all the other checks failed.
Leon Scroggins III588fb042017-07-14 16:32:31 -0400111 return SkRawCodec::NewFromStream(streamDeleter.release(), outResult);
yujieqin916de9f2016-01-25 08:26:16 -0800112#endif
scroggof24f2242015-03-03 08:59:20 -0800113 }
msarett8c8f22a2015-04-01 06:58:48 -0700114
Leon Scroggins III588fb042017-07-14 16:32:31 -0400115 if (bytesRead < bytesToRead) {
116 *outResult = kIncompleteInput;
117 } else {
118 *outResult = kUnimplemented;
119 }
120
msarettf44631b2016-01-13 10:54:20 -0800121 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800122}
123
reed42943c82016-09-12 12:01:44 -0700124SkCodec* SkCodec::NewFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800125 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700126 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800127 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400128 return NewFromStream(new SkMemoryStream(data), nullptr, reader);
scroggof24f2242015-03-03 08:59:20 -0800129}
130
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400131SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info,
132 XformFormat srcFormat, SkStream* stream,
msarettc30c4182016-04-20 11:53:35 -0700133 sk_sp<SkColorSpace> colorSpace, Origin origin)
134 : fEncodedInfo(info)
msarett530c8442016-07-21 11:57:49 -0700135 , fSrcInfo(info.makeImageInfo(width, height, std::move(colorSpace)))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400136 , fSrcXformFormat(srcFormat)
scroggof24f2242015-03-03 08:59:20 -0800137 , fStream(stream)
138 , fNeedsRewind(false)
msarett0e6274f2016-03-21 08:04:40 -0700139 , fOrigin(origin)
scroggo46c57472015-09-30 08:57:13 -0700140 , fDstInfo()
141 , fOptions()
142 , fCurrScanline(-1)
scroggof24f2242015-03-03 08:59:20 -0800143{}
144
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400145SkCodec::SkCodec(const SkEncodedInfo& info, const SkImageInfo& imageInfo,
146 XformFormat srcFormat, SkStream* stream, Origin origin)
msarett549ca322016-08-17 08:54:08 -0700147 : fEncodedInfo(info)
148 , fSrcInfo(imageInfo)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400149 , fSrcXformFormat(srcFormat)
msarett549ca322016-08-17 08:54:08 -0700150 , fStream(stream)
151 , fNeedsRewind(false)
152 , fOrigin(origin)
153 , fDstInfo()
154 , fOptions()
155 , fCurrScanline(-1)
156{}
157
scroggo9b2cdbf42015-07-10 12:07:02 -0700158SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700159
scroggob427db12015-08-12 07:24:13 -0700160bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800161 // Store the value of fNeedsRewind so we can update it. Next read will
162 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700163 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800164 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700165 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700166 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700167 }
scroggob427db12015-08-12 07:24:13 -0700168
scroggo46c57472015-09-30 08:57:13 -0700169 // startScanlineDecode will need to be called before decoding scanlines.
170 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700171 // startIncrementalDecode will need to be called before incrementalDecode.
172 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700173
scroggo19b91532016-10-24 09:03:26 -0700174 // Some codecs do not have a stream. They may hold onto their own data or another codec.
175 // They must handle rewinding themselves.
176 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700177 return false;
178 }
179
180 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800181}
scroggo05245902015-03-25 11:11:52 -0700182
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400183static void zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
184 SkIRect frameRect) {
185 if (!frameRect.intersect(dstInfo.bounds())) {
186 return;
187 }
188 const auto info = dstInfo.makeWH(frameRect.width(), frameRect.height());
189 const size_t bpp = SkColorTypeBytesPerPixel(dstInfo.colorType());
190 const size_t offset = frameRect.x() * bpp + frameRect.y() * rowBytes;
191 auto* eraseDst = SkTAddOffset<void>(pixels, offset);
192 SkSampler::Fill(info, eraseDst, rowBytes, 0, SkCodec::kNo_ZeroInitialized);
193}
194
195SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
196 const Options& options) {
197 const int index = options.fFrameIndex;
198 if (0 == index) {
199 return kSuccess;
200 }
201
202 if (options.fSubset || info.dimensions() != fSrcInfo.dimensions()) {
203 // If we add support for these, we need to update the code that zeroes
204 // a kRestoreBGColor frame.
205 return kInvalidParameters;
206 }
207
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400208 if (index >= this->onGetFrameCount()) {
209 return kIncompleteInput;
210 }
211
212 const auto* frameHolder = this->getFrameHolder();
213 SkASSERT(frameHolder);
214
215 const auto* frame = frameHolder->getFrame(index);
216 SkASSERT(frame);
217
218 const int requiredFrame = frame->getRequiredFrame();
219 if (requiredFrame == kNone) {
220 return kSuccess;
221 }
222
223 if (options.fPriorFrame != kNone) {
224 // Check for a valid frame as a starting point. Alternatively, we could
225 // treat an invalid frame as not providing one, but rejecting it will
226 // make it easier to catch the mistake.
227 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
228 return kInvalidParameters;
229 }
230 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
231 switch (prevFrame->getDisposalMethod()) {
232 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
233 return kInvalidParameters;
234 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
235 // If a frame after the required frame is provided, there is no
236 // need to clear, since it must be covered by the desired frame.
237 if (options.fPriorFrame == requiredFrame) {
238 zero_rect(info, pixels, rowBytes, prevFrame->frameRect());
239 }
240 break;
241 default:
242 break;
243 }
244 return kSuccess;
245 }
246
247 Options prevFrameOptions(options);
248 prevFrameOptions.fFrameIndex = requiredFrame;
249 prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000250 const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400251 if (result == kSuccess) {
252 const auto* prevFrame = frameHolder->getFrame(requiredFrame);
253 const auto disposalMethod = prevFrame->getDisposalMethod();
254 if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
255 zero_rect(info, pixels, rowBytes, prevFrame->frameRect());
256 }
257 }
258
259 return result;
260}
scroggo8e6c7ad2016-09-16 08:20:38 -0700261
scroggoeb602a52015-07-09 08:16:03 -0700262SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000263 const Options* options) {
scroggoeb602a52015-07-09 08:16:03 -0700264 if (kUnknown_SkColorType == info.colorType()) {
265 return kInvalidConversion;
266 }
halcanary96fcdcc2015-08-27 07:41:13 -0700267 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700268 return kInvalidParameters;
269 }
270 if (rowBytes < info.minRowBytes()) {
271 return kInvalidParameters;
272 }
273
scroggo3a7701c2015-09-30 09:15:14 -0700274 if (!this->rewindIfNeeded()) {
275 return kCouldNotRewind;
276 }
277
scroggoeb602a52015-07-09 08:16:03 -0700278 // Default options.
279 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700280 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700281 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400282 } else {
283 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, *options);
284 if (frameIndexResult != kSuccess) {
285 return frameIndexResult;
286 }
287 if (options->fSubset) {
288 SkIRect subset(*options->fSubset);
289 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
290 // FIXME: How to differentiate between not supporting subset at all
291 // and not supporting this particular subset?
292 return kUnimplemented;
293 }
scroggoe7fc14b2015-10-02 13:14:46 -0700294 }
scroggoeb602a52015-07-09 08:16:03 -0700295 }
scroggoe7fc14b2015-10-02 13:14:46 -0700296
297 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
298 // because it supports arbitrary scaling/subset combinations.
299 if (!this->dimensionsSupported(info.dimensions())) {
300 return kInvalidScale;
301 }
302
scroggo8e6c7ad2016-09-16 08:20:38 -0700303 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500304 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700305
msarette6dd0042015-10-09 11:07:34 -0700306 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
307 // successfully.
308 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000309 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700310
311 // A return value of kIncompleteInput indicates a truncated image stream.
312 // In this case, we will fill any uninitialized memory with a default value.
313 // Some subclasses will take care of filling any uninitialized memory on
314 // their own. They indicate that all of the memory has been filled by
315 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400316 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500317 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
318 // there is a subset. In that case, it will use the width of the subset. From here, the
319 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
320 // differenty from the other codecs, and it needs to use the width specified by the info.
321 // Set the subset to null so SkWebpCodec uses the correct width.
322 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700323 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
324 rowsDecoded);
325 }
326
scroggoeb602a52015-07-09 08:16:03 -0700327 return result;
328}
329
scroggo8e6c7ad2016-09-16 08:20:38 -0700330SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000331 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700332 fStartedIncrementalDecode = false;
333
334 if (kUnknown_SkColorType == info.colorType()) {
335 return kInvalidConversion;
336 }
337 if (nullptr == pixels) {
338 return kInvalidParameters;
339 }
340
scroggo8e6c7ad2016-09-16 08:20:38 -0700341 // FIXME: If the rows come after the rows of a previous incremental decode,
342 // we might be able to skip the rewind, but only the implementation knows
343 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
344 // a bottom-up BMP could skip rewinding if the new rows are above the old
345 // rows.)
346 if (!this->rewindIfNeeded()) {
347 return kCouldNotRewind;
348 }
349
350 // Set options.
351 Options optsStorage;
352 if (nullptr == options) {
353 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400354 } else {
355 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, *options);
356 if (frameIndexResult != kSuccess) {
357 return frameIndexResult;
scroggo8e6c7ad2016-09-16 08:20:38 -0700358 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400359 if (options->fSubset) {
360 SkIRect size = SkIRect::MakeSize(info.dimensions());
361 if (!size.contains(*options->fSubset)) {
362 return kInvalidParameters;
363 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700364
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400365 const int top = options->fSubset->top();
366 const int bottom = options->fSubset->bottom();
367 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
368 return kInvalidParameters;
369 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700370 }
371 }
372
373 if (!this->dimensionsSupported(info.dimensions())) {
374 return kInvalidScale;
375 }
376
377 fDstInfo = info;
378 fOptions = *options;
379
Leon Scroggins571b30f2017-07-11 17:35:31 +0000380 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700381 if (kSuccess == result) {
382 fStartedIncrementalDecode = true;
383 } else if (kUnimplemented == result) {
384 // FIXME: This is temporarily necessary, until we transition SkCodec
385 // implementations from scanline decoding to incremental decoding.
386 // SkAndroidCodec will first attempt to use incremental decoding, but
387 // will fall back to scanline decoding if incremental returns
388 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
389 // (after potentially rewinding), but we do not want the next call to
390 // startScanlineDecode() to do a rewind.
391 fNeedsRewind = false;
392 }
393 return result;
394}
395
396
397SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000398 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700399 // Reset fCurrScanline in case of failure.
400 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700401
scroggo3a7701c2015-09-30 09:15:14 -0700402 if (!this->rewindIfNeeded()) {
403 return kCouldNotRewind;
404 }
405
scroggo46c57472015-09-30 08:57:13 -0700406 // Set options.
407 Options optsStorage;
408 if (nullptr == options) {
409 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700410 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700411 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700412 if (!size.contains(*options->fSubset)) {
413 return kInvalidInput;
414 }
415
416 // We only support subsetting in the x-dimension for scanline decoder.
417 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700418 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700419 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700420 }
421 }
422
423 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700424 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700425 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700426 }
427
Leon Scroggins571b30f2017-07-11 17:35:31 +0000428 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700429 if (result != SkCodec::kSuccess) {
430 return result;
431 }
432
433 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700434 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700435 fOptions = *options;
436 return kSuccess;
437}
438
msarette6dd0042015-10-09 11:07:34 -0700439int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700440 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700441 return 0;
scroggo46c57472015-09-30 08:57:13 -0700442 }
443
444 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700445 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700446 return 0;
scroggo46c57472015-09-30 08:57:13 -0700447 }
448
msarette6dd0042015-10-09 11:07:34 -0700449 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
450 if (linesDecoded < countLines) {
451 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
452 countLines, linesDecoded);
453 }
454 fCurrScanline += countLines;
455 return linesDecoded;
456}
457
458bool SkCodec::skipScanlines(int countLines) {
459 if (fCurrScanline < 0) {
460 return false;
461 }
462
463 SkASSERT(!fDstInfo.isEmpty());
464 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
465 // Arguably, we could just skip the scanlines which are remaining,
466 // and return true. We choose to return false so the client
467 // can catch their bug.
468 return false;
469 }
470
471 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700472 fCurrScanline += countLines;
473 return result;
474}
475
msarette6dd0042015-10-09 11:07:34 -0700476int SkCodec::outputScanline(int inputScanline) const {
477 SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height());
478 return this->onOutputScanline(inputScanline);
479}
scroggo46c57472015-09-30 08:57:13 -0700480
msarette6dd0042015-10-09 11:07:34 -0700481int SkCodec::onOutputScanline(int inputScanline) const {
482 switch (this->getScanlineOrder()) {
483 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700484 return inputScanline;
485 case kBottomUp_SkScanlineOrder:
486 return this->getInfo().height() - inputScanline - 1;
487 default:
488 // This case indicates an interlaced gif and is implemented by SkGifCodec.
489 SkASSERT(false);
490 return 0;
scroggo46c57472015-09-30 08:57:13 -0700491 }
msarette6dd0042015-10-09 11:07:34 -0700492}
scroggo46c57472015-09-30 08:57:13 -0700493
msarettf7eb6fc2016-09-13 09:04:11 -0700494uint64_t SkCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
495 switch (dstInfo.colorType()) {
496 case kRGBA_F16_SkColorType: {
497 static constexpr uint64_t transparentColor = 0;
498 static constexpr uint64_t opaqueColor = ((uint64_t) SK_Half1) << 48;
499 return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? opaqueColor : transparentColor;
500 }
501 default: {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000502 // This not only handles the kN32 case, but also k565, kGray8, since
msarettf7eb6fc2016-09-13 09:04:11 -0700503 // the low bits are zeros.
504 return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ?
505 SK_ColorBLACK : SK_ColorTRANSPARENT;
506 }
507 }
508}
509
msarette6dd0042015-10-09 11:07:34 -0700510static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes,
msarettf7eb6fc2016-09-13 09:04:11 -0700511 uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) {
msarette6dd0042015-10-09 11:07:34 -0700512 if (sampler) {
513 sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit);
514 } else {
515 SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit);
516 }
517}
518
519void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
520 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
521
522 void* fillDst;
msarettf7eb6fc2016-09-13 09:04:11 -0700523 const uint64_t fillValue = this->getFillValue(info);
msarette6dd0042015-10-09 11:07:34 -0700524 const int linesRemaining = linesRequested - linesDecoded;
525 SkSampler* sampler = this->getSampler(false);
526
msarett91c22b22016-02-22 12:27:46 -0800527 int fillWidth = info.width();
528 if (fOptions.fSubset) {
529 fillWidth = fOptions.fSubset->width();
530 }
531
msarette6dd0042015-10-09 11:07:34 -0700532 switch (this->getScanlineOrder()) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700533 case kTopDown_SkScanlineOrder: {
msarett91c22b22016-02-22 12:27:46 -0800534 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700535 fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes);
536 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
537 break;
538 }
539 case kBottomUp_SkScanlineOrder: {
540 fillDst = dst;
msarett91c22b22016-02-22 12:27:46 -0800541 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700542 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
543 break;
544 }
msarette6dd0042015-10-09 11:07:34 -0700545 }
scroggo46c57472015-09-30 08:57:13 -0700546}
Matt Sarett313c4632016-10-20 12:35:23 -0400547
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400548static inline SkColorSpaceXform::ColorFormat select_xform_format_ct(SkColorType colorType) {
549 switch (colorType) {
550 case kRGBA_8888_SkColorType:
551 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
552 case kBGRA_8888_SkColorType:
553 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
554 case kRGB_565_SkColorType:
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400555#ifdef SK_PMCOLOR_IS_RGBA
556 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
557#else
558 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
559#endif
560 default:
561 SkASSERT(false);
562 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
563 }
564}
565
Matt Sarettcf3f2342017-03-23 15:32:25 -0400566bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo,
567 SkTransferFunctionBehavior premulBehavior) {
Matt Sarett313c4632016-10-20 12:35:23 -0400568 fColorXform = nullptr;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400569 fXformOnDecode = false;
Matt Sarettcf3f2342017-03-23 15:32:25 -0400570 bool needsColorCorrectPremul = needs_premul(dstInfo, fEncodedInfo) &&
571 SkTransferFunctionBehavior::kRespect == premulBehavior;
572 if (needs_color_xform(dstInfo, fSrcInfo, needsColorCorrectPremul)) {
573 fColorXform = SkColorSpaceXform_Base::New(fSrcInfo.colorSpace(), dstInfo.colorSpace(),
574 premulBehavior);
Matt Sarett313c4632016-10-20 12:35:23 -0400575 if (!fColorXform) {
576 return false;
577 }
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400578
579 // We will apply the color xform when reading the color table unless F16 is requested.
580 fXformOnDecode = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
581 || kRGBA_F16_SkColorType == dstInfo.colorType();
582 if (fXformOnDecode) {
583 fDstXformFormat = select_xform_format(dstInfo.colorType());
584 } else {
585 fDstXformFormat = select_xform_format_ct(dstInfo.colorType());
586 }
Matt Sarett313c4632016-10-20 12:35:23 -0400587 }
588
589 return true;
590}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400591
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400592void SkCodec::applyColorXform(void* dst, const void* src, int count, SkAlphaType at) const {
593 SkASSERT(fColorXform);
594 SkAssertResult(fColorXform->apply(fDstXformFormat, dst,
595 fSrcXformFormat, src,
596 count, at));
597}
598
599void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
600 auto alphaType = select_xform_alpha(fDstInfo.alphaType(), fSrcInfo.alphaType());
601 this->applyColorXform(dst, src, count, alphaType);
602}
603
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400604std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400605 const int frameCount = this->getFrameCount();
606 SkASSERT(frameCount >= 0);
607 if (frameCount <= 0) {
608 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400609 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400610
611 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
612 // Not animated.
613 return std::vector<FrameInfo>{};
614 }
615
616 std::vector<FrameInfo> result(frameCount);
617 for (int i = 0; i < frameCount; ++i) {
618 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
619 }
620 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400621}