Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
protos.cpp File Reference
#include "protos.h"
#include "const.h"
#include "emalloc.h"
#include "freelist.h"
#include "callcpp.h"
#include "tprintf.h"
#include "scanutils.h"
#include "globals.h"
#include "classify.h"
#include "params.h"
#include <stdio.h>
#include <math.h>

Go to the source code of this file.

Namespaces

 tesseract
 

Macros

#define PROTO_INCREMENT   32
 
#define CONFIG_INCREMENT   16
 

Functions

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.

Parameters
ClassThe class to add to
int AddConfigToClass (CLASS_TYPE Class)
 
AddProtoToClass

Add a new proto to this class. Malloc new space and copy the old protos if necessary. Return the proto id for the new proto.

Parameters
ClassThe class to add to
int AddProtoToClass (CLASS_TYPE Class)
 
ClassConfigLength

Return the length of all the protos in this class.

Parameters
ClassThe class to add to
ConfigFIXME
FLOAT32 ClassConfigLength (CLASS_TYPE Class, BIT_VECTOR Config)
 
ClassProtoLength

Return the length of all the protos in this class.

Parameters
ClassThe class to use
FLOAT32 ClassProtoLength (CLASS_TYPE Class)
 
CopyProto

Copy the first proto into the second.

Parameters
SrcSource
DestDestination
void CopyProto (PROTO Src, PROTO Dest)
 
void FillABC (PROTO Proto)
 
void FreeClass (CLASS_TYPE Class)
 
void FreeClassFields (CLASS_TYPE Class)
 
CLASS_TYPE NewClass (int NumProtos, int NumConfigs)
 
void PrintProtos (CLASS_TYPE Class)
 
void ReadClassFromFile (FILE *File, UNICHAR_ID unichar_id)
 
void ReadConfigs (register FILE *File, CLASS_TYPE Class)
 
void ReadProtos (register FILE *File, CLASS_TYPE Class)
 
SplitProto

Add a new proto to this class. Malloc new space and copy the old protos if necessary. Return the proto id for the new proto. Update all configurations so that each config which contained the specified old proto will also contain the new proto. The caller is responsible for actually filling in the appropriate proto params.

int SplitProto (CLASS_TYPE Class, int OldPid)
 
WriteOldConfigFile
Deprecated:
Write the configs in the given class to the specified file in the old config format.
Parameters
FileThe file to write to
ClassThe class to write
void WriteOldConfigFile (FILE *File, CLASS_TYPE Class)
 
WriteOldProtoFile
Deprecated:
Write the protos in the given class to the specified file in the old proto format.
Parameters
FileThe file to write to
ClassThe class to write
void WriteOldProtoFile (FILE *File, CLASS_TYPE Class)
 

Variables

CLASS_STRUCT TrainingData [NUMBER_OF_CLASSES]
 
char * classify_training_file = "MicroFeatures"
 

Macro Definition Documentation

#define CONFIG_INCREMENT   16

Definition at line 43 of file protos.cpp.

#define PROTO_INCREMENT   32

Definition at line 42 of file protos.cpp.

Function Documentation

int AddConfigToClass ( CLASS_TYPE  Class)

Definition at line 63 of file protos.cpp.

