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

Scan information file interface with IMG structure. More...

#include "tpcclibConfig.h"
#include "tpcimage.h"

Go to the source code of this file.

Functions

int imgToSIF (IMG *img, TAC *sif, int copy_header, int copy_frames, int copy_counts, int verbose)
 
int imgFromSIF (IMG *img, TAC *sif, int copy_header, int copy_frames, int copy_counts, int verbose)
 

Detailed Description

Scan information file interface with IMG structure.

Author
Vesa Oikonen

Definition in file imagesif.c.

Function Documentation

◆ imgFromSIF()

int imgFromSIF ( IMG * img,
TAC * sif,
int copy_header,
int copy_frames,
int copy_counts,
int verbose )

Copy information from SIF into IMG.

Todo
Add tests.
See also
imgToSIF, imgInit, tacInit, imgWrite, imgRead, tacWriteSIF
Returns
enum tpcerror (TPCERROR_OK when successful).
Parameters
imgPointer to IMG struct into which content is copied from SIF.
sifPointer to SIF, which actually is stored as a TAC format.
copy_headerSelect whether header contents are copied (1) or not copied (0) from SIF.
copy_framesSelect whether frame times are copied (1) or not copied (0) from SIF.
copy_countsSelect whether counts are copied (1) or not copied (0) from SIF.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout

Definition at line 126 of file imagesif.c.

139 {
140 if(verbose>0) {
141 printf("%s(img, sif, %d, %d, %d, ...)\n", __func__, copy_header, copy_frames, copy_counts);
142 fflush(stdout);
143 }
144
145 if(sif==NULL || img==NULL) return(TPCERROR_FAIL);
146
147 if(copy_header) {
148 if(verbose>1) printf(" copying header.\n");
149 int ret=tacGetHeaderScanstarttime(&sif->h, img->scanStart, NULL);
150 if(ret!=TPCERROR_OK && verbose>0) fprintf(stderr, "Warning: cannot read scan start time in SIF\n");
151 img->isot=tacGetIsotope(sif);
152 if(img->isot==ISOTOPE_UNKNOWN && verbose>0) fprintf(stderr, "Warning: cannot read isotope in SIF\n");
153 ret=tacGetHeaderStudynr(&sif->h, img->studyNr, NULL);
154 }
155
156 if(copy_frames) {
157 if(verbose>1) printf(" copying frame times.\n");
158 if(!sif->isframe || sif->sampleNr!=img->dimt) return(TPCERROR_INVALID_FORMAT);
159 for(int fi=0; fi<img->dimt; fi++) {
160 img->x1[fi]=sif->x1[fi];
161 img->x2[fi]=sif->x2[fi];
162 img->x[fi]=0.5*(img->x1[fi]+img->x2[fi]);
163 }
164 img->tunit=sif->tunit;
165 }
166
167 if(copy_counts) {
168 if(verbose>1) printf(" copying count data.\n");
169 if(sif->format!=TAC_FORMAT_SIF || !sif->isframe || sif->sampleNr!=img->dimt)
171 if(sif->tacNr<4) return(TPCERROR_INVALID_FORMAT);
172 for(int fi=0; fi<img->dimt; fi++) {
173 img->prompts[fi]=sif->c[0].y[fi];
174 img->randoms[fi]=sif->c[1].y[fi];
175 }
178 for(int fi=0; fi<img->dimt; fi++)
179 img->weight[fi]=sif->w[fi];
180 img->weighting=sif->weighting;
181 }
182 }
183
184 return(TPCERROR_OK);
185}
float * x1
Definition tpcimage.h:180
float * x2
Definition tpcimage.h:182
float * prompts
Definition tpcimage.h:195
unsigned short int dimt
Definition tpcimage.h:110
float * weight
Definition tpcimage.h:193
weights weighting
Definition tpcimage.h:191
char scanStart[20]
Definition tpcimage.h:94
float * x
Definition tpcimage.h:184
char studyNr[MAX_STUDYNR_LEN+1]
Definition tpcimage.h:85
float * randoms
Definition tpcimage.h:197
isotope isot
Definition tpcimage.h:88
unit tunit
Definition tpcimage.h:205
double * y
Definition tpctac.h:75
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
IFT h
Optional (but often useful) header information.
Definition tpctac.h:141
double * w
Definition tpctac.h:111
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
weights weighting
Definition tpctac.h:115
int tunit
Definition tpctac.h:109
double * x2
Definition tpctac.h:101
double * x1
Definition tpctac.h:99
int tacNr
Definition tpctac.h:91
int tacGetIsotope(TAC *tac)
Definition tacdc.c:25
int tacGetHeaderScanstarttime(IFT *h, char *s, TPCSTATUS *status)
Definition tacift.c:372
int tacGetHeaderStudynr(IFT *h, char *s, TPCSTATUS *status)
Definition tacift.c:26
@ WEIGHTING_OFF
Not weighted or weights not available (weights for all included samples are 1.0).
@ WEIGHTING_UNKNOWN
Not known; usually assumed that not weighted.
@ TPCERROR_FAIL
General error.
@ TPCERROR_INVALID_FORMAT
Invalid file format.
@ TPCERROR_OK
No error.
@ ISOTOPE_UNKNOWN
Unknown.
Definition tpcisotope.h:51
@ TAC_FORMAT_SIF
Scan information file.
Definition tpctac.h:43

