Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scanedg.cpp File Reference
#include "mfcpch.h"
#include "edgloop.h"
#include "scanedg.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 (IMAGE *t_image, 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 whiteout_block (IMAGE *t_image, PDBLK *block)
 
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

#define BLACK_PIX   0

Definition at line 25 of file scanedg.cpp.

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

Definition at line 27 of file scanedg.cpp.

#define WHITE_PIX   1 /*thresholded colours */

Definition at line 24 of file scanedg.cpp.

Function Documentation

void block_edges ( IMAGE t_image,
PDBLK block,
C_OUTLINE_IT *  outline_it 
)

Definition at line 35 of file scanedg.cpp.

37  {
38  uinT8 margin; // margin colour
39  inT16 x; // line coords
40  inT16 y; // current line
41  ICOORD bleft; // bounding box
42  ICOORD tright;
43  ICOORD block_bleft; // bounding box
44  ICOORD block_tright;
45  int xindex; // index to pixel
46  BLOCK_LINE_IT line_it = block; // line iterator
47  IMAGELINE bwline; // thresholded line
48  // lines in progress
49  CRACKEDGE **ptrline = new CRACKEDGE*[t_image->get_xsize()+1];
50  CRACKEDGE *free_cracks = NULL;
51 
52  block->bounding_box(bleft, tright); // block box
53  block_bleft = bleft;
54  block_tright = tright;
55  for (x = tright.x() - bleft.x(); x >= 0; x--)
56  ptrline[x] = NULL; //no lines in progress
57 
58  bwline.init(t_image->get_xsize());
59 
60  margin = WHITE_PIX;
61 
62  for (y = tright.y() - 1; y >= bleft.y() - 1; y--) {
63  if (y >= block_bleft.y() && y < block_tright.y()) {
64  t_image->get_line(bleft.x(), y, tright.x() - bleft.x(), &bwline, 0);
65  make_margins(block, &line_it, bwline.pixels, margin, bleft.x(),
66  tright.x(), y);
67  } else {
68  x = tright.x() - bleft.x();
69  for (xindex = 0; xindex < x; xindex++)
70  bwline.pixels[xindex] = margin;
71  }
72  line_edges(bleft.x(), y, tright.x() - bleft.x(),
73  margin, bwline.pixels, ptrline, &free_cracks, outline_it);
74  }
75 
76  free_crackedges(free_cracks); // really free them
77  delete[] ptrline;
78 }
#define WHITE_PIX
Definition: scanedg.cpp:24
void free_crackedges(CRACKEDGE *start)
Definition: scanedg.cpp:398
void make_margins(PDBLK *block, BLOCK_LINE_IT *line_it, uinT8 *pixels, uinT8 margin, inT16 left, inT16 right, inT16 y)
Definition: scanedg.cpp:87
inT16 x() const
access function
Definition: points.h:52
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:178
Definition: img.h:325
#define NULL
Definition: host.h:144
void init(inT32 width)
Definition: img.h:343
rectangle iterator
Definition: pdblock.h:166
inT16 y() const
access_function
Definition: points.h:56
uinT8 * pixels
image pixels
Definition: img.h:328
short inT16
Definition: host.h:100
integer coordinate
Definition: points.h:30
void get_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1003
unsigned char uinT8
Definition: host.h:99
inT32 get_xsize()
Definition: img.h:92
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
Definition: pdblock.h:70
void free_crackedges ( CRACKEDGE start)

Definition at line 398 of file scanedg.cpp.

398  {
399  CRACKEDGE *current; // current edge to free
400  CRACKEDGE *next; // next one to free
401 
402  for (current = start; current != NULL; current = next) {
403  next = current->next;
404  delete current; // delete them all
405  }
406 }
#define NULL
Definition: host.h:144
CRACKEDGE * next
Definition: crakedge.h:35
CRACKEDGE* h_edge ( int  sign,
CRACKEDGE join,
CrackPos pos 
)

Definition at line 262 of file scanedg.cpp.

