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 "SkAndroidCodec.h" | |
9 #include "SkCodec.h" | |
10 #include "SkCodecPriv.h" | |
11 #include "SkSampledCodec.h" | |
12 #include "SkWebpAdapterCodec.h" | |
13 | |
14 static bool is_valid_sample_size(int sampleSize) { | |
15 // FIXME: As Leon has mentioned elsewhere, surely there is also a maximum sa mpleSize? | |
scroggo
2015/10/20 13:51:59
Maybe it's bigger than the width/height of the ima
msarett
2015/10/20 14:47:27
We actually don't even fail in this case. We'll a
| |
16 return sampleSize > 0; | |
17 } | |
18 | |
19 SkAndroidCodec::SkAndroidCodec(const SkImageInfo& info) | |
20 : fInfo(info) | |
21 {} | |
22 | |
23 SkAndroidCodec* SkAndroidCodec::NewFromStream(SkStream* stream) { | |
24 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream)); | |
25 if (nullptr == codec) { | |
26 return nullptr; | |
27 } | |
28 | |
29 switch (codec->getEncodedFormat()) { | |
30 case kWEBP_SkEncodedFormat: | |
31 return new SkWebpAdapterCodec((SkWebpCodec*) codec.detach()); | |
32 case kPNG_SkEncodedFormat: | |
33 case kJPEG_SkEncodedFormat: | |
34 return new SkSampledCodec(codec.detach()); | |
35 default: | |
36 // FIXME: SkSampledCodec is temporarily disabled for other formats | |
37 // while focusing on the formats that are supported by | |
38 // BitmapRegionDecoder. | |
39 return nullptr; | |
40 } | |
41 } | |
42 | |
43 SkAndroidCodec* SkAndroidCodec::NewFromData(SkData* data) { | |
44 if (!data) { | |
45 return nullptr; | |
46 } | |
47 | |
48 return NewFromStream(new SkMemoryStream(data)); | |
49 } | |
50 | |
51 SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const { | |
52 if (!is_valid_sample_size(sampleSize)) { | |
53 return SkISize::Make(0, 0); | |
54 } | |
55 | |
56 return this->onGetSampledDimensions(sampleSize); | |
57 } | |
58 | |
59 bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const { | |
60 if (!desiredSubset || !is_valid_subset(*desiredSubset, fInfo.dimensions())) { | |
61 return false; | |
62 } | |
63 | |
64 return this->onGetSupportedSubset(desiredSubset); | |
65 } | |
66 | |
67 SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect & subset) const { | |
68 if (!is_valid_sample_size(sampleSize)) { | |
69 return SkISize::Make(0, 0); | |
70 } | |
71 | |
72 // We require that the input subset is a subset that is supported by SkAndro idCodec. | |
73 // We test this by calling getSupportedSubset() and verifying that no modifi cations | |
74 // are made to the subset. | |
75 SkIRect copySubset = subset; | |
76 if (!this->getSupportedSubset(©Subset) || copySubset != subset) { | |
77 return SkISize::Make(0, 0); | |
78 } | |
79 | |
80 // If the subset is the entire image, for consistency, use onGetSampledDimen sions(). | |
81 if (fInfo.dimensions() == subset.size()) { | |
82 return onGetSampledDimensions(sampleSize); | |
83 } | |
84 | |
85 return SkISize::Make(get_scaled_dimension(subset.width(), sampleSize), | |
scroggo
2015/10/20 13:51:59
Maybe a note that this should perhaps be a virtual
msarett
2015/10/20 14:47:27
Yes I think this is a good idea.
| |
86 get_scaled_dimension(subset.height(), sampleSize)); | |
87 } | |
88 | |
89 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels, | |
90 size_t rowBytes, AndroidOptions* options) { | |
91 if (!pixels) { | |
92 return SkCodec::kInvalidParameters; | |
93 } | |
94 if (rowBytes < info.minRowBytes()) { | |
95 return SkCodec::kInvalidParameters; | |
96 } | |
97 | |
98 AndroidOptions defaultOptions; | |
99 if (!options) { | |
100 options = &defaultOptions; | |
101 } else if (options->fSubset) { | |
102 if (!is_valid_subset(*options->fSubset, fInfo.dimensions())) { | |
103 return SkCodec::kInvalidParameters; | |
104 } | |
105 } | |
106 | |
107 return this->onGetAndroidPixels(info, pixels, rowBytes, *options); | |
108 } | |
109 | |
110 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels, | |
111 size_t rowBytes) { | |
112 return this->getAndroidPixels(info, pixels, rowBytes, nullptr); | |
113 } | |
OLD | NEW |