63  {
64  int NewNumConfigs;
65  int NewConfig;
66  int MaxNumProtos;
68 
69  MaxNumProtos = Class->MaxNumProtos;
70 
71  if (Class->NumConfigs >= Class->MaxNumConfigs) {
72  /* add configs in CONFIG_INCREMENT chunks at a time */
73  NewNumConfigs = (((Class->MaxNumConfigs + CONFIG_INCREMENT) /
75 
76  Class->Configurations =
78  sizeof (BIT_VECTOR) * NewNumConfigs);
79 
80  Class->MaxNumConfigs = NewNumConfigs;
81  }
82  NewConfig = Class->NumConfigs++;
83  Config = NewBitVector (MaxNumProtos);
84  Class->Configurations[NewConfig] = Config;
85  zero_all_bits (Config, WordsInVectorOfSize (MaxNumProtos));
86 
87  return (NewConfig);
88 }
void * Erealloc(void *ptr, size_t size)
Definition: emalloc.cpp:70
#define zero_all_bits(array, length)
Definition: bitvec.h:33
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
CONFIGS Configurations
Definition: protos.h:64
CLUSTERCONFIG Config
inT16 NumConfigs
Definition: protos.h:62
BIT_VECTOR * CONFIGS
Definition: protos.h:40
uinT32 * BIT_VECTOR
Definition: bitvec.h:28
#define CONFIG_INCREMENT
Definition: protos.cpp:43
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:111
inT16 MaxNumProtos
Definition: protos.h:60
inT16 MaxNumConfigs
Definition: protos.h:63
int AddProtoToClass ( CLASS_TYPE  Class)

Definition at line 99 of file protos.cpp.

99  {
100  int i;
101  int Bit;
102  int NewNumProtos;
103  int NewProto;
105 
106  if (Class->NumProtos >= Class->MaxNumProtos) {
107  /* add protos in PROTO_INCREMENT chunks at a time */
108  NewNumProtos = (((Class->MaxNumProtos + PROTO_INCREMENT) /
110 
111  Class->Prototypes = (PROTO) Erealloc (Class->Prototypes,
112  sizeof (PROTO_STRUCT) *
113  NewNumProtos);
114 
115  Class->MaxNumProtos = NewNumProtos;
116 
117  for (i = 0; i < Class->NumConfigs; i++) {
118  Config = Class->Configurations[i];
119  Class->Configurations[i] = ExpandBitVector (Config, NewNumProtos);
120 
121  for (Bit = Class->NumProtos; Bit < NewNumProtos; Bit++)
122  reset_bit(Config, Bit);
123  }
124  }
125  NewProto = Class->NumProtos++;
126  if (Class->NumProtos > MAX_NUM_PROTOS) {
127  tprintf("Ouch! number of protos = %d, vs max of %d!",
128  Class->NumProtos, MAX_NUM_PROTOS);
129  }
130  return (NewProto);
131 }
#define reset_bit(array, bit)
Definition: bitvec.h:59
void * Erealloc(void *ptr, size_t size)
Definition: emalloc.cpp:70
#define MAX_NUM_PROTOS
Definition: intproto.h:45
inT16 NumProtos
Definition: protos.h:59
CONFIGS Configurations
Definition: protos.h:64
CLUSTERCONFIG Config
inT16 NumConfigs
Definition: protos.h:62
uinT32 * BIT_VECTOR
Definition: bitvec.h:28
#define PROTO_INCREMENT
Definition: protos.cpp:42
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
PROTO_STRUCT * PROTO
Definition: protos.h:52
inT16 MaxNumProtos
Definition: protos.h:60
BIT_VECTOR ExpandBitVector(BIT_VECTOR Vector, int NewNumBits)
Definition: bitvec.cpp:48
PROTO Prototypes
Definition: protos.h:61
FLOAT32 ClassConfigLength ( CLASS_TYPE  Class,
BIT_VECTOR  Config 
)

Definition at line 142 of file protos.cpp.

142  {
143  inT16 Pid;
144  FLOAT32 TotalLength = 0;
145 
146  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
147  if (test_bit (Config, Pid)) {
148 
149  TotalLength += (ProtoIn (Class, Pid))->Length;
150  }
151  }
152  return (TotalLength);
153 }
inT16 NumProtos
Definition: protos.h:59
float FLOAT32
Definition: host.h:111
#define test_bit(array, bit)
Definition: bitvec.h:61
#define ProtoIn(Class, Pid)
Definition: protos.h:123
CLUSTERCONFIG Config
short inT16
Definition: host.h:100
FLOAT32 ClassProtoLength ( CLASS_TYPE  Class)

Definition at line 163 of file protos.cpp.

163  {
164  inT16 Pid;
165  FLOAT32 TotalLength = 0;
166 
167  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
168  TotalLength += (ProtoIn (Class, Pid))->Length;
169  }
170  return (TotalLength);
171 }
inT16 NumProtos
Definition: protos.h:59
float FLOAT32
Definition: host.h:111
#define ProtoIn(Class, Pid)
Definition: protos.h:123
short inT16
Definition: host.h:100
void CopyProto ( PROTO  Src,
PROTO  Dest 
)

Definition at line 182 of file protos.cpp.

182  {
183  Dest->X = Src->X;
184  Dest->Y = Src->Y;
185  Dest->Length = Src->Length;
186  Dest->Angle = Src->Angle;
187  Dest->A = Src->A;
188  Dest->B = Src->B;
189  Dest->C = Src->C;
190 }
FLOAT32 B
Definition: protos.h:45
FLOAT32 Length
Definition: protos.h:50
FLOAT32 Y
Definition: protos.h:48
FLOAT32 A
Definition: protos.h:44
FLOAT32 Angle
Definition: protos.h:49
FLOAT32 X
Definition: protos.h:47
FLOAT32 C
Definition: protos.h:46
void FillABC ( PROTO  Proto)

Definition at line 198 of file protos.cpp.

198  {
199  FLOAT32 Slope, Intercept, Normalizer;
200 
201  Slope = tan (Proto->Angle * 2.0 * PI);
202  Intercept = Proto->Y - Slope * Proto->X;
203  Normalizer = 1.0 / sqrt (Slope * Slope + 1.0);
204  Proto->A = Slope * Normalizer;
205  Proto->B = -Normalizer;
206  Proto->C = Intercept * Normalizer;
207 }
FLOAT32 B
Definition: protos.h:45
FLOAT32 Y
Definition: protos.h:48
#define PI
Definition: const.h:19
float FLOAT32
Definition: host.h:111
FLOAT32 A
Definition: protos.h:44
FLOAT32 Angle
Definition: protos.h:49
FLOAT32 X
Definition: protos.h:47
FLOAT32 C
Definition: protos.h:46
void FreeClass ( CLASS_TYPE  Class)

Definition at line 215 of file protos.cpp.

215  {
216  if (Class) {
217  FreeClassFields(Class);
218  delete Class;
219  }
220 }
void FreeClassFields(CLASS_TYPE Class)
Definition: protos.cpp:228
void FreeClassFields ( CLASS_TYPE  Class)

Definition at line 228 of file protos.cpp.

228  {
229  int i;
230 
231  if (Class) {
232  if (Class->MaxNumProtos > 0)
233  memfree (Class->Prototypes);
234  if (Class->MaxNumConfigs > 0) {
235  for (i = 0; i < Class->NumConfigs; i++)
236  FreeBitVector (Class->Configurations[i]);
237  memfree (Class->Configurations);
238  }
239  }
240 }
void memfree(void *element)
Definition: freelist.cpp:30
CONFIGS Configurations
Definition: protos.h:64
void FreeBitVector(BIT_VECTOR BitVector)
Definition: bitvec.cpp:55
inT16 NumConfigs
Definition: protos.h:62
inT16 MaxNumProtos
Definition: protos.h:60
inT16 MaxNumConfigs
Definition: protos.h:63
PROTO Prototypes
Definition: protos.h:61
CLASS_TYPE NewClass ( int  NumProtos,
int  NumConfigs 
)

Definition at line 248 of file protos.cpp.

248  {
249  CLASS_TYPE Class;
250 
251  Class = new CLASS_STRUCT;
252 
253  if (NumProtos > 0)
254  Class->Prototypes = (PROTO) Emalloc (NumProtos * sizeof (PROTO_STRUCT));
255 
256  if (NumConfigs > 0)
257  Class->Configurations = (CONFIGS) Emalloc (NumConfigs *
258  sizeof (BIT_VECTOR));
259  Class->MaxNumProtos = NumProtos;
260  Class->MaxNumConfigs = NumConfigs;
261  Class->NumProtos = 0;
262  Class->NumConfigs = 0;
263  return (Class);
264 
265 }
inT16 NumProtos
Definition: protos.h:59
CONFIGS Configurations
Definition: protos.h:64
inT16 NumConfigs
Definition: protos.h:62
BIT_VECTOR * CONFIGS
Definition: protos.h:40
uinT32 * BIT_VECTOR
Definition: bitvec.h:28
void * Emalloc(size_t Size)
Definition: emalloc.cpp:35
PROTO_STRUCT * PROTO
Definition: protos.h:52
inT16 MaxNumProtos
Definition: protos.h:60
inT16 MaxNumConfigs
Definition: protos.h:63
PROTO Prototypes
Definition: protos.h:61
void PrintProtos ( CLASS_TYPE  Class)

Definition at line 273 of file protos.cpp.

273  {
274  inT16 Pid;
275 
276  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
277  cprintf ("Proto %d:\t", Pid);
278  PrintProto (ProtoIn (Class, Pid));
279  cprintf ("\t");
280  PrintProtoLine (ProtoIn (Class, Pid));
281  new_line();
282  }
283 }
#define PrintProtoLine(Proto)
Definition: protos.h:148
inT16 NumProtos
Definition: protos.h:59
#define ProtoIn(Class, Pid)
Definition: protos.h:123
#define PrintProto(Proto)
Definition: protos.h:133
short inT16
Definition: host.h:100
#define new_line()
Definition: cutil.h:83
void cprintf(const char *format,...)
Definition: callcpp.cpp:41
void ReadClassFromFile ( FILE *  File,
UNICHAR_ID  unichar_id 
)

