TPCCLIB
Loading...
Searching...
No Matches
imgdecayc.c
Go to the documentation of this file.
1
5/*****************************************************************************/
6#include "libtpcimgio.h"
7/*****************************************************************************/
8
9/*****************************************************************************/
18 IMG *image,
20 int mode
21) {
22 float lambda;
23 float cf, dur;
24
25 /* Check for arguments */
26 if(image->status!=IMG_STATUS_OCCUPIED) return(1);
27 if(image->isotopeHalflife<=0.0) return(1);
28 /* Existing/nonexisting decay correction is an error */
29 if(mode==1 && image->decayCorrection!=IMG_DC_NONCORRECTED) return(2);
30 if(mode==0 && image->decayCorrection!=IMG_DC_CORRECTED) return(2);
31
32 /* All time frames */
33 for(int fi=0; fi<image->dimt; fi++) {
34 dur=image->end[fi]-image->start[fi];
35 if(image->end[fi]>0.0) {
36 if(mode==0 && image->decayCorrFactor[fi]>1.000001) {
37 /* if decay correction is to be removed, and factor is known,
38 then use it */
39 cf=1.0/image->decayCorrFactor[fi];
40 } else {
41 lambda=hl2lambda(image->isotopeHalflife); if(lambda<0.0) return(1);
42 /* remove decay correction by giving negative lambda */
43 if(mode==0) lambda=-lambda;
44 if(fi==image->dimt-1 && image->end[fi]<=0.0) return(3);
45 cf=hlLambda2factor_float(lambda, image->start[fi], dur);
46 }
47 if(IMG_TEST) printf("applied_dc_factor[%d] := %g\n", fi+1, cf);
48 /* Set decay correction factor inside IMG for future */
49 if(mode==0) {
50 image->decayCorrFactor[fi]=1.0;
51 } else {
52 image->decayCorrFactor[fi]=cf;
53 }
54 /* All planes, all matrices */
55 for(int pi=0; pi<image->dimz; pi++)
56 for(int i=0; i<image->dimy; i++)
57 for(int j=0; j<image->dimx; j++)
58 image->m[pi][i][j][fi]*=cf;
59 if(mode==0) image->decayCorrection=IMG_DC_NONCORRECTED;
61 /* in some cases left unchanged! */
62 }
63 } /* next frame */
64 return(0);
65}
66/*****************************************************************************/
67
68/*****************************************************************************/
76char *imgIsotope(IMG *img) {
78}
79/*****************************************************************************/
80
81/*****************************************************************************/
89 IMG *image,
91 int mode
92) {
93 float lambda, cf, dur;
94
95 /* Check for arguments */
96 if(image->status!=IMG_STATUS_OCCUPIED) return(1);
97 if(image->isotopeHalflife<=0.0) return(1);
98
99 /* Check that image contains frame times */
100 if(mode!=0 && image->end[image->dimt-1]<=0.0) return(3);
101
102 /* All time frames */
103 for(int fi=0; fi<image->dimt; fi++) {
104 if(mode==0) {
105 image->decayCorrFactor[fi]=1.0;
106 } else {
107 dur=image->end[fi]-image->start[fi];
108 if(image->end[fi]>0.0) {
109 lambda=hl2lambda(image->isotopeHalflife); if(lambda<0.0) return(2);
110 cf=hlLambda2factor_float(lambda, image->start[fi], dur);
111 image->decayCorrFactor[fi]=cf;
112 }
113 }
114 } /* next frame */
115 if(mode==0) image->decayCorrection=IMG_DC_NONCORRECTED;
117 return(0);
118}
119/*****************************************************************************/
120
121/*****************************************************************************/
134 IMG *image,
136 int mode,
138 int verbose,
140 char *status
141) {
142 int isotope;
143 float bf;
144 float cf;
145
146 if(verbose>0) printf("imgBranchingCorrection(*img, %d, %d, *status)\n",
147 mode, verbose);
148 /* Check for arguments */
149 if(status!=NULL) strcpy(status, "invalid input");
150 if(image->status!=IMG_STATUS_OCCUPIED) return(1);
151 if(image->isotopeHalflife<=0.0) {
152 if(verbose>0) printf("Error: unknown isotope.\n");
153 if(status!=NULL) strcpy(status, "unknown isotope");
154 return(2);
155 }
156
157 /* If branching factor not found in IMG, then get it based on half-life */
158 bf=image->branchingFraction;
159 if(bf<=0.0 || bf>=1.0) {
160 isotope=hlIsotopeFromHalflife(image->isotopeHalflife/60.0);
161 if(verbose>2) printf("isotope := %d\n", isotope);
162 bf=branchingFraction(isotope);
163 }
164 if(bf<=0.0 || bf>=1.0) {
165 if(verbose>0) printf("Error: branching fraction unknown for the isotope.\n");
166 if(status!=NULL) strcpy(status, "branching fraction unknown for the isotope");
167 return(3);
168 }
169
170 /* Multiply with BF to remove correction, and divide to correct */
171 if(mode==0) cf=bf; else cf=1.0/bf;
172 /* Process pixel values */
173 if(verbose>1) printf("multiplying data by %g\n", cf);
174 for(int fi=0; fi<image->dimt; fi++) {
175 for(int pi=0; pi<image->dimz; pi++)
176 for(int i=0; i<image->dimy; i++)
177 for(int j=0; j<image->dimx; j++)
178 image->m[pi][i][j][fi]*=cf;
179 }
180
181 /* Fix header contents, too */
182 if(image->calibrationFactor>0.0) image->calibrationFactor*=cf;
183 if(mode==0) image->branchingFraction=0.0;
184 else image->branchingFraction=bf;
185
186 if(status!=NULL) strcpy(status, "ok");
187 return(0);
188}
189/*****************************************************************************/
190
191/*****************************************************************************/
192
float branchingFraction(int isotope)
Definition branch.c:14
double hl2lambda(double halflife)
Definition halflife.c:84
char * hlIsotopeCode(int isotope)
Definition halflife.c:36
float hlLambda2factor_float(float lambda, float frametime, float framedur)
Definition halflife.c:118
int hlIsotopeFromHalflife(double halflife)
Definition halflife.c:195
int IMG_TEST
Definition img.c:6
char * imgIsotope(IMG *img)
Definition imgdecayc.c:76
int imgBranchingCorrection(IMG *image, int mode, int verbose, char *status)
Definition imgdecayc.c:132
int imgSetDecayCorrFactors(IMG *image, int mode)
Definition imgdecayc.c:87
int imgDecayCorrection(IMG *image, int mode)
Definition imgdecayc.c:16
Header file for libtpcimgio.
#define IMG_STATUS_OCCUPIED
#define IMG_DC_CORRECTED
#define IMG_DC_NONCORRECTED
unsigned short int dimx
float branchingFraction
float **** m
char decayCorrection
char status
unsigned short int dimt
float * start
unsigned short int dimz
unsigned short int dimy
float * end
float calibrationFactor
float * decayCorrFactor
float isotopeHalflife