TPCCLIB
Loading...
Searching...
No Matches
imgzavg.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 "Calculate average of 3D or 4D image over z dimension (image planes).",
25 " ",
26 "Usage: @P [Options] imgfile outputfile",
27 " ",
28 "Image file can be in ECAT 6.3 or 7.x, NIfTI-1, or Analyze 7.5 format.",
29 " ",
30 "Options:",
31 " -stdoptions", // List standard options like --help, -v, etc
32 " ",
33 "See also: imgdelpl, imgshrink, imginteg, imgslim, esplit, imgflip",
34 " ",
35 "Keywords: image, cropping",
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 char imgfile[FILENAME_MAX], outfile[FILENAME_MAX];
58
59
60 /*
61 * Get arguments
62 */
63 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
64 imgfile[0]=outfile[0]=(char)0;
65 /* Options */
66 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
67 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
68 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
69 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
70 return(1);
71 } else break;
72
73 /* Print help or version? */
74 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
75 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
76 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
77
78 /* Process other arguments, starting from the first non-option */
79 if(ai<argc) {strlcpy(imgfile, argv[ai], FILENAME_MAX); ai++;}
80 if(ai<argc) {strlcpy(outfile, argv[ai], FILENAME_MAX); ai++;}
81 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
82
83 /* Did we get all the information that we need? */
84 if(!outfile[0]) {
85 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
86 return(1);
87 }
88
89
90 /* In verbose mode print options */
91 if(verbose>1) {
92 printf("imgfile := %s\n", imgfile);
93 printf("outfile := %s\n", outfile);
94 }
95
96
97 /*
98 * Read PET image
99 */
100 if(verbose>0) {printf("reading %s\n", imgfile); fflush(stdout);}
101 IMG img; imgInit(&img);
102 if(imgRead(imgfile, &img)) {
103 fprintf(stderr, "Error: %s\n", img.statmsg);
104 return(2);
105 }
106 if(verbose>1) {
107 printf("dimt := %d\n", img.dimt);
108 printf("dimx := %d\n", img.dimx);
109 printf("dimy := %d\n", img.dimy);
110 printf("dimz := %d\n", img.dimz);
111 }
112 if(img.dimz<2) {
113 // just a copy will be made
114 fprintf(stderr, "Warning: image z dimension is 1.\n");
115 }
116
117
118 /*
119 * Process
120 */
121 if(verbose>0) printf("processing...\n");
122 IMG oimg; imgInit(&oimg);
123 if(imgMeanZ(&img, &oimg)) {
124 fprintf(stderr, "Error: cannot calculate mean over z dimension.\n");
125 imgEmpty(&img); return(6);
126 }
127 imgEmpty(&img);
128
129
130 /*
131 * Save the modified image
132 */
133 if(verbose>0) printf("writing image %s\n", outfile);
134 if(imgWrite(outfile, &oimg)) {
135 fprintf(stderr, "Error: %s\n", oimg.statmsg);
136 imgEmpty(&oimg); return(11);
137 }
138 imgEmpty(&oimg);
139 if(verbose>0) printf("done.\n\n");
140
141 return(0);
142}
143/*****************************************************************************/
144
145/*****************************************************************************/
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
int imgMeanZ(IMG *img1, IMG *img2)
Definition imgflips.c:157
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
unsigned short int dimx
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy
const char * statmsg