Detect connected components in the bitmap
| 570 | |
| 571 | // Detect connected components in the bitmap |
| 572 | ConComp ** Bmp8::FindConComps(int *concomp_cnt, int min_size) const { |
| 573 | (*concomp_cnt) = 0; |
| 574 | |
| 575 | unsigned int **out_bmp_array = CreateBmpBuffer(wid_, hgt_, 0); |
| 576 | if (out_bmp_array == NULL) { |
| 577 | fprintf(stderr, "Cube ERROR (Bmp8::FindConComps): could not allocate " |
| 578 | "bitmap array\n"); |
| 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | // listed of connected components |
| 583 | ConComp **concomp_array = NULL; |
| 584 | |
| 585 | int x; |
| 586 | int y; |
| 587 | int x_nbr; |
| 588 | int y_nbr; |
| 589 | int concomp_id; |
| 590 | int alloc_concomp_cnt = 0; |
| 591 | |
| 592 | // neighbors to check |
| 593 | const int nbr_cnt = 4; |
| 594 | |
| 595 | // relative coordinates of nbrs |
| 596 | int x_del[nbr_cnt] = {-1, 0, 1, -1}, |
| 597 | y_del[nbr_cnt] = {-1, -1, -1, 0}; |
| 598 | |
| 599 | |
| 600 | for (y = 0; y < hgt_; y++) { |
| 601 | for (x = 0; x < wid_; x++) { |
| 602 | // is this a foreground pix |
| 603 | if (line_buff_[y][x] != 0xff) { |
| 604 | int master_concomp_id = 0; |
| 605 | ConComp *master_concomp = NULL; |
| 606 | |
| 607 | // checkout the nbrs |
| 608 | for (int nbr = 0; nbr < nbr_cnt; nbr++) { |
| 609 | x_nbr = x + x_del[nbr]; |
| 610 | y_nbr = y + y_del[nbr]; |
| 611 | |
| 612 | if (x_nbr < 0 || y_nbr < 0 || x_nbr >= wid_ || y_nbr >= hgt_) { |
| 613 | continue; |
| 614 | } |
| 615 | |
| 616 | // is this nbr a foreground pix |
| 617 | if (line_buff_[y_nbr][x_nbr] != 0xff) { |
| 618 | // get its concomp ID |
| 619 | concomp_id = out_bmp_array[y_nbr][x_nbr]; |
| 620 | |
| 621 | // this should not happen |
| 622 | if (concomp_id < 1 || concomp_id > alloc_concomp_cnt) { |
| 623 | fprintf(stderr, "Cube ERROR (Bmp8::FindConComps): illegal " |
| 624 | "connected component id: %d\n", concomp_id); |
| 625 | FreeBmpBuffer(out_bmp_array); |
| 626 | delete []concomp_array; |
| 627 | return NULL; |
| 628 | } |
| 629 |