| 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 "SkWebpCodec.h" | 9 #include "SkWebpCodec.h" |
| 10 #include "SkTemplates.h" | 10 #include "SkTemplates.h" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 return MODE_LAST; | 133 return MODE_LAST; |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 | 136 |
| 137 // The WebP decoding API allows us to incrementally pass chunks of bytes as we r
eceive them to the | 137 // The WebP decoding API allows us to incrementally pass chunks of bytes as we r
eceive them to the |
| 138 // decoder with WebPIAppend. In order to do so, we need to read chunks from the
SkStream. This size | 138 // decoder with WebPIAppend. In order to do so, we need to read chunks from the
SkStream. This size |
| 139 // is arbitrary. | 139 // is arbitrary. |
| 140 static const size_t BUFFER_SIZE = 4096; | 140 static const size_t BUFFER_SIZE = 4096; |
| 141 | 141 |
| 142 bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const { | 142 bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const { |
| 143 if (!desiredSubset) { | |
| 144 return false; | |
| 145 } | |
| 146 | |
| 147 SkIRect bounds = SkIRect::MakeSize(this->getInfo().dimensions()); | |
| 148 if (!desiredSubset->intersect(bounds)) { | |
| 149 return false; | |
| 150 } | |
| 151 | |
| 152 // As stated below, libwebp snaps to even left and top. Make sure top and le
ft are even, so we | 143 // As stated below, libwebp snaps to even left and top. Make sure top and le
ft are even, so we |
| 153 // decode this exact subset. | 144 // decode this exact subset. |
| 154 // Leave right and bottom unmodified, so we suggest a slightly larger subset
than requested. | 145 // Leave right and bottom unmodified, so we suggest a slightly larger subset
than requested. |
| 155 desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1; | 146 desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1; |
| 156 desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1; | 147 desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1; |
| 157 return true; | 148 return true; |
| 158 } | 149 } |
| 159 | 150 |
| 151 bool SkWebpCodec::onGetScaledSubsetDimensions(float desiredScale, Options* optio
ns) const { |
| 152 if (!this->onGetValidSubset(options->fSubset)) { |
| 153 return false; |
| 154 } |
| 155 options->fScaledDimensions = SkISize::Make( |
| 156 SkTMax(1, SkScalarRoundToInt(desiredScale * this->getInfo().width())
), |
| 157 SkTMax(1, SkScalarRoundToInt(desiredScale * this->getInfo().height()
))); |
| 158 // Notice that we may round the size of the subset up to 1. This means that
we must |
| 159 // floor the left and top offsets to ensure that we do not suggest a subset
that is |
| 160 // off the edge of the image. |
| 161 options->fScaledSubset = SkIRect::MakeXYWH( |
| 162 int (desiredScale * options->fSubset->left()), |
| 163 int (desiredScale * options->fSubset->top()), |
| 164 SkTMax(1, SkScalarRoundToInt(desiredScale * options->fSubset->width(
))), |
| 165 SkTMax(1, SkScalarRoundToInt(desiredScale * options->fSubset->height
()))); |
| 166 |
| 167 return true; |
| 168 } |
| 169 |
| 160 SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
size_t rowBytes, | 170 SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
size_t rowBytes, |
| 161 const Options& options, SkPMColor*, int
*, | 171 const Options& options, SkPMColor*, int
*, |
| 162 int* rowsDecoded) { | 172 int* rowsDecoded) { |
| 163 if (!webp_conversion_possible(dstInfo, this->getInfo())) { | 173 if (!webp_conversion_possible(dstInfo, this->getInfo())) { |
| 164 return kInvalidConversion; | 174 return kInvalidConversion; |
| 165 } | 175 } |
| 166 | 176 |
| 167 WebPDecoderConfig config; | 177 WebPDecoderConfig config; |
| 168 if (0 == WebPInitDecoderConfig(&config)) { | 178 if (0 == WebPInitDecoderConfig(&config)) { |
| 169 // ABI mismatch. | 179 // ABI mismatch. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 // Caller is requesting scaling. | 225 // Caller is requesting scaling. |
| 216 config.options.use_scaling = 1; | 226 config.options.use_scaling = 1; |
| 217 config.options.scaled_width = dstDimensions.width(); | 227 config.options.scaled_width = dstDimensions.width(); |
| 218 config.options.scaled_height = dstDimensions.height(); | 228 config.options.scaled_height = dstDimensions.height(); |
| 219 } | 229 } |
| 220 | 230 |
| 221 config.output.colorspace = webp_decode_mode(dstInfo.colorType(), | 231 config.output.colorspace = webp_decode_mode(dstInfo.colorType(), |
| 222 dstInfo.alphaType() == kPremul_SkAlphaType); | 232 dstInfo.alphaType() == kPremul_SkAlphaType); |
| 223 config.output.u.RGBA.rgba = (uint8_t*) dst; | 233 config.output.u.RGBA.rgba = (uint8_t*) dst; |
| 224 config.output.u.RGBA.stride = (int) rowBytes; | 234 config.output.u.RGBA.stride = (int) rowBytes; |
| 225 config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); | 235 // TODO: We can use dstInfo.getSafeSize(rowBytes) here because |
| 236 // https://code.google.com/p/webp/issues/detail?id=258 has |
| 237 // been resolved, but it will require an update to libwebp. |
| 238 config.output.u.RGBA.size = rowBytes * dstInfo.height(); |
| 226 config.output.is_external_memory = 1; | 239 config.output.is_external_memory = 1; |
| 227 | 240 |
| 228 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &co
nfig)); | 241 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &co
nfig)); |
| 229 if (!idec) { | 242 if (!idec) { |
| 230 return kInvalidInput; | 243 return kInvalidInput; |
| 231 } | 244 } |
| 232 | 245 |
| 233 SkAutoMalloc storage(BUFFER_SIZE); | 246 SkAutoMalloc storage(BUFFER_SIZE); |
| 234 uint8_t* buffer = static_cast<uint8_t*>(storage.get()); | 247 uint8_t* buffer = static_cast<uint8_t*>(storage.get()); |
| 235 while (true) { | 248 while (true) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 246 // Break out of the switch statement. Continue the loop. | 259 // Break out of the switch statement. Continue the loop. |
| 247 break; | 260 break; |
| 248 default: | 261 default: |
| 249 return kInvalidInput; | 262 return kInvalidInput; |
| 250 } | 263 } |
| 251 } | 264 } |
| 252 } | 265 } |
| 253 | 266 |
| 254 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) | 267 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) |
| 255 : INHERITED(info, stream) {} | 268 : INHERITED(info, stream) {} |
| OLD | NEW |