blob: 955c86d609cca52f4ddb9925a32fac1ccc33a0dd [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"
msarett1a464672016-01-07 13:17:19 -080012#include "SkData.h"
13#include "SkGifCodec.h"
14#include "SkIcoCodec.h"
msarette16b04a2015-04-15 07:32:19 -070015#include "SkJpegCodec.h"
msarettad3a5c62016-05-06 07:21:26 -070016#ifdef SK_HAS_PNG_LIBRARY
msarettbe1d5552016-01-21 09:05:23 -080017#include "SkPngCodec.h"
yujieqin916de9f2016-01-25 08:26:16 -080018#endif
msarett39b2d5a2016-02-17 08:26:31 -080019#include "SkRawCodec.h"
scroggof24f2242015-03-03 08:59:20 -080020#include "SkStream.h"
msarett1a464672016-01-07 13:17:19 -080021#include "SkWbmpCodec.h"
scroggo6f5e6192015-06-18 12:53:43 -070022#include "SkWebpCodec.h"
scroggof24f2242015-03-03 08:59:20 -080023
msarett74114382015-03-16 11:55:18 -070024struct DecoderProc {
scroggodb30be22015-12-08 18:54:13 -080025 bool (*IsFormat)(const void*, size_t);
msarett74114382015-03-16 11:55:18 -070026 SkCodec* (*NewFromStream)(SkStream*);
27};
28
29static const DecoderProc gDecoderProcs[] = {
msarettad3a5c62016-05-06 07:21:26 -070030#ifdef SK_HAS_JPEG_LIBRARY
msarette16b04a2015-04-15 07:32:19 -070031 { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080032#endif
msarettad3a5c62016-05-06 07:21:26 -070033#ifdef SK_HAS_WEBP_LIBRARY
scroggo6f5e6192015-06-18 12:53:43 -070034 { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080035#endif
msarettad3a5c62016-05-06 07:21:26 -070036#ifdef SK_HAS_GIF_LIBRARY
msarett8c8f22a2015-04-01 06:58:48 -070037 { SkGifCodec::IsGif, SkGifCodec::NewFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080038#endif
msarettad3a5c62016-05-06 07:21:26 -070039#ifdef SK_HAS_PNG_LIBRARY
msarett9bde9182015-03-25 05:27:48 -070040 { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080041#endif
halcanarya096d7a2015-03-27 12:16:53 -070042 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream },
43 { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream }
msarett74114382015-03-16 11:55:18 -070044};
45
scroggodb30be22015-12-08 18:54:13 -080046size_t SkCodec::MinBufferedBytesNeeded() {
47 return WEBP_VP8_HEADER_SIZE;
48}
49
scroggocf98fa92015-11-23 08:14:40 -080050SkCodec* SkCodec::NewFromStream(SkStream* stream,
51 SkPngChunkReader* chunkReader) {
scroggof24f2242015-03-03 08:59:20 -080052 if (!stream) {
halcanary96fcdcc2015-08-27 07:41:13 -070053 return nullptr;
scroggof24f2242015-03-03 08:59:20 -080054 }
scroggo0a7e69c2015-04-03 07:22:22 -070055
56 SkAutoTDelete<SkStream> streamDeleter(stream);
scroggodb30be22015-12-08 18:54:13 -080057
58 // 14 is enough to read all of the supported types.
59 const size_t bytesToRead = 14;
60 SkASSERT(bytesToRead <= MinBufferedBytesNeeded());
61
62 char buffer[bytesToRead];
63 size_t bytesRead = stream->peek(buffer, bytesToRead);
64
65 // It is also possible to have a complete image less than bytesToRead bytes
66 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
67 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
68 // than bytesToRead, so pass that directly to the decoder.
69 // It also is possible the stream uses too small a buffer for peeking, but
70 // we trust the caller to use a large enough buffer.
71
72 if (0 == bytesRead) {
scroggo3ab9f2e2016-01-06 09:53:34 -080073 // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
74 // printf could be useful to notice failures.
75 // SkCodecPrintf("Encoded image data failed to peek!\n");
76
scroggodb30be22015-12-08 18:54:13 -080077 // It is possible the stream does not support peeking, but does support
78 // rewinding.
79 // Attempt to read() and pass the actual amount read to the decoder.
80 bytesRead = stream->read(buffer, bytesToRead);
81 if (!stream->rewind()) {
scroggo3ab9f2e2016-01-06 09:53:34 -080082 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
scroggodb30be22015-12-08 18:54:13 -080083 return nullptr;
84 }
85 }
86
scroggocf98fa92015-11-23 08:14:40 -080087 // PNG is special, since we want to be able to supply an SkPngChunkReader.
88 // But this code follows the same pattern as the loop.
msarettad3a5c62016-05-06 07:21:26 -070089#ifdef SK_HAS_PNG_LIBRARY
scroggodb30be22015-12-08 18:54:13 -080090 if (SkPngCodec::IsPng(buffer, bytesRead)) {
mtklein18300a32016-03-16 13:53:35 -070091 return SkPngCodec::NewFromStream(streamDeleter.release(), chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -080092 } else
93#endif
94 {
scroggocf98fa92015-11-23 08:14:40 -080095 for (DecoderProc proc : gDecoderProcs) {
scroggodb30be22015-12-08 18:54:13 -080096 if (proc.IsFormat(buffer, bytesRead)) {
mtklein18300a32016-03-16 13:53:35 -070097 return proc.NewFromStream(streamDeleter.release());
scroggocf98fa92015-11-23 08:14:40 -080098 }
msarett74114382015-03-16 11:55:18 -070099 }
yujieqin916de9f2016-01-25 08:26:16 -0800100
101#ifdef SK_CODEC_DECODES_RAW
102 // Try to treat the input as RAW if all the other checks failed.
mtklein18300a32016-03-16 13:53:35 -0700103 return SkRawCodec::NewFromStream(streamDeleter.release());
yujieqin916de9f2016-01-25 08:26:16 -0800104#endif
scroggof24f2242015-03-03 08:59:20 -0800105 }
msarett8c8f22a2015-04-01 06:58:48 -0700106
msarettf44631b2016-01-13 10:54:20 -0800107 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800108}
109
scroggocf98fa92015-11-23 08:14:40 -0800110SkCodec* SkCodec::NewFromData(SkData* data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800111 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700112 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800113 }
scroggocf98fa92015-11-23 08:14:40 -0800114 return NewFromStream(new SkMemoryStream(data), reader);
scroggof24f2242015-03-03 08:59:20 -0800115}
116
msarettc30c4182016-04-20 11:53:35 -0700117SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
118 sk_sp<SkColorSpace> colorSpace, Origin origin)
119 : fEncodedInfo(info)
120 , fSrcInfo(info.makeImageInfo(width, height))
scroggof24f2242015-03-03 08:59:20 -0800121 , fStream(stream)
122 , fNeedsRewind(false)
msarettad8bcfe2016-03-07 07:09:03 -0800123 , fColorSpace(colorSpace)
msarett0e6274f2016-03-21 08:04:40 -0700124 , fOrigin(origin)
scroggo46c57472015-09-30 08:57:13 -0700125 , fDstInfo()
126 , fOptions()
127 , fCurrScanline(-1)
scroggof24f2242015-03-03 08:59:20 -0800128{}
129
scroggo9b2cdbf42015-07-10 12:07:02 -0700130SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700131
scroggob427db12015-08-12 07:24:13 -0700132bool SkCodec::rewindIfNeeded() {
scroggo3a7701c2015-09-30 09:15:14 -0700133 if (!fStream) {
134 // Some codecs do not have a stream, but they hold others that do. They
135 // must handle rewinding themselves.
136 return true;
137 }
138
scroggof24f2242015-03-03 08:59:20 -0800139 // Store the value of fNeedsRewind so we can update it. Next read will
140 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700141 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800142 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700143 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700144 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700145 }
scroggob427db12015-08-12 07:24:13 -0700146
scroggo46c57472015-09-30 08:57:13 -0700147 // startScanlineDecode will need to be called before decoding scanlines.
148 fCurrScanline = -1;
149
scroggob427db12015-08-12 07:24:13 -0700150 if (!fStream->rewind()) {
151 return false;
152 }
153
154 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800155}
scroggo05245902015-03-25 11:11:52 -0700156
scroggoeb602a52015-07-09 08:16:03 -0700157SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
158 const Options* options, SkPMColor ctable[], int* ctableCount) {
159 if (kUnknown_SkColorType == info.colorType()) {
160 return kInvalidConversion;
161 }
halcanary96fcdcc2015-08-27 07:41:13 -0700162 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700163 return kInvalidParameters;
164 }
165 if (rowBytes < info.minRowBytes()) {
166 return kInvalidParameters;
167 }
168
169 if (kIndex_8_SkColorType == info.colorType()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700170 if (nullptr == ctable || nullptr == ctableCount) {
scroggoeb602a52015-07-09 08:16:03 -0700171 return kInvalidParameters;
172 }
173 } else {
174 if (ctableCount) {
175 *ctableCount = 0;
176 }
halcanary96fcdcc2015-08-27 07:41:13 -0700177 ctableCount = nullptr;
178 ctable = nullptr;
scroggoeb602a52015-07-09 08:16:03 -0700179 }
180
scroggo3a7701c2015-09-30 09:15:14 -0700181 if (!this->rewindIfNeeded()) {
182 return kCouldNotRewind;
183 }
184
scroggoeb602a52015-07-09 08:16:03 -0700185 // Default options.
186 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700187 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700188 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700189 } else if (options->fSubset) {
190 SkIRect subset(*options->fSubset);
191 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
192 // FIXME: How to differentiate between not supporting subset at all
193 // and not supporting this particular subset?
194 return kUnimplemented;
195 }
scroggoeb602a52015-07-09 08:16:03 -0700196 }
scroggoe7fc14b2015-10-02 13:14:46 -0700197
198 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
199 // because it supports arbitrary scaling/subset combinations.
200 if (!this->dimensionsSupported(info.dimensions())) {
201 return kInvalidScale;
202 }
203
msarette6dd0042015-10-09 11:07:34 -0700204 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
205 // successfully.
206 int rowsDecoded = 0;
207 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, ctable, ctableCount,
208 &rowsDecoded);
scroggoeb602a52015-07-09 08:16:03 -0700209
210 if ((kIncompleteInput == result || kSuccess == result) && ctableCount) {
211 SkASSERT(*ctableCount >= 0 && *ctableCount <= 256);
212 }
msarette6dd0042015-10-09 11:07:34 -0700213
214 // A return value of kIncompleteInput indicates a truncated image stream.
215 // In this case, we will fill any uninitialized memory with a default value.
216 // Some subclasses will take care of filling any uninitialized memory on
217 // their own. They indicate that all of the memory has been filled by
218 // setting rowsDecoded equal to the height.
219 if (kIncompleteInput == result && rowsDecoded != info.height()) {
220 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
221 rowsDecoded);
222 }
223
scroggoeb602a52015-07-09 08:16:03 -0700224 return result;
225}
226
227SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
halcanary96fcdcc2015-08-27 07:41:13 -0700228 return this->getPixels(info, pixels, rowBytes, nullptr, nullptr, nullptr);
scroggoeb602a52015-07-09 08:16:03 -0700229}
scroggo46c57472015-09-30 08:57:13 -0700230
231SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& dstInfo,
232 const SkCodec::Options* options, SkPMColor ctable[], int* ctableCount) {
233 // Reset fCurrScanline in case of failure.
234 fCurrScanline = -1;
235 // Ensure that valid color ptrs are passed in for kIndex8 color type
236 if (kIndex_8_SkColorType == dstInfo.colorType()) {
237 if (nullptr == ctable || nullptr == ctableCount) {
238 return SkCodec::kInvalidParameters;
239 }
240 } else {
241 if (ctableCount) {
242 *ctableCount = 0;
243 }
244 ctableCount = nullptr;
245 ctable = nullptr;
246 }
247
scroggo3a7701c2015-09-30 09:15:14 -0700248 if (!this->rewindIfNeeded()) {
249 return kCouldNotRewind;
250 }
251
scroggo46c57472015-09-30 08:57:13 -0700252 // Set options.
253 Options optsStorage;
254 if (nullptr == options) {
255 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700256 } else if (options->fSubset) {
msarettfdb47572015-10-13 12:50:14 -0700257 SkIRect size = SkIRect::MakeSize(dstInfo.dimensions());
258 if (!size.contains(*options->fSubset)) {
259 return kInvalidInput;
260 }
261
262 // We only support subsetting in the x-dimension for scanline decoder.
263 // Subsetting in the y-dimension can be accomplished using skipScanlines().
264 if (options->fSubset->top() != 0 || options->fSubset->height() != dstInfo.height()) {
265 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700266 }
267 }
268
269 // FIXME: Support subsets somehow?
270 if (!this->dimensionsSupported(dstInfo.dimensions())) {
271 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700272 }
273
274 const Result result = this->onStartScanlineDecode(dstInfo, *options, ctable, ctableCount);
275 if (result != SkCodec::kSuccess) {
276 return result;
277 }
278
279 fCurrScanline = 0;
280 fDstInfo = dstInfo;
281 fOptions = *options;
282 return kSuccess;
283}
284
285SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& dstInfo) {
286 return this->startScanlineDecode(dstInfo, nullptr, nullptr, nullptr);
287}
288
msarette6dd0042015-10-09 11:07:34 -0700289int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700290 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700291 return 0;
scroggo46c57472015-09-30 08:57:13 -0700292 }
293
294 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700295 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700296 return 0;
scroggo46c57472015-09-30 08:57:13 -0700297 }
298
msarette6dd0042015-10-09 11:07:34 -0700299 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
300 if (linesDecoded < countLines) {
301 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
302 countLines, linesDecoded);
303 }
304 fCurrScanline += countLines;
305 return linesDecoded;
306}
307
308bool SkCodec::skipScanlines(int countLines) {
309 if (fCurrScanline < 0) {
310 return false;
311 }
312
313 SkASSERT(!fDstInfo.isEmpty());
314 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
315 // Arguably, we could just skip the scanlines which are remaining,
316 // and return true. We choose to return false so the client
317 // can catch their bug.
318 return false;
319 }
320
321 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700322 fCurrScanline += countLines;
323 return result;
324}
325
msarette6dd0042015-10-09 11:07:34 -0700326int SkCodec::outputScanline(int inputScanline) const {
327 SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height());
328 return this->onOutputScanline(inputScanline);
329}
scroggo46c57472015-09-30 08:57:13 -0700330
msarette6dd0042015-10-09 11:07:34 -0700331int SkCodec::onOutputScanline(int inputScanline) const {
332 switch (this->getScanlineOrder()) {
333 case kTopDown_SkScanlineOrder:
334 case kNone_SkScanlineOrder:
335 return inputScanline;
336 case kBottomUp_SkScanlineOrder:
337 return this->getInfo().height() - inputScanline - 1;
338 default:
339 // This case indicates an interlaced gif and is implemented by SkGifCodec.
340 SkASSERT(false);
341 return 0;
scroggo46c57472015-09-30 08:57:13 -0700342 }
msarette6dd0042015-10-09 11:07:34 -0700343}
scroggo46c57472015-09-30 08:57:13 -0700344
msarette6dd0042015-10-09 11:07:34 -0700345static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes,
346 uint32_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) {
347 if (sampler) {
348 sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit);
349 } else {
350 SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit);
351 }
352}
353
354void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
355 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
356
357 void* fillDst;
scroggoc5560be2016-02-03 09:42:42 -0800358 const uint32_t fillValue = this->getFillValue(info.colorType());
msarette6dd0042015-10-09 11:07:34 -0700359 const int linesRemaining = linesRequested - linesDecoded;
360 SkSampler* sampler = this->getSampler(false);
361
msarett91c22b22016-02-22 12:27:46 -0800362 int fillWidth = info.width();
363 if (fOptions.fSubset) {
364 fillWidth = fOptions.fSubset->width();
365 }
366
msarette6dd0042015-10-09 11:07:34 -0700367 switch (this->getScanlineOrder()) {
368 case kTopDown_SkScanlineOrder:
369 case kNone_SkScanlineOrder: {
msarett91c22b22016-02-22 12:27:46 -0800370 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700371 fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes);
372 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
373 break;
374 }
375 case kBottomUp_SkScanlineOrder: {
376 fillDst = dst;
msarett91c22b22016-02-22 12:27:46 -0800377 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700378 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
379 break;
380 }
381 case kOutOfOrder_SkScanlineOrder: {
382 SkASSERT(1 == linesRequested || this->getInfo().height() == linesRequested);
msarett91c22b22016-02-22 12:27:46 -0800383 const SkImageInfo fillInfo = info.makeWH(fillWidth, 1);
msarette6dd0042015-10-09 11:07:34 -0700384 for (int srcY = linesDecoded; srcY < linesRequested; srcY++) {
385 fillDst = SkTAddOffset<void>(dst, this->outputScanline(srcY) * rowBytes);
386 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
387 }
388 break;
389 }
390 }
scroggo46c57472015-09-30 08:57:13 -0700391}