1 #ifndef _TCUPIXELFORMAT_HPP
2 #define _TCUPIXELFORMAT_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements Quality Program Tester Core
5  * ----------------------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Pixel format descriptor.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "tcuDefs.hpp"
27 #include "tcuRGBA.hpp"
28 
29 namespace tcu
30 {
31 
32 /*--------------------------------------------------------------------*//*!
33  * \brief Fixed-point render target pixel format
34  *//*--------------------------------------------------------------------*/
35 struct PixelFormat
36 {
37 	int redBits;
38 	int greenBits;
39 	int blueBits;
40 	int alphaBits;
41 
PixelFormattcu::PixelFormat42 	PixelFormat (int red, int green, int blue, int alpha)
43 		: redBits(red)
44 		, greenBits(green)
45 		, blueBits(blue)
46 		, alphaBits(alpha)
47 	{
48 	}
49 
PixelFormattcu::PixelFormat50 	PixelFormat (void)
51 		: redBits(0)
52 		, greenBits(0)
53 		, blueBits(0)
54 		, alphaBits(0)
55 	{
56 	}
57 
channelThresholdtcu::PixelFormat58 	static inline int channelThreshold(int bits)
59 	{
60 		if (bits <= 8)
61 		{
62 			// Threshold is 2^(8 - bits)
63 			return 1 << (8 - bits);
64 		}
65 		else
66 		{
67 			// Threshold is bound by the 8-bit buffer value
68 			return 1;
69 		}
70 	}
71 
72 	/*--------------------------------------------------------------------*//*!
73 	 * \brief Get default threshold for per-pixel comparison for this format
74 	 *
75 	 * Per-channel threshold is 2^(8-bits). If alpha channel bits are zero,
76 	 * threshold for that channel is 0.
77 	 *//*--------------------------------------------------------------------*/
getColorThresholdtcu::PixelFormat78 	inline RGBA getColorThreshold (void) const
79 	{
80 		return RGBA(
81 			channelThreshold(redBits),
82 			channelThreshold(greenBits),
83 			channelThreshold(blueBits),
84 			alphaBits ? channelThreshold(alphaBits) : 0);
85 	}
86 
convertChanneltcu::PixelFormat87 	static inline int convertChannel (int val, int bits)
88 	{
89 		if (bits == 0)
90 		{
91 			return 0;
92 		}
93 		else if (bits == 1)
94 		{
95 			return (val & 0x80) ? 0xff : 0;
96 		}
97 		else if (bits < 8)
98 		{
99 			// Emulate precision reduction by replicating the upper bits as the fractional component
100 			int intComp   = val >> (8 - bits);
101 			int fractComp = (intComp << (24 - bits)) | (intComp << (24 - 2 * bits)) | (intComp << (24 - 3 * bits));
102 			return (intComp << (8 - bits)) | (fractComp >> (bits + 16));
103 		}
104 		else
105 		{
106 			// Bits greater than or equal to 8 will have full precision, so no reduction
107 			return val;
108 		}
109 	}
110 
111 	/*--------------------------------------------------------------------*//*!
112 	 * \brief Emulate reduced bit depth
113 	 *
114 	 * The color value bit depth is reduced and converted back. The lowest
115 	 * bits are filled by replicating the upper bits.
116 	 *//*--------------------------------------------------------------------*/
convertColortcu::PixelFormat117 	inline RGBA convertColor (const RGBA& col) const
118 	{
119 		return RGBA(convertChannel(col.getRed(),	redBits),
120 					convertChannel(col.getGreen(),	greenBits),
121 					convertChannel(col.getBlue(),	blueBits),
122 					alphaBits ? convertChannel(col.getAlpha(), alphaBits) : 0xff);
123 	}
124 
operator ==tcu::PixelFormat125 	inline bool operator== (const PixelFormat& other) const
126 	{
127 		return redBits		== other.redBits	&&
128 			   greenBits	== other.greenBits	&&
129 			   blueBits		== other.blueBits	&&
130 			   alphaBits	== other.alphaBits;
131 	}
132 
operator !=tcu::PixelFormat133 	inline bool operator!= (const PixelFormat& other) const
134 	{
135 		return !(*this == other);
136 	}
137 } DE_WARN_UNUSED_TYPE;
138 
139 } // namespace tcu
140 
141 #endif // _TCUPIXELFORMAT_HPP
142