stereoimg

changeset 0:b2a2a355b633

created a repo for stereoimg
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Jun 2012 00:31:51 +0300
parents
children 552f59cf0f73
files Makefile src/stereoimg.c
diffstat 2 files changed, 237 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Makefile	Thu Jun 14 00:31:51 2012 +0300
     1.3 @@ -0,0 +1,34 @@
     1.4 +PREFIX = /usr/local
     1.5 +
     1.6 +src = $(wildcard src/*.c)
     1.7 +obj = $(src:.c=.o)
     1.8 +bin = stereoimg
     1.9 +
    1.10 +CC = gcc
    1.11 +CFLAGS = -pedantic -Wall -g
    1.12 +LDFLAGS = $(libgl) -limago
    1.13 +
    1.14 +ifeq ($(shell uname -s), Darwin)
    1.15 +	libgl = -framework OpenGL -framework GLUT
    1.16 +else
    1.17 +	libgl = -lGL -lglut
    1.18 +endif
    1.19 +
    1.20 +
    1.21 +$(bin): $(obj)
    1.22 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    1.23 +
    1.24 +
    1.25 +.PHONY: clean
    1.26 +clean:
    1.27 +	rm -f $(obj) $(bin)
    1.28 +
    1.29 +
    1.30 +.PHONY: install
    1.31 +install:
    1.32 +	mkdir -p $(PREFIX)/bin
    1.33 +	cp $(bin) $(PREFIX)/bin/$(bin)
    1.34 +
    1.35 +.PHONY: uninstall
    1.36 +uninstall:
    1.37 +	rm -f $(PREFIX)/bin/$(bin)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/stereoimg.c	Thu Jun 14 00:31:51 2012 +0300
     2.3 @@ -0,0 +1,203 @@
     2.4 +/*
     2.5 +Stereoimg - an OpenGL stereoscopic image viewer.
     2.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
     2.7 +
     2.8 +This program is free software: you can redistribute it and/or modify
     2.9 +it under the terms of the GNU General Public License as published by
    2.10 +the Free Software Foundation, either version 3 of the License, or
    2.11 +(at your option) any later version.
    2.12 +
    2.13 +This program is distributed in the hope that it will be useful,
    2.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.16 +GNU General Public License for more details.
    2.17 +
    2.18 +You should have received a copy of the GNU General Public License
    2.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    2.20 +*/
    2.21 +
    2.22 +#include <stdio.h>
    2.23 +#include <stdlib.h>
    2.24 +#include <assert.h>
    2.25 +
    2.26 +#ifndef __APPLE__
    2.27 +#include <GL/glut.h>
    2.28 +#else
    2.29 +#include <GLUT/glut.h>
    2.30 +#endif
    2.31 +
    2.32 +#include <imago2.h>
    2.33 +
    2.34 +struct imgnode {
    2.35 +	char *name;
    2.36 +	int width, height;
    2.37 +	unsigned int tex;
    2.38 +	int swap;
    2.39 +
    2.40 +	struct imgnode *next;
    2.41 +};
    2.42 +
    2.43 +void show_image(struct imgnode *node);
    2.44 +void disp(void);
    2.45 +void reshape(int x, int y);
    2.46 +void keyb(unsigned char key, int x, int y);
    2.47 +int parse_args(int argc, char **argv);
    2.48 +
    2.49 +
    2.50 +struct imgnode *imglist;
    2.51 +int swap_eyes;
    2.52 +
    2.53 +
    2.54 +int main(int argc, char **argv)
    2.55 +{
    2.56 +	glutInit(&argc, argv);
    2.57 +	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);// | GLUT_STEREO);
    2.58 +	glutInitWindowSize(128, 128);
    2.59 +	glutCreateWindow("stereo image viewer");
    2.60 +
    2.61 +	if(parse_args(argc, argv) == -1) {
    2.62 +		return 1;
    2.63 +	}
    2.64 +
    2.65 +	glutDisplayFunc(disp);
    2.66 +	glutReshapeFunc(reshape);
    2.67 +	glutKeyboardFunc(keyb);
    2.68 +	glutIdleFunc(glutPostRedisplay);
    2.69 +
    2.70 +	glEnable(GL_TEXTURE_2D);
    2.71 +	show_image(imglist);
    2.72 +
    2.73 +	glutMainLoop();
    2.74 +	return 0;
    2.75 +}
    2.76 +
    2.77 +
    2.78 +void show_image(struct imgnode *node)
    2.79 +{
    2.80 +	glutReshapeWindow(node->width / 2, node->height);
    2.81 +	glutSetWindowTitle(node->name);
    2.82 +
    2.83 +	glBindTexture(GL_TEXTURE_2D, node->tex);
    2.84 +
    2.85 +	swap_eyes = node->swap;
    2.86 +}
    2.87 +
    2.88 +void disp(void)
    2.89 +{
    2.90 +	glDrawBuffer(swap_eyes ? GL_BACK_RIGHT : GL_BACK_LEFT);
    2.91 +	glClear(GL_COLOR_BUFFER_BIT);
    2.92 +
    2.93 +	glBegin(GL_QUADS);
    2.94 +	glColor3f(1, 1, 1);
    2.95 +	glTexCoord2f(0, 1); glVertex2f(-1, -1);
    2.96 +	glTexCoord2f(0.5, 1); glVertex2f(1, -1);
    2.97 +	glTexCoord2f(0.5, 0); glVertex2f(1, 1);
    2.98 +	glTexCoord2f(0, 0); glVertex2f(-1, 1);
    2.99 +	glEnd();
   2.100 +
   2.101 +	glDrawBuffer(swap_eyes ? GL_BACK_LEFT : GL_BACK_RIGHT);
   2.102 +	glClear(GL_COLOR_BUFFER_BIT);
   2.103 +
   2.104 +	glBegin(GL_QUADS);
   2.105 +	glColor3f(1, 1, 1);
   2.106 +	glTexCoord2f(0.5, 1); glVertex2f(-1, -1);
   2.107 +	glTexCoord2f(1, 1); glVertex2f(1, -1);
   2.108 +	glTexCoord2f(1, 0); glVertex2f(1, 1);
   2.109 +	glTexCoord2f(0.5, 0); glVertex2f(-1, 1);
   2.110 +	glEnd();
   2.111 +
   2.112 +	glutSwapBuffers();
   2.113 +	assert(glGetError() == GL_NO_ERROR);
   2.114 +}
   2.115 +
   2.116 +void reshape(int x, int y)
   2.117 +{
   2.118 +	glViewport(0, 0, x, y);
   2.119 +}
   2.120 +
   2.121 +void keyb(unsigned char key, int x, int y)
   2.122 +{
   2.123 +	switch(key) {
   2.124 +	case 's':
   2.125 +		swap_eyes = !swap_eyes;
   2.126 +		glutPostRedisplay();
   2.127 +		break;
   2.128 +
   2.129 +	case ' ':
   2.130 +		imglist = imglist->next;
   2.131 +		show_image(imglist);
   2.132 +		glutPostRedisplay();
   2.133 +		break;
   2.134 +
   2.135 +	case 27:
   2.136 +	case 'q':
   2.137 +		exit(0);
   2.138 +
   2.139 +	default:
   2.140 +		break;
   2.141 +	}
   2.142 +}
   2.143 +
   2.144 +int parse_args(int argc, char **argv)
   2.145 +{
   2.146 +	int i, cur_swap = 0;
   2.147 +	struct imgnode *node;
   2.148 +	struct imgnode *head = 0, *tail = 0;
   2.149 +
   2.150 +	for(i=1; i<argc; i++) {
   2.151 +		if(argv[i][0] == '-' && argv[i][2] == 0) {
   2.152 +			switch(argv[i][1]) {
   2.153 +			case 's':
   2.154 +				cur_swap = !cur_swap;
   2.155 +				break;
   2.156 +
   2.157 +			default:
   2.158 +				fprintf(stderr, "invalid option: %s\n", argv[i]);
   2.159 +				return -1;
   2.160 +			}
   2.161 +		} else {
   2.162 +			int xsz, ysz;
   2.163 +			void *pix;
   2.164 +			unsigned int tex;
   2.165 +
   2.166 +			if(!(pix = img_load_pixels(argv[i], &xsz, &ysz, IMG_FMT_RGBA32))) {
   2.167 +				fprintf(stderr, "failed to open image: %s\n", argv[i]);
   2.168 +				return -1;
   2.169 +			}
   2.170 +			glGenTextures(1, &tex);
   2.171 +			glBindTexture(GL_TEXTURE_2D, tex);
   2.172 +			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   2.173 +			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   2.174 +			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
   2.175 +			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
   2.176 +			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pix);
   2.177 +			img_free_pixels(pix);
   2.178 +
   2.179 +			if(!(node = malloc(sizeof *node))) {
   2.180 +				perror("failed to allocate image node");
   2.181 +				return -1;
   2.182 +			}
   2.183 +			node->name = argv[i];
   2.184 +			node->width = xsz;
   2.185 +			node->height = ysz;
   2.186 +			node->tex = tex;
   2.187 +			node->swap = cur_swap;
   2.188 +			node->next = 0;
   2.189 +
   2.190 +			if(head) {
   2.191 +				tail->next = node;
   2.192 +				tail = node;
   2.193 +			} else {
   2.194 +				head = tail = node;
   2.195 +			}
   2.196 +		}
   2.197 +	}
   2.198 +
   2.199 +	if(!head) {
   2.200 +		fprintf(stderr, "you must specify one or more images to open\n");
   2.201 +		return -1;
   2.202 +	}
   2.203 +	tail->next = head;
   2.204 +	imglist = head;
   2.205 +	return 0;
   2.206 +}