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