tesseract  4.1.0
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

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

Definition at line 85 of file adaptive.h.

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

Definition at line 98 of file adaptive.h.

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

Definition at line 82 of file adaptive.h.

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

Definition at line 88 of file adaptive.h.

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

Definition at line 91 of file adaptive.h.

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

Definition at line 80 of file adaptive.h.

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

Definition at line 96 of file adaptive.h.

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

Definition at line 94 of file adaptive.h.

Typedef Documentation

Definition at line 66 of file adaptive.h.

Definition at line 75 of file adaptive.h.

Definition at line 50 of file adaptive.h.

Definition at line 44 of file adaptive.h.

Definition at line 35 of file adaptive.h.

Function Documentation

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 */
uint8_t NumPermConfigs
Definition: adaptive.h:58
INT_CLASS NewIntClass(int MaxNumProtos, int MaxNumConfigs)
Definition: intproto.cpp:626
ADAPT_CLASS Class[MAX_NUM_CLASSES]
Definition: adaptive.h:73
INT_TEMPLATES Templates
Definition: adaptive.h:69
void AddIntClass(INT_TEMPLATES Templates, CLASS_ID ClassId, INT_CLASS Class)
Definition: intproto.cpp:231
#define UnusedClassIdIn(T, c)
Definition: intproto.h:177
#define LegalClassId(c)
Definition: intproto.h:176
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 }
void FreeBitVector(BIT_VECTOR BitVector)
Definition: bitvec.cpp:50
#define PermConfigFor(Class, ConfigId)
Definition: adaptive.h:96
#define MAX_NUM_CONFIGS
Definition: intproto.h:47
void FreeTempProto(void *arg)
Definition: adaptive.cpp:81
BIT_VECTOR PermConfigs
Definition: adaptive.h:62
#define ConfigIsPermanent(Class, ConfigId)
Definition: adaptive.h:85
void Efree(void *ptr)
Definition: emalloc.cpp:45
BIT_VECTOR PermProtos
Definition: adaptive.h:61
#define TempConfigFor(Class, ConfigId)
Definition: adaptive.h:94
void FreeTempConfig(TEMP_CONFIG Config)
Definition: adaptive.cpp:74
void destroy_nodes(LIST list, void_dest destructor)
Definition: oldlist.cpp:158
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 }
ADAPT_CLASS Class[MAX_NUM_CLASSES]
Definition: adaptive.h:73
void Efree(void *ptr)
Definition: emalloc.cpp:45
void free_int_templates(INT_TEMPLATES templates)
Definition: intproto.cpp:698
INT_TEMPLATES Templates
Definition: adaptive.h:69
void free_adapted_class(ADAPT_CLASS adapt_class)
Definition: adaptive.cpp:124
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 */
void FreeBitVector(BIT_VECTOR BitVector)
Definition: bitvec.cpp:50
BIT_VECTOR Protos
Definition: adaptive.h:41
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 }
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 
114 
115  for (int i = 0; i < MAX_NUM_CONFIGS; i++)
116  TempConfigFor (Class, i) = nullptr;
117 
118  return (Class);
119 
120 } /* NewAdaptedClass */
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
#define MAX_NUM_CONFIGS
Definition: intproto.h:47
#define zero_all_bits(array, length)
Definition: bitvec.h:33
uint8_t NumPermConfigs
Definition: adaptive.h:58
BIT_VECTOR PermConfigs
Definition: adaptive.h:62
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:81
#define MAX_NUM_PROTOS
Definition: intproto.h:48
#define NIL_LIST
Definition: oldlist.h:76
BIT_VECTOR PermProtos
Definition: adaptive.h:61
void * Emalloc(int Size)
Definition: emalloc.cpp:31
#define TempConfigFor(Class, ConfigId)
Definition: adaptive.h:94
uint8_t MaxNumTimesSeen
Definition: adaptive.h:59
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 */
#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:81
CLUSTERCONFIG Config
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 */
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 */
294  fp->FRead(Class->PermProtos, sizeof(uint32_t),
296  fp->FRead(Class->PermConfigs, sizeof(uint32_t),
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 */
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
ADAPTED_CONFIG Config[MAX_NUM_CONFIGS]
Definition: adaptive.h:64
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:270
#define MAX_NUM_CONFIGS
Definition: intproto.h:47
BIT_VECTOR PermConfigs
Definition: adaptive.h:62
LIST push_last(LIST list, void *item)
Definition: oldlist.cpp:233
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:81
#define test_bit(array, bit)
Definition: bitvec.h:61
#define MAX_NUM_PROTOS
Definition: intproto.h:48
PERM_CONFIG Perm
Definition: adaptive.h:54
#define NIL_LIST
Definition: oldlist.h:76
TEMP_CONFIG Temp
Definition: adaptive.h:53
BIT_VECTOR PermProtos
Definition: adaptive.h:61
void * Emalloc(int Size)
Definition: emalloc.cpp:31
TEMP_CONFIG ReadTempConfig(TFile *fp)
Definition: adaptive.cpp:386
PERM_CONFIG ReadPermConfig(TFile *fp)
Definition: adaptive.cpp:362
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 */
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:270
int UNICHAR_ID
Definition: unichar.h:34
CLUSTERCONFIG Config
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 */
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:270
#define BITSINLONG
Definition: bitvec.h:27
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:81
CLUSTERCONFIG Config
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),
420  fwrite(Class->PermConfigs, sizeof(uint32_t),
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 */
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
ADAPTED_CONFIG Config[MAX_NUM_CONFIGS]
Definition: adaptive.h:64
#define MAX_NUM_CONFIGS
Definition: intproto.h:47
BIT_VECTOR PermConfigs
Definition: adaptive.h:62
void WritePermConfig(FILE *File, PERM_CONFIG Config)
Definition: adaptive.cpp:481
#define test_bit(array, bit)
Definition: bitvec.h:61
#define MAX_NUM_PROTOS
Definition: intproto.h:48
void WriteTempConfig(FILE *File, TEMP_CONFIG Config)
Definition: adaptive.cpp:503
PERM_CONFIG Perm
Definition: adaptive.h:54
TEMP_CONFIG Temp
Definition: adaptive.h:53
BIT_VECTOR PermProtos
Definition: adaptive.h:61
#define first_node(l)
Definition: oldlist.h:92
int count(LIST var_list)
Definition: oldlist.cpp:96
#define iterate(l)
Definition: oldlist.h:101
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 */
UNICHAR_ID * Ambigs
Definition: adaptive.h:47
int UNICHAR_ID
Definition: unichar.h:34
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 */
uint8_t ProtoVectorSize
Definition: adaptive.h:39
BIT_VECTOR Protos
Definition: adaptive.h:41