Index: src/images/SkImageDecoder_ktx.cpp |
diff --git a/src/images/SkImageDecoder_ktx.cpp b/src/images/SkImageDecoder_ktx.cpp |
index 0dd987cef19e2e4986829afffbeba72453a5703d..fd1db5e9d2e0283df9c9f8818a56cdf16428c5f7 100644 |
--- a/src/images/SkImageDecoder_ktx.cpp |
+++ b/src/images/SkImageDecoder_ktx.cpp |
@@ -67,12 +67,30 @@ bool SkKTXImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { |
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 @@ bool SkKTXImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { |
} else if (ktxFile.isRGBA8()) { |
+ // Uncompressed RGBA data |
+ |
// 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; |
- } |
+ // we need to turn off the premultiplier |
+ SkScaledBitmapSampler::Options opts (*this); |
+ if (bSrcIsPremul) { |
+ SkASSERT(bm->alphaType() == kPremul_SkAlphaType); |
+ SkASSERT(!this->getRequireUnpremultipliedColors()); |
- // Uncompressed RGBA data |
- if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, *this)) { |
+ opts.fPremultiplyAlpha = false; |
+ } |
+ |
+ if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, opts)) { |
return false; |
} |
@@ -158,11 +178,6 @@ bool SkKTXImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { |
srcRow += sampler.srcDY() * srcRowBytes; |
} |
- // Reset this in case the decoder needs to be used again. |
- if (setRequireUnpremul) { |
- this->setRequireUnpremultipliedColors(false); |
- } |
- |
return true; |
} |