#include <math.h>
#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
#include "s2tc_compressor.h"
#include "s2tc_common.h"
return sqrtf(comp) + 0.5f;
}
- template<DxtMode dxt, ColorDistFunc ColorDist, CompressionMode mode, bool refine>
+ template<DxtMode dxt, ColorDistFunc ColorDist, CompressionMode mode, RefinementMode refine>
inline void s2tc_encode_block(unsigned char *out, const unsigned char *rgba, int iw, int w, int h, int nrandom)
{
color_t c[16 + (mode == MODE_RANDOM ? nrandom : 0)];
reduce_colors_inplace_2fixpoints(ca, n, n, alpha_dist, (unsigned char) 0, (unsigned char) 255);
}
- if(!refine)
+ if(refine == REFINE_NEVER)
{
if(dxt == DXT5)
{
else if(da[0] <= da[1])
{
// 0
- if(refine)
+ if(refine != REFINE_NEVER)
{
++na0;
sa0 += ca[2];
{
// 1
out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
- if(refine)
+ if(refine != REFINE_NEVER)
{
++na1;
sa1 += ca[2];
{
int bitindex = pindex * 2;
out[bitindex / 8 + 12] |= (1 << (bitindex % 8));
- if(refine)
+ if(refine != REFINE_NEVER)
{
++nc1;
sc1r += refine_component_encode<ColorDist>(c[2].r);
}
else
{
- if(refine)
+ if(refine != REFINE_NEVER)
{
++nc0;
sc0r += refine_component_encode<ColorDist>(c[2].r);
{
int bitindex = pindex * 2;
out[bitindex / 8 + 12] |= (1 << (bitindex % 8));
- if(refine)
+ if(refine != REFINE_NEVER)
{
++nc1;
sc1r += refine_component_encode<ColorDist>(c[2].r);
}
else
{
- if(refine)
+ if(refine != REFINE_NEVER)
{
++nc0;
sc0r += refine_component_encode<ColorDist>(c[2].r);
else if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
{
out[bitindex / 8 + 4] |= (1 << (bitindex % 8));
- if(refine)
+ if(refine != REFINE_NEVER)
{
++nc1;
sc1r += refine_component_encode<ColorDist>(c[2].r);
}
else
{
- if(refine)
+ if(refine != REFINE_NEVER)
{
++nc0;
sc0r += refine_component_encode<ColorDist>(c[2].r);
break;
}
}
- if(refine)
+ if(refine != REFINE_NEVER)
{
if(dxt == DXT5)
{
+ if(refine == REFINE_CHECK)
+ {
+ ca[2] = ca[0];
+ ca[3] = ca[1];
+ }
if(na0)
ca[0] = (2 * sa0 + na0) / (2 * na0);
if(na1)
ca[1] = (2 * sa1 + na1) / (2 * na1);
}
+ if(refine == REFINE_CHECK)
+ {
+ c[2] = c[0];
+ c[3] = c[1];
+ }
if(nc0)
{
c[0].r = refine_component_decode<ColorDist>((2 * sc0r + nc0) / (2 * nc0));
c[1].b = refine_component_decode<ColorDist>((2 * sc1b + nc1) / (2 * nc1));
}
+ if(refine == REFINE_CHECK)
+ {
+ int score_01 = 0;
+ int score_23 = 0;
+ for(x = 0; x < w; ++x)
+ for(y = 0; y < h; ++y)
+ {
+ int pindex = (x+y*4);
+ c[4].r = rgba[(x + y * iw) * 4 + 2];
+ c[4].g = rgba[(x + y * iw) * 4 + 1];
+ c[4].b = rgba[(x + y * iw) * 4 + 0];
+ ca[4] = rgba[(x + y * iw) * 4 + 3];
+ if(dxt == DXT1 && !ca[4])
+ continue;
+ int bitindex = pindex * 2;
+ if(out[bitindex / 8 + (dxt == DXT1 ? 4 : 12)] & (1 << (bitindex % 8)))
+ {
+ // we picked an 1
+ score_01 += ColorDist(c[1], c[4]);
+ score_23 += ColorDist(c[3], c[4]);
+ }
+ else
+ {
+ // we picked a 0
+ score_01 += ColorDist(c[0], c[4]);
+ score_23 += ColorDist(c[2], c[4]);
+ }
+ }
+
+ if(score_23 < score_01)
+ {
+ // refinement was BAD
+ c[0] = c[2];
+ c[1] = c[3];
+ }
+
+ // alpha refinement is always good and doesn't
+ // need to be checked because alpha is linear
+ }
+
if(dxt == DXT5)
{
if(ca[1] < ca[0])
// compile time dispatch magic
template<DxtMode dxt, ColorDistFunc ColorDist, CompressionMode mode>
- inline s2tc_encode_block_func_t s2tc_encode_block_func(bool refine)
+ inline s2tc_encode_block_func_t s2tc_encode_block_func(RefinementMode refine)
{
- if(refine)
- return s2tc_encode_block<dxt, ColorDist, mode, true>;
- else
- return s2tc_encode_block<dxt, ColorDist, mode, false>;
+ switch(refine)
+ {
+ case REFINE_NEVER:
+ return s2tc_encode_block<dxt, ColorDist, mode, REFINE_NEVER>;
+ case REFINE_CHECK:
+ // these color dist functions do not need the refinement check, as they always improve the situation
+ if(ColorDist != color_dist_avg && ColorDist != color_dist_wavg)
+ return s2tc_encode_block<dxt, ColorDist, mode, REFINE_CHECK>;
+ default:
+ case REFINE_ALWAYS:
+ return s2tc_encode_block<dxt, ColorDist, mode, REFINE_ALWAYS>;
+ }
}
template<DxtMode dxt, ColorDistFunc ColorDist>
- inline s2tc_encode_block_func_t s2tc_encode_block_func(int nrandom, bool refine)
+ inline s2tc_encode_block_func_t s2tc_encode_block_func(int nrandom, RefinementMode refine)
{
if(nrandom > 0)
return s2tc_encode_block_func<dxt, ColorDist, MODE_RANDOM>(refine);
}
template<ColorDistFunc ColorDist>
- inline s2tc_encode_block_func_t s2tc_encode_block_func(DxtMode dxt, int nrandom, bool refine)
+ inline s2tc_encode_block_func_t s2tc_encode_block_func(DxtMode dxt, int nrandom, RefinementMode refine)
{
switch(dxt)
{
}
};
-s2tc_encode_block_func_t s2tc_encode_block_func(DxtMode dxt, ColorDistMode cd, int nrandom, bool refine)
+s2tc_encode_block_func_t s2tc_encode_block_func(DxtMode dxt, ColorDistMode cd, int nrandom, RefinementMode refine)
{
switch(cd)
{
html "$i"-nvcompress.dds
fi
- ( S2TC_COLORDIST_MODE=SRGB_MIXED S2TC_RANDOM_COLORS=64 S2TC_REFINE_COLORS=1 t "$i".tga "$i"-rand64-mrgb-r.dds ./s2tc )
- ( S2TC_COLORDIST_MODE=SRGB_MIXED S2TC_RANDOM_COLORS=64 S2TC_REFINE_COLORS=0 t "$i".tga "$i"-rand64-mrgb-n.dds ./s2tc )
- ( S2TC_COLORDIST_MODE=WAVG S2TC_RANDOM_COLORS=64 S2TC_REFINE_COLORS=1 t "$i".tga "$i"-rand64-wavg-r.dds ./s2tc )
- ( S2TC_COLORDIST_MODE=WAVG S2TC_RANDOM_COLORS=64 S2TC_REFINE_COLORS=0 t "$i".tga "$i"-rand64-wavg-n.dds ./s2tc )
- ( S2TC_COLORDIST_MODE=AVG S2TC_RANDOM_COLORS=64 S2TC_REFINE_COLORS=1 t "$i".tga "$i"-rand64-avg-r.dds ./s2tc )
- ( S2TC_COLORDIST_MODE=AVG S2TC_RANDOM_COLORS=64 S2TC_REFINE_COLORS=0 t "$i".tga "$i"-rand64-avg-n.dds ./s2tc )
+ ( S2TC_COLORDIST_MODE=SRGB_MIXED S2TC_RANDOM_COLORS=64 S2TC_REFINE_COLORS=CHECK t "$i".tga "$i"-rand64-mrgb-r.dds ./s2tc )
+ ( S2TC_COLORDIST_MODE=SRGB_MIXED S2TC_RANDOM_COLORS=64 S2TC_REFINE_COLORS=NEVER t "$i".tga "$i"-rand64-mrgb-n.dds ./s2tc )
+ ( S2TC_COLORDIST_MODE=WAVG S2TC_RANDOM_COLORS=64 S2TC_REFINE_COLORS=CHECK t "$i".tga "$i"-rand64-wavg-r.dds ./s2tc )
+ ( S2TC_COLORDIST_MODE=WAVG S2TC_RANDOM_COLORS=64 S2TC_REFINE_COLORS=NEVER t "$i".tga "$i"-rand64-wavg-n.dds ./s2tc )
+ ( S2TC_COLORDIST_MODE=AVG S2TC_RANDOM_COLORS=64 S2TC_REFINE_COLORS=CHECK t "$i".tga "$i"-rand64-avg-r.dds ./s2tc )
+ ( S2TC_COLORDIST_MODE=AVG S2TC_RANDOM_COLORS=64 S2TC_REFINE_COLORS=NEVER t "$i".tga "$i"-rand64-avg-n.dds ./s2tc )
if $use_libtxc_dxtn; then
( LD_PRELOAD=/usr/lib/libtxc_dxtn.so t "$i".tga "$i"-libtxc_dxtn.dds ./s2tc )
fi
- ( S2TC_COLORDIST_MODE=WAVG S2TC_RANDOM_COLORS=0 S2TC_REFINE_COLORS=1 t "$i".tga "$i"-norand-wavg-r.dds ./s2tc )
- ( S2TC_COLORDIST_MODE=WAVG S2TC_RANDOM_COLORS=0 S2TC_REFINE_COLORS=0 t "$i".tga "$i"-norand-wavg-n.dds ./s2tc )
- ( S2TC_COLORDIST_MODE=WAVG S2TC_RANDOM_COLORS=-1 S2TC_REFINE_COLORS=1 t "$i".tga "$i"-faster-wavg-r.dds ./s2tc )
- ( S2TC_COLORDIST_MODE=WAVG S2TC_RANDOM_COLORS=-1 S2TC_REFINE_COLORS=0 t "$i".tga "$i"-faster-wavg-n.dds ./s2tc )
+ ( S2TC_COLORDIST_MODE=WAVG S2TC_RANDOM_COLORS=0 S2TC_REFINE_COLORS=ALWAYS t "$i".tga "$i"-norand-wavg-r.dds ./s2tc )
+ ( S2TC_COLORDIST_MODE=WAVG S2TC_RANDOM_COLORS=0 S2TC_REFINE_COLORS=NEVER t "$i".tga "$i"-norand-wavg-n.dds ./s2tc )
+ ( S2TC_COLORDIST_MODE=WAVG S2TC_RANDOM_COLORS=-1 S2TC_REFINE_COLORS=ALWAYS t "$i".tga "$i"-faster-wavg-r.dds ./s2tc )
+ ( S2TC_COLORDIST_MODE=WAVG S2TC_RANDOM_COLORS=-1 S2TC_REFINE_COLORS=NEVER t "$i".tga "$i"-faster-wavg-n.dds ./s2tc )
html_rowend
done