tesseract  4.00.00dev
scanedg.cpp File Reference
#include "scanedg.h"
#include <memory>
#include "allheaders.h"
#include "edgloop.h"

Go to the source code of this file.

Macros

#define WHITE_PIX   1 /*thresholded colours */
 
#define BLACK_PIX   0
 
#define FLIP_COLOUR(pix)   (1-(pix))
 

Functions

void block_edges (Pix *t_pix, PDBLK *block, C_OUTLINE_IT *outline_it)
 
void make_margins (PDBLK *block, BLOCK_LINE_IT *line_it, uinT8 *pixels, uinT8 margin, inT16 left, inT16 right, inT16 y)
 
void line_edges (inT16 x, inT16 y, inT16 xext, uinT8 uppercolour, uinT8 *bwpos, CRACKEDGE **prevline, CRACKEDGE **free_cracks, C_OUTLINE_IT *outline_it)
 
CRACKEDGEh_edge (int sign, CRACKEDGE *join, CrackPos *pos)
 
CRACKEDGEv_edge (int sign, CRACKEDGE *join, CrackPos *pos)
 
void join_edges (CRACKEDGE *edge1, CRACKEDGE *edge2, CRACKEDGE **free_cracks, C_OUTLINE_IT *outline_it)
 
void free_crackedges (CRACKEDGE *start)
 

Macro Definition Documentation

◆ BLACK_PIX

#define BLACK_PIX   0

Definition at line 28 of file scanedg.cpp.

◆ FLIP_COLOUR

#define FLIP_COLOUR (   pix)    (1-(pix))

Definition at line 30 of file scanedg.cpp.

◆ WHITE_PIX

#define WHITE_PIX   1 /*thresholded colours */

Definition at line 27 of file scanedg.cpp.

Function Documentation

◆ block_edges()

void block_edges ( Pix *  t_pix,
PDBLK block,
C_OUTLINE_IT *  outline_it 
)

Definition at line 38 of file scanedg.cpp.

40  {
41  ICOORD bleft; // bounding box
42  ICOORD tright;
43  BLOCK_LINE_IT line_it = block; // line iterator
44 
45  int width = pixGetWidth(t_pix);
46  int height = pixGetHeight(t_pix);
47  int wpl = pixGetWpl(t_pix);
48  // lines in progress
49  CRACKEDGE **ptrline = new CRACKEDGE*[width + 1];
50  CRACKEDGE *free_cracks = NULL;
51 
52  block->bounding_box(bleft, tright); // block box
53  int block_width = tright.x() - bleft.x();
54  for (int x = block_width; x >= 0; x--)
55  ptrline[x] = NULL; // no lines in progress
56 
57  uinT8* bwline = new uinT8[width];
58 
59  uinT8 margin = WHITE_PIX;
60 
61  for (int y = tright.y() - 1; y >= bleft.y() - 1; y--) {
62  if (y >= bleft.y() && y < tright.y()) {
63  // Get the binary pixels from the image.
64  l_uint32* line = pixGetData(t_pix) + wpl * (height - 1 - y);
65  for (int x = 0; x < block_width; ++x) {
66  bwline[x] = GET_DATA_BIT(line, x + bleft.x()) ^ 1;
67  }
68  make_margins(block, &line_it, bwline, margin, bleft.x(), tright.x(), y);
69  } else {
70  memset(bwline, margin, block_width * sizeof(bwline[0]));
71  }
72  line_edges(bleft.x(), y, block_width,
73  margin, bwline, ptrline, &free_cracks, outline_it);
74  }
75 
76  free_crackedges(free_cracks); // really free them
77  delete[] ptrline;
78  delete[] bwline;
79 }
void line_edges(inT16 x, inT16 y, inT16 xext, uinT8 uppercolour, uinT8 *bwpos, CRACKEDGE **prevline, CRACKEDGE **free_cracks, C_OUTLINE_IT *outline_it)
Definition: scanedg.cpp:144
rectangle iterator
Definition: pdblock.h:144
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
Definition: pdblock.h:59
inT16 x() const
access function
Definition: points.h:52
inT16 y() const
access_function
Definition: points.h:56
#define WHITE_PIX
Definition: scanedg.cpp:27
void make_margins(PDBLK *block, BLOCK_LINE_IT *line_it, uinT8 *pixels, uinT8 margin, inT16 left, inT16 right, inT16 y)
Definition: scanedg.cpp:88
void free_crackedges(CRACKEDGE *start)
Definition: scanedg.cpp:364
uint8_t uinT8
Definition: host.h:35
integer coordinate
Definition: points.h:30

