1
1
/* *****************************************************************************
2
- ** Filename: clustertool.c
2
+ ** Filename: clusttool.cpp
3
3
** Purpose: Misc. tools for use with the clustering routines
4
4
** Author: Dan Johnson
5
5
**
17
17
18
18
// --------------------------Include Files----------------------------------
19
19
#include " clusttool.h"
20
- #include " emalloc.h"
21
- #include < cstdio>
22
20
#include < cmath>
21
+ #include " emalloc.h"
23
22
24
23
using tesseract::TFile;
25
24
@@ -28,6 +27,87 @@ using tesseract::TFile;
28
27
#define QUOTED_TOKENSIZE " 79"
29
28
#define MAXSAMPLESIZE 65535 // /< max num of dimensions in feature space
30
29
30
+ /* *
31
+ * This routine reads N floats from the specified text file
32
+ * and places them into Buffer. If Buffer is nullptr, a buffer
33
+ * is created and passed back to the caller. If EOF is
34
+ * encountered before any floats can be read, nullptr is
35
+ * returned.
36
+ * @param fp open text file to read floats from
37
+ * @param N number of floats to read
38
+ * @param Buffer pointer to buffer to place floats into
39
+ * @return Pointer to buffer holding floats or nullptr if EOF
40
+ * @note Globals: None
41
+ */
42
+ static float *ReadNFloats (TFile *fp, uint16_t N, float Buffer[]) {
43
+ const int kMaxLineSize = 1024 ;
44
+ char line[kMaxLineSize ];
45
+ if (fp->FGets (line, kMaxLineSize ) == nullptr ) {
46
+ tprintf (" Hit EOF in ReadNFloats!\n " );
47
+ return nullptr ;
48
+ }
49
+ bool needs_free = false ;
50
+
51
+ if (Buffer == nullptr ) {
52
+ Buffer = static_cast <float *>(Emalloc (N * sizeof (float )));
53
+ needs_free = true ;
54
+ }
55
+
56
+ char *startptr = line;
57
+ for (int i = 0 ; i < N; i++) {
58
+ char *endptr;
59
+ Buffer[i] = strtof (startptr, &endptr);
60
+ if (endptr == startptr) {
61
+ tprintf (" Read of %d floats failed!\n " , N);
62
+ if (needs_free) Efree (Buffer);
63
+ return nullptr ;
64
+ }
65
+ startptr = endptr;
66
+ }
67
+ return Buffer;
68
+ }
69
+
70
+ /* *
71
+ * This routine writes a text representation of N floats from
72
+ * an array to a file. All of the floats are placed on one line.
73
+ * @param File open text file to write N floats to
74
+ * @param N number of floats to write
75
+ * @param Array array of floats to write
76
+ * @return None
77
+ * @note Globals: None
78
+ */
79
+ static void WriteNFloats (FILE * File, uint16_t N, float Array[]) {
80
+ for (int i = 0 ; i < N; i++)
81
+ fprintf (File, " %9.6f" , Array[i]);
82
+ fprintf (File, " \n " );
83
+ }
84
+
85
+ /* *
86
+ * This routine writes to the specified text file a word
87
+ * which represents the ProtoStyle. It does not append
88
+ * a carriage return to the end.
89
+ * @param File open text file to write prototype style to
90
+ * @param ProtoStyle prototype style to write
91
+ * @return None
92
+ * @note Globals: None
93
+ */
94
+ static void WriteProtoStyle (FILE *File, PROTOSTYLE ProtoStyle) {
95
+ switch (ProtoStyle) {
96
+ case spherical:
97
+ fprintf (File, " spherical" );
98
+ break ;
99
+ case elliptical:
100
+ fprintf (File, " elliptical" );
101
+ break ;
102
+ case mixed:
103
+ fprintf (File, " mixed" );
104
+ break ;
105
+ case automatic:
106
+ fprintf (File, " automatic" );
107
+ break ;
108
+ }
109
+ }
110
+
31
111
/* *
32
112
* This routine reads a single integer from the specified
33
113
* file and checks to ensure that it is between 0 and
@@ -159,46 +239,6 @@ PROTOTYPE *ReadPrototype(TFile *fp, uint16_t N) {
159
239
return Proto;
160
240
}
161
241
162
- /* *
163
- * This routine reads N floats from the specified text file
164
- * and places them into Buffer. If Buffer is nullptr, a buffer
165
- * is created and passed back to the caller. If EOF is
166
- * encountered before any floats can be read, nullptr is
167
- * returned.
168
- * @param fp open text file to read floats from
169
- * @param N number of floats to read
170
- * @param Buffer pointer to buffer to place floats into
171
- * @return Pointer to buffer holding floats or nullptr if EOF
172
- * @note Globals: None
173
- */
174
- float *ReadNFloats (TFile *fp, uint16_t N, float Buffer[]) {
175
- const int kMaxLineSize = 1024 ;
176
- char line[kMaxLineSize ];
177
- if (fp->FGets (line, kMaxLineSize ) == nullptr ) {
178
- tprintf (" Hit EOF in ReadNFloats!\n " );
179
- return nullptr ;
180
- }
181
- bool needs_free = false ;
182
-
183
- if (Buffer == nullptr ) {
184
- Buffer = static_cast <float *>(Emalloc (N * sizeof (float )));
185
- needs_free = true ;
186
- }
187
-
188
- char *startptr = line;
189
- for (int i = 0 ; i < N; i++) {
190
- char *endptr;
191
- Buffer[i] = strtof (startptr, &endptr);
192
- if (endptr == startptr) {
193
- tprintf (" Read of %d floats failed!\n " , N);
194
- if (needs_free) Efree (Buffer);
195
- return nullptr ;
196
- }
197
- startptr = endptr;
198
- }
199
- return Buffer;
200
- }
201
-
202
242
/* *
203
243
* This routine writes an array of dimension descriptors to
204
244
* the specified text file.
@@ -273,78 +313,3 @@ void WritePrototype(FILE *File, uint16_t N, PROTOTYPE *Proto) {
273
313
WriteNFloats (File, N, Proto->Variance .Elliptical );
274
314
}
275
315
}
276
-
277
- /* *
278
- * This routine writes a text representation of N floats from
279
- * an array to a file. All of the floats are placed on one line.
280
- * @param File open text file to write N floats to
281
- * @param N number of floats to write
282
- * @param Array array of floats to write
283
- * @return None
284
- * @note Globals: None
285
- */
286
- void WriteNFloats (FILE * File, uint16_t N, float Array[]) {
287
- for (int i = 0 ; i < N; i++)
288
- fprintf (File, " %9.6f" , Array[i]);
289
- fprintf (File, " \n " );
290
- }
291
-
292
- /* *
293
- * This routine writes to the specified text file a word
294
- * which represents the ProtoStyle. It does not append
295
- * a carriage return to the end.
296
- * @param File open text file to write prototype style to
297
- * @param ProtoStyle prototype style to write
298
- * @return None
299
- * @note Globals: None
300
- */
301
- void WriteProtoStyle (FILE *File, PROTOSTYLE ProtoStyle) {
302
- switch (ProtoStyle) {
303
- case spherical:
304
- fprintf (File, " spherical" );
305
- break ;
306
- case elliptical:
307
- fprintf (File, " elliptical" );
308
- break ;
309
- case mixed:
310
- fprintf (File, " mixed" );
311
- break ;
312
- case automatic:
313
- fprintf (File, " automatic" );
314
- break ;
315
- }
316
- }
317
-
318
- /* *
319
- * This routine writes a textual description of each prototype
320
- * in the prototype list to the specified file. It also
321
- * writes a file header which includes the number of dimensions
322
- * in feature space and the descriptions for each dimension.
323
- * @param File open text file to write prototypes to
324
- * @param N number of dimensions in feature space
325
- * @param ParamDesc descriptions for each dimension
326
- * @param ProtoList list of prototypes to be written
327
- * @param WriteSigProtos true to write out significant prototypes
328
- * @param WriteInsigProtos true to write out insignificants
329
- * @note Globals: None
330
- * @return None
331
- */
332
-
333
- void WriteProtoList (FILE* File, uint16_t N, PARAM_DESC* ParamDesc,
334
- LIST ProtoList, bool WriteSigProtos,
335
- bool WriteInsigProtos) {
336
- PROTOTYPE *Proto;
337
-
338
- /* write file header */
339
- fprintf (File," %0d\n " ,N);
340
- WriteParamDesc (File,N,ParamDesc);
341
-
342
- /* write prototypes */
343
- iterate(ProtoList)
344
- {
345
- Proto = reinterpret_cast <PROTOTYPE *>first_node (ProtoList);
346
- if ((Proto->Significant && WriteSigProtos) ||
347
- (!Proto->Significant && WriteInsigProtos))
348
- WritePrototype (File, N, Proto);
349
- }
350
- }
0 commit comments