/src/Simd/src/Simd/SimdBaseBackground.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Simd Library (http://ermig1979.github.io/Simd). |
3 | | * |
4 | | * Copyright (c) 2011-2017 Yermalayeu Ihar. |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | * of this software and associated documentation files (the "Software"), to deal |
8 | | * in the Software without restriction, including without limitation the rights |
9 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10 | | * copies of the Software, and to permit persons to whom the Software is |
11 | | * furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 | | * SOFTWARE. |
23 | | */ |
24 | | #include "Simd/SimdMath.h" |
25 | | |
26 | | namespace Simd |
27 | | { |
28 | | namespace Base |
29 | | { |
30 | | void BackgroundGrowRangeSlow(const uint8_t * value, size_t valueStride, size_t width, size_t height, |
31 | | uint8_t * lo, size_t loStride, uint8_t * hi, size_t hiStride) |
32 | 0 | { |
33 | 0 | for (size_t row = 0; row < height; ++row) |
34 | 0 | { |
35 | 0 | for (size_t col = 0; col < width; ++col) |
36 | 0 | { |
37 | 0 | if (value[col] < lo[col]) |
38 | 0 | lo[col]--; |
39 | 0 | if (value[col] > hi[col]) |
40 | 0 | hi[col]++; |
41 | 0 | } |
42 | 0 | value += valueStride; |
43 | 0 | lo += loStride; |
44 | 0 | hi += hiStride; |
45 | 0 | } |
46 | 0 | } |
47 | | |
48 | | void BackgroundGrowRangeFast(const uint8_t * value, size_t valueStride, size_t width, size_t height, |
49 | | uint8_t * lo, size_t loStride, uint8_t * hi, size_t hiStride) |
50 | 0 | { |
51 | 0 | for (size_t row = 0; row < height; ++row) |
52 | 0 | { |
53 | 0 | for (size_t col = 0; col < width; ++col) |
54 | 0 | { |
55 | 0 | if (value[col] < lo[col]) |
56 | 0 | lo[col] = value[col]; |
57 | 0 | if (value[col] > hi[col]) |
58 | 0 | hi[col] = value[col]; |
59 | 0 | } |
60 | 0 | value += valueStride; |
61 | 0 | lo += loStride; |
62 | 0 | hi += hiStride; |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | | void BackgroundIncrementCount(const uint8_t * value, size_t valueStride, size_t width, size_t height, |
67 | | const uint8_t * loValue, size_t loValueStride, const uint8_t * hiValue, size_t hiValueStride, |
68 | | uint8_t * loCount, size_t loCountStride, uint8_t * hiCount, size_t hiCountStride) |
69 | 0 | { |
70 | 0 | for (size_t row = 0; row < height; ++row) |
71 | 0 | { |
72 | 0 | for (size_t col = 0; col < width; ++col) |
73 | 0 | { |
74 | 0 | if (value[col] < loValue[col] && loCount[col] < 0xFF) |
75 | 0 | loCount[col]++; |
76 | 0 | if (value[col] > hiValue[col] && hiCount[col] < 0xFF) |
77 | 0 | hiCount[col]++; |
78 | 0 | } |
79 | 0 | value += valueStride; |
80 | 0 | loValue += loValueStride; |
81 | 0 | hiValue += hiValueStride; |
82 | 0 | loCount += loCountStride; |
83 | 0 | hiCount += hiCountStride; |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | | SIMD_INLINE void AdjustLo(const uint8_t & count, uint8_t & value, int threshold) |
88 | 0 | { |
89 | 0 | if (count > threshold) |
90 | 0 | { |
91 | 0 | if (value > 0) |
92 | 0 | value--; |
93 | 0 | } |
94 | 0 | else if (count < threshold) |
95 | 0 | { |
96 | 0 | if (value < 0xFF) |
97 | 0 | value++; |
98 | 0 | } |
99 | 0 | } |
100 | | |
101 | | SIMD_INLINE void AdjustHi(const uint8_t & count, uint8_t & value, int threshold) |
102 | 0 | { |
103 | 0 | if (count > threshold) |
104 | 0 | { |
105 | 0 | if (value < 0xFF) |
106 | 0 | value++; |
107 | 0 | } |
108 | 0 | else if (count < threshold) |
109 | 0 | { |
110 | 0 | if (value > 0) |
111 | 0 | value--; |
112 | 0 | } |
113 | 0 | } |
114 | | |
115 | | void BackgroundAdjustRange(uint8_t * loCount, size_t loCountStride, size_t width, size_t height, |
116 | | uint8_t * loValue, size_t loValueStride, uint8_t * hiCount, size_t hiCountStride, |
117 | | uint8_t * hiValue, size_t hiValueStride, uint8_t threshold) |
118 | 0 | { |
119 | 0 | for (size_t row = 0; row < height; ++row) |
120 | 0 | { |
121 | 0 | for (size_t col = 0; col < width; ++col) |
122 | 0 | { |
123 | 0 | AdjustLo(loCount[col], loValue[col], threshold); |
124 | 0 | AdjustHi(hiCount[col], hiValue[col], threshold); |
125 | 0 | loCount[col] = 0; |
126 | 0 | hiCount[col] = 0; |
127 | 0 | } |
128 | 0 | loValue += loValueStride; |
129 | 0 | hiValue += hiValueStride; |
130 | 0 | loCount += loCountStride; |
131 | 0 | hiCount += hiCountStride; |
132 | 0 | } |
133 | 0 | } |
134 | | |
135 | | void BackgroundAdjustRangeMasked(uint8_t * loCount, size_t loCountStride, size_t width, size_t height, |
136 | | uint8_t * loValue, size_t loValueStride, uint8_t * hiCount, size_t hiCountStride, |
137 | | uint8_t * hiValue, size_t hiValueStride, uint8_t threshold, const uint8_t * mask, size_t maskStride) |
138 | 0 | { |
139 | 0 | for (size_t row = 0; row < height; ++row) |
140 | 0 | { |
141 | 0 | for (size_t col = 0; col < width; ++col) |
142 | 0 | { |
143 | 0 | if (mask[col]) |
144 | 0 | { |
145 | 0 | AdjustLo(loCount[col], loValue[col], threshold); |
146 | 0 | AdjustHi(hiCount[col], hiValue[col], threshold); |
147 | 0 | } |
148 | 0 | loCount[col] = 0; |
149 | 0 | hiCount[col] = 0; |
150 | 0 | } |
151 | 0 | loValue += loValueStride; |
152 | 0 | hiValue += hiValueStride; |
153 | 0 | loCount += loCountStride; |
154 | 0 | hiCount += hiCountStride; |
155 | 0 | mask += maskStride; |
156 | 0 | } |
157 | 0 | } |
158 | | |
159 | | SIMD_INLINE void BackgroundShiftRange(const uint8_t & value, uint8_t & lo, uint8_t & hi) |
160 | 0 | { |
161 | 0 | int add = int(value) - int(hi); |
162 | 0 | int sub = int(lo) - int(value); |
163 | 0 | if (add > 0) |
164 | 0 | { |
165 | 0 | lo = Min(lo + add, 0xFF); |
166 | 0 | hi = Min(hi + add, 0xFF); |
167 | 0 | } |
168 | 0 | if (sub > 0) |
169 | 0 | { |
170 | 0 | lo = Max(lo - sub, 0); |
171 | 0 | hi = Max(hi - sub, 0); |
172 | 0 | } |
173 | 0 | } |
174 | | |
175 | | void BackgroundShiftRange(const uint8_t * value, size_t valueStride, size_t width, size_t height, |
176 | | uint8_t * lo, size_t loStride, uint8_t * hi, size_t hiStride) |
177 | 0 | { |
178 | 0 | for (size_t row = 0; row < height; ++row) |
179 | 0 | { |
180 | 0 | for (size_t col = 0; col < width; ++col) |
181 | 0 | BackgroundShiftRange(value[col], lo[col], hi[col]); |
182 | 0 | value += valueStride; |
183 | 0 | lo += loStride; |
184 | 0 | hi += hiStride; |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | | void BackgroundShiftRangeMasked(const uint8_t * value, size_t valueStride, size_t width, size_t height, |
189 | | uint8_t * lo, size_t loStride, uint8_t * hi, size_t hiStride, const uint8_t * mask, size_t maskStride) |
190 | 0 | { |
191 | 0 | for (size_t row = 0; row < height; ++row) |
192 | 0 | { |
193 | 0 | for (size_t col = 0; col < width; ++col) |
194 | 0 | { |
195 | 0 | if (mask[col]) |
196 | 0 | BackgroundShiftRange(value[col], lo[col], hi[col]); |
197 | 0 | } |
198 | 0 | value += valueStride; |
199 | 0 | lo += loStride; |
200 | 0 | hi += hiStride; |
201 | 0 | mask += maskStride; |
202 | 0 | } |
203 | 0 | } |
204 | | |
205 | | void BackgroundInitMask(const uint8_t * src, size_t srcStride, size_t width, size_t height, |
206 | | uint8_t index, uint8_t value, uint8_t * dst, size_t dstStride) |
207 | 0 | { |
208 | 0 | for (size_t row = 0; row < height; ++row) |
209 | 0 | { |
210 | 0 | for (size_t col = 0; col < width; ++col) |
211 | 0 | { |
212 | 0 | if (src[col] == index) |
213 | 0 | dst[col] = value; |
214 | 0 | } |
215 | 0 | src += srcStride; |
216 | 0 | dst += dstStride; |
217 | 0 | } |
218 | 0 | } |
219 | | } |
220 | | } |