blob: e8bc9ecd85701efd9090a008f43f0003cfffd2aa [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
reed@google.com83acbe02011-04-22 19:18:20 +00008#include "SkColor.h"
Cary Clarka4083c92017-09-15 11:59:23 -04009#include "SkColorData.h"
Ben Wagnereed61282018-04-17 14:14:51 -040010#include "SkColorPriv.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000011#include "SkMathPriv.h"
tomhudson@google.com13e812c2012-01-18 21:28:01 +000012#include "SkRandom.h"
Ben Wagnereed61282018-04-17 14:14:51 -040013#include "SkTypes.h"
reed@google.com83acbe02011-04-22 19:18:20 +000014#include "SkUnPreMultiply.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000015#include "Test.h"
reed@google.com83acbe02011-04-22 19:18:20 +000016
reed@google.com223d81d2012-10-12 14:43:28 +000017#define GetPackedR16As32(packed) (SkGetPackedR16(dc) << (8 - SK_R16_BITS))
18#define GetPackedG16As32(packed) (SkGetPackedG16(dc) << (8 - SK_G16_BITS))
19#define GetPackedB16As32(packed) (SkGetPackedB16(dc) << (8 - SK_B16_BITS))
20
humper@google.com05af1af2013-01-07 16:47:43 +000021static inline void test_premul(skiatest::Reporter* reporter) {
reed@google.com83acbe02011-04-22 19:18:20 +000022 for (int a = 0; a <= 255; a++) {
23 for (int x = 0; x <= 255; x++) {
24 SkColor c0 = SkColorSetARGB(a, x, x, x);
25 SkPMColor p0 = SkPreMultiplyColor(c0);
26
27 SkColor c1 = SkUnPreMultiply::PMColorToColor(p0);
28 SkPMColor p1 = SkPreMultiplyColor(c1);
29
30 // we can't promise that c0 == c1, since c0 -> p0 is a many to one
31 // function, however, we can promise that p0 -> c1 -> p1 : p0 == p1
32 REPORTER_ASSERT(reporter, p0 == p1);
reed@google.com75595d92011-05-03 21:27:49 +000033
34 {
35 int ax = SkMulDiv255Ceiling(x, a);
36 REPORTER_ASSERT(reporter, ax <= a);
37 }
reed@google.com83acbe02011-04-22 19:18:20 +000038 }
39 }
40}
41
tomhudson@google.com13e812c2012-01-18 21:28:01 +000042/**
43 This test fails: SkFourByteInterp does *not* preserve opaque destinations.
44 SkAlpha255To256 implemented as (alpha + 1) is faster than
45 (alpha + (alpha >> 7)), but inaccurate, and Skia intends to phase it out.
46*/
47/*
48static void test_interp(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000049 SkRandom r;
tomhudson@google.com13e812c2012-01-18 21:28:01 +000050
51 U8CPU a0 = 0;
52 U8CPU a255 = 255;
53 for (int i = 0; i < 200; i++) {
54 SkColor colorSrc = r.nextU();
55 SkColor colorDst = r.nextU();
56 SkPMColor src = SkPreMultiplyColor(colorSrc);
57 SkPMColor dst = SkPreMultiplyColor(colorDst);
58
59 REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a0) == dst);
60 REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a255) == src);
61 }
62}
63*/
64
humper@google.com05af1af2013-01-07 16:47:43 +000065static inline void test_fast_interp(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000066 SkRandom r;
tomhudson@google.com13e812c2012-01-18 21:28:01 +000067
68 U8CPU a0 = 0;
69 U8CPU a255 = 255;
70 for (int i = 0; i < 200; i++) {
71 SkColor colorSrc = r.nextU();
72 SkColor colorDst = r.nextU();
73 SkPMColor src = SkPreMultiplyColor(colorSrc);
74 SkPMColor dst = SkPreMultiplyColor(colorDst);
75
76 REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a0) == dst);
77 REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a255) == src);
78 }
79}
reed@google.com83acbe02011-04-22 19:18:20 +000080
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000081DEF_TEST(Color, reporter) {
reed@google.com83acbe02011-04-22 19:18:20 +000082 test_premul(reporter);
tomhudson@google.com13e812c2012-01-18 21:28:01 +000083 //test_interp(reporter);
84 test_fast_interp(reporter);
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000085 //test_565blend();
reed@google.com83acbe02011-04-22 19:18:20 +000086}