blob: 83e2e3fada07c87e91abb4a31536b7c138bb36e3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.com83acbe02011-04-22 19:18:20 +00008#include "Test.h"
9#include "SkColor.h"
tomhudson@google.com13e812c2012-01-18 21:28:01 +000010#include "SkColorPriv.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000011#include "SkMath.h"
tomhudson@google.com13e812c2012-01-18 21:28:01 +000012#include "SkRandom.h"
reed@google.com83acbe02011-04-22 19:18:20 +000013#include "SkUnPreMultiply.h"
14
15static void test_premul(skiatest::Reporter* reporter) {
16 for (int a = 0; a <= 255; a++) {
17 for (int x = 0; x <= 255; x++) {
18 SkColor c0 = SkColorSetARGB(a, x, x, x);
19 SkPMColor p0 = SkPreMultiplyColor(c0);
20
21 SkColor c1 = SkUnPreMultiply::PMColorToColor(p0);
22 SkPMColor p1 = SkPreMultiplyColor(c1);
23
24 // we can't promise that c0 == c1, since c0 -> p0 is a many to one
25 // function, however, we can promise that p0 -> c1 -> p1 : p0 == p1
26 REPORTER_ASSERT(reporter, p0 == p1);
reed@google.com75595d92011-05-03 21:27:49 +000027
28 {
29 int ax = SkMulDiv255Ceiling(x, a);
30 REPORTER_ASSERT(reporter, ax <= a);
31 }
reed@google.com83acbe02011-04-22 19:18:20 +000032 }
33 }
34}
35
tomhudson@google.com13e812c2012-01-18 21:28:01 +000036/**
37 This test fails: SkFourByteInterp does *not* preserve opaque destinations.
38 SkAlpha255To256 implemented as (alpha + 1) is faster than
39 (alpha + (alpha >> 7)), but inaccurate, and Skia intends to phase it out.
40*/
41/*
42static void test_interp(skiatest::Reporter* 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 REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a0) == dst);
54 REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a255) == src);
55 }
56}
57*/
58
59static void test_fast_interp(skiatest::Reporter* reporter) {
60 SkRandom r;
61
62 U8CPU a0 = 0;
63 U8CPU a255 = 255;
64 for (int i = 0; i < 200; i++) {
65 SkColor colorSrc = r.nextU();
66 SkColor colorDst = r.nextU();
67 SkPMColor src = SkPreMultiplyColor(colorSrc);
68 SkPMColor dst = SkPreMultiplyColor(colorDst);
69
70 REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a0) == dst);
71 REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a255) == src);
72 }
73}
reed@google.com83acbe02011-04-22 19:18:20 +000074
75static void TestColor(skiatest::Reporter* reporter) {
76 test_premul(reporter);
tomhudson@google.com13e812c2012-01-18 21:28:01 +000077 //test_interp(reporter);
78 test_fast_interp(reporter);
reed@google.com83acbe02011-04-22 19:18:20 +000079}
80
81#include "TestClassDef.h"
82DEFINE_TESTCLASS("Color", ColorTestClass, TestColor)