File: SimdBaseResizerNearest.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 (121 lines) | stat: -rw-r--r-- 4,967 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
/*
* Simd Library (http://ermig1979.github.io/Simd).
*
* Copyright (c) 2011-2021 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"
#include "Simd/SimdResizer.h"
#include "Simd/SimdCopyPixel.h"

namespace Simd
{
    namespace Base
    {
        ResizerNearest::ResizerNearest(const ResParam& param)
            : Resizer(param)
            , _pixelSize(0)
        {
        }

        void ResizerNearest::EstimateIndex(size_t srcSize, size_t dstSize, size_t channelSize, size_t channels, int32_t* indices)
        {
            if (_param.method == SimdResizeMethodNearest)
            {
                float scale = (float)srcSize / dstSize;
                for (size_t i = 0; i < dstSize; ++i)
                {
                    float alpha = (i + 0.5f) * scale;
                    int index = RestrictRange((int)::floor(alpha), 0, (int)srcSize - 1);
                    for (size_t c = 0; c < channels; c++)
                    {
                        size_t offset = i * channels + c;
                        indices[offset] = (int32_t)((channels * index + c) * channelSize);
                    }
                }
            }
            else if (_param.method == SimdResizeMethodNearestPytorch)
            {
                for (size_t i = 0; i < dstSize; ++i)
                {
                    int index = RestrictRange((int)(i * srcSize / dstSize), 0, (int)srcSize - 1);
                    for (size_t c = 0; c < channels; c++)
                    {
                        size_t offset = i * channels + c;
                        indices[offset] = (int32_t)((channels * index + c) * channelSize);
                    }
                }
            }
            else
                assert(0);
        }

        void ResizerNearest::EstimateParams()
        {
            if (_pixelSize)
                return;
            _pixelSize = _param.PixelSize();
            _iy.Resize(_param.dstH, false, _param.align);
            EstimateIndex(_param.srcH, _param.dstH, 1, 1, _iy.data);
            _ix.Resize(_param.dstW, false, _param.align);
            EstimateIndex(_param.srcW, _param.dstW, _pixelSize, 1, _ix.data);
        }

        void ResizerNearest::Resize(const uint8_t* src, size_t srcStride, uint8_t* dst, size_t dstStride)
        {
            for (size_t dy = 0; dy < _param.dstH; dy++)
            {
                const uint8_t* srcRow = src + _iy[dy] * srcStride;
                for (size_t dx = 0, offset = 0; dx < _param.dstW; dx++, offset += _pixelSize)
                    memcpy(dst + offset, srcRow + _ix[dx], _pixelSize);
                dst += dstStride;
            }
        }

        template<size_t N> void ResizerNearest::Resize(const uint8_t* src, size_t srcStride, uint8_t* dst, size_t dstStride)
        {
            for (size_t dy = 0; dy < _param.dstH; dy++)
            {
                const uint8_t * srcRow = src + _iy[dy] * srcStride;
                for (size_t dx = 0, offset = 0; dx < _param.dstW; dx++, offset += N)
                    CopyPixel<N>(srcRow + _ix[dx], dst + offset);
                dst += dstStride;
            }
        }

        void ResizerNearest::Run(const uint8_t* src, size_t srcStride, uint8_t* dst, size_t dstStride)
        {
            EstimateParams();
            switch (_pixelSize)
            {
            case 1: Resize<1>(src, srcStride, dst, dstStride); break;
            case 2: Resize<2>(src, srcStride, dst, dstStride); break;
            case 3: Resize<3>(src, srcStride, dst, dstStride); break;
            case 4: Resize<4>(src, srcStride, dst, dstStride); break;
            case 6: Resize<6>(src, srcStride, dst, dstStride); break;
            case 8: Resize<8>(src, srcStride, dst, dstStride); break;
            case 12: Resize<12>(src, srcStride, dst, dstStride); break;
            default:
                Resize(src, srcStride, dst, dstStride);
            }
        }
    }
}