16 #define _USE_MATH_DEFINES // for M_PI
19 #include "config_auto.h"
25 #include "allheaders.h"
42 const double TrainingSample::kScaleValues[kSampleScaleSize] = {
48 delete [] micro_features_;
56 if (fwrite(&class_id_,
sizeof(class_id_), 1, fp) != 1)
return false;
57 if (fwrite(&font_id_,
sizeof(font_id_), 1, fp) != 1)
return false;
58 if (fwrite(&page_num_,
sizeof(page_num_), 1, fp) != 1)
return false;
59 if (!bounding_box_.
Serialize(fp))
return false;
60 if (fwrite(&num_features_,
sizeof(num_features_), 1, fp) != 1)
return false;
61 if (fwrite(&num_micro_features_,
sizeof(num_micro_features_), 1, fp) != 1)
63 if (fwrite(&outline_length_,
sizeof(outline_length_), 1, fp) != 1)
65 if (fwrite(features_,
sizeof(*features_), num_features_, fp) != num_features_)
67 if (fwrite(micro_features_,
sizeof(*micro_features_), num_micro_features_,
68 fp) != num_micro_features_)
70 if (fwrite(cn_feature_,
sizeof(*cn_feature_), kNumCNParams, fp) !=
71 kNumCNParams)
return false;
89 if (fread(&class_id_,
sizeof(class_id_), 1, fp) != 1)
return false;
90 if (fread(&font_id_,
sizeof(font_id_), 1, fp) != 1)
return false;
91 if (fread(&page_num_,
sizeof(page_num_), 1, fp) != 1)
return false;
92 if (!bounding_box_.
DeSerialize(swap, fp))
return false;
93 if (fread(&num_features_,
sizeof(num_features_), 1, fp) != 1)
return false;
94 if (fread(&num_micro_features_,
sizeof(num_micro_features_), 1, fp) != 1)
96 if (fread(&outline_length_,
sizeof(outline_length_), 1, fp) != 1)
99 ReverseN(&class_id_,
sizeof(class_id_));
100 ReverseN(&num_features_,
sizeof(num_features_));
101 ReverseN(&num_micro_features_,
sizeof(num_micro_features_));
102 ReverseN(&outline_length_,
sizeof(outline_length_));
105 if (num_features_ > UINT16_MAX)
return false;
106 if (num_micro_features_ > UINT16_MAX)
return false;
109 if (fread(features_,
sizeof(*features_), num_features_, fp)
112 delete [] micro_features_;
113 micro_features_ =
new MicroFeature[num_micro_features_];
114 if (fread(micro_features_,
sizeof(*micro_features_), num_micro_features_,
115 fp) != num_micro_features_)
117 if (fread(cn_feature_,
sizeof(*cn_feature_), kNumCNParams, fp) !=
118 kNumCNParams)
return false;
127 const TBOX& bounding_box,
147 sample->features_are_indexed_ =
false;
148 sample->features_are_mapped_ =
false;
155 for (
int i = 0; i < kNumCNParams; ++i)
156 feature->
Params[i] = cn_feature_[i];
165 if (index >= 0 && index < kSampleRandomSize) {
167 const int yshift = kYShiftValues[index / kSampleScaleSize];
168 double scaling = kScaleValues[index % kSampleScaleSize];
169 for (uint32_t i = 0; i < num_features_; ++i) {
172 sample->features_[i].X = ClipToRange<int>(result + 0.5, 0, UINT8_MAX);
175 sample->features_[i].Y = ClipToRange<int>(result + 0.5, 0, UINT8_MAX);
184 sample->class_id_ = class_id_;
185 sample->font_id_ = font_id_;
186 sample->weight_ = weight_;
187 sample->sample_index_ = sample_index_;
188 sample->num_features_ = num_features_;
189 if (num_features_ > 0) {
191 memcpy(
sample->features_, features_, num_features_ *
sizeof(features_[0]));
193 sample->num_micro_features_ = num_micro_features_;
194 if (num_micro_features_ > 0) {
196 memcpy(
sample->micro_features_, micro_features_,
197 num_micro_features_ *
sizeof(micro_features_[0]));
199 memcpy(
sample->cn_feature_, cn_feature_,
sizeof(*cn_feature_) * kNumCNParams);
200 memcpy(
sample->geo_feature_, geo_feature_,
sizeof(*geo_feature_) *
GeoCount);
213 if (char_features ==
nullptr) {
214 tprintf(
"Error: no features to train on of type %s\n",
221 for (uint32_t f = 0; f < num_features_; ++f) {
232 delete[] micro_features_;
233 char_features = char_desc->
FeatureSets[micro_type];
234 if (char_features ==
nullptr) {
235 tprintf(
"Error: no features to train on of type %s\n",
237 num_micro_features_ = 0;
238 micro_features_ =
nullptr;
241 micro_features_ =
new MicroFeature[num_micro_features_];
242 for (uint32_t f = 0; f < num_micro_features_; ++f) {
243 for (
int d = 0; d <
MFCount; ++d) {
250 if (char_features ==
nullptr) {
251 tprintf(
"Error: no CN feature to train on.\n");
262 if (char_features ==
nullptr) {
263 tprintf(
"Error: no Geo feature to train on.\n");
270 features_are_indexed_ =
false;
271 features_are_mapped_ =
false;
280 features_are_indexed_ =
true;
281 features_are_mapped_ =
false;
291 features_are_indexed_ =
false;
292 features_are_mapped_ =
true;
298 for (uint32_t f = 0; f < num_features_; ++f) {
299 int start_x = features_[f].
X;
301 double dx = cos((features_[f].Theta / 256.0) * 2.0 * M_PI - M_PI);
302 double dy = -sin((features_[f].Theta / 256.0) * 2.0 * M_PI - M_PI);
303 for (
int i = 0; i <= 5; ++i) {
304 int x = static_cast<int>(start_x + dx * i);
305 int y = static_cast<int>(start_y + dy * i);
306 if (x >= 0 && x < 256 && y >= 0 && y < 256)
307 pixSetPixel(pix, x, y, 1);
310 if (unicharset !=
nullptr)
318 #ifndef GRAPHICS_DISABLED
319 for (uint32_t f = 0; f < num_features_; ++f) {
322 #endif // GRAPHICS_DISABLED
330 if (page_pix ==
nullptr)
332 int page_width = pixGetWidth(page_pix);
333 int page_height = pixGetHeight(page_pix);
335 padded_box.
pad(padding, padding);
337 TBOX page_box(0, 0, page_width, page_height);
338 padded_box &= page_box;
339 Box* box = boxCreate(page_box.
left(), page_height - page_box.
top(),
341 Pix* sample_pix = pixClipRectangle(page_pix, box,
nullptr);