00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <QFile>
00026 #include <QIODevice>
00027 #include <QDataStream>
00028 #include <QVector>
00029 #include <QPoint>
00030 #include <QtDebug>
00031
00032 #include <map>
00033 #include <cmath>
00034 #include <iostream>
00035
00036 #include "mferreader.h"
00037 #include "datahandler.h"
00038
00039 int power(int, int);
00040
00041 MferReader::MferReader(const QString &filename)
00042 {
00043 this->filename = filename;
00044 byteorder = QDataStream::BigEndian;
00045 noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
00046 samplingRateUnit = Frequency_hz;
00047 samplingResolutionUnit = Volt;
00048 waveformType = UNDEFINED;
00049
00050 initParsetable();
00051 valid = false;
00052 }
00053
00054 bool MferReader::readFile(const QString &filename)
00055 {
00056 this->filename = filename;
00057 QFile file(filename);
00058 file.open(QIODevice::ReadOnly);
00059 QDataStream in(&file);
00060
00061 parseBuffer(in);
00062 parseBlocks();
00063 valid = true;
00064
00065 return true;
00066 }
00067
00068 void MferReader::parseBlocks()
00069 {
00070 mferFrame.dataBlockLength = 0;
00071 mferFrame.numberOfChannels = 0;
00072 mferFrame.numberOfSequences = 0;
00073
00074 parseFuncMap_cit cit;
00075
00076 for (int i = 0; i < tagsVector.size(); i++)
00077 {
00078 cit = parseFunctions.find(tagsVector[i].type);
00079 if (cit != parseFunctions.end())
00080 {
00081 pt2mem p = cit->second;
00082 (this->*p)(tagsVector[i].value, tagsVector[i].length);
00083 }
00084
00085 if (tagsVector[i].type == MWF_WAV)
00086 {
00087 convertByteArray(tagsVector[i].length);
00088 }
00089 }
00090 }
00091
00092 void MferReader::convertByteArray(int length)
00093 {
00094 if (length > 0)
00095 {
00096 int l = length / sizeof(mfer_data_block);
00097 int i = 0;
00098 mferFrame.block = new mfer_data_block[l];
00099 QDataStream in(baTemp);
00100 in.setByteOrder(byteorder);
00101 while (!in.atEnd())
00102 {
00103 if (i >= l)
00104 {
00105 qDebug() << "overflow in convertByteArray";
00106 break;
00107 }
00108 in >> mferFrame.block[i++];
00109 }
00110 }
00111 }
00112
00113 void MferReader::parseBuffer(QDataStream &in)
00114 {
00115 quint8 d_byte;
00116
00117
00118 while (!in.atEnd())
00119 {
00120 mfer_tags mt;
00121
00122 mt.offset = in.device()->pos();
00123
00124 in >> d_byte;
00125 switch (d_byte)
00126 {
00127
00128 case MWF_IVL:
00129 case MWF_SEN:
00130
00131
00132 case MWF_BLK:
00133 case MWF_CHN:
00134 case MWF_SEQ:
00135
00136
00137 case MWF_WFM:
00138 case MWF_LDN:
00139
00140
00141 case MWF_DTP:
00142 case MWF_OFF:
00143 case MWF_NUL:
00144 case MWF_CMP:
00145
00146
00147 case MWF_BLE:
00148 case MWF_PNT:
00149 case MWF_ZRO:
00150
00151
00152 case MWF_PRE:
00153 case MWF_MAN:
00154 case MWF_EVT:
00155 case MWF_INF:
00156 case MWF_CND:
00157 case MWF_NTE:
00158 case MWF_VER:
00159 case MWF_TXC:
00160 case MWF_FLT:
00161 case MWF_IPD:
00162 mt.type = MFER_TAGS(d_byte);
00163 break;
00164
00165 case MWF_ATT:
00166 mt.type = MFER_TAGS(d_byte);
00167
00168 in >> d_byte;
00169 break;
00170
00171
00172 case MWF_WAV:
00173 mt.type = MFER_TAGS(d_byte);
00174 break;
00175
00176 default:
00177
00178 mt.type = MWF_INVALID;
00179 qDebug() << "Invalid tag: " << (quint16)d_byte;
00180 break;
00181 }
00182
00183 in >> d_byte;
00184
00185 quint32 length = 0;
00186 if (d_byte & (1<<7))
00187 {
00188 int d_numOctets = d_byte & 0x7f;
00189
00190 d_numOctets = qMin(4, d_numOctets);
00191 while (d_numOctets--)
00192 {
00193 length <<= 8;
00194 in >> d_byte;
00195 length |= d_byte;
00196 }
00197 mt.length = length;
00198 }
00199 else
00200 {
00201 mt.length = d_byte;
00202 }
00203
00204 if (mt.type == MWF_WAV)
00205 {
00206 baTemp = in.device()->read(mt.length);
00207 mt.value = NULL;
00208
00209 }
00210 else
00211 {
00212 mt.value = new char[mt.length + 1];
00213 int bytesRead;
00214
00215 if ((bytesRead = in.readRawData(mt.value, mt.length)) < 0
00216 || (unsigned int)bytesRead != mt.length)
00217 {
00218 qDebug() << "an error occurred readRawData";
00219 mt.value[bytesRead] = '\0';
00220 }
00221 mt.value[mt.length] = '\0';
00222 }
00223 tagsVector.push_back(mt);
00224 }
00225 }
00226
00227 int MferReader::fillData(QVector<QVector<double> > &pDest,
00228 int screennumber,
00229 int channel)
00230 {
00231 int rawLength = mferFrame.dataBlockLength * mferFrame.numberOfChannels;
00232 rawLength *= mferFrame.numberOfSequences;
00233
00234 int exp = qAbs(samplingRateExponent);
00235 int res = power(10, exp);
00236
00237 int samplesprSecond = res / samplingRateMantissa;
00238
00239 int samplesprLine = DataHandler::secondsprLine * samplesprSecond;
00240
00241 int samplesprScreen = DataHandler::numberofLines * samplesprLine;
00242
00243 int numberOfFullScreens = (rawLength / samplesprScreen);
00244
00245 int numberOfFullLines = (rawLength / samplesprLine);
00246
00247 int samplesOnLastLine = rawLength % samplesprLine;
00248
00249 int linesOnLastScreen = numberOfFullLines % DataHandler::numberofLines;
00250
00251 int numberOfScreens = 0;
00252 int screenOffset = 0;
00253
00254 screennumber = qBound(0, screennumber, numberOfFullScreens);
00255
00256 if (numberOfFullScreens != 0)
00257 {
00258 numberOfScreens = numberOfFullScreens;
00259 screenOffset = screennumber * samplesprScreen;
00260 }
00261 numberOfScreens++;
00262
00263 int linesToFill = 0;
00264 bool notFullScreens = false;
00265 if (numberOfScreens - 1 == screennumber)
00266 {
00267 linesToFill = linesOnLastScreen;
00268 notFullScreens = true;
00269 }
00270 else
00271 {
00272 linesToFill = DataHandler::numberofLines;
00273 }
00274
00275 int i, j;
00276 double sampres = (samplingResolutionMantissa *
00277 std::pow(10.0, (double)samplingResolutionExponent));
00278 int idx = 0;
00279 for (i = 0; i < linesToFill; i++)
00280 {
00281 for (j = 0; j < samplesprLine; j++)
00282 {
00283 idx = ((i * samplesprLine) + screenOffset) + j;
00284 pDest[i][j] = mferFrame.block[idx] * (sampres * 1000);
00285 }
00286 }
00287 if (notFullScreens)
00288 {
00289 for (j = 0; j < samplesOnLastLine; j++)
00290 {
00291 idx = ((i * samplesprLine) + screenOffset) + j;
00292 pDest[i][j] = mferFrame.block[idx] * (sampres * 1000);
00293 }
00294 for (; j < samplesprLine; j++)
00295 {
00296 pDest[i][j] = 65535.0;
00297 }
00298 i++;
00299 for (; i < DataHandler::numberofLines; i++)
00300 {
00301 for (j = 0; j < samplesprLine; j++)
00302 {
00303 pDest[i][j] = 65535.0;
00304 }
00305 }
00306 }
00307
00308 return numberOfScreens;
00309 }
00310 void MferReader::initParsetable(void)
00311 {
00312
00313 parseFunctions[MWF_IVL] = &MferReader::parse_MWF_IVL;
00314 parseFunctions[MWF_SEN] = &MferReader::parse_MWF_SEN;
00315
00316
00317 parseFunctions[MWF_BLK] = &MferReader::parse_MWF_BLK;
00318 parseFunctions[MWF_CHN] = &MferReader::parse_MWF_CHN;
00319 parseFunctions[MWF_SEQ] = &MferReader::parse_MWF_SEQ;
00320
00321
00322 parseFunctions[MWF_WFM] = &MferReader::parse_MWF_WFM;
00323 parseFunctions[MWF_LDN] = &MferReader::parse_MWF_LDN;
00324
00325
00326 parseFunctions[MWF_BLE] = &MferReader::parse_MWF_BLE;
00327 }
00328
00329 void MferReader::parse_MWF_IVL(char *value, int length)
00330 {
00331 samplingRateUnit = SamplingRateUnit(value[0]);
00332 samplingRateExponent = value[1];
00333
00334 int v = 0;
00335
00336 if (noswap)
00337 {
00338 char *p = (char *)(&v);
00339 for (int i = 2; i < length; i++)
00340 {
00341 *p++ = value[i];
00342 }
00343 }
00344 else
00345 {
00346 register uchar *p = (uchar *)(&v);
00347 while (length-- > 2)
00348 {
00349 *p++ = value[length];
00350 }
00351 }
00352 samplingRateMantissa = v;
00353 }
00354 void MferReader::parse_MWF_SEN(char *value, int length)
00355 {
00356 samplingResolutionUnit = SamplingResolutionUnit(value[0]);
00357 samplingResolutionExponent = value[1];
00358
00359 int v = 0;
00360
00361 if (noswap)
00362 {
00363 char *p = (char *)(&v);
00364 for (int i = 2; i < length; i++)
00365 {
00366 *p++ = value[i];
00367 }
00368 }
00369 else
00370 {
00371 register uchar *p = (uchar *)(&v);
00372 while (length-- > 2)
00373 {
00374 *p++ = value[length];
00375 }
00376 }
00377 samplingResolutionMantissa = v;
00378 }
00379
00380 void MferReader::parse_MWF_BLK(char *value, int length)
00381 {
00382 quint32 v = 0;
00383
00384 if (noswap)
00385 {
00386 char *p = (char *)(&v);
00387 for (int i = 0; i < length; i++)
00388 {
00389 *p++ = value[i];
00390 }
00391 }
00392 else
00393 {
00394 register uchar *p = (uchar *)(&v);
00395 while (length--)
00396 {
00397 *p++ = value[length];
00398 }
00399 }
00400 mferFrame.dataBlockLength = v;
00401 }
00402 void MferReader::parse_MWF_CHN(char *value, int length)
00403 {
00404 quint32 v = 0;
00405
00406 if (noswap)
00407 {
00408 char *p = (char *)(&v);
00409 for (int i = 0; i < length; i++)
00410 {
00411 *p++ = value[i];
00412 }
00413 }
00414 else
00415 {
00416 register uchar *p = (uchar *)(&v);
00417 while (length--)
00418 {
00419 *p++ = value[length];
00420 }
00421 }
00422 mferFrame.numberOfChannels = v;
00423 }
00424 void MferReader::parse_MWF_SEQ(char *value, int length)
00425 {
00426 quint32 v = 0;
00427
00428 if (noswap)
00429 {
00430 char *p = (char *)(&v);
00431 for (int i = 0; i < length; i++)
00432 {
00433 *p++ = value[i];
00434 }
00435 }
00436 else
00437 {
00438 register uchar *p = (uchar *)(&v);
00439 while (length--)
00440 {
00441 *p++ = value[length];
00442 }
00443 }
00444 mferFrame.numberOfSequences = v;
00445 }
00446
00447 void MferReader::parse_MWF_WFM(char *value, int length)
00448 {
00449 if (length > 2)
00450 {
00451
00452 }
00453 else
00454 {
00455 quint16 v = 0;
00456
00457 if (noswap)
00458 {
00459 char *p = (char *)(&v);
00460 for (int i = 0; i < length; i++)
00461 {
00462 *p++ = value[i];
00463 }
00464 }
00465 else
00466 {
00467 register uchar *p = (uchar *)(&v);
00468 while (length--)
00469 {
00470 *p++ = value[length];
00471 }
00472 }
00473 waveformType = WaveformType(v);
00474 }
00475 }
00476
00477 void MferReader::parse_MWF_LDN(char *value, int length)
00478 {
00479 if (length > 2)
00480 {
00481
00482 }
00483 else if (length == 2)
00484 {
00485
00486 }
00487 else
00488 {
00489 quint16 v = 0;
00490
00491 if (noswap)
00492 {
00493 char *p = (char *)(&v);
00494 for (int i = 0; i < length; i++)
00495 {
00496 *p++ = value[i];
00497 }
00498 }
00499 else
00500 {
00501 register uchar *p = (uchar *)(&v);
00502 while (length--)
00503 {
00504 *p++ = value[length];
00505 }
00506 }
00507
00508 }
00509 }
00510
00511 void MferReader::parse_MWF_BLE(char *value, int length)
00512 {
00513 if (*value == 0x00)
00514 {
00515 byteorder = QDataStream::BigEndian;
00516 }
00517 else if (*value == 0x01)
00518 {
00519 byteorder = QDataStream::LittleEndian;
00520 }
00521 if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
00522 {
00523 noswap = (byteorder == QDataStream::BigEndian);
00524 }
00525 else
00526 {
00527 noswap = (byteorder == QDataStream::LittleEndian);
00528 }
00529 }
00530 int MferReader::samplesprSecond() const
00531 {
00532 int exp = qAbs(samplingRateExponent);
00533 int res = power(10, exp);
00534 return (res / samplingRateMantissa);
00535 }
00536
00537 int power (int base, int exp)
00538 {
00539 if(exp >= 1)
00540 return base * (power(base,exp - 1));
00541
00542 else
00543 return 1;
00544 }