image.process\feature\histogram_16f.mbt

95.8% · 252 / 263 points · 674 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 变体 Histogram operations 16-bit/float variants
  3. 3///
  4. 4/// 16位直方图操作 histogram_16 / histogram_equalize_16
  5. 5/// 16位/浮点直方图操作 16-bit/float histogram operations
  6. 6// 直方图操作 16-bit/float 变体
  7. 7// 支持 Image16 / ImageF 两种类型
  8. 8
  9. 9// ========== 辅助函数 ==========
  10. 10
  11. 11///|
  12. 12fn read_u16_le_h(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_h(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_h(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_h(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// ========== histogram ==========
  42. 42
  43. 43///|
  44. 44/// 16-bit 直方图。返回 65536 个 bin 的计数数组。彩色图像先转灰度(BT.601)。
  45. 45/// @see histogram 8-bit 版本,本函数为 Image16 变体
  46. 46pub fn histogram_16(
  47. 47 img : @types.Image16,
  48. 48) -> Array[Int32] raise @types.LoadError {
  49. 49 // 输入验证
  50. 50● if img.width <= 0 || img.height <= 0 {
  51. 51● raise @types.LoadError::DecodeFailed(
  52. 52 "histogram_16: 图像尺寸必须大于0",
  53. 53 )
  54. 54 }
  55. 55 if img.channels < 1 || img.channels > 4 {
  56. 56● raise @types.LoadError::DecodeFailed(
  57. 57 "histogram_16: 通道数必须在1-4之间",
  58. 58 )
  59. 59 }
  60. 60 let expected_size = img.width * img.height * img.channels * 2
  61. 61● if img.data.length() != expected_size {
  62. 62● raise @types.LoadError::DecodeFailed(
  63. 63 "histogram_16: 数据大小不匹配,期望 " +
  64. 64● expected_size.to_string() +
  65. 65 ",实际 " +
  66. 66● img.data.length().to_string(),
  67. 67 )
  68. 68 }
  69. 69
  70. 70● let hist : Array[Int32] = Array::make(65536, 0)
  71. 71 let n = img.width * img.height
  72. 72 let ch = img.channels
  73. 73 for i in 0..<n {
  74. 74● let v = if ch == 1 {
  75. 75● read_u16_le_h(img.data, i * 2)
  76. 76 } else {
  77. 77● let r = read_u16_le_h(img.data, i * ch * 2)
  78. 78● let g = read_u16_le_h(img.data, i * ch * 2 + 2)
  79. 79● let b = read_u16_le_h(img.data, i * ch * 2 + 4)
  80. 80 (r * 299 + g * 587 + b * 114) / 1000
  81. 81 }
  82. 82 hist[v] += 1
  83. 83 }
  84. 84 hist
  85. 85}
  86. 86
  87. 87///|
  88. 88/// float 直方图。将值归一化到 [0,1] 后分 256 个 bin。彩色图像先转灰度。
  89. 89/// @see histogram 8-bit 版本,本函数为 ImageF 变体
  90. 90pub fn histogram_f(img : @types.ImageF) -> Array[Int32] raise @types.LoadError {
  91. 91 // 输入验证
  92. 92● if img.width <= 0 || img.height <= 0 {
  93. 93● raise @types.LoadError::DecodeFailed(
  94. 94 "histogram_f: 图像尺寸必须大于0",
  95. 95 )
  96. 96 }
  97. 97 if img.channels < 1 || img.channels > 4 {
  98. 98● raise @types.LoadError::DecodeFailed(
  99. 99 "histogram_f: 通道数必须在1-4之间",
  100. 100 )
  101. 101 }
  102. 102 let expected_size = img.width * img.height * img.channels * 4
  103. 103● if img.data.length() != expected_size {
  104. 104● raise @types.LoadError::DecodeFailed(
  105. 105 "histogram_f: 数据大小不匹配,期望 " +
  106. 106● expected_size.to_string() +
  107. 107 ",实际 " +
  108. 108● img.data.length().to_string(),
  109. 109 )
  110. 110 }
  111. 111
  112. 112● let hist : Array[Int32] = Array::make(256, 0)
  113. 113 let n = img.width * img.height
  114. 114 let ch = img.channels
  115. 115 for i in 0..<n {
  116. 116● let v = if ch == 1 {
  117. 117● read_float_le_h(img.data, i * 4)
  118. 118 } else {
  119. 119● let r = read_float_le_h(img.data, i * ch * 4)
  120. 120● let g = read_float_le_h(img.data, i * ch * 4 + 4)
  121. 121● let b = read_float_le_h(img.data, i * ch * 4 + 8)
  122. 122 r * 0.299F + g * 0.587F + b * 0.114F
  123. 123 }
  124. 124 let bin = if v < 0.0F {
  125. 125● 0
  126. 126● } else if v >= 1.0F {
  127. 127● 255
  128. 128 } else {
  129. 129● (v * 255.0F + 0.5F).to_int()
  130. 130 }
  131. 131 hist[bin] += 1
  132. 132 }
  133. 133 hist
  134. 134}
  135. 135
  136. 136// ========== histogram_equalize ==========
  137. 137
  138. 138///|
  139. 139/// 16-bit 直方图均衡化。基于灰度直方图计算映射 LUT,应用到所有颜色通道。
  140. 140/// @see histogram_equalize 8-bit 版本,本函数为 Image16 变体
  141. 141pub fn histogram_equalize_16(
  142. 142 img : @types.Image16,
  143. 143) -> @types.Image16 raise @types.LoadError {
  144. 144● let hist = histogram_16(img)
  145. 145 let n = img.width * img.height
  146. 146 let ch = img.channels
  147. 147 let bpp = ch * 2
  148. 148 // 计算 CDF
  149. 149● let cdf : Array[Int32] = Array::make(65536, 0)
  150. 150 let mut cum = 0
  151. 151 for i in 0..<65536 {
  152. 152● cum += hist[i]
  153. 153 cdf[i] = cum
  154. 154 }
  155. 155 let mut cdf_min = 0
  156. 156 for i in 0..<65536 {
  157. 157● if cdf[i] > 0 {
  158. 158● cdf_min = cdf[i]
  159. 159 break
  160. 160 }
  161. 161 }
  162. 162 let denom = n - cdf_min
  163. 163 if denom <= 0 {
  164. 164● return img
  165. 165 }
  166. 166 // 计算映射 LUT
  167. 167● let lut : Array[Int32] = Array::make(65536, 0)
  168. 168 for i in 0..<65536 {
  169. 169● let mapped = (cdf[i] - cdf_min) * 65535 / denom
  170. 170● lut[i] = @types.clamp_int(mapped, 0, 65535)
  171. 171 }
  172. 172 // 应用 LUT
  173. 173● let color_ch = if ch >= 3 { 3 } else { ch }
  174. 174 let n_total = n * ch
  175. 175● let src : Array[Int32] = Array::make(n_total, 0)
  176. 176 for i in 0..<n_total {
  177. 177● src[i] = read_u16_le_h(img.data, i * 2)
  178. 178 }
  179. 179● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  180. 180 for i in 0..<n {
  181. 181● for c in 0..<color_ch {
  182. 182● write_u16_le_h(out, i * bpp + c * 2, lut[src[i * ch + c]])
  183. 183 }
  184. 184 if ch == 4 {
  185. 185○ let alpha_off = i * bpp + 6
  186. 186 out[alpha_off] = img.data[alpha_off]
  187. 187 out[alpha_off + 1] = img.data[alpha_off + 1]
  188. 188 }
  189. 189 }
  190. 190 @types.Image16::{
  191. 191 width: img.width,
  192. 192 height: img.height,
  193. 193 channels: ch,
  194. 194● data: Bytes::from_array(out),
  195. 195 }
  196. 196}
  197. 197
  198. 198///|
  199. 199/// float 直方图均衡化。基于灰度直方图计算映射 LUT,应用到所有颜色通道。
  200. 200/// @see histogram_equalize 8-bit 版本,本函数为 ImageF 变体
  201. 201pub fn histogram_equalize_f(
  202. 202 img : @types.ImageF,
  203. 203) -> @types.ImageF raise @types.LoadError {
  204. 204● let hist = histogram_f(img)
  205. 205 let n = img.width * img.height
  206. 206 let ch = img.channels
  207. 207 let bpp = ch * 4
  208. 208● let cdf : Array[Int32] = Array::make(256, 0)
  209. 209 let mut cum = 0
  210. 210 for i in 0..<256 {
  211. 211● cum += hist[i]
  212. 212 cdf[i] = cum
  213. 213 }
  214. 214 let mut cdf_min = 0
  215. 215 for i in 0..<256 {
  216. 216● if cdf[i] > 0 {
  217. 217● cdf_min = cdf[i]
  218. 218 break
  219. 219 }
  220. 220 }
  221. 221 let denom = n - cdf_min
  222. 222 if denom <= 0 {
  223. 223● return img
  224. 224 }
  225. 225● let lut : Array[Float] = Array::make(256, 0.0F)
  226. 226 for i in 0..<256 {
  227. 227● let mapped = Float::from_int(cdf[i] - cdf_min) / Float::from_int(denom)
  228. 228● lut[i] = @types.clamp_float(mapped, 0.0F, 1.0F)
  229. 229 }
  230. 230◐ let color_ch = if ch >= 3 { 3 } else { ch }
  231. 231 let n_total = n * ch
  232. 232● let src : Array[Float] = Array::make(n_total, 0.0F)
  233. 233 for i in 0..<n_total {
  234. 234● src[i] = read_float_le_h(img.data, i * 4)
  235. 235 }
  236. 236● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  237. 237 for i in 0..<n {
  238. 238● for c in 0..<color_ch {
  239. 239● let v = src[i * ch + c]
  240. 240 let bin = if v < 0.0F {
  241. 241● 0
  242. 242● } else if v >= 1.0F {
  243. 243● 255
  244. 244 } else {
  245. 245● (v * 255.0F + 0.5F).to_int()
  246. 246 }
  247. 247● write_f32_le_h(out, i * bpp + c * 4, lut[bin])
  248. 248 }
  249. 249 if ch == 4 {
  250. 250○ let alpha_off = i * bpp + 12
  251. 251 for b in 0..<4 {
  252. 252○ out[alpha_off + b] = img.data[alpha_off + b]
  253. 253 }
  254. 254 }
  255. 255 }
  256. 256 @types.ImageF::{
  257. 257 width: img.width,
  258. 258 height: img.height,
  259. 259 channels: ch,
  260. 260● data: Bytes::from_array(out),
  261. 261 }
  262. 262}
  263. 263
  264. 264// ========== histogram_normalize ==========
  265. 265
  266. 266///|
  267. 267/// 16-bit 直方图归一化(线性拉伸)。将像素值范围线性映射到 [0, 65535]。
  268. 268/// @see histogram_normalize 8-bit 版本,本函数为 Image16 变体
  269. 269pub fn histogram_normalize_16(
  270. 270 img : @types.Image16,
  271. 271) -> @types.Image16 raise @types.LoadError {
  272. 272 // 输入验证
  273. 273● if img.width <= 0 || img.height <= 0 {
  274. 274● raise @types.LoadError::DecodeFailed(
  275. 275 "histogram_normalize_16: 图像尺寸必须大于0",
  276. 276 )
  277. 277 }
  278. 278 if img.channels < 1 || img.channels > 4 {
  279. 279● raise @types.LoadError::DecodeFailed(
  280. 280 "histogram_normalize_16: 通道数必须在1-4之间",
  281. 281 )
  282. 282 }
  283. 283 let expected_size = img.width * img.height * img.channels * 2
  284. 284● if img.data.length() != expected_size {
  285. 285● raise @types.LoadError::DecodeFailed(
  286. 286 "histogram_normalize_16: 数据大小不匹配,期望 " +
  287. 287● expected_size.to_string() +
  288. 288 ",实际 " +
  289. 289● img.data.length().to_string(),
  290. 290 )
  291. 291 }
  292. 292
  293. 293 let ch = img.channels
  294. 294● let color_ch = if ch >= 3 { 3 } else { ch }
  295. 295 let n = img.width * img.height
  296. 296 let bpp = ch * 2
  297. 297 let n_total = n * ch
  298. 298● let src : Array[Int32] = Array::make(n_total, 0)
  299. 299 for i in 0..<n_total {
  300. 300● src[i] = read_u16_le_h(img.data, i * 2)
  301. 301 }
  302. 302 let mut min_val = 65535
  303. 303 let mut max_val = 0
  304. 304 for i in 0..<n {
  305. 305● for c in 0..<color_ch {
  306. 306● let v = src[i * ch + c]
  307. 307 if v < min_val {
  308. 308● min_val = v
  309. 309 }
  310. 310 if v > max_val {
  311. 311● max_val = v
  312. 312 }
  313. 313 }
  314. 314 }
  315. 315 let range = max_val - min_val
  316. 316 if range == 0 {
  317. 317● return img
  318. 318 }
  319. 319● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  320. 320 for i in 0..<n {
  321. 321● for c in 0..<color_ch {
  322. 322● let v = src[i * ch + c]
  323. 323 let scaled = (v - min_val) * 65535 / range
  324. 324● write_u16_le_h(out, i * bpp + c * 2, @types.clamp_int(scaled, 0, 65535))
  325. 325 }
  326. 326 if ch == 4 {
  327. 327● let alpha_off = i * bpp + 6
  328. 328 out[alpha_off] = img.data[alpha_off]
  329. 329 out[alpha_off + 1] = img.data[alpha_off + 1]
  330. 330 }
  331. 331 }
  332. 332 @types.Image16::{
  333. 333 width: img.width,
  334. 334 height: img.height,
  335. 335 channels: ch,
  336. 336● data: Bytes::from_array(out),
  337. 337 }
  338. 338}
  339. 339
  340. 340///|
  341. 341/// float 直方图归一化(线性拉伸)。将像素值范围线性映射到 [0.0, 1.0]。
  342. 342/// @see histogram_normalize 8-bit 版本,本函数为 ImageF 变体
  343. 343pub fn histogram_normalize_f(
  344. 344 img : @types.ImageF,
  345. 345) -> @types.ImageF raise @types.LoadError {
  346. 346 // 输入验证
  347. 347● if img.width <= 0 || img.height <= 0 {
  348. 348● raise @types.LoadError::DecodeFailed(
  349. 349 "histogram_normalize_f: 图像尺寸必须大于0",
  350. 350 )
  351. 351 }
  352. 352 if img.channels < 1 || img.channels > 4 {
  353. 353● raise @types.LoadError::DecodeFailed(
  354. 354 "histogram_normalize_f: 通道数必须在1-4之间",
  355. 355 )
  356. 356 }
  357. 357 let expected_size = img.width * img.height * img.channels * 4
  358. 358● if img.data.length() != expected_size {
  359. 359● raise @types.LoadError::DecodeFailed(
  360. 360 "histogram_normalize_f: 数据大小不匹配,期望 " +
  361. 361● expected_size.to_string() +
  362. 362 ",实际 " +
  363. 363● img.data.length().to_string(),
  364. 364 )
  365. 365 }
  366. 366
  367. 367 let ch = img.channels
  368. 368● let color_ch = if ch >= 3 { 3 } else { ch }
  369. 369 let n = img.width * img.height
  370. 370 let bpp = ch * 4
  371. 371 let n_total = n * ch
  372. 372● let src : Array[Float] = Array::make(n_total, 0.0F)
  373. 373 for i in 0..<n_total {
  374. 374● src[i] = read_float_le_h(img.data, i * 4)
  375. 375 }
  376. 376 let mut min_val = 1.0F
  377. 377 let mut max_val = 0.0F
  378. 378 for i in 0..<n {
  379. 379● for c in 0..<color_ch {
  380. 380● let v = src[i * ch + c]
  381. 381 if v < min_val {
  382. 382● min_val = v
  383. 383 }
  384. 384 if v > max_val {
  385. 385● max_val = v
  386. 386 }
  387. 387 }
  388. 388 }
  389. 389 let range = max_val - min_val
  390. 390 if range <= 0.000001F {
  391. 391● return img
  392. 392 }
  393. 393● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  394. 394 for i in 0..<n {
  395. 395● for c in 0..<color_ch {
  396. 396● let v = src[i * ch + c]
  397. 397 let scaled = (v - min_val) / range
  398. 398● write_f32_le_h(
  399. 399 out,
  400. 400 i * bpp + c * 4,
  401. 401● @types.clamp_float(scaled, 0.0F, 1.0F),
  402. 402 )
  403. 403 }
  404. 404 if ch == 4 {
  405. 405● let alpha_off = i * bpp + 12
  406. 406 for b in 0..<4 {
  407. 407● out[alpha_off + b] = img.data[alpha_off + b]
  408. 408 }
  409. 409 }
  410. 410 }
  411. 411 @types.ImageF::{
  412. 412 width: img.width,
  413. 413 height: img.height,
  414. 414 channels: ch,
  415. 415● data: Bytes::from_array(out),
  416. 416 }
  417. 417}
  418. 418
  419. 419// ========== histogram_matching ==========
  420. 420
  421. 421///|
  422. 422/// 16-bit 直方图匹配(规定化)。将输入图像的直方图匹配到参考图像的直方图。
  423. 423/// @see histogram_matching 8-bit 版本,本函数为 Image16 变体
  424. 424pub fn histogram_matching_16(
  425. 425 img : @types.Image16,
  426. 426 reference : @types.Image16,
  427. 427) -> @types.Image16 raise @types.LoadError {
  428. 428 // 输入验证
  429. 429● if img.width <= 0 || img.height <= 0 {
  430. 430● raise @types.LoadError::DecodeFailed(
  431. 431 "histogram_matching_16: 图像尺寸必须大于0",
  432. 432 )
  433. 433 }
  434. 434 if img.channels < 1 || img.channels > 4 {
  435. 435● raise @types.LoadError::DecodeFailed(
  436. 436 "histogram_matching_16: 通道数必须在1-4之间",
  437. 437 )
  438. 438 }
  439. 439 let expected_size = img.width * img.height * img.channels * 2
  440. 440● if img.data.length() != expected_size {
  441. 441● raise @types.LoadError::DecodeFailed(
  442. 442 "histogram_matching_16: 数据大小不匹配,期望 " +
  443. 443● expected_size.to_string() +
  444. 444 ",实际 " +
  445. 445● img.data.length().to_string(),
  446. 446 )
  447. 447 }
  448. 448 if reference.width <= 0 || reference.height <= 0 {
  449. 449○ raise @types.LoadError::DecodeFailed(
  450. 450 "histogram_matching_16: 参考图像尺寸必须大于0",
  451. 451 )
  452. 452 }
  453. 453 if reference.channels < 1 || reference.channels > 4 {
  454. 454● raise @types.LoadError::DecodeFailed(
  455. 455 "histogram_matching_16: 参考图像通道数必须在1-4之间",
  456. 456 )
  457. 457 }
  458. 458 let ref_expected_size = reference.width *
  459. 459 reference.height *
  460. 460 reference.channels *
  461. 461 2
  462. 462● if reference.data.length() != ref_expected_size {
  463. 463● raise @types.LoadError::DecodeFailed(
  464. 464 "histogram_matching_16: 参考图像数据大小不匹配,期望 " +
  465. 465● ref_expected_size.to_string() +
  466. 466 ",实际 " +
  467. 467● reference.data.length().to_string(),
  468. 468 )
  469. 469 }
  470. 470
  471. 471 let ch = img.channels
  472. 472● let color_ch = if ch >= 3 { 3 } else { ch }
  473. 473 let n = img.width * img.height
  474. 474 let bpp = ch * 2
  475. 475● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  476. 476 let n_ref = reference.width * reference.height
  477. 477 let ref_ch = reference.channels
  478. 478● let ref_color_ch = if ref_ch >= 3 { 3 } else { ref_ch }
  479. 479 for c in 0..<color_ch {
  480. 480● let hist_in : Array[Int32] = Array::make(65536, 0)
  481. 481● let hist_ref : Array[Int32] = Array::make(65536, 0)
  482. 482 for i in 0..<n {
  483. 483● hist_in[read_u16_le_h(img.data, i * bpp + c * 2)] += 1
  484. 484 }
  485. 485 if c < ref_color_ch {
  486. 486● for i in 0..<n_ref {
  487. 487● hist_ref[read_u16_le_h(reference.data, i * ref_ch * 2 + c * 2)] += 1
  488. 488 }
  489. 489 }
  490. 490● let cdf_in : Array[Float] = Array::make(65536, 0.0F)
  491. 491● let cdf_ref : Array[Float] = Array::make(65536, 0.0F)
  492. 492 let mut cum_in = 0
  493. 493 let mut cum_ref = 0
  494. 494 for i in 0..<65536 {
  495. 495● cum_in += hist_in[i]
  496. 496● cdf_in[i] = Float::from_int(cum_in) / Float::from_int(n)
  497. 497 cum_ref += hist_ref[i]
  498. 498● cdf_ref[i] = Float::from_int(cum_ref) / Float::from_int(n_ref)
  499. 499 }
  500. 500● let lut : Array[Int32] = Array::make(65536, 0)
  501. 501 for i in 0..<65536 {
  502. 502● let target = cdf_in[i]
  503. 503 let mut best_j = 0
  504. 504 let mut best_diff = 1.0F
  505. 505 for j in 0..<65536 {
  506. 506○ let diff = (cdf_ref[j] - target).abs()
  507. 507 if diff < best_diff {
  508. 508● best_diff = diff
  509. 509 best_j = j
  510. 510 }
  511. 511 }
  512. 512 lut[i] = best_j
  513. 513 }
  514. 514 for i in 0..<n {
  515. 515● let v = read_u16_le_h(img.data, i * bpp + c * 2)
  516. 516● write_u16_le_h(out, i * bpp + c * 2, lut[v])
  517. 517 }
  518. 518 }
  519. 519 if ch == 4 {
  520. 520● for i in 0..<n {
  521. 521● let alpha_off = i * bpp + 6
  522. 522 out[alpha_off] = img.data[alpha_off]
  523. 523 out[alpha_off + 1] = img.data[alpha_off + 1]
  524. 524 }
  525. 525 }
  526. 526 @types.Image16::{
  527. 527 width: img.width,
  528. 528 height: img.height,
  529. 529 channels: ch,
  530. 530● data: Bytes::from_array(out),
  531. 531 }
  532. 532}
  533. 533
  534. 534///|
  535. 535/// float 直方图匹配(规定化)。将输入图像的直方图匹配到参考图像的直方图。
  536. 536/// @see histogram_matching 8-bit 版本,本函数为 ImageF 变体
  537. 537pub fn histogram_matching_f(
  538. 538 img : @types.ImageF,
  539. 539 reference : @types.ImageF,
  540. 540) -> @types.ImageF raise @types.LoadError {
  541. 541 // 输入验证
  542. 542● if img.width <= 0 || img.height <= 0 {
  543. 543● raise @types.LoadError::DecodeFailed(
  544. 544 "histogram_matching_f: 图像尺寸必须大于0",
  545. 545 )
  546. 546 }
  547. 547 if img.channels < 1 || img.channels > 4 {
  548. 548● raise @types.LoadError::DecodeFailed(
  549. 549 "histogram_matching_f: 通道数必须在1-4之间",
  550. 550 )
  551. 551 }
  552. 552 let expected_size = img.width * img.height * img.channels * 4
  553. 553● if img.data.length() != expected_size {
  554. 554● raise @types.LoadError::DecodeFailed(
  555. 555 "histogram_matching_f: 数据大小不匹配,期望 " +
  556. 556● expected_size.to_string() +
  557. 557 ",实际 " +
  558. 558● img.data.length().to_string(),
  559. 559 )
  560. 560 }
  561. 561 if reference.width <= 0 || reference.height <= 0 {
  562. 562○ raise @types.LoadError::DecodeFailed(
  563. 563 "histogram_matching_f: 参考图像尺寸必须大于0",
  564. 564 )
  565. 565 }
  566. 566 if reference.channels < 1 || reference.channels > 4 {
  567. 567● raise @types.LoadError::DecodeFailed(
  568. 568 "histogram_matching_f: 参考图像通道数必须在1-4之间",
  569. 569 )
  570. 570 }
  571. 571 let ref_expected_size = reference.width *
  572. 572 reference.height *
  573. 573 reference.channels *
  574. 574 4
  575. 575● if reference.data.length() != ref_expected_size {
  576. 576● raise @types.LoadError::DecodeFailed(
  577. 577 "histogram_matching_f: 参考图像数据大小不匹配,期望 " +
  578. 578● ref_expected_size.to_string() +
  579. 579 ",实际 " +
  580. 580● reference.data.length().to_string(),
  581. 581 )
  582. 582 }
  583. 583
  584. 584 let ch = img.channels
  585. 585● let color_ch = if ch >= 3 { 3 } else { ch }
  586. 586 let n = img.width * img.height
  587. 587 let bpp = ch * 4
  588. 588● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  589. 589 let n_ref = reference.width * reference.height
  590. 590 let ref_ch = reference.channels
  591. 591● let ref_color_ch = if ref_ch >= 3 { 3 } else { ref_ch }
  592. 592 for c in 0..<color_ch {
  593. 593● let hist_in : Array[Int32] = Array::make(256, 0)
  594. 594● let hist_ref : Array[Int32] = Array::make(256, 0)
  595. 595 for i in 0..<n {
  596. 596● let v = read_float_le_h(img.data, i * bpp + c * 4)
  597. 597 let bin = if v < 0.0F {
  598. 598● 0
  599. 599● } else if v >= 1.0F {
  600. 600● 255
  601. 601 } else {
  602. 602● (v * 255.0F + 0.5F).to_int()
  603. 603 }
  604. 604 hist_in[bin] += 1
  605. 605 }
  606. 606 if c < ref_color_ch {
  607. 607● for i in 0..<n_ref {
  608. 608● let v = read_float_le_h(reference.data, i * ref_ch * 4 + c * 4)
  609. 609 let bin = if v < 0.0F {
  610. 610● 0
  611. 611● } else if v >= 1.0F {
  612. 612● 255
  613. 613 } else {
  614. 614● (v * 255.0F + 0.5F).to_int()
  615. 615 }
  616. 616 hist_ref[bin] += 1
  617. 617 }
  618. 618 }
  619. 619● let cdf_in : Array[Float] = Array::make(256, 0.0F)
  620. 620● let cdf_ref : Array[Float] = Array::make(256, 0.0F)
  621. 621 let mut cum_in = 0
  622. 622 let mut cum_ref = 0
  623. 623 for i in 0..<256 {
  624. 624● cum_in += hist_in[i]
  625. 625● cdf_in[i] = Float::from_int(cum_in) / Float::from_int(n)
  626. 626 cum_ref += hist_ref[i]
  627. 627● cdf_ref[i] = Float::from_int(cum_ref) / Float::from_int(n_ref)
  628. 628 }
  629. 629● let lut : Array[Int32] = Array::make(256, 0)
  630. 630 for i in 0..<256 {
  631. 631● let target = cdf_in[i]
  632. 632 let mut best_j = 0
  633. 633 let mut best_diff = 1.0F
  634. 634 for j in 0..<256 {
  635. 635● let diff = (cdf_ref[j] - target).abs()
  636. 636 if diff < best_diff {
  637. 637● best_diff = diff
  638. 638 best_j = j
  639. 639 }
  640. 640 }
  641. 641 lut[i] = best_j
  642. 642 }
  643. 643 for i in 0..<n {
  644. 644● let v = read_float_le_h(img.data, i * bpp + c * 4)
  645. 645 let bin = if v < 0.0F {
  646. 646● 0
  647. 647● } else if v >= 1.0F {
  648. 648● 255
  649. 649 } else {
  650. 650● (v * 255.0F + 0.5F).to_int()
  651. 651 }
  652. 652● let mapped = Float::from_int(lut[bin]) / 255.0F
  653. 653● write_f32_le_h(
  654. 654 out,
  655. 655 i * bpp + c * 4,
  656. 656● @types.clamp_float(mapped, 0.0F, 1.0F),
  657. 657 )
  658. 658 }
  659. 659 }
  660. 660 if ch == 4 {
  661. 661○ for i in 0..<n {
  662. 662○ let alpha_off = i * bpp + 12
  663. 663 for b in 0..<4 {
  664. 664○ out[alpha_off + b] = img.data[alpha_off + b]
  665. 665 }
  666. 666 }
  667. 667 }
  668. 668 @types.ImageF::{
  669. 669 width: img.width,
  670. 670 height: img.height,
  671. 671 channels: ch,
  672. 672● data: Bytes::from_array(out),
  673. 673 }
  674. 674}