test_texcomp
changeset 1:ed40258af7ad
added format enumeration and cleaned up the code
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 09 Jan 2018 08:43:16 +0200 |
parents | f941fd7128c5 |
children | 31d30a9c4491 |
files | main.c |
diffstat | 1 files changed, 129 insertions(+), 39 deletions(-) [+] |
line diff
1.1 --- a/main.c Tue Jan 09 08:15:12 2018 +0200 1.2 +++ b/main.c Tue Jan 09 08:43:16 2018 +0200 1.3 @@ -20,6 +20,9 @@ 1.4 void reshape(int x, int y); 1.5 void keyb(unsigned char key, int x, int y); 1.6 void gen_image(unsigned char *pixels, int xsz, int ysz); 1.7 +unsigned char *load_compressed_image(const char *fname, int *cszptr, int *xszptr, int *yszptr); 1.8 +int dump_compressed_image(const char *fname, unsigned char *data, int size, int w, int h); 1.9 +void print_compressed_formats(void); 1.10 1.11 unsigned int tex, comp_tex; 1.12 const char *texfile; 1.13 @@ -57,36 +60,14 @@ 1.14 int ysz = 512; 1.15 int is_comp = 0; 1.16 int comp_size = 0; 1.17 - int start, tmp; 1.18 - FILE *fp; 1.19 + int tmp; 1.20 + 1.21 + print_compressed_formats(); 1.22 1.23 if(texfile) { 1.24 - if(!(fp = fopen(texfile, "rb"))) { 1.25 - fprintf(stderr, "failed to open compressed texture file: %s: %s\n", texfile, strerror(errno)); 1.26 + if(!(pixels = load_compressed_image(texfile, &comp_size, &xsz, &ysz))) { 1.27 return -1; 1.28 } 1.29 - 1.30 - if(fread(&xsz, sizeof xsz, 1, fp) < 1 || fread(&ysz, sizeof ysz, 1, fp) < 1) { 1.31 - fprintf(stderr, "failed to read compressed texture file header: %s: %s\n", texfile, strerror(errno)); 1.32 - fclose(fp); 1.33 - return -1; 1.34 - } 1.35 - start = ftell(fp); 1.36 - fseek(fp, 0, SEEK_END); 1.37 - comp_size = ftell(fp) - start; 1.38 - fseek(fp, start, SEEK_SET); 1.39 - 1.40 - if(!(pixels = malloc(comp_size))) { 1.41 - abort(); 1.42 - } 1.43 - if(fread(pixels, 1, comp_size, fp) < comp_size) { 1.44 - fprintf(stderr, "failed to read compressed texture file: %s: %s\n", texfile, strerror(errno)); 1.45 - fclose(fp); 1.46 - free(pixels); 1.47 - return -1; 1.48 - } 1.49 - fclose(fp); 1.50 - 1.51 printf("loaded compressed texture file: %s (%dx%d)\n", texfile, xsz, ysz); 1.52 1.53 } else { 1.54 @@ -101,17 +82,7 @@ 1.55 } 1.56 printf("compressed texture is %d bytes (uncompressed was: %d)\n", comp_size, xsz * ysz * 3); 1.57 1.58 - /* dump compressed texture */ 1.59 - if(!(fp = fopen("compressed_texture", "wb"))) { 1.60 - fprintf(stderr, "failed to open compressed texture dump file: %s\n", strerror(errno)); 1.61 - } else { 1.62 - if(fwrite(&xsz, sizeof xsz, 1, fp) < 1 || 1.63 - fwrite(&ysz, sizeof ysz, 1, fp) < 1 || 1.64 - fwrite(pixels, 1, comp_size, fp) < comp_size) { 1.65 - fprintf(stderr, "failed to dump compressed texture: %s\n", strerror(errno)); 1.66 - } 1.67 - fclose(fp); 1.68 - } 1.69 + dump_compressed_image("compressed_texture", pixels, comp_size, xsz, ysz); 1.70 } 1.71 1.72 glGenTextures(1, &tex); 1.73 @@ -143,9 +114,9 @@ 1.74 1.75 if(memcmp(pixels, buf, comp_size) != 0) { 1.76 fprintf(stderr, "submitted and retrieved pixel data differ!\n"); 1.77 - return -1; 1.78 + } else { 1.79 + printf("submitted and retrieved sizes match (%d bytes)\n", comp_size); 1.80 } 1.81 - printf("submitted and retrieved sizes match (%d bytes)\n", comp_size); 1.82 free(buf); 1.83 free(pixels); 1.84 1.85 @@ -229,3 +200,122 @@ 1.86 } 1.87 } 1.88 } 1.89 + 1.90 +unsigned char *load_compressed_image(const char *fname, int *cszptr, int *xszptr, int *yszptr) 1.91 +{ 1.92 + unsigned char *pixels; 1.93 + long start, comp_size; 1.94 + int xsz, ysz; 1.95 + FILE *fp; 1.96 + 1.97 + if(!(fp = fopen(texfile, "rb"))) { 1.98 + fprintf(stderr, "failed to open compressed texture file: %s: %s\n", texfile, strerror(errno)); 1.99 + return 0; 1.100 + } 1.101 + 1.102 + if(fread(&xsz, sizeof xsz, 1, fp) < 1 || fread(&ysz, sizeof ysz, 1, fp) < 1) { 1.103 + fprintf(stderr, "failed to read compressed texture file header: %s: %s\n", texfile, strerror(errno)); 1.104 + fclose(fp); 1.105 + return 0; 1.106 + } 1.107 + start = ftell(fp); 1.108 + fseek(fp, 0, SEEK_END); 1.109 + comp_size = ftell(fp) - start; 1.110 + fseek(fp, start, SEEK_SET); 1.111 + 1.112 + if(!(pixels = malloc(comp_size))) { 1.113 + perror("failed to allocate pixel buffer"); 1.114 + return 0; 1.115 + } 1.116 + if(fread(pixels, 1, comp_size, fp) < comp_size) { 1.117 + fprintf(stderr, "failed to read compressed texture file: %s: %s\n", texfile, strerror(errno)); 1.118 + fclose(fp); 1.119 + free(pixels); 1.120 + return 0; 1.121 + } 1.122 + fclose(fp); 1.123 + 1.124 + *cszptr = comp_size; 1.125 + *xszptr = xsz; 1.126 + *yszptr = ysz; 1.127 + return pixels; 1.128 +} 1.129 + 1.130 +int dump_compressed_image(const char *fname, unsigned char *data, int size, int w, int h) 1.131 +{ 1.132 + FILE *fp; 1.133 + 1.134 + if(!(fp = fopen(fname, "wb"))) { 1.135 + fprintf(stderr, "failed to open compressed texture dump file: %s: %s\n", fname, strerror(errno)); 1.136 + return -1; 1.137 + } 1.138 + 1.139 + if(fwrite(&w, sizeof w, 1, fp) < 1 || 1.140 + fwrite(&h, sizeof h, 1, fp) < 1 || 1.141 + fwrite(data, 1, size, fp) < size) { 1.142 + fprintf(stderr, "failed to dump compressed texture: %s\n", strerror(errno)); 1.143 + } 1.144 + fclose(fp); 1.145 + return 0; 1.146 +} 1.147 + 1.148 +const char *fmtstr(int fmt) 1.149 +{ 1.150 + switch(fmt) { 1.151 + case 0x86b0: return "GL_COMPRESSED_RGB_FXT1_3DFX"; 1.152 + case 0x86b1: return "GL_COMPRESSED_RGBA_FXT1_3DFX"; 1.153 + case 0x8dbb: return "GL_COMPRESSED_RED_RGTC1"; 1.154 + case 0x8dbc: return "GL_COMPRESSED_SIGNED_RED_RGTC1"; 1.155 + case 0x8dbd: return "GL_COMPRESSED_RG_RGTC2"; 1.156 + case 0x8dbe: return "GL_COMPRESSED_SIGNED_RG_RGTC2"; 1.157 + case 0x8e8c: return "GL_COMPRESSED_RGBA_BPTC_UNORM"; 1.158 + case 0x8e8d: return "GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM"; 1.159 + case 0x8e8e: return "GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT"; 1.160 + case 0x8e8f: return "GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT"; 1.161 + case 0x9274: return "GL_COMPRESSED_RGB8_ETC2"; 1.162 + case 0x9275: return "GL_COMPRESSED_SRGB8_ETC2"; 1.163 + case 0x9276: return "GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2"; 1.164 + case 0x9277: return "GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2"; 1.165 + case 0x9278: return "GL_COMPRESSED_RGBA8_ETC2_EAC"; 1.166 + case 0x9279: return "GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC"; 1.167 + case 0x9270: return "GL_COMPRESSED_R11_EAC"; 1.168 + case 0x9271: return "GL_COMPRESSED_SIGNED_R11_EAC"; 1.169 + case 0x9272: return "GL_COMPRESSED_RG11_EAC"; 1.170 + case 0x9273: return "GL_COMPRESSED_SIGNED_RG11_EAC"; 1.171 + case 0x83F0: return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT"; 1.172 + case 0x83F1: return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT"; 1.173 + case 0x83F2: return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT"; 1.174 + case 0x83F3: return "GL_COMPRESSED_RGBA_S3TC_DXT5_EXT"; 1.175 + case 0x8C48: return "GL_COMPRESSED_SRGB_EXT"; 1.176 + case 0x8C49: return "GL_COMPRESSED_SRGB_ALPHA_EXT"; 1.177 + case 0x8C4A: return "GL_COMPRESSED_SLUMINANCE_EXT"; 1.178 + case 0x8C4B: return "GL_COMPRESSED_SLUMINANCE_ALPHA_EXT"; 1.179 + case 0x8C4C: return "GL_COMPRESSED_SRGB_S3TC_DXT1_EXT"; 1.180 + case 0x8C4D: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT"; 1.181 + case 0x8C4E: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT"; 1.182 + case 0x8C4F: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT"; 1.183 + default: 1.184 + break; 1.185 + } 1.186 + return "unknown"; 1.187 +} 1.188 + 1.189 +void print_compressed_formats(void) 1.190 +{ 1.191 + int i, num_fmt; 1.192 + int *fmtlist; 1.193 + 1.194 + glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &num_fmt); 1.195 + printf("%d generic compressed texture formats available:\n", num_fmt); 1.196 + 1.197 + if(!(fmtlist = malloc(num_fmt * sizeof *fmtlist))) { 1.198 + fprintf(stderr, "failed to allocate texture formats enumeration buffer\n"); 1.199 + return; 1.200 + } 1.201 + glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, fmtlist); 1.202 + 1.203 + for(i=0; i<num_fmt; i++) { 1.204 + printf(" %05x: %s\n", fmtlist[i], fmtstr(fmtlist[i])); 1.205 + } 1.206 + free(fmtlist); 1.207 +}