OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010, The Android Open Source Project | 2 * Copyright 2010, The Android Open Source Project |
3 * | 3 * |
4 * Licensed under the Apache License, Version 2.0 (the "License"); | 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
5 * you may not use this file except in compliance with 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 | 6 * You may obtain a copy of the License at |
7 * | 7 * |
8 * http://www.apache.org/licenses/LICENSE-2.0 | 8 * http://www.apache.org/licenses/LICENSE-2.0 |
9 * | 9 * |
10 * Unless required by applicable law or agreed to in writing, software | 10 * Unless required by applicable law or agreed to in writing, software |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 SkSafeUnref(fInputStream); | 104 SkSafeUnref(fInputStream); |
105 } | 105 } |
106 | 106 |
107 virtual Format getFormat() const SK_OVERRIDE { | 107 virtual Format getFormat() const SK_OVERRIDE { |
108 return kWEBP_Format; | 108 return kWEBP_Format; |
109 } | 109 } |
110 | 110 |
111 protected: | 111 protected: |
112 virtual bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *h
eight) SK_OVERRIDE; | 112 virtual bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *h
eight) SK_OVERRIDE; |
113 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRI
DE; | 113 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRI
DE; |
114 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; | 114 virtual Result onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; |
115 | 115 |
116 private: | 116 private: |
117 /** | 117 /** |
118 * Called when determining the output config to request to webp. | 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. | 119 * If the image does not have alpha, there is no need to premultiply. |
120 * If the caller wants unpremultiplied colors, that is respected. | 120 * If the caller wants unpremultiplied colors, that is respected. |
121 */ | 121 */ |
122 bool shouldPremultiply() const { | 122 bool shouldPremultiply() const { |
123 return SkToBool(fHasAlpha) && !this->getRequireUnpremultipliedColors(); | 123 return SkToBool(fHasAlpha) && !this->getRequireUnpremultipliedColors(); |
124 } | 124 } |
(...skipping 30 matching lines...) Expand all Loading... |
155 const char* fLabel; | 155 const char* fLabel; |
156 SkMSec fNow; | 156 SkMSec fNow; |
157 }; | 157 }; |
158 | 158 |
159 #endif | 159 #endif |
160 | 160 |
161 /////////////////////////////////////////////////////////////////////////////// | 161 /////////////////////////////////////////////////////////////////////////////// |
162 | 162 |
163 // This guy exists just to aid in debugging, as it allows debuggers to just | 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. | 164 // set a break-point in one place to see all error exists. |
| 165 static void print_webp_error(const SkBitmap& bm, const char msg[]) { |
| 166 SkDEBUGF(("libwebp error %s [%d %d]", msg, bm.width(), bm.height())); |
| 167 } |
| 168 |
165 static bool return_false(const SkBitmap& bm, const char msg[]) { | 169 static bool return_false(const SkBitmap& bm, const char msg[]) { |
166 SkDEBUGF(("libwebp error %s [%d %d]", msg, bm.width(), bm.height())); | 170 print_webp_error(bm, msg); |
167 return false; // must always return false | 171 return false; // must always return false |
168 } | 172 } |
169 | 173 |
| 174 static SkImageDecoder::Result return_failure(const SkBitmap& bm, const char msg[
]) { |
| 175 print_webp_error(bm, msg); |
| 176 return SkImageDecoder::kFailure; // must always return kFailure |
| 177 } |
| 178 |
| 179 /////////////////////////////////////////////////////////////////////////////// |
| 180 |
170 static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, bool premul
tiply) { | 181 static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, bool premul
tiply) { |
171 WEBP_CSP_MODE mode = MODE_LAST; | 182 WEBP_CSP_MODE mode = MODE_LAST; |
172 const SkColorType ct = decodedBitmap->colorType(); | 183 const SkColorType ct = decodedBitmap->colorType(); |
173 | 184 |
174 if (ct == kN32_SkColorType) { | 185 if (ct == kN32_SkColorType) { |
175 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) | 186 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) |
176 mode = premultiply ? MODE_bgrA : MODE_BGRA; | 187 mode = premultiply ? MODE_bgrA : MODE_BGRA; |
177 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) | 188 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) |
178 mode = premultiply ? MODE_rgbA : MODE_RGBA; | 189 mode = premultiply ? MODE_rgbA : MODE_RGBA; |
179 #else | 190 #else |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 return false; | 413 return false; |
403 } | 414 } |
404 | 415 |
405 if (!directDecode) { | 416 if (!directDecode) { |
406 cropBitmap(decodedBitmap, bitmap, sampleSize, region.x(), region.y(), | 417 cropBitmap(decodedBitmap, bitmap, sampleSize, region.x(), region.y(), |
407 region.width(), region.height(), rect.x(), rect.y()); | 418 region.width(), region.height(), rect.x(), rect.y()); |
408 } | 419 } |
409 return true; | 420 return true; |
410 } | 421 } |
411 | 422 |
412 bool SkWEBPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap, | 423 SkImageDecoder::Result SkWEBPImageDecoder::onDecode(SkStream* stream, SkBitmap*
decodedBitmap, |
413 Mode mode) { | 424 Mode mode) { |
414 #ifdef TIME_DECODE | 425 #ifdef TIME_DECODE |
415 AutoTimeMillis atm("WEBP Decode"); | 426 AutoTimeMillis atm("WEBP Decode"); |
416 #endif | 427 #endif |
417 | 428 |
418 int origWidth, origHeight, hasAlpha; | 429 int origWidth, origHeight, hasAlpha; |
419 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) { | 430 if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) { |
420 return false; | 431 return kFailure; |
421 } | 432 } |
422 this->fHasAlpha = hasAlpha; | 433 this->fHasAlpha = hasAlpha; |
423 | 434 |
424 const int sampleSize = this->getSampleSize(); | 435 const int sampleSize = this->getSampleSize(); |
425 SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize); | 436 SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize); |
426 if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(), | 437 if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(), |
427 sampler.scaledHeight())) { | 438 sampler.scaledHeight())) { |
428 return false; | 439 return kFailure; |
429 } | 440 } |
430 | 441 |
431 // If only bounds are requested, done | 442 // If only bounds are requested, done |
432 if (SkImageDecoder::kDecodeBounds_Mode == mode) { | 443 if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
433 return true; | 444 return kSuccess; |
434 } | 445 } |
435 | 446 |
436 if (!this->allocPixelRef(decodedBitmap, NULL)) { | 447 if (!this->allocPixelRef(decodedBitmap, NULL)) { |
437 return return_false(*decodedBitmap, "allocPixelRef"); | 448 return return_failure(*decodedBitmap, "allocPixelRef"); |
438 } | 449 } |
439 | 450 |
440 SkAutoLockPixels alp(*decodedBitmap); | 451 SkAutoLockPixels alp(*decodedBitmap); |
441 | 452 |
442 WebPDecoderConfig config; | 453 WebPDecoderConfig config; |
443 if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight, | 454 if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight, |
444 this->shouldPremultiply())) { | 455 this->shouldPremultiply())) { |
445 return false; | 456 return kFailure; |
446 } | 457 } |
447 | 458 |
448 // Decode the WebP image data stream using WebP incremental decoding. | 459 // Decode the WebP image data stream using WebP incremental decoding. |
449 return webp_idecode(stream, &config); | 460 return webp_idecode(stream, &config) ? kSuccess : kFailure; |
450 } | 461 } |
451 | 462 |
452 /////////////////////////////////////////////////////////////////////////////// | 463 /////////////////////////////////////////////////////////////////////////////// |
453 | 464 |
454 #include "SkUnPreMultiply.h" | 465 #include "SkUnPreMultiply.h" |
455 | 466 |
456 typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width, | 467 typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width, |
457 const SkPMColor* SK_RESTRICT ctable); | 468 const SkPMColor* SK_RESTRICT ctable); |
458 | 469 |
459 static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width, | 470 static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width, |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 return SkImageDecoder::kUnknown_Format; | 688 return SkImageDecoder::kUnknown_Format; |
678 } | 689 } |
679 | 690 |
680 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { | 691 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { |
681 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NUL
L; | 692 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NUL
L; |
682 } | 693 } |
683 | 694 |
684 static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory); | 695 static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory); |
685 static SkImageDecoder_FormatReg gFormatReg(get_format_webp); | 696 static SkImageDecoder_FormatReg gFormatReg(get_format_webp); |
686 static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory); | 697 static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory); |
OLD | NEW |