void* dispatch(void* dst, const char* filePath, uvec2& dimensions, DataFormatId& formatId, const DataFormatBase* dataFormat, bool rescaleToDim) { using P = typename T::primitive; try { CImg<P> img(filePath); size_t components = static_cast<size_t>(img.spectrum()); if (rescaleToDim) { img.resize(dimensions.x, dimensions.y, -100, -100, 3); } else { dimensions = uvec2(img.width(), img.height()); } auto loadedDataFormat = DataFormatBase::get(dataFormat->getNumericType(), components, sizeof(P) * 8); if (loadedDataFormat) { formatId = loadedDataFormat->getId(); } else { throw DataReaderException( "CImgLoadLayerDispatcher, could not find proper data type", IvwContext); } // Image is up-side-down img.mirror('y'); return CImgToVoidConvert<P>::convert(dst, &img); } catch (CImgIOException& e) { throw DataReaderException(std::string(e.what()), IvwContext); } }
std::shared_ptr<DataRepresentation> CImgLayerRAMLoader::createRepresentation() const { void* data = nullptr; uvec2 dimensions = layerDisk_->getDimensions(); DataFormatId formatId = DataFormatId::NotSpecialized; std::string filePath = layerDisk_->getSourceFile(); 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); } } if (dimensions != uvec2(0)) { // Load and rescale to input dimensions data = CImgUtils::loadLayerData(nullptr, filePath, dimensions, formatId, true); } else { // Load to original dimensions data = CImgUtils::loadLayerData(nullptr, filePath, dimensions, formatId, false); layerDisk_->setDimensions(dimensions); } layerDisk_->updateDataFormat(DataFormatBase::get(formatId)); return layerDisk_->getDataFormat()->dispatch(*this, data); }
void CImgLayerRAMLoader::updateRepresentation(std::shared_ptr<DataRepresentation> dest) const { auto layerDst = std::static_pointer_cast<LayerRAM>(dest); if (layerDisk_->getDimensions() != layerDst->getDimensions()) { layerDst->setDimensions(layerDisk_->getDimensions()); } uvec2 dimensions = layerDisk_->getDimensions(); DataFormatId formatId = DataFormatId::NotSpecialized; std::string filePath = layerDisk_->getSourceFile(); 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); } } if (dimensions != uvec2(0)) { // Load and rescale to input dimensions CImgUtils::loadLayerData(layerDst->getData(), filePath, dimensions, formatId, true); } else { // Load to original dimensions CImgUtils::loadLayerData(layerDst->getData(), filePath, dimensions, formatId, false); layerDisk_->setDimensions(dimensions); } layerDisk_->updateDataFormat(DataFormatBase::get(formatId)); }
std::shared_ptr<Layer> CImgLayerReader::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); } } auto layer = std::make_shared<Layer>(); auto layerDisk = std::make_shared<LayerDisk>(filePath); layerDisk->setLoader(new CImgLayerRAMLoader(layerDisk.get())); layer->addRepresentation(layerDisk); return layer; }
std::shared_ptr<Volume> CImgVolumeReader::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); } } auto volume = std::make_shared<Volume>(); auto volumeDisk = std::make_shared<VolumeDisk>(filePath); volumeDisk->setLoader(new CImgVolumeRAMLoader(volumeDisk.get())); volume->addRepresentation(volumeDisk); return volume; }
std::shared_ptr<Volume> IvfVolumeReader::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); auto volume = std::make_shared<Volume>(); Deserializer d(InviwoApplication::getPtr(), filePath); d.deserialize("RawFile", rawFile_); rawFile_ = fileDirectory + "/" + rawFile_; std::string formatFlag(""); d.deserialize("Format", formatFlag); format_ = DataFormatBase::get(formatFlag); mat4 basisAndOffset; d.deserialize("BasisAndOffset", basisAndOffset); volume->setModelMatrix(basisAndOffset); mat4 worldTransform; d.deserialize("WorldTransform", worldTransform); volume->setWorldMatrix(worldTransform); d.deserialize("Dimension", dimensions_); volume->setDimensions(dimensions_); d.deserialize("DataRange", volume->dataMap_.dataRange); d.deserialize("ValueRange", volume->dataMap_.valueRange); d.deserialize("Unit", volume->dataMap_.valueUnit); volume->getMetaDataMap()->deserialize(d); littleEndian_ = volume->getMetaData<BoolMetaData>("LittleEndian", littleEndian_); auto vd = std::make_shared<VolumeDisk>(filePath, dimensions_, format_); auto loader = util::make_unique<RawVolumeRAMLoader>(rawFile_, filePos_, dimensions_, littleEndian_, format_); vd->setLoader(loader.release()); volume->addRepresentation(vd); return volume; }
void CImgVolumeRAMLoader::updateRepresentation(std::shared_ptr<DataRepresentation> dest) const { auto volumeDst = std::static_pointer_cast<VolumeRAM>(dest); size3_t dimensions = volumeDisk_->getDimensions(); DataFormatId formatId = DataFormatId::NotSpecialized; std::string filePath = volumeDisk_->getSourceFile(); 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); } } CImgUtils::loadVolumeData(volumeDst->getData(), filePath, dimensions, formatId); volumeDisk_->setDimensions(dimensions); }
std::shared_ptr<DataRepresentation> CImgVolumeRAMLoader::createRepresentation() const { void* data = nullptr; size3_t dimensions = volumeDisk_->getDimensions(); DataFormatId formatId = DataFormatId::NotSpecialized; std::string filePath = volumeDisk_->getSourceFile(); 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); } } data = CImgUtils::loadVolumeData(nullptr, filePath, dimensions, formatId); volumeDisk_->setDimensions(dimensions); return volumeDisk_->getDataFormat()->dispatch(*this, data); }
Volume* MPVMVolumeReader::readMetaData(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::istream* f = new std::ifstream(filePath.c_str()); std::string textLine; std::vector<std::string> files; while (!f->eof()) { getline(*f, textLine); textLine = trim(textLine); files.push_back(textLine); }; delete f; 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<Volume*> volumes; for (size_t i = 0; i < files.size(); i++) { Volume* 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 Volume* volume = new 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 StringMetaData* metaData = volume->getMetaData<StringMetaData>("description"); if (metaData) { 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 VolumeRAM* 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]; } } } // Delete the single channel volumes for (size_t i = 0; i < volumes.size(); i++) { delete volumes[i]; } volume->addRepresentation(mvolRAM); printPVMMeta(volume, filePath); return volume; }