Skip to content

Commit 9b991e4

Browse files
committed
DMDetector: extract BitMatrixAccessor from EdgeTracer
1 parent 01811be commit 9b991e4

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

core/src/datamatrix/DMDetector.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -562,22 +562,19 @@ PointF intersect(const RegressionLine& l1, const RegressionLine& l2)
562562
return {x, y};
563563
}
564564

565-
class EdgeTracer
565+
struct BitMatrixAccessor
566566
{
567-
const BitMatrix& image;
568-
PointI p; // current position
569-
PointF d; // current direction
567+
const BitMatrix* image;
570568

571-
enum class StepResult { FOUND, OPEN_END, CLOSED_END };
569+
BitMatrixAccessor(const BitMatrix& image) : image(&image) {}
572570

573571
bool isIn(PointI p) const
574572
{
575573
const int b = 0;
576-
return b <= p.x && p.x < image.width()-b &&
577-
b <= p.y && p.y < image.height()-b;
574+
return b <= p.x && p.x < image->width()-b &&
575+
b <= p.y && p.y < image->height()-b;
578576
}
579577
bool isIn(PointF p) const { return isIn(round(p)); }
580-
bool isIn() const { return isIn(p); }
581578

582579
class Value
583580
{
@@ -596,12 +593,23 @@ class EdgeTracer
596593
auto q = round(p);
597594
if (!isIn(q))
598595
return {};
599-
return {image.get(q.x, q.y)};
596+
return {image->get(q.x, q.y)};
600597
}
601598

602599
bool blackAt(PointF p) const { return getAt(p).isBlack(); }
603600
bool whiteAt(PointF p) const { return getAt(p).isWhite(); }
604601
bool isEdge(PointF pos, PointF dir) const { return whiteAt(pos) && blackAt(pos + dir); }
602+
};
603+
604+
class EdgeTracer : BitMatrixAccessor
605+
{
606+
PointI p; // current position
607+
PointF d; // current direction
608+
609+
enum class StepResult { FOUND, OPEN_END, CLOSED_END };
610+
611+
using BitMatrixAccessor::isIn;
612+
bool isIn() const { return isIn(p); }
605613

606614
StepResult traceStep(PointF dEdge, int maxStepSize, bool goodDirection)
607615
{
@@ -635,18 +643,7 @@ class EdgeTracer
635643
}
636644

637645
public:
638-
EdgeTracer(const BitMatrix& img, PointF p, PointF d) : image(img), p(p), d(d) {}
639-
EdgeTracer& operator=(const EdgeTracer& other)
640-
{
641-
assert(&image == &other.image);
642-
p = other.p;
643-
d = other.d;
644-
return *this;
645-
}
646-
EdgeTracer(const EdgeTracer&) = default;
647-
~EdgeTracer() = default;
648-
EdgeTracer(EdgeTracer&&) noexcept(true) = default;
649-
EdgeTracer& operator=(EdgeTracer&&) = default;
646+
EdgeTracer(const BitMatrix& img, PointI p, PointF d) : BitMatrixAccessor(img), p(p), d(d) {}
650647

651648
bool step(int s = 1)
652649
{
@@ -808,7 +805,7 @@ static DetectorResult DetectNew(const BitMatrix& image, bool tryRotate)
808805
#else
809806
for (auto startDirection : {PointF(-1, 0), PointF(-1, 0), PointF(1, 0), PointF(0, -1), PointF(0, 1)}) {
810807
#endif
811-
EdgeTracer startTracer(image, PointF(image.width()/2, image.height()/2), startDirection);
808+
EdgeTracer startTracer(image, {image.width()/2, image.height()/2}, startDirection);
812809
while (startTracer.step()) {
813810
// go forward until we reach a white/black border
814811
if (!startTracer.isEdgeBehind())

0 commit comments

Comments
 (0)