qvolray

view src/volume.c @ 8:ae10631bb11b

cleaning up a bit to expand
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 07 Apr 2012 16:07:12 +0300
parents
children a6765984e057
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
6 #ifndef __APPLE__
7 #include <GL/gl.h>
8 #else
9 #include <OpenGL/gl.h>
10 #endif
12 #include <imago2.h>
14 struct slice {
15 char *name;
16 struct slice *next;
17 };
19 static struct slice *read_voldesc(const char *fname, struct volume *vol);
20 static char *trim(char *str);
22 struct volume *load_volume(const char *fname)
23 {
24 struct slice *slist;
25 struct volume *vol = 0;
27 if(!(vol = malloc(sizeof *vol))) {
28 perror("failed to allocate volume");
29 return 0;
30 }
31 memset(vol, 0, sizeof *vol);
32 vol->zaspect = 1;
34 if(!(slist = read_voldesc(fname, vol))) {
35 goto err;
36 }
38 glGenTextures(1, &vol->tex_vol);
39 glBindTexture(GL_TEXTURE_3D, vol->tex_vol);
40 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
41 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
42 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
43 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
44 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
46 for(i=0; i<vol->sz[2]; i++) {
47 struct img_pixmap img;
48 struct slice *slice = head;
49 head = head->next;
51 img_init(&img);
52 if(img_load(&img, slice->name) == -1) {
53 fprintf(stderr, "failed to load volume slice: %s\n", slice->name);
54 goto err;
55 }
56 }
58 glGenTextures(1, &vol->tex_grad);
59 glBindTexture(GL_TEXTURE_3D, vol->tex_grad);
60 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
61 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
62 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
63 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
64 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
66 err:
67 while(slist) {
68 struct slice *tmp = slist;
69 slist = slist->next;
70 free(tmp->name);
71 free(tmp);
72 }
73 if(vol) {
74 if(vol->tex_vol)
75 glDeleteTextures(1, &vol->tex_vol);
76 if(vol->tex_grad)
77 glDeleteTextures(1, &vol->tex_grad);
78 free(vol);
79 }
80 return 0;
81 }
83 static struct slice *read_voldesc(const char *fname, struct volume *vol)
84 {
85 FILE *fp = 0;
86 char buf[512];
87 int mode_slices = 0;
88 struct slice *head = 0, *tail;
90 if(!(fp = fopen(fname, "r"))) {
91 fprintf(stderr, "failed to open volume descriptor: %s\n", fname);
92 return 0;
93 }
94 fgets(buf, sizeof buf, fp);
95 if(strstr(buf, "VOLDESC") != buf) {
96 fprintf(stderr, "invalid file format while trying to read volume descriptor: %s\n", fname);
97 goto err;
98 }
100 while(fgets(buf, sizeof buf, fp)) {
101 char *line = trim(buf);
103 if(!*line || *line == '#')
104 continue;
106 if(mode_slices) {
107 /* we're in the part that contains filenames... append to the list */
108 struct slice *node = malloc(sizeof *node);
109 if(!node || !(node->name = malloc(strlen(line) + 1))) {
110 perror("failed to allocate list node");
111 free(node);
112 goto err;
113 }
114 strcpy(node->name, line);
115 node->next = 0;
117 if(head) {
118 tail->next = node;
119 tail = node;
120 } else {
121 head = tail = node;
122 }
123 vol->sz[2]++;
124 } else {
125 /* TODO we're in the options part... parse */
126 }
127 }
128 fclose(fp);
130 if(!vol->sz[2]) {
131 fprintf(stderr, "file: %s contains no slices\n", fname);
132 goto err;
133 }
135 return head;
137 err:
138 while(head) {
139 struct slice *tmp = head;
140 head = head->next;
141 free(tmp->name);
142 free(tmp);
143 }
144 if(fp) {
145 fclose(fp);
146 }
147 return 0;
148 }
150 static char *trim(char *str)
151 {
152 char *tmp = str + strlen(str) - 1;
154 while(isspace(*tmp))
155 tmp--;
156 tmp[1] = 0;
158 tmp = str;
159 while(isspace(*tmp))
160 tmp++;
161 return tmp;
162 }