qrcode-barcode-scanner 0.12.3

QR code decoding and scannability scoring for artistic, AI-generated, and photo-captured QR codes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Perspective geometry + the module-space grid sampler.
//!
//! A 3×3 projective homography maps the unit square (module space) onto the
//! detected symbol quad; `sample_bilinear` reads the luma under it. The
//! [`GridSampler`] wraps the two into "sample the center of module (mx, my)" —
//! the workhorse the structural checks, the ISO grade card, and the artistic
//! rescue confidence pass all sample through.
//!
//! Bounds convention (quirc heritage, source-verified rqrr 0.10.1
//! `prepare.rs:243`): the detection bounds span `grid_size + 1` grid cells, so
//! module (mx, my) of an N-wide symbol has its center at unit
//! `((mx+0.5)/(N+1), (my+0.5)/(N+1))`. All deterministic f32 math.

use crate::input::LumaImage;
use crate::report::Point;

/// Row-major 3×3 projective transform mapping source → destination.
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct Homography(pub [f32; 9]);

impl Homography {
    /// Identity transform (test geometry baseline).
    #[cfg(test)]
    pub(crate) fn identity() -> Self {
        Self([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0])
    }

    /// Apply to a point. Returns `None` when the point projects to infinity.
    pub(crate) fn apply(&self, x: f32, y: f32) -> Option<(f32, f32)> {
        let m = &self.0;
        let w = m[6] * x + m[7] * y + m[8];
        if w.abs() < f32::EPSILON {
            return None;
        }
        Some((
            (m[0] * x + m[1] * y + m[2]) / w,
            (m[3] * x + m[4] * y + m[5]) / w,
        ))
    }

    /// Inverse transform (adjugate / determinant). `None` when singular.
    pub(crate) fn inverse(&self) -> Option<Self> {
        let m = &self.0;
        let det = m[0] * (m[4] * m[8] - m[5] * m[7]) - m[1] * (m[3] * m[8] - m[5] * m[6])
            + m[2] * (m[3] * m[7] - m[4] * m[6]);
        if det.abs() < 1e-12 {
            return None;
        }
        let inv_det = 1.0 / det;
        Some(Self([
            (m[4] * m[8] - m[5] * m[7]) * inv_det,
            (m[2] * m[7] - m[1] * m[8]) * inv_det,
            (m[1] * m[5] - m[2] * m[4]) * inv_det,
            (m[5] * m[6] - m[3] * m[8]) * inv_det,
            (m[0] * m[8] - m[2] * m[6]) * inv_det,
            (m[2] * m[3] - m[0] * m[5]) * inv_det,
            (m[3] * m[7] - m[4] * m[6]) * inv_det,
            (m[1] * m[6] - m[0] * m[7]) * inv_det,
            (m[0] * m[4] - m[1] * m[3]) * inv_det,
        ]))
    }

    /// Homography mapping the unit square (0,0)(1,0)(1,1)(0,1) onto a quad
    /// given clockwise from top-left. The grid-sampling workhorse: unit
    /// coordinates = module space, quad = detected symbol corners.
    pub(crate) fn unit_square_to_quad(quad: [(f32, f32); 4]) -> Option<Self> {
        let [(x0, y0), (x1, y1), (x2, y2), (x3, y3)] = quad;
        let dx1 = x1 - x2;
        let dx2 = x3 - x2;
        let dy1 = y1 - y2;
        let dy2 = y3 - y2;
        let sx = x0 - x1 + x2 - x3;
        let sy = y0 - y1 + y2 - y3;
        let den = dx1 * dy2 - dx2 * dy1;
        if den.abs() < 1e-12 {
            return None;
        }
        let g = (sx * dy2 - dx2 * sy) / den;
        let h = (dx1 * sy - sx * dy1) / den;
        Some(Self([
            x1 - x0 + g * x1,
            x3 - x0 + h * x3,
            x0,
            y1 - y0 + g * y1,
            y3 - y0 + h * y3,
            y0,
            g,
            h,
            1.0,
        ]))
    }
}

