-
Notifications
You must be signed in to change notification settings - Fork 11
/
gen_xf_io.c
741 lines (597 loc) · 22.7 KB
/
gen_xf_io.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/* ----------------------------------------------------------------------------
@COPYRIGHT :
Copyright 1993,1994,1995 David MacDonald,
McConnell Brain Imaging Centre,
Montreal Neurological Institute, McGill University.
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies. The author and McGill University
make no representations about the suitability of this
software for any purpose. It is provided "as is" without
express or implied warranty.
---------------------------------------------------------------------------- */
#include <internal_volume_io.h>
/*--------------------- file format keywords ------------------------------ */
static const STRING TRANSFORM_FILE_HEADER = "MNI Transform File";
static const STRING TYPE_STRING = "Transform_Type";
static const STRING LINEAR_TRANSFORM_STRING = "Linear_Transform";
static const STRING LINEAR_TYPE = "Linear";
static const STRING THIN_PLATE_SPLINE_STRING =
"Thin_Plate_Spline_Transform";
static const STRING INVERT_FLAG_STRING = "Invert_Flag";
static const STRING TRUE_STRING = "True";
static const STRING FALSE_STRING = "False";
static const STRING N_DIMENSIONS_STRING = "Number_Dimensions";
static const STRING POINTS_STRING = "Points";
static const STRING DISPLACEMENTS_STRING = "Displacements";
static const STRING GRID_TRANSFORM_STRING = "Grid_Transform";
static const STRING DISPLACEMENT_VOLUME = "Displacement_Volume";
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_default_transform_file_suffix
@INPUT :
@OUTPUT :
@RETURNS : "xfm"
@DESCRIPTION: Returns the default transform file suffix.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
VIOAPI STRING get_default_transform_file_suffix( void )
{
return( "xfm" );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : output_one_transform
@INPUT : file
filename \ these two used to manufacture unique filenames
volume_count / for grid transform volume files.
invert - whether to invert the transform
transform
@OUTPUT :
@RETURNS :
@DESCRIPTION: Outputs a transform to the MNI transform file.
: Increment *volume_count.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED : Feb 21, 1995 David MacDonald : added grid transforms
---------------------------------------------------------------------------- */
static void output_one_transform(
FILE *file,
STRING filename,
int *volume_count,
BOOLEAN invert,
General_transform *transform )
{
int i, c, trans;
Transform *lin_transform;
STRING volume_filename, base_filename, prefix_filename;
switch( transform->type )
{
case LINEAR:
(void) fprintf( file, "%s = %s;\n", TYPE_STRING, LINEAR_TYPE );
(void) fprintf( file, "%s =\n", LINEAR_TRANSFORM_STRING );
if( invert )
lin_transform = get_inverse_linear_transform_ptr( transform );
else
lin_transform = get_linear_transform_ptr( transform );
for_less( i, 0, 3 )
{
(void) fprintf( file, " %.15g %.15g %.15g %.15g",
Transform_elem(*lin_transform,i,0),
Transform_elem(*lin_transform,i,1),
Transform_elem(*lin_transform,i,2),
Transform_elem(*lin_transform,i,3) );
if( i == 2 )
(void) fprintf( file, ";" );
(void) fprintf( file, "\n" );
}
break;
case THIN_PLATE_SPLINE:
(void) fprintf( file, "%s = %s;\n", TYPE_STRING,
THIN_PLATE_SPLINE_STRING);
if( transform->inverse_flag )
invert = !invert;
if( invert )
(void) fprintf( file, "%s = %s;\n", INVERT_FLAG_STRING,TRUE_STRING);
(void) fprintf( file, "%s = %d;\n", N_DIMENSIONS_STRING,
transform->n_dimensions );
(void) fprintf( file, "%s =\n", POINTS_STRING );
for_less( i, 0, transform->n_points )
{
for_less( c, 0, transform->n_dimensions )
(void) fprintf( file, " %.15g", transform->points[i][c] );
if( i == transform->n_points-1 )
(void) fprintf( file, ";" );
(void) fprintf( file, "\n" );
}
(void) fprintf( file, "%s =\n", DISPLACEMENTS_STRING );
for_less( i, 0, transform->n_points + transform->n_dimensions + 1 )
{
for_less( c, 0, transform->n_dimensions )
(void) fprintf( file, " %.15g", transform->displacements[i][c]);
if( i == transform->n_points + transform->n_dimensions + 1 - 1 )
(void) fprintf( file, ";" );
(void) fprintf( file, "\n" );
}
break;
case GRID_TRANSFORM:
(void) fprintf( file, "%s = %s;\n", TYPE_STRING,
GRID_TRANSFORM_STRING );
if( transform->inverse_flag )
invert = !invert;
if( invert )
(void) fprintf( file, "%s = %s;\n", INVERT_FLAG_STRING,TRUE_STRING);
/*--- the volume will be stored in a file of the same prefix as this
transform, but ending in _grid.mnc */
if( filename == NULL || string_length(filename) == 0 )
prefix_filename = create_string( "grid" );
else
{
prefix_filename = create_string( filename );
i = string_length( prefix_filename ) - 1;
while( i > 0 && prefix_filename[i] != '.' &&
prefix_filename[i] != '/' )
--i;
if( i >= 0 && prefix_filename[i] == '.' )
prefix_filename[i] = END_OF_STRING;
}
/*--- write out the volume filename to the transform file */
volume_filename = alloc_string( string_length(prefix_filename) +
100 );
(void) sprintf( volume_filename, "%s_grid_%d.mnc", prefix_filename,
*volume_count );
/* Increment the volume counter as a side-effect to ensure that grid
* files have different names.
*/
(*volume_count)++;
/*--- decide where to write the volume file */
base_filename = remove_directories_from_filename( volume_filename );
(void) fprintf( file, "%s = %s;\n", DISPLACEMENT_VOLUME, base_filename);
/*--- write the volume file */
(void) output_volume( volume_filename,
MI_ORIGINAL_TYPE, FALSE, 0.0, 0.0,
(Volume) transform->displacement_volume,
NULL, NULL );
delete_string( prefix_filename );
delete_string( volume_filename );
delete_string( base_filename );
break;
case USER_TRANSFORM:
print_error( "Cannot output user transformation.\n" );
output_comments( file, "User transform goes here." );
break;
case CONCATENATED_TRANSFORM:
if( transform->inverse_flag )
invert = !invert;
if( invert )
{
for( trans = get_n_concated_transforms(transform)-1; trans >= 0;
--trans )
{
output_one_transform( file, filename, volume_count, invert,
get_nth_general_transform(transform,trans) );
}
}
else
{
for_less( trans, 0, get_n_concated_transforms(transform) )
{
output_one_transform( file, filename, volume_count, invert,
get_nth_general_transform(transform,trans) );
}
}
break;
}
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : output_transform
file
filename \ these two args used to manufacture unique
volume_count_ptr / filenames for grid transform volumes
comments - can be null
transform
@INPUT :
@OUTPUT :
@RETURNS : OK or ERROR
@DESCRIPTION: Outputs the transform to the file in MNI transform format.
The filename is used to define the prefix for writing out
volumes that are part of grid transforms. The volume_count_ptr
is used to give each volume written in a given file a unique
index, and therefore a unique filename.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED : Feb. 21, 1995 D. MacDonald
---------------------------------------------------------------------------- */
VIOAPI Status output_transform(
FILE *file,
STRING filename,
int *volume_count_ptr,
STRING comments,
General_transform *transform )
{
int volume_count;
/* --- parameter checking */
if( file == NULL )
{
print_error( "output_transform(): passed NULL FILE ptr.\n" );
return( ERROR );
}
/* --- okay write the file */
(void) fprintf( file, "%s\n", TRANSFORM_FILE_HEADER );
output_comments( file, comments );
(void) fprintf( file, "\n" );
/*--- if the user has not initialized the volume count, then do so */
if( volume_count_ptr == NULL )
{
volume_count = 0;
volume_count_ptr = &volume_count;
}
output_one_transform( file, filename, volume_count_ptr, FALSE, transform );
return( OK );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : input_one_transform
@INPUT : file
filename - used to get relative paths
@OUTPUT : transform
@RETURNS : OK or ERROR
@DESCRIPTION: Inputs a transform from the file.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED : Feb. 21, 1995 David MacDonald - added grid transforms
---------------------------------------------------------------------------- */
static Status input_one_transform(
FILE *file,
STRING filename,
General_transform *transform )
{
Status status;
int i, j, n_points, n_dimensions;
Real **points, **displacements;
Real value, *points_1d;
STRING type_name, str, volume_filename, directory, tmp_filename;
Volume volume;
Transform linear_transform;
Transform_types type;
BOOLEAN inverse_flag;
General_transform inverse;
minc_input_options options;
inverse_flag = FALSE;
/* --- read the type of transform */
status = mni_input_keyword_and_equal_sign( file, TYPE_STRING, FALSE );
if( status != OK )
return( status );
if( mni_input_string( file, &type_name, (char) ';', (char) 0 ) != OK )
{
print_error( "input_transform(): missing transform type.\n");
return( ERROR );
}
if( mni_skip_expected_character( file, (char) ';' ) != OK )
return( ERROR );
if( equal_strings( type_name, LINEAR_TYPE ) )
type = LINEAR;
else if( equal_strings( type_name, THIN_PLATE_SPLINE_STRING ) )
type = THIN_PLATE_SPLINE;
else if( equal_strings( type_name, GRID_TRANSFORM_STRING ) )
type = GRID_TRANSFORM;
else
{
delete_string( type_name );
print_error( "input_transform(): invalid transform type.\n");
return( ERROR );
}
delete_string( type_name );
/* --- read the next string */
if( mni_input_string( file, &str, (char) '=', (char) 0 ) != OK )
return( ERROR );
if( equal_strings( str, INVERT_FLAG_STRING ) )
{
delete_string( str );
if( mni_skip_expected_character( file, (char) '=' ) != OK )
return( ERROR );
if( mni_input_string( file, &str, (char) ';', (char) 0 ) != OK )
return( ERROR );
if( mni_skip_expected_character( file, (char) ';' ) != OK )
{
delete_string( str );
return( ERROR );
}
if( equal_strings( str, TRUE_STRING ) )
inverse_flag = TRUE;
else if( equal_strings( str, FALSE_STRING ) )
inverse_flag = FALSE;
else
{
delete_string( str );
print_error( "Expected %s or %s after %s =\n",
TRUE_STRING, FALSE_STRING, INVERT_FLAG_STRING );
return( ERROR );
}
delete_string( str );
if( mni_input_string( file, &str, (char) '=', (char) 0 ) != OK )
return( ERROR );
}
switch( type )
{
case LINEAR:
if( !equal_strings( str, LINEAR_TRANSFORM_STRING ) )
{
print_error( "Expected %s =\n", LINEAR_TRANSFORM_STRING );
delete_string( str );
return( ERROR );
}
delete_string( str );
if( mni_skip_expected_character( file, (char) '=' ) != OK )
return( ERROR );
make_identity_transform( &linear_transform );
/* now read the 3 lines of transforms */
for_less( i, 0, 3 )
{
for_less( j, 0, 4 )
{
if( mni_input_real( file, &value ) != OK )
{
print_error(
"input_transform(): error reading transform elem [%d,%d]\n",
i+1, j+1 );
return( ERROR );
}
Transform_elem(linear_transform,i,j) = value;
}
}
if( mni_skip_expected_character( file, (char) ';' ) != OK )
return( ERROR );
create_linear_transform( transform, &linear_transform );
break;
case THIN_PLATE_SPLINE:
/* --- read Number_Dimensions = 3; */
if( !equal_strings( str, N_DIMENSIONS_STRING ) )
{
print_error( "Expected %s =\n", N_DIMENSIONS_STRING );
delete_string( str );
return( ERROR );
}
delete_string( str );
if( mni_skip_expected_character( file, (char) '=' ) != OK )
return( ERROR );
if( mni_input_int( file, &n_dimensions ) != OK )
return( ERROR );
if( mni_skip_expected_character( file, (char) ';' ) != OK )
return( ERROR );
/* --- read Points = x y z x y z .... ; */
if( mni_input_keyword_and_equal_sign( file, POINTS_STRING, TRUE ) != OK)
return( ERROR );
if( mni_input_reals( file, &n_points, &points_1d ) != OK )
return( ERROR );
if( n_points % n_dimensions != 0 )
{
print_error(
"Number of points (%d) must be multiple of number of dimensions (%d)\n",
n_points, n_dimensions );
return( ERROR );
}
n_points = n_points / n_dimensions;
ALLOC2D( points, n_points, n_dimensions );
for_less( i, 0, n_points )
{
for_less( j, 0, n_dimensions )
{
points[i][j] = points_1d[IJ(i,j,n_dimensions)];
}
}
FREE( points_1d );
/* --- allocate and input the displacements */
ALLOC2D( displacements, n_points + n_dimensions + 1, n_dimensions );
if( mni_input_keyword_and_equal_sign( file, DISPLACEMENTS_STRING, TRUE )
!= OK )
return( ERROR );
for_less( i, 0, n_points + n_dimensions + 1 )
{
for_less( j, 0, n_dimensions )
{
if( mni_input_real( file, &value ) != OK )
{
print_error( "Expected more displacements.\n" );
return( ERROR );
}
displacements[i][j] = value;
}
}
if( mni_skip_expected_character( file, (char) ';' ) != OK )
return( ERROR );
create_thin_plate_transform_real( transform, n_dimensions,
n_points, points, displacements );
FREE2D( points );
FREE2D( displacements );
break;
case GRID_TRANSFORM:
/*--- read the displacement volume filename */
if( !equal_strings( str, DISPLACEMENT_VOLUME ) )
{
print_error( "Expected %s =\n", DISPLACEMENT_VOLUME );
delete_string( str );
return( ERROR );
}
delete_string( str );
if( mni_skip_expected_character( file, (char) '=' ) != OK )
return( ERROR );
if( mni_input_string( file, &volume_filename,
(char) ';', (char) 0 ) != OK )
return( ERROR );
if( mni_skip_expected_character( file, (char) ';' ) != OK )
{
delete_string( volume_filename );
return( ERROR );
}
/*--- if the volume filename is relative, add the required directory */
if( volume_filename[0] != '/' && filename != NULL )
{
directory = extract_directory( filename );
if( string_length(directory) > 0 )
{
tmp_filename = concat_strings( directory, "/" );
concat_to_string( &tmp_filename, volume_filename );
replace_string( &volume_filename, tmp_filename );
}
delete_string( directory );
}
/*--- input the displacement volume */
set_default_minc_input_options( &options );
set_minc_input_vector_to_scalar_flag( &options, FALSE );
if( input_volume( volume_filename, 4, NULL,
MI_ORIGINAL_TYPE, FALSE, 0.0, 0.0,
TRUE, &volume, &options ) != OK )
{
delete_string( volume_filename );
return( ERROR );
}
delete_string( volume_filename );
/*--- create the transform */
create_grid_transform_no_copy( transform, volume );
break;
}
if( inverse_flag )
{
create_inverse_general_transform( transform, &inverse );
delete_general_transform( transform );
*transform = inverse;
}
return( OK );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : input_transform
@INPUT : file
filename - used to define directory for relative filename
@OUTPUT : transform
@RETURNS : OK or ERROR
@DESCRIPTION: Inputs the transform from the file.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED : Feb. 21, 1995 D. MacDonald
---------------------------------------------------------------------------- */
VIOAPI Status input_transform(
FILE *file,
STRING filename,
General_transform *transform )
{
Status status;
int n_transforms;
STRING line;
General_transform next, concated;
/* parameter checking */
if( file == (FILE *) 0 )
{
print_error( "input_transform(): passed NULL FILE ptr.\n");
return( ERROR );
}
/* okay read the header */
if( mni_input_string( file, &line, (char) 0, (char) 0 ) != OK )
{
delete_string( line );
print_error( "input_transform(): could not read header in file.\n");
return( ERROR );
}
if( !equal_strings( line, TRANSFORM_FILE_HEADER ) )
{
delete_string( line );
print_error( "input_transform(): invalid header in file.\n");
return( ERROR );
}
delete_string( line );
n_transforms = 0;
while( (status = input_one_transform( file, filename, &next )) == OK )
{
if( n_transforms == 0 )
*transform = next;
else
{
concat_general_transforms( transform, &next, &concated );
delete_general_transform( transform );
delete_general_transform( &next );
*transform = concated;
}
++n_transforms;
}
if( status == ERROR )
{
print_error( "input_transform: error reading transform.\n" );
return( ERROR );
}
else if( n_transforms == 0 )
{
print_error( "input_transform: no transform present.\n" );
return( ERROR );
}
return( OK );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : output_transform_file
@INPUT : filename
comments
transform
@OUTPUT :
@RETURNS : OK or ERROR
@DESCRIPTION: Opens the file, outputs the transform, and closes the file.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
VIOAPI Status output_transform_file(
STRING filename,
STRING comments,
General_transform *transform )
{
Status status;
FILE *file;
int volume_count;
status = open_file_with_default_suffix( filename,
get_default_transform_file_suffix(),
WRITE_FILE, ASCII_FORMAT, &file );
volume_count = 0;
if( status == OK )
status = output_transform( file, filename, &volume_count,
comments, transform );
if( status == OK )
status = close_file( file );
return( status );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : input_transform_file
@INPUT : filename
@OUTPUT : transform
@RETURNS : OK or ERROR
@DESCRIPTION: Opens the file, inputs the transform, and closes the file.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
VIOAPI Status input_transform_file(
STRING filename,
General_transform *transform )
{
Status status;
FILE *file;
status = open_file_with_default_suffix( filename,
get_default_transform_file_suffix(),
READ_FILE, ASCII_FORMAT, &file );
if( status == OK )
status = input_transform( file, filename, transform );
if( status == OK )
status = close_file( file );
return( status );
}