lin
2025-08-14 dae8bad597b6607a449b32bf76c523423f7720ed
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
/*-------------------------------------------------------------------------
 * drawElements Quality Program Tester Core
 * ----------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Bilinear image comparison.
 *//*--------------------------------------------------------------------*/
 
#include "tcuBilinearImageCompare.hpp"
#include "tcuTexture.hpp"
#include "tcuTextureUtil.hpp"
#include "tcuRGBA.hpp"
 
namespace tcu
{
 
namespace
{
 
enum
{
   NUM_SUBPIXEL_BITS    = 8    //!< Number of subpixel bits used when doing bilinear interpolation.
};
 
// \note Algorithm assumes that colors are packed to 32-bit values as dictated by
//         tcu::RGBA::*_SHIFT values.
 
template<int Channel>
static inline deUint8 getChannel (deUint32 color)
{
   return (deUint8)((color >> (Channel*8)) & 0xff);
}
 
#if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
inline deUint32 readRGBA8Raw (const ConstPixelBufferAccess& src, deUint32 x, deUint32 y)
{
   return *(const deUint32*)((const deUint8*)src.getDataPtr() + y*src.getRowPitch() + x*4);
}
#else
inline deUint32 readRGBA8Raw (const ConstPixelBufferAccess& src, deUint32 x, deUint32 y)
{
   return deReverseBytes32(*(const deUint32*)((const deUint8*)src.getDataPtr() + y*src.getRowPitch() + x*4));
}
#endif
 
inline RGBA readRGBA8 (const ConstPixelBufferAccess& src, deUint32 x, deUint32 y)
{
   deUint32 raw = readRGBA8Raw(src, x, y);
   deUint32 res = 0;
 
   res |= getChannel<0>(raw) << RGBA::RED_SHIFT;
   res |= getChannel<1>(raw) << RGBA::GREEN_SHIFT;
   res |= getChannel<2>(raw) << RGBA::BLUE_SHIFT;
   res |= getChannel<3>(raw) << RGBA::ALPHA_SHIFT;
 
   return RGBA(res);
}
 
inline deUint8 interpolateChannel (deUint32 fx1, deUint32 fy1, deUint8 p00, deUint8 p01, deUint8 p10, deUint8 p11)
{
   const deUint32 fx0        = (1u<<NUM_SUBPIXEL_BITS) - fx1;
   const deUint32 fy0        = (1u<<NUM_SUBPIXEL_BITS) - fy1;
   const deUint32 half        = 1u<<(NUM_SUBPIXEL_BITS*2 - 1);
   const deUint32 sum        = fx0*fy0*p00 + fx1*fy0*p10 + fx0*fy1*p01 + fx1*fy1*p11;
   const deUint32 rounded    = (sum + half) >> (NUM_SUBPIXEL_BITS*2);
 
   DE_ASSERT(de::inRange<deUint32>(rounded, 0, 0xff));
   return (deUint8)rounded;
}
 
RGBA bilinearSampleRGBA8 (const ConstPixelBufferAccess& access, deUint32 u, deUint32 v)
{
   deUint32    x0        = u>>NUM_SUBPIXEL_BITS;
   deUint32    y0        = v>>NUM_SUBPIXEL_BITS;
   deUint32    x1        = x0+1; //de::min(x0+1, (deUint32)(access.getWidth()-1));
   deUint32    y1        = y0+1; //de::min(y0+1, (deUint32)(access.getHeight()-1));
 
   DE_ASSERT(x1 < (deUint32)access.getWidth());
   DE_ASSERT(y1 < (deUint32)access.getHeight());
 
   deUint32    fx1        = u-(x0<<NUM_SUBPIXEL_BITS);
   deUint32    fy1        = v-(y0<<NUM_SUBPIXEL_BITS);
 
   deUint32    p00        = readRGBA8Raw(access, x0, y0);
   deUint32    p10        = readRGBA8Raw(access, x1, y0);
   deUint32    p01        = readRGBA8Raw(access, x0, y1);
   deUint32    p11        = readRGBA8Raw(access, x1, y1);
 
   deUint32    res        = 0;
 
   res |= interpolateChannel(fx1, fy1, getChannel<0>(p00), getChannel<0>(p01), getChannel<0>(p10), getChannel<0>(p11)) << RGBA::RED_SHIFT;
   res |= interpolateChannel(fx1, fy1, getChannel<1>(p00), getChannel<1>(p01), getChannel<1>(p10), getChannel<1>(p11)) << RGBA::GREEN_SHIFT;
   res |= interpolateChannel(fx1, fy1, getChannel<2>(p00), getChannel<2>(p01), getChannel<2>(p10), getChannel<2>(p11)) << RGBA::BLUE_SHIFT;
   res |= interpolateChannel(fx1, fy1, getChannel<3>(p00), getChannel<3>(p01), getChannel<3>(p10), getChannel<3>(p11)) << RGBA::ALPHA_SHIFT;
 
   return RGBA(res);
}
 
bool comparePixelRGBA8 (const ConstPixelBufferAccess& reference, const ConstPixelBufferAccess& result, const RGBA threshold, int x, int y)
{
   const RGBA resPix = readRGBA8(result, (deUint32)x, (deUint32)y);
 
   // Step 1: Compare result pixel to 3x3 neighborhood pixels in reference.
   {
       const deUint32    x0        = (deUint32)de::max(x-1, 0);
       const deUint32    x1        = (deUint32)x;
       const deUint32    x2        = (deUint32)de::min(x+1, reference.getWidth()-1);
       const deUint32    y0        = (deUint32)de::max(y-1, 0);
       const deUint32    y1        = (deUint32)y;
       const deUint32    y2        = (deUint32)de::min(y+1, reference.getHeight()-1);
 
       if (compareThreshold(resPix, readRGBA8(reference, x1, y1), threshold) ||
           compareThreshold(resPix, readRGBA8(reference, x0, y1), threshold) ||
           compareThreshold(resPix, readRGBA8(reference, x2, y1), threshold) ||
           compareThreshold(resPix, readRGBA8(reference, x0, y0), threshold) ||
           compareThreshold(resPix, readRGBA8(reference, x1, y0), threshold) ||
           compareThreshold(resPix, readRGBA8(reference, x2, y0), threshold) ||
           compareThreshold(resPix, readRGBA8(reference, x0, y2), threshold) ||
           compareThreshold(resPix, readRGBA8(reference, x1, y2), threshold) ||
           compareThreshold(resPix, readRGBA8(reference, x2, y2), threshold))
           return true;
   }
 
   // Step 2: Compare using bilinear sampling.
   {
       // \todo [pyry] Optimize sample positions!
       static const deUint32 s_offsets[][2] =
       {
           { 226, 186 },
           { 335, 235 },
           { 279, 334 },
           { 178, 272 },
           { 112, 202 },
           { 306, 117 },
           { 396, 299 },
           { 206, 382 },
           { 146,  96 },
           { 423, 155 },
           { 361, 412 },
           {  84, 339 },
           {  48, 130 },
           { 367,  43 },
           { 455, 367 },
           { 105, 439 },
           {  83,  46 },
           { 217,  24 },
           { 461,  71 },
           { 450, 459 },
           { 239, 469 },
           {  67, 267 },
           { 459, 255 },
           {  13, 416 },
           {  10, 192 },
           { 141, 502 },
           { 503, 304 },
           { 380, 506 }
       };
 
       for (int sampleNdx = 0; sampleNdx < DE_LENGTH_OF_ARRAY(s_offsets); sampleNdx++)
       {
           const int u = (x<<NUM_SUBPIXEL_BITS) + (int)s_offsets[sampleNdx][0] - (1<<NUM_SUBPIXEL_BITS);
           const int v = (y<<NUM_SUBPIXEL_BITS) + (int)s_offsets[sampleNdx][1] - (1<<NUM_SUBPIXEL_BITS);
 
           if (!de::inBounds(u, 0, (reference.getWidth()-1)<<NUM_SUBPIXEL_BITS) ||
               !de::inBounds(v, 0, (reference.getHeight()-1)<<NUM_SUBPIXEL_BITS))
               continue;
 
           if (compareThreshold(resPix, bilinearSampleRGBA8(reference, (deUint32)u, (deUint32)v), threshold))
               return true;
       }
   }
 
   return false;
}
 
bool bilinearCompareRGBA8 (const ConstPixelBufferAccess& reference, const ConstPixelBufferAccess& result, const PixelBufferAccess& errorMask, const RGBA threshold)
{
   DE_ASSERT(reference.getFormat() == TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8) &&
             result.getFormat()    == TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8));
 
