• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkColorPriv.h"
9 #include "include/private/base/SkMath.h"
10 #include "tests/Test.h"
11 
12 #include <algorithm>
13 
14 // our std SkAlpha255To256
test_srcover0(unsigned dst,unsigned alpha)15 static int test_srcover0(unsigned dst, unsigned alpha) {
16     return alpha + SkAlphaMul(dst, SkAlpha255To256(255 - alpha));
17 }
18 
19 // faster hack +1
test_srcover1(unsigned dst,unsigned alpha)20 static int test_srcover1(unsigned dst, unsigned alpha) {
21     return alpha + SkAlphaMul(dst, 256 - alpha);
22 }
23 
24 // slower "correct"
test_srcover2(unsigned dst,unsigned alpha)25 static int test_srcover2(unsigned dst, unsigned alpha) {
26     return alpha + SkMulDiv255Round(dst, 255 - alpha);
27 }
28 
DEF_TEST(SrcOver,reporter)29 DEF_TEST(SrcOver, reporter) {
30     /*  Here's the idea. Can we ensure that when we blend on top of an opaque
31         dst, that the result always stay's opaque (i.e. exactly 255)?
32      */
33 
34     unsigned i;
35     int opaqueCounter0 = 0;
36     int opaqueCounter1 = 0;
37     int opaqueCounter2 = 0;
38     for (i = 0; i <= 255; i++) {
39         unsigned result0 = test_srcover0(0xFF, i);
40         unsigned result1 = test_srcover1(0xFF, i);
41         unsigned result2 = test_srcover2(0xFF, i);
42         opaqueCounter0 += (result0 == 0xFF);
43         opaqueCounter1 += (result1 == 0xFF);
44         opaqueCounter2 += (result2 == 0xFF);
45     }
46 #if 0
47     INFOF(reporter, "---- opaque test: [%d %d %d]\n",
48           opaqueCounter0, opaqueCounter1, opaqueCounter2);
49 #endif
50     // we acknowledge that technique0 does not always return opaque
51     REPORTER_ASSERT(reporter, opaqueCounter0 == 256);
52     REPORTER_ASSERT(reporter, opaqueCounter1 == 256);
53     REPORTER_ASSERT(reporter, opaqueCounter2 == 256);
54 
55     // Now ensure that we never over/underflow a byte
56     for (i = 0; i <= 255; i++) {
57         for (unsigned dst = 0; dst <= 255; dst++) {
58             unsigned r0 = test_srcover0(dst, i);
59             unsigned r1 = test_srcover1(dst, i);
60             unsigned r2 = test_srcover2(dst, i);
61             unsigned max = std::max(dst, i);
62             // ignore the known failure
63             if (dst != 255) {
64                 REPORTER_ASSERT(reporter, r0 <= 255 && r0 >= max);
65             }
66             REPORTER_ASSERT(reporter, r1 <= 255 && r1 >= max);
67             REPORTER_ASSERT(reporter, r2 <= 255 && r2 >= max);
68 
69 #if 0
70             // this shows where r1 (faster) differs from r2 (more exact)
71             if (r1 != r2) {
72                 INFOF(reporter, "--- dst=%d i=%d r1=%d r2=%d exact=%g\n",
73                       dst, i, r1, r2, i + dst - dst*i/255.0f);
74             }
75 #endif
76         }
77     }
78 }
79