Debian Codegraph @

biosig4c++/t210/sopen_scp_read.c

raw symbols SCIP
1 /*
2
3 Copyright (C) 2005-2018 Alois Schloegl <[email protected]>
4 Copyright (C) 2011 Stoyan Mihaylov
5
6 This file is part of the "BioSig for C/C++" repository
7 (biosig4c++) at http://biosig.sf.net/
8
9
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 3
13 of the License, or (at your option) any later version.
14
15
16 */
17
18
19 // #define WITHOUT_SCP_DECODE // use SCP-DECODE if needed, Bimodal, reference beat
20
21 /*
22 the experimental version needs a few more thinks:
23 - Bimodal and RefBeat decoding do not work yet
24
25 - validation and testing
26 */
27
28
29 #include <ctype.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <iconv.h>
33 #include <errno.h>
34
35 #if !defined(__APPLE__) && defined (_LIBICONV_H)
36 #define iconv libiconv
37 #define iconv_open libiconv_open
38 #define iconv_close libiconv_close
39 #endif
40
41 #include "../biosig-dev.h"
42
43 #define min(a,b) (((a) < (b)) ? (a) : (b))
44
45 #include "structures.h"
46 static const uint8_t _NUM_SECTION = 20; //consider first 19 sections of SCP
47 static bool add_filter = true; // additional filtering gives better shape, but use with care
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 #ifndef WITHOUT_SCP_DECODE
54 int scp_decode(HDRTYPE* hdr, struct pointer_section *section, struct DATA_DECODE*, struct DATA_RECORD*, struct DATA_INFO*, bool );
55 void sopen_SCP_clean(struct DATA_DECODE*, struct DATA_RECORD*, struct DATA_INFO*);
56 #endif
57
58 // Huffman Tables
59 uint16_t NHT; /* number of Huffman tables */
60 typedef struct table_t {
61 uint8_t PrefixLength;
62 uint8_t CodeLength;
63 uint8_t TableModeSwitch;
64 int16_t BaseValue;
65 uint32_t BaseCode;
66 } table_t;
67 typedef struct huffman_t {
68 uint16_t NCT; /* number of Code structures in Table #1 */
69 table_t *Table;
70 } huffman_t;
71 huffman_t *Huffman;
72
73 typedef struct htree_t {
74 struct htree_t* child0;
75 struct htree_t* child1;
76 uint16_t idxTable;
77 } htree_t;
78 htree_t **HTrees;
79
80 table_t DefaultTable[19] = {
81 { 1, 1, 1, 0, 0 },
82 { 3, 3, 1, 1, 1 },
83 { 3, 3, 1,-1, 5 },
84 { 4, 4, 1, 2, 3 },
85 { 4, 4, 1,-2, 11},
86 { 5, 5, 1, 3, 7 },
87 { 5, 5, 1,-3, 23},
88 { 6, 6, 1, 4, 15},
89 { 6, 6, 1,-4, 47},
90 { 7, 7, 1, 5, 31},
91 { 7, 7, 1,-5, 95},
92 { 8, 8, 1, 6, 63},
93 { 8, 8, 1,-6, 191},
94 { 9, 9, 1, 7, 127},
95 { 9, 9, 1,-7, 383},
96 {10, 10, 1, 8, 255},
97 {10, 10, 1,-8, 767},
98 {18, 10, 1, 0, 511},
99 {26, 10, 1, 0, 1023}
100 };
101
102 /*
103 This structure defines the fields used for "Annotated ECG"
104 */
105 typedef struct en1064_t {
106 char* test; /* test field for annotated ECG */
107
108 float diastolicBloodPressure;
109 float systolicBloodPressure;
110 char* MedicationDrugs;
111 char* ReferringPhysician;
112 char* LatestConfirmingPhysician;
113 char* Diagnosis;
114 uint8_t EmergencyLevel; /* 0: routine 1-10: increased emergency level */
115
116 float HeartRate;
117 float P_wave[2]; /* start and end */
118 float QRS_wave[2]; /* start and end */
119 float T_wave[2]; /* start and end */
120 float P_QRS_T_axes[3];
121
122 /***** SCP only fields *****/
123 struct {
124 uint8_t HUFFMAN;
125 uint8_t REF_BEAT;
126 uint8_t DIFF;// OBSOLETE
127 uint8_t BIMODAL;// OBSOLETE
128 } FLAG;
129 struct {
130 //uint8_t tag14[41],tag15[41];
131 struct {
132 uint16_t INST_NUMBER; /* tag 14, byte 1-2 */
133 uint16_t DEPT_NUMBER; /* tag 14, byte 3-4 */
134 uint16_t DEVICE_ID; /* tag 14, byte 5-6 */
135 uint8_t DeviceType; /* tag 14, byte 7: 0: Cart, 1: System (or Host) */
136 uint8_t MANUF_CODE; /* tag 14, byte 8 (MANUF_CODE has to be 255) */
137 char* MOD_DESC; /* tag 14, byte 9 (MOD_DESC has to be "Cart1") */
138 uint8_t VERSION; /* tag 14, byte 15 (VERSION has to be 20) */
139 uint8_t PROT_COMP_LEVEL; /* tag 14, byte 16 (PROT_COMP_LEVEL has to be 0xA0 => level II) */
140 uint8_t LANG_SUPP_CODE; /* tag 14, byte 17 (LANG_SUPP_CODE has to be 0x00 => Ascii only, latin and 1-byte code) */
141 uint8_t ECG_CAP_DEV; /* tag 14, byte 18 (ECG_CAP_DEV has to be 0xD0 => Acquire, (No Analysis), Print and Store) */
142 uint8_t MAINS_FREQ; /* tag 14, byte 19 (MAINS_FREQ has to be 0: unspecified, 1: 50 Hz, 2: 60Hz) */
143 char reserved[22]; /* char[35-19] reserved; */
144 char* ANAL_PROG_REV_NUM;
145 char* SERIAL_NUMBER_ACQ_DEV;
146 char* ACQ_DEV_SYS_SW_ID;
147 char* ACQ_DEV_SCP_SW; /* tag 14, byte 38 (SCP_IMPL_SW has to be "OpenECG XML-SCP 1.00") */
148 char* ACQ_DEV_MANUF; /* tag 14, byte 38 (ACQ_DEV_MANUF has to be "Manufacturer") */
149 } Tag14, Tag15;
150 } Section1;
151 struct {
152 } Section2;
153 struct {
154 uint8_t NS, flags;
155 struct {
156 uint32_t start;
157 uint32_t end;
158 // uint8_t id;
159 } *lead;
160 } Section3;
161 struct {
162 uint16_t len_ms, fiducial_sample, N;
163 uint32_t SPR;
164 struct {
165 uint16_t btyp;
166 uint32_t SB;
167 uint32_t fcM;
168 uint32_t SE;
169 uint32_t QB;
170 uint32_t QE;
171 } *beat;
172 } Section4;
173 struct {
174 size_t StartPtr;
175 size_t Length;
176 uint16_t AVM, dT_us;
177 uint8_t DIFF; //diff: see FLAG
178 uint16_t *inlen;
179 int32_t *datablock;
180 } Section5;
181 struct {
182 size_t StartPtr;
183 size_t Length;
184 uint16_t AVM, dT_us;
185 uint8_t DIFF, BIMODAL; //diff, bimodal: see FLAG
186 int32_t *datablock;
187 } Section6;
188 } en1064_t;
189 en1064_t en1064;
190
191 /* new node in Huffman tree */
192 htree_t* newNode(void) {
193 htree_t* T = (htree_t*) malloc(sizeof(htree_t));
194 T->child0 = NULL;
195 T->child1 = NULL;
196 T->idxTable = 0;
197 return(T);
198 }
199
200 /* check Huffman tree */
201 int checkTree(htree_t *T) {
202 int v,v1,v2,v3;
203
204 v1 = (T->child0 == NULL) && (T->child0 == NULL) && (T->idxTable > 0);
205 v2 = (T->idxTable == 0) && (T->child0 != NULL) && checkTree(T->child0);
206 v3 = (T->idxTable == 0) && (T->child1 != NULL) && checkTree(T->child1);
207 v = v1 || v2 || v3;
208 #ifndef ANDROID
209 if (!v) fprintf(stderr,"Warning: Invalid Node in Huffman Tree: %i %p %p\n",T->idxTable,T->child0,T->child1);
210 #endif
211 return(v);
212 }
213
214 /* convert Huffman Table into a Huffman tree */
215 htree_t* makeTree(huffman_t HT) {
216 uint16_t k1,k2;
217 htree_t* T = newNode();
218 htree_t* node;
219 for (k1=0; k1<HT.NCT; k1++) {
220 node = T;
221 uint32_t bc = HT.Table[k1].BaseCode;
222 for (k2=0; k2<HT.Table[k1].CodeLength; k2++, bc>>=1) {
223 if (bc & 0x00000001) {
224 if (node->child1==NULL) node->child1 = newNode();
225 node = node->child1;
226 }
227 else {
228 if (node->child0==NULL) node->child0 = newNode();
229 node = node->child0;
230 }
231 }
232 node->idxTable = k1+1;
233 }
234 return(T);
235 }
236
237 /* get rid of Huffman tree */
238 void freeTree(htree_t* T) {
239 if (T->child0 != NULL) freeTree(T->child0);
240 if (T->child1 != NULL) freeTree(T->child1);
241 free(T);
242 }
243
244 int DecodeHuffman(htree_t *HTrees[], huffman_t *HuffmanTables, uint8_t* indata, size_t inlen, int32_t* outdata, size_t outlen) {
245 uint16_t ActualTable = 0;
246 htree_t *node;
247 size_t k1, k2, i;
248 uint32_t acc;
249 int8_t dlen,k3,r;
250
251 k1=0, k2=0;
252 node = HTrees[ActualTable];
253 r = 0; i = 0;
254 while ((k1 < inlen*8) && (k2 < outlen)) {
255 r = k1 % 8;
256 i = k1 / 8;
257
258 if (!node->idxTable) {
259 if (indata[i] & (1<<(7-r))) {
260 if (node->child1 != NULL)
261 node = node->child1;
262 else {
263 return(-1);
264 }
265 }
266 else {
267 if (node->child0 != NULL)
268 node = node->child0;
269 else {
270 return(-1);
271 }
272 }
273 ++k1;
274 }
275
276 r = k1 % 8;
277 i = k1 / 8;
278
279 if (node->idxTable) {
280 // leaf of tree reached
281 table_t TableEntry = HuffmanTables[ActualTable].Table[node->idxTable - 1];
282 dlen = TableEntry.PrefixLength - TableEntry.CodeLength;
283 if (!TableEntry.TableModeSwitch)
284 // switch Huffman Code
285 ActualTable = TableEntry.BaseValue;
286 else if (dlen) {
287 // no compression
288 acc = 0; //(uint32_t)(indata[i]%(1<<r));
289 for (k3=0; k3*8-r < dlen; k3++)
290 acc = (acc<<8)+(uint32_t)indata[i+k3];
291
292 outdata[k2] = (acc >> (k3*8 - r - dlen)) & ((1L << dlen) - 1L) ;
293 if (outdata[k2] >= (1 << (dlen-1)))
294 outdata[k2] -= 1 << dlen;
295 k1 += dlen;
296 ++k2;
297 }
298 else {
299 // lookup Huffman Table
300 outdata[k2++] = TableEntry.BaseValue;
301 }
302 // reset node to root
303 node = HTrees[ActualTable];
304 }
305 }
306 return(0);
307 };
308
309 void deallocEN1064(en1064_t en1064) {
310 /* free allocated memory */
311 if (en1064.FLAG.HUFFMAN) {
312 size_t k1=0;
313 for (; k1<en1064.FLAG.HUFFMAN; k1++) {
314 if (NHT!=19999) free(Huffman[k1].Table);
315 freeTree(HTrees[k1]);
316 }
317 free(Huffman);
318 free(HTrees);
319 }
320
321 if (en1064.Section3.lead != NULL) free(en1064.Section3.lead);
322 if (en1064.Section4.beat != NULL) free(en1064.Section4.beat);
323 if (en1064.Section5.inlen != NULL) free(en1064.Section5.inlen);
324 if (en1064.Section5.datablock != NULL) free(en1064.Section5.datablock);
325 // if (en1064.Section6.datablock != NULL) free(en1064.Section6.datablock);
326 en1064.Section5.inlen = NULL;
327 en1064.Section5.datablock = NULL;
328 en1064.Section3.lead = NULL;
329 en1064.Section4.beat = NULL;
330 }
331
332
333 /*
334 decode_scp_text converts SCP text strings in various language encodings into UTF-8.
335 hdr is used to identify the language support code of EN1064+A1:2007
336 versionSection is used to handle older versions (specifically 10) in a reasonable way.
337
338 input can be a string that without a 0-terminated character;
339 the end of the input string is determined by inbytesleft.
340 output must be of size outbytesleft+1.
341 */
342 int decode_scp_text(HDRTYPE *hdr, size_t inbytesleft, char *input, size_t outbytesleft, char *output, uint8_t versionSection) {
343
344 #ifdef DEBUG
345 const char *start = output;
346 #endif
347
348 int exitcode = 0;
349 switch (versionSection) {
350 case 13: // EN1064:2005
351 case 20: // EN1064:2007
352 case 26:
353 case 27:
354 case 28:
355 case 29: // SCP3: experimental, testing versions - not official
356 case 30: // SCP3: prEN1064:2017
357 break; // use language conversion code below
358 case 10:
359 exitcode = 0;
360 default:
361 exitcode = -1; // unknown version - do not know whether this is the correct way of doing it.
362 outbytesleft = min(inbytesleft,outbytesleft);
363 memcpy(output,input,outbytesleft);
364 output[outbytesleft]=0;
365 return(exitcode);
366 }
367
368 #if defined(_ICONV_H) || defined (_LIBICONV_H) || defined(_ICONV_H_)
369 /*
370 decode_scp_text converts SCP text strings into UTF-8 strings
371 The table of language support code as defined in
372 CEN Standard EN1064:2005+A1:2007, p.30.
373 */
374 uint8_t LanguageSupportCode = (*(struct aecg*)(hdr->aECG)).Section1.Tag14.LANG_SUPP_CODE;
375 iconv_t cd;
376 if ((LanguageSupportCode & 0x01) == 0)
377 cd = iconv_open ("UTF-8", "ASCII");
378
379 else if ((LanguageSupportCode & 0x03) == 1)
380 cd = iconv_open ("UTF-8", "ISO8859-1");
381
382 else if (LanguageSupportCode == 0x03)
383 cd = iconv_open ("UTF-8", "ISO8859-2");
384
385 else if (LanguageSupportCode == 0x0b)
386 cd = iconv_open ("UTF-8", "ISO8859-4");
387
388 else if (LanguageSupportCode == 0x13)
389 cd = iconv_open ("UTF-8", "ISO8859-5");
390
391 else if (LanguageSupportCode == 0x1b)
392 cd = iconv_open ("UTF-8", "ISO8859-6");
393
394 else if (LanguageSupportCode == 0x23)
395 cd = iconv_open ("UTF-8", "ISO8859-7");
396
397 else if (LanguageSupportCode == 0x2b)
398 cd = iconv_open ("UTF-8", "ISO8859-8");
399
400 else if (LanguageSupportCode == 0x33)
401 cd = iconv_open ("UTF-8", "ISO8859-11");
402
403 else if (LanguageSupportCode == 0x3b)
404 cd = iconv_open ("UTF-8", "ISO8859-15");
405
406 else if (LanguageSupportCode == 0x07)
407 cd = iconv_open ("UTF-8", "ISO-10646");
408
409 else if (LanguageSupportCode == 0x0f) // JIS X 0201-1976 (Japanese) - does not match exactly
410 cd = iconv_open ("UTF-8", "EUC-JISX0213");
411 else if (LanguageSupportCode == 0x17) // JIS X 0208-1997 (Japanese) - does not match exactly
412 cd = iconv_open ("UTF-8", "EUC-JISX0213");
413 else if (LanguageSupportCode == 0x1f) // JIS X 0212-1990 (Japanese) - does not match exactly
414 cd = iconv_open ("UTF-8", "EUC-JISX0213");
415
416 else if (LanguageSupportCode == 0x27)
417 cd = iconv_open ("UTF-8", "GB2312");
418
419 else if (LanguageSupportCode == 0x37)
420 cd = iconv_open ("UTF-8", "UTF-8");
421
422 else if (LanguageSupportCode == 0x2F) // KS C5601-1987 (Korean) - does not match exactly
423 cd = iconv_open ("UTF-8", "EUC-KR");
424 else {
425 biosigERROR(hdr, B4C_CHAR_ENCODING_UNSUPPORTED, "SCP character encoding not supported");
426 return -1;
427 }
428
429 errno = 0; // reset error status
430 int errsv;
431 if (input[inbytesleft-1]==0) {
432
433 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s(%i) decode_scp_text: input=<%s>%i,%i\n", __FILE__, __LINE__, input,(int)inbytesleft,(int)outbytesleft);
434
435 // input string is 0-terminated
436 iconv(cd, &input, &inbytesleft, &output, &outbytesleft);
437 errsv = errno;
438 }
439 else if (inbytesleft < 64) {
440 /* In case the string is not 0-terminated,
441 * the string is copied to make it 0-terminated
442 */
443 char buf[64];
444 char *tmpstr=buf;
445 memcpy(buf,input,inbytesleft);
446 tmpstr[inbytesleft++]=0;
447
448 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s(%i) decode_scp_text: input=<%s>%i,%i\n", __FILE__, __LINE__, input,(int)inbytesleft,(int)outbytesleft);
449
450 iconv(cd, &tmpstr, &inbytesleft, &output, &outbytesleft);
451 errsv = errno;
452 }
453 else {
454 /* In case the string is not 0-terminated,
455 * the string is copied to make it 0-terminated
456 */
457 char *tmpstr=malloc(inbytesleft+1);
458 char *bakstr=tmpstr;
459 strncpy(tmpstr,(char*)input,inbytesleft);
460 tmpstr[inbytesleft]=0;
461 inbytesleft++;
462
463 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s(%i) decode_scp_text: input=<%s>%i,%i\n", __FILE__, __LINE__, tmpstr,(int)inbytesleft,(int)outbytesleft);
464
465 iconv(cd, &tmpstr, &inbytesleft, &output, &outbytesleft);
466 errsv = errno;
467 free(bakstr);
468 }
469 if (errsv)
470 biosigERROR(hdr, B4C_CHAR_ENCODING_UNSUPPORTED, "conversion of SCP text failed");
471
472 #ifdef DEBUG
473 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s(%i) decode_scp_text: [e%i ] output=<%s>%i,%i\n", __FILE__, __LINE__, errsv,start,inbytesleft,outbytesleft);
474 #endif
475
476 return (iconv_close(cd) || errsv);
477
478 #else // if neither _ICONV_H nor _LIBCONV_H are defined
479
480 if ((LanguageSupportCode & 0xFE) != 0) {
481 biosigERROR(hdr, B4C_CHAR_ENCODING_UNSUPPORTED, "SCP character encoding not supported");
482 }
483 else // ASCII encoding is UTF-8 compatible - no convesion needed
484 strncpy(output, input, min(inbytesleft, outbytesleft+1));
485
486 return(LanguageSupportCode & 0xFE);
487
488 #endif
489
490 }
491
492
493 int sopen_SCP_read(HDRTYPE* hdr) {
494 /*
495 this function is a stub or placeholder and need to be defined in order to be useful.
496 It will be called by the function SOPEN in "biosig.c"
497
498 Input:
499 char* Header // contains the file content
500
501 Output:
502 HDRTYPE *hdr // defines the HDR structure accoring to "biosig.h"
503 */
504
505 uint8_t* ptr; // pointer to memory mapping of the file layout
506 uint8_t* PtrCurSect; // point to current section
507 uint8_t* Ptr2datablock=NULL; // pointer to data block
508 int32_t* data=NULL; // point to rawdata
509 uint16_t curSect=0; // current section
510 uint32_t len;
511 uint16_t crc;
512 uint32_t i,k1,k2;
513 size_t curSectPos;
514 size_t sectionStart;
515 int NSections = 12;
516 uint8_t tag;
517 float HighPass=0, LowPass=INFINITY, Notch=-1; // filter settings
518 uint16_t Cal5=0, Cal6=0, Cal0=0; // scaling coefficients
519 uint16_t dT_us = 1000; // sampling interval in microseconds
520
521 /*
522 Try direct conversion SCP->HDR to internal data structure
523 + whole data is loaded once, then no further File I/O is needed.
524 - currently Huffman and Bimodal compression is not supported.
525 */
526
527 struct aecg* aECG;
528 en1064.Section5.inlen = NULL;
529 en1064.Section5.datablock = NULL;
530 en1064.Section3.lead = NULL;
531 en1064.Section4.beat = NULL;
532 if (hdr->aECG == NULL) {
533 hdr->aECG = malloc(sizeof(struct aecg));
534 aECG = (struct aecg*)hdr->aECG;
535 aECG->diastolicBloodPressure=0.0;
536 aECG->systolicBloodPressure=0.0;
537 aECG->MedicationDrugs = NULL;
538 aECG->ReferringPhysician= NULL;
539
540 aECG->LatestConfirmingPhysician=NULL;
541 aECG->Diagnosis=NULL;
542 aECG->EmergencyLevel=0;
543 }
544 else
545 aECG = (struct aecg*)hdr->aECG;
546
547 aECG->Section1.Tag14.VERSION = 0; // acquiring.protocol_revision_number
548 aECG->Section1.Tag15.VERSION = 0; // analyzing.protocol_revision_number
549 aECG->Section1.Tag14.LANG_SUPP_CODE = 0;
550 aECG->FLAG.HUFFMAN = 0;
551 aECG->FLAG.DIFF = 0;
552 aECG->FLAG.REF_BEAT = 0;
553 aECG->FLAG.BIMODAL = 0;
554 #if (BIOSIG_VERSION < 10500)
555 aECG->Section8.NumberOfStatements = 0;
556 aECG->Section8.Statements = NULL;
557 aECG->Section11.NumberOfStatements = 0;
558 aECG->Section11.Statements = NULL;
559 #endif
560 en1064.FLAG.HUFFMAN = 0;
561 en1064.FLAG.DIFF = 0;
562 en1064.FLAG.REF_BEAT = 0;
563 en1064.FLAG.BIMODAL = 0;
564 en1064.Section4.len_ms = 0;
565
566 struct pointer_section section[_NUM_SECTION];
567 #ifndef WITHOUT_SCP_DECODE
568 struct DATA_DECODE decode;
569 struct DATA_RECORD record;
570 struct DATA_INFO textual;
571 bool AS_DECODE = 0;
572
573 decode.length_BdR0 = NULL;
574 decode.samples_BdR0= NULL;
575 decode.length_Res = NULL;
576 decode.samples_Res = NULL;
577 decode.t_Huffman=NULL;
578 decode.flag_Huffman=NULL;
579 decode.data_lead=NULL;
580 decode.data_protected=NULL;
581 decode.data_subtraction=NULL;
582 decode.length_BdR0=NULL;
583 decode.samples_BdR0=NULL;
584 decode.Median=NULL;
585 decode.length_Res=NULL;
586 decode.samples_Res=NULL;
587 decode.Residual=NULL;
588 decode.Reconstructed=NULL;
589
590 //variables inizialization
591 decode.flag_lead.number=0;
592 decode.flag_lead.subtraction=0;
593 decode.flag_lead.all_simultaneously=0;
594 decode.flag_lead.number_simultaneously=0;
595
596 decode.flag_BdR0.length=0;
597 decode.flag_BdR0.fcM=0;
598 decode.flag_BdR0.AVM=0;
599 decode.flag_BdR0.STM=0;
600 decode.flag_BdR0.number_samples=0;
601 decode.flag_BdR0.encoding=0;
602
603 decode.flag_Res.AVM=0;
604 decode.flag_Res.STM=0;
605 decode.flag_Res.number=0;
606 decode.flag_Res.number_samples=0;
607 decode.flag_Res.encoding=0;
608 decode.flag_Res.bimodal=0;
609 decode.flag_Res.decimation_factor=0;
610 #endif
611
612 ptr = hdr->AS.Header;
613 hdr->NRec = 0;
614
615 sectionStart = 6;
616 PtrCurSect = ptr+sectionStart;
617
618 /**** SECTION 0 ****/
619 len = leu32p(PtrCurSect+4);
620 NSections = (len-16)/10;
621
622 if (memcmp(ptr+16, "SCPECG\0\0", 8)) {
623 fprintf(stderr,"Warning SOPEN (SCP): Bytes 11-16 of Section 0 do not contain SCPECG - this violates ISO/DIS 11073-91064 Section 5.3.2.\n" );
624 }
625 section[0].ID = 0;
626 section[0].length = len;
627 section[0].index = 6+16;
628 int K;
629 for (K=1; K<_NUM_SECTION; K++) {
630 section[K].ID = -1;
631 section[K].length = 0;
632 section[K].index = 0;
633 }
634
635 for (K = 1; K < NSections; K++) {
636 // this is needed because fields are not always sorted
637 curSect = leu32p(ptr+6+16+K*10);
638 len = leu32p(ptr+6+16+K*10+2);
639
640 if (VERBOSE_LEVEL > 7)
641 fprintf(stdout,"%s (line %i): #%d section %d/%d %d %d\n",__FILE__,__LINE__,K,curSect,NSections,leu32p(ptr+6+16+K*10+2),leu32p(ptr+6+16+K*10+6)-1);
642
643 if (curSect < _NUM_SECTION) {
644 if (section[curSect].ID >= 0) {
645 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "SCP Section must not be defined twice");
646 return -1;
647 }
648 section[curSect].ID = curSect;
649 section[curSect].length = len;
650 section[curSect].index = leu32p(ptr+6+16+K*10+6)-1;
651 }
652 else if (len > 0)
653 fprintf(stderr,"Warning SOPEN (SCP) : vendor specific section %d is not supported\n",curSect);
654 }
655
656 if (section[1].length) {
657 /**** identify language support code - scan through section 1 for tag 14, byte 17 ****/
658 K = 1;
659 curSect = section[K].ID;
660 len = section[K].length;
661 sectionStart = section[K].index;
662
663 PtrCurSect = ptr+sectionStart;
664 crc = leu16p(PtrCurSect);
665 /*
666 uint16_t tmpcrc = CRCEvaluate((uint8_t*)(PtrCurSect+2),len-2);
667 uint8_t versionSection = *(ptr+sectionStart+8);
668 uint8_t versionProtocol = *(ptr+sectionStart+9);
669 */
670 // future versions might not need to do this, because language encoding is fixed (i.e. known).
671
672 uint32_t len1;
673 curSectPos = 16;
674 while (curSectPos<=len) {
675 tag = *(PtrCurSect+curSectPos);
676 len1 = leu16p(PtrCurSect+curSectPos+1);
677 curSectPos += 3;
678 if (curSectPos+len1 > len) break;
679 if (tag==14) {
680 aECG->Section1.Tag14.LANG_SUPP_CODE = *(PtrCurSect+curSectPos+16); // tag 14, byte 16 (LANG_SUPP_CODE has to be 0x00 => Ascii only,
681 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) Language Support Code is 0x%02x\n",__FILE__,__LINE__,aECG->Section1.Tag14.LANG_SUPP_CODE);
682 break;
683 }
684 curSectPos += len1;
685 }
686 }
687
688 for (K=1; K<_NUM_SECTION; K++) {
689
690 curSect = section[K].ID;
691 len = section[K].length;
692 sectionStart = section[K].index;
693
694 if (VERBOSE_LEVEL>7)
695 fprintf(stdout,"%s (line %i): SCP Section %i %i len=%i secStart=%i HeaderLength=%i\n",__FILE__,__LINE__,K,curSect,len,(int)sectionStart,hdr->HeadLen);
696
697 if (len==0) continue; /***** empty section *****/
698
699 if (sectionStart + len > hdr->HeadLen) {
700 biosigERROR(hdr, B4C_INCOMPLETE_FILE, "%s (line %i): SOPEN(SCP-READ): File incomplete - Section length + start of section is more then total length of header");
701 break;
702 }
703
704 PtrCurSect = ptr+sectionStart;
705 crc = leu16p(PtrCurSect);
706 uint16_t tmpcrc = CRCEvaluate((uint8_t*)(PtrCurSect+2),len-2);
707 uint8_t versionSection = *(ptr+sectionStart+8);
708 uint8_t versionProtocol = *(ptr+sectionStart+9);
709 #ifndef ANDROID
710 if ((crc != 0xffff) && (crc != tmpcrc))
711 fprintf(stderr,"Warning SOPEN(SCP-READ): faulty CRC in section %i: crc=%x, %x\n" ,curSect,crc,tmpcrc);
712 if (curSect != leu16p(PtrCurSect+2))
713 fprintf(stderr,"Warning SOPEN(SCP-READ): Current Section No does not match field in sections (%i %i)\n",curSect,leu16p(PtrCurSect+2));
714 if (len != leu32p(PtrCurSect+4))
715 fprintf(stderr,"Warning SOPEN(SCP-READ): length field in pointer section (%i) does not match length field in sections (%i %i)\n",K,len,leu32p(PtrCurSect+4));
716 if ((versionSection != 13) && (versionSection != 20) && (versionSection != (uint8_t)(hdr->Version*10)))
717 fprintf(stderr,"Warning SOPEN(SCP-READ): Version of section %i is not 13 or 20 but %i. This is not tested.\n", curSect, versionSection);
718 if ((versionProtocol != 13) && (versionProtocol != 20) && (versionProtocol != (uint8_t)(hdr->Version*10)))
719 fprintf(stderr,"Warning SOPEN(SCP-READ): Version of Protocol is not 13 or 20 but %i. This is not tested.\n", versionProtocol);
720 #endif
721 if (VERBOSE_LEVEL>7)
722 fprintf(stdout,"%s (line %i): SCP Section %i %i len=%i secStart=%i version=%i %i \n",__FILE__,__LINE__, K, curSect, len, (int)sectionStart,(int)versionSection, (int)versionProtocol);
723
724 curSectPos = 16;
725
726 /**** SECTION 0: POINTERS TO DATA AREAS IN THE RECORD ****/
727 if (curSect==0)
728 {
729 }
730
731 /**** SECTION 1: HEADER INFORMATION - PATIENT DATA/ECG ACQUISITION DATA ****/
732 else if (curSect==1)
733 {
734 struct tm t0,t1;
735 t0.tm_year = 0;
736 t0.tm_mon = 0;
737 t0.tm_mday = 0;
738 t0.tm_hour = 0;
739 t0.tm_min = 0;
740 t0.tm_sec = 0;
741 t0.tm_isdst= -1; // daylight savings time - unknown
742 hdr->T0 = 0;
743 hdr->Patient.Birthday = 0;
744 uint32_t len1;
745
746 while ((curSectPos<=len) && (*(PtrCurSect+curSectPos) < 255)) {
747 tag = *(PtrCurSect+curSectPos);
748 len1 = leu16p(PtrCurSect+curSectPos+1);
749 if (VERBOSE_LEVEL > 7)
750 fprintf(stdout,"SCP(r): Section 1 Tag %i Len %i <%s>\n",tag,len1, (char*)PtrCurSect+curSectPos);
751
752 curSectPos += 3;
753 if (curSectPos+len1 > len) {
754 #ifndef ANDROID
755 fprintf(stdout,"Warning SCP(read): section 1 corrupted (exceeds file length)\n");
756 #endif
757 break;
758 }
759 if (tag==0) {
760 // convert to UTF8
761 if (!hdr->FLAG.ANONYMOUS) {
762 // Last name or entire name if no first name is provided
763 decode_scp_text(hdr, len1, (char*)PtrCurSect+curSectPos, MAX_LENGTH_NAME, hdr->Patient.Name, versionSection);
764 }
765 }
766 else if (tag==1) {
767 if (!hdr->FLAG.ANONYMOUS) {
768 // First name
769 size_t len = strlen(hdr->Patient.Name);
770 if (len+3 < MAX_LENGTH_NAME) {
771 // unit separator ascii(31), 0x1f is used for separating name componentes
772 strcat(hdr->Patient.Name,"\x1f");
773 len+=1;
774 decode_scp_text(hdr, len1, (char*)PtrCurSect+curSectPos, MAX_LENGTH_NAME-len+1, hdr->Patient.Name+len, versionSection);
775 }
776 }
777 }
778 else if (tag==2) {
779 #ifndef ANDROID
780 if (len1 > MAX_LENGTH_PID) {
781 fprintf(stdout,"Warning SCP(read): length of Patient Id (section1 tag2) exceeds %i>%i\n",len1,MAX_LENGTH_PID);
782 }
783 #endif
784
785 // convert to UTF8
786 decode_scp_text(hdr, len1, (char*)PtrCurSect+curSectPos, MAX_LENGTH_PID, hdr->Patient.Id, versionSection);
787 hdr->Patient.Id[MAX_LENGTH_PID] = 0;
788 if (!strcmp(hdr->Patient.Id,"UNKNOWN"))
789 hdr->Patient.Id[0] = 0;
790 }
791 else if (tag==3) {
792 if (!hdr->FLAG.ANONYMOUS) {
793 // Second last name
794 size_t len = strlen(hdr->Patient.Name);
795 if (len+2 < MAX_LENGTH_NAME) {
796 // unit separator ascii(31), 0x1f is used for separating name componentes
797 strcat(hdr->Patient.Name,"\x1f");
798 len+=1;
799 decode_scp_text(hdr, len1, (char*)PtrCurSect+curSectPos, MAX_LENGTH_NAME-len+1, hdr->Patient.Name+len, versionSection);
800 }
801 }
802 }
803 else if (tag==4) {
804 }
805 else if (tag==5) {
806 t1.tm_year = leu16p(PtrCurSect+curSectPos)-1900;
807 t1.tm_mon = *(PtrCurSect+curSectPos+2)-1;
808 t1.tm_mday = *(PtrCurSect+curSectPos+3);
809 t1.tm_hour = 12;
810 t1.tm_min = 0;
811 t1.tm_sec = 0;
812 t1.tm_isdst= -1; // daylight saving time: unknown
813 // t1.tm_gmtoff = 0;
814 hdr->Patient.Birthday = tm_time2gdf_time(&t1);
815 }
816 else if (tag==6) {
817 hdr->Patient.Height = leu16p(PtrCurSect+curSectPos);
818 }
819 else if (tag==7) {
820 hdr->Patient.Weight = leu16p(PtrCurSect+curSectPos);
821 }
822 else if (tag==8) {
823 hdr->Patient.Sex = *(PtrCurSect+curSectPos);
824 if (hdr->Patient.Sex>2) hdr->Patient.Sex = 0;
825 }
826 else if (tag==9) {
827 }
828 else if (tag==10) {
829 // TODO: convert to UTF8
830 }
831 else if (tag==11) {
832 aECG->systolicBloodPressure = leu16p(PtrCurSect+curSectPos);
833 }
834 else if (tag==12) {
835 aECG->diastolicBloodPressure = leu16p(PtrCurSect+curSectPos);
836 }
837 else if (tag==13) {
838 // TODO: convert to UTF8
839 aECG->Diagnosis = (char*)(PtrCurSect+curSectPos);
840 }
841 else if (tag==14) {
842 /* Acquiring Device ID Number */
843 // TODO: convert to UTF8
844 #ifndef ANDROID
845 if (len1>85)
846 fprintf(stderr,"Warning SCP(r): length of tag14 %i>40\n",len1);
847 #endif
848 memcpy(hdr->ID.Manufacturer._field,(char*)PtrCurSect+curSectPos,min(len1,MAX_LENGTH_MANUF));
849 hdr->ID.Manufacturer._field[min(len1,MAX_LENGTH_MANUF)] = 0;
850 hdr->ID.Manufacturer.Model = hdr->ID.Manufacturer._field+8;
851 hdr->ID.Manufacturer.Version = hdr->ID.Manufacturer._field+36;
852 int tmp = strlen(hdr->ID.Manufacturer.Version)+1;
853 hdr->ID.Manufacturer.SerialNumber = hdr->ID.Manufacturer.Version+tmp;
854 tmp += strlen(hdr->ID.Manufacturer.Version+tmp)+1; // skip SW ID
855 tmp += strlen(hdr->ID.Manufacturer.Version+tmp)+1; // skip SW
856 tmp += strlen(hdr->ID.Manufacturer.Version+tmp)+1; // skip SW
857 hdr->ID.Manufacturer.Name = hdr->ID.Manufacturer.Version+tmp;
858
859 /* might become obsolete */
860 //memcpy(hdr->aECG->Section1.tag14,PtrCurSect+curSectPos,40);
861 //hdr->VERSION = *(PtrCurSect+curSectPos+14)/10.0; // tag 14, byte 15
862 aECG->Section1.Tag14.INST_NUMBER = leu16p(PtrCurSect+curSectPos);
863 aECG->Section1.Tag14.DEPT_NUMBER = leu16p(PtrCurSect+curSectPos+2);
864 aECG->Section1.Tag14.DEVICE_ID = leu16p(PtrCurSect+curSectPos+4);
865 aECG->Section1.Tag14.DeviceType = *(PtrCurSect+curSectPos+ 6);
866 aECG->Section1.Tag14.MANUF_CODE = *(PtrCurSect+curSectPos+ 7); // tag 14, byte 7 (MANUF_CODE has to be 255)
867
868 const char *MANUFACTURER[] = {
869 "unknown","Burdick","Cambridge",
870 "Compumed","Datamed","Fukuda","Hewlett-Packard",
871 "Marquette Electronics","Mortara Instruments",
872 "Nihon Kohden","Okin","Quinton","Siemens","Spacelabs",
873 "Telemed","Hellige","ESA-OTE","Schiller",
874 "Picker-Schwarzer","et medical devices",
875 "Zwönitz",NULL};
876
877 if (!strlen(hdr->ID.Manufacturer.Name)) {
878 if (aECG->Section1.Tag14.MANUF_CODE < 21)
879 hdr->ID.Manufacturer.Name = MANUFACTURER[aECG->Section1.Tag14.MANUF_CODE];
880 else
881 fprintf(stderr,"Warning SOPEN(SCP): unknown manufacturer code\n");
882 }
883
884 aECG->Section1.Tag14.MOD_DESC = (char*)(PtrCurSect+curSectPos+8);
885 aECG->Section1.Tag14.VERSION = *(PtrCurSect+curSectPos+14);
886 aECG->Section1.Tag14.PROT_COMP_LEVEL = *(PtrCurSect+curSectPos+15); // tag 14, byte 15 (PROT_COMP_LEVEL has to be 0xA0 => level II)
887 aECG->Section1.Tag14.LANG_SUPP_CODE = *(PtrCurSect+curSectPos+16); // tag 14, byte 16 (LANG_SUPP_CODE has to be 0x00 => Ascii only, latin and 1-byte code)
888 aECG->Section1.Tag14.ECG_CAP_DEV = *(PtrCurSect+curSectPos+17); // tag 14, byte 17 (ECG_CAP_DEV has to be 0xD0 => Acquire, (No Analysis), Print and Store)
889 aECG->Section1.Tag14.MAINS_FREQ = *(PtrCurSect+curSectPos+18); // tag 14, byte 18 (MAINS_FREQ has to be 0: unspecified, 1: 50 Hz, 2: 60Hz)
890
891 aECG->Section1.Tag14.ANAL_PROG_REV_NUM = (char*)(PtrCurSect+curSectPos+36);
892 tmp = strlen((char*)(PtrCurSect+curSectPos+36));
893 aECG->Section1.Tag14.SERIAL_NUMBER_ACQ_DEV = (char*)(PtrCurSect+curSectPos+36+tmp+1);
894 tmp += strlen((char*)(PtrCurSect+curSectPos+36+tmp+1));
895 aECG->Section1.Tag14.ACQ_DEV_SYS_SW_ID = (char*)(PtrCurSect+curSectPos+36+tmp+1);
896 tmp += strlen((char*)(PtrCurSect+curSectPos+36+tmp+1));
897 aECG->Section1.Tag14.ACQ_DEV_SCP_SW = (char*)(PtrCurSect+curSectPos+36+tmp+1); // tag 14, byte 38 (SCP_IMPL_SW has to be "OpenECG XML-SCP 1.00")
898 tmp += strlen((char*)(PtrCurSect+curSectPos+36+tmp+1));
899 aECG->Section1.Tag14.ACQ_DEV_MANUF = (char*)(PtrCurSect+curSectPos+36+tmp+1); // tag 14, byte 38 (ACQ_DEV_MANUF has to be "Manufacturer")
900
901
902 if (aECG->Section1.Tag14.LANG_SUPP_CODE & 0xFE) {
903 #if _ICONV_H
904 fprintf(stdout, "Warning SCP-ECG: decoding of text strings not ready yet");
905 #else
906 biosigERROR(hdr, B4C_CHAR_ENCODING_UNSUPPORTED, "SCP-SCP: Non-ASCII text string language - conversion not supported");
907 #endif
908 }
909 if (VERBOSE_LEVEL>7)
910 fprintf(stdout,"%s (line %i): Version %i\n",__FILE__,__LINE__,aECG->Section1.Tag14.VERSION);
911
912 }
913 else if (tag==15) {
914 /* Analyzing Device ID Number */
915 // TODO: convert to UTF8
916 //memcpy(hdr->aECG->Section1.tag15,PtrCurSect+curSectPos,40);
917 aECG->Section1.Tag15.VERSION = *(PtrCurSect+curSectPos+14);
918 }
919 else if (tag==16) {
920 /* Acquiring Institution Description */
921 size_t outlen = len1*2+1;
922 hdr->ID.Hospital = malloc(outlen);
923 if (hdr->ID.Hospital) {
924 // convert to UTF8
925 decode_scp_text(hdr, len1, (char*)PtrCurSect+curSectPos, outlen, hdr->ID.Hospital, versionSection);
926 hdr->ID.Hospital[outlen] = 0;
927 }
928 }
929 else if (tag==17) {
930 /* Analyzing Institution Description */
931 // TODO: convert to UTF8
932 }
933 else if (tag==18) {
934 /* Acquiring Institution Description */
935 // TODO: convert to UTF8
936 }
937 else if (tag==19) {
938 /* Analyzing Institution Description */
939 // TODO: convert to UTF8
940 }
941 else if (tag==20) {
942 // TODO: convert to UTF8
943 aECG->ReferringPhysician = (char*)(PtrCurSect+curSectPos);
944 }
945 else if (tag==21) {
946 // TODO: convert to UTF8
947 aECG->MedicationDrugs = (char*)(PtrCurSect+curSectPos);
948 }
949 else if (tag==22) {
950 size_t outlen = len1*2+1;
951 hdr->ID.Technician = malloc(outlen);
952 if (hdr->ID.Technician) {
953 // convert to UTF8
954 decode_scp_text(hdr, len1, (char*)PtrCurSect+curSectPos, outlen, hdr->ID.Technician, versionSection);
955 hdr->ID.Technician[outlen] = 0;
956 }
957 }
958 else if (tag==23) {
959 /* Room Description */
960 // TODO: convert to UTF8
961 }
962 else if (tag==24) {
963 aECG->EmergencyLevel = *(PtrCurSect+curSectPos);
964 }
965 else if (tag==25) {
966 t0.tm_year = leu16p(PtrCurSect+curSectPos)-1900;
967 t0.tm_mon = (*(PtrCurSect+curSectPos+2)) - 1;
968 t0.tm_mday = *(PtrCurSect+curSectPos+3);
969 }
970 else if (tag==26) {
971 t0.tm_hour = *(PtrCurSect+curSectPos);
972 t0.tm_min = *(PtrCurSect+curSectPos+1);
973 t0.tm_sec = *(PtrCurSect+curSectPos+2);
974 }
975 else if (tag==27) {
976 HighPass = leu16p(PtrCurSect+curSectPos)/100.0;
977 }
978 else if (tag==28) {
979 LowPass = leu16p(PtrCurSect+curSectPos);
980 }
981 else if (tag==29) {
982 uint8_t bitmap = *(PtrCurSect+curSectPos);
983 if (bitmap==0)
984 Notch = NAN; // undefined
985 else if ((bitmap & 0x03)==0)
986 Notch = -1; // notch off
987 else if (bitmap & 0x01)
988 Notch = 60.0; // notch 60Hz
989 else if (bitmap & 0x02)
990 Notch = 50.0; // notch 50Hz
991 }
992 else if (tag==30) {
993 /* Free Text Field */
994 // TODO: convert to UTF8
995 }
996 else if (tag==31) {
997 /* ECG Sequence Number */
998 // TODO: convert to UTF8
999 }
1000 else if (tag==32) {
1001 /* History Diagnostic Codes */
1002 // TODO: convert to UTF8
1003 if (PtrCurSect[curSectPos]==0) {
1004 unsigned k=1;
1005 for (; k < len1; k++) {
1006 if ((PtrCurSect[curSectPos+k] > 9) && (PtrCurSect[curSectPos+k] < 40))
1007 hdr->Patient.Impairment.Heart = 2;
1008 else if (PtrCurSect[curSectPos+k]==1)
1009 hdr->Patient.Impairment.Heart = 1;
1010 else if (PtrCurSect[curSectPos+k]==42) {
1011 hdr->Patient.Impairment.Heart = 3;
1012 break;
1013 }
1014 }
1015 }
1016 }
1017 else if (tag==33) {
1018 /* Electrode Configuration Code */
1019 // TODO: convert to UTF8
1020 }
1021 else if (tag==34) {
1022 /* DateTimeZone */
1023 // TODO: convert to UTF8
1024 int16_t tzmin = lei16p(PtrCurSect+curSectPos);
1025 if (tzmin != 0x7fff) {
1026 if (abs(tzmin)<=780)
1027 hdr->tzmin = tzmin;
1028 else
1029 fprintf(stderr,"Warning SOPEN(SCP-READ): invalid time zone (Section 1, Tag34)\n");
1030 }
1031 //fprintf(stdout,"SOPEN(SCP-READ): tzmin = %i %x \n",tzmin,tzmin);
1032 }
1033 else if (tag==35) {
1034 /* Free Text Medical History */
1035 // TODO: convert to UTF8
1036 }
1037 else {
1038 }
1039 curSectPos += len1;
1040 }
1041 hdr->T0 = tm_time2gdf_time(&t0);
1042 }
1043
1044 /**** SECTION 2: HUFFMAN TABLES USED IN ENCODING OF ECG DATA (IF USED) ****/
1045 else if (curSect==2) {
1046 aECG->FLAG.HUFFMAN = 1;
1047 en1064.FLAG.HUFFMAN = 1;
1048
1049 NHT = leu16p(PtrCurSect+curSectPos);
1050 curSectPos += 2;
1051
1052 if (VERBOSE_LEVEL > 7) fprintf(stdout,"SCP(r): Section 2 NHT=%d\n", NHT);
1053
1054 if (NHT==19999) {
1055 en1064.FLAG.HUFFMAN = 1;
1056 Huffman = (huffman_t*)malloc(sizeof(huffman_t));
1057 HTrees = (htree_t**)malloc(sizeof(htree_t*));
1058 Huffman[0].NCT = 19;
1059 Huffman[0].Table = DefaultTable;
1060 HTrees [0] = makeTree(Huffman[0]);
1061 k2 = 0;
1062 #ifndef ANDROID
1063 if (VERBOSE_LEVEL==9)
1064 for (k1=0; k1<Huffman[k2].NCT; k1++)
1065 fprintf(stdout,"%3i: %2i %2i %1i %3i %6u \n",k1,Huffman[k2].Table[k1].PrefixLength,Huffman[k2].Table[k1].CodeLength,Huffman[k2].Table[k1].TableModeSwitch,Huffman[k2].Table[k1].BaseValue,Huffman[k2].Table[k1].BaseCode);
1066
1067 if (!checkTree(HTrees[0])) // ### OPTIONAL, not needed ###
1068 fprintf(stderr,"Warning: invalid Huffman Tree\n");
1069 #endif
1070 }
1071 else {
1072 en1064.FLAG.HUFFMAN = NHT;
1073 Huffman = (huffman_t*)malloc(NHT*sizeof(huffman_t));
1074 for (k2=0; k2<NHT; k2++) {
1075 Huffman[k2].NCT = leu16p(PtrCurSect+curSectPos);
1076 curSectPos += 2;
1077 Huffman[k2].Table = (typeof(Huffman[k2].Table))malloc(Huffman[k2].NCT * sizeof(*Huffman[k2].Table));
1078 HTrees = (htree_t**)malloc(Huffman[k2].NCT*sizeof(htree_t*));
1079 for (k1=0; k1<Huffman[k2].NCT; k1++) {
1080 Huffman[k2].Table[k1].PrefixLength = *(PtrCurSect+curSectPos);
1081 Huffman[k2].Table[k1].CodeLength = *(PtrCurSect+curSectPos+1);
1082 Huffman[k2].Table[k1].TableModeSwitch = *(PtrCurSect+curSectPos+2);
1083 Huffman[k2].Table[k1].BaseValue = lei16p(PtrCurSect+curSectPos+3);
1084 Huffman[k2].Table[k1].BaseCode = leu32p(PtrCurSect+curSectPos+5);
1085 curSectPos += 9;
1086 #ifndef ANDROID
1087 if (VERBOSE_LEVEL==9)
1088 fprintf(stdout,"%3i %3i: %2i %2i %1i %3i %6u \n",k2,k1,Huffman[k2].Table[k1].PrefixLength,Huffman[k2].Table[k1].CodeLength,Huffman[k2].Table[k1].TableModeSwitch,Huffman[k2].Table[k1].BaseValue,Huffman[k2].Table[k1].BaseCode);
1089 #endif
1090 }
1091 HTrees[k2] = makeTree(Huffman[k2]);
1092 if (!checkTree(HTrees[k2])) {
1093 biosigERROR(hdr, B4C_DECOMPRESSION_FAILED, "Warning: invalid Huffman Tree");
1094 // AS_DECODE = 2; // forced use of SCP-DECODE
1095 }
1096 }
1097 }
1098 }
1099
1100 /**** SECTION 3: ECG LEAD DEFINITION ****/
1101 else if (curSect==3)
1102 {
1103 hdr->NS = *(PtrCurSect+curSectPos);
1104 aECG->FLAG.REF_BEAT = (*(PtrCurSect+curSectPos+1) & 0x01);
1105 en1064.Section3.flags = *(PtrCurSect+curSectPos+1);
1106 if (aECG->FLAG.REF_BEAT && (aECG->Section1.Tag14.VERSION > 25)) {
1107 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "REF-BEAT compression is invalid in SCP v3");
1108 }
1109 if (aECG->FLAG.REF_BEAT && !section[4].length) {
1110 #ifndef ANDROID
1111 fprintf(stderr,"Warning (SCP): Reference Beat but no Section 4\n");
1112 #endif
1113 aECG->FLAG.REF_BEAT = 0;
1114 }
1115 #ifndef ANDROID
1116 if (!(en1064.Section3.flags & 0x04) || ((en1064.Section3.flags>>3) != hdr->NS))
1117 fprintf(stderr,"Warning (SCP): channels are not simultaneously recorded! %x %i\n",en1064.Section3.flags,hdr->NS);
1118 #endif
1119
1120 curSectPos += 2;
1121 hdr->CHANNEL = (CHANNEL_TYPE *) realloc(hdr->CHANNEL,hdr->NS* sizeof(CHANNEL_TYPE));
1122 en1064.Section3.lead = (typeof(en1064.Section3.lead))malloc(hdr->NS*sizeof(*en1064.Section3.lead));
1123
1124 uint32_t startindex0;
1125 startindex0 = leu32p(PtrCurSect+curSectPos);
1126 for (i = 0, hdr->SPR=1; i < hdr->NS; i++) {
1127 en1064.Section3.lead[i].start = leu32p(PtrCurSect+curSectPos);
1128 en1064.Section3.lead[i].end = leu32p(PtrCurSect+curSectPos+4);
1129 uint8_t LeadIdCode = *(PtrCurSect+curSectPos+8);
1130 if (LeadIdCode > 184) {
1131 // consider this as undefined LeadId
1132 LeadIdCode = 0;
1133 fprintf(stderr,"Warning (SCP): LeadId of channel %i is %i - which is unspecified\n",i+1, LeadIdCode);
1134 }
1135
1136 hdr->CHANNEL[i].SPR = en1064.Section3.lead[i].end - en1064.Section3.lead[i].start + 1;
1137
1138 if (VERBOSE_LEVEL>7)
1139 fprintf(stdout,"%s (line %i): SCP Section %i #%i SPR=%d/%d [%d..%d]\n",__FILE__,__LINE__,curSect, i, hdr->CHANNEL[i].SPR, hdr->SPR, en1064.Section3.lead[i].end, en1064.Section3.lead[i].start );
1140 hdr->SPR = lcm(hdr->SPR,hdr->CHANNEL[i].SPR);
1141 hdr->CHANNEL[i].LeadIdCode = LeadIdCode;
1142 hdr->CHANNEL[i].Label[0]= 0;
1143 hdr->CHANNEL[i].Transducer[0]= 0;
1144 hdr->CHANNEL[i].LowPass = LowPass;
1145 hdr->CHANNEL[i].HighPass= HighPass;
1146 hdr->CHANNEL[i].Notch = Notch;
1147 curSectPos += 9;
1148
1149 #ifndef ANDROID
1150 if (en1064.Section3.lead[i].start != startindex0)
1151 fprintf(stderr,"Warning SCP(read): starting sample %i of #%i differ to %x in #1\n",en1064.Section3.lead[i].start,*(PtrCurSect+curSectPos+8),startindex0);
1152 #endif
1153 }
1154 }
1155 /**** SECTION 4: QRS LOCATIONS (IF REFERENCE BEATS ARE ENCODED) ****/
1156 else if (curSect==4) {
1157
1158 if (aECG->Section1.Tag14.VERSION > 25)
1159 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Section 4 must not be used in SCP v3");
1160
1161 en1064.Section4.len_ms = leu16p(PtrCurSect+curSectPos); // ### TODO: SCPECGv3 ###
1162 en1064.Section4.fiducial_sample = leu16p(PtrCurSect+curSectPos+2); // ### TODO: SCPECGv3 ###
1163 en1064.Section4.N = leu16p(PtrCurSect+curSectPos+4); // ### TODO: SCPECGv3 ###
1164 en1064.Section4.SPR = hdr->SPR/4;
1165
1166 en1064.Section4.beat = (typeof(en1064.Section4.beat))malloc(en1064.Section4.N*sizeof(*en1064.Section4.beat));
1167
1168 curSectPos += 6;
1169 for (i=0; i < en1064.Section4.N; i++) {
1170 en1064.Section4.beat[i].btyp = leu16p(PtrCurSect+curSectPos);
1171 en1064.Section4.beat[i].SB = leu32p(PtrCurSect+curSectPos+2);
1172 en1064.Section4.beat[i].fcM = leu32p(PtrCurSect+curSectPos+6);
1173 en1064.Section4.beat[i].SE = leu32p(PtrCurSect+curSectPos+10);
1174 curSectPos += 14;
1175 }
1176 for (i=0; i < en1064.Section4.N; i++) {
1177 en1064.Section4.beat[i].QB = leu32p(PtrCurSect+curSectPos);
1178 en1064.Section4.beat[i].QE = leu32p(PtrCurSect+curSectPos+4);
1179 curSectPos += 8;
1180 en1064.Section4.SPR += en1064.Section4.beat[i].QE-en1064.Section4.beat[i].QB-1;
1181 }
1182 if (en1064.Section4.len_ms==0) {
1183 aECG->FLAG.REF_BEAT = 0;
1184 }
1185 }
1186
1187 /**** SECTION 5: ENCODED REFERENCE BEAT DATA IF REFERENCE BEATS ARE STORED ****/
1188 else if (curSect==5) {
1189 Cal5 = leu16p(PtrCurSect+curSectPos);
1190 en1064.Section5.AVM = leu16p(PtrCurSect+curSectPos);
1191 en1064.Section5.dT_us = leu16p(PtrCurSect+curSectPos+2);
1192 en1064.Section5.DIFF = *(PtrCurSect+curSectPos+4);
1193 en1064.Section5.Length = (1000L * en1064.Section4.len_ms) / en1064.Section5.dT_us; // hdr->SPR;
1194 en1064.Section5.inlen = (typeof(en1064.Section5.inlen))malloc(hdr->NS*2);
1195 for (i=0; i < hdr->NS; i++) {
1196 en1064.Section5.inlen[i] = leu16p(PtrCurSect+curSectPos+6+2*i); // ### TODO: SCPECGv3 ###
1197 if (!section[4].length && (en1064.Section5.Length < en1064.Section5.inlen[i]))
1198 en1064.Section5.Length = en1064.Section5.inlen[i];
1199 }
1200 if (!section[4].length && en1064.FLAG.HUFFMAN) {
1201 en1064.Section5.Length *= 5; // decompressed data might need more space
1202 #ifndef ANDROID
1203 fprintf(stderr,"Warning SCPOPEN: Section 4 not defined - size of Sec5 can be only guessed (%i allocated)\n",(int)en1064.Section5.Length);
1204 #endif
1205 }
1206
1207 en1064.Section5.datablock = NULL;
1208 if (aECG->FLAG.REF_BEAT) {
1209 en1064.Section5.datablock = (int32_t*)malloc(4 * hdr->NS * en1064.Section5.Length);
1210
1211 Ptr2datablock = (PtrCurSect+curSectPos+6+2*hdr->NS);
1212 for (i=0; i < hdr->NS; i++) {
1213 en1064.Section5.inlen[i] = leu16p(PtrCurSect+curSectPos+6+2*i); // ### TODO: SCPECGv3 ###
1214 if (en1064.FLAG.HUFFMAN) {
1215 if (DecodeHuffman(HTrees, Huffman, Ptr2datablock, en1064.Section5.inlen[i], en1064.Section5.datablock + en1064.Section5.Length*i, en1064.Section5.Length)) {
1216 biosigERROR(hdr, B4C_DECOMPRESSION_FAILED, "Empty node in Huffman table! Do not know what to do !");
1217 }
1218 if (hdr->AS.B4C_ERRNUM) {
1219 deallocEN1064(en1064);
1220 return(-1);
1221 }
1222 }
1223 else {
1224 for (k1=0; k1<en1064.Section5.Length; k1++)
1225 en1064.Section5.datablock[i*en1064.Section5.Length+k1] = lei16p(Ptr2datablock + 2*(i*en1064.Section5.Length + k1));
1226 }
1227 Ptr2datablock += en1064.Section5.inlen[i];
1228 }
1229 size_t ix;
1230 data = en1064.Section5.datablock;
1231 if (en1064.Section5.DIFF==1)
1232 for (k1 = 0; k1 < hdr->NS; k1++)
1233 for (ix = k1*en1064.Section5.Length+1; ix < (k1+1)*en1064.Section5.Length; ix++)
1234 data[ix] += data[ix-1];
1235
1236 else if (en1064.Section5.DIFF==2)
1237 for (k1 = 0; k1 < hdr->NS; k1++)
1238 for (ix = k1*en1064.Section5.Length+2; ix < (k1+1)*en1064.Section5.Length; ix++)
1239 data[ix] += 2*data[ix-1] - data[ix-2];
1240 }
1241 }
1242
1243 /**** SECTION 6 ****/
1244 else if ((curSect==6) && (section[12].length==0)) {
1245 // Read Section6 only if no Section 12 is available
1246 hdr->NRec = 1;
1247
1248 uint8_t FLAG_HUFFMAN = 0;
1249 uint16_t gdftyp = 5; // int32: internal raw data type
1250 hdr->AS.rawdata = (uint8_t*)realloc(hdr->AS.rawdata,4 * hdr->NS * hdr->SPR * hdr->NRec);
1251 data = (int32_t*)hdr->AS.rawdata;
1252
1253 en1064.Section6.AVM = leu16p(PtrCurSect+curSectPos);
1254 en1064.Section6.dT_us = leu16p(PtrCurSect+curSectPos+2);
1255 hdr->SampleRate = 1e6/en1064.Section6.dT_us;
1256 en1064.Section6.DIFF = *(PtrCurSect+curSectPos+4);
1257 en1064.FLAG.DIFF = *(PtrCurSect+curSectPos+4);
1258 if (hdr->VERSION < 3.0) {
1259 en1064.Section6.BIMODAL = *(PtrCurSect+curSectPos+5);
1260 en1064.FLAG.BIMODAL = *(PtrCurSect+curSectPos+5);
1261 aECG->FLAG.BIMODAL = *(PtrCurSect+curSectPos+5);
1262 }
1263 else {
1264 en1064.Section6.BIMODAL = 0;
1265 en1064.FLAG.BIMODAL = 0;
1266 aECG->FLAG.BIMODAL = 0;
1267 FLAG_HUFFMAN = *(PtrCurSect+curSectPos+5);
1268 }
1269
1270 Cal6 = leu16p(PtrCurSect+curSectPos);
1271 en1064.Section6.dT_us = leu16p(PtrCurSect+curSectPos+2);
1272 aECG->FLAG.DIFF = *(PtrCurSect+curSectPos+4);
1273
1274 if (VERBOSE_LEVEL>7) fprintf(stdout, "%s (line %i) Compression(Diff=%i Huffman=%i RefBeat=%i Bimodal=%i)\n", __func__, __LINE__, aECG->FLAG.DIFF, aECG->FLAG.HUFFMAN, aECG->FLAG.REF_BEAT, aECG->FLAG.BIMODAL);
1275
1276 if ((section[5].length>4) && en1064.Section5.dT_us)
1277 dT_us = en1064.Section5.dT_us;
1278 else
1279 dT_us = en1064.Section6.dT_us;
1280 hdr->SampleRate = 1e6/dT_us;
1281
1282 typeof(hdr->SPR) SPR = ( en1064.FLAG.BIMODAL ? en1064.Section4.SPR : hdr->SPR);
1283
1284 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): %i %i %i\n", __func__ ,__LINE__, dT_us, Cal5, Cal6);
1285
1286 if ((Cal5==0) && (Cal6 >0)) Cal0 = Cal6;
1287 else if ((Cal5 >0) && (Cal6==0)) Cal0 = Cal5;
1288 else if ((Cal5 >0) && (Cal6 >0)) Cal0 = gcd(Cal5,Cal6);
1289 else {
1290 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "SCP with invalid AVM data !");
1291 return(-1);
1292 }
1293 uint16_t cal5 = Cal5/Cal0;
1294 uint16_t cal6 = Cal6/Cal0;
1295
1296 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i): %i %i %i\n",__func__,__LINE__,dT_us,Cal5,Cal6);
1297
1298 Ptr2datablock = (PtrCurSect+curSectPos + 6 + hdr->NS*2); // pointer for huffman decoder
1299 len = 0;
1300 size_t ix;
1301 hdr->AS.bpb = hdr->NS * hdr->SPR*GDFTYP_BITS[gdftyp]>>3;
1302 for (i=0; i < hdr->NS; i++) {
1303 if (VERBOSE_LEVEL>7)
1304 fprintf(stdout,"sec6-%i\n",i);
1305
1306 CHANNEL_TYPE *hc = hdr->CHANNEL+i;
1307 hc->SPR = hdr->SPR;
1308 hc->PhysDimCode = 4275; // PhysDimCode("uV") physical unit "uV"
1309 hc->Cal = Cal0 * 1e-3;
1310 hc->Off = 0;
1311 hc->OnOff = 1; // 1: ON 0:OFF
1312 hc->GDFTYP = gdftyp;
1313 #ifndef NO_BI
1314 hc->bi = i*hdr->SPR*GDFTYP_BITS[gdftyp]>>3;
1315 #endif
1316 // ### TODO: these values should represent the true saturation values ### //
1317 hc->DigMax = ldexp(1.0,20)-1;
1318 hc->DigMin = ldexp(-1.0,20);
1319 hc->PhysMax = hc->DigMax * hc->Cal;
1320 hc->PhysMin = hc->DigMin * hc->Cal;
1321
1322 uint16_t inlen = leu16p(PtrCurSect+curSectPos+6+2*i); // ### TODO: SCPECGv3 ###
1323 if (en1064.FLAG.HUFFMAN) {
1324 if (DecodeHuffman(HTrees, Huffman, Ptr2datablock, inlen, data + i*hdr->SPR, hdr->SPR)) {
1325 biosigERROR(hdr, B4C_DECOMPRESSION_FAILED, "Empty node in Huffman table! Do not know what to do !");
1326 }
1327 if (hdr->AS.B4C_ERRNUM) {
1328 deallocEN1064(en1064);
1329 return(-1);
1330 }
1331 }
1332 else {
1333 for (k1=0, ix = i*hdr->SPR; k1 < SPR; k1++)
1334 data[ix+k1] = lei16p(Ptr2datablock + 2*k1);
1335 }
1336 len += inlen;
1337 Ptr2datablock += inlen;
1338
1339 if (aECG->FLAG.DIFF==1) {
1340 for (ix = i*hdr->SPR+1; ix < i*hdr->SPR + SPR; ix++)
1341 data[ix] += data[ix-1];
1342 }
1343 else if (aECG->FLAG.DIFF==2) {
1344 for (ix = i*hdr->SPR+2; ix < i*hdr->SPR + SPR; ix++)
1345 data[ix] += 2*data[ix-1] - data[ix-2];
1346 }
1347 #ifndef WITHOUT_SCP_DECODE
1348 if (aECG->FLAG.BIMODAL || aECG->FLAG.REF_BEAT) {
1349 // if (aECG->FLAG.BIMODAL) {
1350 // if (aECG->FLAG.REF_BEAT {
1351 /* this is experimental work
1352 Bimodal and RefBeat decompression are under development.
1353 "continue" ignores code below
1354 AS_DECODE=1 will call later SCP-DECODE instead
1355 */
1356 AS_DECODE = 1; continue;
1357 }
1358 #endif
1359
1360 if (aECG->FLAG.BIMODAL) {
1361 // ### FIXME ###
1362 ix = i*hdr->SPR; // memory offset
1363 k1 = en1064.Section4.SPR; // SPR of decimated data
1364 k2 = hdr->SPR; // SPR of sample data
1365 uint32_t k3 = en1064.Section4.N-1; // # of protected zones
1366 uint8_t k4 = 4; // decimation factor
1367 do {
1368 --k2;
1369 data[ix + k2] = data[ix + k1 - 1];
1370 if (k2 > en1064.Section4.beat[k3].QE) { // outside protected zone
1371 if (--k4==0) {k4=4; --k1; };
1372 }
1373 else { // inside protected zone
1374 --k1;
1375 if (k2<en1064.Section4.beat[k3].QB) {--k3; k4=4;};
1376 }
1377 } while (k2 && (k1>0));
1378 }
1379
1380 if (aECG->FLAG.REF_BEAT) {
1381 /* Add reference beats */
1382 // ### FIXME ###
1383 for (k1 = 0; k1 < en1064.Section4.N; k1++) {
1384 if (en1064.Section4.beat[k1].btyp == 0)
1385 for (ix = 0; ix < en1064.Section5.Length; ix++) {
1386 uint32_t ix1 = en1064.Section4.beat[k1].SB - en1064.Section4.beat[k1].fcM + ix;
1387 uint32_t ix2 = i*hdr->SPR + ix1;
1388 if ((en1064.Section4.beat[k1].btyp==0) && (ix1 < hdr->SPR))
1389 data[ix2] = data[ix2] * cal6 + en1064.Section5.datablock[i*en1064.Section5.Length+ix] * cal5;
1390 }
1391 }
1392 }
1393 }
1394
1395 en1064.Section6.datablock = data;
1396
1397 curSectPos += 6 + 2*hdr->NS + len;
1398
1399 if (VERBOSE_LEVEL>8)
1400 fprintf(stdout,"end sec6\n");
1401
1402 }
1403
1404 /**** SECTION 7 ****/
1405 else if (curSect==7) {
1406 #if (BIOSIG_VERSION >= 10500)
1407 hdr->SCP.Section7Length = leu32p(PtrCurSect+4)-curSectPos;
1408 hdr->SCP.Section7 = PtrCurSect+curSectPos;
1409
1410 #endif
1411 uint16_t N_QRS = *(uint8_t*)(PtrCurSect+curSectPos)-1;
1412 uint8_t N_PaceMaker = *(uint8_t*)(PtrCurSect+curSectPos+1);
1413 // uint16_t RRI = leu16p(PtrCurSect+curSectPos+2);
1414 // uint16_t PPI = leu16p(PtrCurSect+curSectPos+4);
1415 curSectPos += 6;
1416 //size_t curSectPos0 = curSectPos; // backup of pointer
1417
1418 // skip data on QRS measurements
1419 /*
1420 // ### FIXME ###
1421 It seems that the P,QRS, and T wave events can not be reconstructed
1422 because they refer to the reference beat and not to the overall signal data.
1423 Maybe Section 4 information need to be used. However, EN1064 does not mention this.
1424
1425 hdr->EVENT.POS = (uint32_t*)realloc(hdr->EVENT.POS, (hdr->EVENT.N+5*N_QRS+N_PaceMaker)*sizeof(*hdr->EVENT.POS));
1426 hdr->EVENT.TYP = (uint16_t*)realloc(hdr->EVENT.TYP, (hdr->EVENT.N+5*N_QRS+N_PaceMaker)*sizeof(*hdr->EVENT.TYP));
1427 hdr->EVENT.DUR = (uint32_t*)realloc(hdr->EVENT.DUR, (hdr->EVENT.N+5*N_QRS+N_PaceMaker)*sizeof(*hdr->EVENT.DUR));
1428 hdr->EVENT.CHN = (uint16_t*)realloc(hdr->EVENT.CHN, (hdr->EVENT.N+5*N_QRS+N_PaceMaker)*sizeof(*hdr->EVENT.CHN));
1429 for (i=0; i < 5*N_QRS; i++) {
1430 hdr->EVENT.DUR[hdr->EVENT.N+i] = 0;
1431 hdr->EVENT.CHN[hdr->EVENT.N+i] = 0;
1432 }
1433 for (i=0; i < 5*N_QRS; i+=5) {
1434 uint8_t typ = *(PtrCurSect+curSectPos+i);
1435 hdr->EVENT.TYP[hdr->EVENT.N] = 0x0502;
1436 hdr->EVENT.TYP[hdr->EVENT.N+1] = 0x8502;
1437 hdr->EVENT.TYP[hdr->EVENT.N+2] = 0x0503;
1438 hdr->EVENT.TYP[hdr->EVENT.N+3] = 0x8503;
1439 hdr->EVENT.TYP[hdr->EVENT.N+4] = 0x8506;
1440 hdr->EVENT.POS[hdr->EVENT.N] = leu16p(PtrCurSect+curSectPos0);
1441 hdr->EVENT.POS[hdr->EVENT.N+1] = leu16p(PtrCurSect+curSectPos0+2);
1442 hdr->EVENT.POS[hdr->EVENT.N+2] = leu16p(PtrCurSect+curSectPos0+4);
1443 hdr->EVENT.POS[hdr->EVENT.N+3] = leu16p(PtrCurSect+curSectPos0+6);
1444 hdr->EVENT.POS[hdr->EVENT.N+4] = leu16p(PtrCurSect+curSectPos0+8);
1445 hdr->EVENT.N+= 5;
1446 curSectPos0 += 16;
1447 }
1448 */
1449 curSectPos += N_QRS*16;
1450 // pace maker information is stored in sparse sampling channel
1451 if (N_PaceMaker>0) {
1452 hdr->EVENT.POS = (uint32_t*)realloc(hdr->EVENT.POS, (hdr->EVENT.N+N_PaceMaker)*sizeof(*hdr->EVENT.POS));
1453 hdr->EVENT.TYP = (uint16_t*)realloc(hdr->EVENT.TYP, (hdr->EVENT.N+N_PaceMaker)*sizeof(*hdr->EVENT.TYP));
1454 hdr->EVENT.DUR = (uint32_t*)realloc(hdr->EVENT.DUR, (hdr->EVENT.N+N_PaceMaker)*sizeof(*hdr->EVENT.DUR));
1455 hdr->EVENT.CHN = (uint16_t*)realloc(hdr->EVENT.CHN, (hdr->EVENT.N+N_PaceMaker)*sizeof(*hdr->EVENT.CHN));
1456 /* add pacemaker channel */
1457 hdr->CHANNEL = (CHANNEL_TYPE *) realloc(hdr->CHANNEL,(++hdr->NS)*sizeof(CHANNEL_TYPE));
1458 i = hdr->NS;
1459 CHANNEL_TYPE *hc = hdr->CHANNEL+i;
1460 hc->SPR = 0; // sparse event channel
1461 hc->PhysDimCode = 4275; // PhysDimCode("uV") physical unit "uV"
1462 hc->Cal = 1;
1463 hc->Off = 0;
1464 hc->OnOff = 1; // 1: ON 0:OFF
1465 strcpy(hc->Transducer,"Pacemaker");
1466 hc->GDFTYP = 3;
1467
1468 // ### these values should represent the true saturation values ###//
1469 hc->DigMax = ldexp(1.0,15)-1;
1470 hc->DigMin = ldexp(-1.0,15);
1471 hc->PhysMax = hc->DigMax * hc->Cal;
1472 hc->PhysMin = hc->DigMin * hc->Cal;
1473 }
1474 // skip pacemaker spike measurements
1475 for (i=0; i < N_PaceMaker; i++) {
1476 ++hdr->EVENT.N;
1477 hdr->EVENT.TYP[hdr->EVENT.N] = 0x7fff;
1478 hdr->EVENT.CHN[hdr->EVENT.N] = hdr->NS;
1479 hdr->EVENT.POS[hdr->EVENT.N] = (uint32_t)(leu16p(PtrCurSect+curSectPos)*hdr->SampleRate*1e-3);
1480 hdr->EVENT.DUR[hdr->EVENT.N] = leu16p(PtrCurSect+curSectPos+2);
1481 curSectPos += 4;
1482 }
1483 // skip pacemaker spike information section
1484 curSectPos += N_PaceMaker*6;
1485
1486 // QRS type information
1487 N_QRS = leu16p(PtrCurSect+curSectPos);
1488 curSectPos += 2;
1489
1490 }
1491
1492 /**** SECTION 8 ****/
1493 else if (curSect==8) {
1494 // TODO: convert to UTF8
1495 #if (BIOSIG_VERSION >= 10500)
1496 hdr->SCP.Section8Length = leu32p(PtrCurSect+4)-curSectPos;
1497 hdr->SCP.Section8 = PtrCurSect+curSectPos;
1498
1499 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %i) %p %d %d %d\n", __func__, __LINE__, hdr->AS.Header, hdr->SCP.Section8Length, (int)curSectPos, (int)(hdr->SCP.Section8-hdr->AS.Header));
1500
1501 #else
1502 aECG->Section8.Confirmed = *(char*)(PtrCurSect+curSectPos);
1503 aECG->Section8.t.tm_year = leu16p(PtrCurSect+curSectPos+1)-1900;
1504 aECG->Section8.t.tm_mon = *(uint8_t*)(PtrCurSect+curSectPos+3)-1;
1505 aECG->Section8.t.tm_mday = *(uint8_t*)(PtrCurSect+curSectPos+4);
1506 aECG->Section8.t.tm_hour = *(uint8_t*)(PtrCurSect+curSectPos+5);
1507 aECG->Section8.t.tm_min = *(uint8_t*)(PtrCurSect+curSectPos+6);
1508 aECG->Section8.t.tm_sec = *(uint8_t*)(PtrCurSect+curSectPos+7);
1509 aECG->Section8.NumberOfStatements = *(uint8_t*)(PtrCurSect+curSectPos+8);
1510 aECG->Section8.Statements= (char**)malloc(aECG->Section8.NumberOfStatements*sizeof(char*));
1511 curSectPos += 9;
1512 uint8_t k=0;
1513 for (; k<aECG->Section8.NumberOfStatements;k++) {
1514 if (curSectPos+3 > len) break;
1515 aECG->Section8.Statements[k] = (char*)(PtrCurSect+curSectPos+3);
1516 curSectPos += 3+leu16p(PtrCurSect+curSectPos+1);
1517 }
1518 #endif
1519 }
1520
1521 /**** SECTION 9 ****/
1522 else if (curSect==9) {
1523 // TODO: convert to UTF8
1524 #if (BIOSIG_VERSION >= 10500)
1525 // hdr->SCP.Section9Length = leu32p(PtrCurSect+4)-curSectPos;
1526 // hdr->SCP.Section9 = PtrCurSect+curSectPos;
1527 #else
1528 aECG->Section9.StartPtr = (char*)(PtrCurSect+curSectPos);
1529 aECG->Section9.Length = len;
1530 #endif
1531 }
1532
1533 /**** SECTION 10 ****/
1534 else if (curSect==10) {
1535 #if (BIOSIG_VERSION >= 10500)
1536 hdr->SCP.Section10Length = leu32p(PtrCurSect+4)-curSectPos;
1537 hdr->SCP.Section10 = PtrCurSect+curSectPos;
1538 #endif
1539 }
1540
1541 /**** SECTION 11 ****/
1542 else if (curSect==11) {
1543 // TODO: convert to UTF8
1544 if(len<curSectPos+9) continue; //Something is very wrong
1545 #if (BIOSIG_VERSION >= 10500)
1546 /*
1547 hdr->SCP.Section11 = realloc(hdr->SCP.Section11, len);
1548 memcpy(hdr->SCP.Section11, PtrCurSect+curSectPos, len);
1549 */
1550 hdr->SCP.Section11Length = leu32p(PtrCurSect+4)-curSectPos;
1551 hdr->SCP.Section11 = PtrCurSect+curSectPos;
1552 #else
1553 aECG->Section11.Confirmed = *(char*)(PtrCurSect+curSectPos);
1554 aECG->Section11.t.tm_year = leu16p(PtrCurSect+curSectPos+1)-1900;
1555 aECG->Section11.t.tm_mon = *(uint8_t*)(PtrCurSect+curSectPos+3)-1;
1556 aECG->Section11.t.tm_mday = *(uint8_t*)(PtrCurSect+curSectPos+4);
1557 aECG->Section11.t.tm_hour = *(uint8_t*)(PtrCurSect+curSectPos+5);
1558 aECG->Section11.t.tm_min = *(uint8_t*)(PtrCurSect+curSectPos+6);
1559 aECG->Section11.t.tm_sec = *(uint8_t*)(PtrCurSect+curSectPos+7);
1560 aECG->Section11.NumberOfStatements = *(uint8_t*)(PtrCurSect+curSectPos+8);
1561 aECG->Section11.Statements= (char**)malloc(aECG->Section11.NumberOfStatements*sizeof(char*));
1562 curSectPos += 9;
1563 uint8_t k=0;
1564 for (; k<aECG->Section11.NumberOfStatements;k++) {
1565 if (curSectPos+4 > len) break;
1566 aECG->Section11.Statements[k] = (char*)(PtrCurSect+curSectPos+4);
1567 curSectPos += 3+leu16p(PtrCurSect+curSectPos+1);
1568 }
1569 #endif
1570 }
1571
1572 #if defined(WITH_SCP3)
1573 /**** SECTION 12 ****/
1574 else if ( (curSect==12) && (versionSection > 25) && (versionProtocol > 25) && (len > 70) ) {
1575
1576 uint32_t sec12_LN = leu32p(PtrCurSect+curSectPos+62);
1577 uint32_t sec12_LMI= leu32p(PtrCurSect+curSectPos+66);
1578 uint32_t sec12_Len1 = 70+sec12_LN+sec12_LMI;
1579 uint8_t sec12_FRST = *(PtrCurSect+curSectPos+16); // TODO: get rid of this field, no benefit
1580 uint8_t sec12_FBMP = *(PtrCurSect+curSectPos+31);
1581
1582 uint16_t gdftyp = 0;
1583 uint8_t bps = *(uint8_t*)(PtrCurSect+curSectPos+9);
1584 double DigMin = -1.0/0.0;
1585 double DigMax = +1.0/0.0;
1586 switch (bps) {
1587 case 1: gdftyp = 1; break;
1588 case 2: gdftyp = 3; break;
1589 case 3: gdftyp = 255+24; break;
1590 case 4: gdftyp = 5; break;
1591 default:
1592 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "invalid number of bytes per samplein SCP3:Section12");
1593 }
1594 DigMin = -ldexp(1.0,bps-1);
1595 DigMax = ldexp(1.0,bps-1)-1.0;
1596
1597 // TODO: why is this needed, why is Section 1 not good enough ?
1598 struct tm t0;
1599 t0.tm_year = leu16p(PtrCurSect+curSectPos+10)-1900;
1600 t0.tm_mon = (*(PtrCurSect+curSectPos+12)) - 1;
1601 t0.tm_mday = *(PtrCurSect+curSectPos+13);
1602 t0.tm_hour = *(PtrCurSect+curSectPos+14);
1603 t0.tm_min = *(PtrCurSect+curSectPos+15);
1604 t0.tm_sec = *(PtrCurSect+curSectPos+16);
1605 hdr->T0 = tm_time2gdf_time(&t0);
1606 hdr->SampleRate = leu32p(PtrCurSect+curSectPos);
1607 hdr->NS = *(uint8_t*)(PtrCurSect+curSectPos+4);
1608 hdr->NRec = leu32p(PtrCurSect+curSectPos+5);
1609 hdr->SPR = 1; // multiplexed
1610 hdr->AS.bpb = bps*hdr->NS;
1611
1612 hdr->AS.rawdata = (uint8_t*)realloc(hdr->AS.rawdata,4 * hdr->NS * hdr->SPR * hdr->NRec);
1613 data = (int32_t*)hdr->AS.rawdata;
1614
1615 /* Leads Definition Block */
1616 hdr->CHANNEL = (CHANNEL_TYPE *) realloc(hdr->CHANNEL,hdr->NS* sizeof(CHANNEL_TYPE));
1617 for (i = 0; i < hdr->NS; i++) {
1618 CHANNEL_TYPE *hc = hdr->CHANNEL+i;
1619 uint8_t LeadIdCode = *(PtrCurSect+curSectPos+sec12_Len1+i*4);
1620 if (LeadIdCode > 184) {
1621 // consider this as undefined LeadId
1622 LeadIdCode = 0;
1623 fprintf(stderr,"Warning (SCP): LeadId of channel %i is %i - which is unspecified\n",i+1, LeadIdCode);
1624 }
1625
1626 hc->bi = bps*i;
1627 hc->bi8 = (bps*i)<<3;
1628 hc->SPR = 1;
1629 hc->LeadIdCode = LeadIdCode;
1630 hc->Label[0] = 0;
1631 hc->Transducer[0] = 0;
1632 hc->OnOff = 1;
1633 hc->Impedance = 0.0/0.0;
1634 hc->GDFTYP = gdftyp;
1635 hc->DigMin = DigMin;
1636 hc->DigMax = DigMax;
1637 hc->Off = 0.0;
1638 hc->Cal = leu16p(PtrCurSect+curSectPos+sec12_Len1+i*4+1)*1e-3;
1639 hc->PhysDimCode = 4275; // PhysDimCode("uV") physical unit "uV"
1640 hc->PhysMin = DigMin*hc->Cal;
1641 hc->PhysMax = DigMax*hc->Cal;
1642 if (sec12_FRST) {
1643 hc->LowPass = leu16p(PtrCurSect+curSectPos+29);
1644 hc->HighPass = leu16p(PtrCurSect+curSectPos+27);
1645 hc->Notch = ((sec12_FBMP==0) ? 60 : ((sec12_FBMP==1) ? 50 : NAN));
1646 }
1647 else { // From Section 1 tags 27-28
1648 hc->LowPass = LowPass;
1649 hc->HighPass = HighPass;
1650 hc->Notch = Notch;
1651 }
1652 }
1653 /*
1654 size_t sz = GDFTYP_BITS[gdftyp] * hdr->NS * hdr->SPR * hdr->NRec / 8;
1655 hdr->AS.rawdata = (uint8_t*)realloc(hdr->AS.rawdata, sz);
1656 memcpy(hdr->AS.rawdata, PtrCurSect+curSectPos+sec12_Len1+hdr->NS*4, sz);
1657 */
1658 hdr->AS.rawdata = PtrCurSect+curSectPos+sec12_Len1+hdr->NS*4;
1659 hdr->AS.first = 0;
1660 hdr->AS.length = hdr->SPR*hdr->NRec;
1661 }
1662
1663 /**** SECTION 13 ****/
1664 else if (curSect==13) {
1665 }
1666
1667 /**** SECTION 14 ****/
1668 else if (curSect==14) {
1669 }
1670
1671 /**** SECTION 15 ****/
1672 else if (curSect==15) {
1673 }
1674
1675 /**** SECTION 16 ****/
1676 else if (curSect==16) {
1677 }
1678
1679 /**** SECTION 17 ****/
1680 else if (curSect==17) {
1681 }
1682
1683 /**** SECTION 18 ****/
1684 else if (curSect==18) {
1685 }
1686 #endif
1687 else {
1688 }
1689 }
1690
1691 /* free allocated memory */
1692 deallocEN1064(en1064);
1693
1694
1695 return 0;
1696 #ifndef WITHOUT_SCP_DECODE
1697 if (AS_DECODE==0) return(0);
1698
1699 /*
1700 ---------------------------------------------------------------------------
1701 Copyright (C) 2006 Eugenio Cervesato.
1702 Developed at the Associazione per la Ricerca in Cardiologia - Pordenone - Italy,
1703
1704 This program is free software; you can redistribute it and/or
1705 modify it under the terms of the GNU General Public License
1706 as published by the Free Software Foundation; either version 2
1707 of the License, or (at your option) any later version.
1708
1709 This program is distributed in the hope that it will be useful,
1710 but WITHOUT ANY WARRANTY; without even the implied warranty of
1711 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1712 GNU General Public License for more details.
1713
1714 You should have received a copy of the GNU General Public License
1715 along with this program; if not, write to the Free Software
1716 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717 ---------------------------------------------------------------------------
1718 */
1719
1720 /* Fall back method:
1721
1722 + implements Huffman, reference beat and Bimodal compression.
1723 - uses piece-wise file access
1724 - defines intermediate data structure
1725 */
1726
1727 #ifndef ANDROID
1728 if (VERBOSE_LEVEL > 7)
1729 fprintf(stdout, "\nUse SCP_DECODE (Diff=%i Huffman=%i RefBeat=%i Bimodal=%i)\n", aECG->FLAG.DIFF, aECG->FLAG.HUFFMAN, aECG->FLAG.REF_BEAT, aECG->FLAG.BIMODAL);
1730 #endif
1731
1732 textual.des.acquiring.protocol_revision_number = aECG->Section1.Tag14.VERSION;
1733 textual.des.analyzing.protocol_revision_number = aECG->Section1.Tag15.VERSION;
1734
1735 decode.flag_Res.bimodal = (aECG->Section1.Tag14.VERSION > 10 ? aECG->FLAG.BIMODAL : 0);
1736 decode.Reconstructed = (int32_t*) hdr->AS.rawdata;
1737
1738 // TODO: check error handling
1739 biosigERROR(hdr, 0, NULL);
1740 if (scp_decode(hdr, section, &decode, &record, &textual, add_filter)) {
1741 if (Cal0>1)
1742 for (i=0; i < hdr->NS * hdr->SPR * hdr->NRec; ++i)
1743 data[i] /= Cal0;
1744 }
1745 else {
1746 biosigERROR(hdr, B4C_CANNOT_OPEN_FILE, "SCP-DECODE can not read file");
1747 return(0);
1748 }
1749
1750 // end of fall back method
1751 decode.Reconstructed = NULL;
1752 sopen_SCP_clean(&decode, &record, &textual);
1753
1754 return(1);
1755 #endif
1756 };
1757
1758 #ifdef __cplusplus
1759 }
1760 #endif
1761