TPCCLIB
Loading...
Searching...
No Matches
pxl2mask.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 "libtpccurveio.h"
20#include "libtpcimgp.h"
21#include "libtpcmodext.h"
22/*****************************************************************************/
23
24/*****************************************************************************/
25static char *info[] = {
26 "Make mask image from specified pixel(s).",
27 "Pixels can be specified as volume range definition files, or as file",
28 "containing list of pixel coordinates.",
29 "A template image (not modified) is needed to get mask dimensions.",
30 " ",
31 "Usage: @P [Options] templatefile maskfile pixel(s)",
32 " ",
33 "Options:",
34 " -stdoptions", // List standard options like --help, -v, etc
35 " ",
36 "Volume range definition (vrd) file is an ASCII text file, which contains",
37 "pixel coordinates (x y z; 1..dimension) of the two opposite corners of",
38 "the extracted image volume, for example:",
39 " corner1 := 63 57 26",
40 " corner2 := 84 71 44",
41 "One or more pixel coordinates (x y z; 1..dimension) can be listed in file,",
42 "for example:",
43 " 24,52,13",
44 " 25,52,14",
45 " ",
46 "See also: pxl2tac, mask2pxl, imgmask, imgmaxp, maskdila",
47 " ",
48 "Keywords: image, pixel, mask",
49 0};
50/*****************************************************************************/
51
52/*****************************************************************************/
53/* Turn on the globbing of the command line, since it is disabled by default in
54 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
55 In Unix&Linux wildcard command line processing is enabled by default. */
56/*
57#undef _CRT_glob
58#define _CRT_glob -1
59*/
60int _dowildcard = -1;
61/*****************************************************************************/
62
63/*****************************************************************************/
67int main(int argc, char **argv)
68{
69 int ai, help=0, version=0, verbose=1;
70 int ret, fileNr=0, firstfile=0;
71 char maskfile[FILENAME_MAX], tempfile[FILENAME_MAX];
72 char *cptr, tmp[128], pxlfile[FILENAME_MAX];
73 IMG img;
74
75
76 /*
77 * Get arguments
78 */
79 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
80 maskfile[0]=tempfile[0]=pxlfile[0]=(char)0;
81 /* Options */
82 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
83 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
84 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
85 /* We should not be here */
86 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
87 return(1);
88 } else break;
89
90 /* Print help or version? */
91 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
92 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
93 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
94
95 /* Process other arguments, starting from the first non-option */
96 if(ai<argc) {strlcpy(tempfile, argv[ai], FILENAME_MAX); ai++;}
97 if(ai<argc) {strlcpy(maskfile, argv[ai], FILENAME_MAX); ai++;}
98 for(; ai<argc; ai++) { // pixel def file(s)
99 if(firstfile==0) firstfile=ai;
100 fileNr++;
101 }
102
103 /* Is something missing or wrong? */
104 if(!maskfile[0] || fileNr<1) {
105 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
106 return(1);
107 }
108
109 /* In verbose mode print arguments and options */
110 if(verbose>1) {
111 printf("tempfile := %s\n", tempfile);
112 printf("maskfile := %s\n", maskfile);
113 printf("fileNr := %d\n", fileNr);
114 fflush(stdout);
115 }
116
117
118 /*
119 * Read the contents of the template PET file to img data structure
120 */
121 {
122 IMG timg;
123 imgInit(&timg);
124 if(verbose>0) printf("reading %s\n", tempfile);
125 ret=imgRead(tempfile, &timg);
126 if(ret) {
127 fprintf(stderr, "Error: %s\n", timg.statmsg);
128 if(verbose>1) printf("ret=%d\n", ret);
129 imgEmpty(&timg); return(2);
130 }
131 if(verbose>0) {
132 printf("pet_dimx := %d\n", timg.dimx);
133 printf("pet_dimy := %d\n", timg.dimy);
134 printf("pet_dimz := %d\n", timg.dimz);
135 //printf("pet_dimt := %d\n", timg.dimt);
136 }
137 imgInit(&img);
138 ret=imgAllocateWithHeader(&img, timg.dimz, timg.dimy, timg.dimx, 1, &timg);
139 imgEmpty(&timg);
140 if(ret!=0) {
141 fprintf(stderr, "Error: cannot allocate mask image.\n");
142 if(verbose>1) printf("ret=%d\n", ret);
143 imgEmpty(&img); return(2);
144 }
145 }
146
147
148 /*
149 * Read pixel definitions
150 */
151 if(verbose==1) printf("reading pixel positions\n");
152 IMG_PIXELS pxl;
153 pxlInit(&pxl);
154 for(ai=firstfile; ai<argc; ai++) {
155 strlcpy(pxlfile, argv[ai], FILENAME_MAX);
156 if(verbose>1) printf("reading %s\n", pxlfile);
157 /* First, try to read file as pixel list */
158 if((ret=pxlRead(&pxl, pxlfile, tmp))==0) {
159 continue;
160 } else if(verbose>1) {
161 printf("could not read as pixel list: %s\n", tmp);
162 if(verbose>2) printf("ret := %d\n", ret);
163 }
164 /* Then try to read as Read Volume Range Definition File */
165 VOL_RANGE vol_range={0,0,0,0,0,0};
166 IMG_PIXEL p={0,0,0,0};
167 if((ret=vrdRead(pxlfile, &vol_range, tmp))==0) {
168 vrdReorder(&vol_range);
169 if(verbose>1) {
170 printf("vol_range.x := %d - %d\n", vol_range.x1, vol_range.x2);
171 printf("vol_range.y := %d - %d\n", vol_range.y1, vol_range.y2);
172 printf("vol_range.z := %d - %d\n", vol_range.z1, vol_range.z2);
173 }
174 /* Add range pixels to the list */
175 long long n=vrdVxlNr(&vol_range);
176 if(n<1) {
177 fprintf(stderr, "Warning: no pixels defined in %s\n", pxlfile);
178 } else {
179 if(pxlAllocateMore(&pxl, n)!=0) {
180 fprintf(stderr, "Error: out of memory.\n");
181 imgEmpty(&img); pxlFree(&pxl); return(3);
182 }
183 for(p.z=vol_range.z1; p.z<=vol_range.z2; p.z++)
184 for(p.x=vol_range.x1; p.x<=vol_range.x2; p.x++)
185 for(p.y=vol_range.y1; p.y<=vol_range.y2; p.y++)
186 pxlAdd(&pxl, &p);
187 }
188 continue;
189 } else if(verbose>1) {
190 printf("could not read as vrd file: %s\n", tmp);
191 if(verbose>2) printf("ret := %d\n", ret);
192 }
193 continue;
194 } // next pixel list or vrd file
195 if(pxl.pxlNr<1) {
196 fprintf(stderr, "Error: no pixels.\n");
197 imgEmpty(&img); pxlFree(&pxl); return(4);
198 }
199 if(verbose>6) {
200 printf("list of pixels:\n");
201 pxlWrite(&pxl, stdout, NULL);
202 }
203 /* Remove any duplicates */
204 ret=pxlRmDuplicates(&pxl);
205 if(ret>0 && verbose>0) {
206 printf("%d pixel duplicates removed.\n", ret);
207 }
208 if(verbose>1) {
209 printf("list of pixels:\n");
210 pxlWrite(&pxl, stdout, NULL);
211 }
212
213 /* Check that pixels are inside image dimensions */
214 ret=0;
215 for(long long int i=0; i<pxl.pxlNr; i++) {
216 if(pxl.p[i].z<1 || pxl.p[i].z>img.dimz) {
217 fprintf(stderr, "Error: pixel outside image z dimension.\n");
218 ret++;
219 }
220 if(pxl.p[i].x<1 || pxl.p[i].x>img.dimx) {
221 fprintf(stderr, "Error: pixel outside image x dimension.\n");
222 ret++;
223 }
224 if(pxl.p[i].y<1 || pxl.p[i].y>img.dimy) {
225 fprintf(stderr, "Error: pixel outside image x dimension.\n");
226 ret++;
227 }
228 }
229 if(ret!=0) {
230 imgEmpty(&img); pxlFree(&pxl); return(4);
231 }
232
233
234 /*
235 * Set mask pixels
236 */
237 if(verbose>0) printf("setting mask contents\n");
238 for(int zi=1; zi<img.dimz; zi++)
239 for(int yi=1; yi<img.dimy; yi++)
240 for(int xi=1; xi<img.dimx; xi++)
241 img.m[zi][yi][xi][0]=(float)0.0;
242 for(long long int i=0; i<pxl.pxlNr; i++)
243 img.m[pxl.p[i].z-1][pxl.p[i].y-1][pxl.p[i].x-1][0]=(float)1.0;
244
245
246 /*
247 * Write the mask image
248 */
249 if(verbose>1) fprintf(stdout, "writing mask in %s\n", maskfile);
250 ret=imgWrite(maskfile, &img);
251 if(ret) {
252 fprintf(stderr, "Error: %s\n", img.statmsg);
253 imgEmpty(&img); return(11);
254 }
255 imgEmpty(&img);
256 if(verbose>0) printf("mask written.\n\n");
257
258 return(0);
259}
260/*****************************************************************************/
261
262/*****************************************************************************/
int imgAllocateWithHeader(IMG *image, int planes, int rows, int columns, int frames, IMG *image_from)
Definition img.c:279
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 libtpccurveio.
Header file for libtpcimgio.
void pxlFree(IMG_PIXELS *pxl)
Definition pixel.c:28
int vrdReorder(VOL_RANGE *vol_range)
Definition vol.c:607
int pxlAllocateMore(IMG_PIXELS *pxl, long long int pxlNr)
Definition pixel.c:74
int vrdVxlNr(VOL_RANGE *vol_range)
Definition vol.c:634
int pxlAdd(IMG_PIXELS *list, IMG_PIXEL *pxl)
Definition pixel.c:139
int pxlWrite(IMG_PIXELS *pxl, FILE *fp, char *status)
Definition pixel.c:299
int pxlRead(IMG_PIXELS *pxl, const char *fname, char *status)
Definition pixel.c:331
long long int pxlRmDuplicates(IMG_PIXELS *list)
Definition pixel.c:273
int vrdRead(char *vdffile, VOL_RANGE *vol_range, char *status)
Definition vol.c:742
void pxlInit(IMG_PIXELS *pxl)
Definition pixel.c:14
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
Header file for libtpcmodext.
IMG_PIXEL * p
long long int pxlNr
unsigned short int dimx
float **** m
unsigned short int dimz
unsigned short int dimy
const char * statmsg