Android-x86
Fork
Donation

  • R/O
  • HTTP
  • SSH
  • HTTPS

external-s2tc: Commit

external/s2tc


Commit MetaInfo

Revision1050adc641764b03480b700ed902c2417de89e50 (tree)
Time2015-12-20 08:34:28
AuthorRudolf Polzer <divverent@xono...>
CommiterRudolf Polzer

Log Message

Convert s2tc_compress and s2tc_decompress tools from C++ to C.

Change Summary

Incremental Difference

--- a/Makefile.am
+++ b/Makefile.am
@@ -7,8 +7,8 @@ endif
77 if ENABLE_TOOLS
88 bin_PROGRAMS = s2tc_compress s2tc_decompress s2tc_from_s3tc
99 s2tc_from_s3tc_SOURCES = s2tc_from_s3tc.cpp s2tc_license.h
10-s2tc_compress_SOURCES = s2tc_compress.cpp txc_dxtn.h s2tc_license.h
11-s2tc_decompress_SOURCES = s2tc_decompress.cpp txc_dxtn.h s2tc_license.h
10+s2tc_compress_SOURCES = s2tc_compress.c txc_dxtn.h s2tc_license.h
11+s2tc_decompress_SOURCES = s2tc_decompress.c txc_dxtn.h s2tc_license.h
1212 if ENABLE_RUNTIME_LINKING
1313 s2tc_compress_LDADD = $(LIBDL_LDADD)
1414 s2tc_decompress_LDADD = $(LIBDL_LDADD)
--- a/s2tc_compress.cpp
+++ b/s2tc_compress.c
@@ -21,25 +21,22 @@
2121 #define S2TC_LICENSE_IDENTIFIER s2tc_compress_license
2222 #include "s2tc_license.h"
2323
24+#include <getopt.h>
25+#include <stdbool.h>
26+#include <stdint.h>
2427 #include <stdio.h>
2528 #include <stdlib.h>
2629 #include <string.h>
27-#include <stdint.h>
28-#include <getopt.h>
29-#include <algorithm>
30-#include "s2tc_common.h"
30+#include <strings.h>
3131
3232 #ifdef ENABLE_RUNTIME_LINKING
3333 #include <dlfcn.h>
3434 #include <GL/gl.h>
35-extern "C"
36-{
37- typedef void (tx_compress_dxtn_t)(GLint srccomps, GLint width, GLint height,
38- const GLubyte *srcPixData, GLenum destformat,
39- GLubyte *dest, GLint dstRowStride);
40-};
35+typedef void (tx_compress_dxtn_t)(GLint srccomps, GLint width, GLint height,
36+ const GLubyte *srcPixData, GLenum destformat,
37+ GLubyte *dest, GLint dstRowStride);
4138 tx_compress_dxtn_t *tx_compress_dxtn = NULL;
42-inline bool load_libraries(const char *n)
39+bool load_libraries(const char *n)
4340 {
4441 void *l = dlopen(n, RTLD_NOW);
4542 if(!l)
@@ -57,10 +54,7 @@ inline bool load_libraries(const char *n)
5754 return true;
5855 }
5956 #else
60-extern "C"
61-{
6257 #include "txc_dxtn.h"
63-};
6458 #endif
6559
6660 /* START stuff that originates from image.c in DarkPlaces */
@@ -429,13 +423,14 @@ unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
429423 return image_buffer;
430424 }
431425
432-// in can be the same as out
426+/* in can be the same as out */
433427 void Image_MipReduce32(const unsigned char *in, unsigned char *out, int *width, int *height, int destwidth, int destheight)
434428 {
435429 const unsigned char *inrow;
436430 int x, y, nextrow;
437- // note: if given odd width/height this discards the last row/column of
438- // pixels, rather than doing a proper box-filter scale down
431+ /* note: if given odd width/height this discards the last row/column of
432+ * pixels, rather than doing a proper box-filter scale down
433+ */
439434 inrow = in;
440435 nextrow = *width * 4;
441436 if (*width > destwidth)
@@ -443,7 +438,7 @@ void Image_MipReduce32(const unsigned char *in, unsigned char *out, int *width,
443438 *width >>= 1;
444439 if (*height > destheight)
445440 {
446- // reduce both
441+ /* reduce both */
447442 *height >>= 1;
448443 for (y = 0;y < *height;y++, inrow += nextrow * 2)
449444 {
@@ -460,7 +455,7 @@ void Image_MipReduce32(const unsigned char *in, unsigned char *out, int *width,
460455 }
461456 else
462457 {
463- // reduce width
458+ /* reduce width */
464459 for (y = 0;y < *height;y++, inrow += nextrow)
465460 {
466461 for (in = inrow, x = 0;x < *width;x++)
@@ -479,7 +474,7 @@ void Image_MipReduce32(const unsigned char *in, unsigned char *out, int *width,
479474 {
480475 if (*height > destheight)
481476 {
482- // reduce height
477+ /* reduce height */
483478 *height >>= 1;
484479 for (y = 0;y < *height;y++, inrow += nextrow * 2)
485480 {
@@ -570,8 +565,13 @@ int usage(const char *me)
570565
571566 int main(int argc, char **argv)
572567 {
573- GLenum dxt = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
574568 const char *infile = NULL, *outfile = NULL;
569+ FILE *outfh;
570+ unsigned char *picdata, *pic;
571+ int x, mipcount, piclen;
572+ const char *fourcc;
573+ int blocksize;
574+ GLenum dxt = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
575575
576576 #ifdef ENABLE_RUNTIME_LINKING
577577 const char *library = "libtxc_dxtn.so";
@@ -617,31 +617,31 @@ int main(int argc, char **argv)
617617 return 1;
618618 #endif
619619
620- FILE *outfh = outfile ? fopen(outfile, "wb") : stdout;
620+ outfh = outfile ? fopen(outfile, "wb") : stdout;
621621 if(!outfh)
622622 {
623623 printf("opening output failed\n");
624624 return 2;
625625 }
626626
627- int piclen;
628- unsigned char *picdata = FS_LoadFile(infile, &piclen);
627+ picdata = FS_LoadFile(infile, &piclen);
629628 if(!picdata)
630629 {
631630 printf("FS_LoadFile failed\n");
632631 return 2;
633632 }
634633
635- unsigned char *pic = LoadTGA_BGRA(picdata, piclen);
636- for(int x = 0; x < image_width*image_height; ++x)
637- std::swap(pic[4*x], pic[4*x+2]);
638- int mipcount = 0;
634+ pic = LoadTGA_BGRA(picdata, piclen);
635+ for(x = 0; x < image_width*image_height; ++x) {
636+ unsigned char h = pic[4*x];
637+ pic[4*x] = pic[4*x+2];
638+ pic[4*x+2] = h;
639+ }
640+ mipcount = 0;
639641 while(image_width >= (1 << mipcount) || image_height >= (1 << mipcount))
640642 ++mipcount;
641- // now, (1 << mipcount) >= width, height
643+ /* now, (1 << mipcount) >= width, height */
642644
643- const char *fourcc;
644- int blocksize;
645645 switch(dxt)
646646 {
647647 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
@@ -660,22 +660,25 @@ int main(int argc, char **argv)
660660 }
661661
662662 {
663+ uint32_t zero = LittleLong(0);
663664 bool alphapixels = false;
664- for(int y = 0; y < image_height; ++y)
665- for(int x = 0; x < image_width; ++x)
665+ int x, y;
666+ uint32_t dds_picsize, dds_mipcount, dds_width, dds_height;
667+
668+ for(y = 0; y < image_height; ++y)
669+ for(x = 0; x < image_width; ++x)
666670 if(pic[(y*image_width+x)*4+3] != 255)
667671 {
668672 alphapixels = true;
669673 break;
670674 }
671675
672- uint32_t zero = LittleLong(0);
673- uint32_t dds_picsize = LittleLong(((image_width+3)/4) * ((image_height+3)/4) * blocksize);
674- uint32_t dds_mipcount = LittleLong(mipcount);
675- uint32_t dds_width = LittleLong(image_width);
676- uint32_t dds_height = LittleLong(image_height);
676+ dds_picsize = LittleLong(((image_width+3)/4) * ((image_height+3)/4) * blocksize);
677+ dds_mipcount = LittleLong(mipcount);
678+ dds_width = LittleLong(image_width);
679+ dds_height = LittleLong(image_height);
677680
678- //0
681+ /* 0 */
679682 fwrite("DDS ", 4, 1, outfh);
680683 fwrite("\x7c\x00\x00\x00", 4, 1, outfh);
681684 fwrite("\x07\x10\x0a\x00", 4, 1, outfh);
@@ -685,7 +688,7 @@ int main(int argc, char **argv)
685688 fwrite(&zero, 4, 1, outfh);
686689 fwrite(&dds_mipcount, 4, 1, outfh);
687690
688- //32
691+ /* 32 */
689692 fwrite(&zero, 4, 1, outfh);
690693 fwrite(&zero, 4, 1, outfh);
691694 fwrite(&zero, 4, 1, outfh);
@@ -695,7 +698,7 @@ int main(int argc, char **argv)
695698 fwrite(&zero, 4, 1, outfh);
696699 fwrite(&zero, 4, 1, outfh);
697700
698- //64
701+ /* 64 */
699702 fwrite(&zero, 4, 1, outfh);
700703 fwrite(&zero, 4, 1, outfh);
701704 fwrite(&zero, 4, 1, outfh);
@@ -705,7 +708,7 @@ int main(int argc, char **argv)
705708 fwrite(&zero, 4, 1, outfh);
706709 fwrite(&zero, 4, 1, outfh);
707710
708- //96
711+ /* 96 */
709712 fwrite(&zero, 4, 1, outfh);
710713 fwrite(&zero, 4, 1, outfh);
711714 fwrite(&zero, 4, 1, outfh);
--- a/s2tc_decompress.cpp
+++ b/s2tc_decompress.c
@@ -24,30 +24,26 @@
2424 #include <stdio.h>
2525 #include <stdlib.h>
2626 #include <string.h>
27+#include <stdbool.h>
2728 #include <stdint.h>
2829 #include <getopt.h>
29-#include <algorithm>
30-#include "s2tc_common.h"
3130
3231 #ifdef ENABLE_RUNTIME_LINKING
3332 #include <dlfcn.h>
3433 #include <GL/gl.h>
35-extern "C"
36-{
37- typedef void (fetch_2d_texel_rgb_dxt1_t)(GLint srcRowStride, const GLubyte *pixdata,
38- GLint i, GLint j, GLvoid *texel);
39- typedef void (fetch_2d_texel_rgba_dxt1_t)(GLint srcRowStride, const GLubyte *pixdata,
34+typedef void (fetch_2d_texel_rgb_dxt1_t)(GLint srcRowStride, const GLubyte *pixdata,
35+ GLint i, GLint j, GLvoid *texel);
36+typedef void (fetch_2d_texel_rgba_dxt1_t)(GLint srcRowStride, const GLubyte *pixdata,
37+ GLint i, GLint j, GLvoid *texel);
38+typedef void (fetch_2d_texel_rgba_dxt3_t)(GLint srcRowStride, const GLubyte *pixdata,
39+ GLint i, GLint j, GLvoid *texel);
40+typedef void (fetch_2d_texel_rgba_dxt5_t)(GLint srcRowStride, const GLubyte *pixdata,
4041 GLint i, GLint j, GLvoid *texel);
41- typedef void (fetch_2d_texel_rgba_dxt3_t)(GLint srcRowStride, const GLubyte *pixdata,
42- GLint i, GLint j, GLvoid *texel);
43- typedef void (fetch_2d_texel_rgba_dxt5_t)(GLint srcRowStride, const GLubyte *pixdata,
44- GLint i, GLint j, GLvoid *texel);
45-};
4642 fetch_2d_texel_rgb_dxt1_t *fetch_2d_texel_rgb_dxt1 = NULL;
4743 fetch_2d_texel_rgba_dxt1_t *fetch_2d_texel_rgba_dxt1 = NULL;
4844 fetch_2d_texel_rgba_dxt3_t *fetch_2d_texel_rgba_dxt3 = NULL;
4945 fetch_2d_texel_rgba_dxt5_t *fetch_2d_texel_rgba_dxt5 = NULL;
50-inline bool load_libraries(const char *n)
46+bool load_libraries(const char *n)
5147 {
5248 void *l = dlopen(n, RTLD_NOW);
5349 if(!l)
@@ -68,10 +64,7 @@ inline bool load_libraries(const char *n)
6864 return true;
6965 }
7066 #else
71-extern "C"
72-{
7367 #include "txc_dxtn.h"
74-};
7568 #endif
7669
7770 uint32_t LittleLong(uint32_t w)
@@ -106,6 +99,11 @@ int usage(const char *me)
10699 int main(int argc, char **argv)
107100 {
108101 const char *infile = NULL, *outfile = NULL;
102+ FILE *infh, *outfh;
103+ uint32_t h[32];
104+ int x, y, width, height, n;
105+ unsigned char t[18];
106+ unsigned char *buf;
109107
110108 #ifdef ENABLE_RUNTIME_LINKING
111109 const char *library = "libtxc_dxtn.so";
@@ -141,24 +139,23 @@ int main(int argc, char **argv)
141139 return 1;
142140 #endif
143141
144- FILE *infh = infile ? fopen(infile, "rb") : stdin;
142+ infh = infile ? fopen(infile, "rb") : stdin;
145143 if(!infh)
146144 {
147145 printf("opening input failed\n");
148146 return 2;
149147 }
150148
151- FILE *outfh = outfile ? fopen(outfile, "wb") : stdout;
149+ outfh = outfile ? fopen(outfile, "wb") : stdout;
152150 if(!outfh)
153151 {
154152 printf("opening output failed\n");
155153 return 2;
156154 }
157155
158- uint32_t h[32];
159156 fread(h, sizeof(h), 1, infh);
160- int height = LittleLong(h[3]);
161- int width = LittleLong(h[4]);
157+ height = LittleLong(h[3]);
158+ width = LittleLong(h[4]);
162159
163160 void (*fetch)(GLint srcRowStride, const GLubyte *pixdata, GLint i, GLint j, GLvoid *texel) = NULL;
164161 int fourcc = LittleLong(h[21]);
@@ -182,7 +179,6 @@ int main(int argc, char **argv)
182179 return 1;
183180 }
184181
185- unsigned char t[18];
186182 memset(t, 0, 18);
187183 t[2] = 2;
188184 t[12] = width % 256;
@@ -193,17 +189,19 @@ int main(int argc, char **argv)
193189 t[17] = 0x28;
194190 fwrite(t, 18, 1, outfh);
195191
196- int n = ((width + 3) / 4) * ((height + 3) / 4);
197- unsigned char *buf = (unsigned char *) malloc(n * blocksize);
192+ n = ((width + 3) / 4) * ((height + 3) / 4);
193+ buf = (unsigned char *) malloc(n * blocksize);
198194 fread(buf, blocksize, n, infh);
199195
200- int x, y;
201196 for(y = 0; y < height; ++y)
202197 for(x = 0; x < width; ++x)
203198 {
204199 char data[4];
200+ char h;
205201 fetch(width, buf, x, y, &data);
206- std::swap(data[0], data[2]);
202+ h = data[0];
203+ data[0] = data[2];
204+ data[2] = h;
207205 fwrite(data, 4, 1, outfh);
208206 }
209207
--- a/s2tc_license.h
+++ b/s2tc_license.h
@@ -1,10 +1,11 @@
1-// A helper source file to include the license text into the shared library
2-//
3-// We do this to ensure that the only condition of the MIT license is
4-// automatically fulfilled without another program author doing anything
5-// special.
1+/* A helper source file to include the license text into the shared library
2+ *
3+ * We do this to ensure that the only condition of the MIT license is
4+ * automatically fulfilled without another program author doing anything
5+ * special.
6+ */
67
7-// NOTE: this is NOT static! We WANT to create an external symbol of this.
8+/* NOTE: this is NOT static! We WANT to create an external symbol of this. */
89 const char *S2TC_LICENSE_IDENTIFIER =
910 "Copyright (C) 2011 Rudolf Polzer All Rights Reserved.\n"
1011 "\n"
Show on old repository browser