epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkColor.h" |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
tomhudson@google.com | 889bd8b | 2011-09-27 17:38:17 +0000 | [diff] [blame] | 11 | #include "SkMath.h" |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 12 | #include "SkRandom.h" |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 13 | #include "SkUnPreMultiply.h" |
| 14 | |
| 15 | static 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.com | 75595d9 | 2011-05-03 21:27:49 +0000 | [diff] [blame] | 27 | |
| 28 | { |
| 29 | int ax = SkMulDiv255Ceiling(x, a); |
| 30 | REPORTER_ASSERT(reporter, ax <= a); |
| 31 | } |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 36 | /** |
| 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 | /* |
| 42 | static 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 | |
| 59 | static 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.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 74 | |
| 75 | static void TestColor(skiatest::Reporter* reporter) { |
| 76 | test_premul(reporter); |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 77 | //test_interp(reporter); |
| 78 | test_fast_interp(reporter); |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | #include "TestClassDef.h" |
| 82 | DEFINE_TESTCLASS("Color", ColorTestClass, TestColor) |