TPCCLIB
Loading...
Searching...
No Matches
tpctacimg.h File Reference

Header file for libtpctacimg. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "tpcextensions.h"
#include "tpctac.h"
#include "tpcimage.h"

Go to the source code of this file.

Functions

int tacAllocateWithIMG (TAC *tac, IMG *img, int tacNr)
 Allocate TAC based on data in IMG.
int imgVb (IMG *img, TAC *btac, double Vb, const int simVb, const int petVolume)
 Correct pixel TACs for vascular blood, or simulate its effect.
int tacimgXMatch (TAC *tac, IMG *img)
int tacimgXCopy (TAC *tac, IMG *img)

Detailed Description

Header file for libtpctacimg.

Header file for template library libtpctacimg.

Author
Vesa Oikonen

Definition in file tpctacimg.h.

Function Documentation

◆ imgVb()

int imgVb ( IMG * img,
TAC * btac,
double Vb,
const int simVb,
const int petVolume )

Correct pixel TACs for vascular blood, or simulate its effect.

See also
tacVb
Returns
enum tpcerror (TPCERROR_OK when successful).
Parameters
imgPointer to IMG data to process.
btacPointer to BTAC data to subtract or add; must contain the same sample times as IMG and y values must be in the same units.
VbVb fraction [0,1].
simVbSwitch to either subtract vascular volume (0) or to simulate it (1).
petVolumeSwitch to model vascular volume as either 0 : Cpet = (1-Vb)*Ct + Vb*Cb, or 1 : Cpet = Ct + Vb*Cb

Definition at line 56 of file misc.c.

70 {
71 if(img==NULL || btac==NULL) return(TPCERROR_FAIL);
72 if(img->dimt<1 || btac->tacNr<1) return(TPCERROR_NO_DATA);
73 if(img->dimt>btac->sampleNr) return(TPCERROR_INCOMPATIBLE_DATA);
74 if(!(Vb>=0.0) || !(Vb<=1.0)) return TPCERROR_INVALID_VALUE;
75 if(Vb==1.0 && petVolume==0 && simVb==0) // combination would lead to divide by zero
77
78 if(simVb==0) {
79 for(int zi=0; zi<img->dimz; zi++)
80 for(int yi=0; yi<img->dimy; yi++)
81 for(int xi=0; xi<img->dimx; xi++)
82 for(int ti=0; ti<img->dimt; ti++) {
83 img->m[zi][yi][xi][ti]-=Vb*btac->c[0].y[ti];
84 if(petVolume==0) img->m[zi][yi][xi][ti]/=(1.0-Vb);
85 }
86 } else {
87 for(int zi=0; zi<img->dimz; zi++)
88 for(int yi=0; yi<img->dimy; yi++)
89 for(int xi=0; xi<img->dimx; xi++)
90 for(int ti=0; ti<img->dimt; ti++) {
91 if(petVolume==0) img->m[zi][yi][xi][ti]*=(1.0-Vb);
92 img->m[zi][yi][xi][ti]+=Vb*btac->c[0].y[ti];
93 }
94 }
95
96 return(TPCERROR_OK);
97}
unsigned short int dimx
Definition tpcimage.h:112
float **** m
Definition tpcimage.h:161
unsigned short int dimt
Definition tpcimage.h:110
unsigned short int dimz
Definition tpcimage.h:116
unsigned short int dimy
Definition tpcimage.h:114
double * y
Definition tpctac.h:75
int sampleNr
Definition tpctac.h:89
TACC * c
Definition tpctac.h:117
int tacNr
Definition tpctac.h:91
@ TPCERROR_INVALID_VALUE
Invalid value.
@ TPCERROR_FAIL
General error.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.
@ TPCERROR_INCOMPATIBLE_DATA
Incompatible data.

◆ tacAllocateWithIMG()

int tacAllocateWithIMG ( TAC * tac,
IMG * img,
int tacNr )
extern

Allocate TAC based on data in IMG.

See also
tacAllocate, tacAllocateMore, tacDuplicate, tacAllocateWithPAR
Returns
enum tpcerror (TPCERROR_OK when successful).
Author
Vesa Oikonen
Parameters
tacPointer to initiated TAC structure; any old contents are deleted. TAC sampleNr and tacNr will be set, although contents will be empty.
Precondition
Structure must be initiated with tacInit.
Parameters
imgPointer to IMG structure which contains frame times and units.
tacNrNumber of TACs to allocate.

Definition at line 16 of file misc.c.

