Skip to content

Commit

Permalink
Argument -g generates default files only when requested #152
Browse files Browse the repository at this point in the history
  • Loading branch information
neurolabusc committed Jan 12, 2018
1 parent ccba52a commit 8ea093f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
17 changes: 14 additions & 3 deletions console/main_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ void showHelp(const char * argv[], struct TDCMopts opts) {
printf(" -ba : anonymize BIDS (y/n, default %c)\n", bidsCh);
printf(" -c : comment stored as NIfTI aux_file (up to 24 characters)\n");
if (opts.isSortDTIbyBVal) bidsCh = 'y'; else bidsCh = 'n';
printf(" -d : diffusion volumes sorted by b-value (y/n, default %c)\n", bidsCh);
//printf(" -d : diffusion volumes sorted by b-value (y/n, default %c)\n", bidsCh);
#ifdef mySegmentByAcq
#define kQstr " %%q=sequence number,"
#else
#define kQstr ""
#endif
printf(" -f : filename (%%a=antenna (coil) number, %%c=comments, %%d=description, %%e echo number, %%f=folder name, %%i ID of patient, %%j seriesInstanceUID, %%k studyInstanceUID, %%m=manufacturer, %%n=name of patient, %%p=protocol,%s %%s=series number, %%t=time, %%u=acquisition number, %%v=vendor, %%x=study ID; %%z sequence name; default '%s')\n", kQstr, opts.filename);
printf(" -g : generate defaults file (y/n, default n)\n");
printf(" -h : show help\n");
printf(" -i : ignore derived, localizer and 2D images (y/n, default n)\n");
printf(" -m : merge 2D slices from same series regardless of study time, echo, coil, orientation, etc. (y/n, default n)\n");
Expand Down Expand Up @@ -200,6 +201,7 @@ int checkUpToDate() {
int main(int argc, const char * argv[])
{
struct TDCMopts opts;
bool isSaveIni = false;
readIniFile(&opts, argv); //set default preferences
#ifdef mydebugtest
//strcpy(opts.indir, "/Users/rorden/desktop/sliceOrder/dicom2/Philips_PARREC_Rotation/NoRotation/DBIEX_4_1.PAR");
Expand Down Expand Up @@ -257,8 +259,16 @@ int main(int argc, const char * argv[])
if (invalidParam(i, argv)) return 0;
if ((argv[i][0] == 'n') || (argv[i][0] == 'N') || (argv[i][0] == '0'))
opts.isSortDTIbyBVal = false;
else
else {
opts.isSortDTIbyBVal = true;
printf("Warning: sorting by b-value is deprecated: do not do this before undistortion.");
printf(" https://www.nitrc.org/forum/message.php?msg_id=22867");
}
} else if ((argv[i][1] == 'g') && ((i+1) < argc)) {
i++;
if (invalidParam(i, argv)) return 0;
if ((argv[i][0] == 'y') || (argv[i][0] == 'Y'))
isSaveIni = true;
} else if ((argv[i][1] == 'i') && ((i+1) < argc)) {
i++;
if (invalidParam(i, argv)) return 0;
Expand Down Expand Up @@ -383,6 +393,7 @@ int main(int argc, const char * argv[])
#else
printf ("Conversion required %f seconds.\n",((float)(clock()-start))/CLOCKS_PER_SEC);
#endif
saveIniFile(opts);
if (isSaveIni)
saveIniFile(opts);
return EXIT_SUCCESS;
}
32 changes: 29 additions & 3 deletions console/nii_dicom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,32 @@ int dcmInt (int lByteLength, unsigned char lBuffer[], bool littleEndian) { //rea
return lBuffer[3]+(lBuffer[2]<<8)+(lBuffer[1]<<16)+(lBuffer[0]<<24); //shortint vs word?
} //dcmInt()

int dcmStrInt (const int lByteLength, const unsigned char lBuffer[]) {//read float stored as a string
/*
//the code below trims strings after integer
// does not appear required not http://en.cppreference.com/w/cpp/string/byte/atoi
// "(atoi) Discards any whitespace characters until the first non-whitespace character is found, then takes as many characters as possible to form a valid integer"
int dcmStrInt (const int lByteLength, const unsigned char lBuffer[]) {//read int stored as a string
//returns first integer e.g. if 0043,1039 is "1000\8\0\0" the result will be 1000
if (lByteLength < 1) return 0; //error
bool isOK = false;
int i = 0;
for (i = 0; i <= lByteLength; i++) {
if ((lBuffer[i] >= '0') && (lBuffer[i] <= '9'))
isOK = true;
else if (isOK)
break;
}
if (!isOK) return 0; //error
char * cString = (char *)malloc(sizeof(char) * (i + 1));
cString[i] =0;
memcpy(cString, (const unsigned char*)(&lBuffer[0]), i);
int ret = atoi(cString);
free(cString);
return ret;
} //dcmStrInt()
*/

int dcmStrInt (const int lByteLength, const unsigned char lBuffer[]) {//read int stored as a string
//#ifdef _MSC_VER
char * cString = (char *)malloc(sizeof(char) * (lByteLength + 1));
//#else
Expand Down Expand Up @@ -2812,9 +2837,9 @@ void set_directionality0018_9075(struct TVolumeDiffusion* ptvd, unsigned char* i

void set_bValGE(struct TVolumeDiffusion* ptvd, int lLength, unsigned char* inbuf) {
//int dcmStrInt (int lByteLength, unsigned char lBuffer[]) {//read float stored as a string
dcmStrInt(lLength, inbuf);

ptvd->_dtiV[0] = dcmStrInt(lLength, inbuf);
if (ptvd->_dtiV[0] > 10000)
printWarning("GE B-Value implausible. see https://github.com/rordenlab/dcm2niix/issues/149\n");
//dd.CSA.numDti = 1; // Always true for GE.

_update_tvd(ptvd);
Expand Down Expand Up @@ -3538,6 +3563,7 @@ uint32_t kUnnest2 = 0xFFFE +(0xE0DD << 16 ); //#define kUnnest2 0xFFFE +(0xE0DD
break;
case kSeriesNum:
d.seriesNum = dcmStrInt(lLength, &buffer[lPos]);

break;
case kAcquNum:
d.acquNum = dcmStrInt(lLength, &buffer[lPos]);
Expand Down
48 changes: 35 additions & 13 deletions console/nii_dicom_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,9 @@ int * nii_SaveDTI(char pathoutname[],int nConvert, struct TDCMsort dcmSort[],str
free(vx);
return NULL;
}
//report values:
//for (int i = 1; i < numDti; i++) //check if all bvalues match first volume
// printMessage("%d bval= %g bvec= %g %g %g\n",i, vx[i].V[0], vx[i].V[1], vx[i].V[2], vx[i].V[3]);
int minB0idx = 0;
float minB0 = vx[0].V[0];
for (int i = 0; i < numDti; i++)
Expand Down Expand Up @@ -2481,7 +2484,7 @@ int saveDcm2Nii(int nConvert, struct TDCMsort dcmSort[],struct TDICOMdata dcmLis
for(int i = 0; i < nConvert; ++i)
dcmList[dcmSort[i].indx].converted2NII = 1;
if (opts.numSeries < 0) { //report series number but do not convert
printMessage("\t%d\t%s\n", dcmList[dcmSort[0].indx].seriesNum, pathoutname);
printMessage("\t%ld\t%s\n", dcmList[dcmSort[0].indx].seriesNum, pathoutname);
printMessage(" %s\n",nameList->str[dcmSort[0].indx]);
return EXIT_SUCCESS;
}
Expand Down Expand Up @@ -3151,12 +3154,21 @@ void readFindPigz (struct TDCMopts *opts, const char * argv[]) {
#if defined(_WIN64) || defined(_WIN32)
strcpy(opts->pigzname,"pigz.exe");
if (!is_exe(opts->pigzname)) {
#if defined(__APPLE__)
#ifdef myDisableZLib
printMessage("Compression requires %s in the same folder as the executable http://macappstore.org/pigz/\n",opts->pigzname);
#else //myUseZLib
printMessage("Compression will be faster with %s in the same folder as the executable http://macappstore.org/pigz/\n",opts->pigzname);
#endif
strcpy(opts->pigzname,"");
#else
#ifdef myDisableZLib
printMessage("Compression requires %s in the same folder as the executable\n",opts->pigzname);
#else //myUseZLib
printMessage("Compression will be faster with %s in the same folder as the executable\n",opts->pigzname);
#endif
strcpy(opts->pigzname,"");
#endif
} else
strcpy(opts->pigzname,".\\pigz"); //drop
#else
Expand Down Expand Up @@ -3203,10 +3215,18 @@ void readFindPigz (struct TDCMopts *opts, const char * argv[]) {
goto pigzFound;
} //n
//Failure:
#ifdef myDisableZLib
#if defined(__APPLE__)
#ifdef myDisableZLib
printMessage("Compression requires 'pigz' to be installed http://macappstore.org/pigz/\n");
#else //myUseZLib
printMessage("Compression will be faster with 'pigz' installed http://macappstore.org/pigz/\n");
#endif
#else //if APPLE else ...
#ifdef myDisableZLib
printMessage("Compression requires 'pigz' to be installed\n");
#else //myUseZLib
#else //myUseZLib
printMessage("Compression will be faster with 'pigz' installed\n");
#endif
#endif
return;
pigzFound: //Success
Expand Down Expand Up @@ -3264,17 +3284,18 @@ void setDefaultOpts (struct TDCMopts *opts, const char * argv[]) { //either "set
#if defined(_WIN64) || defined(_WIN32)
//windows has unusual file permissions for many users - lets save preferences to the registry
void saveIniFile (struct TDCMopts opts) {
HKEY hKey;
if(RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\dcm2nii",0, KEY_SET_VALUE, &hKey) != ERROR_SUCCESS) {
HKEY hKey;
if(RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\dcm2nii",0, KEY_SET_VALUE, &hKey) != ERROR_SUCCESS) {
RegCloseKey(hKey);
return;
}
printMessage("Saving defaults to registry\n");
DWORD dwValue = opts.isGz;
//RegSetValueEx(hKey,"isGZ", 0, REG_DWORD,reinterpret_cast<BYTE *>(&dwValue),sizeof(dwValue));
//RegSetValueExA(hKey, "isGZ", 0, REG_DWORD, (LPDWORD)&dwValue, sizeof(dwValue));
RegSetValueExA(hKey, "isGZ", 0, REG_DWORD, reinterpret_cast<BYTE *>(&dwValue), sizeof(dwValue));
RegSetValueExA(hKey,"filename",0, REG_SZ,(LPBYTE)opts.filename, strlen(opts.filename)+1);
RegCloseKey(hKey);
return;
}
DWORD dwValue = opts.isGz;
//RegSetValueEx(hKey,"isGZ", 0, REG_DWORD,reinterpret_cast<BYTE *>(&dwValue),sizeof(dwValue));
//RegSetValueExA(hKey, "isGZ", 0, REG_DWORD, (LPDWORD)&dwValue, sizeof(dwValue));
RegSetValueExA(hKey, "isGZ", 0, REG_DWORD, reinterpret_cast<BYTE *>(&dwValue), sizeof(dwValue));
RegSetValueExA(hKey,"filename",0, REG_SZ,(LPBYTE)opts.filename, strlen(opts.filename)+1);
RegCloseKey(hKey);
} //saveIniFile()

void readIniFile (struct TDCMopts *opts, const char * argv[]) {
Expand Down Expand Up @@ -3328,6 +3349,7 @@ void saveIniFile (struct TDCMopts opts) {
FILE *fp = fopen(opts.optsname, "w");
//printMessage("%s\n",localfilename);
if (fp == NULL) return;
printMessage("Saving defaults file %s\n", opts.optsname);
fprintf(fp, "isGZ=%d\n", opts.isGz);
fprintf(fp, "isBIDS=%d\n", opts.isCreateBIDS);
fprintf(fp, "filename=%s\n", opts.filename);
Expand Down

0 comments on commit 8ea093f

Please sign in to comment.