TPCCLIB
Loading...
Searching...
No Matches
analyzeio.c File Reference

Procedures for Analyze images. More...

#include "tpcclibConfig.h"
#include "tpcnifti.h"

Go to the source code of this file.

Functions

int anaExists (const char *filename, char *hdrfile, char *imgfile, char *siffile, ANALYZE_DSR *header, TPCSTATUS *status)
 
int anaReadHeader (const char *filename, ANALYZE_DSR *dsr, int verbose)
 

Detailed Description

Procedures for Analyze images.

Definition in file analyzeio.c.

Function Documentation

◆ anaExists()

int anaExists ( const char * filename,
char * hdrfile,
char * imgfile,
char * siffile,
ANALYZE_DSR * header,
TPCSTATUS * status )

Verify if specified file name is an Analyze 7.5 file.

Todo
Add tests.
See also
anaReadHeader, niftiExists, micropetExists
Returns
Returns 1 if it is Analyze, 0 if not.
Parameters
filenameFile name, either header file, image file, or base name without extensions.
hdrfileIf file name refers to an Analyze file, then header file name will be written in this char pointer (space needs to allocated by caller); If header and image are combined, then this will be the name of combined file; enter NULL if not needed.
imgfileIf file name refers to an Analyze file, then image file name will be written in this char pointer (space needs to allocated by caller); If header and image are combined, then this will be the name of combined file; enter NULL if not needed.
siffileIf file name refers to an Analyze file, and if SIF exists, then SIF file name will be written in this char pointer (space needs to allocated by caller); enter NULL if not needed.
headerPointer to Analyze header, which is filled in this function; enter NULL, if not needed.
statusPointer to status data; enter NULL if not needed.

Definition at line 16 of file analyzeio.c.

37 {
38 int verbose=0; if(status!=NULL) verbose=status->verbose;
39 if(verbose>0) {printf("%s(%s, ...)\n", __func__, filename); fflush(stdout);}
40 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
41
42 /* Initiate output */
43 if(hdrfile!=NULL) hdrfile[0]=(char)0;
44 if(imgfile!=NULL) imgfile[0]=(char)0;
45 if(siffile!=NULL) siffile[0]=(char)0;
46
47 /* Empty file name means not Analyze file */
48 if(strnlen(filename, 2)<1) {
49 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FILENAME);
50 return(0);
51 }
52
53 /* Construct the base file name wo extensions */
54 char basefile[FILENAME_MAX]; strlcpy(basefile, filename, FILENAME_MAX);
55 // If file exists it has extensions (similar than NIfTI-1)
56 if(fileExist(basefile)) niftiBasename(basefile);
57 if(verbose>1) printf(" basefile := %s\n", basefile);
58
59 /* Combined header and image file exists? */
60 int combined=0;
61 char temp[FILENAME_MAX+10], localhdrfile[FILENAME_MAX];
62 localhdrfile[0]=(char)0;
63 strcpy(temp, basefile); strcat(temp, ".nii");
64 if(fileExist(temp)) {
65 if(verbose>1) printf(" %s exists.\n", temp);
66 /* Preserve header and image file names */
67 strcpy(localhdrfile, temp);
68 if(hdrfile!=NULL) strlcpy(hdrfile, temp, FILENAME_MAX);
69 if(imgfile!=NULL) strlcpy(imgfile, temp, FILENAME_MAX);
70 combined=1;
71 } else {
72 if(verbose>1) printf(" %s does not exist.\n", temp);
73 /* Not combined file, therefore check that header file exists */
74 strcpy(temp, basefile); strcat(temp, ".hdr");
75 if(!fileExist(temp)) {
76 strcpy(temp, basefile); strcat(temp, ".img.hdr");
77 if(!fileExist(temp)) {
78 if(verbose>1) printf(" hdr file not found.\n");
79 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_MISSING_HEADER);
80 return(0);
81 }
82 }
83 /* Preserve file name of the header */
84 strlcpy(localhdrfile, temp, FILENAME_MAX);
85 if(hdrfile!=NULL) strlcpy(hdrfile, temp, FILENAME_MAX);
86 if(verbose>1) printf(" %s is found.\n", localhdrfile);
87 /* Not combined file, therefore check that image file exists */
88 strcpy(temp, basefile); strcat(temp, ".img");
89 if(!fileExist(temp)) {
90 if(verbose>1) printf(" %s not found.\n", temp);
91 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_FILE);
92 return(0);
93 }
94 /* Preserve image filename */
95 if(imgfile!=NULL) strlcpy(imgfile, temp, FILENAME_MAX);
96 if(verbose>1) printf(" %s is found.\n", temp);
97 }
98 if(verbose>1) {printf(" combined := %d\n", combined); fflush(stdout);}
99
100 /* Read header to check that this indeed is Analyze */
101 {
102 ANALYZE_DSR *dsr, local_dsr; if(header==NULL) dsr=&local_dsr; else dsr=header;
103 int ret=anaReadHeader(localhdrfile, dsr, verbose-2);
104 if(ret!=TPCERROR_OK) {
105 if(verbose>1) {
106 printf(" %s was not identified as Analyze header file.\n", localhdrfile); fflush(stdout);}
107 statusSet(status, __func__, __FILE__, __LINE__, ret);
108 return(0);
109 }
110 if(verbose>1) {printf(" %s is identified as Analyze.\n", localhdrfile); fflush(stdout);}
111 }
112
113 /* SIF exists? */
114 strcpy(temp, basefile); strcat(temp, ".sif");
115 if(verbose>3) printf(" checking if %s exists\n", temp);
116 if(!fileExist(temp)) {
117 strcpy(temp, basefile); strcat(temp, ".img.sif");
118 if(verbose>3) printf(" checking if %s exists\n", temp);
119 if(!fileExist(temp)) {
120 strcpy(temp, basefile); strcat(temp, ".nii.sif");
121 if(verbose>3) printf(" checking if %s exists\n", temp);
122 if(!fileExist(temp)) {
123 if(verbose>0) printf(" SIF not found or accessible.\n");
124 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
125 return(1); // but otherwise ok NIfTI
126 }
127 }
128 }
129 /* Preserve SIF filename */
130 if(siffile!=NULL) strcpy(siffile, temp);
131 if(verbose>1) {printf(" %s is found.\n", temp); fflush(stdout);}
132
133 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
134 return(1);
135}
int anaReadHeader(const char *filename, ANALYZE_DSR *dsr, int verbose)
Definition analyzeio.c:144
int fileExist(const char *filename)
Definition filexist.c:17
void niftiBasename(char *filename)
Definition niftiname.c:14
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
int verbose
Verbose level, used by statusPrint() etc.
@ TPCERROR_NO_FILE
File does not exist.
@ TPCERROR_OK
No error.
@ TPCERROR_INVALID_FILENAME
Invalid file name.
@ TPCERROR_MISSING_HEADER
Missing header.

