Skip to content

Commit ec43e5f

Browse files
committed
Quadrilateral: new Rectangle factory function for bounding-box application
1 parent 95a1c41 commit ec43e5f

File tree

6 files changed

+27
-31
lines changed

6 files changed

+27
-31
lines changed

core/src/GridSampler.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ namespace ZXing {
3838
*/
3939
DetectorResult SampleGrid(const BitMatrix& image, int width, int height, const PerspectiveTransform& mod2Pix);
4040

41-
template <typename PointT = PointF>
42-
Quadrilateral<PointT> Rectangle(int x0, int x1, int y0, int y1, typename PointT::value_t o = 0.5)
43-
{
44-
return {PointT{x0 + o, y0 + o}, {x1 + o, y0 + o}, {x1 + o, y1 + o}, {x0 + o, y1 + o}};
45-
}
46-
4741
class ROI
4842
{
4943
public:

core/src/Quadrilateral.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ Quadrilateral<PointT> Rectangle(int width, int height, typename PointT::value_t
5454
PointT{margin, margin}, {width - margin, margin}, {width - margin, height - margin}, {margin, height - margin}};
5555
}
5656

57+
template <typename PointT = PointF>
58+
Quadrilateral<PointT> Rectangle(int x0, int x1, int y0, int y1, typename PointT::value_t o)
59+
{
60+
return {PointT{x0 + o, y0 + o}, {x1 + o, y0 + o}, {x1 + o, y1 + o}, {x0 + o, y1 + o}};
61+
}
62+
63+
template <typename PointT = PointF>
64+
Quadrilateral<PointT> Rectangle(int left, int top, int width, int height)
65+
{
66+
int right = left + width - 1;
67+
int bottom = top + height - 1;
68+
69+
return {PointT{left, top}, {right, top}, {right, bottom}, {left, bottom}};
70+
}
71+
5772
template <typename PointT = PointF>
5873
Quadrilateral<PointT> CenteredSquare(int size)
5974
{

core/src/datamatrix/DMDetector.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -949,12 +949,9 @@ static DetectorResult DetectPure(const BitMatrix& image)
949949
|| !image.isIn(PointF{left + modSizeX / 2 + (dimT - 1) * modSize, top + modSizeY / 2 + (dimR - 1) * modSize}))
950950
return {};
951951

952-
int right = left + width - 1;
953-
int bottom = top + height - 1;
954-
955952
// Now just read off the bits (this is a crop + subsample)
956953
return {Deflate(image, dimT, dimR, top + modSizeY / 2, left + modSizeX / 2, modSize),
957-
{{left, top}, {right, top}, {right, bottom}, {left, bottom}}};
954+
Rectangle<PointI>(left, top, width, height)};
958955
}
959956

960957
DetectorResults Detect(const BitMatrix& image, bool tryHarder, bool tryRotate, bool isPure)

core/src/maxicode/MCReader.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ static DetectorResult ExtractPureBits(const BitMatrix& image)
4040
}
4141
}
4242

43-
int right = left + width - 1;
44-
int bottom = top + height - 1;
45-
46-
return {std::move(bits), {{left, top}, {right, top}, {right, bottom}, {left, bottom}}};
43+
return {std::move(bits), Rectangle<PointI>(left, top, width, height)};
4744
}
4845

4946
Barcode Reader::decode(const BinaryBitmap& image) const

core/src/pdf417/PDFReader.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,6 @@ static Barcode DecodePure(const BinaryBitmap& image_)
291291
int left, top, width, height;
292292
if (!image.findBoundingBox(left, top, width, height, 9) || (width < 3 * 17 && height < 3 * 17))
293293
return {};
294-
int right = left + width - 1;
295-
int bottom = top + height - 1;
296294

297295
// counter intuitively, using a floating point cursor is about twice as fast an integer one (on an AVX architecture)
298296
BitMatrixCursorF cur(image, centered(PointI{left, top}), PointF{1, 0});
@@ -315,7 +313,7 @@ static Barcode DecodePure(const BinaryBitmap& image_)
315313

316314
auto res = DecodeCodewords(codeWords, NumECCodeWords(info.ecLevel));
317315

318-
return Barcode(std::move(res), {{}, {{left, top}, {right, top}, {right, bottom}, {left, bottom}}}, BarcodeFormat::PDF417);
316+
return Barcode(std::move(res), {{}, Rectangle<PointI>(left, top, width, height)}, BarcodeFormat::PDF417);
319317
}
320318

321319
Barcode

