]> Creatis software - gdcm.git/blob - dcmlib.h
942870abafbf54e6d270d5518ebf0e3e86cdfc13
[gdcm.git] / dcmlib.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 private:
131         static DictSet* Dicts;  // Global dictionary container
132         Dict* RefPubDict;                       // Public Dictionary
133         Dict* RefShaDict;                       // Shadow Dictionary (optional)
134         int swapcode;
135         ElValSet PubElVals;             // Element Values parsed with Public Dictionary
136         ElValSet ShaElVals;             // Element Values parsed with Shadow Dictionary
137 protected:
138 ///// QUESTION: Maybe Print is a better name than write !?
139         int write(ostream&);   
140 ///// QUESTION: Maybe anonymize should be a friend function !?!?
141 /////           See below for an example of how anonymize might be implemented.
142         int anonymize(ostream&);
143 public:
144         gdcmHeader();
145         gdcmHeader(string filename);
146         ~gdcmHeader();
147
148         int SetPubDict(string filename);
149         // When some proprietary shadow groups are disclosed, whe can set
150         // up an additional specific dictionary to access extra information.
151         int SetShaDict(string filename);
152
153         // Retrieve all potentially available tag [tag = (group, element)] names
154         // from the standard (or public) dictionary (hence static). Typical usage:
155         // enable the user of a GUI based interface to select his favorite fields
156         // for sorting or selection.
157         string* GetPubTagNames();
158         // Get the element values themselves:
159         string GetPubElValByName(string TagName);
160         string GetPubElValByNumber(guint16 group, guint16 element);
161         // Get the element value representation: (VR) might be needed by caller
162         // to convert the string typed content to caller's native type (think
163         // of C/C++ vs Python).
164         string GetPubElValRepByName(string TagName);
165         string GetPubElValRepByNumber(guint16 group, guint16 element);
166           
167         // Same thing with the shadow :
168         string* GetShaTagNames(); 
169         string GetShaElValByName(string TagName);
170         string GetShaElValByNumber(guint16 group, guint16 element);
171         string GetShaElValRepByName(string TagName);
172         string GetShaElValRepByNumber(guint16 group, guint16 element);
173
174         // Wrappers of the above (both public and shadow) to avoid bugging the
175         // caller with knowing if ElVal is from the public or shadow dictionary.
176         string GetElValByName(string TagName);
177         string GetElValByNumber(guint16 group, guint16 element);
178         string GetElValRepByName(string TagName);
179         string GetElValRepByNumber(guint16 group, guint16 element);
180
181         int SetPubElValByName(string content, string TagName);
182         int SetPubElValByNumber(string content, guint16 group, guint16 element);
183         int SetShaElValByName(string content, string ShadowTagName);
184         int SetShaElValByNumber(string content, guint16 group, guint16 element);
185
186         int GetSwapCode();
187 };
188
189 // In addition to Dicom header exploration, this class is designed
190 // for accessing the image/volume content. One can also use it to
191 // write Dicom files.
192 ////// QUESTION: this looks still like an open question wether the
193 //////           relationship between a gdcmFile and gdcmHeader is of
194 //////           type IS_A or HAS_A !
195 class gdcmFile: gdcmHeader
196 {
197 private:
198         void* Data;
199         int Parsed;                             // weather allready parsed
200         string OrigFileName;    // To avoid file overwrite
201 public:
202         // Constructor dedicated to writing a new DICOMV3 part10 compliant
203         // file (see SetFileName, SetDcmTag and Write)
204         gdcmFile();
205         // Opens (in read only and when possible) an existing file and checks
206         // for DICOM compliance. Returns NULL on failure.
207         // Note: the in-memory representation of all available tags found in
208         //    the DICOM header is post-poned to first header information access.
209         //    This avoid a double parsing of public part of the header when
210         //    one sets an a posteriori shadow dictionary (efficiency can be
211         //    seen a a side effect).
212         gdcmFile(string filename);
213         // For promotion (performs a deepcopy of pointed header object)
214         gdcmFile(gdcmHeader* header);
215         ~gdcmFile();
216
217         // On writing purposes. When instance was created through
218         // gdcmFile(string filename) then the filename argument MUST be different
219         // from the constructor's one (no overwriting aloud).
220         int SetFileName(string filename);
221
222         // Allocates necessary memory, copies the data (image[s]/volume[s]) to
223         // newly allocated zone and return a pointer to it:
224         void * GetImageData();
225         // Returns size (in bytes) of required memory to contain data
226         // represented in this file.
227         size_t GetImageDataSize();
228         // Copies (at most MaxSize bytes) of data to caller's memory space.
229         // Returns an error code on failure (if MaxSize is not big enough)
230         int PutImageDataHere(void* destination, size_t MaxSize );
231         // Allocates ExpectedSize bytes of memory at this->Data and copies the
232         // pointed data to it.
233         int SetImageData(void * Data, size_t ExpectedSize);
234         // Push to disk.
235         int Write();
236 };
237
238 //class gdcmSerie : gdcmFile;
239 //class gdcmMultiFrame : gdcmFile;
240
241 //
242 //Examples:
243 // * gdcmFile WriteDicom;
244 //   WriteDicom.SetFileName("MyDicomFile.dcm");
245 //      string * AllTags = gdcmHeader.GetDcmTagNames();
246 //   WriteDicom.SetDcmTag(AllTags[5], "253");
247 //   WriteDicom.SetDcmTag("Patient Name", "bozo");
248 //   WriteDicom.SetDcmTag("Patient Name", "bozo");
249 //      WriteDicom.SetImageData(Image);
250 //   WriteDicom.Write();
251 //
252 //
253 //   Anonymize(ostream& output) {
254 //   a = gdcmFile("toto1");
255 //   a.SetPubValueByName("Patient Name", "");
256 //   a.SetPubValueByName("Date", "");
257 //   a.SetPubValueByName("Study Date", "");
258 //   a.write(output);
259 //   }