tesseract  4.1.1
adaptive.h File Reference
#include <cstdio>
#include "intproto.h"
#include "oldlist.h"

Go to the source code of this file.

Classes

struct  TEMP_PROTO_STRUCT
 
struct  TEMP_CONFIG_STRUCT
 
struct  PERM_CONFIG_STRUCT
 
union  ADAPTED_CONFIG
 
struct  ADAPT_CLASS_STRUCT
 
struct  ADAPT_TEMPLATES_STRUCT
 

Macros

#define NumNonEmptyClassesIn(Template)   ((Template)->NumNonEmptyClasses)
 
#define IsEmptyAdaptedClass(Class)   ((Class)->NumPermConfigs == 0 && (Class)->TempProtos == NIL_LIST)
 
#define ConfigIsPermanent(Class, ConfigId)   (test_bit((Class)->PermConfigs, ConfigId))
 
#define MakeConfigPermanent(Class, ConfigId)   (SET_BIT((Class)->PermConfigs, ConfigId))
 
#define MakeProtoPermanent(Class, ProtoId)   (SET_BIT((Class)->PermProtos, ProtoId))
 
#define TempConfigFor(Class, ConfigId)   ((Class)->Config[ConfigId].Temp)
 
#define PermConfigFor(Class, ConfigId)   ((Class)->Config[ConfigId].Perm)
 
#define IncreaseConfidence(TempConfig)   ((TempConfig)->NumTimesSeen++)
 

Typedefs

using TEMP_PROTO = TEMP_PROTO_STRUCT *
 
using TEMP_CONFIG = TEMP_CONFIG_STRUCT *
 
using PERM_CONFIG = PERM_CONFIG_STRUCT *
 
using ADAPT_CLASS = ADAPT_CLASS_STRUCT *
 
using ADAPT_TEMPLATES = ADAPT_TEMPLATES_STRUCT *
 

Functions

void AddAdaptedClass (ADAPT_TEMPLATES Templates, ADAPT_CLASS Class, CLASS_ID ClassId)
 
void FreeTempProto (void *arg)
 
void FreeTempConfig (TEMP_CONFIG Config)
 
ADAPT_CLASS NewAdaptedClass ()
 
void free_adapted_class (ADAPT_CLASS adapt_class)
 
void free_adapted_templates (ADAPT_TEMPLATES templates)
 
TEMP_CONFIG NewTempConfig (int MaxProtoId, int FontinfoId)
 
TEMP_PROTO NewTempProto ()
 
ADAPT_CLASS ReadAdaptedClass (tesseract::TFile *File)
 
PERM_CONFIG ReadPermConfig (tesseract::TFile *File)
 
TEMP_CONFIG ReadTempConfig (tesseract::TFile *File)
 
void WriteAdaptedClass (FILE *File, ADAPT_CLASS Class, int NumConfigs)
 
void WritePermConfig (FILE *File, PERM_CONFIG Config)
 
void WriteTempConfig (FILE *File, TEMP_CONFIG Config)
 

Macro Definition Documentation

◆ ConfigIsPermanent

#define ConfigIsPermanent (   Class,
  ConfigId 
)    (test_bit((Class)->PermConfigs, ConfigId))

Definition at line 83 of file adaptive.h.

◆ IncreaseConfidence

#define IncreaseConfidence (   TempConfig)    ((TempConfig)->NumTimesSeen++)

Definition at line 96 of file adaptive.h.

◆ IsEmptyAdaptedClass

#define IsEmptyAdaptedClass (   Class)    ((Class)->NumPermConfigs == 0 && (Class)->TempProtos == NIL_LIST)

Definition at line 80 of file adaptive.h.

◆ MakeConfigPermanent

#define MakeConfigPermanent (   Class,
  ConfigId 
)    (SET_BIT((Class)->PermConfigs, ConfigId))

Definition at line 86 of file adaptive.h.

◆ MakeProtoPermanent

#define MakeProtoPermanent (   Class,
  ProtoId 
)    (SET_BIT((Class)->PermProtos, ProtoId))

Definition at line 89 of file adaptive.h.

◆ NumNonEmptyClassesIn

#define NumNonEmptyClassesIn (   Template)    ((Template)->NumNonEmptyClasses)

Definition at line 78 of file adaptive.h.

◆ PermConfigFor

#define PermConfigFor (   Class,
  ConfigId 
)    ((Class)->Config[ConfigId].Perm)

Definition at line 94 of file adaptive.h.

◆ TempConfigFor

#define TempConfigFor (   Class,
  ConfigId 
)    ((Class)->Config[ConfigId].Temp)

