diff --git a/tests/ColorFilterTest.cpp b/tests/ColorFilterTest.cpp index b2e37183b5..d0b0d77ed4 100644 --- a/tests/ColorFilterTest.cpp +++ b/tests/ColorFilterTest.cpp @@ -147,3 +147,89 @@ DEF_TEST(LumaColorFilter, reporter) { REPORTER_ASSERT(reporter, SkGetPackedB32(out) == 0); } } + +/////////////////////////////////////////////////////////////////////////////// + +#include "SkColorMatrixFilter.h" + +static void get_brightness_matrix(float amount, float matrix[20]) { + // Spec implementation + // (http://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#brightnessEquivalent) + // + memset(matrix, 0, 20 * sizeof(SkScalar)); + matrix[0] = matrix[6] = matrix[12] = amount; + matrix[18] = 1.f; +} + +static void get_grayscale_matrix(float amount, float matrix[20]) { + // Note, these values are computed to ensure MatrixNeedsClamping is false + // for amount in [0..1] + matrix[0] = 0.2126f + 0.7874f * amount; + matrix[1] = 0.7152f - 0.7152f * amount; + matrix[2] = 1.f - (matrix[0] + matrix[1]); + matrix[3] = matrix[4] = 0.f; + + matrix[5] = 0.2126f - 0.2126f * amount; + matrix[6] = 0.7152f + 0.2848f * amount; + matrix[7] = 1.f - (matrix[5] + matrix[6]); + matrix[8] = matrix[9] = 0.f; + + matrix[10] = 0.2126f - 0.2126f * amount; + matrix[11] = 0.7152f - 0.7152f * amount; + matrix[12] = 1.f - (matrix[10] + matrix[11]); + matrix[13] = matrix[14] = 0.f; + + matrix[15] = matrix[16] = matrix[17] = matrix[19] = 0.f; + matrix[18] = 1.f; +} + +static SkColorFilter* make_cf0() { + SkScalar matrix[20]; + get_brightness_matrix(0.5f, matrix); + return SkColorMatrixFilter::Create(matrix); +} +static SkColorFilter* make_cf1() { + SkScalar matrix[20]; + get_grayscale_matrix(1, matrix); + return SkColorMatrixFilter::Create(matrix); +} +static SkColorFilter* make_cf2() { + SkColorMatrix m0, m1; + get_brightness_matrix(0.5f, m0.fMat); + get_grayscale_matrix(1, m1.fMat); + m0.preConcat(m1); + return SkColorMatrixFilter::Create(m0); +} +static SkColorFilter* make_cf3() { + SkColorMatrix m0, m1; + get_brightness_matrix(0.5f, m0.fMat); + get_grayscale_matrix(1, m1.fMat); + m0.postConcat(m1); + return SkColorMatrixFilter::Create(m0); +} +typedef SkColorFilter* (*CFProc)(); + +// Test that a colormatrix that "should" preserve opaquness actually does. +DEF_TEST(ColorMatrixFilter, reporter) { + const CFProc procs[] = { + make_cf0, make_cf1, make_cf2, make_cf3, + }; + + for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) { + SkAutoTUnref cf(procs[i]()); + + // generate all possible r,g,b triples + for (int r = 0; r < 256; ++r) { + for (int g = 0; g < 256; ++g) { + SkPMColor storage[256]; + for (int b = 0; b < 256; ++b) { + storage[b] = SkPackARGB32(0xFF, r, g, b); + } + cf->filterSpan(storage, 256, storage); + for (int b = 0; b < 256; ++b) { + REPORTER_ASSERT(reporter, 0xFF == SkGetPackedA32(storage[b])); + } + } + } + } +}