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