[Kst] extragear/graphics/kst/src/datasources/naddirect

Eli Fidler eli at staikos.net
Tue Apr 4 22:08:18 CEST 2006


SVN commit 526504 by fidler:

added skip support for readField (fixed skip)
don't show INDEX if NAD object is otherwise empty
standardize name to "NAD Connection"


 M  +3 -3      nadconnection.cpp  
 M  +1 -1      nadconnection.h  
 M  +91 -7     naddirect.cpp  
 M  +1 -0      naddirect.h  


--- trunk/extragear/graphics/kst/src/datasources/naddirect/nadconnection.cpp #526503:526504
@@ -413,8 +413,8 @@
 }
 
 
-int NADConnection::getData(const QString &field, double *buf, long startFrame, long stopFrame) {
-  QString postData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<GetData><DataSet><DataSetName>" + _datasetName + "</DataSetName></DataSet><Endian>LittleEndian</Endian><Fields><Field><FieldName>" + field + "</FieldName><Start type=\"frame\">" + QString::number(startFrame) + "</Start><Stop type=\"frame\">" + QString::number(stopFrame) + "</Stop></Field></Fields></GetData>";
+int NADConnection::getData(const QString &field, double *buf, long startFrame, long stopFrame, long skip) {
+  QString postData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<GetData><DataSet><DataSetName>" + _datasetName + "</DataSetName></DataSet><Endian>LittleEndian</Endian><Fields><Field><FieldName>" + field + "</FieldName><Start type=\"frame\">" + QString::number(startFrame) + "</Start><Stop type=\"frame\">" + QString::number(stopFrame) + "</Stop><Skip type=\"frame\">" + QString::number(skip) + "</Skip></Field></Fields></GetData>";
 
   // remove trailing null
   QByteArray data;
@@ -543,7 +543,7 @@
         if ((fieldStartFrame == INT_MAX) && (fieldStopFrame == INT_MAX)) {
           // no data
         } else {
-          long fieldChunkBytes = ((fieldStopFrame - fieldStartFrame) / (i.data()->skip + 1) + 1) * i.data()->samplesPerFrame * sizeof(double);
+          long fieldChunkBytes = ((fieldStopFrame - fieldStartFrame) / (i.data()->skip) + 1) * i.data()->samplesPerFrame * sizeof(double);
 //          kstdDebug() << "frame:" << fieldStartFrame << "-" << fieldStopFrame << ": fieldChunkBytes=" << fieldChunkBytes << ", curOutPos=" << curOutPos << endl;
           for (long j=0; j < fieldChunkBytes; j += sizeof(double)) {
 //            assert(curOutPos < n);
--- trunk/extragear/graphics/kst/src/datasources/naddirect/nadconnection.h #526503:526504
@@ -77,7 +77,7 @@
 
     QSize range(const QString &field) const;
     int samplesPerFrame(const QString &field) const;
-    int getData(const QString &field, double *buf, long startFrame, long stopFrame);
+    int getData(const QString &field, double *buf, long startFrame, long stopFrame, long skip=1);
 
   private:
     bool NADConnection::updateLastFieldsResponse();
--- trunk/extragear/graphics/kst/src/datasources/naddirect/naddirect.cpp #526503:526504
@@ -48,7 +48,9 @@
   kstdDebug() << "NAD::init()\n";
   if (_conn->isValid()) {
     _fieldList = _conn->getFields();
-    _fieldList.prepend("INDEX");
+    if (!_fieldList.empty()) {
+      _fieldList.prepend("INDEX");
+    }
   }
 
   return update() == KstObject::UPDATE;
@@ -75,7 +77,7 @@
 int NADDirectSource::readField(double *v, const QString& field, int s, int n) {
   kstdDebug() << "NAD::readField(" << field << ", s=" + QString::number(s) + ", n=" + QString::number(n) + ")" << endl;
 
-  if (field.lower() == "index") {
+  if (field == "INDEX") {
     if (n < 0) {
       v[0] = double(s);
       return 1;
@@ -122,8 +124,88 @@
 }
 
 
+/** Reads a field from the file.  Data is returned in the
+ *  double Array v[].  Will skip according to the parameter, but it may not
+ *  be implemented.  If it returns -9999, use the non-skip version instead. */
+// if n > 0, read 1 SAMPLE from each of n FRAMES starting at frame s skipping by 'skip' for field into buffer v
+// if n < 0, read 1 SAMPLE from frame s for field into buffer v
+int NADDirectSource::readField(double *v, const QString& field, int s, int n, int skip, int *lastFrameRead) {
+  kstdDebug() << "NAD::readField(" << field << ", s=" + QString::number(s) + ", n=" + QString::number(n) + ", skip=" + QString::number(skip) + ")" << endl;
+  if (lastFrameRead) *lastFrameRead = -1;
+
+  if (field == "INDEX") {
+    if (n < 0) {
+      v[0] = double(s);
+      if (lastFrameRead) *lastFrameRead = s;
+      return 1;
+    }
+    for (int i = 0; i < n; ++i) {
+      v[i] = double(s + i*skip);
+    }
+    if (lastFrameRead) *lastFrameRead = s + (n - 1) * skip;
+    return n > 0 ? n : 0;
+  }
+
+  if (!_valid || !_conn || !_conn->isValid()) {
+    kstdDebug() << "tried to read from an invalid NAD source" << endl;
+    kstdDebug() << "plugin is valid? " << _valid << endl;
+    return -1;
+  }
+
+  QSize sz = _conn->range(field);
+  long start = sz.width(), end = sz.height();
+  long count = end - start + 1;
+  int spf = _conn->samplesPerFrame(field);
+
+  if (s + start > end) {
+    kstdDebug() << "Nothing to read: (" << start << "," << end << ") " << s << endl;
+    return 0;
+  }
+
+  if (n < 0) { // reading less than 0 -> read only one sample!
+    double *temp = new double[spf];
+    _conn->getData(field, temp, start + s, start + s);
+    v[0] = temp[0];
+    delete[] temp;
+    if (lastFrameRead) *lastFrameRead = s;
+    return 1;
+  } else {
+    if (s + (n-1)*skip >= count) { // trying to read past the end
+      // n = ceil((count-s)/skip)
+      if ((count - s) % skip == 0) {
+        n = (count - s) / skip;
+      } else {
+        n = (count - s) / skip + 1;
+      }
+
+      n = count - s;
+    }
+
+    double *tmp = new double[n * spf];
+//    kstdDebug() << "NAD: calling getData()" << endl;
+    int rc = _conn->getData(field, tmp, start + s, start + s + (n - 1) * skip, skip); // get skipped frames
+    int framesRead = rc/spf;
+    //kstdDebug() << "readObject rc=" << rc << " from=" << start+s << " to=" << start + s + (n - 1) * skip << endl;
+
+    // extract first sample from each returned frame
+    int i = 0;
+    while (i < QMAX(n, framesRead)) {
+      v[i] = tmp[i * spf];
+      ++i;
+    }
+    delete[] tmp;
+
+//    kstdDebug() << "NAD::readField(" << field << ", s=" + QString::number(s) + ", n=" + QString::number(n) + ", skip=" + QString::number(skip) + ") = " << i << endl;
+    if (i > 0 && lastFrameRead) {
+      *lastFrameRead = s + (i - 1) * skip;
+    }
+    return i;
+  }
+}
+
+
 bool NADDirectSource::isValidField(const QString& field) const {
-  bool valid = field.lower() == "index" || _fieldList.contains(field);
+  bool valid = _fieldList.contains(field);
   kstdDebug() << "NAD::isValidField(" << field << ") = " << valid << endl;
   return valid;
 }
@@ -134,7 +216,7 @@
     return 0;
   }
 
-  if (field.lower() == "index") {
+  if (field == "INDEX") {
     kstdDebug() << "NAD::samplesPerFrame(" << field << ") = 1\n";
     return 1;
   }
@@ -207,7 +289,7 @@
   kstdDebug() << "in provides_naddirect" << endl;
 
   QStringList rc;
-  rc += "NAD";
+  rc += "NAD Connection";
   return rc;
 }
 
@@ -246,11 +328,13 @@
   }
 
   if (typeSuggestion) {
-    *typeSuggestion = "NAD";
+    *typeSuggestion = "NAD Connection";
   }
 
   QStringList rc = conn.getFields();
-  rc.prepend("INDEX");
+  if (!rc.empty()) {
+    rc.prepend("INDEX");
+  }
 
   return rc;
 }
--- trunk/extragear/graphics/kst/src/datasources/naddirect/naddirect.h #526503:526504
@@ -32,6 +32,7 @@
     KstObject::UpdateType update(int = -1);
 
     int readField(double *v, const QString &field, int s, int n);
+    int readField(double *v, const QString& field, int s, int n, int skip, int *lastFrameRead);
 
     bool isValidField(const QString &field) const;
 


More information about the Kst mailing list