TPCCLIB
Loading...
Searching...
No Matches
imgflip.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 <math.h>
14#include <string.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 "Flip PET image in X, Y, and/or Z direction(s).",
25 "By default, if no options are given, flipping in X and Y directions.",
26 " ",
27 "Usage: @P [Options] imagefile outputfile",
28 " ",
29 "Options:",
30 " -x",
31 " Flip image in X direction (horizontally).",
32 " -y",
33 " Flip image in Y direction (vertically).",
34 " -z",
35 " Flip image in Z direction (image planes/slices).",
36 " -up | -right",
37 " Flip image to view it from above or right side.",
38 " -stdoptions", // List standard options like --help, -v, etc
39 " ",
40 "See also: esplit, img2cube, imgbox, imgslice, img2tif, ecat2ana",
41 " ",
42 "Keywords: image, tool, software testing",
43 0};
44/*****************************************************************************/
45
46/*****************************************************************************/
47/* Turn on the globbing of the command line, since it is disabled by default in
48 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
49 In Unix&Linux wildcard command line processing is enabled by default. */
50/*
51#undef _CRT_glob
52#define _CRT_glob -1
53*/
54int _dowildcard = -1;
55/*****************************************************************************/
56
57/*****************************************************************************/
61int main(int argc, char **argv)
62{
63 int ai, help=0, version=0, verbose=1;
64 int zi, ret;
65 int flip_vertical=0, flip_horizontal=0, flip_planes=0;
66 int flip_right=0, flip_up=0;
67 char imgfile[FILENAME_MAX], resfile[FILENAME_MAX],
68 *cptr, tmp[FILENAME_MAX];
69 IMG img;
70
71
72 /*
73 * Get arguments
74 */
75 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
76 imgfile[0]=resfile[0]=(char)0;
77 imgInit(&img);
78 /* Options */
79 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
80 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
81 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
82 if(strcasecmp(cptr, "X")==0) {
83 flip_horizontal=1; continue;
84 } else if(strcasecmp(cptr, "Y")==0) {
85 flip_vertical=1; continue;
86 } else if(strcasecmp(cptr, "Z")==0) {
87 flip_planes=1; continue;
88 } else if(strspn(cptr, "XYZxyz")==strlen(cptr)) {
89 if(strpbrk("Xx", cptr)!=NULL) flip_horizontal=1;
90 if(strpbrk("Yy", cptr)!=NULL) flip_vertical=1;
91 if(strpbrk("Zz", cptr)!=NULL) flip_planes=1;
92 continue;
93 } else if(strcasecmp(cptr, "RIGHT")==0 || strcasecmp(cptr, "RIGTH")==0) {
94 flip_right=1; continue;
95 } else if(strcasecmp(cptr, "UP")==0) {
96 flip_up=1; continue;
97 }
98 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
99 return(1);
100 } else break;
101
102 /* Print help or version? */
103 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
104 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
105 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
106
107 /* Process other arguments, starting from the first non-option */
108 for(; ai<argc; ai++) {
109 if(!imgfile[0]) { /* Dynamic ECAT file */
110 strcpy(imgfile, argv[ai]); continue;
111 } else if(!resfile[0]) { /* Output file */
112 strcpy(resfile, argv[ai]); continue;
113 }
114 /* We should not be here */
115 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
116 return(1);
117 } /* next argument */
118
119 /* Is something missing? */
120 if(!resfile[0]) {
121 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
122 return(1);
123 }
124 if(flip_horizontal==0 && flip_vertical==0 && flip_planes==0
125 && flip_up==0 && flip_right==0)
126 {
127 if(verbose>0)
128 fprintf(stderr, "Warning: no flipping was requested; assuming X and Y.\n");
129 flip_vertical=flip_horizontal=1;
130 }
131 if(flip_right && flip_up) {
132 fprintf(stderr, "Error: do not use options -right and -up together.\n");
133 return(1);
134 }
135
136 /* In verbose mode print arguments and options */
137 if(verbose>1) {
138 printf("imgfile := %s\n", imgfile);
139 printf("resfile := %s\n", resfile);
140 printf("flip_x := %d\n", flip_horizontal);
141 printf("flip_y := %d\n", flip_vertical);
142 printf("flip_z := %d\n", flip_planes);
143 printf("flip_right := %d\n", flip_right);
144 printf("flip_up := %d\n", flip_up);
145 }
146 if(verbose>10) IMG_TEST=verbose-10; else IMG_TEST=0;
147
148
149 /*
150 * Read the contents of the image file to img data structure
151 */
152 if(verbose>1) fprintf(stdout, "reading %s\n", imgfile);
153 ret=imgRead(imgfile, &img);
154 if(ret) {
155 fprintf(stderr, "Error: %s\n", img.statmsg); if(verbose>1) imgInfo(&img);
156 imgEmpty(&img); return(2);
157 }
158 /* If planes are to be flipped, then check that they are continuous */
159 if(flip_planes || flip_right || flip_up) {
160 for(zi=1; zi<img.dimz; zi++)
161 if(abs(img.planeNumber[zi]-img.planeNumber[zi-1])>1) {
162 fprintf(stderr, "Error: plane numbers are not continuous.\n");
163 imgEmpty(&img); return(2);
164 }
165 }
166 /* and that there are dimensions to flip */
167 if(flip_right || flip_up) {
168 if(img.dimz<2) {
169 fprintf(stderr, "Error: image has only one plane.\n");
170 imgEmpty(&img); return(2);
171 }
172 if(img.dimx<2) {
173 fprintf(stderr, "Error: image has only one column.\n");
174 imgEmpty(&img); return(2);
175 }
176 if(img.dimy<2) {
177 fprintf(stderr, "Error: image has only one row.\n");
178 imgEmpty(&img); return(2);
179 }
180 }
181
182 /* Check if resfile exists; rename to backup file, if necessary */
183 ret=backupExistingFile(resfile, NULL, tmp); if(ret!=0) {
184 fprintf(stderr, "Error: %s\n", tmp);
185 imgEmpty(&img); return(13);
186 }
187
188
189 /*
190 * Flipping XYZ
191 */
192 if(verbose>1) printf("flipping the image...\n");
193 /* Flip horizontally */
194 if(flip_horizontal) {
195 if(verbose>0) fprintf(stdout, "horizontal flipping\n");
196 imgFlipHorizontal(&img);
197 }
198 /* Flip vertically */
199 if(flip_vertical) {
200 if(verbose>0) fprintf(stdout, "vertical flipping\n");
201 imgFlipVertical(&img);
202 }
203 /* Flip planes */
204 if(flip_planes) {
205 /* We checked before that plane numbers are continuous */
206 if(verbose>0) fprintf(stdout, "flipping planes\n");
207 /* Switch pointers to planes */
208 imgFlipPlanes(&img);
209 if(verbose>20) imgInfo(&img);
210 }
211
212 /*
213 * Look from right or from above
214 */
215 ret=0;
216 if(flip_right) {
217 if(verbose>0) fprintf(stdout, "flipping from right\n");
218 ret=imgFlipRight(&img);
219 if(verbose>20) imgInfo(&img);
220 } else if(flip_up) {
221 if(verbose>0) fprintf(stdout, "flipping from above\n");
222 ret=imgFlipAbove(&img);
223 if(verbose>20) imgInfo(&img);
224 }
225 if(ret!=0) {
226 fprintf(stderr, "Error: cannot flip the image.\n");
227 imgEmpty(&img); return(100+ret);
228 }
229
230
231 /*
232 * Save flipped image file
233 */
234 if(verbose>1) fprintf(stdout, "writing %s\n", resfile);
235 ret=imgWrite(resfile, &img);
236 if(ret) {
237 fprintf(stderr, "Error: %s\n", img.statmsg); if(verbose>2) imgInfo(&img);
238 imgEmpty(&img); return(11);
239 }
240 imgEmpty(&img);
241 if(verbose>0) fprintf(stdout, "done.\n");
242
243 return(0);
244}
245/*****************************************************************************/
246
247/*****************************************************************************/
int backupExistingFile(char *filename, char *backup_ext, char *status)
Definition backup.c:14
int IMG_TEST
Definition img.c:6
void imgInfo(IMG *image)
Definition img.c:359
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
void imgFlipVertical(IMG *img)
Definition imgflips.c:34
void imgFlipHorizontal(IMG *img)
Definition imgflips.c:13
int imgFlipRight(IMG *img)
Definition imgflips.c:73
void imgFlipPlanes(IMG *img)
Definition imgflips.c:55
int imgFlipAbove(IMG *img)
Definition imgflips.c:115
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
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
int * planeNumber
unsigned short int dimz
unsigned short int dimy
const char * statmsg