TPCCLIB
Loading...
Searching...
No Matches
maskcloak.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 "Cloak a mask image in ECAT 6.3 or 7.x, NIfTI-1, or Analyze 7.5 format.",
25 "Original mask image file is overwritten if name for new file is not given.",
26 " ",
27 "Usage: @P [Options] maskfile [newmaskfile]",
28 " ",
29 "Options:",
30 " -xyz | -xy",
31 " Cloak the mask in 3D (default) or 2D.",
32 " -stdoptions", // List standard options like --help, -v, etc
33 " ",
34 "See also: maskdila, maskeros, imgmask, masksize, imgthrs",
35 " ",
36 "Keywords: image, mask, dilation",
37 0};
38/*****************************************************************************/
39
40/*****************************************************************************/
41/* Turn on the globbing of the command line, since it is disabled by default in
42 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
43 In Unix&Linux wildcard command line processing is enabled by default. */
44/*
45#undef _CRT_glob
46#define _CRT_glob -1
47*/
48int _dowildcard = -1;
49/*****************************************************************************/
50
51/*****************************************************************************/
52#if(0)
57int imgMaskCloak(
59 IMG *img,
61 int dim,
63 float id
64) {
65 if(img==NULL) return(1);
66 int dimx=img->dimx, dimy=img->dimy, dimz=img->dimz;
67 if(dimx<1 || dimy<1 || dimz<1) return(2);
68
69 for(int z=0; z<dimz; z++) {
70 for(int y=0; y<dimy; y++) {
71 for(int x=0; x<dimx; x++) if(!(img->m[z][y][x][0]<0.5) && img->m[z][y][x][0]!=id) {
72 int x1=x, x2=x, y1=y, y2=y, z1=z, z2=z;
73 if(x>0) x1=x-1;
74 if(x<dimx-1) x2=x+1;
75 if(y>0) y1=y-1;
76 if(y<dimy-1) y2=y+1;
77 if(dim==3) {
78 if(z>0) z1=z-1;
79 if(z<dimz-1) z2=z+1;
80 }
81 for(int nz=z1; nz<=z2; nz++)
82 for(int ny=y1; ny<=y2; ny++)
83 for(int nx=x1; nx<=x2; nx++)
84 if(z!=nz || y!=ny || x!=nx)
85 if(img->m[nz][ny][nx][0]<0.5)
86 img->m[nz][ny][nx][0]=id;
87 }
88 }
89 }
90
91 return(0);
92}
93#endif
94/*****************************************************************************/
95
96/*****************************************************************************/
100int main(int argc, char **argv)
101{
102 int ai, help=0, version=0, verbose=1;
103 int ret;
104 char maskfile[FILENAME_MAX], outfile[FILENAME_MAX];
105 int dim=3; // 3 (xyz) or 2 (xy)
106 char *cptr=NULL;
107
108
109 /*
110 * Get arguments
111 */
112 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
113 maskfile[0]=outfile[0]=(char)0;
114 /* Options */
115 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
116 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
117 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
118 if(strcasecmp(cptr, "XYZ")==0) {
119 dim=3; continue;
120 } else if(strcasecmp(cptr, "XY")==0) {
121 dim=2; continue;
122 }
123 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
124 return(1);
125 } else break;
126
127 /* Print help or version? */
128 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
129 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
130 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
131
132 /* Process other arguments, starting from the first non-option */
133 if(ai<argc) {strlcpy(maskfile, argv[ai], FILENAME_MAX); ai++;}
134 if(ai<argc) {strlcpy(outfile, argv[ai], FILENAME_MAX); ai++;}
135 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
136
137 /* Did we get all the information that we need? */
138 if(!maskfile[0]) {
139 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
140 return(1);
141 }
142
143
144 /* In verbose mode print options */
145 if(verbose>1) {
146 printf("maskfile := %s\n", maskfile);
147 if(outfile[0]) printf("outfile := %s\n", outfile);
148 printf("dim := %d\n", dim);
149 fflush(stdout);
150 }
151
152 /* If file name for output was not given, the overwrite input */
153 if(!outfile[0]) strcpy(outfile, maskfile);
154
155
156 /*
157 * Read mask
158 */
159 IMG mask; imgInit(&mask);
160
161 if(verbose>0) {printf("reading %s\n", maskfile); fflush(stdout);}
162 ret=imgRead(maskfile, &mask);
163 if(ret) {
164 fprintf(stderr, "Error: %s\n", mask.statmsg);
165 if(verbose>1) printf("ret := %d\n", ret);
166 return(2);
167 }
168 if(mask.dimt>1) {
169 fprintf(stderr, "Error: mask cannot be dynamic image.\n");
170 imgEmpty(&mask); return(2);
171 }
172 /* Check whether mask has any voxels to begin with */
173 long long mn=imgMaskCount(&mask);
174 if(mn==0) {
175 fprintf(stderr, "Warning: initial mask contains no positive voxels.\n");
176 imgEmpty(&mask);
177 return(0);
178 }
179 long long pxlNr=(long long)mask.dimz*mask.dimy*mask.dimx;
180 if(mn==pxlNr) {
181 fprintf(stderr, "Warning: initial mask contains all positive voxels.\n");
182 imgEmpty(&mask);
183 return(0);
184 }
185 if(verbose>1) {
186 printf("initial_nr_of_positive_voxels := %lld\n", mn);
187 }
188
189
190 /*
191 * Set the id number for new mask
192 */
193 float id=0.0;
194 if(imgMax(&mask, &id)!=0 || id<0.9) {
195 fprintf(stderr, "Error: invalid mask ID.\n");
196 imgEmpty(&mask);
197 return(3);
198 }
199 id=1.0+roundf(id);
200 if(verbose>1) printf("new mask ID := %.0f\n", id);
201
202
203 /*
204 * Cloak the mask
205 */
206 if(verbose>0) {printf("cloaking\n"); fflush(stdout);}
207 ret=imgMaskCloak(&mask, dim, id);
208 if(ret!=0) {
209 fprintf(stderr, "Error: cannot cloak the mask.\n");
210 if(verbose>1) printf("ret := %d\n", ret);
211 imgEmpty(&mask);
212 return(8);
213 }
214 long long mnn=imgMaskCount(&mask);
215 if(verbose>0) {
216 fprintf(stdout, "Cloak consists of %lld voxel.\n", mnn-mn);
217 fflush(stdout);
218 }
219
220 /*
221 * Save the modified mask
222 */
223 if(verbose>2) printf("writing mask\n");
224 ret=imgWrite(outfile, &mask);
225 if(ret) {
226 fprintf(stderr, "Error: %s\n", mask.statmsg);
227 imgEmpty(&mask); return(11);
228 }
229 imgEmpty(&mask);
230 if(verbose>0) printf("dilated mask written in %s.\n\n", outfile);
231
232 return(0);
233}
234/*****************************************************************************/
235
236/*****************************************************************************/
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
int imgMax(IMG *img, float *maxvalue)
Definition imgminmax.c:15
Header file for libtpcimgio.
Header file for libtpcimgp.
long long imgMaskCount(IMG *img)
Definition mask.c:15
int imgMaskCloak(IMG *img, int dim, float id)
Definition mask.c:429
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