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)
26 // Tag based hash tables.
27 // We shall use as keys the strings (as the C++ type) obtained by
28 // concatenating the group value and the element value (both of type
29 // unsigned 16 bit integers in Dicom) expressed in hexadecimal.
30 // Example: consider the tag given as (group, element) = (0x0010, 0x0010).
31 // Then the corresponding TagKey shall be the string 0010|0010 (where
32 // the | (pipe symbol) acts as a separator). Refer to
33 // gdcmDictEntry::TranslateToKey for this conversion function.
34 typedef string TagKey;
38 guint16 group; // e.g. 0x0010
39 guint16 element; // e.g. 0x0010
40 string vr; // Value Representation i.e. some clue about the nature
41 // of the data represented e.g. "FD" short for
42 // "Floating Point Double"
43 // CLEAN ME: find the official dicom name for this field !
44 string fourth; // Fourth field containing some semantics.
45 string name; // e.g. "Patient_Name"
46 TagKey key; // This is redundant zith (group, element) but we add
47 // on efficiency purposes.
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;
64 //CLEANME gdcmDictEntry();
65 gdcmDictEntry(guint16 group, guint16 element,
66 string vr, string fourth, string vr);
67 static TagKey TranslateToKey(guint16 group, guint16 element);
68 guint16 GetGroup(void) { return group;};
69 guint16 GetElement(void){return element;};
70 string GetVR(void) {return vr; };
71 void SetVR(string in){vr = in; };
72 string GetFourth(void) {return fourth;};
73 string GetName(void) {return name;};
74 string GetKey(void) {return key;};
77 typedef map<TagKey, gdcmDictEntry*> TagHT;
79 // A single DICOM dictionary i.e. a container for a collection of dictionary
80 // entries. There should be a single public dictionary (THE dictionary of
81 // the actual DICOM v3) but as many shadow dictionaries as imagers
82 // combined with all software versions...
89 gdcmDict(char *FileName); // Read Dict from disk
90 int AppendEntry(gdcmDictEntry* NewEntry);
91 gdcmDictEntry * GetTag(guint32 group, guint32 element);
95 // Container for managing a set of loaded dictionaries. Sharing dictionaries
97 // * reloading an allready loaded dictionary.
98 // * having many in memory representations of the same dictionary.
99 #define PUBDICTNAME "DicomV3Dict"
100 #define PUBDICTFILENAME "../Dicts/dicomV3.dic"
101 typedef string DictKey;
102 typedef map<DictKey, gdcmDict*> DictSetHT;
106 int AppendDict(gdcmDict* NewDict);
108 gdcmDictSet(void); // loads THE DICOM v3 dictionary
109 int LoadDictFromFile(string filename);
110 ///// QUESTION: the following function might not be thread safe !? Maybe
111 ///// we need some mutex here, to avoid concurent creation of
112 ///// the same dictionary !?!?!
113 int LoadDictFromName(string filename);
114 int LoadDictFromFile(char*, DictKey);
115 int LoadAllDictFromDirectory(string DirectoryName);
116 int LoadDicomV3Dict(void);
117 string* GetAllDictNames();
118 void Print(ostream&);
119 gdcmDict* GetDict(DictKey DictName);
120 gdcmDict* GetDefaultPublicDict(void);
123 // The dicom header of a Dicom file contains a set of such ELement VALUES
124 // (when successfuly parsed against a given Dicom dictionary)
127 gdcmDictEntry *entry;
129 // Might prove of some interest (see _ID_DCM_ELEM)
132 string value; // used to be char * valeurElem
133 size_t Offset; // Offset from the begining of file for direct user access
134 ElValue(gdcmDictEntry*);
137 void SetLength(guint32 l){LgrElem = l; };
138 void SetValue(string val){ value = val; };
139 void SetOffset(size_t of){ Offset = of; };
140 string GetValue(void) { return value; };
141 guint32 GetLength(void) { return LgrElem; };
142 size_t GetOffset(void) { return Offset; };
143 guint16 GetGroup(void) { return entry->GetGroup(); };
144 guint16 GetElement(void) { return entry->GetElement(); };
145 string GetKey(void) { return entry->GetKey(); };
146 string GetName(void) { return entry->GetName();};
149 typedef map<TagKey, ElValue*> TagElValueHT;
150 typedef map<string, ElValue*> TagElValueNameHT;
151 // Container for a set of succefully parsed ElValues.
153 // We need both accesses with a TagKey and the Dicentry.Name
155 TagElValueNameHT NameHt;
158 void Print(ostream &);
159 void PrintByName(ostream &);
160 ElValue* GetElement(guint32 group, guint32 element);
161 string GetElValue(guint32 group, guint32 element);
162 string GetElValue(string);
163 TagElValueHT & GetTagHt(void);
166 // The various entries of the explicit value representation (VR) shall
167 // be managed within a dictionary.
168 typedef string VRKey;
169 typedef string VRAtr;
170 typedef map<TagKey, VRAtr> VRHT; // Value Representation Hash Table
172 // The typical usage of objects of this class is to classify a set of
173 // dicom files according to header information e.g. to create a file hierachy
174 // reflecting the Patient/Study/Serie informations, or extracting a given
175 // SerieId. Accesing the content (image[s] or volume[s]) is beyond the
176 // functionality of this class (see dmcFile below).
178 // * the gdcmHeader::Set*Tag* family members cannot be defined as protected
179 // (Swig limitations for as Has_a dependency between gdcmFile and gdcmHeader)
181 //FIXME sw should be qn EndianType !!!
188 // All instances share the same valur representation dictionary
189 static VRHT *dicom_vr;
190 static gdcmDictSet* Dicts; // Global dictionary container
191 gdcmDict* RefPubDict; // Public Dictionary
192 gdcmDict* RefShaDict; // Shadow Dictionary (optional)
193 ElValSet PubElVals; // Element Values parsed with Public Dictionary
194 ElValSet ShaElVals; // Element Values parsed with Shadow Dictionary
195 // In order to inspect/navigate through the file
198 // The tag Image Location ((0028,0200) containing the adress of
199 // the pixels) is not allways present. When we store this information
201 // outside of the elements:
206 guint16 ReadInt16(void);
207 guint32 ReadInt32(void);
208 guint16 SwapShort(guint16);
209 guint32 SwapLong(guint32);
210 void Initialise(void);
211 void CheckSwap(void);
212 void FindLength(ElValue *);
213 void FindVR(ElValue *);
214 void LoadElementValue(ElValue *);
215 void SkipElementValue(ElValue *);
216 void InitVRDict(void);
217 bool IsAnInteger(guint16, guint16, string, guint32);
218 ElValue * ReadNextElement(void);
219 gdcmDictEntry * IsInDicts(guint32, guint32);
220 size_t GetPixelOffset(void);
230 ///// QUESTION: Maybe Print is a better name than write !?
232 ///// QUESTION: Maybe anonymize should be a friend function !?!?
233 ///// See below for an example of how anonymize might be implemented.
234 int anonymize(ostream&);
236 void LoadElements(void);
237 virtual void ParseHeader(void);
238 gdcmHeader(char* filename);
239 virtual ~gdcmHeader();
241 int SetPubDict(string filename);
242 // When some proprietary shadow groups are disclosed, whe can set
243 // up an additional specific dictionary to access extra information.
244 int SetShaDict(string filename);
246 // Retrieve all potentially available tag [tag = (group, element)] names
247 // from the standard (or public) dictionary (hence static). Typical usage:
248 // enable the user of a GUI based interface to select his favorite fields
249 // for sorting or selection.
250 string* GetPubTagNames();
251 // Get the element values themselves:
252 string GetPubElValByName(string TagName);
253 string GetPubElValByNumber(guint16 group, guint16 element);
254 // Get the element value representation: (VR) might be needed by caller
255 // to convert the string typed content to caller's native type (think
256 // of C/C++ vs Python).
257 string GetPubElValRepByName(string TagName);
258 string GetPubElValRepByNumber(guint16 group, guint16 element);
259 void PrintPubElVal(ostream &);
260 void PrintPubDict(ostream &);
262 // Same thing with the shadow :
263 string* GetShaTagNames();
264 string GetShaElValByName(string TagName);
265 string GetShaElValByNumber(guint16 group, guint16 element);
266 string GetShaElValRepByName(string TagName);
267 string GetShaElValRepByNumber(guint16 group, guint16 element);
269 // Wrappers of the above (both public and shadow) to avoid bugging the
270 // caller with knowing if ElVal is from the public or shadow dictionary.
271 string GetElValByName(string TagName);
272 string GetElValByNumber(guint16 group, guint16 element);
273 string GetElValRepByName(string TagName);
274 string GetElValRepByNumber(guint16 group, guint16 element);
276 int SetPubElValByName(string content, string TagName);
277 int SetPubElValByNumber(string content, guint16 group, guint16 element);
278 int SetShaElValByName(string content, string ShadowTagName);
279 int SetShaElValByNumber(string content, guint16 group, guint16 element);
284 // In addition to Dicom header exploration, this class is designed
285 // for accessing the image/volume content. One can also use it to
286 // write Dicom files.
287 ////// QUESTION: this looks still like an open question wether the
288 ////// relationship between a gdcmFile and gdcmHeader is of
289 ////// type IS_A or HAS_A !
290 class gdcmFile: gdcmHeader
294 int Parsed; // weather allready parsed
295 string OrigFileName; // To avoid file overwrite
297 // Constructor dedicated to writing a new DICOMV3 part10 compliant
298 // file (see SetFileName, SetDcmTag and Write)
300 // Opens (in read only and when possible) an existing file and checks
301 // for DICOM compliance. Returns NULL on failure.
302 // Note: the in-memory representation of all available tags found in
303 // the DICOM header is post-poned to first header information access.
304 // This avoid a double parsing of public part of the header when
305 // one sets an a posteriori shadow dictionary (efficiency can be
306 // seen a a side effect).
307 gdcmFile(string & filename);
308 // For promotion (performs a deepcopy of pointed header object)
309 gdcmFile(gdcmHeader* header);
312 // On writing purposes. When instance was created through
313 // gdcmFile(string filename) then the filename argument MUST be different
314 // from the constructor's one (no overwriting aloud).
315 int SetFileName(string filename);
317 // Allocates necessary memory, copies the data (image[s]/volume[s]) to
318 // newly allocated zone and return a pointer to it:
319 void * GetImageData();
320 // Returns size (in bytes) of required memory to contain data
321 // represented in this file.
322 size_t GetImageDataSize();
323 // Copies (at most MaxSize bytes) of data to caller's memory space.
324 // Returns an error code on failure (if MaxSize is not big enough)
325 int PutImageDataHere(void* destination, size_t MaxSize );
326 // Allocates ExpectedSize bytes of memory at this->Data and copies the
327 // pointed data to it.
328 int SetImageData(void * Data, size_t ExpectedSize);
333 //class gdcmSerie : gdcmFile;
334 //class gdcmMultiFrame : gdcmFile;
338 // * gdcmFile WriteDicom;
339 // WriteDicom.SetFileName("MyDicomFile.dcm");
340 // string * AllTags = gdcmHeader.GetDcmTagNames();
341 // WriteDicom.SetDcmTag(AllTags[5], "253");
342 // WriteDicom.SetDcmTag("Patient Name", "bozo");
343 // WriteDicom.SetDcmTag("Patient Name", "bozo");
344 // WriteDicom.SetImageData(Image);
345 // WriteDicom.Write();
348 // Anonymize(ostream& output) {
349 // a = gdcmFile("toto1");
350 // a.SetPubValueByName("Patient Name", "");
351 // a.SetPubValueByName("Date", "");
352 // a.SetPubValueByName("Study Date", "");