Team Fortress 2 Source Code as on 22/4/2020
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.

728 lines
23 KiB

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