TPCCLIB
Loading...
Searching...
No Matches
imgdelpl.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 <unistd.h>
13#include <string.h>
14#include <math.h>
15#include <time.h>
16/*****************************************************************************/
17#include "libtpcmisc.h"
18#include "libtpcmodel.h"
19#include "libtpccurveio.h"
20#include "libtpcimgio.h"
21#include "libtpcimgp.h"
22/*****************************************************************************/
23
24/*****************************************************************************/
25static char *info[] = {
26 "Delete specified planes (z dimension) from PET image in ECAT 6.3, 7.x,",
27 "NIfTI-1, or Analyze 7.5 format. If name for output file is not given,",
28 "then the original image is overwritten.",
29 " ",
30 "Usage: @P [Options] image planenr1 planenr2 [newimage]",
31 " ",
32 "Planes to be deleted can be given either as a range with planenr1",
33 "representing the first and planenr2 the last plane to delete (both >0),",
34 "or the number of planes to delete from the beginning and end (specified",
35 "with negative number or zero).",
36 " ",
37 "Options:",
38 " -zero",
39 " Planes are not actually deleted, but pixel values are set to zero.",
40 " -stdoptions", // List standard options like --help, -v, etc
41 " ",
42 "Example 1: delete planes 2-3",
43 " @P a234dy1.v 2 3 a234dy1_wo2-3.v",
44 "Example 2: delete three first and two last planes",
45 " @P a234dy1.v -3 -2 a234dy1_4-13.v",
46 " ",
47 "See also: imgdelfr, imgdim, imgadd, esplit, imgslim, imgbox, imgzavg",
48 " ",
49 "Keywords: image, cropping, compression, time",
50 0};
51/*****************************************************************************/
52
53/*****************************************************************************/
54/* Turn on the globbing of the command line, since it is disabled by default in
55 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
56 In Unix&Linux wildcard command line processing is enabled by default. */
57/*
58#undef _CRT_glob
59#define _CRT_glob -1
60*/
61int _dowildcard = -1;
62/*****************************************************************************/
63
64/*****************************************************************************/
68int main(int argc, char **argv)
69{
70 int ai, help=0, version=0, verbose=1;
71 int doCrop=1; // 0=zeroes; 1=crop
72 char petfile[FILENAME_MAX], outfile[FILENAME_MAX];
73 int pstart=0, pstop=0;
74 int fstart=0, fend=0;
75
76
77 /*
78 * Get arguments
79 */
80 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
81 petfile[0]=outfile[0]=(char)0;
82 /* Get options */
83 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
84 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
85 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
86 if(strcasecmp(cptr, "CROP")==0) {
87 doCrop=1; continue;
88 } else if(strcasecmp(cptr, "ZERO")==0) {
89 doCrop=0; continue;
90 }
91 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
92 return(1);
93 } else break;
94
95 /* Print help or version? */
96 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
97 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
98 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
99
100 /* Process other arguments, starting from the first non-option */
101 if(ai<argc) {strlcpy(petfile, argv[ai++], FILENAME_MAX);}
102 if(ai<argc) {
103 if(atoi_with_check(argv[ai], &pstart)) {
104 fprintf(stderr, "Error: invalid cut start '%s'.\n", argv[ai]); return(1);}
105 ai++;
106 }
107 if(ai<argc) {
108 if(atoi_with_check(argv[ai], &pstop)) {
109 fprintf(stderr, "Error: invalid cut stop '%s'.\n", argv[ai]); return(1);}
110 ai++;
111 }
112 if(ai<argc) {strlcpy(outfile, argv[ai++], FILENAME_MAX);}
113 if(ai<argc) {fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]); return(1);}
114
115 /* Did we get all the information that we need? */
116 if(!petfile[0]) {
117 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
118 return(1);
119 }
120 if(!outfile[0]) strcpy(outfile, petfile);
121 if(pstart>0 && pstop>=pstart) {
122 /* convert plane range to indices */
123 pstart--; pstop--;
124 } else if(pstart<=0 && pstop<=0 && (pstart<0 || pstop<0)) {
125 /* delete number of planes from beginning/end */
126 if(pstart<0) fstart=-pstart;
127 if(pstop<0) fend=-pstop;
128 pstart=pstop=-1;
129 } else {
130 fprintf(stderr, "Error: invalid plane range.\n");
131 return(1);
132 }
133
134
135 /* In verbose mode print arguments and options */
136 if(verbose>1) {
137 printf("petfile := %s\n", petfile);
138 printf("outfile := %s\n", outfile);
139 printf("doCrop := %d\n", doCrop);
140 if(pstart>=0) {
141 printf("cut_start_plane := %d\n", pstart);
142 printf("cut_stop_plane := %d\n", pstop);
143 } else {
144 printf("cut_planes_from_beginning := %d\n", fstart);
145 printf("cut_planes_from_end := %d\n", fend);
146 }
147 }
148 if(verbose>9) IMG_TEST=verbose-9; else IMG_TEST=0;
149
150
151 /*
152 * Read image
153 */
154 if(verbose>0) fprintf(stdout, "reading image %s\n", petfile);
155 IMG img; imgInit(&img);
156 if(imgRead(petfile, &img)) {fprintf(stderr, "Error: %s\n", img.statmsg); return(2);}
157 if(verbose>1) {
158 printf("dimx := %d\n", img.dimx);
159 printf("dimy := %d\n", img.dimy);
160 printf("dimz := %d\n", img.dimz);
161 printf("dimt := %d\n", img.dimt);
162 }
163
164 /* Check the plane range */
165 if(pstart>=0 && pstop>=img.dimz) {
166 fprintf(stderr, "Error: invalid plane range.\n");
167 imgEmpty(&img); return(1);
168 }
169 if((fstart+fend)>=img.dimz) {
170 fprintf(stderr, "Error: all planes would be cropped.\n");
171 imgEmpty(&img); return(1);
172 }
173
174
175 /*
176 * If contents of selected planes are just to be set to zero,
177 * then do it in place, save the image in new file, and exit.
178 */
179 if(doCrop==0) {
180 if(verbose>1) printf("setting selected pixel values to zero...\n");
181 if(pstart>=0) { // plane range was given
182 for(int zi=pstart; zi<=pstop; zi++)
183 for(int yi=0; yi<img.dimy; yi++)
184 for(int xi=0; xi<img.dimx; xi++)
185 for(int fi=0; fi<img.dimt; fi++)
186 img.m[zi][yi][xi][fi]=0.0;
187 }
188 if(fstart>0) { // delete from beginning
189 for(int zi=0; zi<fstart; zi++)
190 for(int yi=0; yi<img.dimy; yi++)
191 for(int xi=0; xi<img.dimx; xi++)
192 for(int fi=0; fi<img.dimt; fi++)
193 img.m[zi][yi][xi][fi]=0.0;
194 }
195 if(fend>0) { // delete from end
196 for(int zi=img.dimz-fend; zi<img.dimz; zi++)
197 for(int yi=0; yi<img.dimy; yi++)
198 for(int xi=0; xi<img.dimx; xi++)
199 for(int fi=0; fi<img.dimt; fi++)
200 img.m[zi][yi][xi][fi]=0.0;
201 }
202 if(verbose>1) fprintf(stdout, "writing image...\n");
203 if(imgWrite(outfile, &img)) {
204 fprintf(stderr, "Error: %s\n", img.statmsg);
205 imgEmpty(&img); return(11);
206 }
207 if(verbose>0) fprintf(stdout, "%s written.\n", outfile);
208 imgEmpty(&img);
209 return(0);
210 }
211
212
213 /*
214 * Create new IMG without the planes marked to be deleted,
215 * and save it.
216 */
217 if(verbose>1) printf("deleting selected planes...\n");
218 /* Calculate the nr of remaining planes */
219 int planeNr;
220 if(pstart>=0) planeNr=img.dimz-(1+pstop-pstart); else planeNr=img.dimz-fstart-fend;
221 if(planeNr<1) {
222 fprintf(stderr, "Error: all planes would be cropped.\n");
223 imgEmpty(&img); return(4);
224 }
225 if(verbose>2) fprintf(stdout, "allocating memory for %d plane(s)\n", planeNr);
226 IMG out; imgInit(&out);
227 if(imgAllocateWithHeader(&out, planeNr, img.dimy, img.dimx, img.dimt, &img)) {
228 fprintf(stderr, "Error: cannot allocate memory for new image.\n");
229 imgEmpty(&img); return(5);
230 }
231 /* Copy data from the original image */
232 if(verbose>2) fprintf(stdout, "copying data\n");
233 if(pstart>=0) { // copy planes except inside given range
234 int zj=0;
235 for(int zi=0; zi<pstart; zi++, zj++) {
236 for(int yi=0; yi<img.dimy; yi++)
237 for(int xi=0; xi<img.dimx; xi++)
238 for(int fi=0; fi<img.dimt; fi++)
239 out.m[zj][yi][xi][fi]=img.m[zi][yi][xi][fi];
240 }
241 for(int zi=pstop+1; zi<img.dimz; zi++, zj++) {
242 for(int yi=0; yi<img.dimy; yi++)
243 for(int xi=0; xi<img.dimx; xi++)
244 for(int fi=0; fi<img.dimt; fi++)
245 out.m[zj][yi][xi][fi]=img.m[zi][yi][xi][fi];
246 }
247 } else { // copy planes from middle
248 int zj=0;
249 for(int zi=fstart; zi<img.dimz-fend; zi++, zj++) {
250 for(int yi=0; yi<img.dimy; yi++)
251 for(int xi=0; xi<img.dimx; xi++)
252 for(int fi=0; fi<img.dimt; fi++)
253 out.m[zj][yi][xi][fi]=img.m[zi][yi][xi][fi];
254 }
255 }
256 imgEmpty(&img);
257 /* Set plane numbers */
258 for(int zi=0; zi<out.dimz; zi++) out.planeNumber[zi]=1+zi;
259 if(verbose>1) fprintf(stdout, "writing cropped image...\n");
260 if(imgWrite(outfile, &out)) {
261 fprintf(stderr, "Error: %s\n", out.statmsg);
262 imgEmpty(&out); return(11);
263 }
264 if(verbose>0) fprintf(stdout, "%s written.\n", outfile);
265 imgEmpty(&out);
266
267 return(0);
268}
269/*****************************************************************************/
270
271/*****************************************************************************/
int atoi_with_check(const char *int_as_string, int *result_value)
Definition decpoint.c:238
int IMG_TEST
Definition img.c:6
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.
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 libtpcmodel.
unsigned short int dimx
float **** m
unsigned short int dimt
int * planeNumber
unsigned short int dimz
unsigned short int dimy
const char * statmsg