264  {
265  CRACKEDGE *newpt; // return value
266 
267  if (*pos->free_cracks != NULL) {
268  newpt = *pos->free_cracks;
269  *pos->free_cracks = newpt->next; // get one fast
270  } else {
271  newpt = new CRACKEDGE;
272  }
273  newpt->pos.set_y(pos->y + 1); // coords of pt
274  newpt->stepy = 0; // edge is horizontal
275 
276  if (sign > 0) {
277  newpt->pos.set_x(pos->x + 1); // start location
278  newpt->stepx = -1;
279  newpt->stepdir = 0;
280  } else {
281  newpt->pos.set_x(pos->x); // start location
282  newpt->stepx = 1;
283  newpt->stepdir = 2;
284  }
285 
286  if (join == NULL) {
287  newpt->next = newpt; // ptrs to other ends
288  newpt->prev = newpt;
289  } else {
290  if (newpt->pos.x() + newpt->stepx == join->pos.x()
291  && newpt->pos.y() == join->pos.y()) {
292  newpt->prev = join->prev; // update other ends
293  newpt->prev->next = newpt;
294  newpt->next = join; // join up
295  join->prev = newpt;
296  } else {
297  newpt->next = join->next; // update other ends
298  newpt->next->prev = newpt;
299  newpt->prev = join; // join up
300  join->next = newpt;
301  }
302  }
303  return newpt;
304 }
int x
Definition: scanedg.h:33
CRACKEDGE ** free_cracks
Definition: scanedg.h:32
inT8 stepdir
Definition: crakedge.h:33
inT16 x() const
access function
Definition: points.h:52
inT8 stepx
Definition: crakedge.h:31
#define NULL
Definition: host.h:144
inT16 y() const
access_function
Definition: points.h:56
ICOORD pos
Definition: crakedge.h:30
int y
Definition: scanedg.h:34
inT8 stepy
Definition: crakedge.h:32
void set_y(inT16 yin)
rewrite function
Definition: points.h:65
void set_x(inT16 xin)
rewrite function
Definition: points.h:61
LIST join(LIST list1, LIST list2)
Definition: oldlist.cpp:264
CRACKEDGE * prev
Definition: crakedge.h:34
CRACKEDGE * next
Definition: crakedge.h:35
void join_edges ( CRACKEDGE edge1,
CRACKEDGE edge2,
CRACKEDGE **  free_cracks,
C_OUTLINE_IT *  outline_it 
)

Definition at line 365 of file scanedg.cpp.

368  {
369  if (edge1->pos.x() + edge1->stepx != edge2->pos.x()
370  || edge1->pos.y() + edge1->stepy != edge2->pos.y()) {
371  CRACKEDGE *tempedge = edge1;
372  edge1 = edge2; // swap araound
373  edge2 = tempedge;
374  }
375 
376  if (edge1->next == edge2) {
377  // already closed
378  complete_edge(edge1, outline_it);
379  // attach freelist to end
380  edge1->prev->next = *free_cracks;
381  *free_cracks = edge1; // and free list
382  } else {
383  // update opposite ends
384  edge2->prev->next = edge1->next;
385  edge1->next->prev = edge2->prev;
386  edge1->next = edge2; // make joins
387  edge2->prev = edge1;
388  }
389 }
inT16 x() const
access function
Definition: points.h:52
inT8 stepx
Definition: crakedge.h:31
inT16 y() const
access_function
Definition: points.h:56
ICOORD pos
Definition: crakedge.h:30
inT8 stepy
Definition: crakedge.h:32
void complete_edge(CRACKEDGE *start, C_OUTLINE_IT *outline_it)
Definition: edgloop.cpp:40
CRACKEDGE * prev
Definition: crakedge.h:34
CRACKEDGE * next
Definition: crakedge.h:35
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 178 of file scanedg.cpp.

