tesseract  4.0.0-1-g2a2b
adaptive.h File Reference
#include "oldlist.h"
#include "intproto.h"
#include <cstdio>

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)
 
#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

typedef TEMP_PROTO_STRUCTTEMP_PROTO
 
typedef TEMP_CONFIG_STRUCTTEMP_CONFIG
 
typedef PERM_CONFIG_STRUCTPERM_CONFIG
 
typedef ADAPT_CLASS_STRUCTADAPT_CLASS
 
typedef ADAPT_TEMPLATES_STRUCTADAPT_TEMPLATES
 

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 92 of file adaptive.h.

◆ IncreaseConfidence

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

Definition at line 107 of file adaptive.h.

◆ IsEmptyAdaptedClass

#define IsEmptyAdaptedClass (   Class)
Value:
((Class)->NumPermConfigs == 0 && \
(Class)->TempProtos == NIL_LIST)
#define NIL_LIST
Definition: oldlist.h:127

Definition at line 89 of file adaptive.h.

◆ MakeConfigPermanent

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

Definition at line 95 of file adaptive.h.

◆ MakeProtoPermanent

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

Definition at line 98 of file adaptive.h.

◆ NumNonEmptyClassesIn

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

Definition at line 87 of file adaptive.h.

◆ PermConfigFor

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

Definition at line 104 of file adaptive.h.

◆ TempConfigFor

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

Definition at line 101 of file adaptive.h.

Typedef Documentation

◆ ADAPT_CLASS

Definition at line 72 of file adaptive.h.

◆ ADAPT_TEMPLATES

Definition at line 82 of file adaptive.h.

◆ PERM_CONFIG

Definition at line 54 of file adaptive.h.

◆ TEMP_CONFIG

Definition at line 47 of file adaptive.h.

◆ TEMP_PROTO

Definition at line 37 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 46 of file adaptive.cpp.

48  {
49  INT_CLASS IntClass;
50 
51  assert (Templates != nullptr);
52  assert (Class != nullptr);
53  assert (LegalClassId (ClassId));
54  assert (UnusedClassIdIn (Templates->Templates, ClassId));
55  assert (Class->NumPermConfigs == 0);
56 
57  IntClass = NewIntClass (1, 1);
58  AddIntClass (Templates->Templates, ClassId, IntClass);
59 
60  assert (Templates->Class[ClassId] == nullptr);
61  Templates->Class[ClassId] = Class;
62 
63 } /* AddAdaptedClass */
#define UnusedClassIdIn(T, c)
Definition: intproto.h:175
uint8_t NumPermConfigs
Definition: adaptive.h:64
#define LegalClassId(c)
Definition: intproto.h:174
ADAPT_CLASS Class[MAX_NUM_CLASSES]
Definition: adaptive.h:80
void AddIntClass(INT_TEMPLATES Templates, CLASS_ID ClassId, INT_CLASS Class)
Definition: intproto.cpp:232
INT_CLASS NewIntClass(int MaxNumProtos, int MaxNumConfigs)
Definition: intproto.cpp:636
INT_TEMPLATES Templates
Definition: adaptive.h:76

◆ free_adapted_class()

void free_adapted_class ( ADAPT_CLASS  adapt_class)

Definition at line 125 of file adaptive.cpp.

