Esempio n. 1
0
Files Extractor::readFiles() {
	Files files;
	while (i < content.size()) {
		files.push_back(readFile());
	}
	return files;
}
Esempio n. 2
0
ConfigProcessor::Files ConfigProcessor::getConfigMergeFiles(const std::string & config_path)
{
    Files res;

    Poco::Path merge_dir_path(config_path);
    merge_dir_path.setExtension("d");

    std::vector<std::string> merge_dirs;
    merge_dirs.push_back(merge_dir_path.toString());
    if (merge_dir_path.getBaseName() != "conf")    {
        merge_dir_path.setBaseName("conf");
        merge_dirs.push_back(merge_dir_path.toString());
    }

    for (const std::string & merge_dir_name : merge_dirs)
    {
        Poco::File merge_dir(merge_dir_name);
        if (!merge_dir.exists() || !merge_dir.isDirectory())
            continue;
        for (Poco::DirectoryIterator it(merge_dir_name); it != Poco::DirectoryIterator(); ++it)
        {
            Poco::File & file = *it;
            if (file.isFile() && (endsWith(file.path(), ".xml") || endsWith(file.path(), ".conf")))
            {
                res.push_back(file.path());
            }
        }
    }

    return res;
}
Esempio n. 3
0
 ReturnCode operator()(const Files &files, const SufficientInfo &si) const
 {
     MSS_BEGIN(ReturnCode);
     MSS(remove_(files));
     Files cachedFiles;
     if (cache_)
     {
         std::ostringstream oss;
         oss << si;
         using namespace hash::md5;
         Stream md5_base;
         md5_base << oss.str();
         for (auto f: files)
         {
             Stream md5(md5_base);
             md5 << f.name();
             File cachedFile(*cache_);
             cachedFile << md5.hash_hex();
             cachedFile.setType(File::Unknown);
             cachedFiles.push_back(cachedFile);
         }
         bool foundAll = true;
         for (auto cf: cachedFiles)
         {
             foundAll = false;
             if (!isRegular(cf))
                 continue;
             foundAll = true;
         }
         if (foundAll)
         {
             //We found all files in the cache, just copy them and finished
             size_t i = 0;
             for (auto &cf: cachedFiles)
             {
                 receiver_().creater_copyFromCache(cf, files[i]);
                 MSS(copy(cf, files[i]));
                 ++i;
             }
             MSS_END();
         }
     }
     //No cache present, or the files are not present in the cache => we have to generate them
     MSS(receiver_().creater_create(files, si));
     //Check that all files are present
     for (auto f: files)
         MSS(isRegular(f));
     //Put the generated files in the cache
     if (cache_)
     {
         size_t i = 0;
         for (auto &cf: cachedFiles)
         {
             receiver_().creater_copyToCache(files[i], cf);
             MSS(copy(files[i], cf));
             ++i;
         }
     }
     MSS_END();
 }
