tesseract  4.1.0
mfoutline.h File Reference
#include "blobs.h"
#include "fpoint.h"
#include "oldlist.h"
#include "params.h"

Go to the source code of this file.

Classes

struct  MFEDGEPT
 

Macros

#define NORMAL_X_HEIGHT   (0.5)
 
#define NORMAL_BASELINE   (0.0)
 
#define AverageOf(A, B)   (((A) + (B)) / 2)
 
#define MF_SCALE_FACTOR   (NORMAL_X_HEIGHT / kBlnXHeight)
 
#define DegenerateOutline(O)   (((O) == NIL_LIST) || ((O) == list_rest(O)))
 
#define PointAt(O)   ((MFEDGEPT*)first_node(O))
 
#define NextPointAfter(E)   (list_rest(E))
 
#define MakeOutlineCircular(O)   (set_rest(last(O), (O)))
 
#define ClearMark(P)   ((P)->ExtremityMark = false)
 
#define MarkPoint(P)   ((P)->ExtremityMark = true)
 

Typedefs

using MFOUTLINE = LIST
 

Enumerations

enum  DIRECTION {
  north, south, east, west,
  northeast, northwest, southeast, southwest
}
 
enum  OUTLINETYPE { outer, hole }
 
enum  NORM_METHOD { baseline, character }
 

Functions

void ComputeBlobCenter (TBLOB *Blob, TPOINT *BlobCenter)
 
LIST ConvertBlob (TBLOB *Blob)
 
MFOUTLINE ConvertOutline (TESSLINE *Outline)
 
LIST ConvertOutlines (TESSLINE *Outline, LIST ConvertedOutlines, OUTLINETYPE OutlineType)
 
void FilterEdgeNoise (MFOUTLINE Outline, float NoiseSegmentLength)
 
void FindDirectionChanges (MFOUTLINE Outline, float MinSlope, float MaxSlope)
 
void FreeMFOutline (void *agr)
 
void FreeOutlines (LIST Outlines)
 
void MarkDirectionChanges (MFOUTLINE Outline)
 
MFEDGEPTNewEdgePoint ()
 
MFOUTLINE NextExtremity (MFOUTLINE EdgePoint)
 
void NormalizeOutline (MFOUTLINE Outline, float XOrigin)
 
void ChangeDirection (MFOUTLINE Start, MFOUTLINE End, DIRECTION Direction)
 
void CharNormalizeOutline (MFOUTLINE Outline, const DENORM &cn_denorm)
 
void ComputeDirection (MFEDGEPT *Start, MFEDGEPT *Finish, float MinSlope, float MaxSlope)
 
MFOUTLINE NextDirectionChange (MFOUTLINE EdgePoint)
 

Macro Definition Documentation

#define AverageOf (   A,
 
)    (((A) + (B)) / 2)

Macros

Definition at line 62 of file mfoutline.h.

#define ClearMark (   P)    ((P)->ExtremityMark = false)

Definition at line 74 of file mfoutline.h.

#define DegenerateOutline (   O)    (((O) == NIL_LIST) || ((O) == list_rest(O)))

Definition at line 68 of file mfoutline.h.

#define MakeOutlineCircular (   O)    (set_rest(last(O), (O)))

Definition at line 71 of file mfoutline.h.

#define MarkPoint (   P)    ((P)->ExtremityMark = true)

Definition at line 75 of file mfoutline.h.

#define MF_SCALE_FACTOR   (NORMAL_X_HEIGHT / kBlnXHeight)

Definition at line 65 of file mfoutline.h.

#define NextPointAfter (   E)    (list_rest(E))

Definition at line 70 of file mfoutline.h.

#define NORMAL_BASELINE   (0.0)

Definition at line 30 of file mfoutline.h.

#define NORMAL_X_HEIGHT   (0.5)

Include Files and Type Defines

Definition at line 29 of file mfoutline.h.

#define PointAt (   O)    ((MFEDGEPT*)first_node(O))

Definition at line 69 of file mfoutline.h.

Typedef Documentation

using MFOUTLINE = LIST

Definition at line 32 of file mfoutline.h.

Enumeration Type Documentation

enum DIRECTION
Enumerator
north 
south 
east 
west 
northeast 
northwest 
southeast 
southwest 

Definition at line 34 of file mfoutline.h.

34  {
35  north,
36  south,
37  east,
38  west,
39  northeast,
40  northwest,
41  southeast,
42  southwest
43 } DIRECTION;
Definition: mfoutline.h:38
Definition: mfoutline.h:37
DIRECTION
Definition: mfoutline.h:34
Enumerator
baseline 
character 

