ImageFilterTest.cpp revision 5c561cb8d8151d38c69128345106cbf225033a1a
1
2/*
3 * Copyright 2013 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "Test.h"
10#include "SkColorMatrixFilter.h"
11#include "SkColorFilterImageFilter.h"
12#include "SkRect.h"
13
14class ImageFilterTest {
15public:
16
17    static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) {
18        SkScalar s = SkFloatToScalar(amount);
19        SkScalar matrix[20] = { s, 0, 0, 0, 0,
20                                0, s, 0, 0, 0,
21                                0, 0, s, 0, 0,
22                                0, 0, 0, s, 0 };
23        SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
24        return SkColorFilterImageFilter::Create(filter, input);
25    }
26
27    static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkIRect* cropRect = NULL) {
28        SkScalar matrix[20];
29        memset(matrix, 0, 20 * sizeof(SkScalar));
30        matrix[0] = matrix[5] = matrix[10] = SkFloatToScalar(0.2126f);
31        matrix[1] = matrix[6] = matrix[11] = SkFloatToScalar(0.7152f);
32        matrix[2] = matrix[7] = matrix[12] = SkFloatToScalar(0.0722f);
33        matrix[18] = SkFloatToScalar(1.0f);
34        SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
35        return SkColorFilterImageFilter::Create(filter, input, cropRect);
36    }
37
38    static SkImageFilter* make_mode_blue(SkImageFilter* input = NULL) {
39        SkAutoTUnref<SkColorFilter> filter(
40            SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode));
41        return SkColorFilterImageFilter::Create(filter, input);
42    }
43
44    static void Test(skiatest::Reporter* reporter) {
45        {
46            // Check that two non-clipping color matrices concatenate into a single filter.
47            SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f));
48            SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfBrightness));
49            REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0));
50        }
51
52        {
53            // Check that a clipping color matrix followed by a grayscale does not concatenate into a single filter.
54            SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f));
55            SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBrightness));
56            REPORTER_ASSERT(reporter, NULL != halfBrightness->getInput(0));
57        }
58
59        {
60            // Check that a color filter image filter without a crop rect can be
61            // expressed as a color filter.
62            SkAutoTUnref<SkImageFilter> gray(make_grayscale());
63            REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL));
64        }
65
66        {
67            // Check that a color filter image filter with a crop rect cannot
68            // be expressed as a color filter.
69            SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100);
70            SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect));
71            REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL));
72        }
73    }
74};
75
76
77#include "TestClassDef.h"
78DEFINE_TESTCLASS("ImageFilterTest", ImageFilterTestClass, ImageFilterTest::Test)
79