Backtracks from the given lattice node and return the corresponding char mapped segments and character count. The character bounding boxes are optional return arguments, if non-NULL values are passed in.
| 366 | // char mapped segments and character count. The character bounding |
| 367 | // boxes are optional return arguments, if non-NULL values are passed in. |
| 368 | CharSamp **BeamSearch::SplitByNode(SearchObject *srch_obj, |
| 369 | SearchNode *srch_node, |
| 370 | int *char_cnt, |
| 371 | Boxa **char_boxes) const { |
| 372 | // Count the characters (could be less than the path length when in |
| 373 | // phrase mode) |
| 374 | *char_cnt = 0; |
| 375 | SearchNode *node = srch_node; |
| 376 | while (node) { |
| 377 | node = node->ParentNode(); |
| 378 | (*char_cnt)++; |
| 379 | } |
| 380 | |
| 381 | if (*char_cnt == 0) |
| 382 | return NULL; |
| 383 | |
| 384 | // Allocate box array |
| 385 | if (char_boxes) { |
| 386 | if (*char_boxes) |
| 387 | boxaDestroy(char_boxes); // clear existing value |
| 388 | *char_boxes = boxaCreate(*char_cnt); |
| 389 | if (*char_boxes == NULL) |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | // Allocate memory for CharSamp array. |
| 394 | CharSamp **chars = new CharSamp *[*char_cnt]; |
| 395 | |
| 396 | int ch_idx = *char_cnt - 1; |
| 397 | int seg_pt_cnt = srch_obj->SegPtCnt(); |
| 398 | bool success=true; |
| 399 | while (srch_node && ch_idx >= 0) { |
| 400 | // Parent node (could be null) |
| 401 | SearchNode *parent_node = srch_node->ParentNode(); |
| 402 | |
| 403 | // Get the seg pts corresponding to the search node |
| 404 | int st_col = !parent_node ? 0 : parent_node->ColIdx() + 1; |
| 405 | int st_seg_pt = st_col <= 0 ? -1 : st_col - 1; |
| 406 | int end_col = srch_node->ColIdx(); |
| 407 | int end_seg_pt = end_col >= seg_pt_cnt ? seg_pt_cnt : end_col; |
| 408 | |
| 409 | // Get a char sample corresponding to the segmentation points |
| 410 | CharSamp *samp = srch_obj->CharSample(st_seg_pt, end_seg_pt); |
| 411 | if (!samp) { |
| 412 | success = false; |
| 413 | break; |
| 414 | } |
| 415 | samp->SetLabel(srch_node->NodeString()); |
| 416 | chars[ch_idx] = samp; |
| 417 | if (char_boxes) { |
| 418 | // Create the corresponding character bounding box |
| 419 | Box *char_box = boxCreate(samp->Left(), samp->Top(), |
| 420 | samp->Width(), samp->Height()); |
| 421 | if (!char_box) { |
| 422 | success = false; |
| 423 | break; |
| 424 | } |
| 425 | boxaAddBox(*char_boxes, char_box, L_INSERT); |
nothing calls this directly
no test coverage detected