TPCCLIB
Loading...
Searching...
No Matches
dcmdata.c File Reference

Processing DICOM data structs. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tpcextensions.h"
#include "tpcdcm.h"

Go to the source code of this file.

Functions

void dcmfileInit (DCMFILE *d)
 
void dcmitemFree (DCMITEM *d)
 
void dcmfileFree (DCMFILE *d)
 
unsigned short int dcmitemMaxDepth (DCMITEM *d)
 
unsigned short int dcmfileMaxDepth (DCMFILE *df)
 
unsigned short int dcmitemParentNr (DCMITEM *d)
 
char * dcmValueString (DCMITEM *d)
 
long int dcmitemGetInt (DCMITEM *d)
 
double dcmitemGetReal (DCMITEM *d)
 
DCMITEMdcmFindTag (DCMITEM *d, const short int omit, DCMTAG *tag, const int verbose)
 
DCMITEMdcmFindDownTag (DCMITEM *d, const short int omit, DCMTAG *tag, const int verbose)
 
void dcmitemPrint (DCMITEM *d)
 
void dcmTagSet (DCMTAG *tag, unsigned short int group, unsigned short int element)
 
int dcmAddItem (DCMFILE *dcm, DCMITEM *d, short int aschild, DCMTAG tag, dcmvr vr, unsigned int vl, char *rd, const int verbose)
 
int dcmTagIntRange (DCMITEM *d, DCMTAG *tag, int *mi, int *ma, const int verbose)
 

Detailed Description

Processing DICOM data structs.

Definition in file dcmdata.c.

Function Documentation

◆ dcmAddItem()

int dcmAddItem ( DCMFILE * dcm,
DCMITEM * d,
short int aschild,
DCMTAG tag,
dcmvr vr,
unsigned int vl,
char * rd,
const int verbose )

Add an item to DCMFILE data struct.

See also
dcmfileInit, dcmfileFree
Returns
0 if successful, otherwise >0.
Parameters
dcmPointer to DCMFILE.
dPointer to a previous item in DCMFILE, into which to link this item; enter NULL to add as next item to the highest level.
aschildAdd as child; 1=yes, 0=no.
tagTag
vrVR
vlVL; enter 0xFFFFFFFF to use VR's default length.
rdPointer to the item value as byte array
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 501 of file dcmdata.c.

