TPCCLIB
Loading...
Searching...
No Matches
imgmask.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 "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.",
31 " ",
32 "Usage: @P [Options] imgfile maskfile [maskedfile]",
33 " ",
34 "Options:",
35 " -inv",
36 " Pixels that have non-zero value in the mask image will be set to zero",
37 " in the PET image.",
38 " -stdoptions", // List standard options like --help, -v, etc
39 " ",
40 "See also: imgcalc, imgslim, imgadd, simcirc, flat2img, imgmax, maskinv",
41 " ",
42 "Keywords: image, mask, threshold",
43 0};
44/*****************************************************************************/
45
46/*****************************************************************************/
47/* Turn on the globbing of the command line, since it is disabled by default in
48 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
49 In Unix&Linux wildcard command line processing is enabled by default. */
50/*
51#undef _CRT_glob
52#define _CRT_glob -1
53*/
54int _dowildcard = -1;
55/*****************************************************************************/
56
57/*****************************************************************************/
61int main(int argc, char **argv)
62{
63 int ai, help=0, version=0, verbose=1;
64 int ret;
65 int invert=0, overwrite=0;
66 char imgfile[FILENAME_MAX], maskfile[FILENAME_MAX], outfile[FILENAME_MAX];
67 char *cptr=NULL;
68 IMG img, mask;
69
70
71 /*
72 * Get arguments
73 */
74 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
75 imgfile[0]=maskfile[0]=outfile[0]=(char)0;
76 imgInit(&img); imgInit(&mask);
77 /* Options */
78 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
79 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
80 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
81 if(strncasecmp(cptr, "INVERT", 3)==0) {
82 invert=1; 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(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);}
98
99 /* Did we get all the information that we need? */
100 if(!maskfile[0]) {
101 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
102 return(1);
103 }
104 if(!outfile[0]) {strcpy(outfile, imgfile); overwrite=1;}
105
106
107 /* In verbose mode print options */
108 if(verbose>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);
113 }
114
115
116 /*
117 * Read PET and mask image
118 */
119 if(verbose>0) printf("reading %s\n", imgfile);
120 ret=imgRead(imgfile, &img);
121 if(ret) {
122 fprintf(stderr, "Error: %s\n", img.statmsg);
123 if(verbose>1) printf("ret := %d\n", ret);
124 return(2);
125 }
126 if(verbose>0) printf("reading %s\n", maskfile);
127 ret=imgRead(maskfile, &mask);
128 if(ret) {
129 fprintf(stderr, "Error: %s\n", mask.statmsg);
130 if(verbose>1) printf("ret := %d\n", ret);
131 imgEmpty(&img); return(3);
132 }
133 /* Check that dimensions are compatible */
134 if(img.dimx!=mask.dimx || img.dimy!=mask.dimy) {
135 fprintf(stderr, "Error: different image dimensions.\n");
136 if(verbose>0) {
137 printf("image := %d x %d\n", img.dimx, img.dimy);
138 printf("mask := %d x %d\n", mask.dimx, mask.dimy);
139 }
140 imgEmpty(&img); imgEmpty(&mask); return(4);
141 }
142 if(mask.dimz!=1 && img.dimz!=mask.dimz) {
143 fprintf(stderr, "Error: different image dimensions.\n");
144 if(verbose>0) {
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);
147 }
148 imgEmpty(&img); imgEmpty(&mask); return(4);
149 }
150 if(mask.dimt!=1 && img.dimt!=mask.dimt) {
151 fprintf(stderr, "Error: different image dimensions.\n");
152 if(verbose>0) {
153 printf("image := %d x %d x %d x %d\n",
154 img.dimx, img.dimy, img.dimz, img.dimt);
155 printf("mask := %d x %d x %d x %d\n",
156 mask.dimx, mask.dimy, mask.dimz, mask.dimt);
157 }
158 imgEmpty(&img); imgEmpty(&mask); return(4);
159 }
160
161
162 /*
163 * Process the image matrix
164 */
165 if(verbose>0) printf("processing image...\n");
166 int ti, zi, yi, xi, tti, tzi;
167 long long n=0;
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++;
178 }
179 }
180 }
181 imgEmpty(&mask);
182 if(n==0) {
183 fprintf(stderr, "Warning: no pixels modified.\n");
184 if(overwrite) { // no reason to overwrite file when data not modified
185 imgEmpty(&img);
186 return(0);
187 }
188 }
189
190
191 /*
192 * Save the modified image
193 */
194 if(verbose>0) printf("writing image %s\n", outfile);
195 ret=imgWrite(outfile, &img);
196 if(ret) {
197 fprintf(stderr, "Error: %s\n", img.statmsg);
198 imgEmpty(&img); return(11);
199 }
200 imgEmpty(&img);
201 if(verbose>0) printf("done.\n\n");
202
203 return(0);
204}
205/*****************************************************************************/
206
207/*****************************************************************************/
void imgEmpty(IMG *image)
Definition img.c:121
void imgInit(IMG *image)
Definition img.c:60
int imgRead(const char *fname, IMG *img)
Definition imgfile.c:26
int imgWrite(const char *fname, IMG *img)
Definition imgfile.c:136
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
float **** m
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy
const char * statmsg