Definition at line 57 of file mfoutline.h.

Enumerator
outer 
hole 

Definition at line 55 of file mfoutline.h.

OUTLINETYPE
Definition: mfoutline.h:55
Definition: mfoutline.h:55

Function Documentation

void ChangeDirection ( MFOUTLINE  Start,
MFOUTLINE  End,
DIRECTION  Direction 
)

Change the direction of every vector in the specified outline segment to Direction. The segment to be changed starts at Start and ends at End. Note that the previous direction of End must also be changed to reflect the change in direction of the point before it.

Parameters
Start,Enddefines segment of outline to be modified
Directionnew direction to assign to segment

Definition at line 309 of file mfoutline.cpp.

309  {
310  MFOUTLINE Current;
311 
312  for (Current = Start; Current != End; Current = NextPointAfter (Current))
313  PointAt (Current)->Direction = Direction;
314 
315  PointAt (End)->PreviousDirection = Direction;
316 
317 } /* ChangeDirection */
#define PointAt(O)
Definition: mfoutline.h:69
#define NextPointAfter(E)
Definition: mfoutline.h:70
void CharNormalizeOutline ( MFOUTLINE  Outline,
const DENORM cn_denorm 
)

This routine normalizes each point in Outline by translating it to the specified center and scaling it anisotropically according to the given scale factors.

Parameters
Outlineoutline to be character normalized
cn_denorm

Definition at line 326 of file mfoutline.cpp.