519 {
520 if(verbose>0) {
521 printf("%s(dcm, (%04X,%04X))", __func__, tag.group, tag.element);
522 if(d==NULL) printf(", null"); else printf(", ptr");
523 printf(", %d, %s, 0x%08X, rd", aschild, dcmVRName(vr), vl);
524 printf(")\n");
525 }
526 if(dcm==NULL) return(1);
527 if(vr==DCM_VR_INVALID) return(2);
528
529 /* Check that caller has not given previous item pointer when there are none in DCMFILE */
530 if(d!=NULL && dcm->item==NULL) return(3);
531 /* Check that caller has not given previous item pointer that already has child */
532 if(d!=NULL && aschild && d->child_item!=NULL) return(4);
533
534 /* Check whether we currently support the Transfer UID */
535 if(dcm->truid!=DCM_TRUID_LEE) return(5);
536
537 /* Allocate memory for the new element; do not free it here, since it will be part of
538 DCMFILE struct. */
539 if(verbose>1) printf(" allocating memory for the item\n");
540 DCMITEM *item=(DCMITEM*)malloc(sizeof(DCMITEM));
541 if(item==NULL) return(11);
542 item->next_item=item->child_item=(DCMITEM*)NULL;
543 item->fp=dcm->fp; item->truid=dcm->truid;
544 item->rd=(char*)NULL;
545
546 /* Set item tag, VR, and VL */
547 if(verbose>1) printf(" setting item contents\n");
548 item->tag.group=tag.group;
549 item->tag.element=tag.element;
550 item->vr=vr;
551 item->vl=vl;
552 /* Allocate memory for item value */
553 size_t s;
554 if(vl==0xFFFFFFFF) s=dcmVRVLength(vr); else s=vl;
555 if(s>0) {
556 if(item->vl==0xFFFFFFFF) item->vl=(unsigned int)s;
557 if(verbose>1) printf(" allocating %u bytes for the item value\n", (unsigned int)s);
558 item->rd=(char*)calloc(s, sizeof(char));
559 if(item->rd==NULL) {free(item); return(21);}
560 } else {
561 if(verbose>1) printf("zero size for item value\n");
562 if(rd==NULL) {
563 if(verbose>1) printf("... which is ok since value is empty, too.\n");
564 } else {
565 if(verbose>0) printf("... which is not ok because we have value to store.\n");
566 if(item->rd==NULL) {free(item); return(22);}
567 }
568 }
569 /* Copy the item value */
570 if(rd!=NULL && s>0) {
571 if(verbose>1) printf(" copying the item value\n");
572 /* Special treatment for strings, because those tend to be shorter than told */
573 if(vr==DCM_VR_LO || vr==DCM_VR_LT || vr==DCM_VR_PN || vr==DCM_VR_SH || vr==DCM_VR_UI || vr==DCM_VR_UR)
574 {
575 unsigned int len=strnlen(rd, s);
576 if(len<s) strlcpy(item->rd, rd, s);
577 else memcpy(item->rd, rd, s);
578 } else if(vr==DCM_VR_DS || vr==DCM_VR_IS)
579 {
580 unsigned int len=strnlen(rd, s);
581 if(len<s) strlcpy(item->rd, rd, s);
582 else memcpy(item->rd, rd, s);
583 } else {
584 memcpy(item->rd, rd, s);
585 }
586 }
587
588
589 /* If we have the item to link to, then do the linking */
590 if(verbose>1) printf(" link the item.\n");
591 if(d!=NULL) {
592 if(aschild) {
593 d->child_item=item;
594 item->parent_item=d;
595 item->prev_item=(DCMITEM*)NULL;
596 } else if(d->next_item==NULL) {
597 d->next_item=item;
598 item->prev_item=d;
599 item->parent_item=d->parent_item;
600 } else {
601 /* find the last item in the list */
602 DCMITEM *ip=d; while(ip->next_item!=NULL) ip=ip->next_item;
603 ip->next_item=item;
604 item->prev_item=ip;
605 item->parent_item=ip->parent_item;
606 }
607 } else if(dcm->item==NULL) {
608 /* This is truly the first item ever */
609 dcm->item=item;
610 item->prev_item=item->parent_item=(DCMITEM*)NULL;
611 } else {
612 /* Caller lets us find the item to link to */
613 DCMITEM *ip=dcm->item; while(ip->next_item!=NULL) ip=ip->next_item;
614 ip->next_item=item;
615 item->prev_item=ip;
616 item->parent_item=ip->parent_item;
617 }
618
619 if(verbose>2) dcmitemPrint(item);
620
621 if(verbose>1) printf(" all done.\n");
622 return(0);
623}
void dcmitemPrint(DCMITEM *d)
Definition dcmdata.c:467
size_t dcmVRVLength(dcmvr id)
Definition dcmvr.c:144
char * dcmVRName(dcmvr id)
Definition dcmvr.c:126
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
dcmtruid truid
Definition tpcdcm.h:162
DCMITEM * item
Definition tpcdcm.h:164
FILE * fp
Definition tpcdcm.h:160
dcmtruid truid
Definition tpcdcm.h:135
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
FILE * fp
Definition tpcdcm.h:131
char * rd
Definition tpcdcm.h:152
struct DCMITEM * prev_item
Definition tpcdcm.h:149
struct DCMITEM * parent_item
Definition tpcdcm.h:145
DCMTAG tag
Definition tpcdcm.h:137
unsigned short int element
Definition tpcdcm.h:44
unsigned short int group
Definition tpcdcm.h:42
@ DCM_VR_INVALID
Invalid DICOM value representation.
Definition tpcdcm.h:123
@ DCM_VR_UI
DICOM unique identifier (UID), max 64 bytes.
Definition tpcdcm.h:117
@ DCM_VR_PN
DICOM person name, max 64 chars per component group.
Definition tpcdcm.h:109
@ DCM_VR_SH
DICOM short string, max 16 chars.
Definition tpcdcm.h:110
@ DCM_VR_LT
DICOM long text, max 10240 chars.
Definition tpcdcm.h:103
@ DCM_VR_DS
DICOM decimal string, max 16 bytes.
Definition tpcdcm.h:97
@ DCM_VR_IS
DICOM integer string, max 12 bytes.
Definition tpcdcm.h:101
@ DCM_VR_UR
DICOM URI or URL, string of characters.
Definition tpcdcm.h:120
@ DCM_VR_LO
DICOM long string, max 64 chars.
Definition tpcdcm.h:102
@ DCM_TRUID_LEE
Little Endian Explicit VR.
Definition tpcdcm.h:60

