#include <zxing/NotFoundException.h>
#include <zxing/common/detector/WhiteRectangleDetector.h>
#include <zxing/common/detector/MathUtils.h>
#include <sstream>
using std::vector;
using zxing::Ref;
using zxing::ResultPoint;
using zxing::WhiteRectangleDetector;
using zxing::common::detector::MathUtils;
using zxing::BitMatrix;
int WhiteRectangleDetector::INIT_SIZE = 10;
int WhiteRectangleDetector::CORR = 1;
WhiteRectangleDetector::WhiteRectangleDetector(Ref<BitMatrix> image) :
WhiteRectangleDetector(image, INIT_SIZE, image->getWidth() >> 1, image->getHeight() >> 1)
{
}
WhiteRectangleDetector::WhiteRectangleDetector(Ref<BitMatrix> image, int initSize, int x, int y) : image_(image) {
width_ = image->getWidth();
height_ = image->getHeight();
int halfsize = initSize >> 1;
leftInit_ = x - halfsize;
rightInit_ = x + halfsize;
upInit_ = y - halfsize;
downInit_ = y + halfsize;
if (upInit_ < 0 || leftInit_ < 0 || downInit_ >= height_ || rightInit_ >= width_) {
throw NotFoundException("Invalid dimensions WhiteRectangleDetector");
}
}
std::vector<Ref<ResultPoint> > WhiteRectangleDetector::detect() {
int left = leftInit_;
int right = rightInit_;
int up = upInit_;
int down = downInit_;
bool sizeExceeded = false;
bool aBlackPointFoundOnBorder = true;
bool atLeastOneBlackPointFoundOnBorder = false;
while (aBlackPointFoundOnBorder) {
aBlackPointFoundOnBorder = false;
bool rightBorderNotWhite = true;
while (rightBorderNotWhite && right < width_) {
rightBorderNotWhite = containsBlackPoint(up, down, right, false);
if (rightBorderNotWhite) {
right++;
aBlackPointFoundOnBorder = true;
}
}
if (right >= width_) {
sizeExceeded = true;
break;
}
bool bottomBorderNotWhite = true;
while (bottomBorderNotWhite && down < height_) {
bottomBorderNotWhite = containsBlackPoint(left, right, down, true);
if (bottomBorderNotWhite) {
down++;
aBlackPointFoundOnBorder = true;
}
}
if (down >= height_) {
sizeExceeded = true;
break;
}
bool leftBorderNotWhite = true;
while (leftBorderNotWhite && left >= 0) {
leftBorderNotWhite = containsBlackPoint(up, down, left, false);
if (leftBorderNotWhite) {
left--;
aBlackPointFoundOnBorder = true;
}
}
if (left < 0) {
sizeExceeded = true;
break;
}
bool topBorderNotWhite = true;
while (topBorderNotWhite && up >= 0) {
topBorderNotWhite = containsBlackPoint(left, right, up, true);
if (topBorderNotWhite) {
up--;
aBlackPointFoundOnBorder = true;
}
}
if (up < 0) {
sizeExceeded = true;
break;
}
if (aBlackPointFoundOnBorder) {
atLeastOneBlackPointFoundOnBorder = true;
}
}
if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) {
int maxSize = right - left;
Ref<ResultPoint> z(NULL);
for (int i = 1; i < maxSize; i++) {
z = getBlackPointOnSegment(left, down - i, left + i, down);
if (z != NULL) {
break;
}
}
if (z == NULL) {
throw NotFoundException("z == NULL");
}
Ref<ResultPoint> t(NULL);
for (int i = 1; i < maxSize; i++) {
t = getBlackPointOnSegment(left, up + i, left + i, up);
if (t != NULL) {
break;
}
}
if (t == NULL) {
throw NotFoundException("t == NULL");
}
Ref<ResultPoint> x(NULL);
for (int i = 1; i < maxSize; i++) {
x = getBlackPointOnSegment(right, up + i, right - i, up);
if (x != NULL) {
break;
}
}
if (x == NULL) {
throw NotFoundException("x == NULL");
}
Ref<ResultPoint> y(NULL);
for (int i = 1; i < maxSize; i++) {
y = getBlackPointOnSegment(right, down - i, right - i, down);
if (y != NULL) {
break;
}
}
if (y == NULL) {
throw NotFoundException("y == NULL");
}
return centerEdges(y, z, x, t);
} else {
throw NotFoundException("No black point found on border");
}
}
Ref<ResultPoint>
WhiteRectangleDetector::getBlackPointOnSegment(int aX_, int aY_, int bX_, int bY_) {
float aX = float(aX_), aY = float(aY_), bX = float(bX_), bY = float(bY_);
int dist = MathUtils::round(MathUtils::distance(aX, aY, bX, bY));
float xStep = (bX - aX) / dist;
float yStep = (bY - aY) / dist;
for (int i = 0; i < dist; i++) {
int x = MathUtils::round(aX + i * xStep);
int y = MathUtils::round(aY + i * yStep);
if (image_->get(x, y)) {
Ref<ResultPoint> point(new ResultPoint(float(x), float(y)));
return point;
}
}
Ref<ResultPoint> point(NULL);
return point;
}
vector<Ref<ResultPoint> > WhiteRectangleDetector::centerEdges(Ref<ResultPoint> y, Ref<ResultPoint> z,
Ref<ResultPoint> x, Ref<ResultPoint> t) {
float yi = y->getX();
float yj = y->getY();
float zi = z->getX();
float zj = z->getY();
float xi = x->getX();
float xj = x->getY();
float ti = t->getX();
float tj = t->getY();
std::vector<Ref<ResultPoint> > corners(4);
if (yi < (float)width_/2.0f) {
Ref<ResultPoint> pointA(new ResultPoint(ti - CORR, tj + CORR));
Ref<ResultPoint> pointB(new ResultPoint(zi + CORR, zj + CORR));
Ref<ResultPoint> pointC(new ResultPoint(xi - CORR, xj - CORR));
Ref<ResultPoint> pointD(new ResultPoint(yi + CORR, yj - CORR));
corners[0].reset(pointA);
corners[1].reset(pointB);
corners[2].reset(pointC);
corners[3].reset(pointD);
} else {
Ref<ResultPoint> pointA(new ResultPoint(ti + CORR, tj + CORR));
Ref<ResultPoint> pointB(new ResultPoint(zi + CORR, zj - CORR));
Ref<ResultPoint> pointC(new ResultPoint(xi - CORR, xj + CORR));
Ref<ResultPoint> pointD(new ResultPoint(yi - CORR, yj - CORR));
corners[0].reset(pointA);
corners[1].reset(pointB);
corners[2].reset(pointC);
corners[3].reset(pointD);
}
return corners;
}
bool WhiteRectangleDetector::containsBlackPoint(int a, int b, int fixed, bool horizontal) {
if (horizontal) {
for (int x = a; x <= b; x++) {
if (image_->get(x, fixed)) {
return true;
}
}
} else {
for (int y = a; y <= b; y++) {
if (image_->get(fixed, y)) {
return true;
}
}
}
return false;
}