OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkWebpCodec.h" | 8 #include "SkWebpCodec.h" |
9 #include "SkTemplates.h" | 9 #include "SkTemplates.h" |
10 | 10 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 default: | 118 default: |
119 return MODE_LAST; | 119 return MODE_LAST; |
120 } | 120 } |
121 } | 121 } |
122 | 122 |
123 // The WebP decoding API allows us to incrementally pass chunks of bytes as we r eceive them to the | 123 // The WebP decoding API allows us to incrementally pass chunks of bytes as we r eceive them to the |
124 // decoder with WebPIAppend. In order to do so, we need to read chunks from the SkStream. This size | 124 // decoder with WebPIAppend. In order to do so, we need to read chunks from the SkStream. This size |
125 // is arbitrary. | 125 // is arbitrary. |
126 static const size_t BUFFER_SIZE = 4096; | 126 static const size_t BUFFER_SIZE = 4096; |
127 | 127 |
128 bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const { | |
129 if (!desiredSubset) { | |
130 return false; | |
131 } | |
132 | |
133 SkIRect bounds = SkIRect::MakeSize(this->getInfo().dimensions()); | |
134 if (!desiredSubset->intersect(bounds)) { | |
135 return false; | |
136 } | |
137 | |
138 // As stated below, libwebp snaps to even left and top. Make sure top and le ft are even, so we | |
139 // decode this exact subset. | |
140 // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested. | |
141 desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1; | |
142 desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1; | |
143 return true; | |
144 } | |
145 | |
128 SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, | 146 SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, |
129 const Options&, SkPMColor*, int*) { | 147 const Options& options, SkPMColor*, int *) { |
130 switch (this->rewindIfNeeded()) { | 148 switch (this->rewindIfNeeded()) { |
131 case kCouldNotRewind_RewindState: | 149 case kCouldNotRewind_RewindState: |
132 return kCouldNotRewind; | 150 return kCouldNotRewind; |
133 case kRewound_RewindState: | 151 case kRewound_RewindState: |
134 // Rewound to the beginning. Since creation only does a peek, the st ream is at the | 152 // Rewound to the beginning. Since creation only does a peek, the st ream is at the |
135 // correct position. | 153 // correct position. |
136 break; | 154 break; |
137 case kNoRewindNecessary_RewindState: | 155 case kNoRewindNecessary_RewindState: |
138 // Already at the right spot for decoding. | 156 // Already at the right spot for decoding. |
139 break; | 157 break; |
140 } | 158 } |
141 | 159 |
142 if (!conversion_possible(dstInfo, this->getInfo())) { | 160 if (!conversion_possible(dstInfo, this->getInfo())) { |
143 return kInvalidConversion; | 161 return kInvalidConversion; |
144 } | 162 } |
145 | 163 |
146 WebPDecoderConfig config; | 164 WebPDecoderConfig config; |
147 if (0 == WebPInitDecoderConfig(&config)) { | 165 if (0 == WebPInitDecoderConfig(&config)) { |
148 // ABI mismatch. | 166 // ABI mismatch. |
149 // FIXME: New enum for this? | 167 // FIXME: New enum for this? |
150 return kInvalidInput; | 168 return kInvalidInput; |
151 } | 169 } |
152 | 170 |
153 // Free any memory associated with the buffer. Must be called last, so we de clare it first. | 171 // Free any memory associated with the buffer. Must be called last, so we de clare it first. |
154 SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output) ); | 172 SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output) ); |
155 | 173 |
156 SkISize dimensions = dstInfo.dimensions(); | 174 SkIRect bounds = SkIRect::MakeSize(this->getInfo().dimensions()); |
157 if (this->getInfo().dimensions() != dimensions) { | 175 if (!options.fSubset.isEmpty()) { |
176 // Caller is requesting a subset. | |
177 if (!bounds.intersect(options.fSubset)) { | |
msarett
2015/07/21 15:19:37
Per our discussion in person, let's explicitly dis
scroggo
2015/07/21 16:48:56
Done.
| |
178 // The subset is out of bounds. | |
179 return kInvalidParameters; | |
180 } | |
181 | |
182 // This is tricky. libwebp snaps the top and left to even values. We cou ld let libwebp | |
183 // do the snap, and return a subset which is a different one than reques ted. The problem | |
184 // with that approach is that the caller may try to stitch subsets toget her, and if we | |
185 // returned different subsets than requested, there would be artifacts a t the boundaries. | |
186 // Instead, we report that we cannot support odd values for top and left .. | |
187 if (!SkIsAlign2(bounds.fLeft) || !SkIsAlign2(bounds.fTop)) { | |
188 return kInvalidParameters; | |
189 } | |
190 | |
191 #ifdef SK_DEBUG | |
192 { | |
193 // Make a copy, since getValidSubset can change its input. | |
194 SkIRect subset(bounds); | |
195 // That said, getValidSubset should *not* change its input, in this case; otherwise | |
196 // getValidSubset does not match the actual subsets we can do. | |
197 SkASSERT(this->getValidSubset(&subset) && subset == bounds); | |
198 } | |
199 #endif | |
200 | |
201 config.options.use_cropping = 1; | |
202 config.options.crop_left = bounds.fLeft; | |
203 config.options.crop_top = bounds.fTop; | |
204 config.options.crop_width = bounds.width(); | |
205 config.options.crop_height = bounds.height(); | |
206 } | |
207 | |
208 SkISize dstDimensions = dstInfo.dimensions(); | |
209 if (bounds.size() != dstDimensions) { | |
158 // Caller is requesting scaling. | 210 // Caller is requesting scaling. |
159 config.options.use_scaling = 1; | 211 config.options.use_scaling = 1; |
160 config.options.scaled_width = dimensions.width(); | 212 config.options.scaled_width = dstDimensions.width(); |
161 config.options.scaled_height = dimensions.height(); | 213 config.options.scaled_height = dstDimensions.height(); |
162 } | 214 } |
163 | 215 |
164 config.output.colorspace = webp_decode_mode(dstInfo.colorType(), | 216 config.output.colorspace = webp_decode_mode(dstInfo.colorType(), |
165 dstInfo.alphaType() == kPremul_SkAlphaType); | 217 dstInfo.alphaType() == kPremul_SkAlphaType); |
166 config.output.u.RGBA.rgba = (uint8_t*) dst; | 218 config.output.u.RGBA.rgba = (uint8_t*) dst; |
167 config.output.u.RGBA.stride = (int) rowBytes; | 219 config.output.u.RGBA.stride = (int) rowBytes; |
168 config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); | 220 config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); |
169 config.output.is_external_memory = 1; | 221 config.output.is_external_memory = 1; |
170 | 222 |
171 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(NULL, 0, &confi g)); | 223 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(NULL, 0, &confi g)); |
(...skipping 19 matching lines...) Expand all Loading... | |
191 // Break out of the switch statement. Continue the loop. | 243 // Break out of the switch statement. Continue the loop. |
192 break; | 244 break; |
193 default: | 245 default: |
194 return kInvalidInput; | 246 return kInvalidInput; |
195 } | 247 } |
196 } | 248 } |
197 } | 249 } |
198 | 250 |
199 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) | 251 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) |
200 : INHERITED(info, stream) {} | 252 : INHERITED(info, stream) {} |
OLD | NEW |