/*---------------------------------------------------------------------------*/ ADAPT_CLASS NewAdaptedClass() { /* ** Parameters: none ** Globals: none ** Operation: This operation allocates and initializes a new adapted ** class data structure and returns a ptr to it. ** Return: Ptr to new class data structure. ** Exceptions: none ** History: Thu Mar 14 12:58:13 1991, DSJ, Created. */ ADAPT_CLASS Class; int i; Class = (ADAPT_CLASS) Emalloc (sizeof (ADAPT_CLASS_STRUCT)); Class->NumPermConfigs = 0; Class->TempProtos = NIL; 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 */
/*---------------------------------------------------------------------------*/ ADAPT_CLASS ReadAdaptedClass(FILE *File) { /* ** Parameters: ** File open file to read adapted class from ** Globals: none ** Operation: Read an adapted class description from File and return ** a ptr to the adapted class. ** Return: Ptr to new adapted class. ** Exceptions: none ** History: Tue Mar 19 14:11:01 1991, DSJ, Created. */ 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; for (i = 0; i < NumTempProtos; i++) { TempProto = (TEMP_PROTO) c_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 */
/********************************************************************** * AddConfigToClass * * Add a new config to this class. Malloc new space and copy the * old configs if necessary. Return the config id for the new config. **********************************************************************/ int AddConfigToClass(CLASS_TYPE Class) { int NewNumConfigs; int NewConfig; int MaxNumProtos; BIT_VECTOR Config; MaxNumProtos = Class->MaxNumProtos; if (Class->NumConfigs >= Class->MaxNumConfigs) { /* add configs in CONFIG_INCREMENT chunks at a time */ NewNumConfigs = (((Class->MaxNumConfigs + CONFIG_INCREMENT) / CONFIG_INCREMENT) * CONFIG_INCREMENT); Class->Configurations = (CONFIGS) Erealloc (Class->Configurations, sizeof (BIT_VECTOR) * NewNumConfigs); Class->MaxNumConfigs = NewNumConfigs; } NewConfig = Class->NumConfigs++; Config = NewBitVector (MaxNumProtos); Class->Configurations[NewConfig] = Config; zero_all_bits (Config, WordsInVectorOfSize (MaxNumProtos)); return (NewConfig); }
/*---------------------------------------------------------------------------*/ TEMP_CONFIG ReadTempConfig(FILE *File) { /* ** Parameters: ** File open file to read temporary config from ** Globals: none ** Operation: Read a temporary configuration description from File ** and return a ptr to it. ** Return: Ptr to new temporary configuration description. ** Exceptions: none ** History: Tue Mar 19 14:29:59 1991, DSJ, Created. */ TEMP_CONFIG Config; Config = (TEMP_CONFIG) c_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 */
/*---------------------------------------------------------------------------*/ TEMP_CONFIG NewTempConfig(int MaxProtoId) { /* ** Parameters: ** MaxProtoId max id of any proto in new config ** Globals: none ** Operation: This routine allocates and returns a new temporary ** config. ** Return: Ptr to new temp config. ** Exceptions: none ** History: Thu Mar 14 13:28:21 1991, DSJ, Created. */ TEMP_CONFIG Config; int NumProtos = MaxProtoId + 1; Config = (TEMP_CONFIG) c_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; zero_all_bits (Config->Protos, Config->ProtoVectorSize); return (Config); } /* NewTempConfig */
/** * This operation allocates and initializes a new adapted * class data structure and returns a ptr to it. * * @return Ptr to new class data structure. * * @note Globals: none * @note Exceptions: none * @note History: Thu Mar 14 12:58:13 1991, DSJ, Created. */ ADAPT_CLASS NewAdaptedClass() { 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 */
/** * Read an adapted class description from File and return * a ptr to the adapted class. * * @param File open file to read adapted class from * @return Ptr to new adapted class. * * @note Globals: none * @note Exceptions: none * @note History: Tue Mar 19 14:11:01 1991, DSJ, Created. */ ADAPT_CLASS ReadAdaptedClass(FILE *File) { 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 */
/** * Read a temporary configuration description from File * and return a ptr to it. * * @param File open file to read temporary config from * @return Ptr to new temporary configuration description. * * @note Globals: none * @note Exceptions: none * @note History: Tue Mar 19 14:29:59 1991, DSJ, Created. */ TEMP_CONFIG ReadTempConfig(FILE *File) { 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 */
/** * This routine allocates and returns a new temporary config. * * @param MaxProtoId max id of any proto in new config * @param FontinfoId font information from pre-trained templates * @return Ptr to new temp config. * * @note Globals: none * @note Exceptions: none * @note History: Thu Mar 14 13:28:21 1991, DSJ, Created. */ TEMP_CONFIG NewTempConfig(int MaxProtoId, int FontinfoId) { 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 */
/********************************************************************** * ReadConfigs * * Read the prototype configurations for this class from a file. Read * the requested number of lines. **********************************************************************/ void ReadConfigs(register FILE *File, CLASS_TYPE Class) { inT16 Cid; register inT16 Wid; register BIT_VECTOR ThisConfig; int NumWords; int NumConfigs; fscanf (File, "%d %d\n", &NumConfigs, &NumWords); Class->NumConfigs = NumConfigs; Class->MaxNumConfigs = NumConfigs; Class->Configurations = (CONFIGS) Emalloc (sizeof (BIT_VECTOR) * NumConfigs); NumWords = WordsInVectorOfSize (Class->NumProtos); for (Cid = 0; Cid < NumConfigs; Cid++) { ThisConfig = NewBitVector (Class->NumProtos); for (Wid = 0; Wid < NumWords; Wid++) fscanf (File, "%x", &ThisConfig[Wid]); Class->Configurations[Cid] = ThisConfig; } }
/** SetUpForFloat2Int **************************************************/ void SetUpForFloat2Int( LIST LabeledClassList) { MERGE_CLASS MergeClass; CLASS_TYPE Class; int NumProtos; int NumConfigs; int NumWords; int i, j; float Values[3]; PROTO NewProto; PROTO OldProto; BIT_VECTOR NewConfig; BIT_VECTOR OldConfig; // printf("Float2Int ...\n"); iterate(LabeledClassList) { UnicityTableEqEq<int> font_set; MergeClass = (MERGE_CLASS) first_node (LabeledClassList); Class = &TrainingData[unicharset_training.unichar_to_id( MergeClass->Label)]; NumProtos = MergeClass->Class->NumProtos; NumConfigs = MergeClass->Class->NumConfigs; font_set.move(&MergeClass->Class->font_set); Class->NumProtos = NumProtos; Class->MaxNumProtos = NumProtos; Class->Prototypes = (PROTO) Emalloc (sizeof(PROTO_STRUCT) * NumProtos); for(i=0; i < NumProtos; i++) { NewProto = ProtoIn(Class, i); OldProto = ProtoIn(MergeClass->Class, i); Values[0] = OldProto->X; Values[1] = OldProto->Y; Values[2] = OldProto->Angle; Normalize(Values); NewProto->X = OldProto->X; NewProto->Y = OldProto->Y; NewProto->Length = OldProto->Length; NewProto->Angle = OldProto->Angle; NewProto->A = Values[0]; NewProto->B = Values[1]; NewProto->C = Values[2]; } Class->NumConfigs = NumConfigs; Class->MaxNumConfigs = NumConfigs; Class->font_set.move(&font_set); Class->Configurations = (BIT_VECTOR*) Emalloc (sizeof(BIT_VECTOR) * NumConfigs); NumWords = WordsInVectorOfSize(NumProtos); for(i=0; i < NumConfigs; i++) { NewConfig = NewBitVector(NumProtos); OldConfig = MergeClass->Class->Configurations[i]; for(j=0; j < NumWords; j++) NewConfig[j] = OldConfig[j]; Class->Configurations[i] = NewConfig; } } } // SetUpForFloat2Int