Referenced by imgWriteDICOM().

◆ dcmfileFree()

void dcmfileFree ( DCMFILE * d)

Free memory allocated for DCMFILE data. All contents are destroyed.

Precondition
Before first use initialize the struct with dcmfileInit().
See also
dcmfileInit
Author
Vesa Oikonen
Parameters
dPointer to DCMFILE.

Definition at line 67 of file dcmdata.c.

70 {
71 if(d==NULL) return;
72 dcmitemFree(d->item);
73 dcmfileInit(d);
74}
void dcmfileInit(DCMFILE *d)
Definition dcmdata.c:22
void dcmitemFree(DCMITEM *d)
Definition dcmdata.c:39

Referenced by dcmFileList(), dcmFileRead(), dcmMListRead(), imgReadDICOM(), and imgWriteDICOM().

◆ dcmfileInit()

void dcmfileInit ( DCMFILE * d)

Initiate the DCMFILE struct before any use.

See also
dcmfileFree
Parameters
dPointer to DCMFILE.

Definition at line 22 of file dcmdata.c.

25 {
26 if(d==NULL) return;
27 d->filename[0]=(char)0;
28 d->fp=(FILE*)NULL;
30 d->item=(DCMITEM*)NULL;
31}
char filename[FILENAME_MAX]
Definition tpcdcm.h:158
@ DCM_TRUID_UNKNOWN
Unknown Transfer Syntax UID
Definition tpcdcm.h:58

Referenced by dcmfileFree(), dcmFileList(), dcmMListRead(), imgReadDICOM(), and imgWriteDICOM().

◆ dcmfileMaxDepth()

unsigned short int dcmfileMaxDepth ( DCMFILE * df)

Get the maximum depth of DCMFILE items tree.

Returns
Returns the number of item levels under specified DCMFILE, or zero in case of an error or if there are no items.
See also
dcmitemParentNr, dcmitemMaxDepth
Parameters
dfPointer to DCMFILE item.

Definition at line 102 of file dcmdata.c.

105 {
106 if(df==NULL || df->item==NULL) return(0);
107 unsigned short int m=0, n=0;
108 DCMITEM *sd=df->item;
109 while(sd!=NULL) { // go through all sisters
110 n=dcmitemMaxDepth(sd); if(n>m) m=n;
111 sd=sd->next_item;
112 }
113 return(m+1);
114}
unsigned short int dcmitemMaxDepth(DCMITEM *d)
Definition dcmdata.c:83

◆ dcmFindDownTag()

DCMITEM * dcmFindDownTag ( DCMITEM * d,
const short int omit,
DCMTAG * tag,
const int verbose )

Search for specified tag in DCMITEM data tree, but only downward.

Returns
Returns pointer to next item with the tag, or NULL if not found.
Parameters
dPointer to current DICOM item.
omitOmit this item from the search.
tagPointer to the DICOM tag that is searched for.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 428 of file dcmdata.c.

437 {
438 if(d==NULL || tag==NULL) return(NULL);
439 if(verbose>0) printf("%s((%04X,%04X),%d)\n", __func__, tag->group, tag->element, omit);
440 DCMITEM *iptr;
441 if(omit==0) iptr=d; else iptr=d->next_item;
442 while(iptr!=NULL) {
443 if(verbose>2)
444 printf(" checking tag(%04X,%04X)...\n", iptr->tag.group, iptr->tag.element);
445 if(iptr->tag.group==tag->group && iptr->tag.element==tag->element) {
446 if(verbose>2) printf(" found!\n");
447 break;
448 }
449 /* Check if this item has children */
450 if(iptr->child_item!=NULL) {
451 if(verbose>2) printf(" going to search inside children...\n");
452 DCMITEM *rptr=dcmFindDownTag(iptr->child_item, 0, tag, verbose);
453 if(rptr!=NULL) return(rptr);
454 if(verbose>3) printf(" nothing found in any of the children\n");
455 }
456 iptr=iptr->next_item;
457 }
458 /* Stop if we found tag */
459 if(iptr!=NULL) return(iptr);
460 /* Stop anyway */
461 return(NULL);
462}
DCMITEM * dcmFindDownTag(DCMITEM *d, const short int omit, DCMTAG *tag, const int verbose)
Definition dcmdata.c:428

