1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Bilinear image comparison.
22 *//*--------------------------------------------------------------------*/
24 #include "tcuBilinearImageCompare.hpp"
25 #include "tcuTexture.hpp"
26 #include "tcuTextureUtil.hpp"
27 #include "tcuRGBA.hpp"
37 NUM_SUBPIXEL_BITS = 8 //!< Number of subpixel bits used when doing bilinear interpolation.
40 // \note Algorithm assumes that colors are packed to 32-bit values as dictated by
41 // tcu::RGBA::*_SHIFT values.
44 static inline deUint8 getChannel (deUint32 color)
46 return (deUint8)((color >> (Channel*8)) & 0xff);
49 #if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
50 inline deUint32 readRGBA8Raw (const ConstPixelBufferAccess& src, deUint32 x, deUint32 y)
52 return *(const deUint32*)((const deUint8*)src.getDataPtr() + y*src.getRowPitch() + x*4);
55 inline deUint32 readRGBA8Raw (const ConstPixelBufferAccess& src, deUint32 x, deUint32 y)
57 return deReverseBytes32(*(const deUint32*)((const deUint8*)src.getDataPtr() + y*src.getRowPitch() + x*4));
61 inline RGBA readRGBA8 (const ConstPixelBufferAccess& src, deUint32 x, deUint32 y)
63 deUint32 raw = readRGBA8Raw(src, x, y);
66 res |= getChannel<0>(raw) << RGBA::RED_SHIFT;
67 res |= getChannel<1>(raw) << RGBA::GREEN_SHIFT;
68 res |= getChannel<2>(raw) << RGBA::BLUE_SHIFT;
69 res |= getChannel<3>(raw) << RGBA::ALPHA_SHIFT;
74 inline deUint8 interpolateChannel (deUint32 fx1, deUint32 fy1, deUint8 p00, deUint8 p01, deUint8 p10, deUint8 p11)
76 const deUint32 fx0 = (1u<<NUM_SUBPIXEL_BITS) - fx1;
77 const deUint32 fy0 = (1u<<NUM_SUBPIXEL_BITS) - fy1;
78 const deUint32 half = 1u<<(NUM_SUBPIXEL_BITS*2 - 1);
79 const deUint32 sum = fx0*fy0*p00 + fx1*fy0*p10 + fx0*fy1*p01 + fx1*fy1*p11;
80 const deUint32 rounded = (sum + half) >> (NUM_SUBPIXEL_BITS*2);
82 DE_ASSERT(de::inRange<deUint32>(rounded, 0, 0xff));
83 return (deUint8)rounded;
86 RGBA bilinearSampleRGBA8 (const ConstPixelBufferAccess& access, deUint32 u, deUint32 v)
88 deUint32 x0 = u>>NUM_SUBPIXEL_BITS;
89 deUint32 y0 = v>>NUM_SUBPIXEL_BITS;
90 deUint32 x1 = x0+1; //de::min(x0+1, (deUint32)(access.getWidth()-1));
91 deUint32 y1 = y0+1; //de::min(y0+1, (deUint32)(access.getHeight()-1));
93 DE_ASSERT(x1 < (deUint32)access.getWidth());
94 DE_ASSERT(y1 < (deUint32)access.getHeight());
96 deUint32 fx1 = u-(x0<<NUM_SUBPIXEL_BITS);
97 deUint32 fy1 = v-(y0<<NUM_SUBPIXEL_BITS);
99 deUint32 p00 = readRGBA8Raw(access, x0, y0);
100 deUint32 p10 = readRGBA8Raw(access, x1, y0);
101 deUint32 p01 = readRGBA8Raw(access, x0, y1);
102 deUint32 p11 = readRGBA8Raw(access, x1, y1);
106 res |= interpolateChannel(fx1, fy1, getChannel<0>(p00), getChannel<0>(p01), getChannel<0>(p10), getChannel<0>(p11)) << RGBA::RED_SHIFT;
107 res |= interpolateChannel(fx1, fy1, getChannel<1>(p00), getChannel<1>(p01), getChannel<1>(p10), getChannel<1>(p11)) << RGBA::GREEN_SHIFT;
108 res |= interpolateChannel(fx1, fy1, getChannel<2>(p00), getChannel<2>(p01), getChannel<2>(p10), getChannel<2>(p11)) << RGBA::BLUE_SHIFT;
109 res |= interpolateChannel(fx1, fy1, getChannel<3>(p00), getChannel<3>(p01), getChannel<3>(p10), getChannel<3>(p11)) << RGBA::ALPHA_SHIFT;
114 bool comparePixelRGBA8 (const ConstPixelBufferAccess& reference, const ConstPixelBufferAccess& result, const RGBA threshold, int x, int y)
116 const RGBA resPix = readRGBA8(result, (deUint32)x, (deUint32)y);
118 // Step 1: Compare result pixel to 3x3 neighborhood pixels in reference.
120 const deUint32 x0 = (deUint32)de::max(x-1, 0);
121 const deUint32 x1 = (deUint32)x;
122 const deUint32 x2 = (deUint32)de::min(x+1, reference.getWidth()-1);
123 const deUint32 y0 = (deUint32)de::max(y-1, 0);
124 const deUint32 y1 = (deUint32)y;
125 const deUint32 y2 = (deUint32)de::min(y+1, reference.getHeight()-1);
127 if (compareThreshold(resPix, readRGBA8(reference, x1, y1), threshold) ||
128 compareThreshold(resPix, readRGBA8(reference, x0, y1), threshold) ||
129 compareThreshold(resPix, readRGBA8(reference, x2, y1), threshold) ||
130 compareThreshold(resPix, readRGBA8(reference, x0, y0), threshold) ||
131 compareThreshold(resPix, readRGBA8(reference, x1, y0), threshold) ||
132 compareThreshold(resPix, readRGBA8(reference, x2, y0), threshold) ||
133 compareThreshold(resPix, readRGBA8(reference, x0, y2), threshold) ||
134 compareThreshold(resPix, readRGBA8(reference, x1, y2), threshold) ||
135 compareThreshold(resPix, readRGBA8(reference, x2, y2), threshold))
139 // Step 2: Compare using bilinear sampling.
141 // \todo [pyry] Optimize sample positions!
142 static const deUint32 s_offsets[][2] =
174 for (int sampleNdx = 0; sampleNdx < DE_LENGTH_OF_ARRAY(s_offsets); sampleNdx++)
176 const int u = (x<<NUM_SUBPIXEL_BITS) + (int)s_offsets[sampleNdx][0] - (1<<NUM_SUBPIXEL_BITS);
177 const int v = (y<<NUM_SUBPIXEL_BITS) + (int)s_offsets[sampleNdx][1] - (1<<NUM_SUBPIXEL_BITS);
179 if (!de::inBounds(u, 0, (reference.getWidth()-1)<<NUM_SUBPIXEL_BITS) ||
180 !de::inBounds(v, 0, (reference.getHeight()-1)<<NUM_SUBPIXEL_BITS))
183 if (compareThreshold(resPix, bilinearSampleRGBA8(reference, (deUint32)u, (deUint32)v), threshold))
191 bool bilinearCompareRGBA8 (const ConstPixelBufferAccess& reference, const ConstPixelBufferAccess& result, const PixelBufferAccess& errorMask, const RGBA threshold)
193 DE_ASSERT(reference.getFormat() == TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8) &&
194 result.getFormat() == TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8));
196 // Clear error mask first to green (faster this way).
197 clear(errorMask, Vec4(0.0f, 1.0f, 0.0f, 1.0f));
201 for (int y = 0; y < reference.getHeight(); y++)
203 for (int x = 0; x < reference.getWidth(); x++)
205 if (!comparePixelRGBA8(reference, result, threshold, x, y) &&
206 !comparePixelRGBA8(result, reference, threshold, x, y))
209 errorMask.setPixel(Vec4(1.0f, 0.0f, 0.0f, 1.0f), x, y);
219 bool bilinearCompare (const ConstPixelBufferAccess& reference, const ConstPixelBufferAccess& result, const PixelBufferAccess& errorMask, const RGBA threshold)
221 DE_ASSERT(reference.getWidth() == result.getWidth() &&
222 reference.getHeight() == result.getHeight() &&
223 reference.getDepth() == result.getDepth() &&
224 reference.getFormat() == result.getFormat());
225 DE_ASSERT(reference.getWidth() == errorMask.getWidth() &&
226 reference.getHeight() == errorMask.getHeight() &&
227 reference.getDepth() == errorMask.getDepth());
229 if (reference.getFormat() == TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8))
230 return bilinearCompareRGBA8(reference, result, errorMask, threshold);
232 throw InternalError("Unsupported format for bilinear comparison");