anyd 0.1.1

From-scratch encoding and decoding of 1D and 2D barcodes with lossless round-trip and live-video detection
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
//! GS1 DataBar encoding: [`Symbol`] -> [`LinearPattern`].

use super::tables::{
    LTD_CHECK_WEIGHT, LTD_FINDER, LTD_G_SUM, LTD_MODULES, LTD_T_EVEN, LTD_WIDEST, OMN_CHECK_WEIGHT,
    OMN_FINDER, OMN_G_SUM, OMN_MODULES, OMN_T_EVEN_ODD, OMN_WIDEST, gtin_check_digit,
};
use super::widths::interleave;
use super::{DataBarMeta, DataBarVariant};
use crate::error::{Error, Result};
use crate::output::{Encoding, LinearPattern};
use crate::segment::{Mode, Segment};
use crate::symbol::{Symbol, SymbolMeta};
use crate::symbology::Symbology;
use crate::traits::Encode;

/// Light-module margin the encoder frames each symbol with. DataBar does not
/// mandate a specific quiet zone; a 1X margin is used consistently so that
/// re-encoding is stable.
const QUIET_ZONE: usize = 1;

/// Omnidirectional: value multiplier splitting a 14-digit GTIN into two pairs.
const OMN_PAIR_MUL: u64 = 4_537_077;
/// Omnidirectional: multiplier splitting a pair into two data characters.
const OMN_CHAR_MUL: i32 = 1597;
/// Limited: value multiplier splitting the GTIN into two pair values.
const LTD_PAIR_MUL: u64 = 2_013_571;

/// GS1 DataBar encoder.
#[derive(Debug, Default, Clone, Copy)]
pub struct DataBarEncoder;

impl DataBarEncoder {
    /// A new encoder.
    pub fn new() -> Self {
        DataBarEncoder
    }

    /// Build a GS1 DataBar Omnidirectional symbol from a GTIN (13 or 14 digits).
    ///
    /// A 13-digit body has its GS1 check digit appended; a 14-digit GTIN is
    /// validated against its check digit. The stored symbol carries the canonical
    /// 14-digit GTIN as a single numeric segment.
    pub fn build_omni(&self, gtin: &[u8]) -> Result<Symbol> {
        let canonical = normalize_gtin(gtin, DataBarVariant::Omni)?;
        Ok(Symbol::new(
            Symbology::DataBarOmni,
            vec![Segment::numeric(canonical.to_vec())],
            SymbolMeta::DataBar(DataBarMeta::new(DataBarVariant::Omni)),
        ))
    }

    /// Build a GS1 DataBar Limited symbol from a GTIN (13 or 14 digits).
    ///
    /// Limited only encodes GTINs whose indicator (leading) digit is 0 or 1.
    pub fn build_limited(&self, gtin: &[u8]) -> Result<Symbol> {
        let canonical = normalize_gtin(gtin, DataBarVariant::Limited)?;
        Ok(Symbol::new(
            Symbology::DataBarLimited,
            vec![Segment::numeric(canonical.to_vec())],
            SymbolMeta::DataBar(DataBarMeta::new(DataBarVariant::Limited)),
        ))
    }

    /// Build a GS1 DataBar Stacked symbol (the two-row form of DataBar-14) from a
    /// GTIN (13 or 14 digits). The payload is the canonical 14-digit GTIN.
    pub fn build_stacked(&self, gtin: &[u8]) -> Result<Symbol> {
        let canonical = normalize_gtin(gtin, DataBarVariant::Stacked)?;
        Ok(Symbol::new(
            Symbology::DataBarStacked,
            vec![Segment::numeric(canonical.to_vec())],
            SymbolMeta::DataBar(DataBarMeta::new(DataBarVariant::Stacked)),
        ))
    }

    /// Build a GS1 DataBar Stacked Omnidirectional symbol from a GTIN (13 or 14
    /// digits). The payload is the canonical 14-digit GTIN.
    pub fn build_stacked_omni(&self, gtin: &[u8]) -> Result<Symbol> {
        let canonical = normalize_gtin(gtin, DataBarVariant::StackedOmni)?;
        Ok(Symbol::new(
            Symbology::DataBarStackedOmni,
            vec![Segment::numeric(canonical.to_vec())],
            SymbolMeta::DataBar(DataBarMeta::new(DataBarVariant::StackedOmni)),
        ))
    }