Referenced by dcmFindDownTag(), dcmFindTag(), dcmImgIsotope(), dcmImgPos(), dcmImgPxlsize(), dcmMListRead(), and imgReadDICOM().

◆ dcmFindTag()

DCMITEM * dcmFindTag ( DCMITEM * d,
const short int omit,
DCMTAG * tag,
const int verbose )

Search for specified tag in DCMITEM data tree, from left to right, down and up.

Returns
Returns pointer to next item with the tag, or NULL if not found.
Parameters
dPointer to current DICOM item.
omitOmit this item from the search.
tagPointer to the DICOM tag that is searched for.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 375 of file dcmdata.c.

384 {
385 if(d==NULL || tag==NULL) return(NULL);
386 if(verbose>0) printf("%s((%04X,%04X),%d)\n", __func__, tag->group, tag->element, omit);
387 DCMITEM *iptr;
388 if(omit==0) iptr=d; else iptr=d->next_item;
389 while(iptr!=NULL) {
390 if(verbose>2)
391 printf(" checking tag(%04X,%04X)...\n", iptr->tag.group, iptr->tag.element);
392 if(iptr->tag.group==tag->group && iptr->tag.element==tag->element) {
393 if(verbose>2) printf(" found!\n");
394 break;
395 }
396 /* Check if this item has children */
397 if(iptr->child_item!=NULL) {
398 if(verbose>2) printf(" going to search inside children...\n");
399 DCMITEM *rptr=dcmFindDownTag(iptr->child_item, 0, tag, verbose);
400 if(rptr!=NULL) return(rptr);
401 if(verbose>3) printf(" nothing found in any of the children\n");
402 }
403 iptr=iptr->next_item;
404 }
405 /* Stop if we found tag */
406 if(iptr!=NULL) return(iptr);
407 /* Stop if we do not have parent */
408 if(d->parent_item==NULL) return(NULL);
409 /* Search from the parent, but not including the parent */
410 if(verbose>2) printf(" going to search the parents sister...\n");
411 return(dcmFindTag(d->parent_item, 1, tag, verbose));
412
413#if(0)
414 if(d->parent_item==NULL) return(NULL);
415
416 /* Search from the parent */
417 if(verbose>2) printf(" going to search inside parent...\n");
418 return(dcmFindTag(d->parent_item, 1, tag, verbose));
419#endif
420}
DCMITEM * dcmFindTag(DCMITEM *d, const short int omit, DCMTAG *tag, const int verbose)
Definition dcmdata.c:375

Referenced by dcmFileList(), dcmFindTag(), dcmImgDim(), dcmImgIsotope(), dcmImgOrient(), dcmImgPos(), dcmImgPxlsize(), dcmMListRead(), dcmSameImage(), dcmTagIntRange(), and imgReadDICOM().

◆ dcmitemFree()

void dcmitemFree ( DCMITEM * d)

Recursively free memory allocated for DCMITEM items and their children items.

See also
dcmfileFree
Author
Vesa Oikonen
Parameters
dPointer to DCMITEM.

Definition at line 39 of file dcmdata.c.

42 {
43 if(d==NULL) return;
44 /* find the last item in the list */
45 DCMITEM *ip=d; while(ip->next_item!=NULL) ip=ip->next_item;
46 while(ip!=NULL) {
47 /* Free items child and their children */
48 if(ip->child_item!=NULL) dcmitemFree(ip->child_item);
49 /* Free this item and move to previous item */
50 if(ip->prev_item!=NULL) {
51 ip=ip->prev_item;
52 free(ip->next_item->rd); free(ip->next_item);
53 ip->next_item=NULL;
54 } else {
55 free(ip->rd); free(ip); ip=NULL;
56 }
57 }
58}

Referenced by dcmfileFree(), and dcmitemFree().

◆ dcmitemGetInt()

long int dcmitemGetInt ( DCMITEM * d)

Read integer value from given DICOM item.

