forked from tesseract-ocr/tesseract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawtord.cpp
421 lines (374 loc) · 15.3 KB
/
drawtord.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/**********************************************************************
* File: drawtord.cpp (Formerly drawto.c)
* Description: Draw things to do with textord.
* Author: Ray Smith
* Created: Thu Jul 30 15:40:57 BST 1992
*
* (C) Copyright 1992, Hewlett-Packard Ltd.
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
** http://www.apache.org/licenses/LICENSE-2.0
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*
**********************************************************************/
#ifdef HAVE_CONFIG_H
#include "config_auto.h"
#endif
#include "pithsync.h"
#include "topitch.h"
#include "drawtord.h"
#define TO_WIN_XPOS 0 //default window pos
#define TO_WIN_YPOS 0
#define TO_WIN_NAME "Textord"
//title of window
#define EXTERN
EXTERN BOOL_VAR (textord_show_fixed_cuts, FALSE,
"Draw fixed pitch cell boundaries");
EXTERN ScrollView* to_win = NULL;
/**********************************************************************
* create_to_win
*
* Create the to window used to show the fit.
**********************************************************************/
#ifndef GRAPHICS_DISABLED
ScrollView* create_to_win(ICOORD page_tr) {
if (to_win != NULL) return to_win;
to_win = new ScrollView(TO_WIN_NAME, TO_WIN_XPOS, TO_WIN_YPOS,
page_tr.x() + 1, page_tr.y() + 1,
page_tr.x(), page_tr.y(), true);
return to_win;
}
void close_to_win() {
// to_win is leaked, but this enables the user to view the contents.
if (to_win != NULL) {
to_win->Update();
}
}
/**********************************************************************
* plot_box_list
*
* Draw a list of blobs.
**********************************************************************/
void plot_box_list( //make gradients win
ScrollView* win, //window to draw in
BLOBNBOX_LIST *list, //blob list
ScrollView::Color body_colour //colour to draw
) {
BLOBNBOX_IT it = list; //iterator
win->Pen(body_colour);
win->Brush(ScrollView::NONE);
for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
it.data ()->bounding_box ().plot (win);
}
}
/**********************************************************************
* plot_to_row
*
* Draw the blobs of a row in a given colour and draw the line fit.
**********************************************************************/
void plot_to_row( //draw a row
TO_ROW *row, //row to draw
ScrollView::Color colour, //colour to draw in
FCOORD rotation //rotation for line
) {
FCOORD plot_pt; //point to plot
//blobs
BLOBNBOX_IT it = row->blob_list ();
float left, right; //end of row
if (it.empty ()) {
tprintf ("No blobs in row at %g\n", row->parallel_c ());
return;
}
left = it.data ()->bounding_box ().left ();
it.move_to_last ();
right = it.data ()->bounding_box ().right ();
plot_blob_list (to_win, row->blob_list (), colour, ScrollView::BROWN);
to_win->Pen(colour);
plot_pt = FCOORD (left, row->line_m () * left + row->line_c ());
plot_pt.rotate (rotation);
to_win->SetCursor(plot_pt.x (), plot_pt.y ());
plot_pt = FCOORD (right, row->line_m () * right + row->line_c ());
plot_pt.rotate (rotation);
to_win->DrawTo(plot_pt.x (), plot_pt.y ());
}
/**********************************************************************
* plot_parallel_row
*
* Draw the blobs of a row in a given colour and draw the line fit.
**********************************************************************/
void plot_parallel_row( //draw a row
TO_ROW *row, //row to draw
float gradient, //gradients of lines
inT32 left, //edge of block
ScrollView::Color colour, //colour to draw in
FCOORD rotation //rotation for line
) {
FCOORD plot_pt; //point to plot
//blobs
BLOBNBOX_IT it = row->blob_list ();
float fleft = (float) left; //floating version
float right; //end of row
// left=it.data()->bounding_box().left();
it.move_to_last ();
right = it.data ()->bounding_box ().right ();
plot_blob_list (to_win, row->blob_list (), colour, ScrollView::BROWN);
to_win->Pen(colour);
plot_pt = FCOORD (fleft, gradient * left + row->max_y ());
plot_pt.rotate (rotation);
to_win->SetCursor(plot_pt.x (), plot_pt.y ());
plot_pt = FCOORD (fleft, gradient * left + row->min_y ());
plot_pt.rotate (rotation);
to_win->DrawTo(plot_pt.x (), plot_pt.y ());
plot_pt = FCOORD (fleft, gradient * left + row->parallel_c ());
plot_pt.rotate (rotation);
to_win->SetCursor(plot_pt.x (), plot_pt.y ());
plot_pt = FCOORD (right, gradient * right + row->parallel_c ());
plot_pt.rotate (rotation);
to_win->DrawTo(plot_pt.x (), plot_pt.y ());
}
/**********************************************************************
* draw_occupation
*
* Draw the row occupation with points above the threshold in white
* and points below the threshold in black.
**********************************************************************/
void
draw_occupation ( //draw projection
inT32 xleft, //edge of block
inT32 ybottom, //bottom of block
inT32 min_y, //coordinate limits
inT32 max_y, inT32 occupation[], //projection counts
inT32 thresholds[] //for drop out
) {
inT32 line_index; //pixel coord
ScrollView::Color colour; //of histogram
float fleft = (float) xleft; //float version
colour = ScrollView::WHITE;
to_win->Pen(colour);
to_win->SetCursor(fleft, (float) ybottom);
for (line_index = min_y; line_index <= max_y; line_index++) {
if (occupation[line_index - min_y] < thresholds[line_index - min_y]) {
if (colour != ScrollView::BLUE) {
colour = ScrollView::BLUE;
to_win->Pen(colour);
}
}
else {
if (colour != ScrollView::WHITE) {
colour = ScrollView::WHITE;
to_win->Pen(colour);
}
}
to_win->DrawTo(fleft + occupation[line_index - min_y] / 10.0, (float) line_index);
}
colour=ScrollView::STEEL_BLUE;
to_win->Pen(colour);
to_win->SetCursor(fleft, (float) ybottom);
for (line_index = min_y; line_index <= max_y; line_index++) {
to_win->DrawTo(fleft + thresholds[line_index - min_y] / 10.0, (float) line_index);
}
}
/**********************************************************************
* draw_meanlines
*
* Draw the meanlines of the given block in the given colour.
**********************************************************************/
void draw_meanlines( //draw a block
TO_BLOCK *block, //block to draw
float gradient, //gradients of lines
inT32 left, //edge of block
ScrollView::Color colour, //colour to draw in
FCOORD rotation //rotation for line
) {
FCOORD plot_pt; //point to plot
//rows
TO_ROW_IT row_it = block->get_rows ();
TO_ROW *row; //current row
BLOBNBOX_IT blob_it; //blobs
float right; //end of row
to_win->Pen(colour);
for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
row = row_it.data ();
blob_it.set_to_list (row->blob_list ());
blob_it.move_to_last ();
right = blob_it.data ()->bounding_box ().right ();
plot_pt =
FCOORD ((float) left,
gradient * left + row->parallel_c () + row->xheight);
plot_pt.rotate (rotation);
to_win->SetCursor(plot_pt.x (), plot_pt.y ());
plot_pt =
FCOORD ((float) right,
gradient * right + row->parallel_c () + row->xheight);
plot_pt.rotate (rotation);
to_win->DrawTo (plot_pt.x (), plot_pt.y ());
}
}
/**********************************************************************
* plot_word_decisions
*
* Plot a row with words in different colours and fuzzy spaces
* highlighted.
**********************************************************************/
void plot_word_decisions( //draw words
ScrollView* win, //window tro draw in
inT16 pitch, //of block
TO_ROW *row //row to draw
) {
ScrollView::Color colour = ScrollView::MAGENTA; //current colour
ScrollView::Color rect_colour; //fuzzy colour
inT32 prev_x; //end of prev blob
inT16 blob_count; //blobs in word
BLOBNBOX *blob; //current blob
TBOX blob_box; //bounding box
//iterator
BLOBNBOX_IT blob_it = row->blob_list ();
BLOBNBOX_IT start_it = blob_it;//word start
rect_colour = ScrollView::BLACK;
prev_x = -MAX_INT16;
blob_count = 0;
for (blob_it.mark_cycle_pt (); !blob_it.cycled_list (); blob_it.forward ()) {
blob = blob_it.data ();
blob_box = blob->bounding_box ();
if (!blob->joined_to_prev ()
&& blob_box.left () - prev_x > row->max_nonspace) {
if ((blob_box.left () - prev_x >= row->min_space
|| blob_box.left () - prev_x > row->space_threshold)
&& blob_count > 0) {
if (pitch > 0 && textord_show_fixed_cuts)
plot_fp_cells (win, colour, &start_it, pitch, blob_count,
&row->projection, row->projection_left,
row->projection_right,
row->xheight * textord_projection_scale);
blob_count = 0;
start_it = blob_it;
}
if (colour == ScrollView::MAGENTA)
colour = ScrollView::RED;
else
colour = (ScrollView::Color) (colour + 1);
if (blob_box.left () - prev_x < row->min_space) {
if (blob_box.left () - prev_x > row->space_threshold)
rect_colour = ScrollView::GOLDENROD;
else
rect_colour = ScrollView::CORAL;
//fill_color_index(win, rect_colour);
win->Brush(rect_colour);
win->Rectangle (prev_x, blob_box.bottom (),
blob_box.left (), blob_box.top ());
}
}
if (!blob->joined_to_prev())
prev_x = blob_box.right();
if (blob->cblob () != NULL)
blob->cblob ()->plot (win, colour, colour);
if (!blob->joined_to_prev() && blob->cblob() != NULL)
blob_count++;
}
if (pitch > 0 && textord_show_fixed_cuts && blob_count > 0)
plot_fp_cells (win, colour, &start_it, pitch, blob_count,
&row->projection, row->projection_left,
row->projection_right,
row->xheight * textord_projection_scale);
}
/**********************************************************************
* plot_fp_cells
*
* Make a list of fixed pitch cuts and draw them.
**********************************************************************/
void plot_fp_cells( //draw words
ScrollView* win, //window tro draw in
ScrollView::Color colour, //colour of lines
BLOBNBOX_IT *blob_it, //blobs
inT16 pitch, //of block
inT16 blob_count, //no of real blobs
STATS *projection, //vertical
inT16 projection_left, //edges //scale factor
inT16 projection_right,
float projection_scale) {
inT16 occupation; //occupied cells
TBOX word_box; //bounding box
FPSEGPT_LIST seg_list; //list of cuts
FPSEGPT_IT seg_it;
FPSEGPT *segpt; //current point
if (pitsync_linear_version)
check_pitch_sync2 (blob_it, blob_count, pitch, 2, projection,
projection_left, projection_right,
projection_scale, occupation, &seg_list, 0, 0);
else
check_pitch_sync (blob_it, blob_count, pitch, 2, projection, &seg_list);
word_box = blob_it->data ()->bounding_box ();
for (; blob_count > 0; blob_count--)
word_box += box_next (blob_it);
seg_it.set_to_list (&seg_list);
for (seg_it.mark_cycle_pt (); !seg_it.cycled_list (); seg_it.forward ()) {
segpt = seg_it.data ();
if (segpt->faked) {
colour = ScrollView::WHITE;
win->Pen(colour); }
else {
win->Pen(colour); }
win->Line(segpt->position (), word_box.bottom (),segpt->position (), word_box.top ());
}
}
/**********************************************************************
* plot_fp_cells2
*
* Make a list of fixed pitch cuts and draw them.
**********************************************************************/
void plot_fp_cells2( //draw words
ScrollView* win, //window tro draw in
ScrollView::Color colour, //colour of lines
TO_ROW *row, //for location
FPSEGPT_LIST *seg_list //segments to plot
) {
TBOX word_box; //bounding box
FPSEGPT_IT seg_it = seg_list;
//blobs in row
BLOBNBOX_IT blob_it = row->blob_list ();
FPSEGPT *segpt; //current point
word_box = blob_it.data ()->bounding_box ();
for (blob_it.mark_cycle_pt (); !blob_it.cycled_list ();)
word_box += box_next (&blob_it);
for (seg_it.mark_cycle_pt (); !seg_it.cycled_list (); seg_it.forward ()) {
segpt = seg_it.data ();
if (segpt->faked) {
colour = ScrollView::WHITE;
win->Pen(colour); }
else {
win->Pen(colour); }
win->Line(segpt->position (), word_box.bottom (),segpt->position (), word_box.top ());
}
}
/**********************************************************************
* plot_row_cells
*
* Make a list of fixed pitch cuts and draw them.
**********************************************************************/
void plot_row_cells( //draw words
ScrollView* win, //window tro draw in
ScrollView::Color colour, //colour of lines
TO_ROW *row, //for location
float xshift, //amount of shift
ICOORDELT_LIST *cells //cells to draw
) {
TBOX word_box; //bounding box
ICOORDELT_IT cell_it = cells;
//blobs in row
BLOBNBOX_IT blob_it = row->blob_list ();
ICOORDELT *cell; //current cell
word_box = blob_it.data ()->bounding_box ();
for (blob_it.mark_cycle_pt (); !blob_it.cycled_list ();)
word_box += box_next (&blob_it);
win->Pen(colour);
for (cell_it.mark_cycle_pt (); !cell_it.cycled_list (); cell_it.forward ()) {
cell = cell_it.data ();
win->Line(cell->x () + xshift, word_box.bottom (), cell->x () + xshift, word_box.top ());
}
}
#endif // GRAPHICS_DISABLED