◆ free_crackedges()

void free_crackedges ( CRACKEDGE start)

Definition at line 364 of file scanedg.cpp.

364  {
365  CRACKEDGE *current; // current edge to free
366  CRACKEDGE *next; // next one to free
367 
368  for (current = start; current != NULL; current = next) {
369  next = current->next;
370  delete current; // delete them all
371  }
372 }
CRACKEDGE * next
Definition: crakedge.h:35

◆ h_edge()

CRACKEDGE* h_edge ( int  sign,
CRACKEDGE join,
CrackPos pos 
)

Definition at line 228 of file scanedg.cpp.

230  {
231  CRACKEDGE *newpt; // return value
232 
233  if (*pos->free_cracks != NULL) {
234  newpt = *pos->free_cracks;
235  *pos->free_cracks = newpt->next; // get one fast
236  } else {
237  newpt = new CRACKEDGE;
238  }
239  newpt->pos.set_y(pos->y + 1); // coords of pt
240  newpt->stepy = 0; // edge is horizontal
241 
242  if (sign > 0) {
243  newpt->pos.set_x(pos->x + 1); // start location
244  newpt->stepx = -1;
245  newpt->stepdir = 0;
246  } else {
247  newpt->pos.set_x(pos->x); // start location
248  newpt->stepx = 1;
249  newpt->stepdir = 2;
250  }
251 
252  if (join == NULL) {
253  newpt->next = newpt; // ptrs to other ends
254  newpt->prev = newpt;
255  } else {
256  if (newpt->pos.x() + newpt->stepx == join->pos.x()
257  && newpt->pos.y() == join->pos.y()) {
258  newpt->prev = join->prev; // update other ends
259  newpt->prev->next = newpt;
260  newpt->next = join; // join up
261  join->prev = newpt;
262  } else {
263  newpt->next = join->next; // update other ends
264  newpt->next->prev = newpt;
265  newpt->prev = join; // join up
266  join->next = newpt;
267  }
268  }
269  return newpt;
270 }
CRACKEDGE * prev
Definition: crakedge.h:34
int y
Definition: scanedg.h:33
CRACKEDGE ** free_cracks
Definition: scanedg.h:31
void set_x(inT16 xin)
rewrite function
Definition: points.h:61
inT8 stepx
Definition: crakedge.h:31
int x
Definition: scanedg.h:32
inT16 x() const
access function
Definition: points.h:52
inT8 stepdir
Definition: crakedge.h:33
inT8 stepy
Definition: crakedge.h:32
inT16 y() const
access_function
Definition: points.h:56
CRACKEDGE * next
Definition: crakedge.h:35
ICOORD pos
Definition: crakedge.h:30
LIST join(LIST list1, LIST list2)
Definition: oldlist.cpp:258
void set_y(inT16 yin)
rewrite function
Definition: points.h:65

◆ join_edges()

void join_edges ( CRACKEDGE edge1,
CRACKEDGE edge2,
CRACKEDGE **  free_cracks,
C_OUTLINE_IT *  outline_it 
)

Definition at line 331 of file scanedg.cpp.

334  {
335  if (edge1->pos.x() + edge1->stepx != edge2->pos.x()
336  || edge1->pos.y() + edge1->stepy != edge2->pos.y()) {
337  CRACKEDGE *tempedge = edge1;
338  edge1 = edge2; // swap around
339  edge2 = tempedge;
340  }
341 
342  if (edge1->next == edge2) {
343  // already closed
344  complete_edge(edge1, outline_it);
345  // attach freelist to end
346  edge1->prev->next = *free_cracks;
347  *free_cracks = edge1; // and free list
348  } else {
349  // update opposite ends
350  edge2->prev->next = edge1->next;
351  edge1->next->prev = edge2->prev;
352  edge1->next = edge2; // make joins
353  edge2->prev = edge1;
354  }
355 }
CRACKEDGE * prev
Definition: crakedge.h:34
inT8 stepx
Definition: crakedge.h:31
inT16 x() const
access function
Definition: points.h:52
inT8 stepy
Definition: crakedge.h:32
inT16 y() const
access_function
Definition: points.h:56
CRACKEDGE * next
Definition: crakedge.h:35
ICOORD pos
Definition: crakedge.h:30
void complete_edge(CRACKEDGE *start, C_OUTLINE_IT *outline_it)
Definition: edgloop.cpp:37

