add test for opaque-preserving colormatrixfilters

BUG=skia:

Review URL: https://codereview.chromium.org/1069463002
This commit is contained in:
reed 2015-04-09 08:27:27 -07:00 committed by Commit bot
parent 62db8ba68b
commit 0b18c3554c

View File

@ -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)
// <feFunc[R|G|B] type="linear" slope="[amount]">
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<SkColorFilter> 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]));
}
}
}
}
}