-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Expand file tree
/
Copy pathparamsd.cpp
More file actions
348 lines (309 loc) · 11.2 KB
/
paramsd.cpp
File metadata and controls
348 lines (309 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
///////////////////////////////////////////////////////////////////////
// File: paramsd.cpp
// Description: Tesseract parameter Editor
// Author: Joern Wanke
//
// (C) Copyright 2007, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////
//
// The parameters editor is used to edit all the parameters used within
// tesseract from the ui.
// Include automatically generated configuration file if running autoconf.
#ifdef HAVE_CONFIG_H
# include "config_auto.h"
#endif
#ifndef GRAPHICS_DISABLED
# include "params.h" // for ParamsVectors, StringParam, BoolParam
# include "paramsd.h"
# include "scrollview.h" // for SVEvent, ScrollView, SVET_POPUP
# include "svmnode.h" // for SVMenuNode
# include "tesseractclass.h" // for Tesseract
# include <cstdio> // for fclose, fopen, fprintf, FILE
# include <cstdlib> // for atoi
# include <cstring> // for strcmp, strcspn, strlen, strncpy
# include <locale> // for std::locale::classic
# include <map> // for map, _Rb_tree_iterator, map<>::iterator
# include <memory> // for unique_ptr
# include <sstream> // for std::stringstream
# include <utility> // for pair
namespace tesseract {
# define VARDIR "configs/" /*parameters files */
# define MAX_ITEMS_IN_SUBMENU 30
// The following variables should remain static globals, since they
// are used by debug editor, which uses a single Tesseract instance.
//
// Contains the mappings from unique VC ids to their actual pointers.
static std::map<int, ParamContent *> vcMap;
static int nrParams = 0;
static int writeCommands[2];
// Constructors for the various ParamTypes.
ParamContent::ParamContent(tesseract::StringParam *it) {
my_id_ = nrParams;
nrParams++;
param_type_ = VT_STRING;
sIt = it;
vcMap[my_id_] = this;
}
// Constructors for the various ParamTypes.
ParamContent::ParamContent(tesseract::IntParam *it) {
my_id_ = nrParams;
nrParams++;
param_type_ = VT_INTEGER;
iIt = it;
vcMap[my_id_] = this;
}
// Constructors for the various ParamTypes.
ParamContent::ParamContent(tesseract::BoolParam *it) {
my_id_ = nrParams;
nrParams++;
param_type_ = VT_BOOLEAN;
bIt = it;
vcMap[my_id_] = this;
}
// Constructors for the various ParamTypes.
ParamContent::ParamContent(tesseract::DoubleParam *it) {
my_id_ = nrParams;
nrParams++;
param_type_ = VT_DOUBLE;
dIt = it;
vcMap[my_id_] = this;
}
// Gets a VC object identified by its ID.
ParamContent *ParamContent::GetParamContentById(int id) {
return vcMap[id];
}
// Copy the first N words from the source string to the target string.
// Words are delimited by "_".
void ParamsEditor::GetFirstWords(const char *s, // source string
int n, // number of words
char *t // target string
) {
int full_length = strlen(s);
int reqd_len = 0; // No. of chars required
const char *next_word = s;
while ((n > 0) && reqd_len < full_length) {
reqd_len += strcspn(next_word, "_") + 1;
next_word += reqd_len;
n--;
}
strncpy(t, s, reqd_len);
t[reqd_len] = '\0'; // ensure null terminal
}
// Getter for the name.
const char *ParamContent::GetName() const {
if (param_type_ == VT_INTEGER) {
return iIt->name_str();
} else if (param_type_ == VT_BOOLEAN) {
return bIt->name_str();
} else if (param_type_ == VT_DOUBLE) {
return dIt->name_str();
} else if (param_type_ == VT_STRING) {
return sIt->name_str();
} else {
return "ERROR: ParamContent::GetName()";
}
}
// Getter for the description.
const char *ParamContent::GetDescription() const {
if (param_type_ == VT_INTEGER) {
return iIt->info_str();
} else if (param_type_ == VT_BOOLEAN) {
return bIt->info_str();
} else if (param_type_ == VT_DOUBLE) {
return dIt->info_str();
} else if (param_type_ == VT_STRING) {
return sIt->info_str();
} else {
return nullptr;
}
}
// Getter for the value.
std::string ParamContent::GetValue() const {
std::string result;
if (param_type_ == VT_INTEGER) {
result += std::to_string(*iIt);
} else if (param_type_ == VT_BOOLEAN) {
result += std::to_string(*bIt);
} else if (param_type_ == VT_DOUBLE) {
result += std::to_string(*dIt);
} else if (param_type_ == VT_STRING) {
result = sIt->c_str();
}
return result;
}
// Setter for the value.
void ParamContent::SetValue(const char *val) {
// TODO (wanke) Test if the values actually are properly converted.
// (Quickly visible impacts?)
changed_ = true;
if (param_type_ == VT_INTEGER) {
iIt->set_value(atoi(val));
} else if (param_type_ == VT_BOOLEAN) {
bIt->set_value(atoi(val));
} else if (param_type_ == VT_DOUBLE) {
std::stringstream stream(val);
// Use "C" locale for reading double value.
stream.imbue(std::locale::classic());
double d = 0;
stream >> d;
dIt->set_value(d);
} else if (param_type_ == VT_STRING) {
sIt->set_value(val);
}
}
// Gets the up to the first 3 prefixes from s (split by _).
// For example, tesseract_foo_bar will be split into tesseract,foo and bar.
void ParamsEditor::GetPrefixes(const char *s, std::string *level_one, std::string *level_two,
std::string *level_three) {
std::unique_ptr<char[]> p(new char[1024]);
GetFirstWords(s, 1, p.get());
*level_one = p.get();
GetFirstWords(s, 2, p.get());
*level_two = p.get();
GetFirstWords(s, 3, p.get());
*level_three = p.get();
}
// Compare two VC objects by their name.
int ParamContent::Compare(const ParamContent *one, const ParamContent *two) {
return strcmp(one->GetName(), two->GetName());
}
// Find all editable parameters used within tesseract and create a
// SVMenuNode tree from it.
// TODO (wanke): This is actually sort of hackish.
SVMenuNode *ParamsEditor::BuildListOfAllLeaves(tesseract::Tesseract *tess) {
auto *mr = new SVMenuNode();
ParamContent_LIST vclist;
ParamContent_IT vc_it(&vclist);
// Amount counts the number of entries for a specific char*.
// TODO(rays) get rid of the use of std::map.
std::map<const char *, int> amount;
// Add all parameters to a list.
int num_iterations = (tess->params() == nullptr) ? 1 : 2;
for (int v = 0; v < num_iterations; ++v) {
tesseract::ParamsVectors *vec = (v == 0) ? GlobalParams() : tess->params();
for (auto ¶m : vec->int_params) {
vc_it.add_after_then_move(new ParamContent(param));
}
for (auto ¶m : vec->bool_params) {
vc_it.add_after_then_move(new ParamContent(param));
}
for (auto ¶m : vec->string_params) {
vc_it.add_after_then_move(new ParamContent(param));
}
for (auto ¶m : vec->double_params) {
vc_it.add_after_then_move(new ParamContent(param));
}
}
// Count the # of entries starting with a specific prefix.
for (vc_it.mark_cycle_pt(); !vc_it.cycled_list(); vc_it.forward()) {
ParamContent *vc = vc_it.data();
std::string tag;
std::string tag2;
std::string tag3;
GetPrefixes(vc->GetName(), &tag, &tag2, &tag3);
amount[tag.c_str()]++;
amount[tag2.c_str()]++;
amount[tag3.c_str()]++;
}
vclist.sort(ParamContent::Compare); // Sort the list alphabetically.
SVMenuNode *other = mr->AddChild("OTHER");
// go through the list again and this time create the menu structure.
vc_it.move_to_first();
for (vc_it.mark_cycle_pt(); !vc_it.cycled_list(); vc_it.forward()) {
ParamContent *vc = vc_it.data();
std::string tag;
std::string tag2;
std::string tag3;
GetPrefixes(vc->GetName(), &tag, &tag2, &tag3);
if (amount[tag.c_str()] == 1) {
other->AddChild(vc->GetName(), vc->GetId(), vc->GetValue().c_str(), vc->GetDescription());
} else { // More than one would use this submenu -> create submenu.
SVMenuNode *sv = mr->AddChild(tag.c_str());
if ((amount[tag.c_str()] <= MAX_ITEMS_IN_SUBMENU) || (amount[tag2.c_str()] <= 1)) {
sv->AddChild(vc->GetName(), vc->GetId(), vc->GetValue().c_str(), vc->GetDescription());
} else { // Make subsubmenus.
SVMenuNode *sv2 = sv->AddChild(tag2.c_str());
sv2->AddChild(vc->GetName(), vc->GetId(), vc->GetValue().c_str(), vc->GetDescription());
}
}
}
return mr;
}
// Event listener. Waits for SVET_POPUP events and processes them.
void ParamsEditor::Notify(const SVEvent *sve) {
if (sve->type == SVET_POPUP) { // only catch SVET_POPUP!
char *param = sve->parameter;
if (sve->command_id == writeCommands[0]) {
WriteParams(param, false);
} else if (sve->command_id == writeCommands[1]) {
WriteParams(param, true);
} else {
ParamContent *vc = ParamContent::GetParamContentById(sve->command_id);
vc->SetValue(param);
sv_window_->AddMessageF("Setting %s to %s", vc->GetName(), vc->GetValue().c_str());
}
}
}
// Integrate the parameters editor as popupmenu into the existing scrollview
// window (usually the pg editor). If sv == null, create a new empty
// empty window and attach the parameters editor to that window (ugly).
ParamsEditor::ParamsEditor(tesseract::Tesseract *tess, ScrollView *sv) {
if (sv == nullptr) {
const char *name = "ParamEditorMAIN";
sv = new ScrollView(name, 1, 1, 200, 200, 300, 200);
}
sv_window_ = sv;
// Only one event handler per window.
// sv->AddEventHandler((SVEventHandler*) this);
SVMenuNode *svMenuRoot = BuildListOfAllLeaves(tess);
std::string paramfile;
paramfile = tess->datadir.string();
paramfile += VARDIR; // parameters dir
paramfile += "edited"; // actual name
SVMenuNode *std_menu = svMenuRoot->AddChild("Build Config File");
writeCommands[0] = nrParams + 1;
std_menu->AddChild("All Parameters", writeCommands[0], paramfile.c_str(), "Config file name?");
writeCommands[1] = nrParams + 2;
std_menu->AddChild("changed_ Parameters Only", writeCommands[1], paramfile.c_str(),
"Config file name?");
svMenuRoot->BuildMenu(sv, false);
}
// Write all (changed_) parameters to a config file.
void ParamsEditor::WriteParams(char *filename, bool changes_only) {
FILE *fp; // input file
// if file exists
if ((fp = fopen(filename, "rb")) != nullptr) {
fclose(fp);
std::stringstream msg;
msg << "Overwrite file " << filename << "? (Y/N)";
int a = sv_window_->ShowYesNoDialog(msg.str().c_str());
if (a == 'n') {
return;
} // don't write
}
fp = fopen(filename, "wb"); // can we write to it?
if (fp == nullptr) {
sv_window_->AddMessageF("Can't write to file %s", filename);
return;
}
for (auto &iter : vcMap) {
ParamContent *cur = iter.second;
if (!changes_only || cur->HasChanged()) {
fprintf(fp, "%-25s %-12s # %s\n", cur->GetName(), cur->GetValue().c_str(),
cur->GetDescription());
}
}
fclose(fp);
}
} // namespace tesseract
#endif // !GRAPHICS_DISABLED