Definition at line 92 of file adaptive.h.

Typedef Documentation

◆ ADAPT_CLASS

Definition at line 64 of file adaptive.h.

◆ ADAPT_TEMPLATES

Definition at line 72 of file adaptive.h.

◆ PERM_CONFIG

Definition at line 48 of file adaptive.h.

◆ TEMP_CONFIG

Definition at line 42 of file adaptive.h.

◆ TEMP_PROTO

Definition at line 33 of file adaptive.h.

Function Documentation

◆ AddAdaptedClass()

void AddAdaptedClass ( ADAPT_TEMPLATES  Templates,
ADAPT_CLASS  Class,
CLASS_ID  ClassId 
)

This routine adds a new adapted class to an existing set of adapted templates.

Parameters
Templatesset of templates to add new class to
Classnew class to add to templates
ClassIdclass id to associate with new class
Note
Globals: none

Definition at line 45 of file adaptive.cpp.

47  {
48  INT_CLASS IntClass;
49 
50  assert (Templates != nullptr);
51  assert (Class != nullptr);
52  assert (LegalClassId (ClassId));
53  assert (UnusedClassIdIn (Templates->Templates, ClassId));
54  assert (Class->NumPermConfigs == 0);
55 
56  IntClass = NewIntClass (1, 1);
57  AddIntClass (Templates->Templates, ClassId, IntClass);
58 
59  assert (Templates->Class[ClassId] == nullptr);
60  Templates->Class[ClassId] = Class;
61 
62 } /* AddAdaptedClass */

◆ free_adapted_class()

void free_adapted_class ( ADAPT_CLASS  adapt_class)

Definition at line 124 of file adaptive.cpp.

124  {
125  for (int i = 0; i < MAX_NUM_CONFIGS; i++) {
126  if (ConfigIsPermanent (adapt_class, i)
127  && PermConfigFor (adapt_class, i) != nullptr)
128  FreePermConfig (PermConfigFor (adapt_class, i));
129  else if (!ConfigIsPermanent (adapt_class, i)
130  && TempConfigFor (adapt_class, i) != nullptr)
131  FreeTempConfig (TempConfigFor (adapt_class, i));
132  }
133  FreeBitVector (adapt_class->PermProtos);
134  FreeBitVector (adapt_class->PermConfigs);
135  destroy_nodes (adapt_class->TempProtos, FreeTempProto);
136  Efree(adapt_class);
137 }

◆ free_adapted_templates()

void free_adapted_templates ( ADAPT_TEMPLATES  templates)

Definition at line 182 of file adaptive.cpp.

182  {
183 
184  if (templates != nullptr) {
185  for (int i = 0; i < (templates->Templates)->NumClasses; i++)
186  free_adapted_class (templates->Class[i]);
187  free_int_templates (templates->Templates);
188  Efree(templates);
189  }
190 }

◆ FreeTempConfig()

void FreeTempConfig ( TEMP_CONFIG  Config)

This routine frees all memory consumed by a temporary configuration.

Parameters
Configconfig to be freed
Note
Globals: none

Definition at line 74 of file adaptive.cpp.

74  {
75  assert (Config != nullptr);
76  FreeBitVector (Config->Protos);
77  free(Config);
78 } /* FreeTempConfig */

◆ FreeTempProto()

void FreeTempProto ( void *  arg)

Definition at line 81 of file adaptive.cpp.

81  {
82  auto proto = static_cast<PROTO>(arg);
83 
84  free(proto);
85 }

◆ NewAdaptedClass()

ADAPT_CLASS NewAdaptedClass ( )

This operation allocates and initializes a new adapted class data structure and returns a ptr to it.

Returns
Ptr to new class data structure.
Note
Globals: none

Definition at line 102 of file adaptive.cpp.

102  {
103  ADAPT_CLASS Class;
104 
105  Class = static_cast<ADAPT_CLASS>(Emalloc (sizeof (ADAPT_CLASS_STRUCT)));
106  Class->NumPermConfigs = 0;
107  Class->MaxNumTimesSeen = 0;
108  Class->TempProtos = NIL_LIST;
109 
110  Class->PermProtos = NewBitVector (MAX_NUM_PROTOS);
111  Class->PermConfigs = NewBitVector (MAX_NUM_CONFIGS);
112  zero_all_bits (Class->PermProtos, WordsInVectorOfSize (MAX_NUM_PROTOS));
113  zero_all_bits (Class->PermConfigs, WordsInVectorOfSize (MAX_NUM_CONFIGS));
114 
115  for (int i = 0; i < MAX_NUM_CONFIGS; i++)
116  TempConfigFor (Class, i) = nullptr;
117 
118  return (Class);
119 
120 } /* NewAdaptedClass */

