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