TPCCLIB
Loading...
Searching...
No Matches
imgshrink.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 <math.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 "Shrink the size of a dynamic or static PET image to take less memory and",
25 "disk space, and to reduce noise and computation time when testing",
26 "pixel-by-pixel modeling methods.",
27 "Eight neighbouring pixels are averaged into one pixel; thus image size",
28 "will be shrunk into 1/8 of the size of the original image.",
29 "ECAT 6.3, 7.x, and Analyze 7.5 and NIfTI-1 formats are accepted.",
30 " ",
31 "Usage: @P [Options] image shrunkenimage",
32 " ",
33 "Options:",
34 " -z=<Yes|no>",
35 " Nr of image planes (z dimension) is halved (default) or not changed.",
36 " -stdoptions", // List standard options like --help, -v, etc
37 " ",
38 "Example:",
39 " @P i5998dy1.v i5998dy1_shrunken.v",
40 " ",
41 "See also: imgslim, e7vplavg, imgdim, esplit, imgbox, imgswell, imginteg",
42 " ",
43 "Keywords: image, compression, smoothing, cropping, software testing",
44 0};
45/*****************************************************************************/
46
47/*****************************************************************************/
48/* Turn on the globbing of the command line, since it is disabled by default in
49 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
50 In Unix&Linux wildcard command line processing is enabled by default. */
51/*
52#undef _CRT_glob
53#define _CRT_glob -1
54*/
55int _dowildcard = -1;
56/*****************************************************************************/
57
58/*****************************************************************************/
62int main(int argc, char **argv)
63{
64 int ai, help=0, version=0, verbose=1;
65 char petfile[FILENAME_MAX], outfile[FILENAME_MAX];
66 int shrink_planes=1;
67 char *cptr;
68 int ret;
69
70
71 /*
72 * Get arguments
73 */
74 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
75 petfile[0]=outfile[0]=(char)0;
76 /* Get options */
77 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
78 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
79 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
80 if(strncasecmp(cptr, "Z=", 2)==0) {
81 cptr+=2;
82 if(strncasecmp(cptr, "YES", 1)==0) {shrink_planes=1; continue;}
83 if(strncasecmp(cptr, "NO", 1)==0) {shrink_planes=0; continue;}
84 }
85 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
86 return(1);
87 } else break;
88
89 /* Print help or version? */
90 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
91 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
92 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
93
94 /* Process other arguments, starting from the first non-option */
95 if(ai<argc) {strlcpy(petfile, argv[ai++], FILENAME_MAX);}
96 if(ai<argc) {strlcpy(outfile, argv[ai++], FILENAME_MAX);}
97 if(ai<argc) {
98 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
99 return(1);
100 }
101
102 /* Did we get all the information that we need? */
103 if(!outfile[0]) {
104 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
105 return(1);
106 }
107
108 /* In verbose mode print arguments and options */
109 if(verbose>1) {
110 printf("petfile := %s\n", petfile);
111 printf("outfile := %s\n", outfile);
112 printf("shrink_planes := %d\n", shrink_planes);
113 }
114 if(verbose>9) IMG_TEST=verbose-10; else IMG_TEST=0;
115
116
117 /*
118 * Read image
119 */
120 if(verbose>0) fprintf(stdout, "reading image %s\n", petfile);
121 IMG img; imgInit(&img);
122 ret=imgRead(petfile, &img);
123 if(ret) {
124 fprintf(stderr, "Error: %s\n", img.statmsg); if(verbose>1) imgInfo(&img);
125 return(2);
126 }
127 if(verbose>1) {
128 printf("image dimensions: %d %d %d\n", img.dimz, img.dimy, img.dimx);
129 printf("image frame nr: %d\n", img.dimt);
130 }
131 /* Check if PET data is raw or image */
132 if(img.type!=IMG_TYPE_IMAGE) {
133 fprintf(stderr, "Error: %s is not an image.\n", petfile);
134 imgEmpty(&img); return(2);
135 }
136 if(imgNaNs(&img, 1)>0)
137 if(verbose>0) fprintf(stderr, "Warning: missing pixel values.\n");
138
139
140 /*
141 * Shrink the image
142 */
143 if(verbose>0) fprintf(stdout, "shrinking...\n");
144 IMG out; imgInit(&out);
145 ret=imgShrink(&img, &out, shrink_planes);
146 if(ret) {
147 fprintf(stderr, "Error: cannot shrink image.\n");
148 if(verbose>1) printf("ret=%d\n", ret);
149 imgEmpty(&img); imgEmpty(&out); return(3);
150 }
151
152 /* Free memory allocated for original image */
153 imgEmpty(&img);
154
155
156 /*
157 * Save the shrunk image
158 */
159 if(verbose>1) fprintf(stdout, "writing shrunken image in %s\n", outfile);
160 ret=imgWrite(outfile, &out);
161 if(ret) {
162 fprintf(stderr, "Error: %s\n", out.statmsg);
163 imgEmpty(&out); return(11);
164 }
165 if(verbose>0) fprintf(stdout, "Shrunk image was written in %s\n", outfile);
166
167 /* Free memory allocated for output image */
168 imgEmpty(&out);
169
170 return(0);
171}
172/*****************************************************************************/
173
174/*****************************************************************************/
int IMG_TEST
Definition img.c:6
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 imgRead(const char *fname, IMG *img)
Definition imgfile.c:26
int imgWrite(const char *fname, IMG *img)
Definition imgfile.c:136
int imgShrink(IMG *img1, IMG *img2, int doz)
Header file for libtpcimgio.
#define IMG_TYPE_IMAGE
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
char type
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy
const char * statmsg