◆ line_edges()

void line_edges ( inT16  x,
inT16  y,
inT16  xext,
uinT8  uppercolour,
uinT8 bwpos,
CRACKEDGE **  prevline,
CRACKEDGE **  free_cracks,
C_OUTLINE_IT *  outline_it 
)

Definition at line 144 of file scanedg.cpp.

151  {
152  CrackPos pos = {free_cracks, x, y };
153  int xmax; // max x coord
154  int colour; // of current pixel
155  int prevcolour; // of previous pixel
156  CRACKEDGE *current; // current h edge
157  CRACKEDGE *newcurrent; // new h edge
158 
159  xmax = x + xext; // max allowable coord
160  prevcolour = uppercolour; // forced plain margin
161  current = NULL; // nothing yet
162 
163  // do each pixel
164  for (; pos.x < xmax; pos.x++, prevline++) {
165  colour = *bwpos++; // current pixel
166  if (*prevline != NULL) {
167  // changed above
168  // change colour
169  uppercolour = FLIP_COLOUR(uppercolour);
170  if (colour == prevcolour) {
171  if (colour == uppercolour) {
172  // finish a line
173  join_edges(current, *prevline, free_cracks, outline_it);
174  current = NULL; // no edge now
175  } else {
176  // new horiz edge
177  current = h_edge(uppercolour - colour, *prevline, &pos);
178  }
179  *prevline = NULL; // no change this time
180  } else {
181  if (colour == uppercolour)
182  *prevline = v_edge(colour - prevcolour, *prevline, &pos);
183  // 8 vs 4 connection
184  else if (colour == WHITE_PIX) {
185  join_edges(current, *prevline, free_cracks, outline_it);
186  current = h_edge(uppercolour - colour, NULL, &pos);
187  *prevline = v_edge(colour - prevcolour, current, &pos);
188  } else {
189  newcurrent = h_edge(uppercolour - colour, *prevline, &pos);
190  *prevline = v_edge(colour - prevcolour, current, &pos);
191  current = newcurrent; // right going h edge
192  }
193  prevcolour = colour; // remember new colour
194  }
195  } else {
196  if (colour != prevcolour) {
197  *prevline = current = v_edge(colour - prevcolour, current, &pos);
198  prevcolour = colour;
199  }
200  if (colour != uppercolour)
201  current = h_edge(uppercolour - colour, current, &pos);
202  else
203  current = NULL; // no edge now
204  }
205  }
206  if (current != NULL) {
207  // out of block
208  if (*prevline != NULL) { // got one to join to?
209  join_edges(current, *prevline, free_cracks, outline_it);
210  *prevline = NULL; // tidy now
211  } else {
212  // fake vertical
213  *prevline = v_edge(FLIP_COLOUR(prevcolour)-prevcolour, current, &pos);
214  }
215  } else if (*prevline != NULL) {
216  //continue fake
217  *prevline = v_edge(FLIP_COLOUR(prevcolour)-prevcolour, *prevline, &pos);
218  }
219 }
int x
Definition: scanedg.h:32
CRACKEDGE * v_edge(int sign, CRACKEDGE *join, CrackPos *pos)
Definition: scanedg.cpp:279
#define WHITE_PIX
Definition: scanedg.cpp:27
CRACKEDGE * h_edge(int sign, CRACKEDGE *join, CrackPos *pos)
Definition: scanedg.cpp:228
void join_edges(CRACKEDGE *edge1, CRACKEDGE *edge2, CRACKEDGE **free_cracks, C_OUTLINE_IT *outline_it)
Definition: scanedg.cpp:331
#define FLIP_COLOUR(pix)
Definition: scanedg.cpp:30

◆ make_margins()

void make_margins ( PDBLK block,
BLOCK_LINE_IT line_it,
uinT8 pixels,
uinT8  margin,
inT16  left,
inT16  right,
inT16  y 
)

