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;
31 int main(int argc, char **argv)
33 unsigned int glut_flags = GLUT_RGB | GLUT_DOUBLE;
35 glut_flags |= GLUT_SRGB;
40 glutInit(&argc, argv);
41 glutInitWindowSize(800, 600);
42 glutInitDisplayMode(glut_flags);
43 glutCreateWindow("test");
45 glutDisplayFunc(disp);
46 glutReshapeFunc(reshape);
47 glutKeyboardFunc(keyb);
61 unsigned char *pixels, *buf;
68 print_compressed_formats();
71 if(!(pixels = load_compressed_image(texfile, &comp_size, &xsz, &ysz))) {
74 printf("loaded compressed texture file: %s (%dx%d)\n", texfile, xsz, ysz);
77 if(!(pixels = malloc(xsz * ysz * 3))) {
80 gen_image(pixels, xsz, ysz);
82 printf("compressing texture\n");
83 if((comp_size = texcomp(pixels, COMP_FMT, pixels, xsz, ysz, GL_RGB, GL_UNSIGNED_BYTE)) == -1) {
86 printf("compressed texture is %d bytes (uncompressed was: %d)\n", comp_size, xsz * ysz * 3);
88 dump_compressed_image("compressed_texture", pixels, comp_size, xsz, ysz);
91 glGenTextures(1, &tex);
92 glBindTexture(GL_TEXTURE_2D, tex);
93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
94 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
95 glCompressedTexImage2D(GL_TEXTURE_2D, 0, COMP_FMT, xsz, ysz, 0, comp_size, pixels);
97 fprintf(stderr, "failed to upload compressed texture\n");
101 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp);
103 fprintf(stderr, "texture is not compressed\n");
106 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &tmp);
107 if(tmp != comp_size) {
108 fprintf(stderr, "internal compressed size differs (expected: %d, got: %d)!\n", comp_size, tmp);
112 if(!(buf = malloc(comp_size))) {
113 fprintf(stderr, "failed to allocate comparison image buffer (%d bytes)\n", comp_size);
116 glGetCompressedTexImage(GL_TEXTURE_2D, 0, buf);
118 if(memcmp(pixels, buf, comp_size) != 0) {
119 fprintf(stderr, "submitted and retrieved pixel data differ!\n");
121 printf("submitted and retrieved sizes match (%d bytes)\n", comp_size);
127 glEnable(GL_FRAMEBUFFER_SRGB);
130 glEnable(GL_TEXTURE_2D);
134 int texcomp(unsigned char *compix, unsigned int tofmt, unsigned char *pixels,
135 int xsz, int ysz, unsigned int fromfmt, unsigned int fromtype)
141 glGenTextures(1, &tex);
142 glBindTexture(GL_TEXTURE_2D, tex);
143 glTexImage2D(GL_TEXTURE_2D, 0, tofmt, xsz, ysz, 0, fromfmt, fromtype, pixels);
145 fprintf(stderr, "failed to compress texture\n");
149 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp);
151 fprintf(stderr, "texture is not compressed\n");
154 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &comp_size);
156 glGetCompressedTexImage(GL_TEXTURE_2D, 0, compix);
162 glClear(GL_COLOR_BUFFER_BIT);
165 glTexCoord2f(0, 1); glVertex2f(-1, -1);
166 glTexCoord2f(1, 1); glVertex2f(1, -1);
167 glTexCoord2f(1, 0); glVertex2f(1, 1);
168 glTexCoord2f(0, 0); glVertex2f(-1, 1);
172 assert(glGetError() == GL_NO_ERROR);
175 void reshape(int x, int y)
177 float aspect = (float)x / (float)y;
178 glViewport(0, 0, x, y);
180 glMatrixMode(GL_PROJECTION);
182 glScalef(1.0 / aspect, 1, 1);
185 void keyb(unsigned char key, int x, int y)
192 void gen_image(unsigned char *pixels, int xsz, int ysz)
196 for(i=0; i<ysz; i++) {
197 for(j=0; j<xsz; j++) {
200 *pixels++ = xor & 0xff;
201 *pixels++ = (xor << 1) & 0xff;
202 *pixels++ = (xor << 2) & 0xff;
207 unsigned char *load_compressed_image(const char *fname, int *cszptr, int *xszptr, int *yszptr)
209 unsigned char *pixels;
210 long start, comp_size;
214 if(!(fp = fopen(texfile, "rb"))) {
215 fprintf(stderr, "failed to open compressed texture file: %s: %s\n", texfile, strerror(errno));
219 if(fread(&xsz, sizeof xsz, 1, fp) < 1 || fread(&ysz, sizeof ysz, 1, fp) < 1) {
220 fprintf(stderr, "failed to read compressed texture file header: %s: %s\n", texfile, strerror(errno));
225 fseek(fp, 0, SEEK_END);
226 comp_size = ftell(fp) - start;
227 fseek(fp, start, SEEK_SET);
229 if(!(pixels = malloc(comp_size))) {
230 perror("failed to allocate pixel buffer");
233 if(fread(pixels, 1, comp_size, fp) < comp_size) {
234 fprintf(stderr, "failed to read compressed texture file: %s: %s\n", texfile, strerror(errno));
247 int dump_compressed_image(const char *fname, unsigned char *data, int size, int w, int h)
251 if(!(fp = fopen(fname, "wb"))) {
252 fprintf(stderr, "failed to open compressed texture dump file: %s: %s\n", fname, strerror(errno));
256 if(fwrite(&w, sizeof w, 1, fp) < 1 ||
257 fwrite(&h, sizeof h, 1, fp) < 1 ||
258 fwrite(data, 1, size, fp) < size) {
259 fprintf(stderr, "failed to dump compressed texture: %s\n", strerror(errno));
265 const char *fmtstr(int fmt)
268 case 0x86b0: return "GL_COMPRESSED_RGB_FXT1_3DFX";
269 case 0x86b1: return "GL_COMPRESSED_RGBA_FXT1_3DFX";
270 case 0x8dbb: return "GL_COMPRESSED_RED_RGTC1";
271 case 0x8dbc: return "GL_COMPRESSED_SIGNED_RED_RGTC1";
272 case 0x8dbd: return "GL_COMPRESSED_RG_RGTC2";
273 case 0x8dbe: return "GL_COMPRESSED_SIGNED_RG_RGTC2";
274 case 0x8e8c: return "GL_COMPRESSED_RGBA_BPTC_UNORM";
275 case 0x8e8d: return "GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM";
276 case 0x8e8e: return "GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT";
277 case 0x8e8f: return "GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT";
278 case 0x9274: return "GL_COMPRESSED_RGB8_ETC2";
279 case 0x9275: return "GL_COMPRESSED_SRGB8_ETC2";
280 case 0x9276: return "GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2";
281 case 0x9277: return "GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2";
282 case 0x9278: return "GL_COMPRESSED_RGBA8_ETC2_EAC";
283 case 0x9279: return "GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC";
284 case 0x9270: return "GL_COMPRESSED_R11_EAC";
285 case 0x9271: return "GL_COMPRESSED_SIGNED_R11_EAC";
286 case 0x9272: return "GL_COMPRESSED_RG11_EAC";
287 case 0x9273: return "GL_COMPRESSED_SIGNED_RG11_EAC";
288 case 0x83F0: return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT";
289 case 0x83F1: return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT";
290 case 0x83F2: return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT";
291 case 0x83F3: return "GL_COMPRESSED_RGBA_S3TC_DXT5_EXT";
292 case 0x8C48: return "GL_COMPRESSED_SRGB_EXT";
293 case 0x8C49: return "GL_COMPRESSED_SRGB_ALPHA_EXT";
294 case 0x8C4A: return "GL_COMPRESSED_SLUMINANCE_EXT";
295 case 0x8C4B: return "GL_COMPRESSED_SLUMINANCE_ALPHA_EXT";
296 case 0x8C4C: return "GL_COMPRESSED_SRGB_S3TC_DXT1_EXT";
297 case 0x8C4D: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT";
298 case 0x8C4E: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT";
299 case 0x8C4F: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT";
300 case 0x8B90: return "GL_PALETTE4_RGB8_OES";
301 case 0x8B91: return "GL_PALETTE4_RGBA8_OES";
302 case 0x8B92: return "GL_PALETTE4_R5_G6_B5_OES";
303 case 0x8B93: return "GL_PALETTE4_RGBA4_OES";
304 case 0x8B94: return "GL_PALETTE4_RGB5_A1_OES";
305 case 0x8B95: return "GL_PALETTE8_RGB8_OES";
306 case 0x8B96: return "GL_PALETTE8_RGBA8_OES";
307 case 0x8B97: return "GL_PALETTE8_R5_G6_B5_OES";
308 case 0x8B98: return "GL_PALETTE8_RGBA4_OES";
309 case 0x8B99: return "GL_PALETTE8_RGB5_A1_OES";
310 case 0x93B0: return "GL_COMPRESSED_RGBA_ASTC_4";
311 case 0x93B1: return "GL_COMPRESSED_RGBA_ASTC_5";
312 case 0x93B2: return "GL_COMPRESSED_RGBA_ASTC_5";
313 case 0x93B3: return "GL_COMPRESSED_RGBA_ASTC_6";
314 case 0x93B4: return "GL_COMPRESSED_RGBA_ASTC_6";
315 case 0x93B5: return "GL_COMPRESSED_RGBA_ASTC_8";
316 case 0x93B6: return "GL_COMPRESSED_RGBA_ASTC_8";
317 case 0x93B7: return "GL_COMPRESSED_RGBA_ASTC_8";
318 case 0x93B8: return "GL_COMPRESSED_RGBA_ASTC_10";
319 case 0x93B9: return "GL_COMPRESSED_RGBA_ASTC_10";
320 case 0x93BA: return "GL_COMPRESSED_RGBA_ASTC_10";
321 case 0x93BB: return "GL_COMPRESSED_RGBA_ASTC_10";
322 case 0x93BC: return "GL_COMPRESSED_RGBA_ASTC_12";
323 case 0x93BD: return "GL_COMPRESSED_RGBA_ASTC_12";
324 case 0x93D0: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4";
325 case 0x93D1: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5";
326 case 0x93D2: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5";
327 case 0x93D3: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6";
328 case 0x93D4: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6";
329 case 0x93D5: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8";
330 case 0x93D6: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8";
331 case 0x93D7: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8";
332 case 0x93D8: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
333 case 0x93D9: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
334 case 0x93DA: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
335 case 0x93DB: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
336 case 0x93DC: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12";
337 case 0x93DD: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12";
344 void print_compressed_formats(void)
349 glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &num_fmt);
350 printf("%d generic compressed texture formats available:\n", num_fmt);
352 if(!(fmtlist = malloc(num_fmt * sizeof *fmtlist))) {
353 fprintf(stderr, "failed to allocate texture formats enumeration buffer\n");
356 glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, fmtlist);
358 for(i=0; i<num_fmt; i++) {
360 printf(" %05x: %s\n", fmtlist[i], fmtstr(fmtlist[i]));
362 glGetInternalformativ(GL_TEXTURE_2D, fmtlist[i], GL_TEXTURE_COMPRESSED, 1, ¶ms);
363 printf("the format is %s\n", params == GL_TRUE ? "compressed" : "not compressed");