326  {
327  MFOUTLINE First, Current;
328  MFEDGEPT *CurrentPoint;
329 
330  if (Outline == NIL_LIST)
331  return;
332 
333  First = Outline;
334  Current = First;
335  do {
336  CurrentPoint = PointAt(Current);
337  FCOORD pos(CurrentPoint->Point.x, CurrentPoint->Point.y);
338  cn_denorm.LocalNormTransform(pos, &pos);
339  CurrentPoint->Point.x = (pos.x() - UINT8_MAX / 2) * MF_SCALE_FACTOR;
340  CurrentPoint->Point.y = (pos.y() - UINT8_MAX / 2) * MF_SCALE_FACTOR;
341 
342  Current = NextPointAfter(Current);
343  }
344  while (Current != First);
345 
346 } /* CharNormalizeOutline */
Definition: points.h:188
float y
Definition: fpoint.h:30
#define NIL_LIST
Definition: oldlist.h:76
#define PointAt(O)
Definition: mfoutline.h:69
#define NextPointAfter(E)
Definition: mfoutline.h:70
FPOINT Point
Definition: mfoutline.h:46
#define MF_SCALE_FACTOR
Definition: mfoutline.h:65
float x
Definition: fpoint.h:30
void LocalNormTransform(const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:306
void ComputeBlobCenter ( TBLOB Blob,
TPOINT BlobCenter 
)

Public Function Prototypes

void ComputeDirection ( MFEDGEPT Start,
MFEDGEPT Finish,
float  MinSlope,
float  MaxSlope 
)

This routine computes the slope from Start to Finish and and then computes the approximate direction of the line segment from Start to Finish. The direction is quantized into 8 buckets: N, S, E, W, NE, NW, SE, SW Both the slope and the direction are then stored into the appropriate fields of the Start edge point. The direction is also stored into the PreviousDirection field of the Finish edge point.

Parameters
Startstarting point to compute direction from
Finishfinishing point to compute direction to
MinSlopeslope below which lines are horizontal
MaxSlopeslope above which lines are vertical

Definition at line 363 of file mfoutline.cpp.

366  {
367  FVECTOR Delta;
368 
369  Delta.x = Finish->Point.x - Start->Point.x;
370  Delta.y = Finish->Point.y - Start->Point.y;
371  if (Delta.x == 0) {
372  if (Delta.y < 0) {
373  Start->Slope = -FLT_MAX;
374  Start->Direction = south;
375  } else {
376  Start->Slope = FLT_MAX;
377  Start->Direction = north;
378  }
379  } else {
380  Start->Slope = Delta.y / Delta.x;
381  if (Delta.x > 0) {
382  if (Delta.y > 0) {
383  if (Start->Slope > MinSlope) {
384  if (Start->Slope < MaxSlope) {
385  Start->Direction = northeast;
386  } else {
387  Start->Direction = north;
388  }
389  } else {
390  Start->Direction = east;
391  }
392  }
393  else if (Start->Slope < -MinSlope) {
394  if (Start->Slope > -MaxSlope) {
395  Start->Direction = southeast;
396  } else {
397  Start->Direction = south;
398  }
399  } else {
400  Start->Direction = east;
401  }
402  } else if (Delta.y > 0) {
403  if (Start->Slope < -MinSlope) {
404  if (Start->Slope > -MaxSlope) {
405  Start->Direction = northwest;
406  } else {
407  Start->Direction = north;
408  }
409  } else {
410  Start->Direction = west;
411  }
412  } else if (Start->Slope > MinSlope) {
413  if (Start->Slope < MaxSlope) {
414  Start->Direction = southwest;
415  } else {
416  Start->Direction = south;
417  }
418  } else {
419  Start->Direction = west;
420  }
421  }
422  Finish->PreviousDirection = Start->Direction;
423 }
Definition: mfoutline.h:38
float y
Definition: fpoint.h:30
Definition: mfoutline.h:37
DIRECTION PreviousDirection
Definition: mfoutline.h:52
FPOINT Point
Definition: mfoutline.h:46
float x
Definition: fpoint.h:30
DIRECTION Direction
Definition: mfoutline.h:51
float Slope
Definition: mfoutline.h:47
Definition: fpoint.h:29
LIST ConvertBlob ( TBLOB blob)

Convert a blob into a list of MFOUTLINEs (float-based microfeature format).

Definition at line 37 of file mfoutline.cpp.

37  {
38  LIST outlines = NIL_LIST;
39  return (blob == nullptr)
40  ? NIL_LIST
41  : ConvertOutlines(blob->outlines, outlines, outer);
42 }
TESSLINE * outlines
Definition: blobs.h:379
LIST ConvertOutlines(TESSLINE *outline, LIST mf_outlines, OUTLINETYPE outline_type)
Definition: mfoutline.cpp:89
#define NIL_LIST
Definition: oldlist.h:76
MFOUTLINE ConvertOutline ( TESSLINE outline)

Convert a TESSLINE into the float-based MFOUTLINE micro-feature format.

Definition at line 47 of file mfoutline.cpp.

47  {
48  MFEDGEPT *NewPoint;
49  MFOUTLINE MFOutline = NIL_LIST;
50  EDGEPT *EdgePoint;
51  EDGEPT *StartPoint;
52  EDGEPT *NextPoint;
53 
54  if (outline == nullptr || outline->loop == nullptr)
55  return MFOutline;
56 
57  StartPoint = outline->loop;
58  EdgePoint = StartPoint;
59  do {
60  NextPoint = EdgePoint->next;
61 
62  /* filter out duplicate points */
63  if (EdgePoint->pos.x != NextPoint->pos.x ||
64  EdgePoint->pos.y != NextPoint->pos.y) {
65  NewPoint = NewEdgePoint();
66  ClearMark(NewPoint);
67  NewPoint->Hidden = EdgePoint->IsHidden();
68  NewPoint->Point.x = EdgePoint->pos.x;
69  NewPoint->Point.y = EdgePoint->pos.y;
70  MFOutline = push(MFOutline, NewPoint);
71  }
72  EdgePoint = NextPoint;
73  } while (EdgePoint != StartPoint);
74 
75  if (MFOutline != nullptr)
76  MakeOutlineCircular(MFOutline);
77  return MFOutline;
78 }
int16_t x
Definition: blobs.h:73
bool IsHidden() const
Definition: blobs.h:155
EDGEPT * next
Definition: blobs.h:171
bool Hidden
Definition: mfoutline.h:49
float y
Definition: fpoint.h:30
EDGEPT * loop
Definition: blobs.h:259
TPOINT pos
Definition: blobs.h:165
#define ClearMark(P)
Definition: mfoutline.h:74
LIST push(LIST list, void *element)
Definition: oldlist.cpp:219
int16_t y
Definition: blobs.h:74
#define NIL_LIST
Definition: oldlist.h:76
Definition: blobs.h:78
FPOINT Point
Definition: mfoutline.h:46
#define MakeOutlineCircular(O)
Definition: mfoutline.h:71
float x
Definition: fpoint.h:30
MFEDGEPT * NewEdgePoint()
Definition: mfoutline.cpp:205
LIST ConvertOutlines ( TESSLINE outline,
LIST  mf_outlines,
OUTLINETYPE  outline_type 
)

Convert a tree of outlines to a list of MFOUTLINEs (lists of MFEDGEPTs).

Parameters
outlinefirst outline to be converted
mf_outlineslist to add converted outlines to
outline_typeare the outlines outer or holes?

Definition at line 89 of file mfoutline.cpp.

91  {
92  MFOUTLINE mf_outline;
93 
94  while (outline != nullptr) {
95  mf_outline = ConvertOutline(outline);
96  if (mf_outline != nullptr)
97  mf_outlines = push(mf_outlines, mf_outline);
98  outline = outline->next;
99  }
100  return mf_outlines;
101 }
TESSLINE * next
Definition: blobs.h:260
LIST push(LIST list, void *element)
Definition: oldlist.cpp:219
MFOUTLINE ConvertOutline(TESSLINE *outline)
Definition: mfoutline.cpp:47
void FilterEdgeNoise ( MFOUTLINE  Outline,
float  NoiseSegmentLength 
)
void FindDirectionChanges ( MFOUTLINE  Outline,
float  MinSlope,
float  MaxSlope 
)

This routine searches through the specified outline, computes a slope for each vector in the outline, and marks each vector as having one of the following directions: N, S, E, W, NE, NW, SE, SW This information is then stored in the outline and the outline is returned.

Parameters
Outlinemicro-feature outline to analyze
MinSlopecontrols "snapping" of segments to horizontal
MaxSlopecontrols "snapping" of segments to vertical

Definition at line 115 of file mfoutline.cpp.

117  {
118  MFEDGEPT *Current;
119  MFEDGEPT *Last;
120  MFOUTLINE EdgePoint;
121 
122  if (DegenerateOutline (Outline))
123  return;
124 
125  Last = PointAt (Outline);
126  Outline = NextPointAfter (Outline);
127  EdgePoint = Outline;
128  do {
129  Current = PointAt (EdgePoint);
130  ComputeDirection(Last, Current, MinSlope, MaxSlope);
131 
132  Last = Current;
133  EdgePoint = NextPointAfter (EdgePoint);
134  }
135  while (EdgePoint != Outline);
136 
137 } /* FindDirectionChanges */
#define DegenerateOutline(O)
Definition: mfoutline.h:68
#define PointAt(O)
Definition: mfoutline.h:69
#define NextPointAfter(E)
Definition: mfoutline.h:70
void ComputeDirection(MFEDGEPT *Start, MFEDGEPT *Finish, float MinSlope, float MaxSlope)
Definition: mfoutline.cpp:363
void FreeMFOutline ( void *  arg)

This routine deallocates all of the memory consumed by a micro-feature outline.

Parameters
argmicro-feature outline to be freed

Definition at line 146 of file mfoutline.cpp.

146  { //MFOUTLINE Outline)
147  MFOUTLINE Start;
148  auto Outline = static_cast<MFOUTLINE>(arg);
149 
150  /* break the circular outline so we can use std. techniques to deallocate */
151  Start = list_rest (Outline);
152  set_rest(Outline, NIL_LIST);
153  while (Start != nullptr) {
154  free(first_node(Start));
155  Start = pop (Start);
156  }
157 
158 } /* FreeMFOutline */
#define set_rest(l, cell)
Definition: oldlist.h:120
#define list_rest(l)
Definition: oldlist.h:91
#define NIL_LIST
Definition: oldlist.h:76
#define first_node(l)
Definition: oldlist.h:92
LIST pop(LIST list)
Definition: oldlist.cpp:202
void FreeOutlines ( LIST  Outlines)

Release all memory consumed by the specified list of outlines.

Parameters
Outlineslist of mf-outlines to be freed

Definition at line 167 of file mfoutline.cpp.

167  {
168  destroy_nodes(Outlines, FreeMFOutline);
169 } /* FreeOutlines */
void FreeMFOutline(void *arg)
Definition: mfoutline.cpp:146
void destroy_nodes(LIST list, void_dest destructor)
Definition: oldlist.cpp:158
void MarkDirectionChanges ( MFOUTLINE  Outline)

This routine searches through the specified outline and finds the points at which the outline changes direction. These points are then marked as "extremities". This routine is used as an alternative to FindExtremities(). It forces the endpoints of the microfeatures to be at the direction changes rather than at the midpoint between direction changes.

Parameters
Outlinemicro-feature outline to analyze

Definition at line 183 of file mfoutline.cpp.

183  {
184  MFOUTLINE Current;
185  MFOUTLINE Last;
186  MFOUTLINE First;
187 
188  if (DegenerateOutline (Outline))
189  return;
190 
191  First = NextDirectionChange (Outline);
192  Last = First;
193  do {
194  Current = NextDirectionChange (Last);
195  MarkPoint (PointAt (Current));
196  Last = Current;
197  }
198  while (Last != First);
199 
200 } /* MarkDirectionChanges */
#define DegenerateOutline(O)
Definition: mfoutline.h:68
#define MarkPoint(P)
Definition: mfoutline.h:75
#define PointAt(O)
Definition: mfoutline.h:69
MFOUTLINE NextDirectionChange(MFOUTLINE EdgePoint)
Definition: mfoutline.cpp:434
MFEDGEPT* NewEdgePoint ( )

Return a new edge point for a micro-feature outline.

Definition at line 205 of file mfoutline.cpp.

205  {
206  return reinterpret_cast<MFEDGEPT *>(malloc(sizeof(MFEDGEPT)));
207 }
MFOUTLINE NextDirectionChange ( MFOUTLINE  EdgePoint)

This routine returns the next point in the micro-feature outline that has a direction different than EdgePoint. The routine assumes that the outline being searched is not a degenerate outline (i.e. it must have 2 or more edge points).

Parameters
EdgePointstart search from this point
Returns
Point of next direction change in micro-feature outline.
Note
Globals: none

Definition at line 434 of file mfoutline.cpp.

434  {
435  DIRECTION InitialDirection;
436 
437  InitialDirection = PointAt (EdgePoint)->Direction;
438 
439  MFOUTLINE next_pt = nullptr;
440  do {
441  EdgePoint = NextPointAfter(EdgePoint);
442  next_pt = NextPointAfter(EdgePoint);
443  } while (PointAt(EdgePoint)->Direction == InitialDirection &&
444  !PointAt(EdgePoint)->Hidden &&
445  next_pt != nullptr && !PointAt(next_pt)->Hidden);
446 
447  return (EdgePoint);
448 }
#define PointAt(O)
Definition: mfoutline.h:69
DIRECTION
Definition: mfoutline.h:34
#define NextPointAfter(E)
Definition: mfoutline.h:70
MFOUTLINE NextExtremity ( MFOUTLINE  EdgePoint)

This routine returns the next point in the micro-feature outline that is an extremity. The search starts after EdgePoint. The routine assumes that the outline being searched is not a degenerate outline (i.e. it must have 2 or more edge points).

Parameters
EdgePointstart search from this point
Returns
Next extremity in the outline after EdgePoint.
Note
Globals: none

Definition at line 220 of file mfoutline.cpp.

220  {
221  EdgePoint = NextPointAfter(EdgePoint);
222  while (!PointAt(EdgePoint)->ExtremityMark)
223  EdgePoint = NextPointAfter(EdgePoint);
224 
225  return (EdgePoint);
226 
227 } /* NextExtremity */
#define PointAt(O)
Definition: mfoutline.h:69
#define NextPointAfter(E)
Definition: mfoutline.h:70
void NormalizeOutline ( MFOUTLINE  Outline,
float  XOrigin 
)

This routine normalizes the coordinates of the specified outline so that the outline is deskewed down to the baseline, translated so that x=0 is at XOrigin, and scaled so that the height of a character cell from descender to ascender is 1. Of this height, 0.25 is for the descender, 0.25 for the ascender, and 0.5 for the x-height. The y coordinate of the baseline is 0.

Parameters
Outlineoutline to be normalized
XOriginx-origin of text

Definition at line 242 of file mfoutline.cpp.

243  {
244  if (Outline == NIL_LIST)
245  return;
246 
247  MFOUTLINE EdgePoint = Outline;
248  do {
249  MFEDGEPT *Current = PointAt(EdgePoint);
250  Current->Point.y = MF_SCALE_FACTOR *
251  (Current->Point.y - kBlnBaselineOffset);
252  Current->Point.x = MF_SCALE_FACTOR * (Current->Point.x - XOrigin);
253  EdgePoint = NextPointAfter(EdgePoint);
254  } while (EdgePoint != Outline);
255 } /* NormalizeOutline */
float y
Definition: fpoint.h:30
const int kBlnBaselineOffset
Definition: normalis.h:25
#define NIL_LIST
Definition: oldlist.h:76
#define PointAt(O)
Definition: mfoutline.h:69
#define NextPointAfter(E)
Definition: mfoutline.h:70
FPOINT Point
Definition: mfoutline.h:46
#define MF_SCALE_FACTOR
Definition: mfoutline.h:65
float x
Definition: fpoint.h:30