image.core\testgen\barcode.mbt

92% · 23 / 25 points · 113 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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