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 " 583 ConComp **concomp_array = NULL;
590 int alloc_concomp_cnt = 0;
593 const int nbr_cnt = 4;
596 int x_del[nbr_cnt] = {-1, 0, 1, -1},
597 y_del[nbr_cnt] = {-1, -1, -1, 0};
600 for (y = 0; y <
hgt_; y++) {
601 for (x = 0; x <
wid_; x++) {
604 int master_concomp_id = 0;
605 ConComp *master_concomp = NULL;
608 for (
int nbr = 0; nbr < nbr_cnt; nbr++) {
609 x_nbr = x + x_del[nbr];
610 y_nbr = y + y_del[nbr];
612 if (x_nbr < 0 || y_nbr < 0 || x_nbr >=
wid_ || y_nbr >=
hgt_) {
619 concomp_id = out_bmp_array[y_nbr][x_nbr];
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;
632 if (master_concomp != NULL && concomp_id != master_concomp_id) {
634 ConCompPt *pt_ptr = concomp_array[concomp_id - 1]->Head();
635 while (pt_ptr != NULL) {
636 out_bmp_array[pt_ptr->y()][pt_ptr->x()] = master_concomp_id;
637 pt_ptr = pt_ptr->Next();
641 if (!master_concomp->Merge(concomp_array[concomp_id - 1])) {
642 fprintf(stderr,
"Cube ERROR (Bmp8::FindConComps): could not " 643 "merge connected component: %d\n", concomp_id);
644 FreeBmpBuffer(out_bmp_array);
645 delete []concomp_array;
650 delete concomp_array[concomp_id - 1];
651 concomp_array[concomp_id - 1] = NULL;
654 master_concomp_id = concomp_id;
655 master_concomp = concomp_array[master_concomp_id - 1];
657 out_bmp_array[y][x] = master_concomp_id;
659 if (!master_concomp->Add(x, y)) {
660 fprintf(stderr,
"Cube ERROR (Bmp8::FindConComps): could not " 661 "add connected component (%d,%d)\n", x, y);
662 FreeBmpBuffer(out_bmp_array);
663 delete []concomp_array;
671 if (master_concomp == NULL) {
672 master_concomp =
new ConComp();
673 if (master_concomp->Add(x, y) ==
false) {
674 fprintf(stderr,
"Cube ERROR (Bmp8::FindConComps): could not " 675 "allocate or add a connected component\n");
676 FreeBmpBuffer(out_bmp_array);
677 delete []concomp_array;
683 ConComp **temp_con_comp =
686 if (alloc_concomp_cnt > 0) {
687 memcpy(temp_con_comp, concomp_array,
688 alloc_concomp_cnt *
sizeof(*concomp_array));
690 delete []concomp_array;
693 concomp_array = temp_con_comp;
696 concomp_array[alloc_concomp_cnt++] = master_concomp;
697 out_bmp_array[y][x] = alloc_concomp_cnt;
704 FreeBmpBuffer(out_bmp_array);
706 if (alloc_concomp_cnt > 0 && concomp_array != NULL) {
710 ConComp *concomp = NULL;
712 for (
int concomp_idx = 0; concomp_idx < alloc_concomp_cnt; concomp_idx++) {
713 concomp = concomp_array[concomp_idx];
716 if (concomp != NULL) {
718 if (concomp->PtCnt() > min_size) {
719 concomp->SetLeftMost(
true);
720 concomp->SetRightMost(
true);
721 concomp->SetID((*concomp_cnt));
722 concomp_array[(*concomp_cnt)++] = concomp;
730 return concomp_array;
static const int kConCompAllocChunk
unsigned char ** line_buff_