stereoimg
diff src/stereoimg.c @ 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/stereoimg.c Thu Jun 14 00:31:51 2012 +0300 1.3 @@ -0,0 +1,203 @@ 1.4 +/* 1.5 +Stereoimg - an OpenGL stereoscopic image viewer. 1.6 +Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org> 1.7 + 1.8 +This program is free software: you can redistribute it and/or modify 1.9 +it under the terms of the GNU General Public License as published by 1.10 +the Free Software Foundation, either version 3 of the License, or 1.11 +(at your option) any later version. 1.12 + 1.13 +This program is distributed in the hope that it will be useful, 1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +GNU General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 + 1.22 +#include <stdio.h> 1.23 +#include <stdlib.h> 1.24 +#include <assert.h> 1.25 + 1.26 +#ifndef __APPLE__ 1.27 +#include <GL/glut.h> 1.28 +#else 1.29 +#include <GLUT/glut.h> 1.30 +#endif 1.31 + 1.32 +#include <imago2.h> 1.33 + 1.34 +struct imgnode { 1.35 + char *name; 1.36 + int width, height; 1.37 + unsigned int tex; 1.38 + int swap; 1.39 + 1.40 + struct imgnode *next; 1.41 +}; 1.42 + 1.43 +void show_image(struct imgnode *node); 1.44 +void disp(void); 1.45 +void reshape(int x, int y); 1.46 +void keyb(unsigned char key, int x, int y); 1.47 +int parse_args(int argc, char **argv); 1.48 + 1.49 + 1.50 +struct imgnode *imglist; 1.51 +int swap_eyes; 1.52 + 1.53 + 1.54 +int main(int argc, char **argv) 1.55 +{ 1.56 + glutInit(&argc, argv); 1.57 + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);// | GLUT_STEREO); 1.58 + glutInitWindowSize(128, 128); 1.59 + glutCreateWindow("stereo image viewer"); 1.60 + 1.61 + if(parse_args(argc, argv) == -1) { 1.62 + return 1; 1.63 + } 1.64 + 1.65 + glutDisplayFunc(disp); 1.66 + glutReshapeFunc(reshape); 1.67 + glutKeyboardFunc(keyb); 1.68 + glutIdleFunc(glutPostRedisplay); 1.69 + 1.70 + glEnable(GL_TEXTURE_2D); 1.71 + show_image(imglist); 1.72 + 1.73 + glutMainLoop(); 1.74 + return 0; 1.75 +} 1.76 + 1.77 + 1.78 +void show_image(struct imgnode *node) 1.79 +{ 1.80 + glutReshapeWindow(node->width / 2, node->height); 1.81 + glutSetWindowTitle(node->name); 1.82 + 1.83 + glBindTexture(GL_TEXTURE_2D, node->tex); 1.84 + 1.85 + swap_eyes = node->swap; 1.86 +} 1.87 + 1.88 +void disp(void) 1.89 +{ 1.90 + glDrawBuffer(swap_eyes ? GL_BACK_RIGHT : GL_BACK_LEFT); 1.91 + glClear(GL_COLOR_BUFFER_BIT); 1.92 + 1.93 + glBegin(GL_QUADS); 1.94 + glColor3f(1, 1, 1); 1.95 + glTexCoord2f(0, 1); glVertex2f(-1, -1); 1.96 + glTexCoord2f(0.5, 1); glVertex2f(1, -1); 1.97 + glTexCoord2f(0.5, 0); glVertex2f(1, 1); 1.98 + glTexCoord2f(0, 0); glVertex2f(-1, 1); 1.99 + glEnd(); 1.100 + 1.101 + glDrawBuffer(swap_eyes ? GL_BACK_LEFT : GL_BACK_RIGHT); 1.102 + glClear(GL_COLOR_BUFFER_BIT); 1.103 + 1.104 + glBegin(GL_QUADS); 1.105 + glColor3f(1, 1, 1); 1.106 + glTexCoord2f(0.5, 1); glVertex2f(-1, -1); 1.107 + glTexCoord2f(1, 1); glVertex2f(1, -1); 1.108 + glTexCoord2f(1, 0); glVertex2f(1, 1); 1.109 + glTexCoord2f(0.5, 0); glVertex2f(-1, 1); 1.110 + glEnd(); 1.111 + 1.112 + glutSwapBuffers(); 1.113 + assert(glGetError() == GL_NO_ERROR); 1.114 +} 1.115 + 1.116 +void reshape(int x, int y) 1.117 +{ 1.118 + glViewport(0, 0, x, y); 1.119 +} 1.120 + 1.121 +void keyb(unsigned char key, int x, int y) 1.122 +{ 1.123 + switch(key) { 1.124 + case 's': 1.125 + swap_eyes = !swap_eyes; 1.126 + glutPostRedisplay(); 1.127 + break; 1.128 + 1.129 + case ' ': 1.130 + imglist = imglist->next; 1.131 + show_image(imglist); 1.132 + glutPostRedisplay(); 1.133 + break; 1.134 + 1.135 + case 27: 1.136 + case 'q': 1.137 + exit(0); 1.138 + 1.139 + default: 1.140 + break; 1.141 + } 1.142 +} 1.143 + 1.144 +int parse_args(int argc, char **argv) 1.145 +{ 1.146 + int i, cur_swap = 0; 1.147 + struct imgnode *node; 1.148 + struct imgnode *head = 0, *tail = 0; 1.149 + 1.150 + for(i=1; i<argc; i++) { 1.151 + if(argv[i][0] == '-' && argv[i][2] == 0) { 1.152 + switch(argv[i][1]) { 1.153 + case 's': 1.154 + cur_swap = !cur_swap; 1.155 + break; 1.156 + 1.157 + default: 1.158 + fprintf(stderr, "invalid option: %s\n", argv[i]); 1.159 + return -1; 1.160 + } 1.161 + } else { 1.162 + int xsz, ysz; 1.163 + void *pix; 1.164 + unsigned int tex; 1.165 + 1.166 + if(!(pix = img_load_pixels(argv[i], &xsz, &ysz, IMG_FMT_RGBA32))) { 1.167 + fprintf(stderr, "failed to open image: %s\n", argv[i]); 1.168 + return -1; 1.169 + } 1.170 + glGenTextures(1, &tex); 1.171 + glBindTexture(GL_TEXTURE_2D, tex); 1.172 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.173 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.174 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 1.175 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 1.176 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pix); 1.177 + img_free_pixels(pix); 1.178 + 1.179 + if(!(node = malloc(sizeof *node))) { 1.180 + perror("failed to allocate image node"); 1.181 + return -1; 1.182 + } 1.183 + node->name = argv[i]; 1.184 + node->width = xsz; 1.185 + node->height = ysz; 1.186 + node->tex = tex; 1.187 + node->swap = cur_swap; 1.188 + node->next = 0; 1.189 + 1.190 + if(head) { 1.191 + tail->next = node; 1.192 + tail = node; 1.193 + } else { 1.194 + head = tail = node; 1.195 + } 1.196 + } 1.197 + } 1.198 + 1.199 + if(!head) { 1.200 + fprintf(stderr, "you must specify one or more images to open\n"); 1.201 + return -1; 1.202 + } 1.203 + tail->next = head; 1.204 + imglist = head; 1.205 + return 0; 1.206 +}