3dphotoshoot

diff libs/imago/imago_gl.c @ 14:06dc8b9b4f89

added libimago, libjpeg and libpng
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Jun 2015 17:25:49 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/imago/imago_gl.c	Sun Jun 07 17:25:49 2015 +0300
     1.3 @@ -0,0 +1,231 @@
     1.4 +#include "imago2.h"
     1.5 +
     1.6 +/* to avoid dependency to OpenGL, I'll define all the relevant GL macros manually */
     1.7 +#define GL_UNSIGNED_BYTE		0x1401
     1.8 +#define GL_FLOAT				0x1406
     1.9 +
    1.10 +#define GL_LUMINANCE			0x1909
    1.11 +#define GL_RGB					0x1907
    1.12 +#define GL_RGBA					0x1908
    1.13 +
    1.14 +#define GL_RGBA32F				0x8814
    1.15 +#define GL_RGB32F				0x8815
    1.16 +#define GL_LUMINANCE32F			0x8818
    1.17 +
    1.18 +#define GL_TEXTURE_2D			0x0de1
    1.19 +#define GL_TEXTURE_WRAP_S		0x2802
    1.20 +#define GL_TEXTURE_WRAP_T		0x2803
    1.21 +#define GL_TEXTURE_MAG_FILTER	0x2800
    1.22 +#define GL_TEXTURE_MIN_FILTER	0x2801
    1.23 +#define GL_LINEAR				0x2601
    1.24 +#define GL_REPEAT				0x2901
    1.25 +
    1.26 +
    1.27 +typedef unsigned int GLenum;
    1.28 +typedef unsigned int GLuint;
    1.29 +typedef int GLint;
    1.30 +typedef int GLsizei;
    1.31 +typedef void GLvoid;
    1.32 +
    1.33 +/* for the same reason I'll load GL functions dynamically */
    1.34 +#ifndef WIN32
    1.35 +typedef void (*gl_gen_textures_func)(GLsizei, GLuint*);
    1.36 +typedef void (*gl_bind_texture_func)(GLenum, GLuint);
    1.37 +typedef void (*gl_tex_parameteri_func)(GLenum, GLenum, GLint);
    1.38 +typedef void (*gl_tex_image2d_func)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid*);
    1.39 +#else
    1.40 +typedef void (__stdcall *gl_gen_textures_func)(GLsizei, GLuint*);
    1.41 +typedef void (__stdcall *gl_bind_texture_func)(GLenum, GLuint);
    1.42 +typedef void (__stdcall *gl_tex_parameteri_func)(GLenum, GLenum, GLint);
    1.43 +typedef void (__stdcall *gl_tex_image2d_func)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid*);
    1.44 +#endif
    1.45 +
    1.46 +static gl_gen_textures_func gl_gen_textures;
    1.47 +static gl_bind_texture_func gl_bind_texture;
    1.48 +static gl_tex_parameteri_func gl_tex_parameteri;
    1.49 +static gl_tex_image2d_func gl_tex_image2d;
    1.50 +
    1.51 +static int load_glfunc(void);
    1.52 +
    1.53 +unsigned int img_fmt_glfmt(enum img_fmt fmt)
    1.54 +{
    1.55 +	switch(fmt) {
    1.56 +	case IMG_FMT_GREY8:
    1.57 +	case IMG_FMT_GREYF:
    1.58 +		return GL_LUMINANCE;
    1.59 +
    1.60 +	case IMG_FMT_RGB24:
    1.61 +	case IMG_FMT_RGBF:
    1.62 +		return GL_RGB;
    1.63 +
    1.64 +	case IMG_FMT_RGBA32:
    1.65 +	case IMG_FMT_RGBAF:
    1.66 +		return GL_RGBA;
    1.67 +
    1.68 +	default:
    1.69 +		break;
    1.70 +	}
    1.71 +	return 0;
    1.72 +}
    1.73 +
    1.74 +unsigned int img_fmt_gltype(enum img_fmt fmt)
    1.75 +{
    1.76 +	switch(fmt) {
    1.77 +	case IMG_FMT_GREY8:
    1.78 +	case IMG_FMT_RGB24:
    1.79 +	case IMG_FMT_RGBA32:
    1.80 +		return GL_UNSIGNED_BYTE;
    1.81 +
    1.82 +	case IMG_FMT_GREYF:
    1.83 +	case IMG_FMT_RGBF:
    1.84 +	case IMG_FMT_RGBAF:
    1.85 +		return GL_FLOAT;
    1.86 +
    1.87 +	default:
    1.88 +		break;
    1.89 +	}
    1.90 +	return 0;
    1.91 +}
    1.92 +
    1.93 +unsigned int img_fmt_glintfmt(enum img_fmt fmt)
    1.94 +{
    1.95 +	switch(fmt) {
    1.96 +	case IMG_FMT_GREY8:
    1.97 +		return GL_LUMINANCE;
    1.98 +	case IMG_FMT_RGB24:
    1.99 +		return GL_RGB;
   1.100 +	case IMG_FMT_RGBA32:
   1.101 +		return GL_RGBA;
   1.102 +	case IMG_FMT_GREYF:
   1.103 +		return GL_LUMINANCE32F;
   1.104 +	case IMG_FMT_RGBF:
   1.105 +		return GL_RGB32F;
   1.106 +	case IMG_FMT_RGBAF:
   1.107 +		return GL_RGBA32F;
   1.108 +	default:
   1.109 +		break;
   1.110 +	}
   1.111 +	return 0;
   1.112 +}
   1.113 +
   1.114 +unsigned int img_glfmt(struct img_pixmap *img)
   1.115 +{
   1.116 +	return img_fmt_glfmt(img->fmt);
   1.117 +}
   1.118 +
   1.119 +unsigned int img_gltype(struct img_pixmap *img)
   1.120 +{
   1.121 +	return img_fmt_gltype(img->fmt);
   1.122 +}
   1.123 +
   1.124 +unsigned int img_glintfmt(struct img_pixmap *img)
   1.125 +{
   1.126 +	return img_fmt_glintfmt(img->fmt);
   1.127 +}
   1.128 +
   1.129 +unsigned int img_gltexture(struct img_pixmap *img)
   1.130 +{
   1.131 +	unsigned int tex;
   1.132 +	unsigned int intfmt, fmt, type;
   1.133 +
   1.134 +	if(!gl_gen_textures) {
   1.135 +		if(load_glfunc() == -1) {
   1.136 +			fprintf(stderr, "imago: failed to initialize the OpenGL helpers\n");
   1.137 +			return 0;
   1.138 +		}
   1.139 +	}
   1.140 +
   1.141 +	intfmt = img_glintfmt(img);
   1.142 +	fmt = img_glfmt(img);
   1.143 +	type = img_gltype(img);
   1.144 +
   1.145 +	gl_gen_textures(1, &tex);
   1.146 +	gl_bind_texture(GL_TEXTURE_2D, tex);
   1.147 +	gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   1.148 +	gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   1.149 +	gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   1.150 +	gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   1.151 +	gl_tex_image2d(GL_TEXTURE_2D, 0, intfmt, img->width, img->height, 0, fmt, type, img->pixels);
   1.152 +	return tex;
   1.153 +}
   1.154 +
   1.155 +unsigned int img_gltexture_load(const char *fname)
   1.156 +{
   1.157 +	struct img_pixmap img;
   1.158 +	unsigned int tex;
   1.159 +
   1.160 +	img_init(&img);
   1.161 +	if(img_load(&img, fname) == -1) {
   1.162 +		img_destroy(&img);
   1.163 +		return 0;
   1.164 +	}
   1.165 +
   1.166 +	tex = img_gltexture(&img);
   1.167 +	img_destroy(&img);
   1.168 +	return tex;
   1.169 +}
   1.170 +
   1.171 +unsigned int img_gltexture_read_file(FILE *fp)
   1.172 +{
   1.173 +	struct img_pixmap img;
   1.174 +	unsigned int tex;
   1.175 +
   1.176 +	img_init(&img);
   1.177 +	if(img_read_file(&img, fp) == -1) {
   1.178 +		img_destroy(&img);
   1.179 +		return 0;
   1.180 +	}
   1.181 +
   1.182 +	tex = img_gltexture(&img);
   1.183 +	img_destroy(&img);
   1.184 +	return tex;
   1.185 +}
   1.186 +
   1.187 +unsigned int img_gltexture_read(struct img_io *io)
   1.188 +{
   1.189 +	struct img_pixmap img;
   1.190 +	unsigned int tex;
   1.191 +
   1.192 +	img_init(&img);
   1.193 +	if(img_read(&img, io) == -1) {
   1.194 +		img_destroy(&img);
   1.195 +		return 0;
   1.196 +	}
   1.197 +
   1.198 +	tex = img_gltexture(&img);
   1.199 +	img_destroy(&img);
   1.200 +	return tex;
   1.201 +}
   1.202 +
   1.203 +#if defined(__unix__) || defined(__APPLE__)
   1.204 +#ifndef __USE_GNU
   1.205 +#define __USE_GNU
   1.206 +#endif
   1.207 +
   1.208 +#include <dlfcn.h>
   1.209 +#endif
   1.210 +#ifdef WIN32
   1.211 +#include <windows.h>
   1.212 +#endif
   1.213 +
   1.214 +static int load_glfunc(void)
   1.215 +{
   1.216 +#if defined(__unix__) || defined(__APPLE__)
   1.217 +	gl_gen_textures = (gl_gen_textures_func)dlsym(RTLD_DEFAULT, "glGenTextures");
   1.218 +	gl_bind_texture = (gl_bind_texture_func)dlsym(RTLD_DEFAULT, "glBindTexture");
   1.219 +	gl_tex_parameteri = (gl_tex_parameteri_func)dlsym(RTLD_DEFAULT, "glTexParameteri");
   1.220 +	gl_tex_image2d = (gl_tex_image2d_func)dlsym(RTLD_DEFAULT, "glTexImage2D");
   1.221 +#endif
   1.222 +
   1.223 +#ifdef WIN32
   1.224 +	HANDLE dll = LoadLibrary("opengl32.dll");
   1.225 +	if(dll) {
   1.226 +		gl_gen_textures = (gl_gen_textures_func)GetProcAddress(dll, "glGenTextures");
   1.227 +		gl_bind_texture = (gl_bind_texture_func)GetProcAddress(dll, "glBindTexture");
   1.228 +		gl_tex_parameteri = (gl_tex_parameteri_func)GetProcAddress(dll, "glTexParameteri");
   1.229 +		gl_tex_image2d = (gl_tex_image2d_func)GetProcAddress(dll, "glTexImage2D");
   1.230 +	}
   1.231 +#endif
   1.232 +
   1.233 +	return (gl_gen_textures && gl_bind_texture && gl_tex_parameteri && gl_tex_image2d) ? 0 : -1;
   1.234 +}