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 string GetElValue(guint32 group, guint32 element);
161 string GetElValue(string);
164 // The various entries of the explicit value representation (VR) shall
165 // be managed within a dictionary.
166 typedef string VRKey;
167 typedef string VRAtr;
168 typedef map<TagKey, VRAtr> VRHT; // Value Representation Hash Table
170 // The typical usage of objects of this class is to classify a set of
171 // dicom files according to header information e.g. to create a file hierachy
172 // reflecting the Patient/Study/Serie informations, or extracting a given
173 // SerieId. Accesing the content (image[s] or volume[s]) is beyond the
174 // functionality of this class (see dmcFile below).
176 // * the gdcmHeader::Set*Tag* family members cannot be defined as protected
177 // (Swig limitations for as Has_a dependency between gdcmFile and gdcmHeader)
179 //FIXME sw should be qn EndianType !!!
186 // All instances share the same valur representation dictionary
187 static VRHT *dicom_vr;
188 static gdcmDictSet* Dicts; // Global dictionary container
189 gdcmDict* RefPubDict; // Public Dictionary
190 gdcmDict* RefShaDict; // Shadow Dictionary (optional)
191 ElValSet PubElVals; // Element Values parsed with Public Dictionary
192 ElValSet ShaElVals; // Element Values parsed with Shadow Dictionary
193 // In order to inspect/navigate through the file
196 // The tag Image Location ((0028,0200) containing the adress of
197 // the pixels) is not allways present. When we store this information
199 // outside of the elements:
204 size_t PixelPosition;
207 void Initialise(void);
208 void CheckSwap(void);
209 void FindLength(ElValue *);
210 void FindVR(ElValue *);
211 guint32 SwapLong(guint32);
212 short int SwapShort(short int);
213 void InitVRDict(void);
214 ElValue * ReadNextElement(void);
215 gdcmDictEntry * IsInDicts(guint32, guint32);
216 void SetAsidePixelData(ElValue*);
226 ///// QUESTION: Maybe Print is a better name than write !?
228 ///// QUESTION: Maybe anonymize should be a friend function !?!?
229 ///// See below for an example of how anonymize might be implemented.
230 int anonymize(ostream&);
232 bool IsAnInteger(guint16, guint16, string, guint32);
233 virtual void BuildHeader(void);
234 gdcmHeader(char* filename);
235 virtual ~gdcmHeader();
237 int SetPubDict(string filename);
238 // When some proprietary shadow groups are disclosed, whe can set
239 // up an additional specific dictionary to access extra information.
240 int SetShaDict(string filename);
242 // Retrieve all potentially available tag [tag = (group, element)] names
243 // from the standard (or public) dictionary (hence static). Typical usage:
244 // enable the user of a GUI based interface to select his favorite fields
245 // for sorting or selection.
246 string* GetPubTagNames();
247 // Get the element values themselves:
248 string GetPubElValByName(string TagName);
249 string GetPubElValByNumber(guint16 group, guint16 element);
250 // Get the element value representation: (VR) might be needed by caller
251 // to convert the string typed content to caller's native type (think
252 // of C/C++ vs Python).
253 string GetPubElValRepByName(string TagName);
254 string GetPubElValRepByNumber(guint16 group, guint16 element);
255 void PrintPubElVal(ostream &);
256 void PrintPubDict(ostream &);
258 // Same thing with the shadow :
259 string* GetShaTagNames();
260 string GetShaElValByName(string TagName);
261 string GetShaElValByNumber(guint16 group, guint16 element);
262 string GetShaElValRepByName(string TagName);
263 string GetShaElValRepByNumber(guint16 group, guint16 element);
265 // Wrappers of the above (both public and shadow) to avoid bugging the
266 // caller with knowing if ElVal is from the public or shadow dictionary.
267 string GetElValByName(string TagName);
268 string GetElValByNumber(guint16 group, guint16 element);
269 string GetElValRepByName(string TagName);
270 string GetElValRepByNumber(guint16 group, guint16 element);
272 int SetPubElValByName(string content, string TagName);
273 int SetPubElValByNumber(string content, guint16 group, guint16 element);
274 int SetShaElValByName(string content, string ShadowTagName);
275 int SetShaElValByNumber(string content, guint16 group, guint16 element);
280 // In addition to Dicom header exploration, this class is designed
281 // for accessing the image/volume content. One can also use it to
282 // write Dicom files.
283 ////// QUESTION: this looks still like an open question wether the
284 ////// relationship between a gdcmFile and gdcmHeader is of
285 ////// type IS_A or HAS_A !
286 class gdcmFile: gdcmHeader
290 int Parsed; // weather allready parsed
291 string OrigFileName; // To avoid file overwrite
293 // Constructor dedicated to writing a new DICOMV3 part10 compliant
294 // file (see SetFileName, SetDcmTag and Write)
296 // Opens (in read only and when possible) an existing file and checks
297 // for DICOM compliance. Returns NULL on failure.
298 // Note: the in-memory representation of all available tags found in
299 // the DICOM header is post-poned to first header information access.
300 // This avoid a double parsing of public part of the header when
301 // one sets an a posteriori shadow dictionary (efficiency can be
302 // seen a a side effect).
303 gdcmFile(string & filename);
304 // For promotion (performs a deepcopy of pointed header object)
305 gdcmFile(gdcmHeader* header);
308 // On writing purposes. When instance was created through
309 // gdcmFile(string filename) then the filename argument MUST be different
310 // from the constructor's one (no overwriting aloud).
311 int SetFileName(string filename);
313 // Allocates necessary memory, copies the data (image[s]/volume[s]) to
314 // newly allocated zone and return a pointer to it:
315 void * GetImageData();
316 // Returns size (in bytes) of required memory to contain data
317 // represented in this file.
318 size_t GetImageDataSize();
319 // Copies (at most MaxSize bytes) of data to caller's memory space.
320 // Returns an error code on failure (if MaxSize is not big enough)
321 int PutImageDataHere(void* destination, size_t MaxSize );
322 // Allocates ExpectedSize bytes of memory at this->Data and copies the
323 // pointed data to it.
324 int SetImageData(void * Data, size_t ExpectedSize);
329 //class gdcmSerie : gdcmFile;
330 //class gdcmMultiFrame : gdcmFile;
334 // * gdcmFile WriteDicom;
335 // WriteDicom.SetFileName("MyDicomFile.dcm");
336 // string * AllTags = gdcmHeader.GetDcmTagNames();
337 // WriteDicom.SetDcmTag(AllTags[5], "253");
338 // WriteDicom.SetDcmTag("Patient Name", "bozo");
339 // WriteDicom.SetDcmTag("Patient Name", "bozo");
340 // WriteDicom.SetImageData(Image);
341 // WriteDicom.Write();
344 // Anonymize(ostream& output) {
345 // a = gdcmFile("toto1");
346 // a.SetPubValueByName("Patient Name", "");
347 // a.SetPubValueByName("Date", "");
348 // a.SetPubValueByName("Study Date", "");