TPCCLIB
Loading...
Searching...
No Matches
imgmax.c
Go to the documentation of this file.
1
8/*****************************************************************************/
9#include "tpcclibConfig.h"
10/*****************************************************************************/
11#include <stdio.h>
12#include <stdlib.h>
13#include <math.h>
14#include <string.h>
15#include <unistd.h>
16#include <time.h>
17//#include <float.h>
18/*****************************************************************************/
19#include "libtpcmisc.h"
20#include "libtpcimgio.h"
21#include "libtpcimgp.h"
22/*****************************************************************************/
23
24/*****************************************************************************/
25static char *info[] = {
26 "Find maximum pixel value in PET image file(s) in ECAT 6.3 or 7.x, NIfTI-1,",
27 "or Analyze 7.5 format.",
28 " ",
29 "Usage: @P [Options] imgfile(s)",
30 " ",
31 "Options:",
32 " -min",
33 " Minimum values are printed instead of maximum values.",
34 " -both",
35 " Both max and min values are printed.",
36 " -clean",
37 " Only the max and/or min value of all specified files is printed.",
38 " -stdoptions", // List standard options like --help, -v, etc
39 " ",
40 "Example: get the maximum and minimum values inside all ECAT 7 image files",
41 "in the current folder:"
42 " @P -both *.v",
43 " ",
44 "See also: imgmaxp, imgunit, lmhdr, lshdr, eframe, img2tif, img2flat",
45 " ",
46 "Keywords: image, ECAT, NIfTI, Analyze, max, min",
47 0};
48/*****************************************************************************/
49
50/*****************************************************************************/
51/* Turn on the globbing of the command line, since it is disabled by default in
52 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
53 In Unix&Linux wildcard command line processing is enabled by default. */
54/*
55#undef _CRT_glob
56#define _CRT_glob -1
57*/
58int _dowildcard = -1;
59/*****************************************************************************/
60
61/*****************************************************************************/
65int main(int argc, char **argv)
66{
67 int ai, help=0, version=0, verbose=1;
68 int ret, fileNr=0, firstfile=0;
69 int cleanOutput=0;
70 int differing_units=0;
71 int print_what=0; // 0=max, 1=min, 2=both
72 char imgfile[FILENAME_MAX], *cptr=NULL, imgunit[128];
73 IMG img;
74 float imgmax=-FLT_MAX, allmax=-FLT_MAX;
75 float imgmin=FLT_MAX, allmin=FLT_MAX;
76
77
78 /*
79 * Get arguments
80 */
81 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
82 imgfile[0]=(char)0; strcpy(imgunit, "");
83 imgInit(&img);
84 /* Options */
85 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
86 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
87 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
88 if(strncasecmp(cptr, "CLEAN", 1)==0) {
89 cleanOutput=1; continue;
90 } else if(strncasecmp(cptr, "MINIMUM", 3)==0) {
91 print_what=1; continue;
92 } else if(strncasecmp(cptr, "BOTH", 2)==0) {
93 print_what=2; continue;
94 }
95 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
96 return(1);
97 } else break;
98
99 /* Print help or version? */
100 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
101 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
102 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
103
104 /* Process other arguments, starting from the first non-option */
105 for(; ai<argc; ai++) {
106 if(firstfile==0) firstfile=ai;
107 fileNr++;
108 }
109 /* Did we get all the information that we need? */
110 if(fileNr==0) {tpcPrintUsage(argv[0], info, stderr); return(1);}
111
112
113 /* In verbose mode print options */
114 if(verbose>1) {
115 printf("cleanOutput := %d\n", cleanOutput);
116 printf("fileNr := %d\n", fileNr);
117 printf("print_what := %d\n", print_what);
118 }
119
120 if(verbose>2) IMG_TEST=ECAT63_TEST=ECAT7_TEST=verbose-2;
122
123 /*
124 * Process each file
125 */
126 if(verbose>0) printf("processing %d file(s)...\n", fileNr);
127 fileNr=0;
128 for(ai=firstfile; ai<argc; ai++) {
129
130 strlcpy(imgfile, argv[ai], FILENAME_MAX);
131 imgmax=-FLT_MAX; imgmin=FLT_MAX;
132
133 /* Read Image file */
134 if(!cleanOutput) {fprintf(stdout, "%s :\n", imgfile); fflush(stdout);}
135 ret=imgRead(imgfile, &img);
136 if(ret) {
137 fprintf(stderr, "Warning: %s\n", img.statmsg);
138 if(verbose>1) printf("ret := %d\n", ret);
139 continue;
140 }
141
142 /* Get maximum pixel value */
143 ret=imgMinMax(&img, &imgmin, &imgmax);
144 if(ret) {
145 fprintf(stderr, "Warning: cannot read maximum.\n");
146 imgEmpty(&img);
147 continue;
148 }
149 cptr=imgUnit(img.unit);
150 if(imgunit[0] && strcasecmp(imgunit, cptr)!=0) differing_units=1;
151 strcpy(imgunit, imgUnit(img.unit));
152 if(!cleanOutput) {
153 if(print_what==0 || print_what==2)
154 fprintf(stdout, " max := %g %s\n", imgmax, imgunit);
155 if(print_what==1 || print_what==2)
156 fprintf(stdout, " min := %g %s\n", imgmin, imgunit);
157 }
158 if(imgmax>allmax) allmax=imgmax;
159 if(imgmin<allmin) allmin=imgmin;
160 imgEmpty(&img);
161 fileNr++;
162 } /* next file */
163 if(fileNr==0) {
164 fprintf(stderr, "Error: no file(s) could be read.\n");
165 return(3);
166 }
167
168
169 /*
170 * Print the max of all files
171 */
172 if(cleanOutput) {
173 if(print_what==0 || print_what==2)
174 fprintf(stdout, "%g\n", allmax);
175 if(print_what==1 || print_what==2)
176 fprintf(stdout, "%g\n", allmin);
177 } else if(fileNr>1) {
178 if(differing_units) strcpy(imgunit, "");
179 if(print_what==0 || print_what==2)
180 fprintf(stdout, "Maximum of all files := %g %s\n", allmax, imgunit );
181 if(print_what==1 || print_what==2)
182 fprintf(stdout, "Minimum of all files := %g %s\n", allmin, imgunit );
183 }
184
185 return(0);
186}
187/*****************************************************************************/
188
189/*****************************************************************************/
int ECAT63_TEST
Definition ecat63h.c:6
int ECAT7_TEST
Definition ecat7h.c:6
int IMG_TEST
Definition img.c:6
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 imgMinMax(IMG *img, float *minvalue, float *maxvalue)
Definition imgminmax.c:154
char * imgUnit(int dunit)
Definition imgunits.c:315
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
char unit
const char * statmsg