classify/adaptive.cpp File Reference

#include "adaptive.h"
#include "emalloc.h"
#include "freelist.h"
#include <stdio.h>

Go to the source code of this file.

Functions


Function Documentation

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

Adds a new adapted class to an existing set of adapted templates.

Parameters:
Templates set of templates to add new class to
Class new class to add to templates
ClassId class id to associate with new class
Note:
Globals: none
Returns:
The class index of the new class.
Note:
Exceptions: none
Date:
Thu Mar 14 13:06:09 1991, DSJ, Created.

Definition at line 46 of file adaptive.cpp.

References AddIntClass(), assert(), ADAPT_TEMPLATES_STRUCT::Class, LegalClassId, NewIntClass(), NULL, ADAPT_CLASS_STRUCT::NumPermConfigs, ADAPT_TEMPLATES_STRUCT::Templates, and UnusedClassIdIn.

Referenced by MakeNewAdaptedClass().

00048                                       {
00049   INT_CLASS IntClass;
00050   CLASS_INDEX ClassIndex;
00051 
00052   assert (Templates != NULL);
00053   assert (Class != NULL);
00054   assert (LegalClassId (ClassId));
00055   assert (UnusedClassIdIn (Templates->Templates, ClassId));
00056   assert (Class->NumPermConfigs == 0);
00057 
00058   IntClass = NewIntClass (1, 1);
00059   ClassIndex = AddIntClass (Templates->Templates, ClassId, IntClass);
00060 
00061   assert (Templates->Class[ClassIndex] == NULL);
00062 
00063   Templates->Class[ClassIndex] = Class;
00064 
00065   return (ClassIndex);
00066 
00067 }                                /* AddAdaptedClass */

void free_adapted_class ( ADAPT_CLASS  adapt_class  ) 

Free up memory used.

Definition at line 136 of file adaptive.cpp.

References ConfigIsPermanent, destroy_nodes(), Efree(), FreeBitVector(), FreeTempConfig(), FreeTempProto(), MAX_NUM_CONFIGS, NULL, PermConfigFor, ADAPT_CLASS_STRUCT::PermConfigs, ADAPT_CLASS_STRUCT::PermProtos, TempConfigFor, and ADAPT_CLASS_STRUCT::TempProtos.

Referenced by free_adapted_templates().

00136                                                  { 
00137   int i;
00138 
00139   for (i = 0; i < MAX_NUM_CONFIGS; i++) {
00140     if (ConfigIsPermanent (adapt_class, i)
00141       && PermConfigFor (adapt_class, i) != NULL)
00142       Efree (PermConfigFor (adapt_class, i));
00143     else if (!ConfigIsPermanent (adapt_class, i)
00144       && TempConfigFor (adapt_class, i) != NULL)
00145       FreeTempConfig (TempConfigFor (adapt_class, i));
00146   }
00147   FreeBitVector (adapt_class->PermProtos);
00148   FreeBitVector (adapt_class->PermConfigs);
00149   destroy_nodes (adapt_class->TempProtos, FreeTempProto);
00150   Efree(adapt_class); 
00151 }

void free_adapted_templates ( ADAPT_TEMPLATES  templates  ) 

Free memory used.

Definition at line 185 of file adaptive.cpp.

References ADAPT_TEMPLATES_STRUCT::Class, Efree(), free_adapted_class(), free_int_templates(), NULL, NumClassesIn, and ADAPT_TEMPLATES_STRUCT::Templates.

Referenced by compare_tess_blobs(), and ResetAdaptiveClassifier().

00185                                                        { 
00186 
00187   if (templates != NULL) {
00188     int i;
00189     for (i = 0; i < NumClassesIn (templates->Templates); i++)
00190       free_adapted_class (templates->Class[i]);
00191     free_int_templates (templates->Templates);
00192     Efree(templates); 
00193   }
00194 }

void FreeTempConfig ( TEMP_CONFIG  Config  ) 

Frees all memory consumed by a temporary configuration.

