1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2004/11/03 18:08:56 $
7 Version: $Revision: 1.51 $
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.
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.
17 =========================================================================*/
21 #include "gdcmDebug.h"
29 void FillDefaultDataDict(Dict *d);
30 //-----------------------------------------------------------------------------
31 // Constructor / Destructor
34 * @param filename from which to build the dictionary.
36 Dict::Dict(std::string const & filename)
44 std::ifstream from( filename.c_str() );
47 dbg.Verbose(2,"Dict::Dict: can't open dictionary", filename.c_str());
48 // Using default embeded one:
49 FillDefaultDataDict( this );
60 from >> std::ws; //remove white space
61 std::getline(from, name);
63 const DictEntry newEntry(group, element, vr, fourth, name);
64 AddNewEntry(newEntry);
77 // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
78 // assume all the pointed DictEntries are already cleaned-up when
84 //-----------------------------------------------------------------------------
87 * \brief Print all the dictionary entries contained in this dictionary.
88 * Entries will be sorted by tag i.e. the couple (group, element).
89 * @param os The output stream to be written to.
91 void Dict::Print(std::ostream &os)
93 os << "Dict file name : " << Filename << std::endl;
98 * \brief Print all the dictionary entries contained in this dictionary.
99 * Entries will be sorted by tag i.e. the couple (group, element).
100 * @param os The output stream to be written to.
102 void Dict::PrintByKey(std::ostream &os)
104 std::ostringstream s;
106 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
109 s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
110 s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
112 s << tag->second.GetVR() << ", ";
113 s << tag->second.GetFourth() << ", ";
114 s << tag->second.GetName() << "." << std::endl;
120 * \brief Print all the dictionary entries contained in this dictionary.
121 * Entries will be sorted by the name of the dictionary entries.
122 * \warning AVOID USING IT : the name IS NOT an identifier;
123 * unpredictable result
124 * @param os The output stream to be written to.
126 void Dict::PrintByName(std::ostream& os)
128 std::ostringstream s;
130 for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag)
133 s << tag->second.GetName() << ",";
134 s << tag->second.GetVR() << ", ";
135 s << tag->second.GetFourth() << ", ";
136 s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
137 s << std::hex << std::setw(4) << tag->second.GetElement() << ") = ";
138 s << std::dec << std::endl;
143 //-----------------------------------------------------------------------------
147 * \brief adds a new Dicom Dictionary Entry
148 * @param newEntry entry to add
149 * @return false if Dicom Element already exists
151 bool Dict::AddNewEntry(DictEntry const & newEntry)
153 const TagKey & key = newEntry.GetKey();
155 if(KeyHt.count(key) == 1)
157 dbg.Verbose(1, "Dict::AddNewEntry already present", key.c_str());
163 std::map<TagKey, DictEntry>::value_type
164 (newEntry.GetKey(), newEntry));
166 std::map<TagName, DictEntry>::value_type
167 (newEntry.GetName(), newEntry ));
174 * \brief replaces an already existing Dicom Element by a new one
175 * @param newEntry new entry (overwrites any previous one with same tag)
176 * @return false if Dicom Element doesn't exist
178 bool Dict::ReplaceEntry(DictEntry const & newEntry)
180 if ( RemoveEntry(newEntry.GetKey()) )
183 std::map<TagKey, DictEntry>::value_type
184 (newEntry.GetKey(), newEntry));
186 std::map<TagName, DictEntry>::value_type
187 (newEntry.GetName(), newEntry ));
195 * \brief removes an already existing Dicom Dictionary Entry,
196 * identified by its Tag
197 * @param key (group|element)
198 * @return false if Dicom Dictionary Entry doesn't exist
200 bool Dict::RemoveEntry (TagKey const & key)
202 TagKeyHT::const_iterator it = KeyHt.find(key);
203 if(it != KeyHt.end())
205 const DictEntry & entryToDelete = it->second;
206 NameHt.erase(entryToDelete.GetName());
213 dbg.Verbose(1, "Dict::RemoveEntry unfound entry", key.c_str());
219 * \brief removes an already existing Dicom Dictionary Entry,
220 * identified by its group,element number
221 * @param group Dicom group number of the Dicom Element
222 * @param element Dicom element number of the Dicom Element
223 * @return false if Dicom Dictionary Entry doesn't exist
225 bool Dict::RemoveEntry (uint16_t group, uint16_t element)
227 return RemoveEntry(DictEntry::TranslateToKey(group, element));
231 * \brief Get the dictionnary entry identified by it's name.
232 * @param name element of the ElVal to modify
233 * \warning : NEVER use it !
234 * the 'name' IS NOT an identifier within the Dicom Dictionary
235 * the name MAY CHANGE between two versions !
236 * @return the corresponding dictionnary entry when existing, NULL otherwise
238 DictEntry* Dict::GetDictEntryByName(TagName const & name)
240 TagNameHT::iterator it = NameHt.find(name);
241 if ( it == NameHt.end() )
245 return &(it->second);
249 * \brief Get the dictionnary entry identified by a given tag (group,element)
250 * @param group group of the entry to be found
251 * @param element element of the entry to be found
252 * @return the corresponding dictionnary entry when existing, NULL otherwise
254 DictEntry* Dict::GetDictEntryByNumber(uint16_t group, uint16_t element)
256 TagKey key = DictEntry::TranslateToKey(group, element);
257 TagKeyHT::iterator it = KeyHt.find(key);
258 if ( it == KeyHt.end() )
262 return &(it->second);
266 * \brief Consider all the entries of the public dicom dictionnary.
267 * Build all list of all the tag names of all those entries.
268 * \sa DictSet::GetPubDictTagNamesByCategory
269 * @return A list of all entries of the public dicom dictionnary.
271 EntryNamesList* Dict::GetDictEntryNames()
273 EntryNamesList *result = new EntryNamesList;
274 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
276 result->push_back( tag->second.GetName() );
283 * \brief Consider all the entries of the public dicom dictionnary.
284 * Build an hashtable whose keys are the names of the groups
285 * (fourth field in each line of dictionary) and whose corresponding
286 * values are lists of all the dictionnary entries among that
287 * group. Note that apparently the Dicom standard doesn't explicitely
288 * define a name (as a string) for each group.
289 * A typical usage of this method would be to enable a dynamic
290 * configuration of a Dicom file browser: the admin/user can
291 * select in the interface which Dicom tags should be displayed.
292 * \warning Dicom *doesn't* define any name for any 'categorie'
293 * (the dictionnary fourth field was formerly NIH defined
294 * - and no longer he is-
295 * and will be removed when Dicom provides us a text file
296 * with the 'official' Dictionnary, that would be more friendly
297 * than asking us to perform a line by line check of the dictionnary
298 * at the beginning of each year to -try to- guess the changes)
299 * Therefore : please NEVER use that fourth field :-(
301 * @return An hashtable: whose keys are the names of the groups and whose
302 * corresponding values are lists of all the dictionnary entries
305 EntryNamesByCatMap *Dict::GetDictEntryNamesByCategory()
307 EntryNamesByCatMap *result = new EntryNamesByCatMap;
309 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
311 (*result)[tag->second.GetFourth()].push_back(tag->second.GetName());
317 //-----------------------------------------------------------------------------
320 //-----------------------------------------------------------------------------
323 //-----------------------------------------------------------------------------
325 } // end namespace gdcm