◆ NewTempConfig()

TEMP_CONFIG NewTempConfig ( int  MaxProtoId,
int  FontinfoId 
)

This routine allocates and returns a new temporary config.

Parameters
MaxProtoIdmax id of any proto in new config
FontinfoIdfont information from pre-trained templates
Returns
Ptr to new temp config.
Note
Globals: none

Definition at line 203 of file adaptive.cpp.

203  {
204  int NumProtos = MaxProtoId + 1;
205 
206  auto Config = static_cast<TEMP_CONFIG>(malloc(sizeof(TEMP_CONFIG_STRUCT)));
207  Config->Protos = NewBitVector (NumProtos);
208 
209  Config->NumTimesSeen = 1;
210  Config->MaxProtoId = MaxProtoId;
211  Config->ProtoVectorSize = WordsInVectorOfSize (NumProtos);
212  zero_all_bits (Config->Protos, Config->ProtoVectorSize);
213  Config->FontinfoId = FontinfoId;
214 
215  return (Config);
216 
217 } /* NewTempConfig */

◆ NewTempProto()

TEMP_PROTO NewTempProto ( )

This routine allocates and returns a new temporary proto.

Returns
Ptr to new temporary proto.
Note
Globals: none

Definition at line 228 of file adaptive.cpp.

228  {
229  return static_cast<TEMP_PROTO>(malloc(sizeof(TEMP_PROTO_STRUCT)));
230 } /* NewTempProto */

◆ ReadAdaptedClass()

ADAPT_CLASS ReadAdaptedClass ( TFile fp)

Read an adapted class description from file and return a ptr to the adapted class.

Parameters
fpopen file to read adapted class from
Returns
Ptr to new adapted class.
Note
Globals: none

Definition at line 281 of file adaptive.cpp.

281  {
282  int NumTempProtos;
283  int NumConfigs;
284  int i;
285  ADAPT_CLASS Class;
286 
287  /* first read high level adapted class structure */
288  Class = static_cast<ADAPT_CLASS>(Emalloc (sizeof (ADAPT_CLASS_STRUCT)));
289  fp->FRead(Class, sizeof(ADAPT_CLASS_STRUCT), 1);
290 
291  /* then read in the definitions of the permanent protos and configs */
292  Class->PermProtos = NewBitVector (MAX_NUM_PROTOS);
293  Class->PermConfigs = NewBitVector (MAX_NUM_CONFIGS);
294  fp->FRead(Class->PermProtos, sizeof(uint32_t),
295  WordsInVectorOfSize(MAX_NUM_PROTOS));
296  fp->FRead(Class->PermConfigs, sizeof(uint32_t),
297  WordsInVectorOfSize(MAX_NUM_CONFIGS));
298 
299  /* then read in the list of temporary protos */
300  fp->FRead(&NumTempProtos, sizeof(int), 1);
301  Class->TempProtos = NIL_LIST;
302  for (i = 0; i < NumTempProtos; i++) {
303  auto TempProto = static_cast<TEMP_PROTO>(malloc(sizeof(TEMP_PROTO_STRUCT)));
304  fp->FRead(TempProto, sizeof(TEMP_PROTO_STRUCT), 1);
305  Class->TempProtos = push_last (Class->TempProtos, TempProto);
306  }
307 
308  /* then read in the adapted configs */
309  fp->FRead(&NumConfigs, sizeof(int), 1);
310  for (i = 0; i < NumConfigs; i++)
311  if (test_bit (Class->PermConfigs, i))
312  Class->Config[i].Perm = ReadPermConfig(fp);
313  else
314  Class->Config[i].Temp = ReadTempConfig(fp);
315 
316  return (Class);
317 
318 } /* ReadAdaptedClass */

◆ ReadPermConfig()

PERM_CONFIG ReadPermConfig ( TFile fp)

Read a permanent configuration description from file and return a ptr to it.

Parameters
fpopen file to read permanent config from
Returns
Ptr to new permanent configuration description.
Note
Globals: none

Definition at line 362 of file adaptive.cpp.

362  {
363  auto Config = static_cast<PERM_CONFIG>(malloc(sizeof(PERM_CONFIG_STRUCT)));
364  uint8_t NumAmbigs;
365  fp->FRead(&NumAmbigs, sizeof(NumAmbigs), 1);
366  Config->Ambigs = new UNICHAR_ID[NumAmbigs + 1];
367  fp->FRead(Config->Ambigs, sizeof(UNICHAR_ID), NumAmbigs);
368  Config->Ambigs[NumAmbigs] = -1;
369  fp->FRead(&(Config->FontinfoId), sizeof(int), 1);
370 
371  return (Config);
372 
373 } /* ReadPermConfig */

