TPCCLIB
Loading...
Searching...
No Matches
dcmlhdr.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 <time.h>
14#include <string.h>
15/*****************************************************************************/
16#include "tpcextensions.h"
17#include "tpcdcm.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
21static char *info[] = {
22 "List the header contents of a DICOM file.",
23 " ",
24 "Usage: @P [options] dicomfile [> outputfile]",
25 " ",
26 "Options:",
27 " -group=<tag group>",
28 " List only contents with specified tag group.",
29 " -element=<tag element>",
30 " List only contents with specified tag element.",
31 " -tag=<Y|n>",
32 " List (y, default) or do not list (n) the element tag.",
33 " -tagname=<Y|n>",
34 " List (y, default) or do not list (n) the tag description.",
35 " -VR=<y|N>",
36 " List (y) or do not list (n, default) the element VR.",
37 " -VL=<y|N>",
38 " List (y) or do not list (n, default) the element VL.",
39 " -stdoptions", // List standard options like --help, -v, etc
40 " ",
41 "Example:",
42 " @P -group=0002 -element=0010 f1.dcm",
43 " ",
44 "See also: dcmdict, dcmframe, dcmmlist, dcmxform, lmhdr, ana_lhdr, iftlist",
45 " ",
46 "Keywords: DICOM, header",
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 char *cptr;
69 int group=-1, element=-1;
70 int listVR=0; // 0=no, 1=yes
71 int listVL=0; // 0=no, 1=yes
72 int listTag=1; // 0=no, 1=yes
73 int listTagname=1; // 0=no, 1=yes
74 int listValue=1; // 0=no, 1=yes
75 char dcmfile[FILENAME_MAX];
76
77
78 /*
79 * Get arguments
80 */
81 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
82 dcmfile[0]=(char)0;
83 /* Options */
84 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
85 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
86 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
87 if(strncasecmp(cptr, "GROUP=", 6)==0) {
88 unsigned int ui=0;
89 if(sscanf(cptr+6, "%x", &ui)==1) {group=(int)ui; continue;}
90 } else if(strncasecmp(cptr, "ELEMENT=", 8)==0) {
91 unsigned int ui=0;
92 if(sscanf(cptr+8, "%x", &ui)==1) {element=(int)ui; continue;}
93 } else if(strcasecmp(cptr, "VR")==0) {
94 listVR=1; continue;
95 } else if(strncasecmp(cptr, "VR=", 3)==0) {
96 if((listVR=tpcYesNo(cptr+3))>=0) continue;
97 } else if(strncasecmp(cptr, "VL=", 3)==0) {
98 if((listVL=tpcYesNo(cptr+3))>=0) continue;
99 } else if(strncasecmp(cptr, "TAG=", 4)==0) {
100 if((listTag=tpcYesNo(cptr+4))>=0) continue;
101 } else if(strncasecmp(cptr, "TAGNAME=", 8)==0) {
102 if((listTagname=tpcYesNo(cptr+8))>=0) continue;
103 } else if(strncasecmp(cptr, "VALUE=", 6)==0) {
104 if((listValue=tpcYesNo(cptr+6))>=0) continue;
105 }
106 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
107 return(1);
108 } else break;
109
110 TPCSTATUS status; statusInit(&status);
111 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
112 status.verbose=verbose-3;
113
114 /* Print help or version? */
115 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
116 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
117 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
118
119 /* Process other arguments, starting from the first non-option */
120 if(ai<argc) strlcpy(dcmfile, argv[ai++], FILENAME_MAX);
121 if(ai<argc) {
122 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
123 return(1);
124 }
125 /* Did we get all the information that we need? */
126 if(!dcmfile[0]) {
127 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
128 return(1);
129 }
130
131 /* In verbose mode print arguments and options */
132 if(verbose>1) {
133 printf("dcmfile := %s\n", dcmfile);
134 printf("tag := (");
135 if(group<0) printf("0xXXXX,");
136 else printf("0x%04x,", (unsigned short int)group);
137 if(element<0) printf("0xXXXX)\n");
138 else printf("0x%04x)\n", (unsigned short int)element);
139 printf("listVR := %d\n", listVR);
140 printf("listVL := %d\n", listVL);
141 printf("listTag := %d\n", listTag);
142 printf("listTagname := %d\n", listTagname);
143 printf("listValue := %d\n", listValue);
144 }
145
146 /* Check that we're requested to print something */
147 if(!listVR && !listVL && !listTag && !listTagname && !listValue) {
148 fprintf(stderr, "Error: all prints were turned off.\n");
149 return(1);
150 }
151
152 /*
153 * Read file
154 */
155 DCMFILE dcm; dcmfileInit(&dcm);
156 int ret;
157 ret=dcmFileRead(dcmfile, &dcm, 1, &status);
158 if(ret!=TPCERROR_OK) {
159 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
160 dcmfileFree(&dcm); return(2);
161 }
162 if(verbose>0) {
163 printf("max_tree_depth := %u\n", dcmfileMaxDepth(&dcm));
164 }
165
166 /* List the contents */
167 unsigned int itemNr=0;
168 unsigned int listedNr=0;
169 DCMITEM *d1=dcm.item;
170 while(d1!=NULL) {
171 unsigned int parent1Shown=0;
172 /* If user specified tag group and/or element, then check the match */
173 if((group<0 || group==d1->tag.group) &&
174 (element<0 || element==d1->tag.element))
175 {
176 /* Print the tag */
177 if(listTag) printf("(%04x,%04x) ", d1->tag.group, d1->tag.element);
178 /* Print the tag description */
179 if(listTagname) printf("%s ", dcmDictIndexDescr(dcmDictFindTag(&d1->tag)));
180 /* Print the equal sign for IFT format */
181 if((listTag || listTagname) && (listVR || listVL || listValue)) printf(":= ");
182 /* Print the VR */
183 if(listVR) printf("%s ", dcmVRName(d1->vr));
184 /* Print the VL */
185 if(listVL && d1->vl!=0xFFFFFFFF) printf("%u ", d1->vl);
186 /* Print the value */
187 if(listValue) {
188 char *buf=dcmValueString(d1); printf("%s", buf); free(buf);
189 }
190 /* Print the linefeed */
191 printf("\n");
192 parent1Shown=1;
193 }
194 /* If this element has children, then print those */
195 if(d1->child_item!=NULL) {
196 DCMITEM *d2=d1->child_item;
197 while(d2!=NULL) {
198 unsigned int parent2Shown=0;
199 /* If user specified tag group and/or element, then check the match */
200 if((group<0 || group==d2->tag.group) &&
201 (element<0 || element==d2->tag.element))
202 {
203 /* Print parent information if not yet printed */
204 if(!parent1Shown) {
205 printf("(%04x,%04x) ", d1->tag.group, d1->tag.element);
206 printf("%s ", dcmDictIndexDescr(dcmDictFindTag(&d1->tag)));
207 printf("\n");
208 parent1Shown=1;
209 }
210 /* Print the indentation */
211 printf(" ");
212 /* Print the tag */
213 if(listTag) printf("(%04x,%04x) ", d2->tag.group, d2->tag.element);
214 /* Print the tag description */
215 if(listTagname) printf("%s ", dcmDictIndexDescr(dcmDictFindTag(&d2->tag)));
216 /* Print the equal sign for IFT format */
217 if((listTag || listTagname) && (listVR || listVL || listValue)) printf(":= ");
218 /* Print the VR */
219 if(listVR) printf("%s ", dcmVRName(d2->vr));
220 /* Print the VL */
221 if(listVL && d2->vl!=0xFFFFFFFF) printf("%u ", d2->vl);
222 /* Print the value */
223 if(listValue) {
224 char *buf=dcmValueString(d2); printf("%s", buf); free(buf);
225 }
226 /* Print the linefeed */
227 printf("\n");
228 parent2Shown=1;
229 }
230 /* If this element has children, then print those */
231 if(d2->child_item!=NULL) {
232 DCMITEM *d3=d2->child_item;
233 while(d3!=NULL) {
234 /* If user specified tag group and/or element, then check the match */
235 if((group>=0 && group!=d3->tag.group) ||
236 (element>=0 && element!=d3->tag.element))
237 {
238 d3=d3->next_item; itemNr++; continue;
239 }
240 /* Print parent information if not yet printed */
241 if(!parent1Shown) {
242 printf("(%04x,%04x) ", d1->tag.group, d1->tag.element);
243 printf("%s ", dcmDictIndexDescr(dcmDictFindTag(&d1->tag)));
244 printf("\n");
245 parent1Shown=1;
246 }
247 if(!parent2Shown) {
248 printf(" (%04x,%04x) ", d2->tag.group, d2->tag.element);
249 printf("%s ", dcmDictIndexDescr(dcmDictFindTag(&d2->tag)));
250 printf("\n");
251 parent2Shown=1;
252 }
253 /* Print the indentation */
254 printf(" ");
255 /* Print the tag */
256 if(listTag) printf("(%04x,%04x) ", d3->tag.group, d3->tag.element);
257 /* Print the tag description */
258 if(listTagname) printf("%s ", dcmDictIndexDescr(dcmDictFindTag(&d3->tag)));
259 /* Print the equal sign for IFT format */
260 if((listTag || listTagname) && (listVR || listValue)) printf(":= ");
261 /* Print the VR */
262 if(listVR) printf("%s ", dcmVRName(d3->vr));
263 /* Print the VL */
264 if(listVL && d3->vl!=0xFFFFFFFF) printf("%u ", d3->vl);
265 /* Print the value */
266 if(listValue) {
267 char *buf=dcmValueString(d3); printf("%s", buf); free(buf);
268 }
269 /* Print the linefeed */
270 printf("\n");
271 d3=d3->next_item;
272 itemNr++; listedNr++;
273 }
274 }
275 d2=d2->next_item;
276 itemNr++; listedNr++;
277 }
278 }
279 d1=d1->next_item;
280 itemNr++; listedNr++;
281 }
282 dcmfileFree(&dcm);
283
284
285 if(verbose>1) {
286 printf("elementNr := %d\n", itemNr);
287 if(listedNr!=itemNr || verbose>2)
288 printf("selected_tags := %d\n", listedNr);
289 }
290 if(itemNr==0) {
291 fprintf(stderr, "Error: invalid header in %s\n", dcmfile);
292 return(3);
293 }
294 if(listedNr==0) {
295 fprintf(stderr, "Error: required tags not found.\n");
296 return(4);
297 }
298
299 return(0);
300}
301/*****************************************************************************/
302
303/*****************************************************************************/
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
char * dcmValueString(DCMITEM *d)
Definition dcmdata.c:141
char * dcmDictIndexDescr(unsigned int i)
unsigned int dcmDictFindTag(DCMTAG *tag)
int dcmFileRead(const char *filename, DCMFILE *dcm, const short int headerOnly, TPCSTATUS *status)
Definition dcmio.c:768
char * dcmVRName(dcmvr id)
Definition dcmvr.c:126
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
DCMITEM * item
Definition tpcdcm.h:164
dcmvr vr
Definition tpcdcm.h:139
struct DCMITEM * child_item
Definition tpcdcm.h:143
struct DCMITEM * next_item
Definition tpcdcm.h:147
unsigned int vl
Definition tpcdcm.h:141
DCMTAG tag
Definition tpcdcm.h:137
unsigned short int element
Definition tpcdcm.h:44
unsigned short int group
Definition tpcdcm.h:42
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.