46 #include "config_auto.h"
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");
245 fprintf(stderr,
"Please make sure that classes are added to templates");
246 fprintf(stderr,
" in increasing order of ClassIds\n");
312 memset(ProtoSet, 0,
sizeof(*ProtoSet));
355 FLOAT32 EndPad, SidePad, AnglePad;
370 DoFill(&FillSpec, Pruner, ClassMask, ClassCount, WordIndex);
397 cprintf(
"AddProtoToProtoPruner:assert failed: %d < %d",
399 assert(ProtoId < Class->NumProtos);
404 Angle = Proto->
Angle;
406 assert(!
isnan(Angle));
417 Pad =
MAX (fabs (cos (Angle)) * (Length / 2.0 +
426 Pad =
MAX (fabs (sin (Angle)) * (Length / 2.0 +
457 return static_cast<uinT8>(
Modulo(bucket, num_buckets));
461 #ifndef GRAPHICS_DISABLED
474 if (IntMatchWindow !=
NULL)
498 for (ProtoId = 0, TotalLength = 0;
526 assert(ProtoId < Class->NumProtos);
530 Param = Proto->
A * 128;
533 Param = -Proto->
B * 256;
536 Param = Proto->
C * 128;
539 Param = Proto->
Angle * 256;
549 cprintf(
"Converted ffeat to (A=%d,B=%d,C=%d,L=%d)",
576 for (ClassId = 0; ClassId < target_unicharset.
size(); ClassId++) {
577 FClass = &(FloatProtos[ClassId]);
579 strcmp(target_unicharset.
id_to_unichar(ClassId),
" ") != 0) {
580 cprintf(
"Warning: no protos/configs for %s in CreateIntTemplates()\n",
588 for (
int i = 0; i < fs.
size; ++i) {
599 for (ProtoId = 0; ProtoId < FClass->
NumProtos; ProtoId++) {
607 for (ConfigId = 0; ConfigId < FClass->
NumConfigs; ConfigId++) {
612 return (IntTemplates);
617 #ifndef GRAPHICS_DISABLED
633 if (FeatureDisplayWindow) {
655 if (ProtoDisplayWindow) {
691 memset(ProtoSet, 0,
sizeof(*ProtoSet));
771 int i, j, w, x, y, z;
783 int b, bit_number, last_cp_bit_number, new_b, new_i, new_w;
791 uinT32 Mask, NewMask, ClassBits;
798 if (fread(&unicharset_size,
sizeof(
int), 1, File) != 1)
799 cprintf(
"Bad read of inttemp!\n");
801 sizeof(Templates->
NumClasses), 1, File) != 1 ||
804 cprintf(
"Bad read of inttemp!\n");
818 cprintf(
"Bad read of inttemp!\n");
823 if (version_id < 3) {
828 if (version_id < 2) {
829 for (i = 0; i < unicharset_size; ++i) {
830 if (fread(&IndexFor[i],
sizeof(
inT16), 1, File) != 1)
831 cprintf(
"Bad read of inttemp!\n");
834 if (fread(&ClassIdFor[i],
sizeof(
CLASS_ID), 1, File) != 1)
835 cprintf(
"Bad read of inttemp!\n");
851 cprintf(
"Bad read of inttemp!\n");
863 if (version_id < 2) {
864 TempClassPruner[i] = Pruner;
871 if (version_id < 2) {
875 if (ClassIdFor[i] > max_class_id)
876 max_class_id = ClassIdFor[i];
889 if (TempClassPruner[i]->p[x][y][z][w] == 0)
893 if (bit_number > last_cp_bit_number)
897 Mask = SetBitsForMask << b;
898 ClassBits = TempClassPruner[i]->
p[x][y][z][w] & Mask;
905 ClassBits <<= (new_b - b);
907 ClassBits >>= (b - new_b);
911 NewMask = SetBitsForMask << new_b;
912 Templates->
ClassPruners[new_i]->
p[x][y][z][new_w] &= ~NewMask;
913 Templates->
ClassPruners[new_i]->
p[x][y][z][new_w] |= ClassBits;
918 delete TempClassPruner[i];
929 cprintf (
"Bad read of inttemp!\n");
930 if (version_id == 0) {
932 for (j = 0; j < 5; ++j) {
934 if (fread(&junk,
sizeof(junk), 1, File) != 1)
935 cprintf (
"Bad read of inttemp!\n");
938 if (version_id < 4) {
939 for (j = 0; j < MaxNumConfigs; ++j) {
941 cprintf (
"Bad read of inttemp!\n");
945 for (j = 0; j < MaxNumConfigs; j++)
952 cprintf (
"Bad read of inttemp!\n");
956 for (j = 0; j < MaxNumConfigs; j++)
960 if (version_id < 2) {
971 fread((
char *)Lengths,
sizeof(
uinT8),
973 cprintf (
"Bad read of inttemp!\n");
980 if (version_id < 3) {
984 cprintf(
"Bad read of inttemp!\n");
986 if ((nread = fread((
char *) &ProtoSet->
Protos[x].
A, 1,
987 sizeof(
inT8), File)) !=
sizeof(
inT8) ||
988 (nread = fread((
char *) &ProtoSet->
Protos[x].
B, 1,
990 (nread = fread((
char *) &ProtoSet->
Protos[x].
C, 1,
991 sizeof(
inT8), File)) !=
sizeof(
inT8) ||
992 (nread = fread((
char *) &ProtoSet->
Protos[x].
Angle, 1,
994 cprintf(
"Bad read of inttemp!\n");
995 for (y = 0; y < WerdsPerConfigVec; y++)
996 if ((nread = fread((
char *) &ProtoSet->
Protos[x].
Configs[y], 1,
998 cprintf(
"Bad read of inttemp!\n");
1004 cprintf(
"Bad read of inttemp!\n");
1012 for (y = 0; y < WerdsPerConfigVec; y++)
1026 if (version_id < 2) {
1034 if (i < Templates->NumClasses) {
1036 fprintf(stderr,
"Non-contiguous class ids in inttemp\n");
1041 fprintf(stderr,
"Class id %d exceeds NumClassesIn (Templates) %d\n",
1048 if (version_id >= 4) {
1050 if (version_id >= 5) {
1060 delete[] ClassIdFor;
1061 delete[] TempClassPruner;
1067 #ifndef GRAPHICS_DISABLED
1081 if (ProtoDisplayWindow) {
1082 ProtoDisplayWindow->
Clear();
1084 if (FeatureDisplayWindow) {
1085 FeatureDisplayWindow->
Clear();
1092 if (ProtoDisplayWindow) {
1096 if (FeatureDisplayWindow) {
1142 int unicharset_size = target_unicharset.
size();
1143 int version_id = -5;
1145 if (Templates->
NumClasses != unicharset_size) {
1146 cprintf(
"Warning: executing WriteIntTemplates() with %d classes in"
1147 " Templates, while target_unicharset size is %d\n",
1152 fwrite(&unicharset_size,
sizeof(unicharset_size), 1, File);
1153 fwrite(&version_id,
sizeof(version_id), 1, File);
1164 for (i = 0; i < Templates->
NumClasses; i++) {
1165 Class = Templates->
Class[i];
1188 fwrite(&Class->
font_set_id,
sizeof(
int), 1, File);
1217 return (((
FLOAT32) Bucket / NumBuckets) - Offset);
1236 return (((
FLOAT32) (Bucket + 1) / NumBuckets) - Offset);
1256 register uinT32 ClassMask,
1257 register uinT32 ClassCount,
1258 register uinT32 WordIndex) {
1259 register int X, Y, Angle;
1268 if (FillSpec->
YStart < 0)
1273 for (Y = FillSpec->
YStart; Y <= FillSpec->YEnd; Y++)
1276 OldWord = Pruner->
p[X][Y][Angle][WordIndex];
1277 if (ClassCount > (OldWord & ClassMask)) {
1278 OldWord &= ~ClassMask;
1279 OldWord |= ClassCount;
1280 Pruner->
p[X][Y][Angle][WordIndex] = OldWord;
1329 int i, FirstBucket, LastBucket;
1335 if (FirstBucket < 0)
1341 if (debug)
tprintf(
"Circular fill from %d to %d", FirstBucket, LastBucket);
1346 if (i == LastBucket)
1373 int i, FirstBucket, LastBucket;
1376 if (FirstBucket < 0)
1383 if (debug)
tprintf(
"Linear fill from %d to %d", FirstBucket, LastBucket);
1384 for (i = FirstBucket; i <= LastBucket; i++)
1391 #ifndef GRAPHICS_DISABLED
1406 bool* pretrained_on,
int* shape_id) {
1410 int unichar_id = INVALID_UNICHAR_ID;
1419 *adaptive_on =
false;
1420 *pretrained_on =
true;
1421 if (*shape_id >= 0 && *shape_id < shape_table_->NumShapes()) {
1425 tprintf(
"Shape %d, first unichar=%d, font=%d\n",
1426 *shape_id, unichar_id, font_id);
1431 tprintf(
"No shape table loaded!\n");
1437 *adaptive_on =
true;
1438 *pretrained_on =
false;
1441 *adaptive_on =
false;
1442 *pretrained_on =
true;
1444 *adaptive_on =
true;
1445 *pretrained_on =
true;
1457 tprintf(
"Char class '%s' not found in unicharset",
1514 if (*AnglePad > 0.5)
1528 assert (Evidence >= 0.0);
1529 assert (Evidence <= 1.0);
1531 if (Evidence >= 0.90)
1533 else if (Evidence >= 0.75)
1535 else if (Evidence >= 0.50)
1560 Fill->
X = Filler->
X;
1566 while (Filler->
X >= Next->
X) {
1567 Fill->
X = Filler->
X = Next->
X;
1574 Fill->
YEnd = Next->
Y;
1611 #define AS ANGLE_SHIFT
1612 #define NB NUM_CP_BUCKETS
1618 FPOINT Start, Switch1, Switch2, End;
1622 Angle = Proto->
Angle;
1625 HalfLength = Proto->
Length / 2.0;
1653 if ((Angle > 0.0 && Angle < 0.25) || (Angle > 0.5 && Angle < 0.75)) {
1656 Cos = fabs(cos(Angle));
1657 Sin = fabs(sin(Angle));
1660 Start.
x = X - (HalfLength + EndPad) * Cos - SidePad * Sin;
1661 Start.
y = Y - (HalfLength + EndPad) * Sin + SidePad * Cos;
1662 End.
x = 2.0 * X - Start.
x;
1663 End.
y = 2.0 * Y - Start.
y;
1664 Switch1.
x = X - (HalfLength + EndPad) * Cos + SidePad * Sin;
1665 Switch1.
y = Y - (HalfLength + EndPad) * Sin - SidePad * Cos;
1666 Switch2.
x = 2.0 * X - Switch1.
x;
1667 Switch2.
y = 2.0 * Y - Switch1.
y;
1669 if (Switch1.
x > Switch2.
x) {
1680 YAdjust = XAdjust * Cos / Sin;
1682 YAdjust = XAdjust * Sin / Cos;
1689 YAdjust = XAdjust * Sin / Cos;
1697 YAdjust = XAdjust * Cos / Sin;
1706 Cos = fabs(cos(Angle));
1707 Sin = fabs(sin(Angle));
1710 Start.
x = X - (HalfLength + EndPad) * Cos - SidePad * Sin;
1711 Start.
y = Y + (HalfLength + EndPad) * Sin - SidePad * Cos;
1712 End.
x = 2.0 * X - Start.
x;
1713 End.
y = 2.0 * Y - Start.
y;
1714 Switch1.
x = X - (HalfLength + EndPad) * Cos + SidePad * Sin;
1715 Switch1.
y = Y + (HalfLength + EndPad) * Sin + SidePad * Cos;
1716 Switch2.
x = 2.0 * X - Switch1.
x;
1717 Switch2.
y = 2.0 * Y - Switch1.
y;
1719 if (Switch1.
x > Switch2.
x) {
1730 YAdjust = XAdjust * Sin / Cos;
1732 YAdjust = XAdjust * Cos / Sin;
1739 YAdjust = XAdjust * Sin / Cos;
1747 YAdjust = XAdjust * Cos / Sin;
1759 #ifndef GRAPHICS_DISABLED
1775 assert(Feature !=
NULL);
1783 Dx = (Length / 2.0) * cos((Feature->
Theta / 256.0) * 2.0 *
PI -
PI);
1784 Dy = (Length / 2.0) * sin((Feature->
Theta / 256.0) * 2.0 *
PI -
PI);
1787 window->
DrawTo(X + Dx, Y + Dy);
1816 int Xmin, Xmax, Ymin, Ymax;
1821 assert(ProtoId >= 0);
1822 assert(Class !=
NULL);
1823 assert(ProtoId < Class->NumProtos);
1829 Proto = &(ProtoSet->
Protos[ProtoSetIndex]);
1851 Dx = (Length / 2.0) * cos((Proto->
Angle / 256.0) * 2.0 *
PI -
PI);
1852 Dy = (Length / 2.0) * sin((Proto->
Angle / 256.0) * 2.0 *
PI -
PI);
1855 window->
DrawTo(X + Dx, Y + Dy);
1877 cprintf(
"Warning: Param %s truncated from %f to %d!\n",
1880 }
else if (Param > Max) {
1882 cprintf(
"Warning: Param %s truncated from %f to %d!\n",
1886 return static_cast<int>(floor(Param));
1890 #ifndef GRAPHICS_DISABLED
1896 if (IntMatchWindow ==
NULL) {
1901 "x",
"Class to debug");
1903 "x",
"Class to debug");
1905 "x",
"Class to debug");
1907 "0",
"Index to debug");
1908 popup_menu->
BuildMenu(IntMatchWindow,
false);
1917 if (ProtoDisplayWindow ==
NULL) {
1928 if (FeatureDisplayWindow ==
NULL) {
1937 return new ScrollView(name, xpos, ypos, 520, 520, 260, 260,
true);
1939 #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]
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)
bool write_info(FILE *f, const FontInfo &fi)
void * Erealloc(void *ptr, int size)
double classify_cp_angle_pad_tight
double classify_cp_side_pad_loose
bool read_info(FILE *f, FontInfo *fi, bool swap)
INT_CLASS Class[MAX_NUM_CLASSES]
void ClearFeatureSpaceWindow(NORM_METHOD norm_method, ScrollView *window)
#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
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)
bool read_set(FILE *f, FontSet *fs, bool swap)
#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)
#define test_bit(array, bit)
UnicityTableEqEq< int > font_set
UnicityTable< FontInfo > fontinfo_table_
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]
#define MapParam(P, O, N)
#define MAX_NUM_PROTO_SETS
#define ProtoForProtoId(C, P)
double classify_cp_end_pad_medium
bool write_spacing_info(FILE *f, const FontInfo &fi)
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 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()
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()
#define SET_BIT(array, bit)
_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)
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
void cprintf(const char *format,...)
ScrollView * CreateFeatureSpaceWindow(const char *name, int xpos, int ypos)
void GetFirstUnicharAndFont(int shape_id, int *unichar_id, int *font_id) const
SVMenuNode * AddChild(const char *txt)
bool read_spacing_info(FILE *f, FontInfo *fi, bool swap)
const char * string() const
const Shape & GetShape(int shape_id) const
#define WERDS_PER_CONFIG_VEC
ScrollView * FeatureDisplayWindow
struct PROTO_SET_STRUCT * PROTO_SET
uinT32 Configs[WERDS_PER_CONFIG_VEC]
bool write_set(FILE *f, const FontSet &fs)
#define CircularIncrement(i, r)
#define OLD_MAX_NUM_CONFIGS