TPCCLIB
Loading...
Searching...
No Matches
imgunit.c
Go to the documentation of this file.
1
9/*****************************************************************************/
10#include "tpcclibConfig.h"
11/*****************************************************************************/
12#include <stdio.h>
13#include <stdlib.h>
14#include <math.h>
15#include <string.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 "Report or set data calibration units in PET images.",
27 "By default, the calibration units are only shown, not changed.",
28 "Notice that some image formats, like Analyze, do not contain information",
29 "on the calibration units.",
30 "Time frame units are determined by the image format.",
31 " ",
32 "Usage: @P [Options] file(s)",
33 " ",
34 "Options:",
35 " -u=<New unit; e.g. Bq/cc or kBq/ml>",
36 " Set the unit, but does NOT change the pixel values.",
37 " -us=<New unit; e.g. Bq/cc or kBq/ml>",
38 " Set the unit only if unit is not originally defined in the image.",
39 " This does NOT change the pixel values.",
40 " -uc=<New unit; e.g. Bq/cc or kBq/ml>",
41 " Converts pixel values to the specified unit.",
42 " -stdoptions", // List standard options like --help, -v, etc
43 " ",
44 "The following unit conversions are tested (but also others may work):",
45 " Bq/cc -> kBq/ml",
46 " nCi/ml -> kBq/ml",
47 " MBq/ml -> kBq/ml",
48 " kBq/ml -> Bq/cc",
49 " ",
50 "Example: convert the pixel values in ECAT images to Bq/mL",
51 " @P -uc=Bq/ml *.v",
52 " ",
53 "See also: tacunit, lmhdr, lshdr, e7emhdr, imgmax, imgcalc, imglkup",
54 " ",
55 "Keywords: image, ECAT, unit, tool, simulation",
56 0};
57/*****************************************************************************/
58
59/*****************************************************************************/
60/* Turn on the globbing of the command line, since it is disabled by default in
61 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
62 In Unix&Linux wildcard command line processing is enabled by default. */
63/*
64#undef _CRT_glob
65#define _CRT_glob -1
66*/
67int _dowildcard = -1;
68/*****************************************************************************/
69
70/*****************************************************************************/
74int main(int argc, char **argv)
75{
76 int ai, help=0, version=0, verbose=1;
77 int ret, fileNr=0, ffi=0;
78 int ifUnknown=0;
79 int convertUnit=0;
80 char *cptr, petfile[FILENAME_MAX], *newunit=NULL, imgunit[128];
81 IMG img;
82
83
84 /*
85 * Get arguments
86 */
87 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
88 petfile[0]=(char)0; strcpy(imgunit, "");
89 imgInit(&img);
90 /* Options */
91 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
92 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
93 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
94 cptr=argv[ai]+1;
95 if(strncasecmp(cptr, "US=", 3)==0 && newunit==NULL) {
96 cptr+=3; ifUnknown=1;
97 newunit=cptr;
98 if(strlen(newunit)>12) {
99 fprintf(stderr, "Error: unit '%s' too long (>12).\n", newunit);
100 return(1);
101 }
102 if(strlen(newunit)>0) continue;
103 } else if(strncasecmp(cptr, "UC=", 3)==0 && newunit==NULL) {
104 cptr+=3; convertUnit=1;
105 newunit=cptr;
106 if(strlen(newunit)>12) {
107 fprintf(stderr, "Error: unit '%s' too long (>12).\n", newunit);
108 return(1);
109 }
110 if(strlen(newunit)>0) continue;
111 } else if(strncasecmp(cptr, "U=", 2)==0 && newunit==NULL) {
112 cptr+=2; ifUnknown=0;
113 newunit=cptr;
114 if(strlen(newunit)>12) {
115 fprintf(stderr, "Error: unit '%s' too long (>12).\n", newunit);
116 return(1);
117 }
118 if(strlen(newunit)>0) continue;
119 }
120 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
121 return(1);
122 } else break;
123
124 /* Print help or version? */
125 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
126 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
127 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
128
129 /* Process other arguments, starting from the first non-option */
130 for(; ai<argc; ai++) {
131 if(ffi<1) ffi=ai; // save the position of first file
132 fileNr++;
133 }
134
135 /* Is something missing? */
136 if(fileNr<1) {
137 fprintf(stderr, "Error: missing command-line argument; try %s --help\n",
138 argv[0]);
139 return(1);
140 }
141
142 /* In verbose mode print arguments and options */
143 if(verbose>1) {
144 printf("imgunit := %s\n", imgunit);
145 printf("fileNr := %d\n", fileNr);
146 printf("ifUnknown := %d\n", ifUnknown);
147 printf("convertUnit := %d\n", convertUnit);
148 }
149
150
151 /*
152 * Do one file at a time
153 */
154 fileNr=0; // increase when successfully processed
155 for(ai=ffi; ai<argc; ai++) {
156
157 strcpy(petfile, argv[ai]);
158
159 /* Read image file */
160 if(verbose>1) printf("reading %s\n", petfile);
161 ret=imgRead(petfile, &img);
162 if(ret) {
163 fprintf(stderr, "Warning: %s\n", img.statmsg);
164 if(verbose>2) imgInfo(&img);
165 continue;
166 }
167 if(verbose>9) imgInfo(&img);
168
169 /* Get the current image unit */
170 strcpy(imgunit, imgUnit(img.unit));
171 if(verbose>2) printf("existing calibration unit := %d\n", img.unit);
172
173 if(newunit==NULL) {
174 if(verbose>2) printf("print the unit and do nothing else, as requested\n");
175 fprintf(stdout, "# filename := %s\n", petfile);
176 fprintf(stdout, "unit := %s\n", imgunit);
177 imgEmpty(&img);
178 fileNr++;
179 continue;
180 }
181
182 if(ifUnknown!=0 && img.unit!=0 && convertUnit==0) {
183 if(verbose>2) printf("unit exists already; do nothing, as requested\n");
184 if(verbose>0)
185 fprintf(stderr, "%s : unit '%s' is not replaced\n",
186 petfile, imgunit);
187 fileNr++;
188 imgEmpty(&img); continue;
189 }
190
191
192 if(convertUnit!=0) {
193 if(verbose>2) printf("convert concentrations to new units\n");
194 /* Check that there is an old unit */
195 if(img.unit==0) {
196 fprintf(stderr, "%s : cannot convert unknown unit.\n", petfile);
197 imgEmpty(&img); continue;
198 }
199 /* Try to convert it */
200 if(img.unit==imgUnitId(newunit)) {
201 if(verbose>0)
202 fprintf(stderr, "%s: unit is already %s\n", petfile, newunit);
203 imgEmpty(&img); fileNr++;
204 continue;
205 }
206 ret=imgConvertUnit(&img, newunit);
207 if(ret==0) {
208 if(verbose>0)
209 fprintf(stdout, "%s : unit '%s' converted to '%s'\n",
210 petfile, imgunit, newunit);
211 fileNr++;
212 } else {
213 fprintf(stderr, "%s: warning: cannot convert unit %s to %s\n",
214 petfile, imgunit, newunit);
215 imgEmpty(&img); continue;
216 }
217 } else {
218 if(verbose>2) printf("set the unit but keep the concentrations\n");
219 ret=imgSetUnit(&img, newunit);
220 if(ret==0) {
221 if(verbose>0)
222 fprintf(stdout, "%s : unit '%s' replaced by '%s'\n",
223 petfile, imgunit, newunit);
224 fileNr++;
225 } else {
226 fprintf(stderr, "%s: warning: cannot set unit.\n", petfile);
227 imgEmpty(&img); continue;
228 }
229 }
230
231 /* Save the modified image */
232 if(verbose>0) printf("Writing image file %s\n", petfile);
233 if(imgWrite(petfile, &img)) {
234 fprintf(stderr, "Error: %s\n", img.statmsg);
235 imgEmpty(&img); return(11);
236 }
237 imgEmpty(&img);
238
239 } /* next file */
240 if(fileNr==0) {
241 fprintf(stderr, "Error: no file(s) could be processed.\n");
242 return(3);
243 }
244
245 return(0);
246}
247/*****************************************************************************/
248
249/*****************************************************************************/
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 imgConvertUnit(IMG *img, char *unit)
Definition imgarithm.c:480
int imgRead(const char *fname, IMG *img)
Definition imgfile.c:26
int imgWrite(const char *fname, IMG *img)
Definition imgfile.c:136
int imgSetUnit(IMG *img, char *unit)
Definition imgunits.c:328
char * imgUnit(int dunit)
Definition imgunits.c:315
int imgUnitId(char *unit)
Definition imgunits.c:14
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
char unit
const char * statmsg