tesseract  4.00.00dev
mfoutline.cpp File Reference
#include "clusttool.h"
#include "emalloc.h"
#include "mfoutline.h"
#include "blobs.h"
#include "const.h"
#include "mfx.h"
#include "params.h"
#include "classify.h"
#include <math.h>
#include <stdio.h>

Go to the source code of this file.

Namespaces

 tesseract
 

Functions

LIST ConvertBlob (TBLOB *blob)
 
MFOUTLINE ConvertOutline (TESSLINE *outline)
 
LIST ConvertOutlines (TESSLINE *outline, LIST mf_outlines, OUTLINETYPE outline_type)
 
void FindDirectionChanges (MFOUTLINE Outline, FLOAT32 MinSlope, FLOAT32 MaxSlope)
 
void FreeMFOutline (void *arg)
 
void FreeOutlines (LIST Outlines)
 
void MarkDirectionChanges (MFOUTLINE Outline)
 
MFEDGEPTNewEdgePoint ()
 
MFOUTLINE NextExtremity (MFOUTLINE EdgePoint)
 
void NormalizeOutline (MFOUTLINE Outline, FLOAT32 XOrigin)
 
void ChangeDirection (MFOUTLINE Start, MFOUTLINE End, DIRECTION Direction)
 
void CharNormalizeOutline (MFOUTLINE Outline, const DENORM &cn_denorm)
 
void ComputeDirection (MFEDGEPT *Start, MFEDGEPT *Finish, FLOAT32 MinSlope, FLOAT32 MaxSlope)
 
MFOUTLINE NextDirectionChange (MFOUTLINE EdgePoint)
 

Function Documentation

◆ ChangeDirection()

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
Returns
none
Note
Globals: none
Exceptions: none
History: Fri May 4 10:42:04 1990, DSJ, Created.

Definition at line 337 of file mfoutline.cpp.

337  {
338  MFOUTLINE Current;
339 
340  for (Current = Start; Current != End; Current = NextPointAfter (Current))
341  PointAt (Current)->Direction = Direction;
342 
343  PointAt (End)->PreviousDirection = Direction;
344 
345 } /* ChangeDirection */
#define NextPointAfter(E)
Definition: mfoutline.h:68
#define PointAt(O)
Definition: mfoutline.h:67

◆ CharNormalizeOutline()

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
Returns
none
Note
Globals: none
Exceptions: none
History: Fri Dec 14 10:27:11 1990, DSJ, Created.

Definition at line 358 of file mfoutline.cpp.

358  {
359  MFOUTLINE First, Current;
360  MFEDGEPT *CurrentPoint;
361 
362  if (Outline == NIL_LIST)
363  return;
364 
365  First = Outline;
366  Current = First;
367  do {
368  CurrentPoint = PointAt(Current);
369  FCOORD pos(CurrentPoint->Point.x, CurrentPoint->Point.y);
370  cn_denorm.LocalNormTransform(pos, &pos);
371  CurrentPoint->Point.x = (pos.x() - MAX_UINT8 / 2) * MF_SCALE_FACTOR;
372  CurrentPoint->Point.y = (pos.y() - MAX_UINT8 / 2) * MF_SCALE_FACTOR;
373 
374  Current = NextPointAfter(Current);
375  }
376  while (Current != First);
377 
378 } /* CharNormalizeOutline */
Definition: points.h:189
#define NextPointAfter(E)
Definition: mfoutline.h:68
#define MF_SCALE_FACTOR
Definition: mfoutline.h:63
#define MAX_UINT8
Definition: host.h:63
#define PointAt(O)
Definition: mfoutline.h:67
FPOINT Point
Definition: mfoutline.h:40
FLOAT32 x
Definition: fpoint.h:31
#define NIL_LIST
Definition: oldlist.h:126
void LocalNormTransform(const TPOINT &pt, TPOINT *transformed) const
Definition: normalis.cpp:305
FLOAT32 y
Definition: fpoint.h:31

◆ ComputeDirection()