Parameters:
Config config to be freed
Note:
Globals: none
Returns:
none
Note:
Exceptions: none
Date:
Thu Mar 14 13:34:23 1991, DSJ, Created.

Definition at line 80 of file adaptive.cpp.

References assert(), c_free_struct(), Config, destroy_nodes(), FreeBitVector(), memfree(), and NULL.

Referenced by free_adapted_class(), and MakePermanent().

00080                                         { 
00081   assert (Config != NULL);
00082 
00083   destroy_nodes (Config->ContextsSeen, memfree);
00084   FreeBitVector (Config->Protos);
00085   c_free_struct (Config, sizeof (TEMP_CONFIG_STRUCT), "TEMP_CONFIG_STRUCT");
00086 
00087 }                                /* FreeTempConfig */

void FreeTempProto ( void *  arg  ) 

Frees memory of a temporary prototype.

Definition at line 93 of file adaptive.cpp.

References c_free_struct().

Referenced by free_adapted_class(), and MakeTempProtoPerm().

00093                               { 
00094   PROTO proto = (PROTO) arg;
00095 
00096   c_free_struct (proto, sizeof (TEMP_PROTO_STRUCT), "TEMP_PROTO_STRUCT");
00097 }

ADAPT_CLASS NewAdaptedClass (  ) 

Allocates and initializes a new adapted class data structure and returns a ptr to it.

Parameters:
none 
Note:
Globals: none
Returns:
Ptr to new class data structure.
Note:
Exceptions: none
Date:
Thu Mar 14 12:58:13 1991, DSJ, Created.

Definition at line 111 of file adaptive.cpp.

References Emalloc(), MAX_NUM_CONFIGS, MAX_NUM_PROTOS, NewBitVector(), NIL, NULL, ADAPT_CLASS_STRUCT::NumPermConfigs, ADAPT_CLASS_STRUCT::PermConfigs, ADAPT_CLASS_STRUCT::PermProtos, TempConfigFor, ADAPT_CLASS_STRUCT::TempProtos, WordsInVectorOfSize, and zero_all_bits.

Referenced by MakeNewAdaptedClass().

00111                               { 
00112   ADAPT_CLASS Class;
00113   int i;
00114 
00115   Class = (ADAPT_CLASS) Emalloc (sizeof (ADAPT_CLASS_STRUCT));
00116   Class->NumPermConfigs = 0;
00117   Class->TempProtos = NIL;
00118 
00119   Class->PermProtos = NewBitVector (MAX_NUM_PROTOS);
00120   Class->PermConfigs = NewBitVector (MAX_NUM_CONFIGS);
00121   zero_all_bits (Class->PermProtos, WordsInVectorOfSize (MAX_NUM_PROTOS));
00122   zero_all_bits (Class->PermConfigs, WordsInVectorOfSize (MAX_NUM_CONFIGS));
00123 
00124   for (i = 0; i < MAX_NUM_CONFIGS; i++)
00125     TempConfigFor (Class, i) = NULL;
00126 
00127   return (Class);
00128 
00129 }                                /* NewAdaptedClass */

ADAPT_TEMPLATES NewAdaptedTemplates (  ) 

??

Parameters:
none 
Note:
Globals: none
Returns:
none
Note:
Exceptions: none
Date:
Fri Mar 8 10:15:28 1991, DSJ, Created.

Definition at line 164 of file adaptive.cpp.

References ADAPT_TEMPLATES_STRUCT::Class, Emalloc(), MAX_NUM_CLASSES, NewIntTemplates(), NULL, ADAPT_TEMPLATES_STRUCT::NumPermClasses, and ADAPT_TEMPLATES_STRUCT::Templates.

Referenced by AdaptiveClassifier(), compare_tess_blobs(), and InitAdaptiveClassifier().

