00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
#include <volume_io/internal_volume_io.h>
00016
#include <bicpl/vols.h>
00017
00018
#ifndef lint
00019
static char rcsid[] =
"$Header: /software/source//libraries/bicpl/Volumes/output_free.c,v 1.22 2000/02/06 15:30:56 stever Exp $";
00020
#endif
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 public Status
output_volume_free_format(
00039 STRING prefix,
00040 Volume volume,
00041
int axis_ordering[] )
00042 {
00043 Status status;
00044 Real trans, separations[MAX_DIMENSIONS];
00045
int sizes[MAX_DIMENSIONS];
00046
int a1, a2, a3;
00047
int n_bytes_per_voxel, indices[MAX_DIMENSIONS];
00048
void *ptr;
00049 FILE *file;
00050 STRING header_filename, voxel_filename, abs_voxel_filename;
00051 STRING filename_no_dirs;
00052
int axis;
00053 progress_struct progress;
00054 General_transform *voxel_to_world;
00055 Transform *transform;
00056
00057 header_filename = concat_strings( prefix,
".fre" );
00058 abs_voxel_filename = alloc_string( string_length(prefix) + 10 );
00059 abs_voxel_filename = concat_strings( prefix,
".img" );
00060
00061 filename_no_dirs = remove_directories_from_filename( prefix );
00062 voxel_filename = concat_strings( filename_no_dirs,
".img" );
00063
00064 status = open_file( header_filename, WRITE_FILE, ASCII_FORMAT, &file );
00065
00066
if( get_volume_data_type(volume) == UNSIGNED_BYTE )
00067 n_bytes_per_voxel = 1;
00068
else
00069 n_bytes_per_voxel = 2;
00070
00071
if( status == OK )
00072 status = output_int( file, n_bytes_per_voxel );
00073
00074
if( status == OK )
00075 status = output_newline( file );
00076
00077 get_volume_sizes( volume, sizes );
00078 get_volume_separations( volume, separations );
00079
00080 voxel_to_world = get_voxel_to_world_transform( volume );
00081
00082
if( get_transform_type(voxel_to_world) == LINEAR )
00083 transform = get_linear_transform_ptr( voxel_to_world );
00084
else
00085 transform = (Transform *) NULL;
00086
00087 for_less( axis, 0, N_DIMENSIONS )
00088 {
00089
if( status == OK )
00090 {
00091
if( transform != (Transform *) NULL )
00092 {
00093 trans = Transform_elem(*transform,axis,3);
00094
if( separations[axis] < 0.0 )
00095 trans += separations[axis] * (Real) (sizes[axis]-1);
00096 }
00097
else
00098 trans = 0.0;
00099
00100 status = output_float( file, (
float) trans );
00101 }
00102 }
00103
00104
if( status == OK )
00105 status = output_newline( file );
00106
00107 for_less( axis, 0, N_DIMENSIONS )
00108 {
00109
if( status == OK )
00110 status = output_int( file, sizes[axis_ordering[axis]] );
00111
00112
if( status == OK )
00113 status = output_real( file, separations[axis_ordering[axis]]);
00114
00115
if( status == OK )
00116 status = output_character( file, (
char)
' ' );
00117
00118
if( status == OK )
00119 status = output_character( file, (
char) (
'x'+axis_ordering[axis]) );
00120
00121
if( status == OK )
00122 status = output_newline( file );
00123 }
00124
00125
if( status == OK )
00126 status = output_string( file, voxel_filename );
00127
00128
if( status == OK )
00129 status = output_newline( file );
00130
00131
if( status == OK )
00132 status = close_file( file );
00133
00134
if( status == OK )
00135 status = open_file( abs_voxel_filename, WRITE_FILE, BINARY_FORMAT,
00136 &file );
00137
00138
if( status == OK )
00139 {
00140 a1 = axis_ordering[0];
00141 a2 = axis_ordering[1];
00142 a3 = axis_ordering[2];
00143
00144 initialize_progress_report( &progress,
FALSE, sizes[a1] * sizes[a2],
00145
"Writing Volume" );
00146
00147 for_less( indices[a1], 0, sizes[a1] )
00148 {
00149 for_less( indices[a2], 0, sizes[a2] )
00150 {
00151 for_less( indices[a3], 0, sizes[a3] )
00152 {
00153 GET_VOXEL_PTR_3D( ptr, volume,
00154 indices[X],indices[Y],indices[Z] );
00155 status = io_binary_data( file, WRITE_FILE,
00156 ptr, (size_t) n_bytes_per_voxel,1);
00157 }
00158
00159 update_progress_report( &progress, indices[a1] * sizes[a2] +
00160 indices[a2] + 1 );
00161 }
00162 }
00163
00164 terminate_progress_report( &progress );
00165 }
00166
00167 delete_string( header_filename );
00168 delete_string( voxel_filename );
00169 delete_string( abs_voxel_filename );
00170 delete_string( filename_no_dirs );
00171
00172
return( status );
00173 }