ReadClassFromFile

Read in a class description (protos and configs) from a file. Update the class structure record.

Definition at line 321 of file protos.cpp.

321  {
322  CLASS_TYPE Class;
323 
324  Class = &TrainingData[unichar_id];
325 
326  ReadProtos(File, Class);
327 
328  ReadConfigs(File, Class);
329 }
void ReadProtos(register FILE *File, CLASS_TYPE Class)
Definition: protos.cpp:367
void ReadConfigs(register FILE *File, CLASS_TYPE Class)
Definition: protos.cpp:337
CLASS_STRUCT TrainingData[NUMBER_OF_CLASSES]
Definition: protos.cpp:48
void ReadConfigs ( register FILE *  File,
CLASS_TYPE  Class 
)

ReadConfigs

Read the prototype configurations for this class from a file. Read the requested number of lines.

Definition at line 337 of file protos.cpp.

337  {
338  inT16 Cid;
339  register inT16 Wid;
340  register BIT_VECTOR ThisConfig;
341  int NumWords;
342  int NumConfigs;
343 
344  fscanf (File, "%d %d\n", &NumConfigs, &NumWords);
345  Class->NumConfigs = NumConfigs;
346  Class->MaxNumConfigs = NumConfigs;
347  Class->Configurations =
348  (CONFIGS) Emalloc (sizeof (BIT_VECTOR) * NumConfigs);
349  NumWords = WordsInVectorOfSize (Class->NumProtos);
350 
351  for (Cid = 0; Cid < NumConfigs; Cid++) {
352 
353  ThisConfig = NewBitVector (Class->NumProtos);
354  for (Wid = 0; Wid < NumWords; Wid++)
355  fscanf (File, "%x", &ThisConfig[Wid]);
356  Class->Configurations[Cid] = ThisConfig;
357  }
358 }
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
inT16 NumProtos
Definition: protos.h:59
CONFIGS Configurations
Definition: protos.h:64
inT16 NumConfigs
Definition: protos.h:62
BIT_VECTOR * CONFIGS
Definition: protos.h:40
uinT32 * BIT_VECTOR
Definition: bitvec.h:28
void * Emalloc(size_t Size)
Definition: emalloc.cpp:35
short inT16
Definition: host.h:100
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:111
inT16 MaxNumConfigs
Definition: protos.h:63
void ReadProtos ( register FILE *  File,
CLASS_TYPE  Class 
)

