commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "SkImageDecoder.h" |
| 18 | #include "SkImageEncoder.h" |
| 19 | #include "SkColorPriv.h" |
| 20 | #include "SkScaledBitmapSampler.h" |
| 21 | #include "SkStream.h" |
| 22 | #include "SkTemplates.h" |
| 23 | #include "SkUtils.h" |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 24 | |
| 25 | // A WebP decoder only, on top of (subset of) libwebp |
| 26 | // For more information on WebP image format, and libwebp library, see: |
| 27 | // http://code.google.com/speed/webp/ |
| 28 | // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library |
| 29 | // http://review.webmproject.org/gitweb?p=libwebp.git |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | extern "C" { |
| 33 | // If moving libwebp out of skia source tree, path for webp headers must be |
| 34 | // updated accordingly. Here, we enforce using local copy in webp sub-directory. |
| 35 | #include "webp/decode.h" |
| 36 | #include "webp/encode.h" |
| 37 | } |
| 38 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 39 | // this enables timing code to report milliseconds for a decode |
| 40 | //#define TIME_DECODE |
| 41 | |
| 42 | ////////////////////////////////////////////////////////////////////////// |
| 43 | ////////////////////////////////////////////////////////////////////////// |
| 44 | |
| 45 | // Define VP8 I/O on top of Skia stream |
| 46 | |
| 47 | ////////////////////////////////////////////////////////////////////////// |
| 48 | ////////////////////////////////////////////////////////////////////////// |
| 49 | |
| 50 | static const size_t WEBP_VP8_HEADER_SIZE = 64; |
| 51 | static const size_t WEBP_IDECODE_BUFFER_SZ = (1 << 16); |
| 52 | |
| 53 | // Parse headers of RIFF container, and check for valid Webp (VP8) content. |
| 54 | static bool webp_parse_header(SkStream* stream, int* width, int* height, int* alpha) { |
| 55 | unsigned char buffer[WEBP_VP8_HEADER_SIZE]; |
scroggo@google.com | 3c8730a | 2013-08-21 14:56:09 +0000 | [diff] [blame] | 56 | size_t bytesToRead = WEBP_VP8_HEADER_SIZE; |
| 57 | size_t totalBytesRead = 0; |
| 58 | do { |
| 59 | unsigned char* dst = buffer + totalBytesRead; |
| 60 | const size_t bytesRead = stream->read(dst, bytesToRead); |
| 61 | if (0 == bytesRead) { |
| 62 | // Could not read any bytes. Check to see if we are at the end (exit |
| 63 | // condition), and continue reading if not. Important for streams |
| 64 | // that do not have all the data ready. |
| 65 | continue; |
| 66 | } |
| 67 | bytesToRead -= bytesRead; |
| 68 | totalBytesRead += bytesRead; |
| 69 | SkASSERT(bytesToRead + totalBytesRead == WEBP_VP8_HEADER_SIZE); |
| 70 | } while (!stream->isAtEnd() && bytesToRead > 0); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 71 | |
| 72 | WebPBitstreamFeatures features; |
scroggo@google.com | 3c8730a | 2013-08-21 14:56:09 +0000 | [diff] [blame] | 73 | VP8StatusCode status = WebPGetFeatures(buffer, totalBytesRead, &features); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 74 | if (VP8_STATUS_OK != status) { |
| 75 | return false; // Invalid WebP file. |
| 76 | } |
| 77 | *width = features.width; |
| 78 | *height = features.height; |
| 79 | *alpha = features.has_alpha; |
| 80 | |
| 81 | // sanity check for image size that's about to be decoded. |
| 82 | { |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 83 | int64_t size = sk_64_mul(*width, *height); |
| 84 | if (!sk_64_isS32(size)) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | // now check that if we are 4-bytes per pixel, we also don't overflow |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 88 | if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | class SkWEBPImageDecoder: public SkImageDecoder { |
| 96 | public: |
| 97 | SkWEBPImageDecoder() { |
| 98 | fInputStream = NULL; |
| 99 | fOrigWidth = 0; |
| 100 | fOrigHeight = 0; |
| 101 | fHasAlpha = 0; |
| 102 | } |
| 103 | virtual ~SkWEBPImageDecoder() { |
| 104 | SkSafeUnref(fInputStream); |
| 105 | } |
| 106 | |
| 107 | virtual Format getFormat() const SK_OVERRIDE { |
| 108 | return kWEBP_Format; |
| 109 | } |
| 110 | |
| 111 | protected: |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 112 | virtual bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *height) SK_OVERRIDE; |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 113 | virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRIDE; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 114 | virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; |
| 115 | |
| 116 | private: |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 117 | /** |
| 118 | * Called when determining the output config to request to webp. |
| 119 | * If the image does not have alpha, there is no need to premultiply. |
| 120 | * If the caller wants unpremultiplied colors, that is respected. |
| 121 | */ |
| 122 | bool shouldPremultiply() const { |
| 123 | return SkToBool(fHasAlpha) && !this->getRequireUnpremultipliedColors(); |
| 124 | } |
| 125 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 126 | bool setDecodeConfig(SkBitmap* decodedBitmap, int width, int height); |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 127 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 128 | SkStream* fInputStream; |
| 129 | int fOrigWidth; |
| 130 | int fOrigHeight; |
| 131 | int fHasAlpha; |
| 132 | |
| 133 | typedef SkImageDecoder INHERITED; |
| 134 | }; |
| 135 | |
| 136 | ////////////////////////////////////////////////////////////////////////// |
| 137 | |
| 138 | #ifdef TIME_DECODE |
| 139 | |
| 140 | #include "SkTime.h" |
| 141 | |
| 142 | class AutoTimeMillis { |
| 143 | public: |
| 144 | AutoTimeMillis(const char label[]) : |
| 145 | fLabel(label) { |
| 146 | if (NULL == fLabel) { |
| 147 | fLabel = ""; |
| 148 | } |
| 149 | fNow = SkTime::GetMSecs(); |
| 150 | } |
| 151 | ~AutoTimeMillis() { |
| 152 | SkDebugf("---- Time (ms): %s %d\n", fLabel, SkTime::GetMSecs() - fNow); |
| 153 | } |
| 154 | private: |
| 155 | const char* fLabel; |
| 156 | SkMSec fNow; |
| 157 | }; |
| 158 | |
| 159 | #endif |
| 160 | |
| 161 | /////////////////////////////////////////////////////////////////////////////// |
| 162 | |
| 163 | // This guy exists just to aid in debugging, as it allows debuggers to just |
| 164 | // set a break-point in one place to see all error exists. |
| 165 | static bool return_false(const SkBitmap& bm, const char msg[]) { |
| 166 | SkDEBUGF(("libwebp error %s [%d %d]", msg, bm.width(), bm.height())); |
| 167 | return false; // must always return false |
| 168 | } |
| 169 | |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 170 | static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, bool premultiply) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 171 | WEBP_CSP_MODE mode = MODE_LAST; |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 172 | const SkColorType ct = decodedBitmap->colorType(); |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 173 | |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 174 | if (ct == kN32_SkColorType) { |
halcanary@google.com | dedd44a | 2013-12-20 16:35:22 +0000 | [diff] [blame] | 175 | #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) |
| 176 | mode = premultiply ? MODE_bgrA : MODE_BGRA; |
| 177 | #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) |
| 178 | mode = premultiply ? MODE_rgbA : MODE_RGBA; |
| 179 | #else |
| 180 | #error "Skia uses BGRA or RGBA byte order" |
| 181 | #endif |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 182 | } else if (ct == kARGB_4444_SkColorType) { |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 183 | mode = premultiply ? MODE_rgbA_4444 : MODE_RGBA_4444; |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 184 | } else if (ct == kRGB_565_SkColorType) { |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 185 | mode = MODE_RGB_565; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 186 | } |
| 187 | SkASSERT(MODE_LAST != mode); |
| 188 | return mode; |
| 189 | } |
| 190 | |
| 191 | // Incremental WebP image decoding. Reads input buffer of 64K size iteratively |
| 192 | // and decodes this block to appropriate color-space as per config object. |
| 193 | static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) { |
| 194 | WebPIDecoder* idec = WebPIDecode(NULL, 0, config); |
| 195 | if (NULL == idec) { |
| 196 | WebPFreeDecBuffer(&config->output); |
| 197 | return false; |
| 198 | } |
| 199 | |
scroggo@google.com | 4d213ab | 2013-08-28 13:08:54 +0000 | [diff] [blame] | 200 | if (!stream->rewind()) { |
| 201 | SkDebugf("Failed to rewind webp stream!"); |
| 202 | return false; |
| 203 | } |
scroggo@google.com | 3c8730a | 2013-08-21 14:56:09 +0000 | [diff] [blame] | 204 | const size_t readBufferSize = stream->hasLength() ? |
| 205 | SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_BUFFER_SZ; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 206 | SkAutoMalloc srcStorage(readBufferSize); |
| 207 | unsigned char* input = (uint8_t*)srcStorage.get(); |
| 208 | if (NULL == input) { |
| 209 | WebPIDelete(idec); |
| 210 | WebPFreeDecBuffer(&config->output); |
| 211 | return false; |
| 212 | } |
| 213 | |
scroggo@google.com | 80e18c9 | 2013-08-09 19:22:00 +0000 | [diff] [blame] | 214 | bool success = true; |
| 215 | VP8StatusCode status = VP8_STATUS_SUSPENDED; |
| 216 | do { |
| 217 | const size_t bytesRead = stream->read(input, readBufferSize); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 218 | if (0 == bytesRead) { |
scroggo@google.com | 80e18c9 | 2013-08-09 19:22:00 +0000 | [diff] [blame] | 219 | success = false; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 220 | break; |
| 221 | } |
| 222 | |
scroggo@google.com | 80e18c9 | 2013-08-09 19:22:00 +0000 | [diff] [blame] | 223 | status = WebPIAppend(idec, input, bytesRead); |
| 224 | if (VP8_STATUS_OK != status && VP8_STATUS_SUSPENDED != status) { |
| 225 | success = false; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 226 | break; |
| 227 | } |
scroggo@google.com | 80e18c9 | 2013-08-09 19:22:00 +0000 | [diff] [blame] | 228 | } while (VP8_STATUS_OK != status); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 229 | srcStorage.free(); |
| 230 | WebPIDelete(idec); |
| 231 | WebPFreeDecBuffer(&config->output); |
| 232 | |
scroggo@google.com | 80e18c9 | 2013-08-09 19:22:00 +0000 | [diff] [blame] | 233 | return success; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static bool webp_get_config_resize(WebPDecoderConfig* config, |
| 237 | SkBitmap* decodedBitmap, |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 238 | int width, int height, bool premultiply) { |
| 239 | WEBP_CSP_MODE mode = webp_decode_mode(decodedBitmap, premultiply); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 240 | if (MODE_LAST == mode) { |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | if (0 == WebPInitDecoderConfig(config)) { |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | config->output.colorspace = mode; |
| 249 | config->output.u.RGBA.rgba = (uint8_t*)decodedBitmap->getPixels(); |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 250 | config->output.u.RGBA.stride = (int) decodedBitmap->rowBytes(); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 251 | config->output.u.RGBA.size = decodedBitmap->getSize(); |
| 252 | config->output.is_external_memory = 1; |
| 253 | |
| 254 | if (width != decodedBitmap->width() || height != decodedBitmap->height()) { |
| 255 | config->options.use_scaling = 1; |
| 256 | config->options.scaled_width = decodedBitmap->width(); |
| 257 | config->options.scaled_height = decodedBitmap->height(); |
| 258 | } |
| 259 | |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | static bool webp_get_config_resize_crop(WebPDecoderConfig* config, |
| 264 | SkBitmap* decodedBitmap, |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 265 | const SkIRect& region, bool premultiply) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 266 | |
| 267 | if (!webp_get_config_resize(config, decodedBitmap, region.width(), |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 268 | region.height(), premultiply)) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | |
| 272 | config->options.use_cropping = 1; |
| 273 | config->options.crop_left = region.fLeft; |
| 274 | config->options.crop_top = region.fTop; |
| 275 | config->options.crop_width = region.width(); |
| 276 | config->options.crop_height = region.height(); |
| 277 | |
| 278 | return true; |
| 279 | } |
| 280 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 281 | bool SkWEBPImageDecoder::setDecodeConfig(SkBitmap* decodedBitmap, int width, int height) { |
| 282 | SkColorType colorType = this->getPrefColorType(k32Bit_SrcDepth, SkToBool(fHasAlpha)); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 283 | |
| 284 | // YUV converter supports output in RGB565, RGBA4444 and RGBA8888 formats. |
| 285 | if (fHasAlpha) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 286 | if (colorType != kARGB_4444_SkColorType) { |
| 287 | colorType = kN32_SkColorType; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 288 | } |
| 289 | } else { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 290 | if (colorType != kRGB_565_SkColorType && colorType != kARGB_4444_SkColorType) { |
| 291 | colorType = kN32_SkColorType; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame] | 295 | #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 296 | if (!this->chooseFromOneChoice(colorType, width, height)) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 297 | return false; |
| 298 | } |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame] | 299 | #endif |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 300 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 301 | SkAlphaType alphaType = kOpaque_SkAlphaType; |
commit-bot@chromium.org | 915b972 | 2014-04-24 18:55:13 +0000 | [diff] [blame] | 302 | if (SkToBool(fHasAlpha)) { |
| 303 | if (this->getRequireUnpremultipliedColors()) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 304 | alphaType = kUnpremul_SkAlphaType; |
commit-bot@chromium.org | 915b972 | 2014-04-24 18:55:13 +0000 | [diff] [blame] | 305 | } else { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 306 | alphaType = kPremul_SkAlphaType; |
commit-bot@chromium.org | 915b972 | 2014-04-24 18:55:13 +0000 | [diff] [blame] | 307 | } |
commit-bot@chromium.org | 915b972 | 2014-04-24 18:55:13 +0000 | [diff] [blame] | 308 | } |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 309 | return decodedBitmap->setInfo(SkImageInfo::Make(width, height, colorType, alphaType)); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 310 | } |
| 311 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 312 | bool SkWEBPImageDecoder::onBuildTileIndex(SkStreamRewindable* stream, |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 313 | int *width, int *height) { |
| 314 | int origWidth, origHeight, hasAlpha; |
| 315 | if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) { |
| 316 | return false; |
| 317 | } |
| 318 | |
scroggo@google.com | 4d213ab | 2013-08-28 13:08:54 +0000 | [diff] [blame] | 319 | if (!stream->rewind()) { |
| 320 | SkDebugf("Failed to rewind webp stream!"); |
| 321 | return false; |
| 322 | } |
| 323 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 324 | *width = origWidth; |
| 325 | *height = origHeight; |
| 326 | |
| 327 | SkRefCnt_SafeAssign(this->fInputStream, stream); |
| 328 | this->fOrigWidth = origWidth; |
| 329 | this->fOrigHeight = origHeight; |
| 330 | this->fHasAlpha = hasAlpha; |
| 331 | |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | static bool is_config_compatible(const SkBitmap& bitmap) { |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 336 | const SkColorType ct = bitmap.colorType(); |
| 337 | return ct == kARGB_4444_SkColorType || ct == kRGB_565_SkColorType || ct == kN32_SkColorType; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 338 | } |
| 339 | |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 340 | bool SkWEBPImageDecoder::onDecodeSubset(SkBitmap* decodedBitmap, |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 341 | const SkIRect& region) { |
| 342 | SkIRect rect = SkIRect::MakeWH(fOrigWidth, fOrigHeight); |
| 343 | |
| 344 | if (!rect.intersect(region)) { |
| 345 | // If the requested region is entirely outsides the image, return false |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | const int sampleSize = this->getSampleSize(); |
| 350 | SkScaledBitmapSampler sampler(rect.width(), rect.height(), sampleSize); |
| 351 | const int width = sampler.scaledWidth(); |
| 352 | const int height = sampler.scaledHeight(); |
| 353 | |
| 354 | // The image can be decoded directly to decodedBitmap if |
| 355 | // 1. the region is within the image range |
| 356 | // 2. bitmap's config is compatible |
| 357 | // 3. bitmap's size is same as the required region (after sampled) |
| 358 | bool directDecode = (rect == region) && |
| 359 | (decodedBitmap->isNull() || |
| 360 | (is_config_compatible(*decodedBitmap) && |
| 361 | (decodedBitmap->width() == width) && |
| 362 | (decodedBitmap->height() == height))); |
commit-bot@chromium.org | a9f142e | 2013-05-09 16:15:20 +0000 | [diff] [blame] | 363 | |
| 364 | SkBitmap tmpBitmap; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 365 | SkBitmap *bitmap = decodedBitmap; |
| 366 | |
| 367 | if (!directDecode) { |
commit-bot@chromium.org | a9f142e | 2013-05-09 16:15:20 +0000 | [diff] [blame] | 368 | bitmap = &tmpBitmap; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | if (bitmap->isNull()) { |
| 372 | if (!setDecodeConfig(bitmap, width, height)) { |
| 373 | return false; |
| 374 | } |
| 375 | // alloc from native heap if it is a temp bitmap. (prevent GC) |
| 376 | bool allocResult = (bitmap == decodedBitmap) |
| 377 | ? allocPixelRef(bitmap, NULL) |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 378 | : bitmap->tryAllocPixels(); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 379 | if (!allocResult) { |
| 380 | return return_false(*decodedBitmap, "allocPixelRef"); |
| 381 | } |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame] | 382 | #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 383 | } else { |
| 384 | // This is also called in setDecodeConfig in above block. |
| 385 | // i.e., when bitmap->isNull() is true. |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 386 | if (!chooseFromOneChoice(bitmap->colorType(), width, height)) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 387 | return false; |
| 388 | } |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame] | 389 | #endif |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | SkAutoLockPixels alp(*bitmap); |
| 393 | WebPDecoderConfig config; |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 394 | if (!webp_get_config_resize_crop(&config, bitmap, rect, |
| 395 | this->shouldPremultiply())) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 396 | return false; |
| 397 | } |
| 398 | |
| 399 | // Decode the WebP image data stream using WebP incremental decoding for |
| 400 | // the specified cropped image-region. |
| 401 | if (!webp_idecode(this->fInputStream, &config)) { |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | if (!directDecode) { |
| 406 | cropBitmap(decodedBitmap, bitmap, sampleSize, region.x(), region.y(), |
| 407 | region.width(), region.height(), rect.x(), rect.y()); |
| 408 | } |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | bool SkWEBPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap, |
| 413 | Mode mode) { |
| 414 | #ifdef TIME_DECODE |
| 415 | AutoTimeMillis atm("WEBP Decode"); |
| 416 | #endif |
| 417 | |
| 418 | int origWidth, origHeight, hasAlpha; |
| 419 | if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) { |
| 420 | return false; |
| 421 | } |
| 422 | this->fHasAlpha = hasAlpha; |
| 423 | |
| 424 | const int sampleSize = this->getSampleSize(); |
| 425 | SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 426 | if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(), |
| 427 | sampler.scaledHeight())) { |
| 428 | return false; |
| 429 | } |
| 430 | |
scroggo@google.com | bc69ce9 | 2013-07-09 15:45:14 +0000 | [diff] [blame] | 431 | // If only bounds are requested, done |
| 432 | if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
| 433 | return true; |
| 434 | } |
| 435 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 436 | if (!this->allocPixelRef(decodedBitmap, NULL)) { |
| 437 | return return_false(*decodedBitmap, "allocPixelRef"); |
| 438 | } |
| 439 | |
| 440 | SkAutoLockPixels alp(*decodedBitmap); |
| 441 | |
| 442 | WebPDecoderConfig config; |
| 443 | if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight, |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 444 | this->shouldPremultiply())) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 445 | return false; |
| 446 | } |
| 447 | |
| 448 | // Decode the WebP image data stream using WebP incremental decoding. |
| 449 | return webp_idecode(stream, &config); |
| 450 | } |
| 451 | |
| 452 | /////////////////////////////////////////////////////////////////////////////// |
| 453 | |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 454 | #include "SkUnPreMultiply.h" |
| 455 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 456 | typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width, |
| 457 | const SkPMColor* SK_RESTRICT ctable); |
| 458 | |
| 459 | static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width, |
| 460 | const SkPMColor*) { |
| 461 | const uint32_t* SK_RESTRICT src = (const uint32_t*)in; |
| 462 | for (int i = 0; i < width; ++i) { |
| 463 | const uint32_t c = *src++; |
| 464 | rgb[0] = SkGetPackedR32(c); |
| 465 | rgb[1] = SkGetPackedG32(c); |
| 466 | rgb[2] = SkGetPackedB32(c); |
| 467 | rgb += 3; |
| 468 | } |
| 469 | } |
| 470 | |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 471 | static void ARGB_8888_To_RGBA(const uint8_t* in, uint8_t* rgb, int width, |
| 472 | const SkPMColor*) { |
| 473 | const uint32_t* SK_RESTRICT src = (const uint32_t*)in; |
| 474 | const SkUnPreMultiply::Scale* SK_RESTRICT table = |
| 475 | SkUnPreMultiply::GetScaleTable(); |
| 476 | for (int i = 0; i < width; ++i) { |
| 477 | const uint32_t c = *src++; |
| 478 | uint8_t a = SkGetPackedA32(c); |
| 479 | uint8_t r = SkGetPackedR32(c); |
| 480 | uint8_t g = SkGetPackedG32(c); |
| 481 | uint8_t b = SkGetPackedB32(c); |
| 482 | if (0 != a && 255 != a) { |
| 483 | SkUnPreMultiply::Scale scale = table[a]; |
| 484 | r = SkUnPreMultiply::ApplyScale(scale, r); |
| 485 | g = SkUnPreMultiply::ApplyScale(scale, g); |
| 486 | b = SkUnPreMultiply::ApplyScale(scale, b); |
| 487 | } |
| 488 | rgb[0] = r; |
| 489 | rgb[1] = g; |
| 490 | rgb[2] = b; |
| 491 | rgb[3] = a; |
| 492 | rgb += 4; |
| 493 | } |
| 494 | } |
| 495 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 496 | static void RGB_565_To_RGB(const uint8_t* in, uint8_t* rgb, int width, |
| 497 | const SkPMColor*) { |
| 498 | const uint16_t* SK_RESTRICT src = (const uint16_t*)in; |
| 499 | for (int i = 0; i < width; ++i) { |
| 500 | const uint16_t c = *src++; |
| 501 | rgb[0] = SkPacked16ToR32(c); |
| 502 | rgb[1] = SkPacked16ToG32(c); |
| 503 | rgb[2] = SkPacked16ToB32(c); |
| 504 | rgb += 3; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | static void ARGB_4444_To_RGB(const uint8_t* in, uint8_t* rgb, int width, |
| 509 | const SkPMColor*) { |
| 510 | const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in; |
| 511 | for (int i = 0; i < width; ++i) { |
| 512 | const SkPMColor16 c = *src++; |
| 513 | rgb[0] = SkPacked4444ToR32(c); |
| 514 | rgb[1] = SkPacked4444ToG32(c); |
| 515 | rgb[2] = SkPacked4444ToB32(c); |
| 516 | rgb += 3; |
| 517 | } |
| 518 | } |
| 519 | |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 520 | static void ARGB_4444_To_RGBA(const uint8_t* in, uint8_t* rgb, int width, |
| 521 | const SkPMColor*) { |
| 522 | const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in; |
| 523 | const SkUnPreMultiply::Scale* SK_RESTRICT table = |
| 524 | SkUnPreMultiply::GetScaleTable(); |
| 525 | for (int i = 0; i < width; ++i) { |
| 526 | const SkPMColor16 c = *src++; |
| 527 | uint8_t a = SkPacked4444ToA32(c); |
| 528 | uint8_t r = SkPacked4444ToR32(c); |
| 529 | uint8_t g = SkPacked4444ToG32(c); |
| 530 | uint8_t b = SkPacked4444ToB32(c); |
| 531 | if (0 != a && 255 != a) { |
| 532 | SkUnPreMultiply::Scale scale = table[a]; |
| 533 | r = SkUnPreMultiply::ApplyScale(scale, r); |
| 534 | g = SkUnPreMultiply::ApplyScale(scale, g); |
| 535 | b = SkUnPreMultiply::ApplyScale(scale, b); |
| 536 | } |
| 537 | rgb[0] = r; |
| 538 | rgb[1] = g; |
| 539 | rgb[2] = b; |
| 540 | rgb[3] = a; |
| 541 | rgb += 4; |
| 542 | } |
| 543 | } |
| 544 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 545 | static void Index8_To_RGB(const uint8_t* in, uint8_t* rgb, int width, |
| 546 | const SkPMColor* SK_RESTRICT ctable) { |
| 547 | const uint8_t* SK_RESTRICT src = (const uint8_t*)in; |
| 548 | for (int i = 0; i < width; ++i) { |
| 549 | const uint32_t c = ctable[*src++]; |
| 550 | rgb[0] = SkGetPackedR32(c); |
| 551 | rgb[1] = SkGetPackedG32(c); |
| 552 | rgb[2] = SkGetPackedB32(c); |
| 553 | rgb += 3; |
| 554 | } |
| 555 | } |
| 556 | |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 557 | static ScanlineImporter ChooseImporter(SkColorType ct, bool hasAlpha, int* bpp) { |
| 558 | switch (ct) { |
| 559 | case kN32_SkColorType: |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 560 | if (hasAlpha) { |
| 561 | *bpp = 4; |
| 562 | return ARGB_8888_To_RGBA; |
| 563 | } else { |
| 564 | *bpp = 3; |
| 565 | return ARGB_8888_To_RGB; |
| 566 | } |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 567 | case kARGB_4444_SkColorType: |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 568 | if (hasAlpha) { |
| 569 | *bpp = 4; |
| 570 | return ARGB_4444_To_RGBA; |
| 571 | } else { |
| 572 | *bpp = 3; |
| 573 | return ARGB_4444_To_RGB; |
| 574 | } |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 575 | case kRGB_565_SkColorType: |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 576 | *bpp = 3; |
| 577 | return RGB_565_To_RGB; |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 578 | case kIndex_8_SkColorType: |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 579 | *bpp = 3; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 580 | return Index8_To_RGB; |
| 581 | default: |
| 582 | return NULL; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | static int stream_writer(const uint8_t* data, size_t data_size, |
| 587 | const WebPPicture* const picture) { |
| 588 | SkWStream* const stream = (SkWStream*)picture->custom_ptr; |
| 589 | return stream->write(data, data_size) ? 1 : 0; |
| 590 | } |
| 591 | |
| 592 | class SkWEBPImageEncoder : public SkImageEncoder { |
| 593 | protected: |
| 594 | virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE; |
| 595 | |
| 596 | private: |
| 597 | typedef SkImageEncoder INHERITED; |
| 598 | }; |
| 599 | |
| 600 | bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm, |
| 601 | int quality) { |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 602 | const bool hasAlpha = !bm.isOpaque(); |
| 603 | int bpp = -1; |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 604 | const ScanlineImporter scanline_import = ChooseImporter(bm.colorType(), hasAlpha, &bpp); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 605 | if (NULL == scanline_import) { |
| 606 | return false; |
| 607 | } |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 608 | if (-1 == bpp) { |
| 609 | return false; |
| 610 | } |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 611 | |
| 612 | SkAutoLockPixels alp(bm); |
| 613 | SkAutoLockColors ctLocker; |
| 614 | if (NULL == bm.getPixels()) { |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | WebPConfig webp_config; |
commit-bot@chromium.org | bff83f6 | 2013-03-14 15:18:08 +0000 | [diff] [blame] | 619 | if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 620 | return false; |
| 621 | } |
| 622 | |
| 623 | WebPPicture pic; |
| 624 | WebPPictureInit(&pic); |
| 625 | pic.width = bm.width(); |
| 626 | pic.height = bm.height(); |
| 627 | pic.writer = stream_writer; |
| 628 | pic.custom_ptr = (void*)stream; |
| 629 | |
| 630 | const SkPMColor* colors = ctLocker.lockColors(bm); |
| 631 | const uint8_t* src = (uint8_t*)bm.getPixels(); |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 632 | const int rgbStride = pic.width * bpp; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 633 | |
| 634 | // Import (for each scanline) the bit-map image (in appropriate color-space) |
| 635 | // to RGB color space. |
| 636 | uint8_t* rgb = new uint8_t[rgbStride * pic.height]; |
| 637 | for (int y = 0; y < pic.height; ++y) { |
| 638 | scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride, |
| 639 | pic.width, colors); |
| 640 | } |
| 641 | |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 642 | bool ok; |
| 643 | if (bpp == 3) { |
| 644 | ok = SkToBool(WebPPictureImportRGB(&pic, rgb, rgbStride)); |
| 645 | } else { |
| 646 | ok = SkToBool(WebPPictureImportRGBA(&pic, rgb, rgbStride)); |
| 647 | } |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 648 | delete[] rgb; |
| 649 | |
| 650 | ok = ok && WebPEncode(&webp_config, &pic); |
| 651 | WebPPictureFree(&pic); |
| 652 | |
| 653 | return ok; |
| 654 | } |
| 655 | |
| 656 | |
| 657 | /////////////////////////////////////////////////////////////////////////////// |
| 658 | DEFINE_DECODER_CREATOR(WEBPImageDecoder); |
| 659 | DEFINE_ENCODER_CREATOR(WEBPImageEncoder); |
| 660 | /////////////////////////////////////////////////////////////////////////////// |
| 661 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 662 | static SkImageDecoder* sk_libwebp_dfactory(SkStreamRewindable* stream) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 663 | int width, height, hasAlpha; |
| 664 | if (!webp_parse_header(stream, &width, &height, &hasAlpha)) { |
| 665 | return NULL; |
| 666 | } |
| 667 | |
| 668 | // Magic matches, call decoder |
| 669 | return SkNEW(SkWEBPImageDecoder); |
| 670 | } |
| 671 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 672 | static SkImageDecoder::Format get_format_webp(SkStreamRewindable* stream) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 673 | int width, height, hasAlpha; |
| 674 | if (webp_parse_header(stream, &width, &height, &hasAlpha)) { |
| 675 | return SkImageDecoder::kWEBP_Format; |
| 676 | } |
| 677 | return SkImageDecoder::kUnknown_Format; |
| 678 | } |
| 679 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 680 | static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { |
| 681 | return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NULL; |
| 682 | } |
| 683 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 684 | static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory); |
| 685 | static SkImageDecoder_FormatReg gFormatReg(get_format_webp); |
| 686 | static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory); |