1 | /****************************************************************************** |
---|
2 | * a DXT viewing utility |
---|
3 | * |
---|
4 | * Author : Robert Kooima |
---|
5 | * |
---|
6 | * Copyright (C) 2007 Electronic Visualization Laboratory, |
---|
7 | * University of Illinois at Chicago |
---|
8 | * |
---|
9 | * All rights reserved. |
---|
10 | * |
---|
11 | * Redistribution and use in source and binary forms, with or without |
---|
12 | * modification, are permitted provided that the following conditions are met: |
---|
13 | * |
---|
14 | * * Redistributions of source code must retain the above copyright |
---|
15 | * notice, this list of conditions and the following disclaimer. |
---|
16 | * * Redistributions in binary form must reproduce the above |
---|
17 | * copyright notice, this list of conditions and the following disclaimer |
---|
18 | * in the documentation and/or other materials provided with the distribution. |
---|
19 | * * Neither the name of the University of Illinois at Chicago nor |
---|
20 | * the names of its contributors may be used to endorse or promote |
---|
21 | * products derived from this software without specific prior written permission. |
---|
22 | * |
---|
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
27 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
29 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
30 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
32 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
34 | * |
---|
35 | * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/ |
---|
36 | * |
---|
37 | *****************************************************************************/ |
---|
38 | |
---|
39 | |
---|
40 | #include <SDL/SDL.h> |
---|
41 | #include <stdio.h> |
---|
42 | #include <stdlib.h> |
---|
43 | #include <string.h> |
---|
44 | #include <fcntl.h> |
---|
45 | #include <sys/types.h> |
---|
46 | |
---|
47 | #if !defined(WIN32) |
---|
48 | #include <unistd.h> |
---|
49 | #include <sys/mman.h> |
---|
50 | #endif |
---|
51 | |
---|
52 | #define MAXSTR 256 |
---|
53 | |
---|
54 | #include "dxt.h" |
---|
55 | #include "glsl.h" |
---|
56 | |
---|
57 | #ifdef __APPLE__ |
---|
58 | #include <OpenGL/gl.h> |
---|
59 | #endif |
---|
60 | |
---|
61 | #ifdef __linux__ |
---|
62 | #include <GL/gl.h> |
---|
63 | #include <GL/glx.h> |
---|
64 | #include <GL/glext.h> |
---|
65 | #define glGetProcAddress(n) glXGetProcAddressARB((GLubyte *) n) |
---|
66 | #endif |
---|
67 | |
---|
68 | #ifdef _WIN32 |
---|
69 | #include <windows.h> |
---|
70 | #include <GL/gl.h> |
---|
71 | #include "glext.h" |
---|
72 | #define glGetProcAddress(n) wglGetProcAddress(n) |
---|
73 | #endif |
---|
74 | |
---|
75 | |
---|
76 | /*---------------------------------------------------------------------------*/ |
---|
77 | |
---|
78 | #ifndef GLSL_YCOCG |
---|
79 | #ifndef __APPLE__ |
---|
80 | static PFNGLGETCOMPRESSEDTEXIMAGEARBPROC glGetCompressedTexImage; |
---|
81 | static PFNGLCOMPRESSEDTEXIMAGE2DARBPROC glCompressedTexImage2D; |
---|
82 | #endif |
---|
83 | #endif |
---|
84 | |
---|
85 | static void init_gl(void) |
---|
86 | { |
---|
87 | #ifndef __APPLE__ |
---|
88 | glGetCompressedTexImage = (PFNGLGETCOMPRESSEDTEXIMAGEARBPROC) |
---|
89 | glGetProcAddress("glGetCompressedTexImageARB"); |
---|
90 | glCompressedTexImage2D = (PFNGLCOMPRESSEDTEXIMAGE2DARBPROC) |
---|
91 | glGetProcAddress("glCompressedTexImage2DARB"); |
---|
92 | #endif |
---|
93 | |
---|
94 | glEnable(GL_TEXTURE_2D); |
---|
95 | |
---|
96 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
97 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
---|
98 | } |
---|
99 | |
---|
100 | /*---------------------------------------------------------------------------*/ |
---|
101 | |
---|
102 | |
---|
103 | static void in_dxt(int w, int h, int format, const char *name) |
---|
104 | { |
---|
105 | FILE *fin = NULL; |
---|
106 | static int first = 1; |
---|
107 | |
---|
108 | if ((fin = fopen(name, "rb"))) |
---|
109 | { |
---|
110 | GLubyte *p; |
---|
111 | GLsizei sz; |
---|
112 | |
---|
113 | if (format == 5 || format == 6) |
---|
114 | sz = 4 * w * h / 4; |
---|
115 | else |
---|
116 | sz = 8 * (w / 4) * (h / 4); |
---|
117 | |
---|
118 | if ((p = (GLubyte *) malloc(sz))) |
---|
119 | { |
---|
120 | int ww, hh; |
---|
121 | fread( &ww, sizeof( int ), 1, fin ); |
---|
122 | fread( &hh, sizeof( int ), 1, fin ); |
---|
123 | |
---|
124 | fread(p, 1, sz, fin); |
---|
125 | |
---|
126 | if (format == 5 || format == 6) |
---|
127 | glCompressedTexImage2D(GL_TEXTURE_2D, 0, |
---|
128 | GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, |
---|
129 | w, h, 0, sz, p); |
---|
130 | else |
---|
131 | glCompressedTexImage2D(GL_TEXTURE_2D, 0, |
---|
132 | GL_COMPRESSED_RGB_S3TC_DXT1_EXT, |
---|
133 | w, h, 0, sz, p); |
---|
134 | free(p); |
---|
135 | } |
---|
136 | fclose(fin); |
---|
137 | } |
---|
138 | else perror("fopen"); |
---|
139 | } |
---|
140 | |
---|
141 | /*---------------------------------------------------------------------------*/ |
---|
142 | |
---|
143 | static void display(int w, int h) |
---|
144 | { |
---|
145 | glMatrixMode(GL_PROJECTION); |
---|
146 | glLoadIdentity(); |
---|
147 | glFrustum(0, w, 0, h, 0, 1); |
---|
148 | |
---|
149 | glMatrixMode(GL_MODELVIEW); |
---|
150 | glLoadIdentity(); |
---|
151 | |
---|
152 | glClearColor(0,1,0,1); |
---|
153 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
154 | |
---|
155 | glBegin(GL_POLYGON); |
---|
156 | { |
---|
157 | glTexCoord2i(0, 1); glVertex2i(-1, -1); |
---|
158 | glTexCoord2i(1, 1); glVertex2i(+1, -1); |
---|
159 | glTexCoord2i(1, 0); glVertex2i(+1, +1); |
---|
160 | glTexCoord2i(0, 0); glVertex2i(-1, +1); |
---|
161 | } |
---|
162 | glEnd(); |
---|
163 | |
---|
164 | glFinish(); |
---|
165 | |
---|
166 | SDL_GL_SwapBuffers(); |
---|
167 | } |
---|
168 | |
---|
169 | /*---------------------------------------------------------------------------*/ |
---|
170 | |
---|
171 | int main(int argc, char *argv[]) |
---|
172 | { |
---|
173 | int w = 1024; |
---|
174 | int h = 512; |
---|
175 | int format = 1; // DXT1 or DXT5 or DXT5YCoCg (6) |
---|
176 | int out = 0; |
---|
177 | int argi; |
---|
178 | |
---|
179 | /* Process arguments. */ |
---|
180 | |
---|
181 | for (argi = 1; argi < argc; ++argi) |
---|
182 | if (strcmp(argv[argi], "-5") == 0) format = 5; |
---|
183 | else if (strcmp(argv[argi], "-6") == 0) format = 6; |
---|
184 | else if (strcmp(argv[argi], "-1") == 0) format = 1; |
---|
185 | else if (strcmp(argv[argi], "-w") == 0) w = atoi(argv[++argi]); |
---|
186 | else if (strcmp(argv[argi], "-h") == 0) h = atoi(argv[++argi]); |
---|
187 | else break; |
---|
188 | |
---|
189 | /* Use SDL to get an OpenGL context for use as compressor/decompressor. */ |
---|
190 | |
---|
191 | if (SDL_Init(SDL_INIT_VIDEO) == 0) |
---|
192 | { |
---|
193 | //SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); |
---|
194 | //SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); |
---|
195 | //SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); |
---|
196 | //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); |
---|
197 | //SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
---|
198 | |
---|
199 | if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL)) |
---|
200 | { |
---|
201 | SDL_Event e; |
---|
202 | |
---|
203 | init_gl(); |
---|
204 | |
---|
205 | /* Convert and display named files. */ |
---|
206 | |
---|
207 | #if defined(GLSL_YCOCG) |
---|
208 | // Initialize the "OpenGL Extension Wrangler" library |
---|
209 | glewInit(); |
---|
210 | |
---|
211 | if (!glewIsSupported("GL_VERSION_2_0 " |
---|
212 | "GL_ARB_vertex_program " |
---|
213 | "GL_ARB_fragment_program " |
---|
214 | "GL_ARB_texture_compression " |
---|
215 | "GL_EXT_texture_compression_s3tc " |
---|
216 | ) |
---|
217 | ) { |
---|
218 | fprintf(stderr, "GLSL_YCOCG> Unable to load required OpenGL extension\n"); |
---|
219 | exit(-1); |
---|
220 | } |
---|
221 | |
---|
222 | // Load the shaders |
---|
223 | GLchar *FragmentShaderSource; |
---|
224 | GLchar *VertexShaderSource; |
---|
225 | GLSLreadShaderSource("ycocg", &VertexShaderSource, &FragmentShaderSource); |
---|
226 | PHandle = GLSLinstallShaders(VertexShaderSource, FragmentShaderSource); |
---|
227 | |
---|
228 | /* Finally, use the program. */ |
---|
229 | glUseProgramObjectARB(PHandle); |
---|
230 | |
---|
231 | glUseProgramObjectARB(0); |
---|
232 | #endif |
---|
233 | |
---|
234 | #if defined(GLSL_YCOCG) |
---|
235 | if (format == 6) |
---|
236 | { |
---|
237 | glUseProgramObjectARB(PHandle); |
---|
238 | glActiveTexture(GL_TEXTURE0); |
---|
239 | int h=glGetUniformLocationARB(PHandle,"yuvtex"); |
---|
240 | glUniform1iARB(h,0); /* Bind yuvtex to texture unit 0 */ |
---|
241 | } |
---|
242 | #endif |
---|
243 | |
---|
244 | |
---|
245 | for (; argi < argc; ++argi) |
---|
246 | { |
---|
247 | in_dxt(w, h, format, argv[argi]); |
---|
248 | display(w, h); |
---|
249 | |
---|
250 | while (SDL_PollEvent(&e)) |
---|
251 | if (e.type == SDL_QUIT || e.type == SDL_KEYDOWN) |
---|
252 | break; |
---|
253 | } |
---|
254 | |
---|
255 | #if defined(GLSL_YCOCG) |
---|
256 | if (format == 6) |
---|
257 | glUseProgramObjectARB(0); |
---|
258 | #endif |
---|
259 | |
---|
260 | while (SDL_WaitEvent(&e)) |
---|
261 | if (e.type == SDL_QUIT || e.type == SDL_KEYDOWN) |
---|
262 | break; |
---|
263 | |
---|
264 | } |
---|
265 | else fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); |
---|
266 | |
---|
267 | SDL_Quit(); |
---|
268 | } |
---|
269 | else fprintf(stderr, "SDL_Init: %s\n", SDL_GetError()); |
---|
270 | |
---|
271 | return 0; |
---|
272 | } |
---|
273 | |
---|
274 | /*---------------------------------------------------------------------------*/ |
---|