Skip to content

Commit 75ab5aa

Browse files
committed
identify: use quirc_float_t constants and avoid divisions with same denom
This avoids back and forth conversions from int/double to float if quirc_float_t is float.
1 parent 542848d commit 75ab5aa

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

lib/identify.c

+34-34
Original file line numberDiff line numberDiff line change
@@ -79,30 +79,30 @@ static void perspective_setup(quirc_float_t *c,
7979
quirc_float_t x3 = rect[3].x;
8080
quirc_float_t y3 = rect[3].y;
8181

82-
quirc_float_t wden = w * (x2*y3 - x3*y2 + (x3-x2)*y1 + x1*(y2-y3));
83-
quirc_float_t hden = h * (x2*y3 + x1*(y2-y3) - x3*y2 + (x3-x2)*y1);
82+
quirc_float_t wden = (quirc_float_t)1 / (w * (x2*y3 - x3*y2 + (x3-x2)*y1 + x1*(y2-y3)));
83+
quirc_float_t hden = (quirc_float_t)1 / (h * (x2*y3 + x1*(y2-y3) - x3*y2 + (x3-x2)*y1));
8484

8585
c[0] = (x1*(x2*y3-x3*y2) + x0*(-x2*y3+x3*y2+(x2-x3)*y1) +
86-
x1*(x3-x2)*y0) / wden;
86+
x1*(x3-x2)*y0) * wden;
8787
c[1] = -(x0*(x2*y3+x1*(y2-y3)-x2*y1) - x1*x3*y2 + x2*x3*y1
88-
+ (x1*x3-x2*x3)*y0) / hden;
88+
+ (x1*x3-x2*x3)*y0) * hden;
8989
c[2] = x0;
9090
c[3] = (y0*(x1*(y3-y2)-x2*y3+x3*y2) + y1*(x2*y3-x3*y2) +
91-
x0*y1*(y2-y3)) / wden;
91+
x0*y1*(y2-y3)) * wden;
9292
c[4] = (x0*(y1*y3-y2*y3) + x1*y2*y3 - x2*y1*y3 +
93-
y0*(x3*y2-x1*y2+(x2-x3)*y1)) / hden;
93+
y0*(x3*y2-x1*y2+(x2-x3)*y1)) * hden;
9494
c[5] = y0;
95-
c[6] = (x1*(y3-y2) + x0*(y2-y3) + (x2-x3)*y1 + (x3-x2)*y0) / wden;
96-
c[7] = (-x2*y3 + x1*y3 + x3*y2 + x0*(y1-y2) - x3*y1 + (x2-x1)*y0) /
95+
c[6] = (x1*(y3-y2) + x0*(y2-y3) + (x2-x3)*y1 + (x3-x2)*y0) * wden;
96+
c[7] = (-x2*y3 + x1*y3 + x3*y2 + x0*(y1-y2) - x3*y1 + (x2-x1)*y0) *
9797
hden;
9898
}
9999