ReadProtos

Read in all the prototype information from a file. Read the number of lines requested.

Definition at line 367 of file protos.cpp.

367  {
368  register inT16 Pid;
369  register PROTO Proto;
370  int NumProtos;
371 
372  fscanf (File, "%d\n", &NumProtos);
373  Class->NumProtos = NumProtos;
374  Class->MaxNumProtos = NumProtos;
375  Class->Prototypes = (PROTO) Emalloc (sizeof (PROTO_STRUCT) * NumProtos);
376 
377  for (Pid = 0; Pid < NumProtos; Pid++) {
378  Proto = ProtoIn (Class, Pid);
379  fscanf (File, "%f %f %f %f %f %f %f\n",
380  &Proto->X,
381  &Proto->Y,
382  &Proto->Length,
383  &Proto->Angle,
384  &Proto->A,
385  &Proto->B, &Proto->C);
386  }
387 }
FLOAT32 B
Definition: protos.h:45
FLOAT32 Length
Definition: protos.h:50
FLOAT32 Y
Definition: protos.h:48
inT16 NumProtos
Definition: protos.h:59
#define ProtoIn(Class, Pid)
Definition: protos.h:123
FLOAT32 A
Definition: protos.h:44
FLOAT32 Angle
Definition: protos.h:49
void * Emalloc(size_t Size)
Definition: emalloc.cpp:35
FLOAT32 X
Definition: protos.h:47
PROTO_STRUCT * PROTO
Definition: protos.h:52
short inT16
Definition: host.h:100
inT16 MaxNumProtos
Definition: protos.h:60
PROTO Prototypes
Definition: protos.h:61
FLOAT32 C
Definition: protos.h:46
int SplitProto ( CLASS_TYPE  Class,
int  OldPid 
)

