mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2025-06-07 18:02:40 +08:00
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:
parent
7bc8255e04
commit
217e5e5881
@ -224,15 +224,15 @@ bool Tesseract::RecogAllWordsPassN(int pass_n, ETEXT_DESC* monitor,
|
|||||||
monitor->ocr_alive = TRUE;
|
monitor->ocr_alive = TRUE;
|
||||||
if (pass_n == 1) {
|
if (pass_n == 1) {
|
||||||
monitor->progress = 70 * w / words->size();
|
monitor->progress = 70 * w / words->size();
|
||||||
if (monitor->progress_callback != nullptr) {
|
if (monitor->progress_callback2 != nullptr) {
|
||||||
TBOX box = pr_it->word()->word->bounding_box();
|
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());
|
box.right(), box.top(), box.bottom());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
monitor->progress = 70 + 30 * w / words->size();
|
monitor->progress = 70 + 30 * w / words->size();
|
||||||
if (monitor->progress_callback != nullptr) {
|
if (monitor->progress_callback2 != nullptr) {
|
||||||
(*monitor->progress_callback)(monitor->progress, 0, 0, 0, 0);
|
(*monitor->progress_callback2)(monitor, 0, 0, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (monitor->deadline_exceeded() ||
|
if (monitor->deadline_exceeded() ||
|
||||||
|
@ -108,9 +108,13 @@ typedef struct { /*single character */
|
|||||||
* If the cancel function is not null then it is called with the number of
|
* 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.
|
* 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 (*CANCEL_FUNC)(void* cancel_this, int words);
|
||||||
typedef bool (*PROGRESS_FUNC)(int progress, int left, int right, int top,
|
typedef bool (*PROGRESS_FUNC)(int progress, int left, int right, int top,
|
||||||
int bottom);
|
int bottom);
|
||||||
|
typedef bool (*PROGRESS_FUNC2)(ETEXT_DESC* ths, int left, int right, int top,
|
||||||
|
int bottom);
|
||||||
|
|
||||||
class ETEXT_DESC { // output header
|
class ETEXT_DESC { // output header
|
||||||
public:
|
public:
|
||||||
@ -124,6 +128,7 @@ class ETEXT_DESC { // output header
|
|||||||
int8_t err_code; /// for errcode use
|
int8_t err_code; /// for errcode use
|
||||||
CANCEL_FUNC cancel; /// returns true to cancel
|
CANCEL_FUNC cancel; /// returns true to cancel
|
||||||
PROGRESS_FUNC progress_callback; /// called whenever progress increases
|
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
|
void* cancel_this; /// this or other data for cancel
|
||||||
struct timeval end_time; /// Time to stop. Expected to be set only
|
struct timeval end_time; /// Time to stop. Expected to be set only
|
||||||
/// by call to set_deadline_msecs().
|
/// by call to set_deadline_msecs().
|
||||||
@ -137,6 +142,7 @@ class ETEXT_DESC { // output header
|
|||||||
err_code(0),
|
err_code(0),
|
||||||
cancel(nullptr),
|
cancel(nullptr),
|
||||||
progress_callback(nullptr),
|
progress_callback(nullptr),
|
||||||
|
progress_callback2( &default_progress_func ),
|
||||||
cancel_this(nullptr) {
|
cancel_this(nullptr) {
|
||||||
end_time.tv_sec = 0;
|
end_time.tv_sec = 0;
|
||||||
end_time.tv_usec = 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 &&
|
return (now.tv_sec > end_time.tv_sec || (now.tv_sec == end_time.tv_sec &&
|
||||||
now.tv_usec > end_time.tv_usec));
|
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_
|
#endif // CCUTIL_OCRCLASS_H_
|
||||||
|
Loading…
Reference in New Issue
Block a user