use super::bit_matrix::BitMatrix;
use image::RgbaImage;
static DIGIT_PATTERNS: [[u8; 5]; 10] = [
[0, 0, 1, 1, 0], [1, 0, 0, 0, 1], [0, 1, 0, 0, 1], [1, 1, 0, 0, 0], [0, 0, 1, 0, 1], [1, 0, 1, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 1, 1], [1, 0, 0, 1, 0], [0, 1, 0, 1, 0], ];
pub fn encode(
content: &str,
height: i32,
wide_bar_ratio: i32,
narrow_bar: i32,
print_check_digit: bool,
) -> Result<RgbaImage, String> {
let mut digits: Vec<u8> = content
.chars()
.filter(|c| c.is_ascii_digit())
.map(|c| c as u8 - b'0')
.collect();
if digits.is_empty() {
return Err("Interleaved 2 of 5: no digits provided".to_string());
}
if print_check_digit || !digits.len().is_multiple_of(2) {
let mut sum = 0u32;
for (i, &d) in digits.iter().enumerate() {
if i % 2 == 0 {
sum += d as u32 * 3;
} else {
sum += d as u32;
}
}
let check = ((10 - (sum % 10)) % 10) as u8;
digits.push(check);
}
if !digits.len().is_multiple_of(2) {
digits.insert(0, 0);
}
let narrow = narrow_bar.max(1) as usize;
let wide = (narrow_bar * wide_bar_ratio).max(2) as usize;
let pair_count = digits.len() / 2;
let pair_width = 2 * wide + 3 * narrow; let total_module_width = 4 * narrow + pair_count * (pair_width * 2) + wide + 2 * narrow;
let total_width = total_module_width;
let mut bm = BitMatrix::new(total_width, 1);
let mut pos = 0;
bm.set_range(pos, narrow, true);
pos += narrow;
pos += narrow; bm.set_range(pos, narrow, true);
pos += narrow;
pos += narrow;
for pair_idx in 0..pair_count {
let d1 = digits[pair_idx * 2] as usize;
let d2 = digits[pair_idx * 2 + 1] as usize;
let bars = &DIGIT_PATTERNS[d1];
let spaces = &DIGIT_PATTERNS[d2];
for i in 0..5 {
let bar_w = if bars[i] == 1 { wide } else { narrow };
let space_w = if spaces[i] == 1 { wide } else { narrow };
bm.set_range(pos, bar_w, true);
pos += bar_w;
pos += space_w; }
}
bm.set_range(pos, wide, true);
pos += wide;
pos += narrow; bm.set_range(pos, narrow, true);
Ok(bm.to_1d_image(1, height.max(1) as usize))
}