[4] | 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 | #define strdup(x) _strdup(x) |
---|
| 49 | #else |
---|
| 50 | #include <unistd.h> |
---|
| 51 | #include <sys/mman.h> |
---|
| 52 | #endif |
---|
| 53 | |
---|
| 54 | #define MAXSTR 256 |
---|
| 55 | |
---|
| 56 | #include "dxt.h" |
---|
| 57 | #include "glsl.h" |
---|
| 58 | |
---|
| 59 | #ifdef __APPLE__ |
---|
| 60 | #include <OpenGL/gl.h> |
---|
| 61 | #endif |
---|
| 62 | |
---|
| 63 | #ifdef __linux__ |
---|
| 64 | #include <GL/gl.h> |
---|
| 65 | #include <GL/glx.h> |
---|
| 66 | #include <GL/glext.h> |
---|
| 67 | #define glGetProcAddress(n) glXGetProcAddressARB((GLubyte *) n) |
---|
| 68 | #endif |
---|
| 69 | |
---|
| 70 | #ifdef _WIN32 |
---|
| 71 | #include <windows.h> |
---|
| 72 | #include <GL/gl.h> |
---|
| 73 | #include "glext.h" |
---|
| 74 | #define glGetProcAddress(n) wglGetProcAddress(n) |
---|
| 75 | #endif |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | /*---------------------------------------------------------------------------*/ |
---|
| 79 | |
---|
| 80 | byte *in_data; |
---|
| 81 | byte *dxt_data; |
---|
| 82 | byte *ref_data; |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | /*---------------------------------------------------------------------------*/ |
---|
| 86 | |
---|
| 87 | #ifndef GLSL_YCOCG |
---|
| 88 | #ifndef __APPLE__ |
---|
| 89 | static PFNGLGETCOMPRESSEDTEXIMAGEARBPROC glGetCompressedTexImage; |
---|
| 90 | static PFNGLCOMPRESSEDTEXIMAGE2DARBPROC glCompressedTexImage2D; |
---|
| 91 | #endif |
---|
| 92 | #endif |
---|
| 93 | |
---|
| 94 | static void init_gl(void) |
---|
| 95 | { |
---|
| 96 | #ifndef __APPLE__ |
---|
| 97 | glGetCompressedTexImage = (PFNGLGETCOMPRESSEDTEXIMAGEARBPROC) |
---|
| 98 | glGetProcAddress("glGetCompressedTexImageARB"); |
---|
| 99 | glCompressedTexImage2D = (PFNGLCOMPRESSEDTEXIMAGE2DARBPROC) |
---|
| 100 | glGetProcAddress("glCompressedTexImage2DARB"); |
---|
| 101 | #endif |
---|
| 102 | |
---|
| 103 | glEnable(GL_TEXTURE_2D); |
---|
| 104 | |
---|
| 105 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
| 106 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /*---------------------------------------------------------------------------*/ |
---|
| 110 | |
---|
| 111 | static void out_rgb(int w, int h, const char *name) |
---|
| 112 | { |
---|
| 113 | double e; |
---|
| 114 | FILE *fout = NULL; |
---|
| 115 | |
---|
| 116 | if ((fout = fopen(name, "wb"))) |
---|
| 117 | { |
---|
| 118 | GLsizei sz = 3 * w * h; |
---|
| 119 | GLubyte *p; |
---|
| 120 | |
---|
| 121 | if ((p = (GLubyte *) malloc(sz))) |
---|
| 122 | { |
---|
| 123 | glReadPixels( 0,0, w, h, GL_RGB, GL_UNSIGNED_BYTE, p ); |
---|
| 124 | |
---|
| 125 | dxt_data = p; |
---|
| 126 | |
---|
| 127 | fwrite(p, 1, sz, fout); |
---|
| 128 | |
---|
| 129 | //free(p); |
---|
| 130 | |
---|
| 131 | // Compute error |
---|
| 132 | e = ComputeError(ref_data, dxt_data, w, h); |
---|
| 133 | fprintf(stdout, "RMS Error %.4f\n", e); |
---|
| 134 | } |
---|
| 135 | fclose(fout); |
---|
| 136 | } |
---|
| 137 | else perror("fopen"); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | static void in_dxt(int w, int h, int format, const char *name) |
---|
| 142 | { |
---|
| 143 | FILE *fin = NULL; |
---|
| 144 | |
---|
| 145 | if ((fin = fopen(name, "rb"))) |
---|
| 146 | { |
---|
| 147 | GLubyte *p; |
---|
| 148 | GLsizei sz; |
---|
| 149 | |
---|
| 150 | if (format == 5 || format == 6) |
---|
| 151 | sz = 4 * w * h / 4; |
---|
| 152 | else |
---|
| 153 | sz = 8 * (w / 4) * (h / 4); |
---|
| 154 | |
---|
| 155 | if ((p = (GLubyte *) malloc(sz))) |
---|
| 156 | { |
---|
| 157 | int ww, hh; |
---|
| 158 | fread( &ww, sizeof( int ), 1, fin ); |
---|
| 159 | fread( &hh, sizeof( int ), 1, fin ); |
---|
| 160 | |
---|
| 161 | fread(p, 1, sz, fin); |
---|
| 162 | |
---|
| 163 | if (format == 5 || format == 6) |
---|
| 164 | glCompressedTexImage2D(GL_TEXTURE_2D, 0, |
---|
| 165 | GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, |
---|
| 166 | w, h, 0, sz, p); |
---|
| 167 | else |
---|
| 168 | glCompressedTexImage2D(GL_TEXTURE_2D, 0, |
---|
| 169 | GL_COMPRESSED_RGB_S3TC_DXT1_EXT, |
---|
| 170 | w, h, 0, sz, p); |
---|
| 171 | //free(p); |
---|
| 172 | in_data = p; |
---|
| 173 | } |
---|
| 174 | fclose(fin); |
---|
| 175 | } |
---|
| 176 | else perror("fopen"); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /*---------------------------------------------------------------------------*/ |
---|
| 180 | |
---|
| 181 | static void in_rgb(int w, int h, const char *name) |
---|
| 182 | { |
---|
| 183 | FILE *fin = NULL; |
---|
| 184 | |
---|
| 185 | if ((fin = fopen(name, "rb"))) |
---|
| 186 | { |
---|
| 187 | GLubyte *p; |
---|
| 188 | GLsizei sz; |
---|
| 189 | |
---|
| 190 | sz = 4 * w * h; |
---|
| 191 | |
---|
| 192 | if ((p = (GLubyte *) malloc(sz))) |
---|
| 193 | { |
---|
| 194 | memset(p, 0, sz); |
---|
| 195 | |
---|
| 196 | fread(p, 1, sz, fin); |
---|
| 197 | |
---|
| 198 | ref_data = p; |
---|
| 199 | } |
---|
| 200 | fclose(fin); |
---|
| 201 | } |
---|
| 202 | else perror("fopen"); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /*---------------------------------------------------------------------------*/ |
---|
| 206 | |
---|
| 207 | static void display(int w, int h) |
---|
| 208 | { |
---|
| 209 | glMatrixMode(GL_PROJECTION); |
---|
| 210 | glLoadIdentity(); |
---|
| 211 | glFrustum(0, w, 0, h, 0, 1); |
---|
| 212 | |
---|
| 213 | glMatrixMode(GL_MODELVIEW); |
---|
| 214 | glLoadIdentity(); |
---|
| 215 | |
---|
| 216 | glClearColor(0,1,0,0); |
---|
| 217 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 218 | |
---|
| 219 | glBegin(GL_POLYGON); |
---|
| 220 | { |
---|
| 221 | glTexCoord2i(0, 1); glVertex2i(-1, -1); |
---|
| 222 | glTexCoord2i(1, 1); glVertex2i(+1, -1); |
---|
| 223 | glTexCoord2i(1, 0); glVertex2i(+1, +1); |
---|
| 224 | glTexCoord2i(0, 0); glVertex2i(-1, +1); |
---|
| 225 | } |
---|
| 226 | glEnd(); |
---|
| 227 | |
---|
| 228 | SDL_GL_SwapBuffers(); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | /*---------------------------------------------------------------------------*/ |
---|
| 232 | |
---|
| 233 | int main(int argc, char *argv[]) |
---|
| 234 | { |
---|
| 235 | int w = 1024; |
---|
| 236 | int h = 512; |
---|
| 237 | int p = 1; |
---|
| 238 | int format = 1; // DXT1 or DXT5 or DXT5YCoCg (6) |
---|
| 239 | int out = 0; |
---|
| 240 | int argi; |
---|
| 241 | char *rgbfile; |
---|
| 242 | |
---|
| 243 | /* Process arguments. */ |
---|
| 244 | |
---|
| 245 | for (argi = 1; argi < argc; ++argi) |
---|
| 246 | if (strcmp(argv[argi], "-p") == 0) p = 0; |
---|
| 247 | else if (strcmp(argv[argi], "-5") == 0) format = 5; |
---|
| 248 | else if (strcmp(argv[argi], "-6") == 0) format = 6; |
---|
| 249 | else if (strcmp(argv[argi], "-1") == 0) format = 1; |
---|
| 250 | else if (strcmp(argv[argi], "-o") == 0) out = 1; |
---|
| 251 | else if (strcmp(argv[argi], "-r") == 0) rgbfile = strdup(argv[++argi]); |
---|
| 252 | else if (strcmp(argv[argi], "-w") == 0) w = atoi(argv[++argi]); |
---|
| 253 | else if (strcmp(argv[argi], "-h") == 0) h = atoi(argv[++argi]); |
---|
| 254 | else break; |
---|
| 255 | |
---|
| 256 | /* Use SDL to get an OpenGL context for use as compressor/decompressor. */ |
---|
| 257 | |
---|
| 258 | if (SDL_Init(SDL_INIT_VIDEO) == 0) |
---|
| 259 | { |
---|
| 260 | SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); |
---|
| 261 | SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); |
---|
| 262 | SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); |
---|
| 263 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); |
---|
| 264 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
---|
| 265 | |
---|
| 266 | if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL)) |
---|
| 267 | { |
---|
| 268 | SDL_Event e; |
---|
| 269 | |
---|
| 270 | init_gl(); |
---|
| 271 | |
---|
| 272 | /* Convert and display named files. */ |
---|
| 273 | |
---|
| 274 | #if defined(GLSL_YCOCG) |
---|
| 275 | // Initialize the "OpenGL Extension Wrangler" library |
---|
| 276 | glewInit(); |
---|
| 277 | |
---|
| 278 | if (!glewIsSupported("GL_VERSION_2_0 " |
---|
| 279 | "GL_ARB_vertex_program " |
---|
| 280 | "GL_ARB_fragment_program " |
---|
| 281 | "GL_ARB_texture_compression " |
---|
| 282 | "GL_EXT_texture_compression_s3tc " |
---|
| 283 | ) |
---|
| 284 | ) { |
---|
| 285 | fprintf(stderr, "GLSL_YCOCG> Unable to load required OpenGL extension\n"); |
---|
| 286 | exit(-1); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | // Load the shaders |
---|
| 290 | GLchar *FragmentShaderSource; |
---|
| 291 | GLchar *VertexShaderSource; |
---|
| 292 | GLSLreadShaderSource("ycocg", &VertexShaderSource, &FragmentShaderSource); |
---|
| 293 | PHandle = GLSLinstallShaders(VertexShaderSource, FragmentShaderSource); |
---|
| 294 | |
---|
| 295 | /* Finally, use the program. */ |
---|
| 296 | glUseProgramObjectARB(PHandle); |
---|
| 297 | |
---|
| 298 | glUseProgramObjectARB(0); |
---|
| 299 | #endif |
---|
| 300 | |
---|
| 301 | in_dxt(w, h, format, argv[argi]); |
---|
| 302 | |
---|
| 303 | #if defined(GLSL_YCOCG) |
---|
| 304 | if (format == 6) |
---|
| 305 | { |
---|
| 306 | glUseProgramObjectARB(PHandle); |
---|
| 307 | glActiveTexture(GL_TEXTURE0); |
---|
| 308 | int h=glGetUniformLocationARB(PHandle,"yuvtex"); |
---|
| 309 | glUniform1iARB(h,0); /* Bind yuvtex to texture unit 0 */ |
---|
| 310 | } |
---|
| 311 | #endif |
---|
| 312 | |
---|
| 313 | display(w, h); |
---|
| 314 | |
---|
| 315 | #if defined(GLSL_YCOCG) |
---|
| 316 | if (format == 6) |
---|
| 317 | glUseProgramObjectARB(0); |
---|
| 318 | #endif |
---|
| 319 | |
---|
| 320 | if (out) |
---|
| 321 | { |
---|
| 322 | in_rgb(w, h, rgbfile); // ref_data |
---|
| 323 | out_rgb(w, h, "out.rgb"); // dxt_data |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | /* Pause until the user closes the window. */ |
---|
| 327 | |
---|
| 328 | if (p) |
---|
| 329 | { |
---|
| 330 | while (SDL_WaitEvent(&e)) |
---|
| 331 | if (e.type == SDL_QUIT || e.type == SDL_KEYDOWN) |
---|
| 332 | break; |
---|
| 333 | else { |
---|
| 334 | //display(w, h); |
---|
| 335 | } |
---|
| 336 | } |
---|
| 337 | } |
---|
| 338 | else fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); |
---|
| 339 | |
---|
| 340 | SDL_Quit(); |
---|
| 341 | } |
---|
| 342 | else fprintf(stderr, "SDL_Init: %s\n", SDL_GetError()); |
---|
| 343 | |
---|
| 344 | return 0; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | /*---------------------------------------------------------------------------*/ |
---|