| 1 |
/* |
| 2 |
* Copyright 2015 The Etc2Comp Authors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#pragma once |
| 18 |
|
| 19 |
#include "EtcConfig.h" |
| 20 |
#include "EtcColor.h" |
| 21 |
|
| 22 |
#include <math.h> |
| 23 |
|
| 24 |
namespace Etc |
| 25 |
{ |
| 26 |
|
| 27 |
class ColorFloatRGBA |
| 28 |
{ |
| 29 |
public: |
| 30 |
|
| 31 |
ColorFloatRGBA(void) |
| 32 |
{ |
| 33 |
fR = fG = fB = fA = 0.0f; |
| 34 |
} |
| 35 |
|
| 36 |
ColorFloatRGBA(float a_fR, float a_fG, float a_fB, float a_fA) |
| 37 |
{ |
| 38 |
fR = a_fR; |
| 39 |
fG = a_fG; |
| 40 |
fB = a_fB; |
| 41 |
fA = a_fA; |
| 42 |
} |
| 43 |
|
| 44 |
inline ColorFloatRGBA operator+(ColorFloatRGBA& a_rfrgba) |
| 45 |
{ |
| 46 |
ColorFloatRGBA frgba; |
| 47 |
frgba.fR = fR + a_rfrgba.fR; |
| 48 |
frgba.fG = fG + a_rfrgba.fG; |
| 49 |
frgba.fB = fB + a_rfrgba.fB; |
| 50 |
frgba.fA = fA + a_rfrgba.fA; |
| 51 |
return frgba; |
| 52 |
} |
| 53 |
|
| 54 |
inline ColorFloatRGBA operator+(float a_f) |
| 55 |
{ |
| 56 |
ColorFloatRGBA frgba; |
| 57 |
frgba.fR = fR + a_f; |
| 58 |
frgba.fG = fG + a_f; |
| 59 |
frgba.fB = fB + a_f; |
| 60 |
frgba.fA = fA; |
| 61 |
return frgba; |
| 62 |
} |
| 63 |
|
| 64 |
inline ColorFloatRGBA operator-(float a_f) |
| 65 |
{ |
| 66 |
ColorFloatRGBA frgba; |
| 67 |
frgba.fR = fR - a_f; |
| 68 |
frgba.fG = fG - a_f; |
| 69 |
frgba.fB = fB - a_f; |
| 70 |
frgba.fA = fA; |
| 71 |
return frgba; |
| 72 |
} |
| 73 |
|
| 74 |
inline ColorFloatRGBA operator-(ColorFloatRGBA& a_rfrgba) |
| 75 |
{ |
| 76 |
ColorFloatRGBA frgba; |
| 77 |
frgba.fR = fR - a_rfrgba.fR; |
| 78 |
frgba.fG = fG - a_rfrgba.fG; |
| 79 |
frgba.fB = fB - a_rfrgba.fB; |
| 80 |
frgba.fA = fA - a_rfrgba.fA; |
| 81 |
return frgba; |
| 82 |
} |
| 83 |
|
| 84 |
inline ColorFloatRGBA operator*(float a_f) |
| 85 |
{ |
| 86 |
ColorFloatRGBA frgba; |
| 87 |
frgba.fR = fR * a_f; |
| 88 |
frgba.fG = fG * a_f; |
| 89 |
frgba.fB = fB * a_f; |
| 90 |
frgba.fA = fA; |
| 91 |
|
| 92 |
return frgba; |
| 93 |
} |
| 94 |
|
| 95 |
inline ColorFloatRGBA ScaleRGB(float a_f) |
| 96 |
{ |
| 97 |
ColorFloatRGBA frgba; |
| 98 |
frgba.fR = a_f * fR; |
| 99 |
frgba.fG = a_f * fG; |
| 100 |
frgba.fB = a_f * fB; |
| 101 |
frgba.fA = fA; |
| 102 |
|
| 103 |
return frgba; |
| 104 |
} |
| 105 |
|
| 106 |
inline ColorFloatRGBA RoundRGB(void) |
| 107 |
{ |
| 108 |
ColorFloatRGBA frgba; |
| 109 |
frgba.fR = roundf(fR); |
| 110 |
frgba.fG = roundf(fG); |
| 111 |
frgba.fB = roundf(fB); |
| 112 |
|
| 113 |
return frgba; |
| 114 |
} |
| 115 |
|
| 116 |
inline ColorFloatRGBA ToLinear() |
| 117 |
{ |
| 118 |
ColorFloatRGBA frgbaLinear; |
| 119 |
frgbaLinear.fR = LogToLinear(fR); |
| 120 |
frgbaLinear.fG = LogToLinear(fG); |
| 121 |
frgbaLinear.fB = LogToLinear(fB); |
| 122 |
frgbaLinear.fA = fA; |
| 123 |
|
| 124 |
return frgbaLinear; |
| 125 |
} |
| 126 |
|
| 127 |
inline ColorFloatRGBA ToLog(void) |
| 128 |
{ |
| 129 |
ColorFloatRGBA frgbaLog; |
| 130 |
frgbaLog.fR = LinearToLog(fR); |
| 131 |
frgbaLog.fG = LinearToLog(fG); |
| 132 |
frgbaLog.fB = LinearToLog(fB); |
| 133 |
frgbaLog.fA = fA; |
| 134 |
|
| 135 |
return frgbaLog; |
| 136 |
} |
| 137 |
|
| 138 |
inline static ColorFloatRGBA ConvertFromRGBA8(unsigned char a_ucR, |
| 139 |
unsigned char a_ucG, unsigned char a_ucB, unsigned char a_ucA) |
| 140 |
{ |
| 141 |
ColorFloatRGBA frgba; |
| 142 |
|
| 143 |
frgba.fR = (float)a_ucR / 255.0f; |
| 144 |
frgba.fG = (float)a_ucG / 255.0f; |
| 145 |
frgba.fB = (float)a_ucB / 255.0f; |
| 146 |
frgba.fA = (float)a_ucA / 255.0f; |
| 147 |
|
| 148 |
return frgba; |
| 149 |
} |
| 150 |
|
| 151 |
inline static ColorFloatRGBA ConvertFromRGB4(unsigned char a_ucR4, |
| 152 |
unsigned char a_ucG4, |
| 153 |
unsigned char a_ucB4) |
| 154 |
{ |
| 155 |
ColorFloatRGBA frgba; |
| 156 |
|
| 157 |
unsigned char ucR8 = (unsigned char)((a_ucR4 << 4) + a_ucR4); |
| 158 |
unsigned char ucG8 = (unsigned char)((a_ucG4 << 4) + a_ucG4); |
| 159 |
unsigned char ucB8 = (unsigned char)((a_ucB4 << 4) + a_ucB4); |
| 160 |
|
| 161 |
frgba.fR = (float)ucR8 / 255.0f; |
| 162 |
frgba.fG = (float)ucG8 / 255.0f; |
| 163 |
frgba.fB = (float)ucB8 / 255.0f; |
| 164 |
frgba.fA = 1.0f; |
| 165 |
|
| 166 |
return frgba; |
| 167 |
} |
| 168 |
|
| 169 |
inline static ColorFloatRGBA ConvertFromRGB5(unsigned char a_ucR5, |
| 170 |
unsigned char a_ucG5, |
| 171 |
unsigned char a_ucB5) |
| 172 |
{ |
| 173 |
ColorFloatRGBA frgba; |
| 174 |
|
| 175 |
unsigned char ucR8 = (unsigned char)((a_ucR5 << 3) + (a_ucR5 >> 2)); |
| 176 |
unsigned char ucG8 = (unsigned char)((a_ucG5 << 3) + (a_ucG5 >> 2)); |
| 177 |
unsigned char ucB8 = (unsigned char)((a_ucB5 << 3) + (a_ucB5 >> 2)); |
| 178 |
|
| 179 |
frgba.fR = (float)ucR8 / 255.0f; |
| 180 |
frgba.fG = (float)ucG8 / 255.0f; |
| 181 |
frgba.fB = (float)ucB8 / 255.0f; |
| 182 |
frgba.fA = 1.0f; |
| 183 |
|
| 184 |
return frgba; |
| 185 |
} |
| 186 |
|
| 187 |
inline static ColorFloatRGBA ConvertFromR6G7B6(unsigned char a_ucR6, |
| 188 |
unsigned char a_ucG7, |
| 189 |
unsigned char a_ucB6) |
| 190 |
{ |
| 191 |
ColorFloatRGBA frgba; |
| 192 |
|
| 193 |
unsigned char ucR8 = (unsigned char)((a_ucR6 << 2) + (a_ucR6 >> 4)); |
| 194 |
unsigned char ucG8 = (unsigned char)((a_ucG7 << 1) + (a_ucG7 >> 6)); |
| 195 |
unsigned char ucB8 = (unsigned char)((a_ucB6 << 2) + (a_ucB6 >> 4)); |
| 196 |
|
| 197 |
frgba.fR = (float)ucR8 / 255.0f; |
| 198 |
frgba.fG = (float)ucG8 / 255.0f; |
| 199 |
frgba.fB = (float)ucB8 / 255.0f; |
| 200 |
frgba.fA = 1.0f; |
| 201 |
|
| 202 |
return frgba; |
| 203 |
} |
| 204 |
|
| 205 |
// quantize to 4 bits, expand to 8 bits |
| 206 |
inline ColorFloatRGBA QuantizeR4G4B4(void) const |
| 207 |
{ |
| 208 |
ColorFloatRGBA frgba = *this; |
| 209 |
|
| 210 |
// quantize to 4 bits |
| 211 |
frgba = frgba.ClampRGB().ScaleRGB(15.0f).RoundRGB(); |
| 212 |
unsigned int uiR4 = (unsigned int)frgba.fR; |
| 213 |
unsigned int uiG4 = (unsigned int)frgba.fG; |
| 214 |
unsigned int uiB4 = (unsigned int)frgba.fB; |
| 215 |
|
| 216 |
// expand to 8 bits |
| 217 |
frgba.fR = (float) ((uiR4 << 4) + uiR4); |
| 218 |
frgba.fG = (float) ((uiG4 << 4) + uiG4); |
| 219 |
frgba.fB = (float) ((uiB4 << 4) + uiB4); |
| 220 |
|
| 221 |
frgba = frgba.ScaleRGB(1.0f/255.0f); |
| 222 |
|
| 223 |
return frgba; |
| 224 |
} |
| 225 |
|
| 226 |
// quantize to 5 bits, expand to 8 bits |
| 227 |
inline ColorFloatRGBA QuantizeR5G5B5(void) const |
| 228 |
{ |
| 229 |
ColorFloatRGBA frgba = *this; |
| 230 |
|
| 231 |
// quantize to 5 bits |
| 232 |
frgba = frgba.ClampRGB().ScaleRGB(31.0f).RoundRGB(); |
| 233 |
unsigned int uiR5 = (unsigned int)frgba.fR; |
| 234 |
unsigned int uiG5 = (unsigned int)frgba.fG; |
| 235 |
unsigned int uiB5 = (unsigned int)frgba.fB; |
| 236 |
|
| 237 |
// expand to 8 bits |
| 238 |
frgba.fR = (float)((uiR5 << 3) + (uiR5 >> 2)); |
| 239 |
frgba.fG = (float)((uiG5 << 3) + (uiG5 >> 2)); |
| 240 |
frgba.fB = (float)((uiB5 << 3) + (uiB5 >> 2)); |
| 241 |
|
| 242 |
frgba = frgba.ScaleRGB(1.0f / 255.0f); |
| 243 |
|
| 244 |
return frgba; |
| 245 |
} |
| 246 |
|
| 247 |
// quantize to 6/7/6 bits, expand to 8 bits |
| 248 |
inline ColorFloatRGBA QuantizeR6G7B6(void) const |
| 249 |
{ |
| 250 |
ColorFloatRGBA frgba = *this; |
| 251 |
|
| 252 |
// quantize to 6/7/6 bits |
| 253 |
ColorFloatRGBA frgba6 = frgba.ClampRGB().ScaleRGB(63.0f).RoundRGB(); |
| 254 |
ColorFloatRGBA frgba7 = frgba.ClampRGB().ScaleRGB(127.0f).RoundRGB(); |
| 255 |
unsigned int uiR6 = (unsigned int)frgba6.fR; |
| 256 |
unsigned int uiG7 = (unsigned int)frgba7.fG; |
| 257 |
unsigned int uiB6 = (unsigned int)frgba6.fB; |
| 258 |
|
| 259 |
// expand to 8 bits |
| 260 |
frgba.fR = (float)((uiR6 << 2) + (uiR6 >> 4)); |
| 261 |
frgba.fG = (float)((uiG7 << 1) + (uiG7 >> 6)); |
| 262 |
frgba.fB = (float)((uiB6 << 2) + (uiB6 >> 4)); |
| 263 |
|
| 264 |
frgba = frgba.ScaleRGB(1.0f / 255.0f); |
| 265 |
|
| 266 |
return frgba; |
| 267 |
} |
| 268 |
|
| 269 |
inline ColorFloatRGBA ClampRGB(void) |
| 270 |
{ |
| 271 |
ColorFloatRGBA frgba = *this; |
| 272 |
if (frgba.fR < 0.0f) { frgba.fR = 0.0f; } |
| 273 |
if (frgba.fR > 1.0f) { frgba.fR = 1.0f; } |
| 274 |
if (frgba.fG < 0.0f) { frgba.fG = 0.0f; } |
| 275 |
if (frgba.fG > 1.0f) { frgba.fG = 1.0f; } |
| 276 |
if (frgba.fB < 0.0f) { frgba.fB = 0.0f; } |
| 277 |
if (frgba.fB > 1.0f) { frgba.fB = 1.0f; } |
| 278 |
|
| 279 |
return frgba; |
| 280 |
} |
| 281 |
|
| 282 |
inline ColorFloatRGBA ClampRGBA(void) |
| 283 |
{ |
| 284 |
ColorFloatRGBA frgba = *this; |
| 285 |
if (frgba.fR < 0.0f) { frgba.fR = 0.0f; } |
| 286 |
if (frgba.fR > 1.0f) { frgba.fR = 1.0f; } |
| 287 |
if (frgba.fG < 0.0f) { frgba.fG = 0.0f; } |
| 288 |
if (frgba.fG > 1.0f) { frgba.fG = 1.0f; } |
| 289 |
if (frgba.fB < 0.0f) { frgba.fB = 0.0f; } |
| 290 |
if (frgba.fB > 1.0f) { frgba.fB = 1.0f; } |
| 291 |
if (frgba.fA < 0.0f) { frgba.fA = 0.0f; } |
| 292 |
if (frgba.fA > 1.0f) { frgba.fA = 1.0f; } |
| 293 |
|
| 294 |
return frgba; |
| 295 |
} |
| 296 |
|
| 297 |
inline int IntRed(float a_fScale) |
| 298 |
{ |
| 299 |
return (int)roundf(fR * a_fScale); |
| 300 |
} |
| 301 |
|
| 302 |
inline int IntGreen(float a_fScale) |
| 303 |
{ |
| 304 |
return (int)roundf(fG * a_fScale); |
| 305 |
} |
| 306 |
|
| 307 |
inline int IntBlue(float a_fScale) |
| 308 |
{ |
| 309 |
return (int)roundf(fB * a_fScale); |
| 310 |
} |
| 311 |
|
| 312 |
inline int IntAlpha(float a_fScale) |
| 313 |
{ |
| 314 |
return (int)roundf(fA * a_fScale); |
| 315 |
} |
| 316 |
|
| 317 |
float fR, fG, fB, fA; |
| 318 |
}; |
| 319 |
|
| 320 |
} |
| 321 |
|