summaryrefslogtreecommitdiff
path: root/src/static_libs/rg_etc
diff options
context:
space:
mode:
authorCedric BAIL <cedric.bail@samsung.com>2014-02-07 17:44:01 +0900
committerCedric BAIL <cedric.bail@free.fr>2014-04-01 22:00:13 +0900
commit2cac84f56794bfdff45002099f6c9d95376eff8a (patch)
tree6d882a87ea3224f528cc6ffb17ee9d155578a4f9 /src/static_libs/rg_etc
parent03b29127f1bf27ab38147308c5615c35c4ee5b65 (diff)
rg_etc: convert to Unix file format.
Diffstat (limited to 'src/static_libs/rg_etc')
-rw-r--r--src/static_libs/rg_etc/rg_etc1.cpp4892
-rw-r--r--src/static_libs/rg_etc/rg_etc1.h152
2 files changed, 2522 insertions, 2522 deletions
diff --git a/src/static_libs/rg_etc/rg_etc1.cpp b/src/static_libs/rg_etc/rg_etc1.cpp
index a1cd73f546..8e28b53f9d 100644
--- a/src/static_libs/rg_etc/rg_etc1.cpp
+++ b/src/static_libs/rg_etc/rg_etc1.cpp
@@ -1,2446 +1,2446 @@
1// File: rg_etc1.cpp - Fast, high quality ETC1 block packer/unpacker - Rich Geldreich <richgel99@gmail.com> 1// File: rg_etc1.cpp - Fast, high quality ETC1 block packer/unpacker - Rich Geldreich <richgel99@gmail.com>
2// Please see ZLIB license at the end of rg_etc1.h. 2// Please see ZLIB license at the end of rg_etc1.h.
3// 3//
4// For more information Ericsson Texture Compression (ETC/ETC1), see: 4// For more information Ericsson Texture Compression (ETC/ETC1), see:
5// http://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8_texture.txt 5// http://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8_texture.txt
6// 6//
7// v1.04 - 5/15/14 - Fix signed vs. unsigned subtraction problem (noticed when compiled with gcc) in pack_etc1_block_init(). 7// v1.04 - 5/15/14 - Fix signed vs. unsigned subtraction problem (noticed when compiled with gcc) in pack_etc1_block_init().
8// This issue would cause an assert when this func. was called in debug. (Note this module was developed/testing with MSVC, 8// This issue would cause an assert when this func. was called in debug. (Note this module was developed/testing with MSVC,
9// I still need to test it throughly when compiled with gcc.) 9// I still need to test it throughly when compiled with gcc.)
10// 10//
11// v1.03 - 5/12/13 - Initial public release 11// v1.03 - 5/12/13 - Initial public release
12#include "rg_etc1.h" 12#include "rg_etc1.h"
13 13
14#include <stdlib.h> 14#include <stdlib.h>
15#include <memory.h> 15#include <memory.h>
16#include <assert.h> 16#include <assert.h>
17//#include <stdio.h> 17//#include <stdio.h>
18#include <math.h> 18#include <math.h>
19 19
20#pragma warning (disable: 4201) // nonstandard extension used : nameless struct/union 20#pragma warning (disable: 4201) // nonstandard extension used : nameless struct/union
21 21
22#if defined(_DEBUG) || defined(DEBUG) 22#if defined(_DEBUG) || defined(DEBUG)
23#define RG_ETC1_BUILD_DEBUG 23#define RG_ETC1_BUILD_DEBUG
24#endif 24#endif
25 25
26#define RG_ETC1_ASSERT assert 26#define RG_ETC1_ASSERT assert
27 27
28namespace rg_etc1 28namespace rg_etc1
29{ 29{
30 typedef unsigned char uint8; 30 typedef unsigned char uint8;
31 typedef unsigned short uint16; 31 typedef unsigned short uint16;
32 typedef unsigned int uint; 32 typedef unsigned int uint;
33 typedef unsigned int uint32; 33 typedef unsigned int uint32;
34 typedef long long int64; 34 typedef long long int64;
35 typedef unsigned long long uint64; 35 typedef unsigned long long uint64;
36 36
37 const uint32 cUINT32_MAX = 0xFFFFFFFFU; 37 const uint32 cUINT32_MAX = 0xFFFFFFFFU;
38 const uint64 cUINT64_MAX = 0xFFFFFFFFFFFFFFFFULL; //0xFFFFFFFFFFFFFFFFui64; 38 const uint64 cUINT64_MAX = 0xFFFFFFFFFFFFFFFFULL; //0xFFFFFFFFFFFFFFFFui64;
39 39
40 template<typename T> inline T minimum(T a, T b) { return (a < b) ? a : b; } 40 template<typename T> inline T minimum(T a, T b) { return (a < b) ? a : b; }
41 template<typename T> inline T minimum(T a, T b, T c) { return minimum(minimum(a, b), c); } 41 template<typename T> inline T minimum(T a, T b, T c) { return minimum(minimum(a, b), c); }
42 template<typename T> inline T maximum(T a, T b) { return (a > b) ? a : b; } 42 template<typename T> inline T maximum(T a, T b) { return (a > b) ? a : b; }
43 template<typename T> inline T maximum(T a, T b, T c) { return maximum(maximum(a, b), c); } 43 template<typename T> inline T maximum(T a, T b, T c) { return maximum(maximum(a, b), c); }
44 template<typename T> inline T clamp(T value, T low, T high) { return (value < low) ? low : ((value > high) ? high : value); } 44 template<typename T> inline T clamp(T value, T low, T high) { return (value < low) ? low : ((value > high) ? high : value); }
45 template<typename T> inline T square(T value) { return value * value; } 45 template<typename T> inline T square(T value) { return value * value; }
46 template<typename T> inline void zero_object(T& obj) { memset((void*)&obj, 0, sizeof(obj)); } 46 template<typename T> inline void zero_object(T& obj) { memset((void*)&obj, 0, sizeof(obj)); }
47 template<typename T> inline void zero_this(T* pObj) { memset((void*)pObj, 0, sizeof(*pObj)); } 47 template<typename T> inline void zero_this(T* pObj) { memset((void*)pObj, 0, sizeof(*pObj)); }
48 48
49 template<class T, size_t N> T decay_array_to_subtype(T (&a)[N]); 49 template<class T, size_t N> T decay_array_to_subtype(T (&a)[N]);
50 50
51#define RG_ETC1_ARRAY_SIZE(X) (sizeof(X) / sizeof(decay_array_to_subtype(X))) 51#define RG_ETC1_ARRAY_SIZE(X) (sizeof(X) / sizeof(decay_array_to_subtype(X)))
52 52
53 enum eNoClamp { cNoClamp }; 53 enum eNoClamp { cNoClamp };
54 54
55 struct color_quad_u8 55 struct color_quad_u8
56 { 56 {
57 static inline int clamp(int v) { if (v & 0xFFFFFF00U) v = (~(static_cast<int>(v) >> 31)) & 0xFF; return v; } 57 static inline int clamp(int v) { if (v & 0xFFFFFF00U) v = (~(static_cast<int>(v) >> 31)) & 0xFF; return v; }
58 58
59 struct component_traits { enum { cSigned = false, cFloat = false, cMin = 0U, cMax = 255U }; }; 59 struct component_traits { enum { cSigned = false, cFloat = false, cMin = 0U, cMax = 255U }; };
60 60
61 public: 61 public:
62 typedef unsigned char component_t; 62 typedef unsigned char component_t;
63 typedef int parameter_t; 63 typedef int parameter_t;
64 64
65 enum { cNumComps = 4 }; 65 enum { cNumComps = 4 };
66 66
67 union 67 union
68 { 68 {
69 struct 69 struct
70 { 70 {
71 component_t r; 71 component_t r;
72 component_t g; 72 component_t g;
73 component_t b; 73 component_t b;
74 component_t a; 74 component_t a;
75 }; 75 };
76 76
77 component_t c[cNumComps]; 77 component_t c[cNumComps];
78 78
79 uint32 m_u32; 79 uint32 m_u32;
80 }; 80 };
81 81
82 inline color_quad_u8() 82 inline color_quad_u8()
83 { 83 {
84 } 84 }
85 85
86 inline color_quad_u8(const color_quad_u8& other) : m_u32(other.m_u32) 86 inline color_quad_u8(const color_quad_u8& other) : m_u32(other.m_u32)
87 { 87 {
88 } 88 }
89 89
90 explicit inline color_quad_u8(parameter_t y, parameter_t alpha = component_traits::cMax) 90 explicit inline color_quad_u8(parameter_t y, parameter_t alpha = component_traits::cMax)
91 { 91 {
92 set(y, alpha); 92 set(y, alpha);
93 } 93 }
94 94
95 inline color_quad_u8(parameter_t red, parameter_t green, parameter_t blue, parameter_t alpha = component_traits::cMax) 95 inline color_quad_u8(parameter_t red, parameter_t green, parameter_t blue, parameter_t alpha = component_traits::cMax)
96 { 96 {
97 set(red, green, blue, alpha); 97 set(red, green, blue, alpha);
98 } 98 }
99 99
100 explicit inline color_quad_u8(eNoClamp, parameter_t y, parameter_t alpha = component_traits::cMax) 100 explicit inline color_quad_u8(eNoClamp, parameter_t y, parameter_t alpha = component_traits::cMax)
101 { 101 {
102 set_noclamp_y_alpha(y, alpha); 102 set_noclamp_y_alpha(y, alpha);
103 } 103 }
104 104
105 inline color_quad_u8(eNoClamp, parameter_t red, parameter_t green, parameter_t blue, parameter_t alpha = component_traits::cMax) 105 inline color_quad_u8(eNoClamp, parameter_t red, parameter_t green, parameter_t blue, parameter_t alpha = component_traits::cMax)
106 { 106 {
107 set_noclamp_rgba(red, green, blue, alpha); 107 set_noclamp_rgba(red, green, blue, alpha);
108 } 108 }
109 109
110 inline void clear() 110 inline void clear()
111 { 111 {
112 m_u32 = 0; 112 m_u32 = 0;
113 } 113 }
114 114
115 inline color_quad_u8& operator= (const color_quad_u8& other) 115 inline color_quad_u8& operator= (const color_quad_u8& other)
116 { 116 {
117 m_u32 = other.m_u32; 117 m_u32 = other.m_u32;
118 return *this; 118 return *this;
119 } 119 }
120 120
121 inline color_quad_u8& set_rgb(const color_quad_u8& other) 121 inline color_quad_u8& set_rgb(const color_quad_u8& other)
122 { 122 {
123 r = other.r; 123 r = other.r;
124 g = other.g; 124 g = other.g;
125 b = other.b; 125 b = other.b;
126 return *this; 126 return *this;
127 } 127 }
128 128
129 inline color_quad_u8& operator= (parameter_t y) 129 inline color_quad_u8& operator= (parameter_t y)
130 { 130 {
131 set(y, component_traits::cMax); 131 set(y, component_traits::cMax);
132 return *this; 132 return *this;
133 } 133 }
134 134
135 inline color_quad_u8& set(parameter_t y, parameter_t alpha = component_traits::cMax) 135 inline color_quad_u8& set(parameter_t y, parameter_t alpha = component_traits::cMax)
136 { 136 {
137 y = clamp(y); 137 y = clamp(y);
138 alpha = clamp(alpha); 138 alpha = clamp(alpha);
139 r = static_cast<component_t>(y); 139 r = static_cast<component_t>(y);
140 g = static_cast<component_t>(y); 140 g = static_cast<component_t>(y);
141 b = static_cast<component_t>(y); 141 b = static_cast<component_t>(y);
142 a = static_cast<component_t>(alpha); 142 a = static_cast<component_t>(alpha);
143 return *this; 143 return *this;
144 } 144 }
145 145
146 inline color_quad_u8& set_noclamp_y_alpha(parameter_t y, parameter_t alpha = component_traits::cMax) 146 inline color_quad_u8& set_noclamp_y_alpha(parameter_t y, parameter_t alpha = component_traits::cMax)
147 { 147 {
148 RG_ETC1_ASSERT( (y >= component_traits::cMin) && (y <= component_traits::cMax) ); 148 RG_ETC1_ASSERT( (y >= component_traits::cMin) && (y <= component_traits::cMax) );
149 RG_ETC1_ASSERT( (alpha >= component_traits::cMin) && (alpha <= component_traits::cMax) ); 149 RG_ETC1_ASSERT( (alpha >= component_traits::cMin) && (alpha <= component_traits::cMax) );
150 150
151 r = static_cast<component_t>(y); 151 r = static_cast<component_t>(y);
152 g = static_cast<component_t>(y); 152 g = static_cast<component_t>(y);
153 b = static_cast<component_t>(y); 153 b = static_cast<component_t>(y);
154 a = static_cast<component_t>(alpha); 154 a = static_cast<component_t>(alpha);
155 return *this; 155 return *this;
156 } 156 }
157 157
158 inline color_quad_u8& set(parameter_t red, parameter_t green, parameter_t blue, parameter_t alpha = component_traits::cMax) 158 inline color_quad_u8& set(parameter_t red, parameter_t green, parameter_t blue, parameter_t alpha = component_traits::cMax)
159 { 159 {
160 r = static_cast<component_t>(clamp(red)); 160 r = static_cast<component_t>(clamp(red));
161 g = static_cast<component_t>(clamp(green)); 161 g = static_cast<component_t>(clamp(green));
162 b = static_cast<component_t>(clamp(blue)); 162 b = static_cast<component_t>(clamp(blue));
163 a = static_cast<component_t>(clamp(alpha)); 163 a = static_cast<component_t>(clamp(alpha));
164 return *this; 164 return *this;
165 } 165 }
166 166
167 inline color_quad_u8& set_noclamp_rgba(parameter_t red, parameter_t green, parameter_t blue, parameter_t alpha) 167 inline color_quad_u8& set_noclamp_rgba(parameter_t red, parameter_t green, parameter_t blue, parameter_t alpha)
168 { 168 {
169 RG_ETC1_ASSERT( (red >= component_traits::cMin) && (red <= component_traits::cMax) ); 169 RG_ETC1_ASSERT( (red >= component_traits::cMin) && (red <= component_traits::cMax) );
170 RG_ETC1_ASSERT( (green >= component_traits::cMin) && (green <= component_traits::cMax) ); 170 RG_ETC1_ASSERT( (green >= component_traits::cMin) && (green <= component_traits::cMax) );
171 RG_ETC1_ASSERT( (blue >= component_traits::cMin) && (blue <= component_traits::cMax) ); 171 RG_ETC1_ASSERT( (blue >= component_traits::cMin) && (blue <= component_traits::cMax) );
172 RG_ETC1_ASSERT( (alpha >= component_traits::cMin) && (alpha <= component_traits::cMax) ); 172 RG_ETC1_ASSERT( (alpha >= component_traits::cMin) && (alpha <= component_traits::cMax) );
173 173
174 r = static_cast<component_t>(red); 174 r = static_cast<component_t>(red);
175 g = static_cast<component_t>(green); 175 g = static_cast<component_t>(green);
176 b = static_cast<component_t>(blue); 176 b = static_cast<component_t>(blue);
177 a = static_cast<component_t>(alpha); 177 a = static_cast<component_t>(alpha);
178 return *this; 178 return *this;
179 } 179 }
180 180
181 inline color_quad_u8& set_noclamp_rgb(parameter_t red, parameter_t green, parameter_t blue) 181 inline color_quad_u8& set_noclamp_rgb(parameter_t red, parameter_t green, parameter_t blue)
182 { 182 {
183 RG_ETC1_ASSERT( (red >= component_traits::cMin) && (red <= component_traits::cMax) ); 183 RG_ETC1_ASSERT( (red >= component_traits::cMin) && (red <= component_traits::cMax) );
184 RG_ETC1_ASSERT( (green >= component_traits::cMin) && (green <= component_traits::cMax) ); 184 RG_ETC1_ASSERT( (green >= component_traits::cMin) && (green <= component_traits::cMax) );
185 RG_ETC1_ASSERT( (blue >= component_traits::cMin) && (blue <= component_traits::cMax) ); 185 RG_ETC1_ASSERT( (blue >= component_traits::cMin) && (blue <= component_traits::cMax) );
186 186
187 r = static_cast<component_t>(red); 187 r = static_cast<component_t>(red);
188 g = static_cast<component_t>(green); 188 g = static_cast<component_t>(green);
189 b = static_cast<component_t>(blue); 189 b = static_cast<component_t>(blue);
190 return *this; 190 return *this;
191 } 191 }
192 192
193 static inline parameter_t get_min_comp() { return component_traits::cMin; } 193 static inline parameter_t get_min_comp() { return component_traits::cMin; }
194 static inline parameter_t get_max_comp() { return component_traits::cMax; } 194 static inline parameter_t get_max_comp() { return component_traits::cMax; }
195 static inline bool get_comps_are_signed() { return component_traits::cSigned; } 195 static inline bool get_comps_are_signed() { return component_traits::cSigned; }
196 196
197 inline component_t operator[] (uint i) const { RG_ETC1_ASSERT(i < cNumComps); return c[i]; } 197 inline component_t operator[] (uint i) const { RG_ETC1_ASSERT(i < cNumComps); return c[i]; }
198 inline component_t& operator[] (uint i) { RG_ETC1_ASSERT(i < cNumComps); return c[i]; } 198 inline component_t& operator[] (uint i) { RG_ETC1_ASSERT(i < cNumComps); return c[i]; }
199 199
200 inline color_quad_u8& set_component(uint i, parameter_t f) 200 inline color_quad_u8& set_component(uint i, parameter_t f)
201 { 201 {
202 RG_ETC1_ASSERT(i < cNumComps); 202 RG_ETC1_ASSERT(i < cNumComps);
203 203
204 c[i] = static_cast<component_t>(clamp(f)); 204 c[i] = static_cast<component_t>(clamp(f));
205 205
206 return *this; 206 return *this;
207 } 207 }
208 208
209 inline color_quad_u8& set_grayscale(parameter_t l) 209 inline color_quad_u8& set_grayscale(parameter_t l)
210 { 210 {
211 component_t x = static_cast<component_t>(clamp(l)); 211 component_t x = static_cast<component_t>(clamp(l));
212 c[0] = x; 212 c[0] = x;
213 c[1] = x; 213 c[1] = x;
214 c[2] = x; 214 c[2] = x;
215 return *this; 215 return *this;
216 } 216 }
217 217
218 inline color_quad_u8& clamp(const color_quad_u8& l, const color_quad_u8& h) 218 inline color_quad_u8& clamp(const color_quad_u8& l, const color_quad_u8& h)
219 { 219 {
220 for (uint i = 0; i < cNumComps; i++) 220 for (uint i = 0; i < cNumComps; i++)
221 c[i] = static_cast<component_t>(rg_etc1::clamp<parameter_t>(c[i], l[i], h[i])); 221 c[i] = static_cast<component_t>(rg_etc1::clamp<parameter_t>(c[i], l[i], h[i]));
222 return *this; 222 return *this;
223 } 223 }
224 224
225 inline color_quad_u8& clamp(parameter_t l, parameter_t h) 225 inline color_quad_u8& clamp(parameter_t l, parameter_t h)
226 { 226 {
227 for (uint i = 0; i < cNumComps; i++) 227 for (uint i = 0; i < cNumComps; i++)
228 c[i] = static_cast<component_t>(rg_etc1::clamp<parameter_t>(c[i], l, h)); 228 c[i] = static_cast<component_t>(rg_etc1::clamp<parameter_t>(c[i], l, h));
229 return *this; 229 return *this;
230 } 230 }
231 231
232 // Returns CCIR 601 luma (consistent with color_utils::RGB_To_Y). 232 // Returns CCIR 601 luma (consistent with color_utils::RGB_To_Y).
233 inline parameter_t get_luma() const 233 inline parameter_t get_luma() const
234 { 234 {
235 return static_cast<parameter_t>((19595U * r + 38470U * g + 7471U * b + 32768U) >> 16U); 235 return static_cast<parameter_t>((19595U * r + 38470U * g + 7471U * b + 32768U) >> 16U);
236 } 236 }
237 237
238 // Returns REC 709 luma. 238 // Returns REC 709 luma.
239 inline parameter_t get_luma_rec709() const 239 inline parameter_t get_luma_rec709() const
240 { 240 {
241 return static_cast<parameter_t>((13938U * r + 46869U * g + 4729U * b + 32768U) >> 16U); 241 return static_cast<parameter_t>((13938U * r + 46869U * g + 4729U * b + 32768U) >> 16U);
242 } 242 }
243 243
244 inline uint squared_distance_rgb(const color_quad_u8& c) const 244 inline uint squared_distance_rgb(const color_quad_u8& c) const
245 { 245 {
246 return rg_etc1::square(r - c.r) + rg_etc1::square(g - c.g) + rg_etc1::square(b - c.b); 246 return rg_etc1::square(r - c.r) + rg_etc1::square(g - c.g) + rg_etc1::square(b - c.b);
247 } 247 }
248 248
249 inline uint squared_distance_rgba(const color_quad_u8& c) const 249 inline uint squared_distance_rgba(const color_quad_u8& c) const
250 { 250 {
251 return rg_etc1::square(r - c.r) + rg_etc1::square(g - c.g) + rg_etc1::square(b - c.b) + rg_etc1::square(a - c.a); 251 return rg_etc1::square(r - c.r) + rg_etc1::square(g - c.g) + rg_etc1::square(b - c.b) + rg_etc1::square(a - c.a);
252 } 252 }
253 253
254 inline bool rgb_equals(const color_quad_u8& rhs) const 254 inline bool rgb_equals(const color_quad_u8& rhs) const
255 { 255 {
256 return (r == rhs.r) && (g == rhs.g) && (b == rhs.b); 256 return (r == rhs.r) && (g == rhs.g) && (b == rhs.b);
257 } 257 }
258 258
259 inline bool operator== (const color_quad_u8& rhs) const 259 inline bool operator== (const color_quad_u8& rhs) const
260 { 260 {
261 return m_u32 == rhs.m_u32; 261 return m_u32 == rhs.m_u32;
262 } 262 }
263 263
264 color_quad_u8& operator+= (const color_quad_u8& other) 264 color_quad_u8& operator+= (const color_quad_u8& other)
265 { 265 {
266 for (uint i = 0; i < 4; i++) 266 for (uint i = 0; i < 4; i++)
267 c[i] = static_cast<component_t>(clamp(c[i] + other.c[i])); 267 c[i] = static_cast<component_t>(clamp(c[i] + other.c[i]));
268 return *this; 268 return *this;
269 } 269 }
270 270
271 color_quad_u8& operator-= (const color_quad_u8& other) 271 color_quad_u8& operator-= (const color_quad_u8& other)
272 { 272 {
273 for (uint i = 0; i < 4; i++) 273 for (uint i = 0; i < 4; i++)
274 c[i] = static_cast<component_t>(clamp(c[i] - other.c[i])); 274 c[i] = static_cast<component_t>(clamp(c[i] - other.c[i]));
275 return *this; 275 return *this;
276 } 276 }
277 277
278 friend color_quad_u8 operator+ (const color_quad_u8& lhs, const color_quad_u8& rhs) 278 friend color_quad_u8 operator+ (const color_quad_u8& lhs, const color_quad_u8& rhs)
279 { 279 {
280 color_quad_u8 result(lhs); 280 color_quad_u8 result(lhs);
281 result += rhs; 281 result += rhs;
282 return result; 282 return result;
283 } 283 }
284 284
285 friend color_quad_u8 operator- (const color_quad_u8& lhs, const color_quad_u8& rhs) 285 friend color_quad_u8 operator- (const color_quad_u8& lhs, const color_quad_u8& rhs)
286 { 286 {
287 color_quad_u8 result(lhs); 287 color_quad_u8 result(lhs);
288 result -= rhs; 288 result -= rhs;
289 return result; 289 return result;
290 } 290 }
291 }; // class color_quad_u8 291 }; // class color_quad_u8
292 292
293 struct vec3F 293 struct vec3F
294 { 294 {
295 float m_s[3]; 295 float m_s[3];
296 296
297 inline vec3F() { } 297 inline vec3F() { }
298 inline vec3F(float s) { m_s[0] = s; m_s[1] = s; m_s[2] = s; } 298 inline vec3F(float s) { m_s[0] = s; m_s[1] = s; m_s[2] = s; }
299 inline vec3F(float x, float y, float z) { m_s[0] = x; m_s[1] = y; m_s[2] = z; } 299 inline vec3F(float x, float y, float z) { m_s[0] = x; m_s[1] = y; m_s[2] = z; }
300 300
301 inline float operator[] (uint i) const { RG_ETC1_ASSERT(i < 3); return m_s[i]; } 301 inline float operator[] (uint i) const { RG_ETC1_ASSERT(i < 3); return m_s[i]; }
302 302
303 inline vec3F& operator += (const vec3F& other) { for (uint i = 0; i < 3; i++) m_s[i] += other.m_s[i]; return *this; } 303 inline vec3F& operator += (const vec3F& other) { for (uint i = 0; i < 3; i++) m_s[i] += other.m_s[i]; return *this; }
304 304
305 inline vec3F& operator *= (float s) { for (uint i = 0; i < 3; i++) m_s[i] *= s; return *this; } 305 inline vec3F& operator *= (float s) { for (uint i = 0; i < 3; i++) m_s[i] *= s; return *this; }
306 }; 306 };
307 307
308 enum etc_constants 308 enum etc_constants
309 { 309 {
310 cETC1BytesPerBlock = 8U, 310 cETC1BytesPerBlock = 8U,
311 311
312 cETC1SelectorBits = 2U, 312 cETC1SelectorBits = 2U,
313 cETC1SelectorValues = 1U << cETC1SelectorBits, 313 cETC1SelectorValues = 1U << cETC1SelectorBits,
314 cETC1SelectorMask = cETC1SelectorValues - 1U, 314 cETC1SelectorMask = cETC1SelectorValues - 1U,
315 315
316 cETC1BlockShift = 2U, 316 cETC1BlockShift = 2U,
317 cETC1BlockSize = 1U << cETC1BlockShift, 317 cETC1BlockSize = 1U << cETC1BlockShift,
318 318
319 cETC1LSBSelectorIndicesBitOffset = 0, 319 cETC1LSBSelectorIndicesBitOffset = 0,
320 cETC1MSBSelectorIndicesBitOffset = 16, 320 cETC1MSBSelectorIndicesBitOffset = 16,
321 321
322 cETC1FlipBitOffset = 32, 322 cETC1FlipBitOffset = 32,
323 cETC1DiffBitOffset = 33, 323 cETC1DiffBitOffset = 33,
324 324
325 cETC1IntenModifierNumBits = 3, 325 cETC1IntenModifierNumBits = 3,
326 cETC1IntenModifierValues = 1 << cETC1IntenModifierNumBits, 326 cETC1IntenModifierValues = 1 << cETC1IntenModifierNumBits,
327 cETC1RightIntenModifierTableBitOffset = 34, 327 cETC1RightIntenModifierTableBitOffset = 34,
328 cETC1LeftIntenModifierTableBitOffset = 37, 328 cETC1LeftIntenModifierTableBitOffset = 37,
329 329
330 // Base+Delta encoding (5 bit bases, 3 bit delta) 330 // Base+Delta encoding (5 bit bases, 3 bit delta)
331 cETC1BaseColorCompNumBits = 5, 331 cETC1BaseColorCompNumBits = 5,
332 cETC1BaseColorCompMax = 1 << cETC1BaseColorCompNumBits, 332 cETC1BaseColorCompMax = 1 << cETC1BaseColorCompNumBits,
333 333
334 cETC1DeltaColorCompNumBits = 3, 334 cETC1DeltaColorCompNumBits = 3,
335 cETC1DeltaColorComp = 1 << cETC1DeltaColorCompNumBits, 335 cETC1DeltaColorComp = 1 << cETC1DeltaColorCompNumBits,
336 cETC1DeltaColorCompMax = 1 << cETC1DeltaColorCompNumBits, 336 cETC1DeltaColorCompMax = 1 << cETC1DeltaColorCompNumBits,
337 337
338 cETC1BaseColor5RBitOffset = 59, 338 cETC1BaseColor5RBitOffset = 59,
339 cETC1BaseColor5GBitOffset = 51, 339 cETC1BaseColor5GBitOffset = 51,
340 cETC1BaseColor5BBitOffset = 43, 340 cETC1BaseColor5BBitOffset = 43,
341 341
342 cETC1DeltaColor3RBitOffset = 56, 342 cETC1DeltaColor3RBitOffset = 56,
343 cETC1DeltaColor3GBitOffset = 48, 343 cETC1DeltaColor3GBitOffset = 48,
344 cETC1DeltaColor3BBitOffset = 40, 344 cETC1DeltaColor3BBitOffset = 40,
345 345
346 // Absolute (non-delta) encoding (two 4-bit per component bases) 346 // Absolute (non-delta) encoding (two 4-bit per component bases)
347 cETC1AbsColorCompNumBits = 4, 347 cETC1AbsColorCompNumBits = 4,
348 cETC1AbsColorCompMax = 1 << cETC1AbsColorCompNumBits, 348 cETC1AbsColorCompMax = 1 << cETC1AbsColorCompNumBits,
349 349
350 cETC1AbsColor4R1BitOffset = 60, 350 cETC1AbsColor4R1BitOffset = 60,
351 cETC1AbsColor4G1BitOffset = 52, 351 cETC1AbsColor4G1BitOffset = 52,
352 cETC1AbsColor4B1BitOffset = 44, 352 cETC1AbsColor4B1BitOffset = 44,
353 353
354 cETC1AbsColor4R2BitOffset = 56, 354 cETC1AbsColor4R2BitOffset = 56,
355 cETC1AbsColor4G2BitOffset = 48, 355 cETC1AbsColor4G2BitOffset = 48,
356 cETC1AbsColor4B2BitOffset = 40, 356 cETC1AbsColor4B2BitOffset = 40,
357 357
358 cETC1ColorDeltaMin = -4, 358 cETC1ColorDeltaMin = -4,
359 cETC1ColorDeltaMax = 3, 359 cETC1ColorDeltaMax = 3,
360 360
361 // Delta3: 361 // Delta3:
362 // 0 1 2 3 4 5 6 7 362 // 0 1 2 3 4 5 6 7
363 // 000 001 010 011 100 101 110 111 363 // 000 001 010 011 100 101 110 111
364 // 0 1 2 3 -4 -3 -2 -1 364 // 0 1 2 3 -4 -3 -2 -1
365 }; 365 };
366 366
367 static uint8 g_quant5_tab[256+16]; 367 static uint8 g_quant5_tab[256+16];
368 368
369 static const int g_etc1_inten_tables[cETC1IntenModifierValues][cETC1SelectorValues] = 369 static const int g_etc1_inten_tables[cETC1IntenModifierValues][cETC1SelectorValues] =
370 { 370 {
371 { -8, -2, 2, 8 }, { -17, -5, 5, 17 }, { -29, -9, 9, 29 }, { -42, -13, 13, 42 }, 371 { -8, -2, 2, 8 }, { -17, -5, 5, 17 }, { -29, -9, 9, 29 }, { -42, -13, 13, 42 },
372 { -60, -18, 18, 60 }, { -80, -24, 24, 80 }, { -106, -33, 33, 106 }, { -183, -47, 47, 183 } 372 { -60, -18, 18, 60 }, { -80, -24, 24, 80 }, { -106, -33, 33, 106 }, { -183, -47, 47, 183 }
373 }; 373 };
374 374
375 static const uint8 g_etc1_to_selector_index[cETC1SelectorValues] = { 2, 3, 1, 0 }; 375 static const uint8 g_etc1_to_selector_index[cETC1SelectorValues] = { 2, 3, 1, 0 };
376 static const uint8 g_selector_index_to_etc1[cETC1SelectorValues] = { 3, 2, 0, 1 }; 376 static const uint8 g_selector_index_to_etc1[cETC1SelectorValues] = { 3, 2, 0, 1 };
377 377
378 // Given an ETC1 diff/inten_table/selector, and an 8-bit desired color, this table encodes the best packed_color in the low byte, and the abs error in the high byte. 378 // Given an ETC1 diff/inten_table/selector, and an 8-bit desired color, this table encodes the best packed_color in the low byte, and the abs error in the high byte.
379 static uint16 g_etc1_inverse_lookup[2*8*4][256]; // [diff/inten_table/selector][desired_color] 379 static uint16 g_etc1_inverse_lookup[2*8*4][256]; // [diff/inten_table/selector][desired_color]
380 380
381 // g_color8_to_etc_block_config[color][table_index] = Supplies for each 8-bit color value a list of packed ETC1 diff/intensity table/selectors/packed_colors that map to that color. 381 // g_color8_to_etc_block_config[color][table_index] = Supplies for each 8-bit color value a list of packed ETC1 diff/intensity table/selectors/packed_colors that map to that color.
382 // To pack: diff | (inten << 1) | (selector << 4) | (packed_c << 8) 382 // To pack: diff | (inten << 1) | (selector << 4) | (packed_c << 8)
383 static const uint16 g_color8_to_etc_block_config_0_255[2][33] = 383 static const uint16 g_color8_to_etc_block_config_0_255[2][33] =
384 { 384 {
385 { 0x0000, 0x0010, 0x0002, 0x0012, 0x0004, 0x0014, 0x0006, 0x0016, 0x0008, 0x0018, 0x000A, 0x001A, 0x000C, 0x001C, 0x000E, 0x001E, 385 { 0x0000, 0x0010, 0x0002, 0x0012, 0x0004, 0x0014, 0x0006, 0x0016, 0x0008, 0x0018, 0x000A, 0x001A, 0x000C, 0x001C, 0x000E, 0x001E,
386 0x0001, 0x0011, 0x0003, 0x0013, 0x0005, 0x0015, 0x0007, 0x0017, 0x0009, 0x0019, 0x000B, 0x001B, 0x000D, 0x001D, 0x000F, 0x001F, 0xFFFF }, 386 0x0001, 0x0011, 0x0003, 0x0013, 0x0005, 0x0015, 0x0007, 0x0017, 0x0009, 0x0019, 0x000B, 0x001B, 0x000D, 0x001D, 0x000F, 0x001F, 0xFFFF },
387 { 0x0F20, 0x0F30, 0x0E32, 0x0F22, 0x0E34, 0x0F24, 0x0D36, 0x0F26, 0x0C38, 0x0E28, 0x0B3A, 0x0E2A, 0x093C, 0x0E2C, 0x053E, 0x0D2E, 387 { 0x0F20, 0x0F30, 0x0E32, 0x0F22, 0x0E34, 0x0F24, 0x0D36, 0x0F26, 0x0C38, 0x0E28, 0x0B3A, 0x0E2A, 0x093C, 0x0E2C, 0x053E, 0x0D2E,
388 0x1E31, 0x1F21, 0x1D33, 0x1F23, 0x1C35, 0x1E25, 0x1A37, 0x1E27, 0x1839, 0x1D29, 0x163B, 0x1C2B, 0x133D, 0x1B2D, 0x093F, 0x1A2F, 0xFFFF }, 388 0x1E31, 0x1F21, 0x1D33, 0x1F23, 0x1C35, 0x1E25, 0x1A37, 0x1E27, 0x1839, 0x1D29, 0x163B, 0x1C2B, 0x133D, 0x1B2D, 0x093F, 0x1A2F, 0xFFFF },
389 }; 389 };
390 390
391 // Really only [254][11]. 391 // Really only [254][11].
392 static const uint16 g_color8_to_etc_block_config_1_to_254[254][12] = 392 static const uint16 g_color8_to_etc_block_config_1_to_254[254][12] =
393 { 393 {
394 { 0x021C, 0x0D0D, 0xFFFF }, { 0x0020, 0x0021, 0x0A0B, 0x061F, 0xFFFF }, { 0x0113, 0x0217, 0xFFFF }, { 0x0116, 0x031E, 394 { 0x021C, 0x0D0D, 0xFFFF }, { 0x0020, 0x0021, 0x0A0B, 0x061F, 0xFFFF }, { 0x0113, 0x0217, 0xFFFF }, { 0x0116, 0x031E,
395 0x0B0E, 0x0405, 0xFFFF }, { 0x0022, 0x0204, 0x050A, 0x0023, 0xFFFF }, { 0x0111, 0x0319, 0x0809, 0x170F, 0xFFFF }, { 395 0x0B0E, 0x0405, 0xFFFF }, { 0x0022, 0x0204, 0x050A, 0x0023, 0xFFFF }, { 0x0111, 0x0319, 0x0809, 0x170F, 0xFFFF }, {
396 0x0303, 0x0215, 0x0607, 0xFFFF }, { 0x0030, 0x0114, 0x0408, 0x0031, 0x0201, 0x051D, 0xFFFF }, { 0x0100, 0x0024, 0x0306, 396 0x0303, 0x0215, 0x0607, 0xFFFF }, { 0x0030, 0x0114, 0x0408, 0x0031, 0x0201, 0x051D, 0xFFFF }, { 0x0100, 0x0024, 0x0306,
397 0x0025, 0x041B, 0x0E0D, 0xFFFF }, { 0x021A, 0x0121, 0x0B0B, 0x071F, 0xFFFF }, { 0x0213, 0x0317, 0xFFFF }, { 0x0112, 397 0x0025, 0x041B, 0x0E0D, 0xFFFF }, { 0x021A, 0x0121, 0x0B0B, 0x071F, 0xFFFF }, { 0x0213, 0x0317, 0xFFFF }, { 0x0112,
398 0x0505, 0xFFFF }, { 0x0026, 0x070C, 0x0123, 0x0027, 0xFFFF }, { 0x0211, 0x0909, 0xFFFF }, { 0x0110, 0x0315, 0x0707, 398 0x0505, 0xFFFF }, { 0x0026, 0x070C, 0x0123, 0x0027, 0xFFFF }, { 0x0211, 0x0909, 0xFFFF }, { 0x0110, 0x0315, 0x0707,
399 0x0419, 0x180F, 0xFFFF }, { 0x0218, 0x0131, 0x0301, 0x0403, 0x061D, 0xFFFF }, { 0x0032, 0x0202, 0x0033, 0x0125, 0x051B, 399 0x0419, 0x180F, 0xFFFF }, { 0x0218, 0x0131, 0x0301, 0x0403, 0x061D, 0xFFFF }, { 0x0032, 0x0202, 0x0033, 0x0125, 0x051B,
400 0x0F0D, 0xFFFF }, { 0x0028, 0x031C, 0x0221, 0x0029, 0xFFFF }, { 0x0120, 0x0313, 0x0C0B, 0x081F, 0xFFFF }, { 0x0605, 400 0x0F0D, 0xFFFF }, { 0x0028, 0x031C, 0x0221, 0x0029, 0xFFFF }, { 0x0120, 0x0313, 0x0C0B, 0x081F, 0xFFFF }, { 0x0605,
401 0x0417, 0xFFFF }, { 0x0216, 0x041E, 0x0C0E, 0x0223, 0x0127, 0xFFFF }, { 0x0122, 0x0304, 0x060A, 0x0311, 0x0A09, 0xFFFF 401 0x0417, 0xFFFF }, { 0x0216, 0x041E, 0x0C0E, 0x0223, 0x0127, 0xFFFF }, { 0x0122, 0x0304, 0x060A, 0x0311, 0x0A09, 0xFFFF
402 }, { 0x0519, 0x190F, 0xFFFF }, { 0x002A, 0x0231, 0x0503, 0x0415, 0x0807, 0x002B, 0x071D, 0xFFFF }, { 0x0130, 0x0214, 402 }, { 0x0519, 0x190F, 0xFFFF }, { 0x002A, 0x0231, 0x0503, 0x0415, 0x0807, 0x002B, 0x071D, 0xFFFF }, { 0x0130, 0x0214,
403 0x0508, 0x0401, 0x0133, 0x0225, 0x061B, 0xFFFF }, { 0x0200, 0x0124, 0x0406, 0x0321, 0x0129, 0x100D, 0xFFFF }, { 0x031A, 403 0x0508, 0x0401, 0x0133, 0x0225, 0x061B, 0xFFFF }, { 0x0200, 0x0124, 0x0406, 0x0321, 0x0129, 0x100D, 0xFFFF }, { 0x031A,
404 0x0D0B, 0x091F, 0xFFFF }, { 0x0413, 0x0705, 0x0517, 0xFFFF }, { 0x0212, 0x0034, 0x0323, 0x0035, 0x0227, 0xFFFF }, { 404 0x0D0B, 0x091F, 0xFFFF }, { 0x0413, 0x0705, 0x0517, 0xFFFF }, { 0x0212, 0x0034, 0x0323, 0x0035, 0x0227, 0xFFFF }, {
405 0x0126, 0x080C, 0x0B09, 0xFFFF }, { 0x0411, 0x0619, 0x1A0F, 0xFFFF }, { 0x0210, 0x0331, 0x0603, 0x0515, 0x0907, 0x012B, 405 0x0126, 0x080C, 0x0B09, 0xFFFF }, { 0x0411, 0x0619, 0x1A0F, 0xFFFF }, { 0x0210, 0x0331, 0x0603, 0x0515, 0x0907, 0x012B,
406 0xFFFF }, { 0x0318, 0x002C, 0x0501, 0x0233, 0x0325, 0x071B, 0x002D, 0x081D, 0xFFFF }, { 0x0132, 0x0302, 0x0229, 0x110D, 406 0xFFFF }, { 0x0318, 0x002C, 0x0501, 0x0233, 0x0325, 0x071B, 0x002D, 0x081D, 0xFFFF }, { 0x0132, 0x0302, 0x0229, 0x110D,
407 0xFFFF }, { 0x0128, 0x041C, 0x0421, 0x0E0B, 0x0A1F, 0xFFFF }, { 0x0220, 0x0513, 0x0617, 0xFFFF }, { 0x0135, 0x0805, 407 0xFFFF }, { 0x0128, 0x041C, 0x0421, 0x0E0B, 0x0A1F, 0xFFFF }, { 0x0220, 0x0513, 0x0617, 0xFFFF }, { 0x0135, 0x0805,
408 0x0327, 0xFFFF }, { 0x0316, 0x051E, 0x0D0E, 0x0423, 0xFFFF }, { 0x0222, 0x0404, 0x070A, 0x0511, 0x0719, 0x0C09, 0x1B0F, 408 0x0327, 0xFFFF }, { 0x0316, 0x051E, 0x0D0E, 0x0423, 0xFFFF }, { 0x0222, 0x0404, 0x070A, 0x0511, 0x0719, 0x0C09, 0x1B0F,
409 0xFFFF }, { 0x0703, 0x0615, 0x0A07, 0x022B, 0xFFFF }, { 0x012A, 0x0431, 0x0601, 0x0333, 0x012D, 0x091D, 0xFFFF }, { 409 0xFFFF }, { 0x0703, 0x0615, 0x0A07, 0x022B, 0xFFFF }, { 0x012A, 0x0431, 0x0601, 0x0333, 0x012D, 0x091D, 0xFFFF }, {
410 0x0230, 0x0314, 0x0036, 0x0608, 0x0425, 0x0037, 0x0329, 0x081B, 0x120D, 0xFFFF }, { 0x0300, 0x0224, 0x0506, 0x0521, 410 0x0230, 0x0314, 0x0036, 0x0608, 0x0425, 0x0037, 0x0329, 0x081B, 0x120D, 0xFFFF }, { 0x0300, 0x0224, 0x0506, 0x0521,
411 0x0F0B, 0x0B1F, 0xFFFF }, { 0x041A, 0x0613, 0x0717, 0xFFFF }, { 0x0235, 0x0905, 0xFFFF }, { 0x0312, 0x0134, 0x0523, 411 0x0F0B, 0x0B1F, 0xFFFF }, { 0x041A, 0x0613, 0x0717, 0xFFFF }, { 0x0235, 0x0905, 0xFFFF }, { 0x0312, 0x0134, 0x0523,
412 0x0427, 0xFFFF }, { 0x0226, 0x090C, 0x002E, 0x0611, 0x0D09, 0x002F, 0xFFFF }, { 0x0715, 0x0B07, 0x0819, 0x032B, 0x1C0F, 412 0x0427, 0xFFFF }, { 0x0226, 0x090C, 0x002E, 0x0611, 0x0D09, 0x002F, 0xFFFF }, { 0x0715, 0x0B07, 0x0819, 0x032B, 0x1C0F,
413 0xFFFF }, { 0x0310, 0x0531, 0x0701, 0x0803, 0x022D, 0x0A1D, 0xFFFF }, { 0x0418, 0x012C, 0x0433, 0x0525, 0x0137, 0x091B, 413 0xFFFF }, { 0x0310, 0x0531, 0x0701, 0x0803, 0x022D, 0x0A1D, 0xFFFF }, { 0x0418, 0x012C, 0x0433, 0x0525, 0x0137, 0x091B,
414 0x130D, 0xFFFF }, { 0x0232, 0x0402, 0x0621, 0x0429, 0xFFFF }, { 0x0228, 0x051C, 0x0713, 0x100B, 0x0C1F, 0xFFFF }, { 414 0x130D, 0xFFFF }, { 0x0232, 0x0402, 0x0621, 0x0429, 0xFFFF }, { 0x0228, 0x051C, 0x0713, 0x100B, 0x0C1F, 0xFFFF }, {
415 0x0320, 0x0335, 0x0A05, 0x0817, 0xFFFF }, { 0x0623, 0x0527, 0xFFFF }, { 0x0416, 0x061E, 0x0E0E, 0x0711, 0x0E09, 0x012F, 415 0x0320, 0x0335, 0x0A05, 0x0817, 0xFFFF }, { 0x0623, 0x0527, 0xFFFF }, { 0x0416, 0x061E, 0x0E0E, 0x0711, 0x0E09, 0x012F,
416 0xFFFF }, { 0x0322, 0x0504, 0x080A, 0x0919, 0x1D0F, 0xFFFF }, { 0x0631, 0x0903, 0x0815, 0x0C07, 0x042B, 0x032D, 0x0B1D, 416 0xFFFF }, { 0x0322, 0x0504, 0x080A, 0x0919, 0x1D0F, 0xFFFF }, { 0x0631, 0x0903, 0x0815, 0x0C07, 0x042B, 0x032D, 0x0B1D,
417 0xFFFF }, { 0x022A, 0x0801, 0x0533, 0x0625, 0x0237, 0x0A1B, 0xFFFF }, { 0x0330, 0x0414, 0x0136, 0x0708, 0x0721, 0x0529, 417 0xFFFF }, { 0x022A, 0x0801, 0x0533, 0x0625, 0x0237, 0x0A1B, 0xFFFF }, { 0x0330, 0x0414, 0x0136, 0x0708, 0x0721, 0x0529,
418 0x140D, 0xFFFF }, { 0x0400, 0x0324, 0x0606, 0x0038, 0x0039, 0x110B, 0x0D1F, 0xFFFF }, { 0x051A, 0x0813, 0x0B05, 0x0917, 418 0x140D, 0xFFFF }, { 0x0400, 0x0324, 0x0606, 0x0038, 0x0039, 0x110B, 0x0D1F, 0xFFFF }, { 0x051A, 0x0813, 0x0B05, 0x0917,
419 0xFFFF }, { 0x0723, 0x0435, 0x0627, 0xFFFF }, { 0x0412, 0x0234, 0x0F09, 0x022F, 0xFFFF }, { 0x0326, 0x0A0C, 0x012E, 419 0xFFFF }, { 0x0723, 0x0435, 0x0627, 0xFFFF }, { 0x0412, 0x0234, 0x0F09, 0x022F, 0xFFFF }, { 0x0326, 0x0A0C, 0x012E,
420 0x0811, 0x0A19, 0x1E0F, 0xFFFF }, { 0x0731, 0x0A03, 0x0915, 0x0D07, 0x052B, 0xFFFF }, { 0x0410, 0x0901, 0x0633, 0x0725, 420 0x0811, 0x0A19, 0x1E0F, 0xFFFF }, { 0x0731, 0x0A03, 0x0915, 0x0D07, 0x052B, 0xFFFF }, { 0x0410, 0x0901, 0x0633, 0x0725,
421 0x0337, 0x0B1B, 0x042D, 0x0C1D, 0xFFFF }, { 0x0518, 0x022C, 0x0629, 0x150D, 0xFFFF }, { 0x0332, 0x0502, 0x0821, 0x0139, 421 0x0337, 0x0B1B, 0x042D, 0x0C1D, 0xFFFF }, { 0x0518, 0x022C, 0x0629, 0x150D, 0xFFFF }, { 0x0332, 0x0502, 0x0821, 0x0139,
422 0x120B, 0x0E1F, 0xFFFF }, { 0x0328, 0x061C, 0x0913, 0x0A17, 0xFFFF }, { 0x0420, 0x0535, 0x0C05, 0x0727, 0xFFFF }, { 422 0x120B, 0x0E1F, 0xFFFF }, { 0x0328, 0x061C, 0x0913, 0x0A17, 0xFFFF }, { 0x0420, 0x0535, 0x0C05, 0x0727, 0xFFFF }, {
423 0x0823, 0x032F, 0xFFFF }, { 0x0516, 0x071E, 0x0F0E, 0x0911, 0x0B19, 0x1009, 0x1F0F, 0xFFFF }, { 0x0422, 0x0604, 0x090A, 423 0x0823, 0x032F, 0xFFFF }, { 0x0516, 0x071E, 0x0F0E, 0x0911, 0x0B19, 0x1009, 0x1F0F, 0xFFFF }, { 0x0422, 0x0604, 0x090A,
424 0x0B03, 0x0A15, 0x0E07, 0x062B, 0xFFFF }, { 0x0831, 0x0A01, 0x0733, 0x052D, 0x0D1D, 0xFFFF }, { 0x032A, 0x0825, 0x0437, 424 0x0B03, 0x0A15, 0x0E07, 0x062B, 0xFFFF }, { 0x0831, 0x0A01, 0x0733, 0x052D, 0x0D1D, 0xFFFF }, { 0x032A, 0x0825, 0x0437,
425 0x0729, 0x0C1B, 0x160D, 0xFFFF }, { 0x0430, 0x0514, 0x0236, 0x0808, 0x0921, 0x0239, 0x130B, 0x0F1F, 0xFFFF }, { 0x0500, 425 0x0729, 0x0C1B, 0x160D, 0xFFFF }, { 0x0430, 0x0514, 0x0236, 0x0808, 0x0921, 0x0239, 0x130B, 0x0F1F, 0xFFFF }, { 0x0500,
426 0x0424, 0x0706, 0x0138, 0x0A13, 0x0B17, 0xFFFF }, { 0x061A, 0x0635, 0x0D05, 0xFFFF }, { 0x0923, 0x0827, 0xFFFF }, { 426 0x0424, 0x0706, 0x0138, 0x0A13, 0x0B17, 0xFFFF }, { 0x061A, 0x0635, 0x0D05, 0xFFFF }, { 0x0923, 0x0827, 0xFFFF }, {
427 0x0512, 0x0334, 0x003A, 0x0A11, 0x1109, 0x003B, 0x042F, 0xFFFF }, { 0x0426, 0x0B0C, 0x022E, 0x0B15, 0x0F07, 0x0C19, 427 0x0512, 0x0334, 0x003A, 0x0A11, 0x1109, 0x003B, 0x042F, 0xFFFF }, { 0x0426, 0x0B0C, 0x022E, 0x0B15, 0x0F07, 0x0C19,
428 0x072B, 0xFFFF }, { 0x0931, 0x0B01, 0x0C03, 0x062D, 0x0E1D, 0xFFFF }, { 0x0510, 0x0833, 0x0925, 0x0537, 0x0D1B, 0x170D, 428 0x072B, 0xFFFF }, { 0x0931, 0x0B01, 0x0C03, 0x062D, 0x0E1D, 0xFFFF }, { 0x0510, 0x0833, 0x0925, 0x0537, 0x0D1B, 0x170D,
429 0xFFFF }, { 0x0618, 0x032C, 0x0A21, 0x0339, 0x0829, 0xFFFF }, { 0x0432, 0x0602, 0x0B13, 0x140B, 0x101F, 0xFFFF }, { 429 0xFFFF }, { 0x0618, 0x032C, 0x0A21, 0x0339, 0x0829, 0xFFFF }, { 0x0432, 0x0602, 0x0B13, 0x140B, 0x101F, 0xFFFF }, {
430 0x0428, 0x071C, 0x0735, 0x0E05, 0x0C17, 0xFFFF }, { 0x0520, 0x0A23, 0x0927, 0xFFFF }, { 0x0B11, 0x1209, 0x013B, 0x052F, 430 0x0428, 0x071C, 0x0735, 0x0E05, 0x0C17, 0xFFFF }, { 0x0520, 0x0A23, 0x0927, 0xFFFF }, { 0x0B11, 0x1209, 0x013B, 0x052F,
431 0xFFFF }, { 0x0616, 0x081E, 0x0D19, 0xFFFF }, { 0x0522, 0x0704, 0x0A0A, 0x0A31, 0x0D03, 0x0C15, 0x1007, 0x082B, 0x072D, 431 0xFFFF }, { 0x0616, 0x081E, 0x0D19, 0xFFFF }, { 0x0522, 0x0704, 0x0A0A, 0x0A31, 0x0D03, 0x0C15, 0x1007, 0x082B, 0x072D,
432 0x0F1D, 0xFFFF }, { 0x0C01, 0x0933, 0x0A25, 0x0637, 0x0E1B, 0xFFFF }, { 0x042A, 0x0B21, 0x0929, 0x180D, 0xFFFF }, { 432 0x0F1D, 0xFFFF }, { 0x0C01, 0x0933, 0x0A25, 0x0637, 0x0E1B, 0xFFFF }, { 0x042A, 0x0B21, 0x0929, 0x180D, 0xFFFF }, {
433 0x0530, 0x0614, 0x0336, 0x0908, 0x0439, 0x150B, 0x111F, 0xFFFF }, { 0x0600, 0x0524, 0x0806, 0x0238, 0x0C13, 0x0F05, 433 0x0530, 0x0614, 0x0336, 0x0908, 0x0439, 0x150B, 0x111F, 0xFFFF }, { 0x0600, 0x0524, 0x0806, 0x0238, 0x0C13, 0x0F05,
434 0x0D17, 0xFFFF }, { 0x071A, 0x0B23, 0x0835, 0x0A27, 0xFFFF }, { 0x1309, 0x023B, 0x062F, 0xFFFF }, { 0x0612, 0x0434, 434 0x0D17, 0xFFFF }, { 0x071A, 0x0B23, 0x0835, 0x0A27, 0xFFFF }, { 0x1309, 0x023B, 0x062F, 0xFFFF }, { 0x0612, 0x0434,
435 0x013A, 0x0C11, 0x0E19, 0xFFFF }, { 0x0526, 0x0C0C, 0x032E, 0x0B31, 0x0E03, 0x0D15, 0x1107, 0x092B, 0xFFFF }, { 0x0D01, 435 0x013A, 0x0C11, 0x0E19, 0xFFFF }, { 0x0526, 0x0C0C, 0x032E, 0x0B31, 0x0E03, 0x0D15, 0x1107, 0x092B, 0xFFFF }, { 0x0D01,
436 0x0A33, 0x0B25, 0x0737, 0x0F1B, 0x082D, 0x101D, 0xFFFF }, { 0x0610, 0x0A29, 0x190D, 0xFFFF }, { 0x0718, 0x042C, 0x0C21, 436 0x0A33, 0x0B25, 0x0737, 0x0F1B, 0x082D, 0x101D, 0xFFFF }, { 0x0610, 0x0A29, 0x190D, 0xFFFF }, { 0x0718, 0x042C, 0x0C21,
437 0x0539, 0x160B, 0x121F, 0xFFFF }, { 0x0532, 0x0702, 0x0D13, 0x0E17, 0xFFFF }, { 0x0528, 0x081C, 0x0935, 0x1005, 0x0B27, 437 0x0539, 0x160B, 0x121F, 0xFFFF }, { 0x0532, 0x0702, 0x0D13, 0x0E17, 0xFFFF }, { 0x0528, 0x081C, 0x0935, 0x1005, 0x0B27,
438 0xFFFF }, { 0x0620, 0x0C23, 0x033B, 0x072F, 0xFFFF }, { 0x0D11, 0x0F19, 0x1409, 0xFFFF }, { 0x0716, 0x003C, 0x091E, 438 0xFFFF }, { 0x0620, 0x0C23, 0x033B, 0x072F, 0xFFFF }, { 0x0D11, 0x0F19, 0x1409, 0xFFFF }, { 0x0716, 0x003C, 0x091E,
439 0x0F03, 0x0E15, 0x1207, 0x0A2B, 0x003D, 0xFFFF }, { 0x0622, 0x0804, 0x0B0A, 0x0C31, 0x0E01, 0x0B33, 0x092D, 0x111D, 439 0x0F03, 0x0E15, 0x1207, 0x0A2B, 0x003D, 0xFFFF }, { 0x0622, 0x0804, 0x0B0A, 0x0C31, 0x0E01, 0x0B33, 0x092D, 0x111D,
440 0xFFFF }, { 0x0C25, 0x0837, 0x0B29, 0x101B, 0x1A0D, 0xFFFF }, { 0x052A, 0x0D21, 0x0639, 0x170B, 0x131F, 0xFFFF }, { 440 0xFFFF }, { 0x0C25, 0x0837, 0x0B29, 0x101B, 0x1A0D, 0xFFFF }, { 0x052A, 0x0D21, 0x0639, 0x170B, 0x131F, 0xFFFF }, {
441 0x0630, 0x0714, 0x0436, 0x0A08, 0x0E13, 0x0F17, 0xFFFF }, { 0x0700, 0x0624, 0x0906, 0x0338, 0x0A35, 0x1105, 0xFFFF }, { 441 0x0630, 0x0714, 0x0436, 0x0A08, 0x0E13, 0x0F17, 0xFFFF }, { 0x0700, 0x0624, 0x0906, 0x0338, 0x0A35, 0x1105, 0xFFFF }, {
442 0x081A, 0x0D23, 0x0C27, 0xFFFF }, { 0x0E11, 0x1509, 0x043B, 0x082F, 0xFFFF }, { 0x0712, 0x0534, 0x023A, 0x0F15, 0x1307, 442 0x081A, 0x0D23, 0x0C27, 0xFFFF }, { 0x0E11, 0x1509, 0x043B, 0x082F, 0xFFFF }, { 0x0712, 0x0534, 0x023A, 0x0F15, 0x1307,
443 0x1019, 0x0B2B, 0x013D, 0xFFFF }, { 0x0626, 0x0D0C, 0x042E, 0x0D31, 0x0F01, 0x1003, 0x0A2D, 0x121D, 0xFFFF }, { 0x0C33, 443 0x1019, 0x0B2B, 0x013D, 0xFFFF }, { 0x0626, 0x0D0C, 0x042E, 0x0D31, 0x0F01, 0x1003, 0x0A2D, 0x121D, 0xFFFF }, { 0x0C33,
444 0x0D25, 0x0937, 0x111B, 0x1B0D, 0xFFFF }, { 0x0710, 0x0E21, 0x0739, 0x0C29, 0xFFFF }, { 0x0818, 0x052C, 0x0F13, 0x180B, 444 0x0D25, 0x0937, 0x111B, 0x1B0D, 0xFFFF }, { 0x0710, 0x0E21, 0x0739, 0x0C29, 0xFFFF }, { 0x0818, 0x052C, 0x0F13, 0x180B,
445 0x141F, 0xFFFF }, { 0x0632, 0x0802, 0x0B35, 0x1205, 0x1017, 0xFFFF }, { 0x0628, 0x091C, 0x0E23, 0x0D27, 0xFFFF }, { 445 0x141F, 0xFFFF }, { 0x0632, 0x0802, 0x0B35, 0x1205, 0x1017, 0xFFFF }, { 0x0628, 0x091C, 0x0E23, 0x0D27, 0xFFFF }, {
446 0x0720, 0x0F11, 0x1609, 0x053B, 0x092F, 0xFFFF }, { 0x1119, 0x023D, 0xFFFF }, { 0x0816, 0x013C, 0x0A1E, 0x0E31, 0x1103, 446 0x0720, 0x0F11, 0x1609, 0x053B, 0x092F, 0xFFFF }, { 0x1119, 0x023D, 0xFFFF }, { 0x0816, 0x013C, 0x0A1E, 0x0E31, 0x1103,
447 0x1015, 0x1407, 0x0C2B, 0x0B2D, 0x131D, 0xFFFF }, { 0x0722, 0x0904, 0x0C0A, 0x1001, 0x0D33, 0x0E25, 0x0A37, 0x121B, 447 0x1015, 0x1407, 0x0C2B, 0x0B2D, 0x131D, 0xFFFF }, { 0x0722, 0x0904, 0x0C0A, 0x1001, 0x0D33, 0x0E25, 0x0A37, 0x121B,
448 0xFFFF }, { 0x0F21, 0x0D29, 0x1C0D, 0xFFFF }, { 0x062A, 0x0839, 0x190B, 0x151F, 0xFFFF }, { 0x0730, 0x0814, 0x0536, 448 0xFFFF }, { 0x0F21, 0x0D29, 0x1C0D, 0xFFFF }, { 0x062A, 0x0839, 0x190B, 0x151F, 0xFFFF }, { 0x0730, 0x0814, 0x0536,
449 0x0B08, 0x1013, 0x1305, 0x1117, 0xFFFF }, { 0x0800, 0x0724, 0x0A06, 0x0438, 0x0F23, 0x0C35, 0x0E27, 0xFFFF }, { 0x091A, 449 0x0B08, 0x1013, 0x1305, 0x1117, 0xFFFF }, { 0x0800, 0x0724, 0x0A06, 0x0438, 0x0F23, 0x0C35, 0x0E27, 0xFFFF }, { 0x091A,
450 0x1709, 0x063B, 0x0A2F, 0xFFFF }, { 0x1011, 0x1219, 0x033D, 0xFFFF }, { 0x0812, 0x0634, 0x033A, 0x0F31, 0x1203, 0x1115, 450 0x1709, 0x063B, 0x0A2F, 0xFFFF }, { 0x1011, 0x1219, 0x033D, 0xFFFF }, { 0x0812, 0x0634, 0x033A, 0x0F31, 0x1203, 0x1115,
451 0x1507, 0x0D2B, 0xFFFF }, { 0x0726, 0x0E0C, 0x052E, 0x1101, 0x0E33, 0x0F25, 0x0B37, 0x131B, 0x0C2D, 0x141D, 0xFFFF }, { 451 0x1507, 0x0D2B, 0xFFFF }, { 0x0726, 0x0E0C, 0x052E, 0x1101, 0x0E33, 0x0F25, 0x0B37, 0x131B, 0x0C2D, 0x141D, 0xFFFF }, {
452 0x0E29, 0x1D0D, 0xFFFF }, { 0x0810, 0x1021, 0x0939, 0x1A0B, 0x161F, 0xFFFF }, { 0x0918, 0x062C, 0x1113, 0x1217, 0xFFFF 452 0x0E29, 0x1D0D, 0xFFFF }, { 0x0810, 0x1021, 0x0939, 0x1A0B, 0x161F, 0xFFFF }, { 0x0918, 0x062C, 0x1113, 0x1217, 0xFFFF
453 }, { 0x0732, 0x0902, 0x0D35, 0x1405, 0x0F27, 0xFFFF }, { 0x0728, 0x0A1C, 0x1023, 0x073B, 0x0B2F, 0xFFFF }, { 0x0820, 453 }, { 0x0732, 0x0902, 0x0D35, 0x1405, 0x0F27, 0xFFFF }, { 0x0728, 0x0A1C, 0x1023, 0x073B, 0x0B2F, 0xFFFF }, { 0x0820,
454 0x1111, 0x1319, 0x1809, 0xFFFF }, { 0x1303, 0x1215, 0x1607, 0x0E2B, 0x043D, 0xFFFF }, { 0x0916, 0x023C, 0x0B1E, 0x1031, 454 0x1111, 0x1319, 0x1809, 0xFFFF }, { 0x1303, 0x1215, 0x1607, 0x0E2B, 0x043D, 0xFFFF }, { 0x0916, 0x023C, 0x0B1E, 0x1031,
455 0x1201, 0x0F33, 0x0D2D, 0x151D, 0xFFFF }, { 0x0822, 0x0A04, 0x0D0A, 0x1025, 0x0C37, 0x0F29, 0x141B, 0x1E0D, 0xFFFF }, { 455 0x1201, 0x0F33, 0x0D2D, 0x151D, 0xFFFF }, { 0x0822, 0x0A04, 0x0D0A, 0x1025, 0x0C37, 0x0F29, 0x141B, 0x1E0D, 0xFFFF }, {
456 0x1121, 0x0A39, 0x1B0B, 0x171F, 0xFFFF }, { 0x072A, 0x1213, 0x1317, 0xFFFF }, { 0x0830, 0x0914, 0x0636, 0x0C08, 0x0E35, 456 0x1121, 0x0A39, 0x1B0B, 0x171F, 0xFFFF }, { 0x072A, 0x1213, 0x1317, 0xFFFF }, { 0x0830, 0x0914, 0x0636, 0x0C08, 0x0E35,
457 0x1505, 0xFFFF }, { 0x0900, 0x0824, 0x0B06, 0x0538, 0x1123, 0x1027, 0xFFFF }, { 0x0A1A, 0x1211, 0x1909, 0x083B, 0x0C2F, 457 0x1505, 0xFFFF }, { 0x0900, 0x0824, 0x0B06, 0x0538, 0x1123, 0x1027, 0xFFFF }, { 0x0A1A, 0x1211, 0x1909, 0x083B, 0x0C2F,
458 0xFFFF }, { 0x1315, 0x1707, 0x1419, 0x0F2B, 0x053D, 0xFFFF }, { 0x0912, 0x0734, 0x043A, 0x1131, 0x1301, 0x1403, 0x0E2D, 458 0xFFFF }, { 0x1315, 0x1707, 0x1419, 0x0F2B, 0x053D, 0xFFFF }, { 0x0912, 0x0734, 0x043A, 0x1131, 0x1301, 0x1403, 0x0E2D,
459 0x161D, 0xFFFF }, { 0x0826, 0x0F0C, 0x062E, 0x1033, 0x1125, 0x0D37, 0x151B, 0x1F0D, 0xFFFF }, { 0x1221, 0x0B39, 0x1029, 459 0x161D, 0xFFFF }, { 0x0826, 0x0F0C, 0x062E, 0x1033, 0x1125, 0x0D37, 0x151B, 0x1F0D, 0xFFFF }, { 0x1221, 0x0B39, 0x1029,
460 0xFFFF }, { 0x0910, 0x1313, 0x1C0B, 0x181F, 0xFFFF }, { 0x0A18, 0x072C, 0x0F35, 0x1605, 0x1417, 0xFFFF }, { 0x0832, 460 0xFFFF }, { 0x0910, 0x1313, 0x1C0B, 0x181F, 0xFFFF }, { 0x0A18, 0x072C, 0x0F35, 0x1605, 0x1417, 0xFFFF }, { 0x0832,
461 0x0A02, 0x1223, 0x1127, 0xFFFF }, { 0x0828, 0x0B1C, 0x1311, 0x1A09, 0x093B, 0x0D2F, 0xFFFF }, { 0x0920, 0x1519, 0x063D, 461 0x0A02, 0x1223, 0x1127, 0xFFFF }, { 0x0828, 0x0B1C, 0x1311, 0x1A09, 0x093B, 0x0D2F, 0xFFFF }, { 0x0920, 0x1519, 0x063D,
462 0xFFFF }, { 0x1231, 0x1503, 0x1415, 0x1807, 0x102B, 0x0F2D, 0x171D, 0xFFFF }, { 0x0A16, 0x033C, 0x0C1E, 0x1401, 0x1133, 462 0xFFFF }, { 0x1231, 0x1503, 0x1415, 0x1807, 0x102B, 0x0F2D, 0x171D, 0xFFFF }, { 0x0A16, 0x033C, 0x0C1E, 0x1401, 0x1133,
463 0x1225, 0x0E37, 0x161B, 0xFFFF }, { 0x0922, 0x0B04, 0x0E0A, 0x1321, 0x1129, 0xFFFF }, { 0x0C39, 0x1D0B, 0x191F, 0xFFFF 463 0x1225, 0x0E37, 0x161B, 0xFFFF }, { 0x0922, 0x0B04, 0x0E0A, 0x1321, 0x1129, 0xFFFF }, { 0x0C39, 0x1D0B, 0x191F, 0xFFFF
464 }, { 0x082A, 0x1413, 0x1705, 0x1517, 0xFFFF }, { 0x0930, 0x0A14, 0x0736, 0x0D08, 0x1323, 0x1035, 0x1227, 0xFFFF }, { 464 }, { 0x082A, 0x1413, 0x1705, 0x1517, 0xFFFF }, { 0x0930, 0x0A14, 0x0736, 0x0D08, 0x1323, 0x1035, 0x1227, 0xFFFF }, {
465 0x0A00, 0x0924, 0x0C06, 0x0638, 0x1B09, 0x0A3B, 0x0E2F, 0xFFFF }, { 0x0B1A, 0x1411, 0x1619, 0x073D, 0xFFFF }, { 0x1331, 465 0x0A00, 0x0924, 0x0C06, 0x0638, 0x1B09, 0x0A3B, 0x0E2F, 0xFFFF }, { 0x0B1A, 0x1411, 0x1619, 0x073D, 0xFFFF }, { 0x1331,
466 0x1603, 0x1515, 0x1907, 0x112B, 0xFFFF }, { 0x0A12, 0x0834, 0x053A, 0x1501, 0x1233, 0x1325, 0x0F37, 0x171B, 0x102D, 466 0x1603, 0x1515, 0x1907, 0x112B, 0xFFFF }, { 0x0A12, 0x0834, 0x053A, 0x1501, 0x1233, 0x1325, 0x0F37, 0x171B, 0x102D,
467 0x181D, 0xFFFF }, { 0x0926, 0x072E, 0x1229, 0xFFFF }, { 0x1421, 0x0D39, 0x1E0B, 0x1A1F, 0xFFFF }, { 0x0A10, 0x1513, 467 0x181D, 0xFFFF }, { 0x0926, 0x072E, 0x1229, 0xFFFF }, { 0x1421, 0x0D39, 0x1E0B, 0x1A1F, 0xFFFF }, { 0x0A10, 0x1513,
468 0x1617, 0xFFFF }, { 0x0B18, 0x082C, 0x1135, 0x1805, 0x1327, 0xFFFF }, { 0x0932, 0x0B02, 0x1423, 0x0B3B, 0x0F2F, 0xFFFF 468 0x1617, 0xFFFF }, { 0x0B18, 0x082C, 0x1135, 0x1805, 0x1327, 0xFFFF }, { 0x0932, 0x0B02, 0x1423, 0x0B3B, 0x0F2F, 0xFFFF
469 }, { 0x0928, 0x0C1C, 0x1511, 0x1719, 0x1C09, 0xFFFF }, { 0x0A20, 0x1703, 0x1615, 0x1A07, 0x122B, 0x083D, 0xFFFF }, { 469 }, { 0x0928, 0x0C1C, 0x1511, 0x1719, 0x1C09, 0xFFFF }, { 0x0A20, 0x1703, 0x1615, 0x1A07, 0x122B, 0x083D, 0xFFFF }, {
470 0x1431, 0x1601, 0x1333, 0x112D, 0x191D, 0xFFFF }, { 0x0B16, 0x043C, 0x0D1E, 0x1425, 0x1037, 0x1329, 0x181B, 0xFFFF }, { 470 0x1431, 0x1601, 0x1333, 0x112D, 0x191D, 0xFFFF }, { 0x0B16, 0x043C, 0x0D1E, 0x1425, 0x1037, 0x1329, 0x181B, 0xFFFF }, {
471 0x0A22, 0x0C04, 0x0F0A, 0x1521, 0x0E39, 0x1F0B, 0x1B1F, 0xFFFF }, { 0x1613, 0x1717, 0xFFFF }, { 0x092A, 0x1235, 0x1905, 471 0x0A22, 0x0C04, 0x0F0A, 0x1521, 0x0E39, 0x1F0B, 0x1B1F, 0xFFFF }, { 0x1613, 0x1717, 0xFFFF }, { 0x092A, 0x1235, 0x1905,
472 0xFFFF }, { 0x0A30, 0x0B14, 0x0836, 0x0E08, 0x1523, 0x1427, 0xFFFF }, { 0x0B00, 0x0A24, 0x0D06, 0x0738, 0x1611, 0x1D09, 472 0xFFFF }, { 0x0A30, 0x0B14, 0x0836, 0x0E08, 0x1523, 0x1427, 0xFFFF }, { 0x0B00, 0x0A24, 0x0D06, 0x0738, 0x1611, 0x1D09,
473 0x0C3B, 0x102F, 0xFFFF }, { 0x0C1A, 0x1715, 0x1B07, 0x1819, 0x132B, 0x093D, 0xFFFF }, { 0x1531, 0x1701, 0x1803, 0x122D, 473 0x0C3B, 0x102F, 0xFFFF }, { 0x0C1A, 0x1715, 0x1B07, 0x1819, 0x132B, 0x093D, 0xFFFF }, { 0x1531, 0x1701, 0x1803, 0x122D,
474 0x1A1D, 0xFFFF }, { 0x0B12, 0x0934, 0x063A, 0x1433, 0x1525, 0x1137, 0x191B, 0xFFFF }, { 0x0A26, 0x003E, 0x082E, 0x1621, 474 0x1A1D, 0xFFFF }, { 0x0B12, 0x0934, 0x063A, 0x1433, 0x1525, 0x1137, 0x191B, 0xFFFF }, { 0x0A26, 0x003E, 0x082E, 0x1621,
475 0x0F39, 0x1429, 0x003F, 0xFFFF }, { 0x1713, 0x1C1F, 0xFFFF }, { 0x0B10, 0x1335, 0x1A05, 0x1817, 0xFFFF }, { 0x0C18, 475 0x0F39, 0x1429, 0x003F, 0xFFFF }, { 0x1713, 0x1C1F, 0xFFFF }, { 0x0B10, 0x1335, 0x1A05, 0x1817, 0xFFFF }, { 0x0C18,
476 0x092C, 0x1623, 0x1527, 0xFFFF }, { 0x0A32, 0x0C02, 0x1711, 0x1E09, 0x0D3B, 0x112F, 0xFFFF }, { 0x0A28, 0x0D1C, 0x1919, 476 0x092C, 0x1623, 0x1527, 0xFFFF }, { 0x0A32, 0x0C02, 0x1711, 0x1E09, 0x0D3B, 0x112F, 0xFFFF }, { 0x0A28, 0x0D1C, 0x1919,
477 0x0A3D, 0xFFFF }, { 0x0B20, 0x1631, 0x1903, 0x1815, 0x1C07, 0x142B, 0x132D, 0x1B1D, 0xFFFF }, { 0x1801, 0x1533, 0x1625, 477 0x0A3D, 0xFFFF }, { 0x0B20, 0x1631, 0x1903, 0x1815, 0x1C07, 0x142B, 0x132D, 0x1B1D, 0xFFFF }, { 0x1801, 0x1533, 0x1625,
478 0x1237, 0x1A1B, 0xFFFF }, { 0x0C16, 0x053C, 0x0E1E, 0x1721, 0x1529, 0x013F, 0xFFFF }, { 0x0B22, 0x0D04, 0x1039, 0x1D1F, 478 0x1237, 0x1A1B, 0xFFFF }, { 0x0C16, 0x053C, 0x0E1E, 0x1721, 0x1529, 0x013F, 0xFFFF }, { 0x0B22, 0x0D04, 0x1039, 0x1D1F,
479 0xFFFF }, { 0x1813, 0x1B05, 0x1917, 0xFFFF }, { 0x0A2A, 0x1723, 0x1435, 0x1627, 0xFFFF }, { 0x0B30, 0x0C14, 0x0936, 479 0xFFFF }, { 0x1813, 0x1B05, 0x1917, 0xFFFF }, { 0x0A2A, 0x1723, 0x1435, 0x1627, 0xFFFF }, { 0x0B30, 0x0C14, 0x0936,
480 0x0F08, 0x1F09, 0x0E3B, 0x122F, 0xFFFF }, { 0x0C00, 0x0B24, 0x0E06, 0x0838, 0x1811, 0x1A19, 0x0B3D, 0xFFFF }, { 0x0D1A, 480 0x0F08, 0x1F09, 0x0E3B, 0x122F, 0xFFFF }, { 0x0C00, 0x0B24, 0x0E06, 0x0838, 0x1811, 0x1A19, 0x0B3D, 0xFFFF }, { 0x0D1A,
481 0x1731, 0x1A03, 0x1915, 0x1D07, 0x152B, 0xFFFF }, { 0x1901, 0x1633, 0x1725, 0x1337, 0x1B1B, 0x142D, 0x1C1D, 0xFFFF }, { 481 0x1731, 0x1A03, 0x1915, 0x1D07, 0x152B, 0xFFFF }, { 0x1901, 0x1633, 0x1725, 0x1337, 0x1B1B, 0x142D, 0x1C1D, 0xFFFF }, {
482 0x0C12, 0x0A34, 0x073A, 0x1629, 0x023F, 0xFFFF }, { 0x0B26, 0x013E, 0x092E, 0x1821, 0x1139, 0x1E1F, 0xFFFF }, { 0x1913, 482 0x0C12, 0x0A34, 0x073A, 0x1629, 0x023F, 0xFFFF }, { 0x0B26, 0x013E, 0x092E, 0x1821, 0x1139, 0x1E1F, 0xFFFF }, { 0x1913,
483 0x1A17, 0xFFFF }, { 0x0C10, 0x1535, 0x1C05, 0x1727, 0xFFFF }, { 0x0D18, 0x0A2C, 0x1823, 0x0F3B, 0x132F, 0xFFFF }, { 483 0x1A17, 0xFFFF }, { 0x0C10, 0x1535, 0x1C05, 0x1727, 0xFFFF }, { 0x0D18, 0x0A2C, 0x1823, 0x0F3B, 0x132F, 0xFFFF }, {
484 0x0B32, 0x0D02, 0x1911, 0x1B19, 0xFFFF }, { 0x0B28, 0x0E1C, 0x1B03, 0x1A15, 0x1E07, 0x162B, 0x0C3D, 0xFFFF }, { 0x0C20, 484 0x0B32, 0x0D02, 0x1911, 0x1B19, 0xFFFF }, { 0x0B28, 0x0E1C, 0x1B03, 0x1A15, 0x1E07, 0x162B, 0x0C3D, 0xFFFF }, { 0x0C20,
485 0x1831, 0x1A01, 0x1733, 0x152D, 0x1D1D, 0xFFFF }, { 0x1825, 0x1437, 0x1729, 0x1C1B, 0x033F, 0xFFFF }, { 0x0D16, 0x063C, 485 0x1831, 0x1A01, 0x1733, 0x152D, 0x1D1D, 0xFFFF }, { 0x1825, 0x1437, 0x1729, 0x1C1B, 0x033F, 0xFFFF }, { 0x0D16, 0x063C,
486 0x0F1E, 0x1921, 0x1239, 0x1F1F, 0xFFFF }, { 0x0C22, 0x0E04, 0x1A13, 0x1B17, 0xFFFF }, { 0x1635, 0x1D05, 0xFFFF }, { 486 0x0F1E, 0x1921, 0x1239, 0x1F1F, 0xFFFF }, { 0x0C22, 0x0E04, 0x1A13, 0x1B17, 0xFFFF }, { 0x1635, 0x1D05, 0xFFFF }, {
487 0x0B2A, 0x1923, 0x1827, 0xFFFF }, { 0x0C30, 0x0D14, 0x0A36, 0x1A11, 0x103B, 0x142F, 0xFFFF }, { 0x0D00, 0x0C24, 0x0F06, 487 0x0B2A, 0x1923, 0x1827, 0xFFFF }, { 0x0C30, 0x0D14, 0x0A36, 0x1A11, 0x103B, 0x142F, 0xFFFF }, { 0x0D00, 0x0C24, 0x0F06,
488 0x0938, 0x1B15, 0x1F07, 0x1C19, 0x172B, 0x0D3D, 0xFFFF }, { 0x0E1A, 0x1931, 0x1B01, 0x1C03, 0x162D, 0x1E1D, 0xFFFF }, { 488 0x0938, 0x1B15, 0x1F07, 0x1C19, 0x172B, 0x0D3D, 0xFFFF }, { 0x0E1A, 0x1931, 0x1B01, 0x1C03, 0x162D, 0x1E1D, 0xFFFF }, {
489 0x1833, 0x1925, 0x1537, 0x1D1B, 0xFFFF }, { 0x0D12, 0x0B34, 0x083A, 0x1A21, 0x1339, 0x1829, 0x043F, 0xFFFF }, { 0x0C26, 489 0x1833, 0x1925, 0x1537, 0x1D1B, 0xFFFF }, { 0x0D12, 0x0B34, 0x083A, 0x1A21, 0x1339, 0x1829, 0x043F, 0xFFFF }, { 0x0C26,
490 0x023E, 0x0A2E, 0x1B13, 0xFFFF }, { 0x1735, 0x1E05, 0x1C17, 0xFFFF }, { 0x0D10, 0x1A23, 0x1927, 0xFFFF }, { 0x0E18, 490 0x023E, 0x0A2E, 0x1B13, 0xFFFF }, { 0x1735, 0x1E05, 0x1C17, 0xFFFF }, { 0x0D10, 0x1A23, 0x1927, 0xFFFF }, { 0x0E18,
491 0x0B2C, 0x1B11, 0x113B, 0x152F, 0xFFFF }, { 0x0C32, 0x0E02, 0x1D19, 0x0E3D, 0xFFFF }, { 0x0C28, 0x0F1C, 0x1A31, 0x1D03, 491 0x0B2C, 0x1B11, 0x113B, 0x152F, 0xFFFF }, { 0x0C32, 0x0E02, 0x1D19, 0x0E3D, 0xFFFF }, { 0x0C28, 0x0F1C, 0x1A31, 0x1D03,
492 0x1C15, 0x182B, 0x172D, 0x1F1D, 0xFFFF }, { 0x0D20, 0x1C01, 0x1933, 0x1A25, 0x1637, 0x1E1B, 0xFFFF }, { 0x1B21, 0x1929, 492 0x1C15, 0x182B, 0x172D, 0x1F1D, 0xFFFF }, { 0x0D20, 0x1C01, 0x1933, 0x1A25, 0x1637, 0x1E1B, 0xFFFF }, { 0x1B21, 0x1929,
493 0x053F, 0xFFFF }, { 0x0E16, 0x073C, 0x1439, 0xFFFF }, { 0x0D22, 0x0F04, 0x1C13, 0x1F05, 0x1D17, 0xFFFF }, { 0x1B23, 493 0x053F, 0xFFFF }, { 0x0E16, 0x073C, 0x1439, 0xFFFF }, { 0x0D22, 0x0F04, 0x1C13, 0x1F05, 0x1D17, 0xFFFF }, { 0x1B23,
494 0x1835, 0x1A27, 0xFFFF }, { 0x0C2A, 0x123B, 0x162F, 0xFFFF }, { 0x0D30, 0x0E14, 0x0B36, 0x1C11, 0x1E19, 0x0F3D, 0xFFFF 494 0x1835, 0x1A27, 0xFFFF }, { 0x0C2A, 0x123B, 0x162F, 0xFFFF }, { 0x0D30, 0x0E14, 0x0B36, 0x1C11, 0x1E19, 0x0F3D, 0xFFFF
495 }, { 0x0E00, 0x0D24, 0x0A38, 0x1B31, 0x1E03, 0x1D15, 0x192B, 0xFFFF }, { 0x0F1A, 0x1D01, 0x1A33, 0x1B25, 0x1737, 0x1F1B, 495 }, { 0x0E00, 0x0D24, 0x0A38, 0x1B31, 0x1E03, 0x1D15, 0x192B, 0xFFFF }, { 0x0F1A, 0x1D01, 0x1A33, 0x1B25, 0x1737, 0x1F1B,
496 0x182D, 0xFFFF }, { 0x1A29, 0x063F, 0xFFFF }, { 0x0E12, 0x0C34, 0x093A, 0x1C21, 0x1539, 0xFFFF }, { 0x0D26, 0x033E, 496 0x182D, 0xFFFF }, { 0x1A29, 0x063F, 0xFFFF }, { 0x0E12, 0x0C34, 0x093A, 0x1C21, 0x1539, 0xFFFF }, { 0x0D26, 0x033E,
497 0x0B2E, 0x1D13, 0x1E17, 0xFFFF }, { 0x1935, 0x1B27, 0xFFFF }, { 0x0E10, 0x1C23, 0x133B, 0x172F, 0xFFFF }, { 0x0F18, 497 0x0B2E, 0x1D13, 0x1E17, 0xFFFF }, { 0x1935, 0x1B27, 0xFFFF }, { 0x0E10, 0x1C23, 0x133B, 0x172F, 0xFFFF }, { 0x0F18,
498 0x0C2C, 0x1D11, 0x1F19, 0xFFFF }, { 0x0D32, 0x0F02, 0x1F03, 0x1E15, 0x1A2B, 0x103D, 0xFFFF }, { 0x0D28, 0x1C31, 0x1E01, 498 0x0C2C, 0x1D11, 0x1F19, 0xFFFF }, { 0x0D32, 0x0F02, 0x1F03, 0x1E15, 0x1A2B, 0x103D, 0xFFFF }, { 0x0D28, 0x1C31, 0x1E01,
499 0x1B33, 0x192D, 0xFFFF }, { 0x0E20, 0x1C25, 0x1837, 0x1B29, 0x073F, 0xFFFF }, { 0x1D21, 0x1639, 0xFFFF }, { 0x0F16, 499 0x1B33, 0x192D, 0xFFFF }, { 0x0E20, 0x1C25, 0x1837, 0x1B29, 0x073F, 0xFFFF }, { 0x1D21, 0x1639, 0xFFFF }, { 0x0F16,
500 0x083C, 0x1E13, 0x1F17, 0xFFFF }, { 0x0E22, 0x1A35, 0xFFFF }, { 0x1D23, 0x1C27, 0xFFFF }, { 0x0D2A, 0x1E11, 0x143B, 500 0x083C, 0x1E13, 0x1F17, 0xFFFF }, { 0x0E22, 0x1A35, 0xFFFF }, { 0x1D23, 0x1C27, 0xFFFF }, { 0x0D2A, 0x1E11, 0x143B,
501 0x182F, 0xFFFF }, { 0x0E30, 0x0F14, 0x0C36, 0x1F15, 0x1B2B, 0x113D, 0xFFFF }, { 0x0F00, 0x0E24, 0x0B38, 0x1D31, 0x1F01, 501 0x182F, 0xFFFF }, { 0x0E30, 0x0F14, 0x0C36, 0x1F15, 0x1B2B, 0x113D, 0xFFFF }, { 0x0F00, 0x0E24, 0x0B38, 0x1D31, 0x1F01,
502 0x1A2D, 0xFFFF }, { 0x1C33, 0x1D25, 0x1937, 0xFFFF }, { 0x1E21, 0x1739, 0x1C29, 0x083F, 0xFFFF }, { 0x0F12, 0x0D34, 502 0x1A2D, 0xFFFF }, { 0x1C33, 0x1D25, 0x1937, 0xFFFF }, { 0x1E21, 0x1739, 0x1C29, 0x083F, 0xFFFF }, { 0x0F12, 0x0D34,
503 0x0A3A, 0x1F13, 0xFFFF }, { 0x0E26, 0x043E, 0x0C2E, 0x1B35, 0xFFFF }, { 0x1E23, 0x1D27, 0xFFFF }, { 0x0F10, 0x1F11, 503 0x0A3A, 0x1F13, 0xFFFF }, { 0x0E26, 0x043E, 0x0C2E, 0x1B35, 0xFFFF }, { 0x1E23, 0x1D27, 0xFFFF }, { 0x0F10, 0x1F11,
504 0x153B, 0x192F, 0xFFFF }, { 0x0D2C, 0x123D, 0xFFFF }, 504 0x153B, 0x192F, 0xFFFF }, { 0x0D2C, 0x123D, 0xFFFF },
505 }; 505 };
506 506
507 struct etc1_block 507 struct etc1_block
508 { 508 {
509 // big endian uint64: 509 // big endian uint64:
510 // bit ofs: 56 48 40 32 24 16 8 0 510 // bit ofs: 56 48 40 32 24 16 8 0
511 // byte ofs: b0, b1, b2, b3, b4, b5, b6, b7 511 // byte ofs: b0, b1, b2, b3, b4, b5, b6, b7
512 union 512 union
513 { 513 {
514 uint64 m_uint64; 514 uint64 m_uint64;
515 uint8 m_bytes[8]; 515 uint8 m_bytes[8];
516 }; 516 };
517 517
518 uint8 m_low_color[2]; 518 uint8 m_low_color[2];
519 uint8 m_high_color[2]; 519 uint8 m_high_color[2];
520 520
521 enum { cNumSelectorBytes = 4 }; 521 enum { cNumSelectorBytes = 4 };
522 uint8 m_selectors[cNumSelectorBytes]; 522 uint8 m_selectors[cNumSelectorBytes];
523 523
524 inline void clear() 524 inline void clear()
525 { 525 {
526 zero_this(this); 526 zero_this(this);
527 } 527 }
528 528
529 inline uint get_byte_bits(uint ofs, uint num) const 529 inline uint get_byte_bits(uint ofs, uint num) const
530 { 530 {
531 RG_ETC1_ASSERT((ofs + num) <= 64U); 531 RG_ETC1_ASSERT((ofs + num) <= 64U);
532 RG_ETC1_ASSERT(num && (num <= 8U)); 532 RG_ETC1_ASSERT(num && (num <= 8U));
533 RG_ETC1_ASSERT((ofs >> 3) == ((ofs + num - 1) >> 3)); 533 RG_ETC1_ASSERT((ofs >> 3) == ((ofs + num - 1) >> 3));
534 const uint byte_ofs = 7 - (ofs >> 3); 534 const uint byte_ofs = 7 - (ofs >> 3);
535 const uint byte_bit_ofs = ofs & 7; 535 const uint byte_bit_ofs = ofs & 7;
536 return (m_bytes[byte_ofs] >> byte_bit_ofs) & ((1 << num) - 1); 536 return (m_bytes[byte_ofs] >> byte_bit_ofs) & ((1 << num) - 1);
537 } 537 }
538 538
539 inline void set_byte_bits(uint ofs, uint num, uint bits) 539 inline void set_byte_bits(uint ofs, uint num, uint bits)
540 { 540 {
541 RG_ETC1_ASSERT((ofs + num) <= 64U); 541 RG_ETC1_ASSERT((ofs + num) <= 64U);
542 RG_ETC1_ASSERT(num && (num < 32U)); 542 RG_ETC1_ASSERT(num && (num < 32U));
543 RG_ETC1_ASSERT((ofs >> 3) == ((ofs + num - 1) >> 3)); 543 RG_ETC1_ASSERT((ofs >> 3) == ((ofs + num - 1) >> 3));
544 RG_ETC1_ASSERT(bits < (1U << num)); 544 RG_ETC1_ASSERT(bits < (1U << num));
545 const uint byte_ofs = 7 - (ofs >> 3); 545 const uint byte_ofs = 7 - (ofs >> 3);
546 const uint byte_bit_ofs = ofs & 7; 546 const uint byte_bit_ofs = ofs & 7;
547 const uint mask = (1 << num) - 1; 547 const uint mask = (1 << num) - 1;
548 m_bytes[byte_ofs] &= ~(mask << byte_bit_ofs); 548 m_bytes[byte_ofs] &= ~(mask << byte_bit_ofs);
549 m_bytes[byte_ofs] |= (bits << byte_bit_ofs); 549 m_bytes[byte_ofs] |= (bits << byte_bit_ofs);
550 } 550 }
551 551
552 // false = left/right subblocks 552 // false = left/right subblocks
553 // true = upper/lower subblocks 553 // true = upper/lower subblocks
554 inline bool get_flip_bit() const 554 inline bool get_flip_bit() const
555 { 555 {
556 return (m_bytes[3] & 1) != 0; 556 return (m_bytes[3] & 1) != 0;
557 } 557 }
558 558
559 inline void set_flip_bit(bool flip) 559 inline void set_flip_bit(bool flip)
560 { 560 {
561 m_bytes[3] &= ~1; 561 m_bytes[3] &= ~1;
562 m_bytes[3] |= static_cast<uint8>(flip); 562 m_bytes[3] |= static_cast<uint8>(flip);
563 } 563 }
564 564
565 inline bool get_diff_bit() const 565 inline bool get_diff_bit() const
566 { 566 {
567 return (m_bytes[3] & 2) != 0; 567 return (m_bytes[3] & 2) != 0;
568 } 568 }
569 569
570 inline void set_diff_bit(bool diff) 570 inline void set_diff_bit(bool diff)
571 { 571 {
572 m_bytes[3] &= ~2; 572 m_bytes[3] &= ~2;
573 m_bytes[3] |= (static_cast<uint>(diff) << 1); 573 m_bytes[3] |= (static_cast<uint>(diff) << 1);
574 } 574 }
575 575
576 // Returns intensity modifier table (0-7) used by subblock subblock_id. 576 // Returns intensity modifier table (0-7) used by subblock subblock_id.
577 // subblock_id=0 left/top (CW 1), 1=right/bottom (CW 2) 577 // subblock_id=0 left/top (CW 1), 1=right/bottom (CW 2)
578 inline uint get_inten_table(uint subblock_id) const 578 inline uint get_inten_table(uint subblock_id) const
579 { 579 {
580 RG_ETC1_ASSERT(subblock_id < 2); 580 RG_ETC1_ASSERT(subblock_id < 2);
581 const uint ofs = subblock_id ? 2 : 5; 581 const uint ofs = subblock_id ? 2 : 5;
582 return (m_bytes[3] >> ofs) & 7; 582 return (m_bytes[3] >> ofs) & 7;
583 } 583 }
584 584
585 // Sets intensity modifier table (0-7) used by subblock subblock_id (0 or 1) 585 // Sets intensity modifier table (0-7) used by subblock subblock_id (0 or 1)
586 inline void set_inten_table(uint subblock_id, uint t) 586 inline void set_inten_table(uint subblock_id, uint t)
587 { 587 {
588 RG_ETC1_ASSERT(subblock_id < 2); 588 RG_ETC1_ASSERT(subblock_id < 2);
589 RG_ETC1_ASSERT(t < 8); 589 RG_ETC1_ASSERT(t < 8);
590 const uint ofs = subblock_id ? 2 : 5; 590 const uint ofs = subblock_id ? 2 : 5;
591 m_bytes[3] &= ~(7 << ofs); 591 m_bytes[3] &= ~(7 << ofs);
592 m_bytes[3] |= (t << ofs); 592 m_bytes[3] |= (t << ofs);
593 } 593 }
594 594
595 // Returned selector value ranges from 0-3 and is a direct index into g_etc1_inten_tables. 595 // Returned selector value ranges from 0-3 and is a direct index into g_etc1_inten_tables.
596 inline uint get_selector(uint x, uint y) const 596 inline uint get_selector(uint x, uint y) const
597 { 597 {
598 RG_ETC1_ASSERT((x | y) < 4); 598 RG_ETC1_ASSERT((x | y) < 4);
599 599
600 const uint bit_index = x * 4 + y; 600 const uint bit_index = x * 4 + y;
601 const uint byte_bit_ofs = bit_index & 7; 601 const uint byte_bit_ofs = bit_index & 7;
602 const uint8 *p = &m_bytes[7 - (bit_index >> 3)]; 602 const uint8 *p = &m_bytes[7 - (bit_index >> 3)];
603 const uint lsb = (p[0] >> byte_bit_ofs) & 1; 603 const uint lsb = (p[0] >> byte_bit_ofs) & 1;
604 const uint msb = (p[-2] >> byte_bit_ofs) & 1; 604 const uint msb = (p[-2] >> byte_bit_ofs) & 1;
605 const uint val = lsb | (msb << 1); 605 const uint val = lsb | (msb << 1);
606 606
607 return g_etc1_to_selector_index[val]; 607 return g_etc1_to_selector_index[val];
608 } 608 }
609 609
610 // Selector "val" ranges from 0-3 and is a direct index into g_etc1_inten_tables. 610 // Selector "val" ranges from 0-3 and is a direct index into g_etc1_inten_tables.
611 inline void set_selector(uint x, uint y, uint val) 611 inline void set_selector(uint x, uint y, uint val)
612 { 612 {
613 RG_ETC1_ASSERT((x | y | val) < 4); 613 RG_ETC1_ASSERT((x | y | val) < 4);
614 const uint bit_index = x * 4 + y; 614 const uint bit_index = x * 4 + y;
615 615
616 uint8 *p = &m_bytes[7 - (bit_index >> 3)]; 616 uint8 *p = &m_bytes[7 - (bit_index >> 3)];
617 617
618 const uint byte_bit_ofs = bit_index & 7; 618 const uint byte_bit_ofs = bit_index & 7;
619 const uint mask = 1 << byte_bit_ofs; 619 const uint mask = 1 << byte_bit_ofs;
620 620
621 const uint etc1_val = g_selector_index_to_etc1[val]; 621 const uint etc1_val = g_selector_index_to_etc1[val];
622 622
623 const uint lsb = etc1_val & 1; 623 const uint lsb = etc1_val & 1;
624 const uint msb = etc1_val >> 1; 624 const uint msb = etc1_val >> 1;
625 625
626 p[0] &= ~mask; 626 p[0] &= ~mask;
627 p[0] |= (lsb << byte_bit_ofs); 627 p[0] |= (lsb << byte_bit_ofs);
628 628
629 p[-2] &= ~mask; 629 p[-2] &= ~mask;
630 p[-2] |= (msb << byte_bit_ofs); 630 p[-2] |= (msb << byte_bit_ofs);
631 } 631 }
632 632
633 inline void set_base4_color(uint idx, uint16 c) 633 inline void set_base4_color(uint idx, uint16 c)
634 { 634 {
635 if (idx) 635 if (idx)
636 { 636 {
637 set_byte_bits(cETC1AbsColor4R2BitOffset, 4, (c >> 8) & 15); 637 set_byte_bits(cETC1AbsColor4R2BitOffset, 4, (c >> 8) & 15);
638 set_byte_bits(cETC1AbsColor4G2BitOffset, 4, (c >> 4) & 15); 638 set_byte_bits(cETC1AbsColor4G2BitOffset, 4, (c >> 4) & 15);
639 set_byte_bits(cETC1AbsColor4B2BitOffset, 4, c & 15); 639 set_byte_bits(cETC1AbsColor4B2BitOffset, 4, c & 15);
640 } 640 }
641 else 641 else
642 { 642 {
643 set_byte_bits(cETC1AbsColor4R1BitOffset, 4, (c >> 8) & 15); 643 set_byte_bits(cETC1AbsColor4R1BitOffset, 4, (c >> 8) & 15);
644 set_byte_bits(cETC1AbsColor4G1BitOffset, 4, (c >> 4) & 15); 644 set_byte_bits(cETC1AbsColor4G1BitOffset, 4, (c >> 4) & 15);
645 set_byte_bits(cETC1AbsColor4B1BitOffset, 4, c & 15); 645 set_byte_bits(cETC1AbsColor4B1BitOffset, 4, c & 15);
646 } 646 }
647 } 647 }
648 648
649 inline uint16 get_base4_color(uint idx) const 649 inline uint16 get_base4_color(uint idx) const
650 { 650 {
651 uint r, g, b; 651 uint r, g, b;
652 if (idx) 652 if (idx)
653 { 653 {
654 r = get_byte_bits(cETC1AbsColor4R2BitOffset, 4); 654 r = get_byte_bits(cETC1AbsColor4R2BitOffset, 4);
655 g = get_byte_bits(cETC1AbsColor4G2BitOffset, 4); 655 g = get_byte_bits(cETC1AbsColor4G2BitOffset, 4);
656 b = get_byte_bits(cETC1AbsColor4B2BitOffset, 4); 656 b = get_byte_bits(cETC1AbsColor4B2BitOffset, 4);
657 } 657 }
658 else 658 else
659 { 659 {
660 r = get_byte_bits(cETC1AbsColor4R1BitOffset, 4); 660 r = get_byte_bits(cETC1AbsColor4R1BitOffset, 4);
661 g = get_byte_bits(cETC1AbsColor4G1BitOffset, 4); 661 g = get_byte_bits(cETC1AbsColor4G1BitOffset, 4);
662 b = get_byte_bits(cETC1AbsColor4B1BitOffset, 4); 662 b = get_byte_bits(cETC1AbsColor4B1BitOffset, 4);
663 } 663 }
664 return static_cast<uint16>(b | (g << 4U) | (r << 8U)); 664 return static_cast<uint16>(b | (g << 4U) | (r << 8U));
665 } 665 }
666 666
667 inline void set_base5_color(uint16 c) 667 inline void set_base5_color(uint16 c)
668 { 668 {
669 set_byte_bits(cETC1BaseColor5RBitOffset, 5, (c >> 10) & 31); 669 set_byte_bits(cETC1BaseColor5RBitOffset, 5, (c >> 10) & 31);
670 set_byte_bits(cETC1BaseColor5GBitOffset, 5, (c >> 5) & 31); 670 set_byte_bits(cETC1BaseColor5GBitOffset, 5, (c >> 5) & 31);
671 set_byte_bits(cETC1BaseColor5BBitOffset, 5, c & 31); 671 set_byte_bits(cETC1BaseColor5BBitOffset, 5, c & 31);
672 } 672 }
673 673
674 inline uint16 get_base5_color() const 674 inline uint16 get_base5_color() const
675 { 675 {
676 const uint r = get_byte_bits(cETC1BaseColor5RBitOffset, 5); 676 const uint r = get_byte_bits(cETC1BaseColor5RBitOffset, 5);
677 const uint g = get_byte_bits(cETC1BaseColor5GBitOffset, 5); 677 const uint g = get_byte_bits(cETC1BaseColor5GBitOffset, 5);
678 const uint b = get_byte_bits(cETC1BaseColor5BBitOffset, 5); 678 const uint b = get_byte_bits(cETC1BaseColor5BBitOffset, 5);
679 return static_cast<uint16>(b | (g << 5U) | (r << 10U)); 679 return static_cast<uint16>(b | (g << 5U) | (r << 10U));
680 } 680 }
681 681
682 void set_delta3_color(uint16 c) 682 void set_delta3_color(uint16 c)
683 { 683 {
684 set_byte_bits(cETC1DeltaColor3RBitOffset, 3, (c >> 6) & 7); 684 set_byte_bits(cETC1DeltaColor3RBitOffset, 3, (c >> 6) & 7);
685 set_byte_bits(cETC1DeltaColor3GBitOffset, 3, (c >> 3) & 7); 685 set_byte_bits(cETC1DeltaColor3GBitOffset, 3, (c >> 3) & 7);
686 set_byte_bits(cETC1DeltaColor3BBitOffset, 3, c & 7); 686 set_byte_bits(cETC1DeltaColor3BBitOffset, 3, c & 7);
687 } 687 }
688 688
689 inline uint16 get_delta3_color() const 689 inline uint16 get_delta3_color() const
690 { 690 {
691 const uint r = get_byte_bits(cETC1DeltaColor3RBitOffset, 3); 691 const uint r = get_byte_bits(cETC1DeltaColor3RBitOffset, 3);
692 const uint g = get_byte_bits(cETC1DeltaColor3GBitOffset, 3); 692 const uint g = get_byte_bits(cETC1DeltaColor3GBitOffset, 3);
693 const uint b = get_byte_bits(cETC1DeltaColor3BBitOffset, 3); 693 const uint b = get_byte_bits(cETC1DeltaColor3BBitOffset, 3);
694 return static_cast<uint16>(b | (g << 3U) | (r << 6U)); 694 return static_cast<uint16>(b | (g << 3U) | (r << 6U));
695 } 695 }
696 696
697 // Base color 5 697 // Base color 5
698 static uint16 pack_color5(const color_quad_u8& color, bool scaled, uint bias = 127U); 698 static uint16 pack_color5(const color_quad_u8& color, bool scaled, uint bias = 127U);
699 static uint16 pack_color5(uint r, uint g, uint b, bool scaled, uint bias = 127U); 699 static uint16 pack_color5(uint r, uint g, uint b, bool scaled, uint bias = 127U);
700 700
701 static color_quad_u8 unpack_color5(uint16 packed_color5, bool scaled, uint alpha = 255U); 701 static color_quad_u8 unpack_color5(uint16 packed_color5, bool scaled, uint alpha = 255U);
702 static void unpack_color5(uint& r, uint& g, uint& b, uint16 packed_color, bool scaled); 702 static void unpack_color5(uint& r, uint& g, uint& b, uint16 packed_color, bool scaled);
703 703
704 static bool unpack_color5(color_quad_u8& result, uint16 packed_color5, uint16 packed_delta3, bool scaled, uint alpha = 255U); 704 static bool unpack_color5(color_quad_u8& result, uint16 packed_color5, uint16 packed_delta3, bool scaled, uint alpha = 255U);
705 static bool unpack_color5(uint& r, uint& g, uint& b, uint16 packed_color5, uint16 packed_delta3, bool scaled, uint alpha = 255U); 705 static bool unpack_color5(uint& r, uint& g, uint& b, uint16 packed_color5, uint16 packed_delta3, bool scaled, uint alpha = 255U);
706 706
707 // Delta color 3 707 // Delta color 3
708 // Inputs range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax) 708 // Inputs range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax)
709 static uint16 pack_delta3(int r, int g, int b); 709 static uint16 pack_delta3(int r, int g, int b);
710 710
711 // Results range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax) 711 // Results range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax)
712 static void unpack_delta3(int& r, int& g, int& b, uint16 packed_delta3); 712 static void unpack_delta3(int& r, int& g, int& b, uint16 packed_delta3);
713 713
714 // Abs color 4 714 // Abs color 4
715 static uint16 pack_color4(const color_quad_u8& color, bool scaled, uint bias = 127U); 715 static uint16 pack_color4(const color_quad_u8& color, bool scaled, uint bias = 127U);
716 static uint16 pack_color4(uint r, uint g, uint b, bool scaled, uint bias = 127U); 716 static uint16 pack_color4(uint r, uint g, uint b, bool scaled, uint bias = 127U);
717 717
718 static color_quad_u8 unpack_color4(uint16 packed_color4, bool scaled, uint alpha = 255U); 718 static color_quad_u8 unpack_color4(uint16 packed_color4, bool scaled, uint alpha = 255U);
719 static void unpack_color4(uint& r, uint& g, uint& b, uint16 packed_color4, bool scaled); 719 static void unpack_color4(uint& r, uint& g, uint& b, uint16 packed_color4, bool scaled);
720 720
721 // subblock colors 721 // subblock colors
722 static void get_diff_subblock_colors(color_quad_u8* pDst, uint16 packed_color5, uint table_idx); 722 static void get_diff_subblock_colors(color_quad_u8* pDst, uint16 packed_color5, uint table_idx);
723 static bool get_diff_subblock_colors(color_quad_u8* pDst, uint16 packed_color5, uint16 packed_delta3, uint table_idx); 723 static bool get_diff_subblock_colors(color_quad_u8* pDst, uint16 packed_color5, uint16 packed_delta3, uint table_idx);
724 static void get_abs_subblock_colors(color_quad_u8* pDst, uint16 packed_color4, uint table_idx); 724 static void get_abs_subblock_colors(color_quad_u8* pDst, uint16 packed_color4, uint table_idx);
725 725
726 static inline void unscaled_to_scaled_color(color_quad_u8& dst, const color_quad_u8& src, bool color4) 726 static inline void unscaled_to_scaled_color(color_quad_u8& dst, const color_quad_u8& src, bool color4)
727 { 727 {
728 if (color4) 728 if (color4)
729 { 729 {
730 dst.r = src.r | (src.r << 4); 730 dst.r = src.r | (src.r << 4);
731 dst.g = src.g | (src.g << 4); 731 dst.g = src.g | (src.g << 4);
732 dst.b = src.b | (src.b << 4); 732 dst.b = src.b | (src.b << 4);
733 } 733 }
734 else 734 else
735 { 735 {
736 dst.r = (src.r >> 2) | (src.r << 3); 736 dst.r = (src.r >> 2) | (src.r << 3);
737 dst.g = (src.g >> 2) | (src.g << 3); 737 dst.g = (src.g >> 2) | (src.g << 3);
738 dst.b = (src.b >> 2) | (src.b << 3); 738 dst.b = (src.b >> 2) | (src.b << 3);
739 } 739 }
740 dst.a = src.a; 740 dst.a = src.a;
741 } 741 }
742 }; 742 };
743 743
744 // Returns pointer to sorted array. 744 // Returns pointer to sorted array.
745 template<typename T, typename Q> 745 template<typename T, typename Q>
746 T* indirect_radix_sort(uint num_indices, T* pIndices0, T* pIndices1, const Q* pKeys, uint key_ofs, uint key_size, bool init_indices) 746 T* indirect_radix_sort(uint num_indices, T* pIndices0, T* pIndices1, const Q* pKeys, uint key_ofs, uint key_size, bool init_indices)
747 { 747 {
748 RG_ETC1_ASSERT((key_ofs >= 0) && (key_ofs < sizeof(T))); 748 RG_ETC1_ASSERT((key_ofs >= 0) && (key_ofs < sizeof(T)));
749 RG_ETC1_ASSERT((key_size >= 1) && (key_size <= 4)); 749 RG_ETC1_ASSERT((key_size >= 1) && (key_size <= 4));
750 750
751 if (init_indices) 751 if (init_indices)
752 { 752 {
753 T* p = pIndices0; 753 T* p = pIndices0;
754 T* q = pIndices0 + (num_indices >> 1) * 2; 754 T* q = pIndices0 + (num_indices >> 1) * 2;
755 uint i; 755 uint i;
756 for (i = 0; p != q; p += 2, i += 2) 756 for (i = 0; p != q; p += 2, i += 2)
757 { 757 {
758 p[0] = static_cast<T>(i); 758 p[0] = static_cast<T>(i);
759 p[1] = static_cast<T>(i + 1); 759 p[1] = static_cast<T>(i + 1);
760 } 760 }
761 761
762 if (num_indices & 1) 762 if (num_indices & 1)
763 *p = static_cast<T>(i); 763 *p = static_cast<T>(i);
764 } 764 }
765 765
766 uint hist[256 * 4]; 766 uint hist[256 * 4];
767 767
768 memset(hist, 0, sizeof(hist[0]) * 256 * key_size); 768 memset(hist, 0, sizeof(hist[0]) * 256 * key_size);
769 769
770#define RG_ETC1_GET_KEY(p) (*(const uint*)((const uint8*)(pKeys + *(p)) + key_ofs)) 770#define RG_ETC1_GET_KEY(p) (*(const uint*)((const uint8*)(pKeys + *(p)) + key_ofs))
771#define RG_ETC1_GET_KEY_FROM_INDEX(i) (*(const uint*)((const uint8*)(pKeys + (i)) + key_ofs)) 771#define RG_ETC1_GET_KEY_FROM_INDEX(i) (*(const uint*)((const uint8*)(pKeys + (i)) + key_ofs))
772 772
773 if (key_size == 4) 773 if (key_size == 4)
774 { 774 {
775 T* p = pIndices0; 775 T* p = pIndices0;
776 T* q = pIndices0 + num_indices; 776 T* q = pIndices0 + num_indices;
777 for ( ; p != q; p++) 777 for ( ; p != q; p++)
778 { 778 {
779 const uint key = RG_ETC1_GET_KEY(p); 779 const uint key = RG_ETC1_GET_KEY(p);
780 780
781 hist[ key & 0xFF]++; 781 hist[ key & 0xFF]++;
782 hist[256 + ((key >> 8) & 0xFF)]++; 782 hist[256 + ((key >> 8) & 0xFF)]++;
783 hist[512 + ((key >> 16) & 0xFF)]++; 783 hist[512 + ((key >> 16) & 0xFF)]++;
784 hist[768 + ((key >> 24) & 0xFF)]++; 784 hist[768 + ((key >> 24) & 0xFF)]++;
785 } 785 }
786 } 786 }
787 else if (key_size == 3) 787 else if (key_size == 3)
788 { 788 {
789 T* p = pIndices0; 789 T* p = pIndices0;
790 T* q = pIndices0 + num_indices; 790 T* q = pIndices0 + num_indices;
791 for ( ; p != q; p++) 791 for ( ; p != q; p++)
792 { 792 {
793 const uint key = RG_ETC1_GET_KEY(p); 793 const uint key = RG_ETC1_GET_KEY(p);
794 794
795 hist[ key & 0xFF]++; 795 hist[ key & 0xFF]++;
796 hist[256 + ((key >> 8) & 0xFF)]++; 796 hist[256 + ((key >> 8) & 0xFF)]++;
797 hist[512 + ((key >> 16) & 0xFF)]++; 797 hist[512 + ((key >> 16) & 0xFF)]++;
798 } 798 }
799 } 799 }
800 else if (key_size == 2) 800 else if (key_size == 2)
801 { 801 {
802 T* p = pIndices0; 802 T* p = pIndices0;
803 T* q = pIndices0 + (num_indices >> 1) * 2; 803 T* q = pIndices0 + (num_indices >> 1) * 2;
804 804
805 for ( ; p != q; p += 2) 805 for ( ; p != q; p += 2)
806 { 806 {
807 const uint key0 = RG_ETC1_GET_KEY(p); 807 const uint key0 = RG_ETC1_GET_KEY(p);
808 const uint key1 = RG_ETC1_GET_KEY(p+1); 808 const uint key1 = RG_ETC1_GET_KEY(p+1);
809 809
810 hist[ key0 & 0xFF]++; 810 hist[ key0 & 0xFF]++;
811 hist[256 + ((key0 >> 8) & 0xFF)]++; 811 hist[256 + ((key0 >> 8) & 0xFF)]++;
812 812
813 hist[ key1 & 0xFF]++; 813 hist[ key1 & 0xFF]++;
814 hist[256 + ((key1 >> 8) & 0xFF)]++; 814 hist[256 + ((key1 >> 8) & 0xFF)]++;
815 } 815 }
816 816
817 if (num_indices & 1) 817 if (num_indices & 1)
818 { 818 {
819 const uint key = RG_ETC1_GET_KEY(p); 819 const uint key = RG_ETC1_GET_KEY(p);
820 820
821 hist[ key & 0xFF]++; 821 hist[ key & 0xFF]++;
822 hist[256 + ((key >> 8) & 0xFF)]++; 822 hist[256 + ((key >> 8) & 0xFF)]++;
823 } 823 }
824 } 824 }
825 else 825 else
826 { 826 {
827 RG_ETC1_ASSERT(key_size == 1); 827 RG_ETC1_ASSERT(key_size == 1);
828 if (key_size != 1) 828 if (key_size != 1)
829 return NULL; 829 return NULL;
830 830
831 T* p = pIndices0; 831 T* p = pIndices0;
832 T* q = pIndices0 + (num_indices >> 1) * 2; 832 T* q = pIndices0 + (num_indices >> 1) * 2;
833 833
834 for ( ; p != q; p += 2) 834 for ( ; p != q; p += 2)
835 { 835 {
836 const uint key0 = RG_ETC1_GET_KEY(p); 836 const uint key0 = RG_ETC1_GET_KEY(p);
837 const uint key1 = RG_ETC1_GET_KEY(p+1); 837 const uint key1 = RG_ETC1_GET_KEY(p+1);
838 838
839 hist[key0 & 0xFF]++; 839 hist[key0 & 0xFF]++;
840 hist[key1 & 0xFF]++; 840 hist[key1 & 0xFF]++;
841 } 841 }
842 842
843 if (num_indices & 1) 843 if (num_indices & 1)
844 { 844 {
845 const uint key = RG_ETC1_GET_KEY(p); 845 const uint key = RG_ETC1_GET_KEY(p);
846 846
847 hist[key & 0xFF]++; 847 hist[key & 0xFF]++;
848 } 848 }
849 } 849 }
850 850
851 T* pCur = pIndices0; 851 T* pCur = pIndices0;
852 T* pNew = pIndices1; 852 T* pNew = pIndices1;
853 853
854 for (uint pass = 0; pass < key_size; pass++) 854 for (uint pass = 0; pass < key_size; pass++)
855 { 855 {
856 const uint* pHist = &hist[pass << 8]; 856 const uint* pHist = &hist[pass << 8];
857 857
858 uint offsets[256]; 858 uint offsets[256];
859 859
860 uint cur_ofs = 0; 860 uint cur_ofs = 0;
861 for (uint i = 0; i < 256; i += 2) 861 for (uint i = 0; i < 256; i += 2)
862 { 862 {
863 offsets[i] = cur_ofs; 863 offsets[i] = cur_ofs;
864 cur_ofs += pHist[i]; 864 cur_ofs += pHist[i];
865 865
866 offsets[i+1] = cur_ofs; 866 offsets[i+1] = cur_ofs;
867 cur_ofs += pHist[i+1]; 867 cur_ofs += pHist[i+1];
868 } 868 }
869 869
870 const uint pass_shift = pass << 3; 870 const uint pass_shift = pass << 3;
871 871
872 T* p = pCur; 872 T* p = pCur;
873 T* q = pCur + (num_indices >> 1) * 2; 873 T* q = pCur + (num_indices >> 1) * 2;
874 874
875 for ( ; p != q; p += 2) 875 for ( ; p != q; p += 2)
876 { 876 {
877 uint index0 = p[0]; 877 uint index0 = p[0];
878 uint index1 = p[1]; 878 uint index1 = p[1];
879 879
880 uint c0 = (RG_ETC1_GET_KEY_FROM_INDEX(index0) >> pass_shift) & 0xFF; 880 uint c0 = (RG_ETC1_GET_KEY_FROM_INDEX(index0) >> pass_shift) & 0xFF;
881 uint c1 = (RG_ETC1_GET_KEY_FROM_INDEX(index1) >> pass_shift) & 0xFF; 881 uint c1 = (RG_ETC1_GET_KEY_FROM_INDEX(index1) >> pass_shift) & 0xFF;
882 882
883 if (c0 == c1) 883 if (c0 == c1)
884 { 884 {
885 uint dst_offset0 = offsets[c0]; 885 uint dst_offset0 = offsets[c0];
886 886
887 offsets[c0] = dst_offset0 + 2; 887 offsets[c0] = dst_offset0 + 2;
888 888
889 pNew[dst_offset0] = static_cast<T>(index0); 889 pNew[dst_offset0] = static_cast<T>(index0);
890 pNew[dst_offset0 + 1] = static_cast<T>(index1); 890 pNew[dst_offset0 + 1] = static_cast<T>(index1);
891 } 891 }
892 else 892 else
893 { 893 {
894 uint dst_offset0 = offsets[c0]++; 894 uint dst_offset0 = offsets[c0]++;
895 uint dst_offset1 = offsets[c1]++; 895 uint dst_offset1 = offsets[c1]++;
896 896
897 pNew[dst_offset0] = static_cast<T>(index0); 897 pNew[dst_offset0] = static_cast<T>(index0);
898 pNew[dst_offset1] = static_cast<T>(index1); 898 pNew[dst_offset1] = static_cast<T>(index1);
899 } 899 }
900 } 900 }
901 901
902 if (num_indices & 1) 902 if (num_indices & 1)
903 { 903 {
904 uint index = *p; 904 uint index = *p;
905 uint c = (RG_ETC1_GET_KEY_FROM_INDEX(index) >> pass_shift) & 0xFF; 905 uint c = (RG_ETC1_GET_KEY_FROM_INDEX(index) >> pass_shift) & 0xFF;
906 906
907 uint dst_offset = offsets[c]; 907 uint dst_offset = offsets[c];
908 offsets[c] = dst_offset + 1; 908 offsets[c] = dst_offset + 1;
909 909
910 pNew[dst_offset] = static_cast<T>(index); 910 pNew[dst_offset] = static_cast<T>(index);
911 } 911 }
912 912
913 T* t = pCur; 913 T* t = pCur;
914 pCur = pNew; 914 pCur = pNew;
915 pNew = t; 915 pNew = t;
916 } 916 }
917 917
918 return pCur; 918 return pCur;
919 } 919 }
920 920
921#undef RG_ETC1_GET_KEY 921#undef RG_ETC1_GET_KEY
922#undef RG_ETC1_GET_KEY_FROM_INDEX 922#undef RG_ETC1_GET_KEY_FROM_INDEX
923 923
924 uint16 etc1_block::pack_color5(const color_quad_u8& color, bool scaled, uint bias) 924 uint16 etc1_block::pack_color5(const color_quad_u8& color, bool scaled, uint bias)
925 { 925 {
926 return pack_color5(color.r, color.g, color.b, scaled, bias); 926 return pack_color5(color.r, color.g, color.b, scaled, bias);
927 } 927 }
928 928
929 uint16 etc1_block::pack_color5(uint r, uint g, uint b, bool scaled, uint bias) 929 uint16 etc1_block::pack_color5(uint r, uint g, uint b, bool scaled, uint bias)
930 { 930 {
931 if (scaled) 931 if (scaled)
932 { 932 {
933 r = (r * 31U + bias) / 255U; 933 r = (r * 31U + bias) / 255U;
934 g = (g * 31U + bias) / 255U; 934 g = (g * 31U + bias) / 255U;
935 b = (b * 31U + bias) / 255U; 935 b = (b * 31U + bias) / 255U;
936 } 936 }
937 937
938 r = rg_etc1::minimum(r, 31U); 938 r = rg_etc1::minimum(r, 31U);
939 g = rg_etc1::minimum(g, 31U); 939 g = rg_etc1::minimum(g, 31U);
940 b = rg_etc1::minimum(b, 31U); 940 b = rg_etc1::minimum(b, 31U);
941 941
942 return static_cast<uint16>(b | (g << 5U) | (r << 10U)); 942 return static_cast<uint16>(b | (g << 5U) | (r << 10U));
943 } 943 }
944 944
945 color_quad_u8 etc1_block::unpack_color5(uint16 packed_color5, bool scaled, uint alpha) 945 color_quad_u8 etc1_block::unpack_color5(uint16 packed_color5, bool scaled, uint alpha)
946 { 946 {
947 uint b = packed_color5 & 31U; 947 uint b = packed_color5 & 31U;
948 uint g = (packed_color5 >> 5U) & 31U; 948 uint g = (packed_color5 >> 5U) & 31U;
949 uint r = (packed_color5 >> 10U) & 31U; 949 uint r = (packed_color5 >> 10U) & 31U;
950 950
951 if (scaled) 951 if (scaled)
952 { 952 {
953 b = (b << 3U) | (b >> 2U); 953 b = (b << 3U) | (b >> 2U);
954 g = (g << 3U) | (g >> 2U); 954 g = (g << 3U) | (g >> 2U);
955 r = (r << 3U) | (r >> 2U); 955 r = (r << 3U) | (r >> 2U);
956 } 956 }
957 957
958 return color_quad_u8(cNoClamp, r, g, b, rg_etc1::minimum(alpha, 255U)); 958 return color_quad_u8(cNoClamp, r, g, b, rg_etc1::minimum(alpha, 255U));
959 } 959 }
960 960
961 void etc1_block::unpack_color5(uint& r, uint& g, uint& b, uint16 packed_color5, bool scaled) 961 void etc1_block::unpack_color5(uint& r, uint& g, uint& b, uint16 packed_color5, bool scaled)
962 { 962 {
963 color_quad_u8 c(unpack_color5(packed_color5, scaled, 0)); 963 color_quad_u8 c(unpack_color5(packed_color5, scaled, 0));
964 r = c.r; 964 r = c.r;
965 g = c.g; 965 g = c.g;
966 b = c.b; 966 b = c.b;
967 } 967 }
968 968
969 bool etc1_block::unpack_color5(color_quad_u8& result, uint16 packed_color5, uint16 packed_delta3, bool scaled, uint alpha) 969 bool etc1_block::unpack_color5(color_quad_u8& result, uint16 packed_color5, uint16 packed_delta3, bool scaled, uint alpha)
970 { 970 {
971 int dc_r, dc_g, dc_b; 971 int dc_r, dc_g, dc_b;
972 unpack_delta3(dc_r, dc_g, dc_b, packed_delta3); 972 unpack_delta3(dc_r, dc_g, dc_b, packed_delta3);
973 973
974 int b = (packed_color5 & 31U) + dc_b; 974 int b = (packed_color5 & 31U) + dc_b;
975 int g = ((packed_color5 >> 5U) & 31U) + dc_g; 975 int g = ((packed_color5 >> 5U) & 31U) + dc_g;
976 int r = ((packed_color5 >> 10U) & 31U) + dc_r; 976 int r = ((packed_color5 >> 10U) & 31U) + dc_r;
977 977
978 bool success = true; 978 bool success = true;
979 if (static_cast<uint>(r | g | b) > 31U) 979 if (static_cast<uint>(r | g | b) > 31U)
980 { 980 {
981 success = false; 981 success = false;
982 r = rg_etc1::clamp<int>(r, 0, 31); 982 r = rg_etc1::clamp<int>(r, 0, 31);
983 g = rg_etc1::clamp<int>(g, 0, 31); 983 g = rg_etc1::clamp<int>(g, 0, 31);
984 b = rg_etc1::clamp<int>(b, 0, 31); 984 b = rg_etc1::clamp<int>(b, 0, 31);
985 } 985 }
986 986
987 if (scaled) 987 if (scaled)
988 { 988 {
989 b = (b << 3U) | (b >> 2U); 989 b = (b << 3U) | (b >> 2U);
990 g = (g << 3U) | (g >> 2U); 990 g = (g << 3U) | (g >> 2U);
991 r = (r << 3U) | (r >> 2U); 991 r = (r << 3U) | (r >> 2U);
992 } 992 }
993 993
994 result.set_noclamp_rgba(r, g, b, rg_etc1::minimum(alpha, 255U)); 994 result.set_noclamp_rgba(r, g, b, rg_etc1::minimum(alpha, 255U));
995 return success; 995 return success;
996 } 996 }
997 997
998 bool etc1_block::unpack_color5(uint& r, uint& g, uint& b, uint16 packed_color5, uint16 packed_delta3, bool scaled, uint alpha) 998 bool etc1_block::unpack_color5(uint& r, uint& g, uint& b, uint16 packed_color5, uint16 packed_delta3, bool scaled, uint alpha)
999 { 999 {
1000 color_quad_u8 result; 1000 color_quad_u8 result;
1001 const bool success = unpack_color5(result, packed_color5, packed_delta3, scaled, alpha); 1001 const bool success = unpack_color5(result, packed_color5, packed_delta3, scaled, alpha);
1002 r = result.r; 1002 r = result.r;
1003 g = result.g; 1003 g = result.g;
1004 b = result.b; 1004 b = result.b;
1005 return success; 1005 return success;
1006 } 1006 }
1007 1007
1008 uint16 etc1_block::pack_delta3(int r, int g, int b) 1008 uint16 etc1_block::pack_delta3(int r, int g, int b)
1009 { 1009 {
1010 RG_ETC1_ASSERT((r >= cETC1ColorDeltaMin) && (r <= cETC1ColorDeltaMax)); 1010 RG_ETC1_ASSERT((r >= cETC1ColorDeltaMin) && (r <= cETC1ColorDeltaMax));
1011 RG_ETC1_ASSERT((g >= cETC1ColorDeltaMin) && (g <= cETC1ColorDeltaMax)); 1011 RG_ETC1_ASSERT((g >= cETC1ColorDeltaMin) && (g <= cETC1ColorDeltaMax));
1012 RG_ETC1_ASSERT((b >= cETC1ColorDeltaMin) && (b <= cETC1ColorDeltaMax)); 1012 RG_ETC1_ASSERT((b >= cETC1ColorDeltaMin) && (b <= cETC1ColorDeltaMax));
1013 if (r < 0) r += 8; 1013 if (r < 0) r += 8;
1014 if (g < 0) g += 8; 1014 if (g < 0) g += 8;
1015 if (b < 0) b += 8; 1015 if (b < 0) b += 8;
1016 return static_cast<uint16>(b | (g << 3) | (r << 6)); 1016 return static_cast<uint16>(b | (g << 3) | (r << 6));
1017 } 1017 }
1018 1018
1019 void etc1_block::unpack_delta3(int& r, int& g, int& b, uint16 packed_delta3) 1019 void etc1_block::unpack_delta3(int& r, int& g, int& b, uint16 packed_delta3)
1020 { 1020 {
1021 r = (packed_delta3 >> 6) & 7; 1021 r = (packed_delta3 >> 6) & 7;
1022 g = (packed_delta3 >> 3) & 7; 1022 g = (packed_delta3 >> 3) & 7;
1023 b = packed_delta3 & 7; 1023 b = packed_delta3 & 7;
1024 if (r >= 4) r -= 8; 1024 if (r >= 4) r -= 8;
1025 if (g >= 4) g -= 8; 1025 if (g >= 4) g -= 8;
1026 if (b >= 4) b -= 8; 1026 if (b >= 4) b -= 8;
1027 } 1027 }
1028 1028
1029 uint16 etc1_block::pack_color4(const color_quad_u8& color, bool scaled, uint bias) 1029 uint16 etc1_block::pack_color4(const color_quad_u8& color, bool scaled, uint bias)
1030 { 1030 {
1031 return pack_color4(color.r, color.g, color.b, scaled, bias); 1031 return pack_color4(color.r, color.g, color.b, scaled, bias);
1032 } 1032 }
1033 1033
1034 uint16 etc1_block::pack_color4(uint r, uint g, uint b, bool scaled, uint bias) 1034 uint16 etc1_block::pack_color4(uint r, uint g, uint b, bool scaled, uint bias)
1035 { 1035 {
1036 if (scaled) 1036 if (scaled)
1037 { 1037 {
1038 r = (r * 15U + bias) / 255U; 1038 r = (r * 15U + bias) / 255U;
1039 g = (g * 15U + bias) / 255U; 1039 g = (g * 15U + bias) / 255U;
1040 b = (b * 15U + bias) / 255U; 1040 b = (b * 15U + bias) / 255U;
1041 } 1041 }
1042 1042
1043 r = rg_etc1::minimum(r, 15U); 1043 r = rg_etc1::minimum(r, 15U);
1044 g = rg_etc1::minimum(g, 15U); 1044 g = rg_etc1::minimum(g, 15U);
1045 b = rg_etc1::minimum(b, 15U); 1045 b = rg_etc1::minimum(b, 15U);
1046 1046
1047 return static_cast<uint16>(b | (g << 4U) | (r << 8U)); 1047 return static_cast<uint16>(b | (g << 4U) | (r << 8U));
1048 } 1048 }
1049 1049
1050 color_quad_u8 etc1_block::unpack_color4(uint16 packed_color4, bool scaled, uint alpha) 1050 color_quad_u8 etc1_block::unpack_color4(uint16 packed_color4, bool scaled, uint alpha)
1051 { 1051 {
1052 uint b = packed_color4 & 15U; 1052 uint b = packed_color4 & 15U;
1053 uint g = (packed_color4 >> 4U) & 15U; 1053 uint g = (packed_color4 >> 4U) & 15U;
1054 uint r = (packed_color4 >> 8U) & 15U; 1054 uint r = (packed_color4 >> 8U) & 15U;
1055 1055
1056 if (scaled) 1056 if (scaled)
1057 { 1057 {
1058 b = (b << 4U) | b; 1058 b = (b << 4U) | b;
1059 g = (g << 4U) | g; 1059 g = (g << 4U) | g;
1060 r = (r << 4U) | r; 1060 r = (r << 4U) | r;
1061 } 1061 }
1062 1062
1063 return color_quad_u8(cNoClamp, r, g, b, rg_etc1::minimum(alpha, 255U)); 1063 return color_quad_u8(cNoClamp, r, g, b, rg_etc1::minimum(alpha, 255U));
1064 } 1064 }
1065 1065
1066 void etc1_block::unpack_color4(uint& r, uint& g, uint& b, uint16 packed_color4, bool scaled) 1066 void etc1_block::unpack_color4(uint& r, uint& g, uint& b, uint16 packed_color4, bool scaled)
1067 { 1067 {
1068 color_quad_u8 c(unpack_color4(packed_color4, scaled, 0)); 1068 color_quad_u8 c(unpack_color4(packed_color4, scaled, 0));
1069 r = c.r; 1069 r = c.r;
1070 g = c.g; 1070 g = c.g;
1071 b = c.b; 1071 b = c.b;
1072 } 1072 }
1073 1073
1074 void etc1_block::get_diff_subblock_colors(color_quad_u8* pDst, uint16 packed_color5, uint table_idx) 1074 void etc1_block::get_diff_subblock_colors(color_quad_u8* pDst, uint16 packed_color5, uint table_idx)
1075 { 1075 {
1076 RG_ETC1_ASSERT(table_idx < cETC1IntenModifierValues); 1076 RG_ETC1_ASSERT(table_idx < cETC1IntenModifierValues);
1077 const int *pInten_modifer_table = &g_etc1_inten_tables[table_idx][0]; 1077 const int *pInten_modifer_table = &g_etc1_inten_tables[table_idx][0];
1078 1078
1079 uint r, g, b; 1079 uint r, g, b;
1080 unpack_color5(r, g, b, packed_color5, true); 1080 unpack_color5(r, g, b, packed_color5, true);
1081 1081
1082 const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b); 1082 const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b);
1083 1083
1084 const int y0 = pInten_modifer_table[0]; 1084 const int y0 = pInten_modifer_table[0];
1085 pDst[0].set(ir + y0, ig + y0, ib + y0); 1085 pDst[0].set(ir + y0, ig + y0, ib + y0);
1086 1086
1087 const int y1 = pInten_modifer_table[1]; 1087 const int y1 = pInten_modifer_table[1];
1088 pDst[1].set(ir + y1, ig + y1, ib + y1); 1088 pDst[1].set(ir + y1, ig + y1, ib + y1);
1089 1089
1090 const int y2 = pInten_modifer_table[2]; 1090 const int y2 = pInten_modifer_table[2];
1091 pDst[2].set(ir + y2, ig + y2, ib + y2); 1091 pDst[2].set(ir + y2, ig + y2, ib + y2);
1092 1092
1093 const int y3 = pInten_modifer_table[3]; 1093 const int y3 = pInten_modifer_table[3];
1094 pDst[3].set(ir + y3, ig + y3, ib + y3); 1094 pDst[3].set(ir + y3, ig + y3, ib + y3);
1095 } 1095 }
1096 1096
1097 bool etc1_block::get_diff_subblock_colors(color_quad_u8* pDst, uint16 packed_color5, uint16 packed_delta3, uint table_idx) 1097 bool etc1_block::get_diff_subblock_colors(color_quad_u8* pDst, uint16 packed_color5, uint16 packed_delta3, uint table_idx)
1098 { 1098 {
1099 RG_ETC1_ASSERT(table_idx < cETC1IntenModifierValues); 1099 RG_ETC1_ASSERT(table_idx < cETC1IntenModifierValues);
1100 const int *pInten_modifer_table = &g_etc1_inten_tables[table_idx][0]; 1100 const int *pInten_modifer_table = &g_etc1_inten_tables[table_idx][0];
1101 1101
1102 uint r, g, b; 1102 uint r, g, b;
1103 bool success = unpack_color5(r, g, b, packed_color5, packed_delta3, true); 1103 bool success = unpack_color5(r, g, b, packed_color5, packed_delta3, true);
1104 1104
1105 const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b); 1105 const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b);
1106 1106
1107 const int y0 = pInten_modifer_table[0]; 1107 const int y0 = pInten_modifer_table[0];
1108 pDst[0].set(ir + y0, ig + y0, ib + y0); 1108 pDst[0].set(ir + y0, ig + y0, ib + y0);
1109 1109
1110 const int y1 = pInten_modifer_table[1]; 1110 const int y1 = pInten_modifer_table[1];
1111 pDst[1].set(ir + y1, ig + y1, ib + y1); 1111 pDst[1].set(ir + y1, ig + y1, ib + y1);
1112 1112
1113 const int y2 = pInten_modifer_table[2]; 1113 const int y2 = pInten_modifer_table[2];
1114 pDst[2].set(ir + y2, ig + y2, ib + y2); 1114 pDst[2].set(ir + y2, ig + y2, ib + y2);
1115 1115
1116 const int y3 = pInten_modifer_table[3]; 1116 const int y3 = pInten_modifer_table[3];
1117 pDst[3].set(ir + y3, ig + y3, ib + y3); 1117 pDst[3].set(ir + y3, ig + y3, ib + y3);
1118 1118
1119 return success; 1119 return success;
1120 } 1120 }
1121 1121
1122 void etc1_block::get_abs_subblock_colors(color_quad_u8* pDst, uint16 packed_color4, uint table_idx) 1122 void etc1_block::get_abs_subblock_colors(color_quad_u8* pDst, uint16 packed_color4, uint table_idx)
1123 { 1123 {
1124 RG_ETC1_ASSERT(table_idx < cETC1IntenModifierValues); 1124 RG_ETC1_ASSERT(table_idx < cETC1IntenModifierValues);
1125 const int *pInten_modifer_table = &g_etc1_inten_tables[table_idx][0]; 1125 const int *pInten_modifer_table = &g_etc1_inten_tables[table_idx][0];
1126 1126
1127 uint r, g, b; 1127 uint r, g, b;
1128 unpack_color4(r, g, b, packed_color4, true); 1128 unpack_color4(r, g, b, packed_color4, true);
1129 1129
1130 const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b); 1130 const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b);
1131 1131
1132 const int y0 = pInten_modifer_table[0]; 1132 const int y0 = pInten_modifer_table[0];
1133 pDst[0].set(ir + y0, ig + y0, ib + y0); 1133 pDst[0].set(ir + y0, ig + y0, ib + y0);
1134 1134
1135 const int y1 = pInten_modifer_table[1]; 1135 const int y1 = pInten_modifer_table[1];
1136 pDst[1].set(ir + y1, ig + y1, ib + y1); 1136 pDst[1].set(ir + y1, ig + y1, ib + y1);
1137 1137
1138 const int y2 = pInten_modifer_table[2]; 1138 const int y2 = pInten_modifer_table[2];
1139 pDst[2].set(ir + y2, ig + y2, ib + y2); 1139 pDst[2].set(ir + y2, ig + y2, ib + y2);
1140 1140
1141 const int y3 = pInten_modifer_table[3]; 1141 const int y3 = pInten_modifer_table[3];
1142 pDst[3].set(ir + y3, ig + y3, ib + y3); 1142 pDst[3].set(ir + y3, ig + y3, ib + y3);
1143 } 1143 }
1144 1144
1145 bool unpack_etc1_block(const void* pETC1_block, unsigned int* pDst_pixels_rgba, bool preserve_alpha) 1145 bool unpack_etc1_block(const void* pETC1_block, unsigned int* pDst_pixels_rgba, bool preserve_alpha)
1146 { 1146 {
1147 color_quad_u8* pDst = reinterpret_cast<color_quad_u8*>(pDst_pixels_rgba); 1147 color_quad_u8* pDst = reinterpret_cast<color_quad_u8*>(pDst_pixels_rgba);
1148 const etc1_block& block = *static_cast<const etc1_block*>(pETC1_block); 1148 const etc1_block& block = *static_cast<const etc1_block*>(pETC1_block);
1149 1149
1150 const bool diff_flag = block.get_diff_bit(); 1150 const bool diff_flag = block.get_diff_bit();
1151 const bool flip_flag = block.get_flip_bit(); 1151 const bool flip_flag = block.get_flip_bit();
1152 const uint table_index0 = block.get_inten_table(0); 1152 const uint table_index0 = block.get_inten_table(0);
1153 const uint table_index1 = block.get_inten_table(1); 1153 const uint table_index1 = block.get_inten_table(1);
1154 1154
1155 color_quad_u8 subblock_colors0[4]; 1155 color_quad_u8 subblock_colors0[4];
1156 color_quad_u8 subblock_colors1[4]; 1156 color_quad_u8 subblock_colors1[4];
1157 bool success = true; 1157 bool success = true;
1158 1158
1159 if (diff_flag) 1159 if (diff_flag)
1160 { 1160 {
1161 const uint16 base_color5 = block.get_base5_color(); 1161 const uint16 base_color5 = block.get_base5_color();
1162 const uint16 delta_color3 = block.get_delta3_color(); 1162 const uint16 delta_color3 = block.get_delta3_color();
1163 etc1_block::get_diff_subblock_colors(subblock_colors0, base_color5, table_index0); 1163 etc1_block::get_diff_subblock_colors(subblock_colors0, base_color5, table_index0);
1164 1164
1165 if (!etc1_block::get_diff_subblock_colors(subblock_colors1, base_color5, delta_color3, table_index1)) 1165 if (!etc1_block::get_diff_subblock_colors(subblock_colors1, base_color5, delta_color3, table_index1))
1166 success = false; 1166 success = false;
1167 } 1167 }
1168 else 1168 else
1169 { 1169 {
1170 const uint16 base_color4_0 = block.get_base4_color(0); 1170 const uint16 base_color4_0 = block.get_base4_color(0);
1171 etc1_block::get_abs_subblock_colors(subblock_colors0, base_color4_0, table_index0); 1171 etc1_block::get_abs_subblock_colors(subblock_colors0, base_color4_0, table_index0);
1172 1172
1173 const uint16 base_color4_1 = block.get_base4_color(1); 1173 const uint16 base_color4_1 = block.get_base4_color(1);
1174 etc1_block::get_abs_subblock_colors(subblock_colors1, base_color4_1, table_index1); 1174 etc1_block::get_abs_subblock_colors(subblock_colors1, base_color4_1, table_index1);
1175 } 1175 }
1176 1176
1177 if (preserve_alpha) 1177 if (preserve_alpha)
1178 { 1178 {
1179 if (flip_flag) 1179 if (flip_flag)
1180 { 1180 {
1181 for (uint y = 0; y < 2; y++) 1181 for (uint y = 0; y < 2; y++)
1182 { 1182 {
1183 pDst[0].set_rgb(subblock_colors0[block.get_selector(0, y)]); 1183 pDst[0].set_rgb(subblock_colors0[block.get_selector(0, y)]);
1184 pDst[1].set_rgb(subblock_colors0[block.get_selector(1, y)]); 1184 pDst[1].set_rgb(subblock_colors0[block.get_selector(1, y)]);
1185 pDst[2].set_rgb(subblock_colors0[block.get_selector(2, y)]); 1185 pDst[2].set_rgb(subblock_colors0[block.get_selector(2, y)]);
1186 pDst[3].set_rgb(subblock_colors0[block.get_selector(3, y)]); 1186 pDst[3].set_rgb(subblock_colors0[block.get_selector(3, y)]);
1187 pDst += 4; 1187 pDst += 4;
1188 } 1188 }
1189 1189
1190 for (uint y = 2; y < 4; y++) 1190 for (uint y = 2; y < 4; y++)
1191 { 1191 {
1192 pDst[0].set_rgb(subblock_colors1[block.get_selector(0, y)]); 1192 pDst[0].set_rgb(subblock_colors1[block.get_selector(0, y)]);
1193 pDst[1].set_rgb(subblock_colors1[block.get_selector(1, y)]); 1193 pDst[1].set_rgb(subblock_colors1[block.get_selector(1, y)]);
1194 pDst[2].set_rgb(subblock_colors1[block.get_selector(2, y)]); 1194 pDst[2].set_rgb(subblock_colors1[block.get_selector(2, y)]);
1195 pDst[3].set_rgb(subblock_colors1[block.get_selector(3, y)]); 1195 pDst[3].set_rgb(subblock_colors1[block.get_selector(3, y)]);
1196 pDst += 4; 1196 pDst += 4;
1197 } 1197 }
1198 } 1198 }
1199 else 1199 else
1200 { 1200 {
1201 for (uint y = 0; y < 4; y++) 1201 for (uint y = 0; y < 4; y++)
1202 { 1202 {
1203 pDst[0].set_rgb(subblock_colors0[block.get_selector(0, y)]); 1203 pDst[0].set_rgb(subblock_colors0[block.get_selector(0, y)]);
1204 pDst[1].set_rgb(subblock_colors0[block.get_selector(1, y)]); 1204 pDst[1].set_rgb(subblock_colors0[block.get_selector(1, y)]);
1205 pDst[2].set_rgb(subblock_colors1[block.get_selector(2, y)]); 1205 pDst[2].set_rgb(subblock_colors1[block.get_selector(2, y)]);
1206 pDst[3].set_rgb(subblock_colors1[block.get_selector(3, y)]); 1206 pDst[3].set_rgb(subblock_colors1[block.get_selector(3, y)]);
1207 pDst += 4; 1207 pDst += 4;
1208 } 1208 }
1209 } 1209 }
1210 } 1210 }
1211 else 1211 else
1212 { 1212 {
1213 if (flip_flag) 1213 if (flip_flag)
1214 { 1214 {
1215 // 0000 1215 // 0000
1216 // 0000 1216 // 0000
1217 // 1111 1217 // 1111
1218 // 1111 1218 // 1111
1219 for (uint y = 0; y < 2; y++) 1219 for (uint y = 0; y < 2; y++)
1220 { 1220 {
1221 pDst[0] = subblock_colors0[block.get_selector(0, y)]; 1221 pDst[0] = subblock_colors0[block.get_selector(0, y)];
1222 pDst[1] = subblock_colors0[block.get_selector(1, y)]; 1222 pDst[1] = subblock_colors0[block.get_selector(1, y)];
1223 pDst[2] = subblock_colors0[block.get_selector(2, y)]; 1223 pDst[2] = subblock_colors0[block.get_selector(2, y)];
1224 pDst[3] = subblock_colors0[block.get_selector(3, y)]; 1224 pDst[3] = subblock_colors0[block.get_selector(3, y)];
1225 pDst += 4; 1225 pDst += 4;
1226 } 1226 }
1227 1227
1228 for (uint y = 2; y < 4; y++) 1228 for (uint y = 2; y < 4; y++)
1229 { 1229 {
1230 pDst[0] = subblock_colors1[block.get_selector(0, y)]; 1230 pDst[0] = subblock_colors1[block.get_selector(0, y)];
1231 pDst[1] = subblock_colors1[block.get_selector(1, y)]; 1231 pDst[1] = subblock_colors1[block.get_selector(1, y)];
1232 pDst[2] = subblock_colors1[block.get_selector(2, y)]; 1232 pDst[2] = subblock_colors1[block.get_selector(2, y)];
1233 pDst[3] = subblock_colors1[block.get_selector(3, y)]; 1233 pDst[3] = subblock_colors1[block.get_selector(3, y)];
1234 pDst += 4; 1234 pDst += 4;
1235 } 1235 }
1236 } 1236 }
1237 else 1237 else
1238 { 1238 {
1239 // 0011 1239 // 0011
1240 // 0011 1240 // 0011
1241 // 0011 1241 // 0011
1242 // 0011 1242 // 0011
1243 for (uint y = 0; y < 4; y++) 1243 for (uint y = 0; y < 4; y++)
1244 { 1244 {
1245 pDst[0] = subblock_colors0[block.get_selector(0, y)]; 1245 pDst[0] = subblock_colors0[block.get_selector(0, y)];
1246 pDst[1] = subblock_colors0[block.get_selector(1, y)]; 1246 pDst[1] = subblock_colors0[block.get_selector(1, y)];
1247 pDst[2] = subblock_colors1[block.get_selector(2, y)]; 1247 pDst[2] = subblock_colors1[block.get_selector(2, y)];
1248 pDst[3] = subblock_colors1[block.get_selector(3, y)]; 1248 pDst[3] = subblock_colors1[block.get_selector(3, y)];
1249 pDst += 4; 1249 pDst += 4;
1250 } 1250 }
1251 } 1251 }
1252 } 1252 }
1253 1253
1254 return success; 1254 return success;
1255 } 1255 }
1256 1256
1257 struct etc1_solution_coordinates 1257 struct etc1_solution_coordinates
1258 { 1258 {
1259 inline etc1_solution_coordinates() : 1259 inline etc1_solution_coordinates() :
1260 m_unscaled_color(0, 0, 0, 0), 1260 m_unscaled_color(0, 0, 0, 0),
1261 m_inten_table(0), 1261 m_inten_table(0),
1262 m_color4(false) 1262 m_color4(false)
1263 { 1263 {
1264 } 1264 }
1265 1265
1266 inline etc1_solution_coordinates(uint r, uint g, uint b, uint inten_table, bool color4) : 1266 inline etc1_solution_coordinates(uint r, uint g, uint b, uint inten_table, bool color4) :
1267 m_unscaled_color(r, g, b, 255), 1267 m_unscaled_color(r, g, b, 255),
1268 m_inten_table(inten_table), 1268 m_inten_table(inten_table),
1269 m_color4(color4) 1269 m_color4(color4)
1270 { 1270 {
1271 } 1271 }
1272 1272
1273 inline etc1_solution_coordinates(const color_quad_u8& c, uint inten_table, bool color4) : 1273 inline etc1_solution_coordinates(const color_quad_u8& c, uint inten_table, bool color4) :
1274 m_unscaled_color(c), 1274 m_unscaled_color(c),
1275 m_inten_table(inten_table), 1275 m_inten_table(inten_table),
1276 m_color4(color4) 1276 m_color4(color4)
1277 { 1277 {
1278 } 1278 }
1279 1279
1280 inline etc1_solution_coordinates(const etc1_solution_coordinates& other) 1280 inline etc1_solution_coordinates(const etc1_solution_coordinates& other)
1281 { 1281 {
1282 *this = other; 1282 *this = other;
1283 } 1283 }
1284 1284
1285 inline etc1_solution_coordinates& operator= (const etc1_solution_coordinates& rhs) 1285 inline etc1_solution_coordinates& operator= (const etc1_solution_coordinates& rhs)
1286 { 1286 {
1287 m_unscaled_color = rhs.m_unscaled_color; 1287 m_unscaled_color = rhs.m_unscaled_color;
1288 m_inten_table = rhs.m_inten_table; 1288 m_inten_table = rhs.m_inten_table;
1289 m_color4 = rhs.m_color4; 1289 m_color4 = rhs.m_color4;
1290 return *this; 1290 return *this;
1291 } 1291 }
1292 1292
1293 inline void clear() 1293 inline void clear()
1294 { 1294 {
1295 m_unscaled_color.clear(); 1295 m_unscaled_color.clear();
1296 m_inten_table = 0; 1296 m_inten_table = 0;
1297 m_color4 = false; 1297 m_color4 = false;
1298 } 1298 }
1299 1299
1300 inline color_quad_u8 get_scaled_color() const 1300 inline color_quad_u8 get_scaled_color() const
1301 { 1301 {
1302 int br, bg, bb; 1302 int br, bg, bb;
1303 if (m_color4) 1303 if (m_color4)
1304 { 1304 {
1305 br = m_unscaled_color.r | (m_unscaled_color.r << 4); 1305 br = m_unscaled_color.r | (m_unscaled_color.r << 4);
1306 bg = m_unscaled_color.g | (m_unscaled_color.g << 4); 1306 bg = m_unscaled_color.g | (m_unscaled_color.g << 4);
1307 bb = m_unscaled_color.b | (m_unscaled_color.b << 4); 1307 bb = m_unscaled_color.b | (m_unscaled_color.b << 4);
1308 } 1308 }
1309 else 1309 else
1310 { 1310 {
1311 br = (m_unscaled_color.r >> 2) | (m_unscaled_color.r << 3); 1311 br = (m_unscaled_color.r >> 2) | (m_unscaled_color.r << 3);
1312 bg = (m_unscaled_color.g >> 2) | (m_unscaled_color.g << 3); 1312 bg = (m_unscaled_color.g >> 2) | (m_unscaled_color.g << 3);
1313 bb = (m_unscaled_color.b >> 2) | (m_unscaled_color.b << 3); 1313 bb = (m_unscaled_color.b >> 2) | (m_unscaled_color.b << 3);
1314 } 1314 }
1315 return color_quad_u8(br, bg, bb); 1315 return color_quad_u8(br, bg, bb);
1316 } 1316 }
1317 1317
1318 inline void get_block_colors(color_quad_u8* pBlock_colors) 1318 inline void get_block_colors(color_quad_u8* pBlock_colors)
1319 { 1319 {
1320 int br, bg, bb; 1320 int br, bg, bb;
1321 if (m_color4) 1321 if (m_color4)
1322 { 1322 {
1323 br = m_unscaled_color.r | (m_unscaled_color.r << 4); 1323 br = m_unscaled_color.r | (m_unscaled_color.r << 4);
1324 bg = m_unscaled_color.g | (m_unscaled_color.g << 4); 1324 bg = m_unscaled_color.g | (m_unscaled_color.g << 4);
1325 bb = m_unscaled_color.b | (m_unscaled_color.b << 4); 1325 bb = m_unscaled_color.b | (m_unscaled_color.b << 4);
1326 } 1326 }
1327 else 1327 else
1328 { 1328 {
1329 br = (m_unscaled_color.r >> 2) | (m_unscaled_color.r << 3); 1329 br = (m_unscaled_color.r >> 2) | (m_unscaled_color.r << 3);
1330 bg = (m_unscaled_color.g >> 2) | (m_unscaled_color.g << 3); 1330 bg = (m_unscaled_color.g >> 2) | (m_unscaled_color.g << 3);
1331 bb = (m_unscaled_color.b >> 2) | (m_unscaled_color.b << 3); 1331 bb = (m_unscaled_color.b >> 2) | (m_unscaled_color.b << 3);
1332 } 1332 }
1333 const int* pInten_table = g_etc1_inten_tables[m_inten_table]; 1333 const int* pInten_table = g_etc1_inten_tables[m_inten_table];
1334 pBlock_colors[0].set(br + pInten_table[0], bg + pInten_table[0], bb + pInten_table[0]); 1334 pBlock_colors[0].set(br + pInten_table[0], bg + pInten_table[0], bb + pInten_table[0]);
1335 pBlock_colors[1].set(br + pInten_table[1], bg + pInten_table[1], bb + pInten_table[1]); 1335 pBlock_colors[1].set(br + pInten_table[1], bg + pInten_table[1], bb + pInten_table[1]);
1336 pBlock_colors[2].set(br + pInten_table[2], bg + pInten_table[2], bb + pInten_table[2]); 1336 pBlock_colors[2].set(br + pInten_table[2], bg + pInten_table[2], bb + pInten_table[2]);
1337 pBlock_colors[3].set(br + pInten_table[3], bg + pInten_table[3], bb + pInten_table[3]); 1337 pBlock_colors[3].set(br + pInten_table[3], bg + pInten_table[3], bb + pInten_table[3]);
1338 } 1338 }
1339 1339
1340 color_quad_u8 m_unscaled_color; 1340 color_quad_u8 m_unscaled_color;
1341 uint m_inten_table; 1341 uint m_inten_table;
1342 bool m_color4; 1342 bool m_color4;
1343 }; 1343 };
1344 1344
1345 class etc1_optimizer 1345 class etc1_optimizer
1346 { 1346 {
1347 etc1_optimizer(const etc1_optimizer&); 1347 etc1_optimizer(const etc1_optimizer&);
1348 etc1_optimizer& operator= (const etc1_optimizer&); 1348 etc1_optimizer& operator= (const etc1_optimizer&);
1349 1349
1350 public: 1350 public:
1351 etc1_optimizer() 1351 etc1_optimizer()
1352 { 1352 {
1353 clear(); 1353 clear();
1354 } 1354 }
1355 1355
1356 void clear() 1356 void clear()
1357 { 1357 {
1358 m_pParams = NULL; 1358 m_pParams = NULL;
1359 m_pResult = NULL; 1359 m_pResult = NULL;
1360 m_pSorted_luma = NULL; 1360 m_pSorted_luma = NULL;
1361 m_pSorted_luma_indices = NULL; 1361 m_pSorted_luma_indices = NULL;
1362 } 1362 }
1363 1363
1364 struct params : etc1_pack_params 1364 struct params : etc1_pack_params
1365 { 1365 {
1366 params() 1366 params()
1367 { 1367 {
1368 clear(); 1368 clear();
1369 } 1369 }
1370 1370
1371 params(const etc1_pack_params& base_params) : 1371 params(const etc1_pack_params& base_params) :
1372 etc1_pack_params(base_params) 1372 etc1_pack_params(base_params)
1373 { 1373 {
1374 clear_optimizer_params(); 1374 clear_optimizer_params();
1375 } 1375 }
1376 1376
1377 void clear() 1377 void clear()
1378 { 1378 {
1379 etc1_pack_params::clear(); 1379 etc1_pack_params::clear();
1380 clear_optimizer_params(); 1380 clear_optimizer_params();
1381 } 1381 }
1382 1382
1383 void clear_optimizer_params() 1383 void clear_optimizer_params()
1384 { 1384 {
1385 m_num_src_pixels = 0; 1385 m_num_src_pixels = 0;
1386 m_pSrc_pixels = 0; 1386 m_pSrc_pixels = 0;
1387 1387
1388 m_use_color4 = false; 1388 m_use_color4 = false;
1389 static const int s_default_scan_delta[] = { 0 }; 1389 static const int s_default_scan_delta[] = { 0 };
1390 m_pScan_deltas = s_default_scan_delta; 1390 m_pScan_deltas = s_default_scan_delta;
1391 m_scan_delta_size = 1; 1391 m_scan_delta_size = 1;
1392 1392
1393 m_base_color5.clear(); 1393 m_base_color5.clear();
1394 m_constrain_against_base_color5 = false; 1394 m_constrain_against_base_color5 = false;
1395 } 1395 }
1396 1396
1397 uint m_num_src_pixels; 1397 uint m_num_src_pixels;
1398 const color_quad_u8* m_pSrc_pixels; 1398 const color_quad_u8* m_pSrc_pixels;
1399 1399
1400 bool m_use_color4; 1400 bool m_use_color4;
1401 const int* m_pScan_deltas; 1401 const int* m_pScan_deltas;
1402 uint m_scan_delta_size; 1402 uint m_scan_delta_size;
1403 1403
1404 color_quad_u8 m_base_color5; 1404 color_quad_u8 m_base_color5;
1405 bool m_constrain_against_base_color5; 1405 bool m_constrain_against_base_color5;
1406 }; 1406 };
1407 1407
1408 struct results 1408 struct results
1409 { 1409 {
1410 uint64 m_error; 1410 uint64 m_error;
1411 color_quad_u8 m_block_color_unscaled; 1411 color_quad_u8 m_block_color_unscaled;
1412 uint m_block_inten_table; 1412 uint m_block_inten_table;
1413 uint m_n; 1413 uint m_n;
1414 uint8* m_pSelectors; 1414 uint8* m_pSelectors;
1415 bool m_block_color4; 1415 bool m_block_color4;
1416 1416
1417 inline results& operator= (const results& rhs) 1417 inline results& operator= (const results& rhs)
1418 { 1418 {
1419 m_block_color_unscaled = rhs.m_block_color_unscaled; 1419 m_block_color_unscaled = rhs.m_block_color_unscaled;
1420 m_block_color4 = rhs.m_block_color4; 1420 m_block_color4 = rhs.m_block_color4;
1421 m_block_inten_table = rhs.m_block_inten_table; 1421 m_block_inten_table = rhs.m_block_inten_table;
1422 m_error = rhs.m_error; 1422 m_error = rhs.m_error;
1423 RG_ETC1_ASSERT(m_n == rhs.m_n); 1423 RG_ETC1_ASSERT(m_n == rhs.m_n);
1424 memcpy(m_pSelectors, rhs.m_pSelectors, rhs.m_n); 1424 memcpy(m_pSelectors, rhs.m_pSelectors, rhs.m_n);
1425 return *this; 1425 return *this;
1426 } 1426 }
1427 }; 1427 };
1428 1428
1429 void init(const params& params, results& result); 1429 void init(const params& params, results& result);
1430 bool compute(); 1430 bool compute();
1431 1431
1432 private: 1432 private:
1433 struct potential_solution 1433 struct potential_solution
1434 { 1434 {
1435 potential_solution() : m_coords(), m_error(cUINT64_MAX), m_valid(false) 1435 potential_solution() : m_coords(), m_error(cUINT64_MAX), m_valid(false)
1436 { 1436 {
1437 } 1437 }
1438 1438
1439 etc1_solution_coordinates m_coords; 1439 etc1_solution_coordinates m_coords;
1440 uint8 m_selectors[8]; 1440 uint8 m_selectors[8];
1441 uint64 m_error; 1441 uint64 m_error;
1442 bool m_valid; 1442 bool m_valid;
1443 1443
1444 void clear() 1444 void clear()
1445 { 1445 {
1446 m_coords.clear(); 1446 m_coords.clear();
1447 m_error = cUINT64_MAX; 1447 m_error = cUINT64_MAX;
1448 m_valid = false; 1448 m_valid = false;
1449 } 1449 }
1450 }; 1450 };
1451 1451
1452 const params* m_pParams; 1452 const params* m_pParams;
1453 results* m_pResult; 1453 results* m_pResult;
1454 1454
1455 int m_limit; 1455 int m_limit;
1456 1456
1457 vec3F m_avg_color; 1457 vec3F m_avg_color;
1458 int m_br, m_bg, m_bb; 1458 int m_br, m_bg, m_bb;
1459 uint16 m_luma[8]; 1459 uint16 m_luma[8];
1460 uint32 m_sorted_luma[2][8]; 1460 uint32 m_sorted_luma[2][8];
1461 const uint32* m_pSorted_luma_indices; 1461 const uint32* m_pSorted_luma_indices;
1462 uint32* m_pSorted_luma; 1462 uint32* m_pSorted_luma;
1463 1463
1464 uint8 m_selectors[8]; 1464 uint8 m_selectors[8];
1465 uint8 m_best_selectors[8]; 1465 uint8 m_best_selectors[8];
1466 1466
1467 potential_solution m_best_solution; 1467 potential_solution m_best_solution;
1468 potential_solution m_trial_solution; 1468 potential_solution m_trial_solution;
1469 uint8 m_temp_selectors[8]; 1469 uint8 m_temp_selectors[8];
1470 1470
1471 bool evaluate_solution(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution); 1471 bool evaluate_solution(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution);
1472 bool evaluate_solution_fast(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution); 1472 bool evaluate_solution_fast(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution);
1473 }; 1473 };
1474 1474
1475 bool etc1_optimizer::compute() 1475 bool etc1_optimizer::compute()
1476 { 1476 {
1477 const uint n = m_pParams->m_num_src_pixels; 1477 const uint n = m_pParams->m_num_src_pixels;
1478 const int scan_delta_size = m_pParams->m_scan_delta_size; 1478 const int scan_delta_size = m_pParams->m_scan_delta_size;
1479 1479
1480 // Scan through a subset of the 3D lattice centered around the avg block color trying each 3D (555 or 444) lattice point as a potential block color. 1480 // Scan through a subset of the 3D lattice centered around the avg block color trying each 3D (555 or 444) lattice point as a potential block color.
1481 // Each time a better solution is found try to refine the current solution's block color based of the current selectors and intensity table index. 1481 // Each time a better solution is found try to refine the current solution's block color based of the current selectors and intensity table index.
1482 for (int zdi = 0; zdi < scan_delta_size; zdi++) 1482 for (int zdi = 0; zdi < scan_delta_size; zdi++)
1483 { 1483 {
1484 const int zd = m_pParams->m_pScan_deltas[zdi]; 1484 const int zd = m_pParams->m_pScan_deltas[zdi];
1485 const int mbb = m_bb + zd; 1485 const int mbb = m_bb + zd;
1486 if (mbb < 0) continue; else if (mbb > m_limit) break; 1486 if (mbb < 0) continue; else if (mbb > m_limit) break;
1487 1487
1488 for (int ydi = 0; ydi < scan_delta_size; ydi++) 1488 for (int ydi = 0; ydi < scan_delta_size; ydi++)
1489 { 1489 {
1490 const int yd = m_pParams->m_pScan_deltas[ydi]; 1490 const int yd = m_pParams->m_pScan_deltas[ydi];
1491 const int mbg = m_bg + yd; 1491 const int mbg = m_bg + yd;
1492 if (mbg < 0) continue; else if (mbg > m_limit) break; 1492 if (mbg < 0) continue; else if (mbg > m_limit) break;
1493 1493
1494 for (int xdi = 0; xdi < scan_delta_size; xdi++) 1494 for (int xdi = 0; xdi < scan_delta_size; xdi++)
1495 { 1495 {
1496 const int xd = m_pParams->m_pScan_deltas[xdi]; 1496 const int xd = m_pParams->m_pScan_deltas[xdi];
1497 const int mbr = m_br + xd; 1497 const int mbr = m_br + xd;
1498 if (mbr < 0) continue; else if (mbr > m_limit) break; 1498 if (mbr < 0) continue; else if (mbr > m_limit) break;
1499 1499
1500 etc1_solution_coordinates coords(mbr, mbg, mbb, 0, m_pParams->m_use_color4); 1500 etc1_solution_coordinates coords(mbr, mbg, mbb, 0, m_pParams->m_use_color4);
1501 if (m_pParams->m_quality == cHighQuality) 1501 if (m_pParams->m_quality == cHighQuality)
1502 { 1502 {
1503 if (!evaluate_solution(coords, m_trial_solution, &m_best_solution)) 1503 if (!evaluate_solution(coords, m_trial_solution, &m_best_solution))
1504 continue; 1504 continue;
1505 } 1505 }
1506 else 1506 else
1507 { 1507 {
1508 if (!evaluate_solution_fast(coords, m_trial_solution, &m_best_solution)) 1508 if (!evaluate_solution_fast(coords, m_trial_solution, &m_best_solution))
1509 continue; 1509 continue;
1510 } 1510 }
1511 1511
1512 // Now we have the input block, the avg. color of the input pixels, a set of trial selector indices, and the block color+intensity index. 1512 // Now we have the input block, the avg. color of the input pixels, a set of trial selector indices, and the block color+intensity index.
1513 // Now, for each component, attempt to refine the current solution by solving a simple linear equation. For example, for 4 colors: 1513 // Now, for each component, attempt to refine the current solution by solving a simple linear equation. For example, for 4 colors:
1514 // The goal is: 1514 // The goal is:
1515 // pixel0 - (block_color+inten_table[selector0]) + pixel1 - (block_color+inten_table[selector1]) + pixel2 - (block_color+inten_table[selector2]) + pixel3 - (block_color+inten_table[selector3]) = 0 1515 // pixel0 - (block_color+inten_table[selector0]) + pixel1 - (block_color+inten_table[selector1]) + pixel2 - (block_color+inten_table[selector2]) + pixel3 - (block_color+inten_table[selector3]) = 0
1516 // Rearranging this: 1516 // Rearranging this:
1517 // (pixel0 + pixel1 + pixel2 + pixel3) - (block_color+inten_table[selector0]) - (block_color+inten_table[selector1]) - (block_color+inten_table[selector2]) - (block_color+inten_table[selector3]) = 0 1517 // (pixel0 + pixel1 + pixel2 + pixel3) - (block_color+inten_table[selector0]) - (block_color+inten_table[selector1]) - (block_color+inten_table[selector2]) - (block_color+inten_table[selector3]) = 0
1518 // (pixel0 + pixel1 + pixel2 + pixel3) - block_color - inten_table[selector0] - block_color-inten_table[selector1] - block_color-inten_table[selector2] - block_color-inten_table[selector3] = 0 1518 // (pixel0 + pixel1 + pixel2 + pixel3) - block_color - inten_table[selector0] - block_color-inten_table[selector1] - block_color-inten_table[selector2] - block_color-inten_table[selector3] = 0
1519 // (pixel0 + pixel1 + pixel2 + pixel3) - 4*block_color - inten_table[selector0] - inten_table[selector1] - inten_table[selector2] - inten_table[selector3] = 0 1519 // (pixel0 + pixel1 + pixel2 + pixel3) - 4*block_color - inten_table[selector0] - inten_table[selector1] - inten_table[selector2] - inten_table[selector3] = 0
1520 // (pixel0 + pixel1 + pixel2 + pixel3) - 4*block_color - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3]) = 0 1520 // (pixel0 + pixel1 + pixel2 + pixel3) - 4*block_color - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3]) = 0
1521 // (pixel0 + pixel1 + pixel2 + pixel3)/4 - block_color - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3])/4 = 0 1521 // (pixel0 + pixel1 + pixel2 + pixel3)/4 - block_color - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3])/4 = 0
1522 // block_color = (pixel0 + pixel1 + pixel2 + pixel3)/4 - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3])/4 1522 // block_color = (pixel0 + pixel1 + pixel2 + pixel3)/4 - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3])/4
1523 // So what this means: 1523 // So what this means:
1524 // optimal_block_color = avg_input - avg_inten_delta 1524 // optimal_block_color = avg_input - avg_inten_delta
1525 // So the optimal block color can be computed by taking the average block color and subtracting the current average of the intensity delta. 1525 // So the optimal block color can be computed by taking the average block color and subtracting the current average of the intensity delta.
1526 // Unfortunately, optimal_block_color must then be quantized to 555 or 444 so it's not always possible to improve matters using this formula. 1526 // Unfortunately, optimal_block_color must then be quantized to 555 or 444 so it's not always possible to improve matters using this formula.
1527 // Also, the above formula is for unclamped intensity deltas. The actual implementation takes into account clamping. 1527 // Also, the above formula is for unclamped intensity deltas. The actual implementation takes into account clamping.
1528 1528
1529 const uint max_refinement_trials = (m_pParams->m_quality == cLowQuality) ? 2 : (((xd | yd | zd) == 0) ? 4 : 2); 1529 const uint max_refinement_trials = (m_pParams->m_quality == cLowQuality) ? 2 : (((xd | yd | zd) == 0) ? 4 : 2);
1530 for (uint refinement_trial = 0; refinement_trial < max_refinement_trials; refinement_trial++) 1530 for (uint refinement_trial = 0; refinement_trial < max_refinement_trials; refinement_trial++)
1531 { 1531 {
1532 const uint8* pSelectors = m_best_solution.m_selectors; 1532 const uint8* pSelectors = m_best_solution.m_selectors;
1533 const int* pInten_table = g_etc1_inten_tables[m_best_solution.m_coords.m_inten_table]; 1533 const int* pInten_table = g_etc1_inten_tables[m_best_solution.m_coords.m_inten_table];
1534 1534
1535 int delta_sum_r = 0, delta_sum_g = 0, delta_sum_b = 0; 1535 int delta_sum_r = 0, delta_sum_g = 0, delta_sum_b = 0;
1536 const color_quad_u8 base_color(m_best_solution.m_coords.get_scaled_color()); 1536 const color_quad_u8 base_color(m_best_solution.m_coords.get_scaled_color());
1537 for (uint r = 0; r < n; r++) 1537 for (uint r = 0; r < n; r++)
1538 { 1538 {
1539 const uint s = *pSelectors++; 1539 const uint s = *pSelectors++;
1540 const int yd = pInten_table[s]; 1540 const int yd = pInten_table[s];
1541 // Compute actual delta being applied to each pixel, taking into account clamping. 1541 // Compute actual delta being applied to each pixel, taking into account clamping.
1542 delta_sum_r += rg_etc1::clamp<int>(base_color.r + yd, 0, 255) - base_color.r; 1542 delta_sum_r += rg_etc1::clamp<int>(base_color.r + yd, 0, 255) - base_color.r;
1543 delta_sum_g += rg_etc1::clamp<int>(base_color.g + yd, 0, 255) - base_color.g; 1543 delta_sum_g += rg_etc1::clamp<int>(base_color.g + yd, 0, 255) - base_color.g;
1544 delta_sum_b += rg_etc1::clamp<int>(base_color.b + yd, 0, 255) - base_color.b; 1544 delta_sum_b += rg_etc1::clamp<int>(base_color.b + yd, 0, 255) - base_color.b;
1545 } 1545 }
1546 if ((!delta_sum_r) && (!delta_sum_g) && (!delta_sum_b)) 1546 if ((!delta_sum_r) && (!delta_sum_g) && (!delta_sum_b))
1547 break; 1547 break;
1548 const float avg_delta_r_f = static_cast<float>(delta_sum_r) / n; 1548 const float avg_delta_r_f = static_cast<float>(delta_sum_r) / n;
1549 const float avg_delta_g_f = static_cast<float>(delta_sum_g) / n; 1549 const float avg_delta_g_f = static_cast<float>(delta_sum_g) / n;
1550 const float avg_delta_b_f = static_cast<float>(delta_sum_b) / n; 1550 const float avg_delta_b_f = static_cast<float>(delta_sum_b) / n;
1551 const int br1 = rg_etc1::clamp<int>(static_cast<uint>((m_avg_color[0] - avg_delta_r_f) * m_limit / 255.0f + .5f), 0, m_limit); 1551 const int br1 = rg_etc1::clamp<int>(static_cast<uint>((m_avg_color[0] - avg_delta_r_f) * m_limit / 255.0f + .5f), 0, m_limit);
1552 const int bg1 = rg_etc1::clamp<int>(static_cast<uint>((m_avg_color[1] - avg_delta_g_f) * m_limit / 255.0f + .5f), 0, m_limit); 1552 const int bg1 = rg_etc1::clamp<int>(static_cast<uint>((m_avg_color[1] - avg_delta_g_f) * m_limit / 255.0f + .5f), 0, m_limit);
1553 const int bb1 = rg_etc1::clamp<int>(static_cast<uint>((m_avg_color[2] - avg_delta_b_f) * m_limit / 255.0f + .5f), 0, m_limit); 1553 const int bb1 = rg_etc1::clamp<int>(static_cast<uint>((m_avg_color[2] - avg_delta_b_f) * m_limit / 255.0f + .5f), 0, m_limit);
1554 1554
1555 bool skip = false; 1555 bool skip = false;
1556 1556
1557 if ((mbr == br1) && (mbg == bg1) && (mbb == bb1)) 1557 if ((mbr == br1) && (mbg == bg1) && (mbb == bb1))
1558 skip = true; 1558 skip = true;
1559 else if ((br1 == m_best_solution.m_coords.m_unscaled_color.r) && (bg1 == m_best_solution.m_coords.m_unscaled_color.g) && (bb1 == m_best_solution.m_coords.m_unscaled_color.b)) 1559 else if ((br1 == m_best_solution.m_coords.m_unscaled_color.r) && (bg1 == m_best_solution.m_coords.m_unscaled_color.g) && (bb1 == m_best_solution.m_coords.m_unscaled_color.b))
1560 skip = true; 1560 skip = true;
1561 else if ((m_br == br1) && (m_bg == bg1) && (m_bb == bb1)) 1561 else if ((m_br == br1) && (m_bg == bg1) && (m_bb == bb1))
1562 skip = true; 1562 skip = true;
1563 1563
1564 if (skip) 1564 if (skip)
1565 break; 1565 break;
1566 1566
1567 etc1_solution_coordinates coords1(br1, bg1, bb1, 0, m_pParams->m_use_color4); 1567 etc1_solution_coordinates coords1(br1, bg1, bb1, 0, m_pParams->m_use_color4);
1568 if (m_pParams->m_quality == cHighQuality) 1568 if (m_pParams->m_quality == cHighQuality)
1569 { 1569 {
1570 if (!evaluate_solution(coords1, m_trial_solution, &m_best_solution)) 1570 if (!evaluate_solution(coords1, m_trial_solution, &m_best_solution))
1571 break; 1571 break;
1572 } 1572 }
1573 else 1573 else
1574 { 1574 {
1575 if (!evaluate_solution_fast(coords1, m_trial_solution, &m_best_solution)) 1575 if (!evaluate_solution_fast(coords1, m_trial_solution, &m_best_solution))
1576 break; 1576 break;
1577 } 1577 }
1578 1578
1579 } // refinement_trial 1579 } // refinement_trial
1580 1580
1581 } // xdi 1581 } // xdi
1582 } // ydi 1582 } // ydi
1583 } // zdi 1583 } // zdi
1584 1584
1585 if (!m_best_solution.m_valid) 1585 if (!m_best_solution.m_valid)
1586 { 1586 {
1587 m_pResult->m_error = cUINT32_MAX; 1587 m_pResult->m_error = cUINT32_MAX;
1588 return false; 1588 return false;
1589 } 1589 }
1590 1590
1591 const uint8* pSelectors = m_best_solution.m_selectors; 1591 const uint8* pSelectors = m_best_solution.m_selectors;
1592 1592
1593#ifdef RG_ETC1_BUILD_DEBUG 1593#ifdef RG_ETC1_BUILD_DEBUG
1594 { 1594 {
1595 color_quad_u8 block_colors[4]; 1595 color_quad_u8 block_colors[4];
1596 m_best_solution.m_coords.get_block_colors(block_colors); 1596 m_best_solution.m_coords.get_block_colors(block_colors);
1597 1597
1598 const color_quad_u8* pSrc_pixels = m_pParams->m_pSrc_pixels; 1598 const color_quad_u8* pSrc_pixels = m_pParams->m_pSrc_pixels;
1599 uint64 actual_error = 0; 1599 uint64 actual_error = 0;
1600 for (uint i = 0; i < n; i++) 1600 for (uint i = 0; i < n; i++)
1601 actual_error += pSrc_pixels[i].squared_distance_rgb(block_colors[pSelectors[i]]); 1601 actual_error += pSrc_pixels[i].squared_distance_rgb(block_colors[pSelectors[i]]);
1602 1602
1603 RG_ETC1_ASSERT(actual_error == m_best_solution.m_error); 1603 RG_ETC1_ASSERT(actual_error == m_best_solution.m_error);
1604 } 1604 }
1605#endif 1605#endif
1606 1606
1607 m_pResult->m_error = m_best_solution.m_error; 1607 m_pResult->m_error = m_best_solution.m_error;
1608 1608
1609 m_pResult->m_block_color_unscaled = m_best_solution.m_coords.m_unscaled_color; 1609 m_pResult->m_block_color_unscaled = m_best_solution.m_coords.m_unscaled_color;
1610 m_pResult->m_block_color4 = m_best_solution.m_coords.m_color4; 1610 m_pResult->m_block_color4 = m_best_solution.m_coords.m_color4;
1611 1611
1612 m_pResult->m_block_inten_table = m_best_solution.m_coords.m_inten_table; 1612 m_pResult->m_block_inten_table = m_best_solution.m_coords.m_inten_table;
1613 memcpy(m_pResult->m_pSelectors, pSelectors, n); 1613 memcpy(m_pResult->m_pSelectors, pSelectors, n);
1614 m_pResult->m_n = n; 1614 m_pResult->m_n = n;
1615 1615
1616 return true; 1616 return true;
1617 } 1617 }
1618 1618
1619 void etc1_optimizer::init(const params& p, results& r) 1619 void etc1_optimizer::init(const params& p, results& r)
1620 { 1620 {
1621 // This version is hardcoded for 8 pixel subblocks. 1621 // This version is hardcoded for 8 pixel subblocks.
1622 RG_ETC1_ASSERT(p.m_num_src_pixels == 8); 1622 RG_ETC1_ASSERT(p.m_num_src_pixels == 8);
1623 1623
1624 m_pParams = &p; 1624 m_pParams = &p;
1625 m_pResult = &r; 1625 m_pResult = &r;
1626 1626
1627 const uint n = 8; 1627 const uint n = 8;
1628 1628
1629 m_limit = m_pParams->m_use_color4 ? 15 : 31; 1629 m_limit = m_pParams->m_use_color4 ? 15 : 31;
1630 1630
1631 vec3F avg_color(0.0f); 1631 vec3F avg_color(0.0f);
1632 1632
1633 for (uint i = 0; i < n; i++) 1633 for (uint i = 0; i < n; i++)
1634 { 1634 {
1635 const color_quad_u8& c = m_pParams->m_pSrc_pixels[i]; 1635 const color_quad_u8& c = m_pParams->m_pSrc_pixels[i];
1636 const vec3F fc(c.r, c.g, c.b); 1636 const vec3F fc(c.r, c.g, c.b);
1637 1637
1638 avg_color += fc; 1638 avg_color += fc;
1639 1639
1640 m_luma[i] = static_cast<uint16>(c.r + c.g + c.b); 1640 m_luma[i] = static_cast<uint16>(c.r + c.g + c.b);
1641 m_sorted_luma[0][i] = i; 1641 m_sorted_luma[0][i] = i;
1642 } 1642 }
1643 avg_color *= (1.0f / static_cast<float>(n)); 1643 avg_color *= (1.0f / static_cast<float>(n));
1644 m_avg_color = avg_color; 1644 m_avg_color = avg_color;
1645 1645
1646 m_br = rg_etc1::clamp<int>(static_cast<uint>(m_avg_color[0] * m_limit / 255.0f + .5f), 0, m_limit); 1646 m_br = rg_etc1::clamp<int>(static_cast<uint>(m_avg_color[0] * m_limit / 255.0f + .5f), 0, m_limit);
1647 m_bg = rg_etc1::clamp<int>(static_cast<uint>(m_avg_color[1] * m_limit / 255.0f + .5f), 0, m_limit); 1647 m_bg = rg_etc1::clamp<int>(static_cast<uint>(m_avg_color[1] * m_limit / 255.0f + .5f), 0, m_limit);
1648 m_bb = rg_etc1::clamp<int>(static_cast<uint>(m_avg_color[2] * m_limit / 255.0f + .5f), 0, m_limit); 1648 m_bb = rg_etc1::clamp<int>(static_cast<uint>(m_avg_color[2] * m_limit / 255.0f + .5f), 0, m_limit);
1649 1649
1650 if (m_pParams->m_quality <= cMediumQuality) 1650 if (m_pParams->m_quality <= cMediumQuality)
1651 { 1651 {
1652 m_pSorted_luma_indices = indirect_radix_sort(n, m_sorted_luma[0], m_sorted_luma[1], m_luma, 0, sizeof(m_luma[0]), false); 1652 m_pSorted_luma_indices = indirect_radix_sort(n, m_sorted_luma[0], m_sorted_luma[1], m_luma, 0, sizeof(m_luma[0]), false);
1653 m_pSorted_luma = m_sorted_luma[0]; 1653 m_pSorted_luma = m_sorted_luma[0];
1654 if (m_pSorted_luma_indices == m_sorted_luma[0]) 1654 if (m_pSorted_luma_indices == m_sorted_luma[0])
1655 m_pSorted_luma = m_sorted_luma[1]; 1655 m_pSorted_luma = m_sorted_luma[1];
1656 1656
1657 for (uint i = 0; i < n; i++) 1657 for (uint i = 0; i < n; i++)
1658 m_pSorted_luma[i] = m_luma[m_pSorted_luma_indices[i]]; 1658 m_pSorted_luma[i] = m_luma[m_pSorted_luma_indices[i]];
1659 } 1659 }
1660 1660
1661 m_best_solution.m_coords.clear(); 1661 m_best_solution.m_coords.clear();
1662 m_best_solution.m_valid = false; 1662 m_best_solution.m_valid = false;
1663 m_best_solution.m_error = cUINT64_MAX; 1663 m_best_solution.m_error = cUINT64_MAX;
1664 } 1664 }
1665 1665
1666 bool etc1_optimizer::evaluate_solution(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution) 1666 bool etc1_optimizer::evaluate_solution(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution)
1667 { 1667 {
1668 trial_solution.m_valid = false; 1668 trial_solution.m_valid = false;
1669 1669
1670 if (m_pParams->m_constrain_against_base_color5) 1670 if (m_pParams->m_constrain_against_base_color5)
1671 { 1671 {
1672 const int dr = coords.m_unscaled_color.r - m_pParams->m_base_color5.r; 1672 const int dr = coords.m_unscaled_color.r - m_pParams->m_base_color5.r;
1673 const int dg = coords.m_unscaled_color.g - m_pParams->m_base_color5.g; 1673 const int dg = coords.m_unscaled_color.g - m_pParams->m_base_color5.g;
1674 const int db = coords.m_unscaled_color.b - m_pParams->m_base_color5.b; 1674 const int db = coords.m_unscaled_color.b - m_pParams->m_base_color5.b;
1675 1675
1676 if ((rg_etc1::minimum(dr, dg, db) < cETC1ColorDeltaMin) || (rg_etc1::maximum(dr, dg, db) > cETC1ColorDeltaMax)) 1676 if ((rg_etc1::minimum(dr, dg, db) < cETC1ColorDeltaMin) || (rg_etc1::maximum(dr, dg, db) > cETC1ColorDeltaMax))
1677 return false; 1677 return false;
1678 } 1678 }
1679 1679
1680 const color_quad_u8 base_color(coords.get_scaled_color()); 1680 const color_quad_u8 base_color(coords.get_scaled_color());
1681 1681
1682 const uint n = 8; 1682 const uint n = 8;
1683 1683
1684 trial_solution.m_error = cUINT64_MAX; 1684 trial_solution.m_error = cUINT64_MAX;
1685 1685
1686 for (uint inten_table = 0; inten_table < cETC1IntenModifierValues; inten_table++) 1686 for (uint inten_table = 0; inten_table < cETC1IntenModifierValues; inten_table++)
1687 { 1687 {
1688 const int* pInten_table = g_etc1_inten_tables[inten_table]; 1688 const int* pInten_table = g_etc1_inten_tables[inten_table];
1689 1689
1690 color_quad_u8 block_colors[4]; 1690 color_quad_u8 block_colors[4];
1691 for (uint s = 0; s < 4; s++) 1691 for (uint s = 0; s < 4; s++)
1692 { 1692 {
1693 const int yd = pInten_table[s]; 1693 const int yd = pInten_table[s];
1694 block_colors[s].set(base_color.r + yd, base_color.g + yd, base_color.b + yd, 0); 1694 block_colors[s].set(base_color.r + yd, base_color.g + yd, base_color.b + yd, 0);
1695 } 1695 }
1696 1696
1697 uint64 total_error = 0; 1697 uint64 total_error = 0;
1698 1698
1699 const color_quad_u8* pSrc_pixels = m_pParams->m_pSrc_pixels; 1699 const color_quad_u8* pSrc_pixels = m_pParams->m_pSrc_pixels;
1700 for (uint c = 0; c < n; c++) 1700 for (uint c = 0; c < n; c++)
1701 { 1701 {
1702 const color_quad_u8& src_pixel = *pSrc_pixels++; 1702 const color_quad_u8& src_pixel = *pSrc_pixels++;
1703 1703
1704 uint best_selector_index = 0; 1704 uint best_selector_index = 0;
1705 uint best_error = rg_etc1::square(src_pixel.r - block_colors[0].r) + rg_etc1::square(src_pixel.g - block_colors[0].g) + rg_etc1::square(src_pixel.b - block_colors[0].b); 1705 uint best_error = rg_etc1::square(src_pixel.r - block_colors[0].r) + rg_etc1::square(src_pixel.g - block_colors[0].g) + rg_etc1::square(src_pixel.b - block_colors[0].b);
1706 1706
1707 uint trial_error = rg_etc1::square(src_pixel.r - block_colors[1].r) + rg_etc1::square(src_pixel.g - block_colors[1].g) + rg_etc1::square(src_pixel.b - block_colors[1].b); 1707 uint trial_error = rg_etc1::square(src_pixel.r - block_colors[1].r) + rg_etc1::square(src_pixel.g - block_colors[1].g) + rg_etc1::square(src_pixel.b - block_colors[1].b);
1708 if (trial_error < best_error) 1708 if (trial_error < best_error)
1709 { 1709 {
1710 best_error = trial_error; 1710 best_error = trial_error;
1711 best_selector_index = 1; 1711 best_selector_index = 1;
1712 } 1712 }
1713 1713
1714 trial_error = rg_etc1::square(src_pixel.r - block_colors[2].r) + rg_etc1::square(src_pixel.g - block_colors[2].g) + rg_etc1::square(src_pixel.b - block_colors[2].b); 1714 trial_error = rg_etc1::square(src_pixel.r - block_colors[2].r) + rg_etc1::square(src_pixel.g - block_colors[2].g) + rg_etc1::square(src_pixel.b - block_colors[2].b);
1715 if (trial_error < best_error) 1715 if (trial_error < best_error)
1716 { 1716 {
1717 best_error = trial_error; 1717 best_error = trial_error;
1718 best_selector_index = 2; 1718 best_selector_index = 2;
1719 } 1719 }
1720 1720
1721 trial_error = rg_etc1::square(src_pixel.r - block_colors[3].r) + rg_etc1::square(src_pixel.g - block_colors[3].g) + rg_etc1::square(src_pixel.b - block_colors[3].b); 1721 trial_error = rg_etc1::square(src_pixel.r - block_colors[3].r) + rg_etc1::square(src_pixel.g - block_colors[3].g) + rg_etc1::square(src_pixel.b - block_colors[3].b);
1722 if (trial_error < best_error) 1722 if (trial_error < best_error)
1723 { 1723 {
1724 best_error = trial_error; 1724 best_error = trial_error;
1725 best_selector_index = 3; 1725 best_selector_index = 3;
1726 } 1726 }
1727 1727
1728 m_temp_selectors[c] = static_cast<uint8>(best_selector_index); 1728 m_temp_selectors[c] = static_cast<uint8>(best_selector_index);
1729 1729
1730 total_error += best_error; 1730 total_error += best_error;
1731 if (total_error >= trial_solution.m_error) 1731 if (total_error >= trial_solution.m_error)
1732 break; 1732 break;
1733 } 1733 }
1734 1734
1735 if (total_error < trial_solution.m_error) 1735 if (total_error < trial_solution.m_error)
1736 { 1736 {
1737 trial_solution.m_error = total_error; 1737 trial_solution.m_error = total_error;
1738 trial_solution.m_coords.m_inten_table = inten_table; 1738 trial_solution.m_coords.m_inten_table = inten_table;
1739 memcpy(trial_solution.m_selectors, m_temp_selectors, 8); 1739 memcpy(trial_solution.m_selectors, m_temp_selectors, 8);
1740 trial_solution.m_valid = true; 1740 trial_solution.m_valid = true;
1741 } 1741 }
1742 } 1742 }
1743 trial_solution.m_coords.m_unscaled_color = coords.m_unscaled_color; 1743 trial_solution.m_coords.m_unscaled_color = coords.m_unscaled_color;
1744 trial_solution.m_coords.m_color4 = m_pParams->m_use_color4; 1744 trial_solution.m_coords.m_color4 = m_pParams->m_use_color4;
1745 1745
1746 bool success = false; 1746 bool success = false;
1747 if (pBest_solution) 1747 if (pBest_solution)
1748 { 1748 {
1749 if (trial_solution.m_error < pBest_solution->m_error) 1749 if (trial_solution.m_error < pBest_solution->m_error)
1750 { 1750 {
1751 *pBest_solution = trial_solution; 1751 *pBest_solution = trial_solution;
1752 success = true; 1752 success = true;
1753 } 1753 }
1754 } 1754 }
1755 1755
1756 return success; 1756 return success;
1757 } 1757 }
1758 1758
1759 bool etc1_optimizer::evaluate_solution_fast(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution) 1759 bool etc1_optimizer::evaluate_solution_fast(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution)
1760 { 1760 {
1761 if (m_pParams->m_constrain_against_base_color5) 1761 if (m_pParams->m_constrain_against_base_color5)
1762 { 1762 {
1763 const int dr = coords.m_unscaled_color.r - m_pParams->m_base_color5.r; 1763 const int dr = coords.m_unscaled_color.r - m_pParams->m_base_color5.r;
1764 const int dg = coords.m_unscaled_color.g - m_pParams->m_base_color5.g; 1764 const int dg = coords.m_unscaled_color.g - m_pParams->m_base_color5.g;
1765 const int db = coords.m_unscaled_color.b - m_pParams->m_base_color5.b; 1765 const int db = coords.m_unscaled_color.b - m_pParams->m_base_color5.b;
1766 1766
1767 if ((rg_etc1::minimum(dr, dg, db) < cETC1ColorDeltaMin) || (rg_etc1::maximum(dr, dg, db) > cETC1ColorDeltaMax)) 1767 if ((rg_etc1::minimum(dr, dg, db) < cETC1ColorDeltaMin) || (rg_etc1::maximum(dr, dg, db) > cETC1ColorDeltaMax))
1768 { 1768 {
1769 trial_solution.m_valid = false; 1769 trial_solution.m_valid = false;
1770 return false; 1770 return false;
1771 } 1771 }
1772 } 1772 }
1773 1773
1774 const color_quad_u8 base_color(coords.get_scaled_color()); 1774 const color_quad_u8 base_color(coords.get_scaled_color());
1775 1775
1776 const uint n = 8; 1776 const uint n = 8;
1777 1777
1778 trial_solution.m_error = cUINT64_MAX; 1778 trial_solution.m_error = cUINT64_MAX;
1779 1779
1780 for (int inten_table = cETC1IntenModifierValues - 1; inten_table >= 0; --inten_table) 1780 for (int inten_table = cETC1IntenModifierValues - 1; inten_table >= 0; --inten_table)
1781 { 1781 {
1782 const int* pInten_table = g_etc1_inten_tables[inten_table]; 1782 const int* pInten_table = g_etc1_inten_tables[inten_table];
1783 1783
1784 uint block_inten[4]; 1784 uint block_inten[4];
1785 color_quad_u8 block_colors[4]; 1785 color_quad_u8 block_colors[4];
1786 for (uint s = 0; s < 4; s++) 1786 for (uint s = 0; s < 4; s++)
1787 { 1787 {
1788 const int yd = pInten_table[s]; 1788 const int yd = pInten_table[s];
1789 color_quad_u8 block_color(base_color.r + yd, base_color.g + yd, base_color.b + yd, 0); 1789 color_quad_u8 block_color(base_color.r + yd, base_color.g + yd, base_color.b + yd, 0);
1790 block_colors[s] = block_color; 1790 block_colors[s] = block_color;
1791 block_inten[s] = block_color.r + block_color.g + block_color.b; 1791 block_inten[s] = block_color.r + block_color.g + block_color.b;
1792 } 1792 }
1793 1793
1794 // evaluate_solution_fast() enforces/assumesd a total ordering of the input colors along the intensity (1,1,1) axis to more quickly classify the inputs to selectors. 1794 // evaluate_solution_fast() enforces/assumesd a total ordering of the input colors along the intensity (1,1,1) axis to more quickly classify the inputs to selectors.
1795 // The inputs colors have been presorted along the projection onto this axis, and ETC1 block colors are always ordered along the intensity axis, so this classification is fast. 1795 // The inputs colors have been presorted along the projection onto this axis, and ETC1 block colors are always ordered along the intensity axis, so this classification is fast.
1796 // 0 1 2 3 1796 // 0 1 2 3
1797 // 01 12 23 1797 // 01 12 23
1798 const uint block_inten_midpoints[3] = { block_inten[0] + block_inten[1], block_inten[1] + block_inten[2], block_inten[2] + block_inten[3] }; 1798 const uint block_inten_midpoints[3] = { block_inten[0] + block_inten[1], block_inten[1] + block_inten[2], block_inten[2] + block_inten[3] };
1799 1799
1800 uint64 total_error = 0; 1800 uint64 total_error = 0;
1801 const color_quad_u8* pSrc_pixels = m_pParams->m_pSrc_pixels; 1801 const color_quad_u8* pSrc_pixels = m_pParams->m_pSrc_pixels;
1802 if ((m_pSorted_luma[n - 1] * 2) < block_inten_midpoints[0]) 1802 if ((m_pSorted_luma[n - 1] * 2) < block_inten_midpoints[0])
1803 { 1803 {
1804 if (block_inten[0] > m_pSorted_luma[n - 1]) 1804 if (block_inten[0] > m_pSorted_luma[n - 1])
1805 { 1805 {
1806 const uint min_error = labs(block_inten[0] - m_pSorted_luma[n - 1]); 1806 const uint min_error = labs(block_inten[0] - m_pSorted_luma[n - 1]);
1807 if (min_error >= trial_solution.m_error) 1807 if (min_error >= trial_solution.m_error)
1808 continue; 1808 continue;
1809 } 1809 }
1810 1810
1811 memset(&m_temp_selectors[0], 0, n); 1811 memset(&m_temp_selectors[0], 0, n);
1812 1812
1813 for (uint c = 0; c < n; c++) 1813 for (uint c = 0; c < n; c++)
1814 total_error += block_colors[0].squared_distance_rgb(pSrc_pixels[c]); 1814 total_error += block_colors[0].squared_distance_rgb(pSrc_pixels[c]);
1815 } 1815 }
1816 else if ((m_pSorted_luma[0] * 2) >= block_inten_midpoints[2]) 1816 else if ((m_pSorted_luma[0] * 2) >= block_inten_midpoints[2])
1817 { 1817 {
1818 if (m_pSorted_luma[0] > block_inten[3]) 1818 if (m_pSorted_luma[0] > block_inten[3])
1819 { 1819 {
1820 const uint min_error = labs(m_pSorted_luma[0] - block_inten[3]); 1820 const uint min_error = labs(m_pSorted_luma[0] - block_inten[3]);
1821 if (min_error >= trial_solution.m_error) 1821 if (min_error >= trial_solution.m_error)
1822 continue; 1822 continue;
1823 } 1823 }
1824 1824
1825 memset(&m_temp_selectors[0], 3, n); 1825 memset(&m_temp_selectors[0], 3, n);
1826 1826
1827 for (uint c = 0; c < n; c++) 1827 for (uint c = 0; c < n; c++)
1828 total_error += block_colors[3].squared_distance_rgb(pSrc_pixels[c]); 1828 total_error += block_colors[3].squared_distance_rgb(pSrc_pixels[c]);
1829 } 1829 }
1830 else 1830 else
1831 { 1831 {
1832 uint cur_selector = 0, c; 1832 uint cur_selector = 0, c;
1833 for (c = 0; c < n; c++) 1833 for (c = 0; c < n; c++)
1834 { 1834 {
1835 const uint y = m_pSorted_luma[c]; 1835 const uint y = m_pSorted_luma[c];
1836 while ((y * 2) >= block_inten_midpoints[cur_selector]) 1836 while ((y * 2) >= block_inten_midpoints[cur_selector])
1837 if (++cur_selector > 2) 1837 if (++cur_selector > 2)
1838 goto done; 1838 goto done;
1839 const uint sorted_pixel_index = m_pSorted_luma_indices[c]; 1839 const uint sorted_pixel_index = m_pSorted_luma_indices[c];
1840 m_temp_selectors[sorted_pixel_index] = static_cast<uint8>(cur_selector); 1840 m_temp_selectors[sorted_pixel_index] = static_cast<uint8>(cur_selector);
1841 total_error += block_colors[cur_selector].squared_distance_rgb(pSrc_pixels[sorted_pixel_index]); 1841 total_error += block_colors[cur_selector].squared_distance_rgb(pSrc_pixels[sorted_pixel_index]);
1842 } 1842 }
1843done: 1843done:
1844 while (c < n) 1844 while (c < n)
1845 { 1845 {
1846 const uint sorted_pixel_index = m_pSorted_luma_indices[c]; 1846 const uint sorted_pixel_index = m_pSorted_luma_indices[c];
1847 m_temp_selectors[sorted_pixel_index] = 3; 1847 m_temp_selectors[sorted_pixel_index] = 3;
1848 total_error += block_colors[3].squared_distance_rgb(pSrc_pixels[sorted_pixel_index]); 1848 total_error += block_colors[3].squared_distance_rgb(pSrc_pixels[sorted_pixel_index]);
1849 ++c; 1849 ++c;
1850 } 1850 }
1851 } 1851 }
1852 1852
1853 if (total_error < trial_solution.m_error) 1853 if (total_error < trial_solution.m_error)
1854 { 1854 {
1855 trial_solution.m_error = total_error; 1855 trial_solution.m_error = total_error;
1856 trial_solution.m_coords.m_inten_table = inten_table; 1856 trial_solution.m_coords.m_inten_table = inten_table;
1857 memcpy(trial_solution.m_selectors, m_temp_selectors, n); 1857 memcpy(trial_solution.m_selectors, m_temp_selectors, n);
1858 trial_solution.m_valid = true; 1858 trial_solution.m_valid = true;
1859 if (!total_error) 1859 if (!total_error)
1860 break; 1860 break;
1861 } 1861 }
1862 } 1862 }
1863 trial_solution.m_coords.m_unscaled_color = coords.m_unscaled_color; 1863 trial_solution.m_coords.m_unscaled_color = coords.m_unscaled_color;
1864 trial_solution.m_coords.m_color4 = m_pParams->m_use_color4; 1864 trial_solution.m_coords.m_color4 = m_pParams->m_use_color4;
1865 1865
1866 bool success = false; 1866 bool success = false;
1867 if (pBest_solution) 1867 if (pBest_solution)
1868 { 1868 {
1869 if (trial_solution.m_error < pBest_solution->m_error) 1869 if (trial_solution.m_error < pBest_solution->m_error)
1870 { 1870 {
1871 *pBest_solution = trial_solution; 1871 *pBest_solution = trial_solution;
1872 success = true; 1872 success = true;
1873 } 1873 }
1874 } 1874 }
1875 1875
1876 return success; 1876 return success;
1877 } 1877 }
1878 1878
1879 static uint etc1_decode_value(uint diff, uint inten, uint selector, uint packed_c) 1879 static uint etc1_decode_value(uint diff, uint inten, uint selector, uint packed_c)
1880 { 1880 {
1881 const uint limit = diff ? 32 : 16; limit; 1881 const uint limit = diff ? 32 : 16; limit;
1882 RG_ETC1_ASSERT((diff < 2) && (inten < 8) && (selector < 4) && (packed_c < limit)); 1882 RG_ETC1_ASSERT((diff < 2) && (inten < 8) && (selector < 4) && (packed_c < limit));
1883 int c; 1883 int c;
1884 if (diff) 1884 if (diff)
1885 c = (packed_c >> 2) | (packed_c << 3); 1885 c = (packed_c >> 2) | (packed_c << 3);
1886 else 1886 else
1887 c = packed_c | (packed_c << 4); 1887 c = packed_c | (packed_c << 4);
1888 c += g_etc1_inten_tables[inten][selector]; 1888 c += g_etc1_inten_tables[inten][selector];
1889 c = rg_etc1::clamp<int>(c, 0, 255); 1889 c = rg_etc1::clamp<int>(c, 0, 255);
1890 return c; 1890 return c;
1891 } 1891 }
1892 1892
1893 static inline int mul_8bit(int a, int b) { int t = a*b + 128; return (t + (t >> 8)) >> 8; } 1893 static inline int mul_8bit(int a, int b) { int t = a*b + 128; return (t + (t >> 8)) >> 8; }
1894 1894
1895 void pack_etc1_block_init() 1895 void pack_etc1_block_init()
1896 { 1896 {
1897 for (uint diff = 0; diff < 2; diff++) 1897 for (uint diff = 0; diff < 2; diff++)
1898 { 1898 {
1899 const uint limit = diff ? 32 : 16; 1899 const uint limit = diff ? 32 : 16;
1900 1900
1901 for (uint inten = 0; inten < 8; inten++) 1901 for (uint inten = 0; inten < 8; inten++)
1902 { 1902 {
1903 for (uint selector = 0; selector < 4; selector++) 1903 for (uint selector = 0; selector < 4; selector++)
1904 { 1904 {
1905 const uint inverse_table_index = diff + (inten << 1) + (selector << 4); 1905 const uint inverse_table_index = diff + (inten << 1) + (selector << 4);
1906 for (uint color = 0; color < 256; color++) 1906 for (uint color = 0; color < 256; color++)
1907 { 1907 {
1908 uint best_error = cUINT32_MAX, best_packed_c = 0; 1908 uint best_error = cUINT32_MAX, best_packed_c = 0;
1909 for (uint packed_c = 0; packed_c < limit; packed_c++) 1909 for (uint packed_c = 0; packed_c < limit; packed_c++)
1910 { 1910 {
1911 int v = etc1_decode_value(diff, inten, selector, packed_c); 1911 int v = etc1_decode_value(diff, inten, selector, packed_c);
1912 uint err = labs(v - static_cast<int>(color)); 1912 uint err = labs(v - static_cast<int>(color));
1913 if (err < best_error) 1913 if (err < best_error)
1914 { 1914 {
1915 best_error = err; 1915 best_error = err;
1916 best_packed_c = packed_c; 1916 best_packed_c = packed_c;
1917 if (!best_error) 1917 if (!best_error)
1918 break; 1918 break;
1919 } 1919 }
1920 } 1920 }
1921 RG_ETC1_ASSERT(best_error <= 255); 1921 RG_ETC1_ASSERT(best_error <= 255);
1922 g_etc1_inverse_lookup[inverse_table_index][color] = static_cast<uint16>(best_packed_c | (best_error << 8)); 1922 g_etc1_inverse_lookup[inverse_table_index][color] = static_cast<uint16>(best_packed_c | (best_error << 8));
1923 } 1923 }
1924 } 1924 }
1925 } 1925 }
1926 } 1926 }
1927 1927
1928 uint expand5[32]; 1928 uint expand5[32];
1929 for(int i = 0; i < 32; i++) 1929 for(int i = 0; i < 32; i++)
1930 expand5[i] = (i << 3) | (i >> 2); 1930 expand5[i] = (i << 3) | (i >> 2);
1931 1931
1932 for(int i = 0; i < 256 + 16; i++) 1932 for(int i = 0; i < 256 + 16; i++)
1933 { 1933 {
1934 int v = clamp<int>(i - 8, 0, 255); 1934 int v = clamp<int>(i - 8, 0, 255);
1935 g_quant5_tab[i] = static_cast<uint8>(expand5[mul_8bit(v,31)]); 1935 g_quant5_tab[i] = static_cast<uint8>(expand5[mul_8bit(v,31)]);
1936 } 1936 }
1937 } 1937 }
1938 1938
1939 // Packs solid color blocks efficiently using a set of small precomputed tables. 1939 // Packs solid color blocks efficiently using a set of small precomputed tables.
1940 // For random 888 inputs, MSE results are better than Erricson's ETC1 packer in "slow" mode ~9.5% of the time, is slightly worse only ~.01% of the time, and is equal the rest of the time. 1940 // For random 888 inputs, MSE results are better than Erricson's ETC1 packer in "slow" mode ~9.5% of the time, is slightly worse only ~.01% of the time, and is equal the rest of the time.
1941 static uint64 pack_etc1_block_solid_color(etc1_block& block, const uint8* pColor, etc1_pack_params& pack_params) 1941 static uint64 pack_etc1_block_solid_color(etc1_block& block, const uint8* pColor, etc1_pack_params& pack_params)
1942 { 1942 {
1943 pack_params; 1943 pack_params;
1944 RG_ETC1_ASSERT(g_etc1_inverse_lookup[0][255]); 1944 RG_ETC1_ASSERT(g_etc1_inverse_lookup[0][255]);
1945 1945
1946 static uint s_next_comp[4] = { 1, 2, 0, 1 }; 1946 static uint s_next_comp[4] = { 1, 2, 0, 1 };
1947 1947
1948 uint best_error = cUINT32_MAX, best_i = 0; 1948 uint best_error = cUINT32_MAX, best_i = 0;
1949 int best_x = 0, best_packed_c1 = 0, best_packed_c2 = 0; 1949 int best_x = 0, best_packed_c1 = 0, best_packed_c2 = 0;
1950 1950
1951 // For each possible 8-bit value, there is a precomputed list of diff/inten/selector configurations that allow that 8-bit value to be encoded with no error. 1951 // For each possible 8-bit value, there is a precomputed list of diff/inten/selector configurations that allow that 8-bit value to be encoded with no error.
1952 for (uint i = 0; i < 3; i++) 1952 for (uint i = 0; i < 3; i++)
1953 { 1953 {
1954 const uint c1 = pColor[s_next_comp[i]], c2 = pColor[s_next_comp[i + 1]]; 1954 const uint c1 = pColor[s_next_comp[i]], c2 = pColor[s_next_comp[i + 1]];
1955 1955
1956 const int delta_range = 1; 1956 const int delta_range = 1;
1957 for (int delta = -delta_range; delta <= delta_range; delta++) 1957 for (int delta = -delta_range; delta <= delta_range; delta++)
1958 { 1958 {
1959 const int c_plus_delta = rg_etc1::clamp<int>(pColor[i] + delta, 0, 255); 1959 const int c_plus_delta = rg_etc1::clamp<int>(pColor[i] + delta, 0, 255);
1960 1960
1961 const uint16* pTable; 1961 const uint16* pTable;
1962 if (!c_plus_delta) 1962 if (!c_plus_delta)
1963 pTable = g_color8_to_etc_block_config_0_255[0]; 1963 pTable = g_color8_to_etc_block_config_0_255[0];
1964 else if (c_plus_delta == 255) 1964 else if (c_plus_delta == 255)
1965 pTable = g_color8_to_etc_block_config_0_255[1]; 1965 pTable = g_color8_to_etc_block_config_0_255[1];
1966 else 1966 else
1967 pTable = g_color8_to_etc_block_config_1_to_254[c_plus_delta - 1]; 1967 pTable = g_color8_to_etc_block_config_1_to_254[c_plus_delta - 1];
1968 1968
1969 do 1969 do
1970 { 1970 {
1971 const uint x = *pTable++; 1971 const uint x = *pTable++;
1972 1972
1973#ifdef RG_ETC1_BUILD_DEBUG 1973#ifdef RG_ETC1_BUILD_DEBUG
1974 const uint diff = x & 1; 1974 const uint diff = x & 1;
1975 const uint inten = (x >> 1) & 7; 1975 const uint inten = (x >> 1) & 7;
1976 const uint selector = (x >> 4) & 3; 1976 const uint selector = (x >> 4) & 3;
1977 const uint p0 = (x >> 8) & 255; 1977 const uint p0 = (x >> 8) & 255;
1978 RG_ETC1_ASSERT(etc1_decode_value(diff, inten, selector, p0) == (uint)c_plus_delta); 1978 RG_ETC1_ASSERT(etc1_decode_value(diff, inten, selector, p0) == (uint)c_plus_delta);
1979#endif 1979#endif
1980 1980
1981 const uint16* pInverse_table = g_etc1_inverse_lookup[x & 0xFF]; 1981 const uint16* pInverse_table = g_etc1_inverse_lookup[x & 0xFF];
1982 uint16 p1 = pInverse_table[c1]; 1982 uint16 p1 = pInverse_table[c1];
1983 uint16 p2 = pInverse_table[c2]; 1983 uint16 p2 = pInverse_table[c2];
1984 const uint trial_error = rg_etc1::square(c_plus_delta - pColor[i]) + rg_etc1::square(p1 >> 8) + rg_etc1::square(p2 >> 8); 1984 const uint trial_error = rg_etc1::square(c_plus_delta - pColor[i]) + rg_etc1::square(p1 >> 8) + rg_etc1::square(p2 >> 8);
1985 if (trial_error < best_error) 1985 if (trial_error < best_error)
1986 { 1986 {
1987 best_error = trial_error; 1987 best_error = trial_error;
1988 best_x = x; 1988 best_x = x;
1989 best_packed_c1 = p1 & 0xFF; 1989 best_packed_c1 = p1 & 0xFF;
1990 best_packed_c2 = p2 & 0xFF; 1990 best_packed_c2 = p2 & 0xFF;
1991 best_i = i; 1991 best_i = i;
1992 if (!best_error) 1992 if (!best_error)
1993 goto found_perfect_match; 1993 goto found_perfect_match;
1994 } 1994 }
1995 } while (*pTable != 0xFFFF); 1995 } while (*pTable != 0xFFFF);
1996 } 1996 }
1997 } 1997 }
1998found_perfect_match: 1998found_perfect_match:
1999 1999
2000 const uint diff = best_x & 1; 2000 const uint diff = best_x & 1;
2001 const uint inten = (best_x >> 1) & 7; 2001 const uint inten = (best_x >> 1) & 7;
2002 2002
2003 block.m_bytes[3] = static_cast<uint8>(((inten | (inten << 3)) << 2) | (diff << 1)); 2003 block.m_bytes[3] = static_cast<uint8>(((inten | (inten << 3)) << 2) | (diff << 1));
2004 2004
2005 const uint etc1_selector = g_selector_index_to_etc1[(best_x >> 4) & 3]; 2005 const uint etc1_selector = g_selector_index_to_etc1[(best_x >> 4) & 3];
2006 *reinterpret_cast<uint16*>(&block.m_bytes[4]) = (etc1_selector & 2) ? 0xFFFF : 0; 2006 *reinterpret_cast<uint16*>(&block.m_bytes[4]) = (etc1_selector & 2) ? 0xFFFF : 0;
2007 *reinterpret_cast<uint16*>(&block.m_bytes[6]) = (etc1_selector & 1) ? 0xFFFF : 0; 2007 *reinterpret_cast<uint16*>(&block.m_bytes[6]) = (etc1_selector & 1) ? 0xFFFF : 0;
2008 2008
2009 const uint best_packed_c0 = (best_x >> 8) & 255; 2009 const uint best_packed_c0 = (best_x >> 8) & 255;
2010 if (diff) 2010 if (diff)
2011 { 2011 {
2012 block.m_bytes[best_i] = static_cast<uint8>(best_packed_c0 << 3); 2012 block.m_bytes[best_i] = static_cast<uint8>(best_packed_c0 << 3);
2013 block.m_bytes[s_next_comp[best_i]] = static_cast<uint8>(best_packed_c1 << 3); 2013 block.m_bytes[s_next_comp[best_i]] = static_cast<uint8>(best_packed_c1 << 3);
2014 block.m_bytes[s_next_comp[best_i+1]] = static_cast<uint8>(best_packed_c2 << 3); 2014 block.m_bytes[s_next_comp[best_i+1]] = static_cast<uint8>(best_packed_c2 << 3);
2015 } 2015 }
2016 else 2016 else
2017 { 2017 {
2018 block.m_bytes[best_i] = static_cast<uint8>(best_packed_c0 | (best_packed_c0 << 4)); 2018 block.m_bytes[best_i] = static_cast<uint8>(best_packed_c0 | (best_packed_c0 << 4));
2019 block.m_bytes[s_next_comp[best_i]] = static_cast<uint8>(best_packed_c1 | (best_packed_c1 << 4)); 2019 block.m_bytes[s_next_comp[best_i]] = static_cast<uint8>(best_packed_c1 | (best_packed_c1 << 4));
2020 block.m_bytes[s_next_comp[best_i+1]] = static_cast<uint8>(best_packed_c2 | (best_packed_c2 << 4)); 2020 block.m_bytes[s_next_comp[best_i+1]] = static_cast<uint8>(best_packed_c2 | (best_packed_c2 << 4));
2021 } 2021 }
2022 2022
2023 return best_error; 2023 return best_error;
2024 } 2024 }
2025 2025
2026 static uint pack_etc1_block_solid_color_constrained( 2026 static uint pack_etc1_block_solid_color_constrained(
2027 etc1_optimizer::results& results, 2027 etc1_optimizer::results& results,
2028 uint num_colors, const uint8* pColor, 2028 uint num_colors, const uint8* pColor,
2029 etc1_pack_params& pack_params, 2029 etc1_pack_params& pack_params,
2030 bool use_diff, 2030 bool use_diff,
2031 const color_quad_u8* pBase_color5_unscaled) 2031 const color_quad_u8* pBase_color5_unscaled)
2032 { 2032 {
2033 RG_ETC1_ASSERT(g_etc1_inverse_lookup[0][255]); 2033 RG_ETC1_ASSERT(g_etc1_inverse_lookup[0][255]);
2034 2034
2035 pack_params; 2035 pack_params;
2036 static uint s_next_comp[4] = { 1, 2, 0, 1 }; 2036 static uint s_next_comp[4] = { 1, 2, 0, 1 };
2037 2037
2038 uint best_error = cUINT32_MAX, best_i = 0; 2038 uint best_error = cUINT32_MAX, best_i = 0;
2039 int best_x = 0, best_packed_c1 = 0, best_packed_c2 = 0; 2039 int best_x = 0, best_packed_c1 = 0, best_packed_c2 = 0;
2040 2040
2041 // For each possible 8-bit value, there is a precomputed list of diff/inten/selector configurations that allow that 8-bit value to be encoded with no error. 2041 // For each possible 8-bit value, there is a precomputed list of diff/inten/selector configurations that allow that 8-bit value to be encoded with no error.
2042 for (uint i = 0; i < 3; i++) 2042 for (uint i = 0; i < 3; i++)
2043 { 2043 {
2044 const uint c1 = pColor[s_next_comp[i]], c2 = pColor[s_next_comp[i + 1]]; 2044 const uint c1 = pColor[s_next_comp[i]], c2 = pColor[s_next_comp[i + 1]];
2045 2045
2046 const int delta_range = 1; 2046 const int delta_range = 1;
2047 for (int delta = -delta_range; delta <= delta_range; delta++) 2047 for (int delta = -delta_range; delta <= delta_range; delta++)
2048 { 2048 {
2049 const int c_plus_delta = rg_etc1::clamp<int>(pColor[i] + delta, 0, 255); 2049 const int c_plus_delta = rg_etc1::clamp<int>(pColor[i] + delta, 0, 255);
2050 2050
2051 const uint16* pTable; 2051 const uint16* pTable;
2052 if (!c_plus_delta) 2052 if (!c_plus_delta)
2053 pTable = g_color8_to_etc_block_config_0_255[0]; 2053 pTable = g_color8_to_etc_block_config_0_255[0];
2054 else if (c_plus_delta == 255) 2054 else if (c_plus_delta == 255)
2055 pTable = g_color8_to_etc_block_config_0_255[1]; 2055 pTable = g_color8_to_etc_block_config_0_255[1];
2056 else 2056 else
2057 pTable = g_color8_to_etc_block_config_1_to_254[c_plus_delta - 1]; 2057 pTable = g_color8_to_etc_block_config_1_to_254[c_plus_delta - 1];
2058 2058
2059 do 2059 do
2060 { 2060 {
2061 const uint x = *pTable++; 2061 const uint x = *pTable++;
2062 const uint diff = x & 1; 2062 const uint diff = x & 1;
2063 if (static_cast<uint>(use_diff) != diff) 2063 if (static_cast<uint>(use_diff) != diff)
2064 { 2064 {
2065 if (*pTable == 0xFFFF) 2065 if (*pTable == 0xFFFF)
2066 break; 2066 break;
2067 continue; 2067 continue;
2068 } 2068 }
2069 2069
2070 if ((diff) && (pBase_color5_unscaled)) 2070 if ((diff) && (pBase_color5_unscaled))
2071 { 2071 {
2072 const int p0 = (x >> 8) & 255; 2072 const int p0 = (x >> 8) & 255;
2073 int delta = p0 - static_cast<int>(pBase_color5_unscaled->c[i]); 2073 int delta = p0 - static_cast<int>(pBase_color5_unscaled->c[i]);
2074 if ((delta < cETC1ColorDeltaMin) || (delta > cETC1ColorDeltaMax)) 2074 if ((delta < cETC1ColorDeltaMin) || (delta > cETC1ColorDeltaMax))
2075 { 2075 {
2076 if (*pTable == 0xFFFF) 2076 if (*pTable == 0xFFFF)
2077 break; 2077 break;
2078 continue; 2078 continue;
2079 } 2079 }
2080 } 2080 }
2081 2081
2082#ifdef RG_ETC1_BUILD_DEBUG 2082#ifdef RG_ETC1_BUILD_DEBUG
2083 { 2083 {
2084 const uint inten = (x >> 1) & 7; 2084 const uint inten = (x >> 1) & 7;
2085 const uint selector = (x >> 4) & 3; 2085 const uint selector = (x >> 4) & 3;
2086 const uint p0 = (x >> 8) & 255; 2086 const uint p0 = (x >> 8) & 255;
2087 RG_ETC1_ASSERT(etc1_decode_value(diff, inten, selector, p0) == (uint)c_plus_delta); 2087 RG_ETC1_ASSERT(etc1_decode_value(diff, inten, selector, p0) == (uint)c_plus_delta);
2088 } 2088 }
2089#endif 2089#endif
2090 2090
2091 const uint16* pInverse_table = g_etc1_inverse_lookup[x & 0xFF]; 2091 const uint16* pInverse_table = g_etc1_inverse_lookup[x & 0xFF];
2092 uint16 p1 = pInverse_table[c1]; 2092 uint16 p1 = pInverse_table[c1];
2093 uint16 p2 = pInverse_table[c2]; 2093 uint16 p2 = pInverse_table[c2];
2094 2094
2095 if ((diff) && (pBase_color5_unscaled)) 2095 if ((diff) && (pBase_color5_unscaled))
2096 { 2096 {
2097 int delta1 = (p1 & 0xFF) - static_cast<int>(pBase_color5_unscaled->c[s_next_comp[i]]); 2097 int delta1 = (p1 & 0xFF) - static_cast<int>(pBase_color5_unscaled->c[s_next_comp[i]]);
2098 int delta2 = (p2 & 0xFF) - static_cast<int>(pBase_color5_unscaled->c[s_next_comp[i + 1]]); 2098 int delta2 = (p2 & 0xFF) - static_cast<int>(pBase_color5_unscaled->c[s_next_comp[i + 1]]);
2099 if ((delta1 < cETC1ColorDeltaMin) || (delta1 > cETC1ColorDeltaMax) || (delta2 < cETC1ColorDeltaMin) || (delta2 > cETC1ColorDeltaMax)) 2099 if ((delta1 < cETC1ColorDeltaMin) || (delta1 > cETC1ColorDeltaMax) || (delta2 < cETC1ColorDeltaMin) || (delta2 > cETC1ColorDeltaMax))
2100 { 2100 {
2101 if (*pTable == 0xFFFF) 2101 if (*pTable == 0xFFFF)
2102 break; 2102 break;
2103 continue; 2103 continue;
2104 } 2104 }
2105 } 2105 }
2106 2106
2107 const uint trial_error = rg_etc1::square(c_plus_delta - pColor[i]) + rg_etc1::square(p1 >> 8) + rg_etc1::square(p2 >> 8); 2107 const uint trial_error = rg_etc1::square(c_plus_delta - pColor[i]) + rg_etc1::square(p1 >> 8) + rg_etc1::square(p2 >> 8);
2108 if (trial_error < best_error) 2108 if (trial_error < best_error)
2109 { 2109 {
2110 best_error = trial_error; 2110 best_error = trial_error;
2111 best_x = x; 2111 best_x = x;
2112 best_packed_c1 = p1 & 0xFF; 2112 best_packed_c1 = p1 & 0xFF;
2113 best_packed_c2 = p2 & 0xFF; 2113 best_packed_c2 = p2 & 0xFF;
2114 best_i = i; 2114 best_i = i;
2115 if (!best_error) 2115 if (!best_error)
2116 goto found_perfect_match; 2116 goto found_perfect_match;
2117 } 2117 }
2118 } while (*pTable != 0xFFFF); 2118 } while (*pTable != 0xFFFF);
2119 } 2119 }
2120 } 2120 }
2121found_perfect_match: 2121found_perfect_match:
2122 2122
2123 if (best_error == cUINT32_MAX) 2123 if (best_error == cUINT32_MAX)
2124 return best_error; 2124 return best_error;
2125 2125
2126 best_error *= num_colors; 2126 best_error *= num_colors;
2127 2127