Coverage Report

Created: 2025-07-12 06:44

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