OSDN Git Service

also add L*a*b* colorspace (unoptimized, slow, bad)
authorRudolf Polzer <divverent@alientrap.org>
Tue, 12 Jul 2011 10:03:18 +0000 (12:03 +0200)
committerRudolf Polzer <divverent@xonotic.org>
Thu, 14 Jul 2011 17:12:19 +0000 (19:12 +0200)
s2tc.cpp
test.sh

index d770927..284ea17 100644 (file)
--- a/s2tc.cpp
+++ b/s2tc.cpp
@@ -554,6 +554,37 @@ inline int color_dist_srgb(const color_t &a, const color_t &b)
        return SHRR(sy, 4) + SHRR(su, 8) + SHRR(sv, 10);
 }
 
+// FIXME is this correct?
+inline int color_dist_lab_srgb(const color_t &a, const color_t &b)
+{
+       // undo sRGB
+       float ar = powf(a.r / 31.0f, 2.4f);
+       float ag = powf(a.g / 63.0f, 2.4f);
+       float ab = powf(a.b / 31.0f, 2.4f);
+       float br = powf(b.r / 31.0f, 2.4f);
+       float bg = powf(b.g / 63.0f, 2.4f);
+       float bb = powf(b.b / 31.0f, 2.4f);
+       // convert to CIE XYZ
+       float aX = 0.4124f * ar + 0.3576f * ag + 0.1805f * ab;
+       float aY = 0.2126f * ar + 0.7152f * ag + 0.0722f * ab;
+       float aZ = 0.0193f * ar + 0.1192f * ag + 0.9505f * ab;
+       float bX = 0.4124f * br + 0.3576f * bg + 0.1805f * bb;
+       float bY = 0.2126f * br + 0.7152f * bg + 0.0722f * bb;
+       float bZ = 0.0193f * br + 0.1192f * bg + 0.9505f * bb;
+       // convert to CIE Lab
+       float Xn = 0.3127f;
+       float Yn = 0.3290f;
+       float Zn = 0.3583f;
+       float aL = 116 * cbrtf(aY / Yn) - 16;
+       float aA = 500 * (cbrtf(aX / Xn) - cbrtf(aY / Yn));
+       float aB = 200 * (cbrtf(aY / Yn) - cbrtf(aZ / Zn));
+       float bL = 116 * cbrtf(bY / Yn) - 16;
+       float bA = 500 * (cbrtf(bX / Xn) - cbrtf(bY / Yn));
+       float bB = 200 * (cbrtf(bY / Yn) - cbrtf(bZ / Zn));
+       // euclidean distance
+       return 100 * ((aL - bL) * (aL - bL) + (aA - bA) * (aA - bA) + (aB - bB) * (aB - bB));
+}
+
 inline int color_dist_normalmap(const color_t &a, const color_t &b)
 {
        float ca[3], cb[3];
@@ -738,6 +769,7 @@ enum ColorDistMode
        RGB,
        YUV,
        SRGB,
+       LAB,
        AVG,
        NORMALMAP
 };
@@ -940,6 +972,9 @@ void s2tc_encode_block(unsigned char *out, const unsigned char *rgba, int iw, in
                case SRGB:
                        s2tc_encode_block<color_dist_srgb>(out, rgba, iw, w, h, dxt, nrandom);
                        break;
+               case LAB:
+                       s2tc_encode_block<color_dist_lab_srgb>(out, rgba, iw, w, h, dxt, nrandom);
+                       break;
                case AVG:
                        s2tc_encode_block<color_dist_avg>(out, rgba, iw, w, h, dxt, nrandom);
                        break;
@@ -972,7 +1007,7 @@ int usage(const char *me)
                        "    [-o outfile.dds]\n"
                        "    [-t {DXT1|DXT3|DXT5}]\n"
                        "    [-r randomcount]\n"
-                       "    [-c {RGB|YUV|SRGB|AVG|NORMALMAP}]\n",
+                       "    [-c {RGB|YUV|SRGB|LAB|AVG|NORMALMAP}]\n",
                        me);
        return 1;
 }
@@ -1021,6 +1056,8 @@ int main(int argc, char **argv)
                                        cd = YUV;
                                else if(!strcasecmp(optarg, "SRGB"))
                                        cd = SRGB;
+                               else if(!strcasecmp(optarg, "LAB"))
+                                       cd = LAB;
                                else if(!strcasecmp(optarg, "AVG"))
                                        cd = AVG;
                                else if(!strcasecmp(optarg, "NORMALMAP"))
diff --git a/test.sh b/test.sh
index dc21870..7655e87 100644 (file)
--- a/test.sh
+++ b/test.sh
@@ -15,8 +15,12 @@ html_start()
        echo >&3 "<tr><th>Picture</th>"
        echo >&3 "<th>Original</th>"
        echo >&3 "<th>nvcompress</th>"
+       echo >&3 "<th>rand64-L*a*b*</th>"
+       echo >&3 "<th>norand-L*a*b*</th>"
        echo >&3 "<th>rand64-sRGB</th>"
        echo >&3 "<th>norand-sRGB</th>"
+       echo >&3 "<th>rand64-YUV</th>"
+       echo >&3 "<th>norand-YUV</th>"
        echo >&3 "<th>rand64</th>"
        echo >&3 "<th>norand</th>"
        echo >&3 "<th>rand64-avg</th>"
@@ -50,7 +54,7 @@ t()
 }
 
 html_start
-for i in dxtfail base_concrete1a disabled floor_tile3a lift02 panel_ceil1a sunset amelia rms; do
+for i in amelia dxtfail base_concrete1a disabled floor_tile3a lift02 panel_ceil1a sunset rms; do
        html_rowstart "$i"
 
        html "$i".tga
@@ -58,8 +62,12 @@ for i in dxtfail base_concrete1a disabled floor_tile3a lift02 panel_ceil1a sunse
        nvcompress "$i".tga "$i".dds
        html "$i".dds
 
+       t "$i".tga "$i"-rand64-lab.dds  ./s2tc -c LAB  -r 64
+       t "$i".tga "$i"-nogray-lab.dds  ./s2tc -c LAB  -r 0
        t "$i".tga "$i"-rand64-srgb.dds ./s2tc -c SRGB -r 64
        t "$i".tga "$i"-nogray-srgb.dds ./s2tc -c SRGB -r 0
+       t "$i".tga "$i"-rand64-yuv.dds  ./s2tc -c YUV  -r 64
+       t "$i".tga "$i"-nogray-yuv.dds  ./s2tc -c YUV  -r 0
        t "$i".tga "$i"-rand64.dds      ./s2tc -c RGB  -r 64
        t "$i".tga "$i"-norand.dds      ./s2tc -c RGB  -r 0
        t "$i".tga "$i"-rand64-avg.dds  ./s2tc -c AVG  -r 64