scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "SkCodec.h" |
| 9 | #include "SkData.h" |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 10 | #include "SkCodec_libbmp.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 11 | #include "SkCodec_libgif.h" |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 12 | #include "SkCodec_libico.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 13 | #include "SkCodec_libpng.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 14 | #include "SkCodec_wbmp.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 15 | #include "SkCodecPriv.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 16 | #include "SkJpegCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 17 | #include "SkStream.h" |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 18 | #include "SkWebpCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 19 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 20 | struct DecoderProc { |
| 21 | bool (*IsFormat)(SkStream*); |
| 22 | SkCodec* (*NewFromStream)(SkStream*); |
| 23 | }; |
| 24 | |
| 25 | static const DecoderProc gDecoderProcs[] = { |
| 26 | { SkPngCodec::IsPng, SkPngCodec::NewFromStream }, |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 27 | { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream }, |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 28 | { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream }, |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 29 | { SkGifCodec::IsGif, SkGifCodec::NewFromStream }, |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 30 | { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 31 | { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }, |
| 32 | { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 35 | SkCodec* SkCodec::NewFromStream(SkStream* stream) { |
| 36 | if (!stream) { |
| 37 | return NULL; |
| 38 | } |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 39 | |
| 40 | SkAutoTDelete<SkStream> streamDeleter(stream); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 41 | |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 42 | SkAutoTDelete<SkCodec> codec(NULL); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 43 | for (uint32_t i = 0; i < SK_ARRAY_COUNT(gDecoderProcs); i++) { |
| 44 | DecoderProc proc = gDecoderProcs[i]; |
| 45 | const bool correctFormat = proc.IsFormat(stream); |
| 46 | if (!stream->rewind()) { |
| 47 | return NULL; |
| 48 | } |
| 49 | if (correctFormat) { |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 50 | codec.reset(proc.NewFromStream(streamDeleter.detach())); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 51 | break; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 52 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 53 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 54 | |
| 55 | // Set the max size at 128 megapixels (512 MB for kN32). |
| 56 | // This is about 4x smaller than a test image that takes a few minutes for |
| 57 | // dm to decode and draw. |
| 58 | const int32_t maxSize = 1 << 27; |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 59 | if (codec && codec->getInfo().width() * codec->getInfo().height() > maxSize) { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 60 | SkCodecPrintf("Error: Image size too large, cannot decode.\n"); |
| 61 | return NULL; |
| 62 | } else { |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 63 | return codec.detach(); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 64 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | SkCodec* SkCodec::NewFromData(SkData* data) { |
| 68 | if (!data) { |
| 69 | return NULL; |
| 70 | } |
| 71 | return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); |
| 72 | } |
| 73 | |
| 74 | SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream) |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 75 | : INHERITED(info) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 76 | , fStream(stream) |
| 77 | , fNeedsRewind(false) |
| 78 | {} |
| 79 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 80 | SkCodec::RewindState SkCodec::rewindIfNeeded() { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 81 | // Store the value of fNeedsRewind so we can update it. Next read will |
| 82 | // require a rewind. |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 83 | const bool needsRewind = fNeedsRewind; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 84 | fNeedsRewind = true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 85 | if (!needsRewind) { |
| 86 | return kNoRewindNecessary_RewindState; |
| 87 | } |
| 88 | return fStream->rewind() ? kRewound_RewindState |
| 89 | : kCouldNotRewind_RewindState; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 90 | } |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 91 | |
msarett | 9e43cab | 2015-04-29 07:38:43 -0700 | [diff] [blame] | 92 | SkScanlineDecoder* SkCodec::getScanlineDecoder(const SkImageInfo& dstInfo, const Options* options, |
| 93 | SkPMColor ctable[], int* ctableCount) { |
| 94 | |
| 95 | // Set options. |
| 96 | Options optsStorage; |
| 97 | if (NULL == options) { |
| 98 | options = &optsStorage; |
| 99 | } |
| 100 | |
| 101 | fScanlineDecoder.reset(this->onGetScanlineDecoder(dstInfo, *options, ctable, ctableCount)); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 102 | return fScanlineDecoder.get(); |
| 103 | } |
msarett | 9e43cab | 2015-04-29 07:38:43 -0700 | [diff] [blame] | 104 | |
| 105 | SkScanlineDecoder* SkCodec::getScanlineDecoder(const SkImageInfo& dstInfo) { |
| 106 | SkASSERT(kIndex_8_SkColorType != dstInfo.colorType()); |
| 107 | if (kIndex_8_SkColorType == dstInfo.colorType()) { |
| 108 | return NULL; |
| 109 | } |
| 110 | return this->getScanlineDecoder(dstInfo, NULL, NULL, NULL); |
| 111 | } |