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