stb_dxt.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. // SPDX-FileCopyrightText: fabian "ryg" giesen
  2. // SPDX-License-Identifier: MIT
  3. // stb_dxt.h - v1.12 - DXT1/DXT5 compressor
  4. #include <stb_dxt.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #if !defined(STBD_FABS)
  8. #include <math.h>
  9. #endif
  10. #ifndef STBD_FABS
  11. #define STBD_FABS(x) fabs(x)
  12. #endif
  13. static const unsigned char stb__OMatch5[256][2] = {
  14. {0, 0}, {0, 0}, {0, 1}, {0, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 1}, {1, 1},
  15. {1, 1}, {1, 2}, {0, 4}, {2, 1}, {2, 1}, {2, 1}, {2, 2}, {2, 2}, {2, 2},
  16. {2, 3}, {1, 5}, {3, 2}, {3, 2}, {4, 0}, {3, 3}, {3, 3}, {3, 3}, {3, 4},
  17. {3, 4}, {3, 4}, {3, 5}, {4, 3}, {4, 3}, {5, 2}, {4, 4}, {4, 4}, {4, 5},
  18. {4, 5}, {5, 4}, {5, 4}, {5, 4}, {6, 3}, {5, 5}, {5, 5}, {5, 6}, {4, 8},
  19. {6, 5}, {6, 5}, {6, 5}, {6, 6}, {6, 6}, {6, 6}, {6, 7}, {5, 9}, {7, 6},
  20. {7, 6}, {8, 4}, {7, 7}, {7, 7}, {7, 7}, {7, 8}, {7, 8}, {7, 8}, {7, 9},
  21. {8, 7}, {8, 7}, {9, 6}, {8, 8}, {8, 8}, {8, 9}, {8, 9}, {9, 8}, {9, 8},
  22. {9, 8}, {10, 7}, {9, 9}, {9, 9}, {9, 10}, {8, 12}, {10, 9}, {10, 9}, {10, 9},
  23. {10, 10}, {10, 10}, {10, 10}, {10, 11}, {9, 13}, {11, 10}, {11, 10}, {12, 8}, {11, 11},
  24. {11, 11}, {11, 11}, {11, 12}, {11, 12}, {11, 12}, {11, 13}, {12, 11}, {12, 11}, {13, 10},
  25. {12, 12}, {12, 12}, {12, 13}, {12, 13}, {13, 12}, {13, 12}, {13, 12}, {14, 11}, {13, 13},
  26. {13, 13}, {13, 14}, {12, 16}, {14, 13}, {14, 13}, {14, 13}, {14, 14}, {14, 14}, {14, 14},
  27. {14, 15}, {13, 17}, {15, 14}, {15, 14}, {16, 12}, {15, 15}, {15, 15}, {15, 15}, {15, 16},
  28. {15, 16}, {15, 16}, {15, 17}, {16, 15}, {16, 15}, {17, 14}, {16, 16}, {16, 16}, {16, 17},
  29. {16, 17}, {17, 16}, {17, 16}, {17, 16}, {18, 15}, {17, 17}, {17, 17}, {17, 18}, {16, 20},
  30. {18, 17}, {18, 17}, {18, 17}, {18, 18}, {18, 18}, {18, 18}, {18, 19}, {17, 21}, {19, 18},
  31. {19, 18}, {20, 16}, {19, 19}, {19, 19}, {19, 19}, {19, 20}, {19, 20}, {19, 20}, {19, 21},
  32. {20, 19}, {20, 19}, {21, 18}, {20, 20}, {20, 20}, {20, 21}, {20, 21}, {21, 20}, {21, 20},
  33. {21, 20}, {22, 19}, {21, 21}, {21, 21}, {21, 22}, {20, 24}, {22, 21}, {22, 21}, {22, 21},
  34. {22, 22}, {22, 22}, {22, 22}, {22, 23}, {21, 25}, {23, 22}, {23, 22}, {24, 20}, {23, 23},
  35. {23, 23}, {23, 23}, {23, 24}, {23, 24}, {23, 24}, {23, 25}, {24, 23}, {24, 23}, {25, 22},
  36. {24, 24}, {24, 24}, {24, 25}, {24, 25}, {25, 24}, {25, 24}, {25, 24}, {26, 23}, {25, 25},
  37. {25, 25}, {25, 26}, {24, 28}, {26, 25}, {26, 25}, {26, 25}, {26, 26}, {26, 26}, {26, 26},
  38. {26, 27}, {25, 29}, {27, 26}, {27, 26}, {28, 24}, {27, 27}, {27, 27}, {27, 27}, {27, 28},
  39. {27, 28}, {27, 28}, {27, 29}, {28, 27}, {28, 27}, {29, 26}, {28, 28}, {28, 28}, {28, 29},
  40. {28, 29}, {29, 28}, {29, 28}, {29, 28}, {30, 27}, {29, 29}, {29, 29}, {29, 30}, {29, 30},
  41. {30, 29}, {30, 29}, {30, 29}, {30, 30}, {30, 30}, {30, 30}, {30, 31}, {30, 31}, {31, 30},
  42. {31, 30}, {31, 30}, {31, 31}, {31, 31},
  43. };
  44. static const unsigned char stb__OMatch6[256][2] = {
  45. {0, 0}, {0, 1}, {1, 0}, {1, 1}, {1, 1}, {1, 2}, {2, 1}, {2, 2}, {2, 2},
  46. {2, 3}, {3, 2}, {3, 3}, {3, 3}, {3, 4}, {4, 3}, {4, 4}, {4, 4}, {4, 5},
  47. {5, 4}, {5, 5}, {5, 5}, {5, 6}, {6, 5}, {6, 6}, {6, 6}, {6, 7}, {7, 6},
  48. {7, 7}, {7, 7}, {7, 8}, {8, 7}, {8, 8}, {8, 8}, {8, 9}, {9, 8}, {9, 9},
  49. {9, 9}, {9, 10}, {10, 9}, {10, 10}, {10, 10}, {10, 11}, {11, 10}, {8, 16}, {11, 11},
  50. {11, 12}, {12, 11}, {9, 17}, {12, 12}, {12, 13}, {13, 12}, {11, 16}, {13, 13}, {13, 14},
  51. {14, 13}, {12, 17}, {14, 14}, {14, 15}, {15, 14}, {14, 16}, {15, 15}, {15, 16}, {16, 14},
  52. {16, 15}, {17, 14}, {16, 16}, {16, 17}, {17, 16}, {18, 15}, {17, 17}, {17, 18}, {18, 17},
  53. {20, 14}, {18, 18}, {18, 19}, {19, 18}, {21, 15}, {19, 19}, {19, 20}, {20, 19}, {20, 20},
  54. {20, 20}, {20, 21}, {21, 20}, {21, 21}, {21, 21}, {21, 22}, {22, 21}, {22, 22}, {22, 22},
  55. {22, 23}, {23, 22}, {23, 23}, {23, 23}, {23, 24}, {24, 23}, {24, 24}, {24, 24}, {24, 25},
  56. {25, 24}, {25, 25}, {25, 25}, {25, 26}, {26, 25}, {26, 26}, {26, 26}, {26, 27}, {27, 26},
  57. {24, 32}, {27, 27}, {27, 28}, {28, 27}, {25, 33}, {28, 28}, {28, 29}, {29, 28}, {27, 32},
  58. {29, 29}, {29, 30}, {30, 29}, {28, 33}, {30, 30}, {30, 31}, {31, 30}, {30, 32}, {31, 31},
  59. {31, 32}, {32, 30}, {32, 31}, {33, 30}, {32, 32}, {32, 33}, {33, 32}, {34, 31}, {33, 33},
  60. {33, 34}, {34, 33}, {36, 30}, {34, 34}, {34, 35}, {35, 34}, {37, 31}, {35, 35}, {35, 36},
  61. {36, 35}, {36, 36}, {36, 36}, {36, 37}, {37, 36}, {37, 37}, {37, 37}, {37, 38}, {38, 37},
  62. {38, 38}, {38, 38}, {38, 39}, {39, 38}, {39, 39}, {39, 39}, {39, 40}, {40, 39}, {40, 40},
  63. {40, 40}, {40, 41}, {41, 40}, {41, 41}, {41, 41}, {41, 42}, {42, 41}, {42, 42}, {42, 42},
  64. {42, 43}, {43, 42}, {40, 48}, {43, 43}, {43, 44}, {44, 43}, {41, 49}, {44, 44}, {44, 45},
  65. {45, 44}, {43, 48}, {45, 45}, {45, 46}, {46, 45}, {44, 49}, {46, 46}, {46, 47}, {47, 46},
  66. {46, 48}, {47, 47}, {47, 48}, {48, 46}, {48, 47}, {49, 46}, {48, 48}, {48, 49}, {49, 48},
  67. {50, 47}, {49, 49}, {49, 50}, {50, 49}, {52, 46}, {50, 50}, {50, 51}, {51, 50}, {53, 47},
  68. {51, 51}, {51, 52}, {52, 51}, {52, 52}, {52, 52}, {52, 53}, {53, 52}, {53, 53}, {53, 53},
  69. {53, 54}, {54, 53}, {54, 54}, {54, 54}, {54, 55}, {55, 54}, {55, 55}, {55, 55}, {55, 56},
  70. {56, 55}, {56, 56}, {56, 56}, {56, 57}, {57, 56}, {57, 57}, {57, 57}, {57, 58}, {58, 57},
  71. {58, 58}, {58, 58}, {58, 59}, {59, 58}, {59, 59}, {59, 59}, {59, 60}, {60, 59}, {60, 60},
  72. {60, 60}, {60, 61}, {61, 60}, {61, 61}, {61, 61}, {61, 62}, {62, 61}, {62, 62}, {62, 62},
  73. {62, 63}, {63, 62}, {63, 63}, {63, 63},
  74. };
  75. static int stb__Mul8Bit(int a, int b) {
  76. int t = a * b + 128;
  77. return (t + (t >> 8)) >> 8;
  78. }
  79. static void stb__From16Bit(unsigned char* out, unsigned short v) {
  80. int rv = (v & 0xf800) >> 11;
  81. int gv = (v & 0x07e0) >> 5;
  82. int bv = (v & 0x001f) >> 0;
  83. // expand to 8 bits via bit replication
  84. out[0] = static_cast<unsigned char>((rv * 33) >> 2);
  85. out[1] = static_cast<unsigned char>((gv * 65) >> 4);
  86. out[2] = static_cast<unsigned char>((bv * 33) >> 2);
  87. out[3] = 0;
  88. }
  89. static unsigned short stb__As16Bit(int r, int g, int b) {
  90. return (unsigned short)((stb__Mul8Bit(r, 31) << 11) + (stb__Mul8Bit(g, 63) << 5) +
  91. stb__Mul8Bit(b, 31));
  92. }
  93. // linear interpolation at 1/3 point between a and b, using desired rounding
  94. // type
  95. static int stb__Lerp13(int a, int b) {
  96. #ifdef STB_DXT_USE_ROUNDING_BIAS
  97. // with rounding bias
  98. return a + stb__Mul8Bit(b - a, 0x55);
  99. #else
  100. // without rounding bias
  101. // replace "/ 3" by "* 0xaaab) >> 17" if your compiler sucks or you really
  102. // need every ounce of speed.
  103. return (2 * a + b) / 3;
  104. #endif
  105. }
  106. // linear interpolation at 1/2 point between a and b
  107. static int stb__Lerp12(int a, int b) {
  108. return (a + b) / 2;
  109. }
  110. // lerp RGB color
  111. static void stb__Lerp13RGB(unsigned char* out, unsigned char* p1, unsigned char* p2) {
  112. out[0] = (unsigned char)stb__Lerp13(p1[0], p2[0]);
  113. out[1] = (unsigned char)stb__Lerp13(p1[1], p2[1]);
  114. out[2] = (unsigned char)stb__Lerp13(p1[2], p2[2]);
  115. }
  116. static void stb__Lerp12RGB(unsigned char* out, unsigned char* p1, unsigned char* p2) {
  117. out[0] = (unsigned char)stb__Lerp12(p1[0], p2[0]);
  118. out[1] = (unsigned char)stb__Lerp12(p1[1], p2[1]);
  119. out[2] = (unsigned char)stb__Lerp12(p1[2], p2[2]);
  120. }
  121. /****************************************************************************/
  122. static void stb__Eval4Colors(unsigned char* color, unsigned short c0, unsigned short c1) {
  123. stb__From16Bit(color + 0, c0);
  124. stb__From16Bit(color + 4, c1);
  125. stb__Lerp13RGB(color + 8, color + 0, color + 4);
  126. stb__Lerp13RGB(color + 12, color + 4, color + 0);
  127. }
  128. static void stb__Eval3Colors(unsigned char* color, unsigned short c0, unsigned short c1) {
  129. stb__From16Bit(color + 0, c0);
  130. stb__From16Bit(color + 4, c1);
  131. stb__Lerp12RGB(color + 8, color + 0, color + 4);
  132. }
  133. // The color matching function
  134. static unsigned int stb__MatchColorsBlock(unsigned char* block, unsigned char* color) {
  135. unsigned int mask = 0;
  136. int dirr = color[0 * 4 + 0] - color[1 * 4 + 0];
  137. int dirg = color[0 * 4 + 1] - color[1 * 4 + 1];
  138. int dirb = color[0 * 4 + 2] - color[1 * 4 + 2];
  139. int dots[16];
  140. int stops[4];
  141. int i;
  142. int c0Point, halfPoint, c3Point;
  143. for (i = 0; i < 16; i++)
  144. dots[i] = block[i * 4 + 0] * dirr + block[i * 4 + 1] * dirg + block[i * 4 + 2] * dirb;
  145. for (i = 0; i < 4; i++)
  146. stops[i] = color[i * 4 + 0] * dirr + color[i * 4 + 1] * dirg + color[i * 4 + 2] * dirb;
  147. // think of the colors as arranged on a line; project point onto that line,
  148. // then choose next color out of available ones. we compute the crossover
  149. // points for "best color in top half"/"best in bottom half" and then the same
  150. // inside that subinterval.
  151. //
  152. // relying on this 1d approximation isn't always optimal in terms of euclidean
  153. // distance, but it's very close and a lot faster.
  154. // http://cbloomrants.blogspot.com/2008/12/12-08-08-dxtc-summary.html
  155. c0Point = (stops[1] + stops[3]);
  156. halfPoint = (stops[3] + stops[2]);
  157. c3Point = (stops[2] + stops[0]);
  158. for (i = 15; i >= 0; i--) {
  159. int dot = dots[i] * 2;
  160. mask <<= 2;
  161. if (dot < halfPoint)
  162. mask |= (dot < c0Point) ? 1 : 3;
  163. else
  164. mask |= (dot < c3Point) ? 2 : 0;
  165. }
  166. return mask;
  167. }
  168. static unsigned int stb__MatchColorsAlphaBlock(unsigned char* block, unsigned char* color) {
  169. unsigned int mask = 0;
  170. int dirr = color[0 * 4 + 0] - color[1 * 4 + 0];
  171. int dirg = color[0 * 4 + 1] - color[1 * 4 + 1];
  172. int dirb = color[0 * 4 + 2] - color[1 * 4 + 2];
  173. int dots[16];
  174. int stops[3];
  175. int i;
  176. int c0Point, c2Point;
  177. for (i = 0; i < 16; i++)
  178. dots[i] = block[i * 4 + 0] * dirr + block[i * 4 + 1] * dirg + block[i * 4 + 2] * dirb;
  179. for (i = 0; i < 3; i++)
  180. stops[i] = color[i * 4 + 0] * dirr + color[i * 4 + 1] * dirg + color[i * 4 + 2] * dirb;
  181. c0Point = (stops[1] + stops[2]);
  182. c2Point = (stops[2] + stops[0]);
  183. for (i = 15; i >= 0; i--) {
  184. int dot = dots[i] * 2;
  185. mask <<= 2;
  186. if (block[i * 4 + 3] == 0)
  187. mask |= 3;
  188. else if (dot < c2Point)
  189. mask |= (dot < c0Point) ? 0 : 2;
  190. else
  191. mask |= (dot < c0Point) ? 1 : 0;
  192. }
  193. return mask;
  194. }
  195. static void stb__ReorderColors(unsigned short* pmax16, unsigned short* pmin16) {
  196. if (*pmin16 < *pmax16) {
  197. unsigned short t = *pmin16;
  198. *pmin16 = *pmax16;
  199. *pmax16 = t;
  200. }
  201. }
  202. static void stb__FinalizeColors(unsigned short* pmax16, unsigned short* pmin16,
  203. unsigned int* pmask) {
  204. if (*pmax16 < *pmin16) {
  205. unsigned short t = *pmin16;
  206. *pmin16 = *pmax16;
  207. *pmax16 = t;
  208. *pmask ^= 0x55555555;
  209. }
  210. }
  211. // The color optimization function. (Clever code, part 1)
  212. static void stb__OptimizeColorsBlock(unsigned char* block, unsigned short* pmax16,
  213. unsigned short* pmin16) {
  214. int mind, maxd;
  215. unsigned char *minp, *maxp;
  216. double magn;
  217. int v_r, v_g, v_b;
  218. static const int nIterPower = 4;
  219. float covf[6], vfr, vfg, vfb;
  220. // determine color distribution
  221. int cov[6];
  222. int mu[3], min[3], max[3];
  223. int ch, i, iter;
  224. for (ch = 0; ch < 3; ch++) {
  225. const unsigned char* bp = ((const unsigned char*)block) + ch;
  226. int muv, minv, maxv;
  227. muv = minv = maxv = bp[0];
  228. for (i = 4; i < 64; i += 4) {
  229. muv += bp[i];
  230. if (bp[i] < minv)
  231. minv = bp[i];
  232. else if (bp[i] > maxv)
  233. maxv = bp[i];
  234. }
  235. mu[ch] = (muv + 8) >> 4;
  236. min[ch] = minv;
  237. max[ch] = maxv;
  238. }
  239. // determine covariance matrix
  240. for (i = 0; i < 6; i++)
  241. cov[i] = 0;
  242. for (i = 0; i < 16; i++) {
  243. int r = block[i * 4 + 0] - mu[0];
  244. int g = block[i * 4 + 1] - mu[1];
  245. int b = block[i * 4 + 2] - mu[2];
  246. cov[0] += r * r;
  247. cov[1] += r * g;
  248. cov[2] += r * b;
  249. cov[3] += g * g;
  250. cov[4] += g * b;
  251. cov[5] += b * b;
  252. }
  253. // convert covariance matrix to float, find principal axis via power iter
  254. for (i = 0; i < 6; i++)
  255. covf[i] = static_cast<float>(cov[i]) / 255.0f;
  256. vfr = (float)(max[0] - min[0]);
  257. vfg = (float)(max[1] - min[1]);
  258. vfb = (float)(max[2] - min[2]);
  259. for (iter = 0; iter < nIterPower; iter++) {
  260. float r = vfr * covf[0] + vfg * covf[1] + vfb * covf[2];
  261. float g = vfr * covf[1] + vfg * covf[3] + vfb * covf[4];
  262. float b = vfr * covf[2] + vfg * covf[4] + vfb * covf[5];
  263. vfr = r;
  264. vfg = g;
  265. vfb = b;
  266. }
  267. magn = STBD_FABS(vfr);
  268. if (STBD_FABS(vfg) > magn)
  269. magn = STBD_FABS(vfg);
  270. if (STBD_FABS(vfb) > magn)
  271. magn = STBD_FABS(vfb);
  272. if (magn < 4.0f) { // too small, default to luminance
  273. v_r = 299; // JPEG YCbCr luma coefs, scaled by 1000.
  274. v_g = 587;
  275. v_b = 114;
  276. } else {
  277. magn = 512.0 / magn;
  278. v_r = (int)(vfr * magn);
  279. v_g = (int)(vfg * magn);
  280. v_b = (int)(vfb * magn);
  281. }
  282. minp = maxp = block;
  283. mind = maxd = block[0] * v_r + block[1] * v_g + block[2] * v_b;
  284. // Pick colors at extreme points
  285. for (i = 1; i < 16; i++) {
  286. int dot = block[i * 4 + 0] * v_r + block[i * 4 + 1] * v_g + block[i * 4 + 2] * v_b;
  287. if (dot < mind) {
  288. mind = dot;
  289. minp = block + i * 4;
  290. }
  291. if (dot > maxd) {
  292. maxd = dot;
  293. maxp = block + i * 4;
  294. }
  295. }
  296. *pmax16 = stb__As16Bit(maxp[0], maxp[1], maxp[2]);
  297. *pmin16 = stb__As16Bit(minp[0], minp[1], minp[2]);
  298. stb__ReorderColors(pmax16, pmin16);
  299. }
  300. static void stb__OptimizeColorsAlphaBlock(unsigned char* block, unsigned short* pmax16,
  301. unsigned short* pmin16) {
  302. int mind, maxd;
  303. unsigned char *minp, *maxp;
  304. double magn;
  305. int v_r, v_g, v_b;
  306. static const int nIterPower = 4;
  307. float covf[6], vfr, vfg, vfb;
  308. // determine color distribution
  309. int cov[6];
  310. int mu[3], min[3], max[3];
  311. int ch, i, iter;
  312. for (ch = 0; ch < 3; ch++) {
  313. const unsigned char* bp = ((const unsigned char*)block) + ch;
  314. int muv = 0, minv = 256, maxv = -1;
  315. int num = 0;
  316. for (i = 0; i < 64; i += 4) {
  317. if (bp[3 - ch] == 0) {
  318. continue;
  319. }
  320. muv += bp[i];
  321. if (bp[i] < minv)
  322. minv = bp[i];
  323. else if (bp[i] > maxv)
  324. maxv = bp[i];
  325. num++;
  326. }
  327. mu[ch] = num > 0 ? (muv + 8) / num : 0;
  328. min[ch] = minv;
  329. max[ch] = maxv;
  330. }
  331. // determine covariance matrix
  332. for (i = 0; i < 6; i++)
  333. cov[i] = 0;
  334. for (i = 0; i < 16; i++) {
  335. if (block[i * 4 + 3] == 0) {
  336. continue;
  337. }
  338. int r = block[i * 4 + 0] - mu[0];
  339. int g = block[i * 4 + 1] - mu[1];
  340. int b = block[i * 4 + 2] - mu[2];
  341. cov[0] += r * r;
  342. cov[1] += r * g;
  343. cov[2] += r * b;
  344. cov[3] += g * g;
  345. cov[4] += g * b;
  346. cov[5] += b * b;
  347. }
  348. // convert covariance matrix to float, find principal axis via power iter
  349. for (i = 0; i < 6; i++)
  350. covf[i] = static_cast<float>(cov[i]) / 255.0f;
  351. vfr = (float)(max[0] - min[0]);
  352. vfg = (float)(max[1] - min[1]);
  353. vfb = (float)(max[2] - min[2]);
  354. for (iter = 0; iter < nIterPower; iter++) {
  355. float r = vfr * covf[0] + vfg * covf[1] + vfb * covf[2];
  356. float g = vfr * covf[1] + vfg * covf[3] + vfb * covf[4];
  357. float b = vfr * covf[2] + vfg * covf[4] + vfb * covf[5];
  358. vfr = r;
  359. vfg = g;
  360. vfb = b;
  361. }
  362. magn = STBD_FABS(vfr);
  363. if (STBD_FABS(vfg) > magn)
  364. magn = STBD_FABS(vfg);
  365. if (STBD_FABS(vfb) > magn)
  366. magn = STBD_FABS(vfb);
  367. if (magn < 4.0f) { // too small, default to luminance
  368. v_r = 299; // JPEG YCbCr luma coefs, scaled by 1000.
  369. v_g = 587;
  370. v_b = 114;
  371. } else {
  372. magn = 512.0 / magn;
  373. v_r = (int)(vfr * magn);
  374. v_g = (int)(vfg * magn);
  375. v_b = (int)(vfb * magn);
  376. }
  377. minp = maxp = NULL;
  378. mind = 0x7fffffff;
  379. maxd = -0x80000000;
  380. // Pick colors at extreme points
  381. for (i = 0; i < 16; i++) {
  382. if (block[i * 4 + 3] == 0) {
  383. continue;
  384. }
  385. int dot = block[i * 4 + 0] * v_r + block[i * 4 + 1] * v_g + block[i * 4 + 2] * v_b;
  386. if (dot < mind) {
  387. mind = dot;
  388. minp = block + i * 4;
  389. }
  390. if (dot > maxd) {
  391. maxd = dot;
  392. maxp = block + i * 4;
  393. }
  394. }
  395. if (!maxp) {
  396. // all alpha, no color
  397. *pmin16 = 0xffff;
  398. *pmax16 = 0;
  399. } else {
  400. // endpoint colors found
  401. *pmax16 = stb__As16Bit(maxp[0], maxp[1], maxp[2]);
  402. *pmin16 = stb__As16Bit(minp[0], minp[1], minp[2]);
  403. if (*pmax16 == *pmin16) {
  404. // modify the endpoints to indicate presence of an alpha block
  405. if (*pmax16 > 0) {
  406. (*pmax16)--;
  407. } else {
  408. (*pmin16)++;
  409. }
  410. }
  411. stb__ReorderColors(pmax16, pmin16);
  412. }
  413. }
  414. static const float stb__midpoints5[32] = {
  415. 0.015686f, 0.047059f, 0.078431f, 0.111765f, 0.145098f, 0.176471f, 0.207843f, 0.241176f,
  416. 0.274510f, 0.305882f, 0.337255f, 0.370588f, 0.403922f, 0.435294f, 0.466667f, 0.5f,
  417. 0.533333f, 0.564706f, 0.596078f, 0.629412f, 0.662745f, 0.694118f, 0.725490f, 0.758824f,
  418. 0.792157f, 0.823529f, 0.854902f, 0.888235f, 0.921569f, 0.952941f, 0.984314f, 1.0f};
  419. static const float stb__midpoints6[64] = {
  420. 0.007843f, 0.023529f, 0.039216f, 0.054902f, 0.070588f, 0.086275f, 0.101961f, 0.117647f,
  421. 0.133333f, 0.149020f, 0.164706f, 0.180392f, 0.196078f, 0.211765f, 0.227451f, 0.245098f,
  422. 0.262745f, 0.278431f, 0.294118f, 0.309804f, 0.325490f, 0.341176f, 0.356863f, 0.372549f,
  423. 0.388235f, 0.403922f, 0.419608f, 0.435294f, 0.450980f, 0.466667f, 0.482353f, 0.500000f,
  424. 0.517647f, 0.533333f, 0.549020f, 0.564706f, 0.580392f, 0.596078f, 0.611765f, 0.627451f,
  425. 0.643137f, 0.658824f, 0.674510f, 0.690196f, 0.705882f, 0.721569f, 0.737255f, 0.754902f,
  426. 0.772549f, 0.788235f, 0.803922f, 0.819608f, 0.835294f, 0.850980f, 0.866667f, 0.882353f,
  427. 0.898039f, 0.913725f, 0.929412f, 0.945098f, 0.960784f, 0.976471f, 0.992157f, 1.0f};
  428. static unsigned short stb__Quantize5(float x) {
  429. unsigned short q;
  430. x = x < 0 ? 0 : x > 1 ? 1 : x; // saturate
  431. q = (unsigned short)(x * 31);
  432. q += (x > stb__midpoints5[q]);
  433. return q;
  434. }
  435. static unsigned short stb__Quantize6(float x) {
  436. unsigned short q;
  437. x = x < 0 ? 0 : x > 1 ? 1 : x; // saturate
  438. q = (unsigned short)(x * 63);
  439. q += (x > stb__midpoints6[q]);
  440. return q;
  441. }
  442. // The refinement function. (Clever code, part 2)
  443. // Tries to optimize colors to suit block contents better.
  444. // (By solving a least squares system via normal equations+Cramer's rule)
  445. static int stb__RefineBlock(unsigned char* block, unsigned short* pmax16, unsigned short* pmin16,
  446. unsigned int mask) {
  447. static const int w1Tab[4] = {3, 0, 2, 1};
  448. static const int prods[4] = {0x090000, 0x000900, 0x040102, 0x010402};
  449. // ^some magic to save a lot of multiplies in the accumulating loop...
  450. // (precomputed products of weights for least squares system, accumulated
  451. // inside one 32-bit register)
  452. float f;
  453. unsigned short oldMin, oldMax, min16, max16;
  454. int i, akku = 0, xx, xy, yy;
  455. int At1_r, At1_g, At1_b;
  456. int At2_r, At2_g, At2_b;
  457. unsigned int cm = mask;
  458. oldMin = *pmin16;
  459. oldMax = *pmax16;
  460. if ((mask ^ (mask << 2)) < 4) // all pixels have the same index?
  461. {
  462. // yes, linear system would be singular; solve using optimal
  463. // single-color match on average color
  464. int r = 8, g = 8, b = 8;
  465. for (i = 0; i < 16; ++i) {
  466. r += block[i * 4 + 0];
  467. g += block[i * 4 + 1];
  468. b += block[i * 4 + 2];
  469. }
  470. r >>= 4;
  471. g >>= 4;
  472. b >>= 4;
  473. max16 = static_cast<unsigned short>((stb__OMatch5[r][0] << 11) | (stb__OMatch6[g][0] << 5) |
  474. stb__OMatch5[b][0]);
  475. min16 = static_cast<unsigned short>((stb__OMatch5[r][1] << 11) | (stb__OMatch6[g][1] << 5) |
  476. stb__OMatch5[b][1]);
  477. } else {
  478. At1_r = At1_g = At1_b = 0;
  479. At2_r = At2_g = At2_b = 0;
  480. for (i = 0; i < 16; ++i, cm >>= 2) {
  481. int step = cm & 3;
  482. int w1 = w1Tab[step];
  483. int r = block[i * 4 + 0];
  484. int g = block[i * 4 + 1];
  485. int b = block[i * 4 + 2];
  486. akku += prods[step];
  487. At1_r += w1 * r;
  488. At1_g += w1 * g;
  489. At1_b += w1 * b;
  490. At2_r += r;
  491. At2_g += g;
  492. At2_b += b;
  493. }
  494. At2_r = 3 * At2_r - At1_r;
  495. At2_g = 3 * At2_g - At1_g;
  496. At2_b = 3 * At2_b - At1_b;
  497. // extract solutions and decide solvability
  498. xx = akku >> 16;
  499. yy = (akku >> 8) & 0xff;
  500. xy = (akku >> 0) & 0xff;
  501. f = 3.0f / 255.0f / static_cast<float>(xx * yy - xy * xy);
  502. max16 = static_cast<unsigned short>(
  503. stb__Quantize5(static_cast<float>(At1_r * yy - At2_r * xy) * f) << 11);
  504. max16 |= static_cast<unsigned short>(
  505. stb__Quantize6(static_cast<float>(At1_g * yy - At2_g * xy) * f) << 5);
  506. max16 |= static_cast<unsigned short>(
  507. stb__Quantize5(static_cast<float>(At1_b * yy - At2_b * xy) * f) << 0);
  508. min16 = static_cast<unsigned short>(
  509. stb__Quantize5(static_cast<float>(At2_r * xx - At1_r * xy) * f) << 11);
  510. min16 |= static_cast<unsigned short>(
  511. stb__Quantize6(static_cast<float>(At2_g * xx - At1_g * xy) * f) << 5);
  512. min16 |= static_cast<unsigned short>(
  513. stb__Quantize5(static_cast<float>(At2_b * xx - At1_b * xy) * f) << 0);
  514. }
  515. *pmin16 = min16;
  516. *pmax16 = max16;
  517. stb__ReorderColors(pmax16, pmin16);
  518. return oldMin != min16 || oldMax != max16;
  519. }
  520. // Color block compression
  521. static void stb__CompressColorBlock(unsigned char* dest, unsigned char* block, int alpha,
  522. int mode) {
  523. unsigned int mask;
  524. int i;
  525. int refinecount;
  526. unsigned short max16, min16;
  527. unsigned char color[4 * 4];
  528. refinecount = (mode & STB_DXT_HIGHQUAL) ? 2 : 1;
  529. // check if block is constant
  530. for (i = 1; i < 16; i++)
  531. if (((unsigned int*)block)[i] != ((unsigned int*)block)[0])
  532. break;
  533. if (i == 16 && block[3] == 0 && alpha) { // constant alpha
  534. mask = 0xffffffff;
  535. max16 = 0;
  536. min16 = 0xffff;
  537. } else if (i == 16) { // constant color
  538. int r = block[0], g = block[1], b = block[2];
  539. mask = 0xaaaaaaaa;
  540. max16 = static_cast<unsigned short>((stb__OMatch5[r][0] << 11) | (stb__OMatch6[g][0] << 5) |
  541. stb__OMatch5[b][0]);
  542. min16 = static_cast<unsigned short>((stb__OMatch5[r][1] << 11) | (stb__OMatch6[g][1] << 5) |
  543. stb__OMatch5[b][1]);
  544. } else if (alpha) {
  545. stb__OptimizeColorsAlphaBlock(block, &max16, &min16);
  546. stb__Eval3Colors(color, max16, min16);
  547. mask = stb__MatchColorsAlphaBlock(block, color);
  548. } else {
  549. // first step: PCA+map along principal axis
  550. stb__OptimizeColorsBlock(block, &max16, &min16);
  551. if (max16 != min16) {
  552. stb__Eval4Colors(color, max16, min16);
  553. mask = stb__MatchColorsBlock(block, color);
  554. } else
  555. mask = 0;
  556. // third step: refine (multiple times if requested)
  557. for (i = 0; i < refinecount; i++) {
  558. unsigned int lastmask = mask;
  559. if (stb__RefineBlock(block, &max16, &min16, mask)) {
  560. if (max16 != min16) {
  561. stb__Eval4Colors(color, max16, min16);
  562. mask = stb__MatchColorsBlock(block, color);
  563. } else {
  564. mask = 0;
  565. break;
  566. }
  567. }
  568. if (mask == lastmask)
  569. break;
  570. }
  571. }
  572. // write the color block
  573. if (!alpha)
  574. stb__FinalizeColors(&max16, &min16, &mask);
  575. dest[0] = (unsigned char)(max16);
  576. dest[1] = (unsigned char)(max16 >> 8);
  577. dest[2] = (unsigned char)(min16);
  578. dest[3] = (unsigned char)(min16 >> 8);
  579. dest[4] = (unsigned char)(mask);
  580. dest[5] = (unsigned char)(mask >> 8);
  581. dest[6] = (unsigned char)(mask >> 16);
  582. dest[7] = (unsigned char)(mask >> 24);
  583. }
  584. // Alpha block compression (this is easy for a change)
  585. static void stb__CompressAlphaBlock(unsigned char* dest, unsigned char* src, int stride) {
  586. int i, dist, bias, dist4, dist2, bits, mask;
  587. // find min/max color
  588. int mn, mx;
  589. mn = mx = src[0];
  590. for (i = 1; i < 16; i++) {
  591. if (src[i * stride] < mn)
  592. mn = src[i * stride];
  593. else if (src[i * stride] > mx)
  594. mx = src[i * stride];
  595. }
  596. // encode them
  597. dest[0] = (unsigned char)mx;
  598. dest[1] = (unsigned char)mn;
  599. dest += 2;
  600. // determine bias and emit color indices
  601. // given the choice of mx/mn, these indices are optimal:
  602. // http://fgiesen.wordpress.com/2009/12/15/dxt5-alpha-block-index-determination/
  603. dist = mx - mn;
  604. dist4 = dist * 4;
  605. dist2 = dist * 2;
  606. bias = (dist < 8) ? (dist - 1) : (dist / 2 + 2);
  607. bias -= mn * 7;
  608. bits = 0, mask = 0;
  609. for (i = 0; i < 16; i++) {
  610. int a = src[i * stride] * 7 + bias;
  611. int ind, t;
  612. // select index. this is a "linear scale" lerp factor between 0 (val=min)
  613. // and 7 (val=max).
  614. t = (a >= dist4) ? -1 : 0;
  615. ind = t & 4;
  616. a -= dist4 & t;
  617. t = (a >= dist2) ? -1 : 0;
  618. ind += t & 2;
  619. a -= dist2 & t;
  620. ind += (a >= dist);
  621. // turn linear scale into DXT index (0/1 are extremal pts)
  622. ind = -ind & 7;
  623. ind ^= (2 > ind);
  624. // write index
  625. mask |= ind << bits;
  626. if ((bits += 3) >= 8) {
  627. *dest++ = (unsigned char)mask;
  628. mask >>= 8;
  629. bits -= 8;
  630. }
  631. }
  632. }
  633. void stb_compress_bc1_block(unsigned char* dest, const unsigned char* src, int alpha, int mode) {
  634. stb__CompressColorBlock(dest, (unsigned char*)src, alpha, mode);
  635. }
  636. void stb_compress_bc3_block(unsigned char* dest, const unsigned char* src, int mode) {
  637. unsigned char data[16][4];
  638. int i;
  639. stb__CompressAlphaBlock(dest, (unsigned char*)src + 3, 4);
  640. dest += 8;
  641. // make a new copy of the data in which alpha is opaque,
  642. // because code uses a fast test for color constancy
  643. memcpy(data, src, 4 * 16);
  644. for (i = 0; i < 16; ++i)
  645. data[i][3] = 255;
  646. src = &data[0][0];
  647. stb__CompressColorBlock(dest, (unsigned char*)src, 0, mode);
  648. }