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

Copying data from IMG to SIF and vice versa. More...

#include "libtpcimgio.h"

Go to the source code of this file.

Functions

int sif2img (SIF *sif, IMG *img, int copy_header, int copy_frames, int copy_counts, int verbose)
int img2sif (IMG *img, SIF *sif, int copy_header, int copy_frames, int copy_counts, int verbose)

Detailed Description

Copying data from IMG to SIF and vice versa.

Author
Vesa Oikonen

Definition in file img_sif.c.

Function Documentation

◆ img2sif()

int img2sif ( IMG * img,
SIF * sif,
int copy_header,
int copy_frames,
int copy_counts,
int verbose )

Set SIF contents based on data in IMG.

Returns
Returns 0 if successful.
Parameters
imgPointer to IMG struct from which content is copied to SIF.
sifPointer to SIF. Must be initiated with sifInit(), but SIF is allocated here if necessary.
copy_headerSelect whether header contents are copied (1) or not copied (0) to SIF.
copy_framesSelect whether frame times are copied (1) or not copied (0) to SIF.
copy_countsSelect whether counts are copied (1) or not copied (0) to SIF, or created if they do not exist (2).
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 71 of file img_sif.c.

85 {
86 if(verbose>0)
87 printf("img2sif(img, sif, %d, %d, %d, ...)\n", copy_header, copy_frames, copy_counts);
88
89 /* Check the arguments */
90 if(img==NULL || sif==NULL) return(1);
91 if(img->dimt<1) return(1);
92
93 /* Verify that IMG contains frame times */
94 if(!imgExistentTimes(img)) {
95 if(verbose>0) printf(" image does not contain frame times.\n");
96 /* If not, then frame times cannot be copied */
97 copy_frames=0;
98 /* and counts can not be created */
99 if(copy_counts==2) copy_counts=1;
100 }
101
102 /* Check if count data needs to be created */
103 if(copy_counts==2 && imgExistentCounts(img)!=0)
104 copy_counts=1;
105
106 /* Verify that IMG contains isotope information */
107 if(img->isotopeHalflife<=0.0) {
108 if(verbose>0) printf(" image does not contain isotope halflife.\n");
109 /* not, then count data can not be created */
110 if(copy_counts==2) copy_counts=1;
111 }
112
113 /* Allocate memory for SIF if necessary */
114 if((copy_frames || copy_counts) && sif->frameNr!=img->dimt) {
115 if(sifSetmem(sif, img->dimt)!=0) return(3);
116 }
117
118 /* copy SIF header */
119 if(copy_header) {
120 if(verbose>1) printf(" copying header fields.\n");
121 sif->scantime=img->scanStart;
122 sif->colNr=4;
123 sif->version=1;
124 strcpy(sif->isotope_name, imgIsotope(img));
125 if(strlen(img->studyNr)>0 && strcmp(img->studyNr, ".")!=0)
126 strlcpy(sif->studynr, img->studyNr, MAX_STUDYNR_LEN+1);
127 else
128 strcpy(sif->studynr, "");
129 }
130
131 /* copy frame times */
132 if(copy_frames) {
133 if(verbose>1) printf(" copying frame times.\n");
134 for(int fi=0; fi<img->dimt; fi++) {
135 sif->x1[fi]=img->start[fi]; sif->x2[fi]=img->end[fi];
136 }
137 }
138
139 /* copy or create counts, if required */
140 if(copy_counts==2) {
141 if(verbose>1) printf(" creating count data.\n");
142 /* Calculate average of pixel values */
143 long long pxlNr=0;
144 for(int fi=0; fi<img->dimt; fi++) {
145 double v=0.0;
146 for(int k=0; k<img->dimz; k++)
147 for(int j=0; j<img->dimy; j++)
148 for(int i=0; i<img->dimx; i++) if(isfinite(img->m[k][j][i][fi])) {
149 v+=img->m[k][j][i][fi];
150 pxlNr++;
151 }
152 if(pxlNr>0) v/=(double)pxlNr;
153 sif->trues[fi]=v;
154 }
155 /* Multiply by frame durations, unless we have raw data */
156 if(img->type!=IMG_TYPE_RAW)
157 for(int fi=0; fi<img->dimt; fi++)
158 sif->trues[fi]*=(img->end[fi]-img->start[fi]);
159 /* Remove decay correction, unless we have raw data */
160 if(img->type!=IMG_TYPE_RAW
162 double lambda, cf, dur;
163 lambda=-hl2lambda(img->isotopeHalflife);
164 for(int fi=0; fi<img->dimt; fi++) {
165 dur=img->end[fi]-img->start[fi];
166 cf=hlLambda2factor(lambda, img->start[fi], dur);
167 if(cf>0.0) sif->trues[fi]*=cf;
168 }
169 }
170 /* Scale values to a realistic level */
171 double v=sif->trues[0];
172 for(int fi=1; fi<sif->frameNr; fi++) if(sif->trues[fi]>v) v=sif->trues[fi];
173 v=2.0E+07/v;
174 for(int fi=0; fi<sif->frameNr; fi++) sif->trues[fi]*=v;
175 /* Prompts are set to Trues and Randoms are set to zero */
176 for(int fi=0; fi<sif->frameNr; fi++) {
177 sif->prompts[fi]=sif->trues[fi];
178 sif->randoms[fi]=0.0;
179 if(sif->trues[fi]<1.0) sif->trues[fi]=1.0;
180 sif->weights[fi]=img->weight[fi];
181 }
182 } else if(copy_counts!=0) {
183 if(verbose>1) printf(" copying count data.\n");
184 for(int fi=0; fi<img->dimt; fi++) {
185 sif->prompts[fi]=img->prompts[fi];
186 sif->randoms[fi]=img->randoms[fi];
187 sif->trues[fi]=sif->prompts[fi]-sif->randoms[fi];
188 if(sif->trues[fi]<1.0) sif->trues[fi]=1.0;
189 sif->weights[fi]=img->weight[fi];
190 }
191 }
192
193 return(0);
194}
double hl2lambda(double halflife)
Definition halflife.c:84
double hlLambda2factor(double lambda, double frametime, double framedur)
Definition halflife.c:98
int imgExistentTimes(IMG *img)
Definition img.c:613
int imgExistentCounts(IMG *img)
Definition img.c:630
char * imgIsotope(IMG *img)
Definition imgdecayc.c:76
#define IMG_TYPE_RAW
#define IMG_DC_UNKNOWN
#define IMG_DC_CORRECTED
int sifSetmem(SIF *data, int frameNr)
Definition sif.c:56
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
#define MAX_STUDYNR_LEN
Definition libtpcmisc.h:163
unsigned short int dimx
char type
float **** m
char decayCorrection
time_t scanStart
float * prompts
unsigned short int dimt
float * weight
float * start
unsigned short int dimz
unsigned short int dimy
float * end
float isotopeHalflife
char studyNr[MAX_STUDYNR_LEN+1]
float * randoms
double * x1
double * prompts
int frameNr
double * x2
int version
char studynr[MAX_STUDYNR_LEN+1]
time_t scantime
char isotope_name[8]
double * weights
int colNr
double * randoms
double * trues

