Tesseract  3.02
tesseract-ocr/classify/adaptive.cpp File Reference
#include "adaptive.h"
#include "emalloc.h"
#include "freelist.h"
#include "globals.h"
#include "classify.h"
#include <assert.h>
#include <stdio.h>

Go to the source code of this file.

Namespaces

namespace  tesseract

Functions

void AddAdaptedClass (ADAPT_TEMPLATES Templates, ADAPT_CLASS Class, CLASS_ID ClassId)
void FreeTempConfig (TEMP_CONFIG Config)
void FreeTempProto (void *arg)
void FreePermConfig (PERM_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 (FILE *File)
PERM_CONFIG ReadPermConfig (FILE *File)
TEMP_CONFIG ReadTempConfig (FILE *File)
void WriteAdaptedClass (FILE *File, ADAPT_CLASS Class, int NumConfigs)
void WritePermConfig (FILE *File, PERM_CONFIG Config)
void WriteTempConfig (FILE *File, TEMP_CONFIG Config)

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

Definition at line 49 of file adaptive.cpp.

                                       {
  INT_CLASS IntClass;

  assert (Templates != NULL);
  assert (Class != NULL);
  assert (LegalClassId (ClassId));
  assert (UnusedClassIdIn (Templates->Templates, ClassId));
  assert (Class->NumPermConfigs == 0);

  IntClass = NewIntClass (1, 1);
  AddIntClass (Templates->Templates, ClassId, IntClass);

  assert (Templates->Class[ClassId] == NULL);
  Templates->Class[ClassId] = Class;

}                                /* AddAdaptedClass */
void free_adapted_class ( ADAPT_CLASS  adapt_class)

Definition at line 136 of file adaptive.cpp.

                                                 {
  int i;

  for (i = 0; i < MAX_NUM_CONFIGS; i++) {
    if (ConfigIsPermanent (adapt_class, i)
      && PermConfigFor (adapt_class, i) != NULL)
      FreePermConfig (PermConfigFor (adapt_class, i));
    else if (!ConfigIsPermanent (adapt_class, i)
      && TempConfigFor (adapt_class, i) != NULL)
      FreeTempConfig (TempConfigFor (adapt_class, i));
  }
  FreeBitVector (adapt_class->PermProtos);
  FreeBitVector (adapt_class->PermConfigs);
  destroy_nodes (adapt_class->TempProtos, FreeTempProto);
  Efree(adapt_class);
}
void free_adapted_templates ( ADAPT_TEMPLATES  templates)

Definition at line 199 of file adaptive.cpp.

                                                       {

  if (templates != NULL) {
    int i;
    for (i = 0; i < (templates->Templates)->NumClasses; i++)
      free_adapted_class (templates->Class[i]);
    free_int_templates (templates->Templates);
    Efree(templates);
  }
}
void FreePermConfig ( PERM_CONFIG  Config)

Definition at line 96 of file adaptive.cpp.

                                        {
  assert(Config != NULL);
  Efree(Config->Ambigs);
  free_struct(Config, sizeof(PERM_CONFIG_STRUCT), "PERM_CONFIG_STRUCT");
}
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 80 of file adaptive.cpp.

                                        {
  assert (Config != NULL);

  destroy_nodes (Config->ContextsSeen, memfree);
  FreeBitVector (Config->Protos);
  free_struct (Config, sizeof (TEMP_CONFIG_STRUCT), "TEMP_CONFIG_STRUCT");

}                                /* FreeTempConfig */
void FreeTempProto ( void *  arg)

Definition at line 90 of file adaptive.cpp.

                              {
  PROTO proto = (PROTO) arg;

  free_struct (proto, sizeof (TEMP_PROTO_STRUCT), "TEMP_PROTO_STRUCT");
}
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 113 of file adaptive.cpp.

                              {
  ADAPT_CLASS Class;
  int i;

  Class = (ADAPT_CLASS) Emalloc (sizeof (ADAPT_CLASS_STRUCT));
  Class->NumPermConfigs = 0;
  Class->MaxNumTimesSeen = 0;
  Class->TempProtos = NIL_LIST;

  Class->PermProtos = NewBitVector (MAX_NUM_PROTOS);
  Class->PermConfigs = NewBitVector (MAX_NUM_CONFIGS);
  zero_all_bits (Class->PermProtos, WordsInVectorOfSize (MAX_NUM_PROTOS));
  zero_all_bits (Class->PermConfigs, WordsInVectorOfSize (MAX_NUM_CONFIGS));

  for (i = 0; i < MAX_NUM_CONFIGS; i++)
    TempConfigFor (Class, i) = NULL;

  return (Class);

}                                /* NewAdaptedClass */
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 223 of file adaptive.cpp.

                                                          {
  TEMP_CONFIG Config;
  int NumProtos = MaxProtoId + 1;

  Config =
    (TEMP_CONFIG) alloc_struct (sizeof (TEMP_CONFIG_STRUCT),
    "TEMP_CONFIG_STRUCT");
  Config->Protos = NewBitVector (NumProtos);

  Config->NumTimesSeen = 1;
  Config->MaxProtoId = MaxProtoId;
  Config->ProtoVectorSize = WordsInVectorOfSize (NumProtos);
  Config->ContextsSeen = NIL_LIST;
  zero_all_bits (Config->Protos, Config->ProtoVectorSize);
  Config->FontinfoId = FontinfoId;

  return (Config);

}                                /* NewTempConfig */
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 254 of file adaptive.cpp.

                          {
  return ((TEMP_PROTO)
    alloc_struct (sizeof (TEMP_PROTO_STRUCT), "TEMP_PROTO_STRUCT"));
}                                /* NewTempProto */
ADAPT_CLASS ReadAdaptedClass ( FILE *  File)

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

                                         {
  int NumTempProtos;
  int NumConfigs;
  int i;
  ADAPT_CLASS Class;
  TEMP_PROTO TempProto;

  /* first read high level adapted class structure */
  Class = (ADAPT_CLASS) Emalloc (sizeof (ADAPT_CLASS_STRUCT));
  fread ((char *) Class, sizeof (ADAPT_CLASS_STRUCT), 1, File);

  /* then read in the definitions of the permanent protos and configs */
  Class->PermProtos = NewBitVector (MAX_NUM_PROTOS);
  Class->PermConfigs = NewBitVector (MAX_NUM_CONFIGS);
  fread ((char *) Class->PermProtos, sizeof (uinT32),
    WordsInVectorOfSize (MAX_NUM_PROTOS), File);
  fread ((char *) Class->PermConfigs, sizeof (uinT32),
    WordsInVectorOfSize (MAX_NUM_CONFIGS), File);

  /* then read in the list of temporary protos */
  fread ((char *) &NumTempProtos, sizeof (int), 1, File);
  Class->TempProtos = NIL_LIST;
  for (i = 0; i < NumTempProtos; i++) {
    TempProto =
      (TEMP_PROTO) alloc_struct (sizeof (TEMP_PROTO_STRUCT),
      "TEMP_PROTO_STRUCT");
    fread ((char *) TempProto, sizeof (TEMP_PROTO_STRUCT), 1, File);
    Class->TempProtos = push_last (Class->TempProtos, TempProto);
  }

  /* then read in the adapted configs */
  fread ((char *) &NumConfigs, sizeof (int), 1, File);
  for (i = 0; i < NumConfigs; i++)
    if (test_bit (Class->PermConfigs, i))
      Class->Config[i].Perm = ReadPermConfig (File);
    else
      Class->Config[i].Temp = ReadTempConfig (File);

  return (Class);

}                                /* ReadAdaptedClass */
PERM_CONFIG ReadPermConfig ( FILE *  File)

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

                                       {
  PERM_CONFIG Config = (PERM_CONFIG) alloc_struct(sizeof(PERM_CONFIG_STRUCT),
                                                  "PERM_CONFIG_STRUCT");
  uinT8 NumAmbigs;
  fread ((char *) &NumAmbigs, sizeof(uinT8), 1, File);
  Config->Ambigs = (UNICHAR_ID *)Emalloc(sizeof(UNICHAR_ID) * (NumAmbigs + 1));
  fread(Config->Ambigs, sizeof(UNICHAR_ID), NumAmbigs, File);
  Config->Ambigs[NumAmbigs] = -1;
  fread(&(Config->FontinfoId), sizeof(int), 1, File);

  return (Config);

}                                /* ReadPermConfig */
TEMP_CONFIG ReadTempConfig ( FILE *  File)

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

                                       {
  TEMP_CONFIG Config;

  Config =
    (TEMP_CONFIG) alloc_struct (sizeof (TEMP_CONFIG_STRUCT),
    "TEMP_CONFIG_STRUCT");
  fread ((char *) Config, sizeof (TEMP_CONFIG_STRUCT), 1, File);

  Config->Protos = NewBitVector (Config->ProtoVectorSize * BITSINLONG);
  fread ((char *) Config->Protos, sizeof (uinT32),
    Config->ProtoVectorSize, File);

  return (Config);

}                                /* ReadTempConfig */
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 461 of file adaptive.cpp.

                                                                      {
  int NumTempProtos;
  LIST TempProtos;
  int i;

  /* first write high level adapted class structure */
  fwrite ((char *) Class, sizeof (ADAPT_CLASS_STRUCT), 1, File);

  /* then write out the definitions of the permanent protos and configs */
  fwrite ((char *) Class->PermProtos, sizeof (uinT32),
    WordsInVectorOfSize (MAX_NUM_PROTOS), File);
  fwrite ((char *) Class->PermConfigs, sizeof (uinT32),
    WordsInVectorOfSize (MAX_NUM_CONFIGS), File);

  /* then write out the list of temporary protos */
  NumTempProtos = count (Class->TempProtos);
  fwrite ((char *) &NumTempProtos, sizeof (int), 1, File);
  TempProtos = Class->TempProtos;
  iterate (TempProtos) {
    void* proto = first_node(TempProtos);
    fwrite ((char *) proto, sizeof (TEMP_PROTO_STRUCT), 1, File);
  }

  /* then write out the adapted configs */
  fwrite ((char *) &NumConfigs, sizeof (int), 1, File);
  for (i = 0; i < NumConfigs; i++)
    if (test_bit (Class->PermConfigs, i))
      WritePermConfig (File, Class->Config[i].Perm);
    else
      WriteTempConfig (File, Class->Config[i].Temp);

}                                /* WriteAdaptedClass */
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 537 of file adaptive.cpp.

                                                     {
  uinT8 NumAmbigs = 0;

  assert (Config != NULL);
  while (Config->Ambigs[NumAmbigs] > 0) ++NumAmbigs;

  fwrite((char *) &NumAmbigs, sizeof(uinT8), 1, File);
  fwrite(Config->Ambigs, sizeof(UNICHAR_ID), NumAmbigs, File);
  fwrite(&(Config->FontinfoId), sizeof(int), 1, File);
}                                /* WritePermConfig */
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 561 of file adaptive.cpp.

                                                     {
  assert (Config != NULL);
                                 /* contexts not yet implemented */
  assert (Config->ContextsSeen == NULL);

  fwrite ((char *) Config, sizeof (TEMP_CONFIG_STRUCT), 1, File);
  fwrite ((char *) Config->Protos, sizeof (uinT32),
    Config->ProtoVectorSize, File);

}                                /* WriteTempConfig */