125  {
126  for (int i = 0; i < MAX_NUM_CONFIGS; i++) {
127  if (ConfigIsPermanent (adapt_class, i)
128  && PermConfigFor (adapt_class, i) != nullptr)
129  FreePermConfig (PermConfigFor (adapt_class, i));
130  else if (!ConfigIsPermanent (adapt_class, i)
131  && TempConfigFor (adapt_class, i) != nullptr)
132  FreeTempConfig (TempConfigFor (adapt_class, i));
133  }
134  FreeBitVector (adapt_class->PermProtos);
135  FreeBitVector (adapt_class->PermConfigs);
136  destroy_nodes (adapt_class->TempProtos, FreeTempProto);
137  Efree(adapt_class);
138 }
void Efree(void *ptr)
Definition: emalloc.cpp:45
void FreeBitVector(BIT_VECTOR BitVector)
Definition: bitvec.cpp:51
#define TempConfigFor(Class, ConfigId)
Definition: adaptive.h:101
#define MAX_NUM_CONFIGS
Definition: intproto.h:47
void FreeTempConfig(TEMP_CONFIG Config)
Definition: adaptive.cpp:75
void destroy_nodes(LIST list, void_dest destructor)
Definition: oldlist.cpp:186
#define PermConfigFor(Class, ConfigId)
Definition: adaptive.h:104
BIT_VECTOR PermConfigs
Definition: adaptive.h:68
#define ConfigIsPermanent(Class, ConfigId)
Definition: adaptive.h:92
BIT_VECTOR PermProtos
Definition: adaptive.h:67
void FreeTempProto(void *arg)
Definition: adaptive.cpp:82

◆ free_adapted_templates()

void free_adapted_templates ( ADAPT_TEMPLATES  templates)

Definition at line 183 of file adaptive.cpp.

183  {
184 
185  if (templates != nullptr) {
186  for (int i = 0; i < (templates->Templates)->NumClasses; i++)
187  free_adapted_class (templates->Class[i]);
188  free_int_templates (templates->Templates);
189  Efree(templates);
190  }
191 }
void free_int_templates(INT_TEMPLATES templates)
Definition: intproto.cpp:708
void Efree(void *ptr)
Definition: emalloc.cpp:45
ADAPT_CLASS Class[MAX_NUM_CLASSES]
Definition: adaptive.h:80
void free_adapted_class(ADAPT_CLASS adapt_class)
Definition: adaptive.cpp:125
INT_TEMPLATES Templates
Definition: adaptive.h:76

◆ 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 75 of file adaptive.cpp.

75  {
76  assert (Config != nullptr);
77  FreeBitVector (Config->Protos);
78  free(Config);
79 } /* FreeTempConfig */
CLUSTERCONFIG Config
void FreeBitVector(BIT_VECTOR BitVector)
Definition: bitvec.cpp:51

◆ FreeTempProto()

void FreeTempProto ( void *  arg)

Definition at line 82 of file adaptive.cpp.

82  {
83  PROTO proto = (PROTO) arg;
84 
85  free(proto);
86 }
PROTO_STRUCT * PROTO
Definition: protos.h:51

◆ 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 103 of file adaptive.cpp.

103  {
104  ADAPT_CLASS Class;
105 
106  Class = (ADAPT_CLASS) Emalloc (sizeof (ADAPT_CLASS_STRUCT));
107  Class->NumPermConfigs = 0;
108  Class->MaxNumTimesSeen = 0;
109  Class->TempProtos = NIL_LIST;
110 
115 
116  for (int i = 0; i < MAX_NUM_CONFIGS; i++)
117  TempConfigFor (Class, i) = nullptr;
118 
119  return (Class);
120 
121 } /* NewAdaptedClass */
ADAPT_CLASS_STRUCT * ADAPT_CLASS
Definition: adaptive.h:72
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
void * Emalloc(int Size)
Definition: emalloc.cpp:31
#define zero_all_bits(array, length)
Definition: bitvec.h:33
uint8_t NumPermConfigs
Definition: adaptive.h:64
#define TempConfigFor(Class, ConfigId)
Definition: adaptive.h:101
#define MAX_NUM_CONFIGS
Definition: intproto.h:47
#define MAX_NUM_PROTOS
Definition: intproto.h:48
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:82
BIT_VECTOR PermConfigs
Definition: adaptive.h:68
#define NIL_LIST
Definition: oldlist.h:127
BIT_VECTOR PermProtos
Definition: adaptive.h:67
uint8_t MaxNumTimesSeen
Definition: adaptive.h:65

◆ 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 204 of file adaptive.cpp.