Definition at line 399 of file protos.cpp.

399  {
400  int i;
401  int NewPid;
403 
404  NewPid = AddProtoToClass (Class);
405 
406  for (i = 0; i < Class->NumConfigs; i++) {
407  Config = Class->Configurations[i];
408  if (test_bit (Config, OldPid))
409  SET_BIT(Config, NewPid);
410  }
411  return (NewPid);
412 }
CONFIGS Configurations
Definition: protos.h:64
#define test_bit(array, bit)
Definition: bitvec.h:61
CLUSTERCONFIG Config
inT16 NumConfigs
Definition: protos.h:62
int AddProtoToClass(CLASS_TYPE Class)
Definition: protos.cpp:99
uinT32 * BIT_VECTOR
Definition: bitvec.h:28
#define SET_BIT(array, bit)
Definition: bitvec.h:57
void WriteOldConfigFile ( FILE *  File,
CLASS_TYPE  Class 
)

Definition at line 425 of file protos.cpp.

425  {
426  int Cid, Pid;
428 
429  fprintf (File, "%d %d\n", Class->NumConfigs, Class->NumProtos);
430 
431  for (Cid = 0; Cid < Class->NumConfigs; Cid++) {
432  fprintf (File, "1 ");
433 
434  Config = Class->Configurations[Cid];
435 
436  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
437  if (test_bit (Config, Pid))
438  fprintf (File, "1");
439  else
440  fprintf (File, "0");
441  }
442  fprintf (File, "\n");
443  }
444 }
inT16 NumProtos
Definition: protos.h:59
CONFIGS Configurations
Definition: protos.h:64
#define test_bit(array, bit)
Definition: bitvec.h:61
CLUSTERCONFIG Config
inT16 NumConfigs
Definition: protos.h:62
uinT32 * BIT_VECTOR
Definition: bitvec.h:28
void WriteOldProtoFile ( FILE *  File,
CLASS_TYPE  Class 
)

Definition at line 457 of file protos.cpp.

457  {
458  int Pid;
459  PROTO Proto;
460 
461  /* print old header */
462  fprintf (File, "6\n");
463  fprintf (File, "linear essential -0.500000 0.500000\n");
464  fprintf (File, "linear essential -0.250000 0.750000\n");
465  fprintf (File, "linear essential 0.000000 1.000000\n");
466  fprintf (File, "circular essential 0.000000 1.000000\n");
467  fprintf (File, "linear non-essential -0.500000 0.500000\n");
468  fprintf (File, "linear non-essential -0.500000 0.500000\n");
469 
470  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
471  Proto = ProtoIn (Class, Pid);
472 
473  fprintf (File, "significant elliptical 1\n");
474  fprintf (File, " %9.6f %9.6f %9.6f %9.6f %9.6f %9.6f\n",
475  Proto->X, Proto->Y,
476  Proto->Length, Proto->Angle, 0.0, 0.0);
477  fprintf (File, " %9.6f %9.6f %9.6f %9.6f %9.6f %9.6f\n",
478  0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001);
479  }
480 }
FLOAT32 Length
Definition: protos.h:50
FLOAT32 Y
Definition: protos.h:48
inT16 NumProtos
Definition: protos.h:59
#define ProtoIn(Class, Pid)
Definition: protos.h:123
FLOAT32 Angle
Definition: protos.h:49
FLOAT32 X
Definition: protos.h:47

Variable Documentation

char* classify_training_file = "MicroFeatures"

"Training file"

Definition at line 50 of file protos.cpp.

Definition at line 48 of file protos.cpp.