Debian Codegraph @

biosig4c++/t210/sopen_fiff.c

raw symbols SCIP
1 /*
2 sandbox is used for development and under constraction work
3 The functions here are either under construction or experimental.
4 The functions will be either fixed, then they are moved to another place;
5 or the functions are discarded. Do not rely on the interface in this function
6
7 Copyright (C) 2025 Alois Schloegl <[email protected]>
8 This file is part of the "BioSig for C/C++" repository
9 (biosig4c++) at http://biosig.sf.net/
10
11 BioSig is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 3
14 of the License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include <assert.h>
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "../biosig.h"
31
32 #define min(a,b) (((a) < (b)) ? (a) : (b))
33 #define max(a,b) (((a) > (b)) ? (a) : (b))
34
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40
41 uint16_t fiff_physdimcode(int32_t unit) {
42 switch (unit) {
43 case 0: return 0;
44 case 1: return PhysDimCode("m");
45 case 2: return PhysDimCode("kg");
46 case 3: return PhysDimCode("s");
47 case 4: return PhysDimCode("A");
48 case 5: return PhysDimCode("K");
49 case 6: return PhysDimCode("mol");
50 case 7: return PhysDimCode("rad");
51 case 8: return PhysDimCode("sr");
52 case 9: return PhysDimCode("cd");
53
54 case 101: return PhysDimCode("Hz");
55 case 102: return PhysDimCode("N");
56 case 103: return PhysDimCode("Pa");
57 case 104: return PhysDimCode("J");
58 case 105: return PhysDimCode("W");
59 case 106: return PhysDimCode("C");
60 case 107: return PhysDimCode("V");
61 case 108: return PhysDimCode("F");
62 case 109: return PhysDimCode("Ohm");
63 case 110: return PhysDimCode("S");
64
65 case 111: return PhysDimCode("Vs");
66 case 112: return PhysDimCode("T");
67 case 113: return PhysDimCode("H");
68 case 114: return PhysDimCode("°C");
69 case 115: return PhysDimCode("lm");
70 case 116: return PhysDimCode("Lx");
71
72 case 201: return PhysDimCode("T/m");
73 case 202: return PhysDimCode("Am");
74 }
75 return 0;
76 }
77
78 gdftime_t julian2gdftype(void* val) {
79 // https://github.com/mne-tools/mne-python/blob/main/mne/utils/numerics.py
80 return floor(ldexp(beu32p(val)-2440588+719529.5, 32));
81 }
82
83 int sopen_fiff_read(HDRTYPE* hdr) {
84 /* TODO: implement FIFF support
85 define all fields in hdr->....
86 currently only the first hdr->HeadLen bytes are stored in
87 hdr->AS.Header, all other fields still need to be defined.
88 */
89
90 size_t count = hdr->HeadLen;
91 #if defined(_WIN32) || !defined(_SYS_STAT_H) || defined(ZLIB_H)
92 // stat(...) can not be used in windows because including <sys/stat.h> causes a namespace conflict with sopen
93 while (!feof(hdr->FILE.FID)) {
94 void *ptr = realloc(hdr->AS.Header, count*2);
95 if (ptr==NULL) {
96 biosigERROR(hdr, B4C_MEMORY_ALLOCATION_FAILED, "FIFF: memory allocation");
97 return -1;
98 }
99 hdr->AS.Header = (uint8_t*)ptr;
100 count += ifread(hdr->AS.Header+count, 1, count, hdr);
101 }
102 fclose(hdr->FILE.FID);
103 hdr->HeadLen = count;
104 hdr->AS.Header = (uint8_t*)realloc(hdr->AS.Header, count+1);
105 #else
106 struct stat FileBuf;
107 stat(hdr->FileName,&FileBuf);
108 void *ptr = realloc(hdr->AS.Header, FileBuf.st_size);
109 if (ptr==NULL) {
110 biosigERROR(hdr, B4C_MEMORY_ALLOCATION_FAILED, "FIFF: memory allocation");
111 return -1;
112 }
113 hdr->AS.Header = (uint8_t*)ptr;
114 if (!feof(hdr->FILE.FID) )
115 count += ifread(hdr->AS.Header+count, 1, FileBuf.st_size-count, hdr);
116
117 fclose(hdr->FILE.FID);
118 hdr->HeadLen = count;
119 if (count != FileBuf.st_size)
120 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "FIFF: read error");
121 #endif
122
123 if (VERBOSE_LEVEL>7) fprintf(stdout,"# %s line %d: %s(....) \n", __FILE__, __LINE__, __func__);
124
125 char* firstname=NULL;
126 char* middlename=NULL;
127 char* surname=NULL;
128 int nn1=0, nn2=0, nn3=0;
129 float lowpass=0.0, highpass=0.0;
130 uint32_t pos = 0, NumTags=0;
131 int32_t* badchanlist=NULL;
132 int numbadchan=0;
133 while (1) {
134 // fifftag_t* ct = (fifftag_t*)(hdr->AS.Header+pos);
135 NumTags++;
136 int32_t kind = bei32p(hdr->AS.Header+pos);
137 int32_t type = bei32p(hdr->AS.Header+pos+4);
138 int32_t size = bei32p(hdr->AS.Header+pos+8);
139 int32_t next = bei32p(hdr->AS.Header+pos+12);
140 uint8_t *val = hdr->AS.Header+(pos+16);
141
142 if (VERBOSE_LEVEL>8) fprintf(stdout,"# FIFFTAG %d: %d, %08x, %d, %08x :\t%08x %9d %s \n", NumTags, kind, type, size, next, be32toh(*(uint32_t*)val), be32toh(*(uint32_t*)val), (char*)val);
143
144 switch (kind) {
145 case 107: // unused
146 case 108: // nop
147 break;
148
149 case 200: hdr->NS = beu32p(val);
150 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
151 break;
152 case 201: hdr->SampleRate = bef32p(val); break;
153 case 203: {
154 int32_t scanNo = bei32p(val);
155 int32_t logNo = bei32p(val+4);
156 int32_t chankind = bei32p(val+8);
157 float range = bef32p(val+12);
158 float cal = bef32p(val+16);
159 int32_t coil_type = bei32p(val+20);
160 float r1 = bef32p(val+24);
161 float r2 = bef32p(val+28);
162 float r3 = bef32p(val+32);
163
164 uint32_t unit = beu32p(val+72);
165 uint32_t unitm = beu32p(val+76);
166
167 CHANNEL_TYPE* hc = hdr->CHANNEL + scanNo;
168 hc->Cal = range * cal * pow(10.0, unitm);
169 hc->Off = 0.0;
170 hc->OnOff = 1;
171 hc->PhysDimCode = fiff_physdimcode(unit);
172 hc->SPR = hdr->SPR;
173 hc->GDFTYP = 3;
174 hc->LowPass = lowpass;
175 hc->HighPass = highpass;
176 break;
177 }
178 case 204: hdr->T0 = julian2gdftype(val); break;
179 case 208: // first_sample
180 break;
181 case 209: // last_sample
182 break;
183 case 219: lowpass = bef32p(val); break;
184 case 220: numbadchan = size/4;
185 badchanlist = (int32_t*)val;
186 break;
187
188 case 223: highpass = bef32p(val); break;
189 case 228: hdr->SPR = beu32p(val); break;
190
191
192 case 401: nn1=size; firstname=(char*)val; break;
193 case 402: nn2=size; middlename=(char*)val; break;
194 case 403: nn3=size; surname=(char*)val; break;
195 case 404: hdr->Patient.Birthday = julian2gdftype(val); break;
196 case 405: hdr->Patient.Sex = beu32p(val); break;
197 case 406: hdr->Patient.Handedness = beu32p(val); break;
198 case 407: hdr->Patient.Weight = lef32p(val); break;
199 case 408: hdr->Patient.Height = lef32p(val); break;
200 // case 409: break;
201 case 410: strncpy(hdr->Patient.Id, (char*)val, MAX_LENGTH_PID+1); break;
202
203 default:
204 if (VERBOSE_LEVEL>4) fprintf(stdout,"# FIFFTAG %d ignored: %d, %08x, %d, %08x :\t%08x %9d %s \n", NumTags, kind, type, size, next, be32toh(*(uint32_t*)val), be32toh(*(uint32_t*)val), (char*)val);
205
206 ;
207 }
208
209 //
210 if (next==0) pos += size+16;
211 else if (next == -1) break;
212 else pos = next;
213 }
214
215 if (!hdr->FLAG.ANONYMOUS) {
216 strncpy(hdr->Patient.Name, surname, min(nn3,MAX_LENGTH_NAME));
217 if (nn3 < MAX_LENGTH_NAME) {
218 hdr->Patient.Name[nn3] = 0x1f;
219 strncpy(hdr->Patient.Name+nn3+1, firstname, min(nn1,MAX_LENGTH_NAME-nn3-1));
220 }
221 if (nn3+nn1+1 < MAX_LENGTH_NAME) {
222 hdr->Patient.Name[nn3+1+nn1] = 0x1f;
223 strncpy(hdr->Patient.Name+nn3+nn1+2, middlename, min(nn2,MAX_LENGTH_NAME-nn3-2-nn1));
224 }
225 hdr->Patient.Name[min(nn1+nn2+nn3+2,MAX_LENGTH_NAME)]=0;
226 }
227 fflush(stdout);
228
229 /* define channel headers */
230 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
231 for (int k=0; k < numbadchan; k++)
232 hdr->CHANNEL[bei32p(badchanlist+k)].OnOff = 0;
233 for (int k = 0; k < hdr->NS; k++) {
234 CHANNEL_TYPE *hc = hdr->CHANNEL + k;
235 }
236
237 /* define event table */
238 hdr->EVENT.N = 0;
239 //reallocEventTable(hdr, 0);
240
241 /* report status header and return */
242 hdr2ascii(hdr,stdout,4);
243 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "FIFF support not completed");
244 return 0;
245 }
246
247
248
249 #ifdef __cplusplus
250 }
251 #endif
252