From 65379a7c3966b202f3f223769652e6c01b7cd28e Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 23 Apr 2021 10:06:50 +0900 Subject: [PATCH] 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. --- lib/identify.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/identify.c b/lib/identify.c index bddd3c6..7a1096e 100644 --- a/lib/identify.c +++ b/lib/identify.c @@ -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)