]> Creatis software - gdcm.git/blob - src/gdcmDictEntry.cxx
ENH: NEW FEATURE: TagKey is now a union of two uint16_t instead of string this greatl...
[gdcm.git] / src / gdcmDictEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDictEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/11 15:20:46 $
7   Version:   $Revision: 1.51 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmDictEntry.h"
20 #include "gdcmDebug.h"
21 #include "gdcmUtil.h"
22
23 #include <iomanip> // for std::ios::left, ...
24 #include <fstream>
25 #include <stdio.h> // for sprintf
26
27 namespace gdcm 
28 {
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
31 /**
32  * \brief   Constructor
33  * @param   group      DICOM-Group Number
34  * @param   elem       DICOM-Element Number
35  * @param   vr         Value Representation
36  * @param   vm         Value Multiplicity 
37  * @param   name       description of the element
38 */
39
40 DictEntry::DictEntry(uint16_t group, uint16_t elem,
41                      TagName const &vr, 
42                      TagName const &vm,
43                      TagName const &name)
44 {
45    Group   = group;
46    Element = elem;
47    VR      = vr;
48    VM      = vm;
49    Name    = name;
50    Key     = TranslateToKey(group, elem);
51 }
52
53 //-----------------------------------------------------------------------------
54 // Public
55 /**
56  * \brief       If-and only if-the V(alue) R(epresentation)
57  * \            is unset then overwrite it.
58  * @param vr    New V(alue) R(epresentation) to be set.
59  */
60 void DictEntry::SetVR(TagName const &vr) 
61 {
62    if ( IsVRUnknown() )
63    {
64       VR = vr;
65    }
66    else 
67    {
68       gdcmErrorMacro( "Overwriting VR might compromise a dictionary");
69    }
70 }
71
72 /**
73  * \brief       If-and only if-the V(alue) M(ultiplicity)
74  * \            is unset then overwrite it.
75  * @param vm    New V(alue) M(ultiplicity) to be set.
76  */
77 void DictEntry::SetVM(TagName const &vm) 
78 {
79    if ( IsVMUnknown() )
80    {
81       VM = vm;
82    }
83    else 
84    {
85       gdcmErrorMacro( "Overwriting VM might compromise a dictionary");
86    }
87 }
88
89 /**
90  * \brief   concatenates 2 uint16_t (supposed to be a Dicom group number 
91  *                                              and a Dicom element number)
92  * @param  group the Dicom group number used to build the tag
93  * @param  elem the Dicom element number used to build the tag
94  * @return the built tag
95  */
96 TagKey DictEntry::TranslateToKey(uint16_t group, uint16_t elem)
97 {
98    // according to 'Purify', TranslateToKey is one of the most
99    // time consuming methods.
100    // Let's try to shorten it !
101  
102    //return Util::Format("%04x|%04x", group, elem); // too much time !
103 #if FASTTAGKEY
104    TagKey r;
105    r.tab[0] = group;
106    r.tab[1] = elem;
107    return r;
108 #else
109    char res[10];
110    sprintf(res,"%04x|%04x", group, elem);
111    return res;
112 #endif
113 }
114
115 //-----------------------------------------------------------------------------
116 // Protected
117
118 //-----------------------------------------------------------------------------
119 // Private
120
121 //-----------------------------------------------------------------------------
122 // Print
123 /**
124  * \brief   Prints an entry of the Dicom DictionaryEntry
125  * @param   os ostream we want to print in
126  * @param indent Indentation string to be prepended during printing
127  */
128 void DictEntry::Print(std::ostream &os, std::string const & )
129 {
130    std::string vr;
131    std::ostringstream s;
132
133    vr = GetVR();
134    if ( vr==GDCM_UNKNOWN )
135       vr="  ";
136
137    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
138    s << " [" << vr  << "] ";
139
140    if ( PrintLevel >= 1 )
141    {
142       s.setf(std::ios::left);
143       s << std::setw(66-GetName().length()) << " ";
144    }
145
146    s << "[" << GetName()<< "]";
147    os << s.str() << std::endl;
148 }
149
150 //-----------------------------------------------------------------------------
151 } // end namespace gdcm
152