Referenced by imgReadNifti().

◆ imgToSIF()

int imgToSIF ( IMG * img,
TAC * sif,
int copy_header,
int copy_frames,
int copy_counts,
int verbose )

Set SIF contents based on data in IMG.

Todo
Add tests.
See also
imgFromSIF, imgInit, tacInit, imgWrite, tacWrite, tacWriteSIF
Returns
enum tpcerror (TPCERROR_OK when successful).
Parameters
imgPointer to IMG struct from which content is copied to SIF.
sifPointer to SIF, which actually is stored as a TAC format. Previous contents are optionally kept.
Precondition
Must be initialized with tacInit().
Parameters
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 18 of file imagesif.c.

35 {
36 if(verbose>0) {
37 printf("%s(img, sif, %d, %d, %d, ...)\n", __func__, copy_header, copy_frames, copy_counts);
38 fflush(stdout);
39 }
40
41 if(img==NULL || sif==NULL) return(TPCERROR_FAIL);
42
43 /* Verify that IMG contains frame times */
44 if(!imgHasTimes(img)) {
45 if(verbose>0) printf(" image does not contain frame times.\n");
46 /* If not, then frame times cannot be copied */
47 copy_frames=0;
48 /* and counts can not be created */
49 if(copy_counts==2) copy_counts=1;
50 }
51
52 /* Check if count data needs to be created */
53 if(copy_counts==2 && imgHasCounts(img)!=0) copy_counts=1;
54
55 /* Verify that IMG contains isotope information */
56 if(img->isot==ISOTOPE_UNKNOWN) {
57 if(verbose>0) printf(" image does not contain isotope halflife.\n");
58 /* not, then count data can not be created */
59 if(copy_counts==2) copy_counts=1;
60 }
61
62 /* Allocate memory for SIF if necessary */
63 int ret=0;
64 if(sif->sampleNr<1 || sif->tacNr<1) {
65 ret=tacAllocate(sif, img->dimt, 2);
66 sif->sampleNr=img->dimt; sif->tacNr=2;
67 } else if((copy_frames || copy_counts) && sif->sampleNr!=img->dimt) {
68 if(sif->sampleNr<img->dimt)
69 ret=tacAllocateMoreSamples(sif, img->dimt-sif->sampleNr);
70 sif->sampleNr=img->dimt;
71 }
72 if((copy_frames || copy_counts) && sif->tacNr<2) {
73 ret=tacAllocateMore(sif, 2-sif->tacNr);
74 sif->tacNr=2;
75 }
76 if(ret!=0) return(TPCERROR_OUT_OF_MEMORY);
77
78 /* Copy SIF header */
79 if(copy_header) {
80 if(verbose>1) printf(" copying header fields.\n");
82 tacSetHeaderStudynr(&sif->h, img->studyNr);
83 tacSetIsotope(sif, img->isot);
84 }
85
86 /* Copy frame times */
87 if(copy_frames) {
88 if(verbose>1) printf(" copying frame times.\n");
89 for(int i=0; i<img->dimt; i++) {
90 sif->x1[i]=img->x1[i]; sif->x2[i]=img->x2[i]; sif->x[i]=img->x[i];
91 }
92 sif->tunit=UNIT_SEC;
93 }
94
95 /* Copy counts */
96 if(copy_counts==1) {
97 if(verbose>1) printf(" copying count data.\n");
98 for(int i=0; i<img->dimt; i++) {
99 sif->c[0].y[i]=img->prompts[i];
100 sif->c[1].y[i]=img->randoms[i];
101 }
102 }
103
104 /* Create counts, if required and possible */
105 if(copy_counts==2) {
106 if(verbose>1) printf(" creating count data.\n");
107
108 fprintf(stderr, "Warning: imgToSIF() does not yet guess counts.\n"); fflush(stderr);
109
110 for(int i=0; i<img->dimt; i++) {
111 sif->c[0].y[i]=0.0;
112 sif->c[1].y[i]=0.0;
113 }
114 }
115
116 return(TPCERROR_OK);
117}
int imgHasTimes(IMG *img)
Definition image.c:235
int imgHasCounts(IMG *img)
Definition image.c:251
double * x
Definition tpctac.h:97
int tacAllocate(TAC *tac, int sampleNr, int tacNr)
Definition tac.c:130
int tacAllocateMoreSamples(TAC *tac, int addNr)
Allocate memory for more samples in TAC data.
Definition tac.c:435
int tacAllocateMore(TAC *tac, int tacNr)
Definition tac.c:178
void tacSetIsotope(TAC *tac, int isotope)
Definition tacdc.c:41
int tacSetHeaderScanstarttime(IFT *h, const char *s)
Definition tacift.c:427
int tacSetHeaderStudynr(IFT *h, const char *s)
Definition tacift.c:79
@ UNIT_SEC
seconds
@ TPCERROR_OUT_OF_MEMORY
Cannot allocate memory.

Referenced by imgWriteNifti().