tesseract  3.04.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
clusttool.h File Reference
#include "host.h"
#include "cluster.h"
#include <stdio.h>

Go to the source code of this file.

Macros

#define ILLEGALSAMPLESIZE   5000
 
#define ILLEGALCIRCULARSPEC   5001
 
#define ILLEGALMINMAXSPEC   5002
 
#define ILLEGALSIGNIFICANCESPEC   5003
 
#define ILLEGALSTYLESPEC   5004
 
#define ILLEGALSAMPLECOUNT   5005
 
#define ILLEGALMEANSPEC   5006
 
#define ILLEGALVARIANCESPEC   5007
 
#define ILLEGALDISTRIBUTION   5008
 
#define ILLEGALFLOAT   5009
 
#define ILLEGALESSENTIALSPEC   5013
 

Functions

uinT16 ReadSampleSize (FILE *File)
 
PARAM_DESCReadParamDesc (FILE *File, uinT16 N)
 
PROTOTYPEReadPrototype (FILE *File, uinT16 N)
 
PROTOSTYLE ReadProtoStyle (FILE *File)
 
FLOAT32ReadNFloats (FILE *File, uinT16 N, FLOAT32 Buffer[])
 
void WriteParamDesc (FILE *File, uinT16 N, PARAM_DESC ParamDesc[])
 
void WritePrototype (FILE *File, uinT16 N, PROTOTYPE *Proto)
 
void WriteNFloats (FILE *File, uinT16 N, FLOAT32 Array[])
 
void WriteProtoStyle (FILE *File, PROTOSTYLE ProtoStyle)
 
void WriteProtoList (FILE *File, uinT16 N, PARAM_DESC ParamDesc[], LIST ProtoList, BOOL8 WriteSigProtos, BOOL8 WriteInsigProtos)
 

Macro Definition Documentation

#define ILLEGALCIRCULARSPEC   5001

Definition at line 58 of file clusttool.h.

#define ILLEGALDISTRIBUTION   5008

Definition at line 65 of file clusttool.h.

#define ILLEGALESSENTIALSPEC   5013

Definition at line 67 of file clusttool.h.

#define ILLEGALFLOAT   5009

Definition at line 66 of file clusttool.h.

#define ILLEGALMEANSPEC   5006

Definition at line 63 of file clusttool.h.

#define ILLEGALMINMAXSPEC   5002

Definition at line 59 of file clusttool.h.

#define ILLEGALSAMPLECOUNT   5005

Definition at line 62 of file clusttool.h.

#define ILLEGALSAMPLESIZE   5000

Definition at line 57 of file clusttool.h.

#define ILLEGALSIGNIFICANCESPEC   5003

Definition at line 60 of file clusttool.h.

#define ILLEGALSTYLESPEC   5004

Definition at line 61 of file clusttool.h.

#define ILLEGALVARIANCESPEC   5007

Definition at line 64 of file clusttool.h.

Function Documentation

FLOAT32* ReadNFloats ( FILE *  File,
uinT16  N,
FLOAT32  Buffer[] 
)

This routine reads N floats from the specified text file and places them into Buffer. If Buffer is NULL, a buffer is created and passed back to the caller. If EOF is encountered before any floats can be read, NULL is returned.

Parameters
Fileopen text file to read floats from
Nnumber of floats to read
Bufferpointer to buffer to place floats into
Returns
Pointer to buffer holding floats or NULL if EOF
Note
Globals: None
Exceptions: ILLEGALFLOAT
History: 6/6/89, DSJ, Created.

Definition at line 281 of file clusttool.cpp.