   // Clear error mask first to green (faster this way).
   clear(errorMask, Vec4(0.0f, 1.0f, 0.0f, 1.0f));
 
   bool allOk = true;
 
   for (int y = 0; y < reference.getHeight(); y++)
   {
       for (int x = 0; x < reference.getWidth(); x++)
       {
           if (!comparePixelRGBA8(reference, result, threshold, x, y) &&
               !comparePixelRGBA8(result, reference, threshold, x, y))
           {
               allOk = false;
               errorMask.setPixel(Vec4(1.0f, 0.0f, 0.0f, 1.0f), x, y);
           }
       }
   }
 
   return allOk;
}
 
} // anonymous
 
bool bilinearCompare (const ConstPixelBufferAccess& reference, const ConstPixelBufferAccess& result, const PixelBufferAccess& errorMask, const RGBA threshold)
{
   DE_ASSERT(reference.getWidth()    == result.getWidth()    &&
             reference.getHeight()    == result.getHeight()    &&
             reference.getDepth()    == result.getDepth()    &&
             reference.getFormat()    == result.getFormat());
   DE_ASSERT(reference.getWidth()    == errorMask.getWidth()        &&
             reference.getHeight()    == errorMask.getHeight()    &&
             reference.getDepth()    == errorMask.getDepth());
 
   if (reference.getFormat() == TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8))
       return bilinearCompareRGBA8(reference, result, errorMask, threshold);
   else
       throw InternalError("Unsupported format for bilinear comparison");
}
 
} // tcu