image.process\color\retinex_16f.mbt

92.5% · 236 / 255 points · 524 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/// # 多尺度 Retinex 16-bit/float 变体 Multi-scale Retinex 16-bit/float variants
  3. 3///
  4. 4/// 16位/浮点多尺度 Retinex msrcr_16 / msrcr_f
  5. 5/// 16位/浮点多尺度 Retinex 色彩恢复 16-bit/float Multi-Scale Retinex with Color Restoration
  6. 6// 多尺度 Retinex 16-bit/float 变体
  7. 7// SSR / MSR / MSRCR
  8. 8
  9. 9///|
  10. 10fn logf_r(x : Float) -> Float {
  11. 11● Float::from_double(@math.log2(x.to_double()) * 0.6931471805599453)
  12. 12}
  13. 13
  14. 14///|
  15. 15fn read_u16_r(data : Bytes, offset : Int32) -> Int32 {
  16. 16● data[offset].to_int() | (data[offset + 1].to_int() << 8)
  17. 17}
  18. 18
  19. 19///|
  20. 20fn write_u16_r(data : Array[Byte], offset : Int32, v : Int32) -> Unit {
  21. 21● data[offset] = (v & 0xFF).to_byte()
  22. 22● data[offset + 1] = ((v >> 8) & 0xFF).to_byte()
  23. 23}
  24. 24
  25. 25///|
  26. 26fn read_f32_r(data : Bytes, offset : Int32) -> Float {
  27. 27● let b0 = data[offset].to_int()
  28. 28● let b1 = data[offset + 1].to_int()
  29. 29● let b2 = data[offset + 2].to_int()
  30. 30● let b3 = data[offset + 3].to_int()
  31. 31 let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
  32. 32● Float::reinterpret_from_int(bits)
  33. 33}
  34. 34
  35. 35///|
  36. 36fn write_f32_r(data : Array[Byte], offset : Int32, v : Float) -> Unit {
  37. 37● let bits = v.reinterpret_as_int()
  38. 38● data[offset] = (bits & 0xFF).to_byte()
  39. 39● data[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  40. 40● data[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  41. 41● data[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
  42. 42}
  43. 43
  44. 44// ========== SSR 16-bit ==========
  45. 45
  46. 46///|
  47. 47/// 单尺度 Retinex(SSR)16-bit 版本。
  48. 48/// @see ssr 8-bit 版本,本函数为 Image16 变体
  49. 49pub fn ssr_16(
  50. 50 img : @types.Image16,
  51. 51 sigma~ : Float = 80.0F,
  52. 52 gain~ : Float = 1.0F,
  53. 53 offset~ : Float = 0.0F,
  54. 54) -> @types.Image16 raise @types.LoadError {
  55. 55 // 输入验证
  56. 56● if img.width == 0 || img.height == 0 {
  57. 57● return img
  58. 58 }
  59. 59 if img.width < 0 || img.height < 0 {
  60. 60● raise @types.LoadError::DecodeFailed("ssr_16: 图像尺寸不能为负")
  61. 61 }
  62. 62 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  63. 63● raise @types.LoadError::DecodeFailed("ssr_16 仅支持 channels=1/3/4")
  64. 64 }
  65. 65 let expected_size = img.width * img.height * img.channels * 2
  66. 66● if img.data.length() != expected_size {
  67. 67● raise @types.LoadError::DecodeFailed(
  68. 68 "ssr_16: 数据大小不匹配,期望 " +
  69. 69● expected_size.to_string() +
  70. 70 ",实际 " +
  71. 71● img.data.length().to_string(),
  72. 72 )
  73. 73 }
  74. 74 if sigma <= 0.0F {
  75. 75● raise @types.LoadError::DecodeFailed("sigma 必须 > 0")
  76. 76 }
  77. 77 let ch = img.channels
  78. 78 let w = img.width
  79. 79 let h = img.height
  80. 80 let n = w * h
  81. 81 if n == 0 {
  82. 82○ return img
  83. 83 }
  84. 84● let out : Array[Byte] = Array::make(n * ch * 2, b'\x00')
  85. 85 for c in 0..<ch {
  86. 86● if ch == 4 && c == 3 {
  87. 87● for i in 0..<n {
  88. 88● let v = read_u16_r(img.data, (i * ch + 3) * 2)
  89. 89● write_u16_r(out, (i * ch + 3) * 2, v)
  90. 90 }
  91. 91 continue
  92. 92 }
  93. 93● let channel : Array[Float] = Array::make(n, 0.0F)
  94. 94 for i in 0..<n {
  95. 95● let v = read_u16_r(img.data, (i * ch + c) * 2)
  96. 96◐ channel[i] = Float::from_int(if v == 0 { 1 } else { v })
  97. 97 }
  98. 98● let blurred = gaussian_blur_channel(channel, w, h, sigma)
  99. 99 for i in 0..<n {
  100. 100● let log_i = logf_r(channel[i])
  101. 101◐ let log_b = logf_r(if blurred[i] > 0.0F { blurred[i] } else { 1.0F })
  102. 102 let retinex = (log_i - log_b) * gain + offset
  103. 103● let val = (retinex * 30.0F + 32768.0F).to_int()
  104. 104◐ let clamped = if val < 0 { 0 } else if val > 65535 { 65535 } else { val }
  105. 105● write_u16_r(out, (i * ch + c) * 2, clamped)
  106. 106 }
  107. 107 }
  108. 108 @types.Image16::{
  109. 109 width: w,
  110. 110 height: h,
  111. 111 channels: ch,
  112. 112● data: Bytes::from_array(out),
  113. 113 }
  114. 114}
  115. 115
  116. 116// ========== SSR float ==========
  117. 117
  118. 118///|
  119. 119/// 单尺度 Retinex(SSR)float 版本。
  120. 120/// @see ssr 8-bit 版本,本函数为 ImageF 变体
  121. 121pub fn ssr_f(
  122. 122 img : @types.ImageF,
  123. 123 sigma~ : Float = 80.0F,
  124. 124 gain~ : Float = 1.0F,
  125. 125 offset~ : Float = 0.0F,
  126. 126) -> @types.ImageF raise @types.LoadError {
  127. 127 // 输入验证
  128. 128● if img.width == 0 || img.height == 0 {
  129. 129● return img
  130. 130 }
  131. 131 if img.width < 0 || img.height < 0 {
  132. 132● raise @types.LoadError::DecodeFailed("ssr_f: 图像尺寸不能为负")
  133. 133 }
  134. 134 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  135. 135● raise @types.LoadError::DecodeFailed("ssr_f 仅支持 channels=1/3/4")
  136. 136 }
  137. 137 let expected_size = img.width * img.height * img.channels * 4
  138. 138● if img.data.length() != expected_size {
  139. 139● raise @types.LoadError::DecodeFailed(
  140. 140 "ssr_f: 数据大小不匹配,期望 " +
  141. 141● expected_size.to_string() +
  142. 142 ",实际 " +
  143. 143● img.data.length().to_string(),
  144. 144 )
  145. 145 }
  146. 146 if sigma <= 0.0F {
  147. 147● raise @types.LoadError::DecodeFailed("sigma 必须 > 0")
  148. 148 }
  149. 149 let ch = img.channels
  150. 150 let w = img.width
  151. 151 let h = img.height
  152. 152 let n = w * h
  153. 153 if n == 0 {
  154. 154○ return img
  155. 155 }
  156. 156● let out : Array[Byte] = Array::make(n * ch * 4, b'\x00')
  157. 157 for c in 0..<ch {
  158. 158● if ch == 4 && c == 3 {
  159. 159● for i in 0..<n {
  160. 160● let v = read_f32_r(img.data, (i * ch + 3) * 4)
  161. 161● write_f32_r(out, (i * ch + 3) * 4, v)
  162. 162 }
  163. 163 continue
  164. 164 }
  165. 165● let channel : Array[Float] = Array::make(n, 0.0F)
  166. 166 for i in 0..<n {
  167. 167● let v = read_f32_r(img.data, (i * ch + c) * 4)
  168. 168● channel[i] = if v <= 0.0F { 0.0001F } else { v }
  169. 169 }
  170. 170● let blurred = gaussian_blur_channel(channel, w, h, sigma)
  171. 171 for i in 0..<n {
  172. 172● let log_i = logf_r(channel[i])
  173. 173◐ let log_b = logf_r(if blurred[i] > 0.0F { blurred[i] } else { 1.0F })
  174. 174 let retinex = (log_i - log_b) * gain + offset
  175. 175● write_f32_r(out, (i * ch + c) * 4, retinex)
  176. 176 }
  177. 177 }
  178. 178 @types.ImageF::{
  179. 179 width: w,
  180. 180 height: h,
  181. 181 channels: ch,
  182. 182● data: Bytes::from_array(out),
  183. 183 }
  184. 184}
  185. 185
  186. 186// ========== MSR 16-bit ==========
  187. 187
  188. 188///|
  189. 189/// 多尺度 Retinex(MSR)16-bit 版本。
  190. 190/// @see msr 8-bit 版本,本函数为 Image16 变体
  191. 191pub fn msr_16(
  192. 192 img : @types.Image16,
  193. 193 sigmas~ : Array[Float] = [15.0F, 80.0F, 250.0F],
  194. 194 gain~ : Float = 1.0F,
  195. 195 offset~ : Float = 0.0F,
  196. 196) -> @types.Image16 raise @types.LoadError {
  197. 197 // 输入验证
  198. 198● if img.width <= 0 || img.height <= 0 {
  199. 199● raise @types.LoadError::DecodeFailed("msr_16: 图像尺寸必须大于0")
  200. 200 }
  201. 201 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  202. 202● raise @types.LoadError::DecodeFailed("msr_16 仅支持 channels=1/3/4")
  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 "msr_16: 数据大小不匹配,期望 " +
  208. 208● expected_size.to_string() +
  209. 209 ",实际 " +
  210. 210● img.data.length().to_string(),
  211. 211 )
  212. 212 }
  213. 213● if sigmas.length() == 0 {
  214. 214● raise @types.LoadError::DecodeFailed("sigmas 不能为空")
  215. 215 }
  216. 216 for s in sigmas {
  217. 217● if s <= 0.0F {
  218. 218● raise @types.LoadError::DecodeFailed("sigma 必须 > 0")
  219. 219 }
  220. 220 }
  221. 221 let ch = img.channels
  222. 222 let w = img.width
  223. 223 let h = img.height
  224. 224 let n = w * h
  225. 225 if n == 0 {
  226. 226○ return img
  227. 227 }
  228. 228● let out : Array[Byte] = Array::make(n * ch * 2, b'\x00')
  229. 229● let num_scales = sigmas.length()
  230. 230 for c in 0..<ch {
  231. 231● if ch == 4 && c == 3 {
  232. 232● for i in 0..<n {
  233. 233● let v = read_u16_r(img.data, (i * ch + 3) * 2)
  234. 234● write_u16_r(out, (i * ch + 3) * 2, v)
  235. 235 }
  236. 236 continue
  237. 237 }
  238. 238● let channel : Array[Float] = Array::make(n, 0.0F)
  239. 239 for i in 0..<n {
  240. 240● let v = read_u16_r(img.data, (i * ch + c) * 2)
  241. 241◐ channel[i] = Float::from_int(if v == 0 { 1 } else { v })
  242. 242 }
  243. 243● let log_channel : Array[Float] = Array::make(n, 0.0F)
  244. 244 for i in 0..<n {
  245. 245● log_channel[i] = logf_r(channel[i])
  246. 246 }
  247. 247● let retinex_sum : Array[Float] = Array::make(n, 0.0F)
  248. 248 for sigma in sigmas {
  249. 249● let blurred = gaussian_blur_channel(channel, w, h, sigma)
  250. 250 for i in 0..<n {
  251. 251◐ let log_b = logf_r(if blurred[i] > 0.0F { blurred[i] } else { 1.0F })
  252. 252 retinex_sum[i] += log_channel[i] - log_b
  253. 253 }
  254. 254 }
  255. 255● let inv_scales = gain / Float::from_int(num_scales)
  256. 256 for i in 0..<n {
  257. 257● let retinex = retinex_sum[i] * inv_scales + offset
  258. 258● let val = (retinex * 30.0F + 32768.0F).to_int()
  259. 259◐ let clamped = if val < 0 { 0 } else if val > 65535 { 65535 } else { val }
  260. 260● write_u16_r(out, (i * ch + c) * 2, clamped)
  261. 261 }
  262. 262 }
  263. 263 @types.Image16::{
  264. 264 width: w,
  265. 265 height: h,
  266. 266 channels: ch,
  267. 267● data: Bytes::from_array(out),
  268. 268 }
  269. 269}
  270. 270
  271. 271// ========== MSR float ==========
  272. 272
  273. 273///|
  274. 274/// 多尺度 Retinex(MSR)float 版本。
  275. 275/// @see msr 8-bit 版本,本函数为 ImageF 变体
  276. 276pub fn msr_f(
  277. 277 img : @types.ImageF,
  278. 278 sigmas~ : Array[Float] = [15.0F, 80.0F, 250.0F],
  279. 279 gain~ : Float = 1.0F,
  280. 280 offset~ : Float = 0.0F,
  281. 281) -> @types.ImageF raise @types.LoadError {
  282. 282● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  283. 283● raise @types.LoadError::DecodeFailed("msr_f 仅支持 channels=1/3/4")
  284. 284 }
  285. 285● if sigmas.length() == 0 {
  286. 286● raise @types.LoadError::DecodeFailed("sigmas 不能为空")
  287. 287 }
  288. 288 for s in sigmas {
  289. 289● if s <= 0.0F {
  290. 290● raise @types.LoadError::DecodeFailed("sigma 必须 > 0")
  291. 291 }
  292. 292 }
  293. 293 let ch = img.channels
  294. 294 let w = img.width
  295. 295 let h = img.height
  296. 296 let n = w * h
  297. 297 if n == 0 {
  298. 298● return img
  299. 299 }
  300. 300● let out : Array[Byte] = Array::make(n * ch * 4, b'\x00')
  301. 301● let num_scales = sigmas.length()
  302. 302 for c in 0..<ch {
  303. 303● if ch == 4 && c == 3 {
  304. 304● for i in 0..<n {
  305. 305● let v = read_f32_r(img.data, (i * ch + 3) * 4)
  306. 306● write_f32_r(out, (i * ch + 3) * 4, v)
  307. 307 }
  308. 308 continue
  309. 309 }
  310. 310● let channel : Array[Float] = Array::make(n, 0.0F)
  311. 311 for i in 0..<n {
  312. 312● let v = read_f32_r(img.data, (i * ch + c) * 4)
  313. 313● channel[i] = if v <= 0.0F { 0.0001F } else { v }
  314. 314 }
  315. 315● let log_channel : Array[Float] = Array::make(n, 0.0F)
  316. 316 for i in 0..<n {
  317. 317● log_channel[i] = logf_r(channel[i])
  318. 318 }
  319. 319● let retinex_sum : Array[Float] = Array::make(n, 0.0F)
  320. 320 for sigma in sigmas {
  321. 321● let blurred = gaussian_blur_channel(channel, w, h, sigma)
  322. 322 for i in 0..<n {
  323. 323◐ let log_b = logf_r(if blurred[i] > 0.0F { blurred[i] } else { 1.0F })
  324. 324 retinex_sum[i] += log_channel[i] - log_b
  325. 325 }
  326. 326 }
  327. 327● let inv_scales = gain / Float::from_int(num_scales)
  328. 328 for i in 0..<n {
  329. 329● let retinex = retinex_sum[i] * inv_scales + offset
  330. 330● write_f32_r(out, (i * ch + c) * 4, retinex)
  331. 331 }
  332. 332 }
  333. 333 @types.ImageF::{
  334. 334 width: w,
  335. 335 height: h,
  336. 336 channels: ch,
  337. 337● data: Bytes::from_array(out),
  338. 338 }
  339. 339}
  340. 340
  341. 341// ========== MSRCR 16-bit ==========
  342. 342
  343. 343///|
  344. 344/// 带颜色恢复的多尺度 Retinex(MSRCR)16-bit 版本。
  345. 345/// @see msrcr 8-bit 版本,本函数为 Image16 变体
  346. 346pub fn msrcr_16(
  347. 347 img : @types.Image16,
  348. 348 sigmas~ : Array[Float] = [15.0F, 80.0F, 250.0F],
  349. 349 gain~ : Float = 1.0F,
  350. 350 offset~ : Float = 0.0F,
  351. 351 alpha~ : Float = 125.0F,
  352. 352 beta~ : Float = 46.0F,
  353. 353) -> @types.Image16 raise @types.LoadError {
  354. 354 // 输入验证
  355. 355● if img.width <= 0 || img.height <= 0 {
  356. 356● raise @types.LoadError::DecodeFailed("msrcr_16: 图像尺寸必须大于0")
  357. 357 }
  358. 358 if img.channels != 3 && img.channels != 4 {
  359. 359● raise @types.LoadError::DecodeFailed("msrcr_16 需要 RGB/RGBA 图像")
  360. 360 }
  361. 361 let expected_size = img.width * img.height * img.channels * 2
  362. 362● if img.data.length() != expected_size {
  363. 363● raise @types.LoadError::DecodeFailed(
  364. 364 "msrcr_16: 数据大小不匹配,期望 " +
  365. 365● expected_size.to_string() +
  366. 366 ",实际 " +
  367. 367● img.data.length().to_string(),
  368. 368 )
  369. 369 }
  370. 370● if sigmas.length() == 0 {
  371. 371● raise @types.LoadError::DecodeFailed("sigmas 不能为空")
  372. 372 }
  373. 373 let ch = img.channels
  374. 374 let w = img.width
  375. 375 let h = img.height
  376. 376 let n = w * h
  377. 377 if n == 0 {
  378. 378○ return img
  379. 379 }
  380. 380● let out : Array[Byte] = Array::make(n * ch * 2, b'\x00')
  381. 381● let num_scales = sigmas.length()
  382. 382 let channels : Array[Array[Float]] = [
  383. 383● Array::make(n, 0.0F),
  384. 384● Array::make(n, 0.0F),
  385. 385● Array::make(n, 0.0F),
  386. 386 ]
  387. 387 for c in 0..<3 {
  388. 388● for i in 0..<n {
  389. 389● let v = read_u16_r(img.data, (i * ch + c) * 2)
  390. 390◐ channels[c][i] = Float::from_int(if v == 0 { 1 } else { v })
  391. 391 }
  392. 392 }
  393. 393 let msr_results : Array[Array[Float]] = [
  394. 394● Array::make(n, 0.0F),
  395. 395● Array::make(n, 0.0F),
  396. 396● Array::make(n, 0.0F),
  397. 397 ]
  398. 398 for c in 0..<3 {
  399. 399● let log_channel : Array[Float] = Array::make(n, 0.0F)
  400. 400 for i in 0..<n {
  401. 401● log_channel[i] = logf_r(channels[c][i])
  402. 402 }
  403. 403● let retinex_sum : Array[Float] = Array::make(n, 0.0F)
  404. 404 for sigma in sigmas {
  405. 405● let blurred = gaussian_blur_channel(channels[c], w, h, sigma)
  406. 406 for i in 0..<n {
  407. 407◐ let log_b = logf_r(if blurred[i] > 0.0F { blurred[i] } else { 1.0F })
  408. 408 retinex_sum[i] += log_channel[i] - log_b
  409. 409 }
  410. 410 }
  411. 411● let inv_scales = 1.0F / Float::from_int(num_scales)
  412. 412 for i in 0..<n {
  413. 413● msr_results[c][i] = retinex_sum[i] * inv_scales
  414. 414 }
  415. 415 }
  416. 416 for i in 0..<n {
  417. 417● let sum = channels[0][i] + channels[1][i] + channels[2][i]
  418. 418 for c in 0..<3 {
  419. 419● let crf = logf_r(alpha * channels[c][i] / (sum + 1.0F)) * beta
  420. 420 let val = msr_results[c][i] * gain + offset + crf
  421. 421● let mapped = (val * 30.0F + 32768.0F).to_int()
  422. 422 let clamped = if mapped < 0 {
  423. 423○ 0
  424. 424● } else if mapped > 65535 {
  425. 425○ 65535
  426. 426 } else {
  427. 427● mapped
  428. 428 }
  429. 429● write_u16_r(out, (i * ch + c) * 2, clamped)
  430. 430 }
  431. 431 if ch == 4 {
  432. 432● let v = read_u16_r(img.data, (i * ch + 3) * 2)
  433. 433● write_u16_r(out, (i * ch + 3) * 2, v)
  434. 434 }
  435. 435 }
  436. 436 @types.Image16::{
  437. 437 width: w,
  438. 438 height: h,
  439. 439 channels: ch,
  440. 440● data: Bytes::from_array(out),
  441. 441 }
  442. 442}
  443. 443
  444. 444// ========== MSRCR float ==========
  445. 445
  446. 446///|
  447. 447/// 带颜色恢复的多尺度 Retinex(MSRCR)float 版本。
  448. 448/// @see msrcr 8-bit 版本,本函数为 ImageF 变体
  449. 449pub fn msrcr_f(
  450. 450 img : @types.ImageF,
  451. 451 sigmas~ : Array[Float] = [15.0F, 80.0F, 250.0F],
  452. 452 gain~ : Float = 1.0F,
  453. 453 offset~ : Float = 0.0F,
  454. 454 alpha~ : Float = 125.0F,
  455. 455 beta~ : Float = 46.0F,
  456. 456) -> @types.ImageF raise @types.LoadError {
  457. 457● if img.channels != 3 && img.channels != 4 {
  458. 458● raise @types.LoadError::DecodeFailed("msrcr_f 需要 RGB/RGBA 图像")
  459. 459 }
  460. 460● if sigmas.length() == 0 {
  461. 461● raise @types.LoadError::DecodeFailed("sigmas 不能为空")
  462. 462 }
  463. 463 let ch = img.channels
  464. 464 let w = img.width
  465. 465 let h = img.height
  466. 466 let n = w * h
  467. 467 if n == 0 {
  468. 468● return img
  469. 469 }
  470. 470● let out : Array[Byte] = Array::make(n * ch * 4, b'\x00')
  471. 471● let num_scales = sigmas.length()
  472. 472 let channels : Array[Array[Float]] = [
  473. 473● Array::make(n, 0.0F),
  474. 474● Array::make(n, 0.0F),
  475. 475● Array::make(n, 0.0F),
  476. 476 ]
  477. 477 for c in 0..<3 {
  478. 478● for i in 0..<n {
  479. 479● let v = read_f32_r(img.data, (i * ch + c) * 4)
  480. 480● channels[c][i] = if v <= 0.0F { 0.0001F } else { v }
  481. 481 }
  482. 482 }
  483. 483 let msr_results : Array[Array[Float]] = [
  484. 484● Array::make(n, 0.0F),
  485. 485● Array::make(n, 0.0F),
  486. 486● Array::make(n, 0.0F),
  487. 487 ]
  488. 488 for c in 0..<3 {
  489. 489● let log_channel : Array[Float] = Array::make(n, 0.0F)
  490. 490 for i in 0..<n {
  491. 491● log_channel[i] = logf_r(channels[c][i])
  492. 492 }
  493. 493● let retinex_sum : Array[Float] = Array::make(n, 0.0F)
  494. 494 for sigma in sigmas {
  495. 495● let blurred = gaussian_blur_channel(channels[c], w, h, sigma)
  496. 496 for i in 0..<n {
  497. 497◐ let log_b = logf_r(if blurred[i] > 0.0F { blurred[i] } else { 1.0F })
  498. 498 retinex_sum[i] += log_channel[i] - log_b
  499. 499 }
  500. 500 }
  501. 501● let inv_scales = 1.0F / Float::from_int(num_scales)
  502. 502 for i in 0..<n {
  503. 503● msr_results[c][i] = retinex_sum[i] * inv_scales
  504. 504 }
  505. 505 }
  506. 506 for i in 0..<n {
  507. 507● let sum = channels[0][i] + channels[1][i] + channels[2][i]
  508. 508 for c in 0..<3 {
  509. 509● let crf = logf_r(alpha * channels[c][i] / (sum + 1.0F)) * beta
  510. 510 let val = msr_results[c][i] * gain + offset + crf
  511. 511● write_f32_r(out, (i * ch + c) * 4, val)
  512. 512 }
  513. 513 if ch == 4 {
  514. 514● let v = read_f32_r(img.data, (i * ch + 3) * 4)
  515. 515● write_f32_r(out, (i * ch + 3) * 4, v)
  516. 516 }
  517. 517 }
  518. 518 @types.ImageF::{
  519. 519 width: w,
  520. 520 height: h,
  521. 521 channels: ch,
  522. 522● data: Bytes::from_array(out),
  523. 523 }
  524. 524}