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