Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Unified Diff: src/core/SkBitmap.cpp

Issue 16410009: Add an option to create unpremultiplied bitmaps. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/core/SkBitmap.cpp
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp
index 122996a31229cdb28d9120903a4b080a0684f0e0..5f8d04e09e56252184b992a23899e7d2a07c9c77 100644
--- a/src/core/SkBitmap.cpp
+++ b/src/core/SkBitmap.cpp
@@ -268,7 +268,8 @@ void SkBitmap::getBounds(SkIRect* bounds) const {
///////////////////////////////////////////////////////////////////////////////
-void SkBitmap::setConfig(Config c, int width, int height, size_t rowBytes) {
+void SkBitmap::setConfig(Config c, int width, int height, size_t rowBytes,
+ bool premul) {
this->freePixels();
if ((width | height) < 0) {
@@ -286,6 +287,14 @@ void SkBitmap::setConfig(Config c, int width, int height, size_t rowBytes) {
fWidth = width;
fHeight = height;
fRowBytes = SkToU32(rowBytes);
+ if (premul) {
+ fFlags &= ~kColorsAreNotPremultiplied_Flag;
+ } else {
+ if (c != kARGB_8888_Config) {
+ goto err;
+ }
+ fFlags |= kColorsAreNotPremultiplied_Flag;
+ }
fBytesPerPixel = (uint8_t)ComputeBytesPerPixel(c);
@@ -651,8 +660,18 @@ SkColor SkBitmap::getColor(int x, int y) const {
return SkUnPreMultiply::PMColorToColor(c);
}
case SkBitmap::kARGB_8888_Config: {
- uint32_t* addr = this->getAddr32(x, y);
- return SkUnPreMultiply::PMColorToColor(addr[0]);
+ if (this->premultiplied()) {
+ uint32_t* addr = this->getAddr32(x, y);
+ return SkUnPreMultiply::PMColorToColor(addr[0]);
+ } else {
+ SkUnPMColor* addr = this->getAddr32(x, y);
+ SkUnPMColor c = *addr;
+ U8CPU a = SkGetPackedA32(c);
+ U8CPU r = SkGetPackedR32(c);
+ U8CPU g = SkGetPackedG32(c);
+ U8CPU b = SkGetPackedB32(c);
+ return SkColorSetARGB(a, r, g, b);
+ }
}
case kRLE_Index8_Config: {
uint8_t dst;
@@ -768,6 +787,8 @@ void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
// make rgb premultiplied
if (255 != a) {
+ // Unpremultiplied exits above at readyToDraw.
+ SkASSERT(this->premultiplied());
r = SkAlphaMul(r, a);
g = SkAlphaMul(g, a);
b = SkAlphaMul(b, a);
@@ -928,7 +949,8 @@ bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
if (pixelRef != NULL) {
SkBitmap dst;
- dst.setConfig(this->config(), subset.width(), subset.height());
+ dst.setConfig(this->config(), subset.width(), subset.height(), 0,
+ this->premultiplied());
dst.setIsVolatile(this->isVolatile());
dst.setIsOpaque(this->isOpaque());
dst.setPixelRef(pixelRef)->unref();
@@ -947,6 +969,8 @@ bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
}
SkBitmap bm;
+ // kRLE_Index8_Config is not compatible with unpremultiplied.
+ SkASSERT(this->premultiplied());
bm.setConfig(kIndex8_Config, r.width(), r.height());
bm.allocPixels(this->getColorTable());
if (NULL == bm.getPixels()) {
@@ -977,7 +1001,7 @@ bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
}
SkBitmap dst;
- dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes());
+ dst.setConfig(this->config(), r.width(), r.height(), this->rowBytes(), this->premultiplied());
dst.setIsVolatile(this->isVolatile());
dst.setIsOpaque(this->isOpaque());
@@ -1024,6 +1048,12 @@ bool SkBitmap::canCopyTo(Config dstConfig) const {
return false;
}
+ if (!this->premultiplied()) {
+ // Cannot copy unpremultiplied, which returns false for readyToDraw.
+ SkASSERT(kARGB_8888_Config == this->config());
+ return false;
+ }
+
return true;
}
@@ -1031,6 +1061,9 @@ bool SkBitmap::copyTo(SkBitmap* dst, Config dstConfig, Allocator* alloc) const {
if (!this->canCopyTo(dstConfig)) {
return false;
}
+ // Cannot copy an unpremultiplied bitmap.
+ // canCopyTo should have returned false.
+ SkASSERT(this->premultiplied());
// if we have a texture, first get those pixels
SkBitmap tmpSrc;
@@ -1564,6 +1597,12 @@ void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const {
buffer.writeInt(fHeight);
buffer.writeInt(fRowBytes);
buffer.writeInt(fConfig);
+#ifdef BUMP_PICTURE_VERSION
+ // FIXME: Storing whether the colors are premultiplied
+ // should be written to the stream, but breaks picture
+ // serialization without changing PICTURE_VERSION
+ buffer.writeBool(this->premultiplied());
+#endif
buffer.writeBool(this->isOpaque());
if (fPixelRef) {
@@ -1587,8 +1626,13 @@ void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) {
int height = buffer.readInt();
int rowBytes = buffer.readInt();
int config = buffer.readInt();
+#ifdef BUMP_PICTURE_VERSION
+ bool premul = buffer.readBool();
+#else
+ bool premul = true;
+#endif
- this->setConfig((Config)config, width, height, rowBytes);
+ this->setConfig((Config)config, width, height, rowBytes, premul);
this->setIsOpaque(buffer.readBool());
int reftype = buffer.readInt();
@@ -1625,7 +1669,12 @@ SkBitmap::RLEPixels::~RLEPixels() {
void SkBitmap::validate() const {
SkASSERT(fConfig < kConfigCount);
SkASSERT(fRowBytes >= (unsigned)ComputeRowBytes((Config)fConfig, fWidth));
- SkASSERT(fFlags <= (kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag));
+ int8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag
+ | kColorsAreNotPremultiplied_Flag;
+#ifdef SK_BUILD_FOR_ANDROID
+ allFlags |= kHasHardwareMipMap_Flag;
+#endif
+ SkASSERT(fFlags <= allFlags);
SkASSERT(fPixelLockCount >= 0);
SkASSERT(NULL == fColorTable || (unsigned)fColorTable->getRefCnt() < 10000);
SkASSERT((uint8_t)ComputeBytesPerPixel((Config)fConfig) == fBytesPerPixel);

Powered by Google App Engine
This is Rietveld 408576698