aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/images
diff options
context:
space:
mode:
authorGravatar reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2009-07-29 20:56:52 +0000
committerGravatar reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2009-07-29 20:56:52 +0000
commitb6137c3139b1b1da99ad9f6c28ac0d9e8f910ff6 (patch)
tree55490b8ca035fb54db68b3aad3bb6ed2f0287c7c /src/images
parentf18d8760019ae999c63d1f0b0e6e52815202b23b (diff)
remove auto-upscaling from base class (breaks model for allocators) but improve
the upscaling inside the png codec. git-svn-id: http://skia.googlecode.com/svn/trunk@294 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/images')
-rw-r--r--src/images/SkImageDecoder.cpp12
-rw-r--r--src/images/SkImageDecoder_libpng.cpp53
2 files changed, 36 insertions, 29 deletions
diff --git a/src/images/SkImageDecoder.cpp b/src/images/SkImageDecoder.cpp
index 4f9fa9bb1b..4711f89d7f 100644
--- a/src/images/SkImageDecoder.cpp
+++ b/src/images/SkImageDecoder.cpp
@@ -90,6 +90,7 @@ bool SkImageDecoder::allocPixelRef(SkBitmap* bitmap,
}
///////////////////////////////////////////////////////////////////////////////
+
bool SkImageDecoder::decode(SkStream* stream, SkBitmap* bm,
SkBitmap::Config pref, Mode mode) {
// pass a temporary bitmap, so that if we return false, we are assured of
@@ -102,17 +103,6 @@ bool SkImageDecoder::decode(SkStream* stream, SkBitmap* bm,
if (!this->onDecode(stream, &tmp, pref, mode)) {
return false;
}
-
- if (tmp.config() != pref && tmp.canCopyTo(pref)) {
- if (mode == kDecodeBounds_Mode) {
- tmp.setConfig(pref, tmp.width(), tmp.height());
- } else if (mode == kDecodePixels_Mode) {
- SkBitmap tmp2;
- if (tmp.copyTo(&tmp2, pref, this->getAllocator())) {
- tmp.swap(tmp2);
- }
- }
- }
bm->swap(tmp);
return true;
}
diff --git a/src/images/SkImageDecoder_libpng.cpp b/src/images/SkImageDecoder_libpng.cpp
index 8f2652a4eb..6df56b4b8f 100644
--- a/src/images/SkImageDecoder_libpng.cpp
+++ b/src/images/SkImageDecoder_libpng.cpp
@@ -110,10 +110,30 @@ static bool substituteTranspColor(SkBitmap* bm, SkPMColor match) {
return reallyHasAlpha;
}
-static inline bool isDirectModel(SkBitmap::Config config) {
- return config == SkBitmap::kARGB_8888_Config ||
- config == SkBitmap::kARGB_4444_Config ||
- config == SkBitmap::kRGB_565_Config;
+static bool canUpscalePaletteToConfig(SkBitmap::Config prefConfig,
+ bool srcHasAlpha) {
+ switch (prefConfig) {
+ case SkBitmap::kARGB_8888_Config:
+ case SkBitmap::kARGB_4444_Config:
+ return true;
+ case SkBitmap::kRGB_565_Config:
+ // only return true if the src is opaque (since 565 is opaque)
+ return !srcHasAlpha;
+ default:
+ return false;
+ }
+}
+
+// call only if color_type is PALETTE. Returns true if the ctable has alpha
+static bool hasTransparencyInPalette(png_structp png_ptr, png_infop info_ptr) {
+ png_bytep trans;
+ int num_trans;
+
+ if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
+ png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, NULL);
+ return num_trans > 0;
+ }
+ return false;
}
bool SkPNGImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* decodedBitmap,
@@ -213,7 +233,12 @@ bool SkPNGImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* decodedBitmap,
}
if (color_type == PNG_COLOR_TYPE_PALETTE) {
- config = SkBitmap::kIndex8_Config; // defer sniffing for hasAlpha
+ config = SkBitmap::kIndex8_Config;
+ // now see if we can upscale to their requested config
+ bool paletteHasAlpha = hasTransparencyInPalette(png_ptr, info_ptr);
+ if (canUpscalePaletteToConfig(prefConfig, paletteHasAlpha)) {
+ config = prefConfig;
+ }
} else {
png_color_16p transpColor = NULL;
int numTransp = 0;
@@ -286,6 +311,9 @@ bool SkPNGImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* decodedBitmap,
const int sampleSize = this->getSampleSize();
SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize);
+ // we must always return the same config, independent of mode, so if we were
+ // going to respect prefConfig, it must have happened by now
+
decodedBitmap->setConfig(config, sampler.scaledWidth(),
sampler.scaledHeight(), 0);
if (SkImageDecoder::kDecodeBounds_Mode == mode) {
@@ -298,7 +326,6 @@ bool SkPNGImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* decodedBitmap,
// to |= PNG_COLOR_MASK_ALPHA, but all of its pixels are in fact opaque. We care, since we
// draw lots faster if we can flag the bitmap has being opaque
bool reallyHasAlpha = false;
- bool upscaleFromPalette = false;
SkColorTable* colorTable = NULL;
if (color_type == PNG_COLOR_TYPE_PALETTE) {
@@ -352,23 +379,13 @@ bool SkPNGImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* decodedBitmap,
*colorPtr = colorPtr[-1];
}
colorTable->unlockColors(true);
-
- // see if we need to upscale to a direct-model
- if (isDirectModel(prefConfig)) {
- if (!reallyHasAlpha || SkBitmap::kRGB_565_Config != prefConfig) {
- upscaleFromPalette = true;
- config = prefConfig;
- // need to re-call setConfig
- decodedBitmap->setConfig(config, sampler.scaledWidth(),
- sampler.scaledHeight(), 0);
- }
- }
}
SkAutoUnref aur(colorTable);
if (!this->allocPixelRef(decodedBitmap,
- upscaleFromPalette ? NULL : colorTable)) {
+ SkBitmap::kIndex8_Config == config ?
+ colorTable : NULL)) {
return false;
}