File: SimdBaseReduceGray5x5.cpp

package info (click to toggle)
visp 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 119,296 kB
  • sloc: cpp: 500,914; ansic: 52,904; xml: 22,642; python: 7,365; java: 4,247; sh: 482; makefile: 237; objc: 145
file content (351 lines) | stat: -rw-r--r-- 13,645 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Simd Library (http://ermig1979.github.io/Simd).
*
* Copyright (c) 2011-2018 Yermalayeu Ihar.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "Simd/SimdMemory.h"

namespace Simd
{
    namespace Base
    {
        namespace
        {
            struct Buffer
            {
                Buffer(size_t width)
                {
                    _p = Allocate(sizeof(uint16_t) * 3 * width);
                    isc0 = (uint16_t*)_p;
                    isc1 = isc0 + width;
                    iscp = isc1 + width;
                }

                ~Buffer()
                {
                    Free(_p);
                }

                uint16_t * isc0;
                uint16_t * isc1;
                uint16_t * iscp;
            private:
                void *_p;
            };
        }


        /**************************************************************************************************
        *  The Burt & Adelson Reduce operation. This function use 2-D version of algorithm;
        *
        *  Reference:
        *  Frederick M. Waltz and John W.V. Miller. An efficient algorithm for Gaussian blur using
        *  finite-state machines.
        *  SPIE Conf. on Machine Vision Systems for Inspection and Metrology VII. November 1998.
        *
        *
        *  2-D explanation:
        *
        *  src image pixels:   A  B  C  D  E       dst image pixels:   a     b     c
        *                      F  G  H  I  J
        *                      K  L  M  N  O                           d     e     f
        *                      P  Q  R  S  T
        *                      U  V  W  X  Y                           g     h     i
        *
        *  Algorithm visits all src image pixels from left to right and top to bottom.
        *  When visiting src pixel Y, the value of e will be written to the dst image.
        *
        *  State variables before visiting Y:
        *  sr0 = W
        *  sr1 = U + 4V
        *  srp = 4X
        *  sc0[2] = K + 4L + 6M + 4N + O
        *  sc1[2] = (A + 4B + 6C + 4D + E) + 4*(F + 4G + 6H + 4I + J)
        *  scp[2] = 4*(P + 4Q + 6R + 4S + T)
        *
        *  State variables after visiting Y:
        *  sr0 = Y
        *  sr1 = W + 4X
        *  srp = 4X
        *  sc0[2] = U + 4V + 6W + 4X + Y
        *  sc1[2] = (K + 4L + 6M + 4N + O) + 4*(P + 4Q + 6R + 4S + T)
        *  scp[2] = 4*(P + 4Q + 6R + 4S + T)
        *  e =   1 * (A + 4B + 6C + 4D + E)
        *      + 4 * (F + 4G + 6H + 4I + J)
        *      + 6 * (K + 4L + 6M + 4N + O)
        *      + 4 * (P + 4Q + 6R + 4S + T)
        *      + 1 * (U + 4V + 6W + 4X + Y)
        *
        *  Updates when visiting (even x, even y) source pixel:
        *  (all updates occur in parallel)
        *  sr0 <= current
        *  sr1 <= sr0 + srp
        *  sc0[x] <= sr1 + 6*sr0 + srp + current
        *  sc1[x] <= sc0[x] + scp[x]
        *  dst(-1,-1) <= sc1[x] + 6*sc0[x] + scp + (new sc0[x])
        *
        *  Updates when visiting (odd x, even y) source pixel:
        *  srp <= 4*current
        *
        *  Updates when visiting (even x, odd y) source pixel:
        *  sr0 <= current
        *  sr1 <= sr0 + srp
        *  scp[x] <= 4*(sr1 + 6*sr0 + srp + current)
        *
        *  Updates when visting (odd x, odd y) source pixel:
        *  srp <= 4*current
        **************************************************************************************************/
        template <bool compensation> SIMD_INLINE int DivideBy256(int value);

        template <> SIMD_INLINE int DivideBy256<true>(int value)
        {
            return (value + 128) >> 8;
        }

        template <> SIMD_INLINE int DivideBy256<false>(int value)
        {
            return value >> 8;
        }

        template <bool compensation> void ReduceGray5x5(const uint8_t *src, size_t srcWidth, size_t srcHeight, size_t srcStride,
            uint8_t *dst, size_t dstWidth, size_t dstHeight, size_t dstStride)
        {
            assert((srcWidth + 1) / 2 == dstWidth && (srcHeight + 1) / 2 == dstHeight);

            Buffer buffer(dstWidth + 1);

            unsigned short isr0, isr1, isrp;

            const short zeroPixel = 0;

            uint8_t * dy = dst;
            uint8_t * dx = dy;
            const uint8_t * sy = src;
            const uint8_t * sx = sy;

            bool evenY = true;
            bool evenX = true;
            size_t srcy = 0;
            size_t srcx = 0;
            size_t dstx = 0;

            // First row
            {
                isr0 = *sy;
                isr1 = zeroPixel;
                isrp = (unsigned short)(*sy) * 4;

                // Main pixels in first row
                for (sx = sy, evenX = true, srcx = 0, dstx = 0; srcx < srcWidth; ++srcx, ++sx)
                {
                    unsigned short icurrent(*sx);

                    if (evenX)
                    {
                        buffer.isc0[dstx] = isr1 + 6 * isr0 + isrp + icurrent;
                        buffer.isc1[dstx] = 5 * buffer.isc0[dstx];
                        isr1 = isr0 + isrp;
                        isr0 = icurrent;
                    }
                    else
                    {
                        isrp = icurrent * 4;
                        ++dstx;
                    }
                    evenX = !evenX;
                }

                // Last entries in first row
                if (!evenX)
                {
                    // previous srcx was even
                    ++dstx;
                    buffer.isc0[dstx] = isr1 + 11 * isr0;
                    buffer.isc1[dstx] = 5 * buffer.isc0[dstx];
                }
                else
                {
                    // previous srcx was odd
                    buffer.isc0[dstx] = isr1 + 6 * isr0 + isrp + (isrp >> 2);
                    buffer.isc1[dstx] = 5 * buffer.isc0[dstx];
                }
            }
            sy += srcStride;

            // Main Rows
            {
                for (evenY = false, srcy = 1; srcy < srcHeight; ++srcy, sy += srcStride)
                {
                    isr0 = (unsigned short)(*sy);
                    isr1 = zeroPixel;
                    isrp = (unsigned short)(*sy) * 4;

                    if (evenY)
                    {
                        // Even-numbered row
                        // First entry in row
                        sx = sy;
                        isr1 = isr0 + isrp;
                        isr0 = (unsigned short)(*sx);
                        ++sx;
                        dx = dy;

                        unsigned short * p_isc0 = buffer.isc0;
                        unsigned short * p_isc1 = buffer.isc1;
                        unsigned short * p_iscp = buffer.iscp;

                        // Main entries in row
                        for (evenX = false, srcx = 1, dstx = 0; srcx < (srcWidth - 1); srcx += 2, ++sx)
                        {
                            p_isc0++;
                            p_isc1++;
                            p_iscp++;

                            unsigned short icurrent = (unsigned short)(*sx);

                            isrp = icurrent * 4;
                            icurrent = (unsigned short)(*(++sx));

                            unsigned short ip;
                            ip = *p_isc1 + 6 * (*p_isc0) + *p_iscp;
                            *p_isc1 = *p_isc0 + *p_iscp;
                            *p_isc0 = isr1 + 6 * isr0 + isrp + icurrent;
                            isr1 = isr0 + isrp;
                            isr0 = icurrent;
                            ip = ip + *p_isc0;
                            *dx = DivideBy256<compensation>(ip);
                            ++dx;
                        }
                        dstx += p_isc0 - buffer.isc0;

                        //doing the last operation due to even number of operations in previous cycle
                        if (!(srcWidth & 1))
                        {
                            unsigned short icurrent = (unsigned short)(*sx);
                            isrp = icurrent * 4;
                            ++dstx;
                            evenX = !evenX;
                            ++sx;
                        }

                        // Last entries in row
                        if (!evenX)
                        {
                            // previous srcx was even
                            ++dstx;

                            unsigned short ip;
                            ip = buffer.isc1[dstx] + 6 * buffer.isc0[dstx] + buffer.iscp[dstx];
                            buffer.isc1[dstx] = buffer.isc0[dstx] + buffer.iscp[dstx];
                            buffer.isc0[dstx] = isr1 + 11 * isr0;
                            ip = ip + buffer.isc0[dstx];
                            *dx = DivideBy256<compensation>(ip);
                        }
                        else
                        {
                            // Previous srcx was odd
                            unsigned short ip;
                            ip = buffer.isc1[dstx] + 6 * buffer.isc0[dstx] + buffer.iscp[dstx];
                            buffer.isc1[dstx] = buffer.isc0[dstx] + buffer.iscp[dstx];
                            buffer.isc0[dstx] = isr1 + 6 * isr0 + isrp + (isrp >> 2);
                            ip = ip + buffer.isc0[dstx];
                            *dx = DivideBy256<compensation>(ip);
                        }

                        dy += dstStride;
                    }
                    else
                    {
                        // First entry in odd-numbered row
                        sx = sy;
                        isr1 = isr0 + isrp;
                        isr0 = (unsigned short)(*sx);
                        ++sx;

                        // Main entries in odd-numbered row
                        unsigned short * p_iscp = buffer.iscp;

                        for (evenX = false, srcx = 1, dstx = 0; srcx < (srcWidth - 1); srcx += 2, ++sx)
                        {
                            unsigned short icurrent = (unsigned short)(*sx);
                            isrp = icurrent * 4;

                            p_iscp++;

                            icurrent = (unsigned short)(*(++sx));

                            *p_iscp = (isr1 + 6 * isr0 + isrp + icurrent) * 4;
                            isr1 = isr0 + isrp;
                            isr0 = icurrent;
                        }
                        dstx += p_iscp - buffer.iscp;

                        //doing the last operation due to even number of operations in previous cycle
                        if (!(srcWidth & 1))
                        {
                            unsigned short icurrent = (unsigned short)(*sx);
                            isrp = icurrent * 4;
                            ++dstx;
                            evenX = !evenX;
                            ++sx;
                        }

                        // Last entries in row
                        if (!evenX)
                        {
                            // previous srcx was even
                            ++dstx;
                            buffer.iscp[dstx] = (isr1 + 11 * isr0) * 4;
                        }
                        else
                        {
                            buffer.iscp[dstx] = (isr1 + 6 * isr0 + isrp + (isrp >> 2)) * 4;
                        }
                    }
                    evenY = !evenY;
                }
            }

            // Last Rows
            {
                if (!evenY)
                {
                    for (dstx = 1, dx = dy; dstx < (dstWidth + 1); ++dstx, ++dx)
                        *dx = DivideBy256<compensation>(buffer.isc1[dstx] + 11 * buffer.isc0[dstx]);
                }
                else
                {
                    for (dstx = 1, dx = dy; dstx < (dstWidth + 1); ++dstx, ++dx)
                        *dx = DivideBy256<compensation>(buffer.isc1[dstx] + 6 * buffer.isc0[dstx] + buffer.iscp[dstx] + (buffer.iscp[dstx] >> 2));
                }
            }
        }

        void ReduceGray5x5(const uint8_t *src, size_t srcWidth, size_t srcHeight, size_t srcStride,
            uint8_t *dst, size_t dstWidth, size_t dstHeight, size_t dstStride, int compensation)
        {
            if (compensation)
                ReduceGray5x5<true>(src, srcWidth, srcHeight, srcStride, dst, dstWidth, dstHeight, dstStride);
            else
                ReduceGray5x5<false>(src, srcWidth, srcHeight, srcStride, dst, dstWidth, dstHeight, dstStride);
        }
    }
}