Referenced by imgWriteAnalyze(), and imgWriteNifti().

◆ sif2img()

int sif2img ( SIF * sif,
IMG * img,
int copy_header,
int copy_frames,
int copy_counts,
int verbose )

Set IMG contents based on data in SIF.

Returns
Returns 0 if successful.
Parameters
sifPointer to SIF struct from which content is copied to IMG.
imgPointer to IMG. Must be initiated with imgInit(), and allocated if frame time or count information is to be copied.
copy_headerSelect whether SIF header contents are copied (1) or not (0)
copy_framesSelect whether SIF frame times are copied (1) or not (0).
copy_countsSelect whether SIF count contents are copied (1) or not (0).
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 13 of file img_sif.c.

27 {
28 if(verbose>0)
29 printf("sif2img(sif, img, %d, %d, %d, ...)\n", copy_header, copy_frames, copy_counts);
30 if(img==NULL || sif==NULL) return 1;
31
32 if(copy_header) {
33 if(verbose>1) printf(" copying header.\n");
34 img->scanStart=sif->scantime;
36 if(strlen(sif->studynr)>0 && strcmp(sif->studynr, ".")!=0)
38 else
39 strcpy(img->studyNr, "");
40 }
41
42 if(copy_frames) {
43 if(verbose>1) printf(" copying frame times.\n");
44 if(sif->frameNr!=img->dimt) return(3);
45 for(int fi=0; fi<img->dimt; fi++) {
46 img->start[fi]=sif->x1[fi];
47 img->end[fi]=sif->x2[fi];
48 img->mid[fi]=0.5*(img->start[fi]+img->end[fi]);
49 }
50 }
51
52 if(copy_counts) {
53 if(verbose>1) printf(" copying count data.\n");
54 if(sif->frameNr!=img->dimt) return(3);
55 if(sif->colNr<4) return(4);
56 for(int fi=0; fi<img->dimt; fi++) {
57 img->prompts[fi]=sif->prompts[fi];
58 img->randoms[fi]=sif->randoms[fi];
59 img->weight[fi]=sif->weights[fi];
60 }
61 }
62
63 return 0;
64}
double hlFromIsotope(char *isocode)
Definition halflife.c:55
float * mid

Referenced by imgReadAnalyze().