2015-06-05 16:41:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 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 "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "tests/Test.h"
|
2015-06-05 16:41:18 +00:00
|
|
|
|
2018-04-17 18:14:51 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
2015-06-05 16:41:18 +00:00
|
|
|
|
|
|
|
static inline void assert_color(skiatest::Reporter* reporter,
|
|
|
|
SkColor expected, SkColor actual, int tolerance) {
|
|
|
|
REPORTER_ASSERT(reporter, abs((int)(SkColorGetA(expected) - SkColorGetA(actual))) <= tolerance);
|
|
|
|
REPORTER_ASSERT(reporter, abs((int)(SkColorGetR(expected) - SkColorGetR(actual))) <= tolerance);
|
|
|
|
REPORTER_ASSERT(reporter, abs((int)(SkColorGetG(expected) - SkColorGetG(actual))) <= tolerance);
|
|
|
|
REPORTER_ASSERT(reporter, abs((int)(SkColorGetB(expected) - SkColorGetB(actual))) <= tolerance);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void assert_color(skiatest::Reporter* reporter, SkColor expected, SkColor actual) {
|
|
|
|
const int TOLERANCE = 1;
|
|
|
|
assert_color(reporter, expected, actual, TOLERANCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This test case is a mirror of the Android CTS tests for MatrixColorFilter
|
|
|
|
* found in the android.graphics.ColorMatrixColorFilterTest class.
|
|
|
|
*/
|
|
|
|
static inline void test_colorMatrixCTS(skiatest::Reporter* reporter) {
|
|
|
|
|
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocN32Pixels(1,1);
|
|
|
|
|
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
SkPaint paint;
|
|
|
|
|
2019-04-30 16:18:54 +00:00
|
|
|
float blueToCyan[20] = {
|
2015-06-05 16:41:18 +00:00
|
|
|
1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f, 0.0f };
|
2019-04-30 16:18:54 +00:00
|
|
|
paint.setColorFilter(SkColorFilters::Matrix(blueToCyan));
|
2015-06-05 16:41:18 +00:00
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
canvas.drawPoint(0, 0, paint);
|
|
|
|
assert_color(reporter, SK_ColorCYAN, bitmap.getColor(0, 0));
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
canvas.drawPoint(0, 0, paint);
|
|
|
|
assert_color(reporter, SK_ColorGREEN, bitmap.getColor(0, 0));
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas.drawPoint(0, 0, paint);
|
|
|
|
assert_color(reporter, SK_ColorRED, bitmap.getColor(0, 0));
|
|
|
|
|
|
|
|
// color components are clipped, not scaled
|
|
|
|
paint.setColor(SK_ColorMAGENTA);
|
|
|
|
canvas.drawPoint(0, 0, paint);
|
|
|
|
assert_color(reporter, SK_ColorWHITE, bitmap.getColor(0, 0));
|
|
|
|
|
2019-04-30 16:18:54 +00:00
|
|
|
float transparentRedAddBlue[20] = {
|
2015-06-05 16:41:18 +00:00
|
|
|
1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
|
2019-04-30 16:18:54 +00:00
|
|
|
0.0f, 0.0f, 1.0f, 0.0f, 64.0f/255,
|
2015-06-05 16:41:18 +00:00
|
|
|
-0.5f, 0.0f, 0.0f, 1.0f, 0.0f
|
|
|
|
};
|
2019-04-30 16:18:54 +00:00
|
|
|
paint.setColorFilter(SkColorFilters::Matrix(transparentRedAddBlue));
|
2015-06-05 16:41:18 +00:00
|
|
|
bitmap.eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas.drawPoint(0, 0, paint);
|
|
|
|
assert_color(reporter, SkColorSetARGB(128, 255, 0, 64), bitmap.getColor(0, 0), 2);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorCYAN);
|
|
|
|
canvas.drawPoint(0, 0, paint);
|
|
|
|
// blue gets clipped
|
|
|
|
assert_color(reporter, SK_ColorCYAN, bitmap.getColor(0, 0));
|
|
|
|
|
|
|
|
// change array to filter out green
|
|
|
|
REPORTER_ASSERT(reporter, 1.0f == transparentRedAddBlue[6]);
|
|
|
|
transparentRedAddBlue[6] = 0.0f;
|
|
|
|
|
|
|
|
// check that changing the array has no effect
|
|
|
|
canvas.drawPoint(0, 0, paint);
|
|
|
|
assert_color(reporter, SK_ColorCYAN, bitmap.getColor(0, 0));
|
|
|
|
|
|
|
|
// create a new filter with the changed matrix
|
2019-04-30 16:18:54 +00:00
|
|
|
paint.setColorFilter(SkColorFilters::Matrix(transparentRedAddBlue));
|
2015-06-05 16:41:18 +00:00
|
|
|
canvas.drawPoint(0, 0, paint);
|
|
|
|
assert_color(reporter, SK_ColorBLUE, bitmap.getColor(0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(ColorMatrix, reporter) {
|
|
|
|
test_colorMatrixCTS(reporter);
|
|
|
|
}
|
2017-11-29 21:31:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
DEF_TEST(ColorMatrix_clamp_while_unpremul, r) {
|
|
|
|
// This matrix does green += 255/255 and alpha += 32/255. We want to test
|
|
|
|
// that if we pass it opaque alpha and small red and blue values, red and
|
|
|
|
// blue stay unchanged, not pumped up by that ~1.12 intermediate alpha.
|
2019-04-30 16:18:54 +00:00
|
|
|
float m[] = {
|
2017-11-29 21:31:37 +00:00
|
|
|
1, 0, 0, 0, 0,
|
2019-04-30 16:18:54 +00:00
|
|
|
0, 1, 0, 0, 1,
|
2017-11-29 21:31:37 +00:00
|
|
|
0, 0, 1, 0, 0,
|
2019-04-30 16:18:54 +00:00
|
|
|
0, 0, 0, 1, 32.0f/255,
|
2017-11-29 21:31:37 +00:00
|
|
|
};
|
2019-04-30 16:18:54 +00:00
|
|
|
auto filter = SkColorFilters::Matrix(m);
|
2017-11-29 21:31:37 +00:00
|
|
|
|
|
|
|
SkColor filtered = filter->filterColor(0xff0a0b0c);
|
|
|
|
REPORTER_ASSERT(r, SkColorGetA(filtered) == 0xff);
|
|
|
|
REPORTER_ASSERT(r, SkColorGetR(filtered) == 0x0a);
|
|
|
|
REPORTER_ASSERT(r, SkColorGetG(filtered) == 0xff);
|
|
|
|
REPORTER_ASSERT(r, SkColorGetB(filtered) == 0x0c);
|
|
|
|
}
|