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) { |
| 176 // Caller is requesting a subset. |
| 177 if (!bounds.contains(*options.fSubset)) { |
| 178 // The subset is out of bounds. |
| 179 return kInvalidParameters; |
| 180 } |
| 181 |
| 182 bounds = *options.fSubset; |
| 183 |
| 184 // This is tricky. libwebp snaps the top and left to even values. We cou
ld let libwebp |
| 185 // do the snap, and return a subset which is a different one than reques
ted. The problem |
| 186 // with that approach is that the caller may try to stitch subsets toget
her, and if we |
| 187 // returned different subsets than requested, there would be artifacts a
t the boundaries. |
| 188 // Instead, we report that we cannot support odd values for top and left
.. |
| 189 if (!SkIsAlign2(bounds.fLeft) || !SkIsAlign2(bounds.fTop)) { |
| 190 return kInvalidParameters; |
| 191 } |
| 192 |
| 193 #ifdef SK_DEBUG |
| 194 { |
| 195 // Make a copy, since getValidSubset can change its input. |
| 196 SkIRect subset(bounds); |
| 197 // That said, getValidSubset should *not* change its input, in this
case; otherwise |
| 198 // getValidSubset does not match the actual subsets we can do. |
| 199 SkASSERT(this->getValidSubset(&subset) && subset == bounds); |
| 200 } |
| 201 #endif |
| 202 |
| 203 config.options.use_cropping = 1; |
| 204 config.options.crop_left = bounds.fLeft; |
| 205 config.options.crop_top = bounds.fTop; |
| 206 config.options.crop_width = bounds.width(); |
| 207 config.options.crop_height = bounds.height(); |
| 208 } |
| 209 |
| 210 SkISize dstDimensions = dstInfo.dimensions(); |
| 211 if (bounds.size() != dstDimensions) { |
158 // Caller is requesting scaling. | 212 // Caller is requesting scaling. |
159 config.options.use_scaling = 1; | 213 config.options.use_scaling = 1; |
160 config.options.scaled_width = dimensions.width(); | 214 config.options.scaled_width = dstDimensions.width(); |
161 config.options.scaled_height = dimensions.height(); | 215 config.options.scaled_height = dstDimensions.height(); |
162 } | 216 } |
163 | 217 |
164 config.output.colorspace = webp_decode_mode(dstInfo.colorType(), | 218 config.output.colorspace = webp_decode_mode(dstInfo.colorType(), |
165 dstInfo.alphaType() == kPremul_SkAlphaType); | 219 dstInfo.alphaType() == kPremul_SkAlphaType); |
166 config.output.u.RGBA.rgba = (uint8_t*) dst; | 220 config.output.u.RGBA.rgba = (uint8_t*) dst; |
167 config.output.u.RGBA.stride = (int) rowBytes; | 221 config.output.u.RGBA.stride = (int) rowBytes; |
168 config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); | 222 config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); |
169 config.output.is_external_memory = 1; | 223 config.output.is_external_memory = 1; |
170 | 224 |
171 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(NULL, 0, &confi
g)); | 225 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. | 245 // Break out of the switch statement. Continue the loop. |
192 break; | 246 break; |
193 default: | 247 default: |
194 return kInvalidInput; | 248 return kInvalidInput; |
195 } | 249 } |
196 } | 250 } |
197 } | 251 } |
198 | 252 |
199 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) | 253 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) |
200 : INHERITED(info, stream) {} | 254 : INHERITED(info, stream) {} |
OLD | NEW |