blob: c41e3bd62124ae2bfa6b80a5f4e3b8a51fe8c100 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#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"
reed@google.com83acbe02011-04-22 19:18:20 +000015
Ben Wagner9707a7e2019-05-06 17:17:19 -040016DEF_TEST(ColorPremul, reporter) {
reed@google.com83acbe02011-04-22 19:18:20 +000017 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);
reed@google.com75595d92011-05-03 21:27:49 +000028
29 {
30 int ax = SkMulDiv255Ceiling(x, a);
31 REPORTER_ASSERT(reporter, ax <= a);
32 }
reed@google.com83acbe02011-04-22 19:18:20 +000033 }
34 }
35}
36
tomhudson@google.com13e812c2012-01-18 21:28:01 +000037/**
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*/
Ben Wagner9707a7e2019-05-06 17:17:19 -040042DEF_TEST(ColorInterp, reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000043 SkRandom r;
tomhudson@google.com13e812c2012-01-18 21:28:01 +000044
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
Ben Wagner9707a7e2019-05-06 17:17:19 -040053 if (false) {
54 REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a0) == dst);
55 REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a255) == src);
56 }
tomhudson@google.com13e812c2012-01-18 21:28:01 +000057 }
58}
tomhudson@google.com13e812c2012-01-18 21:28:01 +000059
Ben Wagner9707a7e2019-05-06 17:17:19 -040060DEF_TEST(ColorFastIterp, reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000061 SkRandom r;
tomhudson@google.com13e812c2012-01-18 21:28:01 +000062
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}