forked from fristed/BEaST
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mincio.c
166 lines (132 loc) · 4.46 KB
/
mincio.c
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
/* mincio.c
*
* Copyright 2011 Simon Fristed Eskildsen, Vladimir Fonov,
* Pierrick Coupé, Jose V. Manjon
*
* This file is part of mincbeast.
*
* mincbeast is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mincbeast is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mincbeast. If not, see <http://www.gnu.org/licenses/>.
*
* For questions and feedback, please contact:
* Simon Fristed Eskildsen <[email protected]>
*/
#include <float.h>
#include "mincio.h"
void set_volume(float *data, VIO_Volume vol, int *sizes){
int i,j,k;
for (i=0;i<sizes[0];i++)
for (j=0;j<sizes[1];j++)
for (k=0;k<sizes[2];k++)
data[i*sizes[1]*sizes[2]+j*sizes[2]+k] = get_volume_real_value(vol,i,j,k,0,0);
}
void get_volume(float *data, VIO_Volume vol, int *sizes){
int i,j,k;
for (i=0;i<sizes[0];i++)
for (j=0;j<sizes[1];j++)
for (k=0;k<sizes[2];k++)
set_volume_real_value(vol,i,j,k,0,0,data[i*sizes[1]*sizes[2]+j*sizes[2]+k]);
}
int write_volume(char *name, VIO_Volume vol, float *data){
int i,j,k,index,sizes[5];
float min=FLT_MAX,max=FLT_MIN;
fprintf(stderr,"Writing %s\n",name);
get_volume_sizes(vol,sizes);
for (i=0;i<sizes[0];i++){
for (j=0;j<sizes[1];j++){
for (k=0;k<sizes[2];k++){
index=i*sizes[2]*sizes[1] + j*sizes[2] + k;
min=MIN(min,data[index]);
max=MAX(max,data[index]);
}
}
}
set_volume_real_range(vol,min,max);
get_volume(data, vol, sizes);
output_volume( name, NC_FLOAT,FALSE,min,max,vol,NULL, (minc_output_options *)NULL);
return STATUS_OK;
}
int write_minc(char *filename, float *image, image_metadata *meta,VIO_BOOL binary_mask){
VIO_Volume volume;
int i,j,k,index;
float min=FLT_MAX,max=FLT_MIN;
VIO_Real dummy[3];
if(binary_mask)
{
volume = create_volume(3,NULL,NC_BYTE,FALSE,0.0,1.0);
printf("Writing a binary volume...\n");
}
else
volume = create_volume(3,NULL,NC_FLOAT,FALSE,FLT_MIN,FLT_MAX);
if(!volume)
return STATUS_ERR;
if(!binary_mask)
{
for (i=0;i<meta->length[0];i++){
for (j=0;j<meta->length[1];j++){
for (k=0;k<meta->length[2];k++){
index=i*meta->length[2]*meta->length[1] + j*meta->length[2] + k;
min=MIN(min,image[index]);
max=MAX(max,image[index]);
}
}
}
set_volume_real_range(volume,min,max);
} else {
set_volume_real_range(volume,0.0,1.0);
}
set_volume_sizes(volume,meta->length);
dummy[0]=meta->start[0];
dummy[1]=meta->start[1];
dummy[2]=meta->start[2];
set_volume_starts(volume,dummy);
dummy[0]=meta->step[0];
dummy[1]=meta->step[1];
dummy[2]=meta->step[2];
set_volume_separations(volume,dummy);
alloc_volume_data(volume);
get_volume(image, volume, meta->length);
if(!binary_mask)
output_volume( filename, NC_FLOAT,FALSE,min, max,volume,meta->history,(minc_output_options *)NULL);
else
output_volume( filename, NC_BYTE,FALSE,0, 1.0,volume,meta->history,(minc_output_options *)NULL);
delete_volume(volume);
return STATUS_OK;
}
image_metadata * read_minc(char *filename, float **image, int *sizes){
VIO_Volume volume;
VIO_Real dummy[3];
image_metadata *meta;
if( input_volume(filename, 3, NULL, NC_UNSPECIFIED, FALSE, 0.0, 0.0, TRUE, &volume, (minc_input_options *) NULL ) != VIO_OK )
return( NULL );
meta = (image_metadata *)calloc( 1 , sizeof(image_metadata) ) ;
meta->start = calloc(3,sizeof(float));
meta->step = calloc(3,sizeof(float));
meta->length = calloc(3,sizeof(int));
get_volume_sizes(volume,sizes);
*image=malloc(sizes[0]*sizes[1]*sizes[2]*sizeof(**image));
set_volume(*image, volume, sizes);
meta->length[0]=sizes[0];
meta->length[1]=sizes[1];
meta->length[2]=sizes[2];
get_volume_starts(volume,dummy);
meta->start[0]=dummy[0];
meta->start[1]=dummy[1];
meta->start[2]=dummy[2];
get_volume_separations(volume,dummy);
meta->step[0]=dummy[0];
meta->step[1]=dummy[1];
meta->step[2]=dummy[2];
delete_volume(volume);
return meta;
}