Referenced by imgRead().

◆ anaReadHeader()

int anaReadHeader ( const char * filename,
ANALYZE_DSR * dsr,
int verbose )

Read Analyze header contents.

Todo
Add tests.
See also
anaExists, micropetHeaderRead
Returns
enum tpcerror (TPCERROR_OK when successful).
Parameters
filenameName of file to read (including path and extension).
dsrPointer to previously allocated header structure.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout

Definition at line 144 of file analyzeio.c.

151 {
152 if(verbose>0) {printf("%s(%s, ...)\n", __func__, filename); fflush(stdout);}
153
154 /* Check arguments */
155 if(strnlen(filename, 2)<1) return(TPCERROR_INVALID_FILENAME);
156 if(dsr==NULL) return(TPCERROR_FAIL);
157
158 /* Is current platform little endian (1) or not (0) ? */
159 int little=endianLittle();
160 if(verbose>2) {
161 if(little) printf("little endian platform\n"); else printf("big endian platform\n");
162 }
163
164 /* Open file */
165 FILE *fp=fopen(filename, "rb"); if(fp==NULL) return(TPCERROR_CANNOT_OPEN);
166
167 /* Try to read the size of header, which should be stored in the beginning as
168 4-byte integer in both NIfTI and Analyze header files. */
169 int same_order=1;
170 int hdrSize=0;
171 {
172 char buf[4];
173 if(fread(buf, 4, 1, fp)<1) {fclose(fp); return(TPCERROR_CANNOT_READ);}
174 memcpy(&hdrSize, buf, 4);
175 if(hdrSize!=ANALYZE_HEADER_SIZE && hdrSize!=NIFTI2_HEADER_SIZE) {
176 swawbip(&hdrSize, 4); same_order=0;
177 }
178 if(verbose>1) {printf(" sizeof_hdr := %d\n", hdrSize); fflush(stdout);}
179 if(hdrSize==NIFTI2_HEADER_SIZE) {
180 if(verbose>1) {printf(" NIfTI-2 header size.\n"); fflush(stdout);}
181 fclose(fp); return(TPCERROR_INVALID_FORMAT);
182 } else if(hdrSize==ANALYZE_HEADER_SIZE) {
183 if(verbose>1) {printf(" Analyze or NIfTI-1 header size.\n"); fflush(stdout);}
184 } else {
185 if(verbose>1) {printf(" invalid Analyze sizeof_hdr\n"); fflush(stdout);}
187 }
188 }
189
190
191 /* Read file into Analyze header structure */
192 if(verbose>1) {printf(" reading header as binary data\n"); fflush(stdout);}
193 unsigned char buf[ANALYZE_HEADER_SIZE];
194 if(fread(buf, ANALYZE_HEADER_SIZE, 1, fp)<1) {fclose(fp); return(TPCERROR_CANNOT_READ);}
195 /* Close file */
196 fclose(fp);
197
198 /* Check that header does not have the Nifti Magic number */
199 char magic[4];
200 memcpy(magic, buf+344, 4); magic[3]=(char)0;
201 if(strcasecmp(magic, "ni1")==0 || strcasecmp(magic, "n+1")==0) {
202 if(verbose>1) {printf(" Nifti magic number was found"); fflush(stdout);}
204 }
205
206 /* Set the original byte order */
207 if(same_order) dsr->byte_order=little;
208 else {if(little==1) dsr->byte_order=0; else dsr->byte_order=1;}
209
210 /* Set key header structure contents */
211 dsr->h.sizeof_hdr=hdrSize;
212 memcpy(dsr->h.data_type, buf+4, 10);
213 memcpy(dsr->h.db_name, buf+14, 18);
214 if(!same_order) swawbip(buf+32, 4);
215 memcpy(&dsr->h.extents, buf+32, 4);
216 if(!same_order) swabip(buf+36, 2);
217 memcpy(&dsr->h.session_error, buf+36, 2);
218 memcpy(&dsr->h.regular, buf+38, 1);
219 memcpy(&dsr->h.hkey_un0, buf+39, 1);
220
221 /* Set image dimension header structure contents */
222 if(!same_order) swabip(buf+40, 16);
223 memcpy(dsr->h.dim, buf+40, 16);
224 if(!same_order) swabip(buf+56, 2);
225 memcpy(&dsr->h.unused8, buf+56, 2);
226 if(!same_order) swabip(buf+58, 2);
227 memcpy(&dsr->h.unused9, buf+58, 2);
228 if(!same_order) swabip(buf+60, 2);
229 memcpy(&dsr->h.unused10, buf+60, 2);
230 if(!same_order) swabip(buf+62, 2);
231 memcpy(&dsr->h.unused11, buf+62, 2);
232 if(!same_order) swabip(buf+64, 2);
233 memcpy(&dsr->h.unused12, buf+64, 2);
234 if(!same_order) swabip(buf+66, 2);
235 memcpy(&dsr->h.unused13, buf+66, 2);
236 if(!same_order) swabip(buf+68, 2);
237 memcpy(&dsr->h.unused14, buf+68, 2);
238 if(!same_order) swabip(buf+70, 2);
239 memcpy(&dsr->h.datatype, buf+70, 2);
240 if(!same_order) swabip(buf+72, 2);
241 memcpy(&dsr->h.bitpix, buf+72, 2);
242 if(!same_order) swabip(buf+74, 2);
243 memcpy(&dsr->h.dim_un0, buf+74, 2);
244 if(!same_order) swawbip(buf+76, 32);
245 memcpy(dsr->h.pixdim, buf+76, 32);
246 if(!same_order) swawbip(buf+108, 4);
247 memcpy(&dsr->h.vox_offset, buf+108, 4);
248 if(!same_order) swawbip(buf+112, 4);
249 memcpy(&dsr->h.funused1, buf+112, 4);
250 if(!same_order) swawbip(buf+116, 4);
251 memcpy(&dsr->h.funused2, buf+116, 4);
252 if(!same_order) swawbip(buf+120, 4);
253 memcpy(&dsr->h.funused3, buf+120, 4);
254 if(!same_order) swawbip(buf+124, 4);
255 memcpy(&dsr->h.cal_max, buf+124, 4);
256 if(!same_order) swawbip(buf+128, 4);
257 memcpy(&dsr->h.cal_min, buf+128, 4);
258 if(!same_order) swawbip(buf+132, 4);
259 memcpy(&dsr->h.compressed, buf+132, 4);
260 if(!same_order) swawbip(buf+136, 4);
261 memcpy(&dsr->h.verified, buf+136, 4);
262 if(!same_order) swawbip(buf+140, 4);
263 memcpy(&dsr->h.glmax, buf+140, 4);
264 if(!same_order) swawbip(buf+144, 4);
265 memcpy(&dsr->h.glmin, buf+144, 4);
266
267 /* Set data history header structure contents */
268 memcpy(dsr->h.descrip, buf+148, 80);
269 memcpy(dsr->h.aux_file, buf+228, 24);
270 memcpy(&dsr->h.orient, buf+252, 1);
271 memcpy(dsr->h.originator, buf+253, 10);
272 memcpy(dsr->h.generated, buf+263, 10);
273 memcpy(dsr->h.scannum, buf+273, 10);
274 memcpy(dsr->h.patient_id, buf+283, 10);
275 memcpy(dsr->h.exp_date, buf+293, 10);
276 memcpy(dsr->h.exp_time, buf+303, 10);
277 memcpy(dsr->h.hist_un0, buf+313, 3);
278 if(!same_order) swawbip(buf+316, 4);
279 memcpy(&dsr->h.views, buf+316, 4);
280 if(!same_order) swawbip(buf+320, 4);
281 memcpy(&dsr->h.vols_added, buf+320, 4);
282 if(!same_order) swawbip(buf+324, 4);
283 memcpy(&dsr->h.start_field, buf+324,4);
284 if(!same_order) swawbip(buf+328, 4);
285 memcpy(&dsr->h.field_skip, buf+328, 4);
286 if(!same_order) swawbip(buf+332, 4);
287 memcpy(&dsr->h.omax, buf+332, 4);
288 if(!same_order) swawbip(buf+336, 4);
289 memcpy(&dsr->h.omin, buf+336, 4);
290 if(!same_order) swawbip(buf+340, 4);
291 memcpy(&dsr->h.smax, buf+340, 4);
292 if(!same_order) swawbip(buf+344, 4);
293 memcpy(&dsr->h.smin, buf+344, 4);
294
295 /* Check header contents */
296 if(dsr->h.extents!=16384 && dsr->h.extents!=0) {
297 if(verbose>0) printf("h.extents := %d\n", dsr->h.extents);
298 return(TPCERROR_UNSUPPORTED);
299 }
300 if(dsr->h.regular!='r') {
301 if(verbose>1) printf("h.regular := %c\n", dsr->h.regular);
302 return(TPCERROR_UNSUPPORTED);
303 }
304
305 if(verbose>1) {printf(" complete Analyze header was read.\n"); fflush(stdout);}
306
307 return(TPCERROR_OK);
308}
void swabip(void *buf, int size)
Definition endian.c:115
int endianLittle()
Definition endian.c:53
void swawbip(void *buf, int size)
Definition endian.c:138
int byte_order
Definition tpcnifti.h:526
ANALYZE_HEADER h
Definition tpcnifti.h:524
char data_type[10]
Definition tpcnifti.h:423
short int bitpix
Definition tpcnifti.h:456
char patient_id[10]
Definition tpcnifti.h:496
char aux_file[24]
Definition tpcnifti.h:485
short int dim[8]
Definition tpcnifti.h:437
char hist_un0[3]
Definition tpcnifti.h:502
short int unused10
Definition tpcnifti.h:443
float pixdim[8]
Definition tpcnifti.h:460
short int unused8
Definition tpcnifti.h:439
short int dim_un0
Definition tpcnifti.h:458
float compressed
Definition tpcnifti.h:475
short int unused9
Definition tpcnifti.h:441
char scannum[10]
Definition tpcnifti.h:494
short int session_error
Definition tpcnifti.h:430
short int unused11
Definition tpcnifti.h:445
char descrip[80]
Definition tpcnifti.h:483
short int unused14
Definition tpcnifti.h:451
short int unused13
Definition tpcnifti.h:449
char exp_date[10]
Definition tpcnifti.h:498
char exp_time[10]
Definition tpcnifti.h:500
short int datatype
Definition tpcnifti.h:454
char originator[10]
Definition tpcnifti.h:490
float vox_offset
Definition tpcnifti.h:463
char generated[10]
Definition tpcnifti.h:492
short int unused12
Definition tpcnifti.h:447
char db_name[18]
Definition tpcnifti.h:425
@ TPCERROR_FAIL
General error.
@ TPCERROR_INVALID_FORMAT
Invalid file format.
@ TPCERROR_CANNOT_OPEN
Cannot open file.
@ TPCERROR_UNSUPPORTED
Unsupported file type.
@ TPCERROR_CANNOT_READ
Cannot read file.
#define ANALYZE_HEADER_SIZE
Definition tpcnifti.h:41
#define NIFTI2_HEADER_SIZE
Definition tpcnifti.h:37

Referenced by anaExists().