tesseract  4.00.00dev
adaptive.h File Reference
#include "oldlist.h"
#include "intproto.h"
#include <stdio.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)
 
#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:126

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
Exceptions: none
History: Thu Mar 14 13:06:09 1991, DSJ, Created.

Definition at line 50 of file adaptive.cpp.

52  {
53  INT_CLASS IntClass;
54 
55  assert (Templates != NULL);
56  assert (Class != NULL);
57  assert (LegalClassId (ClassId));
58  assert (UnusedClassIdIn (Templates->Templates, ClassId));
59  assert (Class->NumPermConfigs == 0);
60 
61  IntClass = NewIntClass (1, 1);
62  AddIntClass (Templates->Templates, ClassId, IntClass);
63 
64  assert (Templates->Class[ClassId] == NULL);
65  Templates->Class[ClassId] = Class;
66 
67 } /* AddAdaptedClass */
#define UnusedClassIdIn(T, c)
Definition: intproto.h:180
#define LegalClassId(c)
Definition: intproto.h:179
INT_TEMPLATES Templates
Definition: adaptive.h:76
void AddIntClass(INT_TEMPLATES Templates, CLASS_ID ClassId, INT_CLASS Class)
Definition: intproto.cpp:238
ADAPT_CLASS Class[MAX_NUM_CLASSES]
Definition: adaptive.h:80
uinT8 NumPermConfigs
Definition: adaptive.h:64
INT_CLASS NewIntClass(int MaxNumProtos, int MaxNumConfigs)
Definition: intproto.cpp:664

◆ free_adapted_class()

void free_adapted_class ( ADAPT_CLASS  adapt_class)

Definition at line 134 of file adaptive.cpp.

134  {
135  int i;
136 
137  for (i = 0; i < MAX_NUM_CONFIGS; i++) {
138  if (ConfigIsPermanent (adapt_class, i)
139  && PermConfigFor (adapt_class, i) != NULL)
140  FreePermConfig (PermConfigFor (adapt_class, i));
141  else if (!ConfigIsPermanent (adapt_class, i)
142  && TempConfigFor (adapt_class, i) != NULL)
143  FreeTempConfig (TempConfigFor (adapt_class, i));
144  }
145  FreeBitVector (adapt_class->PermProtos);
146  FreeBitVector (adapt_class->PermConfigs);
147  destroy_nodes (adapt_class->TempProtos, FreeTempProto);
148  Efree(adapt_class);
149 }
void FreeBitVector(BIT_VECTOR BitVector)
Definition: bitvec.cpp:54
BIT_VECTOR PermConfigs
Definition: adaptive.h:68
void FreeTempConfig(TEMP_CONFIG Config)
Definition: adaptive.cpp:81
#define PermConfigFor(Class, ConfigId)
Definition: adaptive.h:104
#define TempConfigFor(Class, ConfigId)
Definition: adaptive.h:101
void destroy_nodes(LIST list, void_dest destructor)
Definition: oldlist.cpp:199
void FreeTempProto(void *arg)
Definition: adaptive.cpp:88
BIT_VECTOR PermProtos
Definition: adaptive.h:67
void FreePermConfig(PERM_CONFIG Config)
Definition: adaptive.cpp:94
#define MAX_NUM_CONFIGS
Definition: intproto.h:46
void Efree(void *ptr)
Definition: emalloc.cpp:79
#define ConfigIsPermanent(Class, ConfigId)
Definition: adaptive.h:92

◆ free_adapted_templates()

void free_adapted_templates ( ADAPT_TEMPLATES  templates)

Definition at line 197 of file adaptive.cpp.

197  {
198 
199  if (templates != NULL) {
200  int i;
201  for (i = 0; i < (templates->Templates)->NumClasses; i++)
202  free_adapted_class (templates->Class[i]);
203  free_int_templates (templates->Templates);
204  Efree(templates);
205  }
206 }
INT_TEMPLATES Templates
Definition: adaptive.h:76
void free_adapted_class(ADAPT_CLASS adapt_class)
Definition: adaptive.cpp:134
ADAPT_CLASS Class[MAX_NUM_CLASSES]
Definition: adaptive.h:80
void Efree(void *ptr)
Definition: emalloc.cpp:79
void free_int_templates(INT_TEMPLATES templates)
Definition: intproto.cpp:739

◆ FreeTempConfig()

void FreeTempConfig ( TEMP_CONFIG  Config)

This routine frees all memory consumed by a temporary configuration.