281  {
282  bool needs_free = false;
283  int i;
284  int NumFloatsRead;
285 
286  if (Buffer == NULL) {
287  Buffer = reinterpret_cast<FLOAT32*>(Emalloc(N * sizeof(FLOAT32)));
288  needs_free = true;
289  }
290 
291  for (i = 0; i < N; i++) {
292  NumFloatsRead = tfscanf(File, "%f", &(Buffer[i]));
293  if (NumFloatsRead != 1) {
294  if ((NumFloatsRead == EOF) && (i == 0)) {
295  if (needs_free) {
296  Efree(Buffer);
297  }
298  return NULL;
299  } else {
300  DoError(ILLEGALFLOAT, "Illegal float specification");
301  }
302  }
303  }
304  return Buffer;
305 }
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
void * Emalloc(int Size)
Definition: emalloc.cpp:47
#define ILLEGALFLOAT
Definition: clusttool.h:66
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:233
void Efree(void *ptr)
Definition: emalloc.cpp:79
float FLOAT32
Definition: host.h:111
PARAM_DESC* ReadParamDesc ( FILE *  File,
uinT16  N 
)

This routine reads textual descriptions of sets of parameters which describe the characteristics of feature dimensions.

Exceptions:

  • ILLEGALCIRCULARSPEC
  • ILLEGALESSENTIALSPEC
  • ILLEGALMINMAXSPEC
    Parameters
    Fileopen text file to read N parameter descriptions from
    Nnumber of parameter descriptions to read
    Returns
    Pointer to an array of parameter descriptors.
    Note
    Globals: None
    History: 6/6/89, DSJ, Created.

Definition at line 66 of file clusttool.cpp.

66  {
67  int i;
68  PARAM_DESC *ParamDesc;
69  char Token[TOKENSIZE];
70 
71  ParamDesc = (PARAM_DESC *) Emalloc (N * sizeof (PARAM_DESC));
72  for (i = 0; i < N; i++) {
73  if (tfscanf(File, "%s", Token) != 1)
75  "Illegal circular/linear specification");
76  if (Token[0] == 'c')
77  ParamDesc[i].Circular = TRUE;
78  else
79  ParamDesc[i].Circular = FALSE;
80 
81  if (tfscanf(File, "%s", Token) != 1)
83  "Illegal essential/non-essential spec");
84  if (Token[0] == 'e')
85  ParamDesc[i].NonEssential = FALSE;
86  else
87  ParamDesc[i].NonEssential = TRUE;
88  if (tfscanf(File, "%f%f", &(ParamDesc[i].Min), &(ParamDesc[i].Max)) != 2)
89  DoError (ILLEGALMINMAXSPEC, "Illegal min or max specification");
90  ParamDesc[i].Range = ParamDesc[i].Max - ParamDesc[i].Min;
91  ParamDesc[i].HalfRange = ParamDesc[i].Range / 2;
92  ParamDesc[i].MidRange = (ParamDesc[i].Max + ParamDesc[i].Min) / 2;
93  }
94  return (ParamDesc);
95 }
FLOAT32 Min
Definition: ocrfeatures.h:49
#define FALSE
Definition: capi.h:29
FLOAT32 HalfRange
Definition: ocrfeatures.h:52
#define TOKENSIZE
Definition: clusttool.cpp:29
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
#define ILLEGALMINMAXSPEC
Definition: clusttool.h:59
FLOAT32 MidRange
Definition: ocrfeatures.h:53
#define ILLEGALESSENTIALSPEC
Definition: clusttool.h:67
inT8 NonEssential
Definition: ocrfeatures.h:48
inT8 Circular
Definition: ocrfeatures.h:47
void * Emalloc(int Size)
Definition: emalloc.cpp:47
#define TRUE
Definition: capi.h:28
FLOAT32 Range
Definition: ocrfeatures.h:51
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:233
FLOAT32 Max
Definition: ocrfeatures.h:50
#define ILLEGALCIRCULARSPEC
Definition: clusttool.h:58
PROTOSTYLE ReadProtoStyle ( FILE *  File)

This routine reads an single token from the specified text file and interprets it as a prototype specification.

Parameters
Fileopen text file to read prototype style from
Returns
Prototype style read from text file
Note
Globals: None
Exceptions: ILLEGALSTYLESPEC illegal prototype style specification
History: 6/8/89, DSJ, Created.

