TPCCLIB
Loading...
Searching...
No Matches
imginv.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 "Invert the pixel values in image file, that is, calculates 1/y.",
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 "If name for output file is not given, then the original image is overwritten.",
30 "Pixel values that are negative or close to zero will be set to zero.",
31 " ",
32 "Options:",
33 " -stdoptions", // List standard options like --help, -v, etc
34 " ",
35 "See also: tacinv, imgcalc, imgthrs, imgpeak, imgflip",
36 " ",
37 "Keywords: image, tool",
38 0};
39/*****************************************************************************/
40
41/*****************************************************************************/
42/* Turn on the globbing of the command line, since it is disabled by default in
43 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
44 In Unix&Linux wildcard command line processing is enabled by default. */
45/*
46#undef _CRT_glob
47#define _CRT_glob -1
48*/
49int _dowildcard = -1;
50/*****************************************************************************/
51
52/*****************************************************************************/
56int main(int argc, char **argv)
57{
58 int ai, help=0, version=0, verbose=1;
59 char imgfile[FILENAME_MAX], outfile[FILENAME_MAX];
60
61
62 /*
63 * Get arguments
64 */
65 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
66 imgfile[0]=outfile[0]=(char)0;
67 /* Options */
68 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
69 //char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
70 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
71 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
72 return(1);
73 } else break;
74
75 /* Print help or version? */
76 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
77 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
78 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
79
80 /* Process other arguments, starting from the first non-option */
81 if(ai<argc) {strlcpy(imgfile, argv[ai], FILENAME_MAX); ai++;}
82 if(ai<argc) {strlcpy(outfile, argv[ai], FILENAME_MAX); ai++;}
83 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
84
85 /* Did we get all the information that we need? */
86 if(!imgfile[0]) {
87 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
88 return(1);
89 }
90 if(!outfile[0]) strcpy(outfile, imgfile);
91
92
93 /* In verbose mode print options */
94 if(verbose>1) {
95 printf("imgfile := %s\n", imgfile);
96 printf("outfile := %s\n", outfile);
97 }
98
99
100 /*
101 * Read PET image
102 */
103 if(verbose>0) printf("reading %s\n", imgfile);
104 IMG img; imgInit(&img);
105 if(imgRead(imgfile, &img)) {
106 fprintf(stderr, "Error: %s\n", img.statmsg);
107 return(2);
108 }
109 if(verbose>1) {
110 printf("dimt := %d\n", img.dimt);
111 printf("dimx := %d\n", img.dimx);
112 printf("dimy := %d\n", img.dimy);
113 printf("dimz := %d\n", img.dimz);
114 }
115
116
117 /*
118 * Process
119 */
120 if(verbose>0) printf("processing...\n");
121 if(imgInv(&img)) {
122 fprintf(stderr, "Error: cannot inverse pixel values.\n");
123 imgEmpty(&img); return(3);
124 }
125
126
127 /*
128 * Save the modified image
129 */
130 if(verbose>0) printf("writing image %s\n", outfile);
131 if(imgWrite(outfile, &img)) {
132 fprintf(stderr, "Error: %s\n", img.statmsg);
133 imgEmpty(&img); return(11);
134 }
135 imgEmpty(&img);
136 if(verbose>0) printf("done.\n\n");
137
138 return(0);
139}
140/*****************************************************************************/
141
142/*****************************************************************************/
void imgEmpty(IMG *image)
Definition img.c:121
void imgInit(IMG *image)
Definition img.c:60
int imgInv(IMG *img)
Definition imgarithm.c:314
int imgRead(const char *fname, IMG *img)
Definition imgfile.c:26
int imgWrite(const char *fname, IMG *img)
Definition imgfile.c:136
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