Ejemplo n.º 1
0
/**
 * Creates a ImageDisk representation if there isn't an object already defined.
 **/
void ImageSourceSeries::process() {
    if (fileList_.empty()) return;

    std::string basePath{imageFileDirectory_.get()};
    long currentIndex = currentImageIndex_.get() - 1;
    if ((currentIndex < 0) || (currentIndex >= static_cast<long>(fileList_.size()))) {
        LogError("Invalid image index. Exceeded number of files.");
        return;
    }

    std::string currentFileName{basePath + "/" + fileList_[currentIndex]};
    imageFileName_.set(fileList_[currentIndex]);

    std::string fileExtension = filesystem::getFileExtension(currentFileName);
    auto factory = getNetwork()->getApplication()->getDataReaderFactory();
    if (auto reader = factory->getReaderForTypeAndExtension<Layer>(fileExtension)) {
        try {
            auto outLayer = reader->readData(currentFileName);
            // Call getRepresentation here to force read a ram representation.
            // Otherwise the default image size, i.e. 256x265, will be reported
            // until you do the conversion. Since the LayerDisk does not have any metadata.
            auto ram = outLayer->getRepresentation<LayerRAM>();
            // Hack needs to set format here since LayerDisk does not have a format.
            outLayer->setDataFormat(ram->getDataFormat());

            auto outImage = std::make_shared<Image>(outLayer);
            outImage->getRepresentation<ImageRAM>();

            outport_.setData(outImage);
        } catch (DataReaderException const& e) {
            util::log(e.getContext(),
                      "Could not load data: " + imageFileName_.get() + ", " + e.getMessage(),
                      LogLevel::Error);
        }
    } else {
        LogWarn("Could not find a data reader for file: " << currentFileName);
        // remove file from list
        fileList_.erase(fileList_.begin() + currentIndex);
        // adjust index property
        updateProperties();
    }
}
Ejemplo n.º 2
0
std::shared_ptr<Volume> MPVMVolumeReader::readData(std::string filePath) {
    if (!filesystem::fileExists(filePath)) {
        std::string newPath = filesystem::addBasePath(filePath);

        if (filesystem::fileExists(newPath)) {
            filePath = newPath;
        } else {
            throw DataReaderException("Error could not find input file: " + filePath, IvwContext);
        }
    }

    std::string fileDirectory = filesystem::getFileDirectory(filePath);

    // Read the mpvm file content

    std::string textLine;
    std::vector<std::string> files;
    {
        std::ifstream f(filePath.c_str());
        while (!f.eof()) {
            getline(f, textLine);
            textLine = trim(textLine);
            files.push_back(textLine);
        };
    }

    if (files.empty())
        throw DataReaderException("Error: No PVM files found in " + filePath, IvwContext);

    if (files.size() > 4)
        throw DataReaderException("Error: Maximum 4 pvm files are supported, file: " + filePath,
                                  IvwContext);

    // Read all pvm volumes
    std::vector<std::shared_ptr<Volume>> volumes;
    for (size_t i = 0; i < files.size(); i++) {
        auto newVol = PVMVolumeReader::readPVMData(fileDirectory + files[i]);
        if (newVol)
            volumes.push_back(newVol);
        else
            LogWarn("Could not load " << fileDirectory << files[i]);
    }

    if (volumes.empty())
        throw DataReaderException("No PVM volumes could be read from file: " + filePath,
                                  IvwContext);

    if (volumes.size() == 1) {
        printPVMMeta(*volumes[0], fileDirectory + files[0]);
        return volumes[0];
    }

    // Make sure dimension and format match
    const DataFormatBase* format = volumes[0]->getDataFormat();
    size3_t mdim = volumes[0]->getDimensions();
    for (size_t i = 1; i < volumes.size(); i++) {
        if (format != volumes[i]->getDataFormat() || mdim != volumes[i]->getDimensions()) {
            LogWarn("PVM volumes did not have the same format or dimensions, using first volume.");
            printPVMMeta(*volumes[0], fileDirectory + files[0]);
            return volumes[0];
        }
    }

    // Create new format
    const DataFormatBase* mformat =
        DataFormatBase::get(format->getNumericType(), volumes.size(), format->getSize() * 8);

    // Create new volume
    auto volume = std::make_shared<Volume>();
    glm::mat3 basis = volumes[0]->getBasis();
    volume->setBasis(basis);
    volume->setOffset(-0.5f * (basis[0] + basis[1] + basis[2]));
    volume->setDimensions(mdim);
    volume->dataMap_.initWithFormat(mformat);
    volume->setDataFormat(mformat);
    volume->copyMetaDataFrom(*volumes[0]);

    // Merge descriptions but ignore the rest
    if (auto metaData = volume->getMetaData<StringMetaData>("description")) {
        std::string descStr = metaData->get();
        for (size_t i = 1; i < volumes.size(); i++) {
            metaData = volumes[0]->getMetaData<StringMetaData>("description");
            if (metaData) descStr = descStr + ", " + metaData->get();
        }
        volume->setMetaData<StringMetaData>("description", descStr);
    }

    // Create RAM volume
    auto mvolRAM = createVolumeRAM(mdim, mformat);
    unsigned char* dataPtr = static_cast<unsigned char*>(mvolRAM->getData());

    std::vector<const unsigned char*> volumesDataPtr;
    for (size_t i = 0; i < volumes.size(); i++) {
        volumesDataPtr.push_back(static_cast<const unsigned char*>(
            volumes[i]->getRepresentation<VolumeRAM>()->getData()));
    }

    // Copy the data from the other volumes to the new multichannel volume
    size_t mbytes = mformat->getSize();
    size_t bytes = format->getSize();
    size_t dims = mdim.x * mdim.y * mdim.z;
    size_t vsize = volumesDataPtr.size();
    for (size_t i = 0; i < dims; i++) {
        for (size_t j = 0; j < vsize; j++) {
            for (size_t b = 0; b < bytes; b++) {
                dataPtr[i * mbytes + (j * bytes) + b] = volumesDataPtr[j][i * bytes + b];
            }
        }
    }

    volume->addRepresentation(mvolRAM);
    printPVMMeta(*volume, filePath);
    return volume;
}