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