Blame view

ManageTextures.cpp 9.07 KB
ee96a02c   David Mayerich   first commit from...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  #include "trueeyes.h"
  #include "VolumeDataStruct.h"
  #include "TextureDataStruct.h"
  #include <vector>
  
  //list of volume objects for rendering
  vector<VolumeData> VolumeList;
  
  //list of texture objects used for transfer functions
  vector<TextureData> TextureList;
  
  void CreateAutoVolume()
  {
  	int s = 64;
  	unsigned char* volume = new unsigned char[s*s*s*3];
  	memset(volume, 0, s*s*s*3);
  
  	int x, y, z;
  	for(x=0; x<s; x++)
  		for(y=0; y<s; y++)
  			for(z=0; z<s; z++)
  			{
  				vector3D<float> v = vector3D<float>(x-s/2, y-s/2, z-s/2);
  
  					volume[z*s*s*3 + y*s*3 + x*3 + 0] = x*(255/s);
  					volume[z*s*s*3 + y*s*3 + x*3 + 1] = y*(255/s);
  					volume[z*s*s*3 + y*s*3 + x*3 + 2] = z*(255/s);
  			}
  
  	if(VolumeList.size() > 0)
  	{
  		VolumeList[0].Texture.Clean();
  		VolumeList.clear();
  	}
  
  	//create an OpenGL texture map
  	rts_glTextureMap newTexture;
  	newTexture.Init(volume, GL_TEXTURE_3D, s, s, s, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, GL_LINEAR);
  
  	//create the volume data structure
  	VolumeData newVolume;
  	newVolume.Name = "volume";
  	newVolume.Texture = newTexture;
  	newVolume.FileType = VOLUME_FILE_AUTO;
  	newVolume.Dim = vector3D<int>(s, s, s);
  	newVolume.ExternalComponents = 3;
  	newVolume.ExternalDatatype = GL_UNSIGNED_BYTE;
  	newVolume.InternalComponents = 3;
  	newVolume.InternalDatatype = GL_UNSIGNED_BYTE;
  	
  	//add the volume structure to the list of volumes
  	VolumeList.push_back(newVolume);
  }
  
  //define macros for bit swapping (little endian to big endian in Windows)
  #define SWAP_2(x) ( (((x) & 0xff) << 8) | ((unsigned short)(x) >> 8) )
  #define SWAP_4(x) ( ((x) << 24) | \
           (((x) << 8) & 0x00ff0000) | \
           (((x) >> 8) & 0x0000ff00) | \
           ((x) >> 24) )
  #define FIX_SHORT(x) (*(unsigned short *)&(x) = SWAP_2(*(unsigned short *)&(x)))
  #define FIX_INT(x)   (*(unsigned int *)&(x)   = SWAP_4(*(unsigned int *)&(x)))
  #define FIX_FLOAT(x) FIX_INT(x)
  
  void FlipBits(void* bits, int bpp, int size)
  {
  	int i;
  	if(bpp == 2)
  	{
  		unsigned short* short_bits = (unsigned short*)bits;
  		for(i=0; i<size; i++)
  		{
  			FIX_SHORT(short_bits[i]);
  		}
  	}
  	if(bpp == 4)
  	{
  		unsigned int* int_bits = (unsigned int*)bits;
  		for(i=0; i<size; i++)
  		{
  			FIX_INT(int_bits[i]);
  		}
  	}
  }
  int LoadHighComponentRawVolume(VolumeData& newVolume, int minC, int maxC)
  {
  	//This function loads a single volume with a large number of components as separate textures
  	//This is necessary because the maximum number of components in an OpenGL texture is 4 (RGBA)
  	//Call this function multiple times, specifying the range in minC and maxC
  
  	//get the volume size and allocate space
  	int sx = newVolume.Dim.x;
  	int sy = newVolume.Dim.y;
  	//the total z dimension is the dimension of each file times the number of files
  	int file_z = newVolume.Dim.z;
  	int sz = file_z * newVolume.Filenames.size();
  
  	cout<<"Loading high-component volume----------------"<<endl;
  	cout<<"Size: "<<sx<<","<<sy<<","<<sz<<endl;
  
  	//set the components to the appropriate value
  	int totalC = newVolume.ExternalComponents;
  	int components = maxC - minC + 1;
  	newVolume.ExternalComponents = components;
  
  	int precision = newVolume.GetByteSize(newVolume.ExternalDatatype);
  
  	cout<<"Allocating "<<sx*sy*sz*components*precision<<" bytes..."<<endl;
  	char* bits = (char*)malloc(sx*sy*sz*components*precision);
  	if(bits == NULL)
  	{
  		cout<<"Error allocating main memory."<<endl;
  		return 1;
  	}
  
  	for(int n=0; n<newVolume.Filenames.size(); n++)
  	{
  		FILE *f;
  		f = fopen(newVolume.Filenames[n].getString().c_str(), "rb");
  		//if the file is valid
  		if(f)
  		{
  			//seek past the header
  			fseek(f, newVolume.HeaderSize, SEEK_SET);
  
  			//increment to the start of the desired component range
  			char* dst = bits;
  			dst += precision*minC;
  			fseek(f, precision*minC, SEEK_CUR);
  
  			//load each voxel independently
  			for(int c=0; c<sx*sy*sz; c++)
  			{
  				//read the components within the specified range
  				fread(dst, sizeof(unsigned char), components*precision, f);
  				fseek(f, precision*(totalC - maxC - 1), SEEK_CUR);
  			}
  		}
  		else
  			cout<<"error"<<endl;
  		fclose(f);
  	}
  
  	//create the OpenGL texture map
  	rts_glTextureMap newTexture;
  	newTexture.Init(bits, GL_TEXTURE_3D, sx, sy, sz, newVolume.getInternalFormat(),
  													 newVolume.getExternalFormat(),
  													 newVolume.ExternalDatatype);
  	free(bits);
  
  	//store it in the volume list
  	newVolume.Texture = newTexture;
  	VolumeList.push_back(newVolume);
  
  	return 0;
  }
  
  int LoadRawVolume(VolumeData& newVolume)
  {
  	if(newVolume.ExternalComponents > 4)
  	{
  		LoadHighComponentRawVolume(newVolume, 0, 2);
  		LoadHighComponentRawVolume(newVolume, 3, 5);
  		return 0;
  	}
  	//get the volume size and allocate space
  	int sx = newVolume.Dim.x;
  	int sy = newVolume.Dim.y;
  	//the total z dimension is the dimension of each file times the number of files
  	int sz = newVolume.Dim.z;
  	int file_z;
  	if(newVolume.Filenames.size() == 1)
  		file_z = sz;
  	else if(newVolume.Filenames.size() > 1)
  	{
  		file_z = 1;
  		sz = file_z * newVolume.Filenames.size();
  	}
  	
  	int components = newVolume.ExternalComponents;
  	int precision = newVolume.GetByteSize(newVolume.ExternalDatatype);
  
  	cout<<"Loading RAW volume----------------"<<endl;
  	cout<<"Size: "<<sx<<","<<sy<<","<<sz<<endl;
  	cout<<"Components: "<<components<<endl;
  	cout<<"Precision: "<<precision<<" bytes"<<endl;
  
  	cout<<"Allocating "<<sx*sy*sz*components*precision<<" bytes..."<<endl;
  	char* bits = (char*)malloc(sx*sy*sz*components*precision);
  	if(bits == NULL)
  	{
  		cout<<"Error allocating main memory."<<endl;
  		return 1;
  	}
  
  	for(int n=0; n<newVolume.Filenames.size(); n++)
  	{
  		FILE *f;
  		f = fopen(newVolume.Filenames[n].getString().c_str(), "rb");
  		if(f)
  		{
  			fseek(f, newVolume.HeaderSize, SEEK_SET);
  			fread(bits + sx*sy*file_z*n*components*precision, sizeof(unsigned char), sx*sy*file_z*components*precision, f);
  		}
  		else
  			cout<<"error"<<endl;
  		fclose(f);
  	}
  	
  	if(newVolume.BitType == LOAD_BIG_ENDIAN)
  		FlipBits(bits, precision, sx*sy*sz*components);
  
  	//create the OpenGL texture map
  	rts_glTextureMap newTexture;
  	newTexture.Init(bits, GL_TEXTURE_3D, sx, sy, sz, newVolume.getInternalFormat(),
  													 newVolume.getExternalFormat(),
  													 newVolume.ExternalDatatype);
  	free(bits);
  	//store it in the volume list
  	newVolume.Dim.z = sz;
  	newVolume.Texture = newTexture;
  	VolumeList.push_back(newVolume);
  
  
  	return 0;
  }
  
  
  
  int LoadImages(VolumeData& newVolume)
  {
  	//*******currently, this only supports image formats with 8bpp
  
  	//get the number of images
  	int sz = newVolume.Filenames.size();
  
  	//load the first image to determine the volume size
  	QImage I(newVolume.Filenames[0].getString().c_str());
  	int sx = I.width();
  	int sy = I.height();
  	
  
  	//find the number of color components
  	int components = I.depth()/8;
  
  	int precision = 1;
  
  	//allocate memory for the image
  	char* bits = (char*)malloc(sx*sy*sz*components*precision);
  
  	cout<<"Image size: "<<I.width()<<","<<I.height()<<endl;
  	cout<<"Components: "<<components<<endl;
  	cout<<"Precision: "<<precision<<endl;
  	//copy each image to the correct place in the array
  	for(int i=0; i<sz; i++)
  	{
  		I = QImage(newVolume.Filenames[i].getString().c_str()).mirrored().rgbSwapped();
  		memcpy((unsigned char*)bits + sx*sy*components*precision*i, I.bits(), sx*sy*components*precision);
  	}
  
  	//fill the volume data structure
  	newVolume.Dim = vector3D<int>(sx, sy, sz);
  	newVolume.ExternalComponents = components;
  	newVolume.ExternalDatatype = GL_UNSIGNED_BYTE;
  	newVolume.InternalComponents = components;
  	newVolume.InternalDatatype = GL_UNSIGNED_BYTE;
  	//newVolume.Name = newVolume.Filenames[0].getPrefix();
  
  	rts_glTextureMap newTexture;
  	/*newTexture.Init(bits, GL_TEXTURE_3D, sx, sy, sz, newVolume.getInternalFormat(),
  													 newVolume.getExternalFormat(),
  													 newVolume.ExternalDatatype);*/
  	newTexture.Init(bits, GL_TEXTURE_3D, sx, sy, sz, newVolume.getInternalFormat(),
  													 newVolume.getExternalFormat(),
  													 newVolume.ExternalDatatype);
  	free(bits);
  
  	newVolume.Texture = newTexture;
  	VolumeList.push_back(newVolume);
  	
  
  	return 0;
  }
  
  int LoadTexture(TextureData& newTexture)
  {
  	//*******currently, this only supports image formats with 8bpp
  
  	//load the image
  	QImage I(newTexture.Filename.getString().c_str());
  	//swap because for some reason QImage returns a BGR image as a pointer
  	I = I.rgbSwapped();
  	int sx = I.width();
  	int sy = I.height();
  	
  
  	//find the number of color components
  	int components = I.depth()/8;
  
  	//allocate memory for the image
  	char* bits = (char*)malloc(sx*sy*components);
  
  	cout<<"Image size: "<<I.width()<<","<<I.height()<<endl;
  	cout<<"Components: "<<components<<endl;
  	//copy the image to a memory array
  	memcpy((unsigned char*)bits, I.bits(), sx*sy*components);
  
  
  	//fill the volume data structure
  	newTexture.sX = sx;
  	newTexture.sY = sy;
  	newTexture.ExternalComponents = components;
  	newTexture.InternalComponents = components;
  	newTexture.ExternalDatatype = GL_UNSIGNED_BYTE;
  	newTexture.InternalDatatype = GL_UNSIGNED_BYTE;
  	newTexture.Normalized = true;
  
  	//newVolume.Name = newVolume.Filenames[0].getPrefix();
  
  	rts_glTextureMap texMap;
  	texMap.Init(bits, GL_TEXTURE_2D, sx, sy, 0, newTexture.getInternalFormat(),
  													 newTexture.getExternalFormat(),
  													 newTexture.ExternalDatatype);
  	free(bits);
  
  	newTexture.Texture = texMap;
  	
  	
  
  	return 0;
  
  }