Parameters
Configconfig to be freed
Note
Globals: none
Exceptions: none
History: Thu Mar 14 13:34:23 1991, DSJ, Created.

Definition at line 81 of file adaptive.cpp.

81  {
82  assert (Config != NULL);
83  FreeBitVector (Config->Protos);
84  free(Config);
85 } /* FreeTempConfig */
void FreeBitVector(BIT_VECTOR BitVector)
Definition: bitvec.cpp:54
BIT_VECTOR Protos
Definition: adaptive.h:44

◆ FreeTempProto()

void FreeTempProto ( void *  arg)

Definition at line 88 of file adaptive.cpp.

88  {
89  PROTO proto = (PROTO) arg;
90 
91  free(proto);
92 }
PROTO_STRUCT * PROTO
Definition: protos.h:52

◆ 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
Exceptions: none
History: Thu Mar 14 12:58:13 1991, DSJ, Created.

Definition at line 111 of file adaptive.cpp.

111  {
112  ADAPT_CLASS Class;
113  int i;
114 
115  Class = (ADAPT_CLASS) Emalloc (sizeof (ADAPT_CLASS_STRUCT));
116  Class->NumPermConfigs = 0;
117  Class->MaxNumTimesSeen = 0;
118  Class->TempProtos = NIL_LIST;
119 
124 
125  for (i = 0; i < MAX_NUM_CONFIGS; i++)
126  TempConfigFor (Class, i) = NULL;
127 
128  return (Class);
129 
130 } /* NewAdaptedClass */
BIT_VECTOR PermConfigs
Definition: adaptive.h:68
#define MAX_NUM_PROTOS
Definition: intproto.h:47
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
void * Emalloc(int Size)
Definition: emalloc.cpp:47
ADAPT_CLASS_STRUCT * ADAPT_CLASS
Definition: adaptive.h:72
#define NIL_LIST
Definition: oldlist.h:126
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:89
uinT8 MaxNumTimesSeen
Definition: adaptive.h:65
#define zero_all_bits(array, length)
Definition: bitvec.h:33
#define TempConfigFor(Class, ConfigId)
Definition: adaptive.h:101
BIT_VECTOR PermProtos
Definition: adaptive.h:67
uinT8 NumPermConfigs
Definition: adaptive.h:64
#define MAX_NUM_CONFIGS
Definition: intproto.h:46

◆ 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
Exceptions: none
History: Thu Mar 14 13:28:21 1991, DSJ, Created.

Definition at line 221 of file adaptive.cpp.

221  {
222  int NumProtos = MaxProtoId + 1;
223 
224  TEMP_CONFIG Config = (TEMP_CONFIG) malloc(sizeof(TEMP_CONFIG_STRUCT));
225  Config->Protos = NewBitVector (NumProtos);
226 
227  Config->NumTimesSeen = 1;
228  Config->MaxProtoId = MaxProtoId;
229  Config->ProtoVectorSize = WordsInVectorOfSize (NumProtos);
230  zero_all_bits (Config->Protos, Config->ProtoVectorSize);
231  Config->FontinfoId = FontinfoId;
232 
233  return (Config);
234 
235 } /* NewTempConfig */
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
uinT8 ProtoVectorSize
Definition: adaptive.h:42
BIT_VECTOR Protos
Definition: adaptive.h:44
CLUSTERCONFIG Config
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:89
PROTO_ID MaxProtoId
Definition: adaptive.h:43
#define zero_all_bits(array, length)
Definition: bitvec.h:33
TEMP_CONFIG_STRUCT * TEMP_CONFIG
Definition: adaptive.h:47
uinT8 NumTimesSeen
Definition: adaptive.h:41

◆ NewTempProto()

TEMP_PROTO NewTempProto ( )

This routine allocates and returns a new temporary proto.

Returns
Ptr to new temporary proto.
Note
Globals: none
Exceptions: none
History: Thu Mar 14 13:31:31 1991, DSJ, Created.

Definition at line 248 of file adaptive.cpp.

248  {
249  return (TEMP_PROTO) malloc(sizeof(TEMP_PROTO_STRUCT));
250 } /* NewTempProto */

◆ ReadAdaptedClass()

ADAPT_CLASS ReadAdaptedClass ( TFile fp)

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

Parameters
Fileopen file to read adapted class from
Returns
Ptr to new adapted class.
Note
Globals: none
Exceptions: none
History: Tue Mar 19 14:11:01 1991, DSJ, Created.

Definition at line 306 of file adaptive.cpp.

