qvolray

view src/volume.cc @ 17:535762131d34

fixed to compile on visual studio 2010
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 11 Apr 2012 01:44:45 +0200
parents 17d9dc2edc91
children 3d05c261a2f4
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <list>
6 #include <string>
7 #include <GL/glew.h>
8 #include <imago2.h>
9 #include "volume.h"
11 static void calc_gradients(float *voxels, int xsz, int ysz, int zsz);
12 static struct slice *read_voldesc(const char *fname, struct volume *vol);
13 static char *trim(char *str);
15 Volume::Volume()
16 {
17 sz[0] = sz[1] = sz[2] = 0;
18 zaspect = 1.0;
19 tex = 0;
20 }
22 Volume::~Volume()
23 {
24 if(tex) {
25 glDeleteTextures(1, &tex);
26 }
27 }
29 bool Volume::load(const char *fname)
30 {
31 std::list<std::string> slist;
32 float *voxels = 0, *vptr;
33 struct img_pixmap img;
35 if(!(read_voldesc(fname, &slist))) {
36 return false;
37 }
39 /* load the first image to determine slice dimensions */
40 img_init(&img);
41 if(img_load(&img, slist.begin()->c_str()) == -1) {
42 fprintf(stderr, "failed to load volume slice: %s\n", slist.begin()->c_str());
43 return false;
44 }
46 sz[0] = img.width;
47 sz[1] = img.height;
48 printf("volume dimensions: %dx%dx%d\n", img.width, img.height, sz[2]);
50 /* allocate the whole volume at once */
51 voxels = new float[sz[0] * sz[1] * sz[2] * 4];
52 vptr = voxels;
53 img_destroy(&img);
55 /* put the volume data into the alpha component */
56 //for(auto slice : slist) { // fucking vs2010 doesn't support range-for
57 for(auto iter = slist.begin(); iter != slist.end(); iter++) {
58 auto slice = *iter;
60 int x, y, xsz, ysz;
61 float *pixels, *src;
63 if(!(pixels = (float*)img_load_pixels(slice.c_str(), &xsz, &ysz, IMG_FMT_GREYF))) {
64 fprintf(stderr, "failed to load volume slice: %s\n", slice.c_str());
65 delete [] voxels;
66 return false;
67 }
69 if(xsz != sz[0] || ysz != sz[1]) {
70 fprintf(stderr, "inconsistent dimensions for slice %s: %dx%d (%dx%d expected)\n",
71 slice.c_str(), xsz, ysz, sz[0], sz[1]);
72 delete [] voxels;
73 return false;
74 }
76 src = pixels;
77 for(y=0; y<ysz; y++) {
78 for(x=0; x<xsz; x++) {
79 vptr[0] = vptr[1] = vptr[2] = 0.0f;
80 vptr[3] = *src++;
81 vptr += 4;
82 }
83 }
84 img_free_pixels(pixels);
85 }
87 /* calculate gradients */
88 calc_gradients(voxels, sz[0], sz[1], sz[2]);
90 /* create the volume texture */
91 glGenTextures(1, &tex);
92 glBindTexture(GL_TEXTURE_3D, tex);
93 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
94 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
95 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
96 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
97 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
98 glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA32F_ARB, sz[0], sz[1], sz[2], 0, GL_RGBA, GL_FLOAT, voxels);
100 /* ... and destroy our copy */
101 delete [] voxels;
102 return true;
103 }
105 unsigned int Volume::get_texture() const
106 {
107 return tex;
108 }
110 void Volume::bind(int tex_unit) const
111 {
112 glActiveTexture(GL_TEXTURE0 + tex_unit);
113 glBindTexture(GL_TEXTURE_3D, tex);
114 }
116 struct vec4 { float x, y, z, w; };
118 static void calc_gradients(float *voxels, int xsz, int ysz, int zsz)
119 {
120 int x, y, z, slice_pixels = xsz * ysz;
121 vec4 *vptr = (vec4*)voxels;
123 for(z=0; z<zsz; z++) {
124 for(y=0; y<ysz; y++) {
125 for(x=0; x<xsz; x++) {
126 float x0, x1, y0, y1, z0, z1;
128 x0 = x > 0 ? (vptr - 1)->w : vptr->w;
129 y0 = y > 0 ? (vptr - xsz)->w : vptr->w;
130 z0 = z > 0 ? (vptr - slice_pixels)->w : vptr->w;
131 x1 = x < xsz - 1 ? (vptr + 1)->w : vptr->w;
132 y1 = y < ysz - 1 ? (vptr + xsz)->w : vptr->w;
133 z1 = z < zsz - 1 ? (vptr + slice_pixels)->w : vptr->w;
135 vptr->x = x1 - x0;
136 vptr->y = y1 - y0;
137 vptr->z = z1 - z0;
139 vptr++;
140 }
141 }
142 }
143 }
145 bool Volume::read_voldesc(const char *fname, std::list<std::string> *slist)
146 {
147 FILE *fp = 0;
148 char buf[512], *slash, *prefix = "";
149 int mode_slices = 0;
151 slist->clear();
153 if(!(fp = fopen(fname, "r"))) {
154 fprintf(stderr, "failed to open volume descriptor: %s\n", fname);
155 return false;
156 }
157 fgets(buf, sizeof buf, fp);
158 if(strstr(buf, "VOLDESC") != buf) {
159 fprintf(stderr, "invalid file format while trying to read volume descriptor: %s\n", fname);
160 fclose(fp);
161 return false;
162 }
164 if((slash = strrchr((char*)fname, '/'))) {
165 int len = slash - fname + 1;
166 prefix = (char*)alloca(len + 1);
167 memcpy(prefix, fname, len);
168 prefix[len] = 0;
169 }
171 while(fgets(buf, sizeof buf, fp)) {
172 char *line = trim(buf);
174 if(!*line || *line == '#')
175 continue;
177 if(mode_slices) {
178 /* we're in the part that contains filenames... append to the list */
179 slist->push_back(std::string(prefix) + std::string(line));
180 sz[2]++;
181 } else {
182 /* TODO we're in the options part... parse */
183 if(strcmp(line, "SLICES") == 0) {
184 mode_slices = 1;
185 }
186 }
187 }
188 fclose(fp);
190 if(slist->empty()) {
191 fprintf(stderr, "file: %s contains no slices\n", fname);
192 return false;
193 }
194 return true;
195 }
197 static char *trim(char *str)
198 {
199 char *tmp = str + strlen(str) - 1;
201 while(isspace(*tmp))
202 tmp--;
203 tmp[1] = 0;
205 tmp = str;
206 while(isspace(*tmp))
207 tmp++;
208 return tmp;
209 }