image.photography\photo_enhance\computational_photography.mbt

95.2% · 79 / 83 points · 441 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/// # 计算摄影 photo_enhance/computational_photography
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 计算摄影增强(v15.0.0)
  7. 7// HDR 多曝光融合、色调映射、全景拼接增强、景深模拟、夜景增强
  8. 8
  9. 9///|
  10. 10/// HDR 曝光类型
  11. 11/// `#non_exhaustive` 表示未来可能添加新构造器
  12. 12#derive(Eq, @debug.Debug)
  13. 13#non_exhaustive
  14. 14pub(all) enum ExposureType {
  15. 15 Underexposed
  16. 16 Normal
  17. 17 Overexposed
  18. 18 Unknown
  19. 19}
  20. 20
  21. 21///|
  22. 22pub extend ExposureType with @debug.Debug::{to_repr}
  23. 23
  24. 24///|
  25. 25pub extend ExposureType with Eq::{not_equal, equal}
  26. 26
  27. 27///|
  28. 28/// HDR 图像
  29. 29#derive(Eq, @debug.Debug)
  30. 30pub(all) struct HdrImage {
  31. 31 width : Int32
  32. 32 height : Int32
  33. 33 channels : Int32
  34. 34 data : Array[Double]
  35. 35 exposure_values : Array[Double]
  36. 36 num_exposures : Int32
  37. 37}
  38. 38
  39. 39///|
  40. 40pub extend HdrImage with Eq::{not_equal, equal}
  41. 41
  42. 42///|
  43. 43pub extend HdrImage with @debug.Debug::{to_repr}
  44. 44
  45. 45///|
  46. 46/// 色调映射算法类型
  47. 47/// `#non_exhaustive` 表示未来可能添加新构造器
  48. 48#derive(Eq, @debug.Debug)
  49. 49#non_exhaustive
  50. 50pub(all) enum ToneMappingType {
  51. 51 Reinhard
  52. 52 Drago
  53. 53 ACES
  54. 54 Linear
  55. 55 Gamma
  56. 56}
  57. 57
  58. 58///|
  59. 59pub extend ToneMappingType with @debug.Debug::{to_repr}
  60. 60
  61. 61///|
  62. 62pub extend ToneMappingType with Eq::{not_equal, equal}
  63. 63
  64. 64///|
  65. 65/// 色调映射配置
  66. 66#derive(Eq, @debug.Debug)
  67. 67pub(all) struct ToneMappingConfig {
  68. 68 algorithm : ToneMappingType
  69. 69 exposure : Double
  70. 70 gamma : Double
  71. 71 saturation : Double
  72. 72 contrast : Double
  73. 73}
  74. 74
  75. 75///|
  76. 76pub extend ToneMappingConfig with Eq::{not_equal, equal}
  77. 77
  78. 78///|
  79. 79pub extend ToneMappingConfig with @debug.Debug::{to_repr}
  80. 80
  81. 81///|
  82. 82/// 全景拼接配置
  83. 83#derive(Eq, @debug.Debug)
  84. 84pub(all) struct PanoramaConfig {
  85. 85 overlap_threshold : Double
  86. 86 max_features : Int32
  87. 87 ransac_iterations : Int32
  88. 88 blend_width : Int32
  89. 89 crop_black_borders : Bool
  90. 90}
  91. 91
  92. 92///|
  93. 93pub extend PanoramaConfig with Eq::{not_equal, equal}
  94. 94
  95. 95///|
  96. 96pub extend PanoramaConfig with @debug.Debug::{to_repr}
  97. 97
  98. 98///|
  99. 99/// 全景拼接结果
  100. 100#derive(Eq, @debug.Debug)
  101. 101pub(all) struct PanoramaResult {
  102. 102 width : Int32
  103. 103 height : Int32
  104. 104 num_images : Int32
  105. 105 inliers : Int32
  106. 106 confidence : Double
  107. 107}
  108. 108
  109. 109///|
  110. 110pub extend PanoramaResult with Eq::{not_equal, equal}
  111. 111
  112. 112///|
  113. 113pub extend PanoramaResult with @debug.Debug::{to_repr}
  114. 114
  115. 115///|
  116. 116/// 景深配置
  117. 117#derive(Eq, @debug.Debug)
  118. 118pub(all) struct DepthOfFieldConfig {
  119. 119 focal_point_x : Int32
  120. 120 focal_point_y : Int32
  121. 121 aperture : Double
  122. 122 blur_strength : Double
  123. 123}
  124. 124
  125. 125///|
  126. 126pub extend DepthOfFieldConfig with Eq::{not_equal, equal}
  127. 127
  128. 128///|
  129. 129pub extend DepthOfFieldConfig with @debug.Debug::{to_repr}
  130. 130
  131. 131///|
  132. 132/// 夜景增强配置
  133. 133#derive(Eq, @debug.Debug)
  134. 134pub(all) struct NightEnhanceConfig {
  135. 135 denoise_strength : Double
  136. 136 brightness_boost : Double
  137. 137 contrast_enhance : Double
  138. 138 noise_reduction : Bool
  139. 139}
  140. 140
  141. 141///|
  142. 142pub extend NightEnhanceConfig with Eq::{not_equal, equal}
  143. 143
  144. 144///|
  145. 145pub extend NightEnhanceConfig with @debug.Debug::{to_repr}
  146. 146
  147. 147///|
  148. 148/// 创建默认 HDR 配置
  149. 149pub fn create_default_hdr_config() -> ToneMappingConfig {
  150. 150● ToneMappingConfig::{
  151. 151 algorithm: ToneMappingType::Reinhard,
  152. 152 exposure: 0.0,
  153. 153 gamma: 2.2,
  154. 154 saturation: 1.0,
  155. 155 contrast: 1.0,
  156. 156 }
  157. 157}
  158. 158
  159. 159///|
  160. 160/// 创建默认全景拼接配置
  161. 161pub fn create_default_panorama_config() -> PanoramaConfig {
  162. 162● PanoramaConfig::{
  163. 163 overlap_threshold: 0.3,
  164. 164 max_features: 500,
  165. 165 ransac_iterations: 1000,
  166. 166 blend_width: 50,
  167. 167 crop_black_borders: true,
  168. 168 }
  169. 169}
  170. 170
  171. 171///|
  172. 172/// 创建默认景深配置
  173. 173pub fn create_default_dof_config(
  174. 174 width : Int32,
  175. 175 height : Int32,
  176. 176) -> DepthOfFieldConfig {
  177. 177● DepthOfFieldConfig::{
  178. 178 focal_point_x: width / 2,
  179. 179 focal_point_y: height / 2,
  180. 180 aperture: 2.8,
  181. 181 blur_strength: 1.0,
  182. 182 }
  183. 183}
  184. 184
  185. 185///|
  186. 186/// 创建默认夜景增强配置
  187. 187pub fn create_default_night_config() -> NightEnhanceConfig {
  188. 188● NightEnhanceConfig::{
  189. 189 denoise_strength: 0.5,
  190. 190 brightness_boost: 0.3,
  191. 191 contrast_enhance: 1.2,
  192. 192 noise_reduction: true,
  193. 193 }
  194. 194}
  195. 195
  196. 196///|
  197. 197/// 检测图像曝光类型
  198. 198pub fn detect_exposure_type(img : @types.Image) -> (ExposureType, Double) {
  199. 199● let w = img.width
  200. 200 let h = img.height
  201. 201 let mut sum = 0.0
  202. 202 let mut underexposed = 0
  203. 203 let mut overexposed = 0
  204. 204 let total = w * h
  205. 205
  206. 206 for y in 0..<h {
  207. 207● for x in 0..<w {
  208. 208● let pixel = img.data[y * w + x].to_double()
  209. 209 sum += pixel
  210. 210 if pixel < 50.0 {
  211. 211● underexposed += 1
  212. 212 }
  213. 213 if pixel > 230.0 {
  214. 214● overexposed += 1
  215. 215 }
  216. 216 }
  217. 217 }
  218. 218
  219. 219◐ let avg = if total > 0 { sum / total.to_double() } else { 0.0 }
  220. 220● let under_ratio = underexposed.to_double() / total.to_double()
  221. 221● let over_ratio = overexposed.to_double() / total.to_double()
  222. 222
  223. 223 let exposure_type = if under_ratio > 0.5 || avg < 60.0 {
  224. 224● ExposureType::Underexposed
  225. 225● } else if over_ratio > 0.5 || avg > 200.0 {
  226. 226● ExposureType::Overexposed
  227. 227 } else {
  228. 228● ExposureType::Normal
  229. 229 }
  230. 230
  231. 231 let ev = avg / 128.0
  232. 232 (exposure_type, ev)
  233. 233}
  234. 234
  235. 235///|
  236. 236/// HDR 多曝光融合(简化版,返回 Double 数组)
  237. 237pub fn hdr_merge(
  238. 238 images : Array[@types.Image],
  239. 239 exposures : Array[Double],
  240. 240) -> HdrImage {
  241. 241● if images.length() == 0 {
  242. 242○ return HdrImage::{
  243. 243 width: 0,
  244. 244 height: 0,
  245. 245 channels: 1,
  246. 246 data: [],
  247. 247 exposure_values: [],
  248. 248 num_exposures: 0,
  249. 249 }
  250. 250 }
  251. 251
  252. 252 let first = images[0]
  253. 253 let w = first.width
  254. 254 let h = first.height
  255. 255 let hdr_data : Array[Double] = []
  256. 256
  257. 257 for y in 0..<h {
  258. 258● for x in 0..<w {
  259. 259● let mut sum = 0.0
  260. 260 let mut weight_sum = 0.0
  261. 261
  262. 262● for i in 0..<images.length() {
  263. 263● let img = images[i]
  264. 264 if x < img.width && y < img.height {
  265. 265● let pixel = img.data[y * img.width + x].to_double() / 255.0
  266. 266 let weight = if pixel < 0.5 {
  267. 267● pixel * 2.0
  268. 268 } else {
  269. 269● (1.0 - pixel) * 2.0
  270. 270 }
  271. 271 sum += pixel * weight
  272. 272 weight_sum += weight
  273. 273 }
  274. 274 }
  275. 275
  276. 276● let hdr_value = if weight_sum > 0 { sum / weight_sum } else { 0.5 }
  277. 277● hdr_data.push(hdr_value)
  278. 278 }
  279. 279 }
  280. 280
  281. 281 HdrImage::{
  282. 282 width: w,
  283. 283 height: h,
  284. 284 channels: first.channels,
  285. 285 data: hdr_data,
  286. 286 exposure_values: exposures,
  287. 287● num_exposures: images.length(),
  288. 288 }
  289. 289}
  290. 290
  291. 291///|
  292. 292/// 色调映射(简化版,返回处理后的 Image)
  293. 293pub fn tone_map(hdr : HdrImage, config : ToneMappingConfig) -> @types.Image {
  294. 294● ignore(config)
  295. 295 let w = hdr.width
  296. 296 let h = hdr.height
  297. 297● let out : Array[Byte] = Array::make(w * h, b'\x00')
  298. 298
  299. 299 for y in 0..<h {
  300. 300● for x in 0..<w {
  301. 301● let idx = y * w + x
  302. 302 let pixel = hdr.data[idx]
  303. 303 let l = pixel * 0.18
  304. 304 let mapped = l / (1.0 + l)
  305. 305● let value = (mapped * 255.0).to_int()
  306. 306◐ out[idx] = if value > 127 { b'\xFF' } else { b'\x00' }
  307. 307 }
  308. 308 }
  309. 309
  310. 310 @types.Image::{
  311. 311 width: w,
  312. 312 height: h,
  313. 313 channels: hdr.channels,
  314. 314● data: Bytes::from_array(out),
  315. 315 }
  316. 316}
  317. 317
  318. 318///|
  319. 319/// 全景拼接(简化版)
  320. 320pub fn stitch_panorama(
  321. 321 images : Array[@types.Image],
  322. 322 config : PanoramaConfig,
  323. 323) -> PanoramaResult {
  324. 324● ignore(config)
  325. 325● if images.length() == 0 {
  326. 326○ return PanoramaResult::{
  327. 327 width: 0,
  328. 328 height: 0,
  329. 329 num_images: 0,
  330. 330 inliers: 0,
  331. 331 confidence: 0.0,
  332. 332 }
  333. 333 }
  334. 334
  335. 335 let first = images[0]
  336. 336● let total_width = first.width * images.length()
  337. 337 let height = first.height
  338. 338
  339. 339 PanoramaResult::{
  340. 340 width: total_width,
  341. 341 height,
  342. 342● num_images: images.length(),
  343. 343 inliers: total_width * height,
  344. 344 confidence: 0.7,
  345. 345 }
  346. 346}
  347. 347
  348. 348///|
  349. 349/// 景深模拟(简化版)
  350. 350pub fn apply_depth_of_field(
  351. 351 img : @types.Image,
  352. 352 config : DepthOfFieldConfig,
  353. 353) -> @types.Image {
  354. 354● let w = img.width
  355. 355 let h = img.height
  356. 356● let out : Array[Byte] = Array::make(w * h, b'\x00')
  357. 357● let max_dist = ((w / 2).to_double() * (w / 2).to_double() +
  358. 358● (h / 2).to_double() * (h / 2).to_double()).sqrt()
  359. 359
  360. 360 for y in 0..<h {
  361. 361● for x in 0..<w {
  362. 362● let idx = y * w + x
  363. 363● let dx = (x - config.focal_point_x).to_double()
  364. 364● let dy = (y - config.focal_point_y).to_double()
  365. 365● let dist = (dx * dx + dy * dy).sqrt()
  366. 366 let blur_factor = dist / max_dist * config.blur_strength
  367. 367● let pixel = img.data[idx].to_double()
  368. 368 let blurred = pixel * (1.0 - blur_factor * 0.3)
  369. 369● let value = blurred.to_int()
  370. 370● out[idx] = if value > 127 { b'\xFF' } else { b'\x00' }
  371. 371 }
  372. 372 }
  373. 373
  374. 374 @types.Image::{
  375. 375 width: w,
  376. 376 height: h,
  377. 377 channels: img.channels,
  378. 378● data: Bytes::from_array(out),
  379. 379 }
  380. 380}
  381. 381
  382. 382///|
  383. 383/// 夜景增强(简化版)
  384. 384pub fn enhance_night(
  385. 385 img : @types.Image,
  386. 386 config : NightEnhanceConfig,
  387. 387) -> @types.Image {
  388. 388● let w = img.width
  389. 389 let h = img.height
  390. 390● let out : Array[Byte] = Array::make(w * h, b'\x00')
  391. 391
  392. 392 for y in 0..<h {
  393. 393● for x in 0..<w {
  394. 394● let idx = y * w + x
  395. 395● let pixel = img.data[idx].to_double() / 255.0
  396. 396 let mut enhanced = pixel + config.brightness_boost
  397. 397 enhanced = (enhanced - 0.5) * config.contrast_enhance + 0.5
  398. 398● let value = (enhanced * 255.0).to_int()
  399. 399● out[idx] = if value > 127 { b'\xFF' } else { b'\x00' }
  400. 400 }
  401. 401 }
  402. 402
  403. 403 @types.Image::{
  404. 404 width: w,
  405. 405 height: h,
  406. 406 channels: img.channels,
  407. 407● data: Bytes::from_array(out),
  408. 408 }
  409. 409}
  410. 410
  411. 411///|
  412. 412/// 生成计算摄影报告
  413. 413pub fn computational_photography_report(
  414. 414 hdr : HdrImage,
  415. 415 panorama : PanoramaResult,
  416. 416) -> String {
  417. 417● let mut report = "## 计算摄影处理报告\n\n"
  418. 418 report = report + "### HDR 信息\n\n"
  419. 419 report = report + "| 指标 | 值 |\n|------|------|\n"
  420. 420 report = report +
  421. 421 "| 尺寸 | " +
  422. 422● hdr.width.to_string() +
  423. 423 "x" +
  424. 424● hdr.height.to_string() +
  425. 425 " |\n"
  426. 426● report = report + "| 曝光数 | " + hdr.num_exposures.to_string() + " |\n\n"
  427. 427 report = report + "### 全景拼接信息\n\n"
  428. 428 report = report + "| 指标 | 值 |\n|------|------|\n"
  429. 429 report = report +
  430. 430 "| 拼接尺寸 | " +
  431. 431● panorama.width.to_string() +
  432. 432 "x" +
  433. 433● panorama.height.to_string() +
  434. 434 " |\n"
  435. 435● report = report + "| 图像数 | " + panorama.num_images.to_string() + " |\n"
  436. 436 report = report +
  437. 437 "| 置信度 | " +
  438. 438● (panorama.confidence * 100.0).to_string() +
  439. 439 "% |\n"
  440. 440 report
  441. 441}