Do a better job of enforcing the semantics of the setRequireUnpremultipliedColors flag
R=scroggo@google.com, halcanary@google.com, robertphillips@google.com
Author: krajcevski@google.com
Review URL: https://codereview.chromium.org/322813005
diff --git a/src/images/SkImageDecoder_ktx.cpp b/src/images/SkImageDecoder_ktx.cpp
index 0dd987c..fd1db5e 100644
--- a/src/images/SkImageDecoder_ktx.cpp
+++ b/src/images/SkImageDecoder_ktx.cpp
@@ -67,12 +67,30 @@
return false;
}
+ // Set a flag if our source is premultiplied alpha
+ const SkString premulKey("KTXPremultipliedAlpha");
+ const bool bSrcIsPremul = ktxFile.getValueForKey(premulKey) == SkString("True");
+
// Setup the sampler...
SkScaledBitmapSampler sampler(width, height, this->getSampleSize());
+ // Determine the alpha of the bitmap...
+ SkAlphaType alphaType = kOpaque_SkAlphaType;
+ if (ktxFile.isRGBA8()) {
+ if (this->getRequireUnpremultipliedColors()) {
+ alphaType = kUnpremul_SkAlphaType;
+ // If the client wants unpremul colors and we only have
+ // premul, then we cannot honor their wish.
+ if (bSrcIsPremul) {
+ return false;
+ }
+ } else {
+ alphaType = kPremul_SkAlphaType;
+ }
+ }
+
// Set the config...
- bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight(),
- ktxFile.isRGBA8()? kPremul_SkAlphaType : kOpaque_SkAlphaType));
+ bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight(), alphaType));
if (SkImageDecoder::kDecodeBounds_Mode == mode) {
return true;
}
@@ -134,17 +152,19 @@
} else if (ktxFile.isRGBA8()) {
- // If we know that the image contains premultiplied alpha, then
- // don't premultiply it upon decoding.
- bool setRequireUnpremul = false;
- const SkString premulKey("KTXPremultipliedAlpha");
- if (ktxFile.getValueForKey(premulKey) == SkString("True")) {
- this->setRequireUnpremultipliedColors(true);
- setRequireUnpremul = true;
- }
-
// Uncompressed RGBA data
- if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, *this)) {
+
+ // If we know that the image contains premultiplied alpha, then
+ // we need to turn off the premultiplier
+ SkScaledBitmapSampler::Options opts (*this);
+ if (bSrcIsPremul) {
+ SkASSERT(bm->alphaType() == kPremul_SkAlphaType);
+ SkASSERT(!this->getRequireUnpremultipliedColors());
+
+ opts.fPremultiplyAlpha = false;
+ }
+
+ if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, opts)) {
return false;
}
@@ -158,11 +178,6 @@
srcRow += sampler.srcDY() * srcRowBytes;
}
- // Reset this in case the decoder needs to be used again.
- if (setRequireUnpremul) {
- this->setRequireUnpremultipliedColors(false);
- }
-
return true;
}