Definition at line 241 of file clusttool.cpp.

241  {
242  char Token[TOKENSIZE];
243  PROTOSTYLE Style;
244 
245  if (tfscanf(File, "%s", Token) != 1)
246  DoError (ILLEGALSTYLESPEC, "Illegal prototype style specification");
247  switch (Token[0]) {
248  case 's':
249  Style = spherical;
250  break;
251  case 'e':
252  Style = elliptical;
253  break;
254  case 'm':
255  Style = mixed;
256  break;
257  case 'a':
258  Style = automatic;
259  break;
260  default:
261  Style = elliptical;
262  DoError (ILLEGALSTYLESPEC, "Illegal prototype style specification");
263  }
264  return (Style);
265 }
#define ILLEGALSTYLESPEC
Definition: clusttool.h:61
#define TOKENSIZE
Definition: clusttool.cpp:29
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
Definition: cluster.h:45
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:233
PROTOSTYLE
Definition: cluster.h:44
PROTOTYPE* ReadPrototype ( FILE *  File,
uinT16  N 
)

This routine reads a textual description of a prototype from the specified file.

Exceptions:

  • ILLEGALSIGNIFICANCESPEC
  • ILLEGALSAMPLECOUNT
  • ILLEGALMEANSPEC
  • ILLEGALVARIANCESPEC
  • ILLEGALDISTRIBUTION
    Parameters
    Fileopen text file to read prototype from
    Nnumber of dimensions used in prototype
    Returns
    List of prototypes
    Note
    Globals: None
    History: 6/6/89, DSJ, Created.

Definition at line 113 of file clusttool.cpp.

