In SkPixmap.cpp, change SkAlphaMul to SkMulDiv255.
Add a test that we get the same color back after calling SkBitmap::eraseColor, modulo rounding.
Also update some incorrect docs.
BUG=skia:4297
Review URL: https://codereview.chromium.org/1521673002
diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h
index 33d565c..14f0aa5 100644
--- a/include/core/SkCanvas.h
+++ b/include/core/SkCanvas.h
@@ -613,8 +613,8 @@
* This makes the contents of the canvas undefined. Subsequent calls that
* require reading the canvas contents will produce undefined results. Examples
* include blending and readPixels. The actual implementation is backend-
- * dependent and one legal implementation is to do nothing. Like clear(), this
- * ignores the clip.
+ * dependent and one legal implementation is to do nothing. This method
+ * ignores the current clip.
*
* This function should only be called if the caller intends to subsequently
* draw to the canvas. The canvas may do real work at discard() time in order
@@ -624,7 +624,7 @@
void discard() { this->onDiscard(); }
/**
- * Fill the entire canvas' bitmap (restricted to the current clip) with the
+ * Fill the entire canvas (restricted to the current clip) with the
* specified paint.
* @param paint The paint used to fill the canvas
*/
diff --git a/src/core/SkPixmap.cpp b/src/core/SkPixmap.cpp
index 0098b1b..943287b 100644
--- a/src/core/SkPixmap.cpp
+++ b/src/core/SkPixmap.cpp
@@ -163,9 +163,9 @@
// make rgb premultiplied
if (255 != a) {
- r = SkAlphaMul(r, a);
- g = SkAlphaMul(g, a);
- b = SkAlphaMul(b, a);
+ r = SkMulDiv255Round(r, a);
+ g = SkMulDiv255Round(g, a);
+ b = SkMulDiv255Round(b, a);
}
if (kARGB_4444_SkColorType == this->colorType()) {
@@ -186,13 +186,14 @@
uint32_t* p = this->writable_addr32(area.fLeft, area.fTop);
if (255 != a && kPremul_SkAlphaType == this->alphaType()) {
- r = SkAlphaMul(r, a);
- g = SkAlphaMul(g, a);
- b = SkAlphaMul(b, a);
+ r = SkMulDiv255Round(r, a);
+ g = SkMulDiv255Round(g, a);
+ b = SkMulDiv255Round(b, a);
}
- uint32_t v = kRGBA_8888_SkColorType == this->colorType() ?
- SkPackARGB_as_RGBA(a, r, g, b) : SkPackARGB_as_BGRA(a, r, g, b);
-
+ uint32_t v = kRGBA_8888_SkColorType == this->colorType()
+ ? SkPackARGB_as_RGBA(a, r, g, b)
+ : SkPackARGB_as_BGRA(a, r, g, b);
+
while (--height >= 0) {
sk_memset32(p, v, width);
p = (uint32_t*)((char*)p + rowBytes);
diff --git a/tests/BitmapTest.cpp b/tests/BitmapTest.cpp
index 2bd8490..58e3c8b 100644
--- a/tests/BitmapTest.cpp
+++ b/tests/BitmapTest.cpp
@@ -145,3 +145,24 @@
REPORTER_ASSERT(r, source.getColor(0, 0) == copy.getColor(0, 0));
}
}
+
+static void test_erasecolor_premul(skiatest::Reporter* reporter, SkColorType ct, SkColor input,
+ SkColor expected) {
+ SkBitmap bm;
+ bm.allocPixels(SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType));
+ bm.eraseColor(input);
+ SkDebugf("expected: %x actual: %x\n", expected, bm.getColor(0, 0));
+ REPORTER_ASSERT(reporter, bm.getColor(0, 0) == expected);
+}
+
+/**
+ * This test checks that eraseColor premultiplies the color correctly.
+ */
+DEF_TEST(Bitmap_eraseColor_Premul, r) {
+ SkColor color = 0x80FF0080;
+ test_erasecolor_premul(r, kAlpha_8_SkColorType, color, 0x80000000);
+ test_erasecolor_premul(r, kRGB_565_SkColorType, color, 0xFF840042);
+ test_erasecolor_premul(r, kARGB_4444_SkColorType, color, 0x88FF0080);
+ test_erasecolor_premul(r, kRGBA_8888_SkColorType, color, color);
+ test_erasecolor_premul(r, kBGRA_8888_SkColorType, color, color);
+}