]> Creatis software - gdcm.git/blob - src/gdcmlib.h
* src/gdcmHeader now contains_IdDcmCheckSwap, _IdDcmRecupLgr,
[gdcm.git] / src / gdcmlib.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 #include <string>
12 #include <stddef.h>    // For size_t
13 #include <glib.h>
14 #include <stdio.h>
15
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)
23 #include <map>
24
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;
34
35
36 class DictEntry {
37 private:
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 {
51         //         Uint16 group;
52         //         Uint16 element;
53         //         Uint16 upperGroup;
54         //         Uint16 upperElement;
55         //         DcmEVR evr;
56         //         const char* tagName;
57         //         int vmMin;
58         //         int vmMax;
59         //         const char* standardVersion;
60         //         DcmDictRangeRestriction groupRestriction;
61         //         DcmDictRangeRestriction elementRestriction;
62         //       };
63 public:
64         DictEntry();
65         DictEntry(guint16 group, guint16 element, string  name, string  VR);
66 };
67
68 // A single DICOM dictionary i.e. a container for a collection of dictionary
69 // entries. There should be a single public dictionary (THE dictionary of
70 // the actual DICOM v3) but as many shadow dictionaries as imagers 
71 // combined with all software versions...
72 class Dict {
73         string name;
74         string filename;
75         TagHT entries;
76 public:
77         Dict();
78         Dict(string filename);  // Read Dict from disk
79         int AppendEntry(DictEntry* NewEntry);
80 };
81
82 // Container for managing a set of loaded dictionaries. Sharing dictionaries
83 // should avoid :
84 // * reloading an allready loaded dictionary.
85 // * having many in memory representations of the same dictionary.
86 typedef int DictId;
87 class DictSet {
88 private:
89         map<DictId, Dict*> dicts;
90         int AppendDict(Dict* NewDict);
91 public:
92         DictSet();              // Default constructor loads THE DICOM v3 dictionary
93         int LoadDictFromFile(string filename);
94 ///// QUESTION: the following function might not be thread safe !? Maybe
95 /////           we need some mutex here, to avoid concurent creation of
96 /////           the same dictionary !?!?!
97         int LoadDictFromName(string filename);
98         int LoadAllDictFromDirectory(string directorynanme);
99         string* GetAllDictNames();
100         Dict* GetDict(string DictName);
101 };
102
103 // The dicom header of a Dicom file contains a set of such ELement VALUES
104 // (when successfuly parsed against a given Dicom dictionary)
105 class ElValue {
106         DictEntry entry;
107         string  value;
108 };
109
110 // Container for a set of succefully parsed ElValues.
111 typedef map<TagKey, char*> TagHT;
112 class ElValSet {
113         // We need both accesses with a TagKey and the Dicentry.Name
114 ////// QUESTION: this leads to a double storage of a single ElValue
115         map<TagKey, ElValue> tagHt;
116         map<string, ElValue> NameHt;
117 public:
118         int Add(ElValue);
119 };
120
121 // The typical usage of objects of this class is to classify a set of
122 // dicom files according to header information e.g. to create a file hierachy
123 // reflecting the Patient/Study/Serie informations, or extracting a given
124 // SerieId. Accesing the content (image[s] or volume[s]) is beyond the
125 // functionality of this class (see dmcFile below).
126 // Notes:
127 // * the gdcmHeader::Set*Tag* family members cannot be defined as protected
128 //   (Swig limitations for as Has_a dependency between gdcmFile and gdcmHeader)
129 typedef int _ID_DCM_ELEM;
130 class gdcmHeader {      
131         //enum EndianType {
132                 //LittleEndian, 
133                 //BadLittleEndian,
134                 //BigEndian, 
135                 //BadBigEndian};
136         enum FileType {
137                 Unknown = 0,
138                 TrueDicom,
139                 ExplicitVR,
140                 ImplicitVR,
141                 ACR,
142                 ACR_LIBIDO};
143 private:
144         static DictSet* Dicts;  // Global dictionary container
145         Dict* RefPubDict;       // Public Dictionary
146         Dict* RefShaDict;       // Shadow Dictionary (optional)
147         ElValSet PubElVals;     // Element Values parsed with Public Dictionary
148         ElValSet ShaElVals;     // Element Values parsed with Shadow Dictionary
149         FileType filetype;
150         FILE * fp;
151         long int offsetCourant;
152         int sw;
153         void CheckSwap(void);
154         void setAcrLibido(void);
155         long int RecupLgr(_ID_DCM_ELEM *pleCourant, int sw,
156                           int *skippedLength, int *longueurLue);
157         guint32 SWAP_LONG(guint32);
158 protected:
159 ///// QUESTION: Maybe Print is a better name than write !?
160         int write(ostream&);   
161 ///// QUESTION: Maybe anonymize should be a friend function !?!?
162 /////           See below for an example of how anonymize might be implemented.
163         int anonymize(ostream&);
164 public:
165         gdcmHeader();
166         gdcmHeader(string filename);
167         ~gdcmHeader();
168
169         int SetPubDict(string filename);
170         // When some proprietary shadow groups are disclosed, whe can set
171         // up an additional specific dictionary to access extra information.
172         int SetShaDict(string filename);
173
174         // Retrieve all potentially available tag [tag = (group, element)] names
175         // from the standard (or public) dictionary (hence static). Typical usage:
176         // enable the user of a GUI based interface to select his favorite fields
177         // for sorting or selection.
178         string* GetPubTagNames();
179         // Get the element values themselves:
180         string GetPubElValByName(string TagName);
181         string GetPubElValByNumber(guint16 group, guint16 element);
182         // Get the element value representation: (VR) might be needed by caller
183         // to convert the string typed content to caller's native type (think
184         // of C/C++ vs Python).
185         string GetPubElValRepByName(string TagName);
186         string GetPubElValRepByNumber(guint16 group, guint16 element);
187           
188         // Same thing with the shadow :
189         string* GetShaTagNames(); 
190         string GetShaElValByName(string TagName);
191         string GetShaElValByNumber(guint16 group, guint16 element);
192         string GetShaElValRepByName(string TagName);
193         string GetShaElValRepByNumber(guint16 group, guint16 element);
194
195         // Wrappers of the above (both public and shadow) to avoid bugging the
196         // caller with knowing if ElVal is from the public or shadow dictionary.
197         string GetElValByName(string TagName);
198         string GetElValByNumber(guint16 group, guint16 element);
199         string GetElValRepByName(string TagName);
200         string GetElValRepByNumber(guint16 group, guint16 element);
201
202         int SetPubElValByName(string content, string TagName);
203         int SetPubElValByNumber(string content, guint16 group, guint16 element);
204         int SetShaElValByName(string content, string ShadowTagName);
205         int SetShaElValByNumber(string content, guint16 group, guint16 element);
206
207         int GetSwapCode();
208 };
209
210 // In addition to Dicom header exploration, this class is designed
211 // for accessing the image/volume content. One can also use it to
212 // write Dicom files.
213 ////// QUESTION: this looks still like an open question wether the
214 //////           relationship between a gdcmFile and gdcmHeader is of
215 //////           type IS_A or HAS_A !
216 class gdcmFile: gdcmHeader
217 {
218 private:
219         void* Data;
220         int Parsed;                             // weather allready parsed
221         string OrigFileName;    // To avoid file overwrite
222 public:
223         // Constructor dedicated to writing a new DICOMV3 part10 compliant
224         // file (see SetFileName, SetDcmTag and Write)
225         gdcmFile();
226         // Opens (in read only and when possible) an existing file and checks
227         // for DICOM compliance. Returns NULL on failure.
228         // Note: the in-memory representation of all available tags found in
229         //    the DICOM header is post-poned to first header information access.
230         //    This avoid a double parsing of public part of the header when
231         //    one sets an a posteriori shadow dictionary (efficiency can be
232         //    seen a a side effect).
233         gdcmFile(string filename);
234         // For promotion (performs a deepcopy of pointed header object)
235         gdcmFile(gdcmHeader* header);
236         ~gdcmFile();
237
238         // On writing purposes. When instance was created through
239         // gdcmFile(string filename) then the filename argument MUST be different
240         // from the constructor's one (no overwriting aloud).
241         int SetFileName(string filename);
242
243         // Allocates necessary memory, copies the data (image[s]/volume[s]) to
244         // newly allocated zone and return a pointer to it:
245         void * GetImageData();
246         // Returns size (in bytes) of required memory to contain data
247         // represented in this file.
248         size_t GetImageDataSize();
249         // Copies (at most MaxSize bytes) of data to caller's memory space.
250         // Returns an error code on failure (if MaxSize is not big enough)
251         int PutImageDataHere(void* destination, size_t MaxSize );
252         // Allocates ExpectedSize bytes of memory at this->Data and copies the
253         // pointed data to it.
254         int SetImageData(void * Data, size_t ExpectedSize);
255         // Push to disk.
256         int Write();
257 };
258
259 //class gdcmSerie : gdcmFile;
260 //class gdcmMultiFrame : gdcmFile;
261
262 //
263 //Examples:
264 // * gdcmFile WriteDicom;
265 //   WriteDicom.SetFileName("MyDicomFile.dcm");
266 //      string * AllTags = gdcmHeader.GetDcmTagNames();
267 //   WriteDicom.SetDcmTag(AllTags[5], "253");
268 //   WriteDicom.SetDcmTag("Patient Name", "bozo");
269 //   WriteDicom.SetDcmTag("Patient Name", "bozo");
270 //      WriteDicom.SetImageData(Image);
271 //   WriteDicom.Write();
272 //
273 //
274 //   Anonymize(ostream& output) {
275 //   a = gdcmFile("toto1");
276 //   a.SetPubValueByName("Patient Name", "");
277 //   a.SetPubValueByName("Date", "");
278 //   a.SetPubValueByName("Study Date", "");
279 //   a.write(output);
280 //   }