100100
static void perspective_map(const quirc_float_t *c,
101101
quirc_float_t u, quirc_float_t v, struct quirc_point *ret)
102102
{
103-
quirc_float_t den = c[6]*u + c[7]*v + 1.0;
104-
quirc_float_t x = (c[0]*u + c[1]*v + c[2]) / den;
105-
quirc_float_t y = (c[3]*u + c[4]*v + c[5]) / den;
103+
quirc_float_t den = (quirc_float_t)1 / (c[6]*u + c[7]*v + (quirc_float_t)1.0);
104+
quirc_float_t x = (c[0]*u + c[1]*v + c[2]) * den;
105+
quirc_float_t y = (c[3]*u + c[4]*v + c[5]) * den;
106106

107107
ret->x = (int) rint(x);
108108
ret->y = (int) rint(y);
@@ -114,12 +114,12 @@ static void perspective_unmap(const quirc_float_t *c,
114114
{
115115
quirc_float_t x = in->x;
116116
quirc_float_t y = in->y;
117-
quirc_float_t den = -c[0]*c[7]*y + c[1]*c[6]*y + (c[3]*c[7]-c[4]*c[6])*x +
118-
c[0]*c[4] - c[1]*c[3];
117+
quirc_float_t den = (quirc_float_t)1 / (-c[0]*c[7]*y + c[1]*c[6]*y + (c[3]*c[7]-c[4]*c[6])*x +
118+
c[0]*c[4] - c[1]*c[3]);
119119

120-
*u = -(c[1]*(y-c[5]) - c[2]*c[7]*y + (c[5]*c[7]-c[4])*x + c[2]*c[4]) /
120+
*u = -(c[1]*(y-c[5]) - c[2]*c[7]*y + (c[5]*c[7]-c[4])*x + c[2]*c[4]) *
121121
den;
122-
*v = (c[0]*(y-c[5]) - c[2]*c[6]*y + (c[5]*c[6]-c[3])*x + c[2]*c[3]) /
122+
*v = (c[0]*(y-c[5]) - c[2]*c[6]*y + (c[5]*c[6]-c[3])*x + c[2]*c[3]) *
123123
den;
124124
}
125125

@@ -303,16 +303,16 @@ static uint8_t otsu(const struct quirc *q)
303303
}
304304

305305
// Calculate weighted sum of histogram values
306-
quirc_float_t sum = 0;
306+
quirc_float_t sum = (quirc_float_t)0;
307307
unsigned int i = 0;
308308
for (i = 0; i <= UINT8_MAX; ++i) {
309309
sum += i * histogram[i];
310310
}
311311

312312
// Compute threshold
313-
quirc_float_t sumB = 0;
313+
quirc_float_t sumB = (quirc_float_t)0;
314314
unsigned int q1 = 0;
315-
quirc_float_t max = 0;
315+
quirc_float_t max = (quirc_float_t)0;
316316
uint8_t threshold = 0;
317317
for (i = 0; i <= UINT8_MAX; ++i) {
318318
// Weighted background
@@ -595,9 +595,9 @@ static void find_alignment_pattern(struct quirc *q, int index)
595595
* can estimate its size.
596596
*/
597597
perspective_unmap(c0->c, &b, &u, &v);
598-
perspective_map(c0->c, u, v + 1.0, &a);
598+
perspective_map(c0->c, u, v + (quirc_float_t)1.0, &a);
599599
perspective_unmap(c2->c, &b, &u, &v);
600-
perspective_map(c2->c, u + 1.0, v, &c);
600+
perspective_map(c2->c, u + (quirc_float_t)1.0, v, &c);
601601

602602
size_estimate = abs((a.x - b.x) * -(c.y - b.y) +
603603
(a.y - b.y) * (c.x - b.x));
@@ -670,16 +670,16 @@ static void measure_grid_size(struct quirc *q, int index)
670670

671671
quirc_float_t ab = length(b->corners[0], a->corners[3]);
672672
quirc_float_t capstone_ab_size = (length(b->corners[0], b->corners[3]) + length(a->corners[0], a->corners[3]))/2.0;
673-
quirc_float_t ver_grid = 7.0 * ab / capstone_ab_size;
673+
quirc_float_t ver_grid = (quirc_float_t)7.0 * ab / capstone_ab_size;
674674

675675
quirc_float_t bc = length(b->corners[0], c->corners[1]);
676676
quirc_float_t capstone_bc_size = (length(b->corners[0], b->corners[1]) + length(c->corners[0], c->corners[1]))/2.0;
677-
quirc_float_t hor_grid = 7.0 * bc / capstone_bc_size;
678-
679-
quirc_float_t grid_size_estimate = (ver_grid + hor_grid) / 2;
677+
quirc_float_t hor_grid = (quirc_float_t)7.0 * bc / capstone_bc_size;
678+
679+
quirc_float_t grid_size_estimate = (ver_grid + hor_grid) * (quirc_float_t)(0.5);
680+
681+
int ver = (int)((grid_size_estimate - (quirc_float_t)(17.0 - 2.0)) * (quirc_float_t)(1./4));
680682

681-
int ver = (int)((grid_size_estimate - 17.0 + 2.0) / 4.0);
682-
683683
qr->grid_size = 4*ver + 17;
684684
}
685685

@@ -692,7 +692,7 @@ static int read_cell(const struct quirc *q, int index, int x, int y)
692692
const struct quirc_grid *qr = &q->grids[index];
693693
struct quirc_point p;
694694

695-
perspective_map(qr->c, x + 0.5, y + 0.5, &p);
695+
perspective_map(qr->c, x + (quirc_float_t)0.5, y + (quirc_float_t)0.5, &p);
696696
if (p.y < 0 || p.y >= q->h || p.x < 0 || p.x >= q->w)
697697
return 0;
698698

@@ -814,7 +814,7 @@ static void jiggle_perspective(struct quirc *q, int index)
814814
int i;
815815

816816
for (i = 0; i < 8; i++)
817-
adjustments[i] = qr->c[i] * 0.02;
817+
adjustments[i] = qr->c[i] * (quirc_float_t)0.02;
818818

819819
for (pass = 0; pass < 5; pass++) {
820820
for (i = 0; i < 16; i++) {
@@ -1019,8 +1019,8 @@ static void test_neighbours(struct quirc *q, int i,
10191019
const struct neighbour *hn = &hlist->n[j];
10201020
for (int k = 0; k < vlist->count; k++) {
10211021
const struct neighbour *vn = &vlist->n[k];
1022-
quirc_float_t squareness = fabs(1.0 - hn->distance / vn->distance);
1023-
if (squareness < 0.2)
1022+
quirc_float_t squareness = fabs((quirc_float_t)1.0 - hn->distance / vn->distance);
1023+
if (squareness < (quirc_float_t)0.2)
10241024
record_qr_grid(q, hn->index, i, vn->index);
10251025
}
10261026
}
@@ -1048,17 +1048,17 @@ static void test_grouping(struct quirc *q, unsigned int i)
10481048

10491049
perspective_unmap(c1->c, &c2->center, &u, &v);
10501050

1051-
u = fabs(u - 3.5);
1052-
v = fabs(v - 3.5);
1051+
u = fabs(u - (quirc_float_t)3.5);
1052+
v = fabs(v - (quirc_float_t)3.5);
10531053

1054-
if (u < 0.2 * v) {
1054+
if (u < (quirc_float_t)0.2 * v) {
10551055
struct neighbour *n = &hlist.n[hlist.count++];
10561056

10571057
n->index = j;
10581058
n->distance = v;
10591059
}
10601060

1061-
if (v < 0.2 * u) {
1061+
if (v < (quirc_float_t)0.2 * u) {
10621062
struct neighbour *n = &vlist.n[vlist.count++];
10631063

10641064
n->index = j;

0 commit comments

Comments
 (0)