senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 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" |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 10 | #include "SkBicubicImageFilter.h" |
| 11 | #include "SkBitmap.h" |
| 12 | #include "SkBitmapDevice.h" |
| 13 | #include "SkBitmapSource.h" |
| 14 | #include "SkCanvas.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 15 | #include "SkColorMatrixFilter.h" |
| 16 | #include "SkColorFilterImageFilter.h" |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 17 | #include "SkDeviceImageFilterProxy.h" |
| 18 | #include "SkLightingImageFilter.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 19 | #include "SkRect.h" |
| 20 | |
| 21 | class ImageFilterTest { |
| 22 | public: |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 23 | 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.com | a604c4f | 2013-09-17 07:01:20 +0000 | [diff] [blame] | 40 | canvas.drawRect(SkRect::MakeXYWH(0, 0, |
| 41 | SkIntToScalar(i), |
robertphillips@google.com | 1588354 | 2013-09-16 14:42:48 +0000 | [diff] [blame] | 42 | SkIntToScalar(i)), darkPaint); |
skia.committer@gmail.com | a604c4f | 2013-09-17 07:01:20 +0000 | [diff] [blame] | 43 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 44 | 0, |
| 45 | SkIntToScalar(i), |
robertphillips@google.com | 1588354 | 2013-09-16 14:42:48 +0000 | [diff] [blame] | 46 | SkIntToScalar(i)), lightPaint); |
skia.committer@gmail.com | a604c4f | 2013-09-17 07:01:20 +0000 | [diff] [blame] | 47 | canvas.drawRect(SkRect::MakeXYWH(0, |
| 48 | SkIntToScalar(i), |
| 49 | SkIntToScalar(i), |
robertphillips@google.com | 1588354 | 2013-09-16 14:42:48 +0000 | [diff] [blame] | 50 | SkIntToScalar(i)), lightPaint); |
skia.committer@gmail.com | a604c4f | 2013-09-17 07:01:20 +0000 | [diff] [blame] | 51 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 52 | SkIntToScalar(i), |
| 53 | SkIntToScalar(i), |
robertphillips@google.com | 1588354 | 2013-09-16 14:42:48 +0000 | [diff] [blame] | 54 | SkIntToScalar(i)), darkPaint); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 55 | canvas.restore(); |
| 56 | } |
| 57 | } |
| 58 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 59 | |
| 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.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame^] | 70 | static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageFilter::CropRect* cropRect = NULL) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 71 | 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.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 94 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +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)); |
| 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.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame^] | 112 | #ifdef SK_CROP_RECT_IS_INT |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 113 | SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100); |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame^] | 114 | #else |
| 115 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(0, 0, 100, 100)); |
| 116 | #endif |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 117 | SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect)); |
| 118 | REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL)); |
| 119 | } |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 120 | |
| 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.com | a604c4f | 2013-09-17 07:01:20 +0000 | [diff] [blame] | 136 | |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 137 | 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.com | a604c4f | 2013-09-17 07:01:20 +0000 | [diff] [blame] | 143 | SkRect r = SkRect::MakeWH(SkIntToScalar(kBitmapSize), |
robertphillips@google.com | 1588354 | 2013-09-16 14:42:48 +0000 | [diff] [blame] | 144 | SkIntToScalar(kBitmapSize)); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 145 | canvas.drawRect(r, paint); |
| 146 | } |
| 147 | |
| 148 | { |
| 149 | // This tests for scale bringing width to 0 |
robertphillips@google.com | 1588354 | 2013-09-16 14:42:48 +0000 | [diff] [blame] | 150 | SkSize scale = SkSize::Make(SkFloatToScalar(-0.001f), SK_Scalar1); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 151 | 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.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 162 | } |
| 163 | }; |
| 164 | |
| 165 | |
| 166 | #include "TestClassDef.h" |
| 167 | DEFINE_TESTCLASS("ImageFilterTest", ImageFilterTestClass, ImageFilterTest::Test) |