TPCCLIB
Loading...
Searching...
No Matches
dcmxform.c
1
8/*****************************************************************************/
9#include "tpcclibConfig.h"
10/*****************************************************************************/
11#include <stdio.h>
12#include <stdlib.h>
13#include <math.h>
14#include <time.h>
15#include <string.h>
16/*****************************************************************************/
17#include "tpcextensions.h"
18#include "tpctac.h"
19#include "tpcdcm.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "List the XFORM matrix from DICOM.",
25 " ",
26 "NOT for production use!",
27 " ",
28 "Usage: @P [-Options] dicomfile",
29 " ",
30 "Options:",
31 " -xform=<Y|n>",
32 " List (y, default) or do not list (n) the xform matrix.",
33 " -quatern=<y|N>",
34 " List (y) or do not list (n, default) the NIfTI quatern parameters.",
35 " -stdoptions", // List standard options like --help, -v, etc
36 " ",
37 "See also: dcmlhdr, dcmframe, dcmmlist",
38 " ",
39 "Keywords: image, DICOM",
40 0};
41/*****************************************************************************/
42
43/*****************************************************************************/
44/* Turn on the globbing of the command line, since it is disabled by default in
45 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
46 In Unix&Linux wildcard command line processing is enabled by default. */
47/*
48#undef _CRT_glob
49#define _CRT_glob -1
50*/
51int _dowildcard = -1;
52/*****************************************************************************/
53
54/*****************************************************************************/
58int main(int argc, char **argv)
59{
60 int ai, help=0, version=0, verbose=1;
61 char dcmfile[FILENAME_MAX];
62 int listXform=1; // 0=no, 1=yes
63 int listQuatern=0; // 0=no, 1=yes
64 int ret;
65
66
67 /*
68 * Get arguments
69 */
70 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
71 dcmfile[0]=(char)0;
72 /* Options */
73 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
74 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
75 char *cptr; cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
76 if(strncasecmp(cptr, "XFORM=", 6)==0) {
77 if((listXform=tpcYesNo(cptr+6))>=0) continue;
78 } else if(strncasecmp(cptr, "QUATERN=", 8)==0) {
79 if((listQuatern=tpcYesNo(cptr+8))>=0) continue;
80 }
81 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
82 return(1);
83 } else break;
84
85 TPCSTATUS status; statusInit(&status);
86 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
87 status.verbose=verbose-3;
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(dcmfile, argv[ai++], FILENAME_MAX);
96 if(ai<argc) {
97 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
98 return(1);
99 }
100 /* Did we get all the information that we need? */
101 if(!dcmfile[0]) {
102 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
103 return(1);
104 }
105
106 /* In verbose mode print arguments and options */
107 if(verbose>1) {
108 printf("dcmfile := %s\n", dcmfile);
109 printf("listXform := %d\n", listXform);
110 printf("listQuatern := %d\n", listQuatern);
111 }
112
113 /*
114 * Read file
115 */
116 DCMFILE dcm; dcmfileInit(&dcm);
117 ret=dcmFileRead(dcmfile, &dcm, 1, &status);
118 if(ret!=TPCERROR_OK) {
119 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
120 dcmfileFree(&dcm); return(2);
121 }
122 if(verbose>3) {printf("max_tree_depth := %u\n", dcmfileMaxDepth(&dcm)); fflush(stdout);}
123
124 /* ... and necessary DICOM parameters */
125 if(verbose>1) {printf("reading Image Orientation (Patient)\n"); fflush(stdout);}
126 double iop[6];
127 ret=dcmImgOrient(&dcm, iop, verbose-3);
128 if(ret!=0) {
129 fprintf(stderr, "Error: cannot read Image Orientation (Patient).\n");
130 dcmfileFree(&dcm); return(3);
131 }
132 if(verbose>1) {printf("reading Image Position (Patient)\n"); fflush(stdout);}
133 double ipp[3];
134 ret=dcmImgPos(&dcm, ipp, verbose-3);
135 if(ret!=0) {
136 fprintf(stderr, "Error: cannot read Image Position (Patient).\n");
137 dcmfileFree(&dcm); return(4);
138 }
139 if(verbose>1) {printf("reading Voxel sizes\n"); fflush(stdout);}
140 double pxlsize[3];
141 ret=dcmImgPxlsize(&dcm, pxlsize, verbose-3);
142 if(ret!=0) {
143 fprintf(stderr, "Error: cannot find voxel size.\n");
144 dcmfileFree(&dcm); return(5);
145 }
146 dcmfileFree(&dcm);
147 if(verbose>2) {
148 printf("imgorient := %g", iop[0]); for(int i=1; i<6; i++) printf(", %g", iop[i]);
149 printf("\n");
150 printf("imgpos := %g", ipp[0]); for(int i=1; i<3; i++) printf(", %g", ipp[i]);
151 printf("\n");
152 printf("pxlsize := %g", pxlsize[0]); for(int i=1; i<3; i++) printf(", %g", pxlsize[i]);
153 printf("\n");
154 }
155
156 /*
157 * Calculate XFORM matrix
158 */
159 double xform[16];
160 ret=dcmImgXform(iop, pxlsize, ipp, xform, verbose-1);
161 if(ret!=0) {
162 fprintf(stderr, "Error: cannot calculate XFORM.\n");
163 if(verbose>1) printf("dcmXform() := %d\n", ret);
164 return(7);
165 }
166 /* and print it, if requested */
167 if(listXform!=0) {
168 for(int i=0; i<4; i++) {
169 printf("%g", xform[4*i]);
170 for(int j=1; j<4; j++) printf("\t%g", xform[j+4*i]);
171 printf("\n");
172 }
173 }
174
175 /*
176 * Calculate Quatern parameters for NIfTI, if requested
177 */
178 if(listQuatern!=0) {
179 double quatern[6];
180 ret=dcmXformToQuatern(xform, quatern+0, quatern+3, verbose-3);
181 if(ret!=0) {
182 fprintf(stderr, "Error: cannot calculate quatern parameters.\n");
183 if(verbose>1) printf("dcmXformToQuatern() := %d\n", ret);
184 return(8);
185 }
186
187 printf("quatern_b := %g\n", quatern[0]);
188 printf("quatern_c := %g\n", quatern[1]);
189 printf("quatern_d := %g\n", quatern[2]);
190 printf("qoffset_x := %g\n", quatern[3]);
191 printf("qoffset_y := %g\n", quatern[4]);
192 printf("qoffset_z := %g\n", quatern[5]);
193 }
194
195 return(0);
196}
197/*****************************************************************************/
198
199/*****************************************************************************/
void dcmfileInit(DCMFILE *d)
Definition dcmdata.c:22
void dcmfileFree(DCMFILE *d)
Definition dcmdata.c:67
unsigned short int dcmfileMaxDepth(DCMFILE *df)
Definition dcmdata.c:102
int dcmImgPxlsize(DCMFILE *d, double *pxlsize, const int verbose)
Definition dcmimage.c:320
int dcmImgOrient(DCMFILE *d, double *iop, const int verbose)
Definition dcmimage.c:387
int dcmImgPos(DCMFILE *d, double *imgpos, const int verbose)
Definition dcmimage.c:121
int dcmXformToQuatern(double *xform, double *quatern, double *qoffset, const int verbose)
Definition dcmimage.c:540
int dcmImgXform(double *iop, double *xyzMM, double *imgPos, double *xform, const int verbose)
Definition dcmimage.c:431
int dcmFileRead(const char *filename, DCMFILE *dcm, const short int headerOnly, TPCSTATUS *status)
Definition dcmio.c:768
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcYesNo(const char *s)
Definition proginfo.c:459
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
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
Header file for libtpcdcm.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpctac.