gfx/skia/trunk/src/images/SkImageDecoder_libwebp.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial