TPCCLIB
Loading...
Searching...
No Matches
imgweigh.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 <unistd.h>
15#include <string.h>
16#include <ctype.h>
17#include <math.h>
18#include <time.h>
19/*****************************************************************************/
20#include "libtpcmisc.h"
21#include "libtpcimgio.h"
22#include "libtpcimgp.h"
23/*****************************************************************************/
24
25/*****************************************************************************/
26static char *info[] = {
27 "Constructs a Scan Information File (SIF) from specified dynamic image",
28 "in ECAT, Analyze, or NIfTI format, or sinogram in ECAT format,",
29 "for providing weighting information to modelling software.",
30 "SIF can also be used with Analyze and NIfTI image formats to retain",
31 "the frame time information.",
32 "Note that the count values in the resulting SIF file are not the same as",
33 "the values in SIF file retrieved from the scanner data, but similar",
34 "enough for usual purposes.",
35 " ",
36 "Usage: @P [-Options] petfile sif",
37 " ",
38 "Options:",
39 " -stdoptions", // List standard options like --help, -v, etc
40 " ",
41 "Example:",
42 " @P ub7761dy1.v ub7761dy1.sif",
43 " ",
44 "See also: eframe, tacweigh, imgthrs, imghead, tacframe, simframe",
45 " ",
46 "Keywords: image, SIF, weighting, time, ECAT, NIfTI, Analyze",
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;
69 char *cptr, petfile[FILENAME_MAX], siffile[FILENAME_MAX];
70
71 /*
72 * Get arguments
73 */
74 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
75 petfile[0]=siffile[0]=(char)0;
76 /* Options */
77 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
78 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
79 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
80 /* We should not be here */
81 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
82 return(1);
83 } else break;
84
85 /* Print help or version? */
86 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
87 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
88 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
89
90 /* Process other arguments, starting from the first non-option */
91 if(ai<argc) {strlcpy(petfile, argv[ai], FILENAME_MAX); ai++;}
92 if(ai<argc) {strlcpy(siffile, argv[ai], FILENAME_MAX); ai++;}
93 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
94
95 /* Is something missing or wrong? */
96 if(!siffile[0]) {
97 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
98 return(1);
99 }
100 if(strcasecmp(petfile, siffile)==0) {
101 fprintf(stderr, "Error: check the output filename.\n");
102 return(1);
103 }
104
105
106 /* In verbose mode print arguments and options */
107 if(verbose>1) {
108 printf("petfile := %s\n", petfile);
109 printf("siffile := %s\n", siffile);
110 }
111 if(verbose>3) IMG_TEST=verbose-3; else IMG_TEST=0;
112 if(verbose>5) {ECAT7_TEST=ECAT63_TEST=verbose>5;}
113
114
115
116 /* Read the PET image or sinogram */
117 IMG img; imgInit(&img);
118 if(verbose>0) printf("reading PET data\n");
119 ret=imgRead(petfile, &img);
120 if(ret) {
121 fprintf(stderr, "Error: %s\n", imgStatus(ret));
122 if(verbose>0) imgInfo(&img);
123 imgEmpty(&img); return(2);
124 }
125 if(verbose>1) printf("frame_nr := %d\n", img.dimt);
126
127 /* check the file type */
128 if(img.type!=IMG_TYPE_IMAGE && img.type!=IMG_TYPE_RAW) {
129 fprintf(stderr, "Error: %s is not an image or scan file.\n", petfile);
130 imgEmpty(&img);
131 return(2);
132 }
133
134 /* Compute the SIF data */
135 SIF sif; sifInit(&sif);
136 if(verbose>0) printf("computing SIF\n");
137 ret=img2sif(&img, &sif, 1, 1, 2, 2);
138 if(ret) {
139 fprintf(stderr, "Error: cannot create SIF from %s.\n", petfile);
140 imgEmpty(&img); sifEmpty(&sif); return(3);
141 }
142 if(verbose>2) sifPrint(&sif);
143
144 // Close the image
145 imgEmpty(&img);
146
147 /*
148 * Save the SIF file
149 */
150 if(verbose>1) printf("writing SIF\n");
151 if(sifWrite(&sif, siffile)) {
152 fprintf(stderr, "Error in writing '%s': %s\n", siffile, siferrmsg);
153 sifEmpty(&sif); return(11);
154 }
155 if(verbose>0) fprintf(stderr, "SIF data written in %s\n", siffile);
156
157 /* Prepare to quit */
158 sifEmpty(&sif);
159
160 return(0);
161}
162/*****************************************************************************/
163
164/*****************************************************************************/
int ECAT63_TEST
Definition ecat63h.c:6
int ECAT7_TEST
Definition ecat7h.c:6
int IMG_TEST
Definition img.c:6
void imgInfo(IMG *image)
Definition img.c:359
char * imgStatus(int status_index)
Definition img.c:330
void imgEmpty(IMG *image)
Definition img.c:121
void imgInit(IMG *image)
Definition img.c:60
int img2sif(IMG *img, SIF *sif, int copy_header, int copy_frames, int copy_counts, int verbose)
Definition img_sif.c:71
int imgRead(const char *fname, IMG *img)
Definition imgfile.c:26
Header file for libtpcimgio.
#define IMG_TYPE_RAW
int sifWrite(SIF *data, char *filename)
Definition sifio.c:145
void sifInit(SIF *data)
Definition sif.c:17
void sifPrint(SIF *data)
Definition sifio.c:234
void sifEmpty(SIF *data)
Definition sif.c:33
#define IMG_TYPE_IMAGE
char siferrmsg[128]
Definition sif.c:7
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 type
unsigned short int dimt