185  {
186  CrackPos pos = {free_cracks, x, y };
187  int xmax; // max x coord
188  int colour; // of current pixel
189  int prevcolour; // of previous pixel
190  CRACKEDGE *current; // current h edge
191  CRACKEDGE *newcurrent; // new h edge
192 
193  xmax = x + xext; // max allowable coord
194  prevcolour = uppercolour; // forced plain margin
195  current = NULL; // nothing yet
196 
197  // do each pixel
198  for (; pos.x < xmax; pos.x++, prevline++) {
199  colour = *bwpos++; // current pixel
200  if (*prevline != NULL) {
201  // changed above
202  // change colour
203  uppercolour = FLIP_COLOUR(uppercolour);
204  if (colour == prevcolour) {
205  if (colour == uppercolour) {
206  // finish a line
207  join_edges(current, *prevline, free_cracks, outline_it);
208  current = NULL; // no edge now
209  } else {
210  // new horiz edge
211  current = h_edge(uppercolour - colour, *prevline, &pos);
212  }
213  *prevline = NULL; // no change this time
214  } else {
215  if (colour == uppercolour)
216  *prevline = v_edge(colour - prevcolour, *prevline, &pos);
217  // 8 vs 4 connection
218  else if (colour == WHITE_PIX) {
219  join_edges(current, *prevline, free_cracks, outline_it);
220  current = h_edge(uppercolour - colour, NULL, &pos);
221  *prevline = v_edge(colour - prevcolour, current, &pos);
222  } else {
223  newcurrent = h_edge(uppercolour - colour, *prevline, &pos);
224  *prevline = v_edge(colour - prevcolour, current, &pos);
225  current = newcurrent; // right going h edge
226  }
227  prevcolour = colour; // remember new colour
228  }
229  } else {
230  if (colour != prevcolour) {
231  *prevline = current = v_edge(colour - prevcolour, current, &pos);
232  prevcolour = colour;
233  }
234  if (colour != uppercolour)
235  current = h_edge(uppercolour - colour, current, &pos);
236  else
237  current = NULL; // no edge now
238  }
239  }
240  if (current != NULL) {
241  // out of block
242  if (*prevline != NULL) { // got one to join to?
243  join_edges(current, *prevline, free_cracks, outline_it);
244  *prevline = NULL; // tidy now
245  } else {
246  // fake vertical
247  *prevline = v_edge(FLIP_COLOUR(prevcolour)-prevcolour, current, &pos);
248  }
249  } else if (*prevline != NULL) {
250  //continue fake
251  *prevline = v_edge(FLIP_COLOUR(prevcolour)-prevcolour, *prevline, &pos);
252  }
253 }
#define WHITE_PIX
Definition: scanedg.cpp:24
int x
Definition: scanedg.h:33
#define NULL
Definition: host.h:144
#define FLIP_COLOUR(pix)
Definition: scanedg.cpp:27
void join_edges(CRACKEDGE *edge1, CRACKEDGE *edge2, CRACKEDGE **free_cracks, C_OUTLINE_IT *outline_it)
Definition: scanedg.cpp:365
CRACKEDGE * h_edge(int sign, CRACKEDGE *join, CrackPos *pos)
Definition: scanedg.cpp:262
CRACKEDGE * v_edge(int sign, CRACKEDGE *join, CrackPos *pos)
Definition: scanedg.cpp:313
void make_margins ( PDBLK block,
BLOCK_LINE_IT line_it,
uinT8 pixels,
uinT8  margin,
inT16  left,
inT16  right,
inT16  y 
)

Definition at line 87 of file scanedg.cpp.

95  {
96  PB_LINE_IT *lines;
97  ICOORDELT_LIST *segments; //bits of a line
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  segments = lines->get_line (y);
106  if (!segments->empty ()) {
107  seg_it.set_to_list (segments);
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 segments;
127  delete lines;
128  }
129  else {
130  start = line_it->get_line (y, xext);
131  for (xindex = left; xindex < start; xindex++)
132  pixels[xindex - left] = margin;
133  for (xindex = start + xext; xindex < right; xindex++)
134  pixels[xindex - left] = margin;
135  }
136 }
ICOORDELT_LIST * get_line(inT16 y)
Definition: polyblk.cpp:344
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
inT16 get_line(inT16 y, inT16 &xext)
Definition: pdblock.cpp:375
POLY_BLOCK * poly_block() const
Definition: pdblock.h:62
short inT16
Definition: host.h:100
CRACKEDGE* v_edge ( int  sign,
CRACKEDGE join,
CrackPos pos 
)

