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. |
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 // We will decode the entire image and then perform the color transform. li
bwebp |
| 288 // does not provide a row-by-row API. This is a shame particularly in the F
16 case, |
| 289 // where we need to allocate an extra image-sized buffer. |
| 290 SkAutoTMalloc<uint32_t> pixels; |
| 291 if (kRGBA_F16_SkColorType == dstInfo.colorType()) { |
| 292 pixels.reset(dstDimensions.width() * dstDimensions.height()); |
| 293 config.output.u.RGBA.rgba = (uint8_t*) pixels.get(); |
| 294 config.output.u.RGBA.stride = (int) dstDimensions.width() * sizeof(uint3
2_t); |
| 295 config.output.u.RGBA.size = config.output.u.RGBA.stride * dstDimensions.
height(); |
| 296 } else { |
| 297 config.output.u.RGBA.rgba = (uint8_t*) dst; |
| 298 config.output.u.RGBA.stride = (int) rowBytes; |
| 299 config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); |
| 300 } |
| 301 |
279 WebPIterator frame; | 302 WebPIterator frame; |
280 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame); | 303 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame); |
281 // If this succeeded in NewFromStream(), it should succeed again here. | 304 // If this succeeded in NewFromStream(), it should succeed again here. |
282 SkAssertResult(WebPDemuxGetFrame(fDemux, 1, &frame)); | 305 SkAssertResult(WebPDemuxGetFrame(fDemux, 1, &frame)); |
283 | 306 |
284 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &co
nfig)); | 307 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &co
nfig)); |
285 if (!idec) { | 308 if (!idec) { |
286 return kInvalidInput; | 309 return kInvalidInput; |
287 } | 310 } |
288 | 311 |
| 312 int rowsDecoded; |
| 313 SkCodec::Result result; |
289 switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) { | 314 switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) { |
290 case VP8_STATUS_OK: | 315 case VP8_STATUS_OK: |
291 return kSuccess; | 316 rowsDecoded = dstInfo.height(); |
| 317 result = kSuccess; |
| 318 break; |
292 case VP8_STATUS_SUSPENDED: | 319 case VP8_STATUS_SUSPENDED: |
293 WebPIDecGetRGB(idec, rowsDecoded, nullptr, nullptr, nullptr); | 320 WebPIDecGetRGB(idec, rowsDecodedPtr, nullptr, nullptr, nullptr); |
294 return kIncompleteInput; | 321 rowsDecoded = *rowsDecodedPtr; |
| 322 result = kIncompleteInput; |
| 323 break; |
295 default: | 324 default: |
296 return kInvalidInput; | 325 return kInvalidInput; |
297 } | 326 } |
| 327 |
| 328 if (colorXform) { |
| 329 SkAlphaType xformAlphaType = select_alpha_xform(dstInfo.alphaType(), |
| 330 this->getInfo().alphaTyp
e()); |
| 331 |
| 332 uint32_t* src = (uint32_t*) config.output.u.RGBA.rgba; |
| 333 size_t srcRowBytes = config.output.u.RGBA.stride; |
| 334 for (int y = 0; y < rowsDecoded; y++) { |
| 335 colorXform->apply(dst, src, dstInfo.width(), dstInfo.colorType(), xf
ormAlphaType); |
| 336 dst = SkTAddOffset<void>(dst, rowBytes); |
| 337 src = SkTAddOffset<uint32_t>(src, srcRowBytes); |
| 338 } |
| 339 } |
| 340 |
| 341 return result; |
298 } | 342 } |
299 | 343 |
300 SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info, | 344 SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info, |
301 sk_sp<SkColorSpace> colorSpace, SkStream* stream, WebPD
emuxer* demux, | 345 sk_sp<SkColorSpace> colorSpace, SkStream* stream, WebPD
emuxer* demux, |
302 sk_sp<SkData> data) | 346 sk_sp<SkData> data) |
303 : INHERITED(width, height, info, stream, std::move(colorSpace)) | 347 : INHERITED(width, height, info, stream, std::move(colorSpace)) |
304 , fDemux(demux) | 348 , fDemux(demux) |
305 , fData(std::move(data)) | 349 , fData(std::move(data)) |
306 {} | 350 {} |
OLD | NEW |