1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2004/10/28 19:53:28 $
7 Version: $Revision: 1.50 $
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"
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() );
45 dbg.Error(!from, "Dict::Dict: can't open dictionary",
55 from >> std::ws; //remove white space
56 std::getline(from, name);
58 DictEntry newEntry(group, element, vr, fourth, name);
59 AddNewEntry(newEntry);
71 // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
72 // assume all the pointed DictEntries are already cleaned-up when
78 //-----------------------------------------------------------------------------
81 * \brief Print all the dictionary entries contained in this dictionary.
82 * Entries will be sorted by tag i.e. the couple (group, element).
83 * @param os The output stream to be written to.
85 void Dict::Print(std::ostream &os)
87 os << "Dict file name : " << Filename << std::endl;
92 * \brief Print all the dictionary entries contained in this dictionary.
93 * Entries will be sorted by tag i.e. the couple (group, element).
94 * @param os The output stream to be written to.
96 void Dict::PrintByKey(std::ostream &os)
100 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
103 s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
104 s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
106 s << tag->second.GetVR() << ", ";
107 s << tag->second.GetFourth() << ", ";
108 s << tag->second.GetName() << "." << std::endl;
114 * \brief Print all the dictionary entries contained in this dictionary.
115 * Entries will be sorted by the name of the dictionary entries.
116 * \warning AVOID USING IT : the name IS NOT an identifier;
117 * unpredictable result
118 * @param os The output stream to be written to.
120 void Dict::PrintByName(std::ostream& os)
122 std::ostringstream s;
124 for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag)
127 s << tag->second.GetName() << ",";
128 s << tag->second.GetVR() << ", ";
129 s << tag->second.GetFourth() << ", ";
130 s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
131 s << std::hex << std::setw(4) << tag->second.GetElement() << ") = ";
132 s << std::dec << std::endl;
137 //-----------------------------------------------------------------------------
141 * \brief adds a new Dicom Dictionary Entry
142 * @param newEntry entry to add
143 * @return false if Dicom Element already exists
145 bool Dict::AddNewEntry(DictEntry const & newEntry)
147 const TagKey & key = newEntry.GetKey();
149 if(KeyHt.count(key) == 1)
151 dbg.Verbose(1, "Dict::AddNewEntry already present", key.c_str());
157 std::map<TagKey, DictEntry>::value_type
158 (newEntry.GetKey(), newEntry));
160 std::map<TagName, DictEntry>::value_type
161 (newEntry.GetName(), newEntry ));
168 * \brief replaces an already existing Dicom Element by a new one
169 * @param newEntry new entry (overwrites any previous one with same tag)
170 * @return false if Dicom Element doesn't exist
172 bool Dict::ReplaceEntry(DictEntry const & newEntry)
174 if ( RemoveEntry(newEntry.GetKey()) )
177 std::map<TagKey, DictEntry>::value_type
178 (newEntry.GetKey(), newEntry));
180 std::map<TagName, DictEntry>::value_type
181 (newEntry.GetName(), newEntry ));
189 * \brief removes an already existing Dicom Dictionary Entry,
190 * identified by its Tag
191 * @param key (group|element)
192 * @return false if Dicom Dictionary Entry doesn't exist
194 bool Dict::RemoveEntry (TagKey const & key)
196 TagKeyHT::const_iterator it = KeyHt.find(key);
197 if(it != KeyHt.end())
199 const DictEntry & entryToDelete = it->second;
200 NameHt.erase(entryToDelete.GetName());
207 dbg.Verbose(1, "Dict::RemoveEntry unfound entry", key.c_str());
213 * \brief removes an already existing Dicom Dictionary Entry,
214 * identified by its group,element number
215 * @param group Dicom group number of the Dicom Element
216 * @param element Dicom element number of the Dicom Element
217 * @return false if Dicom Dictionary Entry doesn't exist
219 bool Dict::RemoveEntry (uint16_t group, uint16_t element)
221 return RemoveEntry(DictEntry::TranslateToKey(group, element));
225 * \brief Get the dictionnary entry identified by it's name.
226 * @param name element of the ElVal to modify
227 * \warning : NEVER use it !
228 * the 'name' IS NOT an identifier within the Dicom Dictionary
229 * the name MAY CHANGE between two versions !
230 * @return the corresponding dictionnary entry when existing, NULL otherwise
232 DictEntry* Dict::GetDictEntryByName(TagName const & name)
234 TagNameHT::iterator it = NameHt.find(name);
235 if ( it == NameHt.end() )
239 return &(it->second);
243 * \brief Get the dictionnary entry identified by a given tag (group,element)
244 * @param group group of the entry to be found
245 * @param element element of the entry to be found
246 * @return the corresponding dictionnary entry when existing, NULL otherwise
248 DictEntry* Dict::GetDictEntryByNumber(uint16_t group, uint16_t element)
250 TagKey key = DictEntry::TranslateToKey(group, element);
251 TagKeyHT::iterator it = KeyHt.find(key);
252 if ( it == KeyHt.end() )
256 return &(it->second);
260 * \brief Consider all the entries of the public dicom dictionnary.
261 * Build all list of all the tag names of all those entries.
262 * \sa DictSet::GetPubDictTagNamesByCategory
263 * @return A list of all entries of the public dicom dictionnary.
265 EntryNamesList* Dict::GetDictEntryNames()
267 EntryNamesList *result = new EntryNamesList;
268 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
270 result->push_back( tag->second.GetName() );
277 * \brief Consider all the entries of the public dicom dictionnary.
278 * Build an hashtable whose keys are the names of the groups
279 * (fourth field in each line of dictionary) and whose corresponding
280 * values are lists of all the dictionnary entries among that
281 * group. Note that apparently the Dicom standard doesn't explicitely
282 * define a name (as a string) for each group.
283 * A typical usage of this method would be to enable a dynamic
284 * configuration of a Dicom file browser: the admin/user can
285 * select in the interface which Dicom tags should be displayed.
286 * \warning Dicom *doesn't* define any name for any 'categorie'
287 * (the dictionnary fourth field was formerly NIH defined
288 * - and no longer he is-
289 * and will be removed when Dicom provides us a text file
290 * with the 'official' Dictionnary, that would be more friendly
291 * than asking us to perform a line by line check of the dictionnary
292 * at the beginning of each year to -try to- guess the changes)
293 * Therefore : please NEVER use that fourth field :-(
295 * @return An hashtable: whose keys are the names of the groups and whose
296 * corresponding values are lists of all the dictionnary entries
299 EntryNamesByCatMap *Dict::GetDictEntryNamesByCategory()
301 EntryNamesByCatMap *result = new EntryNamesByCatMap;
303 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
305 (*result)[tag->second.GetFourth()].push_back(tag->second.GetName());
311 //-----------------------------------------------------------------------------
314 //-----------------------------------------------------------------------------
317 //-----------------------------------------------------------------------------
319 } // end namespace gdcm