pgedit.cpp
来自「一个google的OCR源码」· C++ 代码 · 共 1,866 行 · 第 1/4 页
CPP
1,866 行
case WRITE_CMD_EVENT: do_write_file(); break; case TIDY_CMD_EVENT: if (!target_block_list.empty()) { viewing_source = TRUE; // Force viewing target do_tidy_cmd(); } break; case NEW_SOURCE_CMD_EVENT: do_new_source(); break; case IMAGE_CMD_EVENT: display_image =(new_value[0] == 'T'); do_re_display(&word_display); break; case BLOCKS_CMD_EVENT: display_blocks =(new_value[0] == 'T'); do_re_display(&word_display); break; case BASELINES_CMD_EVENT: display_baselines =(new_value[0] == 'T'); do_re_display(&word_display); break; case REFRESH_CMD_EVENT: do_re_display(&word_display); break; case QUIT_CMD_EVENT: if (source_changed || target_changed) { int a = image_win->ShowYesNoDialog( "Changes not saved. Exit anyway?(Y/N)"); if (a == 'y') { exit = TRUE; ScrollView::Exit(); } } else { exit = TRUE; ScrollView::Exit(); } break; case RECOG_WERDS: mode = RECOG_WERDS; break; case RECOG_PSEUDO: mode = RECOG_PSEUDO; break; default: sprintf(msg, "Unrecognised event " INT32FORMAT "(%s)", cmd_event, new_value); image_win->AddMessage(msg); break; } return exit;}/********************************************************************** * process_image_event() * * User has done something in the image window - mouse down or up. Work out * what it is and do something with it. * If DOWN - just remember where it was. * If UP - for each word in the selected area do the operation defined by * the current mode. **********************************************************************/void process_image_event( // action in image win const SVEvent &event) { static ICOORD down; ICOORD up; TBOX selection_box; char msg[80]; switch(event.type) { case SVET_SELECTION: if (event.type == SVET_SELECTION) { down.set_x(event.x - event.x_size); down.set_y(event.y + event.y_size); if (mode == SHOW_POINT_CMD_EVENT) show_point(current_block_list, event.x, event.y); } up.set_x(event.x); up.set_y(event.y); selection_box = TBOX(down, up); switch(mode) { case CHANGE_DISP_CMD_EVENT: process_selected_words(current_block_list, selection_box, &word_blank_and_set_display); break; case COPY_CMD_EVENT: if (!viewing_source) image_win->AddMessage("Can't COPY while viewing target!"); else process_selected_words(current_block_list, selection_box, &word_copy); break; case DELETE_CMD_EVENT: process_selected_words_it(current_block_list, selection_box, &word_delete); break; case CHANGE_TEXT_CMD_EVENT: process_selected_words(current_block_list, selection_box, &word_change_text); break; case TOGGLE_SEG_CMD_EVENT: process_selected_words(current_block_list, selection_box, &word_toggle_seg); break; case DUMP_WERD_CMD_EVENT: process_selected_words(current_block_list, selection_box, &word_dumper); break; case SHOW_BLN_WERD_CMD_EVENT: process_selected_words(current_block_list, selection_box, &word_bln_display); break; case SEGMENT_WERD_CMD_EVENT: re_segment_word(current_block_list, selection_box); break; case ROW_SPACE_STAT_CMD_EVENT: row_space_stat(current_block_list, selection_box); break; case BLOCK_SPACE_STAT_CMD_EVENT: block_space_stat(current_block_list, selection_box); break; case SHOW_POINT_CMD_EVENT: break; // ignore up event case RECOG_WERDS: image_win->AddMessage("Recogging selected words"); process_selected_words(current_block_list, selection_box, &recog_interactive); break; case RECOG_PSEUDO: image_win->AddMessage("Recogging selected blobs"); recog_pseudo_word(current_block_list, selection_box); break; default: sprintf(msg, "Mode %d not yet implemented", mode); image_win->AddMessage(msg); break; } default: break; }}/********************************************************************** * re_scale_and_move_bln_word() * * Scale and move a bln word so that it fits in a specified bounding box. * Scale by width or height to generate the largest image **********************************************************************/float re_scale_and_move_bln_word( // put bln word in box WERD *norm_word, // BL normalised word const TBOX &box // destination box ) { TBOX norm_box = norm_word->bounding_box(); float width_scale_factor; float height_scale_factor; float selected_scale_factor; width_scale_factor = box.width() /(float) norm_box.width(); height_scale_factor = box.height() /(float) ASC_HEIGHT; if ((ASC_HEIGHT * width_scale_factor) <= box.height()) selected_scale_factor = width_scale_factor; else selected_scale_factor = height_scale_factor; norm_word->scale(selected_scale_factor); norm_word->move(ICOORD((box.left() + box.width() / 2), box.bottom())); return selected_scale_factor;}/********************************************************************** * re_segment_word() * * If all selected blobs are in the same row, remove them from their current * word(s) and put them in a new word. Insert the new word in the row at the * appropriate point. Delete any empty words. * **********************************************************************/void re_segment_word( // break/join words BLOCK_LIST *block_list, // blocks to check TBOX &selection_box) { BLOCK_IT block_it(block_list); BLOCK *block; BLOCK *block_to_process = NULL; ROW_IT row_it; ROW *row; ROW *row_to_process = NULL; WERD_IT word_it; WERD *word; WERD *new_word = NULL; BOOL8 polyg = false; PBLOB_IT blob_it; PBLOB_LIST dummy; // Just to initialize new_blob_it. PBLOB_IT new_blob_it = &dummy; PBLOB *blob; /* Find row to process - error if selections from more than one row */ for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) { block = block_it.data(); if (block->bounding_box().overlap(selection_box)) { row_it.set_to_list(block->row_list()); for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) { row = row_it.data(); if (row->bounding_box().overlap(selection_box)) { if (row_to_process == NULL) { block_to_process = block; row_to_process = row; } else { image_win->AddMessage("Cant resegment words " "in more than one row"); return; } } } } } /* Continue with row_to_process */ word_it.set_to_list(row_to_process->word_list()); for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) { word = word_it.data(); polyg = word->flag(W_POLYGON); if (word->bounding_box().overlap(selection_box)) { blob_it.set_to_list(word->gblob_list()); for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) { blob = blob_it.data(); if (gblob_bounding_box(blob, polyg).overlap(selection_box)) { if (new_word == NULL) { new_word = word->shallow_copy(); new_blob_it.set_to_list(new_word->gblob_list()); } new_blob_it.add_to_end(blob_it.extract()); // move blob } } if (blob_it.empty()) { // no blobs in word // so delete word delete word_it.extract(); } } } if (new_word != NULL) { gblob_sort_list(new_word->gblob_list(), polyg); word_it.add_to_end(new_word); word_it.sort(word_comparator); row_to_process->bounding_box().plot(image_win, ScrollView::BLACK, ScrollView::BLACK); word_it.set_to_list(row_to_process->word_list()); for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) word_display(block_to_process, row_to_process, word_it.data()); *current_image_changed = TRUE; }}void block_space_stat( // show space stats BLOCK_LIST *block_list, // blocks to check TBOX &selection_box) { BLOCK_IT block_it(block_list); BLOCK *block; ROW_IT row_it; ROW *row; int block_idx = 0; STATS all_gap_stats(0, MAXSPACING); WERD_IT word_it; WERD *word; PBLOB_IT blob_it; PBLOB *blob; C_BLOB_IT cblob_it; C_BLOB *cblob; TBOX box; inT16 prev_box_right; inT16 gap_width; inT16 min_inter_word_gap; inT16 max_inter_char_gap; /* Find blocks to process */ for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) { block_idx++; block = block_it.data(); if (block->bounding_box().overlap(selection_box)) { /* Process a block */ tprintf("\nBlock %d\n", block_idx); min_inter_word_gap = 3000; max_inter_char_gap = 0; all_gap_stats.clear(); row_it.set_to_list(block->row_list()); for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) { row = row_it.data(); prev_box_right = -1; word_it.set_to_list(row->word_list()); for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) { word = word_it.data(); if (word->flag(W_POLYGON)) { blob_it.set_to_list(word->blob_list()); for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) { blob = blob_it.data(); box = blob->bounding_box(); if (prev_box_right > -1) { gap_width = box.left() - prev_box_right; all_gap_stats.add(gap_width, 1); if (blob_it.at_first()) { if (gap_width < min_inter_word_gap) min_inter_word_gap = gap_width; } else { if (gap_width > max_inter_char_gap) max_inter_char_gap = gap_width; } } prev_box_right = box.right(); } } else { cblob_it.set_to_list(word->cblob_list()); for (cblob_it.mark_cycle_pt(); !cblob_it.cycled_list(); cblob_it.forward()) { cblob = cblob_it.data(); box = cblob->bounding_box(); if (prev_box_right > -1) { gap_width = box.left() - prev_box_right; all_gap_stats.add(gap_width, 1); if (cblob_it.at_first()) { if (gap_width < min_inter_word_gap) min_inter_word_gap = gap_width; } else { if (gap_width > max_inter_char_gap) max_inter_char_gap = gap_width; } } prev_box_right = box.right(); } } } } tprintf("Max inter char gap = %d.\nMin inter word gap = %d.\n", max_inter_char_gap, min_inter_word_gap); all_gap_stats.short_print(NULL, TRUE); all_gap_stats.smooth(2); tprintf("SMOOTHED DATA...\n"); all_gap_stats.short_print(NULL, TRUE); } }}void row_space_stat( // show space stats BLOCK_LIST *block_list, // blocks to check TBOX &selection_box) { BLOCK_IT block_it(block_list); BLOCK *block; ROW_IT row_it; ROW *row; int block_idx = 0; int row_idx; STATS all_gap_stats(0, MAXSPACING); WERD_IT word_it; WERD *word; PBLOB_IT blob_it; PBLOB *blob; C_BLOB_IT cblob_it; C_BLOB *cblob; TBOX box; inT16 prev_box_right; inT16 gap_width; inT16 min_inter_word_gap; inT16 max_inter_char_gap; /* Find rows to process */ for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) { block_idx++; block = block_it.data(); if (block->bounding_box().overlap(selection_box)) { row_it.set_to_list(block->row_list()); row_idx = 0; for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) { row_idx++; row = row_it.data(); if (row->bounding_box().overlap(selection_box)) { /* Process a row */ tprintf("\nBlock %d Row %d\n", block_idx, row_idx); min_inter_word_gap = 3000; max_inter_char_gap = 0; prev_box_right = -1; all_gap_stats.clear(); word_it.set_to_list(row->word_list()); for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) { word = word_it.data(); if (word->flag(W_POLYGON)) { blob_it.set_to_list(word->blob_list()); for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) { blob = blob_it.data(); box = blob->bounding_box(); if (prev_box_right > -1) { gap_width = box.left() - prev_box_right; all_gap_stats.add(gap_width, 1); if (blob_it.at_first()) { if (gap_width < min_inter_word_gap) min_inter_word_gap = gap_width; } else { if (gap_width > max_inter_char_gap) max_inter_char_gap = gap_width; } } prev_box_right = box.right(); } } else { cblob_it.set_to_list(word->cblob_list()); for (cblob_it.mark_cycle_pt(); !cblob_it.cycled_list(); cblob_it.forward()) { cblob = cblob_it.data(); box = cblob->bounding_box(); if (prev_box_right > -1) { gap_width = box.left() - prev_box_right; all_gap_stats.add(gap_width, 1); if (cblob_it.at_first()) { if (gap_width < min_inter_word_gap) min_inter_word_gap = gap_width; } else { if (gap_width > max_inter_char_gap) max_inter_char_gap = gap_width; } } prev_box_right = box.right(); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?