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