From 8f2911f8407b5e151768690ed40ecedde6cd7ad8 Mon Sep 17 00:00:00 2001 From: Mike Klein Date: Wed, 19 Apr 2017 12:40:52 -0400 Subject: [PATCH] add unit tests for parametric_* stages Change-Id: I7fab6d1c7240c17f2cc8436e8c6e7c8d2df940bb Reviewed-on: https://skia-review.googlesource.com/13814 Reviewed-by: Matt Sarett Commit-Queue: Mike Klein --- gn/tests.gni | 1 + tests/ParametricStageTest.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 tests/ParametricStageTest.cpp diff --git a/gn/tests.gni b/gn/tests.gni index 4350ef2..be368f0 100644 --- a/gn/tests.gni +++ b/gn/tests.gni @@ -141,6 +141,7 @@ tests_sources = [ "$_tests/PaintBreakTextTest.cpp", "$_tests/PaintImageFilterTest.cpp", "$_tests/PaintTest.cpp", + "$_tests/ParametricStageTest.cpp", "$_tests/ParsePathTest.cpp", "$_tests/PathCoverageTest.cpp", "$_tests/PathMeasureTest.cpp", diff --git a/tests/ParametricStageTest.cpp b/tests/ParametricStageTest.cpp new file mode 100644 index 0000000..5810148 --- /dev/null +++ b/tests/ParametricStageTest.cpp @@ -0,0 +1,75 @@ +/* + * Copyright 2017 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkColorSpace.h" +#include "SkRasterPipeline.h" +#include "Test.h" + +static void check_error(skiatest::Reporter* r, float limit, SkColorSpaceTransferFn fn) { + float in[256], out[256]; + for (int i = 0; i < 256; i++) { + in [i] = i / 255.0f; + out[i] = 0.0f; // Not likely important. Just being tidy. + } + + const float* ip = in; + float* op = out; + + SkRasterPipeline p; + p.append(SkRasterPipeline::load_f32, &ip); + p.append(SkRasterPipeline::parametric_r, &fn); + p.append(SkRasterPipeline::parametric_g, &fn); + p.append(SkRasterPipeline::parametric_b, &fn); + p.append(SkRasterPipeline::parametric_a, &fn); + p.append(SkRasterPipeline::store_f32, &op); + + p.run(0, 256/4); + + + for (int i = 0; i < 256; i++) { + float want = (in[i] <= fn.fD) ? fn.fC * in[i] + fn.fF + : powf(in[i] * fn.fA + fn.fB, fn.fG) + fn.fE; + float err = fabsf(out[i] - want); + if (err > limit) { + ERRORF(r, "At %d, error was %g (got %g, want %g)", i, err, out[i], want); + } + } +} + +static void check_error(skiatest::Reporter* r, float limit, float gamma) { + check_error(r, limit, { gamma, 1.0f,0,0,0,0,0 }); +} + +DEF_TEST(Parametric_sRGB, r) { + // Test our good buddy the sRGB transfer function in resplendent 7-parameter glory. + check_error(r, 1/510.0f, { + 2.4f, + 1.0f / 1.055f, + 0.055f / 1.055f, + 1.0f / 12.92f, + 0.04045f, + 0.0f, + 0.0f, + }); +} + +// A nice little spread of simple gammas. +DEF_TEST(Parametric_1dot0, r) { check_error(r, 1/510.0f, 1.0f); } + +DEF_TEST(Parametric_1dot2, r) { check_error(r, 1/510.0f, 1.2f); } +DEF_TEST(Parametric_1dot4, r) { check_error(r, 1/510.0f, 1.4f); } +DEF_TEST(Parametric_1dot8, r) { check_error(r, 1/510.0f, 1.8f); } +DEF_TEST(Parametric_2dot0, r) { check_error(r, 1/510.0f, 2.0f); } +DEF_TEST(Parametric_2dot2, r) { check_error(r, 1/510.0f, 2.2f); } +DEF_TEST(Parametric_2dot4, r) { check_error(r, 1/510.0f, 2.4f); } + +DEF_TEST(Parametric_inv_1dot2, r) { check_error(r, 1/510.0f, 1/1.2f); } +DEF_TEST(Parametric_inv_1dot4, r) { check_error(r, 1/510.0f, 1/1.4f); } +DEF_TEST(Parametric_inv_1dot8, r) { check_error(r, 1/510.0f, 1/1.8f); } +DEF_TEST(Parametric_inv_2dot0, r) { check_error(r, 1/510.0f, 1/2.0f); } +DEF_TEST(Parametric_inv_2dot2, r) { check_error(r, 1/510.0f, 1/2.2f); } +DEF_TEST(Parametric_inv_2dot4, r) { check_error(r, 1/510.0f, 1/2.4f); } -- 2.7.4