analyze.c
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  Copyright (c) 2003-2008 by Turku PET Centre
4 
5  Library: analyze.c
6  Description: Procedures for reading and writing Analyze 7.5 images.
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  See the GNU Lesser General Public License for more details:
17  http://www.gnu.org/copyleft/lesser.html
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with this library/program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23  Turku PET Centre, Turku, Finland, http://www.turkupetcentre.fi/
24 
25  Modification history:
26  2003-10-05 Vesa Oikonen
27  First created.
28  2003-12-05 VO
29  Included function anaFlipping().
30  2003-12-10 VO
31  Setting of maximum nr of characters for string printing in
32  anaPrintHeader().
33  2004-02-05 VO
34  Change in function information, no change in compiled code.
35  2004-09-17 VO
36  Doxygen style comments.
37  2007-02-27 VO
38  Included functions anaRemove(), anaRemoveFNameExtension(),
39  anaDatabaseExists() and anaMakeSIFName().
40  2007-17-07 Harri Merisaari
41  modified anaRemoveFNameExtension(char *fname) for ANSI
42  compatibility build option
43  2007-09-11 VO
44  Corrected a bug in anaRemoveFNameExtension().
45  2008-10-03 VO
46  AnaReadImageData() accepts ANALYZE_DT_FLOAT and ANALYZE_DT_SIGNED_INT
47  with both 16 and 32 bit pixel values (previously only 16 bits).
48  Function might need to be rewritten.
49  2008-10-09 VO
50  AnaReadImageData() accepts ANALYZE_DT_COMPLEX, since PVELab writes pixel
51  values in this (32/32) format.
52 
53 
54 ******************************************************************************/
55 #include <stdio.h>
56 #include <string.h>
57 #include <math.h>
58 #include <stdlib.h>
59 #include <time.h>
60 #include <unistd.h>
61 /*****************************************************************************/
62 #include "swap.h"
63 #include "substitutions.h"
64 /*****************************************************************************/
65 #include "include/analyze.h"
66 /*****************************************************************************/
67 
68 /*****************************************************************************/
75 int anaExists(const char *dbname) {
76  char temp[FILENAME_MAX];
77 
78  if(dbname==NULL || strlen(dbname)==0) return(0);
79  /* Header file? */
80  strcpy(temp, dbname); strcat(temp, ".hdr");
81  if(access(temp, 0) == -1) return(0);
82  /* Image data? */
83  strcpy(temp, dbname); strcat(temp, ".img");
84  if(access(temp, 0) == -1) return(0);
85  /* SIF? */
86  strcat(temp, ".sif"); if(access(temp, 0) != -1) return(2);
87  strcpy(temp, dbname); strcat(temp, ".sif");
88  if(access(temp, 0) != -1) return(2);
89  return(1);
90 }
91 /*****************************************************************************/
92 
93 /*****************************************************************************/
102 int anaReadHeader(char *filename, ANALYZE_DSR *h) {
103  unsigned char buf1[ANALYZE_HEADER_KEY_SIZE];
104  unsigned char buf2[ANALYZE_HEADER_IMGDIM_SIZE];
105  unsigned char buf3[ANALYZE_HEADER_HISTORY_SIZE];
106  int little; /* 1 if current platform is little endian (i386), else 0 */
107  FILE *fp;
108  int ret, nr=0, s1, s2, same_order;
109 
110  if(ANALYZE_TEST) printf("anaReadHeader(%s, *dsr)\n", filename);
111 
112  /* Check arguments */
113  if(strlen(filename)<1 || h==NULL) return(1);
114  little=little_endian();
115  /* Open file */
116  fp=fopen(filename, "rb"); if(fp==NULL) return(2);
117  /* Get file size */
118  nr=0; while((ret=fgetc(fp))!=EOF) nr++; rewind(fp);
119  if(nr<1) {fclose(fp); return(3);}
120  /* Read Analyze header key */
121  if(fread(buf1, ANALYZE_HEADER_KEY_SIZE, 1, fp)<1) return(3);
122  /* Read Analyze header image dimension */
123  if(fread(buf2, ANALYZE_HEADER_IMGDIM_SIZE, 1, fp)<1) return(3);
124  /* Read Analyze header image data history */
125  memset(buf3, 0, sizeof(ANALYZE_HEADER_HISTORY));
126  ret=fread(buf3, ANALYZE_HEADER_HISTORY_SIZE, 1, fp);
127  if(ANALYZE_TEST>1 && ret) printf(" complete data_history not found.\n");
128  /* Close file */
129  fclose(fp);
130  /* Compare file size from header contents to the calculated value */
131  /* to determine whether Analyze file is in little or big endian */
132  memcpy(&s1, buf1+0, 4); s2=s1; swawbip(&s2, 4);
133  if(abs(s1-nr)<abs(s2-nr)) same_order=1; else same_order=0;
134  if(ANALYZE_TEST>1) printf("same byte order: %d (s1=%d s2=%d nr=%d)\n",
135  same_order, s1, s2, nr);
136  if(same_order) h->little=little;
137  else {if(little) h->little=0; else h->little=1;}
138 
139  /* Set key header structure contents */
140  if(!same_order) swawbip(buf1+0, 4); memcpy(&h->hk.sizeof_hdr, buf1+0, 4);
141  memcpy(h->hk.data_type, buf1+4, 10);
142  memcpy(h->hk.db_name, buf1+14, 18);
143  if(!same_order) swawbip(buf1+32, 4); memcpy(&h->hk.extents, buf1+32, 4);
144  if(!same_order) swabip(buf1+36, 2); memcpy(&h->hk.session_error, buf1+36, 2);
145  memcpy(&h->hk.regular, buf1+38, 1);
146  memcpy(&h->hk.hkey_un0, buf1+39, 1);
147 
148  /* Set image dimension header structure contents */
149  if(!same_order) swabip(buf2+0, 16); memcpy(h->dime.dim, buf2+0, 16);
150  if(!same_order) swabip(buf2+16, 2); memcpy(&h->dime.unused8, buf2+16, 2);
151  if(!same_order) swabip(buf2+18, 2); memcpy(&h->dime.unused9, buf2+18, 2);
152  if(!same_order) swabip(buf2+20, 2); memcpy(&h->dime.unused10, buf2+20, 2);
153  if(!same_order) swabip(buf2+22, 2); memcpy(&h->dime.unused11, buf2+22, 2);
154  if(!same_order) swabip(buf2+24, 2); memcpy(&h->dime.unused12, buf2+24, 2);
155  if(!same_order) swabip(buf2+26, 2); memcpy(&h->dime.unused13, buf2+26, 2);
156  if(!same_order) swabip(buf2+28, 2); memcpy(&h->dime.unused14, buf2+28, 2);
157  if(!same_order) swabip(buf2+30, 2); memcpy(&h->dime.datatype, buf2+30, 2);
158  if(!same_order) swabip(buf2+32, 2); memcpy(&h->dime.bitpix, buf2+32, 2);
159  if(!same_order) swabip(buf2+34, 2); memcpy(&h->dime.dim_un0, buf2+34, 2);
160  if(!same_order) swawbip(buf2+36, 32); memcpy(h->dime.pixdim, buf2+36, 32);
161  if(!same_order) swawbip(buf2+68, 4); memcpy(&h->dime.vox_offset, buf2+68, 4);
162  if(!same_order) swawbip(buf2+72, 4); memcpy(&h->dime.funused1, buf2+72, 4);
163  if(!same_order) swawbip(buf2+76, 4); memcpy(&h->dime.funused2, buf2+76, 4);
164  if(!same_order) swawbip(buf2+80, 4); memcpy(&h->dime.funused3, buf2+80, 4);
165  if(!same_order) swawbip(buf2+84, 4); memcpy(&h->dime.cal_max, buf2+84, 4);
166  if(!same_order) swawbip(buf2+88, 4); memcpy(&h->dime.cal_min, buf2+88, 4);
167  if(!same_order) swawbip(buf2+92, 4); memcpy(&h->dime.compressed, buf2+92, 4);
168  if(!same_order) swawbip(buf2+96, 4); memcpy(&h->dime.verified, buf2+96, 4);
169  if(!same_order) swawbip(buf2+100, 4); memcpy(&h->dime.glmax, buf2+100, 4);
170  if(!same_order) swawbip(buf2+104, 4); memcpy(&h->dime.glmin, buf2+104, 4);
171 
172  /* Set data history header structure contents */
173  memcpy(h->hist.descrip, buf3+0, 80);
174  memcpy(h->hist.aux_file, buf3+80, 24);
175  memcpy(&h->hist.orient, buf3+104, 1);
176  memcpy(h->hist.originator, buf3+105, 10);
177  memcpy(h->hist.generated, buf3+115, 10);
178  memcpy(h->hist.scannum, buf3+125, 10);
179  memcpy(h->hist.patient_id, buf3+135, 10);
180  memcpy(h->hist.exp_date, buf3+145, 10);
181  memcpy(h->hist.exp_time, buf3+155, 10);
182  memcpy(h->hist.hist_un0, buf3+165, 3);
183  if(!same_order) swawbip(buf3+168, 4); memcpy(&h->hist.views, buf3+168, 4);
184  if(!same_order) swawbip(buf3+172, 4); memcpy(&h->hist.vols_added, buf3+172, 4);
185  if(!same_order) swawbip(buf3+176, 4); memcpy(&h->hist.start_field, buf3+176, 4);
186  if(!same_order) swawbip(buf3+180, 4); memcpy(&h->hist.field_skip, buf3+180, 4);
187  if(!same_order) swawbip(buf3+184, 4); memcpy(&h->hist.omax, buf3+184, 4);
188  if(!same_order) swawbip(buf3+188, 4); memcpy(&h->hist.omin, buf3+188, 4);
189  if(!same_order) swawbip(buf3+192, 4); memcpy(&h->hist.smax, buf3+192, 4);
190  if(!same_order) swawbip(buf3+196, 4); memcpy(&h->hist.smin, buf3+196, 4);
191 
192  /* Check header contents */
193  if(h->hk.extents!=16384 && h->hk.extents!=0) return(11);
194  if(h->hk.regular!='r') return(12);
195 
196  return(0);
197 }
198 /*****************************************************************************/
199 
200 /*****************************************************************************/
209 int anaWriteHeader(char *filename, ANALYZE_DSR *h) {
210  unsigned char buf1[ANALYZE_HEADER_KEY_SIZE];
211  unsigned char buf2[ANALYZE_HEADER_IMGDIM_SIZE];
212  unsigned char buf3[ANALYZE_HEADER_HISTORY_SIZE];
213  FILE *fp;
214  int same_order, little;
215 
216 
217  if(ANALYZE_TEST) printf("anaWriteHeader(%s, *dsr)\n", filename);
218 
219  /* Check arguments */
220  if(strlen(filename)<1 || h==NULL) return(1);
221  little=little_endian();
222  if(little==h->little) same_order=1; else same_order=0;
223 
224  /* Copy header contents into buffers */
225  /* Header key */
226  memset(buf1, 0, sizeof(ANALYZE_HEADER_KEY_SIZE));
227  memcpy(buf1+0, &h->hk.sizeof_hdr, 4); if(!same_order) swawbip(buf1+0, 4);
228  memcpy(buf1+4, &h->hk.data_type, 10);
229  memcpy(buf1+14, &h->hk.db_name, 18);
230  memcpy(buf1+32, &h->hk.extents, 4); if(!same_order) swawbip(buf1+32, 4);
231  memcpy(buf1+36, &h->hk.session_error, 2); if(!same_order) swabip(buf1+36, 2);
232  memcpy(buf1+38, &h->hk.regular, 1);
233  memcpy(buf1+39, &h->hk.hkey_un0, 1);
234  /* Image dimension */
235  memset(buf2, 0, sizeof(ANALYZE_HEADER_IMGDIM_SIZE));
236  memcpy(buf2+0, h->dime.dim, 16); if(!same_order) swabip(buf2+0, 16);
237  memcpy(buf2+16, &h->dime.unused8, 2); if(!same_order) swabip(buf2+16, 2);
238  memcpy(buf2+18, &h->dime.unused9, 2); if(!same_order) swabip(buf2+18, 2);
239  memcpy(buf2+20, &h->dime.unused10, 2); if(!same_order) swabip(buf2+20, 2);
240  memcpy(buf2+22, &h->dime.unused11, 2); if(!same_order) swabip(buf2+22, 2);
241  memcpy(buf2+24, &h->dime.unused12, 2); if(!same_order) swabip(buf2+24, 2);
242  memcpy(buf2+26, &h->dime.unused13, 2); if(!same_order) swabip(buf2+26, 2);
243  memcpy(buf2+28, &h->dime.unused14, 2); if(!same_order) swabip(buf2+28, 2);
244  memcpy(buf2+30, &h->dime.datatype, 2); if(!same_order) swabip(buf2+30, 2);
245  memcpy(buf2+32, &h->dime.bitpix, 2); if(!same_order) swabip(buf2+32, 2);
246  memcpy(buf2+34, &h->dime.dim_un0, 2); if(!same_order) swabip(buf2+34, 2);
247  memcpy(buf2+36, h->dime.pixdim, 32); if(!same_order) swawbip(buf2+36, 32);
248  memcpy(buf2+68, &h->dime.vox_offset, 4); if(!same_order) swawbip(buf2+68, 4);
249  memcpy(buf2+72, &h->dime.funused1, 4); if(!same_order) swawbip(buf2+72, 4);
250  memcpy(buf2+76, &h->dime.funused2, 4); if(!same_order) swawbip(buf2+76, 4);
251  memcpy(buf2+80, &h->dime.funused3, 4); if(!same_order) swawbip(buf2+80, 4);
252  memcpy(buf2+84, &h->dime.cal_max, 4); if(!same_order) swawbip(buf2+84, 4);
253  memcpy(buf2+88, &h->dime.cal_min, 4); if(!same_order) swawbip(buf2+88, 4);
254  memcpy(buf2+92, &h->dime.compressed, 4); if(!same_order) swawbip(buf2+92, 4);
255  memcpy(buf2+96, &h->dime.verified, 4); if(!same_order) swawbip(buf2+96, 4);
256  memcpy(buf2+100, &h->dime.glmax, 4); if(!same_order) swawbip(buf2+100, 4);
257  memcpy(buf2+104, &h->dime.glmin, 4); if(!same_order) swawbip(buf2+104, 4);
258  /* Data history */
259  memset(buf3, 0, sizeof(ANALYZE_HEADER_HISTORY_SIZE));
260  memcpy(buf3+0, &h->hist.descrip, 80);
261  memcpy(buf3+80, &h->hist.aux_file, 24);
262  memcpy(buf3+104, &h->hist.orient, 1);
263  memcpy(buf3+105, &h->hist.originator, 10);
264  memcpy(buf3+115, &h->hist.generated, 10);
265  memcpy(buf3+125, &h->hist.scannum, 10);
266  memcpy(buf3+135, &h->hist.patient_id, 10);
267  memcpy(buf3+145, &h->hist.exp_date, 10);
268  memcpy(buf3+155, &h->hist.exp_time, 10);
269  memcpy(buf3+165, &h->hist.hist_un0, 3);
270  memcpy(buf3+168, &h->hist.views, 4); if(!same_order) swawbip(buf3+168, 4);
271  memcpy(buf3+172, &h->hist.vols_added, 4); if(!same_order) swawbip(buf3+172, 4);
272  memcpy(buf3+176, &h->hist.start_field, 4); if(!same_order) swawbip(buf3+176, 4);
273  memcpy(buf3+180, &h->hist.field_skip, 4); if(!same_order) swawbip(buf3+180, 4);
274  memcpy(buf3+184, &h->hist.omax, 4); if(!same_order) swawbip(buf3+184, 4);
275  memcpy(buf3+188, &h->hist.omin, 4); if(!same_order) swawbip(buf3+188, 4);
276  memcpy(buf3+192, &h->hist.smax, 4); if(!same_order) swawbip(buf3+192, 4);
277  memcpy(buf3+196, &h->hist.smin, 4); if(!same_order) swawbip(buf3+196, 4);
278 
279  /* Open header file for write */
280  fp=fopen(filename, "wb"); if(fp==NULL) return(2);
281  /* Write header key */
282  if(fwrite(buf1, 1, ANALYZE_HEADER_KEY_SIZE, fp) != ANALYZE_HEADER_KEY_SIZE) {
283  fclose(fp); return(3);
284  }
285  /* Write image dimension */
286  if(fwrite(buf2, 1, ANALYZE_HEADER_IMGDIM_SIZE, fp) != ANALYZE_HEADER_IMGDIM_SIZE) {
287  fclose(fp); return(4);
288  }
289  /* Write data history */
290  if(fwrite(buf3, 1, ANALYZE_HEADER_HISTORY_SIZE, fp) != ANALYZE_HEADER_HISTORY_SIZE) {
291  fclose(fp); return(5);
292  }
293  fclose(fp);
294 
295  return(0);
296 }
297 /*****************************************************************************/
298 
299 /*****************************************************************************/
307 int anaPrintHeader(ANALYZE_DSR *h, FILE *fp) {
308  int i;
309 
310  if(fp==NULL || h==NULL) return(1);
311  fprintf(fp, "original_byte_order := %d (1=little, 0=big)\n", h->little);
312  /* Key */
313  fprintf(fp, "header_key.sizeof_hdr := %d\n", h->hk.sizeof_hdr);
314  fprintf(fp, "header_key.data_type := %.10s\n", h->hk.data_type);
315  fprintf(fp, "header_key.db_name := %.18s\n", h->hk.db_name);
316  fprintf(fp, "header_key.extents := %d\n", h->hk.extents);
317  fprintf(fp, "header_key.session_error := %d\n", h->hk.session_error);
318  fprintf(fp, "header_key.regular := %d (%c)\n", (int)h->hk.regular, h->hk.regular);
319  fprintf(fp, "header_key.hkey_un0 := %d\n", (int)h->hk.hkey_un0);
320  /* Image dimension */
321  fprintf(fp, "header_image_dimension.dim :=");
322  for(i=0; i<8; i++) fprintf(fp, " %d", h->dime.dim[i]);
323  fprintf(fp, "\n");
324  fprintf(fp, "header_image_dimension.unused8 := %d\n", h->dime.unused8);
325  fprintf(fp, "header_image_dimension.unused9 := %d\n", h->dime.unused9);
326  fprintf(fp, "header_image_dimension.unused10 := %d\n", h->dime.unused10);
327  fprintf(fp, "header_image_dimension.unused11 := %d\n", h->dime.unused11);
328  fprintf(fp, "header_image_dimension.unused12 := %d\n", h->dime.unused12);
329  fprintf(fp, "header_image_dimension.unused13 := %d\n", h->dime.unused13);
330  fprintf(fp, "header_image_dimension.unused14 := %d\n", h->dime.unused14);
331  fprintf(fp, "header_image_dimension.datatype := %d\n", h->dime.datatype);
332  fprintf(fp, "header_image_dimension.bitpix := %d\n", h->dime.bitpix);
333  fprintf(fp, "header_image_dimension.dim_un0 := %d\n", h->dime.dim_un0);
334  fprintf(fp, "header_image_dimension.pixdim :=");
335  for(i=0; i<8; i++) fprintf(fp, " %g", h->dime.pixdim[i]);
336  fprintf(fp, "\n");
337  fprintf(fp, "header_image_dimension.vox_offset := %g\n", h->dime.vox_offset);
338  fprintf(fp, "header_image_dimension.funused1 := %g\n", h->dime.funused1);
339  fprintf(fp, "header_image_dimension.funused2 := %g\n", h->dime.funused2);
340  fprintf(fp, "header_image_dimension.funused3 := %g\n", h->dime.funused3);
341  fprintf(fp, "header_image_dimension.cal_max := %g\n", h->dime.cal_max);
342  fprintf(fp, "header_image_dimension.cal_min := %g\n", h->dime.cal_min);
343  fprintf(fp, "header_image_dimension.compressed := %g\n", h->dime.compressed);
344  fprintf(fp, "header_image_dimension.verified := %g\n", h->dime.verified);
345  fprintf(fp, "header_image_dimension.glmax := %d\n", h->dime.glmax);
346  fprintf(fp, "header_image_dimension.glmin := %d\n", h->dime.glmin);
347  /* Data history */
348  fprintf(fp, "header_data_history.descrip := %s.80\n", h->hist.descrip);
349  fprintf(fp, "header_data_history.aux_file := %.24s\n", h->hist.aux_file);
350  fprintf(fp, "header_data_history.orient := %d\n", (int)h->hist.orient);
351  fprintf(fp, "header_data_history.originator := %.10s\n", h->hist.originator);
352  fprintf(fp, "header_data_history.generated := %.10s\n", h->hist.generated);
353  fprintf(fp, "header_data_history.scannum := %.10s\n", h->hist.scannum);
354  fprintf(fp, "header_data_history.patient_id := %.10s\n", h->hist.patient_id);
355  fprintf(fp, "header_data_history.exp_date := %.10s\n", h->hist.exp_date);
356  fprintf(fp, "header_data_history.exp_time := %.10s\n", h->hist.exp_time);
357  fprintf(fp, "header_data_history.hist_un0 := %.3s\n", h->hist.hist_un0);
358  fprintf(fp, "header_data_history.views := %d\n", h->hist.views);
359  fprintf(fp, "header_data_history.vols_added := %d\n", h->hist.vols_added);
360  fprintf(fp, "header_data_history.start_field := %d\n", h->hist.start_field);
361  fprintf(fp, "header_data_history.field_skip := %d\n", h->hist.field_skip);
362  fprintf(fp, "header_data_history.omax := %d\n", h->hist.omax);
363  fprintf(fp, "header_data_history.omin := %d\n", h->hist.omin);
364  fprintf(fp, "header_data_history.smax := %d\n", h->hist.smax);
365  fprintf(fp, "header_data_history.smin := %d\n", h->hist.smin);
366 
367  return(0);
368 }
369 /*****************************************************************************/
370 
371 /*****************************************************************************/
381 int anaReadImagedata(FILE *fp, ANALYZE_DSR *h, int frame, float *data) {
382  int dimNr, dimx, dimy, dimz=1, dimt=1, pxlNr=0;
383  int i, n, little, start_pos, rawSize;
384  char *mdata, *mptr;
385  float f, *fptr;
386  short int *sptr;
387  int *iptr;
388  double d;
389 
390 
391  if(ANALYZE_TEST) printf("anaReadImagedata(fp, h, %d, data)\n", frame);
392 
393  /* Check the arguments */
394  if(frame<=0 || fp==NULL || h==NULL || data==NULL) return(1);
395 
396  /* Get the image dimensions from header */
397  dimNr=h->dime.dim[0]; if(dimNr<2) return(2);
398  dimx=h->dime.dim[1];
399  dimy=h->dime.dim[2];
400  if(dimNr>2) dimz=h->dime.dim[3];
401  if(dimNr>3) dimt=h->dime.dim[4]; if(frame>dimt) return(3);
402  pxlNr=dimx*dimy*dimz; if(pxlNr<1) return(4);
403 
404  /* Allocate memory for the binary data */
405  if(h->dime.bitpix<8) return(5); /* We don't support bit data */
406  rawSize=pxlNr*(h->dime.bitpix/8); if(rawSize<1) return(5);
407  if(ANALYZE_TEST>0) printf(" pxlNr=%d rawSize=%d\n", pxlNr, rawSize);
408  mdata=(char*)malloc(rawSize); if(mdata==NULL) return(11);
409 
410  /* Seek the start of current frame data */
411  start_pos=(frame-1)*rawSize;
412  n=(int)h->dime.vox_offset; if((n>0 && frame==1) || (n<0)) start_pos+=abs(n);
413  if(ANALYZE_TEST>2) printf("start_pos=%d\n", start_pos);
414  fseek(fp, start_pos, SEEK_SET);
415  if(ftell(fp)!=start_pos) {
416  if(ANALYZE_TEST>5) printf("could not move to start_pos\n");
417  free(mdata); return(7);
418  }
419 
420  /* Read the data */
421  mptr=mdata;
422  if((n=fread(mptr, rawSize, 1, fp)) < 1) {
423  if(ANALYZE_TEST>5)
424  printf("could read only %d bytes when request was %d\n", n, rawSize);
425  free(mdata); return(8);
426  }
427 
428  /* Convert byte order if necessary */
429  little=little_endian(); mptr=mdata;
430  if(little!=h->little) {
431  if(ANALYZE_TEST>0) printf("byte conversion\n");
432  switch(h->dime.bitpix) {
433  case 8: /* no conversion needed */ break;
434  case 16: swabip(mptr, rawSize); break;
435  case 32: swawbip(mptr, rawSize); break;
436  case 64: swawbip(mptr, rawSize); break;
437  default:
438  if(ANALYZE_TEST>5)
439  printf("unsupported anahdr.dime.bitpix := %d\n", h->dime.bitpix);
440  free(mdata); return(5);
441  }
442  }
443 
444  /* Get scale factor */
445  f=1.0;
446  if(h->dime.funused1>0.0) f*=h->dime.funused1;
447 
448  /* Copy data to float pixel values */
449  mptr=mdata; fptr=data;
450  switch(h->dime.datatype) {
452  if(h->dime.bitpix!=8) {
453  if(ANALYZE_TEST>5)
454  printf("invalid combination of datatype and bitpix (%d, %d)\n",
455  h->dime.datatype, h->dime.bitpix);
456  free(mdata); return(5);
457  }
458  for(i=0; i<pxlNr; i++, mptr++, fptr++) *fptr=f*(float)(*mptr);
459  break;
461  if(h->dime.bitpix!=16) {
462  if(ANALYZE_TEST>5)
463  printf("invalid combination of datatype and bitpix (%d, %d)\n",
464  h->dime.datatype, h->dime.bitpix);
465  free(mdata); return(5);
466  }
467  for(i=0; i<pxlNr; i++, mptr+=2, fptr++) {
468  sptr=(short int*)mptr; *fptr=f*(float)(*sptr);
469  }
470  break;
472  if(h->dime.bitpix!=16 && h->dime.bitpix!=32) {
473  if(ANALYZE_TEST>5)
474  printf("invalid combination of datatype and bitpix (%d, %d)\n",
475  h->dime.datatype, h->dime.bitpix);
476  free(mdata); return(5);
477  }
478  if(h->dime.bitpix==16) {
479  for(i=0; i<pxlNr; i++, mptr+=4, fptr++) {
480  iptr=(int*)mptr; *fptr=f*(float)(*iptr);
481  }
482  } else if(h->dime.bitpix==32) {
483  for(i=0; i<pxlNr; i++, mptr+=4, fptr++) {
484  iptr=(int*)mptr; *fptr=f*(float)(*iptr);
485  }
486  }
487  break;
488  case ANALYZE_DT_FLOAT:
489  if(h->dime.bitpix!=16 && h->dime.bitpix!=32) {
490  if(ANALYZE_TEST>5)
491  printf("invalid combination of datatype and bitpix (%d, %d)\n",
492  h->dime.datatype, h->dime.bitpix);
493  free(mdata); return(5);
494  }
495  if(h->dime.bitpix==16) {
496  memcpy(fptr, mptr, pxlNr*4);
497  for(i=0; i<pxlNr; i++, fptr++) *fptr*=f;
498  } else if(h->dime.bitpix==32) {
499  memcpy(fptr, mptr, pxlNr*4);
500  for(i=0; i<pxlNr; i++, fptr++) *fptr*=f;
501  }
502  break;
503  case ANALYZE_DT_COMPLEX:
504  if(h->dime.bitpix!=32) {
505  if(ANALYZE_TEST>5)
506  printf("invalid combination of datatype and bitpix (%d, %d)\n",
507  h->dime.datatype, h->dime.bitpix);
508  free(mdata); return(5);
509  }
510  if(h->dime.bitpix==32) {
511  memcpy(fptr, mptr, pxlNr*4);
512  for(i=0; i<pxlNr; i++, fptr++) *fptr*=f;
513  }
514  break;
515  case ANALYZE_DT_DOUBLE:
516  if(h->dime.bitpix!=32) {
517  if(ANALYZE_TEST>5)
518  printf("invalid combination of datatype and bitpix (%d, %d)\n",
519  h->dime.datatype, h->dime.bitpix);
520  free(mdata); return(5);
521  }
522  for(i=0; i<pxlNr; i++, mptr+=8, fptr++) {
523  memcpy(&d, mptr, 8); *fptr=f*d;
524  }
525  break;
526  default:
527  if(ANALYZE_TEST>5)
528  printf("unsupported anahdr.dime.datatype := %d\n", h->dime.datatype);
529  free(mdata); return(5);
530  }
531 
532  free(mdata);
533  if(ANALYZE_TEST>1) printf("anaReadImagedata() succeeded\n");
534  return(0);
535 }
536 /*****************************************************************************/
537 
538 /*****************************************************************************/
545 int anaFlipping() {
546  int ret;
547  char *cptr;
548 
549  /* Is there an environment variable name for flipping? */
550  cptr=getenv("ANALYZE_FLIP");
551  if(cptr==NULL) cptr=getenv("ANALYZE_FLIPPING");
552  if(cptr==NULL) cptr=getenv("analyze_flip");
553  if(cptr==NULL) cptr=getenv("analyze_flipping");
554  if(cptr==NULL) {
555  if(ANALYZE_TEST>1) printf("ANALYZE_FLIP = not defined\n");
556  ret=ANALYZE_FLIP_DEFAULT; /* if not, then use default value */
557  } else {
558  if(ANALYZE_TEST>1) printf("ANALYZE_FLIP = '%s'\n", cptr);
559  if(*cptr=='y' || *cptr=='Y' || *cptr=='1') ret=1;
560  else if(*cptr=='n' || *cptr=='N' || *cptr=='0') ret=0;
561  else ret=ANALYZE_FLIP_DEFAULT;
562  }
563  if(ANALYZE_TEST) printf("anaFlipping()=%d\n", ret);
564  return(ret);
565 }
566 /*****************************************************************************/
567 
568 /*****************************************************************************/
576 int anaRemove(const char *dbname) {
577  char datfile[FILENAME_MAX], hdrfile[FILENAME_MAX], siffile[FILENAME_MAX];
578 
579  if(ANALYZE_TEST) printf("anaRemove(%s)\n", dbname);
580  if(anaDatabaseExists(dbname, hdrfile, datfile, siffile)==0) return 0;
581  if(ANALYZE_TEST>2) printf(" removing %s and %s\n", hdrfile, datfile);
582  if(remove(hdrfile)!=0) return 1;
583  if(remove(datfile)!=0) return 2;
584  return 0;
585 }
586 /*****************************************************************************/
587 
588 /*****************************************************************************/
595 void anaRemoveFNameExtension(char *fname) {
596  char *cptr;
597  cptr=strrchr(fname, '.'); if(cptr==NULL) return;
598  if(strcasecmp(cptr, ".")==0 || strcasecmp(cptr, ".img")==0 ||
599  strcasecmp(cptr, ".hdr")==0 || strcasecmp(cptr, ".sif")==0)
600  *cptr=(char)0;
601 }
602 /*****************************************************************************/
603 
604 /*****************************************************************************/
619 int anaDatabaseExists(const char *dbname, char *hdrfile, char *imgfile, char *siffile) {
620  char temp[FILENAME_MAX], database[FILENAME_MAX];
621  int checked=0;
622 
623  if(ANALYZE_TEST)
624  printf("\nanaDatabaseExists(%s, *hdrfile, *imgfile, *siffile)\n", dbname);
625 
626  /* Check the input */
627  if(hdrfile!=NULL) strcpy(hdrfile, "");
628  if(imgfile!=NULL) strcpy(imgfile, "");
629  if(siffile!=NULL) strcpy(siffile, "");
630  if(dbname==NULL || strlen(dbname)==0) return(0);
631 
632  strcpy(database, dbname);
633  while(1) {
634  /* Header file? */
635  strcpy(temp, database); strcat(temp, ".hdr");
636  if(access(temp, 0) != -1) {
637  /* Also image file? */
638  strcpy(temp, database); strcat(temp, ".img");
639  if(access(temp, 0) != -1) {
640  if(hdrfile!=NULL) sprintf(hdrfile, "%s.hdr", database);
641  if(imgfile!=NULL) sprintf(imgfile, "%s.img", database);
642  /* Even SIF? */
643  if(anaMakeSIFName(database, temp)==0) { /* yes! */
644  if(siffile!=NULL) strcpy(siffile, temp); return(2);
645  }
646  /* Image and header files did exist anyway */
647  return(1);
648  }
649  }
650  if(checked==1) break;
651  /* Try to remove extension */
652  anaRemoveFNameExtension(database);
653  checked=1;
654  } /* try again */
655  return(0);
656 }
657 /*****************************************************************************/
658 
659 /*****************************************************************************/
667 int anaMakeSIFName(const char *dbname, char *siffile) {
668  if(dbname==NULL || siffile==NULL) return(1);
669  sprintf(siffile, "%s.sif", dbname); if(access(siffile, 0) != -1) return(0);
670  sprintf(siffile, "%s.SIF", dbname); if(access(siffile, 0) != -1) return(0);
671  sprintf(siffile, "%s.img.sif", dbname); if(access(siffile, 0) != -1) return(0);
672  sprintf(siffile, "%s.IMG.SIF", dbname); if(access(siffile, 0) != -1) return(0);
673  sprintf(siffile, "%s.sif", dbname); return(2);
674 }
675 /*****************************************************************************/
676 
677 /*****************************************************************************/
678