OSDN Git Service

always use the dynamic lib
authorRudolf Polzer <divverent@xonotic.org>
Wed, 13 Jul 2011 14:43:06 +0000 (16:43 +0200)
committerRudolf Polzer <divverent@xonotic.org>
Thu, 14 Jul 2011 17:12:25 +0000 (19:12 +0200)
Makefile
libtxc_dxtn.h
s2tc.cpp
s2tc_libtxc_dxtn.cpp

index 0ea0ea7..a5187e5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,14 +8,14 @@ LDFLAGS = -lm
 
 include $(wildcard *.d)
 
-s2tc: s2tc.o s2tc_compressor.o
+s2tc: s2tc.o libtxc_dxtn.so
        $(CXX) $(LDFLAGS) -o $@ $+
 
-s2tc_decompress: s2tc_decompress.o s2tc_libtxc_dxtn.o s2tc_compressor.o
+s2tc_decompress: s2tc_decompress.o libtxc_dxtn.so
        $(CXX) $(LDFLAGS) -o $@ $+
 
 libtxc_dxtn.so: s2tc_libtxc_dxtn.o s2tc_compressor.o
-       $(CXX) $(LDFLAGS) -shared -o $@ $+
+       $(CXX) $(LDFLAGS) -shared -Wl,-soname=./libtxc_dxtn.so -o $@ $+
 
 clean:
        $(RM) *.o s2tc libtxc_dxtn.so
index 1d33175..60c94f0 100644 (file)
@@ -16,6 +16,9 @@ void fetch_2d_texel_rgba_dxt3(GLint srcRowStride, const GLubyte *pixdata,
                             GLint i, GLint j, GLvoid *texel);
 void fetch_2d_texel_rgba_dxt5(GLint srcRowStride, const GLubyte *pixdata,
                             GLint i, GLint j, GLvoid *texel);
+void tx_compress_dxtn(GLint srccomps, GLint width, GLint height,
+                     const GLubyte *srcPixData, GLenum destFormat,
+                     GLubyte *dest, GLint dstRowStride);
 
 #ifdef __cplusplus
 }
index 6985b1f..ff1baf4 100644 (file)
--- a/s2tc.cpp
+++ b/s2tc.cpp
@@ -4,8 +4,7 @@
 #include <string.h>
 #include <stdint.h>
 #include <getopt.h>
-
-#include "s2tc_compressor.h"
+#include "libtxc_dxtn.h"
 #include "s2tc_common.h"
 
 /* START stuff that originates from image.c in DarkPlaces */
@@ -507,11 +506,10 @@ int main(int argc, char **argv)
        int piclen;
        const char *fourcc;
        int blocksize, alphabits;
-       DxtMode dxt = DXT1;
-       ColorDistMode cd = RGB;
-       int nrandom = 0;
+       GLenum dxt = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
        const char *infile = NULL, *outfile = NULL;
        FILE *outfh;
+       char buf[80];
 
        int opt;
        while((opt = getopt(argc, argv, "i:o:t:r:c:")) != -1)
@@ -526,36 +524,23 @@ int main(int argc, char **argv)
                                break;
                        case 't':
                                if(!strcasecmp(optarg, "DXT1"))
-                                       dxt = DXT1;
+                                       dxt = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
                                else if(!strcasecmp(optarg, "DXT3"))
-                                       dxt = DXT3;
+                                       dxt = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
                                else if(!strcasecmp(optarg, "DXT5"))
-                                       dxt = DXT5;
+                                       dxt = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
                                else
                                        return usage(argv[0]);
                                break;
                        case 'r':
-                               nrandom = atoi(optarg);
+                               snprintf(buf, sizeof(buf), "S2TC_RANDOM_COLORS=%d", atoi(optarg));
+                               buf[sizeof(buf)-1] = 0;
+                               putenv(buf);
                                break;
                        case 'c':
-                               if(!strcasecmp(optarg, "RGB"))
-                                       cd = RGB;
-                               else if(!strcasecmp(optarg, "YUV"))
-                                       cd = YUV;
-                               else if(!strcasecmp(optarg, "SRGB"))
-                                       cd = SRGB;
-                               else if(!strcasecmp(optarg, "SRGB_MIXED"))
-                                       cd = SRGB_MIXED;
-                               else if(!strcasecmp(optarg, "LAB"))
-                                       cd = LAB;
-                               else if(!strcasecmp(optarg, "AVG"))
-                                       cd = AVG;
-                               else if(!strcasecmp(optarg, "WAVG"))
-                                       cd = WAVG;
-                               else if(!strcasecmp(optarg, "NORMALMAP"))
-                                       cd = NORMALMAP;
-                               else
-                                       return usage(argv[0]);
+                               snprintf(buf, sizeof(buf), "S2TC_COLORDIST_MODE=%s", optarg);
+                               buf[sizeof(buf)-1] = 0;
+                               putenv(buf);
                                break;
                        default:
                                return usage(argv[0]);
@@ -585,18 +570,18 @@ int main(int argc, char **argv)
 
        switch(dxt)
        {
-               case DXT1:
+               case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
                        blocksize = 8;
                        alphabits = 1;
                        fourcc = "DXT1";
                        break;
-               case DXT3:
+               case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
                        blocksize = 16;
                        alphabits = 4;
                        fourcc = "DXT3";
                        break;
                default:
-               case DXT5:
+               case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
                        blocksize = 16;
                        alphabits = 8;
                        fourcc = "DXT5";
@@ -656,18 +641,14 @@ int main(int argc, char **argv)
                fwrite(&zero, 4, 1, outfh);
        }
 
