OSDN Git Service

s2tc_decompress: do proper option parsing too
authorRudolf Polzer <divverent@xonotic.org>
Tue, 19 Jul 2011 10:34:31 +0000 (12:34 +0200)
committerRudolf Polzer <divverent@xonotic.org>
Tue, 19 Jul 2011 10:38:50 +0000 (12:38 +0200)
s2tc_compress.cpp
s2tc_decompress.cpp

index 6e80ac3..ed228ca 100644 (file)
@@ -21,7 +21,6 @@
 #define S2TC_LICENSE_IDENTIFIER s2tc_compress_license
 #include "s2tc_license.h"
 
-#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -569,13 +568,9 @@ int usage(const char *me)
 
 int main(int argc, char **argv)
 {
-       unsigned char *pic, *picdata;
-       int piclen;
-       const char *fourcc;
-       int blocksize;
        GLenum dxt = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
        const char *infile = NULL, *outfile = NULL;
-       FILE *outfh;
+
 #ifdef ENABLE_RUNTIME_LINKING
        const char *library = "libtxc_dxtn.so";
 #endif
@@ -620,29 +615,31 @@ int main(int argc, char **argv)
                return 1;
 #endif
 
-       outfh = outfile ? fopen(outfile, "wb") : stdout;
+       FILE *outfh = outfile ? fopen(outfile, "wb") : stdout;
        if(!outfh)
        {
                printf("opening output failed\n");
                return 2;
        }
 
-       picdata = FS_LoadFile(infile, &piclen);
+       int piclen;
+       unsigned char *picdata = FS_LoadFile(infile, &piclen);
        if(!picdata)
        {
                printf("FS_LoadFile failed\n");
                return 2;
        }
-       pic = LoadTGA_BGRA(picdata, piclen);
 
+       unsigned char *pic = LoadTGA_BGRA(picdata, piclen);
        for(int x = 0; x < image_width*image_height; ++x)
                std::swap(pic[4*x], pic[4*x+2]);
-
        int mipcount = 0;
        while(image_width >= (1 << mipcount) || image_height >= (1 << mipcount))
                ++mipcount;
        // now, (1 << mipcount) >= width, height
 
+       const char *fourcc;
+       int blocksize;
        switch(dxt)
        {
                case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
index f42291e..35cbf1b 100644 (file)
 #include "s2tc_license.h"
 
 #include <stdio.h>
-#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdint.h>
+#include <getopt.h>
 #include <algorithm>
+#include "s2tc_common.h"
 
 #ifdef ENABLE_RUNTIME_LINKING
 #include <dlfcn.h>
@@ -45,9 +47,9 @@ fetch_2d_texel_rgb_dxt1_t *fetch_2d_texel_rgb_dxt1 = NULL;
 fetch_2d_texel_rgba_dxt1_t *fetch_2d_texel_rgba_dxt1 = NULL;
 fetch_2d_texel_rgba_dxt3_t *fetch_2d_texel_rgba_dxt3 = NULL;
 fetch_2d_texel_rgba_dxt5_t *fetch_2d_texel_rgba_dxt5 = NULL;
-inline bool load_libraries()
+inline bool load_libraries(const char *n)
 {
-       void *l = dlopen("libtxc_dxtn.so", RTLD_NOW);
+       void *l = dlopen(n, RTLD_NOW);
        if(!l)
        {
                fprintf(stderr, "Cannot load library: %s\n", dlerror());
@@ -70,10 +72,6 @@ extern "C"
 {
 #include "txc_dxtn.h"
 };
-inline bool load_libraries()
-{
-       return true;
-}
 #endif
 
 uint32_t LittleLong(uint32_t w)
@@ -91,19 +89,80 @@ uint32_t LittleLong(uint32_t w)
        return un.u;
 }
 
-int main()
+int usage(const char *me)
+{
+       fprintf(stderr, "usage:\n"
+                       "%s \n"
+                       "    [-i infile.tga]\n"
+                       "    [-o outfile.dds]\n"
+#ifdef ENABLE_RUNTIME_LINKING
+                       "    [-l path_to_libtxc_dxtn.so]\n"
+#endif
+                       ,
+                       me);
+       return 1;
+}
+
+int main(int argc, char **argv)
 {
+       const char *infile = NULL, *outfile = NULL;
+
+#ifdef ENABLE_RUNTIME_LINKING
+       const char *library = "libtxc_dxtn.so";
+#endif
+
+       int opt;
+       while((opt = getopt(argc, argv, "i:o:"
+#ifdef ENABLE_RUNTIME_LINKING
+                                       "l:"
+#endif
+                                       )) != -1)
+       {
+               switch(opt)
+               {
+                       case 'i':
+                               infile = optarg;
+                               break;
+                       case 'o':
+                               outfile = optarg;
+                               break;
+#ifdef ENABLE_RUNTIME_LINKING
+                       case 'l':
+                               library = optarg;
+                               break;
+#endif
+                       default:
+                               return usage(argv[0]);
+                               break;
+               }
+       }
+#ifdef ENABLE_RUNTIME_LINKING
+       if(!load_libraries(library))
+               return 1;
+#endif
+
+       FILE *infh = outfile ? fopen(outfile, "rb") : stdin;
+       if(!infh)
+       {
+               printf("opening input failed\n");
+               return 2;
+       }
+
+       FILE *outfh = outfile ? fopen(outfile, "wb") : stdout;
+       if(!outfh)
+       {
+               printf("opening output failed\n");
+               return 2;
+       }
+
        uint32_t h[32];
-       fread(h, sizeof(h), 1, stdin);
+       fread(h, sizeof(h), 1, infh);
        int height = LittleLong(h[3]);
        int width = LittleLong(h[4]);
-       int fourcc = LittleLong(h[21]);
+
        void (*fetch)(GLint srcRowStride, const GLubyte *pixdata, GLint i, GLint j, GLvoid *texel) = NULL;
+       int fourcc = LittleLong(h[21]);
        int blocksize;
-
-       if(!load_libraries())
-               return 1;
-
        switch(fourcc)
        {
                case 0x31545844:
@@ -132,11 +191,11 @@ int main()
        t[15] = height / 256;
        t[16] = 32;
        t[17] = 0x28;
-       fwrite(t, 18, 1, stdout);
+       fwrite(t, 18, 1, outfh);
 
        int n = ((width + 3) / 4) * ((height + 3) / 4);
        unsigned char *buf = (unsigned char *) malloc(n * blocksize);
-       fread(buf, blocksize, n, stdin);
+       fread(buf, blocksize, n, infh);
 
        int x, y;
        for(y = 0; y < height; ++y)
@@ -145,7 +204,13 @@ int main()
                        char data[4];
                        fetch(width, buf, x, y, &data);
                        std::swap(data[0], data[2]);
-                       fwrite(data, 4, 1, stdout);
+                       fwrite(data, 4, 1, outfh);
                }
+
+       if(infile)
+               fclose(infh);
+       if(outfile)
+               fclose(outfh);
+
        return 0;
 }