◆ ReadTempConfig()

TEMP_CONFIG ReadTempConfig ( TFile fp)

Read a temporary configuration description from file and return a ptr to it.

Parameters
fpopen file to read temporary config from
Returns
Ptr to new temporary configuration description.
Note
Globals: none

Definition at line 386 of file adaptive.cpp.

386  {
387  auto Config = static_cast<TEMP_CONFIG>(malloc(sizeof(TEMP_CONFIG_STRUCT)));
388  fp->FRead(Config, sizeof(TEMP_CONFIG_STRUCT), 1);
389 
390  Config->Protos = NewBitVector (Config->ProtoVectorSize * BITSINLONG);
391  fp->FRead(Config->Protos, sizeof(uint32_t), Config->ProtoVectorSize);
392 
393  return (Config);
394 
395 } /* ReadTempConfig */

◆ WriteAdaptedClass()

void WriteAdaptedClass ( FILE *  File,
ADAPT_CLASS  Class,
int  NumConfigs 
)

This routine writes a binary representation of Class to File.

Parameters
Fileopen file to write Class to
Classadapted class to write to File
NumConfigsnumber of configs in Class
Note
Globals: none

Definition at line 409 of file adaptive.cpp.

409  {
410  int NumTempProtos;
411  LIST TempProtos;
412  int i;
413 
414  /* first write high level adapted class structure */
415  fwrite(Class, sizeof(ADAPT_CLASS_STRUCT), 1, File);
416 
417  /* then write out the definitions of the permanent protos and configs */
418  fwrite(Class->PermProtos, sizeof(uint32_t),
419  WordsInVectorOfSize(MAX_NUM_PROTOS), File);
420  fwrite(Class->PermConfigs, sizeof(uint32_t),
421  WordsInVectorOfSize(MAX_NUM_CONFIGS), File);
422 
423  /* then write out the list of temporary protos */
424  NumTempProtos = count (Class->TempProtos);
425  fwrite(&NumTempProtos, sizeof(int), 1, File);
426  TempProtos = Class->TempProtos;
427  iterate (TempProtos) {
428  void* proto = first_node(TempProtos);
429  fwrite(proto, sizeof(TEMP_PROTO_STRUCT), 1, File);
430  }
431 
432  /* then write out the adapted configs */
433  fwrite(&NumConfigs, sizeof(int), 1, File);
434  for (i = 0; i < NumConfigs; i++)
435  if (test_bit (Class->PermConfigs, i))
436  WritePermConfig (File, Class->Config[i].Perm);
437  else
438  WriteTempConfig (File, Class->Config[i].Temp);
439 
440 } /* WriteAdaptedClass */

◆ WritePermConfig()

void WritePermConfig ( FILE *  File,
PERM_CONFIG  Config 
)

This routine writes a binary representation of a permanent configuration to File.

Parameters
Fileopen file to write Config to
Configpermanent config to write to File
Note
Globals: none

Definition at line 481 of file adaptive.cpp.

481  {
482  uint8_t NumAmbigs = 0;
483 
484  assert (Config != nullptr);
485  while (Config->Ambigs[NumAmbigs] > 0) ++NumAmbigs;
486 
487  fwrite(&NumAmbigs, sizeof(uint8_t), 1, File);
488  fwrite(Config->Ambigs, sizeof(UNICHAR_ID), NumAmbigs, File);
489  fwrite(&(Config->FontinfoId), sizeof(int), 1, File);
490 } /* WritePermConfig */

◆ WriteTempConfig()

void WriteTempConfig ( FILE *  File,
TEMP_CONFIG  Config 
)

This routine writes a binary representation of a temporary configuration to File.

Parameters
Fileopen file to write Config to
Configtemporary config to write to File
Note
Globals: none

Definition at line 503 of file adaptive.cpp.

