Issue 497
[zxing.git] / cpp / core / src / zxing / qrcode / detector / Detector.cpp
index a50424c..6c32558 100644 (file)
@@ -25,6 +25,7 @@
 #include <zxing/qrcode/detector/AlignmentPatternFinder.h>\r
 #include <zxing/qrcode/Version.h>\r
 #include <zxing/common/GridSampler.h>\r
+#include <zxing/DecodeHints.h>\r
 #include <cmath>\r
 #include <sstream>\r
 #include <cstdlib>\r
@@ -42,9 +43,9 @@ Ref<BitMatrix> Detector::getImage() {
    return image_;\r
 }\r
 \r
-Ref<DetectorResult> Detector::detect() {\r
+Ref<DetectorResult> Detector::detect(DecodeHints const& hints) {\r
   FinderPatternFinder finder(image_);\r
-  Ref<FinderPatternInfo> info(finder.find());\r
+  Ref<FinderPatternInfo> info(finder.find(hints));\r
 \r
   Ref<FinderPattern> topLeft(info->getTopLeft());\r
   Ref<FinderPattern> topRight(info->getTopRight());\r
@@ -176,26 +177,32 @@ float Detector::calculateModuleSizeOneWay(Ref<ResultPoint> pattern, Ref<ResultPo
 \r
 float Detector::sizeOfBlackWhiteBlackRunBothWays(int fromX, int fromY, int toX, int toY) {\r
 \r
-  float result = sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY);\r
-\r
-\r
-  // Now count other way -- don't run off image though of course\r
-  int otherToX = fromX - (toX - fromX);\r
-  if (otherToX < 0) {\r
-    // "to" should the be the first value not included, so, the first value off\r
-    // the edge is -1\r
-    otherToX = -1;\r
-  } else if (otherToX >= (int)image_->getWidth()) {\r
-    otherToX = image_->getWidth();\r
-  }\r
-  int otherToY = fromY - (toY - fromY);\r
-  if (otherToY < 0) {\r
-    otherToY = -1;\r
-  } else if (otherToY >= (int)image_->getHeight()) {\r
-    otherToY = image_->getHeight();\r
-  }\r
-  result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);\r
-  return result - 1.0f; // -1 because we counted the middle pixel twice\r
+   float result = sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY);\r
+\r
+   // Now count other way -- don't run off image though of course\r
+   float scale = 1.0f;\r
+   int otherToX = fromX - (toX - fromX);\r
+   if (otherToX < 0) {\r
+     scale = (float) fromX / (float) (fromX - otherToX);\r
+     otherToX = 0;\r
+   } else if (otherToX >= (int)image_->getWidth()) {\r
+     scale = (float) (image_->getWidth() - 1 - fromX) / (float) (otherToX - fromX);\r
+     otherToX = image_->getWidth() - 1;\r
+   }\r
+   int otherToY = (int) (fromY - (toY - fromY) * scale);\r
+\r
+   scale = 1.0f;\r
+   if (otherToY < 0) {\r
+     scale = (float) fromY / (float) (fromY - otherToY);\r
+     otherToY = 0;\r
+   } else if (otherToY >= (int)image_->getHeight()) {\r
+     scale = (float) (image_->getHeight() - 1 - fromY) / (float) (otherToY - fromY);\r
+     otherToY = image_->getHeight() - 1;\r
+   }\r
+   otherToX = (int) (fromX + (otherToX - fromX) * scale);\r
+\r
+   result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);\r
+   return result - 1.0f; // -1 because we counted the middle pixel twice\r
 }\r
 \r
 float Detector::sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY) {\r
@@ -254,6 +261,9 @@ Ref<AlignmentPattern> Detector::findAlignmentInRegion(float overallEstModuleSize
   int allowance = (int)(allowanceFactor * overallEstModuleSize);\r
   int alignmentAreaLeftX = max(0, estAlignmentX - allowance);\r
   int alignmentAreaRightX = min((int)(image_->getWidth() - 1), estAlignmentX + allowance);\r
+  if (alignmentAreaRightX - alignmentAreaLeftX < overallEstModuleSize * 3) {\r
+      throw zxing::ReaderException("region too small to hold alignment pattern");\r
+  }\r
   int alignmentAreaTopY = max(0, estAlignmentY - allowance);\r
   int alignmentAreaBottomY = min((int)(image_->getHeight() - 1), estAlignmentY + allowance);\r
 \r