TPCCLIB
Loading...
Searching...
No Matches
analyze.c
Go to the documentation of this file.
1
7/*****************************************************************************/
9/*****************************************************************************/
10#include "libtpcimgio.h"
11/*****************************************************************************/
12
13/*****************************************************************************/
21 const char *dbname
22) {
23 char temp[FILENAME_MAX];
24
25 if(dbname==NULL || strlen(dbname)==0) return(0);
26 /* Header file? */
27 strlcpy(temp, dbname, FILENAME_MAX); strlcat(temp, ".hdr", FILENAME_MAX);
28 if(access(temp, 0) == -1) return(0);
29 /* Image data? */
30 strlcpy(temp, dbname, FILENAME_MAX); strlcat(temp, ".img", FILENAME_MAX);
31 if(access(temp, 0) == -1) return(0);
32 /* SIF? */
33 strlcat(temp, ".sif", FILENAME_MAX); if(access(temp, 0) != -1) return(2);
34 strlcpy(temp, dbname, FILENAME_MAX); strlcat(temp, ".sif", FILENAME_MAX);
35 if(access(temp, 0) != -1) return(2);
36 return(1);
37}
38/*****************************************************************************/
39
40/*****************************************************************************/
48 const char *filename,
52 char *hdrfile,
56 char *imgfile,
60 char *siffile
61) {
62 char *cptr, basefile[FILENAME_MAX], temp[FILENAME_MAX];
64 int ret;
65
66 if(filename==NULL || strlen(filename)==0) return(0);
67 if(ANALYZE_TEST>1) printf("\nanaExistsNew(%s, *str, *str, *str)\n", filename);
68
69 /* Construct the base file name wo extensions */
70 strcpy(basefile, filename);
71 cptr=strrchr(basefile, '.');
72 if(cptr!=NULL) {
73 if(strncasecmp(cptr, ".HDR", 4)==0 || strncasecmp(cptr, ".IMG", 4)==0 )
74 *cptr=(char)0;
75 }
76 cptr=strrchr(basefile, '.');
77 if(cptr!=NULL) {
78 if(strncasecmp(cptr, ".IMG", 4)==0 )
79 *cptr=(char)0;
80 }
81 if(ANALYZE_TEST>2) printf("\n basefile := %s\n", basefile);
82
83 /* Header file exists? */
84 strcpy(temp, basefile); strcat(temp, ".hdr");
85 if(access(temp, 0) == -1) {
86 strcpy(temp, basefile); strcat(temp, ".img.hdr");
87 if(access(temp, 0) == -1) {
88 if(ANALYZE_TEST) printf("\n hdr file not found or accessible.\n");
89 return(0);
90 }
91 }
92 /* Is this Analyze header file? */
93 if((ret=anaReadHeader(temp, &h))!=0) {
94 if(ANALYZE_TEST) printf("\n %s was not identified as Analyze header file (%d).\n", temp, ret);
95 return(0);
96 }
97 /* Preserve header filename */
98 if(hdrfile!=NULL) strcpy(hdrfile, temp);
99
100 /* Image file exists? */
101 strcpy(temp, basefile); strcat(temp, ".img");
102 if(access(temp, 0) == -1) {
103 if(ANALYZE_TEST) printf("\n %s not found or accessible.\n", temp);
104 return(0);
105 }
106 /* Preserve image filename */
107 if(imgfile!=NULL) strcpy(imgfile, temp);
108
109 /* SIF exists? */
110 strcpy(temp, basefile); strcat(temp, ".sif");
111 if(access(temp, 0) == -1) {
112 strcpy(temp, basefile); strcat(temp, ".img.sif");
113 if(access(temp, 0) == -1) {
114 if(ANALYZE_TEST) printf("\n SIF not found or accessible.\n");
115 if(siffile!=NULL) strcpy(siffile, "");
116 return(1); // but otherwise ok
117 }
118 }
119 /* Preserve SIF filename */
120 if(siffile!=NULL) strcpy(siffile, temp);
121 return(2);
122}
123/*****************************************************************************/
124
125/*****************************************************************************/
133 char *filename,
135 ANALYZE_DSR *h
136) {
137 unsigned char buf1[ANALYZE_HEADER_KEY_SIZE];
138 unsigned char buf2[ANALYZE_HEADER_IMGDIM_SIZE];
139 unsigned char buf3[ANALYZE_HEADER_HISTORY_SIZE];
140 int little; /* 1 if current platform is little endian (i386), else 0 */
141 FILE *fp;
142 int ret, nr=0, s1, s2, same_order;
143
144 if(ANALYZE_TEST) printf("anaReadHeader(%s, *dsr)\n", filename);
145
146 /* Check arguments */
147 if(strlen(filename)<1 || h==NULL) return(1);
148 little=little_endian();
149 /* Open file */
150 fp=fopen(filename, "rb"); if(fp==NULL) return(2);
151 /* Get file size */
152 nr=0; while((ret=fgetc(fp))!=EOF) nr++; rewind(fp);
153 if(nr<1) {fclose(fp); return(3);}
154 /* Read Analyze header key */
155 if(fread(buf1, ANALYZE_HEADER_KEY_SIZE, 1, fp)<1) return(3);
156 /* Read Analyze header image dimension */
157 if(fread(buf2, ANALYZE_HEADER_IMGDIM_SIZE, 1, fp)<1) return(3);
158 /* Read Analyze header image data history */
159 memset(buf3, 0, sizeof(ANALYZE_HEADER_HISTORY));
160 ret=fread(buf3, ANALYZE_HEADER_HISTORY_SIZE, 1, fp);
161 if(ANALYZE_TEST>1 && ret<1) printf(" complete data_history not found.\n");
162 /* Close file */
163 fclose(fp);
164 /* Compare file size from header contents to the calculated value */
165 /* to determine whether Analyze file is in little or big endian */
166 memcpy(&s1, buf1+0, 4); s2=s1; swawbip(&s2, 4);
167 if(abs(s1-nr)<abs(s2-nr)) same_order=1; else same_order=0;
168 if(ANALYZE_TEST>1) printf("same byte order: %d (s1=%d s2=%d nr=%d)\n",
169 same_order, s1, s2, nr);
170 if(same_order) h->little=little;
171 else {if(little) h->little=0; else h->little=1;}
172
173 /* Set key header structure contents */
174 if(!same_order) swawbip(buf1+0, 4);
175 memcpy(&h->hk.sizeof_hdr, buf1+0, 4);
176 memcpy(h->hk.data_type, buf1+4, 10);
177 memcpy(h->hk.db_name, buf1+14, 18);
178 if(!same_order) swawbip(buf1+32, 4);
179 memcpy(&h->hk.extents, buf1+32, 4);
180 if(!same_order) swabip(buf1+36, 2);
181 memcpy(&h->hk.session_error, buf1+36, 2);
182 memcpy(&h->hk.regular, buf1+38, 1);
183 memcpy(&h->hk.hkey_un0, buf1+39, 1);
184
185 /* Set image dimension header structure contents */
186 if(!same_order) swabip(buf2+0, 16);
187 memcpy(h->dime.dim, buf2+0, 16);
188 if(!same_order) swabip(buf2+16, 2);
189 memcpy(&h->dime.unused8, buf2+16, 2);
190 if(!same_order) swabip(buf2+18, 2);
191 memcpy(&h->dime.unused9, buf2+18, 2);
192 if(!same_order) swabip(buf2+20, 2);
193 memcpy(&h->dime.unused10, buf2+20, 2);
194 if(!same_order) swabip(buf2+22, 2);
195 memcpy(&h->dime.unused11, buf2+22, 2);
196 if(!same_order) swabip(buf2+24, 2);
197 memcpy(&h->dime.unused12, buf2+24, 2);
198 if(!same_order) swabip(buf2+26, 2);
199 memcpy(&h->dime.unused13, buf2+26, 2);
200 if(!same_order) swabip(buf2+28, 2);
201 memcpy(&h->dime.unused14, buf2+28, 2);
202 if(!same_order) swabip(buf2+30, 2);
203 memcpy(&h->dime.datatype, buf2+30, 2);
204 if(!same_order) swabip(buf2+32, 2);
205 memcpy(&h->dime.bitpix, buf2+32, 2);
206 if(!same_order) swabip(buf2+34, 2);
207 memcpy(&h->dime.dim_un0, buf2+34, 2);
208 if(!same_order) swawbip(buf2+36, 32);
209 memcpy(h->dime.pixdim, buf2+36, 32);
210 if(!same_order) swawbip(buf2+68, 4);
211 memcpy(&h->dime.vox_offset, buf2+68, 4);
212 if(!same_order) swawbip(buf2+72, 4);
213 memcpy(&h->dime.funused1, buf2+72, 4);
214 if(!same_order) swawbip(buf2+76, 4);
215 memcpy(&h->dime.funused2, buf2+76, 4);
216 if(!same_order) swawbip(buf2+80, 4);
217 memcpy(&h->dime.funused3, buf2+80, 4);
218 if(!same_order) swawbip(buf2+84, 4);
219 memcpy(&h->dime.cal_max, buf2+84, 4);
220 if(!same_order) swawbip(buf2+88, 4);
221 memcpy(&h->dime.cal_min, buf2+88, 4);
222 if(!same_order) swawbip(buf2+92, 4);
223 memcpy(&h->dime.compressed, buf2+92, 4);
224 if(!same_order) swawbip(buf2+96, 4);
225 memcpy(&h->dime.verified, buf2+96, 4);
226 if(!same_order) swawbip(buf2+100, 4);
227 memcpy(&h->dime.glmax, buf2+100, 4);
228 if(!same_order) swawbip(buf2+104, 4);
229 memcpy(&h->dime.glmin, buf2+104, 4);
230
231 /* Set data history header structure contents */
232 memcpy(h->hist.descrip, buf3+0, 80);
233 memcpy(h->hist.aux_file, buf3+80, 24);
234 memcpy(&h->hist.orient, buf3+104, 1);
235 memcpy(h->hist.originator, buf3+105, 10);
236 memcpy(h->hist.generated, buf3+115, 10);
237 memcpy(h->hist.scannum, buf3+125, 10);
238 memcpy(h->hist.patient_id, buf3+135, 10);
239 memcpy(h->hist.exp_date, buf3+145, 10);
240 memcpy(h->hist.exp_time, buf3+155, 10);
241 memcpy(h->hist.hist_un0, buf3+165, 3);
242 if(!same_order) swawbip(buf3+168, 4);
243 memcpy(&h->hist.views, buf3+168, 4);
244 if(!same_order) swawbip(buf3+172, 4);
245 memcpy(&h->hist.vols_added, buf3+172, 4);
246 if(!same_order) swawbip(buf3+176, 4);
247 memcpy(&h->hist.start_field, buf3+176,4);
248 if(!same_order) swawbip(buf3+180, 4);
249 memcpy(&h->hist.field_skip, buf3+180, 4);
250 if(!same_order) swawbip(buf3+184, 4);
251 memcpy(&h->hist.omax, buf3+184, 4);
252 if(!same_order) swawbip(buf3+188, 4);
253 memcpy(&h->hist.omin, buf3+188, 4);
254 if(!same_order) swawbip(buf3+192, 4);
255 memcpy(&h->hist.smax, buf3+192, 4);
256 if(!same_order) swawbip(buf3+196, 4);
257 memcpy(&h->hist.smin, buf3+196, 4);
258
259 /* Check header contents */
260 if(h->hk.extents!=16384 && h->hk.extents!=0) {
261 if(ANALYZE_TEST>1) printf("hk.extents := %d\n", h->hk.extents);
262 return(11);
263 }
264 if(h->hk.regular!='r') {
265 if(ANALYZE_TEST>1) printf("hk.regular := %c\n", h->hk.regular);
266 return(12);
267 }
268
269 return(0);
270}
271/*****************************************************************************/
272
273/*****************************************************************************/
284 char *filename,
286 ANALYZE_DSR *h
287) {
288 unsigned char buf1[ANALYZE_HEADER_KEY_SIZE];
289 unsigned char buf2[ANALYZE_HEADER_IMGDIM_SIZE];
290 unsigned char buf3[ANALYZE_HEADER_HISTORY_SIZE];
291 FILE *fp;
292 int same_order, little;
293
294
295 if(ANALYZE_TEST) printf("anaWriteHeader(%s, *dsr)\n", filename);
296
297 /* Check arguments */
298 if(strlen(filename)<1 || h==NULL) return(1);
299 little=little_endian();
300 if(little==h->little) same_order=1; else same_order=0;
301
302 /* Copy header contents into buffers */
303 /* Header key */
304 memset(buf1, 0, ANALYZE_HEADER_KEY_SIZE);
305 memcpy(buf1+0, &h->hk.sizeof_hdr, 4); if(!same_order) swawbip(buf1+0, 4);
306 memcpy(buf1+4, &h->hk.data_type, 10);
307 memcpy(buf1+14, &h->hk.db_name, 18);
308 memcpy(buf1+32, &h->hk.extents, 4); if(!same_order) swawbip(buf1+32, 4);
309 memcpy(buf1+36, &h->hk.session_error, 2); if(!same_order) swabip(buf1+36, 2);
310 memcpy(buf1+38, &h->hk.regular, 1);
311 memcpy(buf1+39, &h->hk.hkey_un0, 1);
312 /* Image dimension */
313 memset(buf2, 0, ANALYZE_HEADER_IMGDIM_SIZE);
314 memcpy(buf2+0, h->dime.dim, 16); if(!same_order) swabip(buf2+0, 16);
315 memcpy(buf2+16, &h->dime.unused8, 2); if(!same_order) swabip(buf2+16, 2);
316 memcpy(buf2+18, &h->dime.unused9, 2); if(!same_order) swabip(buf2+18, 2);
317 memcpy(buf2+20, &h->dime.unused10, 2); if(!same_order) swabip(buf2+20, 2);
318 memcpy(buf2+22, &h->dime.unused11, 2); if(!same_order) swabip(buf2+22, 2);
319 memcpy(buf2+24, &h->dime.unused12, 2); if(!same_order) swabip(buf2+24, 2);
320 memcpy(buf2+26, &h->dime.unused13, 2); if(!same_order) swabip(buf2+26, 2);
321 memcpy(buf2+28, &h->dime.unused14, 2); if(!same_order) swabip(buf2+28, 2);
322 memcpy(buf2+30, &h->dime.datatype, 2); if(!same_order) swabip(buf2+30, 2);
323 memcpy(buf2+32, &h->dime.bitpix, 2); if(!same_order) swabip(buf2+32, 2);
324 memcpy(buf2+34, &h->dime.dim_un0, 2); if(!same_order) swabip(buf2+34, 2);
325 memcpy(buf2+36, h->dime.pixdim, 32); if(!same_order) swawbip(buf2+36, 32);
326 memcpy(buf2+68, &h->dime.vox_offset, 4); if(!same_order) swawbip(buf2+68, 4);
327 memcpy(buf2+72, &h->dime.funused1, 4); if(!same_order) swawbip(buf2+72, 4);
328 memcpy(buf2+76, &h->dime.funused2, 4); if(!same_order) swawbip(buf2+76, 4);
329 memcpy(buf2+80, &h->dime.funused3, 4); if(!same_order) swawbip(buf2+80, 4);
330 memcpy(buf2+84, &h->dime.cal_max, 4); if(!same_order) swawbip(buf2+84, 4);
331 memcpy(buf2+88, &h->dime.cal_min, 4); if(!same_order) swawbip(buf2+88, 4);
332 memcpy(buf2+92, &h->dime.compressed, 4); if(!same_order) swawbip(buf2+92, 4);
333 memcpy(buf2+96, &h->dime.verified, 4); if(!same_order) swawbip(buf2+96, 4);
334 memcpy(buf2+100, &h->dime.glmax, 4); if(!same_order) swawbip(buf2+100, 4);
335 memcpy(buf2+104, &h->dime.glmin, 4); if(!same_order) swawbip(buf2+104, 4);
336 /* Data history */
337 memset(buf3, 0, ANALYZE_HEADER_HISTORY_SIZE);
338 memcpy(buf3+0, &h->hist.descrip, 80);
339 memcpy(buf3+80, &h->hist.aux_file, 24);
340 memcpy(buf3+104, &h->hist.orient, 1);
341 memcpy(buf3+105, &h->hist.originator, 10);
342 memcpy(buf3+115, &h->hist.generated, 10);
343 memcpy(buf3+125, &h->hist.scannum, 10);
344 memcpy(buf3+135, &h->hist.patient_id, 10);
345 memcpy(buf3+145, &h->hist.exp_date, 10);
346 memcpy(buf3+155, &h->hist.exp_time, 10);
347 memcpy(buf3+165, &h->hist.hist_un0, 3);
348 memcpy(buf3+168, &h->hist.views, 4); if(!same_order) swawbip(buf3+168, 4);
349 memcpy(buf3+172, &h->hist.vols_added,4); if(!same_order) swawbip(buf3+172, 4);
350 memcpy(buf3+176, &h->hist.start_field,4); if(!same_order) swawbip(buf3+176,4);
351 memcpy(buf3+180, &h->hist.field_skip,4); if(!same_order) swawbip(buf3+180, 4);
352 memcpy(buf3+184, &h->hist.omax, 4); if(!same_order) swawbip(buf3+184, 4);
353 memcpy(buf3+188, &h->hist.omin, 4); if(!same_order) swawbip(buf3+188, 4);
354 memcpy(buf3+192, &h->hist.smax, 4); if(!same_order) swawbip(buf3+192, 4);
355 memcpy(buf3+196, &h->hist.smin, 4); if(!same_order) swawbip(buf3+196, 4);
356
357 /* Open header file for write */
358 fp=fopen(filename, "wb"); if(fp==NULL) return(2);
359 /* Write header key */
360 if(fwrite(buf1, 1, ANALYZE_HEADER_KEY_SIZE, fp) != ANALYZE_HEADER_KEY_SIZE) {
361 fclose(fp); return(3);}
362 /* Write image dimension */
363 if(fwrite(buf2, 1, ANALYZE_HEADER_IMGDIM_SIZE, fp) != ANALYZE_HEADER_IMGDIM_SIZE) {
364 fclose(fp); return(4);}
365 /* Write data history */
366 if(fwrite(buf3, 1, ANALYZE_HEADER_HISTORY_SIZE, fp) != ANALYZE_HEADER_HISTORY_SIZE) {
367 fclose(fp); return(5);}
368 fclose(fp);
369
370 return(0);
371}
372/*****************************************************************************/
373
374/*****************************************************************************/
382 ANALYZE_DSR *h,
384 FILE *fp
385) {
386 if(fp==NULL || h==NULL) return(1);
387 fprintf(fp, "original_byte_order := %d (1=little, 0=big)\n", h->little);
388 /* Key */
389 fprintf(fp, "header_key.sizeof_hdr := %d\n", h->hk.sizeof_hdr);
390 fprintf(fp, "header_key.data_type := %.10s\n", h->hk.data_type);
391 fprintf(fp, "header_key.db_name := %.18s\n", h->hk.db_name);
392 fprintf(fp, "header_key.extents := %d\n", h->hk.extents);
393 fprintf(fp, "header_key.session_error := %d\n", h->hk.session_error);
394 fprintf(fp, "header_key.regular := %d (%c)\n",
395 (int)h->hk.regular, h->hk.regular);
396 fprintf(fp, "header_key.hkey_un0 := %d\n", (int)h->hk.hkey_un0);
397 /* Image dimension */
398 fprintf(fp, "header_image_dimension.dim :=");
399 for(int i=0; i<8; i++) fprintf(fp, " %d", h->dime.dim[i]);
400 fprintf(fp, "\n");
401 fprintf(fp, "header_image_dimension.unused8 := %d\n", h->dime.unused8);
402 fprintf(fp, "header_image_dimension.unused9 := %d\n", h->dime.unused9);
403 fprintf(fp, "header_image_dimension.unused10 := %d\n", h->dime.unused10);
404 fprintf(fp, "header_image_dimension.unused11 := %d\n", h->dime.unused11);
405 fprintf(fp, "header_image_dimension.unused12 := %d\n", h->dime.unused12);
406 fprintf(fp, "header_image_dimension.unused13 := %d\n", h->dime.unused13);
407 fprintf(fp, "header_image_dimension.unused14 := %d\n", h->dime.unused14);
408 fprintf(fp, "header_image_dimension.datatype := %d\n", h->dime.datatype);
409 fprintf(fp, "header_image_dimension.bitpix := %d\n", h->dime.bitpix);
410 fprintf(fp, "header_image_dimension.dim_un0 := %d\n", h->dime.dim_un0);
411 fprintf(fp, "header_image_dimension.pixdim :=");
412 for(int i=0; i<8; i++) fprintf(fp, " %g", h->dime.pixdim[i]);
413 fprintf(fp, "\n");
414 fprintf(fp, "header_image_dimension.vox_offset := %g\n", h->dime.vox_offset);
415 fprintf(fp, "header_image_dimension.funused1 := %g\n", h->dime.funused1);
416 fprintf(fp, "header_image_dimension.funused2 := %g\n", h->dime.funused2);
417 fprintf(fp, "header_image_dimension.funused3 := %g\n", h->dime.funused3);
418 fprintf(fp, "header_image_dimension.cal_max := %g\n", h->dime.cal_max);
419 fprintf(fp, "header_image_dimension.cal_min := %g\n", h->dime.cal_min);
420 fprintf(fp, "header_image_dimension.compressed := %g\n", h->dime.compressed);
421 fprintf(fp, "header_image_dimension.verified := %g\n", h->dime.verified);
422 fprintf(fp, "header_image_dimension.glmax := %d\n", h->dime.glmax);
423 fprintf(fp, "header_image_dimension.glmin := %d\n", h->dime.glmin);
424 /* Data history */
425 fprintf(fp, "header_data_history.descrip := %s.80\n", h->hist.descrip);
426 fprintf(fp, "header_data_history.aux_file := %.24s\n", h->hist.aux_file);
427 fprintf(fp, "header_data_history.orient := %d\n", (int)h->hist.orient);
428 fprintf(fp, "header_data_history.originator := %.10s\n", h->hist.originator);
429 fprintf(fp, "header_data_history.generated := %.10s\n", h->hist.generated);
430 fprintf(fp, "header_data_history.scannum := %.10s\n", h->hist.scannum);
431 fprintf(fp, "header_data_history.patient_id := %.10s\n", h->hist.patient_id);
432 fprintf(fp, "header_data_history.exp_date := %.10s\n", h->hist.exp_date);
433 fprintf(fp, "header_data_history.exp_time := %.10s\n", h->hist.exp_time);
434 fprintf(fp, "header_data_history.hist_un0 := %.3s\n", h->hist.hist_un0);
435 fprintf(fp, "header_data_history.views := %d\n", h->hist.views);
436 fprintf(fp, "header_data_history.vols_added := %d\n", h->hist.vols_added);
437 fprintf(fp, "header_data_history.start_field := %d\n", h->hist.start_field);
438 fprintf(fp, "header_data_history.field_skip := %d\n", h->hist.field_skip);
439 fprintf(fp, "header_data_history.omax := %d\n", h->hist.omax);
440 fprintf(fp, "header_data_history.omin := %d\n", h->hist.omin);
441 fprintf(fp, "header_data_history.smax := %d\n", h->hist.smax);
442 fprintf(fp, "header_data_history.smin := %d\n", h->hist.smin);
443
444 return(0);
445}
446/*****************************************************************************/
447
448/*****************************************************************************/
456 FILE *fp,
458 ANALYZE_DSR *h,
460 int frame,
462 float *data
463) {
464 int little;
465 long int dimNr, dimx, dimy, dimz=1, dimt=1;
466 char *mdata, *mptr;
467 float f, *fptr;
468 short int *sptr;
469 int *iptr;
470 double d;
471
472
473 if(ANALYZE_TEST) printf("anaReadImagedata(fp, h, %d, data)\n", frame);
474
475 /* Check the arguments */
476 if(frame<=0 || fp==NULL || h==NULL || data==NULL) return(1);
477
478 /* Get the image dimensions from header */
479 dimNr=h->dime.dim[0]; if(dimNr<2) return(2);
480 dimx=h->dime.dim[1];
481 dimy=h->dime.dim[2];
482 if(dimNr>2) dimz=h->dime.dim[3];
483 if(dimNr>3) dimt=h->dime.dim[4];
484 if(frame>dimt) return(3);
485 long long pxlNr=dimx*dimy*dimz; if(pxlNr<1) return(4);
486
487 /* Allocate memory for the binary data */
488 if(h->dime.bitpix==0 && h->dime.datatype>0) { /* Fix bitpix if necessary */
492 else if(h->dime.datatype==ANALYZE_DT_SIGNED_INT) h->dime.bitpix=32;
493 else if(h->dime.datatype==ANALYZE_DT_FLOAT) h->dime.bitpix=32;
494 else if(h->dime.datatype==ANALYZE_DT_DOUBLE) h->dime.bitpix=64;
495 else if(h->dime.datatype==ANALYZE_DT_RGB) h->dime.bitpix=24;
496 }
497 if(h->dime.bitpix<8) return(5); /* We don't support bit data */
498 long long rawSize=pxlNr*(h->dime.bitpix/8); if(rawSize<1) return(5);
499 if(ANALYZE_TEST>0) printf(" pxlNr=%lld rawSize=%lld\n", pxlNr, rawSize);
500 mdata=(char*)malloc(rawSize); if(mdata==NULL) return(11);
501
502 /* Seek the start of current frame data */
503 long long start_pos=(frame-1)*rawSize;
504 {
505 long int n=(long int)h->dime.vox_offset;
506 if((n>0 && frame==1) || (n<0)) start_pos+=labs(n);
507 }
508 if(ANALYZE_TEST>2) printf("start_pos=%lld\n", start_pos);
509 fseeko(fp, start_pos, SEEK_SET);
510 if(ftello(fp)!=start_pos) {
511 if(ANALYZE_TEST>5) printf("could not move to start_pos\n");
512 free(mdata); return(7);
513 }
514
515 /* Read the data */
516 mptr=mdata;
517 {
518 size_t n=fread(mptr, rawSize, 1, fp);
519 if(n<1) {
520 if(ANALYZE_TEST>5) printf("could read only %zu bytes when request was %lld\n", n, rawSize);
521 free(mdata); return(8);
522 }
523 }
524
525 /* Convert byte order if necessary */
526 little=little_endian(); mptr=mdata;
527 if(little!=h->little) {
528 if(ANALYZE_TEST>0) printf("byte conversion\n");
529 switch(h->dime.bitpix) {
530 case 8: /* no conversion needed */ break;
531 case 16: swabip(mptr, rawSize); break;
532 case 32: swawbip(mptr, rawSize); break;
533 case 64: swawbip(mptr, rawSize); break;
534 default:
535 if(ANALYZE_TEST>5) printf("unsupported anahdr.dime.bitpix := %d\n", h->dime.bitpix);
536 free(mdata); return(5);
537 }
538 }
539
540 /* Get scale factor */
541 f=1.0;
542 if(h->dime.funused1>0.0) f*=h->dime.funused1;
543
544 /* Copy data to float pixel values */
545 mptr=mdata; fptr=data;
546 switch(h->dime.datatype) {
548 if(h->dime.bitpix!=8) {
549 if(ANALYZE_TEST>5) printf("invalid combination of datatype and bitpix (%d, %d)\n",
550 h->dime.datatype, h->dime.bitpix);
551 free(mdata); return(5);
552 }
553 for(long long i=0; i<pxlNr; i++, mptr++, fptr++)
554 *fptr=f*(float)(unsigned char)(*mptr);
555 break;
557 if(h->dime.bitpix!=16) {
558 if(ANALYZE_TEST>5) printf("invalid combination of datatype and bitpix (%d, %d)\n",
559 h->dime.datatype, h->dime.bitpix);
560 free(mdata); return(5);
561 }
562 for(long long i=0; i<pxlNr; i++, mptr+=2, fptr++) {
563 sptr=(short int*)mptr; *fptr=f*(float)(*sptr);
564 }
565 break;
567 if(h->dime.bitpix!=16 && h->dime.bitpix!=32) {
568 if(ANALYZE_TEST>5) printf("invalid combination of datatype and bitpix (%d, %d)\n",
569 h->dime.datatype, h->dime.bitpix);
570 free(mdata); return(5);
571 }
572 if(h->dime.bitpix==16) {
573 for(long long i=0; i<pxlNr; i++, mptr+=2, fptr++) {
574 iptr=(int*)mptr; *fptr=f*(float)(*iptr);
575 }
576 } else if(h->dime.bitpix==32) {
577 for(long long i=0; i<pxlNr; i++, mptr+=4, fptr++) {
578 iptr=(int*)mptr; *fptr=f*(float)(*iptr);
579 }
580 }
581 break;
582 case ANALYZE_DT_FLOAT:
583 if(h->dime.bitpix!=16 && h->dime.bitpix!=32) {
584 if(ANALYZE_TEST>5) printf("invalid combination of datatype and bitpix (%d, %d)\n",
585 h->dime.datatype, h->dime.bitpix);
586 free(mdata); return(5);
587 }
588 if(h->dime.bitpix==16) {
589 memcpy(fptr, mptr, pxlNr*2);
590 for(long long i=0; i<pxlNr; i++, fptr++) *fptr*=f;
591 } else if(h->dime.bitpix==32) {
592 memcpy(fptr, mptr, pxlNr*4);
593 for(long long i=0; i<pxlNr; i++, fptr++) *fptr*=f;
594 }
595 break;
597 if(h->dime.bitpix!=32) {
598 if(ANALYZE_TEST>5) printf("invalid combination of datatype and bitpix (%d, %d)\n",
599 h->dime.datatype, h->dime.bitpix);
600 free(mdata); return(5);
601 }
602 if(h->dime.bitpix==32) {
603 memcpy(fptr, mptr, pxlNr*4);
604 for(long long i=0; i<pxlNr; i++, fptr++) *fptr*=f;
605 }
606 break;
608 /* Add support for 64-bit double, if needed */
609 if(h->dime.bitpix!=32) {
610 if(ANALYZE_TEST>5) printf("invalid combination of datatype and bitpix (%d, %d)\n",
611 h->dime.datatype, h->dime.bitpix);
612 free(mdata); return(5);
613 }
614 for(long long i=0; i<pxlNr; i++, mptr+=8, fptr++) {
615 memcpy(&d, mptr, 8); *fptr=f*d;
616 }
617 break;
618 default:
619 if(ANALYZE_TEST>5) printf("unsupported anahdr.dime.datatype := %d\n", h->dime.datatype);
620 free(mdata); return(5);
621 }
622
623 free(mdata);
624 if(ANALYZE_TEST>1) printf("anaReadImagedata() succeeded\n");
625 return(0);
626}
627/*****************************************************************************/
628
629/*****************************************************************************/
636 int ret;
637 char *cptr;
638
639 /* Is there an environment variable name for flipping? */
640 cptr=getenv("ANALYZE_FLIP");
641 if(cptr==NULL) cptr=getenv("ANALYZE_FLIPPING");
642 if(cptr==NULL) cptr=getenv("analyze_flip");
643 if(cptr==NULL) cptr=getenv("analyze_flipping");
644 if(cptr==NULL) {
645 if(ANALYZE_TEST>1) printf("ANALYZE_FLIP = not defined\n");
646 ret=ANALYZE_FLIP_DEFAULT; /* if not, then use default value */
647 } else {
648 if(ANALYZE_TEST>1) printf("ANALYZE_FLIP = '%s'\n", cptr);
649 if(*cptr=='y' || *cptr=='Y' || *cptr=='1') ret=1;
650 else if(*cptr=='n' || *cptr=='N' || *cptr=='0') ret=0;
651 else ret=ANALYZE_FLIP_DEFAULT;
652 }
653 if(ANALYZE_TEST) printf("anaFlipping()=%d\n", ret);
654 return(ret);
655}
656/*****************************************************************************/
657
658/*****************************************************************************/
668 const char *dbname
669) {
670 char datfile[FILENAME_MAX], hdrfile[FILENAME_MAX], siffile[FILENAME_MAX];
671
672 if(ANALYZE_TEST) printf("anaRemove(%s)\n", dbname);
673 if(anaDatabaseExists(dbname, hdrfile, datfile, siffile)==0) return 0;
674 if(ANALYZE_TEST>2) printf(" removing %s and %s\n", hdrfile, datfile);
675 if(remove(hdrfile)!=0) return 1;
676 if(remove(datfile)!=0) return 2;
677 return 0;
678}
679/*****************************************************************************/
680
681/*****************************************************************************/
689 char *fname
690) {
691 char *cptr;
692 cptr=strrchr(fname, '.'); if(cptr==NULL) return;
693 if(strcasecmp(cptr, ".")==0 || strcasecmp(cptr, ".img")==0 ||
694 strcasecmp(cptr, ".hdr")==0 || strcasecmp(cptr, ".sif")==0)
695 *cptr=(char)0;
696}
697/*****************************************************************************/
698
699/*****************************************************************************/
707 const char *dbname,
710 char *hdrfile,
713 char *imgfile,
716 char *siffile
717) {
718 char temp[FILENAME_MAX], database[FILENAME_MAX];
719 int checked=0;
720
721 if(ANALYZE_TEST)
722 printf("\nanaDatabaseExists(%s, *hdrfile, *imgfile, *siffile)\n", dbname);
723
724 /* Check the input */
725 if(hdrfile!=NULL) strcpy(hdrfile, "");
726 if(imgfile!=NULL) strcpy(imgfile, "");
727 if(siffile!=NULL) strcpy(siffile, "");
728 if(dbname==NULL || strlen(dbname)==0) return(0);
729
730 strcpy(database, dbname);
731 while(1) {
732 /* Header file? */
733 strcpy(temp, database); strcat(temp, ".hdr");
734 if(access(temp, 0) != -1) {
735 /* Also image file? */
736 strcpy(temp, database); strcat(temp, ".img");
737 if(access(temp, 0) != -1) {
738 if(hdrfile!=NULL) sprintf(hdrfile, "%s.hdr", database);
739 if(imgfile!=NULL) sprintf(imgfile, "%s.img", database);
740 /* Even SIF? */
741 if(anaMakeSIFName(database, temp)==0) { /* yes! */
742 if(siffile!=NULL) strcpy(siffile, temp);
743 return(2);
744 }
745 /* Image and header files did exist anyway */
746 return(1);
747 }
748 }
749 if(checked==1) break;
750 /* Try to remove extension */
751 anaRemoveFNameExtension(database);
752 checked=1;
753 } /* try again */
754 return(0);
755}
756/*****************************************************************************/
757
758/*****************************************************************************/
765 const char *dbname,
767 char *siffile
768) {
769 if(dbname==NULL || siffile==NULL) return(1);
770 sprintf(siffile, "%s.sif", dbname); if(access(siffile, 0) != -1) return(0);
771 sprintf(siffile, "%s.SIF", dbname); if(access(siffile, 0) != -1) return(0);
772 sprintf(siffile, "%s.img.sif", dbname); if(access(siffile, 0) != -1) return(0);
773 sprintf(siffile, "%s.IMG.SIF", dbname); if(access(siffile, 0) != -1) return(0);
774 sprintf(siffile, "%s.sif", dbname); return(2);
775}
776/*****************************************************************************/
777
778/*****************************************************************************/
785 ANALYZE_DSR *h,
787 char *field,
789 char *value
790) {
791 int ii;
792 short int si;
793 float f;
794
795 si=atoi(value); ii=atoi(value); f=atof(value);
796 /* Header keys */
797 if(strcmp(field, "header_key.sizeof_hdr")==0 || strcmp(field, "sizeof_hdr")==0) {
798 h->hk.sizeof_hdr=ii;
799 } else if(strcmp(field, "header_key.data_type")==0 || strcmp(field, "data_type")==0) {
800 strlcpy(h->hk.data_type, value, 10);
801 } else if(strcmp(field, "header_key.db_name")==0 || strcmp(field, "db_name")==0) {
802 strlcpy(h->hk.db_name, value, 18);
803 } else if(strcmp(field, "header_key.extents")==0 || strcmp(field, "extents")==0) {
804 h->hk.extents=ii;
805 } else if(strcmp(field, "header_key.session_error")==0 || strcmp(field, "session_error")==0) {
806 h->hk.session_error=si;
807 } else if(strcmp(field, "header_key.regular")==0 || strcmp(field, "regular")==0) {
808 h->hk.regular=value[0];
809 } else if(strcmp(field, "header_key.hkey_un0")==0 || strcmp(field, "hkey_un0")==0) {
810 h->hk.hkey_un0=value[0];
811 /* Header imgdim */
812 } else if(strcmp(field, "header_image_dimension.dim")==0 || strcmp(field, "dim")==0) {
813 sscanf(value, "%hd %hd %hd %hd %hd %hd %hd %hd",
814 h->dime.dim+0, h->dime.dim+1, h->dime.dim+2, h->dime.dim+3,
815 h->dime.dim+4, h->dime.dim+5, h->dime.dim+6, h->dime.dim+7
816 );
817 } else if(strcmp(field, "header_image_dimension.unused8")==0 || strcmp(field, "unused8")==0) {
818 h->dime.unused8=si;
819 } else if(strcmp(field, "header_image_dimension.unused9")==0 || strcmp(field, "unused9")==0) {
820 h->dime.unused9=si;
821 } else if(strcmp(field, "header_image_dimension.unused10")==0 || strcmp(field, "unused10")==0) {
822 h->dime.unused10=si;
823 } else if(strcmp(field, "header_image_dimension.unused11")==0 || strcmp(field, "unused11")==0) {
824 h->dime.unused11=si;
825 } else if(strcmp(field, "header_image_dimension.unused12")==0 || strcmp(field, "unused12")==0) {
826 h->dime.unused12=si;
827 } else if(strcmp(field, "header_image_dimension.unused13")==0 || strcmp(field, "unused13")==0) {
828 h->dime.unused13=si;
829 } else if(strcmp(field, "header_image_dimension.unused14")==0 || strcmp(field, "unused14")==0) {
830 h->dime.unused14=si;
831 } else if(strcmp(field, "header_image_dimension.datatype")==0 || strcmp(field, "datatype")==0) {
832 h->dime.datatype=si;
833 } else if(strcmp(field, "header_image_dimension.bitpix")==0 || strcmp(field, "bitpix")==0) {
834 h->dime.bitpix=si;
835 } else if(strcmp(field, "header_image_dimension.dim_un0")==0 || strcmp(field, "dim_un0")==0) {
836 h->dime.dim_un0=si;
837 } else if(strcmp(field, "header_image_dimension.pixdim")==0 || strcmp(field, "pixdim")==0) {
838 sscanf(value, "%f %f %f %f %f %f %f %f",
839 h->dime.pixdim+0, h->dime.pixdim+1, h->dime.pixdim+2, h->dime.pixdim+3,
840 h->dime.pixdim+4, h->dime.pixdim+5, h->dime.pixdim+6, h->dime.pixdim+7
841 );
842 } else if(strcmp(field, "header_image_dimension.vox_offset")==0 || strcmp(field, "vox_offset")==0) {
843 h->dime.vox_offset=f;
844 } else if(strcmp(field, "header_image_dimension.funused1")==0 || strcmp(field, "funused1")==0) {
845 h->dime.funused1=f;
846 } else if(strcmp(field, "header_image_dimension.funused2")==0 || strcmp(field, "funused2")==0) {
847 h->dime.funused2=f;
848 } else if(strcmp(field, "header_image_dimension.funused3")==0 || strcmp(field, "funused3")==0) {
849 h->dime.funused3=f;
850 } else if(strcmp(field, "header_image_dimension.cal_max")==0 || strcmp(field, "cal_max")==0) {
851 h->dime.cal_max=f;
852 } else if(strcmp(field, "header_image_dimension.cal_min")==0 || strcmp(field, "cal_min")==0) {
853 h->dime.cal_min=f;
854 } else if(strcmp(field, "header_image_dimension.compressed")==0 || strcmp(field, "compressed")==0) {
855 h->dime.compressed=f;
856 } else if(strcmp(field, "header_image_dimension.verified")==0 || strcmp(field, "verified")==0) {
857 h->dime.verified=f;
858 } else if(strcmp(field, "header_image_dimension.glmax")==0 || strcmp(field, "glmax")==0) {
859 h->dime.glmax=ii;
860 } else if(strcmp(field, "header_image_dimension.glmin")==0 || strcmp(field, "glmin")==0) {
861 h->dime.glmin=ii;
862 /* Header history */
863 } else if(strcmp(field, "header_data_history.descrip")==0 || strcmp(field, "descrip")==0) {
864 strlcpy(h->hist.descrip, value, 80);
865 } else if(strcmp(field, "header_data_history.aux_file")==0 || strcmp(field, "aux_file")==0) {
866 strlcpy(h->hist.aux_file, value, 24);
867 } else if(strcmp(field, "header_data_history.orient")==0 || strcmp(field, "orient")==0) {
868 h->hist.orient=value[0];
869 } else if(strcmp(field, "header_data_history.originator")==0 || strcmp(field, "originator")==0) {
870 strlcpy(h->hist.originator, value, 10);
871 } else if(strcmp(field, "header_data_history.generated")==0 || strcmp(field, "generated")==0) {
872 strlcpy(h->hist.generated, value, 10);
873 } else if(strcmp(field, "header_data_history.scannum")==0 || strcmp(field, "scannum")==0) {
874 strlcpy(h->hist.scannum, value, 10);
875 } else if(strcmp(field, "header_data_history.patient_id")==0 || strcmp(field, "patient_id")==0) {
876 strlcpy(h->hist.patient_id, value, 10);
877 } else if(strcmp(field, "header_data_history.exp_date")==0 || strcmp(field, "exp_date")==0) {
878 strlcpy(h->hist.exp_date, value, 10);
879 } else if(strcmp(field, "header_data_history.exp_time")==0 || strcmp(field, "exp_time")==0) {
880 strlcpy(h->hist.exp_time, value, 10);
881 } else if(strcmp(field, "header_data_history.hist_un0")==0 || strcmp(field, "hist_un0")==0) {
882 memcpy(h->hist.hist_un0, value, 3);
883 } else if(strcmp(field, "header_data_history.views")==0 || strcmp(field, "views")==0) {
884 h->hist.views=ii;
885 } else if(strcmp(field, "header_data_history.vols_added")==0 || strcmp(field, "vols_added")==0) {
886 h->hist.vols_added=ii;
887 } else if(strcmp(field, "header_data_history.start_field")==0 || strcmp(field, "start_field")==0) {
888 h->hist.start_field=ii;
889 } else if(strcmp(field, "header_data_history.field_skip")==0 || strcmp(field, "field_skip")==0) {
890 h->hist.field_skip=ii;
891 } else if(strcmp(field, "header_data_history.omax")==0 || strcmp(field, "omax")==0) {
892 h->hist.omax=ii;
893 } else if(strcmp(field, "header_data_history.omin")==0 || strcmp(field, "omin")==0) {
894 h->hist.omin=ii;
895 } else if(strcmp(field, "header_data_history.smax")==0 || strcmp(field, "smax")==0) {
896 h->hist.smax=ii;
897 } else if(strcmp(field, "header_data_history.smin")==0 || strcmp(field, "smin")==0) {
898 h->hist.smin=ii;
899 } else
900 return(1);
901
902 return(0);
903}
904/*****************************************************************************/
905
906/*****************************************************************************/
int anaMakeSIFName(const char *dbname, char *siffile)
Definition analyze.c:763
int anaEditHeader(ANALYZE_DSR *h, char *field, char *value)
Definition analyze.c:783
int anaWriteHeader(char *filename, ANALYZE_DSR *h)
Definition analyze.c:282
int anaExistsNew(const char *filename, char *hdrfile, char *imgfile, char *siffile)
Definition analyze.c:45
int anaFlipping()
Definition analyze.c:635
int anaPrintHeader(ANALYZE_DSR *h, FILE *fp)
Definition analyze.c:380
int anaDatabaseExists(const char *dbname, char *hdrfile, char *imgfile, char *siffile)
Definition analyze.c:704
void anaRemoveFNameExtension(char *fname)
Definition analyze.c:687
int ANALYZE_TEST
Definition analyze.c:8
int anaExists(const char *dbname)
Definition analyze.c:20
int anaReadHeader(char *filename, ANALYZE_DSR *h)
Definition analyze.c:131
int anaReadImagedata(FILE *fp, ANALYZE_DSR *h, int frame, float *data)
Definition analyze.c:454
int anaRemove(const char *dbname)
Definition analyze.c:666
Header file for libtpcimgio.
#define ANALYZE_DT_SIGNED_INT
Definition libtpcimgio.h:57
#define ANALYZE_DT_COMPLEX
Definition libtpcimgio.h:61
#define ANALYZE_DT_SIGNED_SHORT
Definition libtpcimgio.h:55
#define ANALYZE_DT_BINARY
Definition libtpcimgio.h:51
#define ANALYZE_DT_RGB
Definition libtpcimgio.h:65
#define ANALYZE_DT_UNSIGNED_CHAR
Definition libtpcimgio.h:53
#define ANALYZE_DT_DOUBLE
Definition libtpcimgio.h:63
#define ANALYZE_DT_FLOAT
Definition libtpcimgio.h:59
void swabip(void *buf, long long int size)
Definition swap.c:72
void swawbip(void *buf, long long int size)
Definition swap.c:93
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
int little_endian()
Definition swap.c:14
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition strext.c:206
ANALYZE_HEADER_HISTORY hist
ANALYZE_HEADER_KEY hk
ANALYZE_HEADER_IMGDIM dime
short int session_error
Definition libtpcimgio.h:84