tesseract  5.0.0-alpha-619-ge9db
drawtord.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: drawtord.cpp (Formerly drawto.c)
3  * Description: Draw things to do with textord.
4  * Author: Ray Smith
5  *
6  * (C) Copyright 1992, Hewlett-Packard Ltd.
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 #ifdef HAVE_CONFIG_H
20 #include "config_auto.h"
21 #endif
22 
23 #include "pithsync.h"
24 #include "topitch.h"
25 #include "drawtord.h"
26 
27 #define TO_WIN_XPOS 0 //default window pos
28 #define TO_WIN_YPOS 0
29 #define TO_WIN_NAME "Textord"
30  //title of window
31 
33 "Draw fixed pitch cell boundaries");
34 
35 ScrollView* to_win = nullptr;
36 
37 /**********************************************************************
38  * create_to_win
39  *
40  * Create the to window used to show the fit.
41  **********************************************************************/
42 #ifndef GRAPHICS_DISABLED
43 
45  if (to_win != nullptr) return to_win;
47  page_tr.x() + 1, page_tr.y() + 1,
48  page_tr.x(), page_tr.y(), true);
49  return to_win;
50 }
51 
52 
53 void close_to_win() {
54  // to_win is leaked, but this enables the user to view the contents.
55  if (to_win != nullptr) {
56  to_win->Update();
57  }
58 }
59 
60 
61 /**********************************************************************
62  * plot_box_list
63  *
64  * Draw a list of blobs.
65  **********************************************************************/
66 
67 void plot_box_list( //make gradients win
68  ScrollView* win, //window to draw in
69  BLOBNBOX_LIST *list, //blob list
70  ScrollView::Color body_colour //colour to draw
71  ) {
72  BLOBNBOX_IT it = list; //iterator
73 
74  win->Pen(body_colour);
75  win->Brush(ScrollView::NONE);
76  for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
77  it.data ()->bounding_box ().plot (win);
78  }
79 }
80 
81 
82 /**********************************************************************
83  * plot_to_row
84  *
85  * Draw the blobs of a row in a given colour and draw the line fit.
86  **********************************************************************/
87 
88 void plot_to_row( //draw a row
89  TO_ROW *row, //row to draw
90  ScrollView::Color colour, //colour to draw in
91  FCOORD rotation //rotation for line
92  ) {
93  FCOORD plot_pt; //point to plot
94  //blobs
95  BLOBNBOX_IT it = row->blob_list ();
96  float left, right; //end of row
97 
98  if (it.empty ()) {
99  tprintf ("No blobs in row at %g\n", row->parallel_c ());
100  return;
101  }
102  left = it.data ()->bounding_box ().left ();
103  it.move_to_last ();
104  right = it.data ()->bounding_box ().right ();
105  plot_blob_list (to_win, row->blob_list (), colour, ScrollView::BROWN);
106  to_win->Pen(colour);
107  plot_pt = FCOORD (left, row->line_m () * left + row->line_c ());
108  plot_pt.rotate (rotation);
109  to_win->SetCursor(plot_pt.x (), plot_pt.y ());
110  plot_pt = FCOORD (right, row->line_m () * right + row->line_c ());
111  plot_pt.rotate (rotation);
112  to_win->DrawTo(plot_pt.x (), plot_pt.y ());
113 }
114 
115 
116 /**********************************************************************
117  * plot_parallel_row
118  *
119  * Draw the blobs of a row in a given colour and draw the line fit.
120  **********************************************************************/
121 
122 void plot_parallel_row( //draw a row
123  TO_ROW *row, //row to draw
124  float gradient, //gradients of lines
125  int32_t left, //edge of block
126  ScrollView::Color colour, //colour to draw in
127  FCOORD rotation //rotation for line
128  ) {
129  FCOORD plot_pt; //point to plot
130  //blobs
131  BLOBNBOX_IT it = row->blob_list ();
132  auto fleft = static_cast<float>(left); //floating version
133  float right; //end of row
134 
135  // left=it.data()->bounding_box().left();
136  it.move_to_last ();
137  right = it.data ()->bounding_box ().right ();
138  plot_blob_list (to_win, row->blob_list (), colour, ScrollView::BROWN);
139  to_win->Pen(colour);
140  plot_pt = FCOORD (fleft, gradient * left + row->max_y ());
141  plot_pt.rotate (rotation);
142  to_win->SetCursor(plot_pt.x (), plot_pt.y ());
143  plot_pt = FCOORD (fleft, gradient * left + row->min_y ());
144  plot_pt.rotate (rotation);
145  to_win->DrawTo(plot_pt.x (), plot_pt.y ());
146  plot_pt = FCOORD (fleft, gradient * left + row->parallel_c ());
147  plot_pt.rotate (rotation);
148  to_win->SetCursor(plot_pt.x (), plot_pt.y ());
149  plot_pt = FCOORD (right, gradient * right + row->parallel_c ());
150  plot_pt.rotate (rotation);
151  to_win->DrawTo(plot_pt.x (), plot_pt.y ());
152 }
153 
154 
155 /**********************************************************************
156  * draw_occupation
157  *
158  * Draw the row occupation with points above the threshold in white
159  * and points below the threshold in black.
160  **********************************************************************/
161 
162 void
163 draw_occupation ( //draw projection
164 int32_t xleft, //edge of block
165 int32_t ybottom, //bottom of block
166 int32_t min_y, //coordinate limits
167 int32_t max_y, int32_t occupation[], //projection counts
168 int32_t thresholds[] //for drop out
169 ) {
170  int32_t line_index; //pixel coord
171  ScrollView::Color colour; //of histogram
172  auto fleft = static_cast<float>(xleft); //float version
173 
174  colour = ScrollView::WHITE;
175  to_win->Pen(colour);
176  to_win->SetCursor(fleft, static_cast<float>(ybottom));
177  for (line_index = min_y; line_index <= max_y; line_index++) {
178  if (occupation[line_index - min_y] < thresholds[line_index - min_y]) {
179  if (colour != ScrollView::BLUE) {
180  colour = ScrollView::BLUE;
181  to_win->Pen(colour);
182  }
183  }
184  else {
185  if (colour != ScrollView::WHITE) {
186  colour = ScrollView::WHITE;
187  to_win->Pen(colour);
188  }
189  }
190  to_win->DrawTo(fleft + occupation[line_index - min_y] / 10.0, static_cast<float>(line_index));
191  }
192  colour=ScrollView::STEEL_BLUE;
193  to_win->Pen(colour);
194  to_win->SetCursor(fleft, static_cast<float>(ybottom));
195  for (line_index = min_y; line_index <= max_y; line_index++) {
196  to_win->DrawTo(fleft + thresholds[line_index - min_y] / 10.0, static_cast<float>(line_index));
197  }
198 }
199 
200 
201 /**********************************************************************
202  * draw_meanlines
203  *
204  * Draw the meanlines of the given block in the given colour.
205  **********************************************************************/
206 
207 void draw_meanlines( //draw a block
208  TO_BLOCK *block, //block to draw
209  float gradient, //gradients of lines
210  int32_t left, //edge of block
211  ScrollView::Color colour, //colour to draw in
212  FCOORD rotation //rotation for line
213  ) {
214  FCOORD plot_pt; //point to plot
215  //rows
216  TO_ROW_IT row_it = block->get_rows ();
217  TO_ROW *row; //current row
218  BLOBNBOX_IT blob_it; //blobs
219  float right; //end of row
220  to_win->Pen(colour);
221  for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
222  row = row_it.data ();
223  blob_it.set_to_list (row->blob_list ());
224  blob_it.move_to_last ();
225  right = blob_it.data ()->bounding_box ().right ();
226  plot_pt =
227  FCOORD (static_cast<float>(left),
228  gradient * left + row->parallel_c () + row->xheight);
229  plot_pt.rotate (rotation);
230  to_win->SetCursor(plot_pt.x (), plot_pt.y ());
231  plot_pt =
232  FCOORD (right,
233  gradient * right + row->parallel_c () + row->xheight);
234  plot_pt.rotate (rotation);
235  to_win->DrawTo (plot_pt.x (), plot_pt.y ());
236  }
237 }
238 
239 
240 /**********************************************************************
241  * plot_word_decisions
242  *
243  * Plot a row with words in different colours and fuzzy spaces
244  * highlighted.
245  **********************************************************************/
246 
247 void plot_word_decisions( //draw words
248  ScrollView* win, //window tro draw in
249  int16_t pitch, //of block
250  TO_ROW *row //row to draw
251  ) {
252  ScrollView::Color colour = ScrollView::MAGENTA; //current colour
253  ScrollView::Color rect_colour; //fuzzy colour
254  int32_t prev_x; //end of prev blob
255  int16_t blob_count; //blobs in word
256  BLOBNBOX *blob; //current blob
257  TBOX blob_box; //bounding box
258  //iterator
259  BLOBNBOX_IT blob_it = row->blob_list ();
260  BLOBNBOX_IT start_it = blob_it;//word start
261 
262  rect_colour = ScrollView::BLACK;
263  prev_x = -INT16_MAX;
264  blob_count = 0;
265  for (blob_it.mark_cycle_pt (); !blob_it.cycled_list (); blob_it.forward ()) {
266  blob = blob_it.data ();
267  blob_box = blob->bounding_box ();
268  if (!blob->joined_to_prev ()
269  && blob_box.left () - prev_x > row->max_nonspace) {
270  if ((blob_box.left () - prev_x >= row->min_space
271  || blob_box.left () - prev_x > row->space_threshold)
272  && blob_count > 0) {
273  if (pitch > 0 && textord_show_fixed_cuts)
274  plot_fp_cells (win, colour, &start_it, pitch, blob_count,
275  &row->projection, row->projection_left,
276  row->projection_right,
278  blob_count = 0;
279  start_it = blob_it;
280  }
281  if (colour == ScrollView::MAGENTA)
282  colour = ScrollView::RED;
283  else
284  colour = static_cast<ScrollView::Color>(colour + 1);
285  if (blob_box.left () - prev_x < row->min_space) {
286  if (blob_box.left () - prev_x > row->space_threshold)
287  rect_colour = ScrollView::GOLDENROD;
288  else
289  rect_colour = ScrollView::CORAL;
290  //fill_color_index(win, rect_colour);
291  win->Brush(rect_colour);
292  win->Rectangle (prev_x, blob_box.bottom (),
293  blob_box.left (), blob_box.top ());
294  }
295  }
296  if (!blob->joined_to_prev())
297  prev_x = blob_box.right();
298  if (blob->cblob () != nullptr)
299  blob->cblob ()->plot (win, colour, colour);
300  if (!blob->joined_to_prev() && blob->cblob() != nullptr)
301  blob_count++;
302  }
303  if (pitch > 0 && textord_show_fixed_cuts && blob_count > 0)
304  plot_fp_cells (win, colour, &start_it, pitch, blob_count,
305  &row->projection, row->projection_left,
306  row->projection_right,
308 }
309 
310 
311 /**********************************************************************
312  * plot_fp_cells
313  *
314  * Make a list of fixed pitch cuts and draw them.
315  **********************************************************************/
316 
317 void plot_fp_cells( //draw words
318  ScrollView* win, //window tro draw in
319  ScrollView::Color colour, //colour of lines
320  BLOBNBOX_IT *blob_it, //blobs
321  int16_t pitch, //of block
322  int16_t blob_count, //no of real blobs
323  STATS *projection, //vertical
324  int16_t projection_left, //edges //scale factor
325  int16_t projection_right,
326  float projection_scale) {
327  int16_t occupation; //occupied cells
328  TBOX word_box; //bounding box
329  FPSEGPT_LIST seg_list; //list of cuts
330  FPSEGPT_IT seg_it;
331  FPSEGPT *segpt; //current point
332 
333  if (pitsync_linear_version)
334  check_pitch_sync2 (blob_it, blob_count, pitch, 2, projection,
335  projection_left, projection_right,
336  projection_scale, occupation, &seg_list, 0, 0);
337  else
338  check_pitch_sync (blob_it, blob_count, pitch, 2, projection, &seg_list);
339  word_box = blob_it->data ()->bounding_box ();
340  for (; blob_count > 0; blob_count--)
341  word_box += box_next (blob_it);
342  seg_it.set_to_list (&seg_list);
343  for (seg_it.mark_cycle_pt (); !seg_it.cycled_list (); seg_it.forward ()) {
344  segpt = seg_it.data ();
345  if (segpt->faked) {
346  colour = ScrollView::WHITE;
347  win->Pen(colour); }
348  else {
349  win->Pen(colour); }
350  win->Line(segpt->position (), word_box.bottom (),segpt->position (), word_box.top ());
351  }
352 }
353 
354 
355 /**********************************************************************
356  * plot_fp_cells2
357  *
358  * Make a list of fixed pitch cuts and draw them.
359  **********************************************************************/
360 
361 void plot_fp_cells2( //draw words
362  ScrollView* win, //window tro draw in
363  ScrollView::Color colour, //colour of lines
364  TO_ROW *row, //for location
365  FPSEGPT_LIST *seg_list //segments to plot
366  ) {
367  TBOX word_box; //bounding box
368  FPSEGPT_IT seg_it = seg_list;
369  //blobs in row
370  BLOBNBOX_IT blob_it = row->blob_list ();
371  FPSEGPT *segpt; //current point
372 
373  word_box = blob_it.data ()->bounding_box ();
374  for (blob_it.mark_cycle_pt (); !blob_it.cycled_list ();)
375  word_box += box_next (&blob_it);
376  for (seg_it.mark_cycle_pt (); !seg_it.cycled_list (); seg_it.forward ()) {
377  segpt = seg_it.data ();
378  if (segpt->faked) {
379  colour = ScrollView::WHITE;
380  win->Pen(colour); }
381  else {
382  win->Pen(colour); }
383  win->Line(segpt->position (), word_box.bottom (),segpt->position (), word_box.top ());
384  }
385 }
386 
387 
388 /**********************************************************************
389  * plot_row_cells
390  *
391  * Make a list of fixed pitch cuts and draw them.
392  **********************************************************************/
393 
394 void plot_row_cells( //draw words
395  ScrollView* win, //window tro draw in
396  ScrollView::Color colour, //colour of lines
397  TO_ROW *row, //for location
398  float xshift, //amount of shift
399  ICOORDELT_LIST *cells //cells to draw
400  ) {
401  TBOX word_box; //bounding box
402  ICOORDELT_IT cell_it = cells;
403  //blobs in row
404  BLOBNBOX_IT blob_it = row->blob_list ();
405  ICOORDELT *cell; //current cell
406 
407  word_box = blob_it.data ()->bounding_box ();
408  for (blob_it.mark_cycle_pt (); !blob_it.cycled_list ();)
409  word_box += box_next (&blob_it);
410  win->Pen(colour);
411  for (cell_it.mark_cycle_pt (); !cell_it.cycled_list (); cell_it.forward ()) {
412  cell = cell_it.data ();
413  win->Line(cell->x () + xshift, word_box.bottom (), cell->x () + xshift, word_box.top ());
414  }
415 }
416 
417 #endif // GRAPHICS_DISABLED
TO_ROW::min_space
int32_t min_space
Definition: blobbox.h:662
ScrollView
Definition: scrollview.h:97
ScrollView::Brush
void Brush(Color color)
Definition: scrollview.cpp:723
check_pitch_sync2
double check_pitch_sync2(BLOBNBOX_IT *blob_it, int16_t blob_count, int16_t pitch, int16_t pitch_error, STATS *projection, int16_t projection_left, int16_t projection_right, float projection_scale, int16_t &occupation_count, FPSEGPT_LIST *seg_list, int16_t start, int16_t end)
Definition: pithsync.cpp:286
pithsync.h
textord_projection_scale
double textord_projection_scale
Definition: topitch.cpp:51
TO_WIN_NAME
#define TO_WIN_NAME
Definition: drawtord.cpp:28
create_to_win
ScrollView * create_to_win(ICOORD page_tr)
Definition: drawtord.cpp:42
FPSEGPT::faked
bool faked
Definition: pitsync1.h:66
plot_word_decisions
void plot_word_decisions(ScrollView *win, int16_t pitch, TO_ROW *row)
Definition: drawtord.cpp:239
plot_fp_cells2
void plot_fp_cells2(ScrollView *win, ScrollView::Color colour, TO_ROW *row, FPSEGPT_LIST *seg_list)
Definition: drawtord.cpp:351
FCOORD::y
float y() const
Definition: points.h:209
ICOORD
integer coordinate
Definition: points.h:30
TO_ROW::line_m
float line_m() const
Definition: blobbox.h:570
FCOORD::x
float x() const
Definition: points.h:206
plot_fp_cells
void plot_fp_cells(ScrollView *win, ScrollView::Color colour, BLOBNBOX_IT *blob_it, int16_t pitch, int16_t blob_count, STATS *projection, int16_t projection_left, int16_t projection_right, float projection_scale)
Definition: drawtord.cpp:308
TO_ROW::max_y
float max_y() const
Definition: blobbox.h:558
TO_ROW::projection_left
int16_t projection_left
Definition: blobbox.h:647
TBOX::top
int16_t top() const
Definition: rect.h:57
ScrollView::STEEL_BLUE
Definition: scrollview.h:118
TO_ROW::projection_right
int16_t projection_right
Definition: blobbox.h:648
TO_BLOCK
Definition: blobbox.h:691
textord_show_fixed_cuts
bool textord_show_fixed_cuts
Definition: drawtord.cpp:32
ScrollView::BROWN
Definition: scrollview.h:120
TO_WIN_XPOS
#define TO_WIN_XPOS
Definition: drawtord.cpp:26
ScrollView::NONE
Definition: scrollview.h:101
ScrollView::Pen
void Pen(Color color)
Definition: scrollview.cpp:717
ScrollView::DrawTo
void DrawTo(int x, int y)
Definition: scrollview.cpp:524
plot_parallel_row
void plot_parallel_row(TO_ROW *row, float gradient, int32_t left, ScrollView::Color colour, FCOORD rotation)
Definition: drawtord.cpp:117
plot_row_cells
void plot_row_cells(ScrollView *win, ScrollView::Color colour, TO_ROW *row, float xshift, ICOORDELT_LIST *cells)
Definition: drawtord.cpp:383
FPSEGPT::position
int32_t position()
Definition: pitsync1.h:47
ICOORD::x
int16_t x() const
access function
Definition: points.h:51
FCOORD
Definition: points.h:187
BLOBNBOX
Definition: blobbox.h:142
ScrollView::BLUE
Definition: scrollview.h:108
check_pitch_sync
double check_pitch_sync(BLOBNBOX_IT *blob_it, int16_t blob_count, int16_t pitch, int16_t pitch_error, STATS *projection, FPSEGPT_LIST *seg_list)
Definition: pitsync1.cpp:138
plot_to_row
void plot_to_row(TO_ROW *row, ScrollView::Color colour, FCOORD rotation)
Definition: drawtord.cpp:84
TO_WIN_YPOS
#define TO_WIN_YPOS
Definition: drawtord.cpp:27
topitch.h
ScrollView::BLACK
Definition: scrollview.h:102
close_to_win
void close_to_win()
Definition: drawtord.cpp:51
TO_ROW::projection
STATS projection
Definition: blobbox.h:670
ScrollView::MAGENTA
Definition: scrollview.h:109
BLOBNBOX::joined_to_prev
bool joined_to_prev() const
Definition: blobbox.h:255
TO_ROW::parallel_c
float parallel_c() const
Definition: blobbox.h:579
BOOL_VAR
#define BOOL_VAR(name, val, comment)
Definition: params.h:303
TBOX::bottom
int16_t bottom() const
Definition: rect.h:64
ScrollView::WHITE
Definition: scrollview.h:103
TO_ROW::xheight
float xheight
Definition: blobbox.h:656
draw_meanlines
void draw_meanlines(TO_BLOCK *block, float gradient, int32_t left, ScrollView::Color colour, FCOORD rotation)
Definition: drawtord.cpp:200
FCOORD::rotate
void rotate(const FCOORD vec)
Definition: points.h:736
TO_ROW::line_c
float line_c() const
Definition: blobbox.h:573
ScrollView::RED
Definition: scrollview.h:104
STATS
Definition: statistc.h:30
BLOBNBOX::bounding_box
const TBOX & bounding_box() const
Definition: blobbox.h:229
drawtord.h
TO_BLOCK::get_rows
TO_ROW_LIST * get_rows()
Definition: blobbox.h:703
TO_ROW::space_threshold
int32_t space_threshold
Definition: blobbox.h:664
TO_ROW::max_nonspace
int32_t max_nonspace
Definition: blobbox.h:663
box_next
TBOX box_next(BLOBNBOX_IT *it)
Definition: blobbox.cpp:629
ScrollView::GOLDENROD
Definition: scrollview.h:123
TBOX::left
int16_t left() const
Definition: rect.h:71
TO_ROW::min_y
float min_y() const
Definition: blobbox.h:561
TBOX::right
int16_t right() const
Definition: rect.h:78
ScrollView::Line
void Line(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:531
plot_box_list
void plot_box_list(ScrollView *win, BLOBNBOX_LIST *list, ScrollView::Color body_colour)
Definition: drawtord.cpp:64
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
TO_ROW
Definition: blobbox.h:543
ScrollView::Update
static void Update()
Definition: scrollview.cpp:708
ScrollView::SetCursor
void SetCursor(int x, int y)
Definition: scrollview.cpp:518
FPSEGPT
Definition: pitsync1.h:31
ScrollView::CORAL
Definition: scrollview.h:119
ScrollView::Color
Color
Definition: scrollview.h:100
BLOBNBOX::cblob
C_BLOB * cblob() const
Definition: blobbox.h:267
ICOORDELT
Definition: points.h:160
ScrollView::Rectangle
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:599
to_win
ScrollView * to_win
Definition: drawtord.cpp:34
C_BLOB::plot
void plot(ScrollView *window, ScrollView::Color blob_colour, ScrollView::Color child_colour)
Definition: stepblob.cpp:523
plot_blob_list
void plot_blob_list(ScrollView *win, BLOBNBOX_LIST *list, ScrollView::Color body_colour, ScrollView::Color child_colour)
Definition: blobbox.cpp:1068
TO_ROW::blob_list
BLOBNBOX_LIST * blob_list()
Definition: blobbox.h:599
ICOORD::y
int16_t y() const
access_function
Definition: points.h:55
draw_occupation
void draw_occupation(int32_t xleft, int32_t ybottom, int32_t min_y, int32_t max_y, int32_t occupation[], int32_t thresholds[])
Definition: drawtord.cpp:157
TBOX
Definition: rect.h:33