7 #include <GL/freeglut.h>
12 #define COMP_FMT GL_COMPRESSED_SRGB8_ETC2
14 #define COMP_FMT GL_COMPRESSED_RGB8_ETC2
18 int texcomp(unsigned char *compix, unsigned int tofmt, unsigned char *pixels,
19 int xsz, int ysz, unsigned int fromfmt, unsigned int fromtype);
21 void reshape(int x, int y);
22 void keyb(unsigned char key, int x, int y);
23 void gen_image(unsigned char *pixels, int xsz, int ysz);
24 unsigned char *load_compressed_image(const char *fname, int *cszptr, int *xszptr, int *yszptr);
25 int dump_compressed_image(const char *fname, unsigned char *data, int size, int w, int h);
26 void print_compressed_formats(void);
28 unsigned int tex, comp_tex;
32 int main(int argc, char **argv)
35 unsigned int glut_flags = GLUT_RGB | GLUT_DOUBLE;
37 glut_flags |= GLUT_SRGB;
40 for(i=1; i<argc; i++) {
41 if(argv[i][0] == '-') {
42 if(strcmp(argv[i], "-subtest") == 0) {
45 fprintf(stderr, "invalid option: %s\n", argv[i]);
50 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
57 glutInit(&argc, argv);
58 glutInitWindowSize(800, 600);
59 glutInitDisplayMode(glut_flags);
60 glutCreateWindow("test");
62 glutDisplayFunc(disp);
63 glutReshapeFunc(reshape);
64 glutKeyboardFunc(keyb);
78 unsigned char *pixels, *buf;
85 print_compressed_formats();
88 if(!(pixels = load_compressed_image(texfile, &comp_size, &xsz, &ysz))) {
91 printf("loaded compressed texture file: %s (%dx%d)\n", texfile, xsz, ysz);
94 if(!(pixels = malloc(xsz * ysz * 3))) {
97 gen_image(pixels, xsz, ysz);
99 printf("compressing texture\n");
100 if((comp_size = texcomp(pixels, COMP_FMT, pixels, xsz, ysz, GL_RGB, GL_UNSIGNED_BYTE)) == -1) {
103 printf("compressed texture is %d bytes (uncompressed was: %d)\n", comp_size, xsz * ysz * 3);
105 dump_compressed_image("compressed_texture", pixels, comp_size, xsz, ysz);
108 glGenTextures(1, &tex);
109 glBindTexture(GL_TEXTURE_2D, tex);
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
112 glCompressedTexImage2D(GL_TEXTURE_2D, 0, COMP_FMT, xsz, ysz, 0, comp_size, pixels);
114 fprintf(stderr, "failed to upload compressed texture\n");
118 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp);
120 fprintf(stderr, "texture is not compressed\n");
123 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &tmp);
124 if(tmp != comp_size) {
125 fprintf(stderr, "internal compressed size differs (expected: %d, got: %d)!\n", comp_size, tmp);
129 if(!(buf = malloc(comp_size))) {
130 fprintf(stderr, "failed to allocate comparison image buffer (%d bytes)\n", comp_size);
133 glGetCompressedTexImage(GL_TEXTURE_2D, 0, buf);
135 if(memcmp(pixels, buf, comp_size) != 0) {
136 fprintf(stderr, "submitted and retrieved pixel data differ!\n");
138 printf("submitted and retrieved sizes match (%d bytes)\n", comp_size);
142 memset(buf, 0, comp_size);
143 glGetCompressedTextureSubImage(tex, 0, 192, 64, 0, 64, 64, 1, comp_size, buf);
144 glCompressedTexSubImage2D(GL_TEXTURE_2D, 0, 32, 32, 64, 64, COMP_FMT, 2048, buf);
151 glEnable(GL_FRAMEBUFFER_SRGB);
154 glEnable(GL_TEXTURE_2D);
158 int texcomp(unsigned char *compix, unsigned int tofmt, unsigned char *pixels,
159 int xsz, int ysz, unsigned int fromfmt, unsigned int fromtype)
165 glGenTextures(1, &tex);
166 glBindTexture(GL_TEXTURE_2D, tex);
167 glTexImage2D(GL_TEXTURE_2D, 0, tofmt, xsz, ysz, 0, fromfmt, fromtype, pixels);
169 fprintf(stderr, "failed to compress texture\n");
173 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp);
175 fprintf(stderr, "texture is not compressed\n");
178 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &comp_size);
180 glGetCompressedTexImage(GL_TEXTURE_2D, 0, compix);
186 glClear(GL_COLOR_BUFFER_BIT);
189 glTexCoord2f(0, 1); glVertex2f(-1, -1);
190 glTexCoord2f(1, 1); glVertex2f(1, -1);
191 glTexCoord2f(1, 0); glVertex2f(1, 1);
192 glTexCoord2f(0, 0); glVertex2f(-1, 1);
196 assert(glGetError() == GL_NO_ERROR);
199 void reshape(int x, int y)
201 float aspect = (float)x / (float)y;
202 glViewport(0, 0, x, y);
204 glMatrixMode(GL_PROJECTION);
206 glScalef(1.0 / aspect, 1, 1);
209 void keyb(unsigned char key, int x, int y)
216 void gen_image(unsigned char *pixels, int xsz, int ysz)
220 for(i=0; i<ysz; i++) {
221 for(j=0; j<xsz; j++) {
224 *pixels++ = xor & 0xff;
225 *pixels++ = (xor << 1) & 0xff;
226 *pixels++ = (xor << 2) & 0xff;
231 unsigned char *load_compressed_image(const char *fname, int *cszptr, int *xszptr, int *yszptr)
233 unsigned char *pixels;
234 long start, comp_size;
238 if(!(fp = fopen(texfile, "rb"))) {
239 fprintf(stderr, "failed to open compressed texture file: %s: %s\n", texfile, strerror(errno));
243 if(fread(&xsz, sizeof xsz, 1, fp) < 1 || fread(&ysz, sizeof ysz, 1, fp) < 1) {
244 fprintf(stderr, "failed to read compressed texture file header: %s: %s\n", texfile, strerror(errno));
249 fseek(fp, 0, SEEK_END);
250 comp_size = ftell(fp) - start;
251 fseek(fp, start, SEEK_SET);
253 if(!(pixels = malloc(comp_size))) {
254 perror("failed to allocate pixel buffer");
257 if(fread(pixels, 1, comp_size, fp) < comp_size) {
258 fprintf(stderr, "failed to read compressed texture file: %s: %s\n", texfile, strerror(errno));
271 int dump_compressed_image(const char *fname, unsigned char *data, int size, int w, int h)
275 if(!(fp = fopen(fname, "wb"))) {
276 fprintf(stderr, "failed to open compressed texture dump file: %s: %s\n", fname, strerror(errno));
280 if(fwrite(&w, sizeof w, 1, fp) < 1 ||
281 fwrite(&h, sizeof h, 1, fp) < 1 ||
282 fwrite(data, 1, size, fp) < size) {
283 fprintf(stderr, "failed to dump compressed texture: %s\n", strerror(errno));
289 const char *fmtstr(int fmt)
292 case 0x86b0: return "GL_COMPRESSED_RGB_FXT1_3DFX";
293 case 0x86b1: return "GL_COMPRESSED_RGBA_FXT1_3DFX";
294 case 0x8dbb: return "GL_COMPRESSED_RED_RGTC1";
295 case 0x8dbc: return "GL_COMPRESSED_SIGNED_RED_RGTC1";
296 case 0x8dbd: return "GL_COMPRESSED_RG_RGTC2";
297 case 0x8dbe: return "GL_COMPRESSED_SIGNED_RG_RGTC2";
298 case 0x8e8c: return "GL_COMPRESSED_RGBA_BPTC_UNORM";
299 case 0x8e8d: return "GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM";
300 case 0x8e8e: return "GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT";
301 case 0x8e8f: return "GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT";
302 case 0x9274: return "GL_COMPRESSED_RGB8_ETC2";
303 case 0x9275: return "GL_COMPRESSED_SRGB8_ETC2";
304 case 0x9276: return "GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2";
305 case 0x9277: return "GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2";
306 case 0x9278: return "GL_COMPRESSED_RGBA8_ETC2_EAC";
307 case 0x9279: return "GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC";
308 case 0x9270: return "GL_COMPRESSED_R11_EAC";
309 case 0x9271: return "GL_COMPRESSED_SIGNED_R11_EAC";
310 case 0x9272: return "GL_COMPRESSED_RG11_EAC";
311 case 0x9273: return "GL_COMPRESSED_SIGNED_RG11_EAC";
312 case 0x83F0: return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT";
313 case 0x83F1: return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT";
314 case 0x83F2: return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT";
315 case 0x83F3: return "GL_COMPRESSED_RGBA_S3TC_DXT5_EXT";
316 case 0x8C48: return "GL_COMPRESSED_SRGB_EXT";
317 case 0x8C49: return "GL_COMPRESSED_SRGB_ALPHA_EXT";
318 case 0x8C4A: return "GL_COMPRESSED_SLUMINANCE_EXT";
319 case 0x8C4B: return "GL_COMPRESSED_SLUMINANCE_ALPHA_EXT";
320 case 0x8C4C: return "GL_COMPRESSED_SRGB_S3TC_DXT1_EXT";
321 case 0x8C4D: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT";
322 case 0x8C4E: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT";
323 case 0x8C4F: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT";
324 case 0x8B90: return "GL_PALETTE4_RGB8_OES";
325 case 0x8B91: return "GL_PALETTE4_RGBA8_OES";
326 case 0x8B92: return "GL_PALETTE4_R5_G6_B5_OES";
327 case 0x8B93: return "GL_PALETTE4_RGBA4_OES";
328 case 0x8B94: return "GL_PALETTE4_RGB5_A1_OES";
329 case 0x8B95: return "GL_PALETTE8_RGB8_OES";
330 case 0x8B96: return "GL_PALETTE8_RGBA8_OES";
331 case 0x8B97: return "GL_PALETTE8_R5_G6_B5_OES";
332 case 0x8B98: return "GL_PALETTE8_RGBA4_OES";
333 case 0x8B99: return "GL_PALETTE8_RGB5_A1_OES";
334 case 0x93B0: return "GL_COMPRESSED_RGBA_ASTC_4";
335 case 0x93B1: return "GL_COMPRESSED_RGBA_ASTC_5";
336 case 0x93B2: return "GL_COMPRESSED_RGBA_ASTC_5";
337 case 0x93B3: return "GL_COMPRESSED_RGBA_ASTC_6";
338 case 0x93B4: return "GL_COMPRESSED_RGBA_ASTC_6";
339 case 0x93B5: return "GL_COMPRESSED_RGBA_ASTC_8";
340 case 0x93B6: return "GL_COMPRESSED_RGBA_ASTC_8";
341 case 0x93B7: return "GL_COMPRESSED_RGBA_ASTC_8";
342 case 0x93B8: return "GL_COMPRESSED_RGBA_ASTC_10";
343 case 0x93B9: return "GL_COMPRESSED_RGBA_ASTC_10";
344 case 0x93BA: return "GL_COMPRESSED_RGBA_ASTC_10";
345 case 0x93BB: return "GL_COMPRESSED_RGBA_ASTC_10";
346 case 0x93BC: return "GL_COMPRESSED_RGBA_ASTC_12";
347 case 0x93BD: return "GL_COMPRESSED_RGBA_ASTC_12";
348 case 0x93D0: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4";
349 case 0x93D1: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5";
350 case 0x93D2: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5";
351 case 0x93D3: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6";
352 case 0x93D4: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6";
353 case 0x93D5: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8";
354 case 0x93D6: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8";
355 case 0x93D7: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8";
356 case 0x93D8: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
357 case 0x93D9: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
358 case 0x93DA: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
359 case 0x93DB: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
360 case 0x93DC: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12";
361 case 0x93DD: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12";
368 void print_compressed_formats(void)
373 glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &num_fmt);
374 printf("%d generic compressed texture formats available:\n", num_fmt);
376 if(!(fmtlist = malloc(num_fmt * sizeof *fmtlist))) {
377 fprintf(stderr, "failed to allocate texture formats enumeration buffer\n");
380 glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, fmtlist);
382 for(i=0; i<num_fmt; i++) {
384 printf(" %05x: %s\n", fmtlist[i], fmtstr(fmtlist[i]));
386 glGetInternalformativ(GL_TEXTURE_2D, fmtlist[i], GL_TEXTURE_COMPRESSED, 1, ¶ms);
387 printf("the format is %s\n", params == GL_TRUE ? "compressed" : "not compressed");