// Make a text string from the internal data structures. // The input page_res is deleted. char* TessBaseAPI::TesseractToText(PAGE_RES* page_res) { if (page_res != NULL) { int total_length = TextLength(page_res); PAGE_RES_IT page_res_it(page_res); char* result = new char[total_length]; char* ptr = result; for (page_res_it.restart_page(); page_res_it.word () != NULL; page_res_it.forward()) { WERD_RES *word = page_res_it.word(); WERD_CHOICE* choice = word->best_choice; if (choice != NULL) { strcpy(ptr, choice->string().string()); ptr += strlen(ptr); if (word->word->flag(W_EOL)) *ptr++ = '\n'; else *ptr++ = ' '; } } *ptr++ = '\n'; *ptr = '\0'; delete page_res; return result; } return NULL; }
// Return the maximum length that the output text string might occupy. int TessBaseAPI::TextLength(PAGE_RES* page_res) { PAGE_RES_IT page_res_it(page_res); int total_length = 2; // Iterate over the data structures to extract the recognition result. for (page_res_it.restart_page(); page_res_it.word () != NULL; page_res_it.forward()) { WERD_RES *word = page_res_it.word(); WERD_CHOICE* choice = word->best_choice; if (choice != NULL) { total_length += choice->string().length() + 1; for (int i = 0; i < word->reject_map.length(); ++i) { if (word->reject_map[i].rejected()) ++total_length; } } } return total_length; }