void ComputeDirection ( MFEDGEPT Start,
MFEDGEPT Finish,
FLOAT32  MinSlope,
FLOAT32  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
Returns
none
Note
Globals: none
Exceptions: none
History: 7/25/89, DSJ, Created.

Definition at line 399 of file mfoutline.cpp.

402  {
403  FVECTOR Delta;
404 
405  Delta.x = Finish->Point.x - Start->Point.x;
406  Delta.y = Finish->Point.y - Start->Point.y;
407  if (Delta.x == 0)
408  if (Delta.y < 0) {
409  Start->Slope = -MAX_FLOAT32;
410  Start->Direction = south;
411  }
412  else {
413  Start->Slope = MAX_FLOAT32;
414  Start->Direction = north;
415  }
416  else {
417  Start->Slope = Delta.y / Delta.x;
418  if (Delta.x > 0)
419  if (Delta.y > 0)
420  if (Start->Slope > MinSlope)
421  if (Start->Slope < MaxSlope)
422  Start->Direction = northeast;
423  else
424  Start->Direction = north;
425  else
426  Start->Direction = east;
427  else if (Start->Slope < -MinSlope)
428  if (Start->Slope > -MaxSlope)
429  Start->Direction = southeast;
430  else
431  Start->Direction = south;
432  else
433  Start->Direction = east;
434  else if (Delta.y > 0)
435  if (Start->Slope < -MinSlope)
436  if (Start->Slope > -MaxSlope)
437  Start->Direction = northwest;
438  else
439  Start->Direction = north;
440  else
441  Start->Direction = west;
442  else if (Start->Slope > MinSlope)
443  if (Start->Slope < MaxSlope)
444  Start->Direction = southwest;
445  else
446  Start->Direction = south;
447  else
448  Start->Direction = west;
449  }
450  Finish->PreviousDirection = Start->Direction;
451 }
DIRECTION PreviousDirection
Definition: mfoutline.h:46
FPOINT Point
Definition: mfoutline.h:40
Definition: mfoutline.h:36
FLOAT32 x
Definition: fpoint.h:31
FLOAT32 Slope
Definition: mfoutline.h:41
DIRECTION Direction
Definition: mfoutline.h:45
Definition: fpoint.h:29
#define MAX_FLOAT32
Definition: host.h:66
FLOAT32 y
Definition: fpoint.h:31
Definition: mfoutline.h:36

◆ ConvertBlob()

LIST ConvertBlob ( TBLOB blob)

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

Definition at line 40 of file mfoutline.cpp.

40  {
41  LIST outlines = NIL_LIST;
42  return (blob == NULL)
43  ? NIL_LIST
44  : ConvertOutlines(blob->outlines, outlines, outer);
45 }
LIST ConvertOutlines(TESSLINE *outline, LIST mf_outlines, OUTLINETYPE outline_type)
Definition: mfoutline.cpp:92
TESSLINE * outlines
Definition: blobs.h:377
#define NIL_LIST
Definition: oldlist.h:126

◆ ConvertOutline()

MFOUTLINE ConvertOutline ( TESSLINE outline)

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

Definition at line 50 of file mfoutline.cpp.

50  {
51  MFEDGEPT *NewPoint;
52  MFOUTLINE MFOutline = NIL_LIST;
53  EDGEPT *EdgePoint;
54  EDGEPT *StartPoint;
55  EDGEPT *NextPoint;
56 
57  if (outline == NULL || outline->loop == NULL)
58  return MFOutline;
59 
60  StartPoint = outline->loop;
61  EdgePoint = StartPoint;
62  do {
63  NextPoint = EdgePoint->next;
64 
65  /* filter out duplicate points */
66  if (EdgePoint->pos.x != NextPoint->pos.x ||
67  EdgePoint->pos.y != NextPoint->pos.y) {
68  NewPoint = NewEdgePoint();
69  ClearMark(NewPoint);
70  NewPoint->Hidden = EdgePoint->IsHidden();
71  NewPoint->Point.x = EdgePoint->pos.x;
72  NewPoint->Point.y = EdgePoint->pos.y;
73  MFOutline = push(MFOutline, NewPoint);
74  }
75  EdgePoint = NextPoint;
76  } while (EdgePoint != StartPoint);
77 
78  if (MFOutline != NULL)
79  MakeOutlineCircular(MFOutline);
80  return MFOutline;
81 }
LIST push(LIST list, void *element)
Definition: oldlist.cpp:288
bool IsHidden() const
Definition: blobs.h:153
EDGEPT * next
Definition: blobs.h:169
EDGEPT * loop
Definition: blobs.h:257
FPOINT Point
Definition: mfoutline.h:40
Definition: blobs.h:76
FLOAT32 x
Definition: fpoint.h:31
#define MakeOutlineCircular(O)
Definition: mfoutline.h:69
#define NIL_LIST
Definition: oldlist.h:126
MFEDGEPT * NewEdgePoint()
Definition: mfoutline.cpp:221
inT16 y
Definition: blobs.h:72
TPOINT pos
Definition: blobs.h:163
BOOL8 Hidden
Definition: mfoutline.h:43
#define ClearMark(P)
Definition: mfoutline.h:72
inT16 x
Definition: blobs.h:71
FLOAT32 y
Definition: fpoint.h:31

◆ ConvertOutlines()

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 92 of file mfoutline.cpp.

94  {
95  MFOUTLINE mf_outline;
96 
97  while (outline != NULL) {
98  mf_outline = ConvertOutline(outline);
99  if (mf_outline != NULL)
100  mf_outlines = push(mf_outlines, mf_outline);
101  outline = outline->next;
102  }
103  return mf_outlines;
104 }
TESSLINE * next
Definition: blobs.h:258
LIST push(LIST list, void *element)
Definition: oldlist.cpp:288
MFOUTLINE ConvertOutline(TESSLINE *outline)
Definition: mfoutline.cpp:50

◆ FindDirectionChanges()

void FindDirectionChanges ( MFOUTLINE  Outline,
FLOAT32  MinSlope,
FLOAT32  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
Returns
none
Note
Exceptions: none
History: 7/21/89, DSJ, Created.

Definition at line 121 of file mfoutline.cpp.

123  {
124  MFEDGEPT *Current;
125  MFEDGEPT *Last;
126  MFOUTLINE EdgePoint;
127 
128  if (DegenerateOutline (Outline))
129  return;
130 
131  Last = PointAt (Outline);
132  Outline = NextPointAfter (Outline);
133  EdgePoint = Outline;
134  do {
135  Current = PointAt (EdgePoint);
136  ComputeDirection(Last, Current, MinSlope, MaxSlope);
137 
138  Last = Current;
139  EdgePoint = NextPointAfter (EdgePoint);
140  }
141  while (EdgePoint != Outline);
142 
143 } /* FindDirectionChanges */
#define NextPointAfter(E)
Definition: mfoutline.h:68
#define PointAt(O)
Definition: mfoutline.h:67
void ComputeDirection(MFEDGEPT *Start, MFEDGEPT *Finish, FLOAT32 MinSlope, FLOAT32 MaxSlope)
Definition: mfoutline.cpp:399
#define DegenerateOutline(O)
Definition: mfoutline.h:66

◆ FreeMFOutline()

void FreeMFOutline ( void *  arg)

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

Parameters
argmicro-feature outline to be freed
Returns
none
Note
Exceptions: none
History: 7/27/89, DSJ, Created.

Definition at line 155 of file mfoutline.cpp.

155  { //MFOUTLINE Outline)
156  MFOUTLINE Start;
157  MFOUTLINE Outline = (MFOUTLINE) arg;
158 
159  /* break the circular outline so we can use std. techniques to deallocate */
160  Start = list_rest (Outline);
161  set_rest(Outline, NIL_LIST);
162  while (Start != NULL) {
163  free(first_node(Start));
164  Start = pop (Start);
165  }
166 
167 } /* FreeMFOutline */
LIST pop(LIST list)
Definition: oldlist.cpp:271
#define list_rest(l)
Definition: oldlist.h:138
#define set_rest(l, cell)
Definition: oldlist.h:222
#define NIL_LIST
Definition: oldlist.h:126
LIST MFOUTLINE
Definition: mfoutline.h:33
#define first_node(l)
Definition: oldlist.h:139

◆ FreeOutlines()

void FreeOutlines ( LIST  Outlines)

Release all memory consumed by the specified list of outlines.

Parameters
Outlineslist of mf-outlines to be freed
Returns
none
Note
Exceptions: none
History: Thu Dec 13 16:14:50 1990, DSJ, Created.

Definition at line 179 of file mfoutline.cpp.

179  {
180  destroy_nodes(Outlines, FreeMFOutline);
181 } /* FreeOutlines */
void FreeMFOutline(void *arg)
Definition: mfoutline.cpp:155
void destroy_nodes(LIST list, void_dest destructor)
Definition: oldlist.cpp:191

◆ MarkDirectionChanges()

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
Returns
none
Note
Globals: none
Exceptions: none
History: 6/29/90, DSJ, Created.

Definition at line 199 of file mfoutline.cpp.

199  {
200  MFOUTLINE Current;
201  MFOUTLINE Last;
202  MFOUTLINE First;
203 
204  if (DegenerateOutline (Outline))
205  return;
206 
207  First = NextDirectionChange (Outline);
208  Last = First;
209  do {
210  Current = NextDirectionChange (Last);
211  MarkPoint (PointAt (Current));
212  Last = Current;
213  }
214  while (Last != First);
215 
216 } /* MarkDirectionChanges */
MFOUTLINE NextDirectionChange(MFOUTLINE EdgePoint)
Definition: mfoutline.cpp:464
#define PointAt(O)
Definition: mfoutline.h:67
#define DegenerateOutline(O)
Definition: mfoutline.h:66
#define MarkPoint(P)
Definition: mfoutline.h:73

◆ NewEdgePoint()

MFEDGEPT* NewEdgePoint ( )

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

Definition at line 221 of file mfoutline.cpp.

221  {
222  return reinterpret_cast<MFEDGEPT *>(malloc(sizeof(MFEDGEPT)));
223 }

◆ NextDirectionChange()

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
Exceptions: none
History: 7/25/89, DSJ, Created.

Definition at line 464 of file mfoutline.cpp.

464  {
465  DIRECTION InitialDirection;
466 
467  InitialDirection = PointAt (EdgePoint)->Direction;
468 
469  MFOUTLINE next_pt = NULL;
470  do {
471  EdgePoint = NextPointAfter(EdgePoint);
472  next_pt = NextPointAfter(EdgePoint);
473  } while (PointAt(EdgePoint)->Direction == InitialDirection &&
474  !PointAt(EdgePoint)->Hidden &&
475  next_pt != NULL && !PointAt(next_pt)->Hidden);
476 
477  return (EdgePoint);
478 }
#define NextPointAfter(E)
Definition: mfoutline.h:68
DIRECTION
Definition: mfoutline.h:35
#define PointAt(O)
Definition: mfoutline.h:67

◆ NextExtremity()

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
Exceptions: none
History: 7/26/89, DSJ, Created.

Definition at line 238 of file mfoutline.cpp.

238  {
239  EdgePoint = NextPointAfter(EdgePoint);
240  while (!PointAt(EdgePoint)->ExtremityMark)
241  EdgePoint = NextPointAfter(EdgePoint);
242 
243  return (EdgePoint);
244 
245 } /* NextExtremity */
#define NextPointAfter(E)
Definition: mfoutline.h:68
#define PointAt(O)
Definition: mfoutline.h:67

◆ NormalizeOutline()

void NormalizeOutline ( MFOUTLINE  Outline,
FLOAT32  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
Returns
none
Note
Globals: none
Exceptions: none
History: 8/2/89, DSJ, Created.

Definition at line 264 of file mfoutline.cpp.

265  {
266  if (Outline == NIL_LIST)
267  return;
268 
269  MFOUTLINE EdgePoint = Outline;
270  do {
271  MFEDGEPT *Current = PointAt(EdgePoint);
272  Current->Point.y = MF_SCALE_FACTOR *
273  (Current->Point.y - kBlnBaselineOffset);
274  Current->Point.x = MF_SCALE_FACTOR * (Current->Point.x - XOrigin);
275  EdgePoint = NextPointAfter(EdgePoint);
276  } while (EdgePoint != Outline);
277 } /* NormalizeOutline */
#define NextPointAfter(E)
Definition: mfoutline.h:68
#define MF_SCALE_FACTOR
Definition: mfoutline.h:63
#define PointAt(O)
Definition: mfoutline.h:67
FPOINT Point
Definition: mfoutline.h:40
FLOAT32 x
Definition: fpoint.h:31
#define NIL_LIST
Definition: oldlist.h:126
const int kBlnBaselineOffset
Definition: normalis.h:29
FLOAT32 y
Definition: fpoint.h:31