Esempio n. 4
0
void Options::parse()
{
    for (int i = 1; i < _argc; i++)
    {
        if (_argv[i][0] == '-')
        {
            for (int j = 1; _argv[i][j]; j++)
            {
                switch (_argv[i][j])
                {
                case 'v':
                    _verbose = true;
                    break;
                case 'R':
                    _recursive = true;
                    break;
                case 'f':
                    _force = true;
                    break;
                }
            }
        }
        else
        {
            _files.push_back(_argv[i]);
        }
    }
}
Esempio n. 5
0
bool UpkUse7z(LPCTSTR szExeFile, LPCTSTR szExtractPath, Files &files)
{
#if 0 
	static CExeUnpack7z *upk=NULL;
	if(!upk)
	{
		upk = new CExeUnpack7z;
		upk->Initialize(szPath7z, szPathWcx);
	}
	
	strings fs;
	CStringA s1 = CT2CA(szExeFile);
	CStringA s2 = CT2CA(szExtractPath);
	bool bRet = upk->Extract(s1, s2, fs);
	if(bRet)
	{
		for(size_t i=0; i<fs.size(); ++i)
		{
			files.push_back( CString(CA2CT(fs[i].c_str())) );
		}
	}
	return bRet;
#endif
	return false;
}
Esempio n. 6
0
Forest::Files Forest::allRoots() const
{
    assert(invariants_());

    Files files;
    for (auto tree: trees_)
    {
        files.push_back(tree->root());
    }
    return files;
}
Esempio n. 7
0
Forest::Files Forest::allFiles() const
{
    assert(invariants_());

    Files files;
    for (auto tree: trees_)
    {
        for (const auto &f: tree->files())
            files.push_back(f);
    }
    return files;
}
TextSearch::Files TextSearch::search(Files& files, const SearchString& sstr)
{
  Files results;
  for (auto file : files)
  {
    std::ostringstream stext;
    std::ofstream fs;
    fs.open(file, std::fstream::in);
    if (fs.good())
    {
      stext << fs.rdbuf();
      std::string text = stext.str();
      size_t pos = text.find(sstr);
      if (pos < text.size())
        results.push_back(file);
    }
  }
  return results;
}
Esempio n. 9
0
Forest::Files Forest::allFiles(const Tree::Extensions &exts) const
{
    assert(invariants_());

    //Setup a lookup set
    set<string> extensions;
    for (auto e: exts)
        extensions.insert(e);

    Files files;
    for (auto tree: trees_)
    {
        for (const auto &f: tree->files())
        {
            if (extensions.count(f.extension()))
                files.push_back(f);
        }
    }
    return files;
}
        bool getDicomFilesInDirectory(const std::string& path, Files& files) const
        {
            osgDB::DirectoryContents contents = osgDB::getDirectoryContents(path);

            std::sort(contents.begin(), contents.end());

            for(osgDB::DirectoryContents::iterator itr = contents.begin();
                itr != contents.end();
                ++itr)
            {
                std::string localFile = path + "/" + *itr;
                info()<<"contents = "<<localFile<<std::endl;
                if (acceptsExtension(osgDB::getLowerCaseFileExtension(localFile)) &&
                    osgDB::fileType(localFile) == osgDB::REGULAR_FILE)
                {
                    files.push_back(localFile);
                }
            }
            
            return !files.empty();
        }
