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 "SkWebpCodec.h" | 9 #include "SkWebpCodec.h" |
9 #include "SkTemplates.h" | 10 #include "SkTemplates.h" |
10 | 11 |
11 // A WebP decoder on top of (subset of) libwebp | 12 // A WebP decoder on top of (subset of) libwebp |
12 // For more information on WebP image format, and libwebp library, see: | 13 // For more information on WebP image format, and libwebp library, see: |
13 // https://code.google.com/speed/webp/ | 14 // https://code.google.com/speed/webp/ |
14 // http://www.webmproject.org/code/#libwebp-webp-image-library | 15 // http://www.webmproject.org/code/#libwebp-webp-image-library |
15 // https://chromium.googlesource.com/webm/libwebp | 16 // https://chromium.googlesource.com/webm/libwebp |
16 | 17 |
17 // If moving libwebp out of skia source tree, path for webp headers must be | 18 // If moving libwebp out of skia source tree, path for webp headers must be |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 75 |
75 SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { | 76 SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { |
76 SkAutoTDelete<SkStream> streamDeleter(stream); | 77 SkAutoTDelete<SkStream> streamDeleter(stream); |
77 SkImageInfo info; | 78 SkImageInfo info; |
78 if (webp_parse_header(stream, &info)) { | 79 if (webp_parse_header(stream, &info)) { |
79 return SkNEW_ARGS(SkWebpCodec, (info, streamDeleter.detach())); | 80 return SkNEW_ARGS(SkWebpCodec, (info, streamDeleter.detach())); |
80 } | 81 } |
81 return NULL; | 82 return NULL; |
82 } | 83 } |
83 | 84 |
84 static bool conversion_possible(const SkImageInfo& dst, const SkImageInfo& src)
{ | 85 // This version is slightly different from SkCodecPriv's version of conversion_p
ossible. It |
| 86 // supports both byte orders for 8888. |
| 87 static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo&
src) { |
85 if (dst.profileType() != src.profileType()) { | 88 if (dst.profileType() != src.profileType()) { |
86 return false; | 89 return false; |
87 } | 90 } |
| 91 |
| 92 if (!valid_alpha(dst.alphaType(), src.alphaType())) { |
| 93 return false; |
| 94 } |
| 95 |
88 switch (dst.colorType()) { | 96 switch (dst.colorType()) { |
89 // Both byte orders are supported. | 97 // Both byte orders are supported. |
90 case kBGRA_8888_SkColorType: | 98 case kBGRA_8888_SkColorType: |
91 case kRGBA_8888_SkColorType: | 99 case kRGBA_8888_SkColorType: |
92 break; | 100 return true; |
93 case kRGB_565_SkColorType: | 101 case kRGB_565_SkColorType: |
94 if (src.alphaType() == kOpaque_SkAlphaType | 102 return src.alphaType() == kOpaque_SkAlphaType; |
95 && dst.alphaType() == kOpaque_SkAlphaType) | |
96 { | |
97 return true; | |
98 } | |
99 default: | 103 default: |
100 return false; | 104 return false; |
101 } | 105 } |
102 if (dst.alphaType() == src.alphaType()) { | |
103 return true; | |
104 } | |
105 return kPremul_SkAlphaType == dst.alphaType() && | |
106 kUnpremul_SkAlphaType == src.alphaType(); | |
107 } | 106 } |
108 | 107 |
109 SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const { | 108 SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const { |
110 SkISize dim = this->getInfo().dimensions(); | 109 SkISize dim = this->getInfo().dimensions(); |
111 // SkCodec treats zero dimensional images as errors, so the minimum size | 110 // SkCodec treats zero dimensional images as errors, so the minimum size |
112 // that we will recommend is 1x1. | 111 // that we will recommend is 1x1. |
113 dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth)); | 112 dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth)); |
114 dim.fHeight = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fHeight)); | 113 dim.fHeight = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fHeight)); |
115 return dim; | 114 return dim; |
116 } | 115 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1; | 149 desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1; |
151 return true; | 150 return true; |
152 } | 151 } |
153 | 152 |
154 SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
size_t rowBytes, | 153 SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
size_t rowBytes, |
155 const Options& options, SkPMColor*, int
*) { | 154 const Options& options, SkPMColor*, int
*) { |
156 if (!this->rewindIfNeeded()) { | 155 if (!this->rewindIfNeeded()) { |
157 return kCouldNotRewind; | 156 return kCouldNotRewind; |
158 } | 157 } |
159 | 158 |
160 if (!conversion_possible(dstInfo, this->getInfo())) { | 159 if (!webp_conversion_possible(dstInfo, this->getInfo())) { |
161 return kInvalidConversion; | 160 return kInvalidConversion; |
162 } | 161 } |
163 | 162 |
164 WebPDecoderConfig config; | 163 WebPDecoderConfig config; |
165 if (0 == WebPInitDecoderConfig(&config)) { | 164 if (0 == WebPInitDecoderConfig(&config)) { |
166 // ABI mismatch. | 165 // ABI mismatch. |
167 // FIXME: New enum for this? | 166 // FIXME: New enum for this? |
168 return kInvalidInput; | 167 return kInvalidInput; |
169 } | 168 } |
170 | 169 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 // Break out of the switch statement. Continue the loop. | 244 // Break out of the switch statement. Continue the loop. |
246 break; | 245 break; |
247 default: | 246 default: |
248 return kInvalidInput; | 247 return kInvalidInput; |
249 } | 248 } |
250 } | 249 } |
251 } | 250 } |
252 | 251 |
253 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) | 252 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) |
254 : INHERITED(info, stream) {} | 253 : INHERITED(info, stream) {} |
OLD | NEW |