VR must be either UL, US, SL, SS, or IS; otherwise 0 is returned.

Returns
Returns the value as long int, in order to cope with originally unsigned integers.
See also
dcmitemGetInt, dcmValueString, dcmFindTag
Parameters
dPointer to item.

Definition at line 295 of file dcmdata.c.

298 {
299 if(d==NULL || d->rd==NULL) return(0);
300 long int li=0;
301 if(d->vr==DCM_VR_UL) { // unsigned 32-bit int
302 unsigned int i;
303 memcpy(&i, d->rd, 4); if(!endianLittle()) swap(&i, &i, 4);
304 li=(long int)i;
305 } else if(d->vr==DCM_VR_US) { // unsigned 16-bit int
306 unsigned short int i;
307 memcpy(&i, d->rd, 2); if(!endianLittle()) swap(&i, &i, 2);
308 li=(long int)i;
309 } else if(d->vr==DCM_VR_SL) { // signed 32-bit int
310 int i;
311 memcpy(&i, d->rd, 4); if(!endianLittle()) swap(&i, &i, 4);
312 li=(long int)i;
313 } else if(d->vr==DCM_VR_SS) { // signed 16-bit int
314 short int i;
315 memcpy(&i, d->rd, 2); if(!endianLittle()) swap(&i, &i, 2);
316 li=(long int)i;
317 } else if(d->vr==DCM_VR_IS) { // integer string
318 li=atol(d->rd);
319 }
320 return(li);
321}
int endianLittle()
Definition endian.c:53
void swap(void *from, void *to, int size)
Definition endian.c:69
@ DCM_VR_SS
DICOM signed short (16-bit integer), 2 bytes fixed.
Definition tpcdcm.h:113
@ DCM_VR_US
DICOM unsigned short (16-bit) integer, 2 bytes fixed.
Definition tpcdcm.h:121
@ DCM_VR_UL
DICOM unsigned long (32-bit) integer, 4 bytes fixed.
Definition tpcdcm.h:118
@ DCM_VR_SL
DICOM signed long (32-bit integer), 4 bytes fixed.
Definition tpcdcm.h:111

Referenced by dcmImgDim(), dcmImgPos(), dcmMListRead(), dcmSameImage(), dcmTagIntRange(), and imgReadDICOM().

◆ dcmitemGetReal()

double dcmitemGetReal ( DCMITEM * d)

Read floating point value from given DICOM item.

VR must be either FL, FD, DS, UL, US, SL, SS, or IS; otherwise 0 is returned.

Returns
Returns the value as double.
See also
dcmitemGetInt, dcmValueString, dcmFindTag
Parameters
dPointer to item.

Definition at line 331 of file dcmdata.c.

334 {
335 if(d==NULL || d->rd==NULL) return(0);
336 double r=0.0;
337 if(d->vr==DCM_VR_FL) { // 32-bit float
338 float f;
339 memcpy(&f, d->rd, 4); if(!endianLittle()) swap(&f, &f, 4);
340 r=(double)f;
341 } else if(d->vr==DCM_VR_FD) { // 64-bit double
342 double f;
343 memcpy(&f, d->rd, 8); if(!endianLittle()) swap(&f, &f, 8);
344 r=f;
345 } else if(d->vr==DCM_VR_DS) { // decimal string
346 r=atof(d->rd);
347 } else if(d->vr==DCM_VR_UL) { // unsigned 32-bit int
348 unsigned int i;
349 memcpy(&i, d->rd, 4); if(!endianLittle()) swap(&i, &i, 4);
350 r=(double)i;
351 } else if(d->vr==DCM_VR_US) { // unsigned 16-bit int
352 unsigned short int i;
353 memcpy(&i, d->rd, 2); if(!endianLittle()) swap(&i, &i, 2);
354 r=(double)i;
355 } else if(d->vr==DCM_VR_SL) { // signed 32-bit int
356 int i;
357 memcpy(&i, d->rd, 4); if(!endianLittle()) swap(&i, &i, 4);
358 r=(double)i;
359 } else if(d->vr==DCM_VR_SS) { // signed 16-bit int
360 short int i;
361 memcpy(&i, d->rd, 2); if(!endianLittle()) swap(&i, &i, 2);
362 r=(double)i;
363 } else if(d->vr==DCM_VR_IS) { // integer string
364 r=(double)atol(d->rd);
365 }
366 return(r);
367}
@ DCM_VR_FD
DICOM floating point double precision, 8 bytes fixed.
Definition tpcdcm.h:100
@ DCM_VR_FL
DICOM floating point single precision, 4 bytes fixed.
Definition tpcdcm.h:99

