Skip to content

Commit

Permalink
Added check if input PSM value is outside of range (#1236)
Browse files Browse the repository at this point in the history
Wrote a function to throw an error if PSM is outside 0-13 or OEM is outside 0-5.
fixes #1234
  • Loading branch information
jsreid13 authored and zdenop committed Dec 14, 2017
1 parent eba0ae3 commit cdc3533
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions api/tesseractmain.cpp
Expand Up @@ -238,6 +238,13 @@ void FixPageSegMode(tesseract::TessBaseAPI* api,
api->SetPageSegMode(pagesegmode);
}

void checkArgValues (int arg, const char* mode, int count) {
if (arg >= count || arg < 0) {
printf("Invalid %s value, please enter a number between 0-%d", mode, count - 1);
exit(0);
}
}

// NOTE: arg_i is used here to avoid ugly *i so many times in this function
void ParseArgs(const int argc, char** argv, const char** lang,
const char** image, const char** outputbase,
Expand Down Expand Up @@ -293,12 +300,15 @@ void ParseArgs(const int argc, char** argv, const char** lang,
} else if (strcmp(argv[i], "-psm") == 0 && i + 1 < argc) {
// The parameter -psm is deprecated and was replaced by --psm.
// It is still supported for compatibility reasons.
checkArgValues(atoi(argv[i+1]), "PSM", tesseract::PSM_COUNT);
*pagesegmode = static_cast<tesseract::PageSegMode>(atoi(argv[i + 1]));
++i;
} else if (strcmp(argv[i], "--psm") == 0 && i + 1 < argc) {
checkArgValues(atoi(argv[i+1]), "PSM", tesseract::PSM_COUNT);
*pagesegmode = static_cast<tesseract::PageSegMode>(atoi(argv[i + 1]));
++i;
} else if (strcmp(argv[i], "--oem") == 0 && i + 1 < argc) {
checkArgValues(atoi(argv[i+1]), "OEM", tesseract::OEM_COUNT);
*enginemode = static_cast<tesseract::OcrEngineMode>(atoi(argv[i + 1]));
++i;
} else if (strcmp(argv[i], "--print-parameters") == 0) {
Expand Down
1 change: 1 addition & 0 deletions ccstruct/publictypes.h
Expand Up @@ -279,6 +279,7 @@ enum OcrEngineMode {
// default OEM_TESSERACT_ONLY.
OEM_CUBE_ONLY, // Run Cube only - better accuracy, but slower
OEM_TESSERACT_CUBE_COMBINED, // Run both and combine results - best accuracy
OEM_COUNT // Number of OEMs
};

} // namespace tesseract.
Expand Down

0 comments on commit cdc3533

Please sign in to comment.