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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/effects/SkColorMatrix.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.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) {
|
2019-04-22 02:17:03 +00:00
|
|
|
paint->setColorFilter(SkColorFilters::Matrix(matrix));
|
2012-06-04 19:07:41 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 16:18:54 +00:00
|
|
|
static void set_array(SkPaint* paint, const float array[]) {
|
|
|
|
paint->setColorFilter(SkColorFilters::Matrix(array));
|
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() {
|
2018-08-16 14:17:03 +00:00
|
|
|
this->setBGColor(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
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-07-07 13:11:19 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2016-03-17 17:51:11 +00:00
|
|
|
fSolidImg = CreateSolidBitmap(64, 64);
|
|
|
|
fTransparentImg = CreateTransparentBitmap(64, 64);
|
2015-07-07 13:11:19 +00:00
|
|
|
}
|
2011-12-20 20:58:18 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<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
|
|
|
}
|
|
|
|
}
|
2016-03-17 17:51:11 +00:00
|
|
|
return SkImage::MakeFromBitmap(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.
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<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,
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp));
|
2012-10-16 17:50:48 +00:00
|
|
|
canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), paint);
|
2016-03-17 17:51:11 +00:00
|
|
|
return SkImage::MakeFromBitmap(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;
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
2016-03-17 17:51:11 +00:00
|
|
|
const SkImage* bmps[] = { fSolidImg.get(), fTransparentImg.get() };
|
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
|
|
|
|
|
|
|
// Move red into alpha, set color to white
|
2019-04-30 16:18:54 +00:00
|
|
|
float data[20] = {
|
|
|
|
0, 0, 0, 0, 1,
|
|
|
|
0, 0, 0, 0, 1,
|
|
|
|
0, 0, 0, 0, 1,
|
|
|
|
1, 0, 0, 0, 0,
|
2012-10-16 17:50:48 +00:00
|
|
|
};
|
|
|
|
|
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:
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fSolidImg;
|
|
|
|
sk_sp<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; )
|