2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
11 #include "SkGradientShader.h"
12 #include "SkMatrixConvolutionImageFilter.h"
13 #include "SkPixelRef.h"
17 class MatrixConvolutionGM : public GM {
19 MatrixConvolutionGM(SkColor colorOne, SkColor colorTwo, const char* nameSuffix)
20 : fNameSuffix(nameSuffix) {
21 this->setBGColor(0x00000000);
22 fColors[0] = colorOne;
23 fColors[1] = colorTwo;
28 SkString onShortName() override {
29 return SkStringPrintf("matrixconvolution%s", fNameSuffix);
33 // Draw our bitmap in N32, so legacy devices get "premul" values they understand
35 n32Bitmap.allocN32Pixels(80, 80);
36 SkCanvas canvas(n32Bitmap);
37 canvas.clear(0x00000000);
39 paint.setAntiAlias(true);
40 sk_tool_utils::set_portable_typeface(&paint);
41 paint.setColor(0xFFFFFFFF);
42 paint.setTextSize(SkIntToScalar(180));
43 SkPoint pts[2] = { SkPoint::Make(0, 0),
44 SkPoint::Make(0, SkIntToScalar(80)) };
45 SkScalar pos[2] = { 0, SkIntToScalar(80) };
46 paint.setShader(SkGradientShader::MakeLinear(
47 pts, fColors, pos, 2, SkShader::kClamp_TileMode));
48 const char* str = "e";
49 canvas.drawString(str, SkIntToScalar(-10), SkIntToScalar(80), paint);
51 // ... tag the data as sRGB, so color-aware devices do gamut adjustment, etc...
52 fBitmap.setInfo(SkImageInfo::MakeS32(80, 80, kPremul_SkAlphaType));
53 fBitmap.setPixelRef(sk_ref_sp(n32Bitmap.pixelRef()), 0, 0);
56 SkISize onISize() override {
57 return SkISize::Make(500, 300);
60 void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset,
61 SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha,
62 const SkImageFilter::CropRect* cropRect = nullptr) {
63 SkScalar kernel[9] = {
64 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
65 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
66 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
68 SkISize kernelSize = SkISize::Make(3, 3);
69 SkScalar gain = 0.3f, bias = SkIntToScalar(100);
70 if (canvas->imageInfo().colorSpace()) {
71 // TODO: Gain and bias are poorly specified (in the feConvolveMatrix SVG documentation,
72 // there is obviously no mention of gamma or color spaces). Eventually, we need to
73 // decide what to do with these (they generally have an extreme brightening effect).
74 // For now, I'm modifying this GM to use values tuned to preserve luminance across the
75 // range of input values (compared to the legacy math and values).
77 // It's impossible to match the results exactly, because legacy math produces a flat
78 // response (when looking at sRGB encoded results), while gamma-correct math produces
84 paint.setImageFilter(SkMatrixConvolutionImageFilter::Make(kernelSize,
94 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
95 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()),
96 SkIntToScalar(fBitmap.height())));
97 canvas->drawBitmap(fBitmap, 0, 0, &paint);
101 typedef SkMatrixConvolutionImageFilter MCIF;
103 void onOnceBeforeDraw() override {
107 void onDraw(SkCanvas* canvas) override {
108 canvas->clear(SK_ColorBLACK);
109 SkIPoint kernelOffset = SkIPoint::Make(1, 0);
110 SkImageFilter::CropRect rect(SkRect::Make(fBitmap.bounds()));
111 for (int x = 10; x < 310; x += 100) {
112 this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true, &rect);
113 this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &rect);
114 this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &rect);
118 SkImageFilter::CropRect smallRect(SkRect::MakeXYWH(10, 5, 60, 60));
119 this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &smallRect);
120 this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &smallRect);
121 this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &smallRect);
123 this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false, &rect);
124 this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode, false, &rect);
125 this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false, &rect);
131 const char* fNameSuffix;
133 typedef GM INHERITED;
136 //////////////////////////////////////////////////////////////////////////////
138 DEF_GM(return new MatrixConvolutionGM(0xFFFFFFFF, 0x40404040, "");)
139 DEF_GM(return new MatrixConvolutionGM(0xFFFF0000, 0xFF00FF00, "_color");)