forked from NIFTI-Imaging/nifti_clib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nifti_tool.c
5652 lines (4921 loc) · 204 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_hdr1
* nifti_tool -help_hdr2
* nifti_tool -help_nim
* nifti_tool -help_nim1
* nifti_tool -help_nim2
* nifti_tool -help_ana
* nifti_tool -help_datatypes
* nifti_tool -hist
* nifti_tool -ver
* nifti_tool -nifti_hist
* nifti_tool -nifti_ver
* nifti_tool -with_zlib
*
* nifti_tool -check_hdr -infiles f1 ...
* nifti_tool -check_nim -infiles f1 ...
* nifti_tool -disp_cext -infiles f1 ...
* nifti_tool -disp_exts -infiles f1 ...
* nifti_tool -disp_hdr [-field fieldname] [...] -infiles f1 ...
* nifti_tool -disp_hdr1 [-field fieldname] [...] -infiles f1 ...
* nifti_tool -disp_hdr2 [-field fieldname] [...] -infiles f1 ...
* nifti_tool -disp_nim [-field fieldname] [...] -infiles f1 ...
* nifti_tool -disp_ana [-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_hdr1 [-field fieldname] [...] -infiles f1 f2
* nifti_tool -diff_hdr2 [-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_hdr2 [-mod_field fieldname new_val] [...] -infiles f1 ...
* nifti_tool -mod_nim [-mod_field fieldname new_val] [...] -infiles f1 ...
*
* </pre> */
/*-------------------------------------------------------------------------*/
/*! module history */
static const 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 Aug 2005 [rickr] - const/string cleanup for warnings\n",
"1.10 18 Nov 2005 [rickr] - added check_hdr and check_nim actions\n",
"1.11 31 Jan 2006 [rickr] - check for new vox_offset in act_mod_hdrs\n",
"1.12 02 Mar 2006 [rickr]\n"
" - in act_cbl(), check for nt = 0 because of niftilib update 1.17\n",
"1.13 24 Apr 2006 [rickr] - act_disp_ci(): remove time series length check\n",
"1.14 04 Jun 2007 [rickr] - free_opts_mem(), to appease valgrind\n",
"1.15 05 Jun 2007 [rickr] - act_check_hdrs: free(nim)->nifti_image_free()\n",
"1.16 12 Jun 2007 [rickr] - allow creation of datasets via MAKE_IM\n",
" - added nt_image_read, nt_read_header and nt_read_bricks\n"
" to wrap nifti read functions, allowing creation of new datasets\n"
" - added -make_im, -new_dim, -new_datatype and -copy_im\n"
"1.17 13 Jun 2007 [rickr] - added help for -copy_im, enumerate examples\n",
"1.18 23 Jun 2007 [rickr] - main returns 0 on -help, -hist, -ver\n"
"1.19 28 Nov 2007 [rickr] - added -help_datatypes\n",
"1.20 13 Jun 2008 [rickr]\n"
" - added -with_zlib\n"
" - added ability to create extension from text file (for J. Gunter)\n",
"1.21 03 Aug 2008 [rickr] - ANALYZE 7.5 support\n"
" - added -help_ana, -disp_ana,\n"
" -swap_as_analyze, -swap_as_nifti, -swap_as_old\n"
"1.22 08 Oct 2008 [rickr] - allow cbl with indices in 0..nt*nu*nv*nw-1\n"
"1.23 06 Jul 2010 [rickr]\n",
" - in nt_read_bricks, bsize computation should allow for large integers\n"
"1.24 26 Sep 2012 [rickr]\n",
" - changed ana originator from char to short\n"
"2.00 29 Aug 2013 [rickr] - NIFTI-2\n",
"2.01 28 Apr 2015 [rickr] - disp_hdr1/disp_hdr2 to read as those types\n"
"2.02 01 Jun 2015 [rickr]\n",
" - disp_hdr detects type\n"
" - diff_hdr detects type\n"
" - have diff_hdr1/diff_hdr2 to read as those types\n"
"2.03 23 Jul 2015 [rickr] - handle a couple of unknown version cases\n",
"2.04 05 Aug 2015 [rickr] - incorporate nifti-2 writing library change\n",
"2.05 24 Jul 2017 [rickr]\n"
" - display ANALYZE header via appropriate NIFTI-1\n"
" - apply more PRId64 for 64-bit int I/O\n"
"2.06 04 Jan 2019 [rickr]\n",
" - add -mod_hdr2 option, to explicitly modify NIFTI-2 headers\n"
" - mod_hdr and swap_as_nifti fail on valid NIFTI-2 headers\n"
"2.07 19 Jul 2019 [rickr]\n",
" - can apply '-field HDR_SLICE_TIMING_FIELDS' (or NIM_) for easy\n"
" specification of field entries related to slice timing\n"
"2.08 5 Oct 2019 [rickr]\n",
" - added option -run_misc_tests for functionality testing\n"
" - added many corresponding tests\n"
" - added some matrix manipulation macros\n"
" - warn on type, and block print buffer overflow\n"
"----------------------------------------------------------------------\n"
};
static char g_version[] = "version 2.08 (October 5, 2019)";
static int g_debug = 1;
#define _NIFTI_TOOL_C_
#include "nifti2_io.h"
#include "nifti_tool.h"
/* local prototypes */
static int free_opts_mem(nt_opts * nopt);
static int num_volumes(nifti_image * nim);
static char * read_file_text(const char * filename, int * length);
#define NTL_FERR(func,msg,file) \
fprintf(stderr,"** ERROR (%s): %s '%s'\n",func,msg,file)
/* val may be a function call, so evalulate first, and return result */
#define FREE_RETURN(val) \
do{ int tval=(val); free_opts_mem(&opts); return tval; } while(0)
/* these are effectively constant, and are built only for verification */
field_s g_hdr1_fields[NT_HDR1_NUM_FIELDS]; /* nifti_1_header fields */
field_s g_hdr2_fields[NT_HDR2_NUM_FIELDS]; /* nifti_2_header fields */
field_s g_ana_fields [NT_ANA_NUM_FIELDS]; /* nifti_analyze75 */
field_s g_nim1_fields[NT_NIM_NUM_FIELDS]; /* nifti_image fields */
field_s g_nim2_fields[NT_NIM_NUM_FIELDS]; /* nifti2_image fields */
/* slice timing hdr and nim fields */
static const char * g_hdr_timing_fnames[NT_HDR_TIME_NFIELDS] = {
"slice_code", "slice_start", "slice_end", "slice_duration",
"dim_info", "dim", "pixdim", "xyzt_units" };
static const char * g_nim_timing_fnames[NT_NIM_TIME_NFIELDS] = {
"slice_code", "slice_start", "slice_end", "slice_duration",
"slice_dim", "phase_dim", "freq_dim",
"dim", "pixdim", "xyz_units", "time_units" };
int main( int argc, char * argv[] )
{
nt_opts opts;
int rv;
if( (rv = process_opts(argc, argv, &opts)) != 0) /* then return */
{
if( rv < 0 ) FREE_RETURN(1); /* free opts memory, and return */
else FREE_RETURN(0); /* valid usage */
}
if( (rv = verify_opts(&opts, argv[0])) != 0 )
FREE_RETURN(rv);
/* now perform the requested action(s) */
if( (rv = fill_hdr1_field_array(g_hdr1_fields)) != 0 )
FREE_RETURN(rv);
if( (rv = fill_hdr2_field_array(g_hdr2_fields)) != 0 )
FREE_RETURN(rv);
if( (rv = fill_nim1_field_array(g_nim1_fields)) != 0 )
FREE_RETURN(rv);
if( (rv = fill_nim2_field_array(g_nim2_fields)) != 0 )
FREE_RETURN(rv);
if( (rv = fill_ana_field_array(g_ana_fields)) != 0 )
FREE_RETURN(rv);
/* 'check' functions, first */
if( opts.check_hdr || opts.check_nim ) /* allow for both */
FREE_RETURN( act_check_hdrs(&opts) );
/* copy or dts functions -- do not continue after these */
if( opts.cbl ) FREE_RETURN( act_cbl(&opts) );
if( opts.cci ) FREE_RETURN( act_cci(&opts) );
if( opts.dts || opts.dci ) FREE_RETURN( act_disp_ci(&opts) );
/* perform modifications early, in case we allow multiple actions */
if( opts.strip && ((rv = act_strip (&opts)) != 0) ) FREE_RETURN(rv);
if( opts.add_exts && ((rv = act_add_exts (&opts)) != 0) ) FREE_RETURN(rv);
if( opts.rm_exts && ((rv = act_rm_ext (&opts)) != 0) ) FREE_RETURN(rv);
if( opts.mod_hdr && ((rv = act_mod_hdrs (&opts)) != 0) ) FREE_RETURN(rv);
if( opts.mod_hdr2 && ((rv = act_mod_hdr2s(&opts)) != 0) ) FREE_RETURN(rv);
if( opts.mod_nim && ((rv = act_mod_nims (&opts)) != 0) ) FREE_RETURN(rv);
if((opts.swap_hdr || opts.swap_ana || opts.swap_old )
&& ((rv = act_swap_hdrs (&opts)) != 0) ) FREE_RETURN(rv);
/* if a diff, return wither a difference exists (like the UNIX command) */
if( opts.diff_hdr && ((rv = act_diff_hdrs (&opts)) != 0) ) FREE_RETURN(rv);
if( opts.diff_hdr1 && ((rv = act_diff_hdr1s(&opts)) != 0) ) FREE_RETURN(rv);
if( opts.diff_hdr2 && ((rv = act_diff_hdr2s(&opts)) != 0) ) FREE_RETURN(rv);
if( opts.diff_nim && ((rv = act_diff_nims(&opts)) != 0) ) FREE_RETURN(rv);
/* run tests */
if( opts.run_misc_tests && ((rv = act_run_misc_tests(&opts)) != 0) )
FREE_RETURN(rv);
/* last action type is display */
if( opts.disp_exts && ((rv = act_disp_exts(&opts)) != 0) ) FREE_RETURN(rv);
if( opts.disp_cext && ((rv = act_disp_cext(&opts)) != 0) ) FREE_RETURN(rv);
if( opts.disp_hdr && ((rv = act_disp_hdr (&opts)) != 0) ) FREE_RETURN(rv);
if( opts.disp_hdr1 && ((rv = act_disp_hdr1(&opts)) != 0) ) FREE_RETURN(rv);
if( opts.disp_hdr2 && ((rv = act_disp_hdr2(&opts)) != 0) ) FREE_RETURN(rv);
if( opts.disp_nim && ((rv = act_disp_nims(&opts)) != 0) ) FREE_RETURN(rv);
if( opts.disp_ana && ((rv = act_disp_anas(&opts)) != 0) ) FREE_RETURN(rv);
FREE_RETURN(0);
}
/*----------------------------------------------------------------------
* process user options, return 0 on success
*----------------------------------------------------------------------*/
int process_opts( int argc, char * argv[], nt_opts * opts )
{
int ac, count;
memset(opts, 0, sizeof(*opts));
opts->prefix = NULL;
opts->debug = 1; /* init debug level to basic output */
/* init options for creating a new dataset via "MAKE_IM" */
opts->new_datatype = NIFTI_TYPE_INT16;
opts->new_dim[0] = 3;
opts->new_dim[1] = 1; opts->new_dim[2] = 1; opts->new_dim[3] = 1;
if( argc < 2 ) return usage(argv[0], USE_FULL);
/* terminal options are first, the rest are sorted */
for( ac = 1; ac < argc; ac++ )
{
if( ! strcmp(argv[ac], "-help_datatypes") )
{
ac++;
if( ac >= argc )
nifti_disp_type_list(3); /* show all types */
else if( argv[ac][0] == 'd' || argv[ac][0] == 'D' )
nifti_disp_type_list(1); /* show DT_* types */
else if( argv[ac][0] == 't' || argv[ac][0] == 'T' )
nifti_test_datatype_sizes(1); /* test each nbyper and swapsize */
else
nifti_disp_type_list(2); /* show NIFTI_* types */
return 1;
}
else if( ! strcmp(argv[ac], "-help_hdr") )
return usage(argv[0], USE_FIELD_HDR2);
else if( ! strcmp(argv[ac], "-help_hdr1") )
return usage(argv[0], USE_FIELD_HDR1);
else if( ! strcmp(argv[ac], "-help_hdr2") )
return usage(argv[0], USE_FIELD_HDR2);
else if( ! strcmp(argv[ac], "-help_nim") )
return usage(argv[0], USE_FIELD_NIM2);
else if( ! strcmp(argv[ac], "-help_nim1") )
return usage(argv[0], USE_FIELD_NIM1);
else if( ! strcmp(argv[ac], "-help_nim2") )
return usage(argv[0], USE_FIELD_NIM2);
else if( ! strcmp(argv[ac], "-help_ana") )
return usage(argv[0], USE_FIELD_ANA);
else if( ! strcmp(argv[ac], "-help") )
return usage(argv[0], USE_FULL);
else if( ! strcmp(argv[ac], "-hist") )
return usage(argv[0], USE_HIST);
else if( ! strcmp(argv[ac], "-ver") )
return usage(argv[0], USE_VERSION);
else if( ! strcmp(argv[ac], "-nifti_hist") )
{
nifti_disp_lib_hist(1);
nifti_disp_lib_hist(2);
return 1;
}
else if( ! strcmp(argv[ac], "-nifti_ver") )
{
nifti_disp_lib_version();
return 1;
}
else if( ! strcmp(argv[ac], "-with_zlib") ) {
printf("Was NIfTI library compiled with zlib? %s\n",
nifti_compiled_with_zlib() ? "YES" : "NO");
return 1;
}
/* begin normal execution options... */
else if( ! strcmp(argv[ac], "-add_ext") )
{
int new_ecode;
ac++;
CHECK_NEXT_OPT(ac, argc, "-add_ext");
new_ecode = atoll(argv[ac]);
if( add_int(&opts->etypes, new_ecode) ) return -1;
ac++;
CHECK_NEXT_OPT(ac, argc, "-add_ext");
if( add_string(&opts->elist, argv[ac]) ) return -1; /* add extension */
opts->add_exts = 1;
}
else if( ! strcmp(argv[ac], "-add_afni_ext") )
{
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", 12) )
{
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( ! strcmp(argv[ac], "-check_hdr") )
opts->check_hdr = 1;
else if( ! strcmp(argv[ac], "-check_nim") )
opts->check_nim = 1;
else if( ! strcmp(argv[ac], "-copy_brick_list") ||
! strcmp(argv[ac], "-copy_im") ||
! strcmp(argv[ac], "-cbl") )
{
opts->cbl = 1;
}
else if( ! strcmp(argv[ac], "-copy_collapsed_image") ||
! strcmp(argv[ac], "-cci") )
{
/* 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] = atoll(argv[ac]);
}
opts->cci = 1;
}
else if( ! strcmp(argv[ac], "-debug") )
{
ac++;
CHECK_NEXT_OPT(ac, argc, "-debug");
opts->debug = atoi(argv[ac]);
g_debug = opts->debug;
}
else if( ! strcmp(argv[ac], "-diff_hdr") )
opts->diff_hdr = 1;
else if( ! strcmp(argv[ac], "-diff_hdr1") )
opts->diff_hdr1 = 1;
else if( ! strcmp(argv[ac], "-diff_hdr2") )
opts->diff_hdr2 = 1;
else if( ! strcmp(argv[ac], "-diff_nim") )
opts->diff_nim = 1;
else if( ! strncmp(argv[ac], "-disp_exts", 9) )
opts->disp_exts = 1;
else if( ! strncmp(argv[ac], "-disp_cext", 9) )
opts->disp_cext = 1;
else if( ! strcmp(argv[ac], "-disp_hdr") )
opts->disp_hdr = 1;
else if( ! strcmp(argv[ac], "-disp_hdr1") )
opts->disp_hdr1 = 1;
else if( ! strcmp(argv[ac], "-disp_hdr2") )
opts->disp_hdr2 = 1;
else if( ! strcmp(argv[ac], "-disp_nim") )
opts->disp_nim = 1;
else if( ! strcmp(argv[ac], "-disp_ana") )
opts->disp_ana = 1;
else if( ! strcmp(argv[ac], "-dci_lines") || /* before -dts */
! strcmp(argv[ac], "-dts_lines") )
{
opts->dci_lines = 1;
}
else if( ! strcmp(argv[ac], "-disp_collapsed_image") ||
! strcmp(argv[ac], "-disp_ci") )
{
/* 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] = atoll(argv[ac]);
}
opts->dci = 1;
}
else if( ! strcmp(argv[ac], "-disp_ts") ||
! strcmp(argv[ac], "-dts") )
{
/* 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] = atoll(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( ! strcmp(argv[ac], "-field") )
{
ac++;
CHECK_NEXT_OPT(ac, argc, "-field");
/* allow HDR/NIM_S_TIMING FIELDS as special cases */
if( ! strcmp(argv[ac], "HDR_SLICE_TIMING_FIELDS") ) {
for( count = 0; count < NT_HDR_TIME_NFIELDS; count++ )
if( add_string(&opts->flist, g_hdr_timing_fnames[count]) )
return -1;
} else if( ! strcmp(argv[ac], "NIM_SLICE_TIMING_FIELDS") ) {
for( count = 0; count < NT_NIM_TIME_NFIELDS; count++ )
if( add_string(&opts->flist, g_nim_timing_fnames[count]) )
return -1;
} else {
/* otherwise, just add as a typical field */
if( add_string(&opts->flist, argv[ac]) ) return -1; /* add field */
}
}
else if( ! strncmp(argv[ac], "-infiles", 3) )
{
/* 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], "-make_image", 8) )
{
opts->make_im = 1; /* will setup later, as -cbl and MAKE_IM */
}
else if( ! strcmp(argv[ac], "-mod_field") )
{
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( ! strcmp(argv[ac], "-mod_hdr") )
opts->mod_hdr = 1;
else if( ! strcmp(argv[ac], "-mod_hdr2") ) /* 3 Jan 2019 */
opts->mod_hdr2 = 1;
else if( ! strcmp(argv[ac], "-mod_nim") )
opts->mod_nim = 1;
else if( ! strcmp(argv[ac], "-keep_hist") )
opts->keep_hist = 1;
else if( ! strncmp(argv[ac], "-new_dim", 8) )
{
/* we need to read in the 8 dimension values */
int index;
for( index = 0; index < 8; index++ )
{
ac++;
CHECK_NEXT_OPT_MSG(ac,argc,"-new_dim","8 dim values are requred");
if( ! isdigit(argv[ac][0]) && strcmp(argv[ac],"-1") ){
fprintf(stderr,"** -new_dim param %d (= '%s') is not a valid\n"
" consider: 'nifti_tool -help'\n",index,argv[ac]);
return -1;
}
opts->new_dim[index] = atoll(argv[ac]);
}
}
else if( ! strcmp(argv[ac], "-new_datatype") )
{
ac++;
CHECK_NEXT_OPT(ac, argc, "-new_datatype");
opts->new_datatype = atoi(argv[ac]);
}
else if( ! strcmp(argv[ac], "-overwrite") )
opts->overwrite = 1;
else if( ! strcmp(argv[ac], "-prefix") )
{
ac++;
CHECK_NEXT_OPT(ac, argc, "-prefix");
opts->prefix = argv[ac];
}
else if( ! strcmp(argv[ac], "-quiet") )
opts->debug = 0;
else if( ! strcmp(argv[ac], "-rm_ext") )
{
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( ! strcmp(argv[ac], "-run_misc_tests") )
opts->run_misc_tests = 1;
else if( ! strncmp(argv[ac], "-strip_extras", 6) )
opts->strip = 1;
else if( ! strcmp(argv[ac], "-swap_as_analyze") )
opts->swap_ana = 1;
else if( ! strcmp(argv[ac], "-swap_as_nifti") )
opts->swap_hdr = 1;
else if( ! strcmp(argv[ac], "-swap_as_old") )
opts->swap_old = 1;
else
{
fprintf(stderr,"** unknown option: '%s'\n", argv[ac]);
return -1;
}
}
if( opts->make_im )
{
if( opts->infiles.len > 0 )
{
fprintf(stderr,"** -infiles is invalid when using -make_im\n");
return -1;
}
/* apply -make_im via -cbl and "MAKE_IM" */
opts->cbl = 1;
if( add_string(&opts->infiles, NT_MAKE_IM_NAME) ) 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_hdr1 || opts->diff_hdr2
|| opts->diff_nim ) ? 1 : 0;
ac += (opts->disp_hdr || opts->disp_hdr1 || opts->disp_hdr2
|| opts->disp_nim || opts->disp_ana
|| opts->disp_exts || opts->disp_cext) ? 1 : 0;
ac += opts->mod_hdr;
ac += opts->mod_hdr2;
ac += opts->mod_nim;
ac += (opts->swap_hdr || opts->swap_ana || opts->swap_old ) ? 1 : 0;
ac += opts->add_exts;
ac += opts->rm_exts;
ac += opts->run_misc_tests;
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;
}
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_hdr2 || 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_hdr1 || 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_hdr2 || opts->mod_nim ||
opts->swap_hdr || opts->swap_ana || opts->swap_old ) &&
!opts->overwrite )
{
if( opts->infiles.len > 1 )
{
fprintf(stderr,"** without -overwrite, only one input file may be"
" modified at a time\n");
errs++;
}
else 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( !strcmp(argv[ac-1],"-infiles") )
{
/* 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, const char * str)
{
if( slist->len == 0 ) slist->list = NULL; /* just to be safe */
slist->len++;
slist->list = (const 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);
return -1;
}
else if( level == USE_FULL )
use_full(); /* 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_HDR1 )
{
field_s nhdr_fields[NT_HDR1_NUM_FIELDS]; /* just do it all here */
fill_hdr1_field_array(nhdr_fields);
disp_field_s_list("nifti_1_header: ", nhdr_fields, NT_HDR1_NUM_FIELDS);
printf(" sizeof(nifti_1_header) = %d\n", (int)sizeof(nifti_1_header));
}
else if( level == USE_FIELD_HDR2 )
{
field_s nhdr_fields[NT_HDR2_NUM_FIELDS]; /* just do it all here */
fill_hdr2_field_array(nhdr_fields);
disp_field_s_list("nifti_2_header: ", nhdr_fields, NT_HDR2_NUM_FIELDS);
printf(" sizeof(nifti_2_header) = %d\n", (int)sizeof(nifti_2_header));
}
else if( level == USE_FIELD_ANA )
{
field_s nhdr_fields[NT_ANA_NUM_FIELDS]; /* just do it all here */
fill_ana_field_array(nhdr_fields);
disp_field_s_list("nifti_analyze75: ",nhdr_fields,NT_ANA_NUM_FIELDS);
printf(" sizeof(nifti_analyze75) = %d\n", (int)sizeof(nifti_analyze75));
}
else if( level == USE_FIELD_NIM1 )
{
field_s nim_fields[NT_NIM_NUM_FIELDS];
fill_nim1_field_array(nim_fields);
disp_field_s_list("nifti1_image: ", nim_fields, NT_NIM_NUM_FIELDS);
printf(" sizeof(nifti1_image) = %d\n", (int)sizeof(nifti1_image));
}
else if( level == USE_FIELD_NIM2 )
{
field_s nim_fields[NT_NIM_NUM_FIELDS];
fill_nim2_field_array(nim_fields);
disp_field_s_list("nifti2_image: ", nim_fields, NT_NIM_NUM_FIELDS);
printf(" sizeof(nifti2_image) = %d\n", (int)sizeof(nifti2_image));
}
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;
}
return 1;
}
/*----------------------------------------------------------------------
* full usage
*----------------------------------------------------------------------*/
int use_full()
{
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"
" - any or all fields in the nifti_analyze75 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"
" - swap all fields in NIFTI or ANALYZE header structure\n"
" add/rm - any or all extensions in the nifti_image structure\n"
" remove - all extensions and descriptions from the datasets\n"