blob: 0032c7f648dcb33323a47555f10ef4649b713673 [file] [log] [blame]
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001
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"
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +000010#include "SkBicubicImageFilter.h"
11#include "SkBitmap.h"
12#include "SkBitmapDevice.h"
13#include "SkBitmapSource.h"
14#include "SkCanvas.h"
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000015#include "SkColorMatrixFilter.h"
16#include "SkColorFilterImageFilter.h"
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +000017#include "SkDeviceImageFilterProxy.h"
18#include "SkLightingImageFilter.h"
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000019#include "SkRect.h"
20
21class ImageFilterTest {
22public:
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +000023 static const int kBitmapSize = 4;
24
25 static void make_small_bitmap(SkBitmap& bitmap) {
26 bitmap.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize);
27 bitmap.allocPixels();
28 SkBitmapDevice device(bitmap);
29 SkCanvas canvas(&device);
30 canvas.clear(0x00000000);
31 SkPaint darkPaint;
32 darkPaint.setColor(0xFF804020);
33 SkPaint lightPaint;
34 lightPaint.setColor(0xFF244484);
35 const int i = kBitmapSize / 4;
36 for (int y = 0; y < kBitmapSize; y += i) {
37 for (int x = 0; x < kBitmapSize; x += i) {
38 canvas.save();
39 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
40 canvas.drawRect(SkRect::MakeXYWH(0, 0, i, i), darkPaint);
41 canvas.drawRect(SkRect::MakeXYWH(i, 0, i, i), lightPaint);
42 canvas.drawRect(SkRect::MakeXYWH(0, i, i, i), lightPaint);
43 canvas.drawRect(SkRect::MakeXYWH(i, i, i, i), darkPaint);
44 canvas.restore();
45 }
46 }
47 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000048
49 static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) {
50 SkScalar s = SkFloatToScalar(amount);
51 SkScalar matrix[20] = { s, 0, 0, 0, 0,
52 0, s, 0, 0, 0,
53 0, 0, s, 0, 0,
54 0, 0, 0, s, 0 };
55 SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
56 return SkColorFilterImageFilter::Create(filter, input);
57 }
58
59 static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkIRect* cropRect = NULL) {
60 SkScalar matrix[20];
61 memset(matrix, 0, 20 * sizeof(SkScalar));
62 matrix[0] = matrix[5] = matrix[10] = SkFloatToScalar(0.2126f);
63 matrix[1] = matrix[6] = matrix[11] = SkFloatToScalar(0.7152f);
64 matrix[2] = matrix[7] = matrix[12] = SkFloatToScalar(0.0722f);
65 matrix[18] = SkFloatToScalar(1.0f);
66 SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
67 return SkColorFilterImageFilter::Create(filter, input, cropRect);
68 }
69
70 static SkImageFilter* make_mode_blue(SkImageFilter* input = NULL) {
71 SkAutoTUnref<SkColorFilter> filter(
72 SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode));
73 return SkColorFilterImageFilter::Create(filter, input);
74 }
75
76 static void Test(skiatest::Reporter* reporter) {
77 {
78 // Check that two non-clipping color matrices concatenate into a single filter.
79 SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f));
80 SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfBrightness));
81 REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0));
82 }
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +000083
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000084 {
85 // Check that a clipping color matrix followed by a grayscale does not concatenate into a single filter.
86 SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f));
87 SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBrightness));
88 REPORTER_ASSERT(reporter, NULL != halfBrightness->getInput(0));
89 }
90
91 {
92 // Check that a color filter image filter without a crop rect can be
93 // expressed as a color filter.
94 SkAutoTUnref<SkImageFilter> gray(make_grayscale());
95 REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL));
96 }
97
98 {
99 // Check that a color filter image filter with a crop rect cannot
100 // be expressed as a color filter.
101 SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100);
102 SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect));
103 REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL));
104 }
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +0000105
106 {
107 // Tests pass by not asserting
108 SkBitmap bitmap, result;
109 make_small_bitmap(bitmap);
110 result.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize);
111 result.allocPixels();
112
113 {
114 // This tests for :
115 // 1 ) location at (0,0,1)
116 SkPoint3 location(0, 0, SK_Scalar1);
117 // 2 ) location and target at same value
118 SkPoint3 target(location.fX, location.fY, location.fZ);
119 // 3 ) large negative specular exponent value
120 SkScalar specularExponent = SkFloatToScalar(-1000);
121
122 SkPaint paint;
123 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(
124 location, target, specularExponent, SkFloatToScalar(180),
125 0xFFFFFFFF, SK_Scalar1, SK_Scalar1, SK_Scalar1,
126 new SkBitmapSource(bitmap)))->unref();
127 SkCanvas canvas(result);
128 SkRect r = SkRect::MakeWH(kBitmapSize, kBitmapSize);
129 canvas.drawRect(r, paint);
130 }
131
132 {
133 // This tests for scale bringing width to 0
134 SkSize scale = SkSize::Make(SkFloatToScalar(-0.001), SK_Scalar1);
135 SkAutoTUnref<SkBicubicImageFilter> bicubic(
136 SkBicubicImageFilter::CreateMitchell(
137 scale, new SkBitmapSource(bitmap)));
138 SkBitmapDevice device(bitmap);
139 SkDeviceImageFilterProxy proxy(&device);
140 SkIPoint loc = SkIPoint::Make(0, 0);
141 // An empty input should early return and return false
142 REPORTER_ASSERT(reporter,
143 !bicubic->filterImage(&proxy, bitmap, SkMatrix::I(), &result, &loc));
144 }
145 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000146 }
147};
148
149
150#include "TestClassDef.h"
151DEFINE_TESTCLASS("ImageFilterTest", ImageFilterTestClass, ImageFilterTest::Test)