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; }
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; }
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; }
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; }
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; } }