00164                                       { 
00165   ADAPT_TEMPLATES Templates;
00166   int i;
00167 
00168   Templates = (ADAPT_TEMPLATES) Emalloc (sizeof (ADAPT_TEMPLATES_STRUCT));
00169 
00170   Templates->Templates = NewIntTemplates ();
00171   Templates->NumPermClasses = 0;
00172 
00173   for (i = 0; i < MAX_NUM_CLASSES; i++)
00174     Templates->Class[i] = NULL;
00175 
00176   return (Templates);
00177 
00178 }                                /* NewAdaptedTemplates */

TEMP_CONFIG NewTempConfig ( int  MaxProtoId  ) 

Allocates and returns a new temporary config.

Parameters:
MaxProtoId max id of any proto in new config
Note:
Globals: none
Returns:
Ptr to new temp config.
Note:
Exceptions: none
Date:
Thu Mar 14 13:28:21 1991, DSJ, Created.

Definition at line 207 of file adaptive.cpp.

References c_alloc_struct(), Config, NewBitVector(), NIL, WordsInVectorOfSize, and zero_all_bits.

Referenced by MakeNewAdaptedClass(), and MakeNewTemporaryConfig().

00207                                           { 
00208   TEMP_CONFIG Config;
00209   int NumProtos = MaxProtoId + 1;
00210 
00211   Config =
00212     (TEMP_CONFIG) c_alloc_struct (sizeof (TEMP_CONFIG_STRUCT),
00213     "TEMP_CONFIG_STRUCT");
00214   Config->Protos = NewBitVector (NumProtos);
00215 
00216   Config->NumTimesSeen = 1;
00217   Config->MaxProtoId = MaxProtoId;
00218   Config->ProtoVectorSize = WordsInVectorOfSize (NumProtos);
00219   Config->ContextsSeen = NIL;
00220   zero_all_bits (Config->Protos, Config->ProtoVectorSize);
00221 
00222   return (Config);
00223 
00224 }                                /* NewTempConfig */

TEMP_PROTO NewTempProto (  ) 

Allocates and returns a new temporary proto.

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

Definition at line 237 of file adaptive.cpp.

References c_alloc_struct().

Referenced by MakeNewAdaptedClass(), and MakeNewTempProtos().

00237                           { 
00238   return ((TEMP_PROTO)
00239     c_alloc_struct (sizeof (TEMP_PROTO_STRUCT), "TEMP_PROTO_STRUCT"));
00240 }                                /* NewTempProto */

void PrintAdaptedTemplates ( FILE *  File,
ADAPT_TEMPLATES  Templates 
)

Prints a summary of the adapted templates in Templates to File.

Parameters:
File open text file to print Templates to
Templates adapted templates to print to File
Note:
Globals: none
Returns:
none
Note:
Exceptions: none
Date:
Wed Mar 20 13:35:29 1991, DSJ, Created.

Definition at line 254 of file adaptive.cpp.

References ADAPT_TEMPLATES_STRUCT::Class, ClassForIndex, ClassIdForIndex, count(), NumClassesIn, NumIntConfigsIn, NumIntProtosIn, ADAPT_TEMPLATES_STRUCT::NumPermClasses, ADAPT_CLASS_STRUCT::NumPermConfigs, ADAPT_TEMPLATES_STRUCT::Templates, and ADAPT_CLASS_STRUCT::TempProtos.

Referenced by InitAdaptiveClassifier(), and PrintAdaptiveStatistics().

00254                                                                   { 
00255   int i;
00256   INT_CLASS IClass;
00257   ADAPT_CLASS AClass;
00258 
00259   #ifndef SECURE_NAMES
00260   fprintf (File, "\n\nSUMMARY OF ADAPTED TEMPLATES:\n\n");
00261   fprintf (File, "Num classes = %d;  Num permanent classes = %d\n\n",
00262     NumClassesIn (Templates->Templates), Templates->NumPermClasses);
00263   fprintf (File, "Index Id  NC NPC  NP NPP\n");
00264   fprintf (File, "------------------------\n");
00265 
00266   for (i = 0; i < NumClassesIn (Templates->Templates); i++) {
00267     IClass = ClassForIndex (Templates->Templates, i);
00268     AClass = Templates->Class[i];
00269 
00270     fprintf (File, "%5d  %c %3d %3d %3d %3d\n",
00271       i, ClassIdForIndex (Templates->Templates, i),
00272       NumIntConfigsIn (IClass), AClass->NumPermConfigs,
00273       NumIntProtosIn (IClass),
00274       NumIntProtosIn (IClass) - count (AClass->TempProtos));
00275   }
00276   #endif
00277   fprintf (File, "\n");
00278 
00279 }                                /* PrintAdaptedTemplates */

