Debian Codegraph @

biosig4c++/t210/sopen_dcmtk_read.cpp

raw
1 /*
2 Copyright (C) 2018-2022 Alois Schloegl <[email protected]>
3 This file is part of the "BioSig for C/C++" repository
4 (biosig4c++) at https://biosig.sourceforge.io
5
6 BioSig is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 3
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <assert.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25
26 #include "../biosig.h"
27
28 // these functions are stubs
29
30 #ifdef WITH_DCMTK
31 #undef WITH_DICOM
32 #undef WITH_GDCM
33
34 /*
35 DCMTK read files
36 https://support.dcmtk.org/redmine/projects/dcmtk/wiki/Howto_LoadMetaHeader
37 */
38 #include "dcmtk/config/osconfig.h"
39 #include "dcmtk/dcmdata/dctk.h"
40 #include "dcmtk/dcmdata/dcistrmf.h"
41
42 #if (PACKAGE_VERSION_NUMBER >= 367) // seems to work for some earlier versions like e.g. 3.6.5, too
43 #include <cstdio>
44 #include <cstdlib>
45 #include <cstring>
46 #else
47 #define INCLUDE_CSTRING
48 #define INCLUDE_CSTDLIB
49 #define INCLUDE_CSTDIO
50 #endif
51
52 #include "dcmtk/ofstd/ofstdinc.h"
53
54 #include "dcmtk/dcmdata/cmdlnarg.h"
55 #include "dcmtk/ofstd/ofconapp.h"
56 #include "dcmtk/dcmdata/dcuid.h" /* for dcmtk version name */
57 #include "dcmtk/dcmdata/dcostrmz.h" /* for dcmZlibCompressionLevel */
58 #include "dcmtk/dcmdata/dcistrmz.h" /* for dcmZlibExpectRFC1950Encoding */
59 #include "dcmtk/dcmdata/dcfilefo.h" /* for convertToUTF8 */
60
61 #ifdef HAVE_ZLIB
62 #include <zlib.h> /* for zlibVersion() */
63 #endif
64 #ifdef DCMTK_ENABLE_CHARSET_CONVERSION
65 #include "dcmtk/ofstd/ofchrenc.h" /* for OFCharacterEncoding */
66 #endif
67
68 EXTERN_C int sopen_dcmtk_read(HDRTYPE* hdr) {
69
70 if (PACKAGE_VERSION_NUMBER < 361)
71 fprintf(stderr,"WARNING: DCMTK is not 3.6.1 but %s - this is not tested .\n",PACKAGE_VERSION);
72
73 if (VERBOSE_LEVEL > 7) fprintf(stdout,"# %s (line %d): DCMTK %s is used to read dicom files.\n",__func__,__LINE__, PACKAGE_VERSION);
74
75 OFCondition status;
76
77 E_FileReadMode opt_readMode = ERM_autoDetect;
78 E_TransferSyntax opt_ixfer = EXS_Unknown;
79 E_TransferSyntax opt_oxfer = EXS_Unknown;
80 E_GrpLenEncoding opt_oglenc = EGL_recalcGL;
81 E_EncodingType opt_oenctype = EET_ExplicitLength;
82 E_PaddingEncoding opt_opadenc = EPD_noChange;
83 OFCmdUnsignedInt opt_filepad = 0;
84 OFCmdUnsignedInt opt_itempad = 0;
85
86 #ifdef WITH_ZLIB
87 OFCmdUnsignedInt opt_compressionLevel = 0;
88 #endif
89 #ifdef DCMTK_ENABLE_CHARSET_CONVERSION
90 const char *opt_convertToCharset = NULL;
91 size_t opt_conversionFlags = 0;
92 #endif
93
94 DcmFileFormat dfile;
95
96 if (dfile.loadFile(hdr->FileName, opt_ixfer, EGL_noChange, DCM_MaxReadLength, opt_readMode).bad()) {
97 fprintf(stderr,"# %s (line %d): DCMTK failed to read dicom file %s.\n",__func__,__LINE__, hdr->FileName);
98 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "DICOM support not completed");
99 return -1;
100 }
101
102 if (VERBOSE_LEVEL > 7) fprintf(stdout,"# %s (line %d): loadFile successful.\n",__func__,__LINE__);
103
104 OFString csetString;
105 DcmDataset *dset = dfile.getDataset();
106 if (dset->findAndGetOFStringArray(DCM_SpecificCharacterSet, csetString).good()) {
107
108 if (VERBOSE_LEVEL > 7) fprintf(stdout,"# %s (line %d): findAndGetOFStringArray successful.\n",__func__,__LINE__);
109
110 if (csetString.compare("ISO_IR 192") != 0 && csetString.compare("ISO_IR 6") != 0) {
111 #ifdef DCMTK_ENABLE_CHARSET_CONVERSION
112 /* convert all DICOM strings to UTF-8 */
113 if (VERBOSE_LEVEL>7) fprintf(stdout,"# %s (line %d): converting all element values that are affected by SpecificCharacterSet (0008,0005) to UTF-8.\n",__func__,__LINE__);
114
115 status = dset->convertToUTF8();
116 if (status.bad()) biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "converting file to UTF-8 failed");
117 #else
118 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "DICOM read error (character set conversion not available)");
119 #endif
120 }
121 }
122
123 if (VERBOSE_LEVEL > 7) fprintf(stdout,"# %s (line %d): \n",__func__,__LINE__);
124 /*
125 // this fails for example file
126 cond = dfile.convertToUTF8();
127 if (cond.bad())
128 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "converting file to UTF-8 failed");
129
130 if (VERBOSE_LEVEL > 7) fprintf(stdout,"# %s (line %d): convertToUTF8 successful.\n",__func__,__LINE__);
131 */
132
133 /***** extract header1 fields *****/
134 DcmTagKey key;
135 const char *str;
136
137 if (dset->findAndGetString(DCM_PatientName, str).good())
138 strncpy(hdr->Patient.Name, str, MAX_LENGTH_NAME+1);
139
140 if (dset->findAndGetString(DCM_PatientID, str).good())
141 strncpy(hdr->Patient.Id, str, MAX_LENGTH_PID+1);
142
143 if (dset->findAndGetString(DCM_PatientWeight, str).good())
144 hdr->Patient.Weight = atol(str);
145
146 if (dset->findAndGetString(DCM_PatientBirthDate, str).good()) {
147 if ( (str != NULL) && (strlen(str) == 8)) {
148 char *datestr = strdup(str);
149 struct tm t;
150 t.tm_mday=atoi(datestr+6);
151 datestr[6]=0;
152 t.tm_mon=atoi(datestr+4)-1;
153 datestr[4]=0;
154 t.tm_year=atoi(datestr);
155 free(datestr);
156 hdr->T0 = tm_time2gdf_time(&t);
157 }
158 else {
159 if (VERBOSE_LEVEL > 7) fprintf(stdout,"# %s (line %d): invalid birthday string <%s>\n",__func__,__LINE__,str ? str : "");
160 }
161 }
162
163 if (dset->findAndGetString(DCM_PatientSex, str).good() && str!=NULL) {
164 if (strlen(str)==0)
165 hdr->Patient.Sex = 0; // unknown
166 else if (str[0]=='m' || str[0]=='M')
167 hdr->Patient.Sex = 1; // male
168 else if (str[0]=='f' || str[0]=='F')
169 hdr->Patient.Sex = 2; // female
170 }
171
172 if (dset->findAndGetString(DCM_AcquisitionDateTime, str).good() && str!=NULL) {
173 if ( (str != NULL) && (strlen(str) == 8)) {
174 char *datestr = strdup(str);
175 struct tm t0;
176 datestr[14]=0;
177 t0.tm_sec = atoi((char*)datestr+12);
178 datestr[12]=0;
179 t0.tm_min = atoi((char*)datestr+10);
180 datestr[10]=0;
181 t0.tm_hour = atoi((char*)datestr+8);
182 datestr[8]=0;
183 t0.tm_mday = atoi((char*)datestr+6);
184 datestr[6]=0;
185 t0.tm_mon = atoi((char*)datestr+4)-1;
186 datestr[4]=0;
187 t0.tm_year = atoi((char*)datestr)-1900;
188
189 hdr->T0 = tm_time2gdf_time(&t0);
190 }
191 else {
192 if (VERBOSE_LEVEL > 7) fprintf(stdout,"# %s (line %d): invalid birthday string <%s>\n",__func__,__LINE__,str ? str : "");
193 }
194 }
195
196 if (dset->findAndGetString(DCM_Manufacturer, str).good())
197 if (str) strncpy(hdr->ID.Manufacturer._field, str, MAX_LENGTH_MANUF+1);
198
199 if (dset->findAndGetString(DCM_InstitutionName, str).good())
200 hdr->ID.Hospital = strdup(str);
201
202 if (VERBOSE_LEVEL > 7) fprintf(stdout,"# %s (line %d): read H1 successful.\n",__func__,__LINE__);
203
204 DcmSequenceOfItems *seq = NULL;
205 if (dset->findAndGetSequence(DCM_WaveformSequence, seq).bad() || (seq==NULL)) {
206 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "DICOM read error - no Waveform sequence found");
207 return -1;
208 }
209
210 Uint32 numGroups = seq->card();
211 if (numGroups == 0) {
212 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "DICOM read error - no multiplex group founds");
213 return -1;
214 }
215 for (uint32_t i = 0; i < numGroups; i++) {
216 DcmItem *item = seq->getItem(i);
217
218 // Number Of Waveform Channels
219 Uint16 numChannels;
220 if (item->findAndGetUint16(DCM_NumberOfWaveformChannels, numChannels).good())
221 hdr->NS=numChannels;
222
223 // Number of samples
224 Uint32 numSamples;
225 if (item->findAndGetUint32(DCM_NumberOfWaveformSamples, numSamples).good())
226 hdr->NRec=numSamples;
227
228 // Sampling Frequency
229 if (item->findAndGetString(DCM_SamplingFrequency, str).good())
230 hdr->SampleRate = strtod(str, NULL);
231
232 // read the channels
233 hdr->CHANNEL = (CHANNEL_TYPE*)realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
234
235 DcmSequenceOfItems *channelSeq;
236 item->findAndGetSequence(DCM_ChannelDefinitionSequence, channelSeq);
237 assert(hdr->NS == channelSeq->card()); // should be identical to numChannels !
238
239 /***** extract header2 and header 3 fields *****/
240
241 // Read channel attributes
242 size_t bi = 0;
243 hdr->SPR = 1;
244 for (uint32_t k=0; k < hdr->NS; k++) {
245 DcmItem *channelItem = channelSeq->getItem(k);
246 CHANNEL_TYPE* hc = hdr->CHANNEL + k;
247
248 hc->OnOff = 1;
249 hc->SPR = hdr->SPR;
250 hc->Cal = 1.0;
251 hc->Off = 0.0;
252 hc->Transducer[0] = '\0';
253 hc->LowPass = 0.0/0.0;
254 hc->HighPass = 0.0/0.0;
255
256 hc->DigMax = ldexp( 1.0, 15) - 1.0; // 2^15-1
257 hc->DigMin = ldexp(-1.0, 15); // -2^15
258 hc->LeadIdCode = 0;
259 hc->PhysDimCode = 0; // undefined
260 hc->bi = bi;
261
262 if (channelItem->findAndGetString(DCM_ChannelLabel, str).good())
263 strncpy(hc->Label, str, MAX_LENGTH_LABEL+1);
264
265 if (channelItem->findAndGetString(DCM_ChannelSensitivity, str).good())
266 hc->Cal = strtod(str, NULL);
267
268 if (channelItem->findAndGetString(DCM_ChannelBaseline, str).good())
269 hc->Off = strtod(str, NULL);
270
271 if (channelItem->findAndGetString(DCM_FilterLowFrequency, str).good())
272 hc->HighPass = strtod(str, NULL);
273
274 if (channelItem->findAndGetString(DCM_FilterHighFrequency, str).good())
275 hc->LowPass = strtod(str, NULL);
276
277 if (channelItem->findAndGetString(DCM_NotchFilterFrequency, str).good())
278 hc->Notch = strtod(str, NULL);
279
280 Uint16 gdftyp;
281 if (channelItem->findAndGetUint16(DCM_WaveformBitsStored, gdftyp).good()) {
282 switch (gdftyp) {
283 case 32: hc->GDFTYP = 5;
284 hc->DigMax = ldexp(1.0,31)-1.0;
285 hc->DigMin = -ldexp(1.0,31);
286 break;
287 case 24: hc->GDFTYP = 3+256;
288 hc->DigMax = ldexp(1.0,23)-1.0;
289 hc->DigMin = -ldexp(1.0,23);
290 break;
291 case 8: hc->GDFTYP = 1;
292 hc->DigMax = 127;
293 hc->DigMin = -128;
294 break;
295 case 16: hc->GDFTYP = 3;
296 default: hc->GDFTYP = 3;
297 hc->DigMax = ldexp(1.0,15)-1.0;
298 hc->DigMin = -ldexp(1.0,15);
299 }
300 hc->PhysMax = hc->DigMax*hc->Cal+hc->Off;
301 hc->PhysMin = hc->DigMin*hc->Cal+hc->Off;
302 bi += hc->SPR * GDFTYP_BITS[hc->GDFTYP] >> 3;
303 hdr->AS.bpb = bi;
304 }
305
306 // LeadIdCode
307 DcmSequenceOfItems *channelSourceSeq;
308 if (channelItem->findAndGetSequence(DCM_ChannelSourceSequence, channelSourceSeq).good()) {
309 DcmItem *channelSourceItem = channelSourceSeq->getItem(0);
310 if ( (channelSourceItem != NULL) && channelSourceItem->findAndGetString(DCM_CodeValue, str).good())
311 hc->LeadIdCode = atol(strchr(str,':') + 1);
312 }
313
314 // Physical Units
315 DcmSequenceOfItems *channelUnitsSeq;
316 if (channelItem->findAndGetSequence(DCM_ChannelSensitivityUnitsSequence, channelUnitsSeq).good()) {
317 DcmItem *channelUnitsItem = channelUnitsSeq->getItem(0);
318 if ( (channelUnitsItem != NULL) && channelUnitsItem->findAndGetString(DCM_CodeValue, str).good())
319 hc->PhysDimCode = PhysDimCode(str);
320 }
321 }
322
323 // waveform data
324 const Uint8* waveformData8 = NULL;
325 const Uint16* waveformData16 = NULL;
326 unsigned long count = 0;
327 if (item->findAndGetUint16Array(DCM_WaveformData, waveformData16, &count ).good()) {
328 count *= sizeof(uint16_t);
329 hdr->NRec = count/hdr->AS.bpb;
330 hdr->AS.rawdata = (uint8_t*)realloc(hdr->AS.rawdata, count);
331 memcpy(hdr->AS.rawdata, waveformData16, count);
332 hdr->AS.length = count/hdr->AS.bpb;
333 }
334 else if (item->findAndGetUint8Array(DCM_WaveformData, waveformData8, &count ).good()) {
335 hdr->NRec = count/hdr->AS.bpb;
336 hdr->AS.rawdata = (uint8_t*)realloc(hdr->AS.rawdata, count);
337 memcpy(hdr->AS.rawdata, waveformData8, count);
338 hdr->AS.length = count/hdr->AS.bpb;
339 }
340 }
341
342 /***** TODO: extract event information *****/
343 /*
344 hdr->EVENT.SampleRate =
345 hdr->EVENT.POS =
346 hdr->EVENT.TYP =
347 hdr->EVENT.CHN =
348 hdr->EVENT.DUR =
349 hdr->EVENT.CodeDesc =
350 hdr->EVENT.LenCodeDesc =
351 */
352
353 if (VERBOSE_LEVEL>7) fprintf(stdout,"# %s (line %d): DCMTK is used to read dicom files.\n",__func__,__LINE__);
354 return 0;
355 }
356 #endif // DCMTK
357
358