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