Skip to content

Commit 217e5e5

Browse files
author
Jaroslaw Kubik
committed
Add a context-aware progress monitor pointer
The progress_callback field in the ETEXT_DESC monitor type does not take any 'context' parameter, which may make implementing callback functions difficult and may require use of global variables. The new function receives the ETEXT_DESC pointer as an argument. This makes it possible to share the cancel_this field as context carrier if required. The change is backwards-compatible: the old pointer remains as a member of the class, and the default value for the new pointer is a function calling the classic progress notifier. This way the code unaware of the new member will continue to work as before.
1 parent 7bc8255 commit 217e5e5

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/ccmain/control.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,15 @@ bool Tesseract::RecogAllWordsPassN(int pass_n, ETEXT_DESC* monitor,
224224
monitor->ocr_alive = TRUE;
225225
if (pass_n == 1) {
226226
monitor->progress = 70 * w / words->size();
227-
if (monitor->progress_callback != nullptr) {
227+
if (monitor->progress_callback2 != nullptr) {
228228
TBOX box = pr_it->word()->word->bounding_box();
229-
(*monitor->progress_callback)(monitor->progress, box.left(),
229+
(*monitor->progress_callback2)(monitor, box.left(),
230230
box.right(), box.top(), box.bottom());
231231
}
232232
} else {
233233
monitor->progress = 70 + 30 * w / words->size();
234-
if (monitor->progress_callback != nullptr) {
235-
(*monitor->progress_callback)(monitor->progress, 0, 0, 0, 0);
234+
if (monitor->progress_callback2 != nullptr) {
235+
(*monitor->progress_callback2)(monitor, 0, 0, 0, 0);
236236
}
237237
}
238238
if (monitor->deadline_exceeded() ||

src/ccutil/ocrclass.h

+16
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,13 @@ typedef struct { /*single character */
108108
* If the cancel function is not null then it is called with the number of
109109
* user words found. If it returns true then operation is cancelled.
110110
**********************************************************************/
111+
class ETEXT_DESC;
112+
111113
typedef bool (*CANCEL_FUNC)(void* cancel_this, int words);
112114
typedef bool (*PROGRESS_FUNC)(int progress, int left, int right, int top,
113115
int bottom);
116+
typedef bool (*PROGRESS_FUNC2)(ETEXT_DESC* ths, int left, int right, int top,
117+
int bottom);
114118

115119
class ETEXT_DESC { // output header
116120
public:
@@ -124,6 +128,7 @@ class ETEXT_DESC { // output header
124128
int8_t err_code; /// for errcode use
125129
CANCEL_FUNC cancel; /// returns true to cancel
126130
PROGRESS_FUNC progress_callback; /// called whenever progress increases
131+
PROGRESS_FUNC2 progress_callback2;/// monitor-aware progress callback
127132
void* cancel_this; /// this or other data for cancel
128133
struct timeval end_time; /// Time to stop. Expected to be set only
129134
/// by call to set_deadline_msecs().
@@ -137,6 +142,7 @@ class ETEXT_DESC { // output header
137142
err_code(0),
138143
cancel(nullptr),
139144
progress_callback(nullptr),
145+
progress_callback2( &default_progress_func ),
140146
cancel_this(nullptr) {
141147
end_time.tv_sec = 0;
142148
end_time.tv_usec = 0;
@@ -162,6 +168,16 @@ class ETEXT_DESC { // output header
162168
return (now.tv_sec > end_time.tv_sec || (now.tv_sec == end_time.tv_sec &&
163169
now.tv_usec > end_time.tv_usec));
164170
}
171+
172+
private:
173+
static bool default_progress_func(ETEXT_DESC* ths, int left, int right, int top,
174+
int bottom)
175+
{
176+
if ( ths->progress_callback ) {
177+
(*(ths->progress_callback))(ths->progress, left, right, top, bottom);
178+
}
179+
}
180+
165181
};
166182

167183
#endif // CCUTIL_OCRCLASS_H_

0 commit comments

Comments
 (0)