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"
|
2012-10-04 17:18:20 +00:00
|
|
|
#include "SkColor.h"
|
2012-09-18 20:32:34 +00:00
|
|
|
#include "SkMatrixConvolutionImageFilter.h"
|
2012-10-04 17:18:20 +00:00
|
|
|
#include "SkGradientShader.h"
|
2012-09-18 20:32:34 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class MatrixConvolutionGM : public GM {
|
|
|
|
public:
|
|
|
|
MatrixConvolutionGM() : fInitialized(false) {
|
|
|
|
this->setBGColor(0x00000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2012-09-18 20:32:34 +00:00
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("matrixconvolution");
|
|
|
|
}
|
|
|
|
|
|
|
|
void make_bitmap() {
|
2014-01-25 16:46:20 +00:00
|
|
|
fBitmap.allocN32Pixels(80, 80);
|
2014-02-13 17:14:46 +00:00
|
|
|
SkCanvas canvas(fBitmap);
|
2012-09-18 20:32:34 +00:00
|
|
|
canvas.clear(0x00000000);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2014-07-31 12:58:44 +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)) };
|
|
|
|
SkColor colors[2] = { 0xFFFFFFFF, 0x40404040 };
|
|
|
|
SkScalar pos[2] = { 0, SkIntToScalar(80) };
|
|
|
|
paint.setShader(SkGradientShader::CreateLinear(
|
|
|
|
pts, colors, pos, 2, SkShader::kClamp_TileMode))->unref();
|
2012-09-18 20:32:34 +00:00
|
|
|
const char* str = "e";
|
|
|
|
canvas.drawText(str, strlen(str), SkIntToScalar(-10), SkIntToScalar(80), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() {
|
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,
|
|
|
|
const SkImageFilter::CropRect* cropRect = NULL) {
|
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);
|
2012-09-18 20:32:34 +00:00
|
|
|
SkPaint paint;
|
2013-10-18 20:08:14 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> filter(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkMatrixConvolutionImageFilter::Create(kernelSize,
|
|
|
|
kernel,
|
|
|
|
gain,
|
|
|
|
bias,
|
2014-03-12 16:36:08 +00:00
|
|
|
kernelOffset,
|
2014-03-10 10:51:58 +00:00
|
|
|
tileMode,
|
|
|
|
convolveAlpha,
|
|
|
|
NULL,
|
|
|
|
cropRect));
|
2012-09-18 20:32:34 +00:00
|
|
|
paint.setImageFilter(filter);
|
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;
|
|
|
|
|
2012-09-18 20:32:34 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
if (!fInitialized) {
|
|
|
|
make_bitmap();
|
|
|
|
fInitialized = true;
|
|
|
|
}
|
|
|
|
canvas->clear(0x00000000);
|
2014-03-12 16:36:08 +00:00
|
|
|
SkIPoint kernelOffset = SkIPoint::Make(1, 0);
|
2012-10-04 17:18:20 +00:00
|
|
|
for (int x = 10; x < 310; x += 100) {
|
2014-03-12 16:36:08 +00:00
|
|
|
this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true);
|
|
|
|
this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true);
|
|
|
|
this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, true);
|
|
|
|
kernelOffset.fY++;
|
2012-09-18 20:32:34 +00:00
|
|
|
}
|
2014-03-12 16:36:08 +00:00
|
|
|
kernelOffset.fY = 1;
|
2013-10-20 20:21:19 +00:00
|
|
|
SkImageFilter::CropRect rect(SkRect::MakeXYWH(10, 5, 60, 60));
|
2014-03-12 16:36:08 +00:00
|
|
|
this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &rect);
|
|
|
|
this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &rect);
|
|
|
|
this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &rect);
|
2013-10-18 20:08:14 +00:00
|
|
|
|
2014-03-12 16:36:08 +00:00
|
|
|
this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false);
|
|
|
|
this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode, false);
|
|
|
|
this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false);
|
2012-09-18 20:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
bool fInitialized;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new MatrixConvolutionGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|