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 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 29 | static const int kBitmapSize = 4; |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 30 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 31 | static void make_small_bitmap(SkBitmap& bitmap) { |
| 32 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize); |
| 33 | bitmap.allocPixels(); |
| 34 | SkBitmapDevice device(bitmap); |
| 35 | SkCanvas canvas(&device); |
| 36 | canvas.clear(0x00000000); |
| 37 | SkPaint darkPaint; |
| 38 | darkPaint.setColor(0xFF804020); |
| 39 | SkPaint lightPaint; |
| 40 | lightPaint.setColor(0xFF244484); |
| 41 | const int i = kBitmapSize / 4; |
| 42 | for (int y = 0; y < kBitmapSize; y += i) { |
| 43 | for (int x = 0; x < kBitmapSize; x += i) { |
| 44 | canvas.save(); |
| 45 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 46 | canvas.drawRect(SkRect::MakeXYWH(0, 0, |
| 47 | SkIntToScalar(i), |
| 48 | SkIntToScalar(i)), darkPaint); |
| 49 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 50 | 0, |
| 51 | SkIntToScalar(i), |
| 52 | SkIntToScalar(i)), lightPaint); |
| 53 | canvas.drawRect(SkRect::MakeXYWH(0, |
| 54 | SkIntToScalar(i), |
| 55 | SkIntToScalar(i), |
| 56 | SkIntToScalar(i)), lightPaint); |
| 57 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 58 | SkIntToScalar(i), |
| 59 | SkIntToScalar(i), |
| 60 | SkIntToScalar(i)), darkPaint); |
| 61 | canvas.restore(); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 64 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 65 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 66 | static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) { |
| 67 | SkScalar s = amount; |
| 68 | SkScalar matrix[20] = { s, 0, 0, 0, 0, |
| 69 | 0, s, 0, 0, 0, |
| 70 | 0, 0, s, 0, 0, |
| 71 | 0, 0, 0, s, 0 }; |
| 72 | SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix)); |
| 73 | return SkColorFilterImageFilter::Create(filter, input); |
| 74 | } |
| 75 | |
| 76 | static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageFilter::CropRect* cropRect = NULL) { |
| 77 | SkScalar matrix[20]; |
| 78 | memset(matrix, 0, 20 * sizeof(SkScalar)); |
| 79 | matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 80 | matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 81 | matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 82 | matrix[18] = 1.0f; |
| 83 | SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix)); |
| 84 | return SkColorFilterImageFilter::Create(filter, input, cropRect); |
| 85 | } |
| 86 | |
| 87 | DEF_TEST(ImageFilter, 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)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 93 | } |
| 94 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 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)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 100 | } |
| 101 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 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)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 107 | } |
| 108 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 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 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 124 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 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); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 143 | } |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 144 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 145 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 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)); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 157 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 158 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 159 | |
| 160 | { |
| 161 | // Check that all filters offset to their absolute crop rect, |
| 162 | // unaffected by the input crop rect. |
| 163 | // Tests pass by not asserting. |
| 164 | SkBitmap bitmap, temp; |
| 165 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 166 | temp.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 167 | bitmap.allocPixels(); |
| 168 | temp.allocPixels(); |
| 169 | bitmap.eraseARGB(0, 0, 0, 0); |
| 170 | SkBitmapDevice device(temp); |
| 171 | SkDeviceImageFilterProxy proxy(&device); |
| 172 | |
| 173 | SkImageFilter::CropRect inputCropRect(SkRect::MakeXYWH(8, 13, 80, 80)); |
| 174 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 30, 60, 60)); |
| 175 | SkAutoTUnref<SkImageFilter> input(make_grayscale(NULL, &inputCropRect)); |
| 176 | |
| 177 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, SkXfermode::kSrcIn_Mode)); |
| 178 | SkPoint3 location(0, 0, SK_Scalar1); |
| 179 | SkPoint3 target(SK_Scalar1, SK_Scalar1, SK_Scalar1); |
| 180 | SkScalar kernel[9] = { |
| 181 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 182 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 183 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 184 | }; |
| 185 | SkISize kernelSize = SkISize::Make(3, 3); |
| 186 | SkScalar gain = SK_Scalar1, bias = 0; |
| 187 | |
| 188 | SkImageFilter* filters[] = { |
| 189 | SkColorFilterImageFilter::Create(cf.get(), input.get(), &cropRect), |
| 190 | new SkDisplacementMapEffect(SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 191 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 192 | 40.0f, input.get(), input.get(), &cropRect), |
| 193 | new SkBlurImageFilter(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 194 | new SkDropShadowImageFilter(SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_ColorGREEN, input.get(), &cropRect), |
| 195 | SkLightingImageFilter::CreatePointLitDiffuse(location, SK_ColorGREEN, 0, 0, input.get(), &cropRect), |
| 196 | SkLightingImageFilter::CreatePointLitSpecular(location, SK_ColorGREEN, 0, 0, 0, input.get(), &cropRect), |
| 197 | new SkMatrixConvolutionImageFilter(kernelSize, kernel, gain, bias, SkIPoint::Make(1, 1), SkMatrixConvolutionImageFilter::kRepeat_TileMode, false, input.get(), &cropRect), |
| 198 | new SkMergeImageFilter(input.get(), input.get(), SkXfermode::kSrcOver_Mode, &cropRect), |
| 199 | new SkOffsetImageFilter(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 200 | new SkOffsetImageFilter(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 201 | new SkDilateImageFilter(3, 2, input.get(), &cropRect), |
| 202 | new SkErodeImageFilter(2, 3, input.get(), &cropRect), |
| 203 | new SkTileImageFilter(inputCropRect.rect(), cropRect.rect(), input.get()), |
| 204 | new SkXfermodeImageFilter(SkXfermode::Create(SkXfermode::kSrcOver_Mode), input.get(), input.get(), &cropRect), |
| 205 | }; |
| 206 | |
| 207 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 208 | SkImageFilter* filter = filters[i]; |
| 209 | SkBitmap result; |
| 210 | SkIPoint offset; |
| 211 | REPORTER_ASSERT(reporter, filter->filterImage(&proxy, bitmap, SkMatrix::I(), &result, &offset)); |
| 212 | REPORTER_ASSERT(reporter, offset.fX == 20 && offset.fY == 30); |
| 213 | } |
| 214 | |
| 215 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 216 | SkSafeUnref(filters[i]); |
| 217 | } |
| 218 | } |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 219 | } |