OLD | NEW |
| (Empty) |
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 "SkScanlineDecoder.h" | |
9 #include "SkBmpCodec.h" | |
10 #include "SkCodec_libgif.h" | |
11 #include "SkCodec_libpng.h" | |
12 #include "SkCodec_wbmp.h" | |
13 #include "SkCodecPriv.h" | |
14 #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK | |
15 #include "SkJpegCodec.h" | |
16 #endif | |
17 | |
18 struct DecoderProc { | |
19 bool (*IsFormat)(SkStream*); | |
20 SkScanlineDecoder* (*NewFromStream)(SkStream*); | |
21 }; | |
22 | |
23 static const DecoderProc gDecoderProcs[] = { | |
24 { SkPngCodec::IsPng, SkPngCodec::NewSDFromStream }, | |
25 #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK | |
26 { SkJpegCodec::IsJpeg, SkJpegCodec::NewSDFromStream }, | |
27 #endif | |
28 { SkGifCodec::IsGif, SkGifCodec::NewSDFromStream }, | |
29 { SkBmpCodec::IsBmp, SkBmpCodec::NewSDFromStream }, | |
30 { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewSDFromStream }, | |
31 }; | |
32 | |
33 SkScanlineDecoder* SkScanlineDecoder::NewFromStream(SkStream* stream) { | |
34 if (!stream) { | |
35 return nullptr; | |
36 } | |
37 | |
38 SkAutoTDelete<SkStream> streamDeleter(stream); | |
39 | |
40 SkAutoTDelete<SkScanlineDecoder> codec(nullptr); | |
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 nullptr; | |
46 } | |
47 if (correctFormat) { | |
48 codec.reset(proc.NewFromStream(streamDeleter.detach())); | |
49 break; | |
50 } | |
51 } | |
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; | |
57 if (codec && codec->getInfo().width() * codec->getInfo().height() > maxSize)
{ | |
58 SkCodecPrintf("Error: Image size too large, cannot decode.\n"); | |
59 return nullptr; | |
60 } else { | |
61 return codec.detach(); | |
62 } | |
63 } | |
64 | |
65 SkScanlineDecoder* SkScanlineDecoder::NewFromData(SkData* data) { | |
66 if (!data) { | |
67 return nullptr; | |
68 } | |
69 return NewFromStream(new SkMemoryStream(data)); | |
70 } | |
71 | |
72 | |
73 SkCodec::Result SkScanlineDecoder::start(const SkImageInfo& dstInfo, | |
74 const SkCodec::Options* options, SkPMColor ctable[], int* ctableCount) { | |
75 // Ensure that valid color ptrs are passed in for kIndex8 color type | |
76 if (kIndex_8_SkColorType == dstInfo.colorType()) { | |
77 if (nullptr == ctable || nullptr == ctableCount) { | |
78 return SkCodec::kInvalidParameters; | |
79 } | |
80 } else { | |
81 if (ctableCount) { | |
82 *ctableCount = 0; | |
83 } | |
84 ctableCount = nullptr; | |
85 ctable = nullptr; | |
86 } | |
87 | |
88 // Set options. | |
89 SkCodec::Options optsStorage; | |
90 if (nullptr == options) { | |
91 options = &optsStorage; | |
92 } | |
93 | |
94 const SkCodec::Result result = this->onStart(dstInfo, *options, ctable, ctab
leCount); | |
95 if (result != SkCodec::kSuccess) { | |
96 return result; | |
97 } | |
98 | |
99 fCurrScanline = 0; | |
100 fDstInfo = dstInfo; | |
101 fOptions = *options; | |
102 return SkCodec::kSuccess; | |
103 } | |
104 | |
105 SkCodec::Result SkScanlineDecoder::start(const SkImageInfo& dstInfo) { | |
106 return this->start(dstInfo, nullptr, nullptr, nullptr); | |
107 } | |
108 | |
OLD | NEW |