ADAPT_CLASS ReadAdaptedClass ( FILE *  File  ) 

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

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

Definition at line 293 of file adaptive.cpp.

References c_alloc_struct(), Emalloc(), MAX_NUM_CONFIGS, MAX_NUM_PROTOS, NewBitVector(), NIL, push_last(), ReadPermConfig(), ReadTempConfig(), test_bit, and WordsInVectorOfSize.

Referenced by ReadAdaptedTemplates().

00293                                          { 
00294   int NumTempProtos;
00295   int NumConfigs;
00296   int i;
00297   ADAPT_CLASS Class;
00298   TEMP_PROTO TempProto;
00299 
00300   /* 1: Read high level adapted class structure */
00301   Class = (ADAPT_CLASS) Emalloc (sizeof (ADAPT_CLASS_STRUCT));
00302   fread ((char *) Class, sizeof (ADAPT_CLASS_STRUCT), 1, File);
00303 
00304   /* 2: Read in the definitions of the permanent protos and configs */
00305   Class->PermProtos = NewBitVector (MAX_NUM_PROTOS);
00306   Class->PermConfigs = NewBitVector (MAX_NUM_CONFIGS);
00307   fread ((char *) Class->PermProtos, sizeof (UINT32),
00308     WordsInVectorOfSize (MAX_NUM_PROTOS), File);
00309   fread ((char *) Class->PermConfigs, sizeof (UINT32),
00310     WordsInVectorOfSize (MAX_NUM_CONFIGS), File);
00311 
00312   /* 3: Read in the list of temporary protos */
00313   fread ((char *) &NumTempProtos, sizeof (int), 1, File);
00314   Class->TempProtos = NIL;
00315   for (i = 0; i < NumTempProtos; i++) {
00316     TempProto =
00317       (TEMP_PROTO) c_alloc_struct (sizeof (TEMP_PROTO_STRUCT),
00318       "TEMP_PROTO_STRUCT");
00319     fread ((char *) TempProto, sizeof (TEMP_PROTO_STRUCT), 1, File);
00320     Class->TempProtos = push_last (Class->TempProtos, TempProto);
00321   }
00322 
00323   /* 4: Read in the adapted configs */
00324   fread ((char *) &NumConfigs, sizeof (int), 1, File);
00325   for (i = 0; i < NumConfigs; i++)
00326     if (test_bit (Class->PermConfigs, i))
00327       Class->Config[i].Perm = ReadPermConfig (File);
00328   else
00329     Class->Config[i].Temp = ReadTempConfig (File);
00330 
00331   return (Class);
00332 
00333 }                                /* ReadAdaptedClass */

ADAPT_TEMPLATES ReadAdaptedTemplates ( FILE *  File  ) 

Read a set of adapted templates from File and return a ptr to the templates.

Parameters:
File open text file to read adapted templates from
Note:
Globals: none
Returns:
Ptr to adapted templates read from File.
Note:
Exceptions: none
Date:
Mon Mar 18 15:18:10 1991, DSJ, Created.

Definition at line 347 of file adaptive.cpp.

References Emalloc(), FALSE, NumClassesIn, ReadAdaptedClass(), and ReadIntTemplates().

Referenced by InitAdaptiveClassifier().

