Ejemplo n.º 1
0
void PrefWaveformView::loadChannelOrder(){
    QSettings settings;
    const QString url = QFileDialog::getOpenFileName(this, tr("Load File..."), settings.value("CurrentDirectory").toString(),
                                               tr("*|All files") );

    if(url.isEmpty())
      return;
    QMap<int,int> positions;

    QDir CurrentDir;
    settings.setValue("CurrentDirectory", CurrentDir.absoluteFilePath(url));

    QString tmpChannelFile = url;
    if(!QFile(tmpChannelFile).exists()){
        QMessageBox::critical (this,tr("Error !"),
                               tr("The selected file could not be downloaded !")
                               );
        return;
    }

    QFile channelFile(tmpChannelFile);
    if(!channelFile.open(QIODevice::ReadOnly)){
        QMessageBox::critical (this,tr("Error !"),
                               tr("The selected file could not be opened !")
                               );
        return;
    }

    QTextStream positionStream(&channelFile);
    QString line;
    int channel = 0;
    for(line = positionStream.readLine(); !line.isNull();line = positionStream.readLine()){
        bool ok;
        int position = line.toInt(&ok,10);
        if(!ok){
            QMessageBox::critical (this,tr("Error !"),
                                   tr("The selected file does not have the correct format (list of channels number),\n"
                                      "it can not be used."));

            channelFile.close();

            return;
        }
        //The channels are counted from 0 to nbChannels - 1.
        positions.insert(channel,position);
        channel++;
    }

    channelFile.close();

    if(nbChannels != static_cast<int>(positions.count())){
        QMessageBox::critical (this,tr("Error !"),
                               tr("The number of channels in the selected file does not correspond to the number of channels of the current file !\n"
                                  "This file can not be used for the current document.")
                               );
        return;
    }

    //Update the list with the positions get from the file.
    channelList->clear();
    for(int i = 0; i< nbChannels; ++i){
        channelList->insertItem(positions[i],QString::number(i + 1));
        channelPositions[i] = positions[i];
    }

    //signal to the dialog that channel positions have changed so it can update the Apply button.
    emit positionsChanged();
}
Ejemplo n.º 2
0
bool MaskChan::createMaskChanForLabel(int label, const QString& maskFullPath, const QString& channelFullPath, QReadWriteLock* mutex)
{
  QList<long> pairCountList; // x=0, y=1, z=2
  QList<MaskRay*> xRayList;
  QList<MaskRay*> yRayList;
  QList<MaskRay*> zRayList;

  QList<long> x0List;
  QList<long> x1List;
  QList<long> y0List;
  QList<long> y1List;
  QList<long> z0List;
  QList<long> z1List;

  for (int direction=0;direction<3;direction++) {

    QList<MaskRay*> * rayList;
    long pairCount=0L;
    long countCheck=0L;

    if (direction==0) {
      rayList=&xRayList;
    } else if (direction==1) {
      rayList=&yRayList;
    } else {
      rayList=&zRayList;
    }

    long x0,x1,y0,y1,z0,z1;

    axisTracer(direction, label, rayList, pairCount, countCheck, x0, x1, y0, y1, z0, z1);

    x0List.append(x0);
    x1List.append(x1);
    y0List.append(y0);
    y1List.append(y1);
    z0List.append(z0);
    z1List.append(z1);

    pairCountList.append(pairCount);
    pairCount=0L;

    if (countCheck!=labelIndex[label]) {
      qDebug() << "Count check failed : direction=" << direction << " countCheck=" << countCheck << " labelIndex=" << labelIndex[label];
      return false;
    } else {
      //qDebug() << "Direction " << direction << " passed voxel count check";
    }

  }

  // We have computed the ray set for each axis, we will save the one with the
  // smallest size.

  for (int s=0;s<pairCountList.size();s++) {
    //qDebug() << "pairCount " << s << " : " << pairCountList[s];
  }

  unsigned char smallestSize=0;
  if (pairCountList[1]<pairCountList[smallestSize]) {
    smallestSize=1;
  }
  if (pairCountList[2]<pairCountList[smallestSize]) {
    smallestSize=2;
  }

  long x0=x0List[smallestSize];
  long x1=x1List[smallestSize];
  long y0=y0List[smallestSize];
  long y1=y1List[smallestSize];
  long z0=z0List[smallestSize];
  long z1=z1List[smallestSize];

  long totalVoxels=labelIndex[label];

  // allocate space for data
  long unitsNeeded=totalVoxels*cdim;
  void* data=0L;
  if (sourceImage->getDatatype()==V3D_UINT8) {
    data=(void*) new v3d_uint8[unitsNeeded];
  } else {
    data=(void*) new v3d_uint16[unitsNeeded];
  }

  // re-run axis-tracer and populate channel intensity data
  QList<MaskRay*> * rayList;
  long pairCount=0L;
  long countCheck=0L;

  if (smallestSize==0) {
    rayList=&xRayList;
  } else if (smallestSize==1) {
    rayList=&yRayList;
  } else {
    rayList=&zRayList;
  }

  //qDebug() << "calling axisTracer 2nd pass data=" << data;

  rayList->clear(); // will get re-populated

  axisTracer(smallestSize, label, rayList, pairCount, countCheck, x0, x1, y0, y1, z0, z1, data, totalVoxels);

  if (countCheck!=totalVoxels) {
    qDebug() << "In second pass of axisTracer, countCheck=" << countCheck << " totalVoxels=" << totalVoxels;
    exit(1);
  }

  //qDebug() << "Using axis " << smallestSize;

  //qDebug() << "Writing to file and locking with QMutex" << maskFullPath;

  if (mutex!=0L) {
    QWriteLocker locker(mutex); // Will be deleted from stack
  }

  QFile maskFile(maskFullPath);


  ////////////////////////////////////////////////////////////////////////

  //    FILE* fid = fopen(maskFullPath.toAscii().data(), "wb");
  //    if (!fid) {
  //        qDebug() << "Could not open file " << maskFullPath << " to write";
  //        return false;
  //    }

  maskFile.open( QIODevice::WriteOnly );
  QDataStream maskOut(&maskFile);

  //qDebug() << "Writing xdim=" << xdim << " ydim=" << ydim << " zdim=" << zdim;

  maskOut.writeRawData( (const char*) &xdim, sizeof(long));
  maskOut.writeRawData( (const char *)&ydim, sizeof(long));
  maskOut.writeRawData( (const char *)&zdim, sizeof(long));

  float xMicrons=0.0;
  float yMicrons=0.0;
  float zMicrons=0.0;

  maskOut.writeRawData( (const char *)&xMicrons, sizeof(float));
  maskOut.writeRawData( (const char *)&yMicrons, sizeof(float));
  maskOut.writeRawData( (const char *)&zMicrons, sizeof(float));

  maskOut.writeRawData( (const char *)&x0, sizeof(long));
  maskOut.writeRawData( (const char *)&x1, sizeof(long));
  maskOut.writeRawData( (const char *)&y0, sizeof(long));
  maskOut.writeRawData( (const char *)&y1, sizeof(long));
  maskOut.writeRawData( (const char *)&z0, sizeof(long));
  maskOut.writeRawData( (const char *)&z1, sizeof(long));

  maskOut.writeRawData( (const char *)&totalVoxels, sizeof(long));
  maskOut.writeRawData( (const char *)&smallestSize, sizeof(unsigned char));

  if (smallestSize==0) {
    writeMaskList(maskOut, xRayList);
  } else if (smallestSize==1) {
    writeMaskList(maskOut, yRayList);
  } else {
    writeMaskList(maskOut, zRayList);
  }
  //    fflush(fid);
  //    fclose(fid);
  //    fid=0L;
  maskFile.close();

  qDebug() << "Wrote " << maskFullPath;

  // Write out the channel file

  // First, clear the previous masks
  for (int d=0;d<3;d++) {
    QList<MaskRay*> * rayList;
    if (d==0) {
      rayList=&xRayList;
    } else if (d==1) {
      rayList=&yRayList;
    } else {
      rayList=&zRayList;
    }
    for (int i=0;i<rayList->size();i++) {
      MaskRay* ray = (*rayList)[i];
      delete ray;
    }
    rayList->clear();
  }

  QFile channelFile(channelFullPath);
  channelFile.open( QIODevice::WriteOnly );

  //qDebug() << "Writing to file " << channelFullPath;

  //    fid = fopen(channelFullPath.toAscii().data(), "wb");
  //    if (!fid) {
  //        qDebug() << "Could not open file " << channelFullPath << " to write";
  //        return false;
  //    }

  QDataStream chanOut(&channelFile);

  chanOut.writeRawData( (const char *)&totalVoxels, sizeof(long));
  unsigned char numChannels=sourceImage->getCDim();
  chanOut.writeRawData( (const char *)&numChannels, sizeof(unsigned char));

  unsigned char recRed=0;
  unsigned char recGreen=1;
  unsigned char recBlue=2;

  chanOut.writeRawData( (const char *)&recRed, sizeof(unsigned char));
  chanOut.writeRawData( (const char *)&recGreen, sizeof(unsigned char));
  chanOut.writeRawData( (const char *)&recBlue, sizeof(unsigned char));

  unsigned char datatype=1; // 8-bit
  if (sourceImage->getDatatype()==V3D_UINT16) {
    datatype=2; // 16-bit
  }
  chanOut.writeRawData( (const char *)&datatype, sizeof(unsigned char));


  chanOut.writeRawData( (const char *)data, totalVoxels*cdim*datatype);
  //    fflush(fid);
  //    fclose(fid);
  channelFile.close();

  qDebug() << "Wrote " << channelFullPath;
  return true;
}