TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcimgio
img_sif.c
Go to the documentation of this file.
1
5
/******************************************************************************/
6
#include "
libtpcimgio.h
"
7
/******************************************************************************/
8
9
/******************************************************************************/
13
int
sif2img
(
15
SIF
*sif,
18
IMG
*img,
20
int
copy_header,
22
int
copy_frames,
24
int
copy_counts,
26
int
verbose
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
;
35
img->
isotopeHalflife
=60.0*
hlFromIsotope
(sif->
isotope_name
);
36
if
(strlen(sif->
studynr
)>0 && strcmp(sif->
studynr
,
"."
)!=0)
37
strlcpy
(img->
studyNr
, sif->
studynr
,
MAX_STUDYNR_LEN
+1);
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
}
65
/*****************************************************************************/
66
67
/*****************************************************************************/
71
int
img2sif
(
73
IMG
*img,
75
SIF
*sif,
77
int
copy_header,
79
int
copy_frames,
82
int
copy_counts,
84
int
verbose
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
161
&& (img->
decayCorrection
==
IMG_DC_UNKNOWN
|| img->
decayCorrection
==
IMG_DC_CORRECTED
)) {
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
}
195
/******************************************************************************/
196
197
/******************************************************************************/
hl2lambda
double hl2lambda(double halflife)
Definition
halflife.c:84
hlLambda2factor
double hlLambda2factor(double lambda, double frametime, double framedur)
Definition
halflife.c:98
hlFromIsotope
double hlFromIsotope(char *isocode)
Definition
halflife.c:55
imgExistentTimes
int imgExistentTimes(IMG *img)
Definition
img.c:613
imgExistentCounts
int imgExistentCounts(IMG *img)
Definition
img.c:630
img2sif
int img2sif(IMG *img, SIF *sif, int copy_header, int copy_frames, int copy_counts, int verbose)
Definition
img_sif.c:71
sif2img
int sif2img(SIF *sif, IMG *img, int copy_header, int copy_frames, int copy_counts, int verbose)
Definition
img_sif.c:13
imgIsotope
char * imgIsotope(IMG *img)
Definition
imgdecayc.c:76
libtpcimgio.h
Header file for libtpcimgio.
IMG_TYPE_RAW
#define IMG_TYPE_RAW
Definition
libtpcimgio.h:1495
IMG_DC_UNKNOWN
#define IMG_DC_UNKNOWN
Definition
libtpcimgio.h:1529
IMG_DC_CORRECTED
#define IMG_DC_CORRECTED
Definition
libtpcimgio.h:1531
sifSetmem
int sifSetmem(SIF *data, int frameNr)
Definition
sif.c:56
strlcpy
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition
strext.c:244
MAX_STUDYNR_LEN
#define MAX_STUDYNR_LEN
Definition
libtpcmisc.h:163
IMG
Definition
libtpcimgio.h:1657
IMG::dimx
unsigned short int dimx
Definition
libtpcimgio.h:1780
IMG::type
char type
Definition
libtpcimgio.h:1704
IMG::m
float **** m
Definition
libtpcimgio.h:1799
IMG::decayCorrection
char decayCorrection
Definition
libtpcimgio.h:1688
IMG::scanStart
time_t scanStart
Definition
libtpcimgio.h:1692
IMG::prompts
float * prompts
Definition
libtpcimgio.h:1831
IMG::dimt
unsigned short int dimt
Definition
libtpcimgio.h:1778
IMG::weight
float * weight
Definition
libtpcimgio.h:1827
IMG::start
float * start
Definition
libtpcimgio.h:1815
IMG::dimz
unsigned short int dimz
Definition
libtpcimgio.h:1784
IMG::dimy
unsigned short int dimy
Definition
libtpcimgio.h:1782
IMG::end
float * end
Definition
libtpcimgio.h:1817
IMG::isotopeHalflife
float isotopeHalflife
Definition
libtpcimgio.h:1685
IMG::studyNr
char studyNr[MAX_STUDYNR_LEN+1]
Definition
libtpcimgio.h:1677
IMG::randoms
float * randoms
Definition
libtpcimgio.h:1833
IMG::mid
float * mid
Definition
libtpcimgio.h:1819
SIF
Definition
libtpcimgio.h:1443
SIF::x1
double * x1
Definition
libtpcimgio.h:1457
SIF::prompts
double * prompts
Definition
libtpcimgio.h:1461
SIF::frameNr
int frameNr
Definition
libtpcimgio.h:1447
SIF::x2
double * x2
Definition
libtpcimgio.h:1459
SIF::version
int version
Definition
libtpcimgio.h:1451
SIF::studynr
char studynr[MAX_STUDYNR_LEN+1]
Definition
libtpcimgio.h:1453
SIF::scantime
time_t scantime
Definition
libtpcimgio.h:1445
SIF::isotope_name
char isotope_name[8]
Definition
libtpcimgio.h:1455
SIF::weights
double * weights
Definition
libtpcimgio.h:1467
SIF::colNr
int colNr
Definition
libtpcimgio.h:1449
SIF::randoms
double * randoms
Definition
libtpcimgio.h:1463
SIF::trues
double * trues
Definition
libtpcimgio.h:1465
Generated on
for TPCCLIB by
1.17.0