204  {
205  int NumProtos = MaxProtoId + 1;
206 
208  Config->Protos = NewBitVector (NumProtos);
209 
210  Config->NumTimesSeen = 1;
211  Config->MaxProtoId = MaxProtoId;
212  Config->ProtoVectorSize = WordsInVectorOfSize (NumProtos);
213  zero_all_bits (Config->Protos, Config->ProtoVectorSize);
214  Config->FontinfoId = FontinfoId;
215 
216  return (Config);
217 
218 } /* NewTempConfig */
CLUSTERCONFIG Config
TEMP_CONFIG_STRUCT * TEMP_CONFIG
Definition: adaptive.h:47
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
#define zero_all_bits(array, length)
Definition: bitvec.h:33
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:82

◆ 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 229 of file adaptive.cpp.

229  {
230  return (TEMP_PROTO)malloc(sizeof(TEMP_PROTO_STRUCT));
231 } /* 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 282 of file adaptive.cpp.

282  {
283  int NumTempProtos;
284  int NumConfigs;
285  int i;
286  ADAPT_CLASS Class;
287 
288  /* first read high level adapted class structure */
289  Class = (ADAPT_CLASS) Emalloc (sizeof (ADAPT_CLASS_STRUCT));
290  fp->FRead(Class, sizeof(ADAPT_CLASS_STRUCT), 1);
291 
292  /* then read in the definitions of the permanent protos and configs */
295  fp->FRead(Class->PermProtos, sizeof(uint32_t),
297  fp->FRead(Class->PermConfigs, sizeof(uint32_t),
299 
300  /* then read in the list of temporary protos */
301  fp->FRead(&NumTempProtos, sizeof(int), 1);
302  Class->TempProtos = NIL_LIST;
303  for (i = 0; i < NumTempProtos; i++) {
304  TEMP_PROTO TempProto = (TEMP_PROTO)malloc(sizeof(TEMP_PROTO_STRUCT));
305  fp->FRead(TempProto, sizeof(TEMP_PROTO_STRUCT), 1);
306  Class->TempProtos = push_last (Class->TempProtos, TempProto);
307  }
308 
309  /* then read in the adapted configs */
310  fp->FRead(&NumConfigs, sizeof(int), 1);
311  for (i = 0; i < NumConfigs; i++)
312  if (test_bit (Class->PermConfigs, i))
313  Class->Config[i].Perm = ReadPermConfig(fp);
314  else
315  Class->Config[i].Temp = ReadTempConfig(fp);
316 
317  return (Class);
318 
319 } /* ReadAdaptedClass */
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:270
ADAPT_CLASS_STRUCT * ADAPT_CLASS
Definition: adaptive.h:72
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
void * Emalloc(int Size)
Definition: emalloc.cpp:31
TEMP_CONFIG ReadTempConfig(TFile *fp)
Definition: adaptive.cpp:387
PERM_CONFIG Perm
Definition: adaptive.h:59
#define MAX_NUM_CONFIGS
Definition: intproto.h:47
#define MAX_NUM_PROTOS
Definition: intproto.h:48
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:82
BIT_VECTOR PermConfigs
Definition: adaptive.h:68
LIST push_last(LIST list, void *item)
Definition: oldlist.cpp:297
#define NIL_LIST
Definition: oldlist.h:127
BIT_VECTOR PermProtos
Definition: adaptive.h:67
ADAPTED_CONFIG Config[MAX_NUM_CONFIGS]
Definition: adaptive.h:70
TEMP_PROTO_STRUCT * TEMP_PROTO
Definition: adaptive.h:37
TEMP_CONFIG Temp
Definition: adaptive.h:58
#define test_bit(array, bit)
Definition: bitvec.h:61
PERM_CONFIG ReadPermConfig(TFile *fp)
Definition: adaptive.cpp:363

◆ 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 363 of file adaptive.cpp.

363  {
365  uint8_t NumAmbigs;
366  fp->FRead(&NumAmbigs, sizeof(NumAmbigs), 1);
367  Config->Ambigs = new UNICHAR_ID[NumAmbigs + 1];
368  fp->FRead(Config->Ambigs, sizeof(UNICHAR_ID), NumAmbigs);
369  Config->Ambigs[NumAmbigs] = -1;
370  fp->FRead(&(Config->FontinfoId), sizeof(int), 1);
371 
372  return (Config);
373 
374 } /* ReadPermConfig */
int UNICHAR_ID
Definition: unichar.h:35
CLUSTERCONFIG Config
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:270
PERM_CONFIG_STRUCT * PERM_CONFIG
Definition: adaptive.h:54

◆ 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 387 of file adaptive.cpp.

387  {
389  fp->FRead(Config, sizeof(TEMP_CONFIG_STRUCT), 1);
390 
391  Config->Protos = NewBitVector (Config->ProtoVectorSize * BITSINLONG);
392  fp->FRead(Config->Protos, sizeof(uint32_t), Config->ProtoVectorSize);
393 
394  return (Config);
395 
396 } /* ReadTempConfig */
CLUSTERCONFIG Config
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:270
TEMP_CONFIG_STRUCT * TEMP_CONFIG
Definition: adaptive.h:47
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:82
#define BITSINLONG
Definition: bitvec.h:27

◆ 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 410 of file adaptive.cpp.

410  {
411  int NumTempProtos;
412  LIST TempProtos;
413  int i;
414 
415  /* first write high level adapted class structure */
416  fwrite(Class, sizeof(ADAPT_CLASS_STRUCT), 1, File);
417 
418  /* then write out the definitions of the permanent protos and configs */
419  fwrite(Class->PermProtos, sizeof(uint32_t),
421  fwrite(Class->PermConfigs, sizeof(uint32_t),
423 
424  /* then write out the list of temporary protos */
425  NumTempProtos = count (Class->TempProtos);
426  fwrite(&NumTempProtos, sizeof(int), 1, File);
427  TempProtos = Class->TempProtos;
428  iterate (TempProtos) {
429  void* proto = first_node(TempProtos);
430  fwrite(proto, sizeof(TEMP_PROTO_STRUCT), 1, File);
431  }
432 
433  /* then write out the adapted configs */
434  fwrite(&NumConfigs, sizeof(int), 1, File);
435  for (i = 0; i < NumConfigs; i++)
436  if (test_bit (Class->PermConfigs, i))
437  WritePermConfig (File, Class->Config[i].Perm);
438  else
439  WriteTempConfig (File, Class->Config[i].Temp);
440 
441 } /* WriteAdaptedClass */
void WritePermConfig(FILE *File, PERM_CONFIG Config)
Definition: adaptive.cpp:482
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
int count(LIST var_list)
Definition: oldlist.cpp:98
void WriteTempConfig(FILE *File, TEMP_CONFIG Config)
Definition: adaptive.cpp:504
PERM_CONFIG Perm
Definition: adaptive.h:59
#define MAX_NUM_CONFIGS
Definition: intproto.h:47
#define MAX_NUM_PROTOS
Definition: intproto.h:48
BIT_VECTOR PermConfigs
Definition: adaptive.h:68
#define first_node(l)
Definition: oldlist.h:141
BIT_VECTOR PermProtos
Definition: adaptive.h:67
ADAPTED_CONFIG Config[MAX_NUM_CONFIGS]
Definition: adaptive.h:70
#define iterate(l)
Definition: oldlist.h:161
TEMP_CONFIG Temp
Definition: adaptive.h:58
#define test_bit(array, bit)
Definition: bitvec.h:61

◆ 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 482 of file adaptive.cpp.

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

◆ 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 504 of file adaptive.cpp.

504  {
505  assert (Config != nullptr);
506 
507  fwrite(Config, sizeof (TEMP_CONFIG_STRUCT), 1, File);
508  fwrite(Config->Protos, sizeof (uint32_t), Config->ProtoVectorSize, File);
509 
510 } /* WriteTempConfig */
CLUSTERCONFIG Config