2 * Copyright (C) 2011 Rudolf Polzer All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * RUDOLF POLZER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 #define S2TC_LICENSE_IDENTIFIER s2tc_decompress_license
22 #include "s2tc_license.h"
30 #ifdef ENABLE_RUNTIME_LINKING
35 typedef void (fetch_2d_texel_rgb_dxt1_t)(GLint srcRowStride, const GLubyte *pixdata,
36 GLint i, GLint j, GLvoid *texel);
37 typedef void (fetch_2d_texel_rgba_dxt1_t)(GLint srcRowStride, const GLubyte *pixdata,
38 GLint i, GLint j, GLvoid *texel);
39 typedef void (fetch_2d_texel_rgba_dxt3_t)(GLint srcRowStride, const GLubyte *pixdata,
40 GLint i, GLint j, GLvoid *texel);
41 typedef void (fetch_2d_texel_rgba_dxt5_t)(GLint srcRowStride, const GLubyte *pixdata,
42 GLint i, GLint j, GLvoid *texel);
44 fetch_2d_texel_rgb_dxt1_t *fetch_2d_texel_rgb_dxt1 = NULL;
45 fetch_2d_texel_rgba_dxt1_t *fetch_2d_texel_rgba_dxt1 = NULL;
46 fetch_2d_texel_rgba_dxt3_t *fetch_2d_texel_rgba_dxt3 = NULL;
47 fetch_2d_texel_rgba_dxt5_t *fetch_2d_texel_rgba_dxt5 = NULL;
48 inline bool load_libraries()
50 void *l = dlopen("libtxc_dxtn.so", RTLD_NOW);
53 fprintf(stderr, "Cannot load library: %s\n", dlerror());
56 fetch_2d_texel_rgb_dxt1 = (fetch_2d_texel_rgb_dxt1_t *) dlsym(l, "fetch_2d_texel_rgb_dxt1");
57 fetch_2d_texel_rgba_dxt1 = (fetch_2d_texel_rgba_dxt1_t *) dlsym(l, "fetch_2d_texel_rgba_dxt1");
58 fetch_2d_texel_rgba_dxt3 = (fetch_2d_texel_rgba_dxt3_t *) dlsym(l, "fetch_2d_texel_rgba_dxt3");
59 fetch_2d_texel_rgba_dxt5 = (fetch_2d_texel_rgba_dxt5_t *) dlsym(l, "fetch_2d_texel_rgba_dxt5");
60 if(!fetch_2d_texel_rgb_dxt1 || !fetch_2d_texel_rgba_dxt1 || !fetch_2d_texel_rgba_dxt3 || !fetch_2d_texel_rgba_dxt5)
62 fprintf(stderr, "The selected libtxc_dxtn.so does not contain all required symbols.");
73 inline bool load_libraries()
79 uint32_t LittleLong(uint32_t w)
97 fread(h, sizeof(h), 1, stdin);
98 int height = LittleLong(h[3]);
99 int width = LittleLong(h[4]);
100 int fourcc = LittleLong(h[21]);
101 void (*fetch)(GLint srcRowStride, const GLubyte *pixdata, GLint i, GLint j, GLvoid *texel) = NULL;
104 if(!load_libraries())
110 fetch = fetch_2d_texel_rgba_dxt1;
114 fetch = fetch_2d_texel_rgba_dxt3;
118 fetch = fetch_2d_texel_rgba_dxt5;
122 fprintf(stderr, "Only DXT1, DXT3, DXT5 are supported!\n");
131 t[14] = height % 256;
132 t[15] = height / 256;
135 fwrite(t, 18, 1, stdout);
137 int n = ((width + 3) / 4) * ((height + 3) / 4);
138 unsigned char *buf = (unsigned char *) malloc(n * blocksize);
139 fread(buf, blocksize, n, stdin);
142 for(y = 0; y < height; ++y)
143 for(x = 0; x < width; ++x)
146 fetch(width, buf, x, y, &data);
147 std::swap(data[0], data[2]);
148 fwrite(data, 4, 1, stdout);