| 815 | } |
| 816 | |
| 817 | static DetectorResults DetectNew(const BitMatrix& image, bool tryHarder, bool tryRotate) |
| 818 | { |
| 819 | #ifdef PRINT_DEBUG |
| 820 | LogMatrixWriter lmw(log, image, 1, "dm-log.pnm"); |
| 821 | // tryRotate = tryHarder = false; |
| 822 | #endif |
| 823 | |
| 824 | // disable expensive multi-line scan to detect off-center symbols for now |
| 825 | #ifndef __cpp_impl_coroutine |
| 826 | tryHarder = false; |
| 827 | #endif |
| 828 | |
| 829 | // a history log to remember where the tracing already passed by to prevent a later trace from doing the same work twice |
| 830 | ByteMatrix history; |
| 831 | if (tryHarder) |
| 832 | history = ByteMatrix(image.width(), image.height()); |
| 833 | |
| 834 | // instantiate RegressionLine objects outside of Scan function to prevent repetitive std::vector allocations |
| 835 | std::array<DMRegressionLine, 4> lines; |
| 836 | |
| 837 | constexpr int minSymbolSize = 8 * 2; // minimum realistic size in pixel: 8 modules x 2 pixels per module |
| 838 | |
| 839 | for (auto dir : {PointF{-1, 0}, {1, 0}, {0, -1}, {0, 1}}) { |
| 840 | auto center = PointI(image.width() / 2, image.height() / 2); |
| 841 | auto startPos = centered(center - center * dir + minSymbolSize / 2 * dir); |
| 842 | |
| 843 | history.clear(); |
| 844 | |
| 845 | for (int i = 1;; ++i) { |
| 846 | EdgeTracer tracer(image, startPos, dir); |
| 847 | tracer.p += i / 2 * minSymbolSize * (i & 1 ? -1 : 1) * tracer.right(); |
| 848 | if (tryHarder) |
| 849 | tracer.history = &history; |
| 850 | |
| 851 | if (!tracer.isIn()) |
| 852 | break; |
| 853 | |
| 854 | #ifdef __cpp_impl_coroutine |
| 855 | DetectorResult res; |
| 856 | while (res = Scan(tracer, lines), res.isValid()) |
| 857 | co_yield std::move(res); |
| 858 | #else |
| 859 | if (auto res = Scan(tracer, lines); res.isValid()) |
| 860 | return res; |
| 861 | #endif |
| 862 | |
| 863 | if (!tryHarder) |
| 864 | break; // only test center lines |
| 865 | } |
| 866 | |
| 867 | if (!tryRotate) |
| 868 | break; // only test left direction |
| 869 | } |
| 870 | |
| 871 | #ifndef __cpp_impl_coroutine |
| 872 | return {}; |
| 873 | #endif |
| 874 | } |