From ceb1241735e10d464d492883646d8491209028f4 Mon Sep 17 00:00:00 2001 From: Jaap Haitsma Date: Tue, 26 May 2020 21:29:28 -0400 Subject: [PATCH 1/4] Add vscode settings --- .vscode/launch.json | 28 +++++++++++++++++++++++ .vscode/settings.json | 6 +++++ .vscode/tasks.json | 52 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..a204a31 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/inspect", + "args": ["../Downloads/img3.jpg"], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..931aad5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "*.idl": "cpp" + }, + "C_Cpp.errorSquiggles": "Enabled" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..dc790be --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,52 @@ +{ + "version": "2.0.0", + "command": "make", + "type": "shell", + "presentation": { + "reveal": "always" + }, + // Use the standard less compilation problem matcher. + "problemMatcher": { + "owner": "cpp", + "fileLocation": ["relative", "${workspaceRoot}"], + "pattern": { + "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", + "file": 1, + "line": 2, + "column": 3, + "severity": 4, + "message": 5 + } + }, + + "tasks": [ + { + "label": "Debug build", + "group": { + "kind": "build", + "isDefault": true + }, + + "options": { + "env": { + "CFLAGS": "-g -Wall -fPIC", + } + }, + "args": ["all"], + }, + { + "label": "Release build", + + "options": { + "env": { + "CFLAGS": "-O3 -Wall -fPIC", + } + }, + "args": ["all"] + }, + { + "label": "Clean", + "args": ["clean"] + }, + ] +} \ No newline at end of file From bff065d88b8ab3981925e6123537384321b02cad Mon Sep 17 00:00:00 2001 From: Jaap Haitsma Date: Wed, 27 May 2020 21:33:51 -0400 Subject: [PATCH 2/4] Try all possible groups and new algo for grid_size --- .vscode/launch.json | 2 +- lib/identify.c | 91 +++++++++++++++++++++++++------------------- lib/quirc_internal.h | 4 +- 3 files changed, 54 insertions(+), 43 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index a204a31..5165a29 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/inspect", - "args": ["../Downloads/img3.jpg"], + "args": ["../../Downloads/img3-3.jpg"], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], diff --git a/lib/identify.c b/lib/identify.c index fa409d7..c8e2ef3 100644 --- a/lib/identify.c +++ b/lib/identify.c @@ -553,7 +553,8 @@ static int timing_scan(const struct quirc *q, int nondom_step; int a = 0; int i; - int run_length = 0; + int zero_run_length = 0; + int non_zero_run_length = 0; int count = 0; if (p0->x < 0 || p0->y < 0 || p0->x >= q->w || p0->y >= q->h) @@ -599,11 +600,14 @@ static int timing_scan(const struct quirc *q, pixel = q->pixels[y * q->w + x]; if (pixel) { - if (run_length >= 2) + non_zero_run_length++; + if ((zero_run_length >= 2) && (non_zero_run_length >= 2)) { count++; - run_length = 0; + zero_run_length = 0; + } } else { - run_length++; + zero_run_length++; + non_zero_run_length = 0; } a += n; @@ -617,6 +621,32 @@ static int timing_scan(const struct quirc *q, return count; } +static double distance(struct quirc_point a, struct quirc_point b) +{ + return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)); +} + +static void measure_grid_size(struct quirc *q, int index) +{ + struct quirc_grid *qr = &q->grids[index]; + + struct quirc_capstone *a = &(q->capstones[qr->caps[0]]); + struct quirc_capstone *b = &(q->capstones[qr->caps[1]]); + struct quirc_capstone *c = &(q->capstones[qr->caps[2]]); + + double ab = distance(b->corners[0], a->corners[3]); + double capstone_ab_size = (distance(b->corners[0], b->corners[3]) + distance(a->corners[0], a->corners[3]))/2.0; + double ver_grid = 7.0 * ab / capstone_ab_size; + + double bc = distance(b->corners[0], c->corners[1]); + double capstone_bc_size = (distance(b->corners[0], b->corners[1]) + distance(c->corners[0], c->corners[1]))/2.0; + double hor_grid = 7.0 * bc / capstone_bc_size; + + double grid_size_estimate = (ver_grid + hor_grid) / 2; + + qr->grid_size = 4*((int)(grid_size_estimate - 17.0 + 2.0) / 4) + 17; +} + /* Try the measure the timing pattern for a given QR code. This does * not require the global perspective to have been set up, but it * does require that the capstone corners have been set to their @@ -641,13 +671,13 @@ static int measure_timing_pattern(struct quirc *q, int index) perspective_map(cap->c, us[i], vs[i], &qr->tpep[i]); } + + int hscan = timing_scan(q, &qr->tpep[1], &qr->tpep[2]); + int vscan = timing_scan(q, &qr->tpep[1], &qr->tpep[0]); - qr->hscan = timing_scan(q, &qr->tpep[1], &qr->tpep[2]); - qr->vscan = timing_scan(q, &qr->tpep[1], &qr->tpep[0]); - - scan = qr->hscan; - if (qr->vscan > scan) - scan = qr->vscan; + scan = hscan; + if (vscan > scan) + scan = vscan; /* If neither scan worked, we can't go any further. */ if (scan < 0) @@ -925,9 +955,10 @@ static void record_qr_grid(struct quirc *q, int a, int b, int c) /* Check the timing pattern. This doesn't require a perspective * transform. */ - if (measure_timing_pattern(q, qr_index) < 0) - goto fail; - + //if (measure_timing_pattern(q, qr_index) < 0) + // goto fail; + + measure_grid_size(q, qr_index); /* Make an estimate based for the alignment pattern based on extending * lines from capstones A and C. */ @@ -994,31 +1025,16 @@ static void test_neighbours(struct quirc *q, int i, const struct neighbour_list *hlist, const struct neighbour_list *vlist) { - int j, k; - double best_score = 0.0; - int best_h = -1, best_v = -1; - /* Test each possible grouping */ - for (j = 0; j < hlist->count; j++) - for (k = 0; k < vlist->count; k++) { - const struct neighbour *hn = &hlist->n[j]; + for (int j = 0; j < hlist->count; j++) { + const struct neighbour *hn = &hlist->n[j]; + for (int k = 0; k < vlist->count; k++) { const struct neighbour *vn = &vlist->n[k]; - double score = fabs(1.0 - hn->distance / vn->distance); - - if (score > 2.5) - continue; - - if (best_h < 0 || score < best_score) { - best_h = hn->index; - best_v = vn->index; - best_score = score; - } + double squareness = fabs(1.0 - hn->distance / vn->distance); + if (squareness < 0.2) + record_qr_grid(q, hn->index, i, vn->index); } - - if (best_h < 0 || best_v < 0) - return; - - record_qr_grid(q, best_h, i, best_v); + } } static void test_grouping(struct quirc *q, int i) @@ -1028,9 +1044,6 @@ static void test_grouping(struct quirc *q, int i) struct neighbour_list hlist; struct neighbour_list vlist; - if (c1->qr_grid >= 0) - return; - hlist.count = 0; vlist.count = 0; @@ -1041,7 +1054,7 @@ static void test_grouping(struct quirc *q, int i) struct quirc_capstone *c2 = &q->capstones[j]; double u, v; - if (i == j || c2->qr_grid >= 0) + if (i == j) continue; perspective_unmap(c1->c, &c2->center, &u, &v); diff --git a/lib/quirc_internal.h b/lib/quirc_internal.h index a9a54ab..2d49a6b 100644 --- a/lib/quirc_internal.h +++ b/lib/quirc_internal.h @@ -27,7 +27,7 @@ #define QUIRC_MAX_REGIONS 254 #endif #define QUIRC_MAX_CAPSTONES 32 -#define QUIRC_MAX_GRIDS 8 +#define QUIRC_MAX_GRIDS (QUIRC_MAX_CAPSTONES * 2) #define QUIRC_PERSPECTIVE_PARAMS 8 @@ -68,8 +68,6 @@ struct quirc_grid { /* Timing pattern endpoints */ struct quirc_point tpep[3]; - int hscan; - int vscan; /* Grid size and perspective transform */ int grid_size; From af9966c986894a3d6e1b9481bda673bbe23fc175 Mon Sep 17 00:00:00 2001 From: Jaap Haitsma Date: Wed, 27 May 2020 21:38:01 -0400 Subject: [PATCH 3/4] Remove .vscode files --- .vscode/launch.json | 28 ----------------------- .vscode/settings.json | 6 ----- .vscode/tasks.json | 52 ------------------------------------------- 3 files changed, 86 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/settings.json delete mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 5165a29..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "(gdb) Launch", - "type": "cppdbg", - "request": "launch", - "program": "${workspaceFolder}/inspect", - "args": ["../../Downloads/img3-3.jpg"], - "stopAtEntry": false, - "cwd": "${workspaceFolder}", - "environment": [], - "externalConsole": false, - "MIMode": "gdb", - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ] - } - - ] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 931aad5..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "files.associations": { - "*.idl": "cpp" - }, - "C_Cpp.errorSquiggles": "Enabled" -} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index dc790be..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,52 +0,0 @@ -{ - "version": "2.0.0", - "command": "make", - "type": "shell", - "presentation": { - "reveal": "always" - }, - // Use the standard less compilation problem matcher. - "problemMatcher": { - "owner": "cpp", - "fileLocation": ["relative", "${workspaceRoot}"], - "pattern": { - "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", - "file": 1, - "line": 2, - "column": 3, - "severity": 4, - "message": 5 - } - }, - - "tasks": [ - { - "label": "Debug build", - "group": { - "kind": "build", - "isDefault": true - }, - - "options": { - "env": { - "CFLAGS": "-g -Wall -fPIC", - } - }, - "args": ["all"], - }, - { - "label": "Release build", - - "options": { - "env": { - "CFLAGS": "-O3 -Wall -fPIC", - } - }, - "args": ["all"] - }, - { - "label": "Clean", - "args": ["clean"] - }, - ] -} \ No newline at end of file From 00f187d94a6bc20bfce0d7e85f3e0d31e6212814 Mon Sep 17 00:00:00 2001 From: Jaap Haitsma Date: Wed, 16 Jun 2021 22:18:15 -0400 Subject: [PATCH 4/4] Remove unused functions --- lib/identify.c | 138 ++----------------------------------------------- 1 file changed, 4 insertions(+), 134 deletions(-) diff --git a/lib/identify.c b/lib/identify.c index c8e2ef3..88b7948 100644 --- a/lib/identify.c +++ b/lib/identify.c @@ -537,95 +537,12 @@ static void find_leftmost_to_line(void *user_data, int y, int left, int right) } } -/* Do a Bresenham scan from one point to another and count the number - * of black/white transitions. - */ -static int timing_scan(const struct quirc *q, - const struct quirc_point *p0, - const struct quirc_point *p1) -{ - int n = p1->x - p0->x; - int d = p1->y - p0->y; - int x = p0->x; - int y = p0->y; - int *dom, *nondom; - int dom_step; - int nondom_step; - int a = 0; - int i; - int zero_run_length = 0; - int non_zero_run_length = 0; - int count = 0; - - if (p0->x < 0 || p0->y < 0 || p0->x >= q->w || p0->y >= q->h) - return -1; - if (p1->x < 0 || p1->y < 0 || p1->x >= q->w || p1->y >= q->h) - return -1; - - if (abs(n) > abs(d)) { - int swap = n; - - n = d; - d = swap; - - dom = &x; - nondom = &y; - } else { - dom = &y; - nondom = &x; - } - - if (n < 0) { - n = -n; - nondom_step = -1; - } else { - nondom_step = 1; - } - - if (d < 0) { - d = -d; - dom_step = -1; - } else { - dom_step = 1; - } - - x = p0->x; - y = p0->y; - for (i = 0; i <= d; i++) { - int pixel; - - if (y < 0 || y >= q->h || x < 0 || x >= q->w) - break; - - pixel = q->pixels[y * q->w + x]; - - if (pixel) { - non_zero_run_length++; - if ((zero_run_length >= 2) && (non_zero_run_length >= 2)) { - count++; - zero_run_length = 0; - } - } else { - zero_run_length++; - non_zero_run_length = 0; - } - - a += n; - *dom += dom_step; - if (a >= d) { - *nondom += nondom_step; - a -= d; - } - } - - return count; -} - static double distance(struct quirc_point a, struct quirc_point b) { - return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)); + return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)); } - +/* Estimate grid size by determing distance between capstones + */ static void measure_grid_size(struct quirc *q, int index) { struct quirc_grid *qr = &q->grids[index]; @@ -647,50 +564,6 @@ static void measure_grid_size(struct quirc *q, int index) qr->grid_size = 4*((int)(grid_size_estimate - 17.0 + 2.0) / 4) + 17; } -/* Try the measure the timing pattern for a given QR code. This does - * not require the global perspective to have been set up, but it - * does require that the capstone corners have been set to their - * canonical rotation. - * - * For each capstone, we find a point in the middle of the ring band - * which is nearest the centre of the code. Using these points, we do - * a horizontal and a vertical timing scan. - */ -static int measure_timing_pattern(struct quirc *q, int index) -{ - struct quirc_grid *qr = &q->grids[index]; - int i; - int scan; - int ver; - int size; - - for (i = 0; i < 3; i++) { - static const double us[] = {6.5, 6.5, 0.5}; - static const double vs[] = {0.5, 6.5, 6.5}; - struct quirc_capstone *cap = &q->capstones[qr->caps[i]]; - - perspective_map(cap->c, us[i], vs[i], &qr->tpep[i]); - } - - int hscan = timing_scan(q, &qr->tpep[1], &qr->tpep[2]); - int vscan = timing_scan(q, &qr->tpep[1], &qr->tpep[0]); - - scan = hscan; - if (vscan > scan) - scan = vscan; - - /* If neither scan worked, we can't go any further. */ - if (scan < 0) - return -1; - - /* Choose the nearest allowable grid size */ - size = scan * 2 + 13; - ver = (size - 15) / 4; - qr->grid_size = ver * 4 + 17; - - return 0; -} - /* Read a cell from a grid using the currently set perspective * transform. Returns +/- 1 for black/white, 0 for cells which are * out of image bounds. @@ -952,12 +825,9 @@ static void record_qr_grid(struct quirc *q, int a, int b, int c) cap->qr_grid = qr_index; } - /* Check the timing pattern. This doesn't require a perspective + /* Check the timing pattern by measuring grid size. This doesn't require a perspective * transform. */ - //if (measure_timing_pattern(q, qr_index) < 0) - // goto fail; - measure_grid_size(q, qr_index); /* Make an estimate based for the alignment pattern based on extending * lines from capstones A and C.