-
Notifications
You must be signed in to change notification settings - Fork 3
/
rgb_to_minc.c
174 lines (145 loc) · 4.58 KB
/
rgb_to_minc.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
166
167
168
169
170
171
172
173
174
#include <volume_io/internal_volume_io.h>
#include <bicpl.h>
#include <bicpl/images.h>
private Volume convert_pixels_to_volume(
int n_components,
int n_slices,
int n_dimensions,
nc_type vol_type,
pixels_struct pixels[] );
int main(
int argc,
char *argv[] )
{
Volume volume;
STRING input_filename, output_filename;
int n_slices, n_components;
pixels_struct *pixels;
nc_type vol_type;
BOOLEAN two_d_allowed;
initialize_argument_processing( argc, argv );
if( !get_string_argument( NULL, &output_filename ) ||
!get_int_argument( 0, &n_components ) )
{
print( "Usage: %s output.mnc 3|4|23|24 input1.rgb input2.rgb ...\n", argv[0] );
return( 1 );
}
if( n_components > 100 )
{
vol_type = NC_FLOAT;
n_components -= 100;
}
else
vol_type = NC_BYTE;
if( n_components > 20 )
{
two_d_allowed = TRUE;
n_components -= 20;
}
else
{
two_d_allowed = FALSE;
}
n_slices = 0;
pixels = NULL;
while( get_string_argument( "", &input_filename ) )
{
SET_ARRAY_SIZE( pixels, n_slices, n_slices+1, DEFAULT_CHUNK_SIZE );
if( input_rgb_file( input_filename, &pixels[n_slices] ) != OK )
return( 1 );
++n_slices;
}
volume = convert_pixels_to_volume( n_components, n_slices,
(two_d_allowed && n_slices == 1) ? 2 : 3,
vol_type, pixels );
if( volume != NULL )
{
(void) output_volume( output_filename, NC_UNSPECIFIED, FALSE,
0.0, 0.0,
volume, "Converted from pixels",
(minc_output_options *) NULL );
delete_volume( volume );
}
return( 0 );
}
private Volume convert_pixels_to_volume(
int n_components,
int n_slices,
int n_dimensions,
nc_type vol_type,
pixels_struct pixels[] )
{
int i, x, y, z, sizes[4];
Real r, g, b, a;
static char *dim_names2[] = { MIxspace, MIyspace, MIvector_dimension };
static char *dim_names3[] = { MIzspace,
MIxspace, MIyspace, MIvector_dimension };
Volume volume;
Colour colour;
volume = create_volume( n_dimensions+(n_components>1?1:0),
n_dimensions == 2 ? dim_names2 : dim_names3,
vol_type, FALSE, 0.0, 0.0 );
set_volume_real_range( volume, 0.0, 1.0 );
for_less( i, 1, n_slices )
{
if( pixels[i].x_size != pixels[0].x_size ||
pixels[i].y_size != pixels[0].y_size )
{
print_error( "Pixel sizes do not match.\n" );
return( NULL );
}
}
if( n_dimensions == 2 )
{
sizes[0] = pixels[0].x_size;
sizes[1] = pixels[0].y_size;
sizes[2] = n_components;
}
else
{
sizes[0] = n_slices;
sizes[1] = pixels[0].x_size;
sizes[2] = pixels[0].y_size;
sizes[3] = n_components;
}
set_volume_sizes( volume, sizes );
alloc_volume_data( volume );
for_less( x, 0, pixels[0].x_size )
for_less( y, 0, pixels[0].y_size )
for_less( z, 0, n_slices )
{
colour = PIXEL_RGB_COLOUR(pixels[z],x,y);
if( n_components == 1 )
r = (Real) get_Colour_luminance( colour ) / 255.0;
else
{
r = (Real) get_Colour_r(colour) / 255.0;
g = (Real) get_Colour_g(colour) / 255.0;
b = (Real) get_Colour_b(colour) / 255.0;
a = (Real) get_Colour_a(colour) / 255.0;
}
if( n_dimensions == 2 )
{
set_volume_real_value( volume, x, y, 0, 0, 0, r );
if( n_components > 1 )
{
set_volume_real_value( volume, x, y, 1, 0, 0, g );
set_volume_real_value( volume, x, y, 2, 0, 0, b );
}
if( n_components == 4 )
set_volume_real_value( volume, x, y, 3, 0, 0, a );
}
else
{
set_volume_real_value( volume, z, x, y, 0, 0, r );
if( n_components > 1 )
{
set_volume_real_value( volume, z, x, y, 1, 0, g );
set_volume_real_value( volume, z, x, y, 2, 0, b );
}
if( n_components == 4 )
set_volume_real_value( volume, z, x, y, 3, 0, a );
}
}
return( volume );
}