54 #define PROTO_PRUNER_SCALE (4.0)
56 #define INT_DESCENDER (0.0 * INT_CHAR_NORM_RANGE)
57 #define INT_BASELINE (0.25 * INT_CHAR_NORM_RANGE)
58 #define INT_XHEIGHT (0.75 * INT_CHAR_NORM_RANGE)
59 #define INT_CAPHEIGHT (1.0 * INT_CHAR_NORM_RANGE)
61 #define INT_XCENTER (0.5 * INT_CHAR_NORM_RANGE)
62 #define INT_YCENTER (0.5 * INT_CHAR_NORM_RANGE)
63 #define INT_XRADIUS (0.2 * INT_CHAR_NORM_RANGE)
64 #define INT_YRADIUS (0.2 * INT_CHAR_NORM_RANGE)
67 #define INT_MAX_X INT_CHAR_NORM_RANGE
68 #define INT_MAX_Y INT_CHAR_NORM_RANGE
71 #define HV_TOLERANCE (0.0025)
76 #define MAX_NUM_SWITCHES 3
114 #define OLD_MAX_NUM_CONFIGS 32
115 #define OLD_WERDS_PER_CONFIG_VEC ((OLD_MAX_NUM_CONFIGS + BITS_PER_WERD - 1) /\
122 #define CircularIncrement(i,r) (((i) < (r) - 1)?((i)++):((i) = 0))
125 #define MapParam(P,O,N) (floor (((P) + (O)) * (N)))
136 register uinT32 ClassMask,
137 register uinT32 ClassCount,
138 register uinT32 WordIndex);
164 #ifndef GRAPHICS_DISABLED
172 #endif // GRAPHICS_DISABLED
192 "Class Pruner Angle Pad Loose");
194 "Class Pruner Angle Pad Medium");
196 "CLass Pruner Angle Pad Tight");
246 fprintf(stderr,
"Please make sure that classes are added to templates");
247 fprintf(stderr,
" in increasing order of ClassIds\n");
315 memset(ProtoSet, 0,
sizeof(*ProtoSet));
359 FLOAT32 EndPad, SidePad, AnglePad;
374 DoFill(&FillSpec, Pruner, ClassMask, ClassCount, WordIndex);
402 cprintf(
"AddProtoToProtoPruner:assert failed: %d < %d",
404 assert(ProtoId < Class->NumProtos);
409 Angle = Proto->
Angle;
411 assert(!
isnan(Angle));
422 Pad =
MAX (fabs (cos (Angle)) * (Length / 2.0 +
431 Pad =
MAX (fabs (sin (Angle)) * (Length / 2.0 +
460 return static_cast<uinT8>(
Modulo(bucket, num_buckets));
465 #ifndef GRAPHICS_DISABLED
478 if (IntMatchWindow !=
NULL)
504 for (ProtoId = 0, TotalLength = 0;
534 assert(ProtoId < Class->NumProtos);
538 Param = Proto->
A * 128;
541 Param = -Proto->
B * 256;
544 Param = Proto->
C * 128;
547 Param = Proto->
Angle * 256;
557 cprintf(
"Converted ffeat to (A=%d,B=%d,C=%d,L=%d)",
585 for (ClassId = 0; ClassId < target_unicharset.
size(); ClassId++) {
586 FClass = &(FloatProtos[ClassId]);
588 strcmp(target_unicharset.
id_to_unichar(ClassId),
" ") != 0) {
589 cprintf(
"Warning: no protos/configs for %s in CreateIntTemplates()\n",
597 for (
int i = 0; i < fs.
size; ++i) {
608 for (ProtoId = 0; ProtoId < FClass->
NumProtos; ProtoId++) {
616 for (ConfigId = 0; ConfigId < FClass->
NumConfigs; ConfigId++) {
621 return (IntTemplates);
627 #ifndef GRAPHICS_DISABLED
643 if (FeatureDisplayWindow) {
666 if (ProtoDisplayWindow) {
704 memset(ProtoSet, 0,
sizeof(*ProtoSet));
788 int i, j, w, x, y, z;
800 int b, bit_number, last_cp_bit_number, new_b, new_i, new_w;
808 uinT32 Mask, NewMask, ClassBits;
815 if (fread(&unicharset_size,
sizeof(
int), 1, File) != 1)
816 cprintf(
"Bad read of inttemp!\n");
818 sizeof(Templates->
NumClasses), 1, File) != 1 ||
821 cprintf(
"Bad read of inttemp!\n");
835 cprintf(
"Bad read of inttemp!\n");
840 if (version_id < 3) {
845 if (version_id < 2) {
846 for (i = 0; i < unicharset_size; ++i) {
847 if (fread(&IndexFor[i],
sizeof(
inT16), 1, File) != 1)
848 cprintf(
"Bad read of inttemp!\n");
851 if (fread(&ClassIdFor[i],
sizeof(
CLASS_ID), 1, File) != 1)
852 cprintf(
"Bad read of inttemp!\n");
868 cprintf(
"Bad read of inttemp!\n");
880 if (version_id < 2) {
881 TempClassPruner[i] = Pruner;
888 if (version_id < 2) {
892 if (ClassIdFor[i] > max_class_id)
893 max_class_id = ClassIdFor[i];
906 if (TempClassPruner[i]->p[x][y][z][w] == 0)
910 if (bit_number > last_cp_bit_number)
914 Mask = SetBitsForMask << b;
915 ClassBits = TempClassPruner[i]->
p[x][y][z][w] & Mask;
922 ClassBits <<= (new_b - b);
924 ClassBits >>= (b - new_b);
928 NewMask = SetBitsForMask << new_b;
929 Templates->
ClassPruners[new_i]->
p[x][y][z][new_w] &= ~NewMask;
930 Templates->
ClassPruners[new_i]->
p[x][y][z][new_w] |= ClassBits;
935 delete TempClassPruner[i];
946 cprintf (
"Bad read of inttemp!\n");
947 if (version_id == 0) {
949 for (j = 0; j < 5; ++j) {
951 if (fread(&junk,
sizeof(junk), 1, File) != 1)
952 cprintf (
"Bad read of inttemp!\n");
955 if (version_id < 4) {
956 for (j = 0; j < MaxNumConfigs; ++j) {
958 cprintf (
"Bad read of inttemp!\n");
962 for (j = 0; j < MaxNumConfigs; j++)
969 cprintf (
"Bad read of inttemp!\n");
973 for (j = 0; j < MaxNumConfigs; j++)
977 if (version_id < 2) {
988 fread((
char *)Lengths,
sizeof(
uinT8),
990 cprintf (
"Bad read of inttemp!\n");
997 if (version_id < 3) {
1001 cprintf(
"Bad read of inttemp!\n");
1003 if ((nread = fread((
char *) &ProtoSet->
Protos[x].
A, 1,
1004 sizeof(
inT8), File)) !=
sizeof(
inT8) ||
1005 (nread = fread((
char *) &ProtoSet->
Protos[x].
B, 1,
1007 (nread = fread((
char *) &ProtoSet->
Protos[x].
C, 1,
1008 sizeof(
inT8), File)) !=
sizeof(
inT8) ||
1009 (nread = fread((
char *) &ProtoSet->
Protos[x].
Angle, 1,
1011 cprintf(
"Bad read of inttemp!\n");
1012 for (y = 0; y < WerdsPerConfigVec; y++)
1013 if ((nread = fread((
char *) &ProtoSet->
Protos[x].
Configs[y], 1,
1015 cprintf(
"Bad read of inttemp!\n");
1021 cprintf(
"Bad read of inttemp!\n");
1029 for (y = 0; y < WerdsPerConfigVec; y++)
1043 if (version_id < 2) {
1051 if (i < Templates->NumClasses) {
1053 fprintf(stderr,
"Non-contiguous class ids in inttemp\n");
1058 fprintf(stderr,
"Class id %d exceeds NumClassesIn (Templates) %d\n",
1065 if (version_id >= 4) {
1067 if (version_id >= 5) {
1077 delete[] ClassIdFor;
1078 delete[] TempClassPruner;
1085 #ifndef GRAPHICS_DISABLED
1099 if (ProtoDisplayWindow) {
1100 ProtoDisplayWindow->
Clear();
1102 if (FeatureDisplayWindow) {
1103 FeatureDisplayWindow->
Clear();
1110 if (ProtoDisplayWindow) {
1114 if (FeatureDisplayWindow) {
1161 int unicharset_size = target_unicharset.
size();
1162 int version_id = -5;
1164 if (Templates->
NumClasses != unicharset_size) {
1165 cprintf(
"Warning: executing WriteIntTemplates() with %d classes in"
1166 " Templates, while target_unicharset size is %d\n",
1171 fwrite(&unicharset_size,
sizeof(unicharset_size), 1, File);
1172 fwrite(&version_id,
sizeof(version_id), 1, File);
1183 for (i = 0; i < Templates->
NumClasses; i++) {
1184 Class = Templates->
Class[i];
1207 fwrite(&Class->
font_set_id,
sizeof(
int), 1, File);
1238 return (((
FLOAT32) Bucket / NumBuckets) - Offset);
1259 return (((
FLOAT32) (Bucket + 1) / NumBuckets) - Offset);
1266 register uinT32 ClassMask,
1267 register uinT32 ClassCount,
1268 register uinT32 WordIndex) {
1284 register int X, Y, Angle;
1293 if (FillSpec->
YStart < 0)
1298 for (Y = FillSpec->
YStart; Y <= FillSpec->YEnd; Y++)
1301 OldWord = Pruner->
p[X][Y][Angle][WordIndex];
1302 if (ClassCount > (OldWord & ClassMask)) {
1303 OldWord &= ~ClassMask;
1304 OldWord |= ClassCount;
1305 Pruner->
p[X][Y][Angle][WordIndex] = OldWord;
1357 int i, FirstBucket, LastBucket;
1363 if (FirstBucket < 0)
1369 if (debug)
tprintf(
"Circular fill from %d to %d", FirstBucket, LastBucket);
1374 if (i == LastBucket)
1402 int i, FirstBucket, LastBucket;
1405 if (FirstBucket < 0)
1412 if (debug)
tprintf(
"Linear fill from %d to %d", FirstBucket, LastBucket);
1413 for (i = FirstBucket; i <= LastBucket; i++)
1420 #ifndef GRAPHICS_DISABLED
1423 bool* pretrained_on,
int* shape_id) {
1437 int unichar_id = INVALID_UNICHAR_ID;
1446 *adaptive_on =
false;
1447 *pretrained_on =
true;
1448 if (*shape_id >= 0 && *shape_id < shape_table_->NumShapes()) {
1452 tprintf(
"Shape %d, first unichar=%d, font=%d\n",
1453 *shape_id, unichar_id, font_id);
1458 tprintf(
"No shape table loaded!\n");
1464 *adaptive_on =
true;
1465 *pretrained_on =
false;
1468 *adaptive_on =
false;
1469 *pretrained_on =
true;
1471 *adaptive_on =
true;
1472 *pretrained_on =
true;
1484 tprintf(
"Char class '%s' not found in unicharset",
1543 if (*AnglePad > 0.5)
1561 assert (Evidence >= 0.0);
1562 assert (Evidence <= 1.0);
1564 if (Evidence >= 0.90)
1566 else if (Evidence >= 0.75)
1568 else if (Evidence >= 0.50)
1595 Fill->
X = Filler->
X;
1601 while (Filler->
X >= Next->
X) {
1602 Fill->
X = Filler->
X = Next->
X;
1609 Fill->
YEnd = Next->
Y;
1648 #define AS ANGLE_SHIFT
1649 #define NB NUM_CP_BUCKETS
1655 FPOINT Start, Switch1, Switch2, End;
1659 Angle = Proto->
Angle;
1662 HalfLength = Proto->
Length / 2.0;
1690 if ((Angle > 0.0 && Angle < 0.25) || (Angle > 0.5 && Angle < 0.75)) {
1693 Cos = fabs(cos(Angle));
1694 Sin = fabs(sin(Angle));
1697 Start.
x = X - (HalfLength + EndPad) * Cos - SidePad * Sin;
1698 Start.
y = Y - (HalfLength + EndPad) * Sin + SidePad * Cos;
1699 End.
x = 2.0 * X - Start.
x;
1700 End.
y = 2.0 * Y - Start.
y;
1701 Switch1.
x = X - (HalfLength + EndPad) * Cos + SidePad * Sin;
1702 Switch1.
y = Y - (HalfLength + EndPad) * Sin - SidePad * Cos;
1703 Switch2.
x = 2.0 * X - Switch1.
x;
1704 Switch2.
y = 2.0 * Y - Switch1.
y;
1706 if (Switch1.
x > Switch2.
x) {
1717 YAdjust = XAdjust * Cos / Sin;
1719 YAdjust = XAdjust * Sin / Cos;
1726 YAdjust = XAdjust * Sin / Cos;
1734 YAdjust = XAdjust * Cos / Sin;
1743 Cos = fabs(cos(Angle));
1744 Sin = fabs(sin(Angle));
1747 Start.
x = X - (HalfLength + EndPad) * Cos - SidePad * Sin;
1748 Start.
y = Y + (HalfLength + EndPad) * Sin - SidePad * Cos;
1749 End.
x = 2.0 * X - Start.
x;
1750 End.
y = 2.0 * Y - Start.
y;
1751 Switch1.
x = X - (HalfLength + EndPad) * Cos + SidePad * Sin;
1752 Switch1.
y = Y + (HalfLength + EndPad) * Sin + SidePad * Cos;
1753 Switch2.
x = 2.0 * X - Switch1.
x;
1754 Switch2.
y = 2.0 * Y - Switch1.
y;
1756 if (Switch1.
x > Switch2.
x) {
1767 YAdjust = XAdjust * Sin / Cos;
1769 YAdjust = XAdjust * Cos / Sin;
1776 YAdjust = XAdjust * Sin / Cos;
1784 YAdjust = XAdjust * Cos / Sin;
1796 #ifndef GRAPHICS_DISABLED
1813 assert(Feature !=
NULL);
1821 Dx = (Length / 2.0) * cos((Feature->
Theta / 256.0) * 2.0 *
PI -
PI);
1822 Dy = (Length / 2.0) * sin((Feature->
Theta / 256.0) * 2.0 *
PI -
PI);
1825 window->
DrawTo(X + Dx, Y + Dy);
1854 int Xmin, Xmax, Ymin, Ymax;
1859 assert(ProtoId >= 0);
1860 assert(Class !=
NULL);
1861 assert(ProtoId < Class->NumProtos);
1867 Proto = &(ProtoSet->
Protos[ProtoSetIndex]);
1889 Dx = (Length / 2.0) * cos((Proto->
Angle / 256.0) * 2.0 *
PI -
PI);
1890 Dy = (Length / 2.0) * sin((Proto->
Angle / 256.0) * 2.0 *
PI -
PI);
1893 window->
DrawTo(X + Dx, Y + Dy);
1916 cprintf(
"Warning: Param %s truncated from %f to %d!\n",
1919 }
else if (Param > Max) {
1921 cprintf(
"Warning: Param %s truncated from %f to %d!\n",
1925 return static_cast<int>(floor(Param));
1930 #ifndef GRAPHICS_DISABLED
1936 if (IntMatchWindow ==
NULL) {
1941 "x",
"Class to debug");
1943 "x",
"Class to debug");
1945 "x",
"Class to debug");
1947 "0",
"Index to debug");
1948 popup_menu->
BuildMenu(IntMatchWindow,
false);
1957 if (ProtoDisplayWindow ==
NULL) {
1968 if (FeatureDisplayWindow ==
NULL) {
1977 return new ScrollView(name, xpos, ypos, 520, 520, 260, 260,
true);
1979 #endif // GRAPHICS_DISABLED
uinT16 Bucket16For(FLOAT32 param, FLOAT32 offset, int num_buckets)
#define PPrunerMaskFor(I)
void AddProtoToProtoPruner(PROTO Proto, int ProtoId, INT_CLASS Class, bool debug)
CLASS_PRUNER_STRUCT * ClassPruners[MAX_NUM_CLASS_PRUNERS]
void ClearFeatureSpaceWindow(NORM_METHOD norm_method, ScrollView *window)
const UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
void DisplayIntFeature(const INT_FEATURE_STRUCT *Feature, FLOAT32 Evidence)
#define INT_CHAR_NORM_RANGE
FLOAT32 BucketEnd(int Bucket, FLOAT32 Offset, int NumBuckets)
double classify_cp_angle_pad_tight
double classify_cp_side_pad_loose
INT_CLASS Class[MAX_NUM_CLASSES]
#define double_VAR(name, val, comment)
#define ProtoIn(Class, Pid)
double classify_cp_end_pad_tight
#define MaxNumIntProtosIn(C)
#define WERDS_PER_PP_VECTOR
INT_PROTO_STRUCT Protos[PROTOS_PER_PROTO_SET]
uinT8 Bucket8For(FLOAT32 param, FLOAT32 offset, int num_buckets)
int classify_num_cp_levels
void DrawTo(int x, int y)
void BuildMenu(ScrollView *sv, bool menu_bar=true)
struct INT_CLASS_STRUCT * INT_CLASS
void Reverse32(void *ptr)
void InitIntMatchWindowIfReqd()
ScrollView * ProtoDisplayWindow
bool read_set(FILE *f, FontSet *fs, bool swap)
void InitFeatureDisplayWindowIfReqd()
uinT32 PROTO_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR]
double classify_cp_side_pad_tight
void ConvertProto(PROTO Proto, int ProtoId, INT_CLASS Class)
void DisplayIntProto(INT_CLASS Class, PROTO_ID ProtoId, FLOAT32 Evidence)
#define CPrunerWordIndexFor(c)
void AddProtoToClassPruner(PROTO Proto, CLASS_ID ClassId, INT_TEMPLATES Templates)
void UpdateRange(const T1 &x, T2 *lower_bound, T2 *upper_bound)
INT_CLASS NewIntClass(int MaxNumProtos, int MaxNumConfigs)
UnicityTableEqEq< int > font_set
UnicityTable< FontInfo > fontinfo_table_
bool write_info(FILE *f, const FontInfo &fi)
uinT16 ConfigLengths[MAX_NUM_CONFIGS]
bool ContainsUnichar(int unichar_id) const
double classify_pp_end_pad
INT_TEMPLATES CreateIntTemplates(CLASSES FloatProtos, const UNICHARSET &target_unicharset)
void DoFill(FILL_SPEC *FillSpec, CLASS_PRUNER_STRUCT *Pruner, register uinT32 ClassMask, register uinT32 ClassCount, register uinT32 WordIndex)
int AddIntConfig(INT_CLASS Class)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
int classify_learning_debug_level
#define WERDS_PER_CP_VECTOR
PROTO_SET ProtoSets[MAX_NUM_PROTO_SETS]
bool write_spacing_info(FILE *f, const FontInfo &fi)
#define MapParam(P, O, N)
#define MAX_NUM_PROTO_SETS
#define ProtoForProtoId(C, P)
double classify_cp_end_pad_medium
FLOAT32 BucketStart(int Bucket, FLOAT32 Offset, int NumBuckets)
double classify_cp_angle_pad_medium
#define MAX_NUM_CLASS_PRUNERS
double classify_pp_side_pad
ShapeTable * shape_table_
ScrollView * IntMatchWindow
int AddIntProto(INT_CLASS Class)
void InitProtoDisplayWindowIfReqd()
void RenderIntProto(ScrollView *window, INT_CLASS Class, PROTO_ID ProtoId, ScrollView::Color color)
void * Erealloc(void *ptr, int size)
void free_int_class(INT_CLASS int_class)
void ZoomToRectangle(int x1, int y1, int x2, int y2)
void SetCursor(int x, int y)
void ConvertConfig(BIT_VECTOR Config, int ConfigId, INT_CLASS Class)
const char *const id_to_unichar(UNICHAR_ID id) const
INT_TEMPLATES NewIntTemplates()
#define test_bit(array, bit)
double classify_pp_angle_pad
void AddIntClass(INT_TEMPLATES Templates, CLASS_ID ClassId, INT_CLASS Class)
SVEvent * AwaitEvent(SVEventType type)
void FillPPCircularBits(uinT32 ParamTable[NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR], int Bit, FLOAT32 Center, FLOAT32 Spread, bool debug)
INT_TEMPLATES ReadIntTemplates(FILE *File)
#define OLD_WERDS_PER_CONFIG_VEC
#define GetPicoFeatureLength()
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
uinT8 CircBucketFor(FLOAT32 param, FLOAT32 offset, int num_buckets)
void RenderIntFeature(ScrollView *window, const INT_FEATURE_STRUCT *Feature, ScrollView::Color color)
double classify_cp_end_pad_loose
void GetCPPadsForLevel(int Level, FLOAT32 *EndPad, FLOAT32 *SidePad, FLOAT32 *AnglePad)
void UpdateMatchDisplay()
#define PPrunerWordIndexFor(I)
void Reverse16(void *ptr)
#define INT_VAR(name, val, comment)
void InitTableFiller(FLOAT32 EndPad, FLOAT32 SidePad, FLOAT32 AnglePad, PROTO Proto, TABLE_FILLER *Filler)
struct INT_TEMPLATES_STRUCT * INT_TEMPLATES
#define UnusedClassIdIn(T, c)
#define MaxNumClassesIn(T)
int TruncateParam(FLOAT32 Param, int Min, int Max, char *Id)
#define PROTO_PRUNER_SCALE
#define ClassForClassId(T, c)
ScrollView::Color GetMatchColorFor(FLOAT32 Evidence)
STRING DebugStr(int shape_id) const
const T & get(int id) const
Return the object from an id.
void GetNextFill(TABLE_FILLER *Filler, FILL_SPEC *Fill)
#define CPrunerMaskFor(L, c)
#define CPrunerBitIndexFor(c)
void free_int_templates(INT_TEMPLATES templates)
int size() const
Return the size used.
#define PROTOS_PER_PROTO_SET
int IntCastRounded(double x)
double classify_cp_side_pad_medium
BOOL8 FillerDone(TABLE_FILLER *Filler)
#define BITS_PER_CP_VECTOR
#define NUM_BITS_PER_CLASS
FILL_SWITCH Switch[MAX_NUM_SWITCHES]
void FillPPLinearBits(uinT32 ParamTable[NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR], int Bit, FLOAT32 Center, FLOAT32 Spread, bool debug)
CLASS_ID GetClassToDebug(const char *Prompt, bool *adaptive_on, bool *pretrained_on, int *shape_id)
void Rectangle(int x1, int y1, int x2, int y2)
#define SET_BIT(array, bit)
void WriteIntTemplates(FILE *File, INT_TEMPLATES Templates, const UNICHARSET &target_unicharset)
UnicityTable< FontSet > fontset_table_
uinT32 p[NUM_CP_BUCKETS][NUM_CP_BUCKETS][NUM_CP_BUCKETS][WERDS_PER_CP_VECTOR]
double classify_cp_angle_pad_loose
bool contains_unichar(const char *const unichar_repr) const
bool read_spacing_info(FILE *f, FontInfo *fi, bool swap)
ScrollView * CreateFeatureSpaceWindow(const char *name, int xpos, int ypos)
bool read_info(FILE *f, FontInfo *fi, bool swap)
void GetFirstUnicharAndFont(int shape_id, int *unichar_id, int *font_id) const
SVMenuNode * AddChild(const char *txt)
void cprintf(const char *format,...)
const char * string() const
const Shape & GetShape(int shape_id) const
bool write_set(FILE *f, const FontSet &fs)
#define WERDS_PER_CONFIG_VEC
ScrollView * FeatureDisplayWindow
struct PROTO_SET_STRUCT * PROTO_SET
uinT32 Configs[WERDS_PER_CONFIG_VEC]
#define CircularIncrement(i, r)
#define OLD_MAX_NUM_CONFIGS