blob: 09343e2dffd5119ecd44d4cadccfc6195108ff7c [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);
Mike Reedede7bac2017-07-23 15:30:02 -040029 std::unique_ptr<SkCodec> (*MakeFromStream)(std::unique_ptr<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
Mike Reedede7bac2017-07-23 15:30:02 -040034 { SkJpegCodec::IsJpeg, SkJpegCodec::MakeFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080035#endif
msarettad3a5c62016-05-06 07:21:26 -070036#ifdef SK_HAS_WEBP_LIBRARY
Mike Reedede7bac2017-07-23 15:30:02 -040037 { SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080038#endif
Mike Reedede7bac2017-07-23 15:30:02 -040039 { SkGifCodec::IsGif, SkGifCodec::MakeFromStream },
msarettad3a5c62016-05-06 07:21:26 -070040#ifdef SK_HAS_PNG_LIBRARY
Mike Reedede7bac2017-07-23 15:30:02 -040041 { SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080042#endif
Mike Reedede7bac2017-07-23 15:30:02 -040043 { SkBmpCodec::IsBmp, SkBmpCodec::MakeFromStream },
44 { SkWbmpCodec::IsWbmp, SkWbmpCodec::MakeFromStream }
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
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
scroggodb30be22015-12-08 18:54:13 -080063 // 14 is enough to read all of the supported types.
64 const size_t bytesToRead = 14;
65 SkASSERT(bytesToRead <= MinBufferedBytesNeeded());
66
67 char buffer[bytesToRead];
68 size_t bytesRead = stream->peek(buffer, bytesToRead);
69
70 // It is also possible to have a complete image less than bytesToRead bytes
71 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
72 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
73 // than bytesToRead, so pass that directly to the decoder.
74 // It also is possible the stream uses too small a buffer for peeking, but
75 // we trust the caller to use a large enough buffer.
76
77 if (0 == bytesRead) {
scroggo3ab9f2e2016-01-06 09:53:34 -080078 // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
79 // printf could be useful to notice failures.
80 // SkCodecPrintf("Encoded image data failed to peek!\n");
81
scroggodb30be22015-12-08 18:54:13 -080082 // It is possible the stream does not support peeking, but does support
83 // rewinding.
84 // Attempt to read() and pass the actual amount read to the decoder.
85 bytesRead = stream->read(buffer, bytesToRead);
86 if (!stream->rewind()) {
scroggo3ab9f2e2016-01-06 09:53:34 -080087 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -040088 *outResult = kCouldNotRewind;
scroggodb30be22015-12-08 18:54:13 -080089 return nullptr;
90 }
91 }
92
scroggocf98fa92015-11-23 08:14:40 -080093 // PNG is special, since we want to be able to supply an SkPngChunkReader.
94 // But this code follows the same pattern as the loop.
msarettad3a5c62016-05-06 07:21:26 -070095#ifdef SK_HAS_PNG_LIBRARY
scroggodb30be22015-12-08 18:54:13 -080096 if (SkPngCodec::IsPng(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -040097 return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -080098 } else
99#endif
100 {
scroggocf98fa92015-11-23 08:14:40 -0800101 for (DecoderProc proc : gDecoderProcs) {
scroggodb30be22015-12-08 18:54:13 -0800102 if (proc.IsFormat(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400103 return proc.MakeFromStream(std::move(stream), outResult);
scroggocf98fa92015-11-23 08:14:40 -0800104 }
msarett74114382015-03-16 11:55:18 -0700105 }
yujieqin916de9f2016-01-25 08:26:16 -0800106
107#ifdef SK_CODEC_DECODES_RAW
108 // Try to treat the input as RAW if all the other checks failed.
Mike Reedede7bac2017-07-23 15:30:02 -0400109 return SkRawCodec::MakeFromStream(std::move(stream), outResult);
yujieqin916de9f2016-01-25 08:26:16 -0800110#endif
scroggof24f2242015-03-03 08:59:20 -0800111 }
msarett8c8f22a2015-04-01 06:58:48 -0700112
Leon Scroggins III588fb042017-07-14 16:32:31 -0400113 if (bytesRead < bytesToRead) {
114 *outResult = kIncompleteInput;
115 } else {
116 *outResult = kUnimplemented;
117 }
118
msarettf44631b2016-01-13 10:54:20 -0800119 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800120}
121
Mike Reedede7bac2017-07-23 15:30:02 -0400122std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800123 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700124 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800125 }
Mike Reed847068c2017-07-26 11:35:53 -0400126 return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
scroggof24f2242015-03-03 08:59:20 -0800127}
128
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400129SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info,
Mike Reedede7bac2017-07-23 15:30:02 -0400130 XformFormat srcFormat, std::unique_ptr<SkStream> stream,
msarettc30c4182016-04-20 11:53:35 -0700131 sk_sp<SkColorSpace> colorSpace, Origin origin)
132 : fEncodedInfo(info)
msarett530c8442016-07-21 11:57:49 -0700133 , fSrcInfo(info.makeImageInfo(width, height, std::move(colorSpace)))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400134 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400135 , fStream(std::move(stream))
scroggof24f2242015-03-03 08:59:20 -0800136 , fNeedsRewind(false)
msarett0e6274f2016-03-21 08:04:40 -0700137 , fOrigin(origin)
scroggo46c57472015-09-30 08:57:13 -0700138 , fDstInfo()
139 , fOptions()
140 , fCurrScanline(-1)
scroggof24f2242015-03-03 08:59:20 -0800141{}
142
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400143SkCodec::SkCodec(const SkEncodedInfo& info, const SkImageInfo& imageInfo,
Mike Reedede7bac2017-07-23 15:30:02 -0400144 XformFormat srcFormat, std::unique_ptr<SkStream> stream, Origin origin)
msarett549ca322016-08-17 08:54:08 -0700145 : fEncodedInfo(info)
146 , fSrcInfo(imageInfo)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400147 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400148 , fStream(std::move(stream))
msarett549ca322016-08-17 08:54:08 -0700149 , fNeedsRewind(false)
150 , fOrigin(origin)
151 , fDstInfo()
152 , fOptions()
153 , fCurrScanline(-1)
154{}
155
scroggo9b2cdbf42015-07-10 12:07:02 -0700156SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700157
scroggob427db12015-08-12 07:24:13 -0700158bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800159 // Store the value of fNeedsRewind so we can update it. Next read will
160 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700161 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800162 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700163 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700164 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700165 }
scroggob427db12015-08-12 07:24:13 -0700166
scroggo46c57472015-09-30 08:57:13 -0700167 // startScanlineDecode will need to be called before decoding scanlines.
168 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700169 // startIncrementalDecode will need to be called before incrementalDecode.
170 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700171
scroggo19b91532016-10-24 09:03:26 -0700172 // Some codecs do not have a stream. They may hold onto their own data or another codec.
173 // They must handle rewinding themselves.
174 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700175 return false;
176 }
177
178 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800179}
scroggo05245902015-03-25 11:11:52 -0700180
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400181static void zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
182 SkIRect frameRect) {
183 if (!frameRect.intersect(dstInfo.bounds())) {
184 return;
185 }
186 const auto info = dstInfo.makeWH(frameRect.width(), frameRect.height());
187 const size_t bpp = SkColorTypeBytesPerPixel(dstInfo.colorType());
188 const size_t offset = frameRect.x() * bpp + frameRect.y() * rowBytes;
189 auto* eraseDst = SkTAddOffset<void>(pixels, offset);
190 SkSampler::Fill(info, eraseDst, rowBytes, 0, SkCodec::kNo_ZeroInitialized);
191}
192
193SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
194 const Options& options) {
195 const int index = options.fFrameIndex;
196 if (0 == index) {
197 return kSuccess;
198 }
199
200 if (options.fSubset || info.dimensions() != fSrcInfo.dimensions()) {
201 // If we add support for these, we need to update the code that zeroes
202 // a kRestoreBGColor frame.
203 return kInvalidParameters;
204 }
205
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400206 if (index >= this->onGetFrameCount()) {
207 return kIncompleteInput;
208 }
209
210 const auto* frameHolder = this->getFrameHolder();
211 SkASSERT(frameHolder);
212
213 const auto* frame = frameHolder->getFrame(index);
214 SkASSERT(frame);
215
216 const int requiredFrame = frame->getRequiredFrame();
217 if (requiredFrame == kNone) {
218 return kSuccess;
219 }
220
221 if (options.fPriorFrame != kNone) {
222 // Check for a valid frame as a starting point. Alternatively, we could
223 // treat an invalid frame as not providing one, but rejecting it will
224 // make it easier to catch the mistake.
225 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
226 return kInvalidParameters;
227 }
228 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
229 switch (prevFrame->getDisposalMethod()) {
230 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
231 return kInvalidParameters;
232 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
233 // If a frame after the required frame is provided, there is no
234 // need to clear, since it must be covered by the desired frame.
235 if (options.fPriorFrame == requiredFrame) {
236 zero_rect(info, pixels, rowBytes, prevFrame->frameRect());
237 }
238 break;
239 default:
240 break;
241 }
242 return kSuccess;
243 }
244
245 Options prevFrameOptions(options);
246 prevFrameOptions.fFrameIndex = requiredFrame;
247 prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000248 const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400249 if (result == kSuccess) {
250 const auto* prevFrame = frameHolder->getFrame(requiredFrame);
251 const auto disposalMethod = prevFrame->getDisposalMethod();
252 if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
253 zero_rect(info, pixels, rowBytes, prevFrame->frameRect());
254 }
255 }
256
257 return result;
258}
scroggo8e6c7ad2016-09-16 08:20:38 -0700259
scroggoeb602a52015-07-09 08:16:03 -0700260SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000261 const Options* options) {
scroggoeb602a52015-07-09 08:16:03 -0700262 if (kUnknown_SkColorType == info.colorType()) {
263 return kInvalidConversion;
264 }
halcanary96fcdcc2015-08-27 07:41:13 -0700265 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700266 return kInvalidParameters;
267 }
268 if (rowBytes < info.minRowBytes()) {
269 return kInvalidParameters;
270 }
271
scroggo3a7701c2015-09-30 09:15:14 -0700272 if (!this->rewindIfNeeded()) {
273 return kCouldNotRewind;
274 }
275
scroggoeb602a52015-07-09 08:16:03 -0700276 // Default options.
277 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700278 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700279 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400280 } else {
281 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, *options);
282 if (frameIndexResult != kSuccess) {
283 return frameIndexResult;
284 }
285 if (options->fSubset) {
286 SkIRect subset(*options->fSubset);
287 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
288 // FIXME: How to differentiate between not supporting subset at all
289 // and not supporting this particular subset?
290 return kUnimplemented;
291 }
scroggoe7fc14b2015-10-02 13:14:46 -0700292 }
scroggoeb602a52015-07-09 08:16:03 -0700293 }
scroggoe7fc14b2015-10-02 13:14:46 -0700294
295 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
296 // because it supports arbitrary scaling/subset combinations.
297 if (!this->dimensionsSupported(info.dimensions())) {
298 return kInvalidScale;
299 }
300
scroggo8e6c7ad2016-09-16 08:20:38 -0700301 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500302 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700303
msarette6dd0042015-10-09 11:07:34 -0700304 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
305 // successfully.
306 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000307 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700308
309 // A return value of kIncompleteInput indicates a truncated image stream.
310 // In this case, we will fill any uninitialized memory with a default value.
311 // Some subclasses will take care of filling any uninitialized memory on
312 // their own. They indicate that all of the memory has been filled by
313 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400314 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500315 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
316 // there is a subset. In that case, it will use the width of the subset. From here, the
317 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
318 // differenty from the other codecs, and it needs to use the width specified by the info.
319 // Set the subset to null so SkWebpCodec uses the correct width.
320 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700321 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
322 rowsDecoded);
323 }
324
scroggoeb602a52015-07-09 08:16:03 -0700325 return result;
326}
327
scroggo8e6c7ad2016-09-16 08:20:38 -0700328SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000329 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700330 fStartedIncrementalDecode = false;
331
332 if (kUnknown_SkColorType == info.colorType()) {
333 return kInvalidConversion;
334 }
335 if (nullptr == pixels) {
336 return kInvalidParameters;
337 }
338
scroggo8e6c7ad2016-09-16 08:20:38 -0700339 // FIXME: If the rows come after the rows of a previous incremental decode,
340 // we might be able to skip the rewind, but only the implementation knows
341 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
342 // a bottom-up BMP could skip rewinding if the new rows are above the old
343 // rows.)
344 if (!this->rewindIfNeeded()) {
345 return kCouldNotRewind;
346 }
347
348 // Set options.
349 Options optsStorage;
350 if (nullptr == options) {
351 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400352 } else {
353 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, *options);
354 if (frameIndexResult != kSuccess) {
355 return frameIndexResult;
scroggo8e6c7ad2016-09-16 08:20:38 -0700356 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400357 if (options->fSubset) {
358 SkIRect size = SkIRect::MakeSize(info.dimensions());
359 if (!size.contains(*options->fSubset)) {
360 return kInvalidParameters;
361 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700362
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400363 const int top = options->fSubset->top();
364 const int bottom = options->fSubset->bottom();
365 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
366 return kInvalidParameters;
367 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700368 }
369 }
370
371 if (!this->dimensionsSupported(info.dimensions())) {
372 return kInvalidScale;
373 }
374
375 fDstInfo = info;
376 fOptions = *options;
377
Leon Scroggins571b30f2017-07-11 17:35:31 +0000378 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700379 if (kSuccess == result) {
380 fStartedIncrementalDecode = true;
381 } else if (kUnimplemented == result) {
382 // FIXME: This is temporarily necessary, until we transition SkCodec
383 // implementations from scanline decoding to incremental decoding.
384 // SkAndroidCodec will first attempt to use incremental decoding, but
385 // will fall back to scanline decoding if incremental returns
386 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
387 // (after potentially rewinding), but we do not want the next call to
388 // startScanlineDecode() to do a rewind.
389 fNeedsRewind = false;
390 }
391 return result;
392}
393
394
395SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000396 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700397 // Reset fCurrScanline in case of failure.
398 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700399
scroggo3a7701c2015-09-30 09:15:14 -0700400 if (!this->rewindIfNeeded()) {
401 return kCouldNotRewind;
402 }
403
scroggo46c57472015-09-30 08:57:13 -0700404 // Set options.
405 Options optsStorage;
406 if (nullptr == options) {
407 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700408 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700409 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700410 if (!size.contains(*options->fSubset)) {
411 return kInvalidInput;
412 }
413
414 // We only support subsetting in the x-dimension for scanline decoder.
415 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700416 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700417 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700418 }
419 }
420
421 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700422 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700423 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700424 }
425
Leon Scroggins571b30f2017-07-11 17:35:31 +0000426 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700427 if (result != SkCodec::kSuccess) {
428 return result;
429 }
430
431 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700432 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700433 fOptions = *options;
434 return kSuccess;
435}
436
msarette6dd0042015-10-09 11:07:34 -0700437int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700438 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700439 return 0;
scroggo46c57472015-09-30 08:57:13 -0700440 }
441
442 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700443 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700444 return 0;
scroggo46c57472015-09-30 08:57:13 -0700445 }
446
msarette6dd0042015-10-09 11:07:34 -0700447 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
448 if (linesDecoded < countLines) {
449 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
450 countLines, linesDecoded);
451 }
452 fCurrScanline += countLines;
453 return linesDecoded;
454}
455
456bool SkCodec::skipScanlines(int countLines) {
457 if (fCurrScanline < 0) {
458 return false;
459 }
460
461 SkASSERT(!fDstInfo.isEmpty());
462 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
463 // Arguably, we could just skip the scanlines which are remaining,
464 // and return true. We choose to return false so the client
465 // can catch their bug.
466 return false;
467 }
468
469 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700470 fCurrScanline += countLines;
471 return result;
472}
473
msarette6dd0042015-10-09 11:07:34 -0700474int SkCodec::outputScanline(int inputScanline) const {
475 SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height());
476 return this->onOutputScanline(inputScanline);
477}
scroggo46c57472015-09-30 08:57:13 -0700478
msarette6dd0042015-10-09 11:07:34 -0700479int SkCodec::onOutputScanline(int inputScanline) const {
480 switch (this->getScanlineOrder()) {
481 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700482 return inputScanline;
483 case kBottomUp_SkScanlineOrder:
484 return this->getInfo().height() - inputScanline - 1;
485 default:
486 // This case indicates an interlaced gif and is implemented by SkGifCodec.
487 SkASSERT(false);
488 return 0;
scroggo46c57472015-09-30 08:57:13 -0700489 }
msarette6dd0042015-10-09 11:07:34 -0700490}
scroggo46c57472015-09-30 08:57:13 -0700491
msarettf7eb6fc2016-09-13 09:04:11 -0700492uint64_t SkCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
493 switch (dstInfo.colorType()) {
494 case kRGBA_F16_SkColorType: {
495 static constexpr uint64_t transparentColor = 0;
496 static constexpr uint64_t opaqueColor = ((uint64_t) SK_Half1) << 48;
497 return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? opaqueColor : transparentColor;
498 }
499 default: {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000500 // This not only handles the kN32 case, but also k565, kGray8, since
msarettf7eb6fc2016-09-13 09:04:11 -0700501 // the low bits are zeros.
502 return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ?
503 SK_ColorBLACK : SK_ColorTRANSPARENT;
504 }
505 }
506}
507
msarette6dd0042015-10-09 11:07:34 -0700508static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes,
msarettf7eb6fc2016-09-13 09:04:11 -0700509 uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) {
msarette6dd0042015-10-09 11:07:34 -0700510 if (sampler) {
511 sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit);
512 } else {
513 SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit);
514 }
515}
516
517void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
518 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
519
520 void* fillDst;
msarettf7eb6fc2016-09-13 09:04:11 -0700521 const uint64_t fillValue = this->getFillValue(info);
msarette6dd0042015-10-09 11:07:34 -0700522 const int linesRemaining = linesRequested - linesDecoded;
523 SkSampler* sampler = this->getSampler(false);
524
msarett91c22b22016-02-22 12:27:46 -0800525 int fillWidth = info.width();
526 if (fOptions.fSubset) {
527 fillWidth = fOptions.fSubset->width();
528 }
529
msarette6dd0042015-10-09 11:07:34 -0700530 switch (this->getScanlineOrder()) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700531 case kTopDown_SkScanlineOrder: {
msarett91c22b22016-02-22 12:27:46 -0800532 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700533 fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes);
534 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
535 break;
536 }
537 case kBottomUp_SkScanlineOrder: {
538 fillDst = dst;
msarett91c22b22016-02-22 12:27:46 -0800539 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700540 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
541 break;
542 }
msarette6dd0042015-10-09 11:07:34 -0700543 }
scroggo46c57472015-09-30 08:57:13 -0700544}
Matt Sarett313c4632016-10-20 12:35:23 -0400545
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400546static inline SkColorSpaceXform::ColorFormat select_xform_format_ct(SkColorType colorType) {
547 switch (colorType) {
548 case kRGBA_8888_SkColorType:
549 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
550 case kBGRA_8888_SkColorType:
551 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
552 case kRGB_565_SkColorType:
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400553#ifdef SK_PMCOLOR_IS_RGBA
554 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
555#else
556 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
557#endif
558 default:
559 SkASSERT(false);
560 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
561 }
562}
563
Matt Sarettcf3f2342017-03-23 15:32:25 -0400564bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo,
565 SkTransferFunctionBehavior premulBehavior) {
Matt Sarett313c4632016-10-20 12:35:23 -0400566 fColorXform = nullptr;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400567 fXformOnDecode = false;
Matt Sarettcf3f2342017-03-23 15:32:25 -0400568 bool needsColorCorrectPremul = needs_premul(dstInfo, fEncodedInfo) &&
569 SkTransferFunctionBehavior::kRespect == premulBehavior;
570 if (needs_color_xform(dstInfo, fSrcInfo, needsColorCorrectPremul)) {
571 fColorXform = SkColorSpaceXform_Base::New(fSrcInfo.colorSpace(), dstInfo.colorSpace(),
572 premulBehavior);
Matt Sarett313c4632016-10-20 12:35:23 -0400573 if (!fColorXform) {
574 return false;
575 }
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400576
577 // We will apply the color xform when reading the color table unless F16 is requested.
578 fXformOnDecode = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
579 || kRGBA_F16_SkColorType == dstInfo.colorType();
580 if (fXformOnDecode) {
581 fDstXformFormat = select_xform_format(dstInfo.colorType());
582 } else {
583 fDstXformFormat = select_xform_format_ct(dstInfo.colorType());
584 }
Matt Sarett313c4632016-10-20 12:35:23 -0400585 }
586
587 return true;
588}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400589
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400590void SkCodec::applyColorXform(void* dst, const void* src, int count, SkAlphaType at) const {
591 SkASSERT(fColorXform);
592 SkAssertResult(fColorXform->apply(fDstXformFormat, dst,
593 fSrcXformFormat, src,
594 count, at));
595}
596
597void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
598 auto alphaType = select_xform_alpha(fDstInfo.alphaType(), fSrcInfo.alphaType());
599 this->applyColorXform(dst, src, count, alphaType);
600}
601
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400602std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400603 const int frameCount = this->getFrameCount();
604 SkASSERT(frameCount >= 0);
605 if (frameCount <= 0) {
606 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400607 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400608
609 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
610 // Not animated.
611 return std::vector<FrameInfo>{};
612 }
613
614 std::vector<FrameInfo> result(frameCount);
615 for (int i = 0; i < frameCount; ++i) {
616 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
617 }
618 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400619}
Mike Reedede7bac2017-07-23 15:30:02 -0400620
621#ifdef SK_SUPPORT_LEGACY_CODEC_NEW
622SkCodec* SkCodec::NewFromStream(SkStream* str, Result* res, SkPngChunkReader* chunk) {
Mike Reed816afd82017-07-25 15:06:51 -0400623 return MakeFromStream(std::unique_ptr<SkStream>(str), res, chunk).release();
Mike Reedede7bac2017-07-23 15:30:02 -0400624}
625SkCodec* SkCodec::NewFromData(sk_sp<SkData> data, SkPngChunkReader* chunk) {
626 return MakeFromData(std::move(data), chunk).release();
627}
628#endif