113  {
114  char Token[TOKENSIZE];
115  int Status;
116  PROTOTYPE *Proto;
117  int SampleCount;
118  int i;
119 
120  if ((Status = tfscanf(File, "%s", Token)) == 1) {
121  Proto = (PROTOTYPE *) Emalloc (sizeof (PROTOTYPE));
122  Proto->Cluster = NULL;
123  if (Token[0] == 's')
124  Proto->Significant = TRUE;
125  else
126  Proto->Significant = FALSE;
127 
128  Proto->Style = ReadProtoStyle (File);
129 
130  if ((tfscanf(File, "%d", &SampleCount) != 1) || (SampleCount < 0))
131  DoError (ILLEGALSAMPLECOUNT, "Illegal sample count");
132  Proto->NumSamples = SampleCount;
133 
134  Proto->Mean = ReadNFloats (File, N, NULL);
135  if (Proto->Mean == NULL)
136  DoError (ILLEGALMEANSPEC, "Illegal prototype mean");
137 
138  switch (Proto->Style) {
139  case spherical:
140  if (ReadNFloats (File, 1, &(Proto->Variance.Spherical)) == NULL)
141  DoError (ILLEGALVARIANCESPEC, "Illegal prototype variance");
142  Proto->Magnitude.Spherical =
143  1.0 / sqrt ((double) (2.0 * PI * Proto->Variance.Spherical));
144  Proto->TotalMagnitude =
145  pow (Proto->Magnitude.Spherical, (float) N);
146  Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
147  Proto->Weight.Spherical = 1.0 / Proto->Variance.Spherical;
148  Proto->Distrib = NULL;
149  break;
150  case elliptical:
151  Proto->Variance.Elliptical = ReadNFloats (File, N, NULL);
152  if (Proto->Variance.Elliptical == NULL)
153  DoError (ILLEGALVARIANCESPEC, "Illegal prototype variance");
154  Proto->Magnitude.Elliptical =
155  (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
156  Proto->Weight.Elliptical =
157  (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
158  Proto->TotalMagnitude = 1.0;
159  for (i = 0; i < N; i++) {
160  Proto->Magnitude.Elliptical[i] =
161  1.0 /
162  sqrt ((double) (2.0 * PI * Proto->Variance.Elliptical[i]));
163  Proto->Weight.Elliptical[i] =
164  1.0 / Proto->Variance.Elliptical[i];
165  Proto->TotalMagnitude *= Proto->Magnitude.Elliptical[i];
166  }
167  Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
168  Proto->Distrib = NULL;
169  break;
170  case mixed:
171  Proto->Distrib =
172  (DISTRIBUTION *) Emalloc (N * sizeof (DISTRIBUTION));
173  for (i = 0; i < N; i++) {
174  if (tfscanf(File, "%s", Token) != 1)
176  "Illegal prototype distribution");
177  switch (Token[0]) {
178  case 'n':
179  Proto->Distrib[i] = normal;
180  break;
181  case 'u':
182  Proto->Distrib[i] = uniform;
183  break;
184  case 'r':
185  Proto->Distrib[i] = D_random;
186  break;
187  default:
189  "Illegal prototype distribution");
190  }
191  }
192  Proto->Variance.Elliptical = ReadNFloats (File, N, NULL);
193  if (Proto->Variance.Elliptical == NULL)
194  DoError (ILLEGALVARIANCESPEC, "Illegal prototype variance");
195  Proto->Magnitude.Elliptical =
196  (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
197  Proto->Weight.Elliptical =
198  (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
199  Proto->TotalMagnitude = 1.0;
200  for (i = 0; i < N; i++) {
201  switch (Proto->Distrib[i]) {
202  case normal:
203  Proto->Magnitude.Elliptical[i] = 1.0 /
204  sqrt ((double)
205  (2.0 * PI * Proto->Variance.Elliptical[i]));
206  Proto->Weight.Elliptical[i] =
207  1.0 / Proto->Variance.Elliptical[i];
208  break;
209  case uniform:
210  case D_random:
211  Proto->Magnitude.Elliptical[i] = 1.0 /
212  (2.0 * Proto->Variance.Elliptical[i]);
213  break;
214  case DISTRIBUTION_COUNT:
215  ASSERT_HOST(!"Distribution count not allowed!");
216  }
217  Proto->TotalMagnitude *= Proto->Magnitude.Elliptical[i];
218  }
219  Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
220  break;
221  }
222  return (Proto);
223  }
224  else if (Status == EOF)
225  return (NULL);
226  else {
227  DoError (ILLEGALSIGNIFICANCESPEC, "Illegal significance specification");
228  return (NULL);
229  }
230 }
#define PI
Definition: const.h:19
DISTRIBUTION * Distrib
Definition: cluster.h:77
#define ILLEGALMEANSPEC
Definition: clusttool.h:63
#define ILLEGALSIGNIFICANCESPEC
Definition: clusttool.h:60
FLOAT32 * ReadNFloats(FILE *File, uinT16 N, FLOAT32 Buffer[])
Definition: clusttool.cpp:281
FLOAT32 Spherical
Definition: cluster.h:63
DISTRIBUTION
Definition: cluster.h:58
FLOAT32 LogMagnitude
Definition: cluster.h:80
FLOATUNION Variance
Definition: cluster.h:81
FLOAT32 * Mean
Definition: cluster.h:78
Definition: cluster.h:59
#define FALSE
Definition: capi.h:29
#define TOKENSIZE
Definition: clusttool.cpp:29
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
unsigned Significant
Definition: cluster.h:68
FLOATUNION Weight
Definition: cluster.h:83
FLOAT32 TotalMagnitude
Definition: cluster.h:79
unsigned NumSamples
Definition: cluster.h:75
FLOATUNION Magnitude
Definition: cluster.h:82
FLOAT32 * Elliptical
Definition: cluster.h:64
CLUSTER * Cluster
Definition: cluster.h:76
#define ILLEGALVARIANCESPEC
Definition: clusttool.h:64
#define ILLEGALDISTRIBUTION
Definition: clusttool.h:65
#define ILLEGALSAMPLECOUNT
Definition: clusttool.h:62
void * Emalloc(int Size)
Definition: emalloc.cpp:47
Definition: cluster.h:45
#define TRUE
Definition: capi.h:28
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:233
unsigned Style
Definition: cluster.h:74
PROTOSTYLE ReadProtoStyle(FILE *File)
Definition: clusttool.cpp:241
float FLOAT32
Definition: host.h:111
#define ASSERT_HOST(x)
Definition: errcode.h:84
uinT16 ReadSampleSize ( FILE *  File)

This routine reads a single integer from the specified file and checks to ensure that it is between 0 and MAXSAMPLESIZE.

Parameters
Fileopen text file to read sample size from
Returns
Sample size
Note
Globals: None
Exceptions: ILLEGALSAMPLESIZE illegal format or range
History: 6/6/89, DSJ, Created.

Definition at line 43 of file clusttool.cpp.

43  {
44  int SampleSize;
45 
46  if ((tfscanf(File, "%d", &SampleSize) != 1) ||
47  (SampleSize < 0) || (SampleSize > MAXSAMPLESIZE))
48  DoError (ILLEGALSAMPLESIZE, "Illegal sample size");
49  return (SampleSize);
50 }
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
#define MAXSAMPLESIZE
Definition: clusttool.cpp:30
#define ILLEGALSAMPLESIZE
Definition: clusttool.h:57
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:233
void WriteNFloats ( FILE *  File,
uinT16  N,
FLOAT32  Array[] 
)

This routine writes a text representation of N floats from an array to a file. All of the floats are placed on one line.

Parameters
Fileopen text file to write N floats to
Nnumber of floats to write
Arrayarray of floats to write
Returns
None
Note
Globals: None
Exceptions: None
History: 6/6/89, DSJ, Created.

Definition at line 398 of file clusttool.cpp.

398  {
399  for (int i = 0; i < N; i++)
400  fprintf(File, " %9.6f", Array[i]);
401  fprintf(File, "\n");
402 }
void WriteParamDesc ( FILE *  File,
uinT16  N,
PARAM_DESC  ParamDesc[] 
)

This routine writes an array of dimension descriptors to the specified text file.

Parameters
Fileopen text file to write param descriptors to
Nnumber of param descriptors to write
ParamDescarray of param descriptors to write
Returns
None
Note
Globals: None
Exceptions: None
History: 6/6/89, DSJ, Created.

Definition at line 319 of file clusttool.cpp.

319  {
320  int i;
321 
322  for (i = 0; i < N; i++) {
323  if (ParamDesc[i].Circular)
324  fprintf (File, "circular ");
325  else
326  fprintf (File, "linear ");
327 
328  if (ParamDesc[i].NonEssential)
329  fprintf (File, "non-essential ");
330  else
331  fprintf (File, "essential ");
332 
333  fprintf (File, "%10.6f %10.6f\n", ParamDesc[i].Min, ParamDesc[i].Max);
334  }
335 }
void WriteProtoList ( FILE *  File,
uinT16  N,
PARAM_DESC  ParamDesc[],
LIST  ProtoList,
BOOL8  WriteSigProtos,
BOOL8  WriteInsigProtos 
)

This routine writes a textual description of each prototype in the prototype list to the specified file. It also writes a file header which includes the number of dimensions in feature space and the descriptions for each dimension.

Parameters
Fileopen text file to write prototypes to
Nnumber of dimensions in feature space
ParamDescdescriptions for each dimension
ProtoListlist of prototypes to be written
WriteSigProtosTRUE to write out significant prototypes
WriteInsigProtosTRUE to write out insignificants
Note
Globals: None
Returns
None
Note
Exceptions: None
History: 6/12/89, DSJ, Created.

Definition at line 449 of file clusttool.cpp.

456 {
457  PROTOTYPE *Proto;
458 
459  /* write file header */
460  fprintf(File,"%0d\n",N);
461  WriteParamDesc(File,N,ParamDesc);
462 
463  /* write prototypes */
464  iterate(ProtoList)
465  {
466  Proto = (PROTOTYPE *) first_node ( ProtoList );
467  if (( Proto->Significant && WriteSigProtos ) ||
468  ( ! Proto->Significant && WriteInsigProtos ) )
469  WritePrototype( File, N, Proto );
470  }
471 }
void WritePrototype(FILE *File, uinT16 N, PROTOTYPE *Proto)
Definition: clusttool.cpp:348
#define first_node(l)
Definition: oldlist.h:139
#define iterate(l)
Definition: oldlist.h:159
unsigned Significant
Definition: cluster.h:68
void WriteParamDesc(FILE *File, uinT16 N, PARAM_DESC ParamDesc[])
Definition: clusttool.cpp:319
void WriteProtoStyle ( FILE *  File,
PROTOSTYLE  ProtoStyle 
)

This routine writes to the specified text file a word which represents the ProtoStyle. It does not append a carriage return to the end.

Parameters
Fileopen text file to write prototype style to
ProtoStyleprototype style to write
Returns
None
Note
Globals: None
Exceptions: None
History: 6/8/89, DSJ, Created.

Definition at line 415 of file clusttool.cpp.

415  {
416  switch (ProtoStyle) {
417  case spherical:
418  fprintf (File, "spherical");
419  break;
420  case elliptical:
421  fprintf (File, "elliptical");
422  break;
423  case mixed:
424  fprintf (File, "mixed");
425  break;
426  case automatic:
427  fprintf (File, "automatic");
428  break;
429  }
430 }
Definition: cluster.h:45
void WritePrototype ( FILE *  File,
uinT16  N,
PROTOTYPE Proto 
)

This routine writes a textual description of a prototype to the specified text file.

Parameters
Fileopen text file to write prototype to
Nnumber of dimensions in feature space
Protoprototype to write out
Returns
None
Note
Globals: None
Exceptions: None
History: 6/12/89, DSJ, Created.

Definition at line 348 of file clusttool.cpp.

348  {
349  int i;
350 
351  if (Proto->Significant)
352  fprintf (File, "significant ");
353  else
354  fprintf (File, "insignificant ");
355  WriteProtoStyle (File, (PROTOSTYLE) Proto->Style);
356  fprintf (File, "%6d\n\t", Proto->NumSamples);
357  WriteNFloats (File, N, Proto->Mean);
358  fprintf (File, "\t");
359 
360  switch (Proto->Style) {
361  case spherical:
362  WriteNFloats (File, 1, &(Proto->Variance.Spherical));
363  break;
364  case elliptical:
365  WriteNFloats (File, N, Proto->Variance.Elliptical);
366  break;
367  case mixed:
368  for (i = 0; i < N; i++)
369  switch (Proto->Distrib[i]) {
370  case normal:
371  fprintf (File, " %9s", "normal");
372  break;
373  case uniform:
374  fprintf (File, " %9s", "uniform");
375  break;
376  case D_random:
377  fprintf (File, " %9s", "random");
378  break;
379  case DISTRIBUTION_COUNT:
380  ASSERT_HOST(!"Distribution count not allowed!");
381  }
382  fprintf (File, "\n\t");
383  WriteNFloats (File, N, Proto->Variance.Elliptical);
384  }
385 }
DISTRIBUTION * Distrib
Definition: cluster.h:77
FLOAT32 Spherical
Definition: cluster.h:63
FLOATUNION Variance
Definition: cluster.h:81
FLOAT32 * Mean
Definition: cluster.h:78
Definition: cluster.h:59
unsigned Significant
Definition: cluster.h:68
unsigned NumSamples
Definition: cluster.h:75
FLOAT32 * Elliptical
Definition: cluster.h:64
void WriteNFloats(FILE *File, uinT16 N, FLOAT32 Array[])
Definition: clusttool.cpp:398
Definition: cluster.h:45
unsigned Style
Definition: cluster.h:74
PROTOSTYLE
Definition: cluster.h:44
#define ASSERT_HOST(x)
Definition: errcode.h:84
void WriteProtoStyle(FILE *File, PROTOSTYLE ProtoStyle)
Definition: clusttool.cpp:415