Esempio n. 11
0
// Get a list of files in the given directory
Files GetFilesInDirectory(const std::string &path, const std::vector<std::string> &extensions)
{
	DIR *dir;
	dirent *entry;
	Files files;

	std::string finalPath = path;
	if (finalPath[path.size() - 1] != '/')
		finalPath += '/';

	if ((dir = opendir(path.c_str())))
	{
		while ((entry = readdir(dir)))
		{
			std::string filename = std::string(entry->d_name);
			if (filename == "." || filename == "..")
				continue;
			std::string fullPath = finalPath + filename;
			// Although the following function is for checking if a directory
			// exists, it can also be used to check if a path is a directory,
			// saving a little bit of extra code
			if (DirExists(fullPath))
				continue;
			// We want to skip any files not matching the extensions we gave, if any
			std::string thisExtension = GetExtension(filename);
			auto extensionMatch = std::find_if(extensions.begin(), extensions.end(), [&](const std::string &extension)
			{
				return extension == thisExtension;
			});
			if (extensions.empty() || extensionMatch != extensions.end())
				files.push_back(fullPath);
		}
		closedir(dir);
	}

	return files;
}
        virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options* options) const
        {
            notice()<<"Reading DICOM file "<<file<<" using DCMTK"<<std::endl;

            std::string ext = osgDB::getLowerCaseFileExtension(file);
            if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
            
            std::string fileName = file;
            if (ext=="dicom")
            {
                fileName = osgDB::getNameLessExtension(file);
            }

            fileName = osgDB::findDataFile( fileName, options );
            if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;

            Files files;
            
            osgDB::FileType fileType = osgDB::fileType(fileName);
            if (fileType==osgDB::DIRECTORY)
            {
                getDicomFilesInDirectory(fileName, files);
            }
            else
            {
#if 1
                files.push_back(fileName);
#else                            
                if (!getDicomFilesInDirectory(osgDB::getFilePath(fileName), files))
                {
                    files.push_back(fileName);
                }
#endif            
            }

            if (files.empty())
            {
                return ReadResult::FILE_NOT_FOUND;
            }

            osg::ref_ptr<osg::RefMatrix> matrix = new osg::RefMatrix;
            osg::ref_ptr<osg::Image> image;
            unsigned int imageNum = 0;
            EP_Representation pixelRep = EPR_Uint8;
            int numPlanes = 0;
            GLenum pixelFormat = 0;
            GLenum dataType = 0;
            unsigned int pixelSize = 0;
            
            typedef std::list<FileInfo> FileInfoList;
            FileInfoList fileInfoList;

            typedef std::map<double, FileInfo> DistanceFileInfoMap;
            typedef std::map<osg::Vec3d, DistanceFileInfoMap> OrientationFileInfoMap;
            OrientationFileInfoMap orientationFileInfoMap;
            
            unsigned int totalNumSlices = 0;

            for(Files::iterator itr = files.begin();
                itr != files.end();
                ++itr)
            {
                DcmFileFormat fileformat;
                OFCondition status = fileformat.loadFile((*itr).c_str());
                if(!status.good()) return ReadResult::ERROR_IN_READING_FILE;
                
                FileInfo fileInfo;
                fileInfo.filename = *itr;

                double pixelSize_y = 1.0;
                double pixelSize_x = 1.0;
                double sliceThickness = 1.0;
                double imagePositionPatient[3] = {0, 0, 0};
                double imageOrientationPatient[6] = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0 };
                Uint16 numOfSlices = 1;
                
                double value = 0.0;
                if (fileformat.getDataset()->findAndGetFloat64(DCM_PixelSpacing, value,0).good())
                {
                    pixelSize_y = value;
                    fileInfo.matrix(1,1) = pixelSize_y;
                }

                if (fileformat.getDataset()->findAndGetFloat64(DCM_PixelSpacing, value,1).good())
                {
                    pixelSize_x = value;
                    fileInfo.matrix(0,0) = pixelSize_x;
                }

                // Get slice thickness
                if (fileformat.getDataset()->findAndGetFloat64(DCM_SliceThickness, value).good())
                {
                    sliceThickness = value;
                    notice()<<"sliceThickness = "<<sliceThickness<<std::endl;
                    fileInfo.sliceThickness = sliceThickness;
                }
    
                notice()<<"tagExistsWithValue(DCM_NumberOfFrames)="<<fileformat.getDataset()->tagExistsWithValue(DCM_NumberOfFrames)<<std::endl;
                notice()<<"tagExistsWithValue(DCM_NumberOfSlices)="<<fileformat.getDataset()->tagExistsWithValue(DCM_NumberOfSlices)<<std::endl;

                Uint32 numFrames;
                if (fileformat.getDataset()->findAndGetUint32(DCM_NumberOfFrames, numFrames).good())
                {
                    fileInfo.numSlices = numFrames;
                    notice()<<"Read number of frames = "<<numFrames<<std::endl;
                }

                
                OFString numFramesStr;
                if (fileformat.getDataset()->findAndGetOFString(DCM_NumberOfFrames, numFramesStr).good())
                {
                    fileInfo.numSlices = atoi(numFramesStr.c_str());
                    notice()<<"Read number of frames = "<<numFramesStr<<std::endl;
                }

                if (fileformat.getDataset()->findAndGetUint16(DCM_NumberOfFrames, numOfSlices).good())
                {
                    fileInfo.numSlices = numOfSlices;
                    notice()<<"Read number of frames = "<<numOfSlices<<std::endl;
                }
                
                if (fileformat.getDataset()->findAndGetUint16(DCM_NumberOfSlices, numOfSlices).good())
                {
                    fileInfo.numSlices = numOfSlices;
                    notice()<<"Read number of slices = "<<numOfSlices<<std::endl;
                }



                // patient position
                for(int i=0; i<3; ++i)
                {
                    if (fileformat.getDataset()->findAndGetFloat64(DCM_ImagePositionPatient, imagePositionPatient[i],i).good())
                    {
                        notice()<<"Read DCM_ImagePositionPatient["<<i<<"], "<<imagePositionPatient[i]<<std::endl;
                    }
                    else
                    {
                        notice()<<"Have not read DCM_ImagePositionPatient["<<i<<"]"<<std::endl;
                    }
                }
                //notice()<<"imagePositionPatient[2]="<<imagePositionPatient[2]<<std::endl;
                
                fileInfo.matrix.setTrans(imagePositionPatient[0],imagePositionPatient[1],imagePositionPatient[2]);

                for(int i=0; i<6; ++i)
                {
                    double value = 0.0;
                    if (fileformat.getDataset()->findAndGetFloat64(DCM_ImageOrientationPatient, value,i).good())
                    {
                        imageOrientationPatient[i] = value;
                        notice()<<"Read imageOrientationPatient["<<i<<"], "<<imageOrientationPatient[i]<<std::endl;
                    }
                    else
                    {
                        notice()<<"Have not read imageOrientationPatient["<<i<<"]"<<std::endl;
                    }
                }

                osg::Vec3d dirX(imageOrientationPatient[0],imageOrientationPatient[1],imageOrientationPatient[2]);
                osg::Vec3d dirY(imageOrientationPatient[3],imageOrientationPatient[4],imageOrientationPatient[5]);
                osg::Vec3d dirZ = dirX ^ dirY;
                dirZ.normalize();
                
                dirX *= pixelSize_x;
                dirY *= pixelSize_y;
                
                fileInfo.matrix(0,0) = dirX[0];
                fileInfo.matrix(1,0) = dirX[1];
                fileInfo.matrix(2,0) = dirX[2];
                
                fileInfo.matrix(0,1) = dirY[0];
                fileInfo.matrix(1,1) = dirY[1];
                fileInfo.matrix(2,1) = dirY[2];
                
                fileInfo.matrix(0,2) = dirZ[0];
                fileInfo.matrix(1,2) = dirZ[1];
                fileInfo.matrix(2,2) = dirZ[2];
                
                fileInfo.distance = dirZ * (osg::Vec3d(0.0,0.0,0.0)*fileInfo.matrix);

                notice()<<"dirX = "<<dirX<<std::endl;
                notice()<<"dirY = "<<dirY<<std::endl;
                notice()<<"dirZ = "<<dirZ<<std::endl;
                notice()<<"matrix = "<<fileInfo.matrix<<std::endl;
                notice()<<"pos = "<<osg::Vec3d(0.0,0.0,0.0)*fileInfo.matrix<<std::endl;
                notice()<<"dist = "<<fileInfo.distance<<std::endl;
                notice()<<std::endl;

                (orientationFileInfoMap[dirZ])[fileInfo.distance] = fileInfo;

                totalNumSlices += fileInfo.numSlices;

            }

            if (orientationFileInfoMap.empty()) return 0;
            
            typedef std::map<double, FileInfo> DistanceFileInfoMap;
            typedef std::map<osg::Vec3d, DistanceFileInfoMap> OrientationFileInfoMap;
            for(OrientationFileInfoMap::iterator itr = orientationFileInfoMap.begin();
                itr != orientationFileInfoMap.end();
                ++itr)
            {
                notice()<<"Orientation = "<<itr->first<<std::endl;
                DistanceFileInfoMap& dfim = itr->second;
                for(DistanceFileInfoMap::iterator ditr = dfim.begin();
                    ditr != dfim.end();
                    ++ditr)
                {
                    FileInfo& fileInfo = ditr->second;
                    notice()<<"   d = "<<fileInfo.distance<<" "<<fileInfo.filename<<std::endl;
                }
            }
            

            DistanceFileInfoMap& dfim = orientationFileInfoMap.begin()->second;
            if (dfim.empty()) return 0;

            double totalDistance = 0.0;
            if (dfim.size()>1)
            {
                totalDistance = fabs(dfim.rbegin()->first - dfim.begin()->first);
            }
            else
            {
                totalDistance = dfim.begin()->second.sliceThickness * double(dfim.begin()->second.numSlices);
            }
            
            notice()<<"Total Distance including ends "<<totalDistance<<std::endl;

            double averageThickness = totalNumSlices<=1 ? 1.0 : totalDistance / double(totalNumSlices-1);
            
            notice()<<"Average thickness "<<averageThickness<<std::endl;

            for(DistanceFileInfoMap::iterator ditr = dfim.begin();
                ditr != dfim.end();
                ++ditr)
            {
                FileInfo& fileInfo = ditr->second;
                
                std::auto_ptr<DicomImage> dcmImage(new DicomImage(fileInfo.filename.c_str()));
                if (dcmImage.get())
                {
                    if (dcmImage->getStatus()==EIS_Normal)
                    {

                        // get the pixel data
                        const DiPixel* pixelData = dcmImage->getInterData();
                        if(!pixelData) 
                        {
                            warning()<<"Error: no data in DicomImage object."<<std::endl;
                            return ReadResult::ERROR_IN_READING_FILE;
                        }
                        
                        osg::ref_ptr<osg::Image> imageAdapter = new osg::Image;
                        
                        EP_Representation curr_pixelRep;
                        int curr_numPlanes;
                        GLenum curr_pixelFormat;
                        GLenum curr_dataType;
                        unsigned int curr_pixelSize;

                        // create the new image
                        convertPixelTypes(pixelData,
                                          curr_pixelRep, curr_numPlanes, 
                                          curr_dataType, curr_pixelFormat, curr_pixelSize);

                        imageAdapter->setImage(dcmImage->getWidth(), dcmImage->getHeight(), dcmImage->getFrameCount(),
                                              curr_pixelFormat,
                                              curr_pixelFormat,
                                              curr_dataType,
                                              (unsigned char*)(pixelData->getData()),
                                              osg::Image::NO_DELETE);
                        if (!image)
                        {
                            pixelRep = curr_pixelRep;
                            numPlanes = curr_numPlanes;
                            dataType = curr_dataType;
                            pixelFormat = curr_pixelFormat;
                            pixelSize = curr_pixelSize;

                            (*matrix)(0,0) = fileInfo.matrix(0,0);
                            (*matrix)(1,0) = fileInfo.matrix(1,0);
                            (*matrix)(2,0) = fileInfo.matrix(2,0);
                            (*matrix)(0,1) = fileInfo.matrix(0,1);
                            (*matrix)(1,1) = fileInfo.matrix(1,1);
                            (*matrix)(2,1) = fileInfo.matrix(2,1);
                            (*matrix)(0,2) = fileInfo.matrix(0,2) * averageThickness;
                            (*matrix)(1,2) = fileInfo.matrix(1,2) * averageThickness;
                            (*matrix)(2,2) = fileInfo.matrix(2,2) * averageThickness;
                            

                            
                            image = new osg::Image;
                            image->setUserData(matrix.get());
                            image->setFileName(fileName.c_str());
                            image->allocateImage(dcmImage->getWidth(), dcmImage->getHeight(), totalNumSlices, 
                                                 pixelFormat, dataType);
                                                 
                                                 
                            matrix->preMult(osg::Matrix::scale(double(image->s()), double(image->t()), double(image->r())));

                            notice()<<"Image dimensions = "<<image->s()<<", "<<image->t()<<", "<<image->r()<<" pixelFormat=0x"<<std::hex<<pixelFormat<<" dataType=0x"<<std::hex<<dataType<<std::dec<<std::endl;
                        }
                        else if (pixelData->getPlanes()>numPlanes ||
                                 pixelData->getRepresentation()>pixelRep)
                        {
                            notice()<<"Need to reallocated "<<image->s()<<", "<<image->t()<<", "<<image->r()<<std::endl;
                            
                            // record the previous image settings to use when we copy back the content.
                            osg::ref_ptr<osg::Image> previous_image = image;

                            // create the new image
                            convertPixelTypes(pixelData,
                                              pixelRep, numPlanes, 
                                              dataType, pixelFormat, pixelSize);
                                              
                            image = new osg::Image;
                            image->setUserData(previous_image->getUserData());
                            image->setFileName(fileName.c_str());
                            image->allocateImage(dcmImage->getWidth(), dcmImage->getHeight(), totalNumSlices, 
                                                 pixelFormat, dataType);
                                                 
                            osg::copyImage(previous_image.get(), 0,0,0, previous_image->s(), previous_image->t(), imageNum,
                                           image.get(), 0, 0, 0,                                                 
                                           false);
                            
                        }
                        
                        osg::copyImage(imageAdapter.get(), 0,0,0, imageAdapter->s(), imageAdapter->t(), imageAdapter->r(), 
                                       image.get(), 0, 0, imageNum,
                                       false);
                                             
                        imageNum += dcmImage->getFrameCount();
                    }
                    else
                    {
                        warning()<<"Error in reading dicom file "<<fileName.c_str()<<", error = "<<DicomImage::getString(dcmImage->getStatus())<<std::endl;
                    }
                }
            }
            
            if (!image)
            {
                return ReadResult::ERROR_IN_READING_FILE;
            }

            notice()<<"Spacing = "<<*matrix<<std::endl;
            
            return image.get();
        }
