tesseract  4.0.0-1-g2a2b
scanedg.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: scanedg.cpp (Formerly scanedge.c)
3  * Description: Raster scanning crack based edge extractor.
4  * Author: Ray Smith
5  *
6  * (C) Copyright 1991, 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 #include "scanedg.h"
20 
21 #include <memory> // std::unique_ptr
22 
23 #include "allheaders.h"
24 #include "edgloop.h"
25 
26 #define WHITE_PIX 1 /*thresholded colours */
27 #define BLACK_PIX 0
28 // Flips between WHITE_PIX and BLACK_PIX.
29 #define FLIP_COLOUR(pix) (1-(pix))
30 
31 /**********************************************************************
32  * block_edges
33  *
34  * Extract edges from a PDBLK.
35  **********************************************************************/
36 
37 void block_edges(Pix *t_pix, // thresholded image
38  PDBLK *block, // block in image
39  C_OUTLINE_IT* outline_it) {
40  ICOORD bleft; // bounding box
41  ICOORD tright;
42  BLOCK_LINE_IT line_it = block; // line iterator
43 
44  int width = pixGetWidth(t_pix);
45  int height = pixGetHeight(t_pix);
46  int wpl = pixGetWpl(t_pix);
47  // lines in progress
48  std::unique_ptr<CRACKEDGE*[]> ptrline(new CRACKEDGE*[width + 1]);
49  CRACKEDGE *free_cracks = nullptr;
50 
51  block->bounding_box(bleft, tright); // block box
52  ASSERT_HOST(tright.x() <= width);
53  ASSERT_HOST(tright.y() <= height);
54  int block_width = tright.x() - bleft.x();
55  for (int x = block_width; x >= 0; x--)
56  ptrline[x] = nullptr; // no lines in progress
57 
58  std::unique_ptr<uint8_t[]> bwline(new uint8_t[width]);
59 
60  uint8_t margin = WHITE_PIX;
61 
62  for (int y = tright.y() - 1; y >= bleft.y() - 1; y--) {
63  if (y >= bleft.y() && y < tright.y()) {
64  // Get the binary pixels from the image.
65  l_uint32* line = pixGetData(t_pix) + wpl * (height - 1 - y);
66  for (int x = 0; x < block_width; ++x) {
67  bwline[x] = GET_DATA_BIT(line, x + bleft.x()) ^ 1;
68  }
69  make_margins(block, &line_it, bwline.get(), margin, bleft.x(), tright.x(), y);
70  } else {
71  memset(bwline.get(), margin, block_width * sizeof(bwline[0]));
72  }
73  line_edges(bleft.x(), y, block_width,
74  margin, bwline.get(), ptrline.get(), &free_cracks, outline_it);
75  }
76 
77  free_crackedges(free_cracks); // really free them
78 }
79 
80 
81 /**********************************************************************
82  * make_margins
83  *
84  * Get an image line and set to margin non-text pixels.
85  **********************************************************************/
86 
87 void make_margins( //get a line
88  PDBLK *block, //block in image
89  BLOCK_LINE_IT *line_it, //for old style
90  uint8_t *pixels, //pixels to strip
91  uint8_t margin, //white-out pixel
92  int16_t left, //block edges
93  int16_t right,
94  int16_t y //line coord
95  ) {
96  ICOORDELT_IT seg_it;
97  int32_t start; //of segment
98  int16_t xext; //of segment
99  int xindex; //index to pixel
100 
101  if (block->poly_block () != nullptr) {
102  std::unique_ptr<PB_LINE_IT> lines(new PB_LINE_IT (block->poly_block ()));
103  const std::unique_ptr</*non-const*/ ICOORDELT_LIST> segments(
104  lines->get_line(y));
105  if (!segments->empty ()) {
106  seg_it.set_to_list(segments.get());
107  seg_it.mark_cycle_pt ();
108  start = seg_it.data ()->x ();
109  xext = seg_it.data ()->y ();
110  for (xindex = left; xindex < right; xindex++) {
111  if (xindex >= start && !seg_it.cycled_list ()) {
112  xindex = start + xext - 1;
113  seg_it.forward ();
114  start = seg_it.data ()->x ();
115  xext = seg_it.data ()->y ();
116  }
117  else
118  pixels[xindex - left] = margin;
119  }
120  }
121  else {
122  for (xindex = left; xindex < right; xindex++)
123  pixels[xindex - left] = margin;
124  }
125  }
126  else {
127  start = line_it->get_line (y, xext);
128  for (xindex = left; xindex < start; xindex++)
129  pixels[xindex - left] = margin;
130  for (xindex = start + xext; xindex < right; xindex++)
131  pixels[xindex - left] = margin;
132  }
133 }
134 
135 /**********************************************************************
136  * line_edges
137  *
138  * Scan a line for edges and update the edges in progress.
139  * When edges close into loops, send them for approximation.
140  **********************************************************************/
141 
142 void line_edges(int16_t x, // coord of line start
143  int16_t y, // coord of line
144  int16_t xext, // width of line
145  uint8_t uppercolour, // start of prev line
146  uint8_t * bwpos, // thresholded line
147  CRACKEDGE ** prevline, // edges in progress
148  CRACKEDGE **free_cracks,
149  C_OUTLINE_IT* outline_it) {
150  CrackPos pos = {free_cracks, x, y };
151  int xmax; // max x coord
152  int colour; // of current pixel
153  int prevcolour; // of previous pixel
154  CRACKEDGE *current; // current h edge
155  CRACKEDGE *newcurrent; // new h edge
156 
157  xmax = x + xext; // max allowable coord
158  prevcolour = uppercolour; // forced plain margin
159  current = nullptr; // nothing yet
160 
161  // do each pixel
162  for (; pos.x < xmax; pos.x++, prevline++) {
163  colour = *bwpos++; // current pixel
164  if (*prevline != nullptr) {
165  // changed above
166  // change colour
167  uppercolour = FLIP_COLOUR(uppercolour);
168  if (colour == prevcolour) {
169  if (colour == uppercolour) {
170  // finish a line
171  join_edges(current, *prevline, free_cracks, outline_it);
172  current = nullptr; // no edge now
173  } else {
174  // new horiz edge
175  current = h_edge(uppercolour - colour, *prevline, &pos);
176  }
177  *prevline = nullptr; // no change this time
178  } else {
179  if (colour == uppercolour)
180  *prevline = v_edge(colour - prevcolour, *prevline, &pos);
181  // 8 vs 4 connection
182  else if (colour == WHITE_PIX) {
183  join_edges(current, *prevline, free_cracks, outline_it);
184  current = h_edge(uppercolour - colour, nullptr, &pos);
185  *prevline = v_edge(colour - prevcolour, current, &pos);
186  } else {
187  newcurrent = h_edge(uppercolour - colour, *prevline, &pos);
188  *prevline = v_edge(colour - prevcolour, current, &pos);
189  current = newcurrent; // right going h edge
190  }
191  prevcolour = colour; // remember new colour
192  }
193  } else {
194  if (colour != prevcolour) {
195  *prevline = current = v_edge(colour - prevcolour, current, &pos);
196  prevcolour = colour;
197  }
198  if (colour != uppercolour)
199  current = h_edge(uppercolour - colour, current, &pos);
200  else
201  current = nullptr; // no edge now
202  }
203  }
204  if (current != nullptr) {
205  // out of block
206  if (*prevline != nullptr) { // got one to join to?
207  join_edges(current, *prevline, free_cracks, outline_it);
208  *prevline = nullptr; // tidy now
209  } else {
210  // fake vertical
211  *prevline = v_edge(FLIP_COLOUR(prevcolour)-prevcolour, current, &pos);
212  }
213  } else if (*prevline != nullptr) {
214  //continue fake
215  *prevline = v_edge(FLIP_COLOUR(prevcolour)-prevcolour, *prevline, &pos);
216  }
217 }
218 
219 
220 /**********************************************************************
221  * h_edge
222  *
223  * Create a new horizontal CRACKEDGE and join it to the given edge.
224  **********************************************************************/
225 
226 CRACKEDGE *h_edge(int sign, // sign of edge
227  CRACKEDGE* join, // edge to join to
228  CrackPos* pos) {
229  CRACKEDGE *newpt; // return value
230 
231  if (*pos->free_cracks != nullptr) {
232  newpt = *pos->free_cracks;
233  *pos->free_cracks = newpt->next; // get one fast
234  } else {
235  newpt = new CRACKEDGE;
236  }
237  newpt->pos.set_y(pos->y + 1); // coords of pt
238  newpt->stepy = 0; // edge is horizontal
239 
240  if (sign > 0) {
241  newpt->pos.set_x(pos->x + 1); // start location
242  newpt->stepx = -1;
243  newpt->stepdir = 0;
244  } else {
245  newpt->pos.set_x(pos->x); // start location
246  newpt->stepx = 1;
247  newpt->stepdir = 2;
248  }
249 
250  if (join == nullptr) {
251  newpt->next = newpt; // ptrs to other ends
252  newpt->prev = newpt;
253  } else {
254  if (newpt->pos.x() + newpt->stepx == join->pos.x()
255  && newpt->pos.y() == join->pos.y()) {
256  newpt->prev = join->prev; // update other ends
257  newpt->prev->next = newpt;
258  newpt->next = join; // join up
259  join->prev = newpt;
260  } else {
261  newpt->next = join->next; // update other ends
262  newpt->next->prev = newpt;
263  newpt->prev = join; // join up
264  join->next = newpt;
265  }
266  }
267  return newpt;
268 }
269 
270 
271 /**********************************************************************
272  * v_edge
273  *
274  * Create a new vertical CRACKEDGE and join it to the given edge.
275  **********************************************************************/
276 
277 CRACKEDGE *v_edge(int sign, // sign of edge
278  CRACKEDGE* join,
279  CrackPos* pos) {
280  CRACKEDGE *newpt; // return value
281 
282  if (*pos->free_cracks != nullptr) {
283  newpt = *pos->free_cracks;
284  *pos->free_cracks = newpt->next; // get one fast
285  } else {
286  newpt = new CRACKEDGE;
287  }
288  newpt->pos.set_x(pos->x); // coords of pt
289  newpt->stepx = 0; // edge is vertical
290 
291  if (sign > 0) {
292  newpt->pos.set_y(pos->y); // start location
293  newpt->stepy = 1;
294  newpt->stepdir = 3;
295  } else {
296  newpt->pos.set_y(pos->y + 1); // start location
297  newpt->stepy = -1;
298  newpt->stepdir = 1;
299  }
300 
301  if (join == nullptr) {
302  newpt->next = newpt; //ptrs to other ends
303  newpt->prev = newpt;
304  } else {
305  if (newpt->pos.x() == join->pos.x()
306  && newpt->pos.y() + newpt->stepy == join->pos.y()) {
307  newpt->prev = join->prev; // update other ends
308  newpt->prev->next = newpt;
309  newpt->next = join; // join up
310  join->prev = newpt;
311  } else {
312  newpt->next = join->next; // update other ends
313  newpt->next->prev = newpt;
314  newpt->prev = join; // join up
315  join->next = newpt;
316  }
317  }
318  return newpt;
319 }
320 
321 
322 /**********************************************************************
323  * join_edges
324  *
325  * Join 2 edges together. Send the outline for approximation when a
326  * closed loop is formed.
327  **********************************************************************/
328 
329 void join_edges(CRACKEDGE *edge1, // edges to join
330  CRACKEDGE *edge2, // no specific order
331  CRACKEDGE **free_cracks,
332  C_OUTLINE_IT* outline_it) {
333  if (edge1->pos.x() + edge1->stepx != edge2->pos.x()
334  || edge1->pos.y() + edge1->stepy != edge2->pos.y()) {
335  CRACKEDGE *tempedge = edge1;
336  edge1 = edge2; // swap around
337  edge2 = tempedge;
338  }
339 
340  if (edge1->next == edge2) {
341  // already closed
342  complete_edge(edge1, outline_it);
343  // attach freelist to end
344  edge1->prev->next = *free_cracks;
345  *free_cracks = edge1; // and free list
346  } else {
347  // update opposite ends
348  edge2->prev->next = edge1->next;
349  edge1->next->prev = edge2->prev;
350  edge1->next = edge2; // make joins
351  edge2->prev = edge1;
352  }
353 }
354 
355 
356 /**********************************************************************
357  * free_crackedges
358  *
359  * Really free the CRACKEDGEs by giving them back to delete.
360  **********************************************************************/
361 
363  CRACKEDGE *current; // current edge to free
364  CRACKEDGE *next; // next one to free
365 
366  for (current = start; current != nullptr; current = next) {
367  next = current->next;
368  delete current; // delete them all
369  }
370 }
int x
Definition: scanedg.h:31
CRACKEDGE * prev
Definition: crakedge.h:34
CRACKEDGE ** free_cracks
Definition: scanedg.h:30
int8_t stepx
Definition: crakedge.h:31
void free_crackedges(CRACKEDGE *start)
Definition: scanedg.cpp:362
void set_x(int16_t xin)
rewrite function
Definition: points.h:62
CRACKEDGE * v_edge(int sign, CRACKEDGE *join, CrackPos *pos)
Definition: scanedg.cpp:277
void complete_edge(CRACKEDGE *start, C_OUTLINE_IT *outline_it)
Definition: edgloop.cpp:37
#define FLIP_COLOUR(pix)
Definition: scanedg.cpp:29
int16_t y() const
access_function
Definition: points.h:57
void join_edges(CRACKEDGE *edge1, CRACKEDGE *edge2, CRACKEDGE **free_cracks, C_OUTLINE_IT *outline_it)
Definition: scanedg.cpp:329
void line_edges(int16_t x, int16_t y, int16_t xext, uint8_t uppercolour, uint8_t *bwpos, CRACKEDGE **prevline, CRACKEDGE **free_cracks, C_OUTLINE_IT *outline_it)
Definition: scanedg.cpp:142
int y
Definition: scanedg.h:32
void make_margins(PDBLK *block, BLOCK_LINE_IT *line_it, uint8_t *pixels, uint8_t margin, int16_t left, int16_t right, int16_t y)
Definition: scanedg.cpp:87
struct list_rec * next
Definition: oldlist.h:132
CRACKEDGE * h_edge(int sign, CRACKEDGE *join, CrackPos *pos)
Definition: scanedg.cpp:226
int16_t get_line(int16_t y, int16_t &xext)
Definition: pdblock.cpp:347
int8_t stepdir
Definition: crakedge.h:33
integer coordinate
Definition: points.h:32
int16_t x() const
access function
Definition: points.h:53
ICOORDELT_LIST * get_line(int16_t y)
Definition: polyblk.cpp:341
LIST join(LIST list1, LIST list2)
Definition: oldlist.cpp:231
ICOORD pos
Definition: crakedge.h:30
POLY_BLOCK * poly_block() const
Definition: pdblock.h:56
#define WHITE_PIX
Definition: scanedg.cpp:26
CRACKEDGE * next
Definition: crakedge.h:35
page block
Definition: pdblock.h:32
int8_t stepy
Definition: crakedge.h:32
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
Definition: pdblock.h:60
rectangle iterator
Definition: pdblock.h:145
void set_y(int16_t yin)
rewrite function
Definition: points.h:66
void block_edges(Pix *t_pix, PDBLK *block, C_OUTLINE_IT *outline_it)
Definition: scanedg.cpp:37
#define ASSERT_HOST(x)
Definition: errcode.h:84