TPCCLIB
Loading...
Searching...
No Matches
imgaumc.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <math.h>
13#include <string.h>
14#include <unistd.h>
15#include <time.h>
16/*****************************************************************************/
17#include "libtpcmisc.h"
18#include "libtpcimgio.h"
19#include "libtpcimgp.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "Calculate area under moment curve (AUMC) for every pixel in dynamic image.",
25 "Data is not extrapolated, and AUMC is therefore not complete.",
26 " ",
27 "Usage: @P [Options] imgfile outputfile",
28 " ",
29 "Image file can be in ECAT 6.3 or 7.x, NIfTI-1, or Analyze 7.5 format.",
30 "If data units are available, output units will be (Bq/mL)*s^2, if possible.",
31 " ",
32 "Options:",
33 " -MRT",
34 " Instead of AUMC, AUMC/AUC (MRT) is calculated, with units s.",
35 " -log",
36 " Result will be natural log transformed.",
37 " -stdoptions", // List standard options like --help, -v, etc
38 " ",
39 "See also: imgledif, imginteg, imgpeak, imginv, imgthrs",
40 " ",
41 "Keywords: image, peak, time, AUC",
42 0};
43/*****************************************************************************/
44
45/*****************************************************************************/
46/* Turn on the globbing of the command line, since it is disabled by default in
47 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
48 In Unix&Linux wildcard command line processing is enabled by default. */
49/*
50#undef _CRT_glob
51#define _CRT_glob -1
52*/
53int _dowildcard = -1;
54/*****************************************************************************/
55
56/*****************************************************************************/
60int main(int argc, char **argv)
61{
62 int ai, help=0, version=0, verbose=1;
63 char imgfile[FILENAME_MAX], outfile[FILENAME_MAX];
64 int log_transform=0;
65 int mode=0; // 0=AUMC, 1=MRT
66
67
68 /*
69 * Get arguments
70 */
71 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
72 imgfile[0]=outfile[0]=(char)0;
73 /* Options */
74 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
75 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
76 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
77 if(strcasecmp(cptr, "LOG")==0) {
78 log_transform=1; continue;
79 } else if(strcasecmp(cptr, "MRT")==0) {
80 mode=1; continue;
81 } else if(strcasecmp(cptr, "AUMC")==0) {
82 mode=0; continue;
83 }
84 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
85 return(1);
86 } else break;
87
88 /* Print help or version? */
89 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
90 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
91 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
92
93 /* Process other arguments, starting from the first non-option */
94 if(ai<argc) {strlcpy(imgfile, argv[ai], FILENAME_MAX); ai++;}
95 if(ai<argc) {strlcpy(outfile, argv[ai], FILENAME_MAX); ai++;}
96 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
97
98 /* Did we get all the information that we need? */
99 if(!outfile[0]) {
100 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
101 return(1);
102 }
103
104
105 /* In verbose mode print options */
106 if(verbose>1) {
107 printf("imgfile := %s\n", imgfile);
108 printf("outfile := %s\n", outfile);
109 printf("mode := %d\n", mode);
110 printf("log_transform := %d\n", log_transform);
111 }
112
113
114 /*
115 * Read PET image
116 */
117 if(verbose>0) {printf("reading %s\n", imgfile); fflush(stdout);}
118 IMG img; imgInit(&img);
119 if(imgRead(imgfile, &img)) {
120 fprintf(stderr, "Error: %s\n", img.statmsg);
121 return(2);
122 }
123 if(verbose>1) {
124 printf("dimt := %d\n", img.dimt);
125 printf("dimx := %d\n", img.dimx);
126 printf("dimy := %d\n", img.dimy);
127 printf("dimz := %d\n", img.dimz);
128 printf("unit := %s\n", imgUnit(img.unit));
129 }
130 if(img.dimt<2) {
131 fprintf(stderr, "Error: dynamic image required.\n");
132 imgEmpty(&img); return(3);
133 }
134 if(imgNaNs(&img, 1)>0)
135 if(verbose>0) fprintf(stderr, "Warning: missing pixel values.\n");
136 if(!imgExistentTimes(&img)) {
137 fprintf(stderr, "Error: unknown frame times.\n");
138 imgEmpty(&img); return(3);
139 }
140 /* Make sure that there is no overlap in image frames */
141 {
142 if(verbose>0) printf("checking frame overlap\n");
143 int ret=imgFramesCheck(&img, verbose-2);
144 if(ret==1 || ret==3) fprintf(stderr, "Warning: overlapping time frames.\n");
145 if(ret==2 || ret==3) fprintf(stderr, "Warning: gaps between time frames.\n");
146 /* Fix large gaps */
147 if(imgFrameGapFill(&img, verbose-2)) {
148 fprintf(stderr, "Error: cannot fix time frame gap.\n"); fflush(stderr);
149 imgEmpty(&img); return(3);
150 }
151 /* Fix overlaps */
152 if(imgDeleteFrameOverlap(&img)) {
153 fprintf(stderr, "Error: cannot fix time frame overlap.\n"); fflush(stderr);
154 imgEmpty(&img); return(3);
155 }
156 if(verbose>4) {
157 fprintf(stdout, "Frame times (sec):\n");
158 for(int i=0; i<img.dimt; i++)
159 fprintf(stdout, " %e %e %e\n", img.start[i], img.end[i], img.mid[i]);
160 }
161 }
162
163 /* Try to convert to Bq/mL */
164 if(imgConvertUnit(&img, "Bq/mL")!=0) {
165 fprintf(stderr, "Warning: cannot convert units.\n"); fflush(stderr);
166 }
167
168
169 /*
170 * Process
171 */
172 if(verbose>0) printf("processing...\n");
173 IMG oimg; imgInit(&oimg);
174 if(mode==0) {
175 if(imgAUMC(&img, &oimg, verbose-1)) {
176 fprintf(stderr, "Error: cannot calculate AUMC.\n");
177 imgEmpty(&img); return(6);
178 }
179 } else {
180 if(imgMRT(&img, &oimg, verbose-1)) {
181 fprintf(stderr, "Error: cannot calculate MRT.\n");
182 imgEmpty(&img); return(6);
183 }
184 }
185 imgEmpty(&img);
186 if(log_transform) {
187 if(verbose>0) printf("log transform...\n");
188 imgLn(&oimg);
189 }
190
191
192 /*
193 * Save the modified image
194 */
195 if(verbose>0) printf("writing image %s\n", outfile);
196 if(imgWrite(outfile, &oimg)) {
197 fprintf(stderr, "Error: %s\n", oimg.statmsg);
198 imgEmpty(&oimg); return(11);
199 }
200 imgEmpty(&oimg);
201 if(verbose>0) printf("done.\n\n");
202
203 return(0);
204}
205/*****************************************************************************/
206
207/*****************************************************************************/
int imgExistentTimes(IMG *img)
Definition img.c:613
unsigned long long imgNaNs(IMG *img, int fix)
Definition img.c:658
void imgEmpty(IMG *image)
Definition img.c:121
void imgInit(IMG *image)
Definition img.c:60
int imgAUMC(IMG *img, IMG *oimg, int verbose)
Definition imgarithm.c:540
int imgMRT(IMG *img, IMG *oimg, int verbose)
Definition imgarithm.c:606
int imgConvertUnit(IMG *img, char *unit)
Definition imgarithm.c:480
int imgLn(IMG *img)
Definition imgarithm.c:248
int imgRead(const char *fname, IMG *img)
Definition imgfile.c:26
int imgWrite(const char *fname, IMG *img)
Definition imgfile.c:136
int imgFrameGapFill(IMG *img, int verbose)
Definition imgframe.c:50
int imgFramesCheck(IMG *img, int verbose)
Definition imgframe.c:15
int imgDeleteFrameOverlap(IMG *img)
Definition imgframe.c:77
char * imgUnit(int dunit)
Definition imgunits.c:315
Header file for libtpcimgio.
Header file for libtpcimgp.
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:40
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:213
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:383
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:158
unsigned short int dimx
char unit
unsigned short int dimt
float * start
unsigned short int dimz
unsigned short int dimy
float * end
const char * statmsg
float * mid