25 {
26 /* Check the input */
27 if(tac==NULL || img==NULL || tacNr<1) return(TPCERROR_FAIL);
28 if(img->dimt<1) return(TPCERROR_NO_DATA);
29
30 /* Allocate TAC */
31 int ret=tacAllocate(tac, img->dimt, tacNr);
32 if(ret!=TPCERROR_OK) return(ret);
33 tac->sampleNr=img->dimt; tac->tacNr=tacNr;
34
35 /* Copy frame times */
36 tac->isframe=1;
37 for(int i=0; i<img->dimt; i++) {
38 tac->x1[i]=img->x1[i];
39 tac->x2[i]=img->x2[i];
40 tac->x[i]=0.5*(tac->x1[i]+tac->x2[i]);
41 }
42
43 /* Copy data units */
44 tac->cunit=img->cunit;
45 tac->tunit=img->tunit;
46
47 return(TPCERROR_OK);
48}
float * x1
Definition tpcimage.h:180
unit cunit
Definition tpcimage.h:203
float * x2
Definition tpcimage.h:182
unit tunit
Definition tpcimage.h:205
double * x
Definition tpctac.h:97
unit cunit
Definition tpctac.h:105
int isframe
Definition tpctac.h:95
double * x2
Definition tpctac.h:101
unit tunit
Definition tpctac.h:109
double * x1
Definition tpctac.h:99
int tacAllocate(TAC *tac, int sampleNr, int tacNr)
Definition tac.c:130

◆ tacimgXCopy()

int tacimgXCopy ( TAC * tac,
IMG * img )
extern

Copy x values (time frames) from IMG structure into TAC structure. If IMG has more frames than has been allocated for TAC (_sampleNr), then additional frames are ignored. If IMG has less frames than TAC, then error is returned.

Returns
Returns tpcerror; 0 if successful and >0 in case of an error.
See also
tacXCopy, tacIsX, tacSetX, tacimgXMatch, imgXRange
Parameters
tacPointer to TAC structure.
imgPointer to IMG structure.

Definition at line 74 of file x.c.

79 {
80 if(img==NULL || tac==NULL) return(TPCERROR_NO_DATA);
81 if(tac->sampleNr>img->dimt || img->dimt<1) return(TPCERROR_INCOMPATIBLE_DATA);
82
83 int sNr=tac->_sampleNr; if(img->dimt<sNr) sNr=img->dimt;
84
85 for(int i=0; i<sNr; i++) {
86 tac->x1[i]=img->x1[i];
87 tac->x2[i]=img->x2[i];
88 tac->x[i]=0.5*(tac->x1[i]+tac->x2[i]);
89 }
90 tac->isframe=1;
91 tac->tunit=img->tunit;
92 return(TPCERROR_OK);
93}
int _sampleNr
Definition tpctac.h:121

◆ tacimgXMatch()

int tacimgXMatch ( TAC * tac,
IMG * img )
extern

Check whether sample (frame) times are the same (or very close to) in TAC and IMG structure structures. If number of samples/frames is different, then only common number of samples are compared.

Returns
Returns 0 in case of no match, 1 if times do match, and <1 in case of an error.
See also
imgCompareTimes, tacXMatch, tacXUnitConvert, tacimgXCopy
Parameters
tacPointer to TAC structure.
imgPointer to IMG structure.

Definition at line 17 of file x.c.

22 {
23 if(img==NULL || tac==NULL) return(-1);
24
25 double acceptedDif=2.2; // sec
26
27 /* Get the last common sample nr */
28 int sNr=tac->sampleNr; if(img->dimt<sNr) sNr=img->dimt;
29 if(sNr==0) return(0);
30
31 /* Get the x of the last common sample */
32 double tLastImg=0.5*(img->x1[sNr-1]+img->x2[sNr-1]);
33 double tLastTac=tac->x[sNr-1]; if(tac->isframe) tLastTac=0.5*(tac->x1[sNr-1]+tac->x2[sNr-1]);
34 if(acceptedDif>tLastImg) acceptedDif=0.1*tLastImg;
35
36 /* Need to convert x unit? */
37 double tscale=nan("");
38 if(tac->tunit==UNIT_SEC && img->tunit==UNIT_SEC) tscale=1.;
39 else if(tac->tunit==UNIT_SEC && img->tunit==UNIT_MIN) tscale=1.0/60;
40 else if(tac->tunit==UNIT_MIN && img->tunit==UNIT_SEC) tscale=60.;
41 else if(tac->tunit==UNIT_MIN && img->tunit==UNIT_MIN) tscale=1.;
42 else {
43 if(tLastImg>30.*tLastTac) tscale=60.;
44 else if(fabs(tLastImg-tLastTac)<30.) tscale=1.;
45 }
46 if(!isfinite(tscale)) return(0);
47
48 /* Compare sample times frame-by-frame */
49 int n=0;
50 if(tac->isframe==0) { // check frame mid times
51 for(int i=0; i<sNr; i++) {
52 double f=fabs(tac->x[i]*tscale - 0.5*(img->x1[i]+img->x2[i]));
53 if(f>acceptedDif) n++;
54 }
55 } else {
56 for(int i=0; i<sNr; i++) {
57 double f=fabs(tac->x1[i]*tscale - img->x1[i]);
58 if(f>acceptedDif) {n++; continue;}
59 f=fabs(tac->x2[i]*tscale - img->x2[i]);
60 if(f>acceptedDif) n++;
61 }
62 }
63 if(n==0) return(1); else return(0);
64}
@ UNIT_MIN
minutes
@ UNIT_SEC
seconds