1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2004/11/16 02:54:35 $
7 Version: $Revision: 1.52 $
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());
162 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
163 NameHt.insert( TagNameHT::value_type(newEntry.GetName(), newEntry ));
170 * \brief replaces an already existing Dicom Element by a new one
171 * @param newEntry new entry (overwrites any previous one with same tag)
172 * @return false if Dicom Element doesn't exist
174 bool Dict::ReplaceEntry(DictEntry const & newEntry)
176 if ( RemoveEntry(newEntry.GetKey()) )
178 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
179 NameHt.insert( TagNameHT::value_type(newEntry.GetName(), newEntry ));
187 * \brief removes an already existing Dicom Dictionary Entry,
188 * identified by its Tag
189 * @param key (group|element)
190 * @return false if Dicom Dictionary Entry doesn't exist
192 bool Dict::RemoveEntry (TagKey const & key)
194 TagKeyHT::const_iterator it = KeyHt.find(key);
195 if(it != KeyHt.end())
197 const DictEntry& entryToDelete = it->second;
198 NameHt.erase(entryToDelete.GetName());
205 dbg.Verbose(1, "Dict::RemoveEntry unfound entry", key.c_str());
211 * \brief removes an already existing Dicom Dictionary Entry,
212 * identified by its group,element number
213 * @param group Dicom group number of the Dicom Element
214 * @param element Dicom element number of the Dicom Element
215 * @return false if Dicom Dictionary Entry doesn't exist
217 bool Dict::RemoveEntry (uint16_t group, uint16_t element)
219 return RemoveEntry(DictEntry::TranslateToKey(group, element));
223 * \brief Get the dictionnary entry identified by it's name.
224 * @param name element of the ElVal to modify
225 * \warning : NEVER use it !
226 * the 'name' IS NOT an identifier within the Dicom Dictionary
227 * the name MAY CHANGE between two versions !
228 * @return the corresponding dictionnary entry when existing, NULL otherwise
230 DictEntry* Dict::GetDictEntryByName(TagName const & name)
232 TagNameHT::iterator it = NameHt.find(name);
233 if ( it == NameHt.end() )
237 return &(it->second);
241 * \brief Get the dictionnary entry identified by a given tag (group,element)
242 * @param group group of the entry to be found
243 * @param element element of the entry to be found
244 * @return the corresponding dictionnary entry when existing, NULL otherwise
246 DictEntry* Dict::GetDictEntryByNumber(uint16_t group, uint16_t element)
248 TagKey key = DictEntry::TranslateToKey(group, element);
249 TagKeyHT::iterator it = KeyHt.find(key);
250 if ( it == KeyHt.end() )
254 return &(it->second);
258 * \brief Consider all the entries of the public dicom dictionnary.
259 * Build all list of all the tag names of all those entries.
260 * \sa DictSet::GetPubDictTagNamesByCategory
261 * @return A list of all entries of the public dicom dictionnary.
263 EntryNamesList* Dict::GetDictEntryNames()
265 EntryNamesList *result = new EntryNamesList;
266 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
268 result->push_back( tag->second.GetName() );
275 * \brief Consider all the entries of the public dicom dictionnary.
276 * Build an hashtable whose keys are the names of the groups
277 * (fourth field in each line of dictionary) and whose corresponding
278 * values are lists of all the dictionnary entries among that
279 * group. Note that apparently the Dicom standard doesn't explicitely
280 * define a name (as a string) for each group.
281 * A typical usage of this method would be to enable a dynamic
282 * configuration of a Dicom file browser: the admin/user can
283 * select in the interface which Dicom tags should be displayed.
284 * \warning Dicom *doesn't* define any name for any 'categorie'
285 * (the dictionnary fourth field was formerly NIH defined
286 * - and no longer he is-
287 * and will be removed when Dicom provides us a text file
288 * with the 'official' Dictionnary, that would be more friendly
289 * than asking us to perform a line by line check of the dictionnary
290 * at the beginning of each year to -try to- guess the changes)
291 * Therefore : please NEVER use that fourth field :-(
293 * @return An hashtable: whose keys are the names of the groups and whose
294 * corresponding values are lists of all the dictionnary entries
297 EntryNamesByCatMap *Dict::GetDictEntryNamesByCategory()
299 EntryNamesByCatMap *result = new EntryNamesByCatMap;
301 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
303 (*result)[tag->second.GetFourth()].push_back(tag->second.GetName());
309 //-----------------------------------------------------------------------------
312 //-----------------------------------------------------------------------------
315 //-----------------------------------------------------------------------------
317 } // end namespace gdcm