| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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; |
| 47 |
static bool add_filter = true; |
| 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 |
|
| 59 |
uint16_t NHT; |
| 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; |
| 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 |
|
| 104 |
|
| 105 |
typedef struct en1064_t { |
| 106 |
char* test; |
| 107 |
|
| 108 |
float diastolicBloodPressure; |
| 109 |
float systolicBloodPressure; |
| 110 |
char* MedicationDrugs; |
| 111 |
char* ReferringPhysician; |
| 112 |
char* LatestConfirmingPhysician; |
| 113 |
char* Diagnosis; |
| 114 |
uint8_t EmergencyLevel; |
| 115 |
|
| 116 |
float HeartRate; |
| 117 |
float P_wave[2]; |
| 118 |
float QRS_wave[2]; |
| 119 |
float T_wave[2]; |
| 120 |
float P_QRS_T_axes[3]; |
| 121 |
|
| 122 |
|
| 123 |
struct { |
| 124 |
uint8_t HUFFMAN; |
| 125 |
uint8_t REF_BEAT; |
| 126 |
uint8_t DIFF; |
| 127 |
uint8_t BIMODAL; |
| 128 |
} FLAG; |
| 129 |
struct { |
| 130 |
|
| 131 |
struct { |
| 132 |
uint16_t INST_NUMBER; |
| 133 |
uint16_t DEPT_NUMBER; |
| 134 |
uint16_t DEVICE_ID; |
| 135 |
uint8_t DeviceType; |
| 136 |
uint8_t MANUF_CODE; |
| 137 |
char* MOD_DESC; |
| 138 |
uint8_t VERSION; |
| 139 |
uint8_t PROT_COMP_LEVEL; |
| 140 |
uint8_t LANG_SUPP_CODE; |
| 141 |
uint8_t ECG_CAP_DEV; |
| 142 |
uint8_t MAINS_FREQ; |
| 143 |
char reserved[22]; |
| 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; |
| 148 |
char* ACQ_DEV_MANUF; |
| 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 |
|
| 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; |
| 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; |
| 186 |
int32_t *datablock; |
| 187 |
} Section6; |
| 188 |
} en1064_t; |
| 189 |
en1064_t en1064; |
| 190 |
|
| 191 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 281 |
table_t TableEntry = HuffmanTables[ActualTable].Table[node->idxTable - 1]; |
| 282 |
dlen = TableEntry.PrefixLength - TableEntry.CodeLength; |
| 283 |
if (!TableEntry.TableModeSwitch) |
| 284 |
|
| 285 |
ActualTable = TableEntry.BaseValue; |
| 286 |
else if (dlen) { |
| 287 |
|
| 288 |
acc = 0; |
| 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 |
|
| 300 |
outdata[k2++] = TableEntry.BaseValue; |
| 301 |
} |
| 302 |
|
| 303 |
node = HTrees[ActualTable]; |
| 304 |
} |
| 305 |
} |
| 306 |
return(0); |
| 307 |
}; |
| 308 |
|
| 309 |
void deallocEN1064(en1064_t en1064) { |
| 310 |
|
| 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 |
|
| 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 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 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: |
| 351 |
case 20: |
| 352 |
case 26: |
| 353 |
case 27: |
| 354 |
case 28: |
| 355 |
case 29: |
| 356 |
case 30: |
| 357 |
break; |
| 358 |
case 10: |
| 359 |
exitcode = 0; |
| 360 |
default: |
| 361 |
exitcode = -1; |
| 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 |
|
| 371 |
|
| 372 |
|
| 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) |
| 410 |
cd = iconv_open ("UTF-8", "EUC-JISX0213"); |
| 411 |
else if (LanguageSupportCode == 0x17) |
| 412 |
cd = iconv_open ("UTF-8", "EUC-JISX0213"); |
| 413 |
else if (LanguageSupportCode == 0x1f) |
| 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) |
| 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; |
| 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 |
|
| 436 |
iconv(cd, &input, &inbytesleft, &output, &outbytesleft); |
| 437 |
errsv = errno; |
| 438 |
} |
| 439 |
else if (inbytesleft < 64) { |
| 440 |
|
| 441 |
|
| 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 |
|
| 455 |
|
| 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 |
| 479 |
|
| 480 |
if ((LanguageSupportCode & 0xFE) != 0) { |
| 481 |
biosigERROR(hdr, B4C_CHAR_ENCODING_UNSUPPORTED, "SCP character encoding not supported"); |
| 482 |
} |
| 483 |
else |
| 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 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
uint8_t* ptr; |
| 506 |
uint8_t* PtrCurSect; |
| 507 |
uint8_t* Ptr2datablock=NULL; |
| 508 |
int32_t* data=NULL; |
| 509 |
uint16_t curSect=0; |
| 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; |
| 518 |
uint16_t Cal5=0, Cal6=0, Cal0=0; |
| 519 |
uint16_t dT_us = 1000; |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 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; |
| 548 |
aECG->Section1.Tag15.VERSION = 0; |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 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); |
| 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; |
| 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 |
|
| 727 |
if (curSect==0) |
| 728 |
{ |
| 729 |
} |
| 730 |
|
| 731 |
|
| 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; |
| 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 |
|
| 761 |
if (!hdr->FLAG.ANONYMOUS) { |
| 762 |
|
| 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 |
|
| 769 |
size_t len = strlen(hdr->Patient.Name); |
| 770 |
if (len+3 < MAX_LENGTH_NAME) { |
| 771 |
|
| 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 |
|
| 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 |
|
| 794 |
size_t len = strlen(hdr->Patient.Name); |
| 795 |
if (len+2 < MAX_LENGTH_NAME) { |
| 796 |
|
| 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; |
| 813 |
|
| 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 |
|
| 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 |
|
| 839 |
aECG->Diagnosis = (char*)(PtrCurSect+curSectPos); |
| 840 |
} |
| 841 |
else if (tag==14) { |
| 842 |
|
| 843 |
|
| 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; |
| 855 |
tmp += strlen(hdr->ID.Manufacturer.Version+tmp)+1; |
| 856 |
tmp += strlen(hdr->ID.Manufacturer.Version+tmp)+1; |
| 857 |
hdr->ID.Manufacturer.Name = hdr->ID.Manufacturer.Version+tmp; |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 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); |
| 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); |
| 887 |
aECG->Section1.Tag14.LANG_SUPP_CODE = *(PtrCurSect+curSectPos+16); |
| 888 |
aECG->Section1.Tag14.ECG_CAP_DEV = *(PtrCurSect+curSectPos+17); |
| 889 |
aECG->Section1.Tag14.MAINS_FREQ = *(PtrCurSect+curSectPos+18); |
| 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); |
| 898 |
tmp += strlen((char*)(PtrCurSect+curSectPos+36+tmp+1)); |
| 899 |
aECG->Section1.Tag14.ACQ_DEV_MANUF = (char*)(PtrCurSect+curSectPos+36+tmp+1); |
| 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 |
|
| 915 |
|
| 916 |
|
| 917 |
aECG->Section1.Tag15.VERSION = *(PtrCurSect+curSectPos+14); |
| 918 |
} |
| 919 |
else if (tag==16) { |
| 920 |
|
| 921 |
size_t outlen = len1*2+1; |
| 922 |
hdr->ID.Hospital = malloc(outlen); |
| 923 |
if (hdr->ID.Hospital) { |
| 924 |
|
| 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 |
|
| 931 |
|
| 932 |
} |
| 933 |
else if (tag==18) { |
| 934 |
|
| 935 |
|
| 936 |
} |
| 937 |
else if (tag==19) { |
| 938 |
|
| 939 |
|
| 940 |
} |
| 941 |
else if (tag==20) { |
| 942 |
|
| 943 |
aECG->ReferringPhysician = (char*)(PtrCurSect+curSectPos); |
| 944 |
} |
| 945 |
else if (tag==21) { |
| 946 |
|
| 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 |
|
| 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 |
|
| 960 |
|
| 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; |
| 985 |
else if ((bitmap & 0x03)==0) |
| 986 |
Notch = -1; |
| 987 |
else if (bitmap & 0x01) |
| 988 |
Notch = 60.0; |
| 989 |
else if (bitmap & 0x02) |
| 990 |
Notch = 50.0; |
| 991 |
} |
| 992 |
else if (tag==30) { |
| 993 |
|
| 994 |
|
| 995 |
} |
| 996 |
else if (tag==31) { |
| 997 |
|
| 998 |
|
| 999 |
} |
| 1000 |
else if (tag==32) { |
| 1001 |
|
| 1002 |
|
| 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 |
|
| 1019 |
|
| 1020 |
} |
| 1021 |
else if (tag==34) { |
| 1022 |
|
| 1023 |
|
| 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 |
|
| 1032 |
} |
| 1033 |
else if (tag==35) { |
| 1034 |
|
| 1035 |
|
| 1036 |
} |
| 1037 |
else { |
| 1038 |
} |
| 1039 |
curSectPos += len1; |
| 1040 |
} |
| 1041 |
hdr->T0 = tm_time2gdf_time(&t0); |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 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])) |
| 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 |
|
| 1095 |
} |
| 1096 |
} |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
|
| 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 |
|
| 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 |
|
| 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); |
| 1162 |
en1064.Section4.fiducial_sample = leu16p(PtrCurSect+curSectPos+2); |
| 1163 |
en1064.Section4.N = leu16p(PtrCurSect+curSectPos+4); |
| 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 |
|
| 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; |
| 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); |
| 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; |
| 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); |
| 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 |
|
| 1244 |
else if ((curSect==6) && (section[12].length==0)) { |
| 1245 |
|
| 1246 |
hdr->NRec = 1; |
| 1247 |
|
| 1248 |
uint8_t FLAG_HUFFMAN = 0; |
| 1249 |
uint16_t gdftyp = 5; |
| 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); |
| 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; |
| 1309 |
hc->Cal = Cal0 * 1e-3; |
| 1310 |
hc->Off = 0; |
| 1311 |
hc->OnOff = 1; |
| 1312 |
hc->GDFTYP = gdftyp; |
| 1313 |
#ifndef NO_BI |
| 1314 |
hc->bi = i*hdr->SPR*GDFTYP_BITS[gdftyp]>>3; |
| 1315 |
#endif |
| 1316 |
|
| 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); |
| 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 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
AS_DECODE = 1; continue; |
| 1357 |
} |
| 1358 |
#endif |
| 1359 |
|
| 1360 |
if (aECG->FLAG.BIMODAL) { |
| 1361 |
|
| 1362 |
ix = i*hdr->SPR; |
| 1363 |
k1 = en1064.Section4.SPR; |
| 1364 |
k2 = hdr->SPR; |
| 1365 |
uint32_t k3 = en1064.Section4.N-1; |
| 1366 |
uint8_t k4 = 4; |
| 1367 |
do { |
| 1368 |
--k2; |
| 1369 |
data[ix + k2] = data[ix + k1 - 1]; |
| 1370 |
if (k2 > en1064.Section4.beat[k3].QE) { |
| 1371 |
if (--k4==0) {k4=4; --k1; }; |
| 1372 |
} |
| 1373 |
else { |
| 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 |
|
| 1382 |
|
| 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 |
|
| 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 |
|
| 1414 |
|
| 1415 |
curSectPos += 6; |
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
curSectPos += N_QRS*16; |
| 1450 |
|
| 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 |
|
| 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; |
| 1461 |
hc->PhysDimCode = 4275; |
| 1462 |
hc->Cal = 1; |
| 1463 |
hc->Off = 0; |
| 1464 |
hc->OnOff = 1; |
| 1465 |
strcpy(hc->Transducer,"Pacemaker"); |
| 1466 |
hc->GDFTYP = 3; |
| 1467 |
|
| 1468 |
|
| 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 |
|
| 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 |
|
| 1484 |
curSectPos += N_PaceMaker*6; |
| 1485 |
|
| 1486 |
|
| 1487 |
N_QRS = leu16p(PtrCurSect+curSectPos); |
| 1488 |
curSectPos += 2; |
| 1489 |
|
| 1490 |
} |
| 1491 |
|
| 1492 |
|
| 1493 |
else if (curSect==8) { |
| 1494 |
|
| 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 |
|
| 1522 |
else if (curSect==9) { |
| 1523 |
|
| 1524 |
#if (BIOSIG_VERSION >= 10500) |
| 1525 |
|
| 1526 |
|
| 1527 |
#else |
| 1528 |
aECG->Section9.StartPtr = (char*)(PtrCurSect+curSectPos); |
| 1529 |
aECG->Section9.Length = len; |
| 1530 |
#endif |
| 1531 |
} |
| 1532 |
|
| 1533 |
|
| 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 |
|
| 1542 |
else if (curSect==11) { |
| 1543 |
|
| 1544 |
if(len<curSectPos+9) continue; |
| 1545 |
#if (BIOSIG_VERSION >= 10500) |
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 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 |
|
| 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); |
| 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 |
|
| 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; |
| 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 |
|
| 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 |
|
| 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; |
| 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 { |
| 1648 |
hc->LowPass = LowPass; |
| 1649 |
hc->HighPass = HighPass; |
| 1650 |
hc->Notch = Notch; |
| 1651 |
} |
| 1652 |
} |
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 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 |
|
| 1664 |
else if (curSect==13) { |
| 1665 |
} |
| 1666 |
|
| 1667 |
|
| 1668 |
else if (curSect==14) { |
| 1669 |
} |
| 1670 |
|
| 1671 |
|
| 1672 |
else if (curSect==15) { |
| 1673 |
} |
| 1674 |
|
| 1675 |
|
| 1676 |
else if (curSect==16) { |
| 1677 |
} |
| 1678 |
|
| 1679 |
|
| 1680 |
else if (curSect==17) { |
| 1681 |
} |
| 1682 |
|
| 1683 |
|
| 1684 |
else if (curSect==18) { |
| 1685 |
} |
| 1686 |
#endif |
| 1687 |
else { |
| 1688 |
} |
| 1689 |
} |
| 1690 |
|
| 1691 |
|
| 1692 |
deallocEN1064(en1064); |
| 1693 |
|
| 1694 |
|
| 1695 |
return 0; |
| 1696 |
#ifndef WITHOUT_SCP_DECODE |
| 1697 |
if (AS_DECODE==0) return(0); |
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 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 |
|
| 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 |
|
| 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 |
|