image.core\testgen\barcode.mbt
92% · 23 / 25 points · 113 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// Code39 字符表(与 image.codec/barcode/linear_barcodes.mbt 解码器保持一致)。 - 3
pub let code39_chars : Array[String] = [ - 4
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", - 5
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", - 6
"W", "X", "Y", "Z", "-", ".", " ", "$", "/", "+", "%", - 7
] - 8
- 9
///| - 10
/// Code39 条空模式:9 个元素(5 条 4 空),1 = 窄、2 = 宽。 - 11
pub let code39_patterns : Array[Array[Int32]] = [ - 12
[1, 1, 1, 2, 2, 1, 2, 1, 1], - 13
[2, 1, 1, 2, 1, 1, 1, 1, 2], - 14
[1, 1, 2, 2, 1, 1, 1, 1, 2], - 15
[2, 1, 2, 2, 1, 1, 1, 1, 1], - 16
[1, 1, 1, 2, 2, 1, 1, 1, 2], - 17
[2, 1, 1, 2, 2, 1, 1, 1, 1], - 18
[1, 1, 2, 2, 2, 1, 1, 1, 1], - 19
[1, 1, 1, 2, 1, 1, 2, 1, 2], - 20
[2, 1, 1, 2, 1, 1, 2, 1, 1], - 21
[1, 1, 2, 2, 1, 1, 2, 1, 1], - 22
[2, 1, 1, 1, 1, 2, 1, 1, 2], - 23
[1, 1, 2, 1, 1, 2, 1, 1, 2], - 24
[2, 1, 2, 1, 1, 2, 1, 1, 1], - 25
[1, 1, 1, 1, 2, 2, 1, 1, 2], - 26
[2, 1, 1, 1, 2, 2, 1, 1, 1], - 27
[1, 1, 2, 1, 2, 2, 1, 1, 1], - 28
[1, 1, 1, 1, 1, 2, 2, 1, 2], - 29
[2, 1, 1, 1, 1, 2, 2, 1, 1], - 30
[1, 1, 2, 1, 1, 2, 2, 1, 1], - 31
[1, 1, 1, 1, 2, 2, 2, 1, 1], - 32
[2, 1, 1, 1, 1, 1, 1, 2, 2], - 33
[1, 1, 2, 1, 1, 1, 1, 2, 2], - 34
[2, 1, 2, 1, 1, 1, 1, 2, 1], - 35
[1, 1, 1, 1, 2, 1, 1, 2, 2], - 36
[2, 1, 1, 1, 2, 1, 1, 2, 1], - 37
[1, 1, 2, 1, 2, 1, 1, 2, 1], - 38
[1, 1, 1, 1, 1, 1, 2, 2, 2], - 39
[2, 1, 1, 1, 1, 1, 2, 2, 1], - 40
[1, 1, 2, 1, 1, 1, 2, 2, 1], - 41
[1, 1, 1, 1, 2, 1, 2, 2, 1], - 42
[2, 2, 1, 1, 1, 1, 1, 1, 2], - 43
[1, 2, 2, 1, 1, 1, 1, 1, 2], - 44
[2, 2, 2, 1, 1, 1, 1, 1, 1], - 45
[1, 2, 1, 1, 2, 1, 1, 1, 2], - 46
[2, 2, 1, 1, 2, 1, 1, 1, 1], - 47
[1, 2, 2, 1, 2, 1, 1, 1, 1], - 48
[1, 2, 1, 1, 1, 1, 2, 1, 2], - 49
[2, 2, 1, 1, 1, 1, 2, 1, 1], - 50
[1, 2, 2, 1, 1, 1, 2, 1, 1], - 51
[1, 2, 1, 2, 1, 2, 1, 1, 1], - 52
[1, 2, 1, 2, 1, 1, 1, 2, 1], - 53
[1, 2, 1, 1, 1, 2, 1, 2, 1], - 54
[1, 1, 1, 2, 1, 2, 1, 2, 1], - 55
] - 56
- 57
///| - 58
/// Code39 起始/终止符 '*' 的模式。 - 59
pub let code39_start_pattern : Array[Int32] = [1, 2, 1, 1, 2, 1, 2, 1, 1] - 60
- 61
///| - 62
/// 按字符索引(0..42)渲染 Code39 条码为灰度图(白底 255 / 黑条 0)。 - 63
/// codes 中的非法索引会被跳过。module_px 为窄模块像素宽度。 - 64
pub fn render_code39( - 65
codes : Array[Int32], - 66
module_px~ : Int32 = 2, - 67
height~ : Int32 = 40, - 68
) -> @types.Image { - 69◐
let mp = if module_px < 1 { 1 } else { module_px } - 70
let patterns : Array[Array[Int32]] = [] - 71●
patterns.push(code39_start_pattern) - 72
for c in codes { - 73●
if c >= 0 && c < code39_patterns.length() { - 74●
patterns.push(code39_patterns[c]) - 75
} - 76
} - 77●
patterns.push(code39_start_pattern) - 78
let mut bars_w = 0 - 79
for p in patterns { - 80●
for w in p { - 81●
bars_w += w - 82
} - 83
} - 84●
let total_modules = bars_w + patterns.length() - 1 - 85
let w = (total_modules + 2) * mp - 86◐
let h = if height < 2 { 2 } else { height } - 87●
let data : Array[Byte] = Array::make(w * h, b'\xFF') - 88
let mut x = mp - 89●
for ci in 0..<patterns.length() { - 90●
let p = patterns[ci] - 91●
for ei in 0..<p.length() { - 92●
let run = p[ei] * mp - 93
let is_bar = ei % 2 == 0 - 94
if is_bar { - 95●
for dy in 0..<h { - 96●
for dx in 0..<run { - 97●
data[dy * w + x + dx] = b'\x00' - 98
} - 99
} - 100
} - 101
x += run - 102
} - 103●
if ci < patterns.length() - 1 { - 104●
x += mp - 105
} - 106
} - 107
@types.Image::{ - 108
width: w, - 109
height: h, - 110
channels: 1, - 111●
data: Bytes::from_array(data), - 112
} - 113
}