2 // * gdcmlib is a library dedicated to reading and writing dicom files.
3 // * LGPL for the license
4 // * lightweigth as opposed to CTN or DCMTK wich come bundled which try
5 // to implement the full DICOM standard (networking...). gdcmlib concentrates
7 // * Formats: this lib should be able to read ACR-NEMA v1 and v2, Dicom v3 (as
8 // stated in part10). [cf dcmtk/dcmdata/docs/datadict.txt]
9 // * Targeted plateforms: Un*xes and Win32/VC++6.0
12 #include <stddef.h> // For size_t
16 // The requirement for the hash table (or map) that we shall use:
17 // 1/ First, next, last (iterators)
18 // 2/ should be sortable (i.e. sorted by TagKey). This condition
19 // shall be droped since the Win32/VC++ implementation doesn't look
20 // a sorted one. Pffff....
21 // 3/ Make sure we can setup some default size value, which should be
22 // around 4500 entries which is the average dictionary size (said JPR)
25 // Tag based hash tables.
26 // We shall use as keys the strings (as the C++ type) obtained by
27 // concatenating the group value and the element value (both of type
28 // unsigned 16 bit integers in Dicom) expressed in hexadecimal.
29 // Example: consider the tag given as (group, element) = (0x0010, 0x0010).
30 // Then the corresponding TagKey shall be the string 00100010 (or maybe
31 // 0x00100x0010, we need some checks here).
32 typedef string TagKey;
33 typedef map<TagKey, char*> TagHT;
38 ////// QUESTION: it is not sure that we need to store the group and
39 ////// and the element within a DictEntry. What is the point
40 ////// of storing the equivalent of a TagKey within the information
41 ////// accessed through that TagKey !?
42 guint16 group; // e.g. 0x0010
43 guint16 element; // e.g. 0x0010
44 string name; // e.g. "Patient_Name"
45 string ValRep; // Value Representation i.e. some clue about the nature
46 // of the data represented e.g. "FD" short for
47 // "Floating Point Double"
48 // DCMTK has many fields for handling a DictEntry (see below). What are the
49 // relevant ones for gdcmlib ?
50 // struct DBI_SimpleEntry {
54 // Uint16 upperElement;
56 // const char* tagName;
59 // const char* standardVersion;
60 // DcmDictRangeRestriction groupRestriction;
61 // DcmDictRangeRestriction elementRestriction;
65 DictEntry(guint16 group, guint16 element, string name, string VR);
68 guint16 GetGroup(void) { return group;};
69 guint16 GetElement(void) {return element;};
72 // A single DICOM dictionary i.e. a container for a collection of dictionary
73 // entries. There should be a single public dictionary (THE dictionary of
74 // the actual DICOM v3) but as many shadow dictionaries as imagers
75 // combined with all software versions...
82 Dict(string filename); // Read Dict from disk
83 int AppendEntry(DictEntry* NewEntry);
84 DictEntry * GetTag(guint32, guint32); // Is this tag in this Dict ?
87 // Container for managing a set of loaded dictionaries. Sharing dictionaries
89 // * reloading an allready loaded dictionary.
90 // * having many in memory representations of the same dictionary.
94 map<DictId, Dict*> dicts;
95 int AppendDict(Dict* NewDict);
97 DictSet(); // Default constructor loads THE DICOM v3 dictionary
98 int LoadDictFromFile(string filename);
99 ///// QUESTION: the following function might not be thread safe !? Maybe
100 ///// we need some mutex here, to avoid concurent creation of
101 ///// the same dictionary !?!?!
102 int LoadDictFromName(string filename);
103 int LoadAllDictFromDirectory(string directorynanme);
104 string* GetAllDictNames();
105 Dict* GetDict(string DictName);
108 // The dicom header of a Dicom file contains a set of such ELement VALUES
109 // (when successfuly parsed against a given Dicom dictionary)
113 guint32 LgrLueElem; // Longueur Lue
114 // Might prove of some interest (see _ID_DCM_ELEM)
117 guint32 LgrElem; // FIXME probably for bad reasons at parse time !
118 string value; // used to be char * valeurElem
119 size_t Offset; // Offset from the begining of file for direct user access
122 void SetLgrLue(guint32);
123 guint16 GetGroup(void) { return entry->GetGroup(); };
124 guint16 GetElement(void) { return entry->GetElement(); };
125 guint32 GetLgrElem(void) { return LgrElem; };
126 void SetValue(string val) { value = val; };
129 // Container for a set of succefully parsed ElValues.
130 typedef map<TagKey, char*> TagHT;
132 // We need both accesses with a TagKey and the Dicentry.Name
133 ////// QUESTION: this leads to a double storage of a single ElValue
134 map<TagKey, ElValue> tagHt;
135 map<string, ElValue> NameHt;
140 // The various entries of the explicit value representation (VR) shall
141 // be managed within a dictionary.
142 typedef string VRKey;
143 typedef string VRAtr;
144 typedef map<TagKey, VRAtr> VRHT; // Value Representation Hash Table
146 // The typical usage of objects of this class is to classify a set of
147 // dicom files according to header information e.g. to create a file hierachy
148 // reflecting the Patient/Study/Serie informations, or extracting a given
149 // SerieId. Accesing the content (image[s] or volume[s]) is beyond the
150 // functionality of this class (see dmcFile below).
152 // * the gdcmHeader::Set*Tag* family members cannot be defined as protected
153 // (Swig limitations for as Has_a dependency between gdcmFile and gdcmHeader)
168 // All instances share the same valur representation dictionary
169 static VRHT *dicom_vr;
170 static DictSet* Dicts; // Global dictionary container
171 Dict* RefPubDict; // Public Dictionary
172 Dict* RefShaDict; // Shadow Dictionary (optional)
173 ElValSet PubElVals; // Element Values parsed with Public Dictionary
174 ElValSet ShaElVals; // Element Values parsed with Shadow Dictionary
176 // In order to inspect/navigate through the file
179 size_t offsetCourant;
180 // The tag Image Location ((0028,0200) containing the adress of
181 // the pixels) is not allways present. When we store this information
183 // outside of the elements:
188 size_t PixelPosition;
190 void CheckSwap(void);
191 void setAcrLibido(void);
192 long int RecupLgr(ElValue *, int *);
193 guint32 SWAP_LONG(guint32);
194 short int SWAP_SHORT(short int);
195 void InitVRDict(void);
196 ElValue * ReadNextElement(void);
197 DictEntry * IsInDicts(guint32, guint32);
198 void SetAsidePixelData(ElValue*);
200 ///// QUESTION: Maybe Print is a better name than write !?
202 ///// QUESTION: Maybe anonymize should be a friend function !?!?
203 ///// See below for an example of how anonymize might be implemented.
204 int anonymize(ostream&);
207 gdcmHeader(string filename);
210 int SetPubDict(string filename);
211 // When some proprietary shadow groups are disclosed, whe can set
212 // up an additional specific dictionary to access extra information.
213 int SetShaDict(string filename);
215 // Retrieve all potentially available tag [tag = (group, element)] names
216 // from the standard (or public) dictionary (hence static). Typical usage:
217 // enable the user of a GUI based interface to select his favorite fields
218 // for sorting or selection.
219 string* GetPubTagNames();
220 // Get the element values themselves:
221 string GetPubElValByName(string TagName);
222 string GetPubElValByNumber(guint16 group, guint16 element);
223 // Get the element value representation: (VR) might be needed by caller
224 // to convert the string typed content to caller's native type (think
225 // of C/C++ vs Python).
226 string GetPubElValRepByName(string TagName);
227 string GetPubElValRepByNumber(guint16 group, guint16 element);
229 // Same thing with the shadow :
230 string* GetShaTagNames();
231 string GetShaElValByName(string TagName);
232 string GetShaElValByNumber(guint16 group, guint16 element);
233 string GetShaElValRepByName(string TagName);
234 string GetShaElValRepByNumber(guint16 group, guint16 element);
236 // Wrappers of the above (both public and shadow) to avoid bugging the
237 // caller with knowing if ElVal is from the public or shadow dictionary.
238 string GetElValByName(string TagName);
239 string GetElValByNumber(guint16 group, guint16 element);
240 string GetElValRepByName(string TagName);
241 string GetElValRepByNumber(guint16 group, guint16 element);
243 int SetPubElValByName(string content, string TagName);
244 int SetPubElValByNumber(string content, guint16 group, guint16 element);
245 int SetShaElValByName(string content, string ShadowTagName);
246 int SetShaElValByNumber(string content, guint16 group, guint16 element);
251 // In addition to Dicom header exploration, this class is designed
252 // for accessing the image/volume content. One can also use it to
253 // write Dicom files.
254 ////// QUESTION: this looks still like an open question wether the
255 ////// relationship between a gdcmFile and gdcmHeader is of
256 ////// type IS_A or HAS_A !
257 class gdcmFile: gdcmHeader
261 int Parsed; // weather allready parsed
262 string OrigFileName; // To avoid file overwrite
264 // Constructor dedicated to writing a new DICOMV3 part10 compliant
265 // file (see SetFileName, SetDcmTag and Write)
267 // Opens (in read only and when possible) an existing file and checks
268 // for DICOM compliance. Returns NULL on failure.
269 // Note: the in-memory representation of all available tags found in
270 // the DICOM header is post-poned to first header information access.
271 // This avoid a double parsing of public part of the header when
272 // one sets an a posteriori shadow dictionary (efficiency can be
273 // seen a a side effect).
274 gdcmFile(string filename);
275 // For promotion (performs a deepcopy of pointed header object)
276 gdcmFile(gdcmHeader* header);
279 // On writing purposes. When instance was created through
280 // gdcmFile(string filename) then the filename argument MUST be different
281 // from the constructor's one (no overwriting aloud).
282 int SetFileName(string filename);
284 // Allocates necessary memory, copies the data (image[s]/volume[s]) to
285 // newly allocated zone and return a pointer to it:
286 void * GetImageData();
287 // Returns size (in bytes) of required memory to contain data
288 // represented in this file.
289 size_t GetImageDataSize();
290 // Copies (at most MaxSize bytes) of data to caller's memory space.
291 // Returns an error code on failure (if MaxSize is not big enough)
292 int PutImageDataHere(void* destination, size_t MaxSize );
293 // Allocates ExpectedSize bytes of memory at this->Data and copies the
294 // pointed data to it.
295 int SetImageData(void * Data, size_t ExpectedSize);
300 //class gdcmSerie : gdcmFile;
301 //class gdcmMultiFrame : gdcmFile;
305 // * gdcmFile WriteDicom;
306 // WriteDicom.SetFileName("MyDicomFile.dcm");
307 // string * AllTags = gdcmHeader.GetDcmTagNames();
308 // WriteDicom.SetDcmTag(AllTags[5], "253");
309 // WriteDicom.SetDcmTag("Patient Name", "bozo");
310 // WriteDicom.SetDcmTag("Patient Name", "bozo");
311 // WriteDicom.SetImageData(Image);
312 // WriteDicom.Write();
315 // Anonymize(ostream& output) {
316 // a = gdcmFile("toto1");
317 // a.SetPubValueByName("Patient Name", "");
318 // a.SetPubValueByName("Date", "");
319 // a.SetPubValueByName("Study Date", "");