00347                                                  { 
00348   int i;
00349   ADAPT_TEMPLATES Templates;
00350 
00351   /* first read the high level adaptive template struct */
00352   Templates = (ADAPT_TEMPLATES) Emalloc (sizeof (ADAPT_TEMPLATES_STRUCT));
00353   fread ((char *) Templates, sizeof (ADAPT_TEMPLATES_STRUCT), 1, File);
00354 
00355   /* then read in the basic integer templates */
00356   Templates->Templates = ReadIntTemplates (File, FALSE);
00357 
00358   /* then read in the adaptive info for each class */
00359   for (i = 0; i < NumClassesIn (Templates->Templates); i++) {
00360     Templates->Class[i] = ReadAdaptedClass (File);
00361   }
00362   return (Templates);
00363 
00364 }                                /* ReadAdaptedTemplates */

PERM_CONFIG ReadPermConfig ( FILE *  File  ) 

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

Parameters:
File open file to read permanent config from
Note:
Globals: none
Returns:
Ptr to new permanent configuration description.
Note:
First UINT8 holds the number of ambiguities in the file, then read in that many configs.

Exceptions: none

Date:
Tue Mar 19 14:25:26 1991, DSJ, Created.

Definition at line 380 of file adaptive.cpp.

References Config, and Emalloc().

Referenced by ReadAdaptedClass().

00380                                        { 
00381   PERM_CONFIG Config;
00382   UINT8 NumAmbigs;
00383 
00384   fread ((char *) &NumAmbigs, sizeof (UINT8), 1, File);
00385   Config = (PERM_CONFIG) Emalloc (sizeof (char) * (NumAmbigs + 1));
00386   fread (Config, sizeof (char), NumAmbigs, File);
00387   Config[NumAmbigs] = '\0';
00388 
00389   return (Config);
00390 
00391 }                                /* ReadPermConfig */

TEMP_CONFIG ReadTempConfig ( FILE *  File  ) 

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

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

Definition at line 405 of file adaptive.cpp.

References BITSINLONG, c_alloc_struct(), Config, and NewBitVector().

Referenced by ReadAdaptedClass().

00405                                        { 
00406   TEMP_CONFIG Config;
00407 
00408   Config =
00409     (TEMP_CONFIG) c_alloc_struct (sizeof (TEMP_CONFIG_STRUCT),
00410     "TEMP_CONFIG_STRUCT");
00411   fread ((char *) Config, sizeof (TEMP_CONFIG_STRUCT), 1, File);
00412 
00413   Config->Protos = NewBitVector (Config->ProtoVectorSize * BITSINLONG);
00414   fread ((char *) Config->Protos, sizeof (UINT32),
00415     Config->ProtoVectorSize, File);
00416 
00417   return (Config);
00418 
00419 }                                /* ReadTempConfig */

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

Writes a binary representation of Class to File.

Parameters:
File open file to write Class to
Class adapted class to write to File
NumConfigs number of configs in Class
Note:
Globals: none
Returns:
none
Note:
Exceptions: none
Date:
Tue Mar 19 13:33:51 1991, DSJ, Created.

Definition at line 434 of file adaptive.cpp.

References count(), first, iterate, MAX_NUM_CONFIGS, MAX_NUM_PROTOS, test_bit, WordsInVectorOfSize, WritePermConfig(), and WriteTempConfig().

Referenced by WriteAdaptedTemplates().

00434                                                                       { 
00435   int NumTempProtos;
00436   LIST TempProtos;
00437   int i;
00438 
00439   /* first write high level adapted class structure */
00440   fwrite ((char *) Class, sizeof (ADAPT_CLASS_STRUCT), 1, File);
00441 
00442   /* then write out the definitions of the permanent protos and configs */
00443   fwrite ((char *) Class->PermProtos, sizeof (UINT32),
00444     WordsInVectorOfSize (MAX_NUM_PROTOS), File);
00445   fwrite ((char *) Class->PermConfigs, sizeof (UINT32),
00446     WordsInVectorOfSize (MAX_NUM_CONFIGS), File);
00447 
00448   /* then write out the list of temporary protos */
00449   NumTempProtos = count (Class->TempProtos);
00450   fwrite ((char *) &NumTempProtos, sizeof (int), 1, File);
00451   TempProtos = Class->TempProtos;
00452   iterate (TempProtos) {
00453     void* proto = first(TempProtos);
00454     fwrite ((char *) proto, sizeof (TEMP_PROTO_STRUCT), 1, File);
00455   }
00456 
00457   /* then write out the adapted configs */
00458   fwrite ((char *) &NumConfigs, sizeof (int), 1, File);
00459   for (i = 0; i < NumConfigs; i++)
00460     if (test_bit (Class->PermConfigs, i))
00461       WritePermConfig (File, Class->Config[i].Perm);
00462   else
00463     WriteTempConfig (File, Class->Config[i].Temp);
00464 
00465 }// WriteAdaptedClass

