senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 8 | #include "SkBicubicImageFilter.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkBitmapDevice.h" |
| 11 | #include "SkBitmapSource.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 12 | #include "SkBlurImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "SkCanvas.h" |
| 14 | #include "SkColorFilterImageFilter.h" |
| 15 | #include "SkColorMatrixFilter.h" |
| 16 | #include "SkDeviceImageFilterProxy.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 17 | #include "SkDisplacementMapEffect.h" |
| 18 | #include "SkDropShadowImageFilter.h" |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 19 | #include "SkLightingImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 20 | #include "SkMatrixConvolutionImageFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 21 | #include "SkMergeImageFilter.h" |
| 22 | #include "SkMorphologyImageFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 23 | #include "SkOffsetImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 24 | #include "SkRect.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 25 | #include "SkTileImageFilter.h" |
| 26 | #include "SkXfermodeImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 27 | #include "Test.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 28 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame^] | 29 | #if SK_SUPPORT_GPU |
| 30 | #include "GrContextFactory.h" |
| 31 | #include "SkGpuDevice.h" |
| 32 | #endif |
| 33 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 34 | static const int kBitmapSize = 4; |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 35 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 36 | static void make_small_bitmap(SkBitmap& bitmap) { |
| 37 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize); |
| 38 | bitmap.allocPixels(); |
| 39 | SkBitmapDevice device(bitmap); |
| 40 | SkCanvas canvas(&device); |
| 41 | canvas.clear(0x00000000); |
| 42 | SkPaint darkPaint; |
| 43 | darkPaint.setColor(0xFF804020); |
| 44 | SkPaint lightPaint; |
| 45 | lightPaint.setColor(0xFF244484); |
| 46 | const int i = kBitmapSize / 4; |
| 47 | for (int y = 0; y < kBitmapSize; y += i) { |
| 48 | for (int x = 0; x < kBitmapSize; x += i) { |
| 49 | canvas.save(); |
| 50 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 51 | canvas.drawRect(SkRect::MakeXYWH(0, 0, |
| 52 | SkIntToScalar(i), |
| 53 | SkIntToScalar(i)), darkPaint); |
| 54 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 55 | 0, |
| 56 | SkIntToScalar(i), |
| 57 | SkIntToScalar(i)), lightPaint); |
| 58 | canvas.drawRect(SkRect::MakeXYWH(0, |
| 59 | SkIntToScalar(i), |
| 60 | SkIntToScalar(i), |
| 61 | SkIntToScalar(i)), lightPaint); |
| 62 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 63 | SkIntToScalar(i), |
| 64 | SkIntToScalar(i), |
| 65 | SkIntToScalar(i)), darkPaint); |
| 66 | canvas.restore(); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 69 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 70 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 71 | static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) { |
| 72 | SkScalar s = amount; |
| 73 | SkScalar matrix[20] = { s, 0, 0, 0, 0, |
| 74 | 0, s, 0, 0, 0, |
| 75 | 0, 0, s, 0, 0, |
| 76 | 0, 0, 0, s, 0 }; |
| 77 | SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix)); |
| 78 | return SkColorFilterImageFilter::Create(filter, input); |
| 79 | } |
| 80 | |
| 81 | static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageFilter::CropRect* cropRect = NULL) { |
| 82 | SkScalar matrix[20]; |
| 83 | memset(matrix, 0, 20 * sizeof(SkScalar)); |
| 84 | matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 85 | matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 86 | matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 87 | matrix[18] = 1.0f; |
| 88 | SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix)); |
| 89 | return SkColorFilterImageFilter::Create(filter, input, cropRect); |
| 90 | } |
| 91 | |
| 92 | DEF_TEST(ImageFilter, reporter) { |
| 93 | { |
| 94 | // Check that two non-clipping color matrices concatenate into a single filter. |
| 95 | SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f)); |
| 96 | SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfBrightness)); |
| 97 | REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 98 | } |
| 99 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 100 | { |
| 101 | // Check that a clipping color matrix followed by a grayscale does not concatenate into a single filter. |
| 102 | SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f)); |
| 103 | SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBrightness)); |
| 104 | REPORTER_ASSERT(reporter, NULL != halfBrightness->getInput(0)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 105 | } |
| 106 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 107 | { |
| 108 | // Check that a color filter image filter without a crop rect can be |
| 109 | // expressed as a color filter. |
| 110 | SkAutoTUnref<SkImageFilter> gray(make_grayscale()); |
| 111 | REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 112 | } |
| 113 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 114 | { |
| 115 | // Check that a color filter image filter with a crop rect cannot |
| 116 | // be expressed as a color filter. |
| 117 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(0, 0, 100, 100)); |
| 118 | SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect)); |
| 119 | REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL)); |
| 120 | } |
| 121 | |
| 122 | { |
| 123 | // Tests pass by not asserting |
| 124 | SkBitmap bitmap, result; |
| 125 | make_small_bitmap(bitmap); |
| 126 | result.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize); |
| 127 | result.allocPixels(); |
| 128 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 129 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 130 | // This tests for : |
| 131 | // 1 ) location at (0,0,1) |
| 132 | SkPoint3 location(0, 0, SK_Scalar1); |
| 133 | // 2 ) location and target at same value |
| 134 | SkPoint3 target(location.fX, location.fY, location.fZ); |
| 135 | // 3 ) large negative specular exponent value |
| 136 | SkScalar specularExponent = -1000; |
| 137 | |
| 138 | SkAutoTUnref<SkImageFilter> bmSrc(new SkBitmapSource(bitmap)); |
| 139 | SkPaint paint; |
| 140 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular( |
| 141 | location, target, specularExponent, 180, |
| 142 | 0xFFFFFFFF, SK_Scalar1, SK_Scalar1, SK_Scalar1, |
| 143 | bmSrc))->unref(); |
| 144 | SkCanvas canvas(result); |
| 145 | SkRect r = SkRect::MakeWH(SkIntToScalar(kBitmapSize), |
| 146 | SkIntToScalar(kBitmapSize)); |
| 147 | canvas.drawRect(r, paint); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 148 | } |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 149 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 150 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 151 | // This tests for scale bringing width to 0 |
| 152 | SkSize scale = SkSize::Make(-0.001f, SK_Scalar1); |
| 153 | SkAutoTUnref<SkImageFilter> bmSrc(new SkBitmapSource(bitmap)); |
| 154 | SkAutoTUnref<SkBicubicImageFilter> bicubic( |
| 155 | SkBicubicImageFilter::CreateMitchell(scale, bmSrc)); |
| 156 | SkBitmapDevice device(bitmap); |
| 157 | SkDeviceImageFilterProxy proxy(&device); |
| 158 | SkIPoint loc = SkIPoint::Make(0, 0); |
| 159 | // An empty input should early return and return false |
| 160 | REPORTER_ASSERT(reporter, |
| 161 | !bicubic->filterImage(&proxy, bitmap, SkMatrix::I(), &result, &loc)); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 162 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 163 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame^] | 164 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 165 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame^] | 166 | static void test_crop_rects(SkBaseDevice* device, skiatest::Reporter* reporter) { |
| 167 | // Check that all filters offset to their absolute crop rect, |
| 168 | // unaffected by the input crop rect. |
| 169 | // Tests pass by not asserting. |
| 170 | SkBitmap bitmap; |
| 171 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 172 | bitmap.allocPixels(); |
| 173 | bitmap.eraseARGB(0, 0, 0, 0); |
| 174 | SkDeviceImageFilterProxy proxy(device); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 175 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame^] | 176 | SkImageFilter::CropRect inputCropRect(SkRect::MakeXYWH(8, 13, 80, 80)); |
| 177 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 30, 60, 60)); |
| 178 | SkAutoTUnref<SkImageFilter> input(make_grayscale(NULL, &inputCropRect)); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 179 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame^] | 180 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, SkXfermode::kSrcIn_Mode)); |
| 181 | SkPoint3 location(0, 0, SK_Scalar1); |
| 182 | SkPoint3 target(SK_Scalar1, SK_Scalar1, SK_Scalar1); |
| 183 | SkScalar kernel[9] = { |
| 184 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 185 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 186 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 187 | }; |
| 188 | SkISize kernelSize = SkISize::Make(3, 3); |
| 189 | SkScalar gain = SK_Scalar1, bias = 0; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 190 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame^] | 191 | SkImageFilter* filters[] = { |
| 192 | SkColorFilterImageFilter::Create(cf.get(), input.get(), &cropRect), |
| 193 | new SkDisplacementMapEffect(SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 194 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 195 | 40.0f, input.get(), input.get(), &cropRect), |
| 196 | new SkBlurImageFilter(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 197 | new SkDropShadowImageFilter(SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_ColorGREEN, input.get(), &cropRect), |
| 198 | SkLightingImageFilter::CreatePointLitDiffuse(location, SK_ColorGREEN, 0, 0, input.get(), &cropRect), |
| 199 | SkLightingImageFilter::CreatePointLitSpecular(location, SK_ColorGREEN, 0, 0, 0, input.get(), &cropRect), |
| 200 | new SkMatrixConvolutionImageFilter(kernelSize, kernel, gain, bias, SkIPoint::Make(1, 1), SkMatrixConvolutionImageFilter::kRepeat_TileMode, false, input.get(), &cropRect), |
| 201 | new SkMergeImageFilter(input.get(), input.get(), SkXfermode::kSrcOver_Mode, &cropRect), |
| 202 | new SkOffsetImageFilter(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 203 | new SkOffsetImageFilter(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 204 | new SkDilateImageFilter(3, 2, input.get(), &cropRect), |
| 205 | new SkErodeImageFilter(2, 3, input.get(), &cropRect), |
| 206 | new SkTileImageFilter(inputCropRect.rect(), cropRect.rect(), input.get()), |
| 207 | new SkXfermodeImageFilter(SkXfermode::Create(SkXfermode::kSrcOver_Mode), input.get(), input.get(), &cropRect), |
| 208 | }; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 209 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame^] | 210 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 211 | SkImageFilter* filter = filters[i]; |
| 212 | SkBitmap result; |
| 213 | SkIPoint offset; |
| 214 | SkString str; |
| 215 | str.printf("filter %ld", i); |
| 216 | REPORTER_ASSERT_MESSAGE(reporter, filter->filterImage(&proxy, bitmap, SkMatrix::I(), &result, &offset), str.c_str()); |
| 217 | REPORTER_ASSERT_MESSAGE(reporter, offset.fX == 20 && offset.fY == 30, str.c_str()); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 218 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame^] | 219 | |
| 220 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 221 | SkSafeUnref(filters[i]); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | DEF_TEST(ImageFilterCropRect, reporter) { |
| 226 | SkBitmap temp; |
| 227 | temp.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 228 | temp.allocPixels(); |
| 229 | SkBitmapDevice device(temp); |
| 230 | test_crop_rects(&device, reporter); |
| 231 | } |
| 232 | |
| 233 | DEF_GPUTEST(ImageFilterCropRectGPU, reporter, factory) { |
| 234 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
| 235 | SkGpuDevice device(context, SkBitmap::kARGB_8888_Config, 100, 100); |
| 236 | test_crop_rects(&device, reporter); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 237 | } |