Merge branch 'perfect-endpoint-quantization' of https://github.com/castano/stb into test
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parents cae97bd, 254e1c9 · Browse files · 1 file·+31 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+31 added−16 removed
- other:+31 added−16 removed
Provenance
- Commit
cae8e85- Tree
b6fc0a2- Author
Sean Barrett <seanb@radgametools.com>- Committer
Sean Barrett <seanb@radgametools.com>- Parent 1
cae97bd- Parent 2
254e1c9
1 file changed+31 added−16 removed
modified:stb_dxt.h+31 added-16 removed
413 unmodified lines414 *pmax16 = stb__As16Bit(maxp[0],maxp[1],maxp[2]);415 *pmin16 = stb__As16Bit(minp[0],minp[1],minp[2]);416 }417 418-static int stb__sclamp(float y, int p0, int p1)419-{420- int x = (int) y;421- if (x < p0) return p0;422- if (x > p1) return p1;423- return x;418+static const float midpoints5[32] = {419+ 0.015686f, 0.047059f, 0.078431f, 0.111765f, 0.145098f, 0.176471f, 0.207843f, 0.241176f, 0.274510f, 0.305882f, 0.337255f, 0.370588f, 0.403922f, 0.435294f, 0.466667f, 0.5f,420+ 0.533333f, 0.564706f, 0.596078f, 0.629412f, 0.662745f, 0.694118f, 0.725490f, 0.758824f, 0.792157f, 0.823529f, 0.854902f, 0.888235f, 0.921569f, 0.952941f, 0.984314f, 1.0f421+};422+423+static const float midpoints6[64] = {424+ 0.007843f, 0.023529f, 0.039216f, 0.054902f, 0.070588f, 0.086275f, 0.101961f, 0.117647f, 0.133333f, 0.149020f, 0.164706f, 0.180392f, 0.196078f, 0.211765f, 0.227451f, 0.245098f,425+ 0.262745f, 0.278431f, 0.294118f, 0.309804f, 0.325490f, 0.341176f, 0.356863f, 0.372549f, 0.388235f, 0.403922f, 0.419608f, 0.435294f, 0.450980f, 0.466667f, 0.482353f, 0.500000f,426+ 0.517647f, 0.533333f, 0.549020f, 0.564706f, 0.580392f, 0.596078f, 0.611765f, 0.627451f, 0.643137f, 0.658824f, 0.674510f, 0.690196f, 0.705882f, 0.721569f, 0.737255f, 0.754902f,427+ 0.772549f, 0.788235f, 0.803922f, 0.819608f, 0.835294f, 0.850980f, 0.866667f, 0.882353f, 0.898039f, 0.913725f, 0.929412f, 0.945098f, 0.960784f, 0.976471f, 0.992157f, 1.0f428+};429+430+static unsigned short stb__Quantize5(float x) {431+ x = x < 0 ? 0 : x > 1 ? 1 : x; // saturate432+ unsigned short q = (unsigned short)(x * 31);433+ q += (x > midpoints5[q]);434+ return q;435 }436+static unsigned short stb__Quantize6(float x) {437+ x = x < 0 ? 0 : x > 1 ? 1 : x; // saturate438+ unsigned short q = (unsigned short)(x * 63);439+ q += (x > midpoints6[q]);440+ return q;441+}442 443 // The refinement function. (Clever code, part 2)444 // Tries to optimize colors to suit block contents better.445 // (By solving a least squares system via normal equations+Cramer's rule)3 unmodified lines449 static const int prods[4] = { 0x090000,0x000900,0x040102,0x010402 };450 // ^some magic to save a lot of multiplies in the accumulating loop...451 // (precomputed products of weights for least squares system, accumulated inside one 32-bit register)452 436- float frb,fg;453+ float f;454 unsigned short oldMin, oldMax, min16, max16;455 int i, akku = 0, xx,xy,yy;456 int At1_r,At1_g,At1_b;457 int At2_r,At2_g,At2_b;44 unmodified lines502 xx = akku >> 16;503 yy = (akku >> 8) & 0xff;504 xy = (akku >> 0) & 0xff;505 489- frb = 3.0f * 31.0f / 255.0f / (xx*yy - xy*xy);490- fg = frb * 63.0f / 31.0f;506+ f = 3.0f / 255.0f / (xx*yy - xy*xy);507 492- // solve.493- max16 = (unsigned short)(stb__sclamp((At1_r*yy - At2_r*xy)*frb+0.5f,0,31) << 11);494- max16 |= (unsigned short)(stb__sclamp((At1_g*yy - At2_g*xy)*fg +0.5f,0,63) << 5);495- max16 |= (unsigned short)(stb__sclamp((At1_b*yy - At2_b*xy)*frb+0.5f,0,31) << 0);508+ max16 = stb__Quantize5((At1_r*yy - At2_r * xy) * f) << 11;509+ max16 |= stb__Quantize6((At1_g*yy - At2_g * xy) * f) << 5;510+ max16 |= stb__Quantize5((At1_b*yy - At2_b * xy) * f) << 0;511 497- min16 = (unsigned short)(stb__sclamp((At2_r*xx - At1_r*xy)*frb+0.5f,0,31) << 11);498- min16 |= (unsigned short)(stb__sclamp((At2_g*xx - At1_g*xy)*fg +0.5f,0,63) << 5);499- min16 |= (unsigned short)(stb__sclamp((At2_b*xx - At1_b*xy)*frb+0.5f,0,31) << 0);512+ min16 = stb__Quantize5((At2_r*xx - At1_r * xy) * f) << 11;513+ min16 |= stb__Quantize6((At2_g*xx - At1_g * xy) * f) << 5;514+ min16 |= stb__Quantize5((At2_b*xx - At1_b * xy) * f) << 0;515 }516 517 *pmin16 = min16;518 *pmax16 = max16;227 unmodified lines