/// Bilinear sample with white (255) outside — QR quiet-zone-like background.
#[expect(
    clippy::cast_possible_truncation,
    reason = "floor() output bounded by the f32 pixel-coordinate domain"
)]
pub(crate) fn sample_bilinear(img: &LumaImage, x: f32, y: f32) -> u8 {
    let x0 = x.floor();
    let y0 = y.floor();
    let fx = x - x0;
    let fy = y - y0;
    let (ix, iy) = (x0 as i64, y0 as i64);

    let [top_left, top_right, bottom_left, bottom_right] =
        interior(img, ix, iy).unwrap_or_else(|| bordered(img, ix, iy));
    let top = f32::from(top_left) * (1.0 - fx) + f32::from(top_right) * fx;
    let bottom = f32::from(bottom_left) * (1.0 - fx) + f32::from(bottom_right) * fx;
    #[expect(
        clippy::cast_possible_truncation,
        clippy::cast_sign_loss,
        reason = "clamped to 0.0..=255.0"
    )]
    {
        (top * (1.0 - fy) + bottom * fy).clamp(0.0, 255.0) as u8
    }
}

/// The pixels at `(ix, iy)`, `(ix + 1, iy)`, `(ix, iy + 1)` and
/// `(ix + 1, iy + 1)` when all four are in the image, read as two pairs
/// from two rows. `None` on the border and outside, where [`bordered`]
/// answers. Almost every sample of a stress warp is an interior one: this
/// reader spares it the four separate bounds tests of [`bordered`].
fn interior(img: &LumaImage, ix: i64, iy: i64) -> Option<[u8; 4]> {
    let (width, height) = (img.width() as usize, img.height() as usize);
    let (ix, iy) = (usize::try_from(ix).ok()?, usize::try_from(iy).ok()?);
    if ix + 1 >= width || iy + 1 >= height {
        return None;
    }
    let data = img.data();
    let top = iy * width + ix;
    let bottom = top + width;
    let (top, bottom) = (&data[top..top + 2], &data[bottom..bottom + 2]);
    Some([top[0], top[1], bottom[0], bottom[1]])
}

/// The same four pixels with white (255) for each one outside the image.
#[cold]
fn bordered(img: &LumaImage, ix: i64, iy: i64) -> [u8; 4] {
    let (w, h) = (i64::from(img.width()), i64::from(img.height()));
    let fetch = |px: i64, py: i64| -> u8 {
        if px < 0 || py < 0 || px >= w || py >= h {
            255
        } else {
            #[expect(
                clippy::cast_possible_truncation,
                clippy::cast_sign_loss,
                reason = "bounds-checked above against u32-sized dimensions"
            )]
            let idx = (py as usize) * (img.width() as usize) + (px as usize);
            img.data()[idx]
        }
    };
    [
        fetch(ix, iy),
        fetch(ix + 1, iy),
        fetch(ix, iy + 1),
        fetch(ix + 1, iy + 1),
    ]
}

/// Module-space sampler over a detected symbol.
pub(crate) struct GridSampler<'a> {
    img: &'a LumaImage,
    unit_to_image: Homography,
    modules: u32,
}

impl<'a> GridSampler<'a> {
    /// Build from detection corners (clockwise from top-left) + module count.
    pub(crate) fn new(img: &'a LumaImage, corners: [Point; 4], modules: u32) -> Option<Self> {
        let quad = corners.map(|p| (p.x, p.y));
        Some(Self {
            img,
            unit_to_image: Homography::unit_square_to_quad(quad)?,
            modules,
        })
    }

    /// Sample the center of module (mx, my). Off-grid coordinates are legal
    /// (negative / ≥ modules) — they probe the quiet zone.
    #[expect(
        clippy::cast_precision_loss,
        reason = "module indices ≤ 177, exact in f32"
    )]
    pub(crate) fn module(&self, mx: i32, my: i32) -> Option<u8> {
        // rqrr bounds cover N+1 grid cells (quirc heritage) — see module docs.
        let denom = self.modules as f32 + 1.0;
        let (ux, uy) = ((mx as f32 + 0.5) / denom, (my as f32 + 0.5) / denom);
        let (x, y) = self.unit_to_image.apply(ux, uy)?;
        Some(sample_bilinear(self.img, x, y))
    }
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::cast_precision_loss,
        clippy::float_cmp
    )]

    use super::*;

    fn checker(side: u32) -> LumaImage {
        let data = (0..side * side)
            .map(|i| {
                let (x, y) = (i % side, i / side);
                if (x + y) % 2 == 0 { 0 } else { 255 }
            })
            .collect();
        LumaImage::new(data, side, side)
    }

    #[test]
    fn homography_inverse_roundtrips() {
        let h = Homography::unit_square_to_quad([(2.0, 1.0), (10.0, 0.5), (11.0, 9.0), (1.5, 8.0)])
            .unwrap();
        let inv = h.inverse().unwrap();
        for (x, y) in [(0.0, 0.0), (0.5, 0.5), (1.0, 0.0), (0.25, 0.75)] {
            let (fx, fy) = h.apply(x, y).unwrap();
            let (bx, by) = inv.apply(fx, fy).unwrap();
            assert!((bx - x).abs() < 1e-4 && (by - y).abs() < 1e-4, "({x},{y})");
        }
    }

    #[test]
    fn unit_square_maps_corners_onto_quad() {
        let quad = [(3.0, 2.0), (20.0, 3.0), (19.0, 22.0), (2.0, 20.0)];
        let h = Homography::unit_square_to_quad(quad).unwrap();
        let corners = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)];
        for (unit, expected) in corners.iter().zip(quad) {
            let (x, y) = h.apply(unit.0, unit.1).unwrap();
            assert!(
                (x - expected.0).abs() < 1e-3 && (y - expected.1).abs() < 1e-3,
                "unit {unit:?} → ({x},{y}) expected {expected:?}"
            );
        }
    }

    #[test]
    fn bilinear_weights_average_the_checker() {
        // 2×2 checker: (0,0)=0 (1,0)=255 (0,1)=255 (1,1)=0
        let img = checker(2);
        // exact pixel centers read the pixel back
        assert_eq!(sample_bilinear(&img, 0.0, 0.0), 0);
        assert_eq!(sample_bilinear(&img, 1.0, 0.0), 255);
        // the shared midpoint averages all four: (0+255+255+0)/4 = 127.5 → 127
        assert_eq!(sample_bilinear(&img, 0.5, 0.5), 127);
        // quarter along the top row pins the fx weights: 0·0.75 + 255·0.25
        // = 63.75 → 63 (a +/- or */÷ weight swap misses this)
        assert_eq!(sample_bilinear(&img, 0.25, 0.0), 63);
    }

    // ---- direct pins for `inverse` + `apply` -------------------------------
    // The roundtrip test above exercises `inverse` only through `apply`, which
    // is PROJECTIVELY SCALE-INVARIANT: multiplying the whole inverse matrix by
    // any nonzero constant leaves every `apply` result unchanged. So every
    // mutation of the determinant (a global scale on the adjugate) survived a
    // roundtrip test. These tests assert the matrix ELEMENTS directly, making
    // the exact determinant and adjugate load-bearing.

    #[test]
    fn inverse_matches_hand_computed_adjugate() {
        // Distinct primes in every slot — no entry is 0 or ±1, so every
        // sub-product/-difference in the determinant is discriminated (a `*`→`/`
        // or `*`→`+` swap always changes the value).
        let h = Homography([2.0, 3.0, 5.0, 7.0, 11.0, 13.0, 17.0, 19.0, 29.0]);
        // det = 2·(11·29 − 13·19) − 3·(7·29 − 13·17) + 5·(7·19 − 11·17)
        //     = 2·72 − 3·(−18) + 5·(−54) = 144 + 54 − 270 = −72
        // inverse = (adjugateᵀ) / det, adjugateᵀ = [72,8,−16,18,−27,9,−54,13,1]:
        let expected: [f32; 9] = [
            -1.0,         // 72  / −72
            -1.0 / 9.0,   // 8   / −72
            2.0 / 9.0,    // −16 / −72
            -0.25,        // 18  / −72
            0.375,        // −27 / −72
            -0.125,       // 9   / −72
            0.75,         // −54 / −72
            -13.0 / 72.0, // 13  / −72
            -1.0 / 72.0,  // 1   / −72
        ];
        let inv = h.inverse().expect("nonsingular");
        for (k, (&got, want)) in inv.0.iter().zip(expected).enumerate() {
            assert!(
                (got - want).abs() < 1e-4,
                "inv[{k}] = {got}, want {want} (a wrong det rescales EVERY element)"
            );
        }
    }

    #[test]
    fn inverse_of_singular_is_none() {
        // rows 1 and 2 are linearly dependent (row1 = 2·row0) → det = 0. The
        // guard `det.abs() < 1e-12` must refuse; a `<`→`==` swap would slip a
        // 1/0 = ∞ matrix through instead of returning None.
        let singular = Homography([1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 1.0, 1.0, 1.0]);
        assert!(singular.inverse().is_none());
    }

    #[test]
    fn apply_projecting_to_infinity_returns_none() {
        // m[6]=1, m[7]=0, m[8]=0 ⇒ w = x. At x=0 the point projects to
        // infinity (w=0) → None; the `w.abs() < EPSILON` guard is what catches
        // it (a `<`→`==` swap would divide by zero instead).
        let h = Homography([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0]);
        assert!(h.apply(0.0, 5.0).is_none());
        // a finite point still maps: w = 2 → (2/2, 5/2)
        assert_eq!(h.apply(2.0, 5.0), Some((1.0, 2.5)));
    }

    // ---- exact-boundary pins for the three float-epsilon guards -------------
    // These guards are STRICT `<`, so they only differ from `<=`/`==` when the
    // magnitude equals the bound EXACTLY. f32 arithmetic is exact on small
    // integers and powers of two, so an EPSILON / 1e-12 magnitude IS
    // constructible (a diagonal product X·1·1 leaves X untouched).

    #[test]
    fn apply_at_exactly_epsilon_w_still_projects() {
        // sampler 32:20 `w.abs() < f32::EPSILON` — w = m6·x + m7·y + m8. With
        // m6=m7=0 and m8=EPSILON, w == f32::EPSILON EXACTLY for any (x,y). The
        // strict `<` treats this as finite (projects); a `<`→`<=` swap returns
        // None (divide-guard fires).
        let h = Homography([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, f32::EPSILON]);
        let got = h.apply(2.0, 4.0);
        assert!(got.is_some(), "w == EPSILON is finite under strict `<`");
        assert_eq!(got.unwrap(), (2.0 / f32::EPSILON, 4.0 / f32::EPSILON));
    }

    #[test]
    fn inverse_at_exactly_the_determinant_threshold_still_inverts() {
        // sampler 46:22 `det.abs() < 1e-12` — det = X·(1·1) with X = 1e-12_f32
        // (same literal the guard uses), so det == 1e-12 EXACTLY. Strict `<`
        // keeps it non-singular; `<`→`<=` would reject it as singular (None).
        let h = Homography([1e-12, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]);
        assert!(
            h.inverse().is_some(),
            "det == 1e-12 is non-singular under the strict `<` guard"
        );
    }

    #[test]
    fn unit_square_to_quad_at_exactly_the_den_threshold_still_builds() {
        // sampler 75:22 `den.abs() < 1e-12`, two mutants: `<`→`<=` and `<`→`==`.
        // den = dx1·dy2 − dx2·dy1. With x2=y2=0, x1=1e-12,y1=0, x3=0,y3=1:
        // dx1=1e-12, dy2=1, dx2=0, dy1=0 → den == 1e-12 EXACTLY. Strict `<` is
        // false → Some; BOTH `<=` (true) and `==` (true) return None.
        let quad = [(1.0, 1.0), (1e-12, 0.0), (0.0, 0.0), (0.0, 1.0)];
        assert!(
            Homography::unit_square_to_quad(quad).is_some(),
            "den == 1e-12 is non-singular under strict `<`"
        );
        // control: a genuinely singular (collinear) quad still returns None —
        // this also traps `<`→`==` from the other side (0 != 1e-12).
        let collinear = [(0.0, 0.0), (2.0, 0.0), (4.0, 0.0), (6.0, 0.0)];
        assert!(Homography::unit_square_to_quad(collinear).is_none());
    }

    #[test]
    fn bilinear_bottom_row_read_and_weight_are_load_bearing() {
        // sampler line 122 (the `bottom` term). All four values distinct and
        // ≠ 255, so the bottom-row read is discriminated (the checker test hid
        // it: fetch(0,1)==fetch(0,-1)==255 by coincidence).
        //   (0,0)=10 (1,0)=20 / (0,1)=30 (1,1)=40
        let img = LumaImage::new(vec![10, 20, 30, 40], 2, 2);
        // center = mean of the four = 25 (first-principles bilinear midpoint).
        //   122:31 `fetch(ix, iy+1)`→`fetch(ix, iy-1)` reads OOB(255) → 81.
        //   122:73 `fetch(ix+1, iy+1) * fx`→`+ fx` → 35.
        assert_eq!(sample_bilinear(&img, 0.5, 0.5), 25);
        // three-quarters down the left column pins fy on the bottom term:
        // top(10)·0.25 + bottom(30)·0.75 = 25 (mutants → 193 / 55).
        assert_eq!(sample_bilinear(&img, 0.0, 0.75), 25);
    }

    #[test]
    fn unit_square_to_quad_projects_interior_points_exactly() {
        // A genuinely projective (non-affine) quad — exercises the perspective
        // divide, not just an affine scale. H = [2/3,0,0, 0,2/3,0, −1/3,−1/3,1],
        // so apply(u,v) divides by w = 1 − (u+v)/3.
        let quad = [(0.0, 0.0), (1.0, 0.0), (2.0, 2.0), (0.0, 1.0)];
        let h = Homography::unit_square_to_quad(quad).expect("nonsingular quad");
        let cases = [
            ((0.0, 0.0), (0.0, 0.0)), // corners
            ((1.0, 0.0), (1.0, 0.0)),
            ((1.0, 1.0), (2.0, 2.0)),
            ((0.0, 1.0), (0.0, 1.0)),
            ((0.5, 0.5), (0.5, 0.5)), // center: w=2/3
            ((0.5, 0.0), (0.4, 0.0)), // off-center: w=5/6 → (1/3)/(5/6)=0.4
            ((0.25, 0.25), (0.2, 0.2)),
        ];
        for ((u, v), (ex, ey)) in cases {
            let (x, y) = h.apply(u, v).expect("finite");
            assert!(
                (x - ex).abs() < 1e-5 && (y - ey).abs() < 1e-5,
                "({u},{v}) → ({x},{y}), want ({ex},{ey})"
            );
        }
    }

    /// Five columns, four rows, every pixel its own value and none white.
    fn numbered() -> LumaImage {
        LumaImage::new((10..30).collect(), 5, 4)
    }

    /// The pixel at a signed position, white outside the image: what both
    /// readers of the four neighbours answer to.
    fn pixel_or_white(img: &LumaImage, x: i64, y: i64) -> u8 {
        let row = usize::try_from(y)
            .ok()
            .and_then(|y| img.data().chunks(img.width() as usize).nth(y));
        usize::try_from(x)
            .ok()
            .and_then(|x| row?.get(x).copied())
            .unwrap_or(255)
    }

    #[test]
    fn both_neighbour_readers_answer_the_definition_at_every_position() {
        let img = numbered();
        for iy in -2..=5 {
            for ix in -2..=6 {
                let want = [
                    pixel_or_white(&img, ix, iy),
                    pixel_or_white(&img, ix + 1, iy),
                    pixel_or_white(&img, ix, iy + 1),
                    pixel_or_white(&img, ix + 1, iy + 1),
                ];
                assert_eq!(bordered(&img, ix, iy), want, "bordered ({ix},{iy})");
                // the fast reader answers exactly where all four are inside
                let inside = (0..4).contains(&ix) && (0..3).contains(&iy);
                assert_eq!(
                    interior(&img, ix, iy),
                    inside.then_some(want),
                    "interior ({ix},{iy})"
                );
            }
        }
    }

    #[test]
    fn the_interior_reader_declines_a_position_no_image_holds() {
        // a coordinate far outside saturates the cast in `sample_bilinear`
        let img = numbered();
        assert_eq!(interior(&img, i64::MAX, 1), None);
        assert_eq!(interior(&img, 1, i64::MAX), None);
        assert_eq!(interior(&img, i64::MIN, i64::MIN), None);
    }

    #[test]
    fn a_sample_blends_the_same_way_inside_and_on_the_border() {
        let img = numbered();
        // inside: 16, 17 over 21, 22 → 16.25 over 21.25 → 18.75
        assert_eq!(sample_bilinear(&img, 1.25, 1.5), 18);
        // right edge: 19 beside white over 24 beside white, halfway across
        // and a quarter down → 137 over 139.5 → 137.625
        assert_eq!(sample_bilinear(&img, 4.5, 1.25), 137);
        // bottom edge: 26, 27 over white, three quarters down → 26.5 · ¼ + 255 · ¾
        assert_eq!(sample_bilinear(&img, 1.5, 3.75), 197);
        // above and left of the image: only the bottom right neighbour is a pixel
        assert_eq!(sample_bilinear(&img, -0.5, -0.5), 193);
    }
}