rg_etc1.cpp 97 KB

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