Definition at line 313 of file scanedg.cpp.

315  {
316  CRACKEDGE *newpt; // return value
317 
318  if (*pos->free_cracks != NULL) {
319  newpt = *pos->free_cracks;
320  *pos->free_cracks = newpt->next; // get one fast
321  } else {
322  newpt = new CRACKEDGE;
323  }
324  newpt->pos.set_x(pos->x); // coords of pt
325  newpt->stepx = 0; // edge is vertical
326 
327  if (sign > 0) {
328  newpt->pos.set_y(pos->y); // start location
329  newpt->stepy = 1;
330  newpt->stepdir = 3;
331  } else {
332  newpt->pos.set_y(pos->y + 1); // start location
333  newpt->stepy = -1;
334  newpt->stepdir = 1;
335  }
336 
337  if (join == NULL) {
338  newpt->next = newpt; //ptrs to other ends
339  newpt->prev = newpt;
340  } else {
341  if (newpt->pos.x() == join->pos.x()
342  && newpt->pos.y() + newpt->stepy == join->pos.y()) {
343  newpt->prev = join->prev; // update other ends
344  newpt->prev->next = newpt;
345  newpt->next = join; // join up
346  join->prev = newpt;
347  } else {
348  newpt->next = join->next; // update other ends
349  newpt->next->prev = newpt;
350  newpt->prev = join; // join up
351  join->next = newpt;
352  }
353  }
354  return newpt;
355 }
int x
Definition: scanedg.h:33
CRACKEDGE ** free_cracks
Definition: scanedg.h:32
inT8 stepdir
Definition: crakedge.h:33
inT16 x() const
access function
Definition: points.h:52
inT8 stepx
Definition: crakedge.h:31
#define NULL
Definition: host.h:144
inT16 y() const
access_function
Definition: points.h:56
ICOORD pos
Definition: crakedge.h:30
int y
Definition: scanedg.h:34
inT8 stepy
Definition: crakedge.h:32
void set_y(inT16 yin)
rewrite function
Definition: points.h:65
void set_x(inT16 xin)
rewrite function
Definition: points.h:61
LIST join(LIST list1, LIST list2)
Definition: oldlist.cpp:264
CRACKEDGE * prev
Definition: crakedge.h:34
CRACKEDGE * next
Definition: crakedge.h:35
void whiteout_block ( IMAGE t_image,
PDBLK block 
)

Definition at line 145 of file scanedg.cpp.

148  {
149  inT16 x; //line coords
150  inT16 y; //current line
151  inT16 xext; //line width
152  int xindex; //index to pixel
153  uinT8 *dest; //destination pixel
154  TBOX block_box; //bounding box
155  BLOCK_LINE_IT line_it = block; //line iterator
156  IMAGELINE bwline; //thresholded line
157 
158  block_box = block->bounding_box ();
159  for (y = block_box.bottom (); y < block_box.top (); y++) {
160  //find line limits
161  x = line_it.get_line (y, xext);
162  t_image->get_line (x, y, xext, &bwline, 0);
163  dest = bwline.pixels; //destination pixel
164  for (xindex = 0; xindex < xext; xindex++)
165  *dest++ = 1;
166  t_image->put_line (x, y, xext, &bwline, 0);
167  }
168 }
Definition: img.h:325
Definition: rect.h:29
rectangle iterator
Definition: pdblock.h:166
inT16 get_line(inT16 y, inT16 &xext)
Definition: pdblock.cpp:375
void put_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1222
inT16 top() const
Definition: rect.h:53
uinT8 * pixels
image pixels
Definition: img.h:328
short inT16
Definition: host.h:100
void get_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1003
unsigned char uinT8
Definition: host.h:99
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
Definition: pdblock.h:70
inT16 bottom() const
Definition: rect.h:60