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 "SkColorPriv.h" | 8 #include "SkColorPriv.h" |
9 #include "SkNx.h" | 9 #include "SkNx.h" |
10 | 10 |
11 static inline float get_alpha(const Sk4f& f4) { | 11 static inline float get_alpha(const Sk4f& f4) { |
12 return f4.kth<SkPM4f::A>(); | 12 return f4.kth<SkPM4f::A>(); |
13 } | 13 } |
14 | 14 |
15 static inline Sk4f set_alpha(const Sk4f& f4, float alpha) { | 15 static inline Sk4f set_alpha(const Sk4f& f4, float alpha) { |
16 static_assert(3 == SkPM4f::A, ""); | 16 static_assert(3 == SkPM4f::A, ""); |
17 return Sk4f(f4.kth<0>(), f4.kth<1>(), f4.kth<2>(), alpha); | 17 return Sk4f(f4.kth<0>(), f4.kth<1>(), f4.kth<2>(), alpha); |
18 } | 18 } |
19 | 19 |
20 static inline uint32_t to_4b(const Sk4f& f4) { | 20 static inline uint32_t to_4b(const Sk4f& f4) { |
21 uint32_t b4; | 21 uint32_t b4; |
22 SkNx_cast<uint8_t>(f4).store((uint8_t*)&b4); | 22 SkNx_cast<uint8_t>(f4).store((uint8_t*)&b4); |
23 return b4; | 23 return b4; |
24 } | 24 } |
25 | 25 |
26 static inline Sk4f to_4f(uint32_t b4) { | 26 static inline Sk4f to_4f(uint32_t b4) { |
27 return SkNx_cast<float>(Sk4b::Load((const uint8_t*)&b4)); | 27 return SkNx_cast<float>(Sk4b::Load((const uint8_t*)&b4)); |
28 } | 28 } |
29 | 29 |
30 static inline Sk4f s2l(const Sk4f& s4) { | 30 static inline Sk4f srgb_to_linear(const Sk4f& s4) { |
31 return set_alpha(s4 * s4, get_alpha(s4)); | 31 return set_alpha(s4 * s4, get_alpha(s4)); |
32 } | 32 } |
33 | 33 |
34 static inline Sk4f l2s(const Sk4f& l4) { | 34 static inline Sk4f linear_to_srgb(const Sk4f& l4) { |
35 return set_alpha(l4.rsqrt1() * l4, get_alpha(l4)); | 35 return set_alpha(l4.sqrt(), get_alpha(l4)); |
36 } | 36 } |
37 | 37 |
38 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 38 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
39 | 39 |
40 static inline Sk4f Sk4f_fromL32(uint32_t src) { | 40 static inline Sk4f Sk4f_fromL32(uint32_t src) { |
41 return to_4f(src) * Sk4f(1.0f/255); | 41 return to_4f(src) * Sk4f(1.0f/255); |
42 } | 42 } |
43 | 43 |
44 static inline Sk4f Sk4f_fromS32(uint32_t src) { | 44 static inline Sk4f Sk4f_fromS32(uint32_t src) { |
45 return s2l(to_4f(src) * Sk4f(1.0f/255)); | 45 return srgb_to_linear(to_4f(src) * Sk4f(1.0f/255)); |
46 } | 46 } |
47 | 47 |
48 static inline uint32_t Sk4f_toL32(const Sk4f& x4) { | 48 static inline uint32_t Sk4f_toL32(const Sk4f& x4) { |
49 return to_4b(x4 * Sk4f(255) + Sk4f(0.5f)); | 49 return to_4b(x4 * Sk4f(255) + Sk4f(0.5f)); |
50 } | 50 } |
51 | 51 |
52 static inline uint32_t Sk4f_toS32(const Sk4f& x4) { | 52 static inline uint32_t Sk4f_toS32(const Sk4f& x4) { |
53 return to_4b(l2s(x4) * Sk4f(255) + Sk4f(0.5f)); | 53 return to_4b(linear_to_srgb(x4) * Sk4f(255) + Sk4f(0.5f)); |
54 } | 54 } |
55 | |
56 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
57 | |
58 static Sk4f unit_to_l255_round(const SkPM4f& pm4) { | |
59 return Sk4f::Load(pm4.fVec) * Sk4f(255) + Sk4f(0.5f); | |
60 } | |
61 | |
62 static Sk4f unit_to_s255_round(const SkPM4f& pm4) { | |
63 return l2s(Sk4f::Load(pm4.fVec)) * Sk4f(255) + Sk4f(0.5f); | |
64 } | |
65 | |
66 static inline void SkPM4f_l32_src_mode(SkPMColor dst[], const SkPM4f src[], int
count) { | |
67 for (int i = 0; i < (count >> 2); ++i) { | |
68 SkASSERT(src[0].isUnit()); | |
69 SkASSERT(src[1].isUnit()); | |
70 SkASSERT(src[2].isUnit()); | |
71 SkASSERT(src[3].isUnit()); | |
72 Sk4f_ToBytes((uint8_t*)dst, | |
73 unit_to_l255_round(src[0]), unit_to_l255_round(src[1]), | |
74 unit_to_l255_round(src[2]), unit_to_l255_round(src[3])); | |
75 src += 4; | |
76 dst += 4; | |
77 } | |
78 count &= 3; | |
79 for (int i = 0; i < count; ++i) { | |
80 SkASSERT(src[i].isUnit()); | |
81 SkNx_cast<uint8_t>(unit_to_l255_round(src[i])).store((uint8_t*)&dst[i]); | |
82 } | |
83 } | |
84 | |
85 static inline void SkPM4f_l32_srcover_mode(SkPMColor dst[], const SkPM4f src[],
int count) { | |
86 for (int i = 0; i < count; ++i) { | |
87 SkASSERT(src[i].isUnit()); | |
88 Sk4f s4 = Sk4f::Load(src[i].fVec); | |
89 Sk4f d4 = Sk4f_fromL32(dst[i]); | |
90 dst[i] = Sk4f_toL32(s4 + d4 * Sk4f(1 - get_alpha(s4))); | |
91 } | |
92 } | |
93 | |
94 static inline void SkPM4f_s32_src_mode(SkPMColor dst[], const SkPM4f src[], int
count) { | |
95 for (int i = 0; i < (count >> 2); ++i) { | |
96 SkASSERT(src[0].isUnit()); | |
97 SkASSERT(src[1].isUnit()); | |
98 SkASSERT(src[2].isUnit()); | |
99 SkASSERT(src[3].isUnit()); | |
100 Sk4f_ToBytes((uint8_t*)dst, | |
101 unit_to_s255_round(src[0]), unit_to_s255_round(src[1]), | |
102 unit_to_s255_round(src[2]), unit_to_s255_round(src[3])); | |
103 src += 4; | |
104 dst += 4; | |
105 } | |
106 count &= 3; | |
107 for (int i = 0; i < count; ++i) { | |
108 SkASSERT(src[i].isUnit()); | |
109 SkNx_cast<uint8_t>(unit_to_s255_round(src[i])).store((uint8_t*)&dst[i]); | |
110 } | |
111 } | |
112 | |
113 static inline void SkPM4f_s32_srcover_mode(SkPMColor dst[], const SkPM4f src[],
int count) { | |
114 for (int i = 0; i < count; ++i) { | |
115 SkASSERT(src[i].isUnit()); | |
116 Sk4f s4 = Sk4f::Load(src[i].fVec); | |
117 Sk4f d4 = Sk4f_fromS32(dst[i]); | |
118 dst[i] = Sk4f_toS32(s4 + d4 * Sk4f(1 - get_alpha(s4))); | |
119 } | |
120 } | |
121 | |
OLD | NEW |