2011-12-20 20:58:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 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"
|
|
|
|
#include "SkColorMatrixFilter.h"
|
2012-10-16 17:50:48 +00:00
|
|
|
#include "SkGradientShader.h"
|
2015-07-07 13:11:19 +00:00
|
|
|
#include "SkImage.h"
|
2011-12-20 20:58:18 +00:00
|
|
|
|
|
|
|
#define WIDTH 500
|
|
|
|
#define HEIGHT 500
|
|
|
|
|
2015-07-07 13:11:19 +00:00
|
|
|
static void set_color_matrix(SkPaint* paint, const SkColorMatrix& matrix) {
|
2014-02-21 18:46:30 +00:00
|
|
|
paint->setColorFilter(SkColorMatrixFilter::Create(matrix))->unref();
|
2012-06-04 19:07:41 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 13:11:19 +00:00
|
|
|
static void set_array(SkPaint* paint, const SkScalar array[]) {
|
2014-02-21 18:46:30 +00:00
|
|
|
paint->setColorFilter(SkColorMatrixFilter::Create(array))->unref();
|
2012-06-04 19:07:41 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 13:11:19 +00:00
|
|
|
class ColorMatrixGM : public skiagm::GM {
|
2011-12-20 20:58:18 +00:00
|
|
|
public:
|
|
|
|
ColorMatrixGM() {
|
2015-06-15 13:51:08 +00:00
|
|
|
this->setBGColor(sk_tool_utils::color_to_565(0xFF808080));
|
2011-12-20 20:58:18 +00:00
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-12-20 20:58:18 +00:00
|
|
|
protected:
|
2015-07-07 13:11:19 +00:00
|
|
|
SkString onShortName() override {
|
2011-12-20 20:58:18 +00:00
|
|
|
return SkString("colormatrix");
|
|
|
|
}
|
|
|
|
|
2015-07-07 13:11:19 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(WIDTH, HEIGHT);
|
2011-12-20 20:58:18 +00:00
|
|
|
}
|
2015-07-07 13:11:19 +00:00
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
fSolidImg.reset(CreateSolidBitmap(64, 64));
|
|
|
|
fTransparentImg.reset(CreateTransparentBitmap(64, 64));
|
|
|
|
}
|
2011-12-20 20:58:18 +00:00
|
|
|
|
2015-07-07 13:11:19 +00:00
|
|
|
static SkImage* CreateSolidBitmap(int width, int height) {
|
2011-12-20 20:58:18 +00:00
|
|
|
SkBitmap bm;
|
2014-01-25 16:46:20 +00:00
|
|
|
bm.allocN32Pixels(width, height);
|
2011-12-20 20:58:18 +00:00
|
|
|
SkCanvas canvas(bm);
|
2012-01-03 20:51:57 +00:00
|
|
|
canvas.clear(0x0);
|
2011-12-20 20:58:18 +00:00
|
|
|
for (int y = 0; y < height; ++y) {
|
|
|
|
for (int x = 0; x < width; ++x) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0));
|
2012-04-10 17:42:21 +00:00
|
|
|
canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(x),
|
|
|
|
SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint);
|
2011-12-20 20:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-07 13:11:19 +00:00
|
|
|
return SkImage::NewFromBitmap(bm);
|
2011-12-20 20:58:18 +00:00
|
|
|
}
|
2012-10-16 17:50:48 +00:00
|
|
|
|
|
|
|
// creates a bitmap with shades of transparent gray.
|
2015-07-07 13:11:19 +00:00
|
|
|
static SkImage* CreateTransparentBitmap(int width, int height) {
|
2012-10-16 17:50:48 +00:00
|
|
|
SkBitmap bm;
|
2014-01-25 16:46:20 +00:00
|
|
|
bm.allocN32Pixels(width, height);
|
2012-10-16 17:50:48 +00:00
|
|
|
SkCanvas canvas(bm);
|
|
|
|
canvas.clear(0x0);
|
|
|
|
|
|
|
|
SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}};
|
|
|
|
SkColor colors[] = {0x00000000, 0xFFFFFFFF};
|
|
|
|
SkPaint paint;
|
2016-03-09 17:50:50 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
|
|
|
|
SkShader::kClamp_TileMode));
|
2012-10-16 17:50:48 +00:00
|
|
|
canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), paint);
|
2015-07-07 13:11:19 +00:00
|
|
|
return SkImage::NewFromBitmap(bm);
|
2012-10-16 17:50:48 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 13:11:19 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2011-12-20 20:58:18 +00:00
|
|
|
SkPaint paint;
|
|
|
|
SkColorMatrix matrix;
|
|
|
|
|
2012-10-16 17:50:48 +00:00
|
|
|
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
|
2015-07-07 13:11:19 +00:00
|
|
|
const SkImage* bmps[] = { fSolidImg, fTransparentImg };
|
2012-10-16 17:50:48 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(bmps); ++i) {
|
|
|
|
matrix.setIdentity();
|
2015-07-07 13:11:19 +00:00
|
|
|
set_color_matrix(&paint, matrix);
|
|
|
|
canvas->drawImage(bmps[i], 0, 0, &paint);
|
2012-10-16 17:50:48 +00:00
|
|
|
|
|
|
|
matrix.setRotate(SkColorMatrix::kR_Axis, 90);
|
2015-07-07 13:11:19 +00:00
|
|
|
set_color_matrix(&paint, matrix);
|
|
|
|
canvas->drawImage(bmps[i], 80, 0, &paint);
|
2012-10-16 17:50:48 +00:00
|
|
|
|
|
|
|
matrix.setRotate(SkColorMatrix::kG_Axis, 90);
|
2015-07-07 13:11:19 +00:00
|
|
|
set_color_matrix(&paint, matrix);
|
|
|
|
canvas->drawImage(bmps[i], 160, 0, &paint);
|
2012-10-16 17:50:48 +00:00
|
|
|
|
|
|
|
matrix.setRotate(SkColorMatrix::kB_Axis, 90);
|
2015-07-07 13:11:19 +00:00
|
|
|
set_color_matrix(&paint, matrix);
|
|
|
|
canvas->drawImage(bmps[i], 240, 0, &paint);
|
|
|
|
///////////////////////////////////////////////
|
2013-11-25 19:44:07 +00:00
|
|
|
matrix.setSaturation(0.0f);
|
2015-07-07 13:11:19 +00:00
|
|
|
set_color_matrix(&paint, matrix);
|
|
|
|
canvas->drawImage(bmps[i], 0, 80, &paint);
|
2012-10-16 17:50:48 +00:00
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
matrix.setSaturation(0.5f);
|
2015-07-07 13:11:19 +00:00
|
|
|
set_color_matrix(&paint, matrix);
|
|
|
|
canvas->drawImage(bmps[i], 80, 80, &paint);
|
2012-10-16 17:50:48 +00:00
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
matrix.setSaturation(1.0f);
|
2015-07-07 13:11:19 +00:00
|
|
|
set_color_matrix(&paint, matrix);
|
|
|
|
canvas->drawImage(bmps[i], 160, 80, &paint);
|
2012-10-16 17:50:48 +00:00
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
matrix.setSaturation(2.0f);
|
2015-07-07 13:11:19 +00:00
|
|
|
set_color_matrix(&paint, matrix);
|
|
|
|
canvas->drawImage(bmps[i], 240, 80, &paint);
|
|
|
|
///////////////////////////////////////////////
|
2012-10-16 17:50:48 +00:00
|
|
|
matrix.setRGB2YUV();
|
2015-07-07 13:11:19 +00:00
|
|
|
set_color_matrix(&paint, matrix);
|
|
|
|
canvas->drawImage(bmps[i], 0, 160, &paint);
|
2012-10-16 17:50:48 +00:00
|
|
|
|
|
|
|
matrix.setYUV2RGB();
|
2015-07-07 13:11:19 +00:00
|
|
|
set_color_matrix(&paint, matrix);
|
|
|
|
canvas->drawImage(bmps[i], 80, 160, &paint);
|
2012-10-16 17:50:48 +00:00
|
|
|
|
|
|
|
SkScalar s1 = SK_Scalar1;
|
|
|
|
SkScalar s255 = SkIntToScalar(255);
|
|
|
|
// Move red into alpha, set color to white
|
|
|
|
SkScalar data[20] = {
|
|
|
|
0, 0, 0, 0, s255,
|
|
|
|
0, 0, 0, 0, s255,
|
|
|
|
0, 0, 0, 0, s255,
|
|
|
|
s1, 0, 0, 0, 0,
|
|
|
|
};
|
|
|
|
|
2015-07-07 13:11:19 +00:00
|
|
|
set_array(&paint, data);
|
|
|
|
canvas->drawImage(bmps[i], 160, 160, &paint);
|
|
|
|
///////////////////////////////////////////////
|
2012-10-16 17:50:48 +00:00
|
|
|
canvas->translate(0, 240);
|
|
|
|
}
|
2011-12-20 20:58:18 +00:00
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-12-20 20:58:18 +00:00
|
|
|
private:
|
2015-07-07 13:11:19 +00:00
|
|
|
SkAutoTUnref<SkImage> fSolidImg;
|
|
|
|
SkAutoTUnref<SkImage> fTransparentImg;
|
2011-12-20 20:58:18 +00:00
|
|
|
|
2015-07-07 13:11:19 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new ColorMatrixGM; )
|
2011-12-20 20:58:18 +00:00
|
|
|
|