14 size_t uextent,
size_t vextent,
bool uwrap,
bool vwrap,
18 (*dilated)[0] = bitmap[0] || bitmap[1] ||
19 bitmap[uextent] || bitmap[uextent + 1];
21 (*dilated)[0] = (*dilated)[0] ||
22 bitmap[(vextent - 1) * uextent] ||
23 bitmap[(vextent - 1) * uextent + 1];
25 (*dilated)[0] = (*dilated)[0] || bitmap[uextent - 1]
26 || bitmap[2 * uextent - 1];
28 (*dilated)[0] = (*dilated)[0] ||
29 bitmap[(vextent - 1) * uextent + uextent - 1];
33 for (
size_t i = 1; i < uextent - 1; ++i)
35 (*dilated)[i] = bitmap[i - 1] || bitmap[i] || bitmap[i + 1] ||
36 bitmap[uextent + i - 1] || bitmap[uextent + i] ||
37 bitmap[uextent + i + 1];
42 for (
size_t i = 1; i < uextent - 1; ++i)
44 (*dilated)[i] = bitmap[i - 1] || bitmap[i] || bitmap[i + 1] ||
45 bitmap[uextent + i - 1] || bitmap[uextent + i] ||
46 bitmap[uextent + i + 1] ||
47 bitmap[(vextent - 1) * uextent + i - 1] ||
48 bitmap[(vextent - 1) * uextent + i] ||
49 bitmap[(vextent - 1) * uextent + i + 1];
53 (*dilated)[uextent - 1] = bitmap[uextent - 1] || bitmap[uextent - 2] ||
54 bitmap[2 * uextent - 1] || bitmap[2 * uextent - 2];
56 (*dilated)[uextent - 1] = (*dilated)[uextent - 1] ||
57 bitmap[vextent * uextent - 1] ||
58 bitmap[vextent * uextent - 2];
60 (*dilated)[uextent - 1] = (*dilated)[uextent - 1] ||
61 bitmap[uextent] || bitmap[0];
63 (*dilated)[uextent - 1] = (*dilated)[uextent - 1] ||
64 bitmap[(vextent - 1) * uextent];
65 size_t row = 0, prevRow, nextRow = uextent;
66 for (
size_t j = 1; j < vextent - 1; ++j)
70 nextRow = row + uextent;
72 (*dilated)[row] = bitmap[prevRow] || bitmap[prevRow + 1] ||
73 bitmap[row] || bitmap[row + 1] || bitmap[nextRow] ||
76 (*dilated)[row] = (*dilated)[row] || bitmap[row - 1] ||
77 bitmap[nextRow - 1] || bitmap[nextRow + uextent - 1];
78 for (
size_t i = 1; i < uextent - 1; ++i)
80 (*dilated)[row + i] = bitmap[prevRow + i - 1] ||
81 bitmap[prevRow + i ] || bitmap[prevRow + i + 1] ||
82 bitmap[row + i - 1] || bitmap[row + i] ||
83 bitmap[row + i + 1] || bitmap[nextRow + i - 1] ||
84 bitmap[nextRow + i] || bitmap[nextRow + i + 1];
87 (*dilated)[row + uextent - 1] = bitmap[prevRow + uextent - 2] ||
88 bitmap[prevRow + uextent - 1] ||
89 bitmap[row + uextent - 2] || bitmap[row + uextent - 1] ||
90 bitmap[nextRow + uextent - 2] || bitmap[nextRow + uextent - 1];
92 (*dilated)[row + uextent - 1] = (*dilated)[row + uextent - 1] ||
93 bitmap[prevRow] || bitmap[row] || bitmap[nextRow];
96 (*dilated)[(vextent - 1) * uextent] = bitmap[(vextent - 1) * uextent] ||
97 bitmap[(vextent - 1) * uextent + 1] ||
98 bitmap[(vextent - 2) * uextent] || bitmap[(vextent - 2) * uextent + 1];
100 (*dilated)[(vextent - 1) * uextent] =
101 (*dilated)[(vextent - 1) * uextent] || bitmap[0] || bitmap[1];
103 (*dilated)[(vextent - 1) * uextent] =
104 (*dilated)[(vextent - 1) * uextent] ||
105 bitmap[vextent * uextent - 1] ||
106 bitmap[(vextent - 1) * uextent - 1];
108 (*dilated)[(vextent - 1) * uextent] =
109 (*dilated)[(vextent - 1) * uextent] || bitmap[uextent - 1];
113 for (
size_t i = 1; i < uextent - 1; ++i)
115 (*dilated)[(vextent - 1) * uextent + i] =
116 bitmap[(vextent - 1) * uextent + i] ||
117 bitmap[(vextent - 1) * uextent + i - 1] ||
118 bitmap[(vextent - 1) * uextent + i + 1] ||
119 bitmap[(vextent - 2) * uextent + i] ||
120 bitmap[(vextent - 2) * uextent + i - 1] ||
121 bitmap[(vextent - 2) * uextent + i + 1];
126 for (
size_t i = 1; i < uextent - 1; ++i)
128 (*dilated)[(vextent - 1) * uextent + i] =
129 bitmap[(vextent - 1) * uextent + i] ||
130 bitmap[(vextent - 1) * uextent + i - 1] ||
131 bitmap[(vextent - 1) * uextent + i + 1] ||
132 bitmap[(vextent - 2) * uextent + i] ||
133 bitmap[(vextent - 2) * uextent + i - 1] ||
134 bitmap[(vextent - 2) * uextent + i + 1] ||
135 bitmap[i - 1] || bitmap[i] || bitmap[i + 1];
139 (*dilated)[bitmap.
size() - 1] = bitmap[bitmap.
size() - 1] ||
140 bitmap[bitmap.
size() - 2] || bitmap[bitmap.
size() - uextent - 1] ||
141 bitmap[bitmap.
size() - uextent - 2];
143 (*dilated)[bitmap.
size() - 1] = (*dilated)[bitmap.
size() - 1] ||
144 bitmap[uextent - 1] || bitmap[uextent - 2];
146 (*dilated)[bitmap.
size() - 1] = (*dilated)[bitmap.
size() - 1] ||
147 bitmap[bitmap.
size() - uextent] ||
148 bitmap[bitmap.
size() - 2 * uextent];
150 (*dilated)[bitmap.
size() - 1] = (*dilated)[bitmap.
size() - 1] ||
155 size_t uextent,
size_t vextent,
bool uwrap,
bool vwrap,
159 (*dilated)[0] = bitmap[0] || bitmap[1] ||
162 (*dilated)[0] = (*dilated)[0] ||
163 bitmap[(vextent - 1) * uextent];
166 (*dilated)[0] = (*dilated)[0] || bitmap[uextent - 1];
171 for (
size_t i = 1; i < uextent - 1; ++i)
173 (*dilated)[i] = bitmap[i - 1] || bitmap[i] || bitmap[i + 1] ||
179 for (
size_t i = 1; i < uextent - 1; ++i)
181 (*dilated)[i] = bitmap[i - 1] || bitmap[i] || bitmap[i + 1] ||
182 bitmap[uextent + i] || bitmap[(vextent - 1) * uextent + i];
186 (*dilated)[uextent - 1] = bitmap[uextent - 1] || bitmap[uextent - 2] ||
187 bitmap[2 * uextent - 1];
189 (*dilated)[uextent - 1] = (*dilated)[uextent - 1] ||
190 bitmap[vextent * uextent - 1];
193 (*dilated)[uextent - 1] = (*dilated)[uextent - 1] || bitmap[0];
195 size_t row = 0, prevRow, nextRow = uextent;
196 for (
size_t j = 1; j < vextent - 1; ++j)
200 nextRow = row + uextent;
202 (*dilated)[row] = bitmap[prevRow] ||
203 bitmap[row] || bitmap[row + 1] || bitmap[nextRow];
206 (*dilated)[row] = (*dilated)[row] || bitmap[nextRow - 1];
208 for (
size_t i = 1; i < uextent - 1; ++i)
210 (*dilated)[row + i] = bitmap[prevRow + i ] ||
211 bitmap[row + i - 1] || bitmap[row + i] ||
212 bitmap[row + i + 1] || bitmap[nextRow + i];
215 (*dilated)[row + uextent - 1] = bitmap[prevRow + uextent - 1] ||
216 bitmap[row + uextent - 2] || bitmap[row + uextent - 1] ||
217 bitmap[nextRow + uextent - 1];
219 (*dilated)[row + uextent - 1] = (*dilated)[row + uextent - 1] ||
223 (*dilated)[(vextent - 1) * uextent] = bitmap[(vextent - 1) * uextent] ||
224 bitmap[(vextent - 1) * uextent + 1] ||
225 bitmap[(vextent - 2) * uextent];
227 (*dilated)[(vextent - 1) * uextent] =
228 (*dilated)[(vextent - 1) * uextent] || bitmap[0];
230 (*dilated)[(vextent - 1) * uextent] =
231 (*dilated)[(vextent - 1) * uextent] ||
232 bitmap[vextent * uextent - 1];
236 for (
size_t i = 1; i < uextent - 1; ++i)
238 (*dilated)[(vextent - 1) * uextent + i] =
239 bitmap[(vextent - 1) * uextent + i] ||
240 bitmap[(vextent - 1) * uextent + i - 1] ||
241 bitmap[(vextent - 1) * uextent + i + 1] ||
242 bitmap[(vextent - 2) * uextent + i];
247 for (
size_t i = 1; i < uextent - 1; ++i)
249 (*dilated)[(vextent - 1) * uextent + i] =
250 bitmap[(vextent - 1) * uextent + i] ||
251 bitmap[(vextent - 1) * uextent + i - 1] ||
252 bitmap[(vextent - 1) * uextent + i + 1] ||
253 bitmap[(vextent - 2) * uextent + i] ||
258 (*dilated)[bitmap.
size() - 1] = bitmap[bitmap.
size() - 1] ||
259 bitmap[bitmap.
size() - 2] || bitmap[bitmap.
size() - uextent - 1];
261 (*dilated)[bitmap.
size() - 1] = (*dilated)[bitmap.
size() - 1] ||
264 (*dilated)[bitmap.
size() - 1] = (*dilated)[bitmap.
size() - 1] ||
265 bitmap[bitmap.
size() - uextent];
269 size_t uextent,
size_t vextent,
bool uwrap,
bool vwrap,
284 (*eroded)[0] = bitmap[0]
287 && bitmap[uextent + 1]
288 && bitmap[(vextent - 1) * uextent]
289 && bitmap[(vextent - 1) * uextent + 1]
290 && bitmap[uextent - 1]
291 && bitmap[2 * uextent - 1]
292 && bitmap[(vextent - 1) * uextent + uextent - 1];
299 for (
size_t i = 1; i < uextent - 1; ++i)
308 for (
size_t i = 1; i < uextent - 1; ++i)
310 (*eroded)[i] = bitmap[i - 1] && bitmap[i] && bitmap[i + 1] &&
311 bitmap[uextent + i - 1] && bitmap[uextent + i] &&
312 bitmap[uextent + i + 1] &&
313 bitmap[(vextent - 1) * uextent + i - 1] &&
314 bitmap[(vextent - 1) * uextent + i] &&
315 bitmap[(vextent - 1) * uextent + i + 1];
319 (*eroded)[uextent - 1] =
330 (*eroded)[uextent - 1] = bitmap[uextent - 1]
331 && bitmap[uextent - 2]
332 && bitmap[2 * uextent - 1]
333 && bitmap[2 * uextent - 2]
334 && bitmap[vextent * uextent - 1]
335 && bitmap[vextent * uextent - 2]
338 && bitmap[(vextent - 1) * uextent];
340 size_t row = 0, prevRow, nextRow = uextent;
341 for (
size_t j = 1; j < vextent - 1; ++j)
345 nextRow = row + uextent;
352 (*eroded)[row] = bitmap[prevRow]
353 && bitmap[prevRow + 1]
357 && bitmap[nextRow + 1]
359 && bitmap[nextRow - 1]
360 && bitmap[nextRow + uextent - 1];
362 for (
size_t i = 1; i < uextent - 1; ++i)
364 (*eroded)[row + i] = bitmap[prevRow + i - 1] &&
365 bitmap[prevRow + i ] && bitmap[prevRow + i + 1] &&
366 bitmap[row + i - 1] && bitmap[row + i] &&
367 bitmap[row + i + 1] && bitmap[nextRow + i - 1] &&
368 bitmap[nextRow + i] && bitmap[nextRow + i + 1];
371 (*eroded)[row + uextent - 1] =
377 (*eroded)[row + uextent - 1] = bitmap[prevRow + uextent - 2]
378 && bitmap[prevRow + uextent - 1]
379 && bitmap[row + uextent - 2]
380 && bitmap[row + uextent - 1]
381 && bitmap[nextRow + uextent - 2]
382 && bitmap[nextRow + uextent - 1]
389 (*eroded)[(vextent - 1) * uextent] =
402 (*eroded)[(vextent - 1) * uextent] = bitmap[(vextent - 1) * uextent]
403 && bitmap[(vextent - 1) * uextent + 1]
404 && bitmap[(vextent - 2) * uextent]
405 && bitmap[(vextent - 2) * uextent + 1]
408 && bitmap[vextent * uextent - 1]
409 && bitmap[(vextent - 1) * uextent - 1]
410 && bitmap[uextent - 1];
415 for (
size_t i = 1; i < uextent - 1; ++i)
417 (*eroded)[(vextent - 1) * uextent + i] =
false
428 for (
size_t i = 1; i < uextent - 1; ++i)
430 (*eroded)[(vextent - 1) * uextent + i] =
431 bitmap[(vextent - 1) * uextent + i] &&
432 bitmap[(vextent - 1) * uextent + i - 1] &&
433 bitmap[(vextent - 1) * uextent + i + 1] &&
434 bitmap[(vextent - 2) * uextent + i] &&
435 bitmap[(vextent - 2) * uextent + i - 1] &&
436 bitmap[(vextent - 2) * uextent + i + 1] &&
437 bitmap[i - 1] && bitmap[i] && bitmap[i + 1];
441 (*eroded)[bitmap.
size() - 1] =
false
453 (*eroded)[bitmap.
size() - 1] = bitmap[bitmap.
size() - 1]
454 && bitmap[bitmap.
size() - 2]
455 && bitmap[bitmap.
size() - uextent - 1]
456 && bitmap[bitmap.
size() - uextent - 2]
457 && bitmap[uextent - 1]
458 && bitmap[uextent - 2]
459 && bitmap[bitmap.
size() - uextent]
460 && bitmap[bitmap.
size() - 2 * uextent]
466 size_t uextent,
size_t vextent,
bool uwrap,
bool vwrap,
470 (*eroded)[0] = bitmap[0] && bitmap[1] &&
473 (*eroded)[0] = (*eroded)[0] &&
474 bitmap[(vextent - 1) * uextent];
477 (*eroded)[0] = (*eroded)[0] && bitmap[uextent - 1];
482 for (
size_t i = 1; i < uextent - 1; ++i)
484 (*eroded)[i] = bitmap[i - 1] && bitmap[i] && bitmap[i + 1] &&
490 for (
size_t i = 1; i < uextent - 1; ++i)
492 (*eroded)[i] = bitmap[i - 1] && bitmap[i] && bitmap[i + 1] &&
493 bitmap[uextent + i] && bitmap[(vextent - 1) * uextent + i];
497 (*eroded)[uextent - 1] = bitmap[uextent - 1] && bitmap[uextent - 2] &&
498 bitmap[2 * uextent - 1];
500 (*eroded)[uextent - 1] = (*eroded)[uextent - 1] &&
501 bitmap[vextent * uextent - 1];
504 (*eroded)[uextent - 1] = (*eroded)[uextent - 1] && bitmap[0];
506 size_t row = 0, prevRow, nextRow = uextent;
507 for (
size_t j = 1; j < vextent - 1; ++j)
511 nextRow = row + uextent;
513 (*eroded)[row] = bitmap[prevRow] &&
514 bitmap[row] && bitmap[row + 1] && bitmap[nextRow];
517 (*eroded)[row] = (*eroded)[row] && bitmap[nextRow - 1];
519 for (
size_t i = 1; i < uextent - 1; ++i)
521 (*eroded)[row + i] = bitmap[prevRow + i ] &&
522 bitmap[row + i - 1] && bitmap[row + i] &&
523 bitmap[row + i + 1] && bitmap[nextRow + i];
526 (*eroded)[row + uextent - 1] = bitmap[prevRow + uextent - 1] &&
527 bitmap[row + uextent - 2] && bitmap[row + uextent - 1] &&
528 bitmap[nextRow + uextent - 1];
530 (*eroded)[row + uextent - 1] = (*eroded)[row + uextent - 1] &&
534 (*eroded)[(vextent - 1) * uextent] = bitmap[(vextent - 1) * uextent] &&
535 bitmap[(vextent - 1) * uextent + 1] &&
536 bitmap[(vextent - 2) * uextent];
538 (*eroded)[(vextent - 1) * uextent] =
539 (*eroded)[(vextent - 1) * uextent] && bitmap[0];
541 (*eroded)[(vextent - 1) * uextent] =
542 (*eroded)[(vextent - 1) * uextent] &&
543 bitmap[vextent * uextent - 1];
547 for (
size_t i = 1; i < uextent - 1; ++i)
549 (*eroded)[(vextent - 1) * uextent + i] =
550 bitmap[(vextent - 1) * uextent + i] &&
551 bitmap[(vextent - 1) * uextent + i - 1] &&
552 bitmap[(vextent - 1) * uextent + i + 1] &&
553 bitmap[(vextent - 2) * uextent + i];
558 for (
size_t i = 1; i < uextent - 1; ++i)
560 (*eroded)[(vextent - 1) * uextent + i] =
561 bitmap[(vextent - 1) * uextent + i] &&
562 bitmap[(vextent - 1) * uextent + i - 1] &&
563 bitmap[(vextent - 1) * uextent + i + 1] &&
564 bitmap[(vextent - 2) * uextent + i] &&
569 (*eroded)[bitmap.size() - 1] = bitmap[bitmap.size() - 1] &&
570 bitmap[bitmap.size() - 2] && bitmap[bitmap.size() - uextent - 1];
572 (*eroded)[bitmap.size() - 1] = (*eroded)[bitmap.size() - 1] &&
575 (*eroded)[bitmap.size() - 1] = (*eroded)[bitmap.size() - 1] &&
576 bitmap[bitmap.size() - uextent];
587 if (!tempLabels.
size())
589 tempLabels.
push_back(std::make_pair(0,
size_t(0)));
591 int curLabel = tempLabels.
size() - 1;
592 size_t prevRow, row = 0, nextRow = uextent;
593 for (
size_t j = 1; j < vextent - 1; ++j)
597 nextRow = row + uextent;
598 for (
size_t i = 1; i < uextent - 1; ++i)
600 if (!bitmap[row + i])
602 (*preWrappedComponentsImg)[row + i] = 0;
603 ++tempLabels[0].second;
608 n[0] = (*preWrappedComponentsImg)[prevRow + i - 1];
609 n[1] = (*preWrappedComponentsImg)[prevRow + i];
610 n[2] = (*preWrappedComponentsImg)[prevRow + i + 1];
611 n[3] = (*preWrappedComponentsImg)[row + i - 1];
612 n[4] = (*preWrappedComponentsImg)[row + i + 1];
613 n[5] = (*preWrappedComponentsImg)[nextRow + i - 1];
614 n[6] = (*preWrappedComponentsImg)[nextRow + i];
615 n[7] = (*preWrappedComponentsImg)[nextRow + i + 1];
616 (*preWrappedComponentsImg)[row + i] =
Label(n, 8,
617 &curLabel, &tempLabels);
622 for (
size_t i = tempLabels.
size() - 1; i > 0; --i)
628 labels->reserve(condensed.
size());
630 for (
size_t i = 0; i < tempLabels.
size(); ++i)
631 if (i == (
size_t)tempLabels[i].first)
633 labels->push_back(std::make_pair(count, tempLabels[i].second));
634 condensed[i] = count;
638 (*labels)[condensed[tempLabels[i].first]].second
639 += tempLabels[i].second;
641 for (
size_t i = 0; i < preWrappedComponentsImg->
size(); ++i)
642 (*preWrappedComponentsImg)[i] =
643 condensed[tempLabels[(*preWrappedComponentsImg)[i]].first];
644 for (
size_t i = 0; i < relabelComponentsImg->
size(); ++i)
645 (*relabelComponentsImg)[i] =
646 condensed[tempLabels[(*relabelComponentsImg)[i]].first];
650 size_t uextent,
size_t vextent,
bool uwrap,
bool vwrap,
654 componentsImg->
resize(uextent * vextent);
657 tempLabels.
push_back(std::make_pair(0,
size_t(0)));
666 (*componentsImg)[0] = ++curLabel;
667 tempLabels.
push_back(std::make_pair(curLabel,
size_t(1)));
671 (*componentsImg)[0] = 0;
672 ++tempLabels[0].second;
675 for (
size_t i = 1; i < uextent; ++i)
680 if ((*componentsImg)[i - 1])
682 (*componentsImg)[i] = (*componentsImg)[i - 1];
683 ++tempLabels[(*componentsImg)[i]].second;
687 (*componentsImg)[i] = ++curLabel;
688 tempLabels.
push_back(std::make_pair(curLabel,
size_t(1)));
693 (*componentsImg)[i] = 0;
694 ++tempLabels[0].second;
697 size_t prevRow, row = 0;
698 size_t jend = vwrap ? vextent - 1 : vextent;
699 for (
size_t j = 1; j < jend; ++j)
702 row = prevRow + uextent;
706 if ((*componentsImg)[prevRow])
708 (*componentsImg)[row] = (*componentsImg)[prevRow];
709 ++tempLabels[(*componentsImg)[row]].second;
714 n[0] = (*componentsImg)[prevRow + 1];
717 n[1] = (*componentsImg)[prevRow + uextent - 1];
719 (*componentsImg)[row] =
Label(n, uwrap ? 2 : 1, &curLabel,
725 (*componentsImg)[row] = 0;
726 ++tempLabels[0].second;
728 for (
size_t i = 1; i < uextent - 1; ++i)
730 if (!bitmap[row + i])
732 (*componentsImg)[row + i] = 0;
733 ++tempLabels[0].second;
737 n[0] = (*componentsImg)[row + i - 1];
738 n[1] = (*componentsImg)[prevRow + i - 1];
739 n[2] = (*componentsImg)[prevRow + i];
740 n[3] = (*componentsImg)[prevRow + i + 1];
741 (*componentsImg)[row + i] =
Label(n, 4, &curLabel, &tempLabels);
744 if (!bitmap[row + uextent - 1])
746 (*componentsImg)[row + uextent - 1] = 0;
747 ++tempLabels[0].second;
751 n[0] = (*componentsImg)[row + uextent - 2];
752 n[1] = (*componentsImg)[prevRow + uextent - 2];
753 n[2] = (*componentsImg)[prevRow + uextent - 1];
756 n[3] = (*componentsImg)[prevRow];
757 n[4] = (*componentsImg)[row];
759 (*componentsImg)[row + uextent - 1] =
Label(n, uwrap ? 5 : 3, &curLabel,
766 prevRow = (vextent - 2) * uextent;
767 row = (vextent - 1) * uextent;
772 n[0] = (*componentsImg)[prevRow];
773 n[1] = (*componentsImg)[prevRow + 1];
774 n[2] = (*componentsImg)[0];
775 n[3] = (*componentsImg)[1];
778 n[4] = (*componentsImg)[prevRow + uextent - 1];
779 n[5] = (*componentsImg)[uextent - 1];
781 (*componentsImg)[row] =
Label(n, uwrap ? 6 : 4, &curLabel,
786 (*componentsImg)[row] = 0;
787 ++tempLabels[0].second;
789 for (
size_t i = 1; i < uextent - 1; ++i)
791 if (!bitmap[row + i])
793 (*componentsImg)[row + i] = 0;
794 ++tempLabels[0].second;
798 n[0] = (*componentsImg)[row + i - 1];
799 n[1] = (*componentsImg)[prevRow + i - 1];
800 n[2] = (*componentsImg)[prevRow + i];
801 n[3] = (*componentsImg)[prevRow + i + 1];
802 n[4] = (*componentsImg)[i - 1];
803 n[5] = (*componentsImg)[i];
804 n[6] = (*componentsImg)[i + 1];
805 (*componentsImg)[row + i] =
Label(n, 7, &curLabel, &tempLabels);
808 if (!bitmap[row + uextent - 1])
810 (*componentsImg)[row + uextent - 1] = 0;
811 ++tempLabels[0].second;
816 n[0] = (*componentsImg)[row + uextent - 2];
817 n[1] = (*componentsImg)[prevRow + uextent - 2];
818 n[2] = (*componentsImg)[prevRow + uextent - 1];
819 n[3] = (*componentsImg)[uextent - 2];
820 n[4] = (*componentsImg)[uextent - 1];
823 n[5] = (*componentsImg)[prevRow];
824 n[6] = (*componentsImg)[row];
825 n[7] = (*componentsImg)[0];
827 (*componentsImg)[row + uextent - 1] =
Label(n, uwrap ? 8 : 5,
828 &curLabel, &tempLabels);
832 for (
size_t i = tempLabels.
size() - 1; i > 0; --i)
838 labels->reserve(condensed.
size());
840 for (
size_t i = 0; i < tempLabels.
size(); ++i)
841 if (i == (
size_t)tempLabels[i].first)
843 labels->push_back(std::make_pair(count, tempLabels[i].second));
844 condensed[i] = count;
848 (*labels)[condensed[tempLabels[i].first]].second
849 += tempLabels[i].second;
851 for (
size_t i = 0; i < componentsImg->
size(); ++i)
852 (*componentsImg)[i] =
853 condensed[tempLabels[(*componentsImg)[i]].first];
856 int Label(
int n[],
int size,
int* curLabel,
860 int count = 0, found;
861 for (
int i = 0; i < size; ++i)
872 labels->push_back(std::make_pair(*curLabel,
size_t(1)));
877 ++(*labels)[found].second;
882 ++(*labels)[found].second;
884 for (
int i = 0; i < size; ++i)
886 if (n[i] && n[i] != found)
903 if ((
a == b) || ((*labels)[b].first ==
a))
907 if ((*labels)[b].first == b)
909 (*labels)[b].first =
a;
914 if ((*labels)[b].first >
a)
916 (*labels)[b].first =
a;
924 if (labels[
a].first ==
a)
926 return labels[
a].first;
932 size_t vextent,
int label,
bool uwrap,
bool vwrap,
933 int startx,
int starty,
int dirx,
int diry,
934 size_t* targetx,
size_t* targety)
938 if ((
size_t)startx == uextent)
942 if ((
size_t)starty == vextent)
946 *targetx = (uwrap && (size_t)startx == uextent - 1) ? 0 : startx + 1;
948 return componentImg[starty * uextent + startx] == label
949 && ((starty > 0 && componentImg[(starty - 1) * uextent + startx] != label)
950 || (!vwrap && starty == 0)
951 || (vwrap && starty == 0 && componentImg[(vextent - 1) * uextent + startx] != label));
955 if (!uwrap && startx == 0)
959 *targetx = (startx == 0) ? uextent - 1 : startx - 1;
961 return (!vwrap && (
size_t)starty == vextent && componentImg[(starty - 1) * uextent + (*targetx)] == label)
962 || ((
size_t)starty != vextent && (componentImg[starty * uextent + (*targetx)] != label
963 && ((starty > 0 && componentImg[(starty - 1) * uextent + (*targetx)] == label)
964 || (vwrap && starty == 0 && componentImg[(vextent - 1) * uextent + (*targetx)] == label))));
968 if ((
size_t)starty == vextent)
973 *targety = (vwrap && (size_t)starty == vextent - 1) ? 0 : starty + 1;
974 return (!uwrap && (
size_t)startx == uextent && componentImg[starty * uextent + (startx - 1)] == label)
975 || ((size_t)startx != uextent && (componentImg[starty * uextent + startx] != label
976 && (((
size_t)startx > 0 && componentImg[starty * uextent + (startx - 1)] == label)
977 || (uwrap && startx == 0 && componentImg[starty * uextent + (uextent - 1)] == label))));
981 if (!vwrap && starty == 0)
985 if ((
size_t)startx == uextent)
990 *targety = (starty == 0) ? vextent - 1 : starty - 1;
991 return componentImg[(*targety) * uextent + startx] == label
992 && ((startx > 0 && componentImg[(*targety) * uextent + (startx - 1)] != label)
993 || (!uwrap && startx == 0)
994 || (uwrap && startx == 0 && componentImg[(*targety) * uextent + (uextent - 1)] != label));
1001 size_t vextent,
int label,
bool uwrap,
bool vwrap,
1007 int x, y, prevx, prevy;
1011 unsigned int edgesExtent[] = { uextent + 1, vextent + 1, 3, 3 };
1012 edges.
Extent(edgesExtent);
1013 bool prevPixelWasWhite =
true;
1019 for (; firsti < componentImg.
size(); ++firsti)
1021 if (prevPixelWasWhite && componentImg[firsti] == label)
1023 prevPixelWasWhite =
false;
1024 x = firsti % uextent;
1025 y = firsti / uextent;
1028 prevPixelWasWhite = componentImg[firsti] != label;
1030 if (firsti >= componentImg.
size())
1043 if (componentImg[(vextent - 1) * uextent] == label)
1053 if ((x > 0 && (
size_t)y < vextent - 1)
1054 || (!uwrap && !vwrap) || (vwrap && !uwrap && y == 0))
1058 unsigned int edgeIndex[] = { x, y, 1, 2 };
1059 if (edges.
find(edgeIndex))
1066 else if (uwrap && !vwrap && x == 0 && (
size_t)y != vextent - 1)
1069 if (!
IsEdge(componentImg, uextent, vextent, label, uwrap, vwrap,
1070 x, y, 1, 0, &dx, &dy))
1075 unsigned int edgeIndex[] = { x + 1, y, 0, 1 };
1076 if (edges.
find(edgeIndex))
1085 else if (uwrap && !vwrap && x == 0 && (
size_t)y == vextent - 1)
1088 if (!
IsEdge(componentImg, uextent, vextent, label, uwrap, vwrap,
1089 x + 1, y + 1, -1, 0, &dx, &dy))
1095 unsigned int edgeIndex[] = { x + 1, y + 1, 0, 1 };
1096 if (edges.
find(edgeIndex))
1104 else if (!uwrap && vwrap && (
size_t)x == uextent - 1)
1108 if (!
IsEdge(componentImg, uextent, vextent, label, uwrap, vwrap,
1109 x + 1, y + 1, 0, -1, &dx, &dy))
1115 unsigned int edgeIndex[] = { x + 1, y + 1, 1, 0 };
1116 if (edges.
find(edgeIndex))
1128 poly.
push_back(Vec2(x + prevx, y + prevy));
1129 edges[x][y][prevx + 1][prevy + 1] =
true;
1136 size_t nextx, nexty;
1138 for (checkEdge = 0; checkEdge < 3; ++checkEdge)
1142 if (
IsEdge(componentImg, uextent, vextent, label, uwrap, vwrap,
1143 x, y, prevx, prevy, &nextx, &nexty))
1156 edges[x][y][prevx + 1][prevy + 1] =
true;
1158 while (poly[0] != Vec2(x, y));
1159 polys->push_back(poly);
1161 while (firsti < componentImg.
size());
1163 static int fname_int = 0;
1164 std::ostringstream fn;
1165 fn <<
"ComponentLoopsInput" << fname_int <<
".txt";
1167 file.open(fn.str().c_str(), std::ios::out);
1168 for (
size_t j = 0; j < vextent; ++j)
1170 for (
size_t i = 0; i < uextent; ++i)
1172 file << componentImg[j * uextent + i];
1178 std::ostringstream fn2;
1179 fn2 <<
"ComponentLoopsOutput" << fname_int++ <<
".txt";
1180 for (
size_t i = 0; i < polys->size(); ++i)
1181 for (
size_t j = 0; j < (*polys)[i].size(); ++j)
1183 loopsImg[(*polys)[i][j][1] * (uextent + 1) + (*polys)[i][j][0]] = i + 1;
1185 file.open(fn2.str().c_str(), std::ios::out);
1186 for (
size_t j = 0; j < vextent + 1; ++j)
1188 for (
size_t i = 0; i < uextent + 1; ++i)
1190 file << loopsImg[j * (uextent + 1) + i];