void VolumeSeriesSource::loadStep() {
    if(!volume_)
        return;

    VolumeRAM* v = volume_->getWritableRepresentation<VolumeRAM>();
    if (!v)
        return;

    int step = step_.get();
    std::string filename;

    if (files_.size() > 0 && step < static_cast<int>(files_.size()))
        filename = files_[step];
    else
        return;

    std::ifstream f(filename.c_str(), std::ios_base::binary);
    if (!f) {
        LERROR("Could not open file: " << filename);
        return;
    }

    // read the volume as a raw blob, should be fast
    LINFO("Loading raw file " << filename);
    f.read(reinterpret_cast<char*>(v->getData()), v->getNumBytes());
    if (!f.good()) {
        LERROR("Reading from file failed: " << filename);
        return;
    }

    // Special handling for float volumes: normalize values to [0.0; 1.0]
    VolumeRAM_Float* vf = dynamic_cast<VolumeRAM_Float*>(v);
    if (vf && spreadMin_ != spreadMax_) {
        const size_t n = vf->getNumVoxels();

        // use spread values if available
        if (spreadMin_ != spreadMax_) {
            const float d = spreadMax_ - spreadMin_;
            float* voxel = vf->voxel();
            for (size_t i = 0; i < n; ++i)
                voxel[i] = (voxel[i] - spreadMin_) / d;
        } else {
            LINFO("Normalizing float data to [0.0; 1.0]. "
                  << "This might not be what you want, better define 'Spread: <min> <max>' in the .sdat file.");
            const float d = vf->max() - vf->min();
            const float p = vf->min();
            float* voxel = vf->voxel();
            for (size_t i = 0; i < n; ++i)
                voxel[i] = (voxel[i] - p) / d;
        }
        vf->invalidate();
    }

    needUpload_ = true;
    invalidate();
}
VolumeCollection* RawVoxVolumeReader::read(const std::string &url)
throw (tgt::CorruptedFileException, tgt::IOException, std::bad_alloc)
{
    VolumeURL origin(url);
    std::string fileName = origin.getPath();

    LINFO("Reading file " << fileName);

    std::fstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
    if (!fin.good())
        throw tgt::IOException();

    RawVoxHeader header;
    fin.read(reinterpret_cast<char*>(&header), sizeof(header));
    svec3 dimensions = svec3(header.sizeX_, header.sizeY_, header.sizeZ_);

    if(header.magic_ != 1381388120) {
        throw tgt::CorruptedFileException("Wrong magic number.");
    }

    VolumeRAM* dataset;
    switch(header.bitsPerVoxel_) {
    case 8:
        LINFO("Reading 8 bit dataset");
        dataset = new VolumeRAM_UInt8(dimensions);
        break;
    case 16:
        LINFO("Reading 16 bit dataset");
        dataset = new VolumeRAM_UInt16(dimensions);
        break;
    case 32:
        LINFO("Reading 32 bit (float) dataset");
        dataset = new VolumeRAM_Float(dimensions);
        break;
    default:
        LERROR("Unknown bpp!");
        throw tgt::UnsupportedFormatException("Unexpected bpp.");
    }

    fin.read(reinterpret_cast<char*>(dataset->getData()), dataset->getNumBytes());

    if ( fin.eof() ) {
        delete dataset;
        throw tgt::CorruptedFileException();
    }

    fin.close();

    VolumeCollection* volumeCollection = new VolumeCollection();
    Volume* volumeHandle = new Volume(dataset, vec3(1.0f), vec3(0.0f));
    oldVolumePosition(volumeHandle);
    volumeHandle->setOrigin(fileName);
    volumeCollection->add(volumeHandle);

    return volumeCollection;
}
DataRepresentation* VolumeGL2RAMConverter::createFrom(const DataRepresentation* source) {
    const VolumeGL* volumeGL = static_cast<const VolumeGL*>(source);
    VolumeRAM* volume = createVolumeRAM(volumeGL->getDimensions(), volumeGL->getDataFormat());

    if (volume) {
        volumeGL->getTexture()->download(volume->getData());
        return volume;
    } else {
        LogError("Cannot convert format from GL to RAM:" << volumeGL->getDataFormat()->getString());
    }

    return nullptr;
}
DataRepresentation* VolumeCL2RAMConverter::createFrom(const DataRepresentation* source) {
    DataRepresentation* destination = 0;
    const VolumeCL* volumeCL = static_cast<const VolumeCL*>(source);
    size3_t dimensions = volumeCL->getDimensions();
    destination = createVolumeRAM(dimensions, volumeCL->getDataFormat());

    if (destination) {
        VolumeRAM* volumeRAM = static_cast<VolumeRAM*>(destination);
        volumeCL->download(volumeRAM->getData());
        //const cl::CommandQueue& queue = OpenCL::getInstance()->getQueue();
        //queue.enqueueReadImage(volumeCL->getVolume(), true, glm::size3_t(0), glm::size3_t(dimension), 0, 0, volumeRAM->getData());
    }

    return destination;
}
Exemple #5
0
void VolumeCubify::process() {

    if (!enableProcessing_.get()) {
        outport_.setData(inport_.getData(), false);
        return;
    }

    const VolumeRAM* inputVolume = inport_.getData()->getRepresentation<VolumeRAM>();
    VolumeRAM* outputVolume = 0;

    tgt::ivec3 oldims = inputVolume->getDimensions();
    tgt::ivec3 newdims = tgt::ivec3(tgt::max(oldims));
    tgt::ivec3 llf = (newdims - oldims) / 2;
    tgt::ivec3 urb = (newdims + oldims) / 2;

    if(dynamic_cast<const VolumeRAM_UInt8*>(inputVolume))
        outputVolume = new VolumeRAM_UInt8(newdims);
    else if(dynamic_cast<const VolumeRAM_UInt16*>(inputVolume))
        outputVolume = new VolumeRAM_UInt16(newdims);
    else
        LERROR("Unsupported value for getBitsStored()");

    for (int voxel_z=0; voxel_z < newdims.z; voxel_z++) {
        for (int voxel_y=0; voxel_y < newdims.y; voxel_y++) {
            for (int voxel_x=0; voxel_x < newdims.x; voxel_x++) {

                tgt::ivec3 pos = tgt::ivec3(voxel_x, voxel_y, voxel_z);

                if(tgt::hor(tgt::lessThan(pos, llf)) || tgt::hor(tgt::greaterThanEqual(pos, urb)))
                    outputVolume->setVoxelNormalized(0.f, pos);
                else {
                    tgt::ivec3 oldPos = pos - llf;
                    outputVolume->setVoxelNormalized(inputVolume->getVoxelNormalized(oldPos), pos);
                }
            }
        }
    }

    // assign computed volume to outport
    if (outputVolume) {
        Volume* h = new Volume(outputVolume, inport_.getData());
        h->setOffset(h->getOffset() - (tgt::vec3(llf) * h->getSpacing()));
        outport_.setData(h);
    }
    else
        outport_.setData(0);
}
Exemple #6
0
VolumeDisk* VolumeDisk::getSubVolume(tgt::svec3 dimensions, tgt::svec3 offset) const throw (std::bad_alloc){
    // create one voxel volume to get bits for current type
    VolumeFactory vf;
    VolumeRAM* volume = vf.create(getFormat(), tgt::svec3(1,1,1));

    int voxelSize = 1;
    if(volume){
        voxelSize = volume->getBitsAllocated() / 8;
        delete volume;
    }

    // calculate offset
    tgt::ivec3 dataDimsI = static_cast<tgt::ivec3>(getDimensions());
    tgt::ivec3 offsetI = static_cast<tgt::ivec3>(offset);
    int64_t initialStartPos = offset_ + (offsetI.z * (dataDimsI.x*dataDimsI.y)*voxelSize)+(offsetI.y * dataDimsI.x*voxelSize) + (offsetI.x*voxelSize);

    // create new disk representation
    VolumeDisk* newDiskRep = new VolumeDisk(filename_, format_, static_cast<tgt::ivec3>(dimensions), initialStartPos);

    return newDiskRep;
}
	void VolumeNoisifier::apply(){
		if(inport_.hasData()){
			Volume* vol = inport_.getData()->clone();
			const VolumeRAM* vr = vol->getRepresentation<VolumeRAM>();
			VolumeRAM* vrw = vol->getWritableRepresentation<VolumeRAM>();
			size_t numVertices = vol->getNumVoxels();
			
			float mxi = vr->maxNormalizedValue(), mni = vr->minNormalizedValue();
			printf("Max before : %f | Min before : %f\n", mxi, mni);
			float mxia = 0, mnia = FLT_MAX;
			for(size_t i = 0; i < numVertices; ++i){
				float val = vr->getVoxelNormalized(i);
				val += level_.get()*((mxi - mni)*((float)rand()/RAND_MAX) + mni);
				mxia = val > mxia ? val : mxia;
				mnia = val < mnia ? val : mnia;
				vrw->setVoxelNormalized(val, i);
			}
			printf("Max after  : %f | Min after  : %f\n", mxia, mnia);
			outport_.setData(vol);
		}
		
	}
