finder_scan: Improve capstone detection on small images

When using small captured images, I somehow frequently see failures
to recognize a capstone due to rounding errors.
eg. when pb[] = {2 3 8 3 2}.

This commit tries to improve it by using fixed-point arithmetics.
The scaling factor was chosen somehow arbitrary. A moderate scaling
should be enough.
This commit is contained in:
YAMAMOTO Takashi 2021-04-23 10:06:50 +09:00
parent 734258c775
commit 65379a7c39

View file

@ -438,17 +438,18 @@ static void finder_scan(struct quirc *q, unsigned int y)
run_count++;
if (!color && run_count >= 5) {
const int scale = 16;
static const unsigned int check[5] = {1, 1, 3, 1, 1};
unsigned int avg, err;
unsigned int i;
int ok = 1;
avg = (pb[0] + pb[1] + pb[3] + pb[4]) / 4;
avg = (pb[0] + pb[1] + pb[3] + pb[4]) * scale / 4;
err = avg * 3 / 4;
for (i = 0; i < 5; i++)
if (pb[i] < check[i] * avg - err ||
pb[i] > check[i] * avg + err)
if (pb[i] * scale < check[i] * avg - err ||
pb[i] * scale > check[i] * avg + err)
ok = 0;
if (ok)