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 "SkCodecPriv.h" | 8 #include "SkCodecPriv.h" |
9 #include "SkColorSpaceXform.h" | |
9 #include "SkWebpCodec.h" | 10 #include "SkWebpCodec.h" |
10 #include "SkStreamPriv.h" | 11 #include "SkStreamPriv.h" |
11 #include "SkTemplates.h" | 12 #include "SkTemplates.h" |
12 | 13 |
13 // A WebP decoder on top of (subset of) libwebp | 14 // A WebP decoder on top of (subset of) libwebp |
14 // For more information on WebP image format, and libwebp library, see: | 15 // For more information on WebP image format, and libwebp library, see: |
15 // https://code.google.com/speed/webp/ | 16 // https://code.google.com/speed/webp/ |
16 // http://www.webmproject.org/code/#libwebp-webp-image-library | 17 // http://www.webmproject.org/code/#libwebp-webp-image-library |
17 // https://chromium.googlesource.com/webm/libwebp | 18 // https://chromium.googlesource.com/webm/libwebp |
18 | 19 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 break; | 137 break; |
137 default: | 138 default: |
138 return nullptr; | 139 return nullptr; |
139 } | 140 } |
140 | 141 |
141 SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8); | 142 SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8); |
142 return new SkWebpCodec(features.width, features.height, info, std::move(colo rSpace), | 143 return new SkWebpCodec(features.width, features.height, info, std::move(colo rSpace), |
143 streamDeleter.release(), demux.release(), std::move(d ata)); | 144 streamDeleter.release(), demux.release(), std::move(d ata)); |
144 } | 145 } |
145 | 146 |
146 // This version is slightly different from SkCodecPriv's version of conversion_p ossible. It | 147 static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo& src, |
147 // supports both byte orders for 8888. | 148 SkColorSpaceXform* colorXform) { |
148 static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) { | |
149 if (!valid_alpha(dst.alphaType(), src.alphaType())) { | 149 if (!valid_alpha(dst.alphaType(), src.alphaType())) { |
150 return false; | 150 return false; |
151 } | 151 } |
152 | 152 |
153 switch (dst.colorType()) { | 153 switch (dst.colorType()) { |
154 // Both byte orders are supported. | 154 case kRGBA_F16_SkColorType: |
155 return nullptr != colorXform; | |
155 case kBGRA_8888_SkColorType: | 156 case kBGRA_8888_SkColorType: |
156 case kRGBA_8888_SkColorType: | 157 case kRGBA_8888_SkColorType: |
157 return true; | 158 return true; |
158 case kRGB_565_SkColorType: | 159 case kRGB_565_SkColorType: |
159 return src.alphaType() == kOpaque_SkAlphaType; | 160 return nullptr == colorXform && src.alphaType() == kOpaque_SkAlphaTy pe; |
160 default: | 161 default: |
161 return false; | 162 return false; |
162 } | 163 } |
163 } | 164 } |
164 | 165 |
165 SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const { | 166 SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const { |
166 SkISize dim = this->getInfo().dimensions(); | 167 SkISize dim = this->getInfo().dimensions(); |
167 // SkCodec treats zero dimensional images as errors, so the minimum size | 168 // SkCodec treats zero dimensional images as errors, so the minimum size |
168 // that we will recommend is 1x1. | 169 // that we will recommend is 1x1. |
169 dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth)); | 170 dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth)); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 // As stated below, libwebp snaps to even left and top. Make sure top and le ft are even, so we | 204 // As stated below, libwebp snaps to even left and top. Make sure top and le ft are even, so we |
204 // decode this exact subset. | 205 // decode this exact subset. |
205 // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested. | 206 // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested. |
206 desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1; | 207 desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1; |
207 desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1; | 208 desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1; |
208 return true; | 209 return true; |
209 } | 210 } |
210 | 211 |
211 SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, | 212 SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, |
212 const Options& options, SkPMColor*, int *, | 213 const Options& options, SkPMColor*, int *, |
213 int* rowsDecoded) { | 214 int* rowsDecodedPtr) { |
214 if (!webp_conversion_possible(dstInfo, this->getInfo())) { | 215 |
216 std::unique_ptr<SkColorSpaceXform> colorXform = nullptr; | |
217 if (needs_color_xform(dstInfo, this->getInfo())) { | |
218 colorXform = SkColorSpaceXform::New(sk_ref_sp(this->getInfo().colorSpace ()), | |
219 sk_ref_sp(dstInfo.colorSpace())); | |
220 } | |
221 | |
222 if (!webp_conversion_possible(dstInfo, this->getInfo(), colorXform.get())) { | |
215 return kInvalidConversion; | 223 return kInvalidConversion; |
216 } | 224 } |
217 | 225 |
218 WebPDecoderConfig config; | 226 WebPDecoderConfig config; |
219 if (0 == WebPInitDecoderConfig(&config)) { | 227 if (0 == WebPInitDecoderConfig(&config)) { |
220 // ABI mismatch. | 228 // ABI mismatch. |
221 // FIXME: New enum for this? | 229 // FIXME: New enum for this? |
222 return kInvalidInput; | 230 return kInvalidInput; |
223 } | 231 } |
224 | 232 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
262 } | 270 } |
263 | 271 |
264 SkISize dstDimensions = dstInfo.dimensions(); | 272 SkISize dstDimensions = dstInfo.dimensions(); |
265 if (bounds.size() != dstDimensions) { | 273 if (bounds.size() != dstDimensions) { |
266 // Caller is requesting scaling. | 274 // Caller is requesting scaling. |
267 config.options.use_scaling = 1; | 275 config.options.use_scaling = 1; |
268 config.options.scaled_width = dstDimensions.width(); | 276 config.options.scaled_width = dstDimensions.width(); |
269 config.options.scaled_height = dstDimensions.height(); | 277 config.options.scaled_height = dstDimensions.height(); |
270 } | 278 } |
271 | 279 |
272 config.output.colorspace = webp_decode_mode(dstInfo.colorType(), | 280 // FIXME (msarett): |
273 dstInfo.alphaType() == kPremul_SkAlphaType); | 281 // Lossless webp is encoded as BGRA. In that case, it would be more efficie nt to |
274 config.output.u.RGBA.rgba = (uint8_t*) dst; | 282 // to decode BGRA and apply the color xform to a BGRA buffer. |
scroggo
2016/09/07 20:32:58
Why don't we today?
msarett
2016/09/07 22:27:41
I haven't implemented BGRA as an input yet :)
| |
275 config.output.u.RGBA.stride = (int) rowBytes; | 283 config.output.colorspace = colorXform ? MODE_RGBA : |
276 config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); | 284 webp_decode_mode(dstInfo.colorType(), dstInfo.alphaType() == kPremul _SkAlphaType); |
277 config.output.is_external_memory = 1; | 285 config.output.is_external_memory = 1; |
278 | 286 |
287 SkAutoTMalloc<uint32_t> pixels; | |
288 if (kRGBA_F16_SkColorType == dstInfo.colorType()) { | |
289 // The best we can do is to decode into an 8888 buffer and then transfor m the entire | |
290 // buffer to F16. libwebp does not support a row-by-row decode. | |
scroggo
2016/09/07 20:32:58
It seems like this comment mostly applies to the b
msarett
2016/09/07 22:27:41
Yeah, I'll move it up. It's just that it's awful
| |
291 pixels.reset(dstDimensions.width() * dstDimensions.height()); | |
292 config.output.u.RGBA.rgba = (uint8_t*) pixels.get(); | |
293 config.output.u.RGBA.stride = (int) dstDimensions.width() * sizeof(uint3 2_t); | |
294 config.output.u.RGBA.size = config.output.u.RGBA.stride * dstDimensions. height(); | |
295 } else { | |
296 config.output.u.RGBA.rgba = (uint8_t*) dst; | |
297 config.output.u.RGBA.stride = (int) rowBytes; | |
298 config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); | |
299 } | |
300 | |
279 WebPIterator frame; | 301 WebPIterator frame; |
280 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame); | 302 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame); |
281 // If this succeeded in NewFromStream(), it should succeed again here. | 303 // If this succeeded in NewFromStream(), it should succeed again here. |
282 SkAssertResult(WebPDemuxGetFrame(fDemux, 1, &frame)); | 304 SkAssertResult(WebPDemuxGetFrame(fDemux, 1, &frame)); |
283 | 305 |
284 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &co nfig)); | 306 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &co nfig)); |
285 if (!idec) { | 307 if (!idec) { |
286 return kInvalidInput; | 308 return kInvalidInput; |
287 } | 309 } |
288 | 310 |
311 int rowsDecoded; | |
312 SkCodec::Result result; | |
289 switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) { | 313 switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) { |
290 case VP8_STATUS_OK: | 314 case VP8_STATUS_OK: |
291 return kSuccess; | 315 rowsDecoded = dstInfo.height(); |
292 case VP8_STATUS_SUSPENDED: | 316 result = kSuccess; |
293 WebPIDecGetRGB(idec, rowsDecoded, nullptr, nullptr, nullptr); | 317 break; |
294 return kIncompleteInput; | 318 case VP8_STATUS_SUSPENDED: { |
scroggo
2016/09/07 20:32:58
Nit: do you need the braces?
msarett
2016/09/07 22:27:41
No, removed.
| |
319 WebPIDecGetRGB(idec, rowsDecodedPtr, nullptr, nullptr, nullptr); | |
320 | |
321 rowsDecoded = *rowsDecodedPtr; | |
322 result = kIncompleteInput; | |
323 break; | |
324 } | |
295 default: | 325 default: |
296 return kInvalidInput; | 326 return kInvalidInput; |
297 } | 327 } |
328 | |
329 if (colorXform) { | |
330 SkAlphaType xformAlphaType = color_xform_alpha_type(dstInfo.alphaType(), | |
331 this->getInfo().alph aType()); | |
332 | |
333 uint32_t* src = (uint32_t*) config.output.u.RGBA.rgba; | |
334 size_t srcRowBytes = config.output.u.RGBA.stride; | |
335 for (int y = 0; y < rowsDecoded; y++) { | |
336 colorXform->apply(dst, src, dstInfo.width(), dstInfo.colorType(), xf ormAlphaType); | |
337 dst = SkTAddOffset<void>(dst, rowBytes); | |
338 src = SkTAddOffset<uint32_t>(src, srcRowBytes); | |
339 } | |
340 } | |
341 | |
342 return result; | |
298 } | 343 } |
299 | 344 |
300 SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info, | 345 SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info, |
301 sk_sp<SkColorSpace> colorSpace, SkStream* stream, WebPD emuxer* demux, | 346 sk_sp<SkColorSpace> colorSpace, SkStream* stream, WebPD emuxer* demux, |
302 sk_sp<SkData> data) | 347 sk_sp<SkData> data) |
303 : INHERITED(width, height, info, stream, std::move(colorSpace)) | 348 : INHERITED(width, height, info, stream, std::move(colorSpace)) |
304 , fDemux(demux) | 349 , fDemux(demux) |
305 , fData(std::move(data)) | 350 , fData(std::move(data)) |
306 {} | 351 {} |
OLD | NEW |