8#include "tpcclibConfig.h"
23static char *info[] = {
24 "Apply mask image(s) to PET image file in ECAT 6.3 or 7.x, NIfTI-1, or",
25 "Analyze 7.5 format. Mask and PET image must have the same dimensions, except",
26 "if z and/or t dimension in mask image is 1, it is applied to all PET image",
27 "planes and frames, respectively.",
28 "By default, those pixels that have value 0 in the mask image will be set to",
29 "zero in the PET image.",
30 "Notice that PET image file is overwritten; make sure that backup exists.",
32 "Usage: @P [Options] imgfile maskfile [maskedfile]",
36 " Pixels that have non-zero value in the mask image will be set to zero",
40 "See also: imgcalc, imgslim, imgadd, simcirc, flat2img, imgmax, maskinv",
42 "Keywords: image, mask, threshold",
61int main(
int argc,
char **argv)
63 int ai, help=0, version=0, verbose=1;
65 int invert=0, overwrite=0;
66 char imgfile[FILENAME_MAX], maskfile[FILENAME_MAX], outfile[FILENAME_MAX];
74 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
75 imgfile[0]=maskfile[0]=outfile[0]=(char)0;
78 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
79 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
81 if(strncasecmp(cptr,
"INVERT", 3)==0) {
84 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
89 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
94 if(ai<argc) {
strlcpy(imgfile, argv[ai], FILENAME_MAX); ai++;}
95 if(ai<argc) {
strlcpy(maskfile, argv[ai], FILENAME_MAX); ai++;}
96 if(ai<argc) {
strlcpy(outfile, argv[ai], FILENAME_MAX); ai++;}
97 if(ai<argc) {fprintf(stderr,
"Error: too many arguments.\n");
return(1);}
101 fprintf(stderr,
"Error: missing command-line argument; use option --help\n");
104 if(!outfile[0]) {strcpy(outfile, imgfile); overwrite=1;}
109 printf(
"imgfile := %s\n", imgfile);
110 printf(
"maskfile := %s\n", maskfile);
111 printf(
"outfile := %s\n", outfile);
112 printf(
"invert := %d\n", invert);
119 if(verbose>0) printf(
"reading %s\n", imgfile);
122 fprintf(stderr,
"Error: %s\n", img.
statmsg);
123 if(verbose>1) printf(
"ret := %d\n", ret);
126 if(verbose>0) printf(
"reading %s\n", maskfile);
129 fprintf(stderr,
"Error: %s\n", mask.
statmsg);
130 if(verbose>1) printf(
"ret := %d\n", ret);
135 fprintf(stderr,
"Error: different image dimensions.\n");
137 printf(
"image := %d x %d\n", img.
dimx, img.
dimy);
138 printf(
"mask := %d x %d\n", mask.
dimx, mask.
dimy);
143 fprintf(stderr,
"Error: different image dimensions.\n");
145 printf(
"image := %d x %d x %d\n", img.
dimx, img.
dimy, img.
dimz);
146 printf(
"mask := %d x %d x %d\n", mask.
dimx, mask.
dimy, mask.
dimz);
151 fprintf(stderr,
"Error: different image dimensions.\n");
153 printf(
"image := %d x %d x %d x %d\n",
155 printf(
"mask := %d x %d x %d x %d\n",
165 if(verbose>0) printf(
"processing image...\n");
166 int ti, zi, yi, xi, tti, tzi;
168 for(zi=0; zi<img.
dimz; zi++) {
169 if(mask.
dimz==1) tzi=0;
else tzi=zi;
170 for(yi=0; yi<img.
dimy; yi++)
171 for(xi=0; xi<img.
dimx; xi++)
172 for(ti=0; ti<img.
dimt; ti++) {
173 if(mask.
dimt==1) tti=0;
else tti=ti;
174 if(invert==0 && (fabs(mask.
m[tzi][yi][xi][tti])<0.5 || !isfinite(mask.
m[tzi][yi][xi][tti]))) {
175 img.
m[zi][yi][xi][ti]=0.0; n++;
176 }
else if(invert==1 && fabs(mask.
m[tzi][yi][xi][tti])>0.5) {
177 img.
m[zi][yi][xi][ti]=0.0; n++;
183 fprintf(stderr,
"Warning: no pixels modified.\n");
194 if(verbose>0) printf(
"writing image %s\n", outfile);
197 fprintf(stderr,
"Error: %s\n", img.
statmsg);
201 if(verbose>0) printf(
"done.\n\n");
void imgEmpty(IMG *image)
int imgRead(const char *fname, IMG *img)
int imgWrite(const char *fname, IMG *img)
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)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)