void WriteAdaptedTemplates ( FILE *  File,
ADAPT_TEMPLATES  Templates 
)

Saves Templates to File in a binary format.

Parameters:
File open text file to write Templates to
Templates set of adapted templates to write to File
Note:
Globals: none
Returns:
none
Note:
Exceptions: none
Date:
Mon Mar 18 15:07:32 1991, DSJ, Created.

Definition at line 479 of file adaptive.cpp.

References ClassForIndex, NumClassesIn, NumIntConfigsIn, WriteAdaptedClass(), and WriteIntTemplates().

Referenced by EndAdaptiveClassifier().

00479                                                                   { 
00480   int i;
00481 
00482   /* first write the high level adaptive template struct */
00483   fwrite ((char *) Templates, sizeof (ADAPT_TEMPLATES_STRUCT), 1, File);
00484 
00485   /* then write out the basic integer templates */
00486   WriteIntTemplates (File, Templates->Templates);
00487 
00488   /* then write out the adaptive info for each class */
00489   for (i = 0; i < NumClassesIn (Templates->Templates); i++) {
00490     WriteAdaptedClass (File, Templates->Class[i],
00491       NumIntConfigsIn (ClassForIndex
00492       (Templates->Templates, i)));
00493   }
00494 }                                /* WriteAdaptedTemplates */

void WritePermConfig ( FILE *  File,
PERM_CONFIG  Config 
)

Writes a binary representation of a permanent configuration to File.

Parameters:
File open file to write Config to
Config permanent config to write to File
Note:
Globals: none
Returns:
none
Note:
Exceptions: none
Date:
Tue Mar 19 13:55:44 1991, DSJ, Created.

Definition at line 508 of file adaptive.cpp.

References assert(), and NULL.

Referenced by WriteAdaptedClass().

00508                                                      { 
00509   UINT8 NumAmbigs;
00510 
00511   assert (Config != NULL);
00512 
00513   NumAmbigs = strlen (Config);
00514   fwrite ((char *) &NumAmbigs, sizeof (UINT8), 1, File);
00515   fwrite (Config, sizeof (char), NumAmbigs, File);
00516 
00517 }                                /* WritePermConfig */

void WriteTempConfig ( FILE *  File,
TEMP_CONFIG  Config 
)

Writes a binary representation of a temporary configuration to File.

Parameters:
File open file to write Config to
Config temporary config to write to File
Note:
Globals: none
Returns:
none
Note:
Exceptions: none
Date:
Tue Mar 19 14:00:28 1991, DSJ, Created.

Definition at line 531 of file adaptive.cpp.

References assert(), Config, and NULL.

Referenced by WriteAdaptedClass().

00531                                                      { 
00532   assert (Config != NULL);
00533                                  /* contexts not yet implemented */
00534   assert (Config->ContextsSeen == NULL);
00535 
00536   fwrite ((char *) Config, sizeof (TEMP_CONFIG_STRUCT), 1, File);
00537   fwrite ((char *) Config->Protos, sizeof (UINT32),
00538     Config->ProtoVectorSize, File);
00539 
00540 }                                /* WriteTempConfig */


Generated on Wed Feb 28 19:49:17 2007 for Tesseract by  doxygen 1.5.1