blob: 44b917c205af8c78e943fcd320c6fc86ef22e5b0 [file] [log] [blame]
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Test.h"
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +00009#include "TestClassDef.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"
senorblanco@chromium.org6776b822014-01-03 21:48:22 +000018#include "SkBlurImageFilter.h"
19#include "SkDisplacementMapEffect.h"
20#include "SkDropShadowImageFilter.h"
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +000021#include "SkLightingImageFilter.h"
senorblanco@chromium.org6776b822014-01-03 21:48:22 +000022#include "SkMergeImageFilter.h"
23#include "SkMorphologyImageFilter.h"
24#include "SkMatrixConvolutionImageFilter.h"
25#include "SkOffsetImageFilter.h"
26#include "SkTileImageFilter.h"
27#include "SkXfermodeImageFilter.h"
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000028#include "SkRect.h"
29
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000030static const int kBitmapSize = 4;
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +000031
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000032static void make_small_bitmap(SkBitmap& bitmap) {
33 bitmap.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize);
34 bitmap.allocPixels();
35 SkBitmapDevice device(bitmap);
36 SkCanvas canvas(&device);
37 canvas.clear(0x00000000);
38 SkPaint darkPaint;
39 darkPaint.setColor(0xFF804020);
40 SkPaint lightPaint;
41 lightPaint.setColor(0xFF244484);
42 const int i = kBitmapSize / 4;
43 for (int y = 0; y < kBitmapSize; y += i) {
44 for (int x = 0; x < kBitmapSize; x += i) {
45 canvas.save();
46 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
47 canvas.drawRect(SkRect::MakeXYWH(0, 0,
48 SkIntToScalar(i),
49 SkIntToScalar(i)), darkPaint);
50 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i),
51 0,
52 SkIntToScalar(i),
53 SkIntToScalar(i)), lightPaint);
54 canvas.drawRect(SkRect::MakeXYWH(0,
55 SkIntToScalar(i),
56 SkIntToScalar(i),
57 SkIntToScalar(i)), lightPaint);
58 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i),
59 SkIntToScalar(i),
60 SkIntToScalar(i),
61 SkIntToScalar(i)), darkPaint);
62 canvas.restore();
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +000063 }
64 }
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000065}
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000066
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000067static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) {
68 SkScalar s = amount;
69 SkScalar matrix[20] = { s, 0, 0, 0, 0,
70 0, s, 0, 0, 0,
71 0, 0, s, 0, 0,
72 0, 0, 0, s, 0 };
73 SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
74 return SkColorFilterImageFilter::Create(filter, input);
75}
76
77static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageFilter::CropRect* cropRect = NULL) {
78 SkScalar matrix[20];
79 memset(matrix, 0, 20 * sizeof(SkScalar));
80 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
81 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
82 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
83 matrix[18] = 1.0f;
84 SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
85 return SkColorFilterImageFilter::Create(filter, input, cropRect);
86}
87
88DEF_TEST(ImageFilter, reporter) {
89 {
90 // Check that two non-clipping color matrices concatenate into a single filter.
91 SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f));
92 SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfBrightness));
93 REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0));
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000094 }
95
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000096 {
97 // Check that a clipping color matrix followed by a grayscale does not concatenate into a single filter.
98 SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f));
99 SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBrightness));
100 REPORTER_ASSERT(reporter, NULL != halfBrightness->getInput(0));
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000101 }
102
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000103 {
104 // Check that a color filter image filter without a crop rect can be
105 // expressed as a color filter.
106 SkAutoTUnref<SkImageFilter> gray(make_grayscale());
107 REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL));
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000108 }
109
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000110 {
111 // Check that a color filter image filter with a crop rect cannot
112 // be expressed as a color filter.
113 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(0, 0, 100, 100));
114 SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect));
115 REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL));
116 }
117
118 {
119 // Tests pass by not asserting
120 SkBitmap bitmap, result;
121 make_small_bitmap(bitmap);
122 result.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize);
123 result.allocPixels();
124
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000125 {
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000126 // This tests for :
127 // 1 ) location at (0,0,1)
128 SkPoint3 location(0, 0, SK_Scalar1);
129 // 2 ) location and target at same value
130 SkPoint3 target(location.fX, location.fY, location.fZ);
131 // 3 ) large negative specular exponent value
132 SkScalar specularExponent = -1000;
133
134 SkAutoTUnref<SkImageFilter> bmSrc(new SkBitmapSource(bitmap));
135 SkPaint paint;
136 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(
137 location, target, specularExponent, 180,
138 0xFFFFFFFF, SK_Scalar1, SK_Scalar1, SK_Scalar1,
139 bmSrc))->unref();
140 SkCanvas canvas(result);
141 SkRect r = SkRect::MakeWH(SkIntToScalar(kBitmapSize),
142 SkIntToScalar(kBitmapSize));
143 canvas.drawRect(r, paint);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000144 }
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000145
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000146 {
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000147 // This tests for scale bringing width to 0
148 SkSize scale = SkSize::Make(-0.001f, SK_Scalar1);
149 SkAutoTUnref<SkImageFilter> bmSrc(new SkBitmapSource(bitmap));
150 SkAutoTUnref<SkBicubicImageFilter> bicubic(
151 SkBicubicImageFilter::CreateMitchell(scale, bmSrc));
152 SkBitmapDevice device(bitmap);
153 SkDeviceImageFilterProxy proxy(&device);
154 SkIPoint loc = SkIPoint::Make(0, 0);
155 // An empty input should early return and return false
156 REPORTER_ASSERT(reporter,
157 !bicubic->filterImage(&proxy, bitmap, SkMatrix::I(), &result, &loc));
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +0000158 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000159 }
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000160
161 {
162 // Check that all filters offset to their absolute crop rect,
163 // unaffected by the input crop rect.
164 // Tests pass by not asserting.
165 SkBitmap bitmap, temp;
166 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
167 temp.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
168 bitmap.allocPixels();
169 temp.allocPixels();
170 bitmap.eraseARGB(0, 0, 0, 0);
171 SkBitmapDevice device(temp);
172 SkDeviceImageFilterProxy proxy(&device);
173
174 SkImageFilter::CropRect inputCropRect(SkRect::MakeXYWH(8, 13, 80, 80));
175 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 30, 60, 60));
176 SkAutoTUnref<SkImageFilter> input(make_grayscale(NULL, &inputCropRect));
177
178 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, SkXfermode::kSrcIn_Mode));
179 SkPoint3 location(0, 0, SK_Scalar1);
180 SkPoint3 target(SK_Scalar1, SK_Scalar1, SK_Scalar1);
181 SkScalar kernel[9] = {
182 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
183 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
184 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
185 };
186 SkISize kernelSize = SkISize::Make(3, 3);
187 SkScalar gain = SK_Scalar1, bias = 0;
188
189 SkImageFilter* filters[] = {
190 SkColorFilterImageFilter::Create(cf.get(), input.get(), &cropRect),
191 new SkDisplacementMapEffect(SkDisplacementMapEffect::kR_ChannelSelectorType,
192 SkDisplacementMapEffect::kB_ChannelSelectorType,
193 40.0f, input.get(), input.get(), &cropRect),
194 new SkBlurImageFilter(SK_Scalar1, SK_Scalar1, input.get(), &cropRect),
195 new SkDropShadowImageFilter(SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_ColorGREEN, input.get(), &cropRect),
196 SkLightingImageFilter::CreatePointLitDiffuse(location, SK_ColorGREEN, 0, 0, input.get(), &cropRect),
197 SkLightingImageFilter::CreatePointLitSpecular(location, SK_ColorGREEN, 0, 0, 0, input.get(), &cropRect),
198 new SkMatrixConvolutionImageFilter(kernelSize, kernel, gain, bias, SkIPoint::Make(1, 1), SkMatrixConvolutionImageFilter::kRepeat_TileMode, false, input.get(), &cropRect),
199 new SkMergeImageFilter(input.get(), input.get(), SkXfermode::kSrcOver_Mode, &cropRect),
200 new SkOffsetImageFilter(SK_Scalar1, SK_Scalar1, input.get(), &cropRect),
201 new SkOffsetImageFilter(SK_Scalar1, SK_Scalar1, input.get(), &cropRect),
202 new SkDilateImageFilter(3, 2, input.get(), &cropRect),
203 new SkErodeImageFilter(2, 3, input.get(), &cropRect),
204 new SkTileImageFilter(inputCropRect.rect(), cropRect.rect(), input.get()),
205 new SkXfermodeImageFilter(SkXfermode::Create(SkXfermode::kSrcOver_Mode), input.get(), input.get(), &cropRect),
206 };
207
208 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
209 SkImageFilter* filter = filters[i];
210 SkBitmap result;
211 SkIPoint offset;
212 REPORTER_ASSERT(reporter, filter->filterImage(&proxy, bitmap, SkMatrix::I(), &result, &offset));
213 REPORTER_ASSERT(reporter, offset.fX == 20 && offset.fY == 30);
214 }
215
216 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
217 SkSafeUnref(filters[i]);
218 }
219 }
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000220}