Factor out UnPreMultiplyPreservingByteOrder

BUG=
R=reed@google.com

Author: mtklein@google.com

Review URL: https://codereview.chromium.org/130303005

git-svn-id: http://skia.googlecode.com/svn/trunk@13103 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
commit-bot@chromium.org 2014-01-15 22:24:58 +00:00
parent 6a18079de8
commit b06faacaf1
4 changed files with 13 additions and 19 deletions

View File

@ -137,17 +137,11 @@ bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const {
unpremul.setConfig(info);
unpremul.allocPixels();
// Unpremultiply without changing native byte order.
SkAutoLockPixels lockSrc(bitmap), lockDst(unpremul);
const SkPMColor* src = (SkPMColor*)bitmap.getPixels();
uint32_t* dst = (uint32_t*)unpremul.getPixels();
for (size_t i = 0; i < bitmap.getSize()/4; i++) {
const U8CPU a = SkGetPackedA32(src[i]);
const SkUnPreMultiply::Scale s = SkUnPreMultiply::GetScale(a);
dst[i] = SkPackARGB32NoCheck(a,
SkUnPreMultiply::ApplyScale(s, SkGetPackedR32(src[i])),
SkUnPreMultiply::ApplyScale(s, SkGetPackedG32(src[i])),
SkUnPreMultiply::ApplyScale(s, SkGetPackedB32(src[i])));
dst[i] = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(src[i]);
}
bitmap.swap(unpremul);
}

View File

@ -49,6 +49,8 @@ public:
static SkColor PMColorToColor(SkPMColor c);
static uint32_t UnPreMultiplyPreservingByteOrder(SkPMColor c);
private:
static const uint32_t gTable[256];
};

View File

@ -17,6 +17,15 @@ SkColor SkUnPreMultiply::PMColorToColor(SkPMColor c) {
ApplyScale(scale, SkGetPackedB32(c)));
}
uint32_t SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(SkPMColor c) {
const U8CPU a = SkGetPackedA32(c);
const Scale scale = GetScale(a);
return SkPackARGB32NoCheck(a,
ApplyScale(scale, SkGetPackedR32(c)),
ApplyScale(scale, SkGetPackedG32(c)),
ApplyScale(scale, SkGetPackedB32(c)));
}
const uint32_t SkUnPreMultiply::gTable[] = {
0x00000000, 0xFF000000, 0x7F800000, 0x55000000, 0x3FC00000, 0x33000000, 0x2A800000, 0x246DB6DB,
0x1FE00000, 0x1C555555, 0x19800000, 0x172E8BA3, 0x15400000, 0x139D89D9, 0x1236DB6E, 0x11000000,

View File

@ -50,17 +50,6 @@ protected:
virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode);
};
// Returns an unpremultiplied version of color. It will have the same ordering and size as an
// SkPMColor, but the alpha will not be premultiplied.
static SkPMColor unpremultiply_pmcolor(SkPMColor color) {
U8CPU a = SkGetPackedA32(color);
const SkUnPreMultiply::Scale scale = SkUnPreMultiply::GetScale(a);
return SkPackARGB32NoCheck(a,
SkUnPreMultiply::ApplyScale(scale, SkGetPackedR32(color)),
SkUnPreMultiply::ApplyScale(scale, SkGetPackedG32(color)),
SkUnPreMultiply::ApplyScale(scale, SkGetPackedB32(color)));
}
#define BITMAP_INFO (kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast)
bool SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
@ -118,7 +107,7 @@ bool SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; ++j) {
uint32_t* addr = bm->getAddr32(i, j);
*addr = unpremultiply_pmcolor(*addr);
*addr = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(*addr);
}
}
bm->setAlphaType(kUnpremul_SkAlphaType);