core/src/qrcode/QRDetector.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ DetectorResult SampleQR(const BitMatrix& image, const FinderPatternSet& fp)
500500
for (int x = 0; x < N; ++x) {
501501
int x0 = apM[x], x1 = apM[x + 1], y0 = apM[y], y1 = apM[y + 1];
502502
rois.push_back({x0 - (x == 0) * 6, x1 + (x == N - 1) * 7, y0 - (y == 0) * 6, y1 + (y == N - 1) * 7,
503-
PerspectiveTransform{Rectangle(x0, x1, y0, y1),
503+
PerspectiveTransform{Rectangle(x0, x1, y0, y1, 0.5),
504504
{*apP(x, y), *apP(x + 1, y), *apP(x + 1, y + 1), *apP(x, y + 1)}}});
505505
}
506506

@@ -530,10 +530,9 @@ DetectorResult DetectPureQR(const BitMatrix& image)
530530
int left, top, width, height;
531531
if (!image.findBoundingBox(left, top, width, height, MIN_MODULES) || std::abs(width - height) > 1)
532532
return {};
533-
int right = left + width - 1;
534-
int bottom = top + height - 1;
533+
auto pos = Rectangle<PointI>(left, top, width, height);
535534

536-
PointI tl{left, top}, tr{right, top}, bl{left, bottom};
535+
const PointI &tl = pos.topLeft(), &tr = pos.topRight(), &bl = pos.bottomLeft();
537536
Pattern diagonal;
538537
// allow corners be moved one pixel inside to accommodate for possible aliasing artifacts
539538
for (auto [p, d] : {std::pair(tl, PointI{1, 1}), {tr, {-1, 1}}, {bl, {1, -1}}}) {
@@ -561,8 +560,7 @@ DetectorResult DetectPureQR(const BitMatrix& image)
561560
#endif
562561

563562
// Now just read off the bits (this is a crop + subsample)
564-
return {Deflate(image, dimension, dimension, top + moduleSize / 2, left + moduleSize / 2, moduleSize),
565-
{{left, top}, {right, top}, {right, bottom}, {left, bottom}}};
563+
return {Deflate(image, dimension, dimension, top + moduleSize / 2, left + moduleSize / 2, moduleSize), std::move(pos)};
566564
}
567565

568566
DetectorResult DetectPureMQR(const BitMatrix& image)
@@ -574,8 +572,6 @@ DetectorResult DetectPureMQR(const BitMatrix& image)
574572
int left, top, width, height;
575573
if (!image.findBoundingBox(left, top, width, height, MIN_MODULES) || std::abs(width - height) > 1)
576574
return {};
577-
int right = left + width - 1;
578-
int bottom = top + height - 1;
579575

580576
// allow corners be moved one pixel inside to accommodate for possible aliasing artifacts
581577
auto diagonal = BitMatrixCursorI(image, {left, top}, {1, 1}).readPatternFromBlack<Pattern>(1);
@@ -601,7 +597,7 @@ DetectorResult DetectPureMQR(const BitMatrix& image)
601597

602598
// Now just read off the bits (this is a crop + subsample)
603599
return {Deflate(image, dimension, dimension, top + moduleSize / 2, left + moduleSize / 2, moduleSize),
604-
{{left, top}, {right, top}, {right, bottom}, {left, bottom}}};
600+
Rectangle<PointI>(left, top, width, height)};
605601
}
606602

607603
DetectorResult DetectPureRMQR(const BitMatrix& image)
@@ -622,10 +618,9 @@ DetectorResult DetectPureRMQR(const BitMatrix& image)
622618
int left, top, width, height;
623619
if (!image.findBoundingBox(left, top, width, height, MIN_MODULES) || height >= width)
624620
return {};
625-
int right = left + width - 1;
626-
int bottom = top + height - 1;
621+
auto pos = Rectangle<PointI>(left, top, width, height);
627622

628-
PointI tl{left, top}, tr{right, top}, br{right, bottom}, bl{left, bottom};
623+
const PointI &tl = pos.topLeft(), &tr = pos.topRight(), &bl = pos.bottomLeft(), &br = pos.bottomRight();
629624

630625
// allow corners be moved one pixel inside to accommodate for possible aliasing artifacts
631626
auto diagonal = BitMatrixCursorI(image, tl, {1, 1}).readPatternFromBlack<Pattern>(1);
@@ -662,11 +657,11 @@ DetectorResult DetectPureRMQR(const BitMatrix& image)
662657
LogMatrixWriter lmw(log, image, 5, "grid2.pnm");
663658
for (int y = 0; y < dimH; y++)
664659
for (int x = 0; x < dimW; x++)
665-
log(PointF(left + (x + .5f) * moduleSize, top + (y + .5f) * moduleSize));
660+
log(pos.topLeft() + moduleSize * PointF(x + .5f, y + .5f));
666661
#endif
667662

668663
// Now just read off the bits (this is a crop + subsample)
669-
return {Deflate(image, dimW, dimH, top + moduleSize / 2, left + moduleSize / 2, moduleSize), {tl, tr, br, bl}};
664+
return {Deflate(image, dimW, dimH, top + moduleSize / 2, left + moduleSize / 2, moduleSize), std::move(pos)};
670665
}
671666

672667
DetectorResult SampleMQR(const BitMatrix& image, const ConcentricPattern& fp)

0 commit comments

Comments
 (0)