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