/*************************************************** Author : Rohit Suri ***************************************************/ #include #include #include #include #include #include using namespace std; extern "C" { #include "api_scilab.h" #include "Scierror.h" #include "BOOL.h" #include string formats[] = {"jpeg","jpg","bmp","png","JPEG","JPG","BMP","PNG"}; vector > descriptionVector; const char pathSeparator = #ifdef _WIN32 '\\'; #else '/'; #endif bool recursive = false; bool listDirError = 0; bool isImage(string fname) { for(int i=0;i<8;i++) { if(fname.substr(fname.length()-formats[i].length()).compare(formats[i])==0) return 1; } return 0; } void listDir(string dirName, vector ¤tDir) { DIR *dir; struct dirent *entry; if(!(dir = opendir(dirName.c_str()))) { Scierror(999,"Cannot open directory for reading. Please check the directory name.\n"); listDirError = 1; return; } if(!(entry = readdir(dir))) { Scierror(999,"The directory is empty.\n"); listDirError = 1; return; } do { if (entry -> d_type == DT_DIR) { if(strcmp(entry->d_name,".")==0 || strcmp(entry->d_name,"..")==0) continue; if(recursive) { vector nextDir; nextDir.push_back(entry->d_name); listDir(dirName+pathSeparator+entry->d_name,nextDir); } } else { if(isImage(entry->d_name)) { //cout<d_name<d_name); } } }while(entry = readdir(dir)); vector::iterator it = currentDir.begin(); sort(it+1,currentDir.end()); descriptionVector.push_back(currentDir); } /*Calling syntax: imageSet(imageLocation) OR imageSet(imageFolder,'recursive') */ int opencv_imageSet(char *fname, unsigned long fname_len) { // Error management variables SciErr sciErr; //------Local variables------// int *piAddr = NULL; int *piChild = NULL; int *piLen = NULL; char **pstData = NULL; int iRows, iCols; string filePath,fileName,tempPath; char **description = NULL; char ***location = NULL; int *count = NULL; int descriptionCount = 0; int pos = 0; char *objectType = "imageSet"; //------Check number of parameters------// CheckInputArgument(pvApiCtx, 1, 2); CheckOutputArgument(pvApiCtx, 1, 1); //------Get input arguments------// sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } piLen = (int*) malloc(sizeof(int) * iRows * iCols); sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } pstData = (char**) malloc(sizeof(char*) * iRows * iCols); for(int iterPstData = 0; iterPstData < iRows * iCols; iterPstData++) { pstData[iterPstData] = (char*) malloc(sizeof(char) * piLen[iterPstData] + 1); } sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, pstData); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } filePath = string(pstData[0]); if(*getNbInputArgument(pvApiCtx) == 2) { sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } piLen = (int*) malloc(sizeof(int) * iRows * iCols); sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } pstData = (char**) malloc(sizeof(char*) * iRows * iCols); for(int iterPstData = 0; iterPstData < iRows * iCols; iterPstData++) { pstData[iterPstData] = (char*) malloc(sizeof(char) * piLen[iterPstData] + 1); } sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, pstData); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if(strcmp("recursive",pstData[0])!=0) { Scierror(999, "Error: Argument %s is invalid. \n", pstData[0]); return 0; } recursive = true; } //------Actual processing------// while(filePath[filePath.length()-1]==pathSeparator) { filePath = filePath.substr(0,filePath.length()-1); } vector nextDir; nextDir.push_back(filePath.substr(filePath.find_last_of(pathSeparator)+1)); listDir(filePath,nextDir); if(listDirError) { listDirError = 0; //Doing this is very important as it is a global return 0; } for(int i=0;i1) descriptionCount++; } description = (char**) malloc(sizeof(char*) * descriptionCount); count = (int*) malloc(sizeof(int) * descriptionCount); location = (char***) malloc(sizeof(char**) * descriptionCount); for(int i=0;i1) { description[pos] = (char*) malloc(sizeof(char) * descriptionVector[i][0].length() + 1); descriptionVector[i][0].copy(description[pos],descriptionVector[i][0].length()); description[pos][descriptionVector[i][0].length()] = 0; count[pos] = descriptionVector[i].size()-1; location[pos] = (char**) malloc(sizeof(char*) * (descriptionVector[i].size()-1)); for(int j=1;j