TPCCLIB
Loading...
Searching...
No Matches
imgformat.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 <string.h>
13#include <math.h>
14/*****************************************************************************/
15#include "tpcextensions.h"
16#include "tpcift.h"
17#include "tpccsv.h"
18#include "tpctac.h"
19#include "tpcimage.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "Convert PET image data in filename1 into filename2",
25 "in specified file format.",
26 "Data can be written only in a few formats (listed below), but more",
27 "file formats can be read.",
28 " ",
29 "Usage: @P [options] filename1 [filename2]",
30 " ",
31 "Options:",
32 " -f=<format-id>",
33 " Accepted format-id's:",
34 " FLAT - Pixel values are written to binary flat file as 32-bit",
35 " floating point values in order t,z,y,x.",
36 " NIFTI1 - NIfTI-1 single file format.",
37 " Without this option, only the format of the given image is shown.",
38 " -stdoptions", // List standard options like --help, -v, etc
39 " ",
40 "See also: tacformat, img2flat, e63to7, ecat2ana, ecat2nii",
41 " ",
42 "Keywords: image, tool, format",
43 0};
44/*****************************************************************************/
45
46/*****************************************************************************/
47/* Turn on the globbing of the command line, since it is disabled by default in
48 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
49 In Unix&Linux wildcard command line processing is enabled by default. */
50/*
51#undef _CRT_glob
52#define _CRT_glob -1
53*/
54int _dowildcard = -1;
55/*****************************************************************************/
56
57/*****************************************************************************/
61int main(int argc, char **argv)
62{
63 int ai, help=0, version=0, verbose=1;
64 int ret;
65 int new_format=IMG_FORMAT_UNKNOWN;
66 char imgfile1[FILENAME_MAX], imgfile2[FILENAME_MAX];
67
68
69 /*
70 * Get arguments
71 */
72 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
73 imgfile1[0]=imgfile2[0]=(char)0;
74 /* Options */
75 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
76 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
77 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
78 if(strncasecmp(cptr, "F=", 2)==0) {
79 new_format=imgFormatIdentify(cptr+2);
80 if(new_format!=IMG_FORMAT_UNKNOWN) continue;
81 } else if(strncasecmp(cptr, "FORMAT=", 7)==0) {
82 new_format=imgFormatIdentify(cptr+7);
83 if(new_format!=IMG_FORMAT_UNKNOWN) 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(imgfile1, argv[ai++], FILENAME_MAX);
96 if(ai<argc) strlcpy(imgfile2, argv[ai++], FILENAME_MAX);
97 if(ai<argc) {fprintf(stderr, "Error: too many arguments: '%s'.\n", argv[ai]); return(1);}
98 /* Is something missing? */
99 if(!imgfile1[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
100
101 /* In verbose mode print arguments and options */
102 if(verbose>1) {
103 printf("new_format := %s\n", imgFormatDescr(new_format));
104 printf("imgfile1 := %s\n", imgfile1);
105 if(imgfile2[0]) printf("imgfile2 := %s\n", imgfile2);
106 fflush(stdout);
107 }
108
109 TPCSTATUS status; statusInit(&status);
110 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
111 status.verbose=verbose-1;
112
113 /*
114 * Read image data
115 */
116 if(verbose>1) {printf("reading %s\n", imgfile1); fflush(stdout);}
117 IMG img; imgInit(&img);
118 ret=imgRead(&img, imgfile1, &status);
119 if(ret) { // error
120 imgFree(&img);
121 /* If format conversion was requested, then this certainly is bad */
122 if(new_format!=IMG_FORMAT_UNKNOWN) {
123 fprintf(stderr, "Error: %s (%s)\n", errorMsg(status.error), imgfile1); fflush(stderr);
124 return(2);
125 }
126 /* User just wanted to know the current format; try from file name */
127 ret=imgFormatFromFName(imgfile1, &status);
128 if(ret==IMG_FORMAT_UNKNOWN) {
129 fprintf(stderr, "Error: %s (%s)\n", errorMsg(status.error), imgfile1); fflush(stderr);
130 return(2);
131 }
132 fprintf(stdout, "%s\n", imgFormatDescr(ret)); fflush(stdout);
133 return(0);
134 }
135 if(verbose>2) imgContents(&img, stdout);
136
137 /* If new format was not specified, then just print the current format */
138 if(new_format==IMG_FORMAT_UNKNOWN) {
139 fprintf(stdout, "%s\n", imgFormatDescr(img.format)); fflush(stdout);
140 imgFree(&img);
141 return(0);
142 }
143
144 /* If output file name was not given by user, then make it here */
145 if(!imgfile2[0]) {
146 strcpy(imgfile2, imgfile1);
147 filenameRmPath(imgfile2); filenameRmExtension(imgfile2);
148 strcat(imgfile2, imgDefaultExtension(new_format));
149 if(verbose>1) {
150 printf("imgfile2 := %s\n", imgfile2);
151 }
152 }
153
154
155 /* Try to save the data in the new format */
156 if(verbose>1) {printf("writing %s\n", imgfile2); fflush(stdout);}
157 img.oformat=new_format;
158 ret=imgWrite(&img, imgfile2, &status);
159 if(ret==TPCERROR_UNSUPPORTED) {
160 fprintf(stderr, "Error: writing format %s is not supported.\n", tacFormattxt(new_format));
161 return(6);
162 }
163 if(ret!=TPCERROR_OK) {
164 fprintf(stderr, "Error: %s\n", errorMsg(status.error)); fflush(stderr);
165 return(7);
166 }
167 if(verbose>0) {printf(" %s written.\n", imgfile2); fflush(stdout);}
168
169
170 return(0);
171}
172/*****************************************************************************/
173
174/*****************************************************************************/
void filenameRmPath(char *s)
Definition filename.c:20
int filenameRmExtension(char *s)
Definition filename.c:71
void imgContents(IMG *img, FILE *fp)
Definition image.c:293
void imgFree(IMG *img)
Definition image.c:107
void imgInit(IMG *img)
Definition image.c:64
imgformat imgFormatFromFName(const char *fname, TPCSTATUS *status)
Definition imageio.c:394
int imgRead(IMG *img, const char *fname, TPCSTATUS *status)
Definition imageio.c:82
imgformat imgFormatIdentify(const char *s)
Definition imageio.c:466
int imgWrite(IMG *img, const char *fname, TPCSTATUS *status)
Definition imageio.c:214
char * imgFormatDescr(imgformat c)
Definition imageio.c:36
char * imgDefaultExtension(imgformat c)
Definition imageio.c:67
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
char * errorMsg(tpcerror e)
Definition statusmsg.c:68
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
Definition tpcimage.h:82
imgformat format
Definition tpcimage.h:103
imgformat oformat
Definition tpcimage.h:105
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
char * tacFormattxt(tacformat c)
Definition tacio.c:98
Header file for library libtpccsv.
Header file for library libtpcextensions.
@ TPCERROR_UNSUPPORTED
Unsupported file type.
@ TPCERROR_OK
No error.
Header file for library libtpcift.
Header file for libtpcimage.
@ IMG_FORMAT_UNKNOWN
Unknown format.
Definition tpcimage.h:33
Header file for library libtpctac.