Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code for generating config files #4074

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions include/tesseract/baseapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,19 @@ class TESS_API TessBaseAPI {

#endif

/**
* Print Tesseract parameters to the given file.
/**
* Print Tesseract parameters to the given file with descriptions of each option.
* Cannot be used as Tesseract configuration file due to descriptions
* (use DumpVariables instead to create config files).
*/
void PrintVariables(FILE *fp) const;

/**
* Print Tesseract parameters to the given file without descriptions.
* Can be used as Tesseract configuration file.
*/
void DumpVariables(FILE *fp) const;

/**
* Get value of named variable as a string, if it exists.
*/
Expand Down
4 changes: 4 additions & 0 deletions include/tesseract/capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ TESS_API void TessBaseAPIPrintVariables(const TessBaseAPI *handle, FILE *fp);
TESS_API BOOL TessBaseAPIPrintVariablesToFile(const TessBaseAPI *handle,
const char *filename);

TESS_API void TessBaseAPIDumpVariables(const TessBaseAPI *handle, FILE *fp);
TESS_API BOOL TessBaseAPIDumpVariablesToFile(const TessBaseAPI *handle,
const char *filename);

TESS_API int TessBaseAPIInit1(TessBaseAPI *handle, const char *datapath,
const char *language, TessOcrEngineMode oem,
char **configs, int configs_size);
Expand Down
18 changes: 15 additions & 3 deletions src/api/baseapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,21 @@ void TessBaseAPI::PrintFontsTable(FILE *fp) const {

#endif

/** Print Tesseract parameters to the given file. */
/**
* Print Tesseract parameters to the given file with descriptions of each option.
* Cannot be used as Tesseract configuration file due to descriptions
* (use DumpVariables instead to create config files).
*/
void TessBaseAPI::PrintVariables(FILE *fp) const {
ParamUtils::PrintParams(fp, tesseract_->params());
ParamUtils::PrintParams(fp, tesseract_->params(), true);
}

/**
* Print Tesseract parameters to the given file without descriptions.
* Can be used as Tesseract configuration file.
*/
void TessBaseAPI::DumpVariables(FILE *fp) const {
ParamUtils::PrintParams(fp, tesseract_->params(), false);
}

/**
Expand Down Expand Up @@ -1295,7 +1307,7 @@ bool TessBaseAPI::ProcessPage(Pix *pix, int page_index, const char *filename,
if (fp == nullptr) {
tprintf("Error, failed to open file \"%s\"\n", kOldVarsFile);
} else {
PrintVariables(fp);
DumpVariables(fp);
fclose(fp);
}
// Switch to alternate mode for retry.
Expand Down
14 changes: 14 additions & 0 deletions src/api/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ BOOL TessBaseAPIPrintVariablesToFile(const TessBaseAPI *handle, const char *file
return FALSE;
}

void TessBaseAPIDumpVariables(const TessBaseAPI *handle, FILE *fp) {
handle->DumpVariables(fp);
}

BOOL TessBaseAPIDumpVariablesToFile(const TessBaseAPI *handle, const char *filename) {
FILE *fp = fopen(filename, "w");
if (fp != nullptr) {
handle->DumpVariables(fp);
fclose(fp);
return TRUE;
}
return FALSE;
}

int TessBaseAPIInit4(TessBaseAPI *handle, const char *datapath, const char *language,
TessOcrEngineMode mode, char **configs, int configs_size, char **vars_vec,
char **vars_values, size_t vars_vec_size, BOOL set_only_non_debug_params) {
Expand Down
2 changes: 1 addition & 1 deletion src/ccmain/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ bool Tesseract::ProcessTargetWord(const TBOX &word_box, const TBOX &target_word_
if (config_fp == nullptr) {
tprintf("Error, failed to open file \"%s\"\n", backup_config_file_);
} else {
ParamUtils::PrintParams(config_fp, params());
ParamUtils::PrintParams(config_fp, params(), false);
fclose(config_fp);
}
ParamUtils::ReadParamsFile(word_config, SET_PARAM_CONSTRAINT_DEBUG_ONLY, params());
Expand Down
34 changes: 25 additions & 9 deletions src/ccutil/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,27 +161,43 @@ bool ParamUtils::GetParamAsString(const char *name, const ParamsVectors *member_
return false;
}

void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params) {
void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params, bool print_info) {
int num_iterations = (member_params == nullptr) ? 1 : 2;
std::ostringstream stream;
stream.imbue(std::locale::classic());
for (int v = 0; v < num_iterations; ++v) {
const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
for (auto int_param : vec->int_params) {
stream << int_param->name_str() << '\t' << (int32_t)(*int_param) << '\t'
<< int_param->info_str() << '\n';
if (print_info) {
stream << int_param->name_str() << '\t' << (int32_t)(*int_param) << '\t'
<< int_param->info_str() << '\n';
} else {
stream << int_param->name_str() << '\t' << (int32_t)(*int_param) << '\n';
}
}
for (auto bool_param : vec->bool_params) {
stream << bool_param->name_str() << '\t' << bool(*bool_param) << '\t'
<< bool_param->info_str() << '\n';
if (print_info) {
stream << bool_param->name_str() << '\t' << bool(*bool_param) << '\t'
<< bool_param->info_str() << '\n';
} else {
stream << bool_param->name_str() << '\t' << bool(*bool_param) << '\n';
}
}
for (auto string_param : vec->string_params) {
stream << string_param->name_str() << '\t' << string_param->c_str() << '\t'
<< string_param->info_str() << '\n';
if (print_info) {
stream << string_param->name_str() << '\t' << string_param->c_str() << '\t'
<< string_param->info_str() << '\n';
} else {
stream << string_param->name_str() << '\t' << string_param->c_str() << '\n';
}
}
for (auto double_param : vec->double_params) {
stream << double_param->name_str() << '\t' << (double)(*double_param) << '\t'
<< double_param->info_str() << '\n';
if (print_info) {
stream << double_param->name_str() << '\t' << (double)(*double_param) << '\t'
<< double_param->info_str() << '\n';
} else {
stream << double_param->name_str() << '\t' << (double)(*double_param) << '\n';
}
}
}
fprintf(fp, "%s", stream.str().c_str());
Expand Down
2 changes: 1 addition & 1 deletion src/ccutil/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class TESS_API ParamUtils {
std::string *value);

// Print parameters to the given file.
static void PrintParams(FILE *fp, const ParamsVectors *member_params);
static void PrintParams(FILE *fp, const ParamsVectors *member_params, bool print_info = true);

// Resets all parameters back to default values;
static void ResetToDefaults(ParamsVectors *member_params);
Expand Down