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
#ifndef _TCURGBA_HPP
#define _TCURGBA_HPP
/*-------------------------------------------------------------------------
 * 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 RGBA8888 color type.
 *//*--------------------------------------------------------------------*/
 
#include "tcuDefs.hpp"
#include "deInt32.h"
#include "tcuVectorType.hpp"
 
#include <sstream>
 
namespace tcu
{
 
/*--------------------------------------------------------------------*//*!
 * \brief RGBA8888 color struct
 *//*--------------------------------------------------------------------*/
class RGBA
{
public:
   enum
   {
       RED_SHIFT    = 0,
       GREEN_SHIFT    = 8,
       BLUE_SHIFT    = 16,
       ALPHA_SHIFT    = 24
   };
 
   enum
   {
       RED_MASK    = (1<<0),
       GREEN_MASK    = (1<<1),
       BLUE_MASK    = (1<<2),
       ALPHA_MASK    = (1<<3)
   };
 
   RGBA (void) { m_value = 0; }
 
   RGBA (int r, int g, int b, int a)
   {
       DE_ASSERT(deInRange32(r, 0, 255));
       DE_ASSERT(deInRange32(g, 0, 255));
       DE_ASSERT(deInRange32(b, 0, 255));
       DE_ASSERT(deInRange32(a, 0, 255));
       m_value = ((deUint32)a << ALPHA_SHIFT) | ((deUint32)r << RED_SHIFT) | ((deUint32)g << GREEN_SHIFT) | ((deUint32)b << BLUE_SHIFT);
   }
 
   explicit RGBA (deUint32 val)
   {
       m_value = val;
   }
 
   explicit    RGBA                    (const Vec4& v);
 
   void        setRed                    (int v) { DE_ASSERT(deInRange32(v, 0, 255)); m_value = (m_value & ~((deUint32)0xFFu << RED_SHIFT))   | ((deUint32)v << RED_SHIFT);   }
   void        setGreen                (int v) { DE_ASSERT(deInRange32(v, 0, 255)); m_value = (m_value & ~((deUint32)0xFFu << GREEN_SHIFT)) | ((deUint32)v << GREEN_SHIFT); }
   void        setBlue                    (int v) { DE_ASSERT(deInRange32(v, 0, 255)); m_value = (m_value & ~((deUint32)0xFFu << BLUE_SHIFT))  | ((deUint32)v << BLUE_SHIFT);  }
   void        setAlpha                (int v) { DE_ASSERT(deInRange32(v, 0, 255)); m_value = (m_value & ~((deUint32)0xFFu << ALPHA_SHIFT)) | ((deUint32)v << ALPHA_SHIFT); }
   int            getRed                    (void) const { return (int)((m_value >> (deUint32)RED_SHIFT)   & 0xFFu); }
   int            getGreen                (void) const { return (int)((m_value >> (deUint32)GREEN_SHIFT) & 0xFFu); }
   int            getBlue                    (void) const { return (int)((m_value >> (deUint32)BLUE_SHIFT)  & 0xFFu); }
   int            getAlpha                (void) const { return (int)((m_value >> (deUint32)ALPHA_SHIFT) & 0xFFu); }
   deUint32    getPacked                (void) const { return m_value; }
 
   bool        isBelowThreshold        (RGBA thr) const    { return (getRed() <= thr.getRed()) && (getGreen() <= thr.getGreen()) && (getBlue() <= thr.getBlue()) && (getAlpha() <= thr.getAlpha()); }
 
   static RGBA    fromBytes                (const deUint8* bytes)    { return RGBA(bytes[0], bytes[1], bytes[2], bytes[3]); }
   void        toBytes                    (deUint8* bytes) const    { bytes[0] = (deUint8)getRed(); bytes[1] = (deUint8)getGreen(); bytes[2] = (deUint8)getBlue(); bytes[3] = (deUint8)getAlpha(); }
   Vec4        toVec                    (void) const;
   IVec4        toIVec                    (void) const;
 
   bool        operator==                (const RGBA& v) const { return (m_value == v.m_value); }
   bool        operator!=                (const RGBA& v) const { return (m_value != v.m_value); }
 
