18 #include "config_auto.h"
24 #include "allheaders.h"
41 const double TrainingSample::kScaleValues[kSampleScaleSize] = {
47 delete [] micro_features_;
55 if (fwrite(&class_id_,
sizeof(class_id_), 1, fp) != 1)
return false;
56 if (fwrite(&font_id_,
sizeof(font_id_), 1, fp) != 1)
return false;
57 if (fwrite(&page_num_,
sizeof(page_num_), 1, fp) != 1)
return false;
58 if (!bounding_box_.
Serialize(fp))
return false;
59 if (fwrite(&num_features_,
sizeof(num_features_), 1, fp) != 1)
return false;
60 if (fwrite(&num_micro_features_,
sizeof(num_micro_features_), 1, fp) != 1)
62 if (fwrite(features_,
sizeof(*features_), num_features_, fp) != num_features_)
64 if (fwrite(micro_features_,
sizeof(*micro_features_), num_micro_features_,
65 fp) != num_micro_features_)
67 if (fwrite(cn_feature_,
sizeof(*cn_feature_), kNumCNParams, fp) !=
68 kNumCNParams)
return false;
86 if (fread(&class_id_,
sizeof(class_id_), 1, fp) != 1)
return false;
87 if (fread(&font_id_,
sizeof(font_id_), 1, fp) != 1)
return false;
88 if (fread(&page_num_,
sizeof(page_num_), 1, fp) != 1)
return false;
89 if (!bounding_box_.
DeSerialize(swap, fp))
return false;
90 if (fread(&num_features_,
sizeof(num_features_), 1, fp) != 1)
return false;
91 if (fread(&num_micro_features_,
sizeof(num_micro_features_), 1, fp) != 1)
94 ReverseN(&class_id_,
sizeof(class_id_));
95 ReverseN(&num_features_,
sizeof(num_features_));
96 ReverseN(&num_micro_features_,
sizeof(num_micro_features_));
100 if (fread(features_,
sizeof(*features_), num_features_, fp) != num_features_)
102 delete [] micro_features_;
103 micro_features_ =
new MicroFeature[num_micro_features_];
104 if (fread(micro_features_,
sizeof(*micro_features_), num_micro_features_,
105 fp) != num_micro_features_)
107 if (fread(cn_feature_,
sizeof(*cn_feature_), kNumCNParams, fp) !=
108 kNumCNParams)
return false;
121 memcpy(sample->features_, features, num_features *
sizeof(features[0]));
125 sample->features_are_indexed_ =
false;
126 sample->features_are_mapped_ =
false;
135 if (index >= 0 && index < kSampleRandomSize) {
137 int yshift = kYShiftValues[index / kSampleScaleSize];
138 double scaling = kScaleValues[index % kSampleScaleSize];
139 for (
int i = 0; i < num_features_; ++i) {
142 sample->features_[i].
X =
ClipToRange(static_cast<int>(result + 0.5), 0,
146 sample->features_[i].
Y =
ClipToRange(static_cast<int>(result + 0.5), 0,
156 sample->class_id_ = class_id_;
157 sample->font_id_ = font_id_;
158 sample->weight_ = weight_;
159 sample->sample_index_ = sample_index_;
160 sample->num_features_ = num_features_;
161 if (num_features_ > 0) {
163 memcpy(sample->features_, features_, num_features_ *
sizeof(features_[0]));
165 sample->num_micro_features_ = num_micro_features_;
166 if (num_micro_features_ > 0) {
167 sample->micro_features_ =
new MicroFeature[num_micro_features_];
168 memcpy(sample->micro_features_, micro_features_,
169 num_micro_features_ *
sizeof(micro_features_[0]));
171 memcpy(sample->cn_feature_, cn_feature_,
sizeof(*cn_feature_) * kNumCNParams);
172 memcpy(sample->geo_feature_, geo_feature_,
sizeof(*geo_feature_) *
GeoCount);
183 if (features_ !=
NULL)
delete [] features_;
185 if (char_features ==
NULL) {
186 tprintf(
"Error: no features to train on of type %s\n",
193 for (
int f = 0;
f < num_features_; ++
f) {
204 if (micro_features_ !=
NULL)
delete [] micro_features_;
205 char_features = char_desc->
FeatureSets[micro_type];
206 if (char_features ==
NULL) {
207 tprintf(
"Error: no features to train on of type %s\n",
209 num_micro_features_ = 0;
210 micro_features_ =
NULL;
213 micro_features_ =
new MicroFeature[num_micro_features_];
214 for (
int f = 0;
f < num_micro_features_; ++
f) {
215 for (
int d = 0; d <
MFCount; ++d) {
222 if (char_features ==
NULL) {
223 tprintf(
"Error: no CN feature to train on.\n");
234 if (char_features ==
NULL) {
235 tprintf(
"Error: no Geo feature to train on.\n");
242 features_are_indexed_ =
false;
243 features_are_mapped_ =
false;
252 features_are_indexed_ =
true;
253 features_are_mapped_ =
false;
263 features_are_indexed_ =
false;
264 features_are_mapped_ =
true;
270 for (
int f = 0;
f < num_features_; ++
f) {
271 int start_x = features_[
f].
X;
273 double dx = cos((features_[
f].Theta / 256.0) * 2.0 *
PI -
PI);
274 double dy = -sin((features_[
f].Theta / 256.0) * 2.0 *
PI -
PI);
275 for (
int i = 0; i <= 5; ++i) {
276 int x =
static_cast<int>(start_x + dx * i);
277 int y =
static_cast<int>(start_y + dy * i);
278 if (x >= 0 && x < 256 && y >= 0 && y < 256)
279 pixSetPixel(pix, x, y, 1);
282 if (unicharset !=
NULL)
290 #ifndef GRAPHICS_DISABLED
291 for (
int f = 0;
f < num_features_; ++
f) {
294 #endif // GRAPHICS_DISABLED
302 if (page_pix ==
NULL)
304 int page_width = pixGetWidth(page_pix);
305 int page_height = pixGetHeight(page_pix);
307 padded_box.
pad(padding, padding);
309 TBOX page_box(0, 0, page_width, page_height);
310 padded_box &= page_box;
311 Box* box = boxCreate(page_box.
left(), page_height - page_box.
top(),
313 Pix* sample_pix = pixClipRectangle(page_pix, box,
NULL);