image.vision\fieldimg\field_bind.mbt

100% · 68 / 68 points · 162 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/// # 场绑定 fieldimg/field_bind
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 场与 Image 双向转换:Image ↔ ScalarField / VectorField。
  7. 7// Field-Image bidirectional binding: Image ↔ ScalarField / VectorField.
  8. 8//
  9. 9// 映射关系:
  10. 10// 灰度 Image (channels=1) ↔ ScalarField
  11. 11// RGB Image (channels=3) ↔ VectorField (dim=3)
  12. 12// RGBA Image (channels=4) ↔ VectorField (dim=4)
  13. 13
  14. 14///|
  15. 15/// Image 转标量场:将灰度图(channels=1)转为标量场,像素值归一化到 [0, 1]。
  16. 16/// 若 channels > 1,先转灰度(加权平均:0.299R + 0.587G + 0.114B)。
  17. 17/// Image 转 ScalarField;灰度归一化到 [0, 1] Convert Image to ScalarField; grayscale (channels=1) normalized to [0, 1].
  18. 18/// channels > 1 时 For channels > 1
  19. 19pub fn image_to_scalar_field(
  20. 20 img : @types.Image,
  21. 21) -> ScalarField raise @types.LoadError {
  22. 22● if img.width <= 0 || img.height <= 0 {
  23. 23● raise @types.LoadError::DecodeFailed(
  24. 24 "image_to_scalar_field: 图像尺寸必须大于0",
  25. 25 )
  26. 26 }
  27. 27 let w = img.width
  28. 28 let h = img.height
  29. 29 let ch = img.channels
  30. 30 let n = w * h
  31. 31● let data = Array::make(n, 0.0F)
  32. 32 for i in 0..<n {
  33. 33● let base = i * ch
  34. 34 let v = if ch == 1 {
  35. 35● Float::from_int(img.data[base].to_int())
  36. 36● } else if ch >= 3 {
  37. 37● let r = Float::from_int(img.data[base].to_int())
  38. 38● let g = Float::from_int(img.data[base + 1].to_int())
  39. 39● let b = Float::from_int(img.data[base + 2].to_int())
  40. 40 0.299F * r + 0.587F * g + 0.114F * b
  41. 41 } else {
  42. 42● Float::from_int(img.data[base].to_int())
  43. 43 }
  44. 44 data[i] = v * (1.0F / 255.0F)
  45. 45 }
  46. 46● ScalarField::make(w, h, data)
  47. 47}
  48. 48
  49. 49///|
  50. 50/// 标量场转 Image:将标量场转为单通道灰度图,值钳制到 [0, 1] 后映射到 [0, 255]。
  51. 51/// ScalarField 转单通道灰度 Image;值钳位到 [0,1] 再映射到 [0,255] Convert ScalarField to single-channel grayscale Image; values clamped to [0, 1] then mapped to [0, 255].
  52. 52pub fn scalar_field_to_image(field : ScalarField) -> @types.Image {
  53. 53● let n = field.width * field.height
  54. 54● let buf : Array[Byte] = Array::make(n, b'0')
  55. 55 for i in 0..<n {
  56. 56● let v = field.data[i]
  57. 57● let clamped = if v < 0.0F { 0.0F } else if v > 1.0F { 1.0F } else { v }
  58. 58● buf[i] = (clamped * 255.0F + 0.5F).to_int().clamp(min=0, max=255).to_byte()
  59. 59 }
  60. 60 {
  61. 61 width: field.width,
  62. 62 height: field.height,
  63. 63 channels: 1,
  64. 64● data: Bytes::from_array(buf),
  65. 65 }
  66. 66}
  67. 67
  68. 68///|
  69. 69/// Image 转矢量场:将 RGB/RGBA 图转为矢量场(dim=channels),像素值归一化到 [0, 1]。
  70. 70/// Image 转 VectorField;像素值归一化到 [0, 1] Convert Image to VectorField (dim=channels); pixel values normalized to [0, 1].
  71. 71pub fn image_to_vector_field(
  72. 72 img : @types.Image,
  73. 73) -> VectorField raise @types.LoadError {
  74. 74● if img.width <= 0 || img.height <= 0 || img.channels <= 0 {
  75. 75● raise @types.LoadError::DecodeFailed(
  76. 76 "image_to_vector_field: 图像尺寸和通道数必须大于0",
  77. 77 )
  78. 78 }
  79. 79 let w = img.width
  80. 80 let h = img.height
  81. 81 let ch = img.channels
  82. 82 let n = w * h * ch
  83. 83● let data = Array::make(n, 0.0F)
  84. 84 for i in 0..<n {
  85. 85● data[i] = Float::from_int(img.data[i].to_int()) * (1.0F / 255.0F)
  86. 86 }
  87. 87● VectorField::make(w, h, ch, data)
  88. 88}
  89. 89
  90. 90///|
  91. 91/// 矢量场转 Image:将矢量场转为多通道图,值钳制到 [0, 1] 后映射到 [0, 255]。
  92. 92/// VectorField 转多通道 Image;值钳位到 [0,1] 再映射到 [0,255] Convert VectorField to multi-channel Image; values clamped to [0, 1] then mapped to [0, 255].
  93. 93pub fn vector_field_to_image(field : VectorField) -> @types.Image {
  94. 94● let n = field.width * field.height * field.dim
  95. 95● let buf : Array[Byte] = Array::make(n, b'0')
  96. 96 for i in 0..<n {
  97. 97● let v = field.data[i]
  98. 98● let clamped = if v < 0.0F { 0.0F } else if v > 1.0F { 1.0F } else { v }
  99. 99● buf[i] = (clamped * 255.0F + 0.5F).to_int().clamp(min=0, max=255).to_byte()
  100. 100 }
  101. 101 {
  102. 102 width: field.width,
  103. 103 height: field.height,
  104. 104 channels: field.dim,
  105. 105● data: Bytes::from_array(buf),
  106. 106 }
  107. 107}
  108. 108
  109. 109///|
  110. 110/// Image16 转标量场:16-bit 图转标量场,归一化到 [0, 1](除以 65535)。
  111. 111/// Image16 转 ScalarField;归一化到 [0, 1](除以 65535) Convert Image16 to ScalarField; normalized to [0, 1] (divided by 65535).
  112. 112pub fn image16_to_scalar_field(
  113. 113 img : @types.Image16,
  114. 114) -> ScalarField raise @types.LoadError {
  115. 115● if img.width <= 0 || img.height <= 0 {
  116. 116● raise @types.LoadError::DecodeFailed(
  117. 117 "image16_to_scalar_field: 图像尺寸必须大于0",
  118. 118 )
  119. 119 }
  120. 120 let w = img.width
  121. 121 let h = img.height
  122. 122 let ch = img.channels
  123. 123 let n = w * h
  124. 124● let data = Array::make(n, 0.0F)
  125. 125 let inv = 1.0F / 65535.0F
  126. 126 for i in 0..<n {
  127. 127● let base = i * ch * 2
  128. 128● let lo = img.data[base].to_int()
  129. 129● let hi = img.data[base + 1].to_int()
  130. 130 let v = lo | (hi << 8)
  131. 131● data[i] = Float::from_int(v) * inv
  132. 132 }
  133. 133● ScalarField::make(w, h, data)
  134. 134}
  135. 135
  136. 136///|
  137. 137/// ImageF 转标量场:HDR float 图转标量场,取第一通道,钳制到 [0, 1]。
  138. 138/// ImageF 转 ScalarField;取首通道,钳位到 [0, 1] Convert ImageF to ScalarField; takes first channel, clamped to [0, 1].
  139. 139pub fn imagef_to_scalar_field(
  140. 140 img : @types.ImageF,
  141. 141) -> ScalarField raise @types.LoadError {
  142. 142● if img.width <= 0 || img.height <= 0 {
  143. 143● raise @types.LoadError::DecodeFailed(
  144. 144 "imagef_to_scalar_field: 图像尺寸必须大于0",
  145. 145 )
  146. 146 }
  147. 147 let w = img.width
  148. 148 let h = img.height
  149. 149 let ch = img.channels
  150. 150 let n = w * h
  151. 151● let data = Array::make(n, 0.0F)
  152. 152 for i in 0..<n {
  153. 153● let base = i * ch * 4
  154. 154● let bits = img.data[base].to_int() |
  155. 155● (img.data[base + 1].to_int() << 8) |
  156. 156● (img.data[base + 2].to_int() << 16) |
  157. 157● (img.data[base + 3].to_int() << 24)
  158. 158● let v = Float::reinterpret_from_int(bits)
  159. 159● data[i] = if v < 0.0F { 0.0F } else if v > 1.0F { 1.0F } else { v }
  160. 160 }
  161. 161● ScalarField::make(w, h, data)
  162. 162}