503  {
504  assert (Config != nullptr);
505 
506  fwrite(Config, sizeof (TEMP_CONFIG_STRUCT), 1, File);
507  fwrite(Config->Protos, sizeof (uint32_t), Config->ProtoVectorSize, File);
508 
509 } /* WriteTempConfig */
test_bit
#define test_bit(array, bit)
Definition: bitvec.h:59
MAX_NUM_CONFIGS
#define MAX_NUM_CONFIGS
Definition: intproto.h:47
Efree
void Efree(void *ptr)
Definition: emalloc.cpp:45
NIL_LIST
#define NIL_LIST
Definition: oldlist.h:76
FreeTempConfig
void FreeTempConfig(TEMP_CONFIG Config)
Definition: adaptive.cpp:74
ADAPT_TEMPLATES_STRUCT::Templates
INT_TEMPLATES Templates
Definition: adaptive.h:67
UnusedClassIdIn
#define UnusedClassIdIn(T, c)
Definition: intproto.h:177
ADAPT_CLASS_STRUCT::MaxNumTimesSeen
uint8_t MaxNumTimesSeen
Definition: adaptive.h:57
iterate
#define iterate(l)
Definition: oldlist.h:101
ADAPT_TEMPLATES_STRUCT::Class
ADAPT_CLASS Class[MAX_NUM_CLASSES]
Definition: adaptive.h:70
INT_CLASS_STRUCT
Definition: intproto.h:105
ADAPT_CLASS_STRUCT::TempProtos
LIST TempProtos
Definition: adaptive.h:61
MAX_NUM_PROTOS
#define MAX_NUM_PROTOS
Definition: intproto.h:48
free_adapted_class
void free_adapted_class(ADAPT_CLASS adapt_class)
Definition: adaptive.cpp:124
LegalClassId
#define LegalClassId(c)
Definition: intproto.h:176
ConfigIsPermanent
#define ConfigIsPermanent(Class, ConfigId)
Definition: adaptive.h:82
ADAPT_CLASS_STRUCT::Config
ADAPTED_CONFIG Config[MAX_NUM_CONFIGS]
Definition: adaptive.h:62
ADAPT_CLASS_STRUCT::NumPermConfigs
uint8_t NumPermConfigs
Definition: adaptive.h:56
ReadPermConfig
PERM_CONFIG ReadPermConfig(TFile *fp)
Definition: adaptive.cpp:362
UNICHAR_ID
int UNICHAR_ID
Definition: unichar.h:34
AddIntClass
void AddIntClass(INT_TEMPLATES Templates, CLASS_ID ClassId, INT_CLASS Class)
Definition: intproto.cpp:231
tesstrain_utils.int
int
Definition: tesstrain_utils.py:154
ADAPT_CLASS_STRUCT::PermConfigs
BIT_VECTOR PermConfigs
Definition: adaptive.h:60
push_last
LIST push_last(LIST list, void *item)
Definition: oldlist.cpp:227
count
int count(LIST var_list)
Definition: oldlist.cpp:95
ADAPT_CLASS_STRUCT
Definition: adaptive.h:55
tesseract::TFile::FRead
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:288
FreeTempProto
void FreeTempProto(void *arg)
Definition: adaptive.cpp:81
NewIntClass
INT_CLASS NewIntClass(int MaxNumProtos, int MaxNumConfigs)
Definition: intproto.cpp:626
BITSINLONG
const size_t BITSINLONG
Definition: bitvec.h:31
ADAPT_CLASS_STRUCT::PermProtos
BIT_VECTOR PermProtos
Definition: adaptive.h:59
WritePermConfig
void WritePermConfig(FILE *File, PERM_CONFIG Config)
Definition: adaptive.cpp:481
PERM_CONFIG_STRUCT
Definition: adaptive.h:44
first_node
#define first_node(l)
Definition: oldlist.h:92
TEMP_PROTO_STRUCT
Definition: adaptive.h:27
Emalloc
void * Emalloc(int Size)
Definition: emalloc.cpp:31
free_int_templates
void free_int_templates(INT_TEMPLATES templates)
Definition: intproto.cpp:698
PROTO_STRUCT
Definition: protos.h:36
TEMP_CONFIG_STRUCT
Definition: adaptive.h:35
WriteTempConfig
void WriteTempConfig(FILE *File, TEMP_CONFIG Config)
Definition: adaptive.cpp:503
list_rec
Definition: oldlist.h:81
PermConfigFor
#define PermConfigFor(Class, ConfigId)
Definition: adaptive.h:93
destroy_nodes
void destroy_nodes(LIST list, void_dest destructor)
Definition: oldlist.cpp:157
ADAPTED_CONFIG::Perm
PERM_CONFIG Perm
Definition: adaptive.h:52
TempConfigFor
#define TempConfigFor(Class, ConfigId)
Definition: adaptive.h:91
ADAPTED_CONFIG::Temp
TEMP_CONFIG Temp
Definition: adaptive.h:51
Config
CLUSTERCONFIG Config
Definition: commontraining.cpp:88
ReadTempConfig
TEMP_CONFIG ReadTempConfig(TFile *fp)
Definition: adaptive.cpp:386