• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkBicubicImageFilter.h"
11 #include "SkBitmap.h"
12 #include "SkBitmapDevice.h"
13 #include "SkBitmapSource.h"
14 #include "SkCanvas.h"
15 #include "SkColorMatrixFilter.h"
16 #include "SkColorFilterImageFilter.h"
17 #include "SkDeviceImageFilterProxy.h"
18 #include "SkLightingImageFilter.h"
19 #include "SkRect.h"
20 
21 class ImageFilterTest {
22 public:
23     static const int kBitmapSize = 4;
24 
make_small_bitmap(SkBitmap & bitmap)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,
41                                                  SkIntToScalar(i),
42                                                  SkIntToScalar(i)), darkPaint);
43                 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i),
44                                                  0,
45                                                  SkIntToScalar(i),
46                                                  SkIntToScalar(i)), lightPaint);
47                 canvas.drawRect(SkRect::MakeXYWH(0,
48                                                  SkIntToScalar(i),
49                                                  SkIntToScalar(i),
50                                                  SkIntToScalar(i)), lightPaint);
51                 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i),
52                                                  SkIntToScalar(i),
53                                                  SkIntToScalar(i),
54                                                  SkIntToScalar(i)), darkPaint);
55                 canvas.restore();
56             }
57         }
58     }
59 
make_scale(float amount,SkImageFilter * input=NULL)60     static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) {
61         SkScalar s = 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 
make_grayscale(SkImageFilter * input=NULL,const SkImageFilter::CropRect * cropRect=NULL)70     static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageFilter::CropRect* cropRect = NULL) {
71         SkScalar matrix[20];
72         memset(matrix, 0, 20 * sizeof(SkScalar));
73         matrix[0] = matrix[5] = matrix[10] = 0.2126f;
74         matrix[1] = matrix[6] = matrix[11] = 0.7152f;
75         matrix[2] = matrix[7] = matrix[12] = 0.0722f;
76         matrix[18] = 1.0f;
77         SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
78         return SkColorFilterImageFilter::Create(filter, input, cropRect);
79     }
80 
make_mode_blue(SkImageFilter * input=NULL)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 
Test(skiatest::Reporter * reporter)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         }
94 
95         {
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.
112             SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(0, 0, 100, 100));
113             SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect));
114             REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL));
115         }
116 
117         {
118             // Tests pass by not asserting
119             SkBitmap bitmap, result;
120             make_small_bitmap(bitmap);
121             result.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize);
122             result.allocPixels();
123 
124             {
125                 // This tests for :
126                 // 1 ) location at (0,0,1)
127                 SkPoint3 location(0, 0, SK_Scalar1);
128                 // 2 ) location and target at same value
129                 SkPoint3 target(location.fX, location.fY, location.fZ);
130                 // 3 ) large negative specular exponent value
131                 SkScalar specularExponent = -1000;
132 
133                 SkAutoTUnref<SkImageFilter> bmSrc(new SkBitmapSource(bitmap));
134                 SkPaint paint;
135                 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(
136                         location, target, specularExponent, 180,
137                         0xFFFFFFFF, SK_Scalar1, SK_Scalar1, SK_Scalar1,
138                         bmSrc))->unref();
139                 SkCanvas canvas(result);
140                 SkRect r = SkRect::MakeWH(SkIntToScalar(kBitmapSize),
141                                           SkIntToScalar(kBitmapSize));
142                 canvas.drawRect(r, paint);
143             }
144 
145             {
146                 // This tests for scale bringing width to 0
147                 SkSize scale = SkSize::Make(-0.001f, SK_Scalar1);
148                 SkAutoTUnref<SkImageFilter> bmSrc(new SkBitmapSource(bitmap));
149                 SkAutoTUnref<SkBicubicImageFilter> bicubic(
150                     SkBicubicImageFilter::CreateMitchell(scale, bmSrc));
151                 SkBitmapDevice device(bitmap);
152                 SkDeviceImageFilterProxy proxy(&device);
153                 SkIPoint loc = SkIPoint::Make(0, 0);
154                 // An empty input should early return and return false
155                 REPORTER_ASSERT(reporter,
156                     !bicubic->filterImage(&proxy, bitmap, SkMatrix::I(), &result, &loc));
157             }
158         }
159     }
160 };
161 
162 
163 #include "TestClassDef.h"
164 DEFINE_TESTCLASS("ImageFilterTest", ImageFilterTestClass, ImageFilterTest::Test)
165