Esempio n. 13
0
int main(int argc, char** argv)
{
	if(argc < 3){
		cout << "Prague metadata tool" << endl;
		cout << "Usage: " << argv[0] <<" [-s] [-c] [-o  <output file>] <metadata filename1>...<metadata filenameN>" << endl;
		cout << "\t-s\t\t" << "change byteorder of metadata from little-endian to big-endian" << endl;
		cout << "\t-c\t\t" << "save in metacache format" << endl;
		cout << "\t-m\t\t" << "save in multimeta format" << endl;
		cout << "\t-o <filename>\t" << "name of output file [default is out.meta]" << endl;
		return -1;
	}
	bool swap = false;
	bool cache = false;
	bool multi = false;
	Files files;
	int i = 1;
	string outfile("out.meta");
	while(i < argc){
		if(argv[i][0] == '-'){
			switch(argv[i++][1]){
			case 's': swap = true; break;
			case 'c': cache = true; break;
			case 'm': multi = true; break;	
			case 'o': 
				if(i < argc) 
					outfile = argv[i++]; 
				else 
					cerr << "an output file is not specified" << endl;
				break;					
			default: cerr << "unknown option '" << argv[i][1] << "'" << endl;
			}
		}
		else
			files.push_back(argv[i++]);
	}
	PragueMetadata metadata;
	MetaLoader<GetLEValue> loader;
	for(Files::const_iterator i = files.begin(); i != files.end(); ++i)
		if(!loader.load(i->c_str(), metadata)){
			cerr << "Can't load metadata from " << *i << endl;
			continue;
		}
	bool result = false;
	if(swap){
		MetaSaver<SaveBEValue> saver;
		if(!saver.open(outfile.c_str())){
			cerr << "can't open " << outfile << endl;
			return -1;
		}
		if(cache)
			result = saver.saveCache(metadata);
		else if(multi || (metadata.count() > 1))
			result = saver.saveMulti(metadata);
		else 
			result = saver.saveOne(metadata);
	}
	else{
		MetaSaver<SaveValue> saver;
		if(!saver.open(outfile.c_str())){
			cerr << "can't open " << outfile << endl;
			return -1;
		}
		if(cache)
			result = saver.saveCache(metadata);
		else if(multi || (metadata.count() > 1))
			result = saver.saveMulti(metadata);
		else 
			result = saver.saveOne(metadata);
	}
	if(!result){
		cerr << "Can't save metadata to " << outfile << endl;
		return -1;
	}

	return 0;
}