OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "SkColorMatrixFilter.h" | 8 #include "SkColorMatrixFilter.h" |
9 #include "SkColorMatrix.h" | 9 #include "SkColorMatrix.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 static Sk4f unpremul(const Sk4f& x) { | 79 static Sk4f unpremul(const Sk4f& x) { |
80 return x * scale_rgb(1 / x.kth<SK_A32_SHIFT/8>()); // TODO: fast/approx inv
ert? | 80 return x * scale_rgb(1 / x.kth<SK_A32_SHIFT/8>()); // TODO: fast/approx inv
ert? |
81 } | 81 } |
82 | 82 |
83 static Sk4f clamp_0_1(const Sk4f& x) { | 83 static Sk4f clamp_0_1(const Sk4f& x) { |
84 return Sk4f::Max(Sk4f::Min(x, Sk4f(1)), Sk4f(0)); | 84 return Sk4f::Max(Sk4f::Min(x, Sk4f(1)), Sk4f(0)); |
85 } | 85 } |
86 | 86 |
87 static SkPMColor round(const Sk4f& x) { | 87 static SkPMColor round(const Sk4f& x) { |
88 SkPMColor c; | 88 SkPMColor c; |
89 SkNx_cast<uint8_t>(x * Sk4f(255) + Sk4f(0.5f)).store((uint8_t*)&c); | 89 SkNx_cast<uint8_t>(x * Sk4f(255) + Sk4f(0.5f)).store(&c); |
90 return c; | 90 return c; |
91 } | 91 } |
92 | 92 |
93 template <typename Adaptor, typename T> | 93 template <typename Adaptor, typename T> |
94 void filter_span(const float array[], const T src[], int count, T dst[]) { | 94 void filter_span(const float array[], const T src[], int count, T dst[]) { |
95 // c0-c3 are already in [0,1]. | 95 // c0-c3 are already in [0,1]. |
96 const Sk4f c0 = Sk4f::Load(array + 0); | 96 const Sk4f c0 = Sk4f::Load(array + 0); |
97 const Sk4f c1 = Sk4f::Load(array + 4); | 97 const Sk4f c1 = Sk4f::Load(array + 4); |
98 const Sk4f c2 = Sk4f::Load(array + 8); | 98 const Sk4f c2 = Sk4f::Load(array + 8); |
99 const Sk4f c3 = Sk4f::Load(array + 12); | 99 const Sk4f c3 = Sk4f::Load(array + 12); |
(...skipping 25 matching lines...) Expand all Loading... |
125 | 125 |
126 dst[i] = Adaptor::From4f(premul(clamp_0_1(dst4))); | 126 dst[i] = Adaptor::From4f(premul(clamp_0_1(dst4))); |
127 } | 127 } |
128 } | 128 } |
129 | 129 |
130 struct SkPMColorAdaptor { | 130 struct SkPMColorAdaptor { |
131 static SkPMColor From4f(const Sk4f& c4) { | 131 static SkPMColor From4f(const Sk4f& c4) { |
132 return round(c4); | 132 return round(c4); |
133 } | 133 } |
134 static Sk4f To4f(SkPMColor c) { | 134 static Sk4f To4f(SkPMColor c) { |
135 return SkNx_cast<float>(Sk4b::Load((const uint8_t*)&c)) * Sk4f(1.0f/255)
; | 135 return SkNx_cast<float>(Sk4b::Load(&c)) * Sk4f(1.0f/255); |
136 } | 136 } |
137 }; | 137 }; |
138 void SkColorMatrixFilter::filterSpan(const SkPMColor src[], int count, SkPMColor
dst[]) const { | 138 void SkColorMatrixFilter::filterSpan(const SkPMColor src[], int count, SkPMColor
dst[]) const { |
139 filter_span<SkPMColorAdaptor>(fTranspose, src, count, dst); | 139 filter_span<SkPMColorAdaptor>(fTranspose, src, count, dst); |
140 } | 140 } |
141 | 141 |
142 struct SkPM4fAdaptor { | 142 struct SkPM4fAdaptor { |
143 static SkPM4f From4f(const Sk4f& c4) { | 143 static SkPM4f From4f(const Sk4f& c4) { |
144 SkPM4f c; | 144 SkPM4f c; |
145 c4.store(c.fVec); | 145 c4.store(&c); |
146 return c; | 146 return c; |
147 } | 147 } |
148 static Sk4f To4f(const SkPM4f& c) { | 148 static Sk4f To4f(const SkPM4f& c) { |
149 return Sk4f::Load(c.fVec); | 149 return Sk4f::Load(&c); |
150 } | 150 } |
151 }; | 151 }; |
152 void SkColorMatrixFilter::filterSpan4f(const SkPM4f src[], int count, SkPM4f dst
[]) const { | 152 void SkColorMatrixFilter::filterSpan4f(const SkPM4f src[], int count, SkPM4f dst
[]) const { |
153 filter_span<SkPM4fAdaptor>(fTranspose, src, count, dst); | 153 filter_span<SkPM4fAdaptor>(fTranspose, src, count, dst); |
154 } | 154 } |
155 | 155 |
156 /////////////////////////////////////////////////////////////////////////////// | 156 /////////////////////////////////////////////////////////////////////////////// |
157 | 157 |
158 void SkColorMatrixFilter::flatten(SkWriteBuffer& buffer) const { | 158 void SkColorMatrixFilter::flatten(SkWriteBuffer& buffer) const { |
159 SkASSERT(sizeof(fMatrix.fMat)/sizeof(SkScalar) == 20); | 159 SkASSERT(sizeof(fMatrix.fMat)/sizeof(SkScalar) == 20); |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 matrix.setScale(byte_to_scale(SkColorGetR(mul)), | 381 matrix.setScale(byte_to_scale(SkColorGetR(mul)), |
382 byte_to_scale(SkColorGetG(mul)), | 382 byte_to_scale(SkColorGetG(mul)), |
383 byte_to_scale(SkColorGetB(mul)), | 383 byte_to_scale(SkColorGetB(mul)), |
384 1); | 384 1); |
385 matrix.postTranslate(SkIntToScalar(SkColorGetR(add)), | 385 matrix.postTranslate(SkIntToScalar(SkColorGetR(add)), |
386 SkIntToScalar(SkColorGetG(add)), | 386 SkIntToScalar(SkColorGetG(add)), |
387 SkIntToScalar(SkColorGetB(add)), | 387 SkIntToScalar(SkColorGetB(add)), |
388 0); | 388 0); |
389 return SkColorMatrixFilter::Create(matrix); | 389 return SkColorMatrixFilter::Create(matrix); |
390 } | 390 } |
OLD | NEW |