    /// Build a GS1 DataBar Expanded Stacked symbol from a reduced GS1 element string,
    /// placing `columns_per_row` column pairs (codeblocks) on each stacked row.
    ///
    /// `columns_per_row` must be in `1..=11` (ISO/IEC 24724:2011 ยง7.2.8); the common
    /// default is 2. See [`DataBarEncoder::build_expanded`] for the payload format.
    pub fn build_expanded_stacked(&self, gs1: &[u8], columns_per_row: usize) -> Result<Symbol> {
        if !(1..=11).contains(&columns_per_row) {
            return Err(Error::invalid_parameter(
                "DataBar Expanded Stacked columns_per_row must be in 1..=11",
            ));
        }
        // Validate up front by attempting the stacked encodation, and canonicalise
        // the column count to the number actually placed on the first row: a symbol
        // with fewer codeblocks than requested columns is a single row whose module
        // matrix (and therefore the decoded metadata) cannot distinguish the two.
        let effective = super::stacked::effective_columns(gs1, columns_per_row)?;
        Ok(Symbol::new(
            Symbology::DataBarExpandedStacked,
            vec![Segment::byte(gs1.to_vec())],
            SymbolMeta::DataBar(DataBarMeta::expanded_stacked(effective)),
        ))
    }

    /// Build a GS1 DataBar Expanded symbol from a reduced GS1 element string.
    ///
    /// `gs1` is the Application Identifier data in *reduced* form: AI numbers and
    /// their values concatenated, with a GS1 separator (FNC1, byte `0x1D`) after a
    /// variable-length AI's value when another AI follows. The bytes are validated
    /// by running the encodation and stored verbatim as a single [`Segment::byte`].
    pub fn build_expanded(&self, gs1: &[u8]) -> Result<Symbol> {
        // Validate up front by attempting the encodation.
        super::expanded::element_widths(gs1)?;
        Ok(Symbol::new(
            Symbology::DataBarExpanded,
            vec![Segment::byte(gs1.to_vec())],
            SymbolMeta::DataBar(DataBarMeta::new(DataBarVariant::Expanded)),
        ))
    }
}

impl Encode for DataBarEncoder {
    fn encode(&self, symbol: &Symbol) -> Result<Encoding> {
        match symbol.symbology {
            Symbology::DataBarOmni => {
                let val = gtin_value(symbol, DataBarVariant::Omni)?;
                Ok(linear(&omn_total_widths(val)))
            }
            Symbology::DataBarLimited => {
                let val = gtin_value(symbol, DataBarVariant::Limited)?;
                Ok(linear(&ltd_total_widths(val)))
            }
            // Expanded is driven by its metadata: a symbol is encodable only when it
            // carries the canonical Expanded metadata that `build_expanded` and
            // `decode` produce. A DataBarExpanded symbology tagged with any other
            // DataBar variant is a malformed/unsupported combination.
            Symbology::DataBarExpanded
                if matches!(
                    symbol.meta,
                    SymbolMeta::DataBar(DataBarMeta {
                        variant: DataBarVariant::Expanded,
                        ..
                    })
                ) =>
            {
                super::expanded::encode(symbol)
            }
            Symbology::DataBarStacked if is_variant(symbol, DataBarVariant::Stacked) => {
                let val = gtin_value(symbol, DataBarVariant::Stacked)?;
                Ok(super::stacked::encode_stacked(&omn_total_widths(val)))
            }
            Symbology::DataBarStackedOmni if is_variant(symbol, DataBarVariant::StackedOmni) => {
                let val = gtin_value(symbol, DataBarVariant::StackedOmni)?;
                Ok(super::stacked::encode_stacked_omni(&omn_total_widths(val)))
            }
            Symbology::DataBarExpandedStacked
                if is_variant(symbol, DataBarVariant::ExpandedStacked) =>
            {
                super::stacked::encode_expanded_stacked(symbol)
            }
            // A DataBar symbology tagged with a metadata variant that does not match
            // it is a malformed/unsupported combination.
            Symbology::DataBarExpanded
            | Symbology::DataBarStacked
            | Symbology::DataBarStackedOmni
            | Symbology::DataBarExpandedStacked => Err(Error::Unsupported {
                what: "GS1 DataBar symbology without matching variant metadata",
            }),
            _ => Err(Error::invalid_parameter(
                "DataBarEncoder given a non-DataBar symbol",
            )),
        }
    }
}

/// True if the symbol carries DataBar metadata of the given variant.
fn is_variant(symbol: &Symbol, variant: DataBarVariant) -> bool {
    matches!(symbol.meta, SymbolMeta::DataBar(DataBarMeta { variant: v, .. }) if v == variant)
}

/// Expand an element-width sequence into modules, starting with a light element
/// and alternating (ISO/IEC 24724 symbols begin with a light guard element).
fn expand(widths: &[i32]) -> Vec<bool> {
    let mut modules = Vec::with_capacity(widths.iter().map(|&w| w.max(0) as usize).sum());
    let mut dark = false;
    for &w in widths {
        for _ in 0..w {
            modules.push(dark);
        }
        dark = !dark;
    }
    modules
}