   // Color constants.  Designed as methods to avoid static-initialization-order fiasco.
   static inline const RGBA red    (void) { return RGBA(0xFF, 0x0,  0x0,  0xFF); }
   static inline const RGBA green    (void) { return RGBA(0x0,  0xFF, 0x0,  0xFF); }
   static inline const RGBA blue    (void) { return RGBA(0x0,  0x0,  0xFF, 0xFF); }
   static inline const RGBA gray    (void) { return RGBA(0x80, 0x80, 0x80, 0xFF); }
   static inline const RGBA white    (void) { return RGBA(0xFF, 0xFF, 0xFF, 0xFF); }
   static inline const RGBA black    (void) { return RGBA(0x0,  0x0,  0x0,  0xFF); }
 
private:
   deUint32    m_value;
} DE_WARN_UNUSED_TYPE;
 
inline bool compareEqualMasked (RGBA a, RGBA b, deUint32 cmpMask)
{
   RGBA        mask((cmpMask&RGBA::RED_MASK)?0xFF:0, (cmpMask&RGBA::GREEN_MASK)?0xFF:0, (cmpMask&RGBA::BLUE_MASK)?0xFF:0, (cmpMask&RGBA::ALPHA_MASK)?0xFF:0);
   deUint32    aPacked        = a.getPacked();
   deUint32    bPacked        = b.getPacked();
   deUint32    maskPacked    = mask.getPacked();
   return (aPacked & maskPacked) == (bPacked & maskPacked);
}
 
inline RGBA computeAbsDiff (RGBA a, RGBA b)
{
   return RGBA(
       deAbs32(a.getRed()   - b.getRed()),
       deAbs32(a.getGreen() - b.getGreen()),
       deAbs32(a.getBlue()  - b.getBlue()),
       deAbs32(a.getAlpha() - b.getAlpha()));
}
 
inline RGBA blend (RGBA a, RGBA b, float t)
{
   DE_ASSERT(t >= 0.0f && t <= 1.0f);
   float it = 1.0f - t;
   // \todo [petri] Handling of alpha!
   return RGBA(
       (int)(it*(float)a.getRed() + t*(float)b.getRed() + 0.5f),
       (int)(it*(float)a.getGreen() + t*(float)b.getGreen() + 0.5f),
       (int)(it*(float)a.getBlue() + t*(float)b.getBlue() + 0.5f),
       (int)(it*(float)a.getAlpha() + t*(float)b.getAlpha() + 0.5f));
}
 
inline bool compareThreshold (RGBA a, RGBA b, RGBA threshold)
{
   if (a == b) return true;    // Quick-accept
   return computeAbsDiff(a, b).isBelowThreshold(threshold);
}
 
inline RGBA max (RGBA a, RGBA b)
{
   return RGBA(deMax32(a.getRed(),        b.getRed()),
               deMax32(a.getGreen(),    b.getGreen()),
               deMax32(a.getBlue(),    b.getBlue()),
               deMax32(a.getAlpha(),    b.getAlpha()));
}
 
RGBA computeAbsDiffMasked    (RGBA a, RGBA b, deUint32 cmpMask);
bool compareThresholdMasked    (RGBA a, RGBA b, RGBA threshold, deUint32 cmpMask);
 
// Arithmetic operators (saturating if not stated otherwise).
 
inline RGBA operator+ (const RGBA& a, const RGBA& b)
{
   return RGBA(deClamp32(a.getRed()    + b.getRed(),    0, 255),
               deClamp32(a.getGreen()    + b.getGreen(),    0, 255),
               deClamp32(a.getBlue()    + b.getBlue(),    0, 255),
               deClamp32(a.getAlpha()    + b.getAlpha(),    0, 255));
}
 
inline RGBA operator- (const RGBA& a, const RGBA& b)
{
   return RGBA(deClamp32(a.getRed()    - b.getRed(),    0, 255),
               deClamp32(a.getGreen()    - b.getGreen(),    0, 255),
               deClamp32(a.getBlue()    - b.getBlue(),    0, 255),
               deClamp32(a.getAlpha()    - b.getAlpha(),    0, 255));
}
 
inline RGBA operator* (const RGBA& a, const int b)
{
   return RGBA(deClamp32(a.getRed()    * b,    0, 255),
               deClamp32(a.getGreen()    * b,    0, 255),
               deClamp32(a.getBlue()    * b,    0, 255),
               deClamp32(a.getAlpha()    * b,    0, 255));
}
 
inline std::ostream& operator<< (std::ostream& stream, RGBA c)
{
   return stream << "RGBA(" << c.getRed() << ", " << c.getGreen() << ", " << c.getBlue() << ", " << c.getAlpha() << ")";
}
 
} // tcu
 
#endif // _TCURGBA_HPP