image.photography\photo_enhance\hdr.mbt

99.3% · 146 / 147 points · 433 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/// # HDR 高动态范围 photo_enhance/hdr
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// HDR 高动态范围(v11.7.0)
  7. 7// 多曝光融合 + 色调映射(Reinhard / Gamma / Sigmoid)
  8. 8
  9. 9///|
  10. 10/// HDR 配置
  11. 11#derive(Eq, @debug.Debug)
  12. 12pub(all) struct HdrConfig {
  13. 13 fusion_method : String // "average" / "weighted" / "mertens"
  14. 14 tone_mapping : String // "reinhard" / "gamma" / "sigmoid"
  15. 15 gamma : Double // Gamma 校正值
  16. 16 reinhard_key : Double // Reinhard 色调映射关键值
  17. 17 sigmoid_strength : Double // Sigmoid 强度
  18. 18}
  19. 19
  20. 20///|
  21. 21pub extend HdrConfig with Eq::{not_equal, equal}
  22. 22
  23. 23///|
  24. 24pub extend HdrConfig with @debug.Debug::{to_repr}
  25. 25
  26. 26///|
  27. 27/// 创建默认 HDR 配置
  28. 28pub fn hdr_default_config() -> HdrConfig {
  29. 29● HdrConfig::{
  30. 30 fusion_method: "weighted",
  31. 31 tone_mapping: "reinhard",
  32. 32 gamma: 1.0 / 2.2,
  33. 33 reinhard_key: 0.18,
  34. 34 sigmoid_strength: 5.0,
  35. 35 }
  36. 36}
  37. 37
  38. 38///|
  39. 39/// 多曝光融合(平均法)
  40. 40/// 将多张不同曝光的图像取平均
  41. 41pub fn hdr_fusion_average(
  42. 42 images : Array[@types.Image],
  43. 43) -> @types.Image raise @types.LoadError {
  44. 44 // 输入验证
  45. 45● if images.length() == 0 {
  46. 46● raise @types.LoadError::DecodeFailed(
  47. 47 "hdr_fusion_average: 图像数组不能为空",
  48. 48 )
  49. 49 }
  50. 50 let w = images[0].width
  51. 51 let h = images[0].height
  52. 52 let channels = images[0].channels
  53. 53
  54. 54 // 验证第一张图像
  55. 55 if w <= 0 || h <= 0 {
  56. 56● raise @types.LoadError::DecodeFailed(
  57. 57 "hdr_fusion_average: 图像尺寸必须大于0",
  58. 58 )
  59. 59 }
  60. 60 if channels < 1 || channels > 4 {
  61. 61● raise @types.LoadError::DecodeFailed(
  62. 62 "hdr_fusion_average: 通道数必须在1-4之间",
  63. 63 )
  64. 64 }
  65. 65
  66. 66 // 验证所有图像尺寸一致且数据大小正确
  67. 67 let expected_size = w * h * channels
  68. 68● for img_idx in 0..<images.length() {
  69. 69● let img = images[img_idx]
  70. 70 if img.width != w || img.height != h || img.channels != channels {
  71. 71● raise @types.LoadError::DecodeFailed(
  72. 72 "hdr_fusion_average: 第 " +
  73. 73● img_idx.to_string() +
  74. 74 " 张图像尺寸/通道不一致",
  75. 75 )
  76. 76 }
  77. 77● if img.data.length() != expected_size {
  78. 78● raise @types.LoadError::DecodeFailed(
  79. 79 "hdr_fusion_average: 第 " +
  80. 80● img_idx.to_string() +
  81. 81 " 张图像数据大小不匹配",
  82. 82 )
  83. 83 }
  84. 84 }
  85. 85● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  86. 86● let n = images.length().to_double()
  87. 87 for i in 0..<(w * h) {
  88. 88● for c in 0..<channels {
  89. 89● let mut sum = 0.0
  90. 90 for img in images {
  91. 91● sum += img.data[i * channels + c].to_double()
  92. 92 }
  93. 93 let avg = sum / n
  94. 94● out_data[i * channels + c] = avg.min(255.0).max(0.0).to_int().to_byte()
  95. 95 }
  96. 96 }
  97. 97 @types.Image::{
  98. 98 width: w,
  99. 99 height: h,
  100. 100 channels,
  101. 101● data: Bytes::from_array(out_data),
  102. 102 }
  103. 103}
  104. 104
  105. 105///|
  106. 106/// 多曝光融合(加权法)
  107. 107/// 根据图像亮度计算权重,亮部和暗部权重低,中间调权重高
  108. 108pub fn hdr_fusion_weighted(
  109. 109 images : Array[@types.Image],
  110. 110) -> @types.Image raise @types.LoadError {
  111. 111 // 输入验证
  112. 112● if images.length() == 0 {
  113. 113● raise @types.LoadError::DecodeFailed(
  114. 114 "hdr_fusion_weighted: 图像数组不能为空",
  115. 115 )
  116. 116 }
  117. 117 let w = images[0].width
  118. 118 let h = images[0].height
  119. 119 let channels = images[0].channels
  120. 120
  121. 121 // 验证第一张图像
  122. 122 if w <= 0 || h <= 0 {
  123. 123● raise @types.LoadError::DecodeFailed(
  124. 124 "hdr_fusion_weighted: 图像尺寸必须大于0",
  125. 125 )
  126. 126 }
  127. 127 if channels < 1 || channels > 4 {
  128. 128● raise @types.LoadError::DecodeFailed(
  129. 129 "hdr_fusion_weighted: 通道数必须在1-4之间",
  130. 130 )
  131. 131 }
  132. 132
  133. 133 // 验证所有图像尺寸一致且数据大小正确
  134. 134 let expected_size = w * h * channels
  135. 135● for img_idx in 0..<images.length() {
  136. 136● let img = images[img_idx]
  137. 137 if img.width != w || img.height != h || img.channels != channels {
  138. 138● raise @types.LoadError::DecodeFailed(
  139. 139 "hdr_fusion_weighted: 第 " +
  140. 140● img_idx.to_string() +
  141. 141 " 张图像尺寸/通道不一致",
  142. 142 )
  143. 143 }
  144. 144● if img.data.length() != expected_size {
  145. 145● raise @types.LoadError::DecodeFailed(
  146. 146 "hdr_fusion_weighted: 第 " +
  147. 147● img_idx.to_string() +
  148. 148 " 张图像数据大小不匹配",
  149. 149 )
  150. 150 }
  151. 151 }
  152. 152● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  153. 153 // 计算每张图像的权重(基于亮度,中间调权重高)
  154. 154 let weights : Array[Array[Double]] = []
  155. 155 for img in images {
  156. 156● let img_weight : Array[Double] = []
  157. 157 for i in 0..<(w * h) {
  158. 158● let idx = i * channels
  159. 159● let r = img.data[idx].to_double()
  160. 160● let g = if channels >= 2 { img.data[idx + 1].to_double() } else { r }
  161. 161● let b = if channels >= 3 { img.data[idx + 2].to_double() } else { r }
  162. 162 let luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255.0
  163. 163 // 高斯权重:在 0.5 处最大,向两端递减
  164. 164● let weight = @math.exp(-(luminance - 0.5) * (luminance - 0.5) / 0.08)
  165. 165● img_weight.push(weight)
  166. 166 }
  167. 167● weights.push(img_weight)
  168. 168 }
  169. 169 // 加权融合
  170. 170 for i in 0..<(w * h) {
  171. 171● let mut weight_sum = 0.0
  172. 172● for img_idx in 0..<images.length() {
  173. 173● weight_sum += weights[img_idx][i]
  174. 174 }
  175. 175 for c in 0..<channels {
  176. 176● if weight_sum > 0.0 {
  177. 177● let mut sum = 0.0
  178. 178● for img_idx in 0..<images.length() {
  179. 179● let weight = weights[img_idx][i] / weight_sum
  180. 180● sum += images[img_idx].data[i * channels + c].to_double() * weight
  181. 181 }
  182. 182● out_data[i * channels + c] = sum.min(255.0).max(0.0).to_int().to_byte()
  183. 183 } else {
  184. 184○ out_data[i * channels + c] = images[0].data[i * channels + c]
  185. 185 }
  186. 186 }
  187. 187 }
  188. 188 @types.Image::{
  189. 189 width: w,
  190. 190 height: h,
  191. 191 channels,
  192. 192● data: Bytes::from_array(out_data),
  193. 193 }
  194. 194}
  195. 195
  196. 196///|
  197. 197/// Reinhard 色调映射
  198. 198/// L_ mapped = L / (1 + L) (全局色调映射)
  199. 199pub fn tone_mapping_reinhard(
  200. 200 img : @types.Image,
  201. 201 key~ : Double = 0.18,
  202. 202) -> @types.Image raise @types.LoadError {
  203. 203 // 输入验证
  204. 204● if img.width <= 0 || img.height <= 0 {
  205. 205● raise @types.LoadError::DecodeFailed(
  206. 206 "tone_mapping_reinhard: 图像尺寸必须大于0",
  207. 207 )
  208. 208 }
  209. 209 if img.channels < 1 || img.channels > 4 {
  210. 210● raise @types.LoadError::DecodeFailed(
  211. 211 "tone_mapping_reinhard: 通道数必须在1-4之间",
  212. 212 )
  213. 213 }
  214. 214 let expected_size = img.width * img.height * img.channels
  215. 215● if img.data.length() != expected_size {
  216. 216● raise @types.LoadError::DecodeFailed(
  217. 217 "tone_mapping_reinhard: 数据大小不匹配,期望 " +
  218. 218● expected_size.to_string() +
  219. 219 ",实际 " +
  220. 220● img.data.length().to_string(),
  221. 221 )
  222. 222 }
  223. 223 if key <= 0.0 {
  224. 224● raise @types.LoadError::DecodeFailed(
  225. 225 "tone_mapping_reinhard: key必须大于0",
  226. 226 )
  227. 227 }
  228. 228
  229. 229 let k = key
  230. 230 let w = img.width
  231. 231 let h = img.height
  232. 232 let channels = img.channels
  233. 233 let data = img.data
  234. 234 // 计算对数平均亮度
  235. 235 let mut log_sum = 0.0
  236. 236 let delta = 0.0001
  237. 237 for i in 0..<(w * h) {
  238. 238● let idx = i * channels
  239. 239● let r = data[idx].to_double() / 255.0
  240. 240● let g = if channels >= 2 { data[idx + 1].to_double() / 255.0 } else { r }
  241. 241● let b = if channels >= 3 { data[idx + 2].to_double() / 255.0 } else { r }
  242. 242 let luminance = 0.299 * r + 0.587 * g + 0.114 * b
  243. 243● log_sum += @math.ln(luminance + delta)
  244. 244 }
  245. 245● let log_avg = @math.exp(log_sum / (w * h).to_double())
  246. 246 // 应用 Reinhard 色调映射
  247. 247● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  248. 248 for i in 0..<(w * h) {
  249. 249● let idx = i * channels
  250. 250 for c in 0..<channels {
  251. 251● let val = data[idx + c].to_double() / 255.0
  252. 252 // 缩放
  253. 253 let scaled = val * k / (log_avg + delta)
  254. 254 // Reinhard 映射
  255. 255 let mapped = scaled / (1.0 + scaled)
  256. 256 out_data[idx + c] = (mapped * 255.0)
  257. 257● .min(255.0)
  258. 258● .max(0.0)
  259. 259● .to_int()
  260. 260● .to_byte()
  261. 261 }
  262. 262 }
  263. 263 @types.Image::{
  264. 264 width: w,
  265. 265 height: h,
  266. 266 channels,
  267. 267● data: Bytes::from_array(out_data),
  268. 268 }
  269. 269}
  270. 270
  271. 271///|
  272. 272/// Gamma 校正色调映射
  273. 273/// 伽马校正 out = in ^ gamma
  274. 274pub fn tone_mapping_gamma(
  275. 275 img : @types.Image,
  276. 276 gamma~ : Double = 1.0 / 2.2,
  277. 277) -> @types.Image raise @types.LoadError {
  278. 278 // 输入验证
  279. 279● if img.width <= 0 || img.height <= 0 {
  280. 280● raise @types.LoadError::DecodeFailed(
  281. 281 "tone_mapping_gamma: 图像尺寸必须大于0",
  282. 282 )
  283. 283 }
  284. 284 if img.channels < 1 || img.channels > 4 {
  285. 285● raise @types.LoadError::DecodeFailed(
  286. 286 "tone_mapping_gamma: 通道数必须在1-4之间",
  287. 287 )
  288. 288 }
  289. 289 let expected_size = img.width * img.height * img.channels
  290. 290● if img.data.length() != expected_size {
  291. 291● raise @types.LoadError::DecodeFailed(
  292. 292 "tone_mapping_gamma: 数据大小不匹配,期望 " +
  293. 293● expected_size.to_string() +
  294. 294 ",实际 " +
  295. 295● img.data.length().to_string(),
  296. 296 )
  297. 297 }
  298. 298 if gamma <= 0.0 {
  299. 299● raise @types.LoadError::DecodeFailed(
  300. 300 "tone_mapping_gamma: gamma必须大于0",
  301. 301 )
  302. 302 }
  303. 303
  304. 304 let g = gamma
  305. 305 let w = img.width
  306. 306 let h = img.height
  307. 307 let channels = img.channels
  308. 308 let data = img.data
  309. 309● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  310. 310 for i in 0..<(w * h) {
  311. 311● let idx = i * channels
  312. 312 for c in 0..<channels {
  313. 313● let val = data[idx + c].to_double() / 255.0
  314. 314● let mapped = @math.pow(val, g)
  315. 315 out_data[idx + c] = (mapped * 255.0)
  316. 316● .min(255.0)
  317. 317● .max(0.0)
  318. 318● .to_int()
  319. 319● .to_byte()
  320. 320 }
  321. 321 }
  322. 322 @types.Image::{
  323. 323 width: w,
  324. 324 height: h,
  325. 325 channels,
  326. 326● data: Bytes::from_array(out_data),
  327. 327 }
  328. 328}
  329. 329
  330. 330///|
  331. 331/// Sigmoid 色调映射
  332. 332/// Sigmoid 调整 out = 1 / (1 + exp(-strength * (in - 0.5)))
  333. 333pub fn tone_mapping_sigmoid(
  334. 334 img : @types.Image,
  335. 335 strength~ : Double = 5.0,
  336. 336) -> @types.Image raise @types.LoadError {
  337. 337 // 输入验证
  338. 338● if img.width <= 0 || img.height <= 0 {
  339. 339● raise @types.LoadError::DecodeFailed(
  340. 340 "tone_mapping_sigmoid: 图像尺寸必须大于0",
  341. 341 )
  342. 342 }
  343. 343 if img.channels < 1 || img.channels > 4 {
  344. 344● raise @types.LoadError::DecodeFailed(
  345. 345 "tone_mapping_sigmoid: 通道数必须在1-4之间",
  346. 346 )
  347. 347 }
  348. 348 let expected_size = img.width * img.height * img.channels
  349. 349● if img.data.length() != expected_size {
  350. 350● raise @types.LoadError::DecodeFailed(
  351. 351 "tone_mapping_sigmoid: 数据大小不匹配,期望 " +
  352. 352● expected_size.to_string() +
  353. 353 ",实际 " +
  354. 354● img.data.length().to_string(),
  355. 355 )
  356. 356 }
  357. 357 if strength <= 0.0 {
  358. 358● raise @types.LoadError::DecodeFailed(
  359. 359 "tone_mapping_sigmoid: strength必须大于0",
  360. 360 )
  361. 361 }
  362. 362
  363. 363 let s = strength
  364. 364 let w = img.width
  365. 365 let h = img.height
  366. 366 let channels = img.channels
  367. 367 let data = img.data
  368. 368● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  369. 369 for i in 0..<(w * h) {
  370. 370● let idx = i * channels
  371. 371 for c in 0..<channels {
  372. 372● let val = data[idx + c].to_double() / 255.0
  373. 373● let mapped = 1.0 / (1.0 + @math.exp(-s * (val - 0.5)))
  374. 374 out_data[idx + c] = (mapped * 255.0)
  375. 375● .min(255.0)
  376. 376● .max(0.0)
  377. 377● .to_int()
  378. 378● .to_byte()
  379. 379 }
  380. 380 }
  381. 381 @types.Image::{
  382. 382 width: w,
  383. 383 height: h,
  384. 384 channels,
  385. 385● data: Bytes::from_array(out_data),
  386. 386 }
  387. 387}
  388. 388
  389. 389///|
  390. 390/// 完整 HDR 流水线(多曝光融合 + 色调映射)
  391. 391pub fn hdr_process(
  392. 392 images : Array[@types.Image],
  393. 393 config? : HdrConfig,
  394. 394) -> @types.Image raise @types.LoadError {
  395. 395 // 输入验证
  396. 396● if images.length() == 0 {
  397. 397● raise @types.LoadError::DecodeFailed(
  398. 398 "hdr_process: 图像数组不能为空",
  399. 399 )
  400. 400 }
  401. 401
  402. 402● let cfg = match config {
  403. 403● Some(c) => c
  404. 404● None => hdr_default_config()
  405. 405 }
  406. 406
  407. 407 // 配置验证
  408. 408 if cfg.gamma <= 0.0 {
  409. 409● raise @types.LoadError::DecodeFailed("hdr_process: gamma必须大于0")
  410. 410 }
  411. 411 if cfg.reinhard_key <= 0.0 {
  412. 412● raise @types.LoadError::DecodeFailed(
  413. 413 "hdr_process: reinhard_key必须大于0",
  414. 414 )
  415. 415 }
  416. 416 if cfg.sigmoid_strength <= 0.0 {
  417. 417● raise @types.LoadError::DecodeFailed(
  418. 418 "hdr_process: sigmoid_strength必须大于0",
  419. 419 )
  420. 420 }
  421. 421 // 1. 多曝光融合
  422. 422● let fused = match cfg.fusion_method {
  423. 423● "average" => hdr_fusion_average(images)
  424. 424● _ => hdr_fusion_weighted(images)
  425. 425 }
  426. 426 // 2. 色调映射
  427. 427● let tonemapped = match cfg.tone_mapping {
  428. 428● "gamma" => tone_mapping_gamma(fused, gamma=cfg.gamma)
  429. 429● "sigmoid" => tone_mapping_sigmoid(fused, strength=cfg.sigmoid_strength)
  430. 430● _ => tone_mapping_reinhard(fused, key=cfg.reinhard_key)
  431. 431 }
  432. 432 tonemapped
  433. 433}