| OLD | NEW |
| 1 | |
| 2 /* | 1 /* |
| 3 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 4 * | 3 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 7 */ | 6 */ |
| 8 | 7 |
| 9 #include "Test.h" | 8 #include "Test.h" |
| 9 #include "TestClassDef.h" |
| 10 #include "SkBicubicImageFilter.h" | 10 #include "SkBicubicImageFilter.h" |
| 11 #include "SkBitmap.h" | 11 #include "SkBitmap.h" |
| 12 #include "SkBitmapDevice.h" | 12 #include "SkBitmapDevice.h" |
| 13 #include "SkBitmapSource.h" | 13 #include "SkBitmapSource.h" |
| 14 #include "SkCanvas.h" | 14 #include "SkCanvas.h" |
| 15 #include "SkColorMatrixFilter.h" | 15 #include "SkColorMatrixFilter.h" |
| 16 #include "SkColorFilterImageFilter.h" | 16 #include "SkColorFilterImageFilter.h" |
| 17 #include "SkDeviceImageFilterProxy.h" | 17 #include "SkDeviceImageFilterProxy.h" |
| 18 #include "SkLightingImageFilter.h" | 18 #include "SkLightingImageFilter.h" |
| 19 #include "SkRect.h" | 19 #include "SkRect.h" |
| 20 | 20 |
| 21 class ImageFilterTest { | 21 static const int kBitmapSize = 4; |
| 22 public: | |
| 23 static const int kBitmapSize = 4; | |
| 24 | 22 |
| 25 static void make_small_bitmap(SkBitmap& bitmap) { | 23 static void make_small_bitmap(SkBitmap& bitmap) { |
| 26 bitmap.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize); | 24 bitmap.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize); |
| 27 bitmap.allocPixels(); | 25 bitmap.allocPixels(); |
| 28 SkBitmapDevice device(bitmap); | 26 SkBitmapDevice device(bitmap); |
| 29 SkCanvas canvas(&device); | 27 SkCanvas canvas(&device); |
| 30 canvas.clear(0x00000000); | 28 canvas.clear(0x00000000); |
| 31 SkPaint darkPaint; | 29 SkPaint darkPaint; |
| 32 darkPaint.setColor(0xFF804020); | 30 darkPaint.setColor(0xFF804020); |
| 33 SkPaint lightPaint; | 31 SkPaint lightPaint; |
| 34 lightPaint.setColor(0xFF244484); | 32 lightPaint.setColor(0xFF244484); |
| 35 const int i = kBitmapSize / 4; | 33 const int i = kBitmapSize / 4; |
| 36 for (int y = 0; y < kBitmapSize; y += i) { | 34 for (int y = 0; y < kBitmapSize; y += i) { |
| 37 for (int x = 0; x < kBitmapSize; x += i) { | 35 for (int x = 0; x < kBitmapSize; x += i) { |
| 38 canvas.save(); | 36 canvas.save(); |
| 39 canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); | 37 canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 40 canvas.drawRect(SkRect::MakeXYWH(0, 0, | 38 canvas.drawRect(SkRect::MakeXYWH(0, 0, |
| 41 SkIntToScalar(i), | 39 SkIntToScalar(i), |
| 42 SkIntToScalar(i)), darkPaint); | 40 SkIntToScalar(i)), darkPaint); |
| 43 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), | 41 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 44 0, | 42 0, |
| 45 SkIntToScalar(i), | 43 SkIntToScalar(i), |
| 46 SkIntToScalar(i)), lightPaint); | 44 SkIntToScalar(i)), lightPaint); |
| 47 canvas.drawRect(SkRect::MakeXYWH(0, | 45 canvas.drawRect(SkRect::MakeXYWH(0, |
| 48 SkIntToScalar(i), | 46 SkIntToScalar(i), |
| 49 SkIntToScalar(i), | 47 SkIntToScalar(i), |
| 50 SkIntToScalar(i)), lightPaint); | 48 SkIntToScalar(i)), lightPaint); |
| 51 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), | 49 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 52 SkIntToScalar(i), | 50 SkIntToScalar(i), |
| 53 SkIntToScalar(i), | 51 SkIntToScalar(i), |
| 54 SkIntToScalar(i)), darkPaint); | 52 SkIntToScalar(i)), darkPaint); |
| 55 canvas.restore(); | 53 canvas.restore(); |
| 56 } | |
| 57 } | 54 } |
| 58 } | 55 } |
| 56 } |
| 59 | 57 |
| 60 static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL)
{ | 58 static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) { |
| 61 SkScalar s = amount; | 59 SkScalar s = amount; |
| 62 SkScalar matrix[20] = { s, 0, 0, 0, 0, | 60 SkScalar matrix[20] = { s, 0, 0, 0, 0, |
| 63 0, s, 0, 0, 0, | 61 0, s, 0, 0, 0, |
| 64 0, 0, s, 0, 0, | 62 0, 0, s, 0, 0, |
| 65 0, 0, 0, s, 0 }; | 63 0, 0, 0, s, 0 }; |
| 66 SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix)); | 64 SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix)); |
| 67 return SkColorFilterImageFilter::Create(filter, input); | 65 return SkColorFilterImageFilter::Create(filter, input); |
| 66 } |
| 67 |
| 68 static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageF
ilter::CropRect* cropRect = NULL) { |
| 69 SkScalar matrix[20]; |
| 70 memset(matrix, 0, 20 * sizeof(SkScalar)); |
| 71 matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 72 matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 73 matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 74 matrix[18] = 1.0f; |
| 75 SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix)); |
| 76 return SkColorFilterImageFilter::Create(filter, input, cropRect); |
| 77 } |
| 78 |
| 79 DEF_TEST(ImageFilter, reporter) { |
| 80 { |
| 81 // Check that two non-clipping color matrices concatenate into a single
filter. |
| 82 SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f)); |
| 83 SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfBrigh
tness)); |
| 84 REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0)); |
| 68 } | 85 } |
| 69 | 86 |
| 70 static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkIm
ageFilter::CropRect* cropRect = NULL) { | 87 { |
| 71 SkScalar matrix[20]; | 88 // Check that a clipping color matrix followed by a grayscale does not c
oncatenate into a single filter. |
| 72 memset(matrix, 0, 20 * sizeof(SkScalar)); | 89 SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f)); |
| 73 matrix[0] = matrix[5] = matrix[10] = 0.2126f; | 90 SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBright
ness)); |
| 74 matrix[1] = matrix[6] = matrix[11] = 0.7152f; | 91 REPORTER_ASSERT(reporter, NULL != halfBrightness->getInput(0)); |
| 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 } | 92 } |
| 80 | 93 |
| 81 static SkImageFilter* make_mode_blue(SkImageFilter* input = NULL) { | 94 { |
| 82 SkAutoTUnref<SkColorFilter> filter( | 95 // Check that a color filter image filter without a crop rect can be |
| 83 SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mod
e)); | 96 // expressed as a color filter. |
| 84 return SkColorFilterImageFilter::Create(filter, input); | 97 SkAutoTUnref<SkImageFilter> gray(make_grayscale()); |
| 98 REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL)); |
| 85 } | 99 } |
| 86 | 100 |
| 87 static void Test(skiatest::Reporter* reporter) { | 101 { |
| 102 // Check that a color filter image filter with a crop rect cannot |
| 103 // be expressed as a color filter. |
| 104 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(0, 0, 100, 100)); |
| 105 SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect)
); |
| 106 REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL)); |
| 107 } |
| 108 |
| 109 { |
| 110 // Tests pass by not asserting |
| 111 SkBitmap bitmap, result; |
| 112 make_small_bitmap(bitmap); |
| 113 result.setConfig(SkBitmap::kARGB_8888_Config, kBitmapSize, kBitmapSize); |
| 114 result.allocPixels(); |
| 115 |
| 88 { | 116 { |
| 89 // Check that two non-clipping color matrices concatenate into a sin
gle filter. | 117 // This tests for : |
| 90 SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f)); | 118 // 1 ) location at (0,0,1) |
| 91 SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfB
rightness)); | 119 SkPoint3 location(0, 0, SK_Scalar1); |
| 92 REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0)); | 120 // 2 ) location and target at same value |
| 121 SkPoint3 target(location.fX, location.fY, location.fZ); |
| 122 // 3 ) large negative specular exponent value |
| 123 SkScalar specularExponent = -1000; |
| 124 |
| 125 SkAutoTUnref<SkImageFilter> bmSrc(new SkBitmapSource(bitmap)); |
| 126 SkPaint paint; |
| 127 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular( |
| 128 location, target, specularExponent, 180, |
| 129 0xFFFFFFFF, SK_Scalar1, SK_Scalar1, SK_Scalar1, |
| 130 bmSrc))->unref(); |
| 131 SkCanvas canvas(result); |
| 132 SkRect r = SkRect::MakeWH(SkIntToScalar(kBitmapSize), |
| 133 SkIntToScalar(kBitmapSize)); |
| 134 canvas.drawRect(r, paint); |
| 93 } | 135 } |
| 94 | 136 |
| 95 { | 137 { |
| 96 // Check that a clipping color matrix followed by a grayscale does n
ot concatenate into a single filter. | 138 // This tests for scale bringing width to 0 |
| 97 SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f)); | 139 SkSize scale = SkSize::Make(-0.001f, SK_Scalar1); |
| 98 SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBr
ightness)); | 140 SkAutoTUnref<SkImageFilter> bmSrc(new SkBitmapSource(bitmap)); |
| 99 REPORTER_ASSERT(reporter, NULL != halfBrightness->getInput(0)); | 141 SkAutoTUnref<SkBicubicImageFilter> bicubic( |
| 100 } | 142 SkBicubicImageFilter::CreateMitchell(scale, bmSrc)); |
| 101 | 143 SkBitmapDevice device(bitmap); |
| 102 { | 144 SkDeviceImageFilterProxy proxy(&device); |
| 103 // Check that a color filter image filter without a crop rect can be | 145 SkIPoint loc = SkIPoint::Make(0, 0); |
| 104 // expressed as a color filter. | 146 // An empty input should early return and return false |
| 105 SkAutoTUnref<SkImageFilter> gray(make_grayscale()); | 147 REPORTER_ASSERT(reporter, |
| 106 REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL)); | 148 !bicubic->filterImage(&proxy, bitmap, SkMatrix::I(),
&result, &loc)); |
| 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, &cropR
ect)); | |
| 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, kBitmapSi
ze); | |
| 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::CreateSpotLitSpecula
r( | |
| 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 } | 149 } |
| 159 } | 150 } |
| 160 }; | 151 } |
| 161 | |
| 162 | |
| 163 #include "TestClassDef.h" | |
| 164 DEFINE_TESTCLASS("ImageFilterTest", ImageFilterTestClass, ImageFilterTest::Test) | |
| OLD | NEW |