forked from NIFTI-Imaging/nifti_clib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fslio.c
2427 lines (2034 loc) · 80.3 KB
/
fslio.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
/*
fslio.c (Input and output routines for images in FSL)
Mark Jenkinson
FMRIB Image Analysis Group
*/
/*
The fslio.c file was originally part of FSL - FMRIB's Software Library
http://www.fmrib.ox.ac.uk/fsl
fslio.c has now been placed in the public domain.
Developed at FMRIB (Oxford Centre for Functional Magnetic Resonance
Imaging of the Brain), Department of Clinical Neurology, Oxford
University, Oxford, UK
*/
/** \file fslio.c
\brief Main collection of FSL i/o routines, written by Mark Jenkinson, FMRIB
- updates by Rick Reynolds, SSCC, NIMH
*/
#include "fslio.h"
#include "assert.h"
static int FslIgnoreMFQ=0;
static int FslOverrideOutputType=-1;
#define FSLIOERR(x) { fprintf(stderr,"Error:: %s\n",(x)); fflush(stderr); exit(EXIT_FAILURE); }
/************************************************************
* FslFileTypeString
************************************************************/
/*! \fn char* FslFileTypeString(int filetype)
\brief Return a string describing the format of the dataset
\param filetype FSL data format code. Legal values are as defined
by FSL_TYPE.
\return A string with the data format name, e.g. "ANALYZE-7.5"
\sa FSL_TYPE
*/
const char* FslFileTypeString(int filetype)
{
if (filetype==FSL_TYPE_ANALYZE) return "ANALYZE-7.5";
if (filetype==FSL_TYPE_NIFTI) return "NIFTI-1+";
if (filetype==FSL_TYPE_NIFTI_PAIR) return "NIFTI-1";
if (filetype==FSL_TYPE_ANALYZE_GZ) return "ANALYZE-7.5";
if (filetype==FSL_TYPE_NIFTI_GZ) return "NIFTI-1+";
if (filetype==FSL_TYPE_NIFTI_PAIR_GZ) return "NIFTI-1";
return "UNKNOWN";
}
int FslIsValidFileType(int filetype)
{
if ( (filetype!=FSL_TYPE_ANALYZE) && (filetype!=FSL_TYPE_ANALYZE_GZ) &&
(filetype!=FSL_TYPE_NIFTI) && (filetype!=FSL_TYPE_NIFTI_GZ) &&
(filetype!=FSL_TYPE_NIFTI_PAIR) && (filetype!=FSL_TYPE_NIFTI_PAIR_GZ) &&
(filetype!=FSL_TYPE_MINC) && (filetype!=FSL_TYPE_MINC_GZ) ) {
fprintf(stderr,"Error: unrecognised file type: %d\n",filetype);
return 0;
}
return 1;
}
int FslBaseFileType(int filetype)
{
/* returns -1 to indicate error - unrecognised filetype */
if ( (filetype==FSL_TYPE_ANALYZE_GZ) || (filetype==FSL_TYPE_ANALYZE) )
return FSL_TYPE_ANALYZE;
if ( (filetype==FSL_TYPE_NIFTI_GZ) || (filetype==FSL_TYPE_NIFTI) )
return FSL_TYPE_NIFTI;
if ( (filetype==FSL_TYPE_NIFTI_PAIR_GZ) || (filetype==FSL_TYPE_NIFTI_PAIR) )
return FSL_TYPE_NIFTI_PAIR;
if ( (filetype==FSL_TYPE_MINC_GZ) || (filetype==FSL_TYPE_MINC) )
return FSL_TYPE_MINC;
fprintf(stderr,"Error: unrecognised file type (%d)\n",filetype);
return -1;
}
int FslGetFileType2(const FSLIO *fslio, int quiet)
{
FSLIO *mutablefslio;
if (fslio==NULL) FSLIOERR("FslGetFileType: Null pointer passed for FSLIO");
if ( (fslio->file_mode==FSL_TYPE_MINC) || (fslio->file_mode==FSL_TYPE_MINC_GZ) ) {
return fslio->file_mode;
}
if ( !FslIsValidFileType(fslio->file_mode) ) return -1;
if (fslio->niftiptr!=NULL) { /* check that it is nifti_type and filetype are consistent */
if (fslio->niftiptr->nifti_type != FslBaseFileType(fslio->file_mode)) {
if (!quiet) {
fprintf(stderr,"Warning: nifti structure and fsl structure disagree on file type\n");
fprintf(stderr,"nifti = %d and fslio = %d\n",fslio->niftiptr->nifti_type,fslio->file_mode);
}
mutablefslio = (FSLIO *) fslio; /* dodgy and will generate warnings */
mutablefslio->niftiptr->nifti_type = FslBaseFileType(fslio->file_mode);
return fslio->file_mode;
}
}
return fslio->file_mode;
}
int FslGetFileType(const FSLIO *fslio)
{
return FslGetFileType2(fslio,0);
}
void FslSetFileType(FSLIO *fslio, int filetype)
{
if (fslio==NULL) FSLIOERR("FslSetFileType: Null pointer passed for FSLIO");
if ( (filetype==FSL_TYPE_MINC) || (filetype==FSL_TYPE_MINC_GZ) ) {
fslio->file_mode = filetype;
return;
}
if (! FslIsValidFileType(filetype)) { return; }
fslio->file_mode = filetype; /* indicates general nifti - details in niftiptr */
if (fslio->niftiptr!=NULL) {
fslio->niftiptr->nifti_type = FslBaseFileType(filetype);
nifti_set_iname_offset(fslio->niftiptr);
}
}
int FslIsSingleFileType(int filetype)
{
if ( (filetype==FSL_TYPE_NIFTI) || (filetype==FSL_TYPE_NIFTI_GZ) ||
(filetype==FSL_TYPE_MINC) || (filetype==FSL_TYPE_MINC_GZ) )
return 1;
return 0;
}
int FslIsCompressedFileType(int filetype)
{
if ( filetype >=100 ) return 1;
return 0;
}
int FslGetWriteMode(const FSLIO *fslio)
{
if (fslio==NULL) FSLIOERR("FslGetWriteMode: Null pointer passed for FSLIO");
return fslio->write_mode;
}
void FslSetWriteMode(FSLIO *fslio, int mode)
{
if (fslio==NULL) FSLIOERR("FslSetWriteMode: Null pointer passed for FSLIO");
fslio->write_mode = mode;
}
int FslGetEnvOutputType(void)
{
/* return type is one of FSL_TYPE_* or -1 to indicate error */
char *otype;
if (FslOverrideOutputType>=0) return FslOverrideOutputType;
otype = getenv("FSLOUTPUTTYPE");
if (otype == NULL) {
fprintf(stderr,"ERROR:: Environment variable FSLOUTPUTTYPE is not set!\n");
fprintf(stderr,"Please make sure that the appropriate configuration file is sourced by your shell (e.g. by putting it in .profile).\n");
fprintf(stderr,"e.g. bash or sh users add the line \". ${FSLDIR}/etc/fslconf/fsl.sh\"\n");
fprintf(stderr,"e.g. tcsh or csh users add the line \"source ${FSLDIR}/etc/fslconf/fsl.csh\"\n");
exit(EXIT_FAILURE);
}
if (strcmp(otype,"ANALYZE")==0) { return FSL_TYPE_ANALYZE; }
if (strcmp(otype,"ANALYZE_GZ")==0) { return FSL_TYPE_ANALYZE_GZ; }
if (strcmp(otype,"NIFTI")==0) { return FSL_TYPE_NIFTI; }
if (strcmp(otype,"NIFTI_GZ")==0) { return FSL_TYPE_NIFTI_GZ; }
if (strcmp(otype,"NIFTI_PAIR")==0) { return FSL_TYPE_NIFTI_PAIR; }
if (strcmp(otype,"NIFTI_PAIR_GZ")==0) { return FSL_TYPE_NIFTI_PAIR_GZ; }
if (strcmp(otype,"MINC")==0) { return FSL_TYPE_MINC; }
if (strcmp(otype,"MINC_GZ")==0) { return FSL_TYPE_MINC_GZ; }
fprintf(stderr,"ERROR:: Unrecognised value (%s) of environment variable FSLOUTPUT\n",otype);
fprintf(stderr,"Legal values are: ANALYZE, NIFTI, NIFTI_PAIR, MINC, ANALYZE_GZ, NIFTI_GZ, NIFTI_PAIR_GZ, MINC_GZ\n");
exit(EXIT_FAILURE);
return -1;
}
int FslFileType(const char* fname)
{
/* return type is FSL_TYPE_* or -1 to indicate undetermined */
/* use name as first priority but if that is ambiguous then resolve using environment */
int flen;
int retval=-1;
if (fname==NULL) return retval;
flen = strlen(fname);
/* debian@onerussian.com had to group conditions to avoid possible
* illegal memory read-ins */
if (flen<5) return retval; /* smallest name + extension is a.nii */
if (strcmp(fname + flen - 4,".nii")==0) retval=FSL_TYPE_NIFTI;
if (strcmp(fname + flen - 4,".mnc")==0) retval=FSL_TYPE_MINC;
if (strcmp(fname + flen - 4,".hdr")==0) retval=FSL_TYPE_NIFTI_PAIR;
if (strcmp(fname + flen - 4,".img")==0) retval=FSL_TYPE_NIFTI_PAIR;
if ((retval==-1) && (flen<8)) return retval; /* small name + ext.gz is a.nii.gz */
if (strcmp(fname + flen - 7,".nii.gz")==0) retval=FSL_TYPE_NIFTI_GZ;
if (strcmp(fname + flen - 7,".mnc.gz")==0) retval=FSL_TYPE_MINC;
if (strcmp(fname + flen - 7,".hdr.gz")==0) retval=FSL_TYPE_NIFTI_PAIR_GZ;
if (strcmp(fname + flen - 7,".img.gz")==0) retval=FSL_TYPE_NIFTI_PAIR_GZ;
if ( (retval==FSL_TYPE_NIFTI_PAIR) || (retval==FSL_TYPE_NIFTI_PAIR_GZ) ) {
/* If it was hdr or img, check if Analyze was requested by environment */
if ( (FslGetEnvOutputType() == FSL_TYPE_ANALYZE) && (retval == FSL_TYPE_NIFTI_PAIR) )
retval=FSL_TYPE_ANALYZE;
if ( (FslGetEnvOutputType() == FSL_TYPE_ANALYZE_GZ) && (retval == FSL_TYPE_NIFTI_PAIR_GZ) )
retval=FSL_TYPE_ANALYZE_GZ;
}
return retval;
}
/************************************************************
* FslGetReadFileType
************************************************************/
/*! \fn int FslGetReadFileType(const FSLIO *fslio)
\brief return the best estimate of the true file type
This function is used to return the best estimate of the true file type once
a simple open has occurred - for now it is used after a nifti open call is made
\param fslio data structure
\return FSL_TYPE filetype code
\sa FSL_TYPE
*/
int FslGetReadFileType(const FSLIO *fslio)
{
int filetype=FSL_TYPE_ANALYZE; /* unused default */
if (fslio==NULL) FSLIOERR("FslReadGetFileType: Null pointer passed for FSLIO");
/* Don't use fslio->file_mode as it hasn't been set yet */
if (fslio->niftiptr!=NULL) {
/* use the nifti_type and hdr or img name to determine the actual type */
if (fslio->niftiptr->nifti_type == FSL_TYPE_ANALYZE) {
if (FslIsCompressedFileType(FslFileType(fslio->niftiptr->iname))) {
filetype = FSL_TYPE_ANALYZE_GZ;
} else {
filetype = FSL_TYPE_ANALYZE;
}
}
if (fslio->niftiptr->nifti_type == FSL_TYPE_NIFTI_PAIR) {
if (FslIsCompressedFileType(FslFileType(fslio->niftiptr->iname))) {
filetype = FSL_TYPE_NIFTI_PAIR_GZ;
} else {
filetype = FSL_TYPE_NIFTI_PAIR;
}
}
if (fslio->niftiptr->nifti_type == FSL_TYPE_NIFTI) {
if (FslIsCompressedFileType(FslFileType(fslio->niftiptr->fname))) {
filetype = FSL_TYPE_NIFTI_GZ;
} else {
filetype = FSL_TYPE_NIFTI;
}
}
}
if (fslio->mincptr!=NULL) {
fprintf(stderr,"Warning:: Minc is not yet supported\n");
filetype = FSL_TYPE_MINC;
}
return filetype;
}
int FslFileExists(const char *filename)
{
/* return 1 if file(s) exists, otherwise return 0 */
char *hdrname = nifti_findhdrname(filename);
char *imgname = NULL;
if (hdrname!=NULL){
imgname = nifti_findimgname(filename,
FslBaseFileType(FslFileType(hdrname)));
free(hdrname);
if (imgname != NULL) { free(imgname); return 1; }
}
return 0;
}
char *FslMakeBaseName(const char *fname)
{
char *basename;
int blen;
basename = nifti_makebasename(fname);
blen = strlen(basename);
#ifdef HAVE_ZLIB
if ((blen>7) && (strcmp(basename + blen-7,".mnc.gz") == 0))
{ basename[blen-7]='\0'; return basename; }
#endif
if ((blen>4) && (strcmp(basename + blen-4,".mnc") == 0))
{ basename[blen-4]='\0'; return basename; }
return basename;
}
void FslGetHdrImgNames(const char* filename, const FSLIO* fslio,
char** hdrname, char** imgname)
{
char *basename;
int filetype;
basename = FslMakeBaseName(filename);
*hdrname = (char *)calloc(sizeof(char),strlen(basename)+8);
*imgname = (char *)calloc(sizeof(char),strlen(basename)+8);
strcpy(*hdrname,basename);
strcpy(*imgname,basename);
filetype = FslGetFileType(fslio);
if (filetype==FSL_TYPE_NIFTI_GZ) {
strcat(*hdrname,".nii.gz");
strcat(*imgname,".nii.gz");
free(basename);
return;
}
if (filetype==FSL_TYPE_NIFTI) {
strcat(*hdrname,".nii");
strcat(*imgname,".nii");
free(basename);
return;
}
if (filetype==FSL_TYPE_MINC_GZ) {
strcat(*hdrname,".mnc.gz");
strcat(*imgname,".mnc.gz");
free(basename);
return;
}
if (filetype==FSL_TYPE_MINC) {
strcat(*hdrname,".mnc");
strcat(*imgname,".mnc");
free(basename);
return;
}
if ( (filetype==FSL_TYPE_NIFTI_PAIR_GZ) || (filetype==FSL_TYPE_ANALYZE_GZ) ) {
strcat(*hdrname,".hdr.gz");
strcat(*imgname,".img.gz");
free(basename);
return;
}
if ( (filetype==FSL_TYPE_NIFTI_PAIR) || (filetype==FSL_TYPE_ANALYZE) ) {
strcat(*hdrname,".hdr");
strcat(*imgname,".img");
free(basename);
return;
}
fprintf(stderr,"Error: Unrecognised filetype (%d)\n",FslGetFileType(fslio));
free(basename);
/* Failure */
free(*hdrname);
*hdrname = NULL;
free(*imgname);
*imgname = NULL;
}
/***************************************************************
* FslInit()
***************************************************************/
/*! \fn FSLIO *FslInit()
\brief allocate space for the FSLIO struct and set some sensible defaults
\return A pointer to an initialized FSLIO data structure
*/
FSLIO *FslInit(void)
{
FSLIO *fslio;
fslio = (FSLIO *) calloc(1,sizeof(FSLIO));
FslSetInit(fslio);
return fslio;
}
void FslSetInit(FSLIO* fslio)
{
/* set some sensible defaults */
fslio->niftiptr = NULL;
fslio->mincptr = NULL;
FslSetFileType(fslio,FslGetEnvOutputType());
FslSetWriteMode(fslio,0);
fslio->written_hdr = 0;
}
void FslInit4Write(FSLIO* fslio, const char* filename, int ft)
{
/* ft determines filetype if ft>=0*/
int imgtype;
FslSetWriteMode(fslio,1);
/* Determine file type from image name (first priority) or environment (default) */
imgtype = FslFileType(filename);
if (imgtype<0) imgtype = FslGetEnvOutputType();
if (ft >= 0) imgtype = ft;
if (!FslIsValidFileType(imgtype)) {
fprintf(stderr,"Error: Failed to determine file type for writing in FslOpen()\n");
exit(EXIT_FAILURE);
}
if ( (FslBaseFileType(imgtype)!=FSL_TYPE_MINC) ) {
FslInitHeader(fslio, NIFTI_TYPE_FLOAT32,
1, 1, 1, 3, 0.0, 0.0, 0.0, 0.0, 4);
FslSetFileType(fslio,imgtype); /* this is after InitHeader as niftiptr set there */
/* determine the header and image filename */
FslGetHdrImgNames(filename,fslio,&(fslio->niftiptr->fname),&(fslio->niftiptr->iname));
if ( (fslio->niftiptr->fname == NULL) || (fslio->niftiptr->iname == NULL) ) {
fprintf(stderr,"Error: cannot find filenames for %s\n",filename);
}
} else if (FslBaseFileType(imgtype)==FSL_TYPE_MINC) {
fprintf(stderr,"Warning:: Minc is not yet supported\n");
return;
} else {
fprintf(stderr,"Error:: unrecognised image type requested\n");
return;
}
return;
}
void FslInitHeader(FSLIO *fslio, short t,
size_t x, size_t y, size_t z, size_t v,
float vx, float vy, float vz, float tr,
size_t dim)
{
/* NB: This function does not set the file type or write mode*/
if (fslio==NULL) FSLIOERR("FslInitHeader: Null pointer passed for FSLIO");
fslio->niftiptr = nifti_simple_init_nim();
/* make nifti type consistent with fslio */
fslio->niftiptr->nifti_type = FslBaseFileType(fslio->file_mode);
fslio->mincptr = NULL;
FslSetDataType(fslio,t);
FslSetDim(fslio,x,y,z,v);
FslSetVoxDim(fslio,vx,vy,vz,tr);
FslSetTimeUnits(fslio,"s");
FslSetDimensionality(fslio,dim);
}
void FslCloneHeader(FSLIO *dest, const FSLIO *src)
{
/* only clone the information that is stored in the disk version of the header */
/* - therefore _not_ the filenames, output type, write mode, etc */
void *data=NULL;
int filetype, writemode;
int preserve_nifti_values = 0;
if (dest==NULL) FSLIOERR("FslCloneHeader: Null pointer passed for FSLIO");
if (src==NULL) FSLIOERR("FslCloneHeader: Null pointer passed for FSLIO");
if (src->niftiptr!=NULL) {
/* preserve the filenames, output type and write mode */
if (dest->niftiptr != NULL) {
data = dest->niftiptr->data;
preserve_nifti_values = 1;
}
filetype = FslGetFileType2(dest,1);
writemode = FslGetWriteMode(dest);
/* copy _all_ info across */
dest->niftiptr = nifti_copy_nim_info(src->niftiptr);
/* restore old values */
if (preserve_nifti_values) {
dest->niftiptr->data = data;
} else {
/* destroy the values that the nifti copy creates */
free(dest->niftiptr->fname);
free(dest->niftiptr->iname);
nifti_free_extensions(dest->niftiptr);
dest->niftiptr->fname = NULL;
dest->niftiptr->iname = NULL;
dest->niftiptr->data = NULL; /* should already be NULL */
}
FslSetFileType(dest,filetype);
FslSetWriteMode(dest,writemode);
}
if (src->mincptr!=NULL) {
fprintf(stderr,"Warning:: Minc is not yet supported\n");
}
}
int fsl_fileexists(const char* fname)
{
znzFile fp;
fp = znzopen( fname , "rb" , 1 ) ;
if( !znz_isnull(fp) ) { znzclose(fp); return 1; }
return 0;
}
int FslCheckForMultipleFileNames(const char* filename)
{
char *basename, *tmpname;
int singlecount=0, hdrcount=0, imgcount=0, ambiguous=0;
basename = nifti_makebasename(filename);
tmpname = (char *)calloc(strlen(basename) + 10,sizeof(char));
strcpy(tmpname,basename);
strcat(tmpname,".nii");
if (fsl_fileexists(tmpname)) { singlecount++; }
strcpy(tmpname,basename);
strcat(tmpname,".nii.gz");
if (fsl_fileexists(tmpname)) { singlecount++; }
strcpy(tmpname,basename);
strcat(tmpname,".mnc");
if (fsl_fileexists(tmpname)) { singlecount++; }
strcpy(tmpname,basename);
strcat(tmpname,".mnc.gz");
if (fsl_fileexists(tmpname)) { singlecount++; }
strcpy(tmpname,basename);
strcat(tmpname,".img");
if (fsl_fileexists(tmpname)) { imgcount++; }
strcpy(tmpname,basename);
strcat(tmpname,".img.gz");
if (fsl_fileexists(tmpname)) { imgcount++; }
strcpy(tmpname,basename);
strcat(tmpname,".hdr");
if (fsl_fileexists(tmpname)) { hdrcount++; }
strcpy(tmpname,basename);
strcat(tmpname,".hdr.gz");
if (fsl_fileexists(tmpname)) { hdrcount++; }
ambiguous = 1;
if ( (hdrcount==1) && (imgcount==1) && (singlecount==0) ) { ambiguous=0; }
if ( (hdrcount==0) && (imgcount==0) && (singlecount==1) ) { ambiguous=0; }
/* treat no image found as not ambiguous - want opening errors instead */
if ( (hdrcount==0) && (imgcount==0) && (singlecount==0) ) { ambiguous=0; }
free(tmpname);
free(basename);
return ambiguous;
}
int check_for_multiple_filenames(const char* filename)
{
char *basename, *tmpname;
char *otype;
if (FslCheckForMultipleFileNames(filename))
{ /* take action */
basename = nifti_makebasename(filename);
tmpname = (char *)calloc(strlen(basename) + 10,sizeof(char));
fprintf(stderr,"\n\n\nWARNING!!!! Multiple image files detected:\n");
/* list the offending files */
strcpy(tmpname,basename);
strcat(tmpname,".nii");
if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
strcpy(tmpname,basename);
strcat(tmpname,".nii.gz");
if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
strcpy(tmpname,basename);
strcat(tmpname,".mnc");
if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
strcpy(tmpname,basename);
strcat(tmpname,".mnc.gz");
if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
strcpy(tmpname,basename);
strcat(tmpname,".img");
if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
strcpy(tmpname,basename);
strcat(tmpname,".img.gz");
if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
strcpy(tmpname,basename);
strcat(tmpname,".hdr");
if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
strcpy(tmpname,basename);
strcat(tmpname,".hdr.gz");
if (fsl_fileexists(tmpname)) { fprintf(stderr,"%s ",tmpname); }
fprintf(stderr,"\n\n");
if (!FslIgnoreMFQ) {
otype = getenv("FSLMULTIFILEQUIT");
if (otype!=NULL) {
free(tmpname);
fprintf(stderr,"STOPPING PROGRAM\n");
exit(EXIT_FAILURE);
}
}
free(tmpname);
return 1;
}
return 0;
}
/***************************************************************
* FslOpen
***************************************************************/
/*! \fn FSLIO *FslOpen(const char *filename, const char *opts)
\brief Opens a file for either reading or writing.
The format of the output dataset is determined automatically by
passing filetype -1 to FslXOpen.
\sa FslXOpen
*/
FSLIO *FslOpen(const char *filename, const char *opts)
{
/* Note: -1 for filetype indicates that FslXOpen should determine filetype for itself */
return FslXOpen(filename,opts,-1);
}
/***************************************************************
* FslXOpen
***************************************************************/
/*! \fn FSLIO *FslXOpen(const char *filename, const char *opts, int filetype)
\brief Opens a file for either reading or writing
Files to be read are automatically read whether
compressed or not. Also, reading uses the file extension
and will fail if that file does not exist.
For a more robust read, pass the basename in as then all types
will be tried.
\param filename Name (or basename) of the file to open
\param opts Flags for fopen() of dataset, eg "r", "wb", etc.
\param filetype specifies the type of file to be written. Legal
values are as defined by FSL_TYPE. If filetype is less than
zero, then it is ignored and the type is determined by the
filename extension or, failing that, the environment default.
\return pointer to FSLIO dataset datastructure
\sa FSLIO
\sa FSL_TYPE
*/
FSLIO *FslXOpen(const char *filename, const char *opts, int filetype)
{
FSLIO *fslio;
char bopts[1024];
size_t i, bi;
int imgtype;
fslio = FslInit();
bi=0;
for(i=0;i<strlen(opts);i++) {
if (opts[i]=='w') { FslSetWriteMode(fslio,1); }
if (opts[i]!='b' && opts[i]!='t') { bopts[bi++]=opts[i]; }
}
/* add in 'b' (at the end) for windows compatibility */
bopts[bi++]='b';
bopts[bi]='\0';
if (FslGetWriteMode(fslio)==1) {
/** ====================== Open file for writing ====================== **/
FslInit4Write(fslio,filename,filetype);
imgtype = FslGetFileType(fslio);
fslio->written_hdr = 0;
/* open the image file - not the header */
if ( fslio->niftiptr == NULL )
{
free(fslio);
return NULL;
}
fslio->fileptr = znzopen(fslio->niftiptr->iname,bopts,FslIsCompressedFileType(imgtype));
if (znz_isnull(fslio->fileptr)) {
fprintf(stderr,"Error: failed to open file %s\n",fslio->niftiptr->iname);
free(fslio);
return NULL;
}
if (!FslIsSingleFileType(imgtype)) {
/* set up pointer at end of iname_offset for dual file formats (not singles) */
FslSeekVolume(fslio,0);
}
return fslio;
}
/** ======================== Open file for reading ====================== **/
check_for_multiple_filenames(filename);
/* see if the extension indicates a minc file */
imgtype = FslFileType(filename);
if ((imgtype>=0) && (FslBaseFileType(imgtype)==FSL_TYPE_MINC)) {
free(fslio);
fprintf(stderr,"Warning:: Minc is not yet supported\n");
return NULL;
}
/* otherwise open nifti file: read header and open img file (may be same file) */
fslio->fileptr = nifti_image_open(filename,bopts,&(fslio->niftiptr));
if (znz_isnull(fslio->fileptr)) {
fprintf(stderr,"Error: failed to open file %s\n",filename);
return NULL;
}
/* set the file type given what has been read - it uses nifti_type and filenames */
imgtype = FslGetReadFileType(fslio);
FslSetFileType(fslio,imgtype);
FslSetWriteMode(fslio,0);
if (FslBaseFileType(FslGetFileType(fslio))==FSL_TYPE_ANALYZE) {
/* For the ANALYZE case in FSL, must cheat and grab the originator field! */
/* Note that the header file is always separate here and closed by now */
struct dsr ahdr;
short orig[5];
FslReadRawHeader(&ahdr,fslio->niftiptr->fname);
if (fslio->niftiptr->byteorder != nifti_short_order()) {
AvwSwapHeader(&ahdr);
}
/* Read the origin and set the sform up (if origin is non-zero) */
/* Note that signed pixdims are passed in to set the LR orientation */
memcpy(orig,&(ahdr.hist.originator),10);
FslSetAnalyzeSform(fslio, orig, fslio->niftiptr->pixdim[1],
fslio->niftiptr->pixdim[2], fslio->niftiptr->pixdim[3]);
}
/* from now on force all vox dims to be positive - LR info is in sform */
if (fslio->niftiptr!=NULL) {
fslio->niftiptr->dx = fabs(fslio->niftiptr->dx);
fslio->niftiptr->dy = fabs(fslio->niftiptr->dy);
fslio->niftiptr->dz = fabs(fslio->niftiptr->dz);
fslio->niftiptr->pixdim[1] = fabs(fslio->niftiptr->pixdim[1]);
fslio->niftiptr->pixdim[2] = fabs(fslio->niftiptr->pixdim[2]);
fslio->niftiptr->pixdim[3] = fabs(fslio->niftiptr->pixdim[3]);
}
/* set up pointer at end of iname_offset , ready for reading */
FslSeekVolume(fslio,0);
return fslio;
}
/***************************************************************
* FslReadAllVolumes
***************************************************************/
/*! \fn void* FslReadAllVolumes(FSLIO* fslio, char* filename)
\brief Read the header and all data into the FSLIO structure
There is no need for FslOpen or FslClose calls when FslReadAllVolumes()
is called.
<br>This routine allocates the buffer to hold the entire dataset.
<br>The data block returned will contain the data in whatever
datatype it is stored as on disk (therefore it is a void *).
<br>The data buffer will be byteswapped to native-endian.
<br>The data buffer will not be scaled.
<br>The best call to make before this is FslInit() or a calloc() for
fslio. (??? why calloc if this allocates the buffer ???)
\param fslio pointer to an open dataset
\param filename Name of the dataset to read.
\return A pointer to the data block buffer (allocated by this function).
<br> Return Null on error ??? is this true ???
<ul>
<li>Note this pointer is also in the FSLIO structure as
fslio->niftiptr->data.</li>
<li>Note a void pointer is returned, as the datablock is of
variable datatype.</li>
</ul>
*/
void* FslReadAllVolumes(FSLIO* fslio, char* filename)
{
int imgtype;
if (fslio==NULL) FSLIOERR("FslReadAllVolumes: Null pointer passed for FSLIO");
/* see if the extension indicates a minc file */
imgtype = FslFileType(filename);
if ((imgtype>=0) && (FslBaseFileType(imgtype)==FSL_TYPE_MINC)) {
fprintf(stderr,"Warning:: Minc is not yet supported\n");
return NULL;
}
/** otherwise it is a nifti file - so read it! **/
fslio->mincptr = NULL;
/* make sure an FslOpen hasn't locked the file */
if (!znz_isnull(fslio->fileptr)) FslClose(fslio);
fslio->niftiptr = nifti_image_read(filename,1);
/* check for failure, from David Akers */
if (fslio->niftiptr == NULL) {
FSLIOERR("FslReadAllVolumes: error reading NIfTI image");
return(NULL);
}
FslSetFileType(fslio,fslio->niftiptr->nifti_type);
FslSetWriteMode(fslio,0);
return fslio->niftiptr->data;
}
/***************************************************************
* FslReadVolumes
***************************************************************/
/*! \fn size_t FslReadVolumes(FSLIO *fslio, void *buffer, size_t nvols)
\brief Read the first nvols Volumes from a 4D dataset
\param fslio pointer to open dataset
\param buffer buffer to read data into, allocated by ???
\param nvols number of volumes to read
\return Number of volumes read.
*/
size_t FslReadVolumes(FSLIO *fslio, void *buffer, size_t nvols)
{
int volbytes;
size_t retval=0;
if (fslio==NULL) FSLIOERR("FslReadVolumes: Null pointer passed for FSLIO");
if (znz_isnull(fslio->fileptr)) FSLIOERR("FslReadVolumes: Null file pointer");
if (fslio->niftiptr!=NULL) {
fslio->niftiptr->data = buffer;
volbytes = FslGetVolSize(fslio) * fslio->niftiptr->nbyper;
retval = nifti_read_buffer(fslio->fileptr,fslio->niftiptr->data,nvols*volbytes,fslio->niftiptr);
retval /= volbytes;
}
if (fslio->mincptr!=NULL) {
fprintf(stderr,"Warning:: Minc is not yet supported\n");
}
return retval;
}
/***************************************************************
* FslWriteAllVolumes
***************************************************************/
/*! \fn void FslWriteAllVolumes(FSLIO *fslio, const void *buffer)
\brief Writes all data from buffer (using size info from fslio) to file.
Dimension and datatype of buffer are as is specified in nifti_image structure
fslio->niftiptr.
Note: If file format is Analyze (not nifti) and in Neurological order then
SWAP DATA into Radiological order.
\param fslio pointer to open dataset
\param buffer pointer to data array. Size and datatype of this buffer
*/
void FslWriteAllVolumes(FSLIO *fslio, const void *buffer)
{
short x,y,z,t;
if (fslio==NULL || fslio->niftiptr == NULL ) FSLIOERR("FslWriteAllVolumes: Null pointer passed for FSLIO");
FslGetDim(fslio,&x,&y,&z,&t);
FslWriteHeader(fslio);
FslWriteVolumes(fslio,buffer,t);
return;
}
/***************************************************************
* FslWriteVolumes
***************************************************************/
/*! \fn size_t FslWriteVolumes(FSLIO *fslio, const void *buffer, size_t nvols)
\brief Write the first nvols volumes in buffer to disk.
Dimension and datatype of buffer are as is specified in nifti_image structure
fslio->niftiptr.
Note: If file format is Analyze (not nifti) and in Neurological order then
SWAP DATA into Radiological order.
\param fslio pointer to open dataset
\param buffer pointer to data array. Size and datatype of this buffer
\param nvols number of volumes to write
\return number of bytes written
*/
size_t FslWriteVolumes(FSLIO *fslio, const void *buffer, size_t nvols)
{
/* The dimensions and datatype must be set before calling this function */
size_t retval = 0; // initialize to failure
if (fslio==NULL) FSLIOERR("FslWriteVolumes: Null pointer passed for FSLIO");
if ( (!fslio->written_hdr) && (FslIsSingleFileType(FslGetFileType(fslio))) &&
(FslIsCompressedFileType(FslGetFileType(fslio))) )
{ FSLIOERR("FslWriteVolumes: header must be written before data for single compressed file types"); }
if (fslio->niftiptr!=NULL) {
size_t bytes_written;
size_t nbytes;
long int bpv = fslio->niftiptr->nbyper; /* bytes per voxel */
nbytes = nvols * FslGetVolSize(fslio) * bpv;
if ( (FslBaseFileType(FslGetFileType(fslio))==FSL_TYPE_ANALYZE)
&& (FslGetLeftRightOrder(fslio)==FSL_NEUROLOGICAL) ) {
/* If it is Analyze and Neurological order then SWAP DATA into Radiological order */
/* This is nasty - but what else can be done?!? */
char *tmpbuf, *inbuf;
long int x, b, n, nrows;
short nx, ny, nz, nv;
inbuf = (char *) buffer;
tmpbuf = (char *)calloc(nbytes,1);
FslGetDim(fslio,&nx,&ny,&nz,&nv);
nrows = nbytes / (nx * bpv);
for (n=0; n<nrows; n++) {
for (x=0; x<nx; x++) {
for (b=0; b<bpv; b++) {
tmpbuf[b + bpv * (n*nx + nx - 1 - x)] = inbuf[b + bpv * (n*nx + x)];
}
}
}
bytes_written = nifti_write_buffer(fslio->fileptr, tmpbuf, nbytes);
free(tmpbuf);
} else {
bytes_written = nifti_write_buffer(fslio->fileptr, buffer, nbytes);
}
retval = ( bytes_written == nbytes ) ? bytes_written : 0;
}
if (fslio->mincptr!=NULL) {
fprintf(stderr,"Warning:: Minc is not yet supported\n");
}
return retval;
}
/***************************************************************
* FslWriteHeader
***************************************************************/
/*! \fn void FslWriteHeader(FSLIO *fslio)
\brief Writes nifti/anz header and opens img file ready for writing
\param fslio pointer to open dataset
*/
void FslWriteHeader(FSLIO *fslio)
{
/* writes header and opens img file ready for writing */
if (fslio==NULL) FSLIOERR("FslWriteHeader: Null pointer passed for FSLIO");
if (fslio->niftiptr!=NULL) {
fslio->written_hdr = 1;
if (znz_isnull(fslio->fileptr)) FSLIOERR("FslWriteHeader: no file opened!");
strcpy(fslio->niftiptr->descrip,"FSL3.2beta");
if (FslIsSingleFileType(FslGetFileType(fslio))) {
/* write header info but don't close the file */
nifti_image_write_hdr_img2(fslio->niftiptr,2,"wb",fslio->fileptr,NULL);
/* set up pointer at end of iname_offset for single files only */
FslSeekVolume(fslio,0);
} else {
/* open a new hdr file, write it and close it */
nifti_image_write_hdr_img(fslio->niftiptr,0,"wb");
}
}
if (fslio->mincptr!=NULL) {
fprintf(stderr,"Warning:: Minc is not yet supported\n");
}
return;
}
/***************************************************************
* FslReadSliceSeries
***************************************************************/
/*! \fn size_t FslReadSliceSeries(FSLIO *fslio, void *buffer, short slice, size_t nvols)
\brief Read one slice from each of the first nvols volumes in the dataset, ie get an xyt buffer.
Dimension and datatype of buffer are as is specified in nifti_image structure
fslio->niftiptr.
Note: filepointer in file data array is restored to its initial position.
\param fslio pointer to open dataset
\param buffer buffer large enough to hold 1 slice from each volume
\param slice slice number (0 based) to read [0 z-1]
\param nvols number of volumes to read a slice from
\return Number of volumes from which a slice was successfully read. 0 on error.
*/
size_t FslReadSliceSeries(FSLIO *fslio, void *buffer, short slice, size_t nvols)
{
size_t slbytes,volbytes;