stb_dxt.h
This file
Path
stb_dxt.h
729 lines · 22.6 KiB · C
Last changed by
stb_dxt: documentation change for constant alphaSean Barrettbefore adoption5a2c890
History of this fileTake it
The walk stopped after 200 commits: lines older than c918a6b point at it.
fix stb_dxt.h; fix misspelled STB_DXT_IMPLEMENTATION in test jigSean Barrettbefore adoption06feddd
line 1why, one step back1// stb_dxt.h - v1.08b - DXT1/DXT5 compressor - public domaintweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 2–7why, one step back2// original by fabian "ryg" giesen - ported to C by stb3// use '#define STB_DXT_IMPLEMENTATION' before including to create the implementation4//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;stb_dxt: documentation change for constant alphaSean Barrettbefore adoption5a2c890
lines 8–10why, one step back8// Alpha channel is not stored if you specify alpha=0 (but you9// must supply some constant alpha in the alpha channel).10// You can turn on dithering and "high quality" using mode.tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 11–12why, one step back11//12// version history:stb_dxt: fix bug with constant color & varying alphaSean Barrettbefore adoptiond808adb
lines 13–14why, one step back13// v1.08 - (sbt) fix bug in dxt-with-alpha block14// v1.07 - (stb) bc4; allow not using libc; add STB_DXT_STATICfix to new bc5 pathSean Barrettbefore adoptionee19531
line 15why, one step back15// v1.06 - (stb) fix to known-broken 1.05tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 16–24why, one step back16// v1.05 - (stb) support bc5/3dc (Arvids Kokins), use extern "C" in C++ (Pavel Krajcevski)17// v1.04 - (ryg) default to no rounding bias for lerped colors (as per S3TC/DX10 spec);18// single color match fix (allow for inexact color interpolation);19// optimal DXT5 index finder; "high quality" mode that runs multiple refinement steps.20// v1.03 - (stb) endianness support21// v1.02 - (stb) fix alpha encoding bug22// v1.01 - (stb) fix bug converting to RGB that messed up quality, thanks ryg & cbloom23// v1.00 - (stb) first release24//Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 25–28why, one step back25// contributors: 26// Kevin Schmidt (#defines for "freestanding" compilation)27// github:ppiastucki (BC4 support)28// tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 29–39why, one step back29// LICENSE30//31// See end of file for license information.3233#ifndef STB_INCLUDE_STB_DXT_H34#define STB_INCLUDE_STB_DXT_H3536#ifdef __cplusplus37extern "C" {38#endif39Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 40–44why, one step back40#ifdef STB_DXT_STATIC41#define STBDDEF static42#else43#define STBDDEF extern44#endiftweak previous PR mergeSean Barrettbefore adoptionc918a6b
line 45why, one step back45fix stb_dxt.h; fix misspelled STB_DXT_IMPLEMENTATION in test jigSean Barrettbefore adoption06feddd
lines 46–50why, one step back46// compression mode (bitflags)47#define STB_DXT_NORMAL 048#define STB_DXT_DITHER 1 // use dithering. dubious win. never use for normal maps and the like!49#define STB_DXT_HIGHQUAL 2 // high quality mode, does two refinement steps instead of 1. ~30-40% slower.50Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 51–54why, one step back51STBDDEF void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src_rgba_four_bytes_per_pixel, int alpha, int mode);52STBDDEF void stb_compress_bc4_block(unsigned char *dest, const unsigned char *src_r_one_byte_per_pixel);53STBDDEF void stb_compress_bc5_block(unsigned char *dest, const unsigned char *src_rg_two_byte_per_pixel);54fix stb_dxt.h; fix misspelled STB_DXT_IMPLEMENTATION in test jigSean Barrettbefore adoption06feddd
lines 55–56why, one step back55#define STB_COMPRESS_DXT_BLOCK56tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 57–59why, one step back57#ifdef __cplusplus58}59#endiffix stb_dxt.h; fix misspelled STB_DXT_IMPLEMENTATION in test jigSean Barrettbefore adoption06feddd
line 60why, one step back60#endif // STB_INCLUDE_STB_DXT_Htweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 61–78why, one step back6162#ifdef STB_DXT_IMPLEMENTATION6364// configuration options for DXT encoder. set them in the project/makefile or just define65// them at the top.6667// STB_DXT_USE_ROUNDING_BIAS68// use a rounding bias during color interpolation. this is closer to what "ideal"69// interpolation would do but doesn't match the S3TC/DX10 spec. old versions (pre-1.03)70// implicitly had this turned on. 71//72// in case you're targeting a specific type of hardware (e.g. console programmers):73// NVidia and Intel GPUs (as of 2010) as well as DX9 ref use DXT decoders that are closer74// to STB_DXT_USE_ROUNDING_BIAS. AMD/ATI, S3 and DX10 ref are closer to rounding with no bias.75// you also see "(a*5 + b*3) / 8" on some old GPU designs.76// #define STB_DXT_USE_ROUNDING_BIAS7778#include <stdlib.h>Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 79–80why, one step back7980#if !defined(STBD_ABS) || !defined(STBI_FABS)tweak previous PR mergeSean Barrettbefore adoptionc918a6b
line 81why, one step back81#include <math.h>Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 82why, one step back82#endiftweak previous PR mergeSean Barrettbefore adoptionc918a6b
line 83why, one step back83Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 84–96why, one step back84#ifndef STBD_ABS85#define STBD_ABS(i) abs(i)86#endif8788#ifndef STBD_FABS89#define STBD_FABS(x) fabs(x)90#endif9192#ifndef STBD_MEMSET93#include <string.h>94#define STBD_MEMSET memset95#endif96tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 97–159why, one step back97static unsigned char stb__Expand5[32];98static unsigned char stb__Expand6[64];99static unsigned char stb__OMatch5[256][2];100static unsigned char stb__OMatch6[256][2];101static unsigned char stb__QuantRBTab[256+16];102static unsigned char stb__QuantGTab[256+16];103104static int stb__Mul8Bit(int a, int b)105{106 int t = a*b + 128;107 return (t + (t >> 8)) >> 8;108}109110static void stb__From16Bit(unsigned char *out, unsigned short v)111{112 int rv = (v & 0xf800) >> 11;113 int gv = (v & 0x07e0) >> 5;114 int bv = (v & 0x001f) >> 0;115116 out[0] = stb__Expand5[rv];117 out[1] = stb__Expand6[gv];118 out[2] = stb__Expand5[bv];119 out[3] = 0;120}121122static unsigned short stb__As16Bit(int r, int g, int b)123{124 return (stb__Mul8Bit(r,31) << 11) + (stb__Mul8Bit(g,63) << 5) + stb__Mul8Bit(b,31);125}126127// linear interpolation at 1/3 point between a and b, using desired rounding type128static int stb__Lerp13(int a, int b)129{130#ifdef STB_DXT_USE_ROUNDING_BIAS131 // with rounding bias132 return a + stb__Mul8Bit(b-a, 0x55);133#else134 // without rounding bias135 // replace "/ 3" by "* 0xaaab) >> 17" if your compiler sucks or you really need every ounce of speed.136 return (2*a + b) / 3;137#endif138}139140// lerp RGB color141static void stb__Lerp13RGB(unsigned char *out, unsigned char *p1, unsigned char *p2)142{143 out[0] = stb__Lerp13(p1[0], p2[0]);144 out[1] = stb__Lerp13(p1[1], p2[1]);145 out[2] = stb__Lerp13(p1[2], p2[2]);146}147148/****************************************************************************/149150// compute table to reproduce constant colors as accurately as possible151static void stb__PrepareOptTable(unsigned char *Table,const unsigned char *expand,int size)152{153 int i,mn,mx;154 for (i=0;i<256;i++) {155 int bestErr = 256;156 for (mn=0;mn<size;mn++) {157 for (mx=0;mx<size;mx++) {158 int mine = expand[mn];159 int maxe = expand[mx];Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 160why, one step back160 int err = STBD_ABS(stb__Lerp13(maxe, mine) - i);tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 161–165why, one step back161 162 // DX10 spec says that interpolation must be within 3% of "correct" result,163 // add this as error term. (normally we'd expect a random distribution of164 // +-1.5% error, but nowhere in the spec does it say that the error has to be165 // unbiased - better safe than sorry).Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 166why, one step back166 err += STBD_ABS(maxe - mine) * 3 / 100;tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 167–193why, one step back167 168 if(err < bestErr)169 { 170 Table[i*2+0] = mx;171 Table[i*2+1] = mn;172 bestErr = err;173 }174 }175 }176 }177}178179static void stb__EvalColors(unsigned char *color,unsigned short c0,unsigned short c1)180{181 stb__From16Bit(color+ 0, c0);182 stb__From16Bit(color+ 4, c1);183 stb__Lerp13RGB(color+ 8, color+0, color+4);184 stb__Lerp13RGB(color+12, color+4, color+0);185}186187// Block dithering function. Simply dithers a block to 565 RGB.188// (Floyd-Steinberg)189static void stb__DitherBlock(unsigned char *dest, unsigned char *block)190{191 int err[8],*ep1 = err,*ep2 = err+4, *et;192 int ch,y;193Merge branch 'misc-typos' of https://github.com/luzpaz/stb into workingSean Barrettbefore adoption3005bcf
line 194why, one step back194 // process channels separatelytweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 195–197why, one step back195 for (ch=0; ch<3; ++ch) {196 unsigned char *bp = block+ch, *dp = dest+ch;197 unsigned char *quant = (ch == 1) ? stb__QuantGTab+8 : stb__QuantRBTab+8;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 198why, one step back198 STBD_MEMSET(err, 0, sizeof(err));tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 199–381why, one step back199 for(y=0; y<4; ++y) {200 dp[ 0] = quant[bp[ 0] + ((3*ep2[1] + 5*ep2[0]) >> 4)];201 ep1[0] = bp[ 0] - dp[ 0];202 dp[ 4] = quant[bp[ 4] + ((7*ep1[0] + 3*ep2[2] + 5*ep2[1] + ep2[0]) >> 4)];203 ep1[1] = bp[ 4] - dp[ 4];204 dp[ 8] = quant[bp[ 8] + ((7*ep1[1] + 3*ep2[3] + 5*ep2[2] + ep2[1]) >> 4)];205 ep1[2] = bp[ 8] - dp[ 8];206 dp[12] = quant[bp[12] + ((7*ep1[2] + 5*ep2[3] + ep2[2]) >> 4)];207 ep1[3] = bp[12] - dp[12];208 bp += 16;209 dp += 16;210 et = ep1, ep1 = ep2, ep2 = et; // swap211 }212 }213}214215// The color matching function216static unsigned int stb__MatchColorsBlock(unsigned char *block, unsigned char *color,int dither)217{218 unsigned int mask = 0;219 int dirr = color[0*4+0] - color[1*4+0];220 int dirg = color[0*4+1] - color[1*4+1];221 int dirb = color[0*4+2] - color[1*4+2];222 int dots[16];223 int stops[4];224 int i;225 int c0Point, halfPoint, c3Point;226227 for(i=0;i<16;i++)228 dots[i] = block[i*4+0]*dirr + block[i*4+1]*dirg + block[i*4+2]*dirb;229230 for(i=0;i<4;i++)231 stops[i] = color[i*4+0]*dirr + color[i*4+1]*dirg + color[i*4+2]*dirb;232233 // think of the colors as arranged on a line; project point onto that line, then choose234 // next color out of available ones. we compute the crossover points for "best color in top235 // half"/"best in bottom half" and then the same inside that subinterval.236 //237 // relying on this 1d approximation isn't always optimal in terms of euclidean distance,238 // but it's very close and a lot faster.239 // http://cbloomrants.blogspot.com/2008/12/12-08-08-dxtc-summary.html240 241 c0Point = (stops[1] + stops[3]) >> 1;242 halfPoint = (stops[3] + stops[2]) >> 1;243 c3Point = (stops[2] + stops[0]) >> 1;244245 if(!dither) {246 // the version without dithering is straightforward247 for (i=15;i>=0;i--) {248 int dot = dots[i];249 mask <<= 2;250251 if(dot < halfPoint)252 mask |= (dot < c0Point) ? 1 : 3;253 else254 mask |= (dot < c3Point) ? 2 : 0;255 }256 } else {257 // with floyd-steinberg dithering258 int err[8],*ep1 = err,*ep2 = err+4;259 int *dp = dots, y;260261 c0Point <<= 4;262 halfPoint <<= 4;263 c3Point <<= 4;264 for(i=0;i<8;i++)265 err[i] = 0;266267 for(y=0;y<4;y++)268 {269 int dot,lmask,step;270271 dot = (dp[0] << 4) + (3*ep2[1] + 5*ep2[0]);272 if(dot < halfPoint)273 step = (dot < c0Point) ? 1 : 3;274 else275 step = (dot < c3Point) ? 2 : 0;276 ep1[0] = dp[0] - stops[step];277 lmask = step;278279 dot = (dp[1] << 4) + (7*ep1[0] + 3*ep2[2] + 5*ep2[1] + ep2[0]);280 if(dot < halfPoint)281 step = (dot < c0Point) ? 1 : 3;282 else283 step = (dot < c3Point) ? 2 : 0;284 ep1[1] = dp[1] - stops[step];285 lmask |= step<<2;286287 dot = (dp[2] << 4) + (7*ep1[1] + 3*ep2[3] + 5*ep2[2] + ep2[1]);288 if(dot < halfPoint)289 step = (dot < c0Point) ? 1 : 3;290 else291 step = (dot < c3Point) ? 2 : 0;292 ep1[2] = dp[2] - stops[step];293 lmask |= step<<4;294295 dot = (dp[3] << 4) + (7*ep1[2] + 5*ep2[3] + ep2[2]);296 if(dot < halfPoint)297 step = (dot < c0Point) ? 1 : 3;298 else299 step = (dot < c3Point) ? 2 : 0;300 ep1[3] = dp[3] - stops[step];301 lmask |= step<<6;302303 dp += 4;304 mask |= lmask << (y*8);305 { int *et = ep1; ep1 = ep2; ep2 = et; } // swap306 }307 }308309 return mask;310}311312// The color optimization function. (Clever code, part 1)313static void stb__OptimizeColorsBlock(unsigned char *block, unsigned short *pmax16, unsigned short *pmin16)314{315 int mind = 0x7fffffff,maxd = -0x7fffffff;316 unsigned char *minp, *maxp;317 double magn;318 int v_r,v_g,v_b;319 static const int nIterPower = 4;320 float covf[6],vfr,vfg,vfb;321322 // determine color distribution323 int cov[6];324 int mu[3],min[3],max[3];325 int ch,i,iter;326327 for(ch=0;ch<3;ch++)328 {329 const unsigned char *bp = ((const unsigned char *) block) + ch;330 int muv,minv,maxv;331332 muv = minv = maxv = bp[0];333 for(i=4;i<64;i+=4)334 {335 muv += bp[i];336 if (bp[i] < minv) minv = bp[i];337 else if (bp[i] > maxv) maxv = bp[i];338 }339340 mu[ch] = (muv + 8) >> 4;341 min[ch] = minv;342 max[ch] = maxv;343 }344345 // determine covariance matrix346 for (i=0;i<6;i++)347 cov[i] = 0;348349 for (i=0;i<16;i++)350 {351 int r = block[i*4+0] - mu[0];352 int g = block[i*4+1] - mu[1];353 int b = block[i*4+2] - mu[2];354355 cov[0] += r*r;356 cov[1] += r*g;357 cov[2] += r*b;358 cov[3] += g*g;359 cov[4] += g*b;360 cov[5] += b*b;361 }362363 // convert covariance matrix to float, find principal axis via power iter364 for(i=0;i<6;i++)365 covf[i] = cov[i] / 255.0f;366367 vfr = (float) (max[0] - min[0]);368 vfg = (float) (max[1] - min[1]);369 vfb = (float) (max[2] - min[2]);370371 for(iter=0;iter<nIterPower;iter++)372 {373 float r = vfr*covf[0] + vfg*covf[1] + vfb*covf[2];374 float g = vfr*covf[1] + vfg*covf[3] + vfb*covf[4];375 float b = vfr*covf[2] + vfg*covf[4] + vfb*covf[5];376377 vfr = r;378 vfg = g;379 vfb = b;380 }381Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 382–384why, one step back382 magn = STBD_FABS(vfr);383 if (STBD_FABS(vfg) > magn) magn = STBD_FABS(vfg);384 if (STBD_FABS(vfb) > magn) magn = STBD_FABS(vfb);tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 385–586why, one step back385386 if(magn < 4.0f) { // too small, default to luminance387 v_r = 299; // JPEG YCbCr luma coefs, scaled by 1000.388 v_g = 587;389 v_b = 114;390 } else {391 magn = 512.0 / magn;392 v_r = (int) (vfr * magn);393 v_g = (int) (vfg * magn);394 v_b = (int) (vfb * magn);395 }396397 // Pick colors at extreme points398 for(i=0;i<16;i++)399 {400 int dot = block[i*4+0]*v_r + block[i*4+1]*v_g + block[i*4+2]*v_b;401402 if (dot < mind) {403 mind = dot;404 minp = block+i*4;405 }406407 if (dot > maxd) {408 maxd = dot;409 maxp = block+i*4;410 }411 }412413 *pmax16 = stb__As16Bit(maxp[0],maxp[1],maxp[2]);414 *pmin16 = stb__As16Bit(minp[0],minp[1],minp[2]);415}416417static int stb__sclamp(float y, int p0, int p1)418{419 int x = (int) y;420 if (x < p0) return p0;421 if (x > p1) return p1;422 return x;423}424425// The refinement function. (Clever code, part 2)426// Tries to optimize colors to suit block contents better.427// (By solving a least squares system via normal equations+Cramer's rule)428static int stb__RefineBlock(unsigned char *block, unsigned short *pmax16, unsigned short *pmin16, unsigned int mask)429{430 static const int w1Tab[4] = { 3,0,2,1 };431 static const int prods[4] = { 0x090000,0x000900,0x040102,0x010402 };432 // ^some magic to save a lot of multiplies in the accumulating loop...433 // (precomputed products of weights for least squares system, accumulated inside one 32-bit register)434435 float frb,fg;436 unsigned short oldMin, oldMax, min16, max16;437 int i, akku = 0, xx,xy,yy;438 int At1_r,At1_g,At1_b;439 int At2_r,At2_g,At2_b;440 unsigned int cm = mask;441442 oldMin = *pmin16;443 oldMax = *pmax16;444445 if((mask ^ (mask<<2)) < 4) // all pixels have the same index?446 {447 // yes, linear system would be singular; solve using optimal448 // single-color match on average color449 int r = 8, g = 8, b = 8;450 for (i=0;i<16;++i) {451 r += block[i*4+0];452 g += block[i*4+1];453 b += block[i*4+2];454 }455456 r >>= 4; g >>= 4; b >>= 4;457458 max16 = (stb__OMatch5[r][0]<<11) | (stb__OMatch6[g][0]<<5) | stb__OMatch5[b][0];459 min16 = (stb__OMatch5[r][1]<<11) | (stb__OMatch6[g][1]<<5) | stb__OMatch5[b][1];460 } else {461 At1_r = At1_g = At1_b = 0;462 At2_r = At2_g = At2_b = 0;463 for (i=0;i<16;++i,cm>>=2) {464 int step = cm&3;465 int w1 = w1Tab[step];466 int r = block[i*4+0];467 int g = block[i*4+1];468 int b = block[i*4+2];469470 akku += prods[step];471 At1_r += w1*r;472 At1_g += w1*g;473 At1_b += w1*b;474 At2_r += r;475 At2_g += g;476 At2_b += b;477 }478479 At2_r = 3*At2_r - At1_r;480 At2_g = 3*At2_g - At1_g;481 At2_b = 3*At2_b - At1_b;482483 // extract solutions and decide solvability484 xx = akku >> 16;485 yy = (akku >> 8) & 0xff;486 xy = (akku >> 0) & 0xff;487488 frb = 3.0f * 31.0f / 255.0f / (xx*yy - xy*xy);489 fg = frb * 63.0f / 31.0f;490491 // solve.492 max16 = stb__sclamp((At1_r*yy - At2_r*xy)*frb+0.5f,0,31) << 11;493 max16 |= stb__sclamp((At1_g*yy - At2_g*xy)*fg +0.5f,0,63) << 5;494 max16 |= stb__sclamp((At1_b*yy - At2_b*xy)*frb+0.5f,0,31) << 0;495496 min16 = stb__sclamp((At2_r*xx - At1_r*xy)*frb+0.5f,0,31) << 11;497 min16 |= stb__sclamp((At2_g*xx - At1_g*xy)*fg +0.5f,0,63) << 5;498 min16 |= stb__sclamp((At2_b*xx - At1_b*xy)*frb+0.5f,0,31) << 0;499 }500501 *pmin16 = min16;502 *pmax16 = max16;503 return oldMin != min16 || oldMax != max16;504}505506// Color block compression507static void stb__CompressColorBlock(unsigned char *dest, unsigned char *block, int mode)508{509 unsigned int mask;510 int i;511 int dither;512 int refinecount;513 unsigned short max16, min16;514 unsigned char dblock[16*4],color[4*4];515 516 dither = mode & STB_DXT_DITHER;517 refinecount = (mode & STB_DXT_HIGHQUAL) ? 2 : 1;518519 // check if block is constant520 for (i=1;i<16;i++)521 if (((unsigned int *) block)[i] != ((unsigned int *) block)[0])522 break;523524 if(i == 16) { // constant color525 int r = block[0], g = block[1], b = block[2];526 mask = 0xaaaaaaaa;527 max16 = (stb__OMatch5[r][0]<<11) | (stb__OMatch6[g][0]<<5) | stb__OMatch5[b][0];528 min16 = (stb__OMatch5[r][1]<<11) | (stb__OMatch6[g][1]<<5) | stb__OMatch5[b][1];529 } else {530 // first step: compute dithered version for PCA if desired531 if(dither)532 stb__DitherBlock(dblock,block);533534 // second step: pca+map along principal axis535 stb__OptimizeColorsBlock(dither ? dblock : block,&max16,&min16);536 if (max16 != min16) {537 stb__EvalColors(color,max16,min16);538 mask = stb__MatchColorsBlock(block,color,dither);539 } else540 mask = 0;541542 // third step: refine (multiple times if requested)543 for (i=0;i<refinecount;i++) {544 unsigned int lastmask = mask;545 546 if (stb__RefineBlock(dither ? dblock : block,&max16,&min16,mask)) {547 if (max16 != min16) {548 stb__EvalColors(color,max16,min16);549 mask = stb__MatchColorsBlock(block,color,dither);550 } else {551 mask = 0;552 break;553 }554 }555 556 if(mask == lastmask)557 break;558 }559 }560561 // write the color block562 if(max16 < min16)563 {564 unsigned short t = min16;565 min16 = max16;566 max16 = t;567 mask ^= 0x55555555;568 }569570 dest[0] = (unsigned char) (max16);571 dest[1] = (unsigned char) (max16 >> 8);572 dest[2] = (unsigned char) (min16);573 dest[3] = (unsigned char) (min16 >> 8);574 dest[4] = (unsigned char) (mask);575 dest[5] = (unsigned char) (mask >> 8);576 dest[6] = (unsigned char) (mask >> 16);577 dest[7] = (unsigned char) (mask >> 24);578}579580// Alpha block compression (this is easy for a change)581static void stb__CompressAlphaBlock(unsigned char *dest,unsigned char *src, int stride)582{583 int i,dist,bias,dist4,dist2,bits,mask;584585 // find min/max color586 int mn,mx;fix to new bc5 pathSean Barrettbefore adoptionee19531
line 587why, one step back587 mn = mx = src[0];tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 588–590why, one step back588589 for (i=1;i<16;i++)590 {fix to new bc5 pathSean Barrettbefore adoptionee19531
lines 591–592why, one step back591 if (src[i*stride] < mn) mn = src[i*stride];592 else if (src[i*stride] > mx) mx = src[i*stride];tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 593–654why, one step back593 }594595 // encode them596 ((unsigned char *)dest)[0] = mx;597 ((unsigned char *)dest)[1] = mn;598 dest += 2;599600 // determine bias and emit color indices601 // 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 610 for (i=0;i<16;i++) {611 int a = src[i*stride]*7 + bias;612 int ind,t;613614 // select index. this is a "linear scale" lerp factor between 0 (val=min) and 7 (val=max).615 t = (a >= dist4) ? -1 : 0; ind = t & 4; a -= dist4 & t;616 t = (a >= dist2) ? -1 : 0; ind += t & 2; a -= dist2 & t;617 ind += (a >= dist);618 619 // turn linear scale into DXT index (0/1 are extremal pts)620 ind = -ind & 7;621 ind ^= (2 > ind);622623 // write index624 mask |= ind << bits;625 if((bits += 3) >= 8) {626 *dest++ = mask;627 mask >>= 8;628 bits -= 8;629 }630 }631}632633static void stb__InitDXT()634{635 int i;636 for(i=0;i<32;i++)637 stb__Expand5[i] = (i<<3)|(i>>2);638639 for(i=0;i<64;i++)640 stb__Expand6[i] = (i<<2)|(i>>4);641642 for(i=0;i<256+16;i++)643 {644 int v = i-8 < 0 ? 0 : i-8 > 255 ? 255 : i-8;645 stb__QuantRBTab[i] = stb__Expand5[stb__Mul8Bit(v,31)];646 stb__QuantGTab[i] = stb__Expand6[stb__Mul8Bit(v,63)];647 }648649 stb__PrepareOptTable(&stb__OMatch5[0][0],stb__Expand5,32);650 stb__PrepareOptTable(&stb__OMatch6[0][0],stb__Expand6,64);651}652653void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src, int alpha, int mode)654{fix stb_dxt.h; fix misspelled STB_DXT_IMPLEMENTATION in test jigSean Barrettbefore adoption06feddd
line 655why, one step back655 unsigned char data[16][4];tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 656–662why, one step back656 static int init=1;657 if (init) {658 stb__InitDXT();659 init=0;660 }661662 if (alpha) {stb_dxt: fix bug with constant color & varying alphaSean Barrettbefore adoptiond808adb
line 663why, one step back663 int i;tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 664–665why, one step back664 stb__CompressAlphaBlock(dest,(unsigned char*) src+3, 4);665 dest += 8;stb_dxt: fix bug with constant color & varying alphaSean Barrettbefore adoptiond808adb
lines 666–671why, one step back666 // make a new copy of the data in which alpha is opaque,667 // because code uses a fast test for color constancy668 memcpy(data, src, 4*16);669 for (i=0; i < 16; ++i)670 data[i][3] = 255;671 src = &data[0][0];tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 672–674why, one step back672 }673674 stb__CompressColorBlock(dest,(unsigned char*) src,mode);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 675–679why, one step back675}676677void stb_compress_bc4_block(unsigned char *dest, const unsigned char *src)678{679 stb__CompressAlphaBlock(dest,(unsigned char*) src, 1);tweak previous PR mergeSean Barrettbefore adoptionc918a6b
lines 680–729why, one step back680}681682void stb_compress_bc5_block(unsigned char *dest, const unsigned char *src)683{684 stb__CompressAlphaBlock(dest,(unsigned char*) src,2);685 stb__CompressAlphaBlock(dest + 8,(unsigned char*) src+1,2);686}687#endif // STB_DXT_IMPLEMENTATION688689/*690------------------------------------------------------------------------------691This software is available under 2 licenses -- choose whichever you prefer.692------------------------------------------------------------------------------693ALTERNATIVE A - MIT License694Copyright (c) 2017 Sean Barrett695Permission is hereby granted, free of charge, to any person obtaining a copy of 696this software and associated documentation files (the "Software"), to deal in 697the Software without restriction, including without limitation the rights to 698use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 699of the Software, and to permit persons to whom the Software is furnished to do 700so, subject to the following conditions:701The above copyright notice and this permission notice shall be included in all 702copies or substantial portions of the Software.703THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 704IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 705FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 706AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 707LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 708OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 709SOFTWARE.710------------------------------------------------------------------------------711ALTERNATIVE B - Public Domain (www.unlicense.org)712This is free and unencumbered software released into the public domain.713Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 714software, either in source code form or as a compiled binary, for any purpose, 715commercial or non-commercial, and by any means.716In jurisdictions that recognize copyright laws, the author or authors of this 717software dedicate any and all copyright interest in the software to the public 718domain. We make this dedication for the benefit of the public at large and to 719the detriment of our heirs and successors. We intend this dedication to be an 720overt act of relinquishment in perpetuity of all present and future rights to 721this software under copyright law.722THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 723IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 724FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 725AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 726ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 727WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.728------------------------------------------------------------------------------729*/