| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#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 |
|
| 80 |
return floor(ldexp(beu32p(val)-2440588+719529.5, 32)); |
| 81 |
} |
| 82 |
|
| 83 |
int sopen_fiff_read(HDRTYPE* hdr) { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
size_t count = hdr->HeadLen; |
| 91 |
#if defined(_WIN32) || !defined(_SYS_STAT_H) || defined(ZLIB_H) |
| 92 |
|
| 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 |
|
| 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: |
| 146 |
case 108: |
| 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: |
| 180 |
break; |
| 181 |
case 209: |
| 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 |
|
| 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 |
|
| 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 |
|
| 238 |
hdr->EVENT.N = 0; |
| 239 |
|
| 240 |
|
| 241 |
|
| 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 |
|