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.
This commit is contained in:
Jaroslaw Kubik 2018-05-29 21:48:51 +02:00
parent 7bc8255e04
commit 217e5e5881
2 changed files with 20 additions and 4 deletions

View File

@ -224,15 +224,15 @@ bool Tesseract::RecogAllWordsPassN(int pass_n, ETEXT_DESC* monitor,
monitor->ocr_alive = TRUE;
if (pass_n == 1) {
monitor->progress = 70 * w / words->size();
if (monitor->progress_callback != nullptr) {
if (monitor->progress_callback2 != nullptr) {
TBOX box = pr_it->word()->word->bounding_box();
(*monitor->progress_callback)(monitor->progress, box.left(),
(*monitor->progress_callback2)(monitor, box.left(),
box.right(), box.top(), box.bottom());
}
} else {
monitor->progress = 70 + 30 * w / words->size();
if (monitor->progress_callback != nullptr) {
(*monitor->progress_callback)(monitor->progress, 0, 0, 0, 0);
if (monitor->progress_callback2 != nullptr) {
(*monitor->progress_callback2)(monitor, 0, 0, 0, 0);
}
}
if (monitor->deadline_exceeded() ||

View File

@ -108,9 +108,13 @@ typedef struct { /*single character */
* If the cancel function is not null then it is called with the number of
* user words found. If it returns true then operation is cancelled.
**********************************************************************/
class ETEXT_DESC;
typedef bool (*CANCEL_FUNC)(void* cancel_this, int words);
typedef bool (*PROGRESS_FUNC)(int progress, int left, int right, int top,
int bottom);
typedef bool (*PROGRESS_FUNC2)(ETEXT_DESC* ths, int left, int right, int top,
int bottom);
class ETEXT_DESC { // output header
public:
@ -124,6 +128,7 @@ class ETEXT_DESC { // output header
int8_t err_code; /// for errcode use
CANCEL_FUNC cancel; /// returns true to cancel
PROGRESS_FUNC progress_callback; /// called whenever progress increases
PROGRESS_FUNC2 progress_callback2;/// monitor-aware progress callback
void* cancel_this; /// this or other data for cancel
struct timeval end_time; /// Time to stop. Expected to be set only
/// by call to set_deadline_msecs().
@ -137,6 +142,7 @@ class ETEXT_DESC { // output header
err_code(0),
cancel(nullptr),
progress_callback(nullptr),
progress_callback2( &default_progress_func ),
cancel_this(nullptr) {
end_time.tv_sec = 0;
end_time.tv_usec = 0;
@ -162,6 +168,16 @@ class ETEXT_DESC { // output header
return (now.tv_sec > end_time.tv_sec || (now.tv_sec == end_time.tv_sec &&
now.tv_usec > end_time.tv_usec));
}
private:
static bool default_progress_func(ETEXT_DESC* ths, int left, int right, int top,
int bottom)
{
if ( ths->progress_callback ) {
(*(ths->progress_callback))(ths->progress, left, right, top, bottom);
}
}
};
#endif // CCUTIL_OCRCLASS_H_