Referenced by dcmImgPxlsize(), dcmMListRead(), and imgReadDICOM().

◆ dcmitemMaxDepth()

unsigned short int dcmitemMaxDepth ( DCMITEM * d)

Get the maximum depth of DCMITEM tree.

Returns
Returns the number of levels under specified item, not including given item itself.
See also
dcmitemParentNr, dcmfileMaxDepth
Parameters
dPointer to DCMITEM item.

Definition at line 83 of file dcmdata.c.

86 {
87 if(d==NULL || d->child_item==NULL) return(0);
88 unsigned short int m=0, n=0;
89 DCMITEM *cd=d->child_item;
90 /* go through all children */
91 while(cd!=NULL) {
92 n=dcmitemMaxDepth(cd); if(n>m) m=n;
93 cd=cd->next_item;
94 }
95 return(m+1);
96}

Referenced by dcmfileMaxDepth(), and dcmitemMaxDepth().

◆ dcmitemParentNr()

unsigned short int dcmitemParentNr ( DCMITEM * d)

Check how deep in DCMITEM tree this item is.

Returns
Returns the number of parents this item has.
See also
dcmitemMaxDepth, dcmfileMaxDepth
Parameters
dPointer to DCMITEM.

Definition at line 122 of file dcmdata.c.

125 {
126 if(d==NULL) return(0);
127 unsigned short int n=0;
128 DCMITEM *pd=d->parent_item;
129 while(pd!=NULL) {n++; pd=pd->parent_item;}
130 return(n);
131}

Referenced by dcmFileReadNextElement().

◆ dcmitemPrint()

void dcmitemPrint ( DCMITEM * d)

Print contents of given DICOM item into stdout.

Parameters
dPointer to item.

Definition at line 467 of file dcmdata.c.

470 {
471 if(d==NULL) {printf("(null)\n"); fflush(stdout); return;}
472 printf("tag(%04X,%04X)", d->tag.group, d->tag.element); fflush(stdout);
473 printf(" VR=%s", dcmVRName(d->vr)); fflush(stdout);
474 if(d->vl==0xFFFFFFFF) printf(" VL=%08X", d->vl); else printf(" VL=%u", d->vl);
475 fflush(stdout);
476 char *buf=dcmValueString(d); printf(" '%s'", buf); free(buf);
477 printf("\n"); fflush(stdout);
478}
char * dcmValueString(DCMITEM *d)
Definition dcmdata.c:141

Referenced by dcmAddItem(), dcmFileWrite(), dcmMListRead(), and imgReadDICOM().

◆ dcmTagIntRange()

int dcmTagIntRange ( DCMITEM * d,
DCMTAG * tag,
int * mi,
int * ma,
const int verbose )

Search the range of integer values for specified tag in DCMITEM data tree.

Returns
0 if successful, otherwise >0.
Parameters
dPointer to top DICOM item.
tagPointer to the DICOM tag that is searched for.
miPointer to int where min value is written; NULL if not needed.
maPointer to int where max value is written; NULL if not needed.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 631 of file dcmdata.c.

642 {
643 if(d==NULL || tag==NULL) return(1);
644 if(verbose>0) printf("%s(%04X,%04X)\n", __func__, tag->group, tag->element);
645
646 /* Find the first occurrence of tag */
647 DCMITEM *iptr=dcmFindTag(d, 0, tag, 0);
648 if(iptr==NULL) {
649 if(verbose>1) printf(" tag not found.\n");
650 return(2);
651 }
652 int iv=dcmitemGetInt(iptr); if(verbose>2) printf(" value := %d\n", iv);
653 int mini=iv;
654 int maxi=iv;
655 /* Find the rest of occurrences */
656 while(iptr!=NULL) {
657 iptr=dcmFindTag(iptr, 1, tag, 0);
658 if(iptr!=NULL) {
659 iv=dcmitemGetInt(iptr); if(verbose>2) printf(" value := %d\n", iv);
660 if(iv>maxi) maxi=iv; else if(iv<mini) mini=iv;
661 }
662 }
663 if(verbose>1) {
664 printf(" minimum_value := %d\n", mini);
665 printf(" maximum_value := %d\n", maxi);
666 }
667 if(mi!=NULL) *mi=mini;
668 if(ma!=NULL) *ma=maxi;
669 return(0);
670}
long int dcmitemGetInt(DCMITEM *d)
Definition dcmdata.c:295