-       unsigned char *opic = (unsigned char *) malloc(image_width * image_height * 4);
        for(;;)
        {
-               rgb565_image(opic, pic, image_width, image_height, 4, 1, alphabits);
-               s2tc_encode_block_func_t encode_block = s2tc_encode_block_func(dxt, cd, nrandom);
-               for(y = 0; y < image_height; y += 4)
-                       for(x = 0; x < image_width; x += 4)
-                       {
-                               unsigned char block[16];
-                               encode_block(block, opic + (x + y * image_width) * 4, image_width, min(4, image_width - x), min(4, image_height - y), nrandom);
-                               fwrite(block, blocksize, 1, outfh);
-                       }
+               int blocks_w = (image_width + 3) / 4;
+               int blocks_h = (image_height + 3) / 4;
+               GLubyte *obuf = (GLubyte *) malloc(blocksize * blocks_w * blocks_h);
+               tx_compress_dxtn(4, image_width, image_height, pic, dxt, obuf, blocks_w * blocksize);
+               fwrite(obuf, blocksize * blocks_w * blocks_h, 1, outfh);
+               free(obuf);
                if(image_width == 1 && image_height == 1)
                        break;
                Image_MipReduce32(pic, pic, &image_width, &image_height, 1, 1);
index a8792cf..2446846 100644 (file)
@@ -1,6 +1,7 @@
 #include "libtxc_dxtn.h"
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 #include "s2tc_compressor.h"
 
 void fetch_2d_texel_rgb_dxt1(GLint srcRowStride, const GLubyte *pixdata,
@@ -121,16 +122,20 @@ void tx_compress_dxtn(GLint srccomps, GLint width, GLint height,
        GLint dstRowDiff;
        unsigned char *rgba = (unsigned char *) malloc(width * height * 4);
        unsigned char *srcaddr;
+       DxtMode dxt;
 
        switch (destFormat) {
                case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
                case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
+                       dxt = DXT1;
                        rgb565_image(rgba, srcPixData, width, height, srccomps, 0, 1);
                        break;
                case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
+                       dxt = DXT3;
                        rgb565_image(rgba, srcPixData, width, height, srccomps, 0, 4);
                        break;
                case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
+                       dxt = DXT5;
                        rgb565_image(rgba, srcPixData, width, height, srccomps, 0, 8);
                        break;
                default:
@@ -139,7 +144,39 @@ void tx_compress_dxtn(GLint srccomps, GLint width, GLint height,
                        return;
        }
 
-       s2tc_encode_block_func_t encode_block;
+       ColorDistMode cd = WAVG;
+       int nrandom = -1;
+       {
+               const char *v = getenv("S2TC_COLORDIST_MODE");
+               if(v)
+               {
+                       if(!strcasecmp(v, "RGB"))
+                               cd = RGB;
+                       else if(!strcasecmp(v, "YUV"))
+                               cd = YUV;
+                       else if(!strcasecmp(v, "SRGB"))
+                               cd = SRGB;
+                       else if(!strcasecmp(v, "SRGB_MIXED"))
+                               cd = SRGB_MIXED;
+                       else if(!strcasecmp(v, "LAB"))
+                               cd = LAB;
+                       else if(!strcasecmp(v, "AVG"))
+                               cd = AVG;
+                       else if(!strcasecmp(v, "WAVG"))
+                               cd = WAVG;
+                       else if(!strcasecmp(v, "NORMALMAP"))
+                               cd = NORMALMAP;
+                       else
+                               fprintf(stderr, "Invalid color dist mode: %s\n", v);
+               }
+       }
+       {
+               const char *v = getenv("S2TC_RANDOM_COLORS");
+               if(v)
+                       nrandom = atoi(v);
+       }
+
+       s2tc_encode_block_func_t encode_block = s2tc_encode_block_func(dxt, cd, nrandom);
        switch (destFormat) {
                case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
                case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
@@ -147,7 +184,6 @@ void tx_compress_dxtn(GLint srccomps, GLint width, GLint height,
                        dstRowDiff = dstRowStride >= (width * 2) ? dstRowStride - (((width + 3) & ~3) * 2) : 0;
                        /*      fprintf(stderr, "dxt1 tex width %d tex height %d dstRowStride %d\n",
                                width, height, dstRowStride); */
-                       encode_block = s2tc_encode_block_func(DXT1, WAVG, -1);
                        for (j = 0; j < height; j += 4) {
                                if (height > j + 3) numypixels = 4;
                                else numypixels = height - j;
@@ -166,7 +202,6 @@ void tx_compress_dxtn(GLint srccomps, GLint width, GLint height,
                        dstRowDiff = dstRowStride >= (width * 4) ? dstRowStride - (((width + 3) & ~3) * 4) : 0;
                        /*      fprintf(stderr, "dxt3 tex width %d tex height %d dstRowStride %d\n",
                                width, height, dstRowStride); */
-                       encode_block = s2tc_encode_block_func(DXT3, WAVG, -1);
                        for (j = 0; j < height; j += 4) {
                                if (height > j + 3) numypixels = 4;
                                else numypixels = height - j;
@@ -185,7 +220,6 @@ void tx_compress_dxtn(GLint srccomps, GLint width, GLint height,
                        dstRowDiff = dstRowStride >= (width * 4) ? dstRowStride - (((width + 3) & ~3) * 4) : 0;
                        /*      fprintf(stderr, "dxt5 tex width %d tex height %d dstRowStride %d\n",
                                width, height, dstRowStride); */
-                       encode_block = s2tc_encode_block_func(DXT5, WAVG, -1);
                        for (j = 0; j < height; j += 4) {
                                if (height > j + 3) numypixels = 4;
                                else numypixels = height - j;