85 int ret, fi, pi, xi, yi;
86 float *fdata=NULL, *fptr;
88 char datfile[FILENAME_MAX], hdrfile[FILENAME_MAX], siffile[FILENAME_MAX];
90 int dimNr, dimx, dimy, dimz=1, dimt=1, pxlNr=0;
95 if(
IMG_TEST) printf(
"imgReadAnalyze(%s, *img)\n", dbname);
106 strcpy(datfile, dbname); cptr=strrchr(datfile,
'.');
107 if(cptr!=NULL && (strcmp(cptr,
".img")==0 || strcmp(cptr,
".hdr")==0)) {
108 *cptr=(char)0; strcpy(hdrfile, datfile);
111 strcat(datfile,
".img"); strcat(hdrfile,
".hdr");
117 strcpy(datfile, dbname); strcat(datfile,
".img");
118 strcpy(hdrfile, dbname); strcat(hdrfile,
".hdr");
132 if(
IMG_TEST) fprintf(stdout,
"reading image data %s\n", datfile);
140 if(dimNr>2) {dimz=dsr.
dime.
dim[3];
if(dimNr>3) dimt=dsr.
dime.
dim[4];}
141 pxlNr=dimx*dimy*dimz;
155 for(pi=0; pi<dimz; pi++) img->
planeNumber[pi]=pi+1;
158 if(strstr(dsr.
hist.
descrip,
"Decay corrected.")!=NULL)
160 else if(strstr(dsr.
hist.
descrip,
"No decay correction.")!=NULL)
166 fdata=malloc(pxlNr*
sizeof(
float));
170 for(fi=0; fi<dimt; fi++) {
177 for(pi=0; pi<img->
dimz; pi++)
178 for(yi=dimy-1; yi>=0; yi--)
179 for(xi=dimx-1; xi>=0; xi--)
180 img->
m[pi][yi][xi][fi]=*fptr++;
182 for(pi=dimz-1; pi>=0; pi--)
183 for(yi=dimy-1; yi>=0; yi--)
184 for(xi=dimx-1; xi>=0; xi--)
185 img->
m[pi][yi][xi][fi]=*fptr++;
193 strcpy(siffile, dbname); strcat(siffile,
".sif");
194 if(access(siffile, 0) == -1) {
195 strcpy(siffile, datfile); strcat(siffile,
".sif");
198 if(
IMG_TEST) printf(
"reading SIF file %s\n", siffile);
199 if(access(siffile, 0) == -1) {
200 if(
IMG_TEST) printf(
" No SIF file; therefore unknown frame times.\n");
209 if(st!=NULL) strftime(buf, 128,
"%Y-%m-%d %H:%M:%S", st);
210 else strcpy(buf,
"1900-01-01 00:00:00");
216 for(fi=0; fi<sif.
frameNr; fi++) {
221 for(fi=0; fi<sif.
frameNr; fi++) {
255 int ret, fi, pi, xi, yi, little;
258 char datfile[FILENAME_MAX], hdrfile[FILENAME_MAX];
262 short int *sdata, *sptr, smin, smax;
265 if(
IMG_TEST) printf(
"imgWriteAnalyze(%s, *img)\n", dbname);
274 strcpy(datfile, dbname); strcat(datfile,
".img");
275 strcpy(hdrfile, dbname); strcat(hdrfile,
".hdr");
288 cptr=strrchr(dbname,
'/');
if(cptr==NULL) cptr=strrchr(dbname,
'\\');
289 if(cptr!=NULL) cptr++;
if(cptr==NULL) cptr=dbname;
311 else strcpy(dsr.
hist.
descrip,
"No decay correction.");
326 if(
IMG_TEST) printf(
"scaling data to short ints\n");
331 if(g<1E-20) g=1.0;
else g=32767./g; dsr.
dime.
funused1=1.0/g;
332 if(
IMG_TEST) printf(
"min=%g max=%g scale_factor=%g\n",
337 sdata=malloc(pxlNr*
sizeof(
short int));
344 if((fp=fopen(datfile,
"wb")) == NULL) {
352 smin=smax=temp_roundf(g*img->
m[0][0][0][0]);
353 for(fi=0; fi<img->
dimt; fi++) {
356 for(pi=0; pi<img->
dimz; pi++)
357 for(yi=img->
dimy-1; yi>=0; yi--)
358 for(xi=img->
dimx-1; xi>=0; xi--) {
359 *sptr=temp_roundf(g*img->
m[pi][yi][xi][fi]);
360 if(*sptr>smax) smax=*sptr;
else if(*sptr<smin) smin=*sptr;
364 for(pi=img->
dimz-1; pi>=0; pi--)
365 for(yi=img->
dimy-1; yi>=0; yi--)
366 for(xi=img->
dimx-1; xi>=0; xi--) {
367 *sptr=temp_roundf(g*img->
m[pi][yi][xi][fi]);
368 if(*sptr>smax) smax=*sptr;
else if(*sptr<smin) smin=*sptr;
373 little=little_endian();
375 swabip(sdata, pxlNr*
sizeof(
short int));
377 if(fwrite(sdata, 2, pxlNr, fp) != pxlNr) {
379 free(sdata); fclose(fp);
387 if(
IMG_TEST) printf(
"smin=%d smax=%d\n", smin, smax);
415 char hdrfile[FILENAME_MAX], siffile[FILENAME_MAX];
421 if(
IMG_TEST) printf(
"\nimgReadAnalyzeHeader(%s, *img)\n", dbname);
436 if(
IMG_TEST>1) printf(
"anaReadHeader() return value := %d\n", ret);
482 int dimNr, dimx, dimy, dimz=1, dimt=1, pxlNr=0;
484 if(
IMG_TEST) printf(
"\nimgGetAnalyzeHeader(*img, *dsr)\n");
500 if(dimNr>2) {dimz=h->
dime.
dim[3];
if(dimNr>3) dimt=h->
dime.
dim[4];}
501 pxlNr=dimx*dimy*dimz;
517 if(strstr(h->
hist.
descrip,
"Decay corrected.")!=NULL)
519 else if(strstr(h->
hist.
descrip,
"No decay correction.")!=NULL)
547 if(
IMG_TEST) printf(
"\nimgSetAnalyzeHeader(*img, *dsr)\n");
564 cptr=strrchr(dbname,
'/');
if(cptr==NULL) cptr=strrchr(dbname,
'\\');
565 if(cptr!=NULL) cptr++;
if(cptr==NULL) cptr=(
char*)dbname;
587 else strcpy(dsr->
hist.
descrip,
"No decay correction.");
609 if(g<1E-20) g=1.0;
else g=32767./g; dsr->
dime.
funused1=1.0/g;
632 if(
IMG_TEST) printf(
"\nimgReadAnalyzeFirstFrame(%s, *img)\n", fname);
677 float *fdata=NULL, *fptr;
679 char datfile[FILENAME_MAX], hdrfile[FILENAME_MAX], siffile[FILENAME_MAX];
683 if(
IMG_TEST) printf(
"\nimgReadAnalyzeFrame(%s, %d, *img, %d)\n",
684 fname, frame_to_read, frame_index);
708 if(
IMG_TEST>2) fprintf(stdout,
"reading image data %s\n", datfile);
712 fdata=malloc(img->
dimx*img->
dimy*img->
dimz*
sizeof(
float));
725 for(pi=0; pi<img->
dimz; pi++)
726 for(yi=img->
dimy-1; yi>=0; yi--)
727 for(xi=img->
dimx-1; xi>=0; xi--)
728 img->
m[pi][yi][xi][frame_index]=*fptr++;
730 for(pi=img->
dimz-1; pi>=0; pi--)
731 for(yi=img->
dimy-1; yi>=0; yi--)
732 for(xi=img->
dimx-1; xi>=0; xi--)
733 img->
m[pi][yi][xi][frame_index]=*fptr++;
745 if(sif.
frameNr>=frame_to_read) {
746 img->
start[frame_index]=sif.
x1[frame_to_read-1];
747 img->
end[frame_index]=sif.
x2[frame_to_read-1];
748 img->
mid[frame_index]=0.5*(img->
start[frame_index]+img->
end[frame_index]);
783 int ret=0, pxlNr, zi, xi, yi, little;
785 short int *sdata=NULL, *sptr;
786 char datfile[FILENAME_MAX], hdrfile[FILENAME_MAX], siffile[FILENAME_MAX];
788 float scale_factor=1.0;
791 if(
IMG_TEST) printf(
"\nimgWriteAnalyzeFrame(%s, %d, *img, %d, %g, %g)\n",
792 dbname, frame_to_write, frame_index, fmin, fmax);
814 sprintf(hdrfile,
"%s.hdr", dbname);
815 sprintf(datfile,
"%s.img", dbname);
816 sprintf(siffile,
"%s.sif", dbname);
820 if(frame_to_write==0) frame_to_write=1;
821 dsr.
dime.
dim[4]=frame_to_write;
823 if(fabs(scale_factor)>1.0E-20) scale_factor=1.0/scale_factor;
830 if(access(datfile, 0) != -1)
remove(datfile);
849 if(frame_to_write==0) frame_to_write=dsr.
dime.
dim[4]+1;
850 if(dsr.
dime.
dim[4]<frame_to_write) {
852 dsr.
dime.
dim[4]=frame_to_write;
857 printf(
"frame_to_write := %d\n", frame_to_write);
858 printf(
"hdrfile := %s\n", hdrfile);
859 printf(
"datfile := %s\n", datfile);
860 printf(
"siffile := %s\n", siffile);
865 sdata=(
short int*)malloc(pxlNr*
sizeof(
short int));
869 if(frame_to_write==1) fp=fopen(datfile,
"wb");
else fp=fopen(datfile,
"r+b");
872 if(fseek(fp, (frame_to_write-1)*pxlNr*img->
dimz*
sizeof(
short int), SEEK_SET)!=0) {
874 little=little_endian();
877 for(zi=0; zi<img->
dimz; zi++) {
879 for(yi=img->
dimy-1; yi>=0; yi--)
for(xi=img->
dimx-1; xi>=0; xi--) {
880 *sptr=temp_roundf(scale_factor*img->
m[zi][yi][xi][frame_index]); sptr++;
883 sptr=sdata;
if(little!=dsr.
little) swabip(sptr, pxlNr*
sizeof(
short int));
886 if(fwrite(sptr,
sizeof(
short int), pxlNr, fp) != pxlNr) {
891 for(zi=img->
dimz-1; zi>=0; zi--) {
893 for(yi=img->
dimy-1; yi>=0; yi--)
for(xi=img->
dimx-1; xi>=0; xi--) {
894 *sptr=temp_roundf(scale_factor*img->
m[zi][yi][xi][frame_index]); sptr++;
897 sptr=sdata;
if(little!=dsr.
little) swabip(sptr, pxlNr*
sizeof(
short int));
900 if(fwrite(sptr,
sizeof(
short int), pxlNr, fp) != pxlNr) {