]> Creatis software - gdcm.git/blob - src/gdcm.h
* python/gdcm.i: added a typemap that converts a C++ hashing table
[gdcm.git] / src / gdcm.h
1 // gdcmlib Intro:  
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
6 //   on reading and 
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
10 //
11 //
12 // TODO
13 // The declarations commented out and starting with "TODO Swig" needed
14 // to be temporarily removed for swig to proceed correctly (in fact
15 // problems appears at loading of _gdcm.[so/dll]). So, simply uncomment
16 // the declaration once you provided the definition of the method...
17
18 #include <string>
19 #include <stddef.h>    // For size_t
20 #include <glib.h>
21 #include <stdio.h>
22
23 // The requirement for the hash table (or map) that we shall use:
24 // 1/ First, next, last (iterators)
25 // 2/ should be sortable (i.e. sorted by TagKey). This condition
26 //    shall be droped since the Win32/VC++ implementation doesn't look
27 //    a sorted one. Pffff....
28 // 3/ Make sure we can setup some default size value, which should be
29 //    around 4500 entries which is the average dictionary size (said JPR)
30 #include <map>
31
32
33 // Tag based hash tables.
34 // We shall use as keys the strings (as the C++ type) obtained by
35 // concatenating the group value and the element value (both of type
36 // unsigned 16 bit integers in Dicom) expressed in hexadecimal.
37 // Example: consider the tag given as (group, element) = (0x0010, 0x0010).
38 // Then the corresponding TagKey shall be the string 0010|0010 (where
39 // the | (pipe symbol) acts as a separator). Refer to 
40 // gdcmDictEntry::TranslateToKey for this conversion function.
41 typedef string TagKey;
42
43 class gdcmDictEntry {
44 private:
45         guint16 group;    // e.g. 0x0010
46         guint16 element;  // e.g. 0x0010
47         string  vr;       // Value Representation i.e. some clue about the nature
48                           // of the data represented e.g. "FD" short for
49                           // "Floating Point Double"
50         // CLEAN ME: find the official dicom name for this field !
51         string  fourth;   // Fourth field containing some semantics.
52         string  name;     // e.g. "Patient_Name"
53         TagKey  key;      // This is redundant zith (group, element) but we add
54                           // on efficiency purposes.
55         // DCMTK has many fields for handling a DictEntry (see below). What are the
56         // relevant ones for gdcmlib ?
57         //      struct DBI_SimpleEntry {
58         //         Uint16 group;
59         //         Uint16 element;
60         //         Uint16 upperGroup;
61         //         Uint16 upperElement;
62         //         DcmEVR evr;
63         //         const char* tagName;
64         //         int vmMin;
65         //         int vmMax;
66         //         const char* standardVersion;
67         //         DcmDictRangeRestriction groupRestriction;
68         //         DcmDictRangeRestriction elementRestriction;
69         //       };
70 public:
71         //CLEANME gdcmDictEntry();
72         gdcmDictEntry(guint16 group, guint16 element,
73                       string vr, string fourth, string vr);
74         static TagKey TranslateToKey(guint16 group, guint16 element);
75         guint16 GetGroup(void)  { return group;};
76         guint16 GetElement(void){return element;};
77         string  GetVR(void)     {return vr; };
78         void    SetVR(string in){vr = in; };
79         string  GetFourth(void) {return fourth;};
80         string  GetName(void)   {return name;};
81         string  GetKey(void)    {return key;};
82 };
83   
84 typedef map<TagKey, gdcmDictEntry*> TagHT;
85
86 // A single DICOM dictionary i.e. a container for a collection of dictionary
87 // entries. There should be a single public dictionary (THE dictionary of
88 // the actual DICOM v3) but as many shadow dictionaries as imagers 
89 // combined with all software versions...
90 class gdcmDict {
91         string name;
92         string filename;
93         TagHT entries;
94 public:
95         gdcmDict(const char* FileName);   // Read Dict from disk
96         // TODO Swig int AppendEntry(gdcmDictEntry* NewEntry);
97         gdcmDictEntry * GetTag(guint32 group, guint32 element);
98         void Print(ostream&);
99 };
100
101 // Container for managing a set of loaded dictionaries. Sharing dictionaries
102 // should avoid :
103 // * reloading an allready loaded dictionary.
104 // * having many in memory representations of the same dictionary.
105 typedef string DictKey;
106 typedef map<DictKey, gdcmDict*> DictSetHT;
107 class gdcmDictSet {
108 private:
109         string DictPath;      // Directory path to dictionaries
110         DictSetHT dicts;
111         int AppendDict(gdcmDict* NewDict);
112         int LoadDictFromFile(string filename, DictKey);
113         void SetDictPath(void);
114 public:
115         gdcmDictSet(void);    // loads THE DICOM v3 dictionary
116         // TODO Swig int LoadDictFromFile(string filename);
117 ///// QUESTION: the following function might not be thread safe !? Maybe
118 /////           we need some mutex here, to avoid concurent creation of
119 /////           the same dictionary !?!?!
120         // TODO Swig int LoadDictFromName(string filename);
121         // TODO Swig int LoadAllDictFromDirectory(string DirectoryName);
122         // TODO Swig string* GetAllDictNames();
123         int LoadDicomV3Dict(void);
124         void Print(ostream&);
125         gdcmDict* GetDict(DictKey DictName);
126         gdcmDict* GetDefaultPublicDict(void);
127 };
128
129 // The dicom header of a Dicom file contains a set of such ELement VALUES
130 // (when successfuly parsed against a given Dicom dictionary)
131 class ElValue {
132 private:
133         gdcmDictEntry *entry;
134         guint32 LgrElem;
135         // Might prove of some interest (see _ID_DCM_ELEM)
136         // int Swap;
137 public:
138         string  value;     // used to be char * valeurElem
139         size_t Offset;     // Offset from the begining of file for direct user access
140         ElValue(gdcmDictEntry*);
141         void   SetVR(string);
142         string GetVR(void);
143         void SetLength(guint32 l){LgrElem = l; };
144         void SetValue(string val){ value = val; };
145         void SetOffset(size_t of){ Offset = of; };
146         string  GetValue(void)   { return value; };
147         guint32 GetLength(void) { return LgrElem; };
148         size_t  GetOffset(void)  { return Offset; };
149         guint16 GetGroup(void)   { return entry->GetGroup(); };
150         guint16 GetElement(void) { return entry->GetElement(); };
151         string  GetKey(void)     { return entry->GetKey(); };
152         string  GetName(void)    { return entry->GetName();};
153 };
154
155 typedef map<TagKey, ElValue*> TagElValueHT;
156 typedef map<string, ElValue*> TagElValueNameHT;
157 // Container for a set of succefully parsed ElValues.
158 class ElValSet {
159         // We need both accesses with a TagKey and the Dicentry.Name
160         TagElValueHT tagHt;
161         TagElValueNameHT NameHt;
162 public:
163         void Add(ElValue*);
164         void Print(ostream &);
165         void PrintByName(ostream &);
166         ElValue* GetElement(guint32 group, guint32 element);
167         string GetElValue(guint32 group, guint32 element);
168         string GetElValue(string);
169         TagElValueHT & GetTagHt(void);
170 };
171
172 // The various entries of the explicit value representation (VR) shall
173 // be managed within a dictionary. 
174 typedef string VRKey;
175 typedef string VRAtr;
176 typedef map<TagKey, VRAtr> VRHT;    // Value Representation Hash Table
177
178 // The typical usage of objects of this class is to classify a set of
179 // dicom files according to header information e.g. to create a file hierachy
180 // reflecting the Patient/Study/Serie informations, or extracting a given
181 // SerieId. Accesing the content (image[s] or volume[s]) is beyond the
182 // functionality of this class (see dmcFile below).
183 // Notes:
184 // * the gdcmHeader::Set*Tag* family members cannot be defined as protected
185 //   (Swig limitations for as Has_a dependency between gdcmFile and gdcmHeader)
186 class gdcmHeader {      
187 //FIXME sw should be qn EndianType !!!
188         //enum EndianType {
189                 //LittleEndian, 
190                 //BadLittleEndian,
191                 //BigEndian, 
192                 //BadBigEndian};
193 private:
194         // All instances share the same value representation dictionary
195         static VRHT *dicom_vr;
196         static gdcmDictSet* Dicts;  // Global dictionary container
197         gdcmDict* RefPubDict;       // Public Dictionary
198         gdcmDict* RefShaDict;       // Shadow Dictionary (optional)
199         ElValSet PubElVals;     // Element Values parsed with Public Dictionary
200         ElValSet ShaElVals;     // Element Values parsed with Shadow Dictionary
201         // In order to inspect/navigate through the file
202         string filename;
203         FILE * fp;
204         // The tag Image Location ((0028,0200) containing the adress of
205         // the pixels) is not allways present. When we store this information
206         // FIXME
207         // outside of the elements:
208         guint16 grPixel;
209         guint16 numPixel;
210         int sw;
211
212         guint16 ReadInt16(void);
213         guint32 ReadInt32(void);
214         guint16 SwapShort(guint16);
215         guint32 SwapLong(guint32);
216         void Initialise(void);
217         void CheckSwap(void);
218         void FindLength(ElValue *);
219         void FindVR(ElValue *);
220         void LoadElementValue(ElValue *);
221         void SkipElementValue(ElValue *);
222         void InitVRDict(void);
223         bool IsAnInteger(guint16, guint16, string, guint32);
224         ElValue * ReadNextElement(void);
225         gdcmDictEntry * IsInDicts(guint32, guint32);
226         size_t GetPixelOffset(void);
227 protected:
228         enum FileType {
229                 Unknown = 0,
230                 TrueDicom,
231                 ExplicitVR,
232                 ImplicitVR,
233                 ACR,
234                 ACR_LIBIDO};
235         FileType filetype;
236 ///// QUESTION: Maybe Print is a better name than write !?
237         int write(ostream&);   
238 ///// QUESTION: Maybe anonymize should be a friend function !?!?
239 /////           See below for an example of how anonymize might be implemented.
240         int anonymize(ostream&);
241 public:
242         void LoadElements(void);
243         virtual void ParseHeader(void);
244         gdcmHeader(const char* filename);
245         virtual ~gdcmHeader();
246
247         // TODO Swig int SetPubDict(string filename);
248         // When some proprietary shadow groups are disclosed, whe can set
249         // up an additional specific dictionary to access extra information.
250         // TODO Swig int SetShaDict(string filename);
251
252         // Retrieve all potentially available tag [tag = (group, element)] names
253         // from the standard (or public) dictionary (hence static). Typical usage:
254         // enable the user of a GUI based interface to select his favorite fields
255         // for sorting or selection.
256         // TODO Swig string* GetPubTagNames();
257         // Get the element values themselves:
258         string GetPubElValByName(string TagName);
259         string GetPubElValByNumber(guint16 group, guint16 element);
260         // Get the element value representation: (VR) might be needed by caller
261         // to convert the string typed content to caller's native type (think
262         // of C/C++ vs Python).
263         // TODO Swig string GetPubElValRepByName(string TagName);
264         // TODO Swig string GetPubElValRepByNumber(guint16 group, guint16 element);
265         TagElValueHT & GetPubElVal(void) { return PubElVals.GetTagHt(); };
266         void   PrintPubElVal(ostream & os = std::cout);
267         void   PrintPubDict(ostream &);
268           
269         // Same thing with the shadow :
270         // TODO Swig string* GetShaTagNames(); 
271         // TODO Swig string GetShaElValByName(string TagName);
272         // TODO Swig string GetShaElValByNumber(guint16 group, guint16 element);
273         // TODO Swig string GetShaElValRepByName(string TagName);
274         // TODO Swig string GetShaElValRepByNumber(guint16 group, guint16 element);
275
276         // Wrappers of the above (both public and shadow) to avoid bugging the
277         // caller with knowing if ElVal is from the public or shadow dictionary.
278         // TODO Swig string GetElValByName(string TagName);
279         // TODO Swig string GetElValByNumber(guint16 group, guint16 element);
280         // TODO Swig string GetElValRepByName(string TagName);
281         // TODO Swig string GetElValRepByNumber(guint16 group, guint16 element);
282
283         // TODO Swig int SetPubElValByName(string content, string TagName);
284         // TODO Swig int SetPubElValByNumber(string content, guint16 group, guint16 element);
285         // TODO Swig int SetShaElValByName(string content, string ShadowTagName);
286         // TODO Swig int SetShaElValByNumber(string content, guint16 group, guint16 element);
287
288         // TODO Swig int GetSwapCode();
289 };
290
291 // In addition to Dicom header exploration, this class is designed
292 // for accessing the image/volume content. One can also use it to
293 // write Dicom files.
294 ////// QUESTION: this looks still like an open question wether the
295 //////           relationship between a gdcmFile and gdcmHeader is of
296 //////           type IS_A or HAS_A !
297 class gdcmFile: gdcmHeader
298 {
299 private:
300         void* Data;
301         int Parsed;                             // weather allready parsed
302         string OrigFileName;    // To avoid file overwrite
303 public:
304         // Constructor dedicated to writing a new DICOMV3 part10 compliant
305         // file (see SetFileName, SetDcmTag and Write)
306         // TODO Swig gdcmFile();
307         // Opens (in read only and when possible) an existing file and checks
308         // for DICOM compliance. Returns NULL on failure.
309         // Note: the in-memory representation of all available tags found in
310         //    the DICOM header is post-poned to first header information access.
311         //    This avoid a double parsing of public part of the header when
312         //    one sets an a posteriori shadow dictionary (efficiency can be
313         //    seen a a side effect).
314         gdcmFile(string & filename);
315         // For promotion (performs a deepcopy of pointed header object)
316         // TODO Swig gdcmFile(gdcmHeader* header);
317         // TODO Swig ~gdcmFile();
318
319         // On writing purposes. When instance was created through
320         // gdcmFile(string filename) then the filename argument MUST be different
321         // from the constructor's one (no overwriting aloud).
322         // TODO Swig int SetFileName(string filename);
323
324         // Allocates necessary memory, copies the data (image[s]/volume[s]) to
325         // newly allocated zone and return a pointer to it:
326         // TODO Swig void * GetImageData();
327         // Returns size (in bytes) of required memory to contain data
328         // represented in this file.
329         // TODO Swig size_t GetImageDataSize();
330         // Copies (at most MaxSize bytes) of data to caller's memory space.
331         // Returns an error code on failure (if MaxSize is not big enough)
332         // TODO Swig int PutImageDataHere(void* destination, size_t MaxSize );
333         // Allocates ExpectedSize bytes of memory at this->Data and copies the
334         // pointed data to it.
335         // TODO Swig int SetImageData(void * Data, size_t ExpectedSize);
336         // Push to disk.
337         // TODO Swig int Write();
338 };
339
340 //class gdcmSerie : gdcmFile;
341 //class gdcmMultiFrame : gdcmFile;
342
343 //
344 //Examples:
345 // * gdcmFile WriteDicom;
346 //   WriteDicom.SetFileName("MyDicomFile.dcm");
347 //      string * AllTags = gdcmHeader.GetDcmTagNames();
348 //   WriteDicom.SetDcmTag(AllTags[5], "253");
349 //   WriteDicom.SetDcmTag("Patient Name", "bozo");
350 //   WriteDicom.SetDcmTag("Patient Name", "bozo");
351 //      WriteDicom.SetImageData(Image);
352 //   WriteDicom.Write();
353 //
354 //
355 //   Anonymize(ostream& output) {
356 //   a = gdcmFile("toto1");
357 //   a.SetPubValueByName("Patient Name", "");
358 //   a.SetPubValueByName("Date", "");
359 //   a.SetPubValueByName("Study Date", "");
360 //   a.write(output);
361 //   }