Referenced by dcmImgDim(), and dcmMListRead().

◆ dcmTagSet()

void dcmTagSet ( DCMTAG * tag,
unsigned short int group,
unsigned short int element )

Set DICOM Tag group and element.

Parameters
tagPointer to Tag to set.
groupTag Group
elementTag Element

Definition at line 483 of file dcmdata.c.

490 {
491 tag->group=group;
492 tag->element=element;
493}

Referenced by dcmFileList(), dcmImgDim(), dcmImgIsotope(), dcmImgOrient(), dcmImgPos(), dcmImgPxlsize(), dcmSameImage(), imgReadDICOM(), and imgWriteDICOM().

◆ dcmValueString()

char * dcmValueString ( DCMITEM * d)

Pre-process the DICOM element value into format suitable for printing.

Note
Use only for printing information for the user.
Returns
Returns pointer to locally allocated null-terminated string.
Postcondition
Free the pointer after use.
See also
dcmitemGetInt, dcmitemGetReal, dcmFindTag

Attribute tag

Float

Double

Unsigned 32-bit int

Unsigned 16-bit int

Signed 32-bit int

Signed 16-bit int

Parameters
dPointer to item containing the value to print.

Definition at line 141 of file dcmdata.c.

144 {
145 if(d==NULL) return((char*)NULL);
146
147 /* For sequence, return string 'na' */
148 if(d->vr==DCM_VR_SQ) {
149 char *s=malloc(3); strcpy(s, "na"); // do not write char *s="na";
150 return(s);
151 }
152
153 /* If there is no value, then return string 'empty', or
154 'na', if value just was not stored (pixel data) */
155 if(d->vl==0) {
156 char *s=malloc(6); strcpy(s, "empty");
157 return(s);
158 } else if(d->rd==NULL) {
159 char *s=malloc(3); strcpy(s, "na");
160 return(s);
161 }
162
163 unsigned int len;
164 if(d->vl==0xFFFFFFFF) len=(unsigned int)dcmVRVLength(d->vr); else len=d->vl;
165
166 /* String values */
167 if(d->vr==DCM_VR_CS || d->vr==DCM_VR_DS || d->vr==DCM_VR_IS ||
168 d->vr==DCM_VR_LO || d->vr==DCM_VR_LT || d->vr==DCM_VR_PN ||
169 d->vr==DCM_VR_SH || d->vr==DCM_VR_ST)
170 {
171 char *s=malloc(len+1);
172 memcpy(s, d->rd, len); s[len]=(char)0;
173 return(s);
174 }
175
176 /* More string values */
177 if(d->vr==DCM_VR_AS || d->vr==DCM_VR_PN ||
178 d->vr==DCM_VR_DA || d->vr==DCM_VR_DT || d->vr==DCM_VR_TM ||
179 d->vr==DCM_VR_UT || d->vr==DCM_VR_AE ||
180 d->vr==DCM_VR_UI || d->vr==DCM_VR_UR)
181 {
182 char *s=malloc(len+1);
183 memcpy(s, d->rd, len); s[len]=(char)0;
184 return(s);
185 }
186
187
188 if(d->vr==DCM_VR_AT) {
189 DCMTAG tag;
190 memcpy(&tag.group, d->rd, 2);
191 memcpy(&tag.element, d->rd+2, 2);
192 if(!endianLittle()) {
193 swap(&tag.group, &tag.group, 2);
194 swap(&tag.element, &tag.element, 2);
195 }
196 char *s=malloc(14);
197 sprintf(s, "0x%04x,0x%04x", tag.group, tag.element);
198 return(s);
199 }
200
201
202 if(d->vr==DCM_VR_FL) {
203 float f;
204 memcpy(&f, d->rd, 4);
205 if(!endianLittle()) swap(&f, &f, 4);
206 char *s=malloc(16);
207 sprintf(s, "%g", f);
208 return(s);
209 }
210
211
212 if(d->vr==DCM_VR_FD) {
213 char *s=malloc(32);
214 /*if(d->vl==2) {
215 printf("\nFD VL=%d\n", d->vl);
216 float f;
217 memcpy(&f, d->rd+4, 4);
218 if(!endianLittle()) swap(&f, &f, 4);
219 sprintf(s, "%g", f);
220 } else {
221 double f;
222 memcpy(&f, d->rd, 8);
223 if(!endianLittle()) swap(&f, &f, 8);
224 sprintf(s, "%g", f);
225 }
226 */
227 double f;
228 memcpy(&f, d->rd, 8);
229 if(!endianLittle()) swap(&f, &f, 8);
230 sprintf(s, "%g", f);
231 return(s);
232 }
233
234
235 if(d->vr==DCM_VR_UL) {
236 unsigned int i;
237 memcpy(&i, d->rd, 4);
238 if(!endianLittle()) swap(&i, &i, 4);
239 char *s=malloc(16);
240 sprintf(s, "%u", i);
241 return(s);
242 }
243
244
245 if(d->vr==DCM_VR_US) {
246 unsigned short int i;
247 memcpy(&i, d->rd, 2);
248 if(!endianLittle()) swap(&i, &i, 2);
249 char *s=malloc(8);
250 sprintf(s, "%u", i);
251 return(s);
252 }
253
254
255 if(d->vr==DCM_VR_SL) {
256 int i;
257 memcpy(&i, d->rd, 4);
258 if(!endianLittle()) swap(&i, &i, 4);
259 char *s=malloc(16);
260 sprintf(s, "%d", i);
261 return(s);
262 }
263
264
265 if(d->vr==DCM_VR_SS) {
266 short int i;
267 memcpy(&i, d->rd, 2);
268 if(!endianLittle()) swap(&i, &i, 2);
269 char *s=malloc(8);
270 sprintf(s, "%d", i);
271 return(s);
272 }
273
274/* Not (yet) printed:
275 DCM_VR_OB, ///< DICOM other byte string, even bytes, endian insensitive.
276 DCM_VR_OD, ///< DICOM other double (64-bit) stream, endian sensitive.
277 DCM_VR_OF, ///< DICOM other float (32-bit) stream, endian sensitive.
278 DCM_VR_OL, ///< DICOM other long (32-bit) stream, endian sensitive.
279 DCM_VR_OW, ///< DICOM other word (16-bit) stream, even bytes, endian sensitive.
280 DCM_VR_UC, ///< DICOM unlimited characters.
281 DCM_VR_UN, ///< DICOM unknown, any valid length of another VR.
282 DCM_VR_INVALID ///< Invalid DICOM value representation.
283*/
284 char *s=malloc(3); strcpy(s, "na"); // do not write char *s="na";
285 return(s);
286}
@ DCM_VR_DT
DICOM date time, max 26 bytes.
Definition tpcdcm.h:98
@ DCM_VR_CS
DICOM code (control) string, max 16 bytes.
Definition tpcdcm.h:95
@ DCM_VR_UT
DICOM unlimited text, character string.
Definition tpcdcm.h:122
@ DCM_VR_TM
DICOM time HHMMSS.FFFFFF, max 14 bytes.
Definition tpcdcm.h:115
@ DCM_VR_AS
DICOM age string, 4 bytes fixed.
Definition tpcdcm.h:93
@ DCM_VR_ST
DICOM short text, max 1024 chars.
Definition tpcdcm.h:114
@ DCM_VR_AT
DICOM attribute tag, 4 bytes fixed.
Definition tpcdcm.h:94
@ DCM_VR_DA
DICOM date in format YYYYMMDD, 8 bytes fixed.
Definition tpcdcm.h:96
@ DCM_VR_AE
DICOM application entity, max 16 bytes.
Definition tpcdcm.h:92
@ DCM_VR_SQ
DICOM sequence of zero or more elements (used for nested data).
Definition tpcdcm.h:112

Referenced by dcmFileReadNextElement(), dcmImgIsotope(), dcmImgOrient(), dcmImgPos(), dcmImgPxlsize(), dcmitemPrint(), dcmMListRead(), and imgReadDICOM().