forked from NIFTI-Imaging/nifti_clib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nifti_tool.c
3381 lines (2943 loc) · 118 KB
/
nifti_tool.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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------------------*/
/*! \file nifti_tool.c
* \brief a tool for nifti file perusal, manipulation and copying
* written by Rick Reynolds, SSCC, NIMH, January 2005
* <pre>
*
* usage: nifti_tool [options] -infiles files...
*
* Via this tool, one should be able to:
*
* - copy a set of volumes (sub-bricks) from one dataset to another
* - copy a dataset, restricting some dimensions to given indices
*
* - display the contents of a nifti_image (or various fields)
* - display the contents of a nifti1_header (or various fields)
* - display AFNI extensions (they are text)
* - display the time series at coordinates i,j,k
* - display the data from any collapsed image
*
* - do a diff on two nifti_image structs (or various fields)
* - do a diff on two nifti1_header structs (or various fields)
*
* - add an AFNI extension
* - remove any extension
*
* - modify any field(s) of a nifti_image
* - modify any field(s) of a nifti1_struct
*
* usage forms:
*
* nifti_tool -help
* nifti_tool -help_hdr
* nifti_tool -help_nim
* nifti_tool -hist
* nifti_tool -ver
* nifti_tool -nifti_hist
* nifti_tool -nifti_ver
*
* nifti_tool -check_hdr -infiles f1 ...
* nifti_tool -check_nim -infiles f1 ...
* nifti_tool -disp_exts -infiles f1 ...
* nifti_tool -disp_hdr [-field fieldname] [...] -infiles f1 ...
* nifti_tool -disp_nim [-field fieldname] [...] -infiles f1 ...
* nifti_tool -disp_ts I J K [-dci_lines] -infiles f1 ...
* nifti_tool -disp_ci I J K T U V W [-dci_lines] -infiles f1 ...
*
* nifti_tool -diff_hdr [-field fieldname] [...] -infiles f1 f2
* nifti_tool -diff_nim [-field fieldname] [...] -infiles f1 f2
*
* nifti_tool -add_afni_ext "extension in quotes" -infiles f1 ...
* nifti_tool -add_comment_ext "extension in quotes" -infiles f1 ...
* nifti_tool -rm_ext ext_index -infiles f1 ...
*
* nifti_tool -mod_hdr [-mod_field fieldname new_val] [...] -infiles f1 ...
* nifti_tool -mod_nim [-mod_field fieldname new_val] [...] -infiles f1 ...
*
* </pre> */
/*-------------------------------------------------------------------------*/
/*! module history */
static char * g_history[] =
{
"----------------------------------------------------------------------\n"
"nifti_tool modification history:\n"
"\n",
"0.1 30 December 2004 [rickr]\n"
" (Rick Reynolds of the National Institutes of Health, SSCC/DIRP/NIMH)\n"
" - skeleton version: options read and printed\n"
"\n",
"1.0 07 January 2005 [rickr]\n"
" - initial release version\n"
"\n",
"1.1 14 January 2005 [rickr]\n"
" - changed all non-error/non-debug output from stderr to stdout\n"
" note: creates a mis-match between normal output and debug messages\n"
" - modified act_diff_hdrs and act_diff_nims to do the processing in\n"
" lower-level functions\n",
" - added functions diff_hdrs, diff_hdrs_list, diff_nims, diff_nims_list\n"
" - added function get_field, to return a struct pointer via a fieldname\n"
" - made 'quiet' output more quiet (no description on output)\n"
" - made hdr and nim_fields arrays global, so do not pass in main()\n"
" - return (from main()) after first act_diff() difference\n"
"\n",
"1.2 9 February 2005 [rickr] - minor\n"
" - defined a local NTL_FERR macro (so it does not come from nifti1_io.h)\n"
" - added new set_byte_order parameter to nifti_set_filenames\n"
"\n",
"1.3 23 February 2005 [rickr] - sourceforge.net merge\n"
" - moved to utils directory\n"
" - added simple casts of 3 pointers for -pedantic warnings\n"
" - added a doxygen comment for the file\n"
"\n",
"1.4 02 March 2005 [rickr] - small update\n"
" - no validation in nifti_read_header calls\n"
"\n",
"1.5 05 April 2005 [rickr] - small update\n"
" - refuse mod_hdr for gzipped files (we cannot do partial overwrites)\n"
"\n",
"1.6 08 April 2005 [rickr] - added cbl, cci and dts functionality\n"
" - added -cbl: 'copy brick list' dataset copy functionality\n"
" - added -ccd: 'copy collapsed data' dataset copy functionality\n"
" - added -disp_ts: 'disp time series' data display functionality\n"
" - moved raw data display to disp_raw_data()\n"
"\n",
"1.7 14 April 2005 [rickr] - added data display functionality\n"
" - added -dci: 'display collapsed image' functionality\n"
" - modified -dts to use -dci\n"
" - modified and updated the help in use_full()\n"
" - changed copy_collapsed_dims to copy_collapsed_image, etc.\n",
" - fixed problem in disp_raw_data() for printing NT_DT_CHAR_PTR\n"
" - modified act_disp_ci():\n"
" o was act_disp_ts(), now displays arbitrary collapsed image data\n"
" o added missed debug filename act_disp_ci()\n"
" o can now save free() of data pointer for end of file loop\n",
" - modified disp_raw_data()\n"
" o takes a flag for whether to print newline\n"
" o trailing spaces and zeros are removed from printing floats\n"
" - added clear_float_zeros(), to remove trailing zeros\n"
"\n",
"1.8 19 April 2005 [rickr] - COMMENT extensions\n"
" - added int_list struct, and keep_hist,etypes,command fields to nt_opts\n"
" - added -add_comment_ext action\n"
" - allowed for removal of multiple extensions, including option of ALL\n"
" - added -keep_hist option, to store the command as a COMMENT extension\n",
" (includes fill_cmd_string() and add_int(), is done for all actions)\n"
" - added remove_ext_list(), for removing a list of extensions by indices\n"
" - added -strip action, to strip all extensions and descrip fields\n"
"\n",
"1.9 25 August 2005 [rickr] - const/string cleanup for warnings\n"
"\n",
"1.10 18 November 2005 [rickr] - added check_hdr and check_nim actions\n"
"\n",
"1.11 31 January 2006 [rickr] - check for new vox_offset in act_mod_hdrs\n"
"\n",
"1.12 02 March 2006 [rickr]\n"
" - in act_cbl(), check for nt = 0 because of niftilib update 1.17\n"
"----------------------------------------------------------------------\n"
};
static char g_version[] = "version 1.12 (March 2, 2006)";
static int g_debug = 1;
#define _NIFTI_TOOL_C_
#include "nifti1_io.h"
#include "nifti_tool.h"
#define NTL_FERR(func,msg,file) \
fprintf(stderr,"** ERROR (%s): %s '%s'\n",func,msg,file)
/* these are effectively constant, and are built only for verification */
field_s g_hdr_fields[NT_HDR_NUM_FIELDS]; /* nifti_1_header fields */
field_s g_nim_fields[NT_NIM_NUM_FIELDS]; /* nifti_image fields */
int main( int argc, char * argv[] )
{
nt_opts opts;
int rv;
if( (rv = process_opts(argc, argv, &opts)) != 0)
return rv;
if( (rv = verify_opts(&opts, argv[0])) != 0 )
return rv;
/* now perform the requested action(s) */
if( (rv = fill_hdr_field_array(g_hdr_fields)) != 0 )
return rv;
if( (rv = fill_nim_field_array(g_nim_fields)) != 0 )
return rv;
/* 'check' functions, first */
if( opts.check_hdr || opts.check_nim ) /* allow for both */
return act_check_hdrs(&opts);
/* copy or dts functions */
if( opts.cbl ) return act_cbl(&opts); /* just return */
if( opts.cci ) return act_cci(&opts);
if( opts.dts || opts.dci ) return act_disp_ci(&opts);
/* perform modifications early, in case we allow multiple actions */
if( opts.strip && ((rv = act_strip (&opts)) != 0) ) return rv;
if( opts.add_exts && ((rv = act_add_exts (&opts)) != 0) ) return rv;
if( opts.rm_exts && ((rv = act_rm_ext (&opts)) != 0) ) return rv;
if( opts.mod_hdr && ((rv = act_mod_hdrs (&opts)) != 0) ) return rv;
if( opts.mod_nim && ((rv = act_mod_nims (&opts)) != 0) ) return rv;
/* if a diff, return wither a difference exists (like the UNIX command) */
if( opts.diff_hdr && ((rv = act_diff_hdrs(&opts)) != 0) ) return rv;
if( opts.diff_nim && ((rv = act_diff_nims(&opts)) != 0) ) return rv;
/* last action type is display */
if( opts.disp_exts && ((rv = act_disp_exts(&opts)) != 0) ) return rv;
if( opts.disp_hdr && ((rv = act_disp_hdrs(&opts)) != 0) ) return rv;
if( opts.disp_nim && ((rv = act_disp_nims(&opts)) != 0) ) return rv;
return 0;
}
/*----------------------------------------------------------------------
* process user options, return 0 on success
*----------------------------------------------------------------------*/
int process_opts( int argc, char * argv[], nt_opts * opts )
{
int ac;
memset(opts, 0, sizeof(*opts));
opts->prefix = NULL;
opts->debug = 1; /* init debug level to basic output */
if( argc < 2 ) return usage(argv[0], USE_SHORT);
/* terminal options are first, the rest are sorted */
for( ac = 1; ac < argc; ac++ )
{
if( ! strncmp(argv[ac], "-help_hdr", 9) )
return usage(argv[0], USE_FIELD_HDR);
if( ! strncmp(argv[ac], "-help_nim", 9) )
return usage(argv[0], USE_FIELD_NIM);
if( ! strncmp(argv[ac], "-help", 5) )
return usage(argv[0], USE_FULL);
else if( ! strncmp(argv[ac], "-hist", 5) )
return usage(argv[0], USE_HIST);
else if( ! strncmp(argv[ac], "-ver", 2) )
return usage(argv[0], USE_VERSION);
else if( ! strncmp(argv[ac], "-nifti_hist", 11) )
{
nifti_disp_lib_hist();
return 1;
}
else if( ! strncmp(argv[ac], "-nifti_ver", 10) )
{
nifti_disp_lib_version();
return 1;
}
/* begin normal execution options... */
else if( ! strncmp(argv[ac], "-add_afni_ext", 9) )
{
ac++;
CHECK_NEXT_OPT(ac, argc, "-add_afni_ext");
if( add_string(&opts->elist, argv[ac]) ) return 1; /* add extension */
if( add_int(&opts->etypes, NIFTI_ECODE_AFNI) ) return 1;
opts->add_exts = 1;
}
else if( ! strncmp(argv[ac], "-add_comment_ext", 9) )
{
ac++;
CHECK_NEXT_OPT(ac, argc, "-add_comment_ext");
if( add_string(&opts->elist, argv[ac]) ) return 1; /* add extension */
if( add_int(&opts->etypes, NIFTI_ECODE_COMMENT) ) return 1;
opts->add_exts = 1;
}
else if( ! strncmp(argv[ac], "-check_hdr", 10) )
opts->check_hdr = 1;
else if( ! strncmp(argv[ac], "-check_nim", 10) )
opts->check_nim = 1;
else if( ! strncmp(argv[ac], "-copy_brick_list", 11) ||
! strncmp(argv[ac], "-cbl", 4) )
{
opts->cbl = 1;
}
else if( ! strncmp(argv[ac], "-copy_collapsed_image", 10) ||
! strncmp(argv[ac], "-cci", 4) )
{
/* we need to read in the 7 dimension values */
int index;
opts->ci_dims[0] = 0;
for( index = 1; index < 8; index++ )
{
ac++;
CHECK_NEXT_OPT_MSG(ac,argc,"-cci","7 dimension values are requred");
if( ! isdigit(argv[ac][0]) && strcmp(argv[ac],"-1") ){
fprintf(stderr,"** -cci param %d (= '%s') is not a valid\n"
" consider: 'nifti_tool -help'\n",index,argv[ac]);
return 1;
}
opts->ci_dims[index] = atoi(argv[ac]);
}
opts->cci = 1;
}
else if( ! strncmp(argv[ac], "-debug", 6) )
{
ac++;
CHECK_NEXT_OPT(ac, argc, "-debug");
opts->debug = atoi(argv[ac]);
}
else if( ! strncmp(argv[ac], "-diff_hdr", 8) )
opts->diff_hdr = 1;
else if( ! strncmp(argv[ac], "-diff_nim", 8) )
opts->diff_nim = 1;
else if( ! strncmp(argv[ac], "-disp_exts", 7) )
opts->disp_exts = 1;
else if( ! strncmp(argv[ac], "-disp_hdr", 8) )
opts->disp_hdr = 1;
else if( ! strncmp(argv[ac], "-disp_nim", 8) )
opts->disp_nim = 1;
else if( ! strncmp(argv[ac], "-dci_lines", 6) || /* before -dts */
! strncmp(argv[ac], "-dts_lines", 6) )
{
opts->dci_lines = 1;
}
else if( ! strncmp(argv[ac], "-disp_collapsed_image", 10) ||
! strncmp(argv[ac], "-disp_ci", 8) )
{
/* we need to read in the 7 dimension values */
int index;
opts->ci_dims[0] = 0;
for( index = 1; index < 8; index++ )
{
ac++;
CHECK_NEXT_OPT_MSG(ac,argc,"-disp_ci",
"7 dimension values are requred");
if( ! isdigit(argv[ac][0]) && strcmp(argv[ac],"-1") ){
fprintf(stderr,"** -disp_ci param %d (= '%s') is not a valid\n"
" consider: 'nifti_tool -help'\n",index,argv[ac]);
return 1;
}
opts->ci_dims[index] = atoi(argv[ac]);
}
opts->dci = 1;
}
else if( ! strncmp(argv[ac], "-disp_ts", 10) ||
! strncmp(argv[ac], "-dts", 4) )
{
/* we need to read in the ijk indices into the ci_dims array */
int index;
for( index = 1; index <= 3; index++ )
{
ac++;
CHECK_NEXT_OPT_MSG(ac,argc,"-dts","i,j,k indices are requied\n");
if( ! isdigit(argv[ac][0]) ){
fprintf(stderr,"** -dts param %d (= '%s') is not a number\n"
" consider: 'nifti_tool -help'\n",index,argv[ac]);
return 1;
}
opts->ci_dims[index] = atoi(argv[ac]);
}
/* and fill the rest of the array */
opts->ci_dims[0] = 0;
for( index = 4; index < 8; index++ ) opts->ci_dims[index] = -1;
opts->dts = 1;
}
else if( ! strncmp(argv[ac], "-field", 2) )
{
ac++;
CHECK_NEXT_OPT(ac, argc, "-field");
if( add_string(&opts->flist, argv[ac]) ) return 1; /* add field */
}
else if( ! strncmp(argv[ac], "-infiles", 3) )
{
int count;
/* for -infiles, get all next arguments until a '-' or done */
ac++;
for( count = 0; (ac < argc) && (argv[ac][0] != '-'); ac++, count++ )
if( add_string(&opts->infiles, argv[ac]) ) return 1; /* add field */
if( count > 0 && ac < argc ) ac--; /* more options to process */
if( g_debug > 2 ) fprintf(stderr,"+d have %d file names\n", count);
}
else if( ! strncmp(argv[ac], "-mod_field", 6) )
{
ac++;
CHECK_NEXT_OPT(ac, argc, "-mod_field");
if( add_string(&opts->flist, argv[ac]) ) return 1; /* add field */
ac++;
CHECK_NEXT_OPT(ac, argc, "-mod_field (2)");
if( add_string(&opts->vlist, argv[ac]) ) return 1; /* add value */
}
else if( ! strncmp(argv[ac], "-mod_hdr", 7) )
opts->mod_hdr = 1;
else if( ! strncmp(argv[ac], "-mod_nim", 7) )
opts->mod_nim = 1;
else if( ! strncmp(argv[ac], "-keep_hist", 5) )
opts->keep_hist = 1;
else if( ! strncmp(argv[ac], "-overwrite", 6) )
opts->overwrite = 1;
else if( ! strncmp(argv[ac], "-prefix", 4) )
{
ac++;
CHECK_NEXT_OPT(ac, argc, "-prefix");
opts->prefix = argv[ac];
}
else if( ! strncmp(argv[ac], "-quiet", 3) )
opts->debug = 0;
else if( ! strncmp(argv[ac], "-rm_ext", 7) )
{
ac++;
CHECK_NEXT_OPT(ac, argc, "-rm_ext");
if( strcmp(argv[ac],"ALL") == 0 ) /* special case, pass -1 */
{
if( add_string(&opts->elist, "-1") ) return 1;
}
else
{
int index = atoi(argv[ac]);
if( (index != -1) && ((index > 1000) || !isdigit(*argv[ac])) ){
fprintf(stderr,
"** '-rm_ext' requires an extension index (read '%s')\n",
argv[ac]);
return 1;
}
if( add_string(&opts->elist, argv[ac]) ) return 1;
}
opts->rm_exts = 1;
}
else if( ! strncmp(argv[ac], "-strip_extras", 6) )
opts->strip = 1;
else
{
fprintf(stderr,"** unknown option: '%s'\n", argv[ac]);
return 1;
}
}
/* verify for programming purposes */
if( opts->add_exts && ( opts->elist.len != opts->etypes.len ) )
{
fprintf(stderr,"** ext list length (%d) != etype length (%d)\n",
opts->elist.len, opts->etypes.len);
return 1;
}
g_debug = opts->debug;
nifti_set_debug_level(g_debug);
fill_cmd_string(opts, argc, argv); /* copy this command */
if( g_debug > 2 ) disp_nt_opts("options read: ", opts);
return 0;
}
/*----------------------------------------------------------------------
* verify that the options make sense
*----------------------------------------------------------------------*/
int verify_opts( nt_opts * opts, char * prog )
{
int ac, errs = 0; /* number of requested action types */
/* check that only one of disp, diff, mod or add_*_ext is used */
ac = (opts->check_hdr || opts->check_nim ) ? 1 : 0;
ac += (opts->diff_hdr || opts->diff_nim ) ? 1 : 0;
ac += (opts->disp_hdr || opts->disp_nim || opts->disp_exts) ? 1 : 0;
ac += (opts->mod_hdr || opts->mod_nim ) ? 1 : 0;
ac += (opts->add_exts || opts->rm_exts ) ? 1 : 0;
ac += (opts->strip ) ? 1 : 0;
ac += (opts->cbl ) ? 1 : 0;
ac += (opts->cci ) ? 1 : 0;
ac += (opts->dts || opts->dci ) ? 1 : 0;
if( ac < 1 )
{
fprintf(stderr,
"** no action option, so nothing to do...\n"
" (try one of '-add...', '-diff...', '-disp...' or '-mod...')\n"
" (see '%s -help' for details)\n", prog);
return 1;
}
else if( ac > 1 )
{
fprintf(stderr,
"** only one action option is allowed, please use only one of:\n"
" '-add_...', '-check_...', '-diff_...', '-disp_...',\n"
" '-mod_...', '-strip', '-dts', '-cbl' or '-cci'\n"
" (see '%s -help' for details)\n", prog);
return 1;
}
/* can modify nifti_1_header or nifti_image, but not both */
if( opts->mod_hdr && opts->mod_nim )
{
fprintf(stderr,"** cannot use both '-mod_hdr' and '-mod_nim'\n");
return 1;
}
/* can add or remove extensions, but not both */
if( opts->add_exts && opts->rm_exts )
{
fprintf(stderr,"** cannot use both '-add_*_ext' and '-rm_ext'\n");
return 1;
}
if( (opts->add_exts || opts->rm_exts) && opts->elist.len <= 0 )
{
fprintf(stderr,"** missing extensions to add or remove\n");
return 1;
}
/* if modify, then we need fields and corresponding values */
if( opts->mod_hdr || opts->mod_nim )
{
if( opts->flist.len <= 0 )
{
fprintf(stderr,"** missing field to modify (need '-mod_field' opt)\n");
return 1;
}
if( opts->flist.len != opts->vlist.len )
{
fprintf(stderr,"** error: modifying %d fields with %d values\n",
opts->flist.len, opts->vlist.len);
return 1;
}
}
/* verify the number of files given for each of 4 action types */
/* -diff_... : require nfiles == 2 */
if( opts->diff_hdr || opts->diff_nim )
{
if( opts->infiles.len != 2 )
{
fprintf(stderr,"** '-diff_XXX' options require exactly 2 inputs files\n");
return 1;
}
}
/* if we are making changes, but not overwriting... */
else if( (opts->elist.len > 0 || opts->mod_hdr || opts->mod_nim) &&
!opts->overwrite )
{
if( opts->infiles.len > 1 )
{
fprintf(stderr,"** without -overwrite, only one input file may be"
" modified at a time\n");
errs++;
}
if( ! opts->prefix )
{
fprintf(stderr,"** missing -prefix for output file\n");
errs++;
}
}
if( opts->dci_lines && ! opts->dts && ! opts->dci )
{
fprintf(stderr,"** option '-dci_lines' must only be used with '-dts'\n");
errs++;
}
if( opts->infiles.len <= 0 ) /* in any case */
{
fprintf(stderr,"** missing input files (see -infiles option)\n");
errs++;
}
if ( opts->overwrite && opts->prefix )
{
fprintf(stderr, "** please specify only one of -prefix and -overwrite\n");
errs++;
}
if( errs ) return 1;
if( g_debug > 1 ) fprintf(stderr,"+d options seem valid\n");
return 0;
}
/*----------------------------------------------------------------------
* re-assemble the command string into opts->command
*----------------------------------------------------------------------*/
int fill_cmd_string( nt_opts * opts, int argc, char * argv[])
{
char * cp;
int len, remain = NT_CMD_LEN; /* NT_CMD_LEN is max command len */
int c, ac;
int has_space; /* arguments containing space must be quoted */
int skip = 0; /* counter to skip some of the arguments */
/* get the first argument separately */
len = sprintf( opts->command, "\n command: %s", argv[0] );
cp = opts->command + len;
remain -= len;
/* get the rest, with special attention to input files */
for( ac = 1; ac < argc; ac++ )
{
if( skip ){ skip--; continue; } /* then skip these arguments */
len = strlen(argv[ac]);
if( len + 3 >= remain ) { /* extra 3 for space and possible '' */
fprintf(stderr,"FCS: no space remaining for command, continuing...\n");
return 1;
}
/* put the argument in, possibly with '' */
has_space = 0;
for( c = 0; c < len-1; c++ )
if( isspace(argv[ac][c]) ){ has_space = 1; break; }
if( has_space ) len = sprintf(cp, " '%s'", argv[ac]);
else len = sprintf(cp, " %s", argv[ac]);
remain -= len;
/* infiles is okay, but after the *next* argument, we may skip files */
/* (danger, will robinson! hack alert!) */
if( !strncmp(argv[ac-1],"-infiles",3) )
{
/* if more than 4 (just to be arbitrary) input files,
include only the first and last */
if( opts->infiles.len > 4 )
skip = opts->infiles.len - 2;
}
cp += len;
}
if( g_debug > 1 ){
fprintf(stderr,"+d filled command string, %d args, %d bytes\n",
argc, (int)(cp - opts->command));
if( g_debug > 2 ) fprintf(stderr,"%s\n", opts->command);
}
return 0;
}
/*----------------------------------------------------------------------
* - only bother to alloc one pointer at a time (don't need efficiency here)
* - return 0 on success
*----------------------------------------------------------------------*/
int add_int(int_list * ilist, int val)
{
if( ilist->len == 0 ) ilist->list = NULL; /* just to be safe */
ilist->len++;
ilist->list = (int *)realloc(ilist->list,ilist->len*sizeof(int));
if( ! ilist->list ){
fprintf(stderr,"** failed to alloc %d (int *) elements\n",ilist->len);
return -1;
}
ilist->list[ilist->len-1] = val;
return 0;
}
/*----------------------------------------------------------------------
* - do not duplicate the string
* - only bother to alloc one pointer at a time (don't need efficiency here)
* - return 0 on success
*----------------------------------------------------------------------*/
int add_string(str_list * slist, char * str)
{
if( slist->len == 0 ) slist->list = NULL; /* just to be safe */
slist->len++;
slist->list = (char **)realloc(slist->list,slist->len*sizeof(char *));
if( ! slist->list ){
fprintf(stderr,"** failed to alloc %d (char *) elements\n",slist->len);
return -1;
}
slist->list[slist->len-1] = str;
return 0;
}
/*----------------------------------------------------------------------
* display information on using the program
*----------------------------------------------------------------------*/
int usage(char * prog, int level)
{
int c, len;
if( level == USE_SHORT )
{
fprintf(stdout,"usage %s [options] -infiles files...\n", prog);
fprintf(stdout,"usage %s -help\n", prog);
}
else if( level == USE_FULL )
use_full("nifti_tool"); /* let's not allow paths in here */
else if( level == USE_HIST )
{
len = sizeof(g_history)/sizeof(char *);
for( c = 0; c < len; c++)
fputs(g_history[c], stdout);
}
else if( level == USE_FIELD_HDR )
{
field_s nhdr_fields[NT_HDR_NUM_FIELDS]; /* just do it all here */
fill_hdr_field_array(nhdr_fields);
disp_field_s_list("nifti_1_header: ", nhdr_fields, NT_HDR_NUM_FIELDS);
}
else if( level == USE_FIELD_NIM )
{
field_s nim_fields[NT_NIM_NUM_FIELDS];
fill_nim_field_array(nim_fields);
disp_field_s_list("nifti_image: ", nim_fields, NT_NIM_NUM_FIELDS);
}
else if( level == USE_VERSION )
fprintf(stdout, "%s, %s\n", prog, g_version);
else
fprintf(stdout,"** illegal level for usage(): %d\n", level);
return 1;
}
/*----------------------------------------------------------------------
* full usage
*----------------------------------------------------------------------*/
int use_full(char * prog)
{
printf(
"nifti_tool\n"
"\n"
" - display, modify or compare nifti structures in datasets\n"
" - copy a dataset by selecting a list of volumes from the original\n"
" - copy a dataset, collapsing any dimensions, each to a single index\n"
" - display a time series for a voxel, or more generally, the data\n"
" from any collapsed image, in ASCII text\n");
printf(
"\n"
" This program can be used to display information from nifti datasets,\n"
" to modify information in nifti datasets, to look for differences\n"
" between two nifti datasets (like the UNIX 'diff' command), and to copy\n"
" a dataset to a new one, either by restricting any dimensions, or by\n"
" copying a list of volumes (the time dimension) from a dataset.\n"
"\n");
printf(
" Only one action type is allowed, e.g. one cannot modify a dataset\n"
" and then take a 'diff'.\n"
"\n");
printf(
" one can display - any or all fields in the nifti_1_header structure\n"
" - any or all fields in the nifti_image structure\n"
" - the extensions in the nifti_image structure\n"
" - the time series from a 4-D dataset, given i,j,k\n"
" - the data from any collapsed image, given dims. list\n"
"\n");
printf(
" one can check - perform internal check on the nifti_1_header struct\n"
" (by nifti_hdr_looks_good())\n"
" - perform internal check on the nifti_image struct\n"
" (by nifti_nim_is_valid())\n"
"\n");
printf(
" one can modify - any or all fields in the nifti_1_header structure\n"
" - any or all fields in the nifti_image structure\n"
" add/rm - any or all extensions in the nifti_image structure\n"
" remove - all extensions and descriptions from the datasets\n"
"\n");
printf(
" one can compare - any or all field pairs of nifti_1_header structures\n"
" - any or all field pairs of nifti_image structures\n"
"\n"
" one can copy - an arbitrary list of dataset volumes (time points)\n"
" - a dataset, collapsing across arbitrary dimensions\n"
" (restricting those dimensions to the given indices)\n"
"\n");
printf(
" Note: to learn about which fields exist in either of the structures,\n"
" or to learn a field's type, size of each element, or the number\n"
" of elements in the field, use either the '-help_hdr' option, or\n"
" the '-help_nim' option. No further options are required.\n"
" ------------------------------\n");
printf(
"\n"
" usage styles:\n"
"\n"
" nifti_tool -help : show this help\n"
" nifti_tool -help_hdr : show nifti_1_header field info\n"
" nifti_tool -help_nim : show nifti_image field info\n"
"\n");
printf(
" nifti_tool -ver : show the current version\n"
" nifti_tool -hist : show the modification history\n"
" nifti_tool -nifti_ver : show the nifti library version\n"
" nifti_tool -nifti_hist : show the nifti library history\n"
"\n"
"\n");
printf(
" nifti_tool -check_hdr -infiles f1 ...\n"
" nifti_tool -check_nim -infiles f1 ...\n"
"\n");
printf(
" nifti_tool -copy_brick_list -infiles f1'[indices...]'\n"
" nifti_tool -copy_collapsed_image I J K T U V W -infiles f1\n"
"\n");
printf(
" nifti_tool -disp_hdr [-field FIELDNAME] [...] -infiles f1 ...\n"
" nifti_tool -disp_nim [-field FIELDNAME] [...] -infiles f1 ...\n"
" nifti_tool -disp_exts -infiles f1 ...\n"
" nifti_tool -disp_ts I J K [-dci_lines] -infiles f1 ...\n"
" nifti_tool -disp_ci I J K T U V W [-dci_lines] -infiles f1 ...\n"
"\n");
printf(
" nifti_tool -mod_hdr [-mod_field FIELDNAME NEW_VAL] [...] -infiles f1\n"
" nifti_tool -mod_nim [-mod_field FIELDNAME NEW_VAL] [...] -infiles f1\n"
"\n"
" nifti_tool -add_afni_ext 'extension in quotes' [...] -infiles f1\n"
" nifti_tool -add_comment_ext 'extension in quotes' [...] -infiles f1\n"
" nifti_tool -rm_ext INDEX [...] -infiles f1 ...\n"
" nifti_tool -strip_extras -infiles f1 ...\n"
"\n");
printf(
" nifti_tool -diff_hdr [-field FIELDNAME] [...] -infiles f1 f2\n"
" nifti_tool -diff_nim [-field FIELDNAME] [...] -infiles f1 f2\n"
"\n"
" ------------------------------\n");
printf(
"\n"
" selected examples:\n"
"\n"
" checks header (for problems):\n"
"\n"
" nifti_tool -check_hdr -infiles dset0.nii dset1.nii\n"
" nifti_tool -check_hdr -infiles *.nii *.hdr\n"
" nifti_tool -check_hdr -quiet -infiles *.nii *.hdr\n"
"\n");
printf(
" show header differences:\n"
"\n"
" nifti_tool -diff_hdr -field dim -field intent_code \\\n"
" -infiles dset0.nii dset1.nii \n"
"\n"
" display structures or fields:\n"
"\n");
printf(
" nifti_tool -disp_hdr -infiles dset0.nii dset1.nii dset2.nii\n"
" nifti_tool -disp_hdr -field dim -field descrip -infiles dset0.nii\n"
"\n"
" nifti_tool -disp_exts -infiles dset0.nii dset1.nii dset2.nii\n"
"\n"
" nifti_tool -disp_ts 23 0 172 -infiles dset1_time.nii\n"
"\n"
" nifti_tool -disp_ci 23 0 172 -1 0 0 0 -infiles dset1_time.nii\n"
"\n");
printf(
" copy brick list, or copy collapsed image:\n"
"\n"
" nifti_tool -cbl -prefix new_07.nii -infiles dset0.nii'[0,7]'\n"
" nifti_tool -cbl -prefix new_partial.nii \\\n"
" -infiles dset0.nii'[3..$(2)]'\n"
"\n"
" nifti_tool -cci 5 4 17 -1 -1 -1 -1 -prefix new_5_4_17.nii\n"
" nifti_tool -cci 5 0 17 -1 -1 2 -1 -keep_hist \\\n"
" -prefix new_5_0_17_2.nii\n"
"\n");
printf(
" modify the header:\n"
"\n"
" nifti_tool -mod_hdr -prefix dnew -infiles dset0.nii \\\n"
" -mod_field dim '4 64 64 20 30 1 1 1 1'\n"
" nifti_tool -mod_hdr -prefix dnew -infiles dset0.nii \\\n"
" -mod_field descrip 'beer, brats and cheese, mmmmm...'\n"
"\n");
printf(
" strip, add or remove extensions:\n"
"\n"
" nifti_tool -strip -overwrite -infiles *.nii\n"
"\n"
" nifti_tool -add_comment 'converted from MY_AFNI_DSET+orig' \\\n"
" -prefix dnew -infiles dset0.nii\n"
"\n");
printf(
" nifti_tool -rm_ext ALL -prefix dset1 -infiles dset0.nii\n"
" nifti_tool -rm_ext 2 -rm_ext 3 -rm_ext 5 -overwrite \\\n"
" -infiles dset0.nii\n"
"\n"
" ------------------------------\n");
printf(
"\n"
" options for check actions:\n"
"\n");
printf(
" -check_hdr : check for a valid nifti_1_header struct\n"
"\n"
" This action is used to check the nifti_1_header structure for\n"
" problems. The nifti_hdr_looks_good() function is used for the\n"
" test, and currently checks:\n"
" \n"
" dim[], sizeof_hdr, magic, datatype\n"
" \n"
" More tests can be requested of the author.\n"
"\n");
printf(
" e.g. perform checks on the headers of some datasets\n"
" nifti_tool -check_hdr -infiles dset0.nii dset1.nii\n"
" nifti_tool -check_hdr -infiles *.nii *.hdr\n"
" \n"
" e.g. add the -quiet option, so that only erros are reported\n"
" nifti_tool -check_hdr -quiet -infiles *.nii *.hdr\n"
"\n");
printf(
" -check_nim : check for a valid nifti_image struct\n"
"\n"
" This action is used to check the nifti_image structure for\n"
" problems. This is tested via both nifti_convert_nhdr2nim()\n"
" and nifti_nim_is_valid(), though other functions are called\n"
" below them, of course. Current checks are:\n"
"\n");
printf(
" dim[], sizeof_hdr, datatype, fname, iname, nifti_type\n"
" \n"
" Note that creation of a nifti_image structure depends on good\n"
" header fields. So errors are terminal, meaning this check would\n"
" probably report at most one error, even if more exist. The\n"
" -check_hdr action is more complete.\n"
"\n");
printf(
" More tests can be requested of the author.\n"
"\n");
printf(
" e.g. nifti_tool -check_nim -infiles dset0.nii dset1.nii\n"
" e.g. nifti_tool -check_nim -infiles *.nii *.hdr\n"
"\n");
printf(
" ------------------------------\n");
printf(
"\n"
" options for copy actions:\n"
"\n"
" -copy_brick_list : copy a list of volumes to a new dataset\n"
" -cbl : (a shorter, alternative form)\n"
"\n");
printf(
" This action allows the user to copy a list of volumes (over time)\n"
" from one dataset to another. The listed volumes can be in any\n"
" order and contain repeats, but are of course restricted to\n"
" the set of values {1, 2, ..., nt-1}, from dimension 4.\n"
"\n");
printf(
" This option is a flag. The index list is specified with the input\n"
" dataset, contained in square brackets. Note that square brackets\n"
" are special to most UNIX shells, so they should be contained\n"
" within single quotes. Syntax of an index list:\n"
"\n"
" notes:\n"
"\n");
printf(
" - indices start at zero\n"
" - indices end at nt-1, which has the special symbol '$'\n"
" - single indices should be separated with commas, ','\n"
" e.g. -infiles dset0.nii'[0,3,8,5,2,2,2]'\n"
" - ranges may be specified using '..' or '-' \n");
printf(
" e.g. -infiles dset0.nii'[2..95]'\n"
" e.g. -infiles dset0.nii'[2..$]'\n"
" - ranges may have step values, specified in ()\n"
" example: 2 through 95 with a step of 3, i.e. {2,5,8,11,...,95}\n"
" e.g. -infiles dset0.nii'[2..95(3)]'\n"
"\n");
printf(
" This functionality applies only to 4-dimensional datasets.\n"
"\n"
" e.g. to copy sub-bricks 0 and 7:\n"
" nifti_tool -cbl -prefix new_07.nii -infiles dset0.nii'[0,7]'\n"
"\n"
" e.g. to copy an entire dataset:\n"
" nifti_tool -cbl -prefix new_all.nii -infiles dset0.nii'[0..$]'\n"
"\n");
printf(
" e.g. to copy every other time point, skipping the first three:\n"
" nifti_tool -cbl -prefix new_partial.nii \\\n"
" -infiles dset0.nii'[3..$(2)]'\n"
"\n"
"\n"
" -copy_collapsed_image ... : copy a list of volumes to a new dataset\n"
" -cci I J K T U V W : (a shorter, alternative form)\n"
"\n");
printf(
" This action allows the user to copy a collapsed dataset, where\n"
" some dimensions are collapsed to a given index. For instance, the\n"
" X dimension could be collapsed to i=42, and the time dimensions\n"
" could be collapsed to t=17. To collapse a dimension, set Di to\n"
" the desired index, where i is in {0..ni-1}. Any dimension that\n"
" should not be collapsed must be listed as -1.\n"
"\n");
printf(
" Any number (of valid) dimensions can be collapsed, even down to a\n"
" a single value, by specifying enough valid indices. The resulting\n"
" dataset will then have a reduced number of non-trivial dimensions.\n"
"\n"
" Assume dset0.nii has nim->dim[8] = { 4, 64, 64, 21, 80, 1, 1, 1 }.\n"
" Note that this is a 4-dimensional dataset.\n"
"\n");
printf(
" e.g. copy the time series for voxel i,j,k = 5,4,17\n"
" nifti_tool -cci 5 4 17 -1 -1 -1 -1 -prefix new_5_4_17.nii\n"
"\n"
" e.g. read the single volume at time point 26\n"
" nifti_tool -cci -1 -1 -1 26 -1 -1 -1 -prefix new_t26.nii\n"
"\n");
printf(
" Assume dset1.nii has nim->dim[8] = { 6, 64, 64, 21, 80, 4, 3, 1 }.\n"
" Note that this is a 6-dimensional dataset.\n"
"\n"