Definition at line 88 of file scanedg.cpp.

96  {
97  PB_LINE_IT *lines;
98  ICOORDELT_IT seg_it;
99  inT32 start; //of segment
100  inT16 xext; //of segment
101  int xindex; //index to pixel
102 
103  if (block->poly_block () != NULL) {
104  lines = new PB_LINE_IT (block->poly_block ());
105  const std::unique_ptr</*non-const*/ ICOORDELT_LIST> segments(lines->get_line (y));
106  if (!segments->empty ()) {
107  seg_it.set_to_list (segments.get());
108  seg_it.mark_cycle_pt ();
109  start = seg_it.data ()->x ();
110  xext = seg_it.data ()->y ();
111  for (xindex = left; xindex < right; xindex++) {
112  if (xindex >= start && !seg_it.cycled_list ()) {
113  xindex = start + xext - 1;
114  seg_it.forward ();
115  start = seg_it.data ()->x ();
116  xext = seg_it.data ()->y ();
117  }
118  else
119  pixels[xindex - left] = margin;
120  }
121  }
122  else {
123  for (xindex = left; xindex < right; xindex++)
124  pixels[xindex - left] = margin;
125  }
126  delete lines;
127  }
128  else {
129  start = line_it->get_line (y, xext);
130  for (xindex = left; xindex < start; xindex++)
131  pixels[xindex - left] = margin;
132  for (xindex = start + xext; xindex < right; xindex++)
133  pixels[xindex - left] = margin;
134  }
135 }
int32_t inT32
Definition: host.h:38
int16_t inT16
Definition: host.h:36
ICOORDELT_LIST * get_line(inT16 y)
Definition: polyblk.cpp:343
POLY_BLOCK * poly_block() const
Definition: pdblock.h:55
inT16 get_line(inT16 y, inT16 &xext)
Definition: pdblock.cpp:344

◆ v_edge()

CRACKEDGE* v_edge ( int  sign,
CRACKEDGE join,
CrackPos pos 
)

Definition at line 279 of file scanedg.cpp.

281  {
282  CRACKEDGE *newpt; // return value
283 
284  if (*pos->free_cracks != NULL) {
285  newpt = *pos->free_cracks;
286  *pos->free_cracks = newpt->next; // get one fast
287  } else {
288  newpt = new CRACKEDGE;
289  }
290  newpt->pos.set_x(pos->x); // coords of pt
291  newpt->stepx = 0; // edge is vertical
292 
293  if (sign > 0) {
294  newpt->pos.set_y(pos->y); // start location
295  newpt->stepy = 1;
296  newpt->stepdir = 3;
297  } else {
298  newpt->pos.set_y(pos->y + 1); // start location
299  newpt->stepy = -1;
300  newpt->stepdir = 1;
301  }
302 
303  if (join == NULL) {
304  newpt->next = newpt; //ptrs to other ends
305  newpt->prev = newpt;
306  } else {
307  if (newpt->pos.x() == join->pos.x()
308  && newpt->pos.y() + newpt->stepy == join->pos.y()) {
309  newpt->prev = join->prev; // update other ends
310  newpt->prev->next = newpt;
311  newpt->next = join; // join up
312  join->prev = newpt;
313  } else {
314  newpt->next = join->next; // update other ends
315  newpt->next->prev = newpt;
316  newpt->prev = join; // join up
317  join->next = newpt;
318  }
319  }
320  return newpt;
321 }
CRACKEDGE * prev
Definition: crakedge.h:34
int y
Definition: scanedg.h:33
CRACKEDGE ** free_cracks
Definition: scanedg.h:31
void set_x(inT16 xin)
rewrite function
Definition: points.h:61
inT8 stepx
Definition: crakedge.h:31
int x
Definition: scanedg.h:32
inT16 x() const
access function
Definition: points.h:52
inT8 stepdir
Definition: crakedge.h:33
inT8 stepy
Definition: crakedge.h:32
inT16 y() const
access_function
Definition: points.h:56
CRACKEDGE * next
Definition: crakedge.h:35
ICOORD pos
Definition: crakedge.h:30
LIST join(LIST list1, LIST list2)
Definition: oldlist.cpp:258
void set_y(inT16 yin)
rewrite function
Definition: points.h:65