epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 8 | #include "SkColor.h" |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 9 | #include "SkColorPriv.h" |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 10 | #include "SkMathPriv.h" |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 11 | #include "SkRandom.h" |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 12 | #include "SkUnPreMultiply.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "Test.h" |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 14 | |
reed@google.com | 223d81d | 2012-10-12 14:43:28 +0000 | [diff] [blame] | 15 | #define GetPackedR16As32(packed) (SkGetPackedR16(dc) << (8 - SK_R16_BITS)) |
| 16 | #define GetPackedG16As32(packed) (SkGetPackedG16(dc) << (8 - SK_G16_BITS)) |
| 17 | #define GetPackedB16As32(packed) (SkGetPackedB16(dc) << (8 - SK_B16_BITS)) |
| 18 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 19 | static inline void test_premul(skiatest::Reporter* reporter) { |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 20 | for (int a = 0; a <= 255; a++) { |
| 21 | for (int x = 0; x <= 255; x++) { |
| 22 | SkColor c0 = SkColorSetARGB(a, x, x, x); |
| 23 | SkPMColor p0 = SkPreMultiplyColor(c0); |
| 24 | |
| 25 | SkColor c1 = SkUnPreMultiply::PMColorToColor(p0); |
| 26 | SkPMColor p1 = SkPreMultiplyColor(c1); |
| 27 | |
| 28 | // we can't promise that c0 == c1, since c0 -> p0 is a many to one |
| 29 | // function, however, we can promise that p0 -> c1 -> p1 : p0 == p1 |
| 30 | REPORTER_ASSERT(reporter, p0 == p1); |
reed@google.com | 75595d9 | 2011-05-03 21:27:49 +0000 | [diff] [blame] | 31 | |
| 32 | { |
| 33 | int ax = SkMulDiv255Ceiling(x, a); |
| 34 | REPORTER_ASSERT(reporter, ax <= a); |
| 35 | } |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 40 | /** |
| 41 | This test fails: SkFourByteInterp does *not* preserve opaque destinations. |
| 42 | SkAlpha255To256 implemented as (alpha + 1) is faster than |
| 43 | (alpha + (alpha >> 7)), but inaccurate, and Skia intends to phase it out. |
| 44 | */ |
| 45 | /* |
| 46 | static void test_interp(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 47 | SkRandom r; |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 48 | |
| 49 | U8CPU a0 = 0; |
| 50 | U8CPU a255 = 255; |
| 51 | for (int i = 0; i < 200; i++) { |
| 52 | SkColor colorSrc = r.nextU(); |
| 53 | SkColor colorDst = r.nextU(); |
| 54 | SkPMColor src = SkPreMultiplyColor(colorSrc); |
| 55 | SkPMColor dst = SkPreMultiplyColor(colorDst); |
| 56 | |
| 57 | REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a0) == dst); |
| 58 | REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a255) == src); |
| 59 | } |
| 60 | } |
| 61 | */ |
| 62 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 63 | static inline void test_fast_interp(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 64 | SkRandom r; |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 65 | |
| 66 | U8CPU a0 = 0; |
| 67 | U8CPU a255 = 255; |
| 68 | for (int i = 0; i < 200; i++) { |
| 69 | SkColor colorSrc = r.nextU(); |
| 70 | SkColor colorDst = r.nextU(); |
| 71 | SkPMColor src = SkPreMultiplyColor(colorSrc); |
| 72 | SkPMColor dst = SkPreMultiplyColor(colorDst); |
| 73 | |
| 74 | REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a0) == dst); |
| 75 | REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a255) == src); |
| 76 | } |
| 77 | } |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 78 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 79 | DEF_TEST(Color, reporter) { |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 80 | test_premul(reporter); |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 81 | //test_interp(reporter); |
| 82 | test_fast_interp(reporter); |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 83 | //test_565blend(); |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 84 | } |