• 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 "SkColor.h"
9 #include "SkColorData.h"
10 #include "SkColorPriv.h"
11 #include "SkMathPriv.h"
12 #include "SkRandom.h"
13 #include "SkTypes.h"
14 #include "SkUnPreMultiply.h"
15 #include "Test.h"
16 
17 #define GetPackedR16As32(packed)    (SkGetPackedR16(dc) << (8 - SK_R16_BITS))
18 #define GetPackedG16As32(packed)    (SkGetPackedG16(dc) << (8 - SK_G16_BITS))
19 #define GetPackedB16As32(packed)    (SkGetPackedB16(dc) << (8 - SK_B16_BITS))
20 
test_premul(skiatest::Reporter * reporter)21 static inline void test_premul(skiatest::Reporter* reporter) {
22     for (int a = 0; a <= 255; a++) {
23         for (int x = 0; x <= 255; x++) {
24             SkColor c0 = SkColorSetARGB(a, x, x, x);
25             SkPMColor p0 = SkPreMultiplyColor(c0);
26 
27             SkColor c1 = SkUnPreMultiply::PMColorToColor(p0);
28             SkPMColor p1 = SkPreMultiplyColor(c1);
29 
30             // we can't promise that c0 == c1, since c0 -> p0 is a many to one
31             // function, however, we can promise that p0 -> c1 -> p1 : p0 == p1
32             REPORTER_ASSERT(reporter, p0 == p1);
33 
34             {
35                 int ax = SkMulDiv255Ceiling(x, a);
36                 REPORTER_ASSERT(reporter, ax <= a);
37             }
38         }
39     }
40 }
41 
42 /**
43   This test fails: SkFourByteInterp does *not* preserve opaque destinations.
44   SkAlpha255To256 implemented as (alpha + 1) is faster than
45   (alpha + (alpha >> 7)), but inaccurate, and Skia intends to phase it out.
46 */
47 /*
48 static void test_interp(skiatest::Reporter* reporter) {
49     SkRandom r;
50 
51     U8CPU a0 = 0;
52     U8CPU a255 = 255;
53     for (int i = 0; i < 200; i++) {
54         SkColor colorSrc = r.nextU();
55         SkColor colorDst = r.nextU();
56         SkPMColor src = SkPreMultiplyColor(colorSrc);
57         SkPMColor dst = SkPreMultiplyColor(colorDst);
58 
59         REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a0) == dst);
60         REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a255) == src);
61     }
62 }
63 */
64 
test_fast_interp(skiatest::Reporter * reporter)65 static inline void test_fast_interp(skiatest::Reporter* reporter) {
66     SkRandom r;
67 
68     U8CPU a0 = 0;
69     U8CPU a255 = 255;
70     for (int i = 0; i < 200; i++) {
71         SkColor colorSrc = r.nextU();
72         SkColor colorDst = r.nextU();
73         SkPMColor src = SkPreMultiplyColor(colorSrc);
74         SkPMColor dst = SkPreMultiplyColor(colorDst);
75 
76         REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a0) == dst);
77         REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a255) == src);
78     }
79 }
80 
DEF_TEST(Color,reporter)81 DEF_TEST(Color, reporter) {
82     test_premul(reporter);
83     //test_interp(reporter);
84     test_fast_interp(reporter);
85     //test_565blend();
86 }
87