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/SkColor.h"
9 #include "include/core/SkTypes.h"
10 #include "include/core/SkUnPreMultiply.h"
11 #include "include/private/SkColorData.h"
12 #include "include/private/base/SkCPUTypes.h"
13 #include "src/base/SkMathPriv.h"
14 #include "src/base/SkRandom.h"
15 #include "tests/Test.h"
16
DEF_TEST(ColorPremul,reporter)17 DEF_TEST(ColorPremul, reporter) {
18 for (int a = 0; a <= 255; a++) {
19 for (int x = 0; x <= 255; x++) {
20 SkColor c0 = SkColorSetARGB(a, x, x, x);
21 SkPMColor p0 = SkPreMultiplyColor(c0);
22
23 SkColor c1 = SkUnPreMultiply::PMColorToColor(p0);
24 SkPMColor p1 = SkPreMultiplyColor(c1);
25
26 // we can't promise that c0 == c1, since c0 -> p0 is a many to one
27 // function, however, we can promise that p0 -> c1 -> p1 : p0 == p1
28 REPORTER_ASSERT(reporter, p0 == p1);
29
30 {
31 int ax = SkMulDiv255Ceiling(x, a);
32 REPORTER_ASSERT(reporter, ax <= a);
33 }
34 }
35 }
36 }
37
38 /**
39 This test fails: SkFourByteInterp does *not* preserve opaque destinations.
40 SkAlpha255To256 implemented as (alpha + 1) is faster than
41 (alpha + (alpha >> 7)), but inaccurate, and Skia intends to phase it out.
42 */
DEF_TEST(ColorInterp,reporter)43 DEF_TEST(ColorInterp, reporter) {
44 SkRandom r;
45
46 U8CPU a0 = 0;
47 U8CPU a255 = 255;
48 for (int i = 0; i < 200; i++) {
49 SkColor colorSrc = r.nextU();
50 SkColor colorDst = r.nextU();
51 SkPMColor src = SkPreMultiplyColor(colorSrc);
52 SkPMColor dst = SkPreMultiplyColor(colorDst);
53
54 if ((false)) {
55 REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a0) == dst);
56 REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a255) == src);
57 }
58 }
59 }
60
DEF_TEST(ColorFastIterp,reporter)61 DEF_TEST(ColorFastIterp, reporter) {
62 SkRandom r;
63
64 U8CPU a0 = 0;
65 U8CPU a255 = 255;
66 for (int i = 0; i < 200; i++) {
67 SkColor colorSrc = r.nextU();
68 SkColor colorDst = r.nextU();
69 SkPMColor src = SkPreMultiplyColor(colorSrc);
70 SkPMColor dst = SkPreMultiplyColor(colorDst);
71
72 REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a0) == dst);
73 REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a255) == src);
74 }
75 }
76