summaryrefslogtreecommitdiffstats
path: root/src/Runtime/Source/Engine/Include/NVImageScaler.h
blob: 069604a6cc71e6aa35c6e605192af2cd1a335453 (plain)
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
/****************************************************************************
**
** Copyright (C) 1993-2009 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#pragma once

namespace Q3DStudio {
namespace ImageScaler {

    //==============================================================================
    /**
     *
     */
    template <INT32 TNumChannels>
    inline void BilinearReduceRows(const CHAR *inSrcBuffer, INT32 inSrcWidth, INT32 inSrcHeight,
                                   CHAR *outDstBuffer, INT32 inDstHeight)
    {
        INT32 theDDAConst = static_cast<INT32>(1024.0 * inDstHeight / inSrcHeight);
        INT32 theDDAAccum = 0;
        INT32 thePixelCount;

        INT32 theSrcRow;
        INT32 theSrcCol;
        INT32 theDstRow;

        INT32 theChannelAccum[TNumChannels] = { 0 };

        CHAR *theDstPointer = outDstBuffer;
        const CHAR *theSrcPointer = inSrcBuffer;
        const CHAR *theSrcColPointer = NULL;
        CHAR *theDstColPointer = NULL;

        INT32 theStepSize = TNumChannels;
        INT32 theSrcStride = TNumChannels * inSrcWidth;
        INT32 theDstStride = TNumChannels * inSrcWidth;

        for (theSrcCol = 0; theSrcCol < inSrcWidth; ++theSrcCol) {
            theSrcColPointer = theSrcPointer + (theSrcCol * theStepSize);
            theDstColPointer = theDstPointer + (theSrcCol * theStepSize);

            theSrcRow = 0L;
            theDstRow = 0L;
            thePixelCount = 0L;

            for (INT32 idx = 0; idx < TNumChannels; ++idx)
                theChannelAccum[idx] = 0L;

            theDDAAccum = 0L;

            while (theSrcRow < inSrcHeight) {
                while ((theDDAAccum < 1024L) && (theSrcRow < inSrcHeight)) {
                    for (INT32 idx = 0; idx < TNumChannels; ++idx)
                        theChannelAccum[idx] +=
                            1024L * theSrcColPointer[(theSrcRow * theSrcStride) + idx];

                    theDDAAccum += theDDAConst;
                    thePixelCount += 1024L;
                    ++theSrcRow;
                }

                theDDAAccum = (theSrcRow < inSrcHeight) ? (theDDAAccum - 1024L) : (0L);
                thePixelCount -= theDDAAccum;

                for (INT32 idx = 0; idx < TNumChannels; ++idx)
                    theChannelAccum[idx] -= theDDAAccum
                        * (INT32)theSrcColPointer[((theSrcRow - 1) * theSrcStride) + idx];

                for (INT32 idx = 0; idx < TNumChannels; ++idx)
                    theDstColPointer[(theDstRow * theDstStride) + idx] =
                        (CHAR)(theChannelAccum[idx] / thePixelCount);

                thePixelCount = 1024L - theDDAAccum;
                ++theDstRow;

                if (theDstRow >= inDstHeight)
                    break;

                for (INT32 idx = 0; idx < TNumChannels; ++idx)
                    theChannelAccum[idx] = thePixelCount
                        * (INT32)theSrcColPointer[((theSrcRow - 1) * theSrcStride) + idx];
            }
        }
    }

    inline void BilinearReduceRows(const CHAR *inSrcBuffer, INT32 inSrcWidth, INT32 inSrcHeight,
                                   INT32 inChannels, CHAR *outDstBuffer, INT32 inDstHeight)
    {
        switch (inChannels) {
        case 4:
            BilinearReduceRows<4>(inSrcBuffer, inSrcWidth, inSrcHeight, outDstBuffer, inDstHeight);
            break;
        case 3:
            BilinearReduceRows<3>(inSrcBuffer, inSrcWidth, inSrcHeight, outDstBuffer, inDstHeight);
            break;
        case 2:
            BilinearReduceRows<2>(inSrcBuffer, inSrcWidth, inSrcHeight, outDstBuffer, inDstHeight);
            break;
        case 1:
            BilinearReduceRows<1>(inSrcBuffer, inSrcWidth, inSrcHeight, outDstBuffer, inDstHeight);
            break;
        }
    }

    template <INT32 TNumChannels>
    inline void BilinearReduceCols(const CHAR *inSrcBuffer, INT32 inSrcWidth, INT32 inSrcHeight,
                                   CHAR *outDstBuffer, INT32 inDstWidth)
    {
        INT32 theDDAConst = static_cast<INT32>(1024.0 * inDstWidth / inSrcWidth);
        INT32 theDDAAccum = 0L;
        INT32 thePixelCount;

        INT32 theSrcRow;
        INT32 theSrcCol;
        INT32 theDstCol;

        INT32 theChannelAccum[TNumChannels];

        CHAR *theDstPointer = outDstBuffer;
        const CHAR *theSrcPointer = inSrcBuffer;
        const CHAR *theSrcRowPointer;
        CHAR *theDstRowPointer;

        INT32 theSrcStepSize = TNumChannels;
        INT32 theDstStepSize = TNumChannels;

        for (theSrcRow = 0; theSrcRow < inSrcHeight; ++theSrcRow) {

            theSrcRowPointer = theSrcPointer + (theSrcRow * inSrcWidth * theSrcStepSize);
            theDstRowPointer = theDstPointer + (theSrcRow * inDstWidth * theDstStepSize);

            theSrcCol = 0L;
            theDstCol = 0L;
            for (INT32 idx = 0; idx < TNumChannels; ++idx)
                theChannelAccum[idx] = 0L;
            thePixelCount = 0L;
            theDDAAccum = 0L;

            while (theSrcCol < inSrcWidth) {
                while ((theDDAAccum < 1024L) && (theSrcCol < inSrcWidth)) {
                    for (INT32 idx = 0; idx < TNumChannels; ++idx)
                        theChannelAccum[idx] +=
                            1024L * theSrcRowPointer[(theSrcCol * theSrcStepSize) + idx];

                    theDDAAccum += theDDAConst;
                    thePixelCount += 1024L;
                    ++theSrcCol;
                }

                theDDAAccum = (theSrcCol < inSrcWidth) ? (theDDAAccum - 1024L) : (0L);
                thePixelCount -= theDDAAccum;

                for (INT32 idx = 0; idx < TNumChannels; ++idx)
                    theChannelAccum[idx] -= theDDAAccum
                        * (INT32)theSrcRowPointer[((theSrcCol - 1) * theSrcStepSize) + idx];

                for (INT32 idx = 0; idx < TNumChannels; ++idx)
                    theDstRowPointer[(theDstCol * theDstStepSize) + idx] =
                        (CHAR)(theChannelAccum[idx] / thePixelCount);

                thePixelCount = 1024L - theDDAAccum;
                ++theDstCol;

                if (theDstCol >= inDstWidth) {
                    break;
                }

                for (INT32 idx = 0; idx < TNumChannels; ++idx)
                    theChannelAccum[idx] = thePixelCount
                        * (INT32)theSrcRowPointer[((theSrcCol - 1) * theSrcStepSize) + idx];
            }
        }
    }

    inline void BilinearReduceCols(const CHAR *inSrcBuffer, INT32 inSrcWidth, INT32 inSrcHeight,
                                   INT32 inChannels, CHAR *&outDstBuffer, INT32 inDstWidth)
    {
        switch (inChannels) {
        case 4:
            BilinearReduceCols<4>(inSrcBuffer, inSrcWidth, inSrcHeight, outDstBuffer, inDstWidth);
            break;
        case 3:
            BilinearReduceCols<3>(inSrcBuffer, inSrcWidth, inSrcHeight, outDstBuffer, inDstWidth);
            break;
        case 2:
            BilinearReduceCols<2>(inSrcBuffer, inSrcWidth, inSrcHeight, outDstBuffer, inDstWidth);
            break;
        case 1:
            BilinearReduceCols<1>(inSrcBuffer, inSrcWidth, inSrcHeight, outDstBuffer, inDstWidth);
            break;
        }
    }

    template <INT32 TNumChannels>
    inline void BilinearReduceImage(const CHAR *inSrcBuffer, INT32 inSrcWidth, INT32 inSrcHeight,
                                    CHAR *outDstBuffer, INT32 inDstWidth, INT32 inDstHeight)
    {
        // Number larger than 1024
        // One pixel in the source image is equivalent to this much of a pixel
        // in terms of dest pixels.
        INT32 theXIncrement = static_cast<INT32>(1024.f * inSrcWidth / inDstWidth);
        // How many rows do we skip, in fixed point arithmetic, when we bump lines
        INT32 theYIncrement = static_cast<INT32>(1024.f * inSrcHeight / inDstHeight);
        // We are starting at position .5 in textel space
        INT32 theSrcYPtr = 0;
        CHAR *theDstPtr = outDstBuffer;
        for (INT32 theHeightIndex = 0; theHeightIndex < inDstHeight;
             ++theHeightIndex, theSrcYPtr += theYIncrement) {
            INT32 theSrcXPtr = 0;
            for (INT32 theWidthIndex = 0; theWidthIndex < inDstWidth;
                 ++theWidthIndex, theSrcXPtr += theXIncrement, theDstPtr += TNumChannels) {
                INT32 theAccum[TNumChannels] = { 0 };
                INT32 theNumPixels = 0;
                // Pull all reasonable pixels from the source image.
                INT32 theStartRow = (theSrcYPtr / 1024);
                INT32 theStopRow = (theSrcYPtr + theYIncrement) / 1024;
                INT32 theStartColumn = theSrcXPtr / 1024;
                INT32 theStopColumn = (theSrcXPtr + theXIncrement) / 1024;
                // Average everything between the columns
                for (INT32 theRow = theStartRow; theRow < theStopRow; ++theRow) {
                    INT32 theSrcAddr = (theRow * inSrcWidth + theStartColumn) * TNumChannels;
                    for (INT32 theCol = theStartColumn; theCol < theStopColumn;
                         ++theCol, theSrcAddr += TNumChannels) {
                        ++theNumPixels;
                        for (INT32 idx = 0; idx < TNumChannels; ++idx)
                            theAccum[idx] += inSrcBuffer[theSrcAddr + idx];
                    }
                }

                for (INT32 idx = 0; idx < TNumChannels; ++idx)
                    theDstPtr[idx] = theAccum[idx] / theNumPixels;
            }
        }
    }

    template <INT32 TNumChannels>
    inline void NearestReduceImage(const CHAR *inSrcBuffer, INT32 inSrcWidth, INT32 inSrcHeight,
                                   CHAR *outDstBuffer, INT32 inDstWidth, INT32 inDstHeight)
    {
        // Number larger than 1024
        // One pixel in the source image is equivalent to this much of a pixel
        // in terms of dest pixels.
        INT32 theXIncrement = static_cast<INT32>(1024.f * inSrcWidth / inDstWidth);
        // How many rows do we skip, in fixed point arithmetic, when we bump lines
        INT32 theYIncrement = static_cast<INT32>(1024.f * inSrcHeight / inDstHeight);
        // We are starting at position .5 in textel space
        INT32 theSrcYPtr = theYIncrement / 2;
        CHAR *theDstPtr = outDstBuffer;
        for (INT32 theHeightIndex = 0; theHeightIndex < inDstHeight;
             ++theHeightIndex, theSrcYPtr += theYIncrement) {
            INT32 theSrcRow = (theSrcYPtr / 1024) * inSrcWidth * TNumChannels;
            INT32 theSrcXPtr = theXIncrement / 2;
            for (INT32 theWidthIndex = 0; theWidthIndex < inDstWidth;
                 ++theWidthIndex, theSrcXPtr += theXIncrement, theDstPtr += TNumChannels) {
                INT32 theSrcPtr = theSrcRow + (theSrcXPtr >> 10) * TNumChannels;
                for (INT32 idx = 0; idx < TNumChannels; ++idx)
                    theDstPtr[idx] = inSrcBuffer[theSrcPtr + idx];
            }
        }
    }

    inline void NearestReduceImage(const CHAR *inSrcBuffer, INT32 inSrcWidth, INT32 inSrcHeight,
                                   INT32 inChannels, CHAR *outDstBuffer, INT32 inDstWidth,
                                   INT32 inDstHeight)
    {
        switch (inChannels) {
        case 4:
            NearestReduceImage<4>(inSrcBuffer, inSrcWidth, inSrcHeight, outDstBuffer, inDstWidth,
                                  inDstHeight);
            break;
        case 3:
            NearestReduceImage<3>(inSrcBuffer, inSrcWidth, inSrcHeight, outDstBuffer, inDstWidth,
                                  inDstHeight);
            break;
        case 2:
            NearestReduceImage<2>(inSrcBuffer, inSrcWidth, inSrcHeight, outDstBuffer, inDstWidth,
                                  inDstHeight);
            break;
        case 1:
            NearestReduceImage<1>(inSrcBuffer, inSrcWidth, inSrcHeight, outDstBuffer, inDstWidth,
                                  inDstHeight);
            break;
        }
    }
}
}