image.process\color\dehaze_16f.mbt

98.9% · 90 / 91 points · 262 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/// # 去雾 16-bit/float 变体 Dehaze 16-bit/float variants
  3. 3///
  4. 4/// 16位/浮点去雾 dehaze_16 / dehaze_f
  5. 5/// 16位/浮点去雾 16-bit/float dehazing
  6. 6// 去雾 16-bit/float 变体(color 包)
  7. 7// dehaze / guided_filter 支持 Image16 / ImageF
  8. 8
  9. 9// ========== 辅助函数 ==========
  10. 10
  11. 11///|
  12. 12fn read_u16_le_dh(data : Bytes, offset : Int32) -> Int32 {
  13. 13● data[offset].to_int() + data[offset + 1].to_int() * 256
  14. 14}
  15. 15
  16. 16///|
  17. 17fn write_u16_le_dh(buf : Array[Byte], offset : Int32, val : Int32) -> Unit {
  18. 18● buf[offset] = (val & 0xFF).to_byte()
  19. 19● buf[offset + 1] = ((val >> 8) & 0xFF).to_byte()
  20. 20}
  21. 21
  22. 22///|
  23. 23fn read_float_le_dh(data : Bytes, offset : Int32) -> Float {
  24. 24● let b0 = data[offset].to_int()
  25. 25● let b1 = data[offset + 1].to_int()
  26. 26● let b2 = data[offset + 2].to_int()
  27. 27● let b3 = data[offset + 3].to_int()
  28. 28 let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
  29. 29● Float::reinterpret_from_int(bits)
  30. 30}
  31. 31
  32. 32///|
  33. 33fn write_f32_le_dh(buf : Array[Byte], offset : Int32, val : Float) -> Unit {
  34. 34● let bits = val.reinterpret_as_int()
  35. 35● buf[offset] = (bits & 0xFF).to_byte()
  36. 36● buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  37. 37● buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  38. 38● buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
  39. 39}
  40. 40
  41. 41///|
  42. 42fn img16_to_img8_dh(img : @types.Image16) -> @types.Image {
  43. 43● let w = img.width
  44. 44 let h = img.height
  45. 45 let ch = img.channels
  46. 46● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  47. 47 for i in 0..<(w * h * ch) {
  48. 48● let v16 = read_u16_le_dh(img.data, i * 2)
  49. 49● data8[i] = (v16 / 257).to_byte()
  50. 50 }
  51. 51 @types.Image::{
  52. 52 width: w,
  53. 53 height: h,
  54. 54 channels: ch,
  55. 55● data: Bytes::from_array(data8),
  56. 56 }
  57. 57}
  58. 58
  59. 59///|
  60. 60fn imgf_to_img8_dh(img : @types.ImageF) -> @types.Image {
  61. 61● let w = img.width
  62. 62 let h = img.height
  63. 63 let ch = img.channels
  64. 64● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  65. 65 for i in 0..<(w * h * ch) {
  66. 66● let vf = read_float_le_dh(img.data, i * 4)
  67. 67● let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255)
  68. 68● data8[i] = v8.to_byte()
  69. 69 }
  70. 70 @types.Image::{
  71. 71 width: w,
  72. 72 height: h,
  73. 73 channels: ch,
  74. 74● data: Bytes::from_array(data8),
  75. 75 }
  76. 76}
  77. 77
  78. 78///|
  79. 79fn img8_to_img16_dh(img : @types.Image) -> @types.Image16 {
  80. 80● let w = img.width
  81. 81 let h = img.height
  82. 82 let ch = img.channels
  83. 83● let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  84. 84 for i in 0..<(w * h * ch) {
  85. 85● let v8 = img.data[i].to_int()
  86. 86● write_u16_le_dh(out, i * 2, v8 * 257)
  87. 87 }
  88. 88 @types.Image16::{
  89. 89 width: w,
  90. 90 height: h,
  91. 91 channels: ch,
  92. 92● data: Bytes::from_array(out),
  93. 93 }
  94. 94}
  95. 95
  96. 96///|
  97. 97fn img8_to_imgf_dh(img : @types.Image) -> @types.ImageF {
  98. 98● let w = img.width
  99. 99 let h = img.height
  100. 100 let ch = img.channels
  101. 101● let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  102. 102 for i in 0..<(w * h * ch) {
  103. 103● let v8 = img.data[i].to_int()
  104. 104● write_f32_le_dh(out, i * 4, Float::from_int(v8) / 255.0F)
  105. 105 }
  106. 106 @types.ImageF::{
  107. 107 width: w,
  108. 108 height: h,
  109. 109 channels: ch,
  110. 110● data: Bytes::from_array(out),
  111. 111 }
  112. 112}
  113. 113
  114. 114// ========== Dehaze 16-bit ==========
  115. 115
  116. 116///|
  117. 117/// 16-bit 图像去雾。
  118. 118/// @see dehaze 8-bit 版本,本函数为 Image16 变体
  119. 119pub fn dehaze_16(
  120. 120 img : @types.Image16,
  121. 121 patch_size~ : Int32 = 7,
  122. 122 omega~ : Float = 0.95F,
  123. 123 t0~ : Float = 0.1F,
  124. 124) -> @types.Image16 raise @types.LoadError {
  125. 125 // 输入验证
  126. 126● if img.width <= 0 || img.height <= 0 {
  127. 127● raise @types.LoadError::DecodeFailed("dehaze_16: 图像尺寸必须大于0")
  128. 128 }
  129. 129 if img.channels != 3 && img.channels != 4 {
  130. 130● raise @types.LoadError::DecodeFailed(
  131. 131 "dehaze_16: 仅支持RGB/RGBA图像(channels=3/4)",
  132. 132 )
  133. 133 }
  134. 134 let expected_size = img.width * img.height * img.channels * 2
  135. 135● if img.data.length() != expected_size {
  136. 136● raise @types.LoadError::DecodeFailed(
  137. 137 "dehaze_16: 数据大小不匹配,期望 " +
  138. 138● expected_size.to_string() +
  139. 139 ",实际 " +
  140. 140● img.data.length().to_string(),
  141. 141 )
  142. 142 }
  143. 143● let img8 = img16_to_img8_dh(img)
  144. 144● let out8 = dehaze(img8, patch_size~, omega~, t0~)
  145. 145● img8_to_img16_dh(out8)
  146. 146}
  147. 147
  148. 148// ========== Dehaze float ==========
  149. 149
  150. 150///|
  151. 151/// float 图像去雾。
  152. 152/// @see dehaze 8-bit 版本,本函数为 ImageF 变体
  153. 153pub fn dehaze_f(
  154. 154 img : @types.ImageF,
  155. 155 patch_size~ : Int32 = 7,
  156. 156 omega~ : Float = 0.95F,
  157. 157 t0~ : Float = 0.1F,
  158. 158) -> @types.ImageF raise @types.LoadError {
  159. 159 // 输入验证
  160. 160● if img.width <= 0 || img.height <= 0 {
  161. 161● raise @types.LoadError::DecodeFailed("dehaze_f: 图像尺寸必须大于0")
  162. 162 }
  163. 163 if img.channels != 3 && img.channels != 4 {
  164. 164○ raise @types.LoadError::DecodeFailed(
  165. 165 "dehaze_f: 仅支持RGB/RGBA图像(channels=3/4)",
  166. 166 )
  167. 167 }
  168. 168 let expected_size = img.width * img.height * img.channels * 4
  169. 169● if img.data.length() != expected_size {
  170. 170● raise @types.LoadError::DecodeFailed(
  171. 171 "dehaze_f: 数据大小不匹配,期望 " +
  172. 172● expected_size.to_string() +
  173. 173 ",实际 " +
  174. 174● img.data.length().to_string(),
  175. 175 )
  176. 176 }
  177. 177● let img8 = imgf_to_img8_dh(img)
  178. 178● let out8 = dehaze(img8, patch_size~, omega~, t0~)
  179. 179● img8_to_imgf_dh(out8)
  180. 180}
  181. 181
  182. 182// ========== Guided Filter 16-bit ==========
  183. 183
  184. 184///|
  185. 185/// 16-bit 引导滤波。
  186. 186/// @see guided_filter 8-bit 版本,本函数为 Image16 变体
  187. 187pub fn guided_filter_16(
  188. 188 img : @types.Image16,
  189. 189 input : Array[Float],
  190. 190 radius~ : Int32 = 20,
  191. 191 eps~ : Float = 0.001F,
  192. 192) -> Array[Float] raise @types.LoadError {
  193. 193 // 输入验证
  194. 194● if img.width <= 0 || img.height <= 0 {
  195. 195● raise @types.LoadError::DecodeFailed(
  196. 196 "guided_filter_16: 图像尺寸必须大于0",
  197. 197 )
  198. 198 }
  199. 199 if img.channels < 1 || img.channels > 4 {
  200. 200● raise @types.LoadError::DecodeFailed(
  201. 201 "guided_filter_16: 通道数必须在1-4之间",
  202. 202 )
  203. 203 }
  204. 204 let expected_size = img.width * img.height * img.channels * 2
  205. 205● if img.data.length() != expected_size {
  206. 206● raise @types.LoadError::DecodeFailed(
  207. 207 "guided_filter_16: 数据大小不匹配,期望 " +
  208. 208● expected_size.to_string() +
  209. 209 ",实际 " +
  210. 210● img.data.length().to_string(),
  211. 211 )
  212. 212 }
  213. 213 if radius < 1 {
  214. 214● raise @types.LoadError::DecodeFailed("guided_filter_16: radius必须>=1")
  215. 215 }
  216. 216 if eps <= 0.0F {
  217. 217● raise @types.LoadError::DecodeFailed("guided_filter_16: eps必须大于0")
  218. 218 }
  219. 219● let img8 = img16_to_img8_dh(img)
  220. 220● guided_filter(img8, input, radius~, eps~)
  221. 221}
  222. 222
  223. 223// ========== Guided Filter float ==========
  224. 224
  225. 225///|
  226. 226/// float 引导滤波。
  227. 227/// @see guided_filter 8-bit 版本,本函数为 ImageF 变体
  228. 228pub fn guided_filter_f(
  229. 229 img : @types.ImageF,
  230. 230 input : Array[Float],
  231. 231 radius~ : Int32 = 20,
  232. 232 eps~ : Float = 0.001F,
  233. 233) -> Array[Float] raise @types.LoadError {
  234. 234 // 输入验证
  235. 235● if img.width <= 0 || img.height <= 0 {
  236. 236● raise @types.LoadError::DecodeFailed(
  237. 237 "guided_filter_f: 图像尺寸必须大于0",
  238. 238 )
  239. 239 }
  240. 240 if img.channels < 1 || img.channels > 4 {
  241. 241● raise @types.LoadError::DecodeFailed(
  242. 242 "guided_filter_f: 通道数必须在1-4之间",
  243. 243 )
  244. 244 }
  245. 245 let expected_size = img.width * img.height * img.channels * 4
  246. 246● if img.data.length() != expected_size {
  247. 247● raise @types.LoadError::DecodeFailed(
  248. 248 "guided_filter_f: 数据大小不匹配,期望 " +
  249. 249● expected_size.to_string() +
  250. 250 ",实际 " +
  251. 251● img.data.length().to_string(),
  252. 252 )
  253. 253 }
  254. 254 if radius < 1 {
  255. 255● raise @types.LoadError::DecodeFailed("guided_filter_f: radius必须>=1")
  256. 256 }
  257. 257 if eps <= 0.0F {
  258. 258● raise @types.LoadError::DecodeFailed("guided_filter_f: eps必须大于0")
  259. 259 }
  260. 260● let img8 = imgf_to_img8_dh(img)
  261. 261● guided_filter(img8, input, radius~, eps~)
  262. 262}