add SkAlphaBlend255

git-svn-id: http://skia.googlecode.com/svn/trunk@2426 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2011-10-06 20:04:36 +00:00
parent 01744a46e8
commit 0abb499daf
2 changed files with 39 additions and 27 deletions

View File

@ -44,6 +44,21 @@ static inline int SkAlphaBlend(int src, int dst, int scale256) {
return dst + SkAlphaMul(src - dst, scale256); return dst + SkAlphaMul(src - dst, scale256);
} }
/**
* Returns (src * alpha + dst * (255 - alpha)) / 255
*
* This is more accurate than SkAlphaBlend, but slightly slower
*/
static inline int SkAlphaBlend255(S16CPU src, S16CPU dst, U8CPU alpha) {
SkASSERT((int16_t)src == src);
SkASSERT((int16_t)dst == dst);
SkASSERT((uint8_t)alpha == alpha);
int prod = SkMulS16(src - dst, alpha) + 128;
prod = (prod + (prod >> 8)) >> 8;
return dst + prod;
}
#define SK_R16_BITS 5 #define SK_R16_BITS 5
#define SK_G16_BITS 6 #define SK_G16_BITS 6
#define SK_B16_BITS 5 #define SK_B16_BITS 5

View File

@ -10,40 +10,35 @@
#include "SkMath.h" #include "SkMath.h"
#include "SkPoint.h" #include "SkPoint.h"
#include "SkRandom.h" #include "SkRandom.h"
#include "SkColorPriv.h"
#if 0 static float float_blend(int src, int dst, float unit) {
static U8CPU premul_fast(U8CPU a, U8CPU x) { return dst + (src - dst) * unit;
return a * x * 32897 >> 23;
} }
static U8CPU premul_trunc(U8CPU a, U8CPU x) { static void test_blend(skiatest::Reporter* reporter) {
double result = a * x; for (int src = 0; src <= 255; src++) {
result /= 255.0; for (int dst = 0; dst <= 255; dst++) {
return (unsigned)floor(result + 0.0); for (int a = 0; a <= 255; a++) {
} int r0 = SkAlphaBlend255(src, dst, a);
float f1 = float_blend(src, dst, a / 255.f);
int r1 = SkScalarRoundToInt(f1);
static U8CPU premul_round(U8CPU a, U8CPU x) { if (r0 != r1) {
double result = a * x; float diff = sk_float_abs(f1 - r1);
result /= 255.0; diff = sk_float_abs(diff - 0.5f);
return (unsigned)floor(result + 0.5); if (diff > (1 / 255.f)) {
} #ifdef SK_DEBUG
SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
static void test_premul(skiatest::Reporter* reporter) { src, dst, a, r0, f1);
for (int a = 0; a <= 255; a++) { #endif
for (int x = 0; x <= 255; x++) { REPORTER_ASSERT(reporter, false);
unsigned curr_trunc = SkMulDiv255Trunc(a, x); }
unsigned curr_round = SkMulDiv255Round(a, x); }
unsigned fast = premul_fast(a, x);
unsigned slow_round = premul_round(a, x);
unsigned slow_trunc = premul_trunc(a, x);
if (fast != slow || curr != fast) {
SkDebugf("---- premul(%d %d) curr=%d fast=%d slow=%d\n", a, x,
curr, fast, slow);
} }
} }
} }
} }
#endif
#if defined(SkLONGLONG) #if defined(SkLONGLONG)
static int symmetric_fixmul(int a, int b) { static int symmetric_fixmul(int a, int b) {
@ -501,7 +496,9 @@ static void TestMath(skiatest::Reporter* reporter) {
SkDebugf("SinCos: maximum error = %d\n", maxDiff); SkDebugf("SinCos: maximum error = %d\n", maxDiff);
#endif #endif
// test_premul(reporter); #ifdef SK_SCALAR_IS_FLOAT
test_blend(reporter);
#endif
} }
#include "TestClassDef.h" #include "TestClassDef.h"