blob: 4165f2f75011856e50a90d12764796986a734eb6 [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));
skia.committer@gmail.coma604c4f2013-09-17 07:01:20 +000040 canvas.drawRect(SkRect::MakeXYWH(0, 0,
41 SkIntToScalar(i),
robertphillips@google.com15883542013-09-16 14:42:48 +000042 SkIntToScalar(i)), darkPaint);
skia.committer@gmail.coma604c4f2013-09-17 07:01:20 +000043 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i),
44 0,
45 SkIntToScalar(i),
robertphillips@google.com15883542013-09-16 14:42:48 +000046 SkIntToScalar(i)), lightPaint);
skia.committer@gmail.coma604c4f2013-09-17 07:01:20 +000047 canvas.drawRect(SkRect::MakeXYWH(0,
48 SkIntToScalar(i),
49 SkIntToScalar(i),
robertphillips@google.com15883542013-09-16 14:42:48 +000050 SkIntToScalar(i)), lightPaint);
skia.committer@gmail.coma604c4f2013-09-17 07:01:20 +000051 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i),
52 SkIntToScalar(i),
53 SkIntToScalar(i),
robertphillips@google.com15883542013-09-16 14:42:48 +000054 SkIntToScalar(i)), darkPaint);
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +000055 canvas.restore();
56 }
57 }
58 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000059
60 static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) {
61 SkScalar s = SkFloatToScalar(amount);
62 SkScalar matrix[20] = { s, 0, 0, 0, 0,
63 0, s, 0, 0, 0,
64 0, 0, s, 0, 0,
65 0, 0, 0, s, 0 };
66 SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
67 return SkColorFilterImageFilter::Create(filter, input);
68 }
69
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000070 static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageFilter::CropRect* cropRect = NULL) {
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000071 SkScalar matrix[20];
72 memset(matrix, 0, 20 * sizeof(SkScalar));
73 matrix[0] = matrix[5] = matrix[10] = SkFloatToScalar(0.2126f);
74 matrix[1] = matrix[6] = matrix[11] = SkFloatToScalar(0.7152f);
75 matrix[2] = matrix[7] = matrix[12] = SkFloatToScalar(0.0722f);
76 matrix[18] = SkFloatToScalar(1.0f);
77 SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
78 return SkColorFilterImageFilter::Create(filter, input, cropRect);
79 }
80
81 static SkImageFilter* make_mode_blue(SkImageFilter* input = NULL) {
82 SkAutoTUnref<SkColorFilter> filter(
83 SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode));
84 return SkColorFilterImageFilter::Create(filter, input);
85 }
86
87 static void Test(skiatest::Reporter* reporter) {
88 {
89 // Check that two non-clipping color matrices concatenate into a single filter.
90 SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f));
91 SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfBrightness));
92 REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0));
93 }
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +000094
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000095 {
96 // Check that a clipping color matrix followed by a grayscale does not concatenate into a single filter.
97 SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f));
98 SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBrightness));
99 REPORTER_ASSERT(reporter, NULL != halfBrightness->getInput(0));
100 }
101
102 {
103 // Check that a color filter image filter without a crop rect can be
104 // expressed as a color filter.
105 SkAutoTUnref<SkImageFilter> gray(make_grayscale());
106 REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL));
107 }
108
109 {
110 // Check that a color filter image filter with a crop rect cannot
111 // be expressed as a color filter.
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +0000112#ifdef SK_CROP_RECT_IS_INT
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000113 SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100);
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +0000114#else
115 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(0, 0, 100, 100));
116#endif
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000117 SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect));
118 REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL));
119 }
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +0000120
121 {
122 // Tests pass by not asserting
123 SkBitmap bitmap, result;
124 make_small_bitmap(bitmap);
125 result.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize);
126 result.allocPixels();
127
128 {
129 // This tests for :
130 // 1 ) location at (0,0,1)
131 SkPoint3 location(0, 0, SK_Scalar1);
132 // 2 ) location and target at same value
133 SkPoint3 target(location.fX, location.fY, location.fZ);
134 // 3 ) large negative specular exponent value
135 SkScalar specularExponent = SkFloatToScalar(-1000);
skia.committer@gmail.coma604c4f2013-09-17 07:01:20 +0000136
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +0000137 SkPaint paint;
138 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(
139 location, target, specularExponent, SkFloatToScalar(180),
140 0xFFFFFFFF, SK_Scalar1, SK_Scalar1, SK_Scalar1,
141 new SkBitmapSource(bitmap)))->unref();
142 SkCanvas canvas(result);
skia.committer@gmail.coma604c4f2013-09-17 07:01:20 +0000143 SkRect r = SkRect::MakeWH(SkIntToScalar(kBitmapSize),
robertphillips@google.com15883542013-09-16 14:42:48 +0000144 SkIntToScalar(kBitmapSize));
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +0000145 canvas.drawRect(r, paint);
146 }
147
148 {
149 // This tests for scale bringing width to 0
robertphillips@google.com15883542013-09-16 14:42:48 +0000150 SkSize scale = SkSize::Make(SkFloatToScalar(-0.001f), SK_Scalar1);
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +0000151 SkAutoTUnref<SkBicubicImageFilter> bicubic(
152 SkBicubicImageFilter::CreateMitchell(
153 scale, new SkBitmapSource(bitmap)));
154 SkBitmapDevice device(bitmap);
155 SkDeviceImageFilterProxy proxy(&device);
156 SkIPoint loc = SkIPoint::Make(0, 0);
157 // An empty input should early return and return false
158 REPORTER_ASSERT(reporter,
159 !bicubic->filterImage(&proxy, bitmap, SkMatrix::I(), &result, &loc));
160 }
161 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000162 }
163};
164
165
166#include "TestClassDef.h"
167DEFINE_TESTCLASS("ImageFilterTest", ImageFilterTestClass, ImageFilterTest::Test)