306  {
307  int NumTempProtos;
308  int NumConfigs;
309  int i;
310  ADAPT_CLASS Class;
311 
312  /* first read high level adapted class structure */
313  Class = (ADAPT_CLASS) Emalloc (sizeof (ADAPT_CLASS_STRUCT));
314  fp->FRead(Class, sizeof(ADAPT_CLASS_STRUCT), 1);
315 
316  /* then read in the definitions of the permanent protos and configs */
319  fp->FRead(Class->PermProtos, sizeof(uinT32),
321  fp->FRead(Class->PermConfigs, sizeof(uinT32),
323 
324  /* then read in the list of temporary protos */
325  fp->FRead(&NumTempProtos, sizeof(int), 1);
326  Class->TempProtos = NIL_LIST;
327  for (i = 0; i < NumTempProtos; i++) {
328  TEMP_PROTO TempProto = (TEMP_PROTO) malloc(sizeof(TEMP_PROTO_STRUCT));
329  fp->FRead(TempProto, sizeof(TEMP_PROTO_STRUCT), 1);
330  Class->TempProtos = push_last (Class->TempProtos, TempProto);
331  }
332 
333  /* then read in the adapted configs */
334  fp->FRead(&NumConfigs, sizeof(int), 1);
335  for (i = 0; i < NumConfigs; i++)
336  if (test_bit (Class->PermConfigs, i))
337  Class->Config[i].Perm = ReadPermConfig(fp);
338  else
339  Class->Config[i].Temp = ReadTempConfig(fp);
340 
341  return (Class);
342 
343 } /* ReadAdaptedClass */
PERM_CONFIG ReadPermConfig(TFile *fp)
Definition: adaptive.cpp:392
ADAPTED_CONFIG Config[MAX_NUM_CONFIGS]
Definition: adaptive.h:70
BIT_VECTOR PermConfigs
Definition: adaptive.h:68
#define MAX_NUM_PROTOS
Definition: intproto.h:47
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
void * Emalloc(int Size)
Definition: emalloc.cpp:47
LIST push_last(LIST list, void *item)
Definition: oldlist.cpp:332
ADAPT_CLASS_STRUCT * ADAPT_CLASS
Definition: adaptive.h:72
#define NIL_LIST
Definition: oldlist.h:126
TEMP_PROTO_STRUCT * TEMP_PROTO
Definition: adaptive.h:37
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:89
TEMP_CONFIG Temp
Definition: adaptive.h:58
PERM_CONFIG Perm
Definition: adaptive.h:59
uint32_t uinT32
Definition: host.h:39
TEMP_CONFIG ReadTempConfig(TFile *fp)
Definition: adaptive.cpp:418
#define test_bit(array, bit)
Definition: bitvec.h:61
BIT_VECTOR PermProtos
Definition: adaptive.h:67
#define MAX_NUM_CONFIGS
Definition: intproto.h:46
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:108

◆ ReadPermConfig()

PERM_CONFIG ReadPermConfig ( TFile fp)

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

Parameters
Fileopen file to read permanent config from
Returns
Ptr to new permanent configuration description.
Note
Globals: none
Exceptions: none
History: Tue Mar 19 14:25:26 1991, DSJ, Created.

Definition at line 392 of file adaptive.cpp.

392  {
393  PERM_CONFIG Config = (PERM_CONFIG) malloc(sizeof(PERM_CONFIG_STRUCT));
394  uinT8 NumAmbigs;
395  fp->FRead(&NumAmbigs, sizeof(uinT8), 1);
396  Config->Ambigs = new UNICHAR_ID[NumAmbigs + 1];
397  fp->FRead(Config->Ambigs, sizeof(UNICHAR_ID), NumAmbigs);
398  Config->Ambigs[NumAmbigs] = -1;
399  fp->FRead(&(Config->FontinfoId), sizeof(int), 1);
400 
401  return (Config);
402 
403 } /* ReadPermConfig */
int UNICHAR_ID
Definition: unichar.h:33
PERM_CONFIG_STRUCT * PERM_CONFIG
Definition: adaptive.h:54
CLUSTERCONFIG Config
uint8_t uinT8
Definition: host.h:35
UNICHAR_ID * Ambigs
Definition: adaptive.h:51
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:108

◆ ReadTempConfig()

TEMP_CONFIG ReadTempConfig ( TFile fp)

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

Parameters
Fileopen file to read temporary config from
Returns
Ptr to new temporary configuration description.
Note
Globals: none
Exceptions: none
History: Tue Mar 19 14:29:59 1991, DSJ, Created.

Definition at line 418 of file adaptive.cpp.

418  {
419  TEMP_CONFIG Config = (TEMP_CONFIG) malloc(sizeof(TEMP_CONFIG_STRUCT));
420  fp->FRead(Config, sizeof(TEMP_CONFIG_STRUCT), 1);
421 
422  Config->Protos = NewBitVector (Config->ProtoVectorSize * BITSINLONG);
423  fp->FRead(Config->Protos, sizeof(uinT32), Config->ProtoVectorSize);
424 
425  return (Config);
426 
427 } /* ReadTempConfig */
#define BITSINLONG
Definition: bitvec.h:27
uinT8 ProtoVectorSize
Definition: adaptive.h:42
BIT_VECTOR Protos
Definition: adaptive.h:44
CLUSTERCONFIG Config
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:89
uint32_t uinT32
Definition: host.h:39
TEMP_CONFIG_STRUCT * TEMP_CONFIG
Definition: adaptive.h:47
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:108

◆ 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
Exceptions: none
History: Tue Mar 19 13:33:51 1991, DSJ, Created.

Definition at line 443 of file adaptive.cpp.

443  {
444  int NumTempProtos;
445  LIST TempProtos;
446  int i;
447 
448  /* first write high level adapted class structure */
449  fwrite ((char *) Class, sizeof (ADAPT_CLASS_STRUCT), 1, File);
450 
451  /* then write out the definitions of the permanent protos and configs */
452  fwrite ((char *) Class->PermProtos, sizeof (uinT32),
454  fwrite ((char *) Class->PermConfigs, sizeof (uinT32),
456 
457  /* then write out the list of temporary protos */
458  NumTempProtos = count (Class->TempProtos);
459  fwrite ((char *) &NumTempProtos, sizeof (int), 1, File);
460  TempProtos = Class->TempProtos;
461  iterate (TempProtos) {
462  void* proto = first_node(TempProtos);
463  fwrite ((char *) proto, sizeof (TEMP_PROTO_STRUCT), 1, File);
464  }
465 
466  /* then write out the adapted configs */
467  fwrite ((char *) &NumConfigs, sizeof (int), 1, File);
468  for (i = 0; i < NumConfigs; i++)
469  if (test_bit (Class->PermConfigs, i))
470  WritePermConfig (File, Class->Config[i].Perm);
471  else
472  WriteTempConfig (File, Class->Config[i].Temp);
473 
474 } /* WriteAdaptedClass */
#define MAX_NUM_PROTOS
Definition: intproto.h:47
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
void WritePermConfig(FILE *File, PERM_CONFIG Config)
Definition: adaptive.cpp:519
uint32_t uinT32
Definition: host.h:39
void WriteTempConfig(FILE *File, TEMP_CONFIG Config)
Definition: adaptive.cpp:543
#define first_node(l)
Definition: oldlist.h:139
#define test_bit(array, bit)
Definition: bitvec.h:61
#define MAX_NUM_CONFIGS
Definition: intproto.h:46
#define iterate(l)
Definition: oldlist.h:159
int count(LIST var_list)
Definition: oldlist.cpp:103

◆ 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
Exceptions: none
History: Tue Mar 19 13:55:44 1991, DSJ, Created.

Definition at line 519 of file adaptive.cpp.

519  {
520  uinT8 NumAmbigs = 0;
521 
522  assert (Config != NULL);
523  while (Config->Ambigs[NumAmbigs] > 0) ++NumAmbigs;
524 
525  fwrite((char *) &NumAmbigs, sizeof(uinT8), 1, File);
526  fwrite(Config->Ambigs, sizeof(UNICHAR_ID), NumAmbigs, File);
527  fwrite(&(Config->FontinfoId), sizeof(int), 1, File);
528 } /* WritePermConfig */
int UNICHAR_ID
Definition: unichar.h:33
uint8_t uinT8
Definition: host.h:35
UNICHAR_ID * Ambigs
Definition: adaptive.h:51

◆ 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
Exceptions: none
History: Tue Mar 19 14:00:28 1991, DSJ, Created.

Definition at line 543 of file adaptive.cpp.

543  {
544  assert (Config != NULL);
545 
546  fwrite ((char *) Config, sizeof (TEMP_CONFIG_STRUCT), 1, File);
547  fwrite ((char *) Config->Protos, sizeof (uinT32),
548  Config->ProtoVectorSize, File);
549 
550 } /* WriteTempConfig */
uint32_t uinT32
Definition: host.h:39