2012-09-18 20:32:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2012-10-04 17:18:20 +00:00
|
|
|
#include "SkColor.h"
|
|
|
|
#include "SkGradientShader.h"
|
2016-12-27 16:04:12 +00:00
|
|
|
#include "SkMatrixConvolutionImageFilter.h"
|
|
|
|
#include "SkPixelRef.h"
|
2012-09-18 20:32:34 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class MatrixConvolutionGM : public GM {
|
|
|
|
public:
|
2016-12-27 16:04:12 +00:00
|
|
|
MatrixConvolutionGM(SkColor colorOne, SkColor colorTwo, const char* nameSuffix)
|
|
|
|
: fNameSuffix(nameSuffix) {
|
2012-09-18 20:32:34 +00:00
|
|
|
this->setBGColor(0x00000000);
|
2016-12-27 16:04:12 +00:00
|
|
|
fColors[0] = colorOne;
|
|
|
|
fColors[1] = colorTwo;
|
2012-09-18 20:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
SkString onShortName() override {
|
2016-12-27 16:04:12 +00:00
|
|
|
return SkStringPrintf("matrixconvolution%s", fNameSuffix);
|
2012-09-18 20:32:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
void makeBitmap() {
|
2016-12-27 16:04:12 +00:00
|
|
|
// Draw our bitmap in N32, so legacy devices get "premul" values they understand
|
|
|
|
SkBitmap n32Bitmap;
|
|
|
|
n32Bitmap.allocN32Pixels(80, 80);
|
|
|
|
SkCanvas canvas(n32Bitmap);
|
2012-09-18 20:32:34 +00:00
|
|
|
canvas.clear(0x00000000);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2012-09-18 20:32:34 +00:00
|
|
|
paint.setColor(0xFFFFFFFF);
|
|
|
|
paint.setTextSize(SkIntToScalar(180));
|
2012-10-04 17:18:20 +00:00
|
|
|
SkPoint pts[2] = { SkPoint::Make(0, 0),
|
|
|
|
SkPoint::Make(0, SkIntToScalar(80)) };
|
|
|
|
SkScalar pos[2] = { 0, SkIntToScalar(80) };
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(
|
2016-12-27 16:04:12 +00:00
|
|
|
pts, fColors, pos, 2, SkShader::kClamp_TileMode));
|
2012-09-18 20:32:34 +00:00
|
|
|
const char* str = "e";
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas.drawString(str, SkIntToScalar(-10), SkIntToScalar(80), paint);
|
2016-12-27 16:04:12 +00:00
|
|
|
|
|
|
|
// ... tag the data as sRGB, so color-aware devices do gamut adjustment, etc...
|
|
|
|
fBitmap.setInfo(SkImageInfo::MakeS32(80, 80, kPremul_SkAlphaType));
|
|
|
|
fBitmap.setPixelRef(sk_ref_sp(n32Bitmap.pixelRef()), 0, 0);
|
2012-09-18 20:32:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(500, 300);
|
2012-09-18 20:32:34 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 16:36:08 +00:00
|
|
|
void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset,
|
2013-10-18 20:08:14 +00:00
|
|
|
SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha,
|
2015-08-27 14:41:13 +00:00
|
|
|
const SkImageFilter::CropRect* cropRect = nullptr) {
|
2012-09-18 20:32:34 +00:00
|
|
|
SkScalar kernel[9] = {
|
|
|
|
SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
|
|
|
|
SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
|
|
|
|
SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
|
|
|
|
};
|
|
|
|
SkISize kernelSize = SkISize::Make(3, 3);
|
2013-11-25 19:44:07 +00:00
|
|
|
SkScalar gain = 0.3f, bias = SkIntToScalar(100);
|
2016-12-27 16:04:12 +00:00
|
|
|
if (canvas->imageInfo().colorSpace()) {
|
|
|
|
// TODO: Gain and bias are poorly specified (in the feConvolveMatrix SVG documentation,
|
|
|
|
// there is obviously no mention of gamma or color spaces). Eventually, we need to
|
|
|
|
// decide what to do with these (they generally have an extreme brightening effect).
|
|
|
|
// For now, I'm modifying this GM to use values tuned to preserve luminance across the
|
|
|
|
// range of input values (compared to the legacy math and values).
|
|
|
|
//
|
|
|
|
// It's impossible to match the results exactly, because legacy math produces a flat
|
|
|
|
// response (when looking at sRGB encoded results), while gamma-correct math produces
|
|
|
|
// a curve.
|
|
|
|
gain = 0.25f;
|
|
|
|
bias = 16.5f;
|
|
|
|
}
|
2012-09-18 20:32:34 +00:00
|
|
|
SkPaint paint;
|
2016-04-08 15:01:20 +00:00
|
|
|
paint.setImageFilter(SkMatrixConvolutionImageFilter::Make(kernelSize,
|
|
|
|
kernel,
|
|
|
|
gain,
|
|
|
|
bias,
|
|
|
|
kernelOffset,
|
|
|
|
tileMode,
|
|
|
|
convolveAlpha,
|
|
|
|
nullptr,
|
|
|
|
cropRect));
|
2013-01-14 16:27:50 +00:00
|
|
|
canvas->save();
|
2013-10-18 20:08:14 +00:00
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()),
|
|
|
|
SkIntToScalar(fBitmap.height())));
|
|
|
|
canvas->drawBitmap(fBitmap, 0, 0, &paint);
|
2013-01-14 16:27:50 +00:00
|
|
|
canvas->restore();
|
2012-09-18 20:32:34 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 20:08:14 +00:00
|
|
|
typedef SkMatrixConvolutionImageFilter MCIF;
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
this->makeBitmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2015-04-09 18:13:24 +00:00
|
|
|
canvas->clear(SK_ColorBLACK);
|
2014-03-12 16:36:08 +00:00
|
|
|
SkIPoint kernelOffset = SkIPoint::Make(1, 0);
|
2015-12-07 15:48:34 +00:00
|
|
|
SkImageFilter::CropRect rect(SkRect::Make(fBitmap.bounds()));
|
2012-10-04 17:18:20 +00:00
|
|
|
for (int x = 10; x < 310; x += 100) {
|
2015-12-07 15:48:34 +00:00
|
|
|
this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true, &rect);
|
|
|
|
this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &rect);
|
|
|
|
this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &rect);
|
2014-03-12 16:36:08 +00:00
|
|
|
kernelOffset.fY++;
|
2012-09-18 20:32:34 +00:00
|
|
|
}
|
2014-03-12 16:36:08 +00:00
|
|
|
kernelOffset.fY = 1;
|
2015-12-07 15:48:34 +00:00
|
|
|
SkImageFilter::CropRect smallRect(SkRect::MakeXYWH(10, 5, 60, 60));
|
|
|
|
this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &smallRect);
|
|
|
|
this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &smallRect);
|
|
|
|
this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &smallRect);
|
|
|
|
|
|
|
|
this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false, &rect);
|
|
|
|
this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode, false, &rect);
|
|
|
|
this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false, &rect);
|
2012-09-18 20:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkBitmap fBitmap;
|
2016-12-27 16:04:12 +00:00
|
|
|
SkColor fColors[2];
|
|
|
|
const char* fNameSuffix;
|
2015-09-03 20:32:33 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
2012-09-18 20:32:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-12-27 16:04:12 +00:00
|
|
|
DEF_GM(return new MatrixConvolutionGM(0xFFFFFFFF, 0x40404040, "");)
|
|
|
|
DEF_GM(return new MatrixConvolutionGM(0xFFFF0000, 0xFF00FF00, "_color");)
|
2012-09-18 20:32:34 +00:00
|
|
|
|
|
|
|
}
|