ocrclass.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**********************************************************************
  2. * File: ocrclass.h
  3. * Description: Class definitions and constants for the OCR API.
  4. * Author: Hewlett-Packard Co
  5. *
  6. * (C) Copyright 1996, Hewlett-Packard Co.
  7. ** Licensed under the Apache License, Version 2.0 (the "License");
  8. ** you may not use this file except in compliance with the License.
  9. ** You may obtain a copy of the License at
  10. ** http://www.apache.org/licenses/LICENSE-2.0
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. *
  17. **********************************************************************/
  18. /**********************************************************************
  19. * This file contains typedefs for all the structures used by
  20. * the HP OCR interface.
  21. * The structures are designed to allow them to be used with any
  22. * structure alignment up to 8.
  23. **********************************************************************/
  24. #ifndef CCUTIL_OCRCLASS_H_
  25. #define CCUTIL_OCRCLASS_H_
  26. #include <chrono>
  27. #include <ctime>
  28. #ifdef _WIN32
  29. #include <winsock2.h> // for timeval
  30. #endif
  31. /**********************************************************************
  32. * EANYCODE_CHAR
  33. * Description of a single character. The character code is defined by
  34. * the character set of the current font.
  35. * Output text is sent as an array of these structures.
  36. * Spaces and line endings in the output are represented in the
  37. * structures of the surrounding characters. They are not directly
  38. * represented as characters.
  39. * The first character in a word has a positive value of blanks.
  40. * Missing information should be set to the defaults in the comments.
  41. * If word bounds are known, but not character bounds, then the top and
  42. * bottom of each character should be those of the word. The left of the
  43. * first and right of the last char in each word should be set. All other
  44. * lefts and rights should be set to -1.
  45. * If set, the values of right and bottom are left+width and top+height.
  46. * Most of the members come directly from the parameters to ocr_append_char.
  47. * The formatting member uses the enhancement parameter and combines the
  48. * line direction stuff into the top 3 bits.
  49. * The coding is 0=RL char, 1=LR char, 2=DR NL, 3=UL NL, 4=DR Para,
  50. * 5=UL Para, 6=TB char, 7=BT char. API users do not need to know what
  51. * the coding is, only that it is backwards compatible with the previous
  52. * version.
  53. **********************************************************************/
  54. typedef struct { /*single character */
  55. // It should be noted that the format for char_code for version 2.0 and beyond
  56. // is UTF8 which means that ASCII characters will come out as one structure
  57. // but other characters will be returned in two or more instances of this
  58. // structure with a single byte of the UTF8 code in each, but each will have
  59. // the same bounding box. Programs which want to handle languagues with
  60. // different characters sets will need to handle extended characters
  61. // appropriately, but *all* code needs to be prepared to receive UTF8 coded
  62. // characters for characters such as bullet and fancy quotes.
  63. uint16_t char_code; /*character itself */
  64. int16_t left; /*of char (-1) */
  65. int16_t right; /*of char (-1) */
  66. int16_t top; /*of char (-1) */
  67. int16_t bottom; /*of char (-1) */
  68. int16_t font_index; /*what font (0) */
  69. uint8_t confidence; /*0=perfect, 100=reject (0/100) */
  70. uint8_t point_size; /*of char, 72=i inch, (10) */
  71. int8_t blanks; /*no of spaces before this char (1) */
  72. uint8_t formatting; /*char formatting (0) */
  73. } EANYCODE_CHAR; /*single character */
  74. /**********************************************************************
  75. * ETEXT_DESC
  76. * Description of the output of the OCR engine.
  77. * This structure is used as both a progress monitor and the final
  78. * output header, since it needs to be a valid progress monitor while
  79. * the OCR engine is storing its output to shared memory.
  80. * During progress, all the buffer info is -1.
  81. * Progress starts at 0 and increases to 100 during OCR. No other constraint.
  82. * Additionally the progress callback contains the bounding box of the word that
  83. * is currently being processed.
  84. * Every progress callback, the OCR engine must set ocr_alive to 1.
  85. * The HP side will set ocr_alive to 0. Repeated failure to reset
  86. * to 1 indicates that the OCR engine is dead.
  87. * If the cancel function is not null then it is called with the number of
  88. * user words found. If it returns true then operation is cancelled.
  89. **********************************************************************/
  90. class ETEXT_DESC;
  91. using CANCEL_FUNC = bool (*)(void*, int);
  92. using PROGRESS_FUNC = bool (*)(int, int, int, int, int);
  93. using PROGRESS_FUNC2 = bool (*)(ETEXT_DESC*, int, int, int, int);
  94. class ETEXT_DESC { // output header
  95. public:
  96. int16_t count{0}; /// chars in this buffer(0)
  97. int16_t progress{0}; /// percent complete increasing (0-100)
  98. /** Progress monitor covers word recognition and it does not cover layout
  99. * analysis.
  100. * See Ray comment in https://github.com/tesseract-ocr/tesseract/pull/27 */
  101. int8_t more_to_come{0}; /// true if not last
  102. volatile int8_t ocr_alive{0}; /// ocr sets to 1, HP 0
  103. int8_t err_code{0}; /// for errcode use
  104. CANCEL_FUNC cancel{nullptr}; /// returns true to cancel
  105. PROGRESS_FUNC progress_callback{
  106. nullptr}; /// called whenever progress increases
  107. PROGRESS_FUNC2 progress_callback2; /// monitor-aware progress callback
  108. void* cancel_this{nullptr}; /// this or other data for cancel
  109. struct timeval end_time;
  110. /// Time to stop. Expected to be set only
  111. /// by call to set_deadline_msecs().
  112. EANYCODE_CHAR text[1]{}; /// character data
  113. ETEXT_DESC() : progress_callback2(&default_progress_func) {
  114. auto chrono_end_time = std::chrono::time_point<std::chrono::steady_clock,
  115. std::chrono::milliseconds>();
  116. timePointToTimeval(chrono_end_time, &end_time);
  117. }
  118. // Sets the end time to be deadline_msecs milliseconds from now.
  119. void set_deadline_msecs(int32_t deadline_msecs) {
  120. if (deadline_msecs > 0) {
  121. auto chrono_end_time = std::chrono::steady_clock::now() +
  122. std::chrono::milliseconds(deadline_msecs);
  123. timePointToTimeval(chrono_end_time, &end_time);
  124. }
  125. }
  126. // Returns false if we've not passed the end_time, or have not set a deadline.
  127. bool deadline_exceeded() const {
  128. if (end_time.tv_sec == 0 && end_time.tv_usec == 0)
  129. return false;
  130. auto chrono_now = std::chrono::steady_clock::now();
  131. struct timeval now;
  132. timePointToTimeval(chrono_now, &now);
  133. return (now.tv_sec > end_time.tv_sec ||
  134. (now.tv_sec == end_time.tv_sec && now.tv_usec > end_time.tv_usec));
  135. }
  136. private:
  137. static void timePointToTimeval(
  138. std::chrono::steady_clock::time_point chrono_point, struct timeval* tv) {
  139. auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(
  140. chrono_point.time_since_epoch());
  141. tv->tv_sec = millisecs.count() / 1000;
  142. tv->tv_usec = (millisecs.count() % 1000) * 1000;
  143. }
  144. static bool default_progress_func(ETEXT_DESC* ths, int left, int right,
  145. int top, int bottom) {
  146. if (ths->progress_callback != nullptr) {
  147. return (*(ths->progress_callback))(ths->progress, left, right, top,
  148. bottom);
  149. }
  150. return true;
  151. }
  152. };
  153. #endif // CCUTIL_OCRCLASS_H_