608 unsigned int **out_bmp_array = CreateBmpBuffer(
wid_,
hgt_, 0);
609 if (out_bmp_array ==
NULL) {
610 fprintf(stderr,
"Cube ERROR (Bmp8::FindConComps): could not allocate "
616 ConComp **concomp_array =
NULL;
623 int alloc_concomp_cnt = 0;
626 const int nbr_cnt = 4;
629 int x_del[nbr_cnt] = {-1, 0, 1, -1},
630 y_del[nbr_cnt] = {-1, -1, -1, 0};
633 for (y = 0; y <
hgt_; y++) {
634 for (x = 0; x <
wid_; x++) {
637 int master_concomp_id = 0;
638 ConComp *master_concomp =
NULL;
641 for (
int nbr = 0; nbr < nbr_cnt; nbr++) {
642 x_nbr = x + x_del[nbr];
643 y_nbr = y + y_del[nbr];
645 if (x_nbr < 0 || y_nbr < 0 || x_nbr >= wid_ || y_nbr >= hgt_) {
652 concomp_id = out_bmp_array[y_nbr][x_nbr];
655 if (concomp_id < 1 || concomp_id > alloc_concomp_cnt) {
656 fprintf(stderr,
"Cube ERROR (Bmp8::FindConComps): illegal "
657 "connected component id: %d\n", concomp_id);
658 FreeBmpBuffer(out_bmp_array);
659 delete []concomp_array;
665 if (master_concomp !=
NULL && concomp_id != master_concomp_id) {
667 ConCompPt *pt_ptr = concomp_array[concomp_id - 1]->Head();
668 while (pt_ptr !=
NULL) {
669 out_bmp_array[pt_ptr->y()][pt_ptr->x()] = master_concomp_id;
670 pt_ptr = pt_ptr->Next();
674 if (!master_concomp->Merge(concomp_array[concomp_id - 1])) {
675 fprintf(stderr,
"Cube ERROR (Bmp8::FindConComps): could not "
676 "merge connected component: %d\n", concomp_id);
677 FreeBmpBuffer(out_bmp_array);
678 delete []concomp_array;
683 delete concomp_array[concomp_id - 1];
684 concomp_array[concomp_id - 1] =
NULL;
687 master_concomp_id = concomp_id;
688 master_concomp = concomp_array[master_concomp_id - 1];
690 out_bmp_array[y][x] = master_concomp_id;
692 if (!master_concomp->Add(x, y)) {
693 fprintf(stderr,
"Cube ERROR (Bmp8::FindConComps): could not "
694 "add connected component (%d,%d)\n", x, y);
695 FreeBmpBuffer(out_bmp_array);
696 delete []concomp_array;
704 if (master_concomp ==
NULL) {
705 master_concomp =
new ConComp();
706 if (master_concomp ==
NULL || master_concomp->Add(x, y) ==
false) {
707 fprintf(stderr,
"Cube ERROR (Bmp8::FindConComps): could not "
708 "allocate or add a connected component\n");
709 FreeBmpBuffer(out_bmp_array);
710 delete []concomp_array;
716 ConComp **temp_con_comp =
718 if (temp_con_comp ==
NULL) {
719 fprintf(stderr,
"Cube ERROR (Bmp8::FindConComps): could not "
720 "extend array of connected components\n");
721 FreeBmpBuffer(out_bmp_array);
722 delete []concomp_array;
726 if (alloc_concomp_cnt > 0) {
727 memcpy(temp_con_comp, concomp_array,
728 alloc_concomp_cnt *
sizeof(*concomp_array));
730 delete []concomp_array;
733 concomp_array = temp_con_comp;
736 concomp_array[alloc_concomp_cnt++] = master_concomp;
737 out_bmp_array[y][x] = alloc_concomp_cnt;
744 FreeBmpBuffer(out_bmp_array);
746 if (alloc_concomp_cnt > 0 && concomp_array !=
NULL) {
750 ConComp *concomp =
NULL;
752 for (
int concomp_idx = 0; concomp_idx < alloc_concomp_cnt; concomp_idx++) {
753 concomp = concomp_array[concomp_idx];
756 if (concomp !=
NULL) {
758 if (concomp->PtCnt() > min_size) {
759 concomp->SetLeftMost(
true);
760 concomp->SetRightMost(
true);
761 concomp->SetID((*concomp_cnt));
762 concomp_array[(*concomp_cnt)++] = concomp;
770 return concomp_array;
static const int kConCompAllocChunk
unsigned char ** line_buff_