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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkTypes.h" |
| 10 | #include "include/core/SkUnPreMultiply.h" |
| 11 | #include "include/private/SkColorData.h" |
| 12 | #include "include/utils/SkRandom.h" |
| 13 | #include "src/core/SkMathPriv.h" |
| 14 | #include "tests/Test.h" |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 15 | |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 16 | DEF_TEST(ColorPremul, reporter) { |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 17 | for (int a = 0; a <= 255; a++) { |
| 18 | for (int x = 0; x <= 255; x++) { |
| 19 | SkColor c0 = SkColorSetARGB(a, x, x, x); |
| 20 | SkPMColor p0 = SkPreMultiplyColor(c0); |
| 21 | |
| 22 | SkColor c1 = SkUnPreMultiply::PMColorToColor(p0); |
| 23 | SkPMColor p1 = SkPreMultiplyColor(c1); |
| 24 | |
| 25 | // we can't promise that c0 == c1, since c0 -> p0 is a many to one |
| 26 | // function, however, we can promise that p0 -> c1 -> p1 : p0 == p1 |
| 27 | REPORTER_ASSERT(reporter, p0 == p1); |
reed@google.com | 75595d9 | 2011-05-03 21:27:49 +0000 | [diff] [blame] | 28 | |
| 29 | { |
| 30 | int ax = SkMulDiv255Ceiling(x, a); |
| 31 | REPORTER_ASSERT(reporter, ax <= a); |
| 32 | } |
reed@google.com | 83acbe0 | 2011-04-22 19:18:20 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 37 | /** |
| 38 | This test fails: SkFourByteInterp does *not* preserve opaque destinations. |
| 39 | SkAlpha255To256 implemented as (alpha + 1) is faster than |
| 40 | (alpha + (alpha >> 7)), but inaccurate, and Skia intends to phase it out. |
| 41 | */ |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 42 | DEF_TEST(ColorInterp, reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 43 | SkRandom r; |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 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 | |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 53 | if (false) { |
| 54 | REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a0) == dst); |
| 55 | REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a255) == src); |
| 56 | } |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 59 | |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 60 | DEF_TEST(ColorFastIterp, reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 61 | SkRandom r; |
tomhudson@google.com | 13e812c | 2012-01-18 21:28:01 +0000 | [diff] [blame] | 62 | |
| 63 | U8CPU a0 = 0; |
| 64 | U8CPU a255 = 255; |
| 65 | for (int i = 0; i < 200; i++) { |
| 66 | SkColor colorSrc = r.nextU(); |
| 67 | SkColor colorDst = r.nextU(); |
| 68 | SkPMColor src = SkPreMultiplyColor(colorSrc); |
| 69 | SkPMColor dst = SkPreMultiplyColor(colorDst); |
| 70 | |
| 71 | REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a0) == dst); |
| 72 | REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a255) == src); |
| 73 | } |
| 74 | } |