TPCCLIB
Loading...
Searching...
No Matches
mask2pxl.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 "List the pixel coordinates in a mask image.",
25 "Pixels with |value| >= 0.5 in mask image are considered as part of mask,",
26 "and their coordinates (x,y,z,f) are written in specified file or stdout.",
27 " ",
28 "Usage: @P [Options] maskfile [pxlfile]",
29 " ",
30 "Options:",
31 " -stdoptions", // List standard options like --help, -v, etc
32 " ",
33 "See also: pxl2mask, maskinv, maskconj, maskdila, pxl2tac, imgprofi",
34 " ",
35 "Keywords: image, mask, pixel",
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 maskfile[FILENAME_MAX], pxlfile[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 maskfile[0]=pxlfile[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(maskfile, argv[ai], FILENAME_MAX); ai++;}
82 if(ai<argc) {strlcpy(pxlfile, argv[ai], FILENAME_MAX); ai++;}
83 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
84
85 /* Did we get all the information that we need? */
86 if(!maskfile[0]) {
87 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
88 return(1);
89 }
90
91
92 /* In verbose mode print options */
93 if(verbose>1) {
94 printf("maskfile := %s\n", maskfile);
95 if(pxlfile[0]) printf("pxlfile := %s\n", pxlfile);
96 fflush(stdout);
97 }
98
99
100 /*
101 * Read mask image
102 */
103 IMG mask; imgInit(&mask);
104
105 if(verbose>0) {printf("reading %s\n", maskfile); fflush(stdout);}
106 ret=imgRead(maskfile, &mask);
107 if(ret) {
108 fprintf(stderr, "Error: %s\n", mask.statmsg);
109 if(verbose>1) printf("ret := %d\n", ret);
110 return(2);
111 }
112 if(mask.dimt>1) {
113 fprintf(stderr, "Error: mask cannot be dynamic image.\n");
114 imgEmpty(&mask); return(2);
115 }
116
117
118 /*
119 * Get pixel list from mask image
120 */
121 if(verbose>0) {printf("processing the mask\n"); fflush(stdout);}
122 IMG_PIXELS pxl; pxlInit(&pxl);
123 ret=pxlAddFromMask(&pxl, &mask);
124 if(ret==0) {
125 fprintf(stderr, "Error: no voxels are part of mask; empty list not saved.\n");
126 imgEmpty(&mask);
127 return(3);
128 }
129 if(verbose>1) {
130 printf("nr_of_mask_voxels := %u\n", ret);
131 }
132 imgEmpty(&mask);
133
134
135 /*
136 * Save or print the pixel list
137 */
138 if(!pxlfile[0]) {
139 pxlWrite(&pxl, stdout, NULL);
140 pxlFree(&pxl);
141 return(0);
142 }
143 FILE *fp; fp=fopen(pxlfile, "w");
144 if(fp==NULL) {
145 fprintf(stderr, "Error: cannot open file for writing (%s).\n", pxlfile);
146 pxlFree(&pxl); return(11);
147 }
148 ret=pxlWrite(&pxl, fp, NULL);
149 fclose(fp); pxlFree(&pxl);
150 if(ret) {
151 fprintf(stderr, "Error: cannot write in file %s.\n", pxlfile);
152 return(12);
153 }
154 if(verbose>0) printf("Mask pixel coordinates written in %s\n", pxlfile);
155
156 return(0);
157}
158/*****************************************************************************/
159
160/*****************************************************************************/
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
Header file for libtpcimgio.
void pxlFree(IMG_PIXELS *pxl)
Definition pixel.c:28
int pxlWrite(IMG_PIXELS *pxl, FILE *fp, char *status)
Definition pixel.c:299
void pxlInit(IMG_PIXELS *pxl)
Definition pixel.c:14
long long int pxlAddFromMask(IMG_PIXELS *list, IMG *img)
Definition pixel.c:185
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 dimt
const char * statmsg