void VolumeGL2RAMConverter::update(const DataRepresentation* source, DataRepresentation* destination) {
    const VolumeGL* volumeSrc = static_cast<const VolumeGL*>(source);
    VolumeRAM* volumeDst = static_cast<VolumeRAM*>(destination);

    if (volumeSrc->getDimensions() != volumeDst->getDimensions()) {
        volumeDst->setDimensions(volumeSrc->getDimensions());
    }

    volumeSrc->getTexture()->download(volumeDst->getData());

    if (volumeDst->hasHistograms())
        volumeDst->getHistograms()->setValid(false);
}
Exemple #9
0
Histogram1D createHistogram1DFromVolume(const VolumeBase* handle, int bucketCount, size_t channel /*= 0*/) {
    RealWorldMapping rwm = handle->getRealWorldMapping();

    const VolumeMinMax* volumeMinMax = handle->getDerivedData<VolumeMinMax>();
    tgtAssert(channel < volumeMinMax->getNumChannels(), "invalid channel");
    float min = volumeMinMax->getMinNormalized(channel);
    float max = volumeMinMax->getMaxNormalized(channel);
    min = rwm.normalizedToRealWorld(min);
    max = rwm.normalizedToRealWorld(max);

    Histogram1D h(min, max, bucketCount);

    // prefer RAM over disk representation, but only if RAM volume is already present
    const VolumeRAM* volumeRam = 0;
    const VolumeDisk* volumeDisk = 0;
    if (handle->hasRepresentation<VolumeRAM>())
        volumeRam = handle->getRepresentation<VolumeRAM>();
    else if (handle->hasRepresentation<VolumeDisk>())
        volumeDisk = handle->getRepresentation<VolumeDisk>();
    else {
        LWARNINGC("voreen.Histogram", "Unable to compute 1D histogram: neither disk nor ram represenation available");
        return h;
    }
    tgtAssert(volumeRam || volumeDisk, "no representation");

    // iterate over slices
    tgt::svec3 dims = handle->getDimensions();
    tgt::svec3 pos;
    for (pos.z = 0; pos.z < dims.z; ++pos.z) {
        try {
            boost::this_thread::sleep(boost::posix_time::seconds(0));
        }
        catch(boost::thread_interrupted&)
        {
            throw boost::thread_interrupted();
        }

        if (volumeRam) {
            // access volume data in RAM directly
            for (pos.y = 0; pos.y < dims.y; ++pos.y) {
                for (pos.x = 0; pos.x < dims.x; ++pos.x) {
                    float val = volumeRam->getVoxelNormalized(pos, channel);
                    val = rwm.normalizedToRealWorld(val);
                    h.addSample(val);
                }
            }
        }
        else if (volumeDisk) {
            try {
                // temporarily load current slice into RAM
                VolumeRAM* sliceVolume = volumeDisk->loadSlices(pos.z, pos.z);
                tgtAssert(sliceVolume, "null pointer returned (exception expected)");
                for (pos.y = 0; pos.y < dims.y; ++pos.y) {
                    for (pos.x = 0; pos.x < dims.x; ++pos.x) {
                        float val = sliceVolume->getVoxelNormalized(tgt::svec3(pos.x, pos.y, 0), channel);
                        val = rwm.normalizedToRealWorld(val);
                        h.addSample(val);
                    }
                }
                delete sliceVolume;
            }
            catch (tgt::Exception& e) {
                LWARNINGC("voreen.Histogram", "Unable to compute 1D histogram: failed to load slice from disk volume: " + std::string(e.what()));
                return h;
            }
        }
        else {
            tgtAssert(false, "should never get here");
        }
    }

    return h;
}
Exemple #10
0
void VolumeMasking::maskVolume() {
    tgtAssert(inport_.hasData(), "Inport has not data");

    forceUpdate_ = false;
    const VolumeRAM* vol = inport_.getData()->getRepresentation<VolumeRAM>();

    if (vol) {
        VolumeRAM* v = 0;
        v = vol->clone();

        tgt::Texture* maskTexture = maskFunction_.get()->getTexture();
        TransFunc1DKeys* tf = dynamic_cast<TransFunc1DKeys*>(maskFunction_.get());
        maskTexture->downloadTexture();
        const int maskTexDim = maskTexture->getDimensions().x;

        RealWorldMapping rwm = inport_.getData()->getRealWorldMapping();
        // apply masking
        if (passedVoxelAction_.isSelected("maxIntensity")) {
            for (size_t i=0; i<v->getNumVoxels(); i++) {
                float intensity = vol->getVoxelNormalized(i);
                intensity = rwm.normalizedToRealWorld(intensity);
                intensity = tf->realWorldToNormalized(intensity);
                int index = static_cast<int>(intensity*(maskTexDim-1));
                index = tgt::clamp(index, 0, maskTexDim-1);

                int alpha = maskTexture->texel< tgt::Vector4<uint8_t> >(static_cast<size_t>(index)).a;
                v->setVoxelNormalized(alpha == 0 ? 0.f : 1.f, i);
                //progressBar_->setProgress(static_cast<float>(i) / static_cast<float>(v->getNumVoxels()));
            }
        }
        else if (passedVoxelAction_.isSelected("passThrough")) {
            for (size_t i=0; i<v->getNumVoxels(); i++) {
                float intensity = vol->getVoxelNormalized(i);
                intensity = rwm.normalizedToRealWorld(intensity);
                intensity = tf->realWorldToNormalized(intensity);
                int index = static_cast<int>(intensity*(maskTexDim-1));
                index = tgt::clamp(index, 0, maskTexDim-1);

                int alpha = maskTexture->texel< tgt::Vector4<uint8_t> >(static_cast<size_t>(index)).a;
                if (alpha == 0)
                    v->setVoxelNormalized(0.f, i);
            }
        }
        else if (passedVoxelAction_.isSelected("alpha")) {
            for (size_t i=0; i<v->getNumVoxels(); i++) {
                float intensity = vol->getVoxelNormalized(i);
                intensity = rwm.normalizedToRealWorld(intensity);
                intensity = tf->realWorldToNormalized(intensity);
                int index = static_cast<int>(intensity*(maskTexDim-1));
                index = tgt::clamp(index, 0, maskTexDim-1);

                int alpha = maskTexture->texel< tgt::Vector4<uint8_t> >(static_cast<size_t>(index)).a;
                v->setVoxelNormalized((float)alpha / 255.0f, i);
            }
        }
        else {
            LWARNING("Unknown voxel action: " << passedVoxelAction_.get());
        }

        Volume* vh = new Volume(v, inport_.getData());
        if (passedVoxelAction_.isSelected("alpha"))
            vh->setRealWorldMapping(RealWorldMapping());
        outport_.setData(vh);
    }
    else {
        outport_.setData(0);
    }
}
Exemple #11
0
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;
}
Exemple #12
0
VolumeRAM* VolumeDiskRaw::loadBrick(const tgt::svec3& pOffset, const tgt::svec3& pDimensions) const
    throw (tgt::Exception)
{
    //check for wrong parameter
    if(tgt::hmul(pDimensions) == 0)
        throw std::invalid_argument("requested brick dimensions are zero!");
    if(!tgt::hand(tgt::lessThanEqual(pOffset+pDimensions,getDimensions())))
        throw std::invalid_argument("requested brick outside volume date!");

    //create new VolumeRam
    VolumeFactory vf;
    VolumeRAM* vr = vf.create(getFormat(), pDimensions);
    if (!vr)
        throw VoreenException("Failed to create VolumeRAM");

    //open file
    FILE* fin;
    fin = fopen(getFileName().c_str(),"rb");
    if (!fin)
        throw tgt::FileException("Failed to open file for reading: " + getFileName());

    size_t bytesPerVoxel = static_cast<size_t>(vr->getBitsAllocated() / 8);
    size_t numVoxels = pDimensions.x;
    size_t numBytes = numVoxels * bytesPerVoxel;

    int64_t offset = getOffset();
    if(offset < 0) {
        //Assume data is aligned to end of file.

        // get file size:
        fseek(fin, 0, SEEK_END);
        int64_t fileSize = ftell(fin);
        rewind(fin);

        //calculate real offset:
        offset = fileSize - hmul(getDimensions())*bytesPerVoxel;
    }

    //modify offset to start at first slice
    offset += getDimensions().x*getDimensions().y*pOffset.z*bytesPerVoxel;

    fseek(fin, static_cast<long>(offset), SEEK_SET);

    //read into ram
    size_t pointerOffset = 0;
    for(size_t z = 0; z < pDimensions.z; z++){
        for(size_t y = 0; y < pDimensions.y; y++) {
            //read into ram
            if(fread(reinterpret_cast<char*>(vr->getData())+pointerOffset, numBytes, 1, fin) != 1) {
                fclose(fin);
                delete vr;
                throw tgt::FileException("Failed to read from file: " + getFileName());
            }
            //move offset
            pointerOffset += numBytes;

            //move to next read
            if(y < pDimensions.y)
                fseek(fin, static_cast<long>(getDimensions().x-pDimensions.x),SEEK_SET);
        }
        //move to next read
        if(z < pDimensions.z)
            fseek(fin,static_cast<long>((getDimensions().x*getDimensions().y)-(pDimensions.x*pDimensions.y)),SEEK_SET);
    }

    fclose(fin);

    //swap endian
    if(getSwapEndian()) {
        Volume* tempHandle = new Volume(vr, vec3(1.0f), vec3(0.0f));
        VolumeOperatorSwapEndianness::APPLY_OP(tempHandle);
        tempHandle->releaseAllRepresentations();
        delete tempHandle;
    }

    return vr;
}
Exemple #13
0
VolumeRAM* VolumeDiskRaw::loadSlices(const size_t firstSlice, const size_t lastSlice) const
    throw (tgt::Exception)
{
    //check for wrong parameter
    if(getDimensions().z <= lastSlice)
        throw std::invalid_argument("lastSlice is out of volume dimension!!!");
    if(firstSlice > lastSlice)
        throw std::invalid_argument("firstSlice has to be less or equal lastSlice!!!");

    //create new VolumeRam
    VolumeFactory vf;
    VolumeRAM* vr = vf.create(getFormat(), tgt::svec3(getDimensions().x,getDimensions().y, lastSlice-firstSlice+1));
    if (!vr)
        throw VoreenException("Failed to create VolumeRAM");

    //open file
    std::ifstream infile(getFileName().c_str(), std::ios::in | std::ios::binary);
    if (infile.fail())
        throw tgt::FileException("Failed to open file for reading: " + getFileName());

    size_t bytesPerVoxel = static_cast<size_t>(vr->getBitsAllocated() / 8);
    size_t numVoxels = getDimensions().x*getDimensions().y*(lastSlice-firstSlice+1);
    size_t numBytes = numVoxels * bytesPerVoxel;

    int64_t offset = getOffset();

    if(offset < 0) {
        //Assume data is aligned to end of file.

        // get file size:
        infile.seekg( 0, infile.end);
        std::streampos fileSize = infile.tellg();
        infile.seekg( 0, infile.beg);

        //calculate real offset:
        offset = static_cast<std::string::size_type>(fileSize) - hmul(getDimensions())*bytesPerVoxel;
    }

    //modify offset to start at first slice
    offset += getDimensions().x*getDimensions().y*firstSlice*bytesPerVoxel;
    infile.seekg(offset);

    //read into ram
    infile.read(reinterpret_cast<char*>(vr->getData()),numBytes);

    if (infile.fail()) {
        //LERRORC("voreen.RepresentationConverterLoadFromDisk", "read() failed");
        infile.close();
        delete vr;
        throw tgt::FileException("Failed to read from file: " + getFileName());
    }
    infile.close();

    //swap endian
    if(getSwapEndian()) {
        Volume* tempHandle = new Volume(vr, vec3(1.0f), vec3(0.0f));
        VolumeOperatorSwapEndianness::APPLY_OP(tempHandle);
        tempHandle->releaseAllRepresentations();
        delete tempHandle;
    }

    return vr;
}
Exemple #14
0
VolumeRAM* VolumeDiskRaw::loadVolume() const
    throw (tgt::Exception)
{
    VolumeRAM* volume = 0;
    LDEBUG("Creating volume from diskrepr. " << getFileName() << " format: " << getFormat());
    VolumeFactory vf;
    try {
        volume = vf.create(getFormat(), getDimensions());
    }
    catch (std::bad_alloc&) {
        throw tgt::Exception("bad allocation");
    }
    if (!volume)
        throw VoreenException("Failed to create VolumeRAM");

    FILE* fin;
    fin = fopen(getFileName().c_str(),"rb");

    if (fin == 0) {
        throw tgt::IOException("Unable to open raw file for reading", getFileName());
    }

    size_t bytesPerVoxel = static_cast<size_t>(volume->getBitsAllocated() / 8);
    size_t numVoxels = hmul(getDimensions());
    size_t numBytes = numVoxels * bytesPerVoxel;

    int64_t offset = getOffset();
    if(offset < 0) {
        //Assume data is aligned to end of file.

        // get file size:
        fseek(fin, 0, SEEK_END);
        int64_t fileSize = ftell(fin);
        rewind(fin);

        //calculate real offset:
        offset = fileSize - numBytes;
    }
#ifdef _MSC_VER
    _fseeki64(fin, offset, SEEK_SET);
#else
    fseek(fin, offset, SEEK_SET);
#endif

    if(fread(reinterpret_cast<char*>(volume->getData()), numBytes, 1, fin) != 1) {
        //LERRORC("voreen.RepresentationConverterLoadFromDisk", "fread() failed");
        fclose(fin);
        delete volume;
        throw tgt::FileException("Failed to read from file: " + getFileName());
    }

    fclose(fin);

    if (getSwapEndian()) {
        Volume* tempHandle = new Volume(volume, vec3(1.0f), vec3(0.0f));
        VolumeOperatorSwapEndianness::APPLY_OP(tempHandle);
        tempHandle->releaseAllRepresentations();
        delete tempHandle;
    }

    return volume;
}
Exemple #15
0
VolumeRepresentation* RepresentationConverterLoadFromDisk::convert(const VolumeRepresentation* source) const {
    const VolumeDisk* dr = dynamic_cast<const VolumeDisk*>(source);

    if(dr) {
        VolumeRAM* volume = 0;
        LDEBUGC("voreen.RepresentationConverterLoadFromDisk", "creating volume from diskrepr. " << dr->getFileName() << " format: " << dr->getFormat());
        VolumeFactory vf;
        volume = vf.create(dr->getFormat(), dr->getDimensions());

        if(!volume)
            return 0;

        FILE* fin;
        fin = fopen(dr->getFileName().c_str(),"rb");

        if (fin == 0)
            throw tgt::IOException("Unable to open raw file for reading", dr->getFileName());

        size_t bytesPerVoxel = static_cast<size_t>(volume->getBitsAllocated() / 8);
        size_t numVoxels = hmul(dr->getDimensions());
        size_t numBytes = numVoxels * bytesPerVoxel;

        int64_t offset = dr->getOffset();
        if(offset < 0) {
            //Assume data is aligned to end of file.

            // get file size:
            fseek(fin, 0, SEEK_END);
            int64_t fileSize = ftell(fin);
            rewind(fin);

            //calculate real offset:
            offset = fileSize - numBytes;
        }
#ifdef _MSC_VER
        _fseeki64(fin, offset, SEEK_SET);
#else
        fseek(fin, offset, SEEK_SET);
#endif

        if(fread(reinterpret_cast<char*>(volume->getData()), numBytes, 1, fin) != 1) {
            LERRORC("voreen.RepresentationConverterLoadFromDisk", "fread() failed");
            fclose(fin);
            delete volume;
            return 0;
        }

        fclose(fin);

        if(dr->getSwapEndian()) {
            Volume* tempHandle = new Volume(volume, vec3(1.0f), vec3(0.0f));
            VolumeOperatorSwapEndianness::APPLY_OP(tempHandle);
            tempHandle->releaseAllRepresentations();
        }

        return volume;
    }
    else {
        //should have checked before...
        //LERROR("Failed to convert!");
        return 0;
    }
}