Browse Source

Add feature to replace abs/fabs and memset with your own.

Kevin Schmidt 8 years ago
parent
commit
9b3358fec1
1 changed files with 22 additions and 7 deletions
  1. 22 7
      stb_dxt.h

+ 22 - 7
stb_dxt.h

@@ -61,8 +61,23 @@ void stb_compress_bc5_block(unsigned char *dest, const unsigned char *src_rg_two
 // #define STB_DXT_USE_ROUNDING_BIAS
 
 #include <stdlib.h>
+
+#if !defined(STBD_ABS) || !defined(STBI_FABS)
 #include <math.h>
-#include <string.h> // memset
+#endif
+
+#ifndef STBD_ABS
+#define STBD_ABS(i)           abs(i)
+#endif
+
+#ifndef STBD_FABS
+#define STBD_FABS(x)          fabs(x)
+#endif
+
+#ifndef STBD_MEMSET
+#include <string.h>
+#define STBD_MEMSET(x)        memset(x)
+#endif
 
 static unsigned char stb__Expand5[32];
 static unsigned char stb__Expand6[64];
@@ -127,13 +142,13 @@ static void stb__PrepareOptTable(unsigned char *Table,const unsigned char *expan
          for (mx=0;mx<size;mx++) {
             int mine = expand[mn];
             int maxe = expand[mx];
-            int err = abs(stb__Lerp13(maxe, mine) - i);
+            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 += abs(maxe - mine) * 3 / 100;
+            err += STBD_ABS(maxe - mine) * 3 / 100;
             
             if(err < bestErr)
             { 
@@ -165,7 +180,7 @@ static void stb__DitherBlock(unsigned char *dest, unsigned char *block)
   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;
-      memset(err, 0, sizeof(err));
+      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];
@@ -349,9 +364,9 @@ static void stb__OptimizeColorsBlock(unsigned char *block, unsigned short *pmax1
     vfb = b;
   }
 
-  magn = fabs(vfr);
-  if (fabs(vfg) > magn) magn = fabs(vfg);
-  if (fabs(vfb) > magn) magn = fabs(vfb);
+  magn = STBD_FABS(vfr);
+  if (STBD_FABS(vfg) > magn) magn = STBD_FABS(vfg);
+  if (STBD_FABS(vfb) > magn) magn = STBD_FABS(vfb);
 
    if(magn < 4.0f) { // too small, default to luminance
       v_r = 299; // JPEG YCbCr luma coefs, scaled by 1000.