|
|
@ -1,4 +1,4 @@ |
|
|
|
// stb_dxt.h - v1.10 - DXT1/DXT5 compressor - public domain
|
|
|
|
// stb_dxt.h - v1.11 - DXT1/DXT5 compressor - public domain
|
|
|
|
// original by fabian "ryg" giesen - ported to C by stb
|
|
|
|
// original by fabian "ryg" giesen - ported to C by stb
|
|
|
|
// use '#define STB_DXT_IMPLEMENTATION' before including to create the implementation
|
|
|
|
// use '#define STB_DXT_IMPLEMENTATION' before including to create the implementation
|
|
|
|
//
|
|
|
|
//
|
|
|
@ -10,6 +10,7 @@ |
|
|
|
// You can turn on dithering and "high quality" using mode.
|
|
|
|
// You can turn on dithering and "high quality" using mode.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// version history:
|
|
|
|
// version history:
|
|
|
|
|
|
|
|
// v1.11 - (ryg) avoid racy global init, better single-color tables, remove dither
|
|
|
|
// v1.10 - (i.c) various small quality improvements
|
|
|
|
// v1.10 - (i.c) various small quality improvements
|
|
|
|
// v1.09 - (stb) update documentation re: surprising alpha channel requirement
|
|
|
|
// v1.09 - (stb) update documentation re: surprising alpha channel requirement
|
|
|
|
// v1.08 - (stb) fix bug in dxt-with-alpha block
|
|
|
|
// v1.08 - (stb) fix bug in dxt-with-alpha block
|
|
|
@ -29,6 +30,7 @@ |
|
|
|
// Kevin Schmidt (#defines for "freestanding" compilation)
|
|
|
|
// Kevin Schmidt (#defines for "freestanding" compilation)
|
|
|
|
// github:ppiastucki (BC4 support)
|
|
|
|
// github:ppiastucki (BC4 support)
|
|
|
|
// Ignacio Castano - improve DXT endpoint quantization
|
|
|
|
// Ignacio Castano - improve DXT endpoint quantization
|
|
|
|
|
|
|
|
// Alan Hickman - static table initialization
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// LICENSE
|
|
|
|
// LICENSE
|
|
|
|
//
|
|
|
|
//
|
|
|
@ -49,7 +51,7 @@ extern "C" { |
|
|
|
|
|
|
|
|
|
|
|
// compression mode (bitflags)
|
|
|
|
// compression mode (bitflags)
|
|
|
|
#define STB_DXT_NORMAL 0 |
|
|
|
#define STB_DXT_NORMAL 0 |
|
|
|
#define STB_DXT_DITHER 1 // use dithering. dubious win. never use for normal maps and the like!
|
|
|
|
#define STB_DXT_DITHER 1 // use dithering. was always dubious, now deprecated. does nothing!
|
|
|
|
#define STB_DXT_HIGHQUAL 2 // high quality mode, does two refinement steps instead of 1. ~30-40% slower.
|
|
|
|
#define STB_DXT_HIGHQUAL 2 // high quality mode, does two refinement steps instead of 1. ~30-40% slower.
|
|
|
|
|
|
|
|
|
|
|
|
STBDDEF void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src_rgba_four_bytes_per_pixel, int alpha, int mode); |
|
|
|
STBDDEF void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src_rgba_four_bytes_per_pixel, int alpha, int mode); |
|
|
@ -81,29 +83,82 @@ STBDDEF void stb_compress_bc5_block(unsigned char *dest, const unsigned char *sr |
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdlib.h> |
|
|
|
|
|
|
|
|
|
|
|
#if !defined(STBD_ABS) || !defined(STBI_FABS) |
|
|
|
#if !defined(STBD_FABS) |
|
|
|
#include <math.h> |
|
|
|
#include <math.h> |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifndef STBD_ABS |
|
|
|
|
|
|
|
#define STBD_ABS(i) abs(i) |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef STBD_FABS |
|
|
|
#ifndef STBD_FABS |
|
|
|
#define STBD_FABS(x) fabs(x) |
|
|
|
#define STBD_FABS(x) fabs(x) |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifndef STBD_MEMSET |
|
|
|
static const unsigned char stb__OMatch5[256][2] = { |
|
|
|
#include <string.h> |
|
|
|
{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 1, 1 }, { 1, 1 }, { 1, 1 }, |
|
|
|
#define STBD_MEMSET memset |
|
|
|
{ 1, 1 }, { 1, 1 }, { 1, 1 }, { 1, 1 }, { 1, 1 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, |
|
|
|
#endif |
|
|
|
{ 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, |
|
|
|
|
|
|
|
{ 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, |
|
|
|
static unsigned char stb__Expand5[32]; |
|
|
|
{ 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 5, 5 }, { 5, 5 }, |
|
|
|
static unsigned char stb__Expand6[64]; |
|
|
|
{ 5, 5 }, { 5, 5 }, { 5, 5 }, { 5, 5 }, { 5, 5 }, { 5, 5 }, { 6, 6 }, { 6, 6 }, |
|
|
|
static unsigned char stb__OMatch5[256][2]; |
|
|
|
{ 6, 6 }, { 6, 6 }, { 6, 6 }, { 6, 6 }, { 6, 6 }, { 6, 6 }, { 7, 7 }, { 7, 7 }, |
|
|
|
static unsigned char stb__OMatch6[256][2]; |
|
|
|
{ 7, 7 }, { 7, 7 }, { 7, 7 }, { 7, 7 }, { 7, 7 }, { 7, 7 }, { 8, 8 }, { 8, 8 }, |
|
|
|
static unsigned char stb__QuantRBTab[256+16]; |
|
|
|
{ 8, 8 }, { 8, 8 }, { 8, 8 }, { 8, 8 }, { 8, 8 }, { 8, 8 }, { 8, 8 }, { 9, 9 }, |
|
|
|
static unsigned char stb__QuantGTab[256+16]; |
|
|
|
{ 9, 9 }, { 9, 9 }, { 9, 9 }, { 9, 9 }, { 9, 9 }, { 9, 9 }, { 9, 9 }, { 10, 10 }, |
|
|
|
|
|
|
|
{ 10, 10 }, { 10, 10 }, { 10, 10 }, { 10, 10 }, { 10, 10 }, { 10, 10 }, { 10, 10 }, { 11, 11 }, |
|
|
|
|
|
|
|
{ 11, 11 }, { 11, 11 }, { 11, 11 }, { 11, 11 }, { 11, 11 }, { 11, 11 }, { 11, 11 }, { 12, 12 }, |
|
|
|
|
|
|
|
{ 12, 12 }, { 12, 12 }, { 12, 12 }, { 12, 12 }, { 12, 12 }, { 12, 12 }, { 12, 12 }, { 12, 12 }, |
|
|
|
|
|
|
|
{ 13, 13 }, { 13, 13 }, { 13, 13 }, { 13, 13 }, { 13, 13 }, { 13, 13 }, { 13, 13 }, { 13, 13 }, |
|
|
|
|
|
|
|
{ 14, 14 }, { 14, 14 }, { 14, 14 }, { 14, 14 }, { 14, 14 }, { 14, 14 }, { 14, 14 }, { 14, 14 }, |
|
|
|
|
|
|
|
{ 15, 15 }, { 15, 15 }, { 15, 15 }, { 15, 15 }, { 15, 15 }, { 15, 15 }, { 15, 15 }, { 15, 15 }, |
|
|
|
|
|
|
|
{ 16, 16 }, { 16, 16 }, { 16, 16 }, { 16, 16 }, { 16, 16 }, { 16, 16 }, { 16, 16 }, { 16, 16 }, |
|
|
|
|
|
|
|
{ 16, 16 }, { 17, 17 }, { 17, 17 }, { 17, 17 }, { 17, 17 }, { 17, 17 }, { 17, 17 }, { 17, 17 }, |
|
|
|
|
|
|
|
{ 17, 17 }, { 18, 18 }, { 18, 18 }, { 18, 18 }, { 18, 18 }, { 18, 18 }, { 18, 18 }, { 18, 18 }, |
|
|
|
|
|
|
|
{ 18, 18 }, { 19, 19 }, { 19, 19 }, { 19, 19 }, { 19, 19 }, { 19, 19 }, { 19, 19 }, { 19, 19 }, |
|
|
|
|
|
|
|
{ 19, 19 }, { 20, 20 }, { 20, 20 }, { 20, 20 }, { 20, 20 }, { 20, 20 }, { 20, 20 }, { 20, 20 }, |
|
|
|
|
|
|
|
{ 20, 20 }, { 20, 20 }, { 21, 21 }, { 21, 21 }, { 21, 21 }, { 21, 21 }, { 21, 21 }, { 21, 21 }, |
|
|
|
|
|
|
|
{ 21, 21 }, { 21, 21 }, { 22, 22 }, { 22, 22 }, { 22, 22 }, { 22, 22 }, { 22, 22 }, { 22, 22 }, |
|
|
|
|
|
|
|
{ 22, 22 }, { 22, 22 }, { 23, 23 }, { 23, 23 }, { 23, 23 }, { 23, 23 }, { 23, 23 }, { 23, 23 }, |
|
|
|
|
|
|
|
{ 23, 23 }, { 23, 23 }, { 24, 24 }, { 24, 24 }, { 24, 24 }, { 24, 24 }, { 24, 24 }, { 24, 24 }, |
|
|
|
|
|
|
|
{ 24, 24 }, { 24, 24 }, { 24, 24 }, { 25, 25 }, { 25, 25 }, { 25, 25 }, { 25, 25 }, { 25, 25 }, |
|
|
|
|
|
|
|
{ 25, 25 }, { 25, 25 }, { 25, 25 }, { 26, 26 }, { 26, 26 }, { 26, 26 }, { 26, 26 }, { 26, 26 }, |
|
|
|
|
|
|
|
{ 26, 26 }, { 26, 26 }, { 26, 26 }, { 27, 27 }, { 27, 27 }, { 27, 27 }, { 27, 27 }, { 27, 27 }, |
|
|
|
|
|
|
|
{ 27, 27 }, { 27, 27 }, { 27, 27 }, { 28, 28 }, { 28, 28 }, { 28, 28 }, { 28, 28 }, { 28, 28 }, |
|
|
|
|
|
|
|
{ 28, 28 }, { 28, 28 }, { 28, 28 }, { 28, 28 }, { 29, 29 }, { 29, 29 }, { 29, 29 }, { 29, 29 }, |
|
|
|
|
|
|
|
{ 29, 29 }, { 29, 29 }, { 29, 29 }, { 29, 29 }, { 30, 30 }, { 30, 30 }, { 30, 30 }, { 30, 30 }, |
|
|
|
|
|
|
|
{ 30, 30 }, { 30, 30 }, { 30, 30 }, { 30, 30 }, { 31, 31 }, { 31, 31 }, { 31, 31 }, { 31, 31 }, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
static const unsigned char stb__OMatch6[256][2] = { |
|
|
|
|
|
|
|
{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 1, 1 }, { 1, 1 }, { 1, 1 }, { 1, 1 }, { 2, 2 }, |
|
|
|
|
|
|
|
{ 2, 2 }, { 2, 2 }, { 2, 2 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 4, 4 }, |
|
|
|
|
|
|
|
{ 4, 4 }, { 4, 4 }, { 4, 4 }, { 5, 5 }, { 5, 5 }, { 5, 5 }, { 5, 5 }, { 6, 6 }, |
|
|
|
|
|
|
|
{ 6, 6 }, { 6, 6 }, { 6, 6 }, { 7, 7 }, { 7, 7 }, { 7, 7 }, { 7, 7 }, { 8, 8 }, |
|
|
|
|
|
|
|
{ 8, 8 }, { 8, 8 }, { 8, 8 }, { 9, 9 }, { 9, 9 }, { 9, 9 }, { 9, 9 }, { 10, 10 }, |
|
|
|
|
|
|
|
{ 10, 10 }, { 10, 10 }, { 10, 10 }, { 11, 11 }, { 11, 11 }, { 11, 11 }, { 11, 11 }, { 12, 12 }, |
|
|
|
|
|
|
|
{ 12, 12 }, { 12, 12 }, { 12, 12 }, { 13, 13 }, { 13, 13 }, { 13, 13 }, { 13, 13 }, { 14, 14 }, |
|
|
|
|
|
|
|
{ 14, 14 }, { 14, 14 }, { 14, 14 }, { 15, 15 }, { 15, 15 }, { 15, 15 }, { 15, 15 }, { 16, 16 }, |
|
|
|
|
|
|
|
{ 16, 16 }, { 16, 16 }, { 16, 16 }, { 16, 16 }, { 17, 17 }, { 17, 17 }, { 17, 17 }, { 17, 17 }, |
|
|
|
|
|
|
|
{ 18, 18 }, { 18, 18 }, { 18, 18 }, { 18, 18 }, { 19, 19 }, { 19, 19 }, { 19, 19 }, { 19, 19 }, |
|
|
|
|
|
|
|
{ 20, 20 }, { 20, 20 }, { 20, 20 }, { 20, 20 }, { 21, 21 }, { 21, 21 }, { 21, 21 }, { 21, 21 }, |
|
|
|
|
|
|
|
{ 22, 22 }, { 22, 22 }, { 22, 22 }, { 22, 22 }, { 23, 23 }, { 23, 23 }, { 23, 23 }, { 23, 23 }, |
|
|
|
|
|
|
|
{ 24, 24 }, { 24, 24 }, { 24, 24 }, { 24, 24 }, { 25, 25 }, { 25, 25 }, { 25, 25 }, { 25, 25 }, |
|
|
|
|
|
|
|
{ 26, 26 }, { 26, 26 }, { 26, 26 }, { 26, 26 }, { 27, 27 }, { 27, 27 }, { 27, 27 }, { 27, 27 }, |
|
|
|
|
|
|
|
{ 28, 28 }, { 28, 28 }, { 28, 28 }, { 28, 28 }, { 29, 29 }, { 29, 29 }, { 29, 29 }, { 29, 29 }, |
|
|
|
|
|
|
|
{ 30, 30 }, { 30, 30 }, { 30, 30 }, { 30, 30 }, { 31, 31 }, { 31, 31 }, { 31, 31 }, { 31, 31 }, |
|
|
|
|
|
|
|
{ 32, 32 }, { 32, 32 }, { 32, 32 }, { 32, 32 }, { 32, 32 }, { 33, 33 }, { 33, 33 }, { 33, 33 }, |
|
|
|
|
|
|
|
{ 33, 33 }, { 34, 34 }, { 34, 34 }, { 34, 34 }, { 34, 34 }, { 35, 35 }, { 35, 35 }, { 35, 35 }, |
|
|
|
|
|
|
|
{ 35, 35 }, { 36, 36 }, { 36, 36 }, { 36, 36 }, { 36, 36 }, { 37, 37 }, { 37, 37 }, { 37, 37 }, |
|
|
|
|
|
|
|
{ 37, 37 }, { 38, 38 }, { 38, 38 }, { 38, 38 }, { 38, 38 }, { 39, 39 }, { 39, 39 }, { 39, 39 }, |
|
|
|
|
|
|
|
{ 39, 39 }, { 40, 40 }, { 40, 40 }, { 40, 40 }, { 40, 40 }, { 41, 41 }, { 41, 41 }, { 41, 41 }, |
|
|
|
|
|
|
|
{ 41, 41 }, { 42, 42 }, { 42, 42 }, { 42, 42 }, { 42, 42 }, { 43, 43 }, { 43, 43 }, { 43, 43 }, |
|
|
|
|
|
|
|
{ 43, 43 }, { 44, 44 }, { 44, 44 }, { 44, 44 }, { 44, 44 }, { 45, 45 }, { 45, 45 }, { 45, 45 }, |
|
|
|
|
|
|
|
{ 45, 45 }, { 46, 46 }, { 46, 46 }, { 46, 46 }, { 46, 46 }, { 47, 47 }, { 47, 47 }, { 47, 47 }, |
|
|
|
|
|
|
|
{ 47, 47 }, { 48, 48 }, { 48, 48 }, { 48, 48 }, { 48, 48 }, { 48, 48 }, { 49, 49 }, { 49, 49 }, |
|
|
|
|
|
|
|
{ 49, 49 }, { 49, 49 }, { 50, 50 }, { 50, 50 }, { 50, 50 }, { 50, 50 }, { 51, 51 }, { 51, 51 }, |
|
|
|
|
|
|
|
{ 51, 51 }, { 51, 51 }, { 52, 52 }, { 52, 52 }, { 52, 52 }, { 52, 52 }, { 53, 53 }, { 53, 53 }, |
|
|
|
|
|
|
|
{ 53, 53 }, { 53, 53 }, { 54, 54 }, { 54, 54 }, { 54, 54 }, { 54, 54 }, { 55, 55 }, { 55, 55 }, |
|
|
|
|
|
|
|
{ 55, 55 }, { 55, 55 }, { 56, 56 }, { 56, 56 }, { 56, 56 }, { 56, 56 }, { 57, 57 }, { 57, 57 }, |
|
|
|
|
|
|
|
{ 57, 57 }, { 57, 57 }, { 58, 58 }, { 58, 58 }, { 58, 58 }, { 58, 58 }, { 59, 59 }, { 59, 59 }, |
|
|
|
|
|
|
|
{ 59, 59 }, { 59, 59 }, { 60, 60 }, { 60, 60 }, { 60, 60 }, { 60, 60 }, { 61, 61 }, { 61, 61 }, |
|
|
|
|
|
|
|
{ 61, 61 }, { 61, 61 }, { 62, 62 }, { 62, 62 }, { 62, 62 }, { 62, 62 }, { 63, 63 }, { 63, 63 }, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static int stb__Mul8Bit(int a, int b) |
|
|
|
static int stb__Mul8Bit(int a, int b) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -117,9 +172,10 @@ static void stb__From16Bit(unsigned char *out, unsigned short v) |
|
|
|
int gv = (v & 0x07e0) >> 5; |
|
|
|
int gv = (v & 0x07e0) >> 5; |
|
|
|
int bv = (v & 0x001f) >> 0; |
|
|
|
int bv = (v & 0x001f) >> 0; |
|
|
|
|
|
|
|
|
|
|
|
out[0] = stb__Expand5[rv]; |
|
|
|
// expand to 8 bits via bit replication
|
|
|
|
out[1] = stb__Expand6[gv]; |
|
|
|
out[0] = (rv * 33) >> 2; |
|
|
|
out[2] = stb__Expand5[bv]; |
|
|
|
out[1] = (gv * 65) >> 4; |
|
|
|
|
|
|
|
out[2] = (bv * 33) >> 2; |
|
|
|
out[3] = 0; |
|
|
|
out[3] = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -151,35 +207,6 @@ static void stb__Lerp13RGB(unsigned char *out, unsigned char *p1, unsigned char |
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************/ |
|
|
|
/****************************************************************************/ |
|
|
|
|
|
|
|
|
|
|
|
// compute table to reproduce constant colors as accurately as possible
|
|
|
|
|
|
|
|
static void stb__PrepareOptTable(unsigned char *Table,const unsigned char *expand,int size) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int i,mn,mx; |
|
|
|
|
|
|
|
for (i=0;i<256;i++) { |
|
|
|
|
|
|
|
int bestErr = 256; |
|
|
|
|
|
|
|
for (mn=0;mn<size;mn++) { |
|
|
|
|
|
|
|
for (mx=0;mx<size;mx++) { |
|
|
|
|
|
|
|
int mine = expand[mn]; |
|
|
|
|
|
|
|
int maxe = expand[mx]; |
|
|
|
|
|
|
|
int err = STBD_ABS(stb__Lerp13(maxe, mine) - i); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DX10 spec says that interpolation must be within 3% of "correct" result,
|
|
|
|
|
|
|
|
// add this as error term. (normally we'd expect a random distribution of
|
|
|
|
|
|
|
|
// +-1.5% error, but nowhere in the spec does it say that the error has to be
|
|
|
|
|
|
|
|
// unbiased - better safe than sorry).
|
|
|
|
|
|
|
|
err += STBD_ABS(maxe - mine) * 3 / 100; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(err < bestErr) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Table[i*2+0] = (unsigned char)mx; |
|
|
|
|
|
|
|
Table[i*2+1] = (unsigned char)mn; |
|
|
|
|
|
|
|
bestErr = err; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void stb__EvalColors(unsigned char *color,unsigned short c0,unsigned short c1) |
|
|
|
static void stb__EvalColors(unsigned char *color,unsigned short c0,unsigned short c1) |
|
|
|
{ |
|
|
|
{ |
|
|
|
stb__From16Bit(color+ 0, c0); |
|
|
|
stb__From16Bit(color+ 0, c0); |
|
|
@ -188,36 +215,8 @@ static void stb__EvalColors(unsigned char *color,unsigned short c0,unsigned shor |
|
|
|
stb__Lerp13RGB(color+12, color+4, color+0); |
|
|
|
stb__Lerp13RGB(color+12, color+4, color+0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Block dithering function. Simply dithers a block to 565 RGB.
|
|
|
|
|
|
|
|
// (Floyd-Steinberg)
|
|
|
|
|
|
|
|
static void stb__DitherBlock(unsigned char *dest, unsigned char *block) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int err[8],*ep1 = err,*ep2 = err+4, *et; |
|
|
|
|
|
|
|
int ch,y; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// process channels separately
|
|
|
|
|
|
|
|
for (ch=0; ch<3; ++ch) { |
|
|
|
|
|
|
|
unsigned char *bp = block+ch, *dp = dest+ch; |
|
|
|
|
|
|
|
unsigned char *quant = (ch == 1) ? stb__QuantGTab+8 : stb__QuantRBTab+8; |
|
|
|
|
|
|
|
STBD_MEMSET(err, 0, sizeof(err)); |
|
|
|
|
|
|
|
for(y=0; y<4; ++y) { |
|
|
|
|
|
|
|
dp[ 0] = quant[bp[ 0] + ((3*ep2[1] + 5*ep2[0]) >> 4)]; |
|
|
|
|
|
|
|
ep1[0] = bp[ 0] - dp[ 0]; |
|
|
|
|
|
|
|
dp[ 4] = quant[bp[ 4] + ((7*ep1[0] + 3*ep2[2] + 5*ep2[1] + ep2[0]) >> 4)]; |
|
|
|
|
|
|
|
ep1[1] = bp[ 4] - dp[ 4]; |
|
|
|
|
|
|
|
dp[ 8] = quant[bp[ 8] + ((7*ep1[1] + 3*ep2[3] + 5*ep2[2] + ep2[1]) >> 4)]; |
|
|
|
|
|
|
|
ep1[2] = bp[ 8] - dp[ 8]; |
|
|
|
|
|
|
|
dp[12] = quant[bp[12] + ((7*ep1[2] + 5*ep2[3] + ep2[2]) >> 4)]; |
|
|
|
|
|
|
|
ep1[3] = bp[12] - dp[12]; |
|
|
|
|
|
|
|
bp += 16; |
|
|
|
|
|
|
|
dp += 16; |
|
|
|
|
|
|
|
et = ep1, ep1 = ep2, ep2 = et; // swap
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The color matching function
|
|
|
|
// The color matching function
|
|
|
|
static unsigned int stb__MatchColorsBlock(unsigned char *block, unsigned char *color,int dither) |
|
|
|
static unsigned int stb__MatchColorsBlock(unsigned char *block, unsigned char *color) |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned int mask = 0; |
|
|
|
unsigned int mask = 0; |
|
|
|
int dirr = color[0*4+0] - color[1*4+0]; |
|
|
|
int dirr = color[0*4+0] - color[1*4+0]; |
|
|
@ -246,68 +245,14 @@ static unsigned int stb__MatchColorsBlock(unsigned char *block, unsigned char *c |
|
|
|
halfPoint = (stops[3] + stops[2]); |
|
|
|
halfPoint = (stops[3] + stops[2]); |
|
|
|
c3Point = (stops[2] + stops[0]); |
|
|
|
c3Point = (stops[2] + stops[0]); |
|
|
|
|
|
|
|
|
|
|
|
if(!dither) { |
|
|
|
for (i=15;i>=0;i--) { |
|
|
|
// the version without dithering is straightforward
|
|
|
|
int dot = dots[i]*2; |
|
|
|
for (i=15;i>=0;i--) { |
|
|
|
mask <<= 2; |
|
|
|
int dot = dots[i]*2; |
|
|
|
|
|
|
|
mask <<= 2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(dot < halfPoint) |
|
|
|
if(dot < halfPoint) |
|
|
|
mask |= (dot < c0Point) ? 1 : 3; |
|
|
|
mask |= (dot < c0Point) ? 1 : 3; |
|
|
|
else |
|
|
|
else |
|
|
|
mask |= (dot < c3Point) ? 2 : 0; |
|
|
|
mask |= (dot < c3Point) ? 2 : 0; |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// with floyd-steinberg dithering
|
|
|
|
|
|
|
|
int err[8],*ep1 = err,*ep2 = err+4; |
|
|
|
|
|
|
|
int *dp = dots, y; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c0Point <<= 3; |
|
|
|
|
|
|
|
halfPoint <<= 3; |
|
|
|
|
|
|
|
c3Point <<= 3; |
|
|
|
|
|
|
|
for(i=0;i<8;i++) |
|
|
|
|
|
|
|
err[i] = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(y=0;y<4;y++) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int dot,lmask,step; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dot = (dp[0] << 4) + (3*ep2[1] + 5*ep2[0]); |
|
|
|
|
|
|
|
if(dot < halfPoint) |
|
|
|
|
|
|
|
step = (dot < c0Point) ? 1 : 3; |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
step = (dot < c3Point) ? 2 : 0; |
|
|
|
|
|
|
|
ep1[0] = dp[0] - stops[step]; |
|
|
|
|
|
|
|
lmask = step; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dot = (dp[1] << 4) + (7*ep1[0] + 3*ep2[2] + 5*ep2[1] + ep2[0]); |
|
|
|
|
|
|
|
if(dot < halfPoint) |
|
|
|
|
|
|
|
step = (dot < c0Point) ? 1 : 3; |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
step = (dot < c3Point) ? 2 : 0; |
|
|
|
|
|
|
|
ep1[1] = dp[1] - stops[step]; |
|
|
|
|
|
|
|
lmask |= step<<2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dot = (dp[2] << 4) + (7*ep1[1] + 3*ep2[3] + 5*ep2[2] + ep2[1]); |
|
|
|
|
|
|
|
if(dot < halfPoint) |
|
|
|
|
|
|
|
step = (dot < c0Point) ? 1 : 3; |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
step = (dot < c3Point) ? 2 : 0; |
|
|
|
|
|
|
|
ep1[2] = dp[2] - stops[step]; |
|
|
|
|
|
|
|
lmask |= step<<4; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dot = (dp[3] << 4) + (7*ep1[2] + 5*ep2[3] + ep2[2]); |
|
|
|
|
|
|
|
if(dot < halfPoint) |
|
|
|
|
|
|
|
step = (dot < c0Point) ? 1 : 3; |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
step = (dot < c3Point) ? 2 : 0; |
|
|
|
|
|
|
|
ep1[3] = dp[3] - stops[step]; |
|
|
|
|
|
|
|
lmask |= step<<6; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dp += 4; |
|
|
|
|
|
|
|
mask |= lmask << (y*8); |
|
|
|
|
|
|
|
{ int *et = ep1; ep1 = ep2; ep2 = et; } // swap
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return mask; |
|
|
|
return mask; |
|
|
@ -316,7 +261,7 @@ static unsigned int stb__MatchColorsBlock(unsigned char *block, unsigned char *c |
|
|
|
// The color optimization function. (Clever code, part 1)
|
|
|
|
// The color optimization function. (Clever code, part 1)
|
|
|
|
static void stb__OptimizeColorsBlock(unsigned char *block, unsigned short *pmax16, unsigned short *pmin16) |
|
|
|
static void stb__OptimizeColorsBlock(unsigned char *block, unsigned short *pmax16, unsigned short *pmin16) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int mind = 0x7fffffff,maxd = -0x7fffffff; |
|
|
|
int mind,maxd; |
|
|
|
unsigned char *minp, *maxp; |
|
|
|
unsigned char *minp, *maxp; |
|
|
|
double magn; |
|
|
|
double magn; |
|
|
|
int v_r,v_g,v_b; |
|
|
|
int v_r,v_g,v_b; |
|
|
@ -398,8 +343,10 @@ static void stb__OptimizeColorsBlock(unsigned char *block, unsigned short *pmax1 |
|
|
|
v_b = (int) (vfb * magn); |
|
|
|
v_b = (int) (vfb * magn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
minp = maxp = block; |
|
|
|
|
|
|
|
mind = maxd = block[0]*v_r + block[1]*v_g + block[2]*v_b; |
|
|
|
// Pick colors at extreme points
|
|
|
|
// Pick colors at extreme points
|
|
|
|
for(i=0;i<16;i++) |
|
|
|
for(i=1;i<16;i++) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int dot = block[i*4+0]*v_r + block[i*4+1]*v_g + block[i*4+2]*v_b; |
|
|
|
int dot = block[i*4+0]*v_r + block[i*4+1]*v_g + block[i*4+2]*v_b; |
|
|
|
|
|
|
|
|
|
|
@ -418,12 +365,12 @@ static void stb__OptimizeColorsBlock(unsigned char *block, unsigned short *pmax1 |
|
|
|
*pmin16 = stb__As16Bit(minp[0],minp[1],minp[2]); |
|
|
|
*pmin16 = stb__As16Bit(minp[0],minp[1],minp[2]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static const float midpoints5[32] = { |
|
|
|
static const float stb__midpoints5[32] = { |
|
|
|
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, |
|
|
|
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, |
|
|
|
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.0f |
|
|
|
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.0f |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static const float midpoints6[64] = { |
|
|
|
static const float stb__midpoints6[64] = { |
|
|
|
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, |
|
|
|
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, |
|
|
|
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, |
|
|
|
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, |
|
|
|
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, |
|
|
|
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, |
|
|
@ -435,7 +382,7 @@ static unsigned short stb__Quantize5(float x) |
|
|
|
unsigned short q; |
|
|
|
unsigned short q; |
|
|
|
x = x < 0 ? 0 : x > 1 ? 1 : x; // saturate
|
|
|
|
x = x < 0 ? 0 : x > 1 ? 1 : x; // saturate
|
|
|
|
q = (unsigned short)(x * 31); |
|
|
|
q = (unsigned short)(x * 31); |
|
|
|
q += (x > midpoints5[q]); |
|
|
|
q += (x > stb__midpoints5[q]); |
|
|
|
return q; |
|
|
|
return q; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -444,7 +391,7 @@ static unsigned short stb__Quantize6(float x) |
|
|
|
unsigned short q; |
|
|
|
unsigned short q; |
|
|
|
x = x < 0 ? 0 : x > 1 ? 1 : x; // saturate
|
|
|
|
x = x < 0 ? 0 : x > 1 ? 1 : x; // saturate
|
|
|
|
q = (unsigned short)(x * 63); |
|
|
|
q = (unsigned short)(x * 63); |
|
|
|
q += (x > midpoints6[q]); |
|
|
|
q += (x > stb__midpoints6[q]); |
|
|
|
return q; |
|
|
|
return q; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -532,12 +479,10 @@ static void stb__CompressColorBlock(unsigned char *dest, unsigned char *block, i |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned int mask; |
|
|
|
unsigned int mask; |
|
|
|
int i; |
|
|
|
int i; |
|
|
|
int dither; |
|
|
|
|
|
|
|
int refinecount; |
|
|
|
int refinecount; |
|
|
|
unsigned short max16, min16; |
|
|
|
unsigned short max16, min16; |
|
|
|
unsigned char dblock[16*4],color[4*4]; |
|
|
|
unsigned char color[4*4]; |
|
|
|
|
|
|
|
|
|
|
|
dither = mode & STB_DXT_DITHER; |
|
|
|
|
|
|
|
refinecount = (mode & STB_DXT_HIGHQUAL) ? 2 : 1; |
|
|
|
refinecount = (mode & STB_DXT_HIGHQUAL) ? 2 : 1; |
|
|
|
|
|
|
|
|
|
|
|
// check if block is constant
|
|
|
|
// check if block is constant
|
|
|
@ -551,15 +496,11 @@ static void stb__CompressColorBlock(unsigned char *dest, unsigned char *block, i |
|
|
|
max16 = (stb__OMatch5[r][0]<<11) | (stb__OMatch6[g][0]<<5) | stb__OMatch5[b][0]; |
|
|
|
max16 = (stb__OMatch5[r][0]<<11) | (stb__OMatch6[g][0]<<5) | stb__OMatch5[b][0]; |
|
|
|
min16 = (stb__OMatch5[r][1]<<11) | (stb__OMatch6[g][1]<<5) | stb__OMatch5[b][1]; |
|
|
|
min16 = (stb__OMatch5[r][1]<<11) | (stb__OMatch6[g][1]<<5) | stb__OMatch5[b][1]; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// first step: compute dithered version for PCA if desired
|
|
|
|
// first step: PCA+map along principal axis
|
|
|
|
if(dither) |
|
|
|
stb__OptimizeColorsBlock(block,&max16,&min16); |
|
|
|
stb__DitherBlock(dblock,block); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// second step: pca+map along principal axis
|
|
|
|
|
|
|
|
stb__OptimizeColorsBlock(dither ? dblock : block,&max16,&min16); |
|
|
|
|
|
|
|
if (max16 != min16) { |
|
|
|
if (max16 != min16) { |
|
|
|
stb__EvalColors(color,max16,min16); |
|
|
|
stb__EvalColors(color,max16,min16); |
|
|
|
mask = stb__MatchColorsBlock(block,color,dither); |
|
|
|
mask = stb__MatchColorsBlock(block,color); |
|
|
|
} else |
|
|
|
} else |
|
|
|
mask = 0; |
|
|
|
mask = 0; |
|
|
|
|
|
|
|
|
|
|
@ -567,10 +508,10 @@ static void stb__CompressColorBlock(unsigned char *dest, unsigned char *block, i |
|
|
|
for (i=0;i<refinecount;i++) { |
|
|
|
for (i=0;i<refinecount;i++) { |
|
|
|
unsigned int lastmask = mask; |
|
|
|
unsigned int lastmask = mask; |
|
|
|
|
|
|
|
|
|
|
|
if (stb__RefineBlock(dither ? dblock : block,&max16,&min16,mask)) { |
|
|
|
if (stb__RefineBlock(block,&max16,&min16,mask)) { |
|
|
|
if (max16 != min16) { |
|
|
|
if (max16 != min16) { |
|
|
|
stb__EvalColors(color,max16,min16); |
|
|
|
stb__EvalColors(color,max16,min16); |
|
|
|
mask = stb__MatchColorsBlock(block,color,dither); |
|
|
|
mask = stb__MatchColorsBlock(block,color); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
mask = 0; |
|
|
|
mask = 0; |
|
|
|
break; |
|
|
|
break; |
|
|
@ -654,35 +595,9 @@ static void stb__CompressAlphaBlock(unsigned char *dest,unsigned char *src, int |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void stb__InitDXT() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int i; |
|
|
|
|
|
|
|
for(i=0;i<32;i++) |
|
|
|
|
|
|
|
stb__Expand5[i] = (unsigned char)((i<<3)|(i>>2)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(i=0;i<64;i++) |
|
|
|
|
|
|
|
stb__Expand6[i] = (unsigned char)((i<<2)|(i>>4)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(i=0;i<256+16;i++) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int v = i-8 < 0 ? 0 : i-8 > 255 ? 255 : i-8; |
|
|
|
|
|
|
|
stb__QuantRBTab[i] = stb__Expand5[stb__Mul8Bit(v,31)]; |
|
|
|
|
|
|
|
stb__QuantGTab[i] = stb__Expand6[stb__Mul8Bit(v,63)]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stb__PrepareOptTable(&stb__OMatch5[0][0],stb__Expand5,32); |
|
|
|
|
|
|
|
stb__PrepareOptTable(&stb__OMatch6[0][0],stb__Expand6,64); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src, int alpha, int mode) |
|
|
|
void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src, int alpha, int mode) |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned char data[16][4]; |
|
|
|
unsigned char data[16][4]; |
|
|
|
static int init=1; |
|
|
|
|
|
|
|
if (init) { |
|
|
|
|
|
|
|
stb__InitDXT(); |
|
|
|
|
|
|
|
init=0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (alpha) { |
|
|
|
if (alpha) { |
|
|
|
int i; |
|
|
|
int i; |
|
|
|
stb__CompressAlphaBlock(dest,(unsigned char*) src+3, 4); |
|
|
|
stb__CompressAlphaBlock(dest,(unsigned char*) src+3, 4); |
|
|
@ -710,6 +625,56 @@ void stb_compress_bc5_block(unsigned char *dest, const unsigned char *src) |
|
|
|
} |
|
|
|
} |
|
|
|
#endif // STB_DXT_IMPLEMENTATION
|
|
|
|
#endif // STB_DXT_IMPLEMENTATION
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Compile with STB_DXT_IMPLEMENTATION and STB_DXT_GENERATE_TABLES
|
|
|
|
|
|
|
|
// defined to generate the tables above.
|
|
|
|
|
|
|
|
#ifdef STB_DXT_GENERATE_TABLES |
|
|
|
|
|
|
|
#include <stdio.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int i, j; |
|
|
|
|
|
|
|
const char *omatch_names[] = { "stb__OMatch5", "stb__OMatch6" }; |
|
|
|
|
|
|
|
int dequant_mults[2] = { 33*4, 65 }; // .4 fixed-point dequant multipliers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// optimal endpoint tables
|
|
|
|
|
|
|
|
for (i = 0; i < 2; ++i) { |
|
|
|
|
|
|
|
int dequant = dequant_mults[i]; |
|
|
|
|
|
|
|
int size = i ? 64 : 32; |
|
|
|
|
|
|
|
printf("static const unsigned char %s[256][2] = {\n", omatch_names[i]); |
|
|
|
|
|
|
|
for (int j = 0; j < 256; ++j) { |
|
|
|
|
|
|
|
int mn, mx; |
|
|
|
|
|
|
|
int best_mn = 0, best_mx = 0; |
|
|
|
|
|
|
|
int best_err = 256 * 100; |
|
|
|
|
|
|
|
for (mn=0;mn<size;mn++) { |
|
|
|
|
|
|
|
for (mx=0;mx<size;mx++) { |
|
|
|
|
|
|
|
int mine = (mn * dequant) >> 4; |
|
|
|
|
|
|
|
int maxe = (mx * dequant) >> 4; |
|
|
|
|
|
|
|
int err = abs(stb__Lerp13(maxe, mine) - j); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DX10 spec says that interpolation must be within 3% of "correct" result,
|
|
|
|
|
|
|
|
// add this as error term. Normally we'd expect a random distribution of
|
|
|
|
|
|
|
|
// +-1.5% error, but nowhere in the spec does it say that the error has to be
|
|
|
|
|
|
|
|
// unbiased - better safe than sorry.
|
|
|
|
|
|
|
|
err += abs(maxe - mine) * 3; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(err < best_err) { |
|
|
|
|
|
|
|
best_mn = mn; |
|
|
|
|
|
|
|
best_mx = mx; |
|
|
|
|
|
|
|
best_err = err; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if ((j % 8) == 0) printf(" "); // 2 spaces, third is done below
|
|
|
|
|
|
|
|
printf(" { %2d, %2d },", best_mx, best_mn); |
|
|
|
|
|
|
|
if ((j % 8) == 7) printf("\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
printf("};\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
------------------------------------------------------------------------------ |
|
|
|
------------------------------------------------------------------------------ |
|
|
|
This software is available under 2 licenses -- choose whichever you prefer. |
|
|
|
This software is available under 2 licenses -- choose whichever you prefer. |
|
|
|