/// Wrap an element-width sequence as a linear encoding.
pub(super) fn linear(widths: &[i32]) -> Encoding {
    Encoding::Linear(LinearPattern {
        modules: expand(widths),
        quiet_zone: QUIET_ZONE,
    })
}

/// Read the canonical GTIN value (its 13-digit body as an integer) from a symbol.
fn gtin_value(symbol: &Symbol, variant: DataBarVariant) -> Result<u64> {
    let digits = extract_digits(&symbol.segments)?;
    let canonical = normalize_gtin(&digits, variant)?;
    Ok(body_value(&canonical))
}

/// Collect the digit bytes from a symbol's numeric segments.
fn extract_digits(segments: &[Segment]) -> Result<Vec<u8>> {
    let mut out = Vec::new();
    for seg in segments {
        match seg.mode {
            Mode::Numeric => {
                for &b in &seg.data {
                    if !b.is_ascii_digit() {
                        return Err(Error::invalid_data("DataBar payload must be digits"));
                    }
                    out.push(b);
                }
            }
            _ => {
                return Err(Error::invalid_data(
                    "DataBar payload must be a numeric GTIN",
                ));
            }
        }
    }
    Ok(out)
}

/// Validate `digits` (13 or 14) and return the canonical 14-digit GTIN.
fn normalize_gtin(digits: &[u8], variant: DataBarVariant) -> Result<[u8; 14]> {
    if digits.len() != 13 && digits.len() != 14 {
        return Err(Error::capacity(format!(
            "DataBar GTIN must be 13 or 14 digits, got {}",
            digits.len()
        )));
    }
    if let Some(&b) = digits.iter().find(|b| !b.is_ascii_digit()) {
        return Err(Error::invalid_data(format!(
            "DataBar GTIN contains non-digit byte {b:#04x}"
        )));
    }
    let mut body = [0u8; 13];
    body.copy_from_slice(&digits[..13]);
    let check = gtin_check_digit(&body);
    if digits.len() == 14 && digits[13] != check {
        return Err(Error::invalid_data(format!(
            "DataBar GTIN check digit mismatch: expected '{}', got '{}'",
            check as char, digits[13] as char
        )));
    }
    if variant == DataBarVariant::Limited && body[0] != b'0' && body[0] != b'1' {
        return Err(Error::capacity(
            "DataBar Limited requires a GTIN indicator digit of 0 or 1",
        ));
    }
    let mut canonical = [0u8; 14];
    canonical[..13].copy_from_slice(&body);
    canonical[13] = check;
    Ok(canonical)
}

/// The integer value of a canonical GTIN's 13-digit body.
fn body_value(canonical: &[u8; 14]) -> u64 {
    canonical[..13]
        .iter()
        .fold(0u64, |acc, &b| acc * 10 + (b - b'0') as u64)
}

// ======== Omnidirectional ========

/// Select the Omnidirectional group for a data character value.
///
/// `outside` chooses the outer (groups 0-4) versus inner (groups 5-8) subset.
fn omn_group(val: i32, outside: bool) -> usize {
    let end = if outside { 4 } else { 8 };
    let mut i = if outside { 0 } else { 5 };
    while i < end {
        if val < OMN_G_SUM[i + 1] {
            return i;
        }
        i += 1;
    }
    i
}

/// The 46 element widths of a DataBar Omnidirectional symbol for GTIN value `val`.
///
/// Mirrors `zint_dbar_omn` (zint `rss.c`): four data characters (outer, inner,
/// inner, outer), two finder patterns chosen by the checksum, and guards.
pub(super) fn omn_total_widths(val: u64) -> [i32; 46] {
    let left_pair = (val / OMN_PAIR_MUL) as i32;
    let right_pair = (val % OMN_PAIR_MUL) as i32;
    let dc = [
        left_pair / OMN_CHAR_MUL,
        left_pair % OMN_CHAR_MUL,
        right_pair / OMN_CHAR_MUL,
        right_pair % OMN_CHAR_MUL,
    ];

    let mut dw = [[0i32; 8]; 4];
    for (i, dw_i) in dw.iter_mut().enumerate() {
        let outside = i % 2 == 0;
        let group = omn_group(dc[i], outside);
        let v = dc[i] - OMN_G_SUM[group];
        let v_div = v / OMN_T_EVEN_ODD[group];
        let v_mod = v % OMN_T_EVEN_ODD[group];
        let (v_odd, v_even) = if outside {
            (v_div, v_mod)
        } else {
            (v_mod, v_div)
        };
        let w = interleave(
            v_odd as i64,
            v_even as i64,
            OMN_MODULES[group],
            OMN_MODULES[group + 9],
            4,
            OMN_WIDEST[group],
            !outside,
        );
        dw_i.copy_from_slice(&w);
    }

    // Checksum -> two finder-pattern indices (Table 5).
    let mut checksum = 0i32;
    for (i, dw_i) in dw.iter().enumerate() {
        for (j, &w) in dw_i.iter().enumerate() {
            checksum += OMN_CHECK_WEIGHT[i][j] * w;
        }
    }
    checksum %= 79;
    if checksum >= 8 {
        checksum += 1;
    }
    if checksum >= 72 {
        checksum += 1;
    }
    let c_left = (checksum / 9) as usize;
    let c_right = (checksum % 9) as usize;

    let mut tw = [0i32; 46];
    tw[0] = 1;
    tw[1] = 1;
    tw[44] = 1;
    tw[45] = 1;
    for i in 0..8 {
        tw[i + 2] = dw[0][i];
        tw[i + 15] = dw[1][7 - i];
        tw[i + 23] = dw[3][i];
        tw[i + 36] = dw[2][7 - i];
    }
    for i in 0..5 {
        tw[i + 10] = OMN_FINDER[c_left][i] as i32;
        tw[i + 31] = OMN_FINDER[c_right][4 - i] as i32;
    }
    tw
}

/// The right finder-pattern index (`c_right`, 0..=8) encoded in an Omnidirectional
/// element-width sequence. Used by the Stacked Omnidirectional separator, whose
/// finder-value-3 special case (ISO/IEC 24724:2011 ยง5.3.2.2) applies when it is 3.
pub(super) fn omn_right_finder_index(tw: &[i32; 46]) -> usize {
    let mut finder_right = [tw[31], tw[32], tw[33], tw[34], tw[35]];
    finder_right.reverse();
    OMN_FINDER
        .iter()
        .position(|f| f.iter().zip(&finder_right).all(|(&a, &b)| a as i32 == b))
        .unwrap_or(0)
}

// ======== Limited ========

/// Select the Limited group for a pair value, returning `(group, remainder)`.
fn ltd_group(val: i32) -> (usize, i32) {
    for i in (1..=6).rev() {
        if val >= LTD_G_SUM[i] {
            return (i, val - LTD_G_SUM[i]);
        }
    }
    (0, val)
}

/// The 47 element widths of a DataBar Limited symbol for GTIN value `val`.
///
/// Mirrors `zint_dbar_ltd` (zint `rss.c`): two pair characters flanking a
/// checksum-selected finder pattern, plus guards (including the wide right guard).
pub(super) fn ltd_total_widths(val: u64) -> [i32; 47] {
    let pair_vals = [(val / LTD_PAIR_MUL) as i32, (val % LTD_PAIR_MUL) as i32];

    let mut pw = [[0i32; 14]; 2];
    for (i, pw_i) in pw.iter_mut().enumerate() {
        let (group, v) = ltd_group(pair_vals[i]);
        let odd = v / LTD_T_EVEN[group];
        let even = v % LTD_T_EVEN[group];
        let w = interleave(
            odd as i64,
            even as i64,
            LTD_MODULES[group],
            26 - LTD_MODULES[group],
            7,
            LTD_WIDEST[group],
            false,
        );
        pw_i.copy_from_slice(&w);
    }

    let mut checksum = 0i32;
    for i in 0..14 {
        checksum += LTD_CHECK_WEIGHT[0][i] * pw[0][i];
        checksum += LTD_CHECK_WEIGHT[1][i] * pw[1][i];
    }
    checksum %= 89;
    let cfp = &LTD_FINDER[checksum as usize];

    let mut tw = [0i32; 47];
    tw[0] = 1;
    tw[1] = 1;
    tw[44] = 1;
    tw[45] = 1;
    tw[46] = 5;
    for i in 0..14 {
        tw[i + 2] = pw[0][i];
        tw[i + 16] = cfp[i] as i32;
        tw[i + 30] = pw[1][i];
    }
    tw
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn omni_symbol_is_96_modules() {
        let Encoding::Linear(p) = linear(&omn_total_widths(0)) else {
            panic!("expected linear");
        };
        assert_eq!(p.modules.len(), 96);
    }

    #[test]
    fn limited_symbol_is_79_modules() {
        let Encoding::Linear(p) = linear(&ltd_total_widths(0)) else {
            panic!("expected linear");
        };
        assert_eq!(p.modules.len(), 79);
    }

    #[test]
    fn check_digit_matches_known_gtins() {
        // Independently computed GS1 mod-10 check digits.
        assert_eq!(gtin_check_digit(b"2001234567890"), b'9');
        assert_eq!(gtin_check_digit(b"0095011015300"), b'7');
        assert_eq!(gtin_check_digit(b"0000000000000"), b'0');
    }
}