]> Creatis software - gdcm.git/blob - src/gdcmlib.h
2afa1d1797b3b24c56b82b52a81bc4593d71fb35
[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
14 // The requirement for the hash table (or map) that we shall use:
15 // 1/ First, next, last (iterators)
16 // 2/ should be sortable (i.e. sorted by TagKey). This condition
17 //    shall be droped since the Win32/VC++ implementation doesn't look
18 //    a sorted one. Pffff....
19 // 3/ Make sure we can setup some default size value, which should be
20 //    around 4500 entries which is the average dictionary size (said JPR)
21 #include <map>
22
23 // Tag based hash tables.
24 // We shall use as keys the strings (as the C++ type) obtained by
25 // concatenating the group value and the element value (both of type
26 // unsigned 16 bit integers in Dicom) expressed in hexadecimal.
27 // Example: consider the tag given as (group, element) = (0x0010, 0x0010).
28 // Then the corresponding TagKey shall be the string 00100010 (or maybe
29 // 0x00100x0010, we need some checks here).
30 typedef string TagKey;
31 typedef map<TagKey, char*> TagHT;
32
33 // Dummy declaration for the time being
34 typedef int guint16;    // We shall need glib.h !
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<srting, 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 class gdcmHeader {      
130         enum EndianType {
131                 LittleEndian, 
132                 BadLittleEndian,
133                 BigEndian, 
134                 BadBigEndian};
135         enum FileType {
136                 Unknown = 0,
137                 TrueDicom,
138                 ExplicitVR,
139                 ImplicitVR,
140                 ACR,
141                 ACR_LIBIDO};
142 private:
143         static DictSet* Dicts;  // Global dictionary container
144         Dict* RefPubDict;                       // Public Dictionary
145         Dict* RefShaDict;                       // Shadow Dictionary (optional)
146         int swapcode;
147         ElValSet PubElVals;             // Element Values parsed with Public Dictionary
148         ElValSet ShaElVals;             // Element Values parsed with Shadow Dictionary
149         FileType filetype;
150         int sw;
151         EndianType CheckSwap();
152         void _setAcrLibido();
153         guint32 _IdDcmRecupLgr(ID_DCM_HDR *e, int sw, int *skippedLength, int *longueurLue);
154 protected:
155 ///// QUESTION: Maybe Print is a better name than write !?
156         int write(ostream&);   
157 ///// QUESTION: Maybe anonymize should be a friend function !?!?
158 /////           See below for an example of how anonymize might be implemented.
159         int anonymize(ostream&);
160 public:
161         gdcmHeader();
162         gdcmHeader(string filename);
163         ~gdcmHeader();
164
165         int SetPubDict(string filename);
166         // When some proprietary shadow groups are disclosed, whe can set
167         // up an additional specific dictionary to access extra information.
168         int SetShaDict(string filename);
169
170         // Retrieve all potentially available tag [tag = (group, element)] names
171         // from the standard (or public) dictionary (hence static). Typical usage:
172         // enable the user of a GUI based interface to select his favorite fields
173         // for sorting or selection.
174         string* GetPubTagNames();
175         // Get the element values themselves:
176         string GetPubElValByName(string TagName);
177         string GetPubElValByNumber(guint16 group, guint16 element);
178         // Get the element value representation: (VR) might be needed by caller
179         // to convert the string typed content to caller's native type (think
180         // of C/C++ vs Python).
181         string GetPubElValRepByName(string TagName);
182         string GetPubElValRepByNumber(guint16 group, guint16 element);
183           
184         // Same thing with the shadow :
185         string* GetShaTagNames(); 
186         string GetShaElValByName(string TagName);
187         string GetShaElValByNumber(guint16 group, guint16 element);
188         string GetShaElValRepByName(string TagName);
189         string GetShaElValRepByNumber(guint16 group, guint16 element);
190
191         // Wrappers of the above (both public and shadow) to avoid bugging the
192         // caller with knowing if ElVal is from the public or shadow dictionary.
193         string GetElValByName(string TagName);
194         string GetElValByNumber(guint16 group, guint16 element);
195         string GetElValRepByName(string TagName);
196         string GetElValRepByNumber(guint16 group, guint16 element);
197
198         int SetPubElValByName(string content, string TagName);
199         int SetPubElValByNumber(string content, guint16 group, guint16 element);
200         int SetShaElValByName(string content, string ShadowTagName);
201         int SetShaElValByNumber(string content, guint16 group, guint16 element);
202
203         int GetSwapCode();
204 };
205
206 // In addition to Dicom header exploration, this class is designed
207 // for accessing the image/volume content. One can also use it to
208 // write Dicom files.
209 ////// QUESTION: this looks still like an open question wether the
210 //////           relationship between a gdcmFile and gdcmHeader is of
211 //////           type IS_A or HAS_A !
212 class gdcmFile: gdcmHeader
213 {
214 private:
215         void* Data;
216         int Parsed;                             // weather allready parsed
217         string OrigFileName;    // To avoid file overwrite
218 public:
219         // Constructor dedicated to writing a new DICOMV3 part10 compliant
220         // file (see SetFileName, SetDcmTag and Write)
221         gdcmFile();
222         // Opens (in read only and when possible) an existing file and checks
223         // for DICOM compliance. Returns NULL on failure.
224         // Note: the in-memory representation of all available tags found in
225         //    the DICOM header is post-poned to first header information access.
226         //    This avoid a double parsing of public part of the header when
227         //    one sets an a posteriori shadow dictionary (efficiency can be
228         //    seen a a side effect).
229         gdcmFile(string filename);
230         // For promotion (performs a deepcopy of pointed header object)
231         gdcmFile(gdcmHeader* header);
232         ~gdcmFile();
233
234         // On writing purposes. When instance was created through
235         // gdcmFile(string filename) then the filename argument MUST be different
236         // from the constructor's one (no overwriting aloud).
237         int SetFileName(string filename);
238
239         // Allocates necessary memory, copies the data (image[s]/volume[s]) to
240         // newly allocated zone and return a pointer to it:
241         void * GetImageData();
242         // Returns size (in bytes) of required memory to contain data
243         // represented in this file.
244         size_t GetImageDataSize();
245         // Copies (at most MaxSize bytes) of data to caller's memory space.
246         // Returns an error code on failure (if MaxSize is not big enough)
247         int PutImageDataHere(void* destination, size_t MaxSize );
248         // Allocates ExpectedSize bytes of memory at this->Data and copies the
249         // pointed data to it.
250         int SetImageData(void * Data, size_t ExpectedSize);
251         // Push to disk.
252         int Write();
253 };
254
255 //class gdcmSerie : gdcmFile;
256 //class gdcmMultiFrame : gdcmFile;
257
258 //
259 //Examples:
260 // * gdcmFile WriteDicom;
261 //   WriteDicom.SetFileName("MyDicomFile.dcm");
262 //      string * AllTags = gdcmHeader.GetDcmTagNames();
263 //   WriteDicom.SetDcmTag(AllTags[5], "253");
264 //   WriteDicom.SetDcmTag("Patient Name", "bozo");
265 //   WriteDicom.SetDcmTag("Patient Name", "bozo");
266 //      WriteDicom.SetImageData(Image);
267 //   WriteDicom.Write();
268 //
269 //
270 //   Anonymize(ostream& output) {
271 //   a = gdcmFile("toto1");
272 //   a.SetPubValueByName("Patient Name", "");
273 //   a.SetPubValueByName("Date", "");
274 //   a.SetPubValueByName("Study Date", "");
275 //   a.write(output);
276 //   }