You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

719 lines
22 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // stb_dxt.h - v1.07 - DXT1/DXT5 compressor - public domain
  2. // original by fabian "ryg" giesen - ported to C by stb
  3. // use '#define STB_DXT_IMPLEMENTATION' before including to create the implementation
  4. //
  5. // USAGE:
  6. // call stb_compress_dxt_block() for every block (you must pad)
  7. // source should be a 4x4 block of RGBA data in row-major order;
  8. // A is ignored if you specify alpha=0; you can turn on dithering
  9. // and "high quality" using mode.
  10. //
  11. // version history:
  12. // v1.07 - bc4; allow not using libc; add STB_DXT_STATIC
  13. // v1.06 - (stb) fix to known-broken 1.05
  14. // v1.05 - (stb) support bc5/3dc (Arvids Kokins), use extern "C" in C++ (Pavel Krajcevski)
  15. // v1.04 - (ryg) default to no rounding bias for lerped colors (as per S3TC/DX10 spec);
  16. // single color match fix (allow for inexact color interpolation);
  17. // optimal DXT5 index finder; "high quality" mode that runs multiple refinement steps.
  18. // v1.03 - (stb) endianness support
  19. // v1.02 - (stb) fix alpha encoding bug
  20. // v1.01 - (stb) fix bug converting to RGB that messed up quality, thanks ryg & cbloom
  21. // v1.00 - (stb) first release
  22. //
  23. // contributors:
  24. // Kevin Schmidt (#defines for "freestanding" compilation)
  25. // github:ppiastucki (BC4 support)
  26. //
  27. // LICENSE
  28. //
  29. // See end of file for license information.
  30. #ifndef STB_INCLUDE_STB_DXT_H
  31. #define STB_INCLUDE_STB_DXT_H
  32. // compression mode (bitflags)
  33. #define STB_DXT_NORMAL 0
  34. #define STB_DXT_DITHER 1 // use dithering. dubious win. never use for normal maps and the like!
  35. #define STB_DXT_HIGHQUAL 2 // high quality mode, does two refinement steps instead of 1. ~30-40% slower.
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #ifdef STB_DXT_STATIC
  40. #define STBDDEF static
  41. #else
  42. #define STBDDEF extern
  43. #endif
  44. STBDDEF void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src_rgba_four_bytes_per_pixel, int alpha, int mode);
  45. STBDDEF void stb_compress_bc4_block(unsigned char *dest, const unsigned char *src_r_one_byte_per_pixel);
  46. STBDDEF void stb_compress_bc5_block(unsigned char *dest, const unsigned char *src_rg_two_byte_per_pixel);
  47. #ifdef __cplusplus
  48. }
  49. #endif
  50. #define STB_COMPRESS_DXT_BLOCK
  51. #ifdef STB_DXT_IMPLEMENTATION
  52. // configuration options for DXT encoder. set them in the project/makefile or just define
  53. // them at the top.
  54. // STB_DXT_USE_ROUNDING_BIAS
  55. // use a rounding bias during color interpolation. this is closer to what "ideal"
  56. // interpolation would do but doesn't match the S3TC/DX10 spec. old versions (pre-1.03)
  57. // implicitly had this turned on.
  58. //
  59. // in case you're targeting a specific type of hardware (e.g. console programmers):
  60. // NVidia and Intel GPUs (as of 2010) as well as DX9 ref use DXT decoders that are closer
  61. // to STB_DXT_USE_ROUNDING_BIAS. AMD/ATI, S3 and DX10 ref are closer to rounding with no bias.
  62. // you also see "(a*5 + b*3) / 8" on some old GPU designs.
  63. // #define STB_DXT_USE_ROUNDING_BIAS
  64. #include <stdlib.h>
  65. #if !defined(STBD_ABS) || !defined(STBI_FABS)
  66. #include <math.h>
  67. #endif
  68. #ifndef STBD_ABS
  69. #define STBD_ABS(i) abs(i)
  70. #endif
  71. #ifndef STBD_FABS
  72. #define STBD_FABS(x) fabs(x)
  73. #endif
  74. #ifndef STBD_MEMSET
  75. #include <string.h>
  76. #define STBD_MEMSET(x) memset(x)
  77. #endif
  78. static unsigned char stb__Expand5[32];
  79. static unsigned char stb__Expand6[64];
  80. static unsigned char stb__OMatch5[256][2];
  81. static unsigned char stb__OMatch6[256][2];
  82. static unsigned char stb__QuantRBTab[256+16];
  83. static unsigned char stb__QuantGTab[256+16];
  84. static int stb__Mul8Bit(int a, int b)
  85. {
  86. int t = a*b + 128;
  87. return (t + (t >> 8)) >> 8;
  88. }
  89. static void stb__From16Bit(unsigned char *out, unsigned short v)
  90. {
  91. int rv = (v & 0xf800) >> 11;
  92. int gv = (v & 0x07e0) >> 5;
  93. int bv = (v & 0x001f) >> 0;
  94. out[0] = stb__Expand5[rv];
  95. out[1] = stb__Expand6[gv];
  96. out[2] = stb__Expand5[bv];
  97. out[3] = 0;
  98. }
  99. static unsigned short stb__As16Bit(int r, int g, int b)
  100. {
  101. return (stb__Mul8Bit(r,31) << 11) + (stb__Mul8Bit(g,63) << 5) + stb__Mul8Bit(b,31);
  102. }
  103. // linear interpolation at 1/3 point between a and b, using desired rounding type
  104. static int stb__Lerp13(int a, int b)
  105. {
  106. #ifdef STB_DXT_USE_ROUNDING_BIAS
  107. // with rounding bias
  108. return a + stb__Mul8Bit(b-a, 0x55);
  109. #else
  110. // without rounding bias
  111. // replace "/ 3" by "* 0xaaab) >> 17" if your compiler sucks or you really need every ounce of speed.
  112. return (2*a + b) / 3;
  113. #endif
  114. }
  115. // lerp RGB color
  116. static void stb__Lerp13RGB(unsigned char *out, unsigned char *p1, unsigned char *p2)
  117. {
  118. out[0] = stb__Lerp13(p1[0], p2[0]);
  119. out[1] = stb__Lerp13(p1[1], p2[1]);
  120. out[2] = stb__Lerp13(p1[2], p2[2]);
  121. }
  122. /****************************************************************************/
  123. // compute table to reproduce constant colors as accurately as possible
  124. static void stb__PrepareOptTable(unsigned char *Table,const unsigned char *expand,int size)
  125. {
  126. int i,mn,mx;
  127. for (i=0;i<256;i++) {
  128. int bestErr = 256;
  129. for (mn=0;mn<size;mn++) {
  130. for (mx=0;mx<size;mx++) {
  131. int mine = expand[mn];
  132. int maxe = expand[mx];
  133. int err = STBD_ABS(stb__Lerp13(maxe, mine) - i);
  134. // DX10 spec says that interpolation must be within 3% of "correct" result,
  135. // add this as error term. (normally we'd expect a random distribution of
  136. // +-1.5% error, but nowhere in the spec does it say that the error has to be
  137. // unbiased - better safe than sorry).
  138. err += STBD_ABS(maxe - mine) * 3 / 100;
  139. if(err < bestErr)
  140. {
  141. Table[i*2+0] = mx;
  142. Table[i*2+1] = mn;
  143. bestErr = err;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. static void stb__EvalColors(unsigned char *color,unsigned short c0,unsigned short c1)
  150. {
  151. stb__From16Bit(color+ 0, c0);
  152. stb__From16Bit(color+ 4, c1);
  153. stb__Lerp13RGB(color+ 8, color+0, color+4);
  154. stb__Lerp13RGB(color+12, color+4, color+0);
  155. }
  156. // Block dithering function. Simply dithers a block to 565 RGB.
  157. // (Floyd-Steinberg)
  158. static void stb__DitherBlock(unsigned char *dest, unsigned char *block)
  159. {
  160. int err[8],*ep1 = err,*ep2 = err+4, *et;
  161. int ch,y;
  162. // process channels seperately
  163. for (ch=0; ch<3; ++ch) {
  164. unsigned char *bp = block+ch, *dp = dest+ch;
  165. unsigned char *quant = (ch == 1) ? stb__QuantGTab+8 : stb__QuantRBTab+8;
  166. STBD_MEMSET(err, 0, sizeof(err));
  167. for(y=0; y<4; ++y) {
  168. dp[ 0] = quant[bp[ 0] + ((3*ep2[1] + 5*ep2[0]) >> 4)];
  169. ep1[0] = bp[ 0] - dp[ 0];
  170. dp[ 4] = quant[bp[ 4] + ((7*ep1[0] + 3*ep2[2] + 5*ep2[1] + ep2[0]) >> 4)];
  171. ep1[1] = bp[ 4] - dp[ 4];
  172. dp[ 8] = quant[bp[ 8] + ((7*ep1[1] + 3*ep2[3] + 5*ep2[2] + ep2[1]) >> 4)];
  173. ep1[2] = bp[ 8] - dp[ 8];
  174. dp[12] = quant[bp[12] + ((7*ep1[2] + 5*ep2[3] + ep2[2]) >> 4)];
  175. ep1[3] = bp[12] - dp[12];
  176. bp += 16;
  177. dp += 16;
  178. et = ep1, ep1 = ep2, ep2 = et; // swap
  179. }
  180. }
  181. }
  182. // The color matching function
  183. static unsigned int stb__MatchColorsBlock(unsigned char *block, unsigned char *color,int dither)
  184. {
  185. unsigned int mask = 0;
  186. int dirr = color[0*4+0] - color[1*4+0];
  187. int dirg = color[0*4+1] - color[1*4+1];
  188. int dirb = color[0*4+2] - color[1*4+2];
  189. int dots[16];
  190. int stops[4];
  191. int i;
  192. int c0Point, halfPoint, c3Point;
  193. for(i=0;i<16;i++)
  194. dots[i] = block[i*4+0]*dirr + block[i*4+1]*dirg + block[i*4+2]*dirb;
  195. for(i=0;i<4;i++)
  196. stops[i] = color[i*4+0]*dirr + color[i*4+1]*dirg + color[i*4+2]*dirb;
  197. // think of the colors as arranged on a line; project point onto that line, then choose
  198. // next color out of available ones. we compute the crossover points for "best color in top
  199. // half"/"best in bottom half" and then the same inside that subinterval.
  200. //
  201. // relying on this 1d approximation isn't always optimal in terms of euclidean distance,
  202. // but it's very close and a lot faster.
  203. // http://cbloomrants.blogspot.com/2008/12/12-08-08-dxtc-summary.html
  204. c0Point = (stops[1] + stops[3]) >> 1;
  205. halfPoint = (stops[3] + stops[2]) >> 1;
  206. c3Point = (stops[2] + stops[0]) >> 1;
  207. if(!dither) {
  208. // the version without dithering is straightforward
  209. for (i=15;i>=0;i--) {
  210. int dot = dots[i];
  211. mask <<= 2;
  212. if(dot < halfPoint)
  213. mask |= (dot < c0Point) ? 1 : 3;
  214. else
  215. mask |= (dot < c3Point) ? 2 : 0;
  216. }
  217. } else {
  218. // with floyd-steinberg dithering
  219. int err[8],*ep1 = err,*ep2 = err+4;
  220. int *dp = dots, y;
  221. c0Point <<= 4;
  222. halfPoint <<= 4;
  223. c3Point <<= 4;
  224. for(i=0;i<8;i++)
  225. err[i] = 0;
  226. for(y=0;y<4;y++)
  227. {
  228. int dot,lmask,step;
  229. dot = (dp[0] << 4) + (3*ep2[1] + 5*ep2[0]);
  230. if(dot < halfPoint)
  231. step = (dot < c0Point) ? 1 : 3;
  232. else
  233. step = (dot < c3Point) ? 2 : 0;
  234. ep1[0] = dp[0] - stops[step];
  235. lmask = step;
  236. dot = (dp[1] << 4) + (7*ep1[0] + 3*ep2[2] + 5*ep2[1] + ep2[0]);
  237. if(dot < halfPoint)
  238. step = (dot < c0Point) ? 1 : 3;
  239. else
  240. step = (dot < c3Point) ? 2 : 0;
  241. ep1[1] = dp[1] - stops[step];
  242. lmask |= step<<2;
  243. dot = (dp[2] << 4) + (7*ep1[1] + 3*ep2[3] + 5*ep2[2] + ep2[1]);
  244. if(dot < halfPoint)
  245. step = (dot < c0Point) ? 1 : 3;
  246. else
  247. step = (dot < c3Point) ? 2 : 0;
  248. ep1[2] = dp[2] - stops[step];
  249. lmask |= step<<4;
  250. dot = (dp[3] << 4) + (7*ep1[2] + 5*ep2[3] + ep2[2]);
  251. if(dot < halfPoint)
  252. step = (dot < c0Point) ? 1 : 3;
  253. else
  254. step = (dot < c3Point) ? 2 : 0;
  255. ep1[3] = dp[3] - stops[step];
  256. lmask |= step<<6;
  257. dp += 4;
  258. mask |= lmask << (y*8);
  259. { int *et = ep1; ep1 = ep2; ep2 = et; } // swap
  260. }
  261. }
  262. return mask;
  263. }
  264. // The color optimization function. (Clever code, part 1)
  265. static void stb__OptimizeColorsBlock(unsigned char *block, unsigned short *pmax16, unsigned short *pmin16)
  266. {
  267. int mind = 0x7fffffff,maxd = -0x7fffffff;
  268. unsigned char *minp, *maxp;
  269. double magn;
  270. int v_r,v_g,v_b;
  271. static const int nIterPower = 4;
  272. float covf[6],vfr,vfg,vfb;
  273. // determine color distribution
  274. int cov[6];
  275. int mu[3],min[3],max[3];
  276. int ch,i,iter;
  277. for(ch=0;ch<3;ch++)
  278. {
  279. const unsigned char *bp = ((const unsigned char *) block) + ch;
  280. int muv,minv,maxv;
  281. muv = minv = maxv = bp[0];
  282. for(i=4;i<64;i+=4)
  283. {
  284. muv += bp[i];
  285. if (bp[i] < minv) minv = bp[i];
  286. else if (bp[i] > maxv) maxv = bp[i];
  287. }
  288. mu[ch] = (muv + 8) >> 4;
  289. min[ch] = minv;
  290. max[ch] = maxv;
  291. }
  292. // determine covariance matrix
  293. for (i=0;i<6;i++)
  294. cov[i] = 0;
  295. for (i=0;i<16;i++)
  296. {
  297. int r = block[i*4+0] - mu[0];
  298. int g = block[i*4+1] - mu[1];
  299. int b = block[i*4+2] - mu[2];
  300. cov[0] += r*r;
  301. cov[1] += r*g;
  302. cov[2] += r*b;
  303. cov[3] += g*g;
  304. cov[4] += g*b;
  305. cov[5] += b*b;
  306. }
  307. // convert covariance matrix to float, find principal axis via power iter
  308. for(i=0;i<6;i++)
  309. covf[i] = cov[i] / 255.0f;
  310. vfr = (float) (max[0] - min[0]);
  311. vfg = (float) (max[1] - min[1]);
  312. vfb = (float) (max[2] - min[2]);
  313. for(iter=0;iter<nIterPower;iter++)
  314. {
  315. float r = vfr*covf[0] + vfg*covf[1] + vfb*covf[2];
  316. float g = vfr*covf[1] + vfg*covf[3] + vfb*covf[4];
  317. float b = vfr*covf[2] + vfg*covf[4] + vfb*covf[5];
  318. vfr = r;
  319. vfg = g;
  320. vfb = b;
  321. }
  322. magn = STBD_FABS(vfr);
  323. if (STBD_FABS(vfg) > magn) magn = STBD_FABS(vfg);
  324. if (STBD_FABS(vfb) > magn) magn = STBD_FABS(vfb);
  325. if(magn < 4.0f) { // too small, default to luminance
  326. v_r = 299; // JPEG YCbCr luma coefs, scaled by 1000.
  327. v_g = 587;
  328. v_b = 114;
  329. } else {
  330. magn = 512.0 / magn;
  331. v_r = (int) (vfr * magn);
  332. v_g = (int) (vfg * magn);
  333. v_b = (int) (vfb * magn);
  334. }
  335. // Pick colors at extreme points
  336. for(i=0;i<16;i++)
  337. {
  338. int dot = block[i*4+0]*v_r + block[i*4+1]*v_g + block[i*4+2]*v_b;
  339. if (dot < mind) {
  340. mind = dot;
  341. minp = block+i*4;
  342. }
  343. if (dot > maxd) {
  344. maxd = dot;
  345. maxp = block+i*4;
  346. }
  347. }
  348. *pmax16 = stb__As16Bit(maxp[0],maxp[1],maxp[2]);
  349. *pmin16 = stb__As16Bit(minp[0],minp[1],minp[2]);
  350. }
  351. static int stb__sclamp(float y, int p0, int p1)
  352. {
  353. int x = (int) y;
  354. if (x < p0) return p0;
  355. if (x > p1) return p1;
  356. return x;
  357. }
  358. // The refinement function. (Clever code, part 2)
  359. // Tries to optimize colors to suit block contents better.
  360. // (By solving a least squares system via normal equations+Cramer's rule)
  361. static int stb__RefineBlock(unsigned char *block, unsigned short *pmax16, unsigned short *pmin16, unsigned int mask)
  362. {
  363. static const int w1Tab[4] = { 3,0,2,1 };
  364. static const int prods[4] = { 0x090000,0x000900,0x040102,0x010402 };
  365. // ^some magic to save a lot of multiplies in the accumulating loop...
  366. // (precomputed products of weights for least squares system, accumulated inside one 32-bit register)
  367. float frb,fg;
  368. unsigned short oldMin, oldMax, min16, max16;
  369. int i, akku = 0, xx,xy,yy;
  370. int At1_r,At1_g,At1_b;
  371. int At2_r,At2_g,At2_b;
  372. unsigned int cm = mask;
  373. oldMin = *pmin16;
  374. oldMax = *pmax16;
  375. if((mask ^ (mask<<2)) < 4) // all pixels have the same index?
  376. {
  377. // yes, linear system would be singular; solve using optimal
  378. // single-color match on average color
  379. int r = 8, g = 8, b = 8;
  380. for (i=0;i<16;++i) {
  381. r += block[i*4+0];
  382. g += block[i*4+1];
  383. b += block[i*4+2];
  384. }
  385. r >>= 4; g >>= 4; b >>= 4;
  386. max16 = (stb__OMatch5[r][0]<<11) | (stb__OMatch6[g][0]<<5) | stb__OMatch5[b][0];
  387. min16 = (stb__OMatch5[r][1]<<11) | (stb__OMatch6[g][1]<<5) | stb__OMatch5[b][1];
  388. } else {
  389. At1_r = At1_g = At1_b = 0;
  390. At2_r = At2_g = At2_b = 0;
  391. for (i=0;i<16;++i,cm>>=2) {
  392. int step = cm&3;
  393. int w1 = w1Tab[step];
  394. int r = block[i*4+0];
  395. int g = block[i*4+1];
  396. int b = block[i*4+2];
  397. akku += prods[step];
  398. At1_r += w1*r;
  399. At1_g += w1*g;
  400. At1_b += w1*b;
  401. At2_r += r;
  402. At2_g += g;
  403. At2_b += b;
  404. }
  405. At2_r = 3*At2_r - At1_r;
  406. At2_g = 3*At2_g - At1_g;
  407. At2_b = 3*At2_b - At1_b;
  408. // extract solutions and decide solvability
  409. xx = akku >> 16;
  410. yy = (akku >> 8) & 0xff;
  411. xy = (akku >> 0) & 0xff;
  412. frb = 3.0f * 31.0f / 255.0f / (xx*yy - xy*xy);
  413. fg = frb * 63.0f / 31.0f;
  414. // solve.
  415. max16 = stb__sclamp((At1_r*yy - At2_r*xy)*frb+0.5f,0,31) << 11;
  416. max16 |= stb__sclamp((At1_g*yy - At2_g*xy)*fg +0.5f,0,63) << 5;
  417. max16 |= stb__sclamp((At1_b*yy - At2_b*xy)*frb+0.5f,0,31) << 0;
  418. min16 = stb__sclamp((At2_r*xx - At1_r*xy)*frb+0.5f,0,31) << 11;
  419. min16 |= stb__sclamp((At2_g*xx - At1_g*xy)*fg +0.5f,0,63) << 5;
  420. min16 |= stb__sclamp((At2_b*xx - At1_b*xy)*frb+0.5f,0,31) << 0;
  421. }
  422. *pmin16 = min16;
  423. *pmax16 = max16;
  424. return oldMin != min16 || oldMax != max16;
  425. }
  426. // Color block compression
  427. static void stb__CompressColorBlock(unsigned char *dest, unsigned char *block, int mode)
  428. {
  429. unsigned int mask;
  430. int i;
  431. int dither;
  432. int refinecount;
  433. unsigned short max16, min16;
  434. unsigned char dblock[16*4],color[4*4];
  435. dither = mode & STB_DXT_DITHER;
  436. refinecount = (mode & STB_DXT_HIGHQUAL) ? 2 : 1;
  437. // check if block is constant
  438. for (i=1;i<16;i++)
  439. if (((unsigned int *) block)[i] != ((unsigned int *) block)[0])
  440. break;
  441. if(i == 16) { // constant color
  442. int r = block[0], g = block[1], b = block[2];
  443. mask = 0xaaaaaaaa;
  444. max16 = (stb__OMatch5[r][0]<<11) | (stb__OMatch6[g][0]<<5) | stb__OMatch5[b][0];
  445. min16 = (stb__OMatch5[r][1]<<11) | (stb__OMatch6[g][1]<<5) | stb__OMatch5[b][1];
  446. } else {
  447. // first step: compute dithered version for PCA if desired
  448. if(dither)
  449. stb__DitherBlock(dblock,block);
  450. // second step: pca+map along principal axis
  451. stb__OptimizeColorsBlock(dither ? dblock : block,&max16,&min16);
  452. if (max16 != min16) {
  453. stb__EvalColors(color,max16,min16);
  454. mask = stb__MatchColorsBlock(block,color,dither);
  455. } else
  456. mask = 0;
  457. // third step: refine (multiple times if requested)
  458. for (i=0;i<refinecount;i++) {
  459. unsigned int lastmask = mask;
  460. if (stb__RefineBlock(dither ? dblock : block,&max16,&min16,mask)) {
  461. if (max16 != min16) {
  462. stb__EvalColors(color,max16,min16);
  463. mask = stb__MatchColorsBlock(block,color,dither);
  464. } else {
  465. mask = 0;
  466. break;
  467. }
  468. }
  469. if(mask == lastmask)
  470. break;
  471. }
  472. }
  473. // write the color block
  474. if(max16 < min16)
  475. {
  476. unsigned short t = min16;
  477. min16 = max16;
  478. max16 = t;
  479. mask ^= 0x55555555;
  480. }
  481. dest[0] = (unsigned char) (max16);
  482. dest[1] = (unsigned char) (max16 >> 8);
  483. dest[2] = (unsigned char) (min16);
  484. dest[3] = (unsigned char) (min16 >> 8);
  485. dest[4] = (unsigned char) (mask);
  486. dest[5] = (unsigned char) (mask >> 8);
  487. dest[6] = (unsigned char) (mask >> 16);
  488. dest[7] = (unsigned char) (mask >> 24);
  489. }
  490. // Alpha block compression (this is easy for a change)
  491. static void stb__CompressAlphaBlock(unsigned char *dest,unsigned char *src, int stride)
  492. {
  493. int i,dist,bias,dist4,dist2,bits,mask;
  494. // find min/max color
  495. int mn,mx;
  496. mn = mx = src[0];
  497. for (i=1;i<16;i++)
  498. {
  499. if (src[i*stride] < mn) mn = src[i*stride];
  500. else if (src[i*stride] > mx) mx = src[i*stride];
  501. }
  502. // encode them
  503. ((unsigned char *)dest)[0] = mx;
  504. ((unsigned char *)dest)[1] = mn;
  505. dest += 2;
  506. // determine bias and emit color indices
  507. // given the choice of mx/mn, these indices are optimal:
  508. // http://fgiesen.wordpress.com/2009/12/15/dxt5-alpha-block-index-determination/
  509. dist = mx-mn;
  510. dist4 = dist*4;
  511. dist2 = dist*2;
  512. bias = (dist < 8) ? (dist - 1) : (dist/2 + 2);
  513. bias -= mn * 7;
  514. bits = 0,mask=0;
  515. for (i=0;i<16;i++) {
  516. int a = src[i*stride]*7 + bias;
  517. int ind,t;
  518. // select index. this is a "linear scale" lerp factor between 0 (val=min) and 7 (val=max).
  519. t = (a >= dist4) ? -1 : 0; ind = t & 4; a -= dist4 & t;
  520. t = (a >= dist2) ? -1 : 0; ind += t & 2; a -= dist2 & t;
  521. ind += (a >= dist);
  522. // turn linear scale into DXT index (0/1 are extremal pts)
  523. ind = -ind & 7;
  524. ind ^= (2 > ind);
  525. // write index
  526. mask |= ind << bits;
  527. if((bits += 3) >= 8) {
  528. *dest++ = mask;
  529. mask >>= 8;
  530. bits -= 8;
  531. }
  532. }
  533. }
  534. static void stb__InitDXT()
  535. {
  536. int i;
  537. for(i=0;i<32;i++)
  538. stb__Expand5[i] = (i<<3)|(i>>2);
  539. for(i=0;i<64;i++)
  540. stb__Expand6[i] = (i<<2)|(i>>4);
  541. for(i=0;i<256+16;i++)
  542. {
  543. int v = i-8 < 0 ? 0 : i-8 > 255 ? 255 : i-8;
  544. stb__QuantRBTab[i] = stb__Expand5[stb__Mul8Bit(v,31)];
  545. stb__QuantGTab[i] = stb__Expand6[stb__Mul8Bit(v,63)];
  546. }
  547. stb__PrepareOptTable(&stb__OMatch5[0][0],stb__Expand5,32);
  548. stb__PrepareOptTable(&stb__OMatch6[0][0],stb__Expand6,64);
  549. }
  550. void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src, int alpha, int mode)
  551. {
  552. static int init=1;
  553. if (init) {
  554. stb__InitDXT();
  555. init=0;
  556. }
  557. if (alpha) {
  558. stb__CompressAlphaBlock(dest,(unsigned char*) src+3, 4);
  559. dest += 8;
  560. }
  561. stb__CompressColorBlock(dest,(unsigned char*) src,mode);
  562. }
  563. void stb_compress_bc4_block(unsigned char *dest, const unsigned char *src)
  564. {
  565. stb__CompressAlphaBlock(dest,(unsigned char*) src, 1);
  566. }
  567. void stb_compress_bc5_block(unsigned char *dest, const unsigned char *src)
  568. {
  569. stb__CompressAlphaBlock(dest,(unsigned char*) src,2);
  570. stb__CompressAlphaBlock(dest + 8,(unsigned char*) src+1,2);
  571. }
  572. #endif // STB_DXT_IMPLEMENTATION
  573. #endif // STB_INCLUDE_STB_DXT_H
  574. /*
  575. ------------------------------------------------------------------------------
  576. This software is available under 2 licenses -- choose whichever you prefer.
  577. ------------------------------------------------------------------------------
  578. ALTERNATIVE A - MIT License
  579. Copyright (c) 2017 Sean Barrett
  580. Permission is hereby granted, free of charge, to any person obtaining a copy of
  581. this software and associated documentation files (the "Software"), to deal in
  582. the Software without restriction, including without limitation the rights to
  583. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  584. of the Software, and to permit persons to whom the Software is furnished to do
  585. so, subject to the following conditions:
  586. The above copyright notice and this permission notice shall be included in all
  587. copies or substantial portions of the Software.
  588. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  589. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  590. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  591. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  592. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  593. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  594. SOFTWARE.
  595. ------------------------------------------------------------------------------
  596. ALTERNATIVE B - Public Domain (www.unlicense.org)
  597. This is free and unencumbered software released into the public domain.
  598. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  599. software, either in source code form or as a compiled binary, for any purpose,
  600. commercial or non-commercial, and by any means.
  601. In jurisdictions that recognize copyright laws, the author or authors of this
  602. software dedicate any and all copyright interest in the software to the public
  603. domain. We make this dedication for the benefit of the public at large and to
  604. the detriment of our heirs and successors. We intend this dedication to be an
  605. overt act of relinquishment in perpetuity of all present and future rights to
  606. this software under copyright law.
  607. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  608. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  609. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  610. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  611. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  612. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  613. ------------------------------------------------------------------------------
  614. */