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