Skip to main content

anyd/codes/gridmatrix/
mod.rs

1//! Grid Matrix (AIMD014 / GB/T 27766) encoder and structural decoder.
2//!
3//! Grid Matrix is a Chinese national 2D matrix symbology. A symbol is a square array
4//! of `2·v + 1` **macromodules** per side (version `v` = 1…13); each macromodule is a
5//! 6×6 block whose fixed perimeter forms the alternating "grid" and whose 4×4 interior
6//! carries a 2-bit layer-id plus two 7-bit codewords. Error correction is Reed–Solomon
7//! over GF(2^7).
8//!
9//! Layout:
10//! - [`gf`]     — GF(2^7) arithmetic (primitive `0x89`) and Reed–Solomon.
11//! - `tables`   — versions, EC levels, and the per-version RS block structure.
12//! - `data`     — mode-switched data encodation and canonical segmentation.
13//! - `layout`   — macromodule placement, the frame, and the layer-id metadata.
14//! - `encode`   — [`Symbol`] → [`BitMatrix`].
15//! - `decode`   — [`BitMatrix`] → [`Symbol`].
16//!
17//! # Scope
18//!
19//! All 13 versions are implemented, along with the full five-level Reed–Solomon block
20//! structure (transcribed from zint). Data encodation covers **Numeric**, **Upper**,
21//! **Lower** and **Byte** modes; **Mixed** and **GB2312 Chinese** modes are modelled
22//! but not produced by the encoder (the decoder reports them as unsupported). See
23//! `data` for the exact per-mode bit formats and the small, documented deviations in
24//! byte mode.
25//!
26//! # Losslessness and metadata
27//!
28//! [`GridMatrixEncoder::build`] splits the payload into a canonical mode segmentation
29//! that the decoder reconstructs exactly, so `encode(decode(x)) == x`. [`GridMatrixMeta`]
30//! records the version, EC level, and per-segment mode sequence needed to re-encode
31//! identically.
32//!
33//! Grid Matrix defines **no** QR-style data mask: the alternating macromodule frame
34//! already balances the module pattern, and the version/EC metadata rides in redundant
35//! layer-id rings rather than a masked format field. `GridMatrixMeta` therefore stores
36//! no mask (a deliberate deviation from the task's generic "version/EC/mask" shape).
37//!
38//! [`Symbol`]: crate::Symbol
39//! [`BitMatrix`]: crate::output::BitMatrix
40
41mod data;
42mod decode;
43mod encode;
44pub mod gf;
45mod layout;
46mod tables;
47
48pub use data::GmMode;
49pub use decode::GridMatrixDecoder;
50pub use encode::GridMatrixEncoder;
51pub use tables::{EcLevel, Version};
52
53/// Grid Matrix parameters needed to re-encode a symbol identically (lossless
54/// round-trip).
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct GridMatrixMeta {
57    /// Symbol version (1–13), which fixes the module size.
58    pub version: Version,
59    /// Reed–Solomon error-correction level.
60    pub ec_level: EcLevel,
61    /// The Grid Matrix encodation mode of each data segment, in symbol order.
62    /// Parallel to [`crate::Symbol::segments`].
63    pub modes: Vec<GmMode>,
64}
65
66impl Default for GridMatrixMeta {
67    fn default() -> Self {
68        GridMatrixMeta {
69            version: Version::new(1).unwrap(),
70            ec_level: EcLevel::L5,
71            modes: Vec::new(),
72        }
73    }
74}