1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "sk_image_filter_factory.h"
17 #include "include/effects/SkImageFilters.h"
18 #include "include/core/SkColorFilter.h"
19
20 namespace OHOS {
21 namespace Rosen {
22 static constexpr float GRAYSCALE_PARAONE = 0.2126f;
23 static constexpr float GRAYSCALE_PARATWO = 0.7152f;
24 static constexpr float GRAYSCALE_PARATHREE = 0.0722f;
25
Blur(float radius,SkTileMode tileMode)26 sk_sp<SkImageFilter> SKImageFilterFactory::Blur(float radius, SkTileMode tileMode)
27 {
28 return SkImageFilters::Blur(radius, radius, tileMode, nullptr);
29 }
30
Brightness(float degree)31 sk_sp<SkImageFilter> SKImageFilterFactory::Brightness(float degree)
32 {
33 float matrix[20] = {
34 1, 0, 0, 0, degree,
35 0, 1, 0, 0, degree,
36 0, 0, 1, 0, degree,
37 0, 0, 0, 1, 0
38 };
39
40 return SkImageFilters::ColorFilter(SkColorFilters::Matrix(matrix), nullptr);
41 }
42
Grayscale()43 sk_sp<SkImageFilter> SKImageFilterFactory::Grayscale()
44 {
45 float matrix[20] = {
46 GRAYSCALE_PARAONE, GRAYSCALE_PARATWO, GRAYSCALE_PARATHREE, 0, 0,
47 GRAYSCALE_PARAONE, GRAYSCALE_PARATWO, GRAYSCALE_PARATHREE, 0, 0,
48 GRAYSCALE_PARAONE, GRAYSCALE_PARATWO, GRAYSCALE_PARATHREE, 0, 0,
49 0, 0, 0, 1, 0
50 };
51
52 return SkImageFilters::ColorFilter(SkColorFilters::Matrix(matrix), nullptr);
53 }
54
Invert()55 sk_sp<SkImageFilter> SKImageFilterFactory::Invert()
56 {
57 /* invert matrix */
58 float matrix[20] = {
59 -1.0, 0, 0, 0, 1,
60 0, -1.0, 0, 0, 1,
61 0, 0, -1.0, 0, 1,
62 0, 0, 0, 1, 0
63 };
64
65 return SkImageFilters::ColorFilter(SkColorFilters::Matrix(matrix), nullptr);
66 }
67
ApplyColorMatrix(const PixelColorMatrix & matrix)68 sk_sp<SkImageFilter> SKImageFilterFactory::ApplyColorMatrix(const PixelColorMatrix &matrix)
69 {
70 return SkImageFilters::ColorFilter(SkColorFilters::Matrix(matrix.val), nullptr);
71 }
72 } // namespcae Rosen
73 } // namespace OHOS