OSDN Git Service

more fix for test.sh
[android-x86/external-s2tc.git] / s2tc_decompress.cpp
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <algorithm>
6
7 extern "C"
8 {
9 #include "txc_dxtn.h"
10 };
11
12 uint32_t LittleLong(uint32_t w)
13 {
14         union
15         {
16                 unsigned char c[4];
17                 uint32_t u;
18         }
19         un;
20         un.c[0] = w;
21         un.c[1] = w >> 8;
22         un.c[2] = w >> 16;
23         un.c[3] = w >> 24;
24         return un.u;
25 }
26
27 int main()
28 {
29         uint32_t h[32];
30         fread(h, sizeof(h), 1, stdin);
31         int height = LittleLong(h[3]);
32         int width = LittleLong(h[4]);
33         int fourcc = LittleLong(h[21]);
34         void (*fetch)(GLint srcRowStride, const GLubyte *pixdata, GLint i, GLint j, GLvoid *texel) = NULL;
35         int blocksize;
36
37         switch(fourcc)
38         {
39                 case 0x31545844:
40                         fetch = fetch_2d_texel_rgba_dxt1;
41                         blocksize = 8;
42                         break;
43                 case 0x33545844:
44                         fetch = fetch_2d_texel_rgba_dxt3;
45                         blocksize = 16;
46                         break;
47                 case 0x35545844:
48                         fetch = fetch_2d_texel_rgba_dxt5;
49                         blocksize = 16;
50                         break;
51                 default:
52                         fprintf(stderr, "Only DXT1, DXT3, DXT5 are supported!\n");
53                         return 1;
54         }
55
56         unsigned char t[18];
57         memset(t, 0, 18);
58         t[2]  = 2;
59         t[12] = width % 256;
60         t[13] = width / 256;
61         t[14] = height % 256;
62         t[15] = height / 256;
63         t[16] = 32;
64         t[17] = 0x28;
65         fwrite(t, 18, 1, stdout);
66
67         int n = ((width + 3) / 4) * ((height + 3) / 4);
68         unsigned char *buf = (unsigned char *) malloc(n * blocksize);
69         fread(buf, blocksize, n, stdin);
70
71         int x, y;
72         for(y = 0; y < height; ++y)
73                 for(x = 0; x < width; ++x)
74                 {
75                         char data[4];
76                         fetch(width, buf, x, y, &data);
77                         std::swap(data[0], data[2]);
78                         fwrite(data, 4, 1, stdout);
79                 }
80         return 0;
81 }