27 #define MAX_LINE_LEN 1024
54 if (label32_ !=
NULL) {
63 if (label32_ !=
NULL) {
72 if (label32_ !=
NULL) {
78 SetLabel(reinterpret_cast<const char_32 *>(str32.c_str()));
86 unsigned short first_char;
87 unsigned short last_char;
88 unsigned short norm_top;
89 unsigned short norm_bottom;
90 unsigned short norm_aspect_ratio;
96 if (fp->
Read(&val32,
sizeof(val32)) !=
sizeof(val32)) {
99 if (val32 != 0xabd0fefe) {
103 if (fp->
Read(&val32,
sizeof(val32)) !=
sizeof(val32)) {
108 label32 =
new char_32[val32 + 1];
109 if (label32 ==
NULL) {
113 if (fp->
Read(label32, val32 *
sizeof(*label32)) !=
114 (val32 *
sizeof(*label32))) {
123 if (fp->
Read(&page,
sizeof(page)) !=
sizeof(page)) {
126 if (fp->
Read(&left,
sizeof(left)) !=
sizeof(left)) {
129 if (fp->
Read(&top,
sizeof(top)) !=
sizeof(top)) {
132 if (fp->
Read(&first_char,
sizeof(first_char)) !=
sizeof(first_char)) {
135 if (fp->
Read(&last_char,
sizeof(last_char)) !=
sizeof(last_char)) {
138 if (fp->
Read(&norm_top,
sizeof(norm_top)) !=
sizeof(norm_top)) {
141 if (fp->
Read(&norm_bottom,
sizeof(norm_bottom)) !=
sizeof(norm_bottom)) {
144 if (fp->
Read(&norm_aspect_ratio,
sizeof(norm_aspect_ratio)) !=
145 sizeof(norm_aspect_ratio)) {
150 if (char_samp ==
NULL) {
154 char_samp->label32_ = label32;
155 char_samp->page_ = page;
156 char_samp->left_ = left;
157 char_samp->top_ = top;
158 char_samp->first_char_ = first_char;
159 char_samp->last_char_ = last_char;
160 char_samp->norm_top_ = norm_top;
161 char_samp->norm_bottom_ = norm_bottom;
162 char_samp->norm_aspect_ratio_ = norm_aspect_ratio;
176 unsigned short first_char;
177 unsigned short last_char;
178 unsigned short norm_top;
179 unsigned short norm_bottom;
180 unsigned short norm_aspect_ratio;
185 if (fread(&val32, 1,
sizeof(val32), fp) !=
sizeof(val32)) {
188 if (val32 != 0xabd0fefe) {
192 if (fread(&val32, 1,
sizeof(val32), fp) !=
sizeof(val32)) {
197 label32 =
new char_32[val32 + 1];
198 if (label32 ==
NULL) {
202 if (fread(label32, 1, val32 *
sizeof(*label32), fp) !=
203 (val32 *
sizeof(*label32))) {
212 if (fread(&page, 1,
sizeof(page), fp) !=
sizeof(page)) {
215 if (fread(&left, 1,
sizeof(left), fp) !=
sizeof(left)) {
218 if (fread(&top, 1,
sizeof(top), fp) !=
sizeof(top)) {
221 if (fread(&first_char, 1,
sizeof(first_char), fp) !=
sizeof(first_char)) {
224 if (fread(&last_char, 1,
sizeof(last_char), fp) !=
sizeof(last_char)) {
227 if (fread(&norm_top, 1,
sizeof(norm_top), fp) !=
sizeof(norm_top)) {
230 if (fread(&norm_bottom, 1,
sizeof(norm_bottom), fp) !=
sizeof(norm_bottom)) {
233 if (fread(&norm_aspect_ratio, 1,
sizeof(norm_aspect_ratio), fp) !=
234 sizeof(norm_aspect_ratio)) {
239 if (char_samp ==
NULL) {
243 char_samp->label32_ = label32;
244 char_samp->page_ = page;
245 char_samp->left_ = left;
246 char_samp->top_ = top;
247 char_samp->first_char_ = first_char;
248 char_samp->last_char_ = last_char;
249 char_samp->norm_top_ = norm_top;
250 char_samp->norm_bottom_ = norm_bottom;
251 char_samp->norm_aspect_ratio_ = norm_aspect_ratio;
263 if (scaled_samp ==
NULL) {
266 if (scaled_samp->
ScaleFrom(
this, isotropic) ==
false) {
270 scaled_samp->left_ = left_;
271 scaled_samp->top_ = top_;
272 scaled_samp->page_ = page_;
274 scaled_samp->first_char_ = first_char_;
275 scaled_samp->last_char_ = last_char_;
276 scaled_samp->norm_top_ = norm_top_;
277 scaled_samp->norm_bottom_ = norm_bottom_;
278 scaled_samp->norm_aspect_ratio_ = norm_aspect_ratio_;
284 unsigned char *data) {
287 if (char_samp ==
NULL) {
302 if (fwrite(&val32, 1,
sizeof(val32), fp) !=
sizeof(val32)) {
307 if (fwrite(&val32, 1,
sizeof(val32), fp) !=
sizeof(val32)) {
311 if (label32_ !=
NULL) {
312 if (fwrite(label32_, 1, val32 *
sizeof(*label32_), fp) !=
313 (val32 *
sizeof(*label32_))) {
318 if (fwrite(&page_, 1,
sizeof(page_), fp) !=
sizeof(page_)) {
321 if (fwrite(&left_, 1,
sizeof(left_), fp) !=
sizeof(left_)) {
324 if (fwrite(&top_, 1,
sizeof(top_), fp) !=
sizeof(top_)) {
327 if (fwrite(&first_char_, 1,
sizeof(first_char_), fp) !=
328 sizeof(first_char_)) {
331 if (fwrite(&last_char_, 1,
sizeof(last_char_), fp) !=
sizeof(last_char_)) {
334 if (fwrite(&norm_top_, 1,
sizeof(norm_top_), fp) !=
sizeof(norm_top_)) {
337 if (fwrite(&norm_bottom_, 1,
sizeof(norm_bottom_), fp) !=
338 sizeof(norm_bottom_)) {
341 if (fwrite(&norm_aspect_ratio_, 1,
sizeof(norm_aspect_ratio_), fp) !=
342 sizeof(norm_aspect_ratio_)) {
360 int cropped_left = 0;
362 int cropped_wid =
wid_;
363 int cropped_hgt =
hgt_;
365 &cropped_wid, &cropped_hgt);
367 if (cropped_wid == 0 || cropped_hgt == 0) {
373 cropped_wid, cropped_hgt);
381 cropped_wid / (cropped_wid + cropped_hgt));
386 Copy(cropped_left, cropped_top, cropped_wid, cropped_hgt, cropped_samp);
393 int max_hist_wnd,
int min_con_comp_size)
const {
400 if (concomp_cnt <= 0 || !concomp_array) {
402 delete []concomp_array;
407 for (
int concomp = 0; concomp < concomp_cnt; concomp++) {
408 int concomp_seg_cnt = 0;
412 concomp_array[concomp]->
Segment(max_hist_wnd, &concomp_seg_cnt);
414 if (concomp_alloc_seg ==
NULL) {
416 concomp_seg_array = concomp_array + concomp;
419 concomp_seg_array = concomp_alloc_seg;
420 delete concomp_array[concomp];
423 for (
int seg_idx = 0; seg_idx < concomp_seg_cnt; seg_idx++) {
425 if (concomp_seg_array[seg_idx]->
Width() < 2 &&
426 concomp_seg_array[seg_idx]->
Height() < 2) {
427 delete concomp_seg_array[seg_idx];
434 if (temp_segm_array ==
NULL) {
435 fprintf(stderr,
"Cube ERROR (CharSamp::Segment): could not "
436 "allocate additional connected components\n");
437 delete []concomp_seg_array;
438 delete []concomp_array;
443 memcpy(temp_segm_array, seg_array, seg_cnt *
sizeof(*seg_array));
446 seg_array = temp_segm_array;
448 seg_array[seg_cnt++] = concomp_seg_array[seg_idx];
451 if (concomp_alloc_seg !=
NULL) {
452 delete []concomp_alloc_seg;
455 delete []concomp_array;
458 if (seg_cnt > 0 && seg_array !=
NULL) {
459 qsort(seg_array, seg_cnt,
sizeof(*seg_array), right_2_left ?
462 (*segment_cnt) = seg_cnt;
468 int seg_flags_size,
int *seg_flags,
469 bool *left_most,
bool *right_most,
474 end_concomp = strt_concomp + seg_flags_size;
479 for (concomp = strt_concomp; concomp < end_concomp; concomp++) {
480 if (!seg_flags || seg_flags[concomp - strt_concomp] != 0) {
482 min_id = concomp_array[concomp]->
ID();
483 max_id = concomp_array[concomp]->
ID();
486 UpdateRange(concomp_array[concomp]->ID(), &min_id, &max_id);
491 if (concomp_cnt < 1 || !once || min_id == -1 || max_id == -1) {
495 int id_cnt = max_id - min_id + 1;
496 bool *id_exist =
new bool[id_cnt];
497 bool *left_most_exist =
new bool[id_cnt];
498 bool *right_most_exist =
new bool[id_cnt];
499 if (!id_exist || !left_most_exist || !right_most_exist)
501 memset(id_exist, 0, id_cnt *
sizeof(*id_exist));
502 memset(left_most_exist, 0, id_cnt *
sizeof(*left_most_exist));
503 memset(right_most_exist, 0, id_cnt *
sizeof(*right_most_exist));
511 int unq_left_most = 0;
512 int unq_right_most = 0;
513 for (concomp = strt_concomp; concomp < end_concomp; concomp++) {
514 if (!seg_flags || seg_flags[concomp - strt_concomp] != 0) {
516 left = concomp_array[concomp]->
Left();
517 right = concomp_array[concomp]->
Right();
518 top = concomp_array[concomp]->
Top();
519 bottom = concomp_array[concomp]->
Bottom();
523 concomp_array[concomp]->
Right(), &left, &right);
525 concomp_array[concomp]->
Bottom(), &top, &bottom);
528 int concomp_id = concomp_array[concomp]->
ID() - min_id;
529 if (!id_exist[concomp_id]) {
530 id_exist[concomp_id] =
true;
533 if (concomp_array[concomp]->LeftMost()) {
534 if (left_most_exist[concomp_id] ==
false) {
535 left_most_exist[concomp_id] =
true;
539 if (concomp_array[concomp]->RightMost()) {
540 if (right_most_exist[concomp_id] ==
false) {
541 right_most_exist[concomp_id] =
true;
548 delete []left_most_exist;
549 delete []right_most_exist;
550 if (!once || left == -1 || top == -1 || right == -1 || bottom == -1) {
553 (*left_most) = (unq_left_most >= unq_ids);
554 (*right_most) = (unq_right_most >= unq_ids);
556 CharSamp *samp =
new CharSamp(left, top, right - left + 1, bottom - top + 1);
562 for (concomp = strt_concomp; concomp < end_concomp; concomp++) {
563 if (!seg_flags || seg_flags[concomp - strt_concomp] != 0) {
566 samp->
line_buff_[pt_ptr->
y() - top][pt_ptr->
x() - left] = 0;
567 pt_ptr = pt_ptr->
Next();
593 unsigned char *raw_data = *raw_data_ptr;
596 memcpy(&val32, raw_data,
sizeof(val32));
597 raw_data +=
sizeof(val32);
598 if (val32 != 0xabd0fefe) {
602 memcpy(&val32, raw_data,
sizeof(val32));
603 raw_data +=
sizeof(val32);
606 label32 =
new char_32[val32 + 1];
607 if (label32 ==
NULL) {
611 memcpy(label32, raw_data, val32 *
sizeof(*label32));
612 raw_data += (val32 *
sizeof(*label32));
621 if (char_samp ==
NULL) {
626 char_samp->label32_ = label32;
627 memcpy(&char_samp->page_, raw_data,
sizeof(char_samp->page_));
628 raw_data +=
sizeof(char_samp->page_);
629 memcpy(&char_samp->left_, raw_data,
sizeof(char_samp->left_));
630 raw_data +=
sizeof(char_samp->left_);
631 memcpy(&char_samp->top_, raw_data,
sizeof(char_samp->top_));
632 raw_data +=
sizeof(char_samp->top_);
633 memcpy(&char_samp->first_char_, raw_data,
sizeof(char_samp->first_char_));
634 raw_data +=
sizeof(char_samp->first_char_);
635 memcpy(&char_samp->last_char_, raw_data,
sizeof(char_samp->last_char_));
636 raw_data +=
sizeof(char_samp->last_char_);
637 memcpy(&char_samp->norm_top_, raw_data,
sizeof(char_samp->norm_top_));
638 raw_data +=
sizeof(char_samp->norm_top_);
639 memcpy(&char_samp->norm_bottom_, raw_data,
sizeof(char_samp->norm_bottom_));
640 raw_data +=
sizeof(char_samp->norm_bottom_);
641 memcpy(&char_samp->norm_aspect_ratio_, raw_data,
642 sizeof(char_samp->norm_aspect_ratio_));
643 raw_data +=
sizeof(char_samp->norm_aspect_ratio_);
651 (*raw_data_ptr) = raw_data;
658 CharSamp *scaled_bmp =
Scale(conv_grid_size, conv_grid_size);
663 unsigned char *buff = scaled_bmp->
RawData();
666 int bmp_size = conv_grid_size * conv_grid_size;
667 for (input = 0; input < bmp_size; input++) {
668 features[input] = 255.0f - (1.0f * buff[input]);