Add an option to create unpremultiplied bitmaps.
Currently they cannot be used directly by Skia, but
the pixels can be used elsewhere.
SkImageDecoder:
Add functions to require unpremultiplied output
and query the presence of the requirement
SkImageDecoder_libpng:
SkImageDecoder_libwebp:
SkImageDecoder_WIC:
Respect the requirement for unpremultiplied output.
TODO: Fix SkImageDecoder_CG.
SkScaledBitmapSampler:
Add procs to skip premultiplication and a boolean
parameter to use those procs.
ImageDecodingTest:
Test unpremultiplied bitmap decoding.
SampleUnpremul:
Add a sample which allows visually comparing between the
unpremultiplied version (copied into a premultiplied bitmap,
since drawing unpremultiplied is not currently supported)
and a premultiplied version of image files.
gm.h:
Add a getter for the resource path, so Samples can use it.
As of patch set 13, https://codereview.chromium.org/16816016/
and https://codereview.chromium.org/16983004/, which were
approved separately.
R=reed@google.com
Review URL: https://codereview.chromium.org/16410009
git-svn-id: http://skia.googlecode.com/svn/trunk@9612 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/images/SkImageDecoder_libwebp.cpp b/src/images/SkImageDecoder_libwebp.cpp
index 95b9a97..9cf8449 100644
--- a/src/images/SkImageDecoder_libwebp.cpp
+++ b/src/images/SkImageDecoder_libwebp.cpp
@@ -114,7 +114,17 @@
virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
private:
+ /**
+ * Called when determining the output config to request to webp.
+ * If the image does not have alpha, there is no need to premultiply.
+ * If the caller wants unpremultiplied colors, that is respected.
+ */
+ bool shouldPremultiply() const {
+ return SkToBool(fHasAlpha) && !this->getRequireUnpremultipliedColors();
+ }
+
bool setDecodeConfig(SkBitmap* decodedBitmap, int width, int height);
+
SkStream* fInputStream;
int fOrigWidth;
int fOrigHeight;
@@ -157,18 +167,16 @@
return false; // must always return false
}
-static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, int hasAlpha) {
+static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, bool premultiply) {
WEBP_CSP_MODE mode = MODE_LAST;
SkBitmap::Config config = decodedBitmap->config();
- // For images that have alpha, choose appropriate color mode (MODE_rgbA,
- // MODE_rgbA_4444) that pre-multiplies RGB pixel values with transparency
- // factor (alpha).
+
if (config == SkBitmap::kARGB_8888_Config) {
- mode = hasAlpha ? MODE_rgbA : MODE_RGBA;
+ mode = premultiply ? MODE_rgbA : MODE_RGBA;
} else if (config == SkBitmap::kARGB_4444_Config) {
- mode = hasAlpha ? MODE_rgbA_4444 : MODE_RGBA_4444;
+ mode = premultiply ? MODE_rgbA_4444 : MODE_RGBA_4444;
} else if (config == SkBitmap::kRGB_565_Config) {
- mode = MODE_RGB_565;
+ mode = MODE_RGB_565;
}
SkASSERT(MODE_LAST != mode);
return mode;
@@ -224,8 +232,8 @@
static bool webp_get_config_resize(WebPDecoderConfig* config,
SkBitmap* decodedBitmap,
- int width, int height, int hasAlpha) {
- WEBP_CSP_MODE mode = webp_decode_mode(decodedBitmap, hasAlpha);
+ int width, int height, bool premultiply) {
+ WEBP_CSP_MODE mode = webp_decode_mode(decodedBitmap, premultiply);
if (MODE_LAST == mode) {
return false;
}
@@ -251,10 +259,10 @@
static bool webp_get_config_resize_crop(WebPDecoderConfig* config,
SkBitmap* decodedBitmap,
- const SkIRect& region, int hasAlpha) {
+ const SkIRect& region, bool premultiply) {
if (!webp_get_config_resize(config, decodedBitmap, region.width(),
- region.height(), hasAlpha)) {
+ region.height(), premultiply)) {
return false;
}
@@ -372,7 +380,8 @@
SkAutoLockPixels alp(*bitmap);
WebPDecoderConfig config;
- if (!webp_get_config_resize_crop(&config, bitmap, rect, fHasAlpha)) {
+ if (!webp_get_config_resize_crop(&config, bitmap, rect,
+ this->shouldPremultiply())) {
return false;
}
@@ -430,7 +439,7 @@
WebPDecoderConfig config;
if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight,
- hasAlpha)) {
+ this->shouldPremultiply())) {
return false;
}