test_texcomp

changeset 0:f941fd7128c5

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 09 Jan 2018 08:15:12 +0200
parents
children ed40258af7ad
files Makefile main.c
diffstat 2 files changed, 243 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Makefile	Tue Jan 09 08:15:12 2018 +0200
     1.3 @@ -0,0 +1,12 @@
     1.4 +obj = main.o
     1.5 +bin = test
     1.6 +
     1.7 +CFLAGS = -pedantic -Wall -g
     1.8 +LDFLAGS = -lGL -lglut
     1.9 +
    1.10 +$(bin): $(obj)
    1.11 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    1.12 +
    1.13 +.PHONY: clean
    1.14 +clean:
    1.15 +	rm -f $(obj) $(bin)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/main.c	Tue Jan 09 08:15:12 2018 +0200
     2.3 @@ -0,0 +1,231 @@
     2.4 +#include <stdio.h>
     2.5 +#include <stdlib.h>
     2.6 +#include <string.h>
     2.7 +#include <errno.h>
     2.8 +#include <assert.h>
     2.9 +#include <GL/freeglut.h>
    2.10 +
    2.11 +#undef USE_SRGB
    2.12 +
    2.13 +#ifdef USE_SRGB
    2.14 +#define COMP_FMT	GL_COMPRESSED_SRGB8_ETC2
    2.15 +#else
    2.16 +#define COMP_FMT	GL_COMPRESSED_RGB8_ETC2
    2.17 +#endif
    2.18 +
    2.19 +int init(void);
    2.20 +int texcomp(unsigned char *compix, unsigned int tofmt, unsigned char *pixels,
    2.21 +		int xsz, int ysz, unsigned int fromfmt, unsigned int fromtype);
    2.22 +void disp(void);
    2.23 +void reshape(int x, int y);
    2.24 +void keyb(unsigned char key, int x, int y);
    2.25 +void gen_image(unsigned char *pixels, int xsz, int ysz);
    2.26 +
    2.27 +unsigned int tex, comp_tex;
    2.28 +const char *texfile;
    2.29 +
    2.30 +int main(int argc, char **argv)
    2.31 +{
    2.32 +	unsigned int glut_flags = GLUT_RGB | GLUT_DOUBLE;
    2.33 +#ifdef USE_SRGB
    2.34 +	glut_flags |= GLUT_SRGB;
    2.35 +#endif
    2.36 +
    2.37 +	texfile = argv[1];
    2.38 +
    2.39 +	glutInit(&argc, argv);
    2.40 +	glutInitWindowSize(800, 600);
    2.41 +	glutInitDisplayMode(glut_flags);
    2.42 +	glutCreateWindow("test");
    2.43 +
    2.44 +	glutDisplayFunc(disp);
    2.45 +	glutReshapeFunc(reshape);
    2.46 +	glutKeyboardFunc(keyb);
    2.47 +
    2.48 +	if(init() == -1) {
    2.49 +		return 1;
    2.50 +	}
    2.51 +
    2.52 +	glutMainLoop();
    2.53 +	return 0;
    2.54 +}
    2.55 +
    2.56 +int init(void)
    2.57 +{
    2.58 +	unsigned char *pixels, *buf;
    2.59 +	int xsz = 512;
    2.60 +	int ysz = 512;
    2.61 +	int is_comp = 0;
    2.62 +	int comp_size = 0;
    2.63 +	int start, tmp;
    2.64 +	FILE *fp;
    2.65 +
    2.66 +	if(texfile) {
    2.67 +		if(!(fp = fopen(texfile, "rb"))) {
    2.68 +			fprintf(stderr, "failed to open compressed texture file: %s: %s\n", texfile, strerror(errno));
    2.69 +			return -1;
    2.70 +		}
    2.71 +
    2.72 +		if(fread(&xsz, sizeof xsz, 1, fp) < 1 || fread(&ysz, sizeof ysz, 1, fp) < 1) {
    2.73 +			fprintf(stderr, "failed to read compressed texture file header: %s: %s\n", texfile, strerror(errno));
    2.74 +			fclose(fp);
    2.75 +			return -1;
    2.76 +		}
    2.77 +		start = ftell(fp);
    2.78 +		fseek(fp, 0, SEEK_END);
    2.79 +		comp_size = ftell(fp) - start;
    2.80 +		fseek(fp, start, SEEK_SET);
    2.81 +
    2.82 +		if(!(pixels = malloc(comp_size))) {
    2.83 +			abort();
    2.84 +		}
    2.85 +		if(fread(pixels, 1, comp_size, fp) < comp_size) {
    2.86 +			fprintf(stderr, "failed to read compressed texture file: %s: %s\n", texfile, strerror(errno));
    2.87 +			fclose(fp);
    2.88 +			free(pixels);
    2.89 +			return -1;
    2.90 +		}
    2.91 +		fclose(fp);
    2.92 +
    2.93 +		printf("loaded compressed texture file: %s (%dx%d)\n", texfile, xsz, ysz);
    2.94 +
    2.95 +	} else {
    2.96 +		if(!(pixels = malloc(xsz * ysz * 3))) {
    2.97 +			abort();
    2.98 +		}
    2.99 +		gen_image(pixels, xsz, ysz);
   2.100 +
   2.101 +		printf("compressing texture\n");
   2.102 +		if((comp_size = texcomp(pixels, COMP_FMT, pixels, xsz, ysz, GL_RGB, GL_UNSIGNED_BYTE)) == -1) {
   2.103 +			return -1;
   2.104 +		}
   2.105 +		printf("compressed texture is %d bytes (uncompressed was: %d)\n", comp_size, xsz * ysz * 3);
   2.106 +
   2.107 +		/* dump compressed texture */
   2.108 +		if(!(fp = fopen("compressed_texture", "wb"))) {
   2.109 +			fprintf(stderr, "failed to open compressed texture dump file: %s\n", strerror(errno));
   2.110 +		} else {
   2.111 +			if(fwrite(&xsz, sizeof xsz, 1, fp) < 1 ||
   2.112 +					fwrite(&ysz, sizeof ysz, 1, fp) < 1 ||
   2.113 +					fwrite(pixels, 1, comp_size, fp) < comp_size) {
   2.114 +				fprintf(stderr, "failed to dump compressed texture: %s\n", strerror(errno));
   2.115 +			}
   2.116 +			fclose(fp);
   2.117 +		}
   2.118 +	}
   2.119 +
   2.120 +	glGenTextures(1, &tex);
   2.121 +	glBindTexture(GL_TEXTURE_2D, tex);
   2.122 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   2.123 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   2.124 +	glCompressedTexImage2D(GL_TEXTURE_2D, 0, COMP_FMT, xsz, ysz, 0, comp_size, pixels);
   2.125 +	if(glGetError()) {
   2.126 +		fprintf(stderr, "failed to upload compressed texture\n");
   2.127 +		return -1;
   2.128 +	}
   2.129 +
   2.130 +	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp);
   2.131 +	if(!is_comp) {
   2.132 +		fprintf(stderr, "texture is not compressed\n");
   2.133 +		return -1;
   2.134 +	}
   2.135 +	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &tmp);
   2.136 +	if(tmp != comp_size) {
   2.137 +		fprintf(stderr, "internal compressed size differs (expected: %d, got: %d)!\n", comp_size, tmp);
   2.138 +		return -1;
   2.139 +	}
   2.140 +
   2.141 +	if(!(buf = malloc(comp_size))) {
   2.142 +		fprintf(stderr, "failed to allocate comparison image buffer (%d bytes)\n", comp_size);
   2.143 +		return -1;
   2.144 +	}
   2.145 +	glGetCompressedTexImage(GL_TEXTURE_2D, 0, buf);
   2.146 +
   2.147 +	if(memcmp(pixels, buf, comp_size) != 0) {
   2.148 +		fprintf(stderr, "submitted and retrieved pixel data differ!\n");
   2.149 +		return -1;
   2.150 +	}
   2.151 +	printf("submitted and retrieved sizes match (%d bytes)\n", comp_size);
   2.152 +	free(buf);
   2.153 +	free(pixels);
   2.154 +
   2.155 +#ifdef USE_SRGB
   2.156 +	glEnable(GL_FRAMEBUFFER_SRGB);
   2.157 +#endif
   2.158 +
   2.159 +	glEnable(GL_TEXTURE_2D);
   2.160 +	return 0;
   2.161 +}
   2.162 +
   2.163 +int texcomp(unsigned char *compix, unsigned int tofmt, unsigned char *pixels,
   2.164 +		int xsz, int ysz, unsigned int fromfmt, unsigned int fromtype)
   2.165 +{
   2.166 +	unsigned int tex;
   2.167 +	int is_comp = 0;
   2.168 +	int comp_size = 0;
   2.169 +
   2.170 +	glGenTextures(1, &tex);
   2.171 +	glBindTexture(GL_TEXTURE_2D, tex);
   2.172 +	glTexImage2D(GL_TEXTURE_2D, 0, tofmt, xsz, ysz, 0, fromfmt, fromtype, pixels);
   2.173 +	if(glGetError()) {
   2.174 +		fprintf(stderr, "failed to compress texture\n");
   2.175 +		return -1;
   2.176 +	}
   2.177 +
   2.178 +	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp);
   2.179 +	if(!is_comp) {
   2.180 +		fprintf(stderr, "texture is not compressed\n");
   2.181 +		return -1;
   2.182 +	}
   2.183 +	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &comp_size);
   2.184 +
   2.185 +	glGetCompressedTexImage(GL_TEXTURE_2D, 0, compix);
   2.186 +	return comp_size;
   2.187 +}
   2.188 +
   2.189 +void disp(void)
   2.190 +{
   2.191 +	glClear(GL_COLOR_BUFFER_BIT);
   2.192 +
   2.193 +	glBegin(GL_QUADS);
   2.194 +	glTexCoord2f(0, 1); glVertex2f(-1, -1);
   2.195 +	glTexCoord2f(1, 1); glVertex2f(1, -1);
   2.196 +	glTexCoord2f(1, 0); glVertex2f(1, 1);
   2.197 +	glTexCoord2f(0, 0);	glVertex2f(-1, 1);
   2.198 +	glEnd();
   2.199 +
   2.200 +	glutSwapBuffers();
   2.201 +	assert(glGetError() == GL_NO_ERROR);
   2.202 +}
   2.203 +
   2.204 +void reshape(int x, int y)
   2.205 +{
   2.206 +	float aspect = (float)x / (float)y;
   2.207 +	glViewport(0, 0, x, y);
   2.208 +
   2.209 +	glMatrixMode(GL_PROJECTION);
   2.210 +	glLoadIdentity();
   2.211 +	glScalef(1.0 / aspect, 1, 1);
   2.212 +}
   2.213 +
   2.214 +void keyb(unsigned char key, int x, int y)
   2.215 +{
   2.216 +	if(key == 27) {
   2.217 +		exit(0);
   2.218 +	}
   2.219 +}
   2.220 +
   2.221 +void gen_image(unsigned char *pixels, int xsz, int ysz)
   2.222 +{
   2.223 +	int i, j;
   2.224 +
   2.225 +	for(i=0; i<ysz; i++) {
   2.226 +		for(j=0; j<xsz; j++) {
   2.227 +			int xor = i ^ j;
   2.228 +
   2.229 +			*pixels++ = xor & 0xff;
   2.230 +			*pixels++ = (xor << 1) & 0xff;
   2.231 +			*pixels++ = (xor << 2) & 0xff;
   2.232 +		}
   2.233 +	}
   2.234 +}