Debian Codegraph @

biosig4c++/t210/scp-decode.cpp

raw symbols SCIP
1 /*
2 $Id: scp-decode.cpp,v 1.24 2008-07-12 20:46:58 schloegl Exp $
3 Copyright (C) 2011,2014 Alois Schloegl <[email protected]>
4 Copyright (C) 2011 Stoyan Mihaylov
5 This function is part of the "BioSig for C/C++" repository
6 (biosig4c++) at http://biosig.sf.net/
7
8 Modifications by Alois Schloegl
9 Jul 2011: get rid of warnings for unitialized variables and signed/unsigned comparison
10 Jun 2007: replaced ultoa with sprintf
11 Aug 2007: On-The-Fly-Decompression using ZLIB
12 Oct 2007: Consider SunOS/SPARC platform
13 obsolete code sections marked, this reduced SegFault from 18 to 1.
14
15
16 ---------------------------------------------------------------------------
17 Copyright (C) 2006 Eugenio Cervesato.
18 Developed at the Associazione per la Ricerca in Cardiologia - Pordenone - Italy,
19 based on the work of Eugenio Cervesato & Giorgio De Odorico. The original
20 Copyright and comments follow.
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License
24 as published by the Free Software Foundation; either version 3
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 ---------------------------------------------------------------------------
36
37 ______________________________________________________________________________
38
39 scp-decode.cpp This is the "decode" module of the program SCP-AV.
40 It opens an SCP-ECG v1.0 to v2.0 test file and
41 extracts all the informations.
42
43 Release 2.3 - feb 2006
44 ---------------------------------------------------------------------------
45 **************************************************************************
46
47 ************************* original Copyright *****************************
48 Copyright (C) 2003-2004 Eugenio Cervesato & Giorgio De Odorico.
49 Developed at the Associazione per la Ricerca in Cardiologia - Pordenone - Italy.
50
51 This program is free software; you can redistribute it and/or
52 modify it under the terms of the GNU General Public License
53 as published by the Free Software Foundation; either version 2
54 of the License, or (at your option) any later version.
55
56 This program is distributed in the hope that it will be useful,
57 but WITHOUT ANY WARRANTY; without even the implied warranty of
58 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
59 GNU General Public License for more details.
60
61 You should have received a copy of the GNU General Public License
62 along with this program; if not, write to the Free Software
63 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
64 ---------------------------------------------------------------------------
65 */
66 //______________________________________________________________________________
67 /*
68 scp-decode.cpp This is the "decode" module of the program SCP-AV.
69 It opens an SCP-ECG v1.3 or v2.0 test file and
70 extracts all the informations.
71
72 Developed by ING. GIORGIO DE ODORICO ([email protected])
73
74 Documentation of the standard comes mainly from:
75 http://www.centc251.org/TCMeet/doclist/TCdoc02/N02-015-prEN1064.pdf
76
77 Internationalization, test, bug fix by Eugenio Cervesato ([email protected])
78
79 Release 2.1 - february 2004
80
81 */
82 // ************************* end of the original Copyright *****************************
83
84 // contribution of Michael Breuss <[email protected]>. see 'by MB' in the sources
85 // contribution of Stelios Sfakianakis <[email protected]>. see 'by SS' in the sources
86 // contribution of Federico cantini <[email protected]>. see 'by FeC' in the sources
87
88 //void remark(char *string);
89
90 //
91 #define WITH_OBSOLETE_PARTS
92
93
94 // by E.C. 13.10.2003 part nedded to compile with gcc (Linux).
95 // To compile with Borland C++ add the conditional define: WIN32.
96 // In porting, I nedded to adapt fseek() and write a custom ultoa()
97
98 #define COMPAT
99
100 //______________________________________________________________________________
101
102 //#include <iostream>
103 #include <cstring> //strcat, strcpy
104 #include <cstdio>
105 #include <stdlib.h>
106 using namespace std;
107
108 /*
109 error handling should use error variables local to each HDR
110 otherwise, sopen() etc. is not re-entrant.
111
112 Therefore, use of variables B4C_ERRNUM and B4C_ERRMSG is deprecated;
113 Instead, use biosigERROR for setting error status, and
114 serror2(hdr), hdr->AS.B4C_ERRNUM, hdr->AS.B4C_ERRMSG for error handling.
115
116 */
117 __attribute__ ((deprecated)) extern int B4C_ERRNUM;
118 __attribute__ ((deprecated)) extern const char *B4C_ERRMSG;
119
120
121 //______________________________________________________________________________
122 // FILE POINTERS
123
124 #include "../biosig-dev.h"
125 #include "structures.h"
126 #include "codes.h"
127 // the following define is private of Eugenio Cervesato. Please other readers ignore it!
128 #ifdef CPPBUILDER3
129 #include "CPPBUILDER3.h" // inside are definitions needed to run on C++Builder GUI as a standalone module and bypass ZLIB
130 #endif
131 // end of private define.
132 HDRTYPE* in;
133
134 //---------------------------------------------------------------------------
135 static uint32_t _COUNT_BYTE=1UL; // counter of bytes read
136 static uint32_t _DIM_FILE; // file length in byte
137 static const uint8_t _NUM_SECTION=12U; // sections over 11 are not considered
138
139 //______________________________________________________________________________
140 // section 2
141 uint8_t Input_Bit(uint8_t*,uint16_t&,uint16_t,uint8_t&,bool&);
142 int16_t Input_Bits(uint8_t*,uint16_t&,uint16_t,uint8_t&,uint8_t,bool&);
143 void decompress(TREE_NODE*,int16_t*,uint8_t&,uint16_t&,int32_t*,uint16_t,uint16_t&,table_H*,uint16_t*,uint16_t&);
144 void Tree_Destroy(TREE_NODE*);
145 TREE_NODE *Tree_Create(TREE_NODE*,uint16_t,table_H*,uint16_t);
146 void Huffman(int32_t*,uint16_t*,uint8_t*,uint16_t&,uint16_t,table_H*,uint16_t*);
147 void InitHuffman(table_H*); //inizialize default Huffman table
148
149 //______________________________________________________________________________
150 // sections 3, 4, 5 and 6
151 template<class t1>
152 void Differences(int32_t*,t1,uint8_t);
153 void Multiply(int32_t*,uint32_t,uint16_t);
154 void Interpolate(int32_t*,int32_t*,f_lead,lead*,f_Res,Protected_Area*,uint32_t);
155 void ExecFilter(int32_t*,int32_t*,uint32_t&,uint16_t);
156 void DoFilter(int32_t*,int32_t*,f_Res,f_lead,lead*,Protected_Area*,Subtraction_Zone*);
157 void DoAdd(int32_t*,int32_t*,f_Res,int32_t*,f_BdR0,Subtraction_Zone*,f_lead,lead*);
158 void Opt_Filter(int32_t*, int32_t*, f_Res, f_lead, lead*, Protected_Area*);
159
160 //______________________________________________________________________________
161 // INTERNAL FUNCTIONS
162 char* ReadString(char*,uint16_t); //read a string
163 char *FindString(char*,uint16_t); // calculate the length of a string and write it down
164 int16_t Look(alfabetic*,uint16_t,uint16_t,uint16_t); //look at a number in alfabetic and give the position of the array
165
166 //______________________________________________________________________________
167 template<class t1>
168 void ReadByte(t1&); //read a byte from stream
169 void Skip(uint16_t); //skip some bytes
170
171 //______________________________________________________________________________
172 uint16_t ReadCRC(); //read first 6 bytes of the file
173 bool Check_CRC(uint16_t,uint32_t,uint32_t); // CRC check
174 //______________________________________________________________________________
175
176 uint32_t ID_section(uint32_t, int8_t &version); //read section ID header
177 void sectionsOptional(pointer_section*,DATA_DECODE &,DATA_RECORD&,DATA_INFO&); //handles optional sections
178
179 #ifdef WITH_OBSOLETE_PARTS
180 void section_0(pointer_section*, int size_max); //read section 0
181 void Init_S1(DATA_INFO &inf);
182 void section_1(pointer_section,DATA_INFO&); //read section 1 data
183 void section_1_0(demographic&); //read tag 0 of section 1
184 void section_1_1(demographic&); //read tag 1 of section 1
185 void section_1_2(demographic&); // ... and so on ...
186 void section_1_3(demographic&);
187 void section_1_4(demographic&);
188 void section_1_5(demographic&);
189 void section_1_6(demographic&);
190 void section_1_7(demographic&);
191 void section_1_8(demographic&);
192 void section_1_9(demographic&);
193 void section_1_10(clinic&,uint16_t&);
194 void section_1_11(demographic&);
195 void section_1_12(demographic&);
196 void section_1_13(clinic&,uint16_t&);
197 void section_1_14(descriptive&);
198 void section_1_15(descriptive&);
199 void section_1_16(descriptive&);
200 void section_1_17(descriptive&);
201 void section_1_18(descriptive&);
202 void section_1_19(descriptive&);
203 void section_1_20(clinic&);
204 void section_1_21(clinic&);
205 void section_1_22(clinic&);
206 void section_1_23(descriptive&);
207 void section_1_24(descriptive&);
208 void section_1_25(device&);
209 void section_1_26(device&);
210 void section_1_27(device&);
211 void section_1_28(device&);
212 void section_1_29(device&);
213 void section_1_30(clinic&,uint16_t&);
214 void section_1_31(device&);
215 void section_1_32(clinic&,uint16_t&, int8_t version);
216 void section_1_33(device&);
217 void section_1_34(device&);
218 void section_1_35(clinic&,uint16_t&);
219 void section_1_(); //skip tags of the manufacturer of the section 1
220 void section_1_255(); //read tag 255 of section 1
221 void section_7(pointer_section,DATA_RECORD&, int8_t version); //read section 7
222 void section_8(pointer_section,DATA_INFO&); //read section 8
223 void section_10(pointer_section,DATA_RECORD&, int8_t version); //read section 10
224 void section_11(pointer_section,DATA_INFO&); //read section 11
225 #endif
226 void section_2(pointer_section,DATA_DECODE&); //read section 2
227 void section_3(pointer_section,DATA_DECODE&, int8_t version); //read section 3
228 void section_4(pointer_section,DATA_DECODE&, int8_t version); //read section 4
229 bool section_5(pointer_section,DATA_DECODE&,bool); //read section 5
230 void section_6(pointer_section,DATA_DECODE&,bool); //read section 6
231
232 //______________________________________________________________________________
233 void Decode_Data(pointer_section*,DATA_DECODE&,bool&);
234
235 //______________________________________________________________________________
236 #define STR_NULL StrNull()
237 void *FreeWithCare(void*P){
238 //Stoyan - else I got problems with some scp files - prestandart or so
239 if(P)
240 free(P);
241 return NULL;
242 }
243
244 void *mymalloc(size_t size) // by E.C. 07.11.2003 this is a workaround for a bug
245 { // present somewhere in memory allocation.
246 // char buff[30]; // this problem should be fixed next!
247 // ultoa(size, buff, 10); // used for debug purposes, shows the size
248 // remark(buff);
249 // fprintf(stdout,"MYMEMALLOC: %i\n",size);
250 // void *res=malloc(size*2); // this way each time a doubled memory is requested. And it works!!
251 void *res=malloc(size);
252 return res;
253 }
254 const int StrNullLen=strlen(" unspecified/unknown ");
255 char * StrNull(){
256 //Stoyan this way we can release everything
257 char*Ret=(char*)mymalloc(StrNullLen+4);
258 strcpy(Ret," unspecified/unknown ");
259 return Ret;
260 }
261
262 /* moved by MB
263 must be declared before first call (otherwise compiler error)
264 */
265 //--------------------------------BYTE & BIT------------------------------------
266 template<class t1>
267 void ReadByte(t1 &number)
268 {
269 //read the requested number of bytes and
270 //convert in decimal, taking into account that the first byte is the LSB.
271 //the sign of the number is kept
272 uint8_t *num, dim=sizeof(t1);
273 uint8_t mask=0xFF;
274
275
276 if(dim!=0 && (num=(uint8_t*)mymalloc(dim))==NULL)
277 {
278 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
279 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
280 return;
281 }
282 ifread(num,dim,1,in);
283 // *num = *(uint8_t*)(in->AS.Header+_COUNT_BYTE);
284 number=0;
285 _COUNT_BYTE+=dim;
286
287 while((dim--)>0)
288 {
289 number<<=8;
290 number+=num[dim]&mask;
291 }
292 free(num);
293 }//end ReadByte
294
295 // MAIN
296
297 EXTERN_C void sopen_SCP_clean(struct DATA_DECODE *decode, struct DATA_RECORD *record, struct DATA_INFO *textual) {
298 FreeWithCare(decode->length_BdR0);
299 FreeWithCare(decode->samples_BdR0);
300 FreeWithCare(decode->length_Res);
301 FreeWithCare(decode->samples_Res);
302 FreeWithCare(decode->t_Huffman);
303 FreeWithCare(decode->flag_Huffman);
304 FreeWithCare(decode->data_lead);
305 FreeWithCare(decode->data_protected);
306 FreeWithCare(decode->data_subtraction);
307 FreeWithCare(decode->Median);
308 FreeWithCare(decode->Residual);
309 FreeWithCare(decode->Reconstructed);
310
311 FreeWithCare(record->data_spike);
312 FreeWithCare(record->type_BdR);
313 FreeWithCare(record->data_BdR);
314 FreeWithCare(record->data_additional);
315 FreeWithCare(record->lead_block);
316 FreeWithCare(textual->text_dim);
317 FreeWithCare(textual->data_statement);
318 FreeWithCare(textual->text_statement);
319
320
321 FreeWithCare(textual->ana.last_name);
322 FreeWithCare(textual->ana.first_name);
323 FreeWithCare(textual->ana.ID);
324 FreeWithCare(textual->ana.second_last_name);
325 FreeWithCare(textual->cli.text_drug);
326 FreeWithCare(textual->cli.text_diagnose);
327 FreeWithCare(textual->cli.referring_physician);
328 FreeWithCare(textual->cli.latest_confirming_physician);
329 FreeWithCare(textual->cli.technician_description);
330 FreeWithCare(textual->cli.text_free_text);
331 FreeWithCare(textual->cli.text_free_medical_hystory);
332 FreeWithCare(textual->cli.medical_hystory);
333 FreeWithCare(textual->cli.free_text);
334 FreeWithCare(textual->cli.drug);
335
336 FreeWithCare(textual->des.acquiring.model_description);
337 FreeWithCare(textual->des.acquiring.analysing_program_revision_number);
338 FreeWithCare(textual->des.acquiring.serial_number_device);
339 FreeWithCare(textual->des.acquiring.device_system_software);
340 FreeWithCare(textual->des.acquiring.device_SCP_implementation_software);
341 FreeWithCare(textual->des.acquiring.manifacturer_trade_name);
342 FreeWithCare(textual->des.analyzing.model_description);
343 FreeWithCare(textual->des.analyzing.analysing_program_revision_number);
344 FreeWithCare(textual->des.analyzing.serial_number_device);
345 FreeWithCare(textual->des.analyzing.device_system_software);
346 FreeWithCare(textual->des.analyzing.device_SCP_implementation_software);
347 FreeWithCare(textual->des.analyzing.manifacturer_trade_name);
348 FreeWithCare(textual->des.acquiring_institution);
349 FreeWithCare(textual->des.analyzing_institution);
350 FreeWithCare(textual->des.acquiring_department);
351 FreeWithCare(textual->des.analyzing_department);
352 FreeWithCare(textual->des.room);
353 FreeWithCare(textual->dev.sequence_number);
354 FreeWithCare((char*)textual->dev.TZ.description);
355
356 }
357 //There is serious problem if we try to transfer whole structures between c and c++. I am not sure were exactly it is, but using pointers - solve it. extern C is not enough. Stoyan
358 EXTERN_C int scp_decode(HDRTYPE* hdr, pointer_section *section, struct DATA_DECODE *decode, struct DATA_RECORD *info_recording, struct DATA_INFO *info_textual, bool add_filter)
359 {
360 uint16_t CRC;
361 uint32_t pos;
362 if (hdr->FILE.OPEN) {
363 ifseek(hdr,0,SEEK_SET);
364 }
365 else
366 hdr = ifopen(hdr,"rb");
367
368 if (!hdr->FILE.OPEN)
369 {
370 fprintf(stdout,"Cannot open the file %s.\n",hdr->FileName);
371 return FALSE; // by E.C. 15.10.2003 now return FALSE
372 }
373
374 in = hdr;
375 _COUNT_BYTE=1UL;
376 CRC=ReadCRC();
377 pos=_COUNT_BYTE;
378 ReadByte(_DIM_FILE);
379 // if (CRC != 0xFFFF) Check_CRC(CRC,pos,_DIM_FILE-2U); // by E.C. may 2004 CARDIOLINE 1.0
380 ifseek(in, 0L, SEEK_SET);
381
382 //mandatory sections
383 #ifdef WITH_OBSOLETE_PARTS
384 section_0(section, _DIM_FILE); // by E.C. may 2004 check file size
385 section_1(section[1],*info_textual);
386 sectionsOptional(section,*decode,*info_recording,*info_textual);
387 #else
388
389 if (section[2].length>0)
390 section_2(section[2],*decode); //HUFFMAN
391 if (section[3].length>0)
392 section_3(section[3],*decode,hdr->aECG->Section1.Tag14.VERSION); //lead
393 if (section[4].length)
394 section_4(section[4],*decode,hdr->aECG->Section1.Tag15.VERSION); // fiducial locations
395 if (section[5].length)
396 if (!section_5(section[5],*decode,section[2].length))
397 section[5].length=0 ; //type 0 median beat
398 if (section[6].length)
399 section_6(section[6],*decode,section[2].length); //rhythm compressed data
400
401 #endif
402
403 ifclose(in);
404
405 Decode_Data(section,*decode,add_filter);
406 return TRUE; // by E.C. 15.10.2003 now return TRUE
407 }
408 //______________________________________________________________________________
409 // COMPUTATIONAL FUNCTIONS
410
411 #ifdef WITH_OBSOLETE_PARTS
412 //------------------------------STRINGS----------------------------------------
413 char *ReadString(char *temp_string, uint16_t num)
414 //read a string from the stream.
415 //the first extracted byte is written for fist.
416 //each byte read from the stream is first "transformed" in char.
417 {
418 if(temp_string)
419 free(temp_string);
420 if(!num)
421 return NULL;//before alocating memory, which will be loosed in case of num=0
422 if((temp_string=(char*)mymalloc(sizeof(char)*(num+2)))==NULL) // by E.C. 26.02.2004 one more byte
423 {
424 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
425 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
426 return NULL;
427 }
428 _COUNT_BYTE+=num;
429
430 ifread(temp_string,sizeof(char),num,in);
431 if (temp_string[num-1]!='\0')
432 temp_string[num]='\0';
433
434 return temp_string;
435 }//end ReadString
436
437 int16_t Look(alfabetic *code_, uint16_t a, uint16_t b, uint16_t key_)
438 // look num in code_.number and give the element position
439 {
440 uint16_t middle=(a+b)/2U;
441
442 if(code_[middle].number==key_)
443 return middle;
444 if(a>=b)
445 return -1;
446 if(code_[middle].number<key_)
447 return Look(code_,middle+1,b,key_);
448 else
449 return Look(code_,a,middle-1,key_);
450 }//end Look
451
452 char *FindString(char *temp_string,uint16_t max)
453 //read bytes until NULL
454 //Stoyan - there were memory leaks
455 {
456 if(temp_string)
457 free(temp_string);
458 char c;
459 uint16_t num=0;
460 //fpos_t
461 long filepos;
462
463 if(!max)
464 return NULL;
465
466 filepos = iftell(in); //FGETPOS(in,&filepos);
467 do
468 {
469 c=ifgetc(in);
470 ++num;
471 }
472 while(c!='\0' && num<max);
473
474 ifseek(in,filepos COMPAT,0);
475
476 if((temp_string=(char*)mymalloc(sizeof(char)*(num+2)))==NULL) // by E.C. one extra byte nedded
477 { // for later str_cat()
478 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
479 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
480 return NULL;
481 }
482
483 if(!num)
484 return NULL;
485
486 _COUNT_BYTE+=num;
487
488 ifread(temp_string,sizeof(char),num,in);
489 if (temp_string[num-1]!='\0')
490 temp_string[num]='\0';
491
492 return temp_string;
493 }//end FindString
494 #endif
495
496 void Skip(uint16_t num)
497 //skip num bytes from the stream
498 {
499 if(num>0U)
500 ifseek(in,num,1U);
501 _COUNT_BYTE+=num;
502 }//end Skip
503
504 //______________________________________________________________________________
505 // INITIALIZATION FUNCTIONS
506
507 void InitHuffman(table_H *riga)
508 //build the default Huffman table
509 {
510 /*
511 The table is contructed as stated in the protocol SCP-ECG; each structure in each line.
512 Columns are:
513 bit_prefix = number of bits in the prefix
514 bit_code = number of bits in the code
515 TMS = table mode
516 base_value = base value (decoded)
517 cp = prefix code (in bits)
518 base_code = decimal value of reversed cp
519
520 From the stream I take a bit at a time until I find a correspondent value in the Huffman table.
521 If:
522 nbp=nbc the decode value is vb
523 nbp!=nbc
524 if m=1 I take nbc-nbp bits next in the stream
525 if m=0 change to table vb
526 Remark: Huffman tables stored in the stream contain vb in 2 complemented bytes.
527 The decimal value of cp is included. Furthermore, always the MSB comes first!
528 In my Huffman tables I set cp as a decimal value. Infact, I read a group of bits
529 from the stream and convert them by multipying by 2^pos (where pos is
530 0 for bit 0, 1 for bit 1 and so on). So I read bits as they are.
531
532 DEFAULT HUFFMAN TABLE
533 nbp nbc m vb cp10 cp
534 1, 1, 1, 0, 0, 0,
535 3, 3, 1, 1, 1, 4,
536 3, 3, 1, -1, 5, 5,
537 4, 4, 1, 2, 3, 12,
538 4, 4, 1, -2, 11, 13,
539 5, 5, 1, 3, 7, 28,
540 5, 5, 1, -3, 23, 29,
541 6, 6, 1, 4, 15, 60,
542 6, 6, 1, -4, 47, 61,
543 7, 7, 1, 5, 31, 124,
544 7, 7, 1, -5, 95, 125,
545 8, 8, 1, 6, 63, 252,
546 8, 8, 1, -6, 191, 253,
547 9, 9, 1, 7, 127, 508,
548 9, 9, 1, -7, 383, 509,
549 10, 10, 1, 8, 255, 1020,
550 10, 10, 1, -8, 767, 1021,
551 10, 18, 1, 0, 511, 1022,
552 10, 26, 1, 0, 1023, 1023,
553 */
554
555 uint8_t i;
556
557 i= 0U; riga[i].bit_prefix= 1U; riga[i].bit_code= 1U; riga[i].TMS=1U; riga[i].base_value= 0; riga[i].base_code= 0UL;
558 i= 1U; riga[i].bit_prefix= 3U; riga[i].bit_code= 3U; riga[i].TMS=1U; riga[i].base_value= 1; riga[i].base_code= 1UL;
559 i= 2U; riga[i].bit_prefix= 3U; riga[i].bit_code= 3U; riga[i].TMS=1U; riga[i].base_value=-1; riga[i].base_code= 5UL;
560 i= 3U; riga[i].bit_prefix= 4U; riga[i].bit_code= 4U; riga[i].TMS=1U; riga[i].base_value= 2; riga[i].base_code= 3UL;
561 i= 4U; riga[i].bit_prefix= 4U; riga[i].bit_code= 4U; riga[i].TMS=1U; riga[i].base_value=-2; riga[i].base_code= 11UL;
562 i= 5U; riga[i].bit_prefix= 5U; riga[i].bit_code= 5U; riga[i].TMS=1U; riga[i].base_value= 3; riga[i].base_code= 7UL;
563 i= 6U; riga[i].bit_prefix= 5U; riga[i].bit_code= 5U; riga[i].TMS=1U; riga[i].base_value=-3; riga[i].base_code= 23UL;
564 i= 7U; riga[i].bit_prefix= 6U; riga[i].bit_code= 6U; riga[i].TMS=1U; riga[i].base_value= 4; riga[i].base_code= 15UL;
565 i= 8U; riga[i].bit_prefix= 6U; riga[i].bit_code= 6U; riga[i].TMS=1U; riga[i].base_value=-4; riga[i].base_code= 47UL;
566 i= 9U; riga[i].bit_prefix= 7U; riga[i].bit_code= 7U; riga[i].TMS=1U; riga[i].base_value= 5; riga[i].base_code= 31UL;
567 i=10U; riga[i].bit_prefix= 7U; riga[i].bit_code= 7U; riga[i].TMS=1U; riga[i].base_value=-5; riga[i].base_code= 95UL;
568 i=11U; riga[i].bit_prefix= 8U; riga[i].bit_code= 8U; riga[i].TMS=1U; riga[i].base_value= 6; riga[i].base_code= 63UL;
569 i=12U; riga[i].bit_prefix= 8U; riga[i].bit_code= 8U; riga[i].TMS=1U; riga[i].base_value=-6; riga[i].base_code= 191UL;
570 i=13U; riga[i].bit_prefix= 9U; riga[i].bit_code= 9U; riga[i].TMS=1U; riga[i].base_value= 7; riga[i].base_code= 127UL;
571 i=14U; riga[i].bit_prefix= 9U; riga[i].bit_code= 9U; riga[i].TMS=1U; riga[i].base_value=-7; riga[i].base_code= 383UL;
572 i=15U; riga[i].bit_prefix=10U; riga[i].bit_code=10U; riga[i].TMS=1U; riga[i].base_value= 8; riga[i].base_code= 255UL;
573 i=16U; riga[i].bit_prefix=10U; riga[i].bit_code=10U; riga[i].TMS=1U; riga[i].base_value=-8; riga[i].base_code= 767UL;
574 i=17U; riga[i].bit_prefix=10U; riga[i].bit_code=18U; riga[i].TMS=1U; riga[i].base_value= 0; riga[i].base_code= 511UL;
575 i=18U; riga[i].bit_prefix=10U; riga[i].bit_code=26U; riga[i].TMS=1U; riga[i].base_value= 0; riga[i].base_code=1023UL;
576 }//end InitHuffman
577
578 //______________________________________________________________________________
579 // handle sections
580
581 uint16_t ReadCRC()
582 // read the CRC of the entire file or of a section and convert it to decimal.
583 {
584 uint16_t dim;
585
586 ReadByte(dim);
587
588 return dim;
589 }//end ReadCRC
590
591 bool Check_CRC(uint16_t CRC, uint32_t pos, uint32_t length)
592 /* CRC check starting from pos for Length
593
594 Remark: all computations are in byte.
595 A = new byte
596 B = temp byte
597 CRCHI = MSB of the CRC (16 bits)
598 CRCLO = LSB of the CRC
599
600 START:
601 for A=first_byte to last_byte in block do:
602 A = A xor CRCHI
603 CRCHI = A
604 shift A right 4 times {fulfill with zeroes}
605 A = A xor CRCHI {I J K L M N O P}
606 CRCHI = CRCLO {swap CRCHI, CRCLO}
607 CRCLO = A
608 rotate A left 4 times {M N O P I J K L}
609 B = A { temp save }
610 rotate A left once { N O P I J K L M }
611 A = A and $1F { 0 0 0 I J L L M }
612 CRCHI = A xor CRCHI
613 A = B and $F0 { M N O P 0 0 0 0 }
614 CRCHI = A xor CRCHI { CRCHI complete }
615 rotate B left once { N O P 0 0 0 0 M }
616 B = B and $E0 { N O P 0 0 0 0 0 }
617 CRCLO = B xor CRCLO { CRCLO complete }
618 end
619
620 Final check on the CRC is accomplished by adding or concatenating CRCHI and CRCLO at
621 the end of the data stream. Calculating the checksum of the resulting data stream shall result in
622 a zero CRC if the data was correctly received.
623
624 */
625 {
626 uint32_t i;
627 uint8_t A, B;
628 uint8_t CRCLO, CRCHI;
629
630 CRCLO=0xFF;
631 CRCHI=0xFF;
632 ifseek(in,pos-1,0U);
633 for(i=1;i<=length;i++)
634 {
635 A=ifgetc(in);
636 A^=CRCHI;
637 A^=(A>>4);
638 CRCHI=CRCLO;
639 CRCLO=A;
640 A=(A<<4)|(A>>4U);
641 B=A;
642 A=(A<<1)|(A>>7U);
643 A&=0x1F;
644 CRCHI^=A;
645 A=B&0xF0;
646 CRCHI^=A;
647 B=(B<<1)|(B>>7U);
648 B&=0xE0;
649 CRCLO^=B;
650 }//end for
651
652 CRCLO-=CRC%256UL;
653 CRCHI-=CRC/256UL;
654 if ((CRCLO==CRCHI) && (CRCLO==0))
655 return 1;
656 else
657 {
658 fprintf(stderr,"Cannot read the file: BAD CRC.\n");
659 // exit(2);
660 return 0;
661 }
662 }//end Check_CRC
663
664 uint32_t ID_section(uint32_t pos, int8_t &version)
665 //read section Header
666 {
667 uint32_t dim;
668 uint16_t CRC;
669
670 CRC=ReadCRC();
671 Skip(2U);
672 ReadByte(dim);
673 // if (CRC != 0xFFFF) Check_CRC(CRC,pos+2,dim-2); // by E.C. may 2004 CARDIOLINE 1.0
674 ifseek(in,pos+7L,0);
675 ReadByte(version); // by E.C. may 2004 store the version number
676 Skip(7U);
677
678 return dim;
679 }//end ID_section
680
681
682 void sectionsOptional(pointer_section *section, DATA_DECODE &block1, DATA_RECORD &block2, DATA_INFO &block3)
683 //handles optional sections
684 {
685 uint8_t i=0, bimodal;
686 //initialization
687 block1.t_Huffman=NULL;
688 block1.flag_Huffman=NULL;
689 block1.data_lead=NULL;
690 block1.data_protected=NULL;
691 block1.data_subtraction=NULL;
692 block1.length_BdR0=NULL;
693 block1.samples_BdR0=NULL;
694 block1.Median=NULL;
695 block1.length_Res=NULL;
696 block1.samples_Res=NULL;
697 block1.Residual=NULL;
698 // block1.Reconstructed=NULL;
699 block2.data_spike=NULL;
700 block2.type_BdR=NULL;
701 block2.data_BdR=NULL;
702 block2.data_additional=NULL;
703 block2.lead_block=NULL;
704 block3.text_dim=NULL;
705 block3.text_report=NULL;
706 block3.data_statement=NULL;
707 block3.text_statement=NULL;
708
709 //variables inizialization
710 block1.flag_lead.number=0;
711 block1.flag_lead.subtraction=0;
712 block1.flag_lead.all_simultaneously=0;
713 block1.flag_lead.number_simultaneously=0;
714
715 block1.flag_BdR0.length=0;
716 block1.flag_BdR0.fcM=0;
717 block1.flag_BdR0.AVM=0;
718 block1.flag_BdR0.STM=0;
719 block1.flag_BdR0.number_samples=0;
720 block1.flag_BdR0.encoding=0;
721
722 block1.flag_Res.AVM=0;
723 block1.flag_Res.STM=0;
724 block1.flag_Res.number=0;
725 block1.flag_Res.number_samples=0;
726 block1.flag_Res.encoding=0;
727 block1.flag_Res.bimodal=0;
728 block1.flag_Res.decimation_factor=0;
729
730 block2.data_global.number=0;
731 block2.data_global.number_QRS=0;
732 block2.data_global.number_spike=0;
733 block2.data_global.average_RR=0;
734 block2.data_global.average_PP=0;
735 block2.data_global.ventricular_rate=0;
736 block2.data_global.atrial_rate=0;
737 block2.data_global.QT_corrected=0;
738 block2.data_global.formula_type=0;
739 block2.data_global.number_tag=0;
740
741 block2.header_lead.number_lead=0;
742 block2.header_lead.number_lead_measurement=0;
743
744 while(i<_NUM_SECTION)
745 {
746 if(section[i].ID)
747 switch(section[i].ID)
748 {
749 case 2: if(section[i].length)
750 section_2(section[i],block1); //HUFFMAN
751 break;
752 case 3: if(section[i].length)
753 section_3(section[i],block1,block3.des.acquiring.protocol_revision_number); //lead
754 break;
755 case 4: if(section[i].length)
756 {
757 if((block3.des.acquiring.protocol_revision_number>10) && section[6].length) // by E.C. 27.02.2004 whole section to be included in {} !
758 {
759 ifseek(in,section[6].index+22,0);
760 ReadByte(bimodal);
761 block1.flag_Res.bimodal=bimodal;
762 }
763 else
764 block1.flag_Res.bimodal=0;
765 section_4(section[i],block1,block3.des.analyzing.protocol_revision_number); // fiducial locations
766 }
767 break;
768 case 5: if(section[i].length)
769 if (!section_5(section[i],block1,section[2].length)) section[i].length=0 ; //type 0 median beat
770 break;
771 case 6: if(section[i].length)
772 section_6(section[i],block1,section[2].length); //rhythm compressed data
773 break;
774
775 #ifdef WITH_OBSOLETE_PARTS
776 /*
777 case 7: if(section[i].length)
778 section_7(section[i],block2,block3.des.acquiring.protocol_revision_number); //global measurements
779 break;
780 case 8: if(section[i].length)
781 section_8(section[i],block3); //full text interpretative statements
782 break;
783 case 10:if(section[i].length)
784 section_10(section[i],block2,block3.des.acquiring.protocol_revision_number); //lead measurement block
785 break;
786 case 11:if(section[i].length) //universal ECG interpretative statements
787 // section_11(section[i],block3);
788 break;
789 */
790 #endif
791 }//end switch
792 ++i;
793 }//end while
794 }//end sectionsOptional
795
796 //______________________________________________________________________________
797 // sections
798 //______________________________________________________________________________
799
800 #ifdef WITH_OBSOLETE_PARTS
801
802 //______________________________________________________________________________
803 // section 0
804 //______________________________________________________________________________
805
806 void section_0(pointer_section *info, int size_max)
807 // section 0
808 //build info_sections with ID, offset and length of each section
809 {
810 uint32_t pos, dim, ini;
811 uint16_t ind;
812 uint8_t i;
813 int8_t version;
814
815 ifseek(in,6L,0);
816 pos=ID_section(7L, version)+7L; //length + offset
817 _COUNT_BYTE=7L+16L;
818
819 for(i=0;i<_NUM_SECTION;i++)
820 {
821 info[i].ID=0;
822 info[i].length=0L;
823 info[i].index=0L;
824 }
825 while((_COUNT_BYTE+10)<=pos)
826 {
827 ReadByte(ind);
828 if(ind>11U)
829 Skip(8U);
830 else
831 {
832 ReadByte(dim);
833 if(dim)
834 {
835 ReadByte(ini);
836 if (ini<(unsigned)size_max) { // by E.C. may 2004 check overflow of file
837 info[ind].ID=ind;
838 info[ind].length=dim;
839 info[ind].index=ini;
840 }
841 }//end if dim
842 else
843 Skip(4U);
844 }//end else
845 }//end while
846 }//end section_0
847
848 //______________________________________________________________________________
849 // section 1
850 //______________________________________________________________________________
851 void Init_S1(DATA_INFO &inf)
852 {
853 inf.ana.last_name=STR_NULL;
854 inf.ana.first_name=STR_NULL;
855 inf.ana.ID=STR_NULL;
856 inf.ana.second_last_name=STR_NULL;
857 inf.ana.age.value=0;
858 inf.ana.age.unit=0;
859 inf.ana.height.value=0;
860 inf.ana.height.unit=0;
861 inf.ana.weight.value=0;
862 inf.ana.weight.unit=0;
863 inf.ana.sex=0;
864 inf.ana.race=0;
865 inf.ana.systolic_pressure=0;
866 inf.ana.diastolic_pressure=0;
867
868 inf.cli.number_drug=0;
869 inf.cli.text_drug=STR_NULL;
870 inf.cli.number_diagnose=0;
871 inf.cli.text_diagnose=STR_NULL;
872 inf.cli.referring_physician=STR_NULL;
873 inf.cli.latest_confirming_physician=STR_NULL;
874 inf.cli.technician_description=STR_NULL;
875 inf.cli.number_text=0;
876 inf.cli.text_free_text=STR_NULL;
877 inf.cli.number_hystory=0;
878 inf.cli.number_free_hystory=0;
879 inf.cli.text_free_medical_hystory=STR_NULL;
880 inf.cli.free_text=NULL;
881 inf.cli.medical_hystory=NULL;
882 inf.cli.drug=NULL;
883
884 inf.des.acquiring.institution_number=0;
885 inf.des.acquiring.department_number=0;
886 inf.des.acquiring.ID=0;
887 inf.des.acquiring.type=2;
888 inf.des.acquiring.manifacturer=0;
889 inf.des.acquiring.model_description=STR_NULL;
890 inf.des.acquiring.protocol_revision_number=0;
891 inf.des.acquiring.category=255;
892 inf.des.acquiring.language=255;
893 inf.des.acquiring.capability[0]=1;
894 inf.des.acquiring.capability[1]=2;
895 inf.des.acquiring.capability[2]=3;
896 inf.des.acquiring.capability[3]=4;
897 inf.des.acquiring.AC=0;
898 inf.des.acquiring.analysing_program_revision_number=STR_NULL;
899 inf.des.acquiring.serial_number_device=STR_NULL;
900 inf.des.acquiring.device_system_software=STR_NULL;
901 inf.des.acquiring.device_SCP_implementation_software=STR_NULL;
902 inf.des.acquiring.manifacturer_trade_name=STR_NULL;
903 inf.des.analyzing.institution_number=0;
904 inf.des.analyzing.department_number=0;
905 inf.des.analyzing.ID=0;
906 inf.des.analyzing.type=2;
907 inf.des.analyzing.manifacturer=0;
908 inf.des.analyzing.model_description=STR_NULL;
909 inf.des.analyzing.protocol_revision_number=0;
910 inf.des.analyzing.category=255;
911 inf.des.analyzing.language=255;
912 inf.des.analyzing.capability[0]=1;
913 inf.des.analyzing.capability[1]=2;
914 inf.des.analyzing.capability[2]=3;
915 inf.des.analyzing.capability[3]=4;
916 inf.des.analyzing.AC=0;
917 inf.des.analyzing.analysing_program_revision_number=STR_NULL;
918 inf.des.analyzing.serial_number_device=STR_NULL;
919 inf.des.analyzing.device_system_software=STR_NULL;
920 inf.des.analyzing.device_SCP_implementation_software=STR_NULL;
921 inf.des.analyzing.manifacturer_trade_name=STR_NULL;
922 inf.des.acquiring_institution=STR_NULL;
923 inf.des.analyzing_institution=STR_NULL;
924 inf.des.acquiring_department=STR_NULL;
925 inf.des.analyzing_department=STR_NULL;
926 inf.des.room=STR_NULL;
927 inf.des.stat_code=0;
928
929 inf.dev.baseline_filter=0;
930 inf.dev.lowpass_filter=0;
931 inf.dev.other_filter[0]=0;
932 inf.dev.other_filter[1]=0;
933 inf.dev.other_filter[2]=0;
934 inf.dev.other_filter[3]=0;
935 inf.dev.sequence_number=STR_NULL;
936 inf.dev.electrode_configuration.value=0;
937 inf.dev.electrode_configuration.unit=0;
938 inf.dev.TZ.offset=0;
939 inf.dev.TZ.index=0;
940 inf.dev.TZ.description=STR_NULL;
941 }
942
943 void section_1(pointer_section info_sections, DATA_INFO &inf)
944 // section 1
945 {
946 uint8_t tag;
947 uint32_t num=info_sections.length+_COUNT_BYTE;
948 uint16_t dim=0U;
949 int8_t version;
950
951 _COUNT_BYTE=info_sections.index;
952 ifseek(in,info_sections.index-1,0);
953 ID_section(info_sections.index, version);
954
955 Init_S1(inf);
956
957 do
958 {
959 ReadByte(tag);
960 switch(tag)
961 {
962 case 0:
963 section_1_0(inf.ana); break;
964 case 1:
965 section_1_1(inf.ana); break;
966 case 2:
967 section_1_2(inf.ana); break;
968 case 3:
969 section_1_3(inf.ana); break;
970 case 4:
971 section_1_4(inf.ana); break;
972 case 5:
973 section_1_5(inf.ana); break;
974 case 6:
975 section_1_6(inf.ana); break;
976 case 7:
977 section_1_7(inf.ana); break;
978 case 8:
979 section_1_8(inf.ana); break;
980 case 9:
981 section_1_9(inf.ana); break;
982 case 10:
983 if(!inf.cli.number_drug)
984 {
985 inf.cli.drug=NULL;
986 inf.cli.text_drug=(char*)FreeWithCare(inf.cli.text_drug);
987 dim=0;
988 }
989 section_1_10(inf.cli,dim); break;
990 case 11:
991 section_1_11(inf.ana); break;
992 case 12:
993 section_1_12(inf.ana); break;
994 case 13:
995 if(!inf.cli.number_diagnose)
996 {
997 inf.cli.diagnose=NULL;
998 inf.cli.text_diagnose=(char*)FreeWithCare(inf.cli.text_diagnose);
999 dim=0;
1000 }
1001 section_1_13(inf.cli,dim); break;
1002 case 14:
1003 section_1_14(inf.des); break;
1004 case 15:
1005 section_1_15(inf.des); break;
1006 case 16:
1007 section_1_16(inf.des); break;
1008 case 17:
1009 section_1_17(inf.des); break;
1010 case 18:
1011 section_1_18(inf.des); break;
1012 case 19:
1013 section_1_19(inf.des); break;
1014 case 20:
1015 section_1_20(inf.cli); break;
1016 case 21:
1017 section_1_21(inf.cli); break;
1018 case 22:
1019 section_1_22(inf.cli); break;
1020 case 23:
1021 section_1_23(inf.des); break;
1022 case 24:
1023 section_1_24(inf.des); break;
1024 case 25:
1025 section_1_25(inf.dev); break;
1026 case 26:
1027 section_1_26(inf.dev); break;
1028 case 27:
1029 section_1_27(inf.dev); break;
1030 case 28:
1031 section_1_28(inf.dev); break;
1032 case 29:
1033 section_1_29(inf.dev); break;
1034 case 30:
1035 if(!inf.cli.number_text)
1036 {
1037 inf.cli.text_free_text=(char*)FreeWithCare(inf.cli.text_free_text);
1038 dim=0;
1039 }
1040 section_1_30(inf.cli,dim); break;
1041 case 31:
1042 section_1_31(inf.dev); break;
1043 case 32:
1044 if(!inf.cli.number_hystory)
1045 {
1046 inf.cli.medical_hystory=NULL;
1047 dim=0;
1048 }
1049 section_1_32(inf.cli,dim,inf.des.acquiring.protocol_revision_number); break;
1050 case 33:
1051 section_1_33(inf.dev); break;
1052 case 34:
1053 section_1_34(inf.dev); break;
1054 case 35:
1055 if(!inf.cli.number_free_hystory)
1056 {
1057 inf.cli.free_medical_hystory=NULL;
1058 inf.cli.text_free_medical_hystory=(char*)FreeWithCare(inf.cli.text_free_medical_hystory);
1059 dim=0;
1060 }
1061 section_1_35(inf.cli,dim); break;
1062 case 255:
1063 section_1_255(); break;
1064 default:
1065 section_1_();
1066 break;
1067 }//end switch
1068 }//end do
1069 while((tag!=255) && (_COUNT_BYTE<num));
1070 if ((inf.des.analyzing.protocol_revision_number==0) && (version>0))
1071 inf.des.analyzing.protocol_revision_number=version; // by E.C. may 2004 CARDIOLINE 1.0
1072 }//end section_1
1073
1074 void section_1_0(demographic &ana)
1075 // section 1 tag 0
1076 {
1077 uint16_t dim;
1078
1079 ReadByte(dim);
1080 ana.last_name=ReadString(ana.last_name,dim);
1081 }//end section_1_0
1082
1083 void section_1_1(demographic &ana)
1084 // section 1 tag 1
1085 {
1086 uint16_t dim;
1087
1088 ReadByte(dim);
1089 ana.first_name=ReadString(ana.first_name,dim);
1090 }//end section_1_1
1091
1092 void section_1_2(demographic &ana)
1093 //section 1 tag 2
1094 {
1095 uint16_t dim;
1096
1097 ReadByte(dim);
1098 ana.ID=ReadString(ana.ID,dim);
1099 }//end section_1_2
1100
1101 void section_1_3(demographic &ana)
1102 // section 1 tag 3
1103 {
1104 uint16_t dim;
1105
1106 ReadByte(dim);
1107 ana.second_last_name=ReadString(ana.second_last_name,dim);
1108 }//end section_1_3
1109
1110 void section_1_4(demographic &ana)
1111 // section 1 tag 4
1112 {
1113 uint16_t dim;
1114
1115 ReadByte(dim);
1116 ReadByte(ana.age.value);
1117 ReadByte(ana.age.unit);
1118 if(ana.age.unit>5)
1119 ana.age.unit=0;
1120 }//end section_1_4
1121
1122 void section_1_5(demographic &ana)
1123 // section 1 tag 5
1124 {
1125 uint16_t dim;
1126
1127 uint8_t m, g;
1128 uint16_t a;
1129
1130 ReadByte(dim);
1131 ReadByte(a);
1132 ReadByte(m);
1133 ReadByte(g);
1134
1135 struct tm tmf; // by E.C. feb 2006
1136 tmf.tm_year = a - 1900;
1137 tmf.tm_mon = m - 1;
1138 tmf.tm_mday = g;
1139 tmf.tm_hour = 0;
1140 tmf.tm_min = 0;
1141 tmf.tm_sec = 0;
1142 tmf.tm_isdst = 0;
1143 ana.date_birth2 = mktime(&tmf); // store date in native format
1144
1145 }//end section_1_5
1146
1147 void section_1_6(demographic &ana)
1148 // section 1 tag 6
1149 {
1150 uint16_t dim;
1151
1152 ReadByte(dim);
1153 ReadByte(ana.height.value);
1154 ReadByte(ana.height.unit);
1155 if(ana.height.unit>3)
1156 ana.height.unit=0;
1157 }//end section_1_6
1158
1159 void section_1_7(demographic &ana)
1160 // section 1 tag 7
1161 {
1162 uint16_t dim;
1163
1164 ReadByte(dim);
1165 ReadByte(ana.weight.value);
1166 ReadByte(ana.weight.unit);
1167 if(ana.weight.unit>4)
1168 ana.weight.unit=0;
1169 }//end section_1_7
1170
1171 void section_1_8(demographic &ana)
1172 // section 1 tag 8
1173 {
1174 uint16_t dim;
1175
1176 ReadByte(dim);
1177 ReadByte(ana.sex);
1178 if(ana.sex>2)
1179 ana.sex=3;
1180 }//end section_1_8
1181
1182 void section_1_9(demographic &ana)
1183 // section 1 tag 9
1184 {
1185 uint16_t dim;
1186
1187 ReadByte(dim);
1188 ReadByte(ana.race);
1189 if(ana.race>3)
1190 ana.race=0;
1191 }//end section_1_9
1192
1193 void section_1_10(clinic &cli, uint16_t &dim)
1194 // section 1 tag 10
1195 {
1196 uint16_t val;
1197 uint8_t code_;
1198 char *temp_string=NULL, *pos_char;
1199 int16_t pos;
1200
1201 ReadByte(val);
1202 /*
1203 this tag may have more instances; each instance have a: table code (1 byte), class code (1 byte)
1204 drug code (1 byte), text description of the drug (at least 1 byte with NULL).
1205 */
1206 if(val)
1207 {
1208 if((cli.drug=(info_drug*)realloc(cli.drug,sizeof(info_drug)*(cli.number_drug+1)))==NULL)
1209 {
1210 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1211 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1212 return;
1213 }
1214 ReadByte(cli.drug[cli.number_drug].table);
1215 ReadByte(code_);
1216 if(!cli.drug[cli.number_drug].table)
1217 {
1218 pos=Look(class_drug,0,15,code_);
1219 if(pos<=0)
1220 cli.drug[cli.number_drug].classes=0;
1221 else
1222 cli.drug[cli.number_drug].classes=pos;
1223 }
1224 else
1225 cli.drug[cli.number_drug].classes=code_;
1226 ReadByte(cli.drug[cli.number_drug].drug_code );
1227 if(!cli.drug[cli.number_drug].table)
1228 {
1229 code_=cli.drug[cli.number_drug].drug_code +256*cli.drug[cli.number_drug].classes;
1230 pos=Look(class_drug,16,88,code_);
1231 if(pos<0)
1232 pos=0;
1233 cli.drug[cli.number_drug].drug_code =pos;
1234 }
1235 cli.drug[cli.number_drug].length=val-3; //string length + NULL
1236 if(cli.drug[cli.number_drug].length)
1237 {
1238 temp_string=ReadString(temp_string,cli.drug[cli.number_drug].length);
1239 strcat(temp_string,STR_END);
1240 dim+=strlen(temp_string);
1241 if((cli.text_drug=(char*)realloc(cli.text_drug,sizeof(char)*(dim+1)))==NULL)
1242 {
1243 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1244 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1245 return;
1246 }
1247 pos_char=cli.text_drug;
1248 pos_char+=dim-strlen(temp_string);
1249 strcpy(pos_char,temp_string);
1250 free(temp_string);
1251 }
1252 cli.number_drug++;
1253 }
1254 }//end section_1_10
1255
1256 void section_1_11(demographic &ana)
1257 // section 1 tag 11
1258 {
1259 uint16_t dim;
1260
1261 ReadByte(dim);
1262 if(dim)
1263 ReadByte(ana.systolic_pressure);
1264 else
1265 ana.systolic_pressure=0;
1266 }//end section_1_11
1267
1268 void section_1_12(demographic &ana)
1269 // section 1 tag 12
1270 {
1271 uint16_t dim;
1272
1273 ReadByte(dim);
1274 if(dim)
1275 ReadByte(ana.diastolic_pressure);
1276 else
1277 ana.diastolic_pressure=0;
1278 }//end section_1_12
1279
1280 void section_1_13(clinic &cli, uint16_t &dim)
1281 // section 1 tag 13
1282 {
1283 uint16_t val;
1284 char *temp_string=NULL, *pos_char;
1285
1286 ReadByte(val);
1287 if(val)
1288 {
1289 if((cli.diagnose=(numeric*)realloc(cli.diagnose,sizeof(numeric)*(cli.number_diagnose+1)))==NULL)
1290 {
1291 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1292 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1293 return;
1294 }
1295 cli.diagnose[cli.number_diagnose].unit=cli.number_diagnose+1;
1296 cli.diagnose[cli.number_diagnose].value=val;
1297 temp_string=ReadString(temp_string,cli.diagnose[cli.number_diagnose].value);
1298 strcat(temp_string,STR_END);
1299 dim+=strlen(temp_string);
1300 if((cli.text_diagnose=(char*)realloc(cli.text_diagnose,dim+1))==NULL)
1301 {
1302 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1303 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1304 return;
1305 }
1306 pos_char=cli.text_diagnose;
1307 pos_char+=dim-strlen(temp_string);
1308 strcpy(pos_char,temp_string);
1309 free(temp_string);
1310 cli.number_diagnose++;
1311 }
1312 }//end section_1_13
1313
1314 void section_1_14(descriptive &des)
1315 // section 1 tag 14
1316 {
1317 uint16_t dim, dim_to_skip;
1318 uint8_t i, mask, code_;
1319 int16_t pos;
1320 //fpos_t filepos, filepos_iniz;
1321 long filepos, filepos_iniz;
1322
1323 ReadByte(dim);
1324 filepos = iftell(in); //FGETPOS(in,&filepos);
1325 //FGETPOS(in,&filepos_iniz); // by E.C. may 2004 ESAOTE save to reposition at the end of this section
1326 filepos_iniz=filepos;
1327 dim_to_skip=dim;
1328 dim+=filepos COMPAT;
1329 ReadByte(des.acquiring.institution_number);
1330 ReadByte(des.acquiring.department_number);
1331 ReadByte(des.acquiring.ID);
1332 ReadByte(des.acquiring.type);
1333 if(des.acquiring.type>1)
1334 des.acquiring.type=2;
1335 ReadByte(des.acquiring.manifacturer);
1336 if(des.acquiring.manifacturer>20 && des.acquiring.manifacturer!=255)
1337 des.acquiring.manifacturer=0;
1338 des.acquiring.model_description=ReadString(des.acquiring.model_description,6);
1339 ReadByte(des.acquiring.protocol_revision_number);
1340 ReadByte(des.acquiring.category);
1341 pos=Look(compatibility,0,3,des.acquiring.category);
1342 if(pos<0)
1343 pos=4;
1344 des.acquiring.category=pos;
1345 ReadByte(code_);
1346 if(code_<128U)
1347 pos=0;
1348 else if((code_<192U) && (code_>=128U))
1349 pos=1;
1350 else
1351 {
1352 pos=Look(language_code,2,15,code_);
1353 if(pos<0)
1354 pos=16;
1355 }
1356 des.acquiring.language=pos;
1357 ReadByte(code_);
1358 mask=0x10;
1359 for(i=0;i<4;i++)
1360 {
1361 if(code_&mask)
1362 des.acquiring.capability[i]=i+4;
1363 else
1364 des.acquiring.capability[i]=i;
1365 mask<<=1;
1366 }
1367 ReadByte(des.acquiring.AC);
1368 if(des.acquiring.AC>2)
1369 des.acquiring.AC=0;
1370 Skip(16);
1371 des.acquiring.analysing_program_revision_number=(char*)FreeWithCare(des.acquiring.analysing_program_revision_number);
1372 des.acquiring.serial_number_device=(char*)FreeWithCare(des.acquiring.serial_number_device);
1373 des.acquiring.device_system_software=(char*)FreeWithCare(des.acquiring.device_system_software);
1374 des.acquiring.device_SCP_implementation_software=(char*)FreeWithCare(des.acquiring.device_SCP_implementation_software);
1375 des.acquiring.manifacturer_trade_name=(char*)FreeWithCare(des.acquiring.manifacturer_trade_name);
1376 ReadByte(i);
1377 if(!i)
1378 des.acquiring.analysing_program_revision_number=(char*)FreeWithCare(des.acquiring.analysing_program_revision_number);
1379 else
1380 des.acquiring.analysing_program_revision_number=ReadString(des.acquiring.analysing_program_revision_number,i);
1381
1382 filepos = iftell(in); //FGETPOS(in,&filepos);
1383 des.acquiring.serial_number_device=FindString(des.acquiring.serial_number_device,dim-filepos COMPAT);
1384 if ((des.acquiring.protocol_revision_number==10) || (des.acquiring.protocol_revision_number==11))
1385 // by E.C. may 2004 CARDIOLINE 1.0 & ESAOTE 1.1
1386 ifseek(in,filepos_iniz COMPAT +dim_to_skip,0); // reposition file pointer
1387 else {
1388 filepos = iftell(in); //FGETPOS(in,&filepos);
1389 des.acquiring.device_system_software=FindString(des.acquiring.device_system_software,dim-filepos COMPAT);
1390 filepos = iftell(in); //FGETPOS(in,&filepos);
1391 des.acquiring.device_SCP_implementation_software=FindString(des.acquiring.device_SCP_implementation_software,dim-filepos COMPAT);
1392 filepos = iftell(in); //FGETPOS(in,&filepos);
1393 des.acquiring.manifacturer_trade_name=FindString(des.acquiring.manifacturer_trade_name,dim-filepos COMPAT);
1394 }
1395 }//end section_1_14
1396
1397 void section_1_15(descriptive &des)
1398 // section 1 tag 15
1399 {
1400 uint16_t dim;
1401 uint8_t i, mask, code_;
1402 int16_t pos;
1403 //fpos_t filepos;
1404 long filepos;
1405
1406 ReadByte(dim);
1407 filepos = iftell(in); //FGETPOS(in,&filepos);
1408 dim+=filepos COMPAT;
1409 ReadByte(des.analyzing.institution_number);
1410 ReadByte(des.analyzing.department_number);
1411 ReadByte(des.analyzing.ID);
1412 ReadByte(des.analyzing.type);
1413 if(des.analyzing.type>1)
1414 des.analyzing.type=2;
1415 ReadByte(des.analyzing.manifacturer);
1416 if(des.analyzing.manifacturer>20 && des.analyzing.manifacturer!=255)
1417 des.analyzing.manifacturer=0;
1418 des.analyzing.model_description=ReadString(des.analyzing.model_description,6);
1419 ReadByte(des.analyzing.protocol_revision_number);
1420 ReadByte(des.analyzing.category);
1421 pos=Look(compatibility,0,3,des.analyzing.category);
1422 if(pos<0)
1423 pos=4;
1424 des.analyzing.category=pos;
1425 ReadByte(code_);
1426 if(code_<128U)
1427 pos=0;
1428 else if((code_<192U) && (code_>=128U))
1429 pos=1;
1430 else
1431 {
1432 pos=Look(language_code,2,15,code_);
1433 if(pos<0)
1434 pos=16;
1435 }
1436 des.analyzing.language=pos;
1437 ReadByte(code_);
1438 mask=0x10;
1439 for(i=0;i<4;i++)
1440 {
1441 if(code_&mask)
1442 des.analyzing.capability[i]=i+4;
1443 else
1444 des.analyzing.capability[i]=i;
1445 mask<<=1;
1446 }
1447 ReadByte(des.analyzing.AC);
1448 if(des.analyzing.AC>2)
1449 des.analyzing.AC=0;
1450 Skip(16);
1451 des.analyzing.analysing_program_revision_number=(char*)FreeWithCare(des.analyzing.analysing_program_revision_number);
1452 des.analyzing.serial_number_device=(char*)FreeWithCare(des.analyzing.serial_number_device);
1453 des.analyzing.device_system_software=(char*)FreeWithCare(des.analyzing.device_system_software);
1454 des.analyzing.device_SCP_implementation_software=(char*)FreeWithCare(des.analyzing.device_SCP_implementation_software);
1455 des.analyzing.manifacturer_trade_name=(char*)FreeWithCare(des.analyzing.manifacturer_trade_name);
1456
1457 ReadByte(i);
1458 if(!i)
1459 des.analyzing.analysing_program_revision_number=(char*)FreeWithCare(des.analyzing.analysing_program_revision_number);
1460 else
1461 des.analyzing.analysing_program_revision_number=ReadString(des.analyzing.analysing_program_revision_number,i);
1462
1463 filepos = iftell(in); //FGETPOS(in,&filepos);
1464 des.analyzing.serial_number_device=FindString(des.analyzing.serial_number_device,dim-filepos COMPAT);
1465 filepos = iftell(in); //FGETPOS(in,&filepos);
1466 des.analyzing.device_system_software=FindString(des.analyzing.device_system_software,dim-filepos COMPAT);
1467 filepos = iftell(in); //FGETPOS(in,&filepos);
1468 des.analyzing.device_SCP_implementation_software=FindString(des.analyzing.device_SCP_implementation_software,dim-filepos COMPAT);
1469 filepos = iftell(in); //FGETPOS(in,&filepos);
1470 des.analyzing.manifacturer_trade_name=FindString(des.analyzing.manifacturer_trade_name,dim-filepos COMPAT);
1471 }//end section_1_15
1472
1473 void section_1_16(descriptive &des)
1474 // section 1 tag 16
1475 {
1476 uint16_t dim;
1477
1478 ReadByte(dim);
1479 des.acquiring_institution=ReadString(des.acquiring_institution,dim);
1480 }//end section_1_16
1481
1482 void section_1_17(descriptive &des)
1483 // section 1 tag 17
1484 {
1485 uint16_t dim;
1486
1487 ReadByte(dim);
1488 des.analyzing_institution=ReadString(des.analyzing_institution,dim);
1489 }//end section_1_17
1490
1491 void section_1_18(descriptive &des)
1492 // section 1 tag 18
1493 {
1494 uint16_t dim;
1495
1496 ReadByte(dim);
1497 des.acquiring_department=ReadString(des.acquiring_department,dim);
1498 }//end section_1_18
1499
1500 void section_1_19(descriptive &des)
1501 // section 1 tag 19
1502 {
1503 uint16_t dim;
1504
1505 ReadByte(dim);
1506 des.analyzing_department=ReadString(des.analyzing_department,dim);
1507 }//end section_1_19
1508
1509 void section_1_20(clinic &cli)
1510 // section 1 tag 20
1511 {
1512 uint16_t dim;
1513
1514 ReadByte(dim);
1515 cli.referring_physician=ReadString(cli.referring_physician,dim);
1516 }//end section_1_20
1517
1518 void section_1_21(clinic &cli)
1519 // section 1 tag 21
1520 {
1521 uint16_t dim;
1522
1523 ReadByte(dim);
1524 cli.latest_confirming_physician=ReadString(cli.latest_confirming_physician,dim);
1525 }//end section_1_21
1526
1527 void section_1_22(clinic &cli)
1528 // section 1 tag 22
1529 {
1530 uint16_t dim;
1531
1532 ReadByte(dim);
1533 cli.technician_description=ReadString(cli.technician_description,dim);
1534 }//end section_1_22
1535
1536 void section_1_23(descriptive &des)
1537 // section 1 tag 23
1538 {
1539 uint16_t dim;
1540
1541 ReadByte(dim);
1542 des.room=ReadString(des.room,dim);
1543 }//end section_1_23
1544
1545 void section_1_24(descriptive &des)
1546 // section 1 tag 24
1547 {
1548 uint16_t dim;
1549
1550 ReadByte(dim);
1551 ReadByte(des.stat_code);
1552 }//end section_1_24
1553
1554 void section_1_25(device &dev)
1555 // section 1 tag 25
1556 {
1557 uint16_t dim;
1558 uint8_t m, g;
1559 uint16_t a;
1560
1561 ReadByte(dim);
1562 ReadByte(a);
1563 ReadByte(m);
1564 ReadByte(g);
1565
1566 struct tm tmf; // by E.C. feb 2006
1567 tmf.tm_year = a - 1900;
1568 tmf.tm_mon = m - 1;
1569 tmf.tm_mday = g;
1570 tmf.tm_hour = 0;
1571 tmf.tm_min = 0;
1572 tmf.tm_sec = 0;
1573 tmf.tm_isdst = 0;
1574 dev.date_acquisition2 = mktime(&tmf); // store date in native format
1575
1576 }//end section_1_25
1577
1578 void section_1_26(device &dev)
1579 // section 1 tag 26
1580 {
1581 uint16_t dim;
1582 uint8_t h, m, s;
1583
1584 ReadByte(dim);
1585 ReadByte(h);
1586 ReadByte(m);
1587 ReadByte(s);
1588
1589 dev.time_acquisition2 = (time_t) (s + m*(60 + h*24)); // by E.C. feb 2006 time in seconds
1590 }//end section_1_26
1591
1592 void section_1_27(device &dev)
1593 // section 1 tag 27
1594 {
1595 uint16_t dim;
1596
1597 ReadByte(dim);
1598 ReadByte(dev.baseline_filter);
1599 }//end section_1_27
1600
1601 void section_1_28(device &dev)
1602 // section 1 tag 28
1603 {
1604 uint16_t dim;
1605
1606 ReadByte(dim);
1607 ReadByte(dev.lowpass_filter);
1608 }//end section_1_28
1609
1610 void section_1_29(device &dev)
1611 // section 1 tag 29
1612 {
1613 uint16_t dim;
1614 uint8_t mask=0x1, val, i, max=4, ris=0;
1615
1616 ReadByte(dim);
1617 ReadByte(val);
1618 for(i=0;i<max;i++)
1619 {
1620 if(val&mask)
1621 dev.other_filter[i]=i+1;
1622 else
1623 dev.other_filter[i]=0;
1624 ris|=val&mask;
1625 mask<<=1;
1626 }
1627 int w=dim;
1628 while (--w) ReadByte(val); // by E.C. may 2004 CARDIOLINE 1.0
1629 }//end section_1_29
1630
1631 void section_1_30(clinic &cli, uint16_t &dim)
1632 // section 1 tag 30
1633 {
1634 uint16_t val;
1635 char *temp_string=0, *pos_char;
1636
1637 ReadByte(val);
1638 if(val)
1639 {
1640 if((cli.free_text=(numeric*)realloc(cli.free_text,sizeof(numeric)*(cli.number_text+1)))==NULL)
1641 {
1642 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1643 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1644 return;
1645 }
1646 cli.free_text[cli.number_text].unit=cli.number_text+1;
1647 cli.free_text[cli.number_text].value=val;
1648 temp_string=ReadString(temp_string,cli.free_text[cli.number_text].value);
1649 strcat(temp_string,STR_END);
1650 dim+=strlen(temp_string);
1651 if((cli.text_free_text=(char*)realloc(cli.text_free_text,dim+1))==NULL)
1652 {
1653 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1654 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1655 return;
1656 }
1657 pos_char=cli.text_free_text;
1658 pos_char+=dim-strlen(temp_string);
1659 strcpy(pos_char,temp_string);
1660 free(temp_string);
1661 cli.number_text++;
1662 }
1663 }//end section_1_30
1664
1665 void section_1_31(device &dev)
1666 // section 1 tag 31
1667 {
1668 uint16_t dim;
1669
1670 ReadByte(dim);
1671 if(dim)
1672 dev.sequence_number=ReadString(dev.sequence_number,dim);
1673 else{
1674 dev.sequence_number=(char*)FreeWithCare(dev.sequence_number);
1675 dev.sequence_number=STR_NULL;
1676 }
1677 }//end section_1_31
1678
1679 void section_1_32(clinic &cli, uint16_t &dim, int8_t version)
1680 // section 1 tag 32
1681 {
1682 uint16_t val;
1683 uint8_t pos;
1684 int16_t ris;
1685
1686 ReadByte(val);
1687 if(val)
1688 {
1689 dim+=val;
1690 //first byte designates the Diagnostic Code Table which is applied; if 0 then the codes used are documented in the standard book.
1691 // second byte is the code
1692 if((cli.medical_hystory=(numeric*)realloc(cli.medical_hystory,sizeof(numeric)*(cli.number_hystory+1)))==NULL)
1693 {
1694 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1695 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1696 return;
1697 }
1698 ReadByte(pos);
1699 cli.medical_hystory[cli.number_hystory].value=pos;
1700 if (version != 10) {
1701 ReadByte(pos); // 1 byte vers. 1.0; 2 byte vers. 2.0 by E.C. may 2004
1702 ris=Look(_hystory,0,26,pos);
1703 if(ris<0)
1704 pos=26;
1705 else
1706 pos=ris;
1707 cli.medical_hystory[cli.number_hystory].unit=pos;
1708 }
1709 cli.number_hystory++;
1710 }
1711 }//end section_1_32
1712
1713 void section_1_33(device &dev)
1714 // section 1 tag 33
1715 {
1716 uint16_t dim;
1717 uint8_t pos;
1718
1719 ReadByte(dim);
1720 ReadByte(pos);
1721 if(pos>6)
1722 pos=0;
1723 dev.electrode_configuration.value=pos;
1724
1725 ReadByte(pos);
1726 if(pos>6)
1727 pos=0;
1728 dev.electrode_configuration.unit=pos;
1729 }//end section_1_33
1730
1731 void section_1_34(device &dev)
1732 // section 1 tag 34
1733 {
1734 uint16_t dim;
1735
1736 ReadByte(dim);
1737 ReadByte(dev.TZ.offset); //complemented if negative
1738 ReadByte(dev.TZ.index);
1739 if(dim-4)
1740 dev.TZ.description = FindString((char*)dev.TZ.description,dim-4);
1741 else{
1742 dev.TZ.description = (const char*)realloc((char*)dev.TZ.description,4);
1743 strcpy((char*)dev.TZ.description,"-");
1744 }
1745 }//end section_1_34
1746
1747 void section_1_35(clinic &cli, uint16_t &dim)
1748 // section 1 tag 35
1749 {
1750 uint16_t val;
1751 char *temp_string=NULL, *pos_char;
1752
1753 ReadByte(val);
1754 if(val)
1755 {
1756 if((cli.free_medical_hystory=(numeric*)realloc(cli.free_medical_hystory,sizeof(numeric)*(cli.number_free_hystory+1)))==NULL)
1757 {
1758 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1759 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1760 return;
1761 }
1762 cli.free_medical_hystory[cli.number_free_hystory].unit=cli.number_free_hystory+1;
1763 cli.free_medical_hystory[cli.number_free_hystory].value=val;
1764 temp_string=ReadString(temp_string,cli.free_medical_hystory[cli.number_free_hystory].value);
1765 strcat(temp_string,STR_END);
1766 dim+=strlen(temp_string);
1767 if((cli.text_free_medical_hystory=(char*)realloc(cli.text_free_medical_hystory,dim+1))==NULL)
1768 {
1769 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1770 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1771 return;
1772 }
1773 pos_char=cli.text_free_medical_hystory;
1774 pos_char+=dim-strlen(temp_string);
1775 strcpy(pos_char,temp_string);
1776 free(temp_string);
1777 cli.number_free_hystory++;
1778 }
1779 }//end section_1_35
1780
1781 void section_1_()
1782 // section 1 tag 36..254 are manufacturer specifics and are not utilized
1783 {
1784 uint16_t dim;
1785
1786 ReadByte(dim);
1787 Skip(dim);
1788 }//end section_1_
1789
1790 void section_1_255()
1791 // section 1 tag 255
1792 {
1793 uint16_t dim;
1794
1795 ReadByte(dim);
1796 }//end section_1_255
1797 #endif
1798
1799 //______________________________________________________________________________
1800 // section 2
1801 //______________________________________________________________________________
1802
1803 void section_2(pointer_section info_sections,DATA_DECODE &data)
1804 //build Huffman tables if included in the file; if none then use del default one
1805 //cannot read the dummy Huffman table
1806 {
1807 uint16_t nt, i, j, ns=0, pos, dim;
1808 //fpos_t filepos;
1809 long filepos;
1810 int8_t version;
1811
1812 _COUNT_BYTE=info_sections.index;
1813 ifseek(in,info_sections.index-1,0);
1814 ID_section(info_sections.index, version);
1815 dim=info_sections.length-16;
1816
1817 ReadByte(nt);
1818 if(nt!=19999U)
1819 {
1820 if((data.flag_Huffman=(uint16_t*)mymalloc(sizeof(uint16_t)*(nt+1)))==NULL)
1821 {
1822 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1823 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1824 return;
1825 }
1826 data.flag_Huffman[0]=nt;
1827 filepos = iftell(in); //FGETPOS(in,&filepos);
1828 for(i=1;i<=data.flag_Huffman[0];i++)
1829 {
1830 ReadByte(data.flag_Huffman[i]);
1831 ns+=data.flag_Huffman[i];
1832 Skip(9*data.flag_Huffman[i]);
1833 }
1834 ifseek(in,filepos COMPAT,0);
1835 if((ns*9)>dim || !ns)
1836 {
1837 B4C_ERRNUM = B4C_UNSPECIFIC_ERROR;
1838 B4C_ERRMSG = "SCP-DECODE: Cannot read data";
1839 return;
1840 }
1841 if(ns!=0 && (data.t_Huffman=(table_H*)mymalloc(sizeof(table_H)*ns))==NULL) //array of 5 columns and ns rows
1842 {
1843 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1844 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1845 return;
1846 }
1847 pos=0;
1848 for(j=0;j<data.flag_Huffman[0];j++)
1849 {
1850 Skip(2);
1851 for(i=0;i<data.flag_Huffman[j+1];i++)
1852 {
1853 ReadByte(data.t_Huffman[pos+i].bit_prefix);
1854 ReadByte(data.t_Huffman[pos+i].bit_code);
1855 ReadByte(data.t_Huffman[pos+i].TMS);
1856 ReadByte(data.t_Huffman[pos+i].base_value);
1857 ReadByte(data.t_Huffman[pos+i].base_code);
1858 }
1859 pos+=data.flag_Huffman[j+1]*9; // by E.C. may 2004
1860 }
1861 }
1862 else
1863 {
1864 if((data.flag_Huffman=(uint16_t*)mymalloc(sizeof(uint16_t)*2))==NULL)
1865 {
1866 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1867 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1868 return;
1869 }
1870 data.flag_Huffman[0]=1;
1871 data.flag_Huffman[1]=19; //number of rows of the default Huffman table
1872 if((data.t_Huffman=(table_H*)mymalloc(sizeof(table_H)*data.flag_Huffman[1]))==NULL)
1873 {
1874 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1875 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1876 return;
1877 }
1878 InitHuffman(data.t_Huffman);
1879 }
1880 }//end section_2
1881
1882 //______________________________________________________________________________
1883 // section 3
1884 //______________________________________________________________________________
1885
1886 void section_3(pointer_section info_sections,DATA_DECODE &data, int8_t version)
1887 {
1888 uint8_t val, mask=0x1, i;
1889 int8_t version_loc;
1890
1891 _COUNT_BYTE=info_sections.index;
1892 ifseek(in,info_sections.index-1,0);
1893 ID_section(info_sections.index, version_loc);
1894
1895 ReadByte(data.flag_lead.number);
1896 ReadByte(val);
1897 if(val&mask)
1898 data.flag_lead.subtraction=1;
1899 else
1900 data.flag_lead.subtraction=0;
1901 mask<<=2;
1902 if(val&mask)
1903 data.flag_lead.all_simultaneously=1;
1904 else
1905 data.flag_lead.all_simultaneously=0;
1906 data.flag_lead.number_simultaneously=(val&0xF8)>>3;
1907
1908 if (version==11) // by E.C. may 2004 ESAOTE
1909 data.flag_lead.number_simultaneously=8;
1910
1911 if(data.flag_lead.number!=0 && (data.data_lead=(lead*)mymalloc(sizeof(lead)*data.flag_lead.number))==NULL)
1912 {
1913 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1914 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1915 return;
1916 }
1917 for(i=0;i<data.flag_lead.number;i++)
1918 {
1919 ReadByte(data.data_lead[i].start);
1920 ReadByte(data.data_lead[i].end);
1921 ReadByte(data.data_lead[i].ID);
1922 if(data.data_lead[i].ID>85)
1923 data.data_lead[i].ID=0;
1924 }
1925 }//end section_3
1926
1927 //______________________________________________________________________________
1928 // section 4
1929 //______________________________________________________________________________
1930
1931 void section_4(pointer_section info_sections,DATA_DECODE &data,int8_t version)
1932 {
1933 uint16_t i;
1934 int8_t version_loc;
1935
1936 _COUNT_BYTE=info_sections.index;
1937 ifseek(in,info_sections.index-1,0);
1938 ID_section(info_sections.index, version_loc);
1939
1940 ReadByte(data.flag_BdR0.length);
1941 ReadByte(data.flag_BdR0.fcM);
1942 ReadByte(data.flag_Res.number);
1943 if(data.flag_Res.bimodal || data.flag_lead.subtraction) // by E.C. may 2004
1944 {
1945 if(data.flag_Res.number!=0 && (data.data_subtraction=(Subtraction_Zone*)mymalloc(sizeof(Subtraction_Zone)*data.flag_Res.number))==NULL)
1946 {
1947 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1948 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1949 return;
1950 }
1951 for(i=0;i<data.flag_Res.number;i++)
1952 {
1953 ReadByte(data.data_subtraction[i].beat_type);
1954 ReadByte(data.data_subtraction[i].SB);
1955 ReadByte(data.data_subtraction[i].fc);
1956 ReadByte(data.data_subtraction[i].SE);
1957 }//end for
1958 }//end if
1959 if(data.flag_Res.bimodal || data.flag_lead.subtraction) // by E.C. may 2004
1960 {
1961 if(data.flag_Res.number!=0 && (data.data_protected=(Protected_Area*)mymalloc(sizeof(Protected_Area)*data.flag_Res.number))==NULL)
1962 {
1963 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
1964 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
1965 return;
1966 }
1967 for(i=0;i<data.flag_Res.number;i++)
1968 if ((version==10) || (version==11)) { // by E.C. may 2004 SCP 1.0 no data
1969 data.data_protected[i].QB=data.data_subtraction[i].SB;
1970 data.data_protected[i].QE=data.data_subtraction[i].SE;
1971 } else {
1972 ReadByte(data.data_protected[i].QB);
1973 ReadByte(data.data_protected[i].QE);
1974 }//end if/for
1975 }//end if
1976 }//end section_4
1977
1978 //______________________________________________________________________________
1979 // section 5
1980 //______________________________________________________________________________
1981
1982 bool section_5(pointer_section info_sections,DATA_DECODE &data, bool sez2)
1983 {
1984 uint16_t i;
1985 uint32_t t, dim;
1986 uint16_t value;
1987 int8_t version;
1988
1989 _COUNT_BYTE=info_sections.index;
1990 ifseek(in,info_sections.index-1,0);
1991 ID_section(info_sections.index, version);
1992
1993 ReadByte(data.flag_BdR0.AVM);
1994 ReadByte(data.flag_BdR0.STM);
1995 ReadByte(data.flag_BdR0.encoding);
1996 if(data.flag_BdR0.encoding>2)
1997 data.flag_BdR0.encoding=0;
1998 Skip(1);
1999 if(data.flag_lead.number!=0 && (data.length_BdR0=(uint16_t*)mymalloc(sizeof(uint16_t)*data.flag_lead.number))==NULL)
2000 {
2001 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
2002 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
2003 return false;
2004 }
2005 dim=0;
2006 for(i=0;i<data.flag_lead.number;i++)
2007 {
2008 ReadByte(data.length_BdR0[i]); //number of samples (2 bytes each) for each lead
2009 dim+=data.length_BdR0[i];
2010 }
2011 if (data.flag_BdR0.length==0) return false; // by E.C. 12/09/2007
2012 if(sez2)
2013 {
2014 data.flag_BdR0.number_samples=(uint32_t)data.flag_BdR0.length*1000L/(uint32_t)data.flag_BdR0.STM; //number di campioni per elettrodo
2015 dim*=sizeof(uint8_t);
2016 if(dim!=0 && (data.samples_BdR0=(uint8_t*)mymalloc(dim))==NULL)
2017 {
2018 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
2019 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
2020 return false;
2021 }
2022 ifread(data.samples_BdR0,sizeof(uint8_t),dim,in);
2023 }
2024 else
2025 {
2026 data.flag_BdR0.number_samples=dim/(sizeof(int16_t)*data.flag_lead.number); //number of samples per lead
2027 dim>>=1;
2028 dim*=sizeof(int32_t);
2029 if(dim!=0 && (data.Median=(int32_t*)mymalloc(dim))==NULL)
2030 {
2031 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
2032 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
2033 return false;
2034 }
2035 dim/=sizeof(int32_t);
2036 for(t=0;t<dim;t++)
2037 {
2038 ReadByte(value);
2039 data.Median[t]=value;
2040 if(value>0x7FFF)
2041 data.Median[t]|=0xFFFF0000;
2042 }
2043 }
2044 return true;
2045 }//end section_5
2046
2047 //______________________________________________________________________________
2048 // section 6
2049 //______________________________________________________________________________
2050
2051 void section_6(pointer_section info_sections,DATA_DECODE &data, bool sez2)
2052 {
2053 uint16_t i;
2054 uint32_t t, dim;
2055 uint16_t value;
2056 int8_t version;
2057
2058 _COUNT_BYTE=info_sections.index;
2059 ifseek(in,info_sections.index-1,0);
2060 ID_section(info_sections.index, version);
2061
2062 ReadByte(data.flag_Res.AVM);
2063 ReadByte(data.flag_Res.STM);
2064 ReadByte(data.flag_Res.encoding);
2065 if(data.flag_Res.encoding>2)
2066 data.flag_Res.encoding=0;
2067 Skip(1);
2068 if(data.flag_lead.number!=0 && (data.length_Res=(uint16_t*)mymalloc(sizeof(uint16_t)*data.flag_lead.number))==NULL)
2069 {
2070 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
2071 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
2072 return;
2073 }
2074 dim=0;
2075 for(i=0;i<data.flag_lead.number;i++)
2076 {
2077 ReadByte(data.length_Res[i]); //number of bytes for each lead
2078 dim+=data.length_Res[i];
2079 }
2080 if(sez2)
2081 {
2082 data.flag_Res.number_samples=data.data_lead[1].end-data.data_lead[1].start+1; //number of samples after interpolation
2083 dim*=sizeof(uint8_t);
2084 if(dim!=0 && (data.samples_Res=(uint8_t*)mymalloc(dim))==NULL)
2085 {
2086 B4C_ERRNUM = B4C_INSUFFICIENT_MEMORY;
2087 B4C_ERRMSG = "SCP-DECODE: Not enough memory";
2088 return;
2089 }
2090 ifread(data.samples_Res,sizeof(uint8_t),dim,in);
2091 }
2092 else
2093 {
2094 data.flag_Res.number_samples=dim/(sizeof(int16_t)*data.flag_lead.number); //number of samples per lead
2095 dim>>=1;
2096 dim*=sizeof(int32_t);
2097 if(dim!=0 && (data.Residual=(int32_t*)mymalloc(dim))==NULL)
2098 {
2099 fprintf(stderr,"Not enough memory"); // no, exit //
2100 exit(2);
2101 }
2102 dim/=sizeof(int32_t);
2103 for(t=0;t<dim;t++)
2104 {
2105 ReadByte(value);
2106 data.Residual[t]=value;
2107 if(value>0x7FFF)
2108 data.Residual[t]|=0xFFFF0000;
2109 }
2110 }
2111 }//end section_6
2112
2113 #ifdef WITH_OBSOLETE_PARTS
2114
2115 //______________________________________________________________________________
2116 // section 7
2117 //______________________________________________________________________________
2118
2119 void section_7(pointer_section info_sections ,DATA_RECORD &data, int8_t version)
2120 {
2121 uint16_t i, j, dim;
2122 uint8_t lung;
2123 //fpos_t filepos;
2124 long filepos;
2125 int8_t version_loc;
2126 uint32_t length_eval;
2127
2128 _COUNT_BYTE=info_sections.index;
2129 ifseek(in,info_sections.index-1,0);
2130 ID_section(info_sections.index, version_loc);
2131
2132 ReadByte(data.data_global.number);
2133 //each value should be checked in _special!
2134 ReadByte(data.data_global.number_spike);
2135 if (version==11) ReadByte(data.data_global.number_spike); // by E.C. may 2004 x ESAOTE!! This is an implementation error, for sure!
2136 ReadByte(data.data_global.average_RR);
2137 ReadByte(data.data_global.average_PP);
2138 if(Look(_special,0,3,data.data_global.number)<0)
2139 {
2140 if(data.data_global.number!=0 && (data.data_BdR=(BdR_measurement*)mymalloc(sizeof(BdR_measurement)*data.data_global.number))==NULL)
2141 {
2142 fprintf(stderr,"Not enough memory"); // no, exit //
2143 exit(2);
2144 }
2145 for(i=0;i<data.data_global.number;i++)
2146 {
2147 ReadByte(data.data_BdR[i].P_onset);
2148 ReadByte(data.data_BdR[i].P_offset);
2149 ReadByte(data.data_BdR[i].QRS_onset);
2150 ReadByte(data.data_BdR[i].QRS_offset);
2151 ReadByte(data.data_BdR[i].T_offset);
2152 ReadByte(data.data_BdR[i].P_axis);
2153 ReadByte(data.data_BdR[i].QRS_axis);
2154 ReadByte(data.data_BdR[i].T_axis);
2155 }//end for
2156 }
2157 if(Look(_special,0,3,data.data_global.number_spike)<0)
2158 {
2159 if(data.data_global.number_spike!=0 && (data.data_spike=(spike*)mymalloc(sizeof(spike)*data.data_global.number_spike))==NULL)
2160 {
2161 fprintf(stderr,"Not enough memory"); // no, exit //
2162 exit(2);
2163 }
2164 //spike time is in ms from the start of recording
2165 //amplitude is in signed uV
2166 for(i=0;i<data.data_global.number_spike;i++)
2167 {
2168 ReadByte(data.data_spike[i].time);
2169 ReadByte(data.data_spike[i].amplitude);
2170 }//end for
2171 for(i=0;i<data.data_global.number_spike;i++)
2172 {
2173 ReadByte(data.data_spike[i].type);
2174 if(data.data_spike[i].type>3)
2175 data.data_spike[i].type=0;
2176 ReadByte(data.data_spike[i].source);
2177 if(data.data_spike[i].source>2)
2178 data.data_spike[i].source=0;
2179 ReadByte(data.data_spike[i].index);
2180 ReadByte(data.data_spike[i].pulse_width);
2181 }//end for
2182 }//end if
2183 if (version<13) { // by E.C. may 2004 CARDIOLINE & ESAOTE missing!!
2184 if ((data.data_global.average_RR>0) &&
2185 (data.data_global.average_RR<10000))
2186 data.data_global.ventricular_rate=60000.0/data.data_global.average_RR+0.5;
2187 return;
2188 }
2189 // Insert by F.C.
2190 if (version>=13) {
2191 length_eval = 16 + 6 + data.data_global.number * 16 + data.data_global.number_spike * 4 + data.data_global.number_spike * 6;
2192 if (length_eval >= info_sections.length)
2193 return;
2194 }
2195 // End of F.C. insertion
2196 ReadByte(data.data_global.number_QRS);
2197 if (data.data_global.number_QRS==29999) return; // by E.C. 12/09/2007
2198 if(Look(_special,0,3,data.data_global.number_QRS)<0)
2199 {
2200 filepos = iftell(in); //FGETPOS(in,&filepos); //necessary for ESAOTE and CARDIOLINE test files
2201 dim=info_sections.index+info_sections.length-filepos COMPAT+1;
2202 if(data.data_global.number_QRS>dim)
2203 {
2204 fprintf(stderr,"Error: Cannot extract these data!!!");
2205 exit(2); //necessary for ESAOTE and CARDIOLINE test files
2206 }
2207 if(data.data_global.number_QRS!=0 && (data.type_BdR=(uint8_t*)mymalloc(sizeof(uint8_t)*data.data_global.number_QRS))==NULL)
2208 {
2209 fprintf(stderr,"Not enough memory"); // no, exit //
2210 exit(2);
2211 }
2212 for(i=0;i<data.data_global.number_QRS;i++)
2213 ReadByte(data.type_BdR[i]);
2214 }
2215 // Insert by F.C.
2216 if (version>=13) {
2217 length_eval += (2 + data.data_global.number_QRS);
2218 if (length_eval >= info_sections.length)
2219 return;
2220 }
2221 // End of F.C. insertion
2222 ReadByte(data.data_global.ventricular_rate);
2223 ReadByte(data.data_global.atrial_rate);
2224 ReadByte(data.data_global.QT_corrected);
2225 ReadByte(data.data_global.formula_type);
2226 if(data.data_global.formula_type>2)
2227 data.data_global.formula_type=0;
2228 ReadByte(data.data_global.number_tag);
2229 if(data.data_global.number_tag)
2230 {
2231 data.data_global.number_tag-=2;
2232 data.data_global.number_tag/=7; // tag number
2233 //warnig: this calculation is relative to the structure of STANDARD v2.0!
2234 if(data.data_global.number_tag!=0 && (data.data_additional=(additional_measurement*)mymalloc(sizeof(additional_measurement)*data.data_global.number_tag))==NULL)
2235 {
2236 fprintf(stderr,"Not enough memory"); // no, exit //
2237 exit(2);
2238 }
2239 for(i=0;i<data.data_global.number_tag;i++)
2240 {
2241 ReadByte(data.data_additional[i].ID);
2242 if(data.data_additional[i].ID==255)
2243 break;
2244 if(data.data_additional[i].ID>3)
2245 data.data_additional[i].ID=4;
2246 ReadByte(lung);
2247 if(lung)
2248 {
2249 //warning:(255 is undefined)
2250 for(j=0;j<5;j++)
2251 ReadByte(data.data_additional[i].byte[j]);
2252 }//end if
2253 }//end for
2254 }//end if
2255 }//end section_7
2256
2257 //______________________________________________________________________________
2258 // section 8
2259 //______________________________________________________________________________
2260
2261 void section_8(pointer_section info_sections,DATA_INFO &data)
2262 {
2263 uint8_t m, g, h, s, i;
2264 uint16_t a, dim;
2265 char *c;
2266 //fpos_t filepos;
2267 long filepos;
2268 int8_t version;
2269
2270 _COUNT_BYTE=info_sections.index;
2271 ifseek(in,info_sections.index-1,0);
2272 ID_section(info_sections.index, version);
2273
2274 ReadByte(data.flag_report.type);
2275 if(data.flag_report.type>2)
2276 data.flag_report.type=3;
2277 ReadByte(a);
2278 ReadByte(m);
2279 ReadByte(g);
2280
2281 ReadByte(h);
2282 ReadByte(m);
2283 ReadByte(s);
2284
2285 struct tm tmf;
2286 tmf.tm_year = a;
2287 tmf.tm_mon = m;
2288 tmf.tm_mday = g;
2289 tmf.tm_hour = h;
2290 tmf.tm_min = m;
2291 tmf.tm_sec = s;
2292
2293 data.flag_report.date = (char*)mymalloc(18);
2294 strftime(data.flag_report.date,18,"%d %b %Y", &tmf);
2295
2296 data.flag_report.time = (char*)mymalloc(18);
2297 strftime(data.flag_report.date,18,"%H:%M:%S", &tmf);
2298
2299 ReadByte(data.flag_report.number);
2300 if(data.flag_report.number)
2301 {
2302 filepos = iftell(in); //FGETPOS(in,&filepos);
2303 if(data.flag_report.number!=0 && (data.text_dim=(numeric*)mymalloc(data.flag_report.number*sizeof(numeric)))==NULL)
2304 {
2305 fprintf(stderr,"Not enough memory"); // no, exit //
2306 exit(2);
2307 }
2308 dim=0;
2309 for(i=0;i<data.flag_report.number;i++)
2310 {
2311 //this should start with 1, but I documented 0!!
2312 ReadByte(data.text_dim[i].unit); //sequence number
2313 ReadByte(data.text_dim[i].value); //length
2314 dim+=data.text_dim[i].value;
2315 Skip(data.text_dim[i].value);
2316 }
2317 ifseek(in,filepos COMPAT,0);
2318 if(dim!=0 && (data.text_report=(char*)mymalloc((dim+1)*sizeof(char)))==NULL)
2319 {
2320 fprintf(stderr,"Not enough memory"); // no, exit //
2321 exit(2);
2322 }
2323 c=data.text_report;
2324 for(i=0;i<data.flag_report.number;i++)
2325 {
2326 Skip(3);
2327 char *temp_string=ReadString(NULL,data.text_dim[i].value);
2328 strcat(temp_string,STR_END);
2329 strcpy(c,temp_string);
2330 c+=strlen(temp_string);
2331 free(temp_string);
2332 }//end for
2333 }
2334 }//end section_8
2335
2336 //______________________________________________________________________________
2337 // section 10
2338 //______________________________________________________________________________
2339
2340 void section_10(pointer_section info_sections, DATA_RECORD &data, int8_t version)
2341 {
2342 uint16_t dim, n1, n2, i, j, k, id, val, mask;
2343 int16_t skip;
2344 int8_t version_loc;
2345
2346 _COUNT_BYTE=info_sections.index;
2347 ifseek(in,info_sections.index-1,0);
2348 ID_section(info_sections.index, version_loc);
2349
2350 ReadByte(data.header_lead.number_lead);
2351 ReadByte(dim);
2352 if(dim<6) //no measures
2353 {
2354 if (version != 10) {
2355 fprintf(stderr,"Error: no measures or cannot extract section 10 data!!!");
2356 return; // by E.C. 12/09/2007
2357 }
2358 }
2359 n1=(dim>>1)-2;
2360 if(n1>31) // for now 33 are defined
2361 n1=31; //ignore next bytes
2362 data.header_lead.number_lead_measurement=n1; //max number of statements by the manufacturer (2 bytes each)
2363 if(data.header_lead.number_lead) // by E.C. 17.11.2003 deleted "!" in the if
2364 {
2365 if((data.lead_block=(lead_measurement_block*)mymalloc(data.header_lead.number_lead*sizeof(lead_measurement_block)))==NULL)
2366 {
2367 fprintf(stderr,"Not enough memory"); // no, exit //
2368 exit(2);
2369 }
2370 for(i=0;i<data.header_lead.number_lead;i++)
2371 {
2372 if(data.header_lead.number_lead_measurement) // by E.C. 17.11.2003 deleted "!" in the if
2373 {
2374 ReadByte(id);
2375 if(id>85)
2376 id=0;
2377 ReadByte(dim);
2378 n2=(dim>>1);
2379 if(n2>n1)
2380 skip=(n2-n1)<<1; // bytes to skip
2381 else
2382 skip=0;
2383 data.lead_block[i].ID=id;
2384 //warnig: values relative to _SPECIAL
2385 for(j=1;j<=data.header_lead.number_lead_measurement;j++)
2386 {
2387 switch(j)
2388 {
2389 case 1: ReadByte(data.lead_block[i].P_duration);
2390 break;
2391 case 2: ReadByte(data.lead_block[i].PR_interval);
2392 break;
2393 case 3: ReadByte(data.lead_block[i].QRS_duration);
2394 break;
2395 case 4: ReadByte(data.lead_block[i].QT_interval);
2396 break;
2397 case 5: ReadByte(data.lead_block[i].Q_duration);
2398 break;
2399 case 6: ReadByte(data.lead_block[i].R_duration);
2400 break;
2401 case 7: ReadByte(data.lead_block[i].S_duration);
2402 break;
2403 case 8: ReadByte(data.lead_block[i].R1_duration);
2404 break;
2405 case 9: ReadByte(data.lead_block[i].S1_duration);
2406 break;
2407 case 10: ReadByte(data.lead_block[i].Q_amplitude);
2408 break;
2409 case 11: ReadByte(data.lead_block[i].R_amplitude);
2410 break;
2411 case 12: ReadByte(data.lead_block[i].S_amplitude);
2412 break;
2413 case 13: ReadByte(data.lead_block[i].R1_amplitude);
2414 break;
2415 case 14: ReadByte(data.lead_block[i].S1_amplitude);
2416 break;
2417 case 15: ReadByte(data.lead_block[i].J_point_amplitude);
2418 break;
2419 case 16: ReadByte(data.lead_block[i].Pp_amplitude);
2420 break;
2421 case 17: ReadByte(data.lead_block[i].Pm_amplitude);
2422 break;
2423 case 18: ReadByte(data.lead_block[i].Tp_amplitude);
2424 break;
2425 case 19: ReadByte(data.lead_block[i].Tm_amplitude);
2426 break;
2427 case 20: ReadByte(data.lead_block[i].ST_slope);
2428 break;
2429 case 21: ReadByte(data.lead_block[i].P_morphology);
2430 if(data.lead_block[i].P_morphology)
2431 data.lead_block[i].P_morphology=0;
2432 break;
2433 case 22: ReadByte(data.lead_block[i].T_morphology);
2434 if(data.lead_block[i].T_morphology)
2435 data.lead_block[i].T_morphology=0;
2436 break;
2437 case 23: ReadByte(data.lead_block[i].iso_electric_segment_onset_QRS);
2438 break;
2439 case 24: ReadByte(data.lead_block[i].iso_electric_segment_offset_QRS);
2440 break;
2441 case 25: ReadByte(data.lead_block[i].intrinsicoid_deflection);
2442 break;
2443 case 26: ReadByte(val);
2444 mask=0x3;
2445 for(k=0;k<8;k++)
2446 {
2447 data.lead_block[i].quality_recording[k]=mask&val;
2448 // TODO: code has no effect
2449 mask<<2;
2450 }
2451 break;
2452 case 27: ReadByte(data.lead_block[i].ST_amplitude_Jplus20);
2453 break;
2454 case 28: ReadByte(data.lead_block[i].ST_amplitude_Jplus60);
2455 break;
2456 case 29: ReadByte(data.lead_block[i].ST_amplitude_Jplus80);
2457 break;
2458 case 30: ReadByte(data.lead_block[i].ST_amplitude_JplusRR16);
2459 break;
2460 case 31: ReadByte(data.lead_block[i].ST_amplitude_JplusRR8);
2461 break;
2462 }//end switch
2463 }//end for
2464 if(skip)
2465 Skip(skip);
2466 }//end if
2467 }//end for
2468 }//end if
2469 }//end section_10
2470
2471 //______________________________________________________________________________
2472 // section 11
2473 //______________________________________________________________________________
2474
2475 void section_11(pointer_section info_sections,DATA_INFO &data)
2476 /*
2477 expressions (ASCII) should be either:
2478 1) diagnostic statement_probability_modifiers;
2479 2) diagnostic statement_probability_modifier_conjunctive term_diagnostic statement_probability_modifier...;
2480 in the test files I found only 1 diagnostic statement per expression, ending with a NULL.
2481 */
2482 {
2483 uint8_t m, g, h, s, i, j;
2484 uint16_t a, dim;
2485 char *temp_string=0, *punt, c;
2486 //fpos_t filepos;
2487 long filepos;
2488 int8_t version;
2489
2490 _COUNT_BYTE=info_sections.index;
2491 ifseek(in,info_sections.index-1,0);
2492 ID_section(info_sections.index, version);
2493
2494 ReadByte(data.flag_statement.type);
2495 if(data.flag_statement.type>2)
2496 data.flag_statement.type=3;
2497 ReadByte(a);
2498 ReadByte(m);
2499 ReadByte(g);
2500 ReadByte(h);
2501 ReadByte(m);
2502 ReadByte(s);
2503
2504 struct tm tmf;
2505 tmf.tm_year = a;
2506 tmf.tm_mon = m;
2507 tmf.tm_mday = g;
2508 tmf.tm_hour = h;
2509 tmf.tm_min = m;
2510 tmf.tm_sec = s;
2511
2512 data.flag_statement.date = (char*)mymalloc(18);
2513 strftime(data.flag_statement.date,18,"%d %b %Y", &tmf);
2514
2515 data.flag_statement.time = (char*)mymalloc(18);
2516 strftime(data.flag_statement.time,18,"%H:%M:%S", &tmf);
2517
2518 ReadByte(data.flag_statement.number); //number of expressions
2519 if(!data.flag_statement.number)
2520 {
2521 filepos = iftell(in); //FGETPOS(in,&filepos);
2522 if(data.flag_statement.number!=0 && (data.data_statement=(statement_coded*)mymalloc(data.flag_statement.number*sizeof(statement_coded)))==NULL)
2523 {
2524 fprintf(stderr,"Not enough memory"); // no, exit //
2525 exit(2);
2526 }
2527 dim=0;
2528 for(i=0;i<data.flag_statement.number;i++)
2529 {
2530 ReadByte(data.data_statement[i].sequence_number);
2531 ReadByte(data.data_statement[i].length);
2532 dim+=data.data_statement[i].length-1;
2533 ReadByte(data.data_statement[i].type);
2534 data.data_statement[i].number_field=1;
2535 if(data.data_statement[i].type==1)
2536 {
2537 for(j=1;j<(data.data_statement[i].length-1);j++)
2538 {
2539 c=ifgetc(in);
2540 if(c=='\0')
2541 ++data.data_statement[i].number_field;
2542 }
2543 }
2544 else
2545 Skip(data.data_statement[i].length-1);
2546 }
2547 ifseek(in,filepos COMPAT,0);
2548 if(dim!=0 && (data.text_statement=(char*)mymalloc(dim))==NULL)
2549 {
2550 fprintf(stderr,"Not enough memory"); // no, exit //
2551 exit(2);
2552 }
2553 punt=data.text_statement;
2554 for(i=0;i<data.flag_statement.number;i++)
2555 {
2556 Skip(4);
2557 if(data.data_statement[i].type==1)
2558 {
2559 dim=data.data_statement[i].length;
2560 for(j=0;j<data.data_statement[i].number_field;j++)
2561 {
2562 temp_string=FindString(temp_string,dim);
2563 strcat(temp_string,STR_END);
2564 strcpy(punt,temp_string);
2565 punt+=strlen(temp_string);
2566 dim-=strlen(temp_string);
2567 free(temp_string);
2568 }
2569 }
2570 else
2571 {
2572 temp_string=ReadString(temp_string,data.data_statement[i].length);
2573 strcat(temp_string,STR_END);
2574 strcpy(punt,temp_string);
2575 punt+=strlen(temp_string);
2576 free(temp_string);
2577 }
2578 }//end for
2579 }//end if
2580 }//end section_11
2581
2582 #endif
2583
2584 //______________________________________________________________________________
2585 // CALCULATIONS
2586 //______________________________________________________________________________
2587
2588 //______________________________________________________________________________
2589 // MULTIPLICATION BY AVM
2590 void Multiply(int32_t *dati, uint32_t num, uint16_t AVM)
2591 {
2592 uint32_t i;
2593
2594
2595 for(i=0;i<num;i++)
2596 dati[i]=dati[i]*AVM; // AS 2007: scale in nV - no round of errors
2597 }//end Multiply
2598
2599 //______________________________________________________________________________
2600 // FIRST AND SECOND DIFFERENCES
2601 template<class t1>
2602 void Differences(int32_t *dati, t1 flag, uint8_t num)
2603 {
2604 uint8_t i;
2605 uint16_t j;
2606
2607 for(i=0;i<num;i++)
2608 for(j=flag.encoding;j<flag.number_samples;j++)
2609 if(flag.encoding==1)
2610 dati[(j+flag.number_samples*i)]+=dati[(j+flag.number_samples*i)-1];
2611 else
2612 dati[(j+flag.number_samples*i)]+=(dati[(j+flag.number_samples*i)-1]<<1)-dati[(j+flag.number_samples*i)-2];
2613 }//end Differences
2614
2615 //______________________________________________________________________________
2616 // HUFFMAN DECODING
2617 void Tree_Destroy(TREE_NODE *radix)
2618 {
2619 if (radix!=NULL)
2620 {
2621 Tree_Destroy(radix->next_0);
2622 Tree_Destroy(radix->next_1);
2623 free(radix);
2624 }
2625 return;
2626 }
2627
2628 TREE_NODE *Tree_Create(TREE_NODE *tree, uint16_t n_of_struct, table_H *table, uint16_t pos)
2629 //build a tree
2630 {
2631 uint8_t i,j;
2632 uint32_t mask;
2633 TREE_NODE *temp;
2634
2635 //build the root
2636 if((tree=(TREE_NODE *)mymalloc(sizeof(TREE_NODE)))==NULL)
2637 {
2638 fprintf(stderr,"Not enough memory"); // no, exit //
2639 exit(2);
2640 }
2641 tree->next_0=NULL;
2642 tree->next_1=NULL;
2643 tree->row=-1; //-1 means no row in the table
2644
2645 for (j=0;j<n_of_struct;j++)
2646 {
2647 temp=tree;
2648 mask=0x00000001;
2649 for (i=0;i<table[j+pos].bit_prefix;i++)
2650 {
2651 if (table[j+pos].base_code & mask)
2652 {
2653 if (temp->next_1==NULL)
2654 {
2655 if((temp->next_1=(TREE_NODE *)mymalloc(sizeof(TREE_NODE)))==NULL)
2656 {
2657 fprintf(stderr,"Not enough memory"); // no, exit //
2658 exit(2);
2659 }
2660 temp->next_1->next_0=NULL;
2661 temp->next_1->next_1=NULL;
2662 temp->next_1->row=-1;
2663 }
2664 temp=temp->next_1;
2665 }//end if
2666 else
2667 {
2668 if (temp->next_0==NULL)
2669 {
2670 if((temp->next_0=(TREE_NODE *)mymalloc(sizeof(TREE_NODE)))==NULL)
2671 {
2672 fprintf(stderr,"Not enough memory"); // no, exit //
2673 exit(2);
2674 }
2675 temp->next_0->next_0=NULL;
2676 temp->next_0->next_1=NULL;
2677 temp->next_0->row=-1;
2678 }
2679 temp=temp->next_0;
2680 }//end else
2681 mask <<=1;
2682 }//end for i
2683 temp->row=j; //marks the j row in the table
2684 }//end for j
2685
2686 return tree;
2687 }//end Tree_Create
2688
2689 uint8_t Input_Bit(uint8_t *raw, uint16_t &pos, uint16_t max, uint8_t &mask, bool &err)
2690 {
2691 uint8_t value;
2692
2693 if(pos==max)
2694 {
2695 err=1;
2696 return 0;
2697 }
2698
2699 value=raw[pos]&mask;
2700 mask>>=1;
2701 if(!mask)
2702 {
2703 mask=0x80;
2704 ++pos;
2705 }
2706
2707 return( value ? 1 : 0 );
2708 }//end Input_Bit
2709
2710 int16_t Input_Bits(uint8_t *raw, uint16_t &pos, uint16_t max, uint8_t &mask, uint8_t bit_count, bool &err)
2711 {
2712 uint16_t temp;
2713 int16_t value;
2714
2715 if(pos==max)
2716 {
2717 err=1;
2718 return 0;
2719 }
2720
2721 temp=1<<(bit_count-1);
2722 value=0;
2723 do
2724 {
2725 if(raw[pos]&mask)
2726 value |= temp;
2727 temp>>=1;
2728 mask>>=1;
2729 if(!mask)
2730 {
2731 mask=0x80;
2732 ++pos;
2733 if(pos==max) {
2734 mask=0; // by E.C. may 2004
2735 break;
2736 }
2737 }
2738 } while(temp);
2739 if(temp)
2740 err=1;
2741
2742 if (value & (1<<(bit_count-1))) // by E.C. may 2004 negative value. extend to the left
2743 value|=(-1<<bit_count); // this approach is good for any number of bits
2744
2745 return value;
2746 }//end InputBits
2747
2748 void decompress(TREE_NODE *tree, uint8_t *raw_in, uint16_t &pos_in, uint16_t max_in, int32_t *raw_out, uint16_t &pos_out, uint16_t &max_out, table_H *table ,uint16_t *flag, uint16_t &pos_tH)
2749 {
2750 uint16_t i, j;
2751 uint8_t nbits;
2752 TREE_NODE *temp;
2753 uint8_t mask=0x80;
2754 bool err;
2755 uint16_t maxIN=max_in+pos_in;
2756
2757 j=0;
2758 while(pos_in<maxIN) //check number of bytes
2759 {
2760 temp=tree;
2761 for (;;)
2762 {
2763 err=0;
2764 if (Input_Bit(raw_in,pos_in,maxIN,mask,err)) // for each bit read follow the tree
2765 temp=temp->next_1;
2766 else
2767 temp=temp->next_0;
2768 if (temp==NULL)
2769 {
2770 fprintf(stderr,"Tree overflow");
2771 err=1;
2772 break;
2773 }
2774 if (temp->row > -1) // row found in the table: exit for
2775 break;
2776 if(pos_in==maxIN)
2777 err=1;
2778 if(err)
2779 break;
2780 }//end for
2781 if(err) // check for error conditions and if exit while
2782 {
2783 break; // by E.C. may 2004
2784 }
2785 if(table[temp->row+pos_tH].TMS!=1) // switch to another table
2786 {
2787 Tree_Destroy(tree); // destroy the tree and rebuild with the new table
2788 pos_tH=0;
2789 for(i=1;i<table[temp->row+pos_tH].base_value;i++)
2790 pos_tH+=flag[i]; // offset of the table
2791 tree=Tree_Create(tree,flag[table[temp->row+pos_tH].base_value],table,pos_tH);
2792 continue;
2793 }//end if table
2794 else
2795 {
2796 nbits=table[temp->row+pos_tH].bit_code-table[temp->row+pos_tH].bit_prefix;
2797 if (nbits) // bit of the code != bit of the prefix
2798 {
2799 if(pos_in==maxIN)
2800 {
2801 err=1;
2802 break; // by E.C. may 2004
2803 }
2804 raw_out[pos_out]=Input_Bits(raw_in,pos_in,maxIN,mask,nbits,err); // take value from the stream
2805 if(err) // check for error conditions and if exit while
2806 {
2807 break; // by E.C. may 2004
2808 }
2809 ++pos_out;
2810 }//end if nbits
2811 else // bit of the code = bit of the prefix
2812 {
2813 raw_out[pos_out]=table[temp->row+pos_tH].base_value; // take value from the table
2814 ++pos_out;
2815 }
2816 }//end else if table
2817 ++j;
2818 if (j==max_out) break; // by E.C. may 2004
2819 } //end while
2820 pos_in=maxIN; // by E.C. 23.02.2004: align for safety
2821 max_out=j; // flows here anyhow!
2822 if (max_out>4900) {
2823 max_out=5000; // by E.C. may 2004 ESAOTE
2824 pos_out=(pos_out+100)/max_out*max_out; // align pointer
2825 }
2826 }//end decompress
2827
2828 //data.data_BdR0 , data.length_BdR0 , data.samples_BdR0 , data.flag_BdR0.number_samples , data.flag_lead.number , data.t_Huffman , data.flag_Huffman
2829 //out_data , length , in_data , n_samples , n_lead , t_Huffman , flag_Huffman
2830 void Huffman(int32_t *out_data, uint16_t *length, uint8_t *in_data, uint16_t &n_samples, uint16_t n_lead, table_H *t_Huffman, uint16_t *flag_Huffman)
2831 {
2832 TREE_NODE *tree = NULL;
2833 uint16_t pos_in, pos_out, pos_tH;
2834 uint8_t i;
2835
2836 pos_in=0;
2837 pos_out=0;
2838 pos_tH=0;
2839 tree=Tree_Create(tree,flag_Huffman[1],t_Huffman,pos_tH);
2840 for(i=0;i<n_lead;i++)
2841 {
2842 pos_tH=0;
2843 if(pos_tH)
2844 {
2845 Tree_Destroy(tree);
2846 tree=Tree_Create(tree,flag_Huffman[1],t_Huffman,pos_tH);
2847 }
2848 decompress(tree,in_data,pos_in,length[i],out_data,pos_out,n_samples,t_Huffman,flag_Huffman,pos_tH);
2849 }
2850 Tree_Destroy(tree);
2851 }//end Huffman
2852
2853 void Decode_Data(pointer_section *section, DATA_DECODE &data, bool &add_filter)
2854 {
2855 uint32_t t;
2856 uint32_t dim_B, dim_R, dim_R_, number_samples_;
2857
2858 int32_t *dati_Res_ = NULL;
2859 if(!data.flag_Huffman){//Or we will get crash
2860 return;
2861 }
2862 //Decode the reference beat
2863 if(section[5].length)
2864 {
2865 if(section[2].length && data.flag_Huffman[0])
2866 {
2867 dim_B=data.flag_BdR0.number_samples*sizeof(int32_t)*data.flag_lead.number; //whole number of bytes
2868 if(dim_B!=0 && (data.Median=(int32_t*)mymalloc(dim_B))==NULL)
2869 {
2870 fprintf(stderr,"Not enough memory"); // no, exit //
2871 exit(2);
2872 }
2873 Huffman(data.Median,data.length_BdR0,data.samples_BdR0,data.flag_BdR0.number_samples,data.flag_lead.number,data.t_Huffman,data.flag_Huffman);
2874 free(data.samples_BdR0);
2875 data.samples_BdR0=NULL;
2876 }
2877 else
2878 {
2879 dim_B=data.flag_BdR0.number_samples*sizeof(int16_t)*data.flag_lead.number; //number of samples of all leads
2880 //they shuld be equal!!!
2881 }
2882 free(data.length_BdR0);
2883 data.length_BdR0=NULL;
2884 if(data.flag_BdR0.encoding)
2885 Differences(data.Median,data.flag_BdR0,data.flag_lead.number);
2886
2887 Multiply(data.Median,data.flag_BdR0.number_samples*data.flag_lead.number,data.flag_BdR0.AVM);
2888 }//end if
2889
2890 //Decode rhythm data
2891 if(section[6].length)
2892 {
2893 dim_R_=data.flag_Res.number_samples*data.flag_lead.number*sizeof(int32_t); //whole number of bytes
2894 number_samples_=data.flag_Res.number_samples; //number of samples per lead after interpolation
2895 if(dim_R_!=0 && (data.Residual=(int32_t*)mymalloc(dim_R_))==NULL)
2896 {
2897 fprintf(stderr,"Not enough memory"); // no, exit //
2898 exit(2);
2899 }
2900 if(section[2].length && data.flag_Huffman[0])
2901 Huffman(data.Residual,data.length_Res,data.samples_Res,data.flag_Res.number_samples,data.flag_lead.number,data.t_Huffman,data.flag_Huffman);
2902
2903 dim_R=data.flag_Res.number_samples*sizeof(int32_t)*data.flag_lead.number; //number of bytes of all leads before interpolation (also, data.flag_Res.number_samples has been changed to the number of samples from HUffman)
2904 free(data.samples_Res);
2905 free(data.length_Res);
2906 data.samples_Res=NULL;
2907 data.length_Res=NULL;
2908
2909 if(data.flag_Res.encoding)
2910 Differences(data.Residual,data.flag_Res,data.flag_lead.number);
2911
2912 //calculate resampling rate
2913 if(data.flag_Res.bimodal || data.flag_lead.subtraction) // by E.C. 19.02.2004 (i.e. to open p120n00)
2914 {
2915 if(section[5].length)
2916 data.flag_Res.decimation_factor=data.flag_Res.STM/data.flag_BdR0.STM;
2917 else
2918 data.flag_Res.decimation_factor=0; //no interpolation!!
2919 //exec interpolation
2920 if(data.flag_Res.decimation_factor>1) // by E.C. 19.02.2004 (i.e. to open pd3471)
2921 {
2922 //dim_R = number of bytes of decimated signal (4 bytes (int32_t) )
2923 //data.flag_Res.number_samples = samples of the decimated signal
2924 //dim_R_ = number of bytes of the reconstructed signal (4 bytes (int32_t) )
2925 //number_samples_ = samples of the reconstructed signal
2926
2927 data.flag_Res.number_samples=number_samples_; //number of samples per lead
2928 number_samples_=dim_R/(sizeof(int32_t)*data.flag_lead.number);
2929 dim_R=dim_R_;
2930 dim_R_=(dim_R/sizeof(int32_t))*sizeof(float)*2;
2931
2932 //dim_R_ = number of bytes of decimated signal (4 bytes (int32_t) )
2933 //number_samples_ = samples of the decimated signal
2934 //dim_R = number of bytes of the reconstructed signal (4 bytes (int32_t) )
2935 //data.flag_Res.number_samples = samples of the reconstructed signal
2936
2937 if(dim_R_!=0 && (dati_Res_=(int32_t*)mymalloc(dim_R_))==NULL)
2938 {
2939 fprintf(stderr,"Not enough memory"); // no, exit //
2940 exit(2);
2941 }
2942 Interpolate(dati_Res_,data.Residual,data.flag_lead,data.data_lead,data.flag_Res,data.data_protected,number_samples_);
2943 DoFilter(data.Residual,dati_Res_,data.flag_Res,data.flag_lead,data.data_lead,data.data_protected,data.data_subtraction);
2944 free(dati_Res_);
2945 //dim_R modifies
2946 }
2947 }
2948 Multiply(data.Residual,data.flag_Res.number_samples*data.flag_lead.number,data.flag_Res.AVM);
2949
2950 /* AS 2007-10-23: for some (unknown) reason, sometimes the memory allocation has the wrong size
2951 doing the memory allocation in sopen_scp_read does it correctly.
2952 Number of files with SegFaults is reduced by 3.
2953
2954 if(dim_R!=0 && (data.Reconstructed=(int32_t*)mymalloc(dim_R))==NULL)
2955 {
2956 fprintf(stderr,"Not enough memory"); // no, exit //
2957 exit(2);
2958 }
2959 */
2960 unsigned dim_RR=dim_R/sizeof(int32_t); // by E.C. 15.10.2003 This to correct a trivial error
2961 for(t=0;t<dim_RR;t++) // of array overflow!!
2962 data.Reconstructed[t]=data.Residual[t]; // by E.C. 19.02.2004: first copy rhythm then add the reference beat
2963
2964 if(section[3].length && section[5].length && data.flag_lead.subtraction)
2965 {
2966 DoAdd(data.Reconstructed,data.Residual,data.flag_Res,data.Median,data.flag_BdR0,data.data_subtraction,data.flag_lead,data.data_lead);
2967 if (add_filter && (data.flag_Res.decimation_factor>1)){ // by E.C. 25.02.2004
2968 // now that the signal is completely reconstructructed, and if decimation was performed
2969 // during the compression phase, do an extra low-pass filter outside protected zones !
2970 // This shows lower RMS values of about 0.5 - 1.0
2971 // If you don't like this extra filtering please uncheck the menu option and reopen
2972 for(t=0;t<dim_RR;t++)
2973 data.Residual[t]=data.Reconstructed[t]; // reuse array as temp storage
2974 Opt_Filter(data.Reconstructed,data.Residual,data.flag_Res,data.flag_lead,data.data_lead,data.data_protected);
2975 for(t=0;t<dim_RR;t++)
2976 data.Residual[t]=data.Reconstructed[t]; // reuse array as temp storage
2977 Opt_Filter(data.Reconstructed,data.Residual,data.flag_Res,data.flag_lead,data.data_lead,data.data_protected);
2978 }
2979 else add_filter=false;
2980 }
2981 else add_filter=false;
2982 }
2983 }
2984
2985 //______________________________________________________________________________
2986 // sections 3, 4, 5, and 6
2987 // dati_Res_ ,data.Residual ,dim_R ,data.flag_lead.number ,data.flag_Res, data.data_lead ,data.data_protected)
2988
2989 void Interpolate(int32_t *raw_out, int32_t *raw_in, f_lead flag_L, lead *marker_A, f_Res flag, Protected_Area *marker_Z, uint32_t sample_Huff)
2990 //dim_out = n byte in raw_out
2991 //dim_in = " raw_in
2992 //marker_Z = no-subtraction zones
2993 //marker_A = start/end for each lead
2994 //dec = decimation factor
2995 //
2996 //for each lead, copy the first and the last two samples, then insert max 3 samples
2997 // between each couple (this means that starting from the first and excluded the last,
2998 // I create 4 samples).
2999 //marker_Z contains the start (marker_Z->info.start=QB) and the end (marker_Z->info..end=QE)
3000 //of all the no-subtraction zones.
3001 //marker_A contains the start (marker_A.start = fist sample) and the end (marker_A.end = last sample)
3002 //of lead data.
3003 /* given the interval [a,b] where to perform the interpolation, assumed that
3004 b>=a, the algorithm does:
3005 - mINS=((QB(k)-1)-(QE(k-1)+1)+1)/dec=(b-a+1)/dec (# of samples to expand)
3006 - mCOPY=(b-a+1)%dec (# of samples left)
3007 - in a "normal" situation where (b-a+1) is a multiple of dec (and mCOPY is 0) the algorithm does:
3008 - insert two samples as found in a and a+1
3009 - insert dec samples according to the formula:
3010 Z[i]=Va+j*D with D=(Vb-Va)/dec and i=0,1,2,3
3011 (where Va=a and Vb=a+1)
3012 i.e. between two samples I insert 3 samples.
3013 Finally, I insert two samples b-1 and b.
3014 - in "not normal" conditions, I do:
3015 for num=(b-a+1)<4: mCOPY may be from 1 to 3.
3016 samples are copied as they are;
3017
3018 If a>b : may be
3019 - the first sample is the first of the first protected zone (a=b+1=1)
3020 - empty no-protection zone (a=b+1)
3021 - the last sample is the last of the last protected zone (a=b+1)
3022 in these situations samples are copied as they are.
3023 */
3024 {
3025 uint16_t a, b; //interpolation range
3026 float v; //interpolation value
3027 uint8_t j; //working variable
3028 uint16_t dim; //number of samples (to skip) into a protected zone
3029 uint16_t mINS, mCOPY; //number of samples to insert, copy
3030 int16_t num; //number of samples to interpolate
3031 uint32_t pos_in, pos_out;
3032 uint16_t nz; //index of protected zones, i.e. index of QRS
3033 uint8_t ne; //index of lead
3034
3035 pos_in=0;
3036 pos_out=0;
3037 for(ne=0;ne<flag_L.number;ne++) // by E.C. 19.02.2004 loop on leads
3038 {
3039 for (nz=0;nz<=flag.number;nz++) // loop on QRS for each lead
3040 {
3041 //set limits for interpolation
3042 if(nz==0)
3043 {
3044 a=marker_A[ne].start; // first interval 1 - QB[0]
3045 b=marker_Z[nz].QB-1;
3046 }
3047 else if (nz==flag.number)
3048 {
3049 a=marker_Z[nz-1].QE+1; // last interval QE[flag.number-1] - 5000
3050 b=marker_A[ne].end;
3051 }
3052 else
3053 {
3054 a=marker_Z[nz-1].QE+1; // interval between two QRS
3055 b=marker_Z[nz].QB-1;
3056 }
3057 //number of samples
3058 num=b-a+1;
3059 if(num>0)
3060 {
3061 mINS=num/flag.decimation_factor; //number to interpolate
3062 mCOPY=num%flag.decimation_factor; //if, residual number to copy
3063 if(mINS)
3064 {
3065 //store first two samples equal to the first value in the list ...
3066 raw_out[pos_out++]=raw_in[pos_in];
3067 raw_out[pos_out++]=raw_in[pos_in];
3068 }
3069 // ... then proceed with interpolation
3070 dim=mINS;
3071 while((dim--)>1)
3072 {
3073 v=1.0*(raw_in[pos_in+1]-raw_in[pos_in])/flag.decimation_factor; // by E.C. 23.02.2004 (float v)
3074 // rounding improves RMS!
3075 for(j=0;j<flag.decimation_factor;j++)
3076 raw_out[pos_out++]=raw_in[pos_in]+j*v;
3077 if(pos_in<(sample_Huff*(ne+1))) // by E.C. 19.02.2004 check overflow
3078 ++pos_in;
3079 }//end while
3080 if(pos_in>=(sample_Huff*(ne+1))) //check overflow
3081 break;
3082 if(mINS)
3083 {
3084 raw_out[pos_out++]=raw_in[pos_in];
3085 raw_out[pos_out++]=raw_in[pos_in];
3086 if(pos_in<(sample_Huff*(ne+1))) // by E.C. 19.02.2004 check overflow
3087 ++pos_in;
3088 }
3089 //not normal situation? insert mCOPY samples
3090 while((mCOPY--)>0)
3091 if(pos_in<(sample_Huff*(ne+1))) // by E.C. 19.02.2004 check overflow
3092 raw_out[pos_out++]=raw_in[pos_in++];
3093 else
3094 raw_out[pos_out++]=0;
3095 }//end if num>0
3096
3097 if (nz<flag.number) {
3098 dim=marker_Z[nz].QE-marker_Z[nz].QB+1; // copy protected zone
3099 while((dim--)>0)
3100 raw_out[pos_out++]=raw_in[pos_in++];
3101 }
3102 }//for nz
3103 pos_in=sample_Huff*(ne+1); // by E.C. 19.02.2004 align, never mind!!
3104 pos_out=(pos_out+100)/5000*5000; // by E.C. may 2004 for testing purposes only
3105 }//for ne
3106 }//end Interpolate
3107
3108 void ExecFilter(int32_t *raw_in, int32_t *raw_out, uint32_t &pos, uint16_t dim)
3109 //filter from pos for dim samples
3110 {
3111 int32_t v; //value
3112 uint16_t i;
3113
3114 if (dim>0)
3115 {
3116 //fist sample = unchanged
3117 raw_out[pos]=raw_in[pos]; pos++;
3118 if(dim>2)
3119 for(i=2;i<dim;i++) // do filter
3120 { // as mean of three samples
3121 v=raw_in[pos-1]+raw_in[pos]+raw_in[pos+1];
3122
3123 if(v>=0) // as suggested by the standard for rounding
3124 v+=1;
3125 else
3126 v-=1;
3127
3128 raw_out[pos++]=v/3; // by E.C. 24.02.2004 in this case, declaring v as float doesn't change results
3129 }
3130 }
3131 //last sample = unchanged
3132 if(dim>1) {
3133 raw_out[pos]=raw_in[pos]; pos++; }
3134 }//end ExecFilter
3135
3136 void DoFilter(int32_t *raw_out, int32_t *raw_in, f_Res flag, f_lead flag_L, lead *marker_A, Protected_Area *marker_P, Subtraction_Zone *marker_S)
3137 //filter low-pass outside the proteced zones (marker_Z) for each lead (marker_A)
3138 // but taking into account transients at the boundaries of the subtraction zones (marker_S)
3139 //It's included rounding.
3140 {
3141 uint16_t a, b=0; //interval
3142 int16_t num;
3143 uint32_t pos;
3144 uint16_t nz;
3145 uint8_t ne; //index of lead
3146
3147 pos=0; // by E.C. 19.02.2004 function redefined such as Interpolate()
3148 for(ne=0;ne<flag_L.number;ne++) // loop on leads
3149 {
3150 for (nz=0;nz<=flag.number;nz++) // loop on QRS of each lead
3151 {
3152 //set interval to filter
3153 if(nz==0)
3154 {
3155 a=marker_A[ne].start;
3156 if (marker_S[nz].beat_type==0) {
3157 b=marker_S[nz].SB-1;
3158 num=b-a+1;
3159 ExecFilter(raw_in,raw_out,pos,num); // if, 1 - SB[0]-1
3160 a=marker_S[nz].SB; // by E.C. may 2004 (handle open and close intervals)
3161 }
3162 b=marker_P[nz].QB;
3163 num=b-a+1;
3164 ExecFilter(raw_in,raw_out,pos,num); // (1 or) SB[0] - QB[0]
3165 }
3166 else if (nz==flag.number)
3167 {
3168 a=marker_P[nz-1].QE;
3169 if (marker_S[nz-1].beat_type==0) {
3170 b=marker_S[nz-1].SE;
3171 num=b-a+1;
3172 ExecFilter(raw_in,raw_out,pos,num); // if, QE[last] - SE[last]
3173 a=marker_S[nz-1].SE+1;
3174 }
3175 b=marker_A[ne].end;
3176 num=b-a+1;
3177 ExecFilter(raw_in,raw_out,pos,num); // (QE[last] or) SE[last]+1 - 5000
3178 }
3179 else
3180 {
3181 a=b+1;
3182 if (marker_S[nz-1].beat_type==0) {
3183 b=marker_S[nz-1].SE;
3184 num=b-a+1;
3185 ExecFilter(raw_in,raw_out,pos,num); // in between
3186 a=marker_S[nz-1].SE+1;
3187 }
3188 if (marker_S[nz].beat_type==0) {
3189 b=marker_S[nz].SB-1;
3190 num=b-a+1;
3191 ExecFilter(raw_in,raw_out,pos,num);
3192 a=marker_S[nz].SB;
3193 }
3194 b=marker_P[nz].QB;
3195 num=b-a+1;
3196 ExecFilter(raw_in,raw_out,pos,num);
3197 }
3198
3199 if (nz<flag.number) {
3200 a=marker_P[nz].QB+1;
3201 b=marker_P[nz].QE-1;
3202 num=b-a+1; // copy protected zone
3203 while((num--)>0) {
3204 raw_out[pos]=raw_in[pos]; pos++; }
3205 }
3206 }//for nz
3207 } // for ne/ng
3208 }//end DoFilter
3209
3210 void DoAdd(int32_t *raw_out, int32_t *raw_R, f_Res flag_R, int32_t *raw_B, f_BdR0 flag_B, Subtraction_Zone *marker_S, f_lead flag_L, lead *marker_A)
3211 //add BdR0 with the rhythm data for all leads
3212 {
3213 uint16_t pos_B;
3214 uint32_t pos_R, pos_out;
3215 uint16_t ns, a , b, num;
3216 uint8_t ne; //index of leads
3217
3218 pos_R=0; // by E.C. 19.02.2004 add reference beat to rhythm
3219 for(ne=0;ne<flag_L.number;ne++) // loop on lead
3220 for (ns=0;ns<flag_R.number;ns++) // loop on QRS
3221 if(marker_S[ns].beat_type==0) // only reference beat!
3222 {
3223 pos_B=(flag_B.fcM-1)-(marker_S[ns].fc-marker_S[ns].SB); //initial position in reference beat
3224 a= marker_S[ns].SB;
3225 b= marker_S[ns].SE;
3226 num=b-a+1; //number of samples to add
3227 pos_out= a-1;
3228 pos_out+= ne*flag_R.number_samples;
3229 pos_B+= ne*flag_B.number_samples;
3230 while((num--)>0)
3231 {
3232 raw_out[pos_out]+=raw_B[pos_B];
3233 pos_out++;
3234 pos_B++;
3235 pos_R++;
3236 }
3237 }//end for/if
3238 }//end DoAdd
3239
3240 void Opt_Filter(int32_t *raw_out, int32_t *raw_in, f_Res flag, f_lead flag_L, lead *marker_A, Protected_Area *marker_P)
3241 // by E.C. 25.02.2004
3242 // do an extra low-pass filter outside protected zones (marker_P)
3243 // in the range of the signal (marker_A) for each lead
3244 // this is simpler than DoFilter()
3245 {
3246 uint16_t a, b=0; //interval for filtering
3247 int16_t num;
3248 uint32_t pos;
3249 uint16_t nz;
3250 uint8_t ne; //lead index
3251
3252 pos=0;
3253 for(ne=0;ne<flag_L.number;ne++) // loop on leads
3254 {
3255 for (nz=0;nz<=flag.number;nz++) // loop on QRS of each lead
3256 {
3257 // establish the interval
3258 if(nz==0)
3259 {
3260 a=marker_A[ne].start;
3261 b=marker_P[nz].QB;
3262 num=b-a+1;
3263 ExecFilter(raw_in,raw_out,pos,num); // from start to first protected zone
3264 }
3265 else if (nz==flag.number)
3266 {
3267 a=marker_P[nz-1].QE;
3268 b=marker_A[ne].end;
3269 num=b-a+1;
3270 ExecFilter(raw_in,raw_out,pos,num); // from last protected zone to end
3271 }
3272 else
3273 {
3274 a=b+1;
3275 b=marker_P[nz].QB;
3276 num=b-a+1;
3277 ExecFilter(raw_in,raw_out,pos,num); // between two protected zones
3278 }
3279
3280 if (nz<flag.number) {
3281 a=marker_P[nz].QB+1;
3282 b=marker_P[nz].QE-1;
3283 num=b-a+1; // copy the protected zone as is
3284 while((num--)>0) {
3285 raw_out[pos]=raw_in[pos]; pos++; }
3286 }
3287 }//for nz
3288 } // for ne
3289 }//end Opt_Filter