TPCCLIB
Loading...
Searching...
No Matches
img2tif.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/*****************************************************************************/
18#include "libtpcmisc.h"
19#include "libtpcimgio.h"
20#include "libtpcimgp.h"
21/*****************************************************************************/
22
23/*****************************************************************************/
24static char *info[] = {
25 "Extract the matrices of an ECAT 6.3 or ECAT 7 image or sinogram, or",
26 "NIfTI, Analyze 7.5, or microPET image, to TIFF 6.0 image.",
27 " ",
28 "Usage: @P [Options] imgfile [tiffile]",
29 " ",
30 "Options:",
31 " -p=<Plane>",
32 " A specified image plane is extracted; by default all planes.",
33 " -f=<Frame>",
34 " A specified image frame is extracted; by default all frames.",
35 " -s[=Value]",
36 " Color scale of output images is fixed from 0 to max of all",
37 " image matrices, or from 0 to Value.",
38 " -L",
39 " Log10 transform.",
40 " -L1",
41 " Log10 transform, adding 1 to pixel values before transform.",
42 " -rb",
43 " Apply rainbow colorscale instead of default grayscale.",
44 " -rbw",
45 " Apply rainbow colorscale with white background.",
46 " -gr",
47 " Apply grayscale (0=black, highest=white).",
48 " -gi",
49 " Apply inverse grayscale; default.",
50 " -th=<nr>",
51 " Number of matrices tiled horizontally; set to a large value to draw",
52 " all matrices in one row.",
53 " -tv=<nr>",
54 " Number of matrices tiled vertically; set to a large value to draw",
55 " all matrices in one column.",
56 " -stdoptions", // List standard options like --help, -v, etc
57 " ",
58 "Example 1: Make TIFF of plane 8 and frame 17, which is yet scaled to",
59 "the level of whole image maximum:",
60 " @P -s -p=8 -f=17 s2345dy1.v s2345_pl08_fr17.tif",
61 "Example 2: Make TIFF of all image matrices in rainbow color scale:",
62 " @P -rb s2345sum.img s2345sum.tif",
63 "Example 3: Make TIFF of all image matrices, scaling the colors to value 3:",
64 " @P -S=3 a3456bp.v a3456bp.tif",
65 " ",
66 "See also: imgslice, imgmax, imgthrs, imgunit, img2flat",
67 " ",
68 "Keywords: image, sinogram, TIFF",
69 0};
70/*****************************************************************************/
71
72/*****************************************************************************/
73/* Turn on the globbing of the command line, since it is disabled by default in
74 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
75 In Unix&Linux wildcard command line processing is enabled by default. */
76/*
77#undef _CRT_glob
78#define _CRT_glob -1
79*/
80int _dowildcard = -1;
81/*****************************************************************************/
82
83/*****************************************************************************/
87int main(int argc, char **argv)
88{
89 int ai, help=0, version=0, verbose=1;
90 int pi, ret, fixedScale=0, trLog10=0;
91 int frame=-1, plane=-1;
92 int colorscale=PET_GRAYSCALE_INV;
93 char ecatfile[FILENAME_MAX], tiffile[FILENAME_MAX], *cptr=NULL;
94 char tmp[256];
95 int tileX=0, tileY=0;
96 IMG img;
97 float imgmax=-1.0;
98
99
100 /*
101 * Get arguments
102 */
103 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
104 ecatfile[0]=tiffile[0]=(char)0;
105 imgInit(&img);
106 /* Options */
107 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
108 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
109 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
110 cptr=argv[ai]+1;
111 if(strncasecmp(cptr, "P=", 2)==0 && strlen(cptr)>2) {
112 plane=atoi(cptr+2); if(plane>0) continue;
113 } else if(strncasecmp(cptr, "F=", 2)==0 && strlen(cptr)>2) {
114 frame=atoi(cptr+2)-1; if(frame>=0) continue;
115 } else if(strcasecmp(cptr, "RBW")==0) {
116 colorscale=PET_RAINBOW_WB; continue;
117 } else if(strncasecmp(cptr, "R", 1)==0) {
118 colorscale=PET_RAINBOW; continue;
119 } else if(strncasecmp(cptr, "GR", 2)==0) {
120 colorscale=PET_GRAYSCALE; continue;
121 } else if(strncasecmp(cptr, "GI", 2)==0) {
122 colorscale=PET_GRAYSCALE_INV; continue;
123 } else if(strcasecmp(cptr, "S")==0) {
124 fixedScale=1; continue;
125 } else if(strncasecmp(cptr, "S=", 2)==0) {
126 fixedScale=1; imgmax=atof_dpi(cptr+2); if(imgmax>0.0) continue;
127 } else if(strcasecmp(cptr, "L")==0) {
128 trLog10=1; continue;
129 } else if(strcasecmp(cptr, "L1")==0) {
130 trLog10=2; continue;
131 } else if(strncasecmp(cptr, "TH=", 3)==0 && strlen(cptr)>3) {
132 tileX=atoi(cptr+3); if(tileX>0) continue;
133 } else if(strncasecmp(cptr, "TV=", 3)==0 && strlen(cptr)>3) {
134 tileY=atoi(cptr+3); if(tileY>0) continue;
135 }
136 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
137 return(1);
138 } else break;
139
140 /* Print help or version? */
141 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
142 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
143 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
144
145 /* Arguments */
146 for(; ai<argc; ai++) {
147 if(!ecatfile[0]) {
148 strcpy(ecatfile, argv[ai]); continue;
149 } else if(!tiffile[0]) {
150 strcpy(tiffile, argv[ai]); continue;
151 }
152 fprintf(stderr, "Error: invalid argument '%s'\n", argv[ai]);
153 return(1);
154 }
155
156 /* Is something missing? */
157 if(!ecatfile[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
158
159 /* If TIFF filename was not given, then make it from image filename */
160 if(!tiffile[0]) {
161 strcpy(tiffile, ecatfile);
162 cptr=strrchr(tiffile, '.'); if(cptr!=NULL) *cptr=(char)0;
163 strcat(tiffile, ".tif");
164 }
165 if(strcasecmp(ecatfile, tiffile)==0) {
166 fprintf(stderr, "Error: same name for input and output file.\n");
167 return(1);
168 }
169
170
171 /* In verbose mode print arguments and options */
172 if(verbose>1) {
173 for(ai=0; ai<argc; ai++)
174 printf("%s ", argv[ai]);
175 printf("\n");
176 printf("ecatfile := %s\n", ecatfile);
177 printf("tiffile := %s\n", tiffile);
178 printf("plane := %d\nframe := %d\nfixedScale := %d\nimgmax := %g\n",
179 plane, 1+frame, fixedScale, imgmax);
180 printf("Log10 transformation := %d\n", trLog10);
181 if(tileX>0) printf("tileX := %d\n", tileX);
182 if(tileY>0) printf("tileY := %d\n", tileY);
183 }
184
185
186 /*
187 * Read image file
188 */
189 if(verbose>1) fprintf(stdout, "reading %s\n", ecatfile);
190 ret=imgRead(ecatfile, &img);
191 if(ret) {
192 fprintf(stderr, "Error: %s\n", img.statmsg);
193 if(verbose>1) printf("ret := %d\n", ret);
194 return(2);
195 }
196 if(verbose>9) imgInfo(&img);
197 if(imgNaNs(&img, 1)>0)
198 if(verbose>0) fprintf(stderr, "Warning: missing pixel values.\n");
199
200
201 /*
202 * Switch the plane number to the order nr
203 * Check the frame number
204 */
205 if(frame>=0 && img.dimt<(frame+1)) {
206 fprintf(stderr, "Error: file does not contain frame %d\n", frame+1);
207 imgEmpty(&img); return(3);
208 }
209 if(plane>0) {
210 for(pi=0; pi<img.dimz; pi++)
211 if(img.planeNumber[pi]==plane) {plane=pi; break;}
212 if(pi==img.dimz) {
213 fprintf(stderr, "Error: file does not contain plane %d\n", plane);
214 imgEmpty(&img); return(3);
215 }
216 if(verbose>1)
217 printf("Required plane was on index %d in the datafile.\n", plane);
218 }
219
220
221 /*
222 * Make log10 transformation if required
223 */
224 if(trLog10>0) {
225 if(verbose>0) fprintf(stdout, "computing logarithms.\n");
226 if(trLog10>1) ret=imgArithmConst(&img, 1.0, '+', -1.0, verbose-3);
227 ret=imgLog10(&img);
228 }
229
230
231 /*
232 * If fixed color scale was required, then search the max of all image.
233 * Unless, of course, max was specified by user.
234 */
235 if(fixedScale && imgmax<=0.0) {
236 ret=imgMax(&img, &imgmax);
237 if(verbose>0) fprintf(stdout, "maximum pixel value in %s is %g, unit %s\n",
238 ecatfile, imgmax, imgUnit(img.unit) );
239 }
240
241
242 /*
243 * Make TIFF files from required matrices
244 */
245 if(fixedScale==0) imgmax=-1.0;
246 ret=tiffWriteImg(&img, plane, frame, &imgmax, colorscale, tiffile,
247 tileX, tileY, verbose-2, tmp);
248 if(ret) {
249 fprintf(stderr, "Error (%d) in writing %s: %s.\n", ret, tiffile, tmp);
250 imgEmpty(&img);
251 return(7);
252 }
253 if(fixedScale==0) {
254 if(verbose>0)
255 fprintf(stdout, "maximum pixel value in matrix is %g, unit %s\n",
256 imgmax, imgUnit(img.unit) );
257 if(verbose>1) printf("img.unit := %d\n", img.unit);
258 } else if(verbose>1) printf("imgmax := %g\n", imgmax);
259 if(verbose>0) printf("%s written.\n", tiffile);
260
261 imgEmpty(&img);
262 return(0);
263}
264/*****************************************************************************/
265
266/*****************************************************************************/
double atof_dpi(char *str)
Definition decpoint.c:59
void imgInfo(IMG *image)
Definition img.c:359
unsigned long long imgNaNs(IMG *img, int fix)
Definition img.c:658
void imgEmpty(IMG *image)
Definition img.c:121
void imgInit(IMG *image)
Definition img.c:60
int imgLog10(IMG *img)
Definition imgarithm.c:270
int imgArithmConst(IMG *img, float operand, char operation, float ulimit, int verbose)
Definition imgarithm.c:100
int imgRead(const char *fname, IMG *img)
Definition imgfile.c:26
int imgMax(IMG *img, float *maxvalue)
Definition imgminmax.c:15
int tiffWriteImg(IMG *img, int plane, int frame, float *maxvalue, int colorscale, char *fname, int matXdim, int matYdim, int verbose, char *status)
Definition imgtiff.c:15
char * imgUnit(int dunit)
Definition imgunits.c:315
Header file for libtpcimgio.
Header file for libtpcimgp.
#define PET_RAINBOW_WB
Definition libtpcimgp.h:38
#define PET_GRAYSCALE_INV
Definition libtpcimgp.h:34
#define PET_RAINBOW
Definition libtpcimgp.h:36
#define PET_GRAYSCALE
Definition libtpcimgp.h:32
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
unsigned short int dimt
int * planeNumber
unsigned short int dimz
const char * statmsg