OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrColorSpaceXform.h" | 8 #include "GrColorSpaceXform.h" |
9 #include "SkColorSpace.h" | 9 #include "SkColorSpace.h" |
| 10 #include "SkMatrix44.h" |
10 | 11 |
11 static inline bool sk_float_almost_equals(float x, float y, float tol) { | 12 static inline bool sk_float_almost_equals(float x, float y, float tol) { |
12 return sk_float_abs(x - y) <= tol; | 13 return sk_float_abs(x - y) <= tol; |
13 } | 14 } |
14 | 15 |
15 static inline bool matrix_is_almost_identity(const SkMatrix44& m, | 16 static inline bool matrix_is_almost_identity(const SkMatrix44& m, |
16 SkMScalar tol = SK_MScalar1 / (1 <<
12)) { | 17 SkMScalar tol = SK_MScalar1 / (1 <<
12)) { |
17 return | 18 return |
18 sk_float_almost_equals(m.getFloat(0, 0), 1.0f, tol) && | 19 sk_float_almost_equals(m.getFloat(0, 0), 1.0f, tol) && |
19 sk_float_almost_equals(m.getFloat(0, 1), 0.0f, tol) && | 20 sk_float_almost_equals(m.getFloat(0, 1), 0.0f, tol) && |
20 sk_float_almost_equals(m.getFloat(0, 2), 0.0f, tol) && | 21 sk_float_almost_equals(m.getFloat(0, 2), 0.0f, tol) && |
21 sk_float_almost_equals(m.getFloat(0, 3), 0.0f, tol) && | 22 sk_float_almost_equals(m.getFloat(0, 3), 0.0f, tol) && |
22 sk_float_almost_equals(m.getFloat(1, 0), 0.0f, tol) && | 23 sk_float_almost_equals(m.getFloat(1, 0), 0.0f, tol) && |
23 sk_float_almost_equals(m.getFloat(1, 1), 1.0f, tol) && | 24 sk_float_almost_equals(m.getFloat(1, 1), 1.0f, tol) && |
24 sk_float_almost_equals(m.getFloat(1, 2), 0.0f, tol) && | 25 sk_float_almost_equals(m.getFloat(1, 2), 0.0f, tol) && |
25 sk_float_almost_equals(m.getFloat(1, 3), 0.0f, tol) && | 26 sk_float_almost_equals(m.getFloat(1, 3), 0.0f, tol) && |
26 sk_float_almost_equals(m.getFloat(2, 0), 0.0f, tol) && | 27 sk_float_almost_equals(m.getFloat(2, 0), 0.0f, tol) && |
27 sk_float_almost_equals(m.getFloat(2, 1), 0.0f, tol) && | 28 sk_float_almost_equals(m.getFloat(2, 1), 0.0f, tol) && |
28 sk_float_almost_equals(m.getFloat(2, 2), 1.0f, tol) && | 29 sk_float_almost_equals(m.getFloat(2, 2), 1.0f, tol) && |
29 sk_float_almost_equals(m.getFloat(2, 3), 0.0f, tol) && | 30 sk_float_almost_equals(m.getFloat(2, 3), 0.0f, tol) && |
30 sk_float_almost_equals(m.getFloat(3, 0), 0.0f, tol) && | 31 sk_float_almost_equals(m.getFloat(3, 0), 0.0f, tol) && |
31 sk_float_almost_equals(m.getFloat(3, 1), 0.0f, tol) && | 32 sk_float_almost_equals(m.getFloat(3, 1), 0.0f, tol) && |
32 sk_float_almost_equals(m.getFloat(3, 2), 0.0f, tol) && | 33 sk_float_almost_equals(m.getFloat(3, 2), 0.0f, tol) && |
33 sk_float_almost_equals(m.getFloat(3, 3), 1.0f, tol); | 34 sk_float_almost_equals(m.getFloat(3, 3), 1.0f, tol); |
34 } | 35 } |
35 | 36 |
| 37 GrColorSpaceXform::GrColorSpaceXform(const SkMatrix44& srcToDst) { |
| 38 srcToDst.asColMajorf(fSrcToDst); |
| 39 } |
| 40 |
36 sk_sp<GrColorSpaceXform> GrColorSpaceXform::Make(SkColorSpace* src, SkColorSpace
* dst) { | 41 sk_sp<GrColorSpaceXform> GrColorSpaceXform::Make(SkColorSpace* src, SkColorSpace
* dst) { |
37 if (!src || !dst) { | 42 if (!src || !dst) { |
38 // Invalid | 43 // Invalid |
39 return nullptr; | 44 return nullptr; |
40 } | 45 } |
41 | 46 |
42 if (src == dst) { | 47 if (src == dst) { |
43 // Quick equality check - no conversion needed in this case | 48 // Quick equality check - no conversion needed in this case |
44 return nullptr; | 49 return nullptr; |
45 } | 50 } |
46 | 51 |
47 SkMatrix44 srcToDst(SkMatrix44::kUninitialized_Constructor); | 52 SkMatrix44 srcToDst(SkMatrix44::kUninitialized_Constructor); |
48 if (!dst->xyz().invert(&srcToDst)) { | 53 if (!dst->xyz().invert(&srcToDst)) { |
49 return nullptr; | 54 return nullptr; |
50 } | 55 } |
51 srcToDst.postConcat(src->xyz()); | 56 srcToDst.postConcat(src->xyz()); |
52 | 57 |
53 if (matrix_is_almost_identity(srcToDst)) { | 58 if (matrix_is_almost_identity(srcToDst)) { |
54 return nullptr; | 59 return nullptr; |
55 } | 60 } |
56 | 61 |
57 return sk_make_sp<GrColorSpaceXform>(srcToDst); | 62 return sk_make_sp<GrColorSpaceXform>(srcToDst); |
58 } | 63 } |
OLD | NEW |