TPCCLIB
Loading...
Searching...
No Matches
maskinv.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 "Invert mask image in ECAT 6.3 or 7.x, NIfTI-1, or Analyze 7.5 format.",
25 "Pixels with value zero are set to 1, and other pixels are set to 0.",
26 " ",
27 "Usage: @P [Options] maskfile [invertedfile]",
28 " ",
29 "Options:",
30 " -stdoptions", // List standard options like --help, -v, etc
31 " ",
32 "See also: maskconj, maskdila, maskeros, imgmask, imgthrs, masklbl, img2dft",
33 " ",
34 "Keywords: image, mask, invert",
35 0};
36/*****************************************************************************/
37
38/*****************************************************************************/
39/* Turn on the globbing of the command line, since it is disabled by default in
40 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
41 In Unix&Linux wildcard command line processing is enabled by default. */
42/*
43#undef _CRT_glob
44#define _CRT_glob -1
45*/
46int _dowildcard = -1;
47/*****************************************************************************/
48
49/*****************************************************************************/
53int main(int argc, char **argv)
54{
55 int ai, help=0, version=0, verbose=1;
56 int ret;
57 char maskfile[FILENAME_MAX], outfile[FILENAME_MAX];
58 char *cptr=NULL;
59
60
61 /*
62 * Get arguments
63 */
64 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
65 maskfile[0]=outfile[0]=(char)0;
66 /* Options */
67 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
68 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
69 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
70 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
71 return(1);
72 } else break;
73
74 /* Print help or version? */
75 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
76 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
77 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
78
79 /* Process other arguments, starting from the first non-option */
80 if(ai<argc) {strlcpy(maskfile, argv[ai], FILENAME_MAX); ai++;}
81 if(ai<argc) {strlcpy(outfile, argv[ai], FILENAME_MAX); ai++;}
82 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
83
84 /* Did we get all the information that we need? */
85 if(!maskfile[0]) {
86 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
87 return(1);
88 }
89 if(!outfile[0]) {
90 strcpy(outfile, maskfile);
91 }
92
93
94 /* In verbose mode print options */
95 if(verbose>1) {
96 printf("maskfile := %s\n", maskfile);
97 printf("outfile := %s\n", outfile);
98 fflush(stdout);
99 }
100
101
102 /*
103 * Read mask, and optional PET image
104 */
105 IMG mask; imgInit(&mask);
106
107 if(verbose>0) {printf("reading %s\n", maskfile); fflush(stdout);}
108 ret=imgRead(maskfile, &mask);
109 if(ret) {
110 fprintf(stderr, "Error: %s\n", mask.statmsg);
111 if(verbose>1) printf("ret := %d\n", ret);
112 return(2);
113 }
114 if(mask.dimt>1) {
115 fprintf(stderr, "Error: mask cannot be dynamic image.\n");
116 imgEmpty(&mask); return(2);
117 }
118 /* Check whether mask has any voxels to begin with */
119 unsigned int mn;
120 mn=imgMaskCount(&mask);
121 if(mn==0) {
122 fprintf(stderr, "Warning: initial mask contains no positive voxels.\n");
123 imgEmpty(&mask);
124 return(0);
125 }
126 if(verbose>1) {
127 printf("initial_nr_of_positive_voxels := %u\n", mn);
128 fflush(stdout);
129 }
130
131
132 /*
133 * Invert the mask
134 */
135 if(verbose>0) {printf("inverting\n"); fflush(stdout);}
136 imgMaskInvert(&mask);
137 /* Check whether mask has any voxels left */
138 mn=imgMaskCount(&mask);
139 if(mn==0) {
140 fprintf(stderr, "Warning: all voxels are zeroes; empty mask not saved.\n");
141 imgEmpty(&mask);
142 return(0);
143 }
144 if(verbose>1) {
145 printf("nr_of_positive_voxels := %u\n", mn);
146 }
147
148
149 /*
150 * Save the modified mask
151 */
152 if(verbose>2) printf("writing mask\n");
153 ret=imgWrite(outfile, &mask);
154 if(ret) {
155 fprintf(stderr, "Error: %s\n", mask.statmsg);
156 imgEmpty(&mask); return(11);
157 }
158 imgEmpty(&mask);
159 if(verbose>0) printf("mask written.\n\n");
160
161 return(0);
162}
163/*****************************************************************************/
164
165/*****************************************************************************/
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.
long long imgMaskCount(IMG *img)
Definition mask.c:15
void imgMaskInvert(IMG *img)
Definition mask.c:216
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 dimt
const char * statmsg