image.domain\forensics\image_source_verification.mbt

90.7% · 194 / 214 points · 942 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/// # 图像来源验证 forensics/image_source_verification
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 图像来源鉴定(v27.0.0)
  7. 7// AI 生成图像检测、物理生成(真实拍摄)图像检测、图像取证分析
  8. 8
  9. 9///|
  10. 10/// 图像来源类型
  11. 11/// `#non_exhaustive` 表示未来可能添加新构造器
  12. 12#derive(Eq)
  13. 13#non_exhaustive
  14. 14pub(all) enum ImageSourceType {
  15. 15 AIGenerated
  16. 16 PhysicalCaptured
  17. 17 Unknown
  18. 18 Mixed
  19. 19}
  20. 20
  21. 21///|
  22. 22pub extend ImageSourceType with Eq::{not_equal, equal}
  23. 23
  24. 24///|
  25. 25/// 鉴定结果
  26. 26pub(all) struct SourceVerificationResult {
  27. 27 source_type : ImageSourceType
  28. 28 ai_probability : Double
  29. 29 physical_probability : Double
  30. 30 confidence : Double
  31. 31 decision_threshold : Double
  32. 32 features : SourceVerificationFeatures
  33. 33 processing_time_ms : Double
  34. 34}
  35. 35
  36. 36///|
  37. 37/// 来源鉴定特征
  38. 38pub(all) struct SourceVerificationFeatures {
  39. 39 noise_pattern_score : Double
  40. 40 frequency_domain_score : Double
  41. 41 color_distribution_score : Double
  42. 42 texture_score : Double
  43. 43 edge_score : Double
  44. 44 compression_artifact_score : Double
  45. 45 detail_score : Double
  46. 46 metadata_score : Double
  47. 47 overall_score : Double
  48. 48}
  49. 49
  50. 50///|
  51. 51/// 鉴定配置
  52. 52pub(all) struct SourceVerificationConfig {
  53. 53 decision_threshold : Double
  54. 54 use_noise_analysis : Bool
  55. 55 use_frequency_analysis : Bool
  56. 56 use_color_analysis : Bool
  57. 57 use_texture_analysis : Bool
  58. 58 use_edge_analysis : Bool
  59. 59 use_compression_analysis : Bool
  60. 60 use_detail_analysis : Bool
  61. 61 weights : Array[Double]
  62. 62}
  63. 63
  64. 64///|
  65. 65/// 噪声模式分析结果
  66. 66pub(all) struct NoiseAnalysisResult {
  67. 67 noise_level : Double
  68. 68 noise_correlation : Double
  69. 69 noise_spectrum_flatness : Double
  70. 70 noise_pattern_regularity : Double
  71. 71 ai_noise_indicator : Double
  72. 72}
  73. 73
  74. 74///|
  75. 75/// 频域分析结果
  76. 76pub(all) struct FrequencyAnalysisResult {
  77. 77 high_frequency_energy : Double
  78. 78 low_frequency_energy : Double
  79. 79 frequency_spectrum_flatness : Double
  80. 80 spectral_centroid : Double
  81. 81 spectral_spread : Double
  82. 82 ai_frequency_indicator : Double
  83. 83}
  84. 84
  85. 85///|
  86. 86/// 颜色分布分析结果
  87. 87pub(all) struct ColorAnalysisResult {
  88. 88 color_histogram_smoothness : Double
  89. 89 color_transition_naturalness : Double
  90. 90 color_quantization_level : Double
  91. 91 skin_tone_accuracy : Double
  92. 92 ai_color_indicator : Double
  93. 93}
  94. 94
  95. 95///|
  96. 96/// 纹理分析结果
  97. 97pub(all) struct TextureAnalysisResult {
  98. 98 texture_regularity : Double
  99. 99 texture_complexity : Double
  100. 100 micro_texture_consistency : Double
  101. 101 macro_texture_consistency : Double
  102. 102 ai_texture_indicator : Double
  103. 103}
  104. 104
  105. 105///|
  106. 106/// 边缘分析结果
  107. 107pub(all) struct EdgeAnalysisResult {
  108. 108 edge_sharpness : Double
  109. 109 edge_continuity : Double
  110. 110 edge_consistency : Double
  111. 111 edge_artifact_level : Double
  112. 112 ai_edge_indicator : Double
  113. 113}
  114. 114
  115. 115///|
  116. 116/// 压缩伪影分析结果
  117. 117pub(all) struct CompressionAnalysisResult {
  118. 118 blocking_artifact_level : Double
  119. 119 ringing_artifact_level : Double
  120. 120 compression_noise_level : Double
  121. 121 quantization_table_consistency : Double
  122. 122 ai_compression_indicator : Double
  123. 123}
  124. 124
  125. 125///|
  126. 126/// 细节分析结果
  127. 127pub(all) struct DetailAnalysisResult {
  128. 128 eye_detail_consistency : Double
  129. 129 finger_detail_consistency : Double
  130. 130 hair_detail_consistency : Double
  131. 131 text_readability : Double
  132. 132 symmetry_consistency : Double
  133. 133 ai_detail_indicator : Double
  134. 134}
  135. 135
  136. 136///|
  137. 137/// 创建默认鉴定配置
  138. 138pub fn create_default_source_verification_config() -> SourceVerificationConfig {
  139. 139● SourceVerificationConfig::{
  140. 140 decision_threshold: 0.5,
  141. 141 use_noise_analysis: true,
  142. 142 use_frequency_analysis: true,
  143. 143 use_color_analysis: true,
  144. 144 use_texture_analysis: true,
  145. 145 use_edge_analysis: true,
  146. 146 use_compression_analysis: true,
  147. 147 use_detail_analysis: true,
  148. 148 weights: [0.2, 0.15, 0.1, 0.15, 0.1, 0.1, 0.2],
  149. 149 }
  150. 150}
  151. 151
  152. 152///|
  153. 153/// 噪声模式分析
  154. 154pub fn analyze_noise_pattern(
  155. 155 img : @types.Image,
  156. 156) -> NoiseAnalysisResult raise @types.LoadError {
  157. 157 // 输入验证
  158. 158● if img.width <= 0 || img.height <= 0 {
  159. 159● raise @types.LoadError::DecodeFailed(
  160. 160 "analyze_noise_pattern: 图像尺寸必须大于0",
  161. 161 )
  162. 162 }
  163. 163 if img.channels < 1 || img.channels > 4 {
  164. 164○ raise @types.LoadError::DecodeFailed(
  165. 165 "analyze_noise_pattern: 通道数必须在1-4之间",
  166. 166 )
  167. 167 }
  168. 168 let expected_size = img.width * img.height * img.channels
  169. 169● if img.data.length() != expected_size {
  170. 170○ raise @types.LoadError::DecodeFailed(
  171. 171 "analyze_noise_pattern: 数据大小不匹配",
  172. 172 )
  173. 173 }
  174. 174 let w = img.width
  175. 175 let h = img.height
  176. 176 let total = w * h
  177. 177
  178. 178 // 计算噪声水平(高频分量)
  179. 179 let mut noise_level = 0.0
  180. 180 for y in 1..<(h - 1) {
  181. 181● for x in 1..<(w - 1) {
  182. 182● let idx = y * w + x
  183. 183● let center = img.data[idx].to_double()
  184. 184 let avg = (
  185. 185● img.data[idx - 1].to_double() +
  186. 186● img.data[idx + 1].to_double() +
  187. 187● img.data[idx - w].to_double() +
  188. 188● img.data[idx + w].to_double()
  189. 189 ) /
  190. 190 4.0
  191. 191 noise_level += (center - avg) * (center - avg)
  192. 192 }
  193. 193 }
  194. 194● noise_level /= total.to_double()
  195. 195● noise_level = noise_level.sqrt()
  196. 196
  197. 197 // 噪声相关性(相邻像素噪声的相关性)
  198. 198 let mut noise_correlation = 0.0
  199. 199 let mut count = 0
  200. 200 for y in 0..<h {
  201. 201● for x in 0..<(w - 1) {
  202. 202● let n1 = img.data[y * w + x].to_double()
  203. 203● let n2 = img.data[y * w + x + 1].to_double()
  204. 204 noise_correlation += n1 * n2
  205. 205 count += 1
  206. 206 }
  207. 207 }
  208. 208 if count > 0 {
  209. 209● noise_correlation /= count.to_double()
  210. 210 }
  211. 211
  212. 212 // AI 生成图像通常噪声水平较低且相关性较高
  213. 213 let ai_noise_indicator = if noise_level < 10.0 && noise_correlation > 100.0 {
  214. 214● 0.8
  215. 215● } else if noise_level < 15.0 {
  216. 216○ 0.6
  217. 217 } else {
  218. 218● 0.3
  219. 219 }
  220. 220
  221. 221 NoiseAnalysisResult::{
  222. 222 noise_level,
  223. 223 noise_correlation,
  224. 224 noise_spectrum_flatness: 0.5,
  225. 225 noise_pattern_regularity: 0.5,
  226. 226 ai_noise_indicator,
  227. 227 }
  228. 228}
  229. 229
  230. 230///|
  231. 231/// 频域分析(简化版,使用梯度能量代替 FFT)
  232. 232pub fn analyze_frequency_domain(
  233. 233 img : @types.Image,
  234. 234) -> FrequencyAnalysisResult raise @types.LoadError {
  235. 235 // 输入验证
  236. 236● if img.width <= 0 || img.height <= 0 {
  237. 237● raise @types.LoadError::DecodeFailed(
  238. 238 "analyze_frequency_domain: 图像尺寸必须大于0",
  239. 239 )
  240. 240 }
  241. 241 if img.channels < 1 || img.channels > 4 {
  242. 242○ raise @types.LoadError::DecodeFailed(
  243. 243 "analyze_frequency_domain: 通道数必须在1-4之间",
  244. 244 )
  245. 245 }
  246. 246 let expected_size = img.width * img.height * img.channels
  247. 247● if img.data.length() != expected_size {
  248. 248○ raise @types.LoadError::DecodeFailed(
  249. 249 "analyze_frequency_domain: 数据大小不匹配",
  250. 250 )
  251. 251 }
  252. 252 let w = img.width
  253. 253 let h = img.height
  254. 254 let total = w * h
  255. 255
  256. 256 // 高频能量(梯度)
  257. 257 let mut high_freq_energy = 0.0
  258. 258 let mut low_freq_energy = 0.0
  259. 259
  260. 260 for y in 1..<(h - 1) {
  261. 261● for x in 1..<(w - 1) {
  262. 262● let idx = y * w + x
  263. 263● let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double()
  264. 264● let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double()
  265. 265● high_freq_energy += (gx * gx + gy * gy).sqrt()
  266. 266 }
  267. 267 }
  268. 268● high_freq_energy /= total.to_double()
  269. 269
  270. 270 // 低频能量(局部平均)
  271. 271 for y in 0..<h {
  272. 272● for x in 0..<w {
  273. 273● low_freq_energy += img.data[y * w + x].to_double()
  274. 274 }
  275. 275 }
  276. 276● low_freq_energy /= total.to_double()
  277. 277
  278. 278 // 频谱平坦度
  279. 279 let spectrum_flatness = if high_freq_energy > 0.0 {
  280. 280● low_freq_energy / (low_freq_energy + high_freq_energy)
  281. 281 } else {
  282. 282● 0.5
  283. 283 }
  284. 284
  285. 285 // AI 生成图像通常高频能量较低
  286. 286 let ai_frequency_indicator = if high_freq_energy < 20.0 {
  287. 287● 0.75
  288. 288● } else if high_freq_energy < 30.0 {
  289. 289● 0.55
  290. 290 } else {
  291. 291● 0.3
  292. 292 }
  293. 293
  294. 294 FrequencyAnalysisResult::{
  295. 295 high_frequency_energy: high_freq_energy,
  296. 296 low_frequency_energy: low_freq_energy,
  297. 297 frequency_spectrum_flatness: spectrum_flatness,
  298. 298 spectral_centroid: 0.5,
  299. 299 spectral_spread: 0.5,
  300. 300 ai_frequency_indicator,
  301. 301 }
  302. 302}
  303. 303
  304. 304///|
  305. 305/// 颜色分布分析
  306. 306pub fn analyze_color_distribution(
  307. 307 img : @types.Image,
  308. 308) -> ColorAnalysisResult raise @types.LoadError {
  309. 309 // 输入验证
  310. 310● if img.width <= 0 || img.height <= 0 {
  311. 311● raise @types.LoadError::DecodeFailed(
  312. 312 "analyze_color_distribution: 图像尺寸必须大于0",
  313. 313 )
  314. 314 }
  315. 315 if img.channels < 1 || img.channels > 4 {
  316. 316○ raise @types.LoadError::DecodeFailed(
  317. 317 "analyze_color_distribution: 通道数必须在1-4之间",
  318. 318 )
  319. 319 }
  320. 320 let expected_size = img.width * img.height * img.channels
  321. 321● if img.data.length() != expected_size {
  322. 322● raise @types.LoadError::DecodeFailed(
  323. 323 "analyze_color_distribution: 数据大小不匹配",
  324. 324 )
  325. 325 }
  326. 326 let w = img.width
  327. 327 let h = img.height
  328. 328 let total = w * h
  329. 329
  330. 330 // 颜色直方图平滑度
  331. 331 let histogram : Array[Int32] = []
  332. 332 for _ in 0..<64 {
  333. 333● histogram.push(0)
  334. 334 }
  335. 335 for i in 0..<total {
  336. 336● let bin = img.data[i].to_int() * 64 / 256
  337. 337 histogram[bin] += 1
  338. 338 }
  339. 339
  340. 340 let mut smoothness = 0.0
  341. 341 for i in 1..<63 {
  342. 342● let diff = (histogram[i + 1] - 2 * histogram[i] + histogram[i - 1])
  343. 343● .to_double()
  344. 344● .abs()
  345. 345 smoothness += diff
  346. 346 }
  347. 347 smoothness /= 64.0
  348. 348
  349. 349 // 颜色过渡自然度
  350. 350 let mut transition_naturalness = 0.0
  351. 351 let mut count = 0
  352. 352 for y in 0..<h {
  353. 353● for x in 0..<(w - 1) {
  354. 354● let diff = (img.data[y * w + x].to_double() -
  355. 355● img.data[y * w + x + 1].to_double()).abs()
  356. 356 transition_naturalness += diff
  357. 357 count += 1
  358. 358 }
  359. 359 }
  360. 360 if count > 0 {
  361. 361● transition_naturalness /= count.to_double()
  362. 362 }
  363. 363
  364. 364 // AI 生成图像通常颜色过渡过于平滑
  365. 365 let ai_color_indicator = if smoothness < 100.0 &&
  366. 366 transition_naturalness < 10.0 {
  367. 367● 0.7
  368. 368● } else if smoothness < 200.0 {
  369. 369● 0.5
  370. 370 } else {
  371. 371○ 0.3
  372. 372 }
  373. 373
  374. 374 ColorAnalysisResult::{
  375. 375 color_histogram_smoothness: smoothness,
  376. 376 color_transition_naturalness: transition_naturalness,
  377. 377 color_quantization_level: 0.5,
  378. 378 skin_tone_accuracy: 0.5,
  379. 379 ai_color_indicator,
  380. 380 }
  381. 381}
  382. 382
  383. 383///|
  384. 384/// 纹理分析
  385. 385pub fn analyze_texture(
  386. 386 img : @types.Image,
  387. 387) -> TextureAnalysisResult raise @types.LoadError {
  388. 388 // 输入验证
  389. 389● if img.width <= 0 || img.height <= 0 {
  390. 390● raise @types.LoadError::DecodeFailed(
  391. 391 "analyze_texture: 图像尺寸必须大于0",
  392. 392 )
  393. 393 }
  394. 394 if img.channels < 1 || img.channels > 4 {
  395. 395○ raise @types.LoadError::DecodeFailed(
  396. 396 "analyze_texture: 通道数必须在1-4之间",
  397. 397 )
  398. 398 }
  399. 399 let expected_size = img.width * img.height * img.channels
  400. 400● if img.data.length() != expected_size {
  401. 401○ raise @types.LoadError::DecodeFailed(
  402. 402 "analyze_texture: 数据大小不匹配",
  403. 403 )
  404. 404 }
  405. 405 let w = img.width
  406. 406 let h = img.height
  407. 407 let total = w * h
  408. 408
  409. 409 // 纹理复杂度(局部方差)
  410. 410 let mut texture_complexity = 0.0
  411. 411 for y in 1..<(h - 1) {
  412. 412● for x in 1..<(w - 1) {
  413. 413● let idx = y * w + x
  414. 414● let center = img.data[idx].to_double()
  415. 415 let mut sum = 0.0
  416. 416 for dy in -1..<2 {
  417. 417● for dx in -1..<2 {
  418. 418● if dx == 0 && dy == 0 {
  419. 419● continue
  420. 420 }
  421. 421● sum += img.data[(y + dy) * w + (x + dx)].to_double()
  422. 422 }
  423. 423 }
  424. 424 let avg = sum / 8.0
  425. 425 texture_complexity += (center - avg) * (center - avg)
  426. 426 }
  427. 427 }
  428. 428● texture_complexity /= total.to_double()
  429. 429● texture_complexity = texture_complexity.sqrt()
  430. 430
  431. 431 // 纹理规律性
  432. 432 let mut texture_regularity = 0.0
  433. 433 for y in 0..<(h - 2) {
  434. 434● for x in 0..<(w - 2) {
  435. 435● let p1 = img.data[y * w + x].to_double()
  436. 436● let p2 = img.data[y * w + x + 2].to_double()
  437. 437● texture_regularity += (p1 - p2).abs()
  438. 438 }
  439. 439 }
  440. 440● texture_regularity /= total.to_double()
  441. 441
  442. 442 // AI 生成图像通常纹理复杂度较低且规律性较高
  443. 443 let ai_texture_indicator = if texture_complexity < 15.0 &&
  444. 444 texture_regularity < 10.0 {
  445. 445● 0.7
  446. 446● } else if texture_complexity < 25.0 {
  447. 447● 0.5
  448. 448 } else {
  449. 449● 0.3
  450. 450 }
  451. 451
  452. 452 TextureAnalysisResult::{
  453. 453 texture_regularity,
  454. 454 texture_complexity,
  455. 455 micro_texture_consistency: 0.5,
  456. 456 macro_texture_consistency: 0.5,
  457. 457 ai_texture_indicator,
  458. 458 }
  459. 459}
  460. 460
  461. 461///|
  462. 462/// 边缘分析
  463. 463pub fn analyze_edges(
  464. 464 img : @types.Image,
  465. 465) -> EdgeAnalysisResult raise @types.LoadError {
  466. 466 // 输入验证
  467. 467● if img.width <= 0 || img.height <= 0 {
  468. 468● raise @types.LoadError::DecodeFailed(
  469. 469 "analyze_edges: 图像尺寸必须大于0",
  470. 470 )
  471. 471 }
  472. 472 if img.channels < 1 || img.channels > 4 {
  473. 473○ raise @types.LoadError::DecodeFailed(
  474. 474 "analyze_edges: 通道数必须在1-4之间",
  475. 475 )
  476. 476 }
  477. 477 let expected_size = img.width * img.height * img.channels
  478. 478● if img.data.length() != expected_size {
  479. 479○ raise @types.LoadError::DecodeFailed("analyze_edges: 数据大小不匹配")
  480. 480 }
  481. 481 let w = img.width
  482. 482 let h = img.height
  483. 483 let _ = w * h
  484. 484
  485. 485 // 边缘锐度(梯度幅值)
  486. 486 let mut edge_sharpness = 0.0
  487. 487 let mut edge_count = 0
  488. 488 for y in 1..<(h - 1) {
  489. 489● for x in 1..<(w - 1) {
  490. 490● let idx = y * w + x
  491. 491● let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double()
  492. 492● let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double()
  493. 493● let magnitude = (gx * gx + gy * gy).sqrt()
  494. 494 if magnitude > 30.0 {
  495. 495● edge_sharpness += magnitude
  496. 496 edge_count += 1
  497. 497 }
  498. 498 }
  499. 499 }
  500. 500 if edge_count > 0 {
  501. 501● edge_sharpness /= edge_count.to_double()
  502. 502 }
  503. 503
  504. 504 // 边缘连续性
  505. 505 let mut edge_continuity = 0.0
  506. 506 for y in 1..<(h - 1) {
  507. 507● for x in 1..<(w - 1) {
  508. 508● let idx = y * w + x
  509. 509● let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double()
  510. 510● let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double()
  511. 511● let magnitude = (gx * gx + gy * gy).sqrt()
  512. 512 if magnitude > 30.0 {
  513. 513 // 检查邻域是否也有边缘
  514. 514● let mut neighbor_edges = 0
  515. 515 for dy in -1..<2 {
  516. 516● for dx in -1..<2 {
  517. 517● if dx == 0 && dy == 0 {
  518. 518● continue
  519. 519 }
  520. 520 let ny = y + dy
  521. 521 let nx = x + dx
  522. 522 if ny < 1 || ny > h - 2 || nx < 1 || nx > w - 2 {
  523. 523● continue
  524. 524 }
  525. 525 let nidx = ny * w + nx
  526. 526● let ngx = img.data[nidx + 1].to_double() -
  527. 527● img.data[nidx - 1].to_double()
  528. 528● let ngy = img.data[nidx + w].to_double() -
  529. 529● img.data[nidx - w].to_double()
  530. 530● let nmag = (ngx * ngx + ngy * ngy).sqrt()
  531. 531 if nmag > 30.0 {
  532. 532● neighbor_edges += 1
  533. 533 }
  534. 534 }
  535. 535 }
  536. 536● edge_continuity += neighbor_edges.to_double() / 8.0
  537. 537 }
  538. 538 }
  539. 539 }
  540. 540 if edge_count > 0 {
  541. 541● edge_continuity /= edge_count.to_double()
  542. 542 }
  543. 543
  544. 544 // AI 生成图像通常边缘锐度较低且连续性较差
  545. 545 let ai_edge_indicator = if edge_sharpness < 50.0 && edge_continuity < 0.5 {
  546. 546● 0.65
  547. 547● } else if edge_sharpness < 70.0 {
  548. 548○ 0.45
  549. 549 } else {
  550. 550● 0.25
  551. 551 }
  552. 552
  553. 553 EdgeAnalysisResult::{
  554. 554 edge_sharpness,
  555. 555 edge_continuity,
  556. 556 edge_consistency: 0.5,
  557. 557 edge_artifact_level: 0.5,
  558. 558 ai_edge_indicator,
  559. 559 }
  560. 560}
  561. 561
  562. 562///|
  563. 563/// 压缩伪影分析
  564. 564pub fn analyze_compression_artifacts(
  565. 565 img : @types.Image,
  566. 566) -> CompressionAnalysisResult raise @types.LoadError {
  567. 567 // 输入验证
  568. 568● if img.width <= 0 || img.height <= 0 {
  569. 569● raise @types.LoadError::DecodeFailed(
  570. 570 "analyze_compression_artifacts: 图像尺寸必须大于0",
  571. 571 )
  572. 572 }
  573. 573 if img.channels < 1 || img.channels > 4 {
  574. 574○ raise @types.LoadError::DecodeFailed(
  575. 575 "analyze_compression_artifacts: 通道数必须在1-4之间",
  576. 576 )
  577. 577 }
  578. 578 let expected_size = img.width * img.height * img.channels
  579. 579● if img.data.length() != expected_size {
  580. 580○ raise @types.LoadError::DecodeFailed(
  581. 581 "analyze_compression_artifacts: 数据大小不匹配",
  582. 582 )
  583. 583 }
  584. 584 let w = img.width
  585. 585 let h = img.height
  586. 586 let total = w * h
  587. 587
  588. 588 // 块效应检测(8x8 块边界)
  589. 589 let mut blocking_artifact = 0.0
  590. 590 let mut block_count = 0
  591. 591 for y in 0..<h {
  592. 592● for x in 0..<w {
  593. 593● if x % 8 == 0 && x > 0 && x < w - 1 {
  594. 594● let diff = (img.data[y * w + x].to_double() -
  595. 595● img.data[y * w + x - 1].to_double()).abs()
  596. 596 blocking_artifact += diff
  597. 597 block_count += 1
  598. 598 }
  599. 599 if y % 8 == 0 && y > 0 && y < h - 1 {
  600. 600● let diff = (img.data[y * w + x].to_double() -
  601. 601● img.data[(y - 1) * w + x].to_double()).abs()
  602. 602 blocking_artifact += diff
  603. 603 block_count += 1
  604. 604 }
  605. 605 }
  606. 606 }
  607. 607 if block_count > 0 {
  608. 608● blocking_artifact /= block_count.to_double()
  609. 609 }
  610. 610
  611. 611 // 振铃效应检测(边缘附近的振荡)
  612. 612 let mut ringing_artifact = 0.0
  613. 613 for y in 2..<(h - 2) {
  614. 614● for x in 2..<(w - 2) {
  615. 615● let idx = y * w + x
  616. 616● let center = img.data[idx].to_double()
  617. 617● let _ = img.data[idx - 2].to_double()
  618. 618● let p2 = img.data[idx - 1].to_double()
  619. 619● let p3 = img.data[idx + 1].to_double()
  620. 620● let _ = img.data[idx + 2].to_double()
  621. 621 // 检测振荡模式
  622. 622 if (p2 > center && p3 > center) || (p2 < center && p3 < center) {
  623. 623● ringing_artifact += 1.0
  624. 624 }
  625. 625 }
  626. 626 }
  627. 627● ringing_artifact /= total.to_double()
  628. 628
  629. 629 // AI 生成图像通常压缩伪影较少
  630. 630 let ai_compression_indicator = if blocking_artifact < 5.0 &&
  631. 631 ringing_artifact < 0.1 {
  632. 632● 0.6
  633. 633● } else if blocking_artifact < 10.0 {
  634. 634● 0.4
  635. 635 } else {
  636. 636● 0.2
  637. 637 }
  638. 638
  639. 639 CompressionAnalysisResult::{
  640. 640 blocking_artifact_level: blocking_artifact,
  641. 641 ringing_artifact_level: ringing_artifact,
  642. 642 compression_noise_level: 0.5,
  643. 643 quantization_table_consistency: 0.5,
  644. 644 ai_compression_indicator,
  645. 645 }
  646. 646}
  647. 647
  648. 648///|
  649. 649/// 细节分析
  650. 650pub fn analyze_details(
  651. 651 img : @types.Image,
  652. 652) -> DetailAnalysisResult raise @types.LoadError {
  653. 653 // 输入验证
  654. 654● if img.width <= 0 || img.height <= 0 {
  655. 655● raise @types.LoadError::DecodeFailed(
  656. 656 "analyze_details: 图像尺寸必须大于0",
  657. 657 )
  658. 658 }
  659. 659 if img.channels < 1 || img.channels > 4 {
  660. 660○ raise @types.LoadError::DecodeFailed(
  661. 661 "analyze_details: 通道数必须在1-4之间",
  662. 662 )
  663. 663 }
  664. 664 let expected_size = img.width * img.height * img.channels
  665. 665● if img.data.length() != expected_size {
  666. 666○ raise @types.LoadError::DecodeFailed(
  667. 667 "analyze_details: 数据大小不匹配",
  668. 668 )
  669. 669 }
  670. 670 let w = img.width
  671. 671 let h = img.height
  672. 672 let _ = w * h
  673. 673
  674. 674 // 细节一致性(高频细节的局部一致性)
  675. 675 let mut detail_consistency = 0.0
  676. 676 let mut detail_count = 0
  677. 677 for y in 2..<(h - 2) {
  678. 678● for x in 2..<(w - 2) {
  679. 679● let idx = y * w + x
  680. 680● let _ = img.data[idx].to_double()
  681. 681 // 检查 5x5 邻域内的细节变化
  682. 682 let mut variance = 0.0
  683. 683 let mut sum = 0.0
  684. 684 let mut count = 0
  685. 685 for dy in -2..<3 {
  686. 686● for dx in -2..<3 {
  687. 687● let val = img.data[(y + dy) * w + (x + dx)].to_double()
  688. 688 sum += val
  689. 689 count += 1
  690. 690 }
  691. 691 }
  692. 692● let avg = sum / count.to_double()
  693. 693 for dy in -2..<3 {
  694. 694● for dx in -2..<3 {
  695. 695● let val = img.data[(y + dy) * w + (x + dx)].to_double()
  696. 696 variance += (val - avg) * (val - avg)
  697. 697 }
  698. 698 }
  699. 699● variance /= count.to_double()
  700. 700 if variance > 100.0 {
  701. 701● detail_consistency += variance.sqrt()
  702. 702 detail_count += 1
  703. 703 }
  704. 704 }
  705. 705 }
  706. 706 if detail_count > 0 {
  707. 707● detail_consistency /= detail_count.to_double()
  708. 708 }
  709. 709
  710. 710 // 对称性检测(左右对称性)
  711. 711 let mut symmetry = 0.0
  712. 712 let half_w = w / 2
  713. 713 for y in 0..<h {
  714. 714● for x in 0..<half_w {
  715. 715● let left = img.data[y * w + x].to_double()
  716. 716● let right = img.data[y * w + (w - 1 - x)].to_double()
  717. 717● symmetry += (left - right).abs()
  718. 718 }
  719. 719 }
  720. 720● symmetry /= (h * half_w).to_double()
  721. 721
  722. 722 // AI 生成图像通常细节一致性较差且对称性不自然
  723. 723 let ai_detail_indicator = if detail_consistency < 20.0 && symmetry > 30.0 {
  724. 724○ 0.7
  725. 725● } else if detail_consistency < 30.0 {
  726. 726● 0.5
  727. 727 } else {
  728. 728● 0.3
  729. 729 }
  730. 730
  731. 731 DetailAnalysisResult::{
  732. 732 eye_detail_consistency: detail_consistency,
  733. 733 finger_detail_consistency: detail_consistency,
  734. 734 hair_detail_consistency: detail_consistency,
  735. 735 text_readability: 0.5,
  736. 736 symmetry_consistency: symmetry,
  737. 737 ai_detail_indicator,
  738. 738 }
  739. 739}
  740. 740
  741. 741///|
  742. 742/// 综合来源鉴定
  743. 743/// @deprecated 已废弃,请使用 verify_image_source_enhanced 代替
  744. 744#deprecated("Use verify_image_source_enhanced instead", skip_current_package=true)
  745. 745pub fn verify_image_source(
  746. 746 img : @types.Image,
  747. 747 config : SourceVerificationConfig,
  748. 748) -> SourceVerificationResult raise @types.LoadError {
  749. 749 // 输入验证
  750. 750 if img.width <= 0 || img.height <= 0 {
  751. 751 raise @types.LoadError::DecodeFailed(
  752. 752 "verify_image_source: 图像尺寸必须大于0",
  753. 753 )
  754. 754 }
  755. 755 if img.channels < 1 || img.channels > 4 {
  756. 756 raise @types.LoadError::DecodeFailed(
  757. 757 "verify_image_source: 通道数必须在1-4之间",
  758. 758 )
  759. 759 }
  760. 760 let expected_size = img.width * img.height * img.channels
  761. 761 if img.data.length() != expected_size {
  762. 762 raise @types.LoadError::DecodeFailed(
  763. 763 "verify_image_source: 数据大小不匹配",
  764. 764 )
  765. 765 }
  766. 766 let noise_score = if config.use_noise_analysis {
  767. 767 analyze_noise_pattern(img).ai_noise_indicator
  768. 768 } else {
  769. 769 0.5
  770. 770 }
  771. 771 let freq_score = if config.use_frequency_analysis {
  772. 772 analyze_frequency_domain(img).ai_frequency_indicator
  773. 773 } else {
  774. 774 0.5
  775. 775 }
  776. 776 let color_score = if config.use_color_analysis {
  777. 777 analyze_color_distribution(img).ai_color_indicator
  778. 778 } else {
  779. 779 0.5
  780. 780 }
  781. 781 let texture_score = if config.use_texture_analysis {
  782. 782 analyze_texture(img).ai_texture_indicator
  783. 783 } else {
  784. 784 0.5
  785. 785 }
  786. 786 let edge_score = if config.use_edge_analysis {
  787. 787 analyze_edges(img).ai_edge_indicator
  788. 788 } else {
  789. 789 0.5
  790. 790 }
  791. 791 let compression_score = if config.use_compression_analysis {
  792. 792 analyze_compression_artifacts(img).ai_compression_indicator
  793. 793 } else {
  794. 794 0.5
  795. 795 }
  796. 796 let detail_score = if config.use_detail_analysis {
  797. 797 analyze_details(img).ai_detail_indicator
  798. 798 } else {
  799. 799 0.5
  800. 800 }
  801. 801
  802. 802 // 加权平均
  803. 803 let mut weighted_sum = 0.0
  804. 804 let mut weight_sum = 0.0
  805. 805 let scores = [
  806. 806 noise_score, freq_score, color_score, texture_score, edge_score, compression_score,
  807. 807 detail_score,
  808. 808 ]
  809. 809 for i in 0..<scores.length() {
  810. 810 let weight = if i < config.weights.length() {
  811. 811 config.weights[i]
  812. 812 } else {
  813. 813 1.0
  814. 814 }
  815. 815 weighted_sum += scores[i] * weight
  816. 816 weight_sum += weight
  817. 817 }
  818. 818 let overall_score = if weight_sum > 0.0 {
  819. 819 weighted_sum / weight_sum
  820. 820 } else {
  821. 821 0.5
  822. 822 }
  823. 823
  824. 824 let features = SourceVerificationFeatures::{
  825. 825 noise_pattern_score: noise_score,
  826. 826 frequency_domain_score: freq_score,
  827. 827 color_distribution_score: color_score,
  828. 828 texture_score,
  829. 829 edge_score,
  830. 830 compression_artifact_score: compression_score,
  831. 831 detail_score,
  832. 832 metadata_score: 0.5,
  833. 833 overall_score,
  834. 834 }
  835. 835
  836. 836 // 决策
  837. 837 let ai_probability = overall_score
  838. 838 let physical_probability = 1.0 - overall_score
  839. 839 let confidence = if ai_probability > 0.7 || ai_probability < 0.3 {
  840. 840 (ai_probability - 0.5).abs() * 2.0
  841. 841 } else {
  842. 842 0.3
  843. 843 }
  844. 844
  845. 845 let source_type = if ai_probability >= config.decision_threshold {
  846. 846 ImageSourceType::AIGenerated
  847. 847 } else {
  848. 848 ImageSourceType::PhysicalCaptured
  849. 849 }
  850. 850
  851. 851 SourceVerificationResult::{
  852. 852 source_type,
  853. 853 ai_probability,
  854. 854 physical_probability,
  855. 855 confidence,
  856. 856 decision_threshold: config.decision_threshold,
  857. 857 features,
  858. 858 processing_time_ms: 0.0,
  859. 859 }
  860. 860}
  861. 861
  862. 862///|
  863. 863/// 生成鉴定报告
  864. 864pub fn source_verification_report_to_string(
  865. 865 result : SourceVerificationResult,
  866. 866) -> String {
  867. 867● let mut report = "## 图像来源鉴定报告\n\n"
  868. 868 report = report + "### 鉴定结论\n\n"
  869. 869 report = report + "| 指标 | 值 |\n|------|------|\n"
  870. 870 let source_str = if result.source_type == ImageSourceType::AIGenerated {
  871. 871● "AI 生成"
  872. 872● } else if result.source_type == ImageSourceType::PhysicalCaptured {
  873. 873● "物理生成(真实拍摄)"
  874. 874○ } else if result.source_type == ImageSourceType::Unknown {
  875. 875○ "未知"
  876. 876 } else {
  877. 877○ "混合"
  878. 878 }
  879. 879 report = report + "| 来源类型 | " + source_str + " |\n"
  880. 880 report = report +
  881. 881 "| AI 生成概率 | " +
  882. 882● (result.ai_probability * 100.0).to_string() +
  883. 883 "% |\n"
  884. 884 report = report +
  885. 885 "| 物理生成概率 | " +
  886. 886● (result.physical_probability * 100.0).to_string() +
  887. 887 "% |\n"
  888. 888 report = report +
  889. 889 "| 置信度 | " +
  890. 890● (result.confidence * 100.0).to_string() +
  891. 891 "% |\n"
  892. 892 report = report +
  893. 893 "| 决策阈值 | " +
  894. 894● result.decision_threshold.to_string() +
  895. 895 " |\n\n"
  896. 896
  897. 897 report = report + "### 特征分析\n\n"
  898. 898 report = report +
  899. 899 "| 特征 | AI 指示值 | 说明 |\n|------|-----------|------|\n"
  900. 900 report = report +
  901. 901 "| 噪声模式 | " +
  902. 902● (result.features.noise_pattern_score * 100.0).to_string() +
  903. 903 "% | AI 生成图像噪声通常较低且相关性高 |\n"
  904. 904 report = report +
  905. 905 "| 频域特征 | " +
  906. 906● (result.features.frequency_domain_score * 100.0).to_string() +
  907. 907 "% | AI 生成图像高频能量通常较低 |\n"
  908. 908 report = report +
  909. 909 "| 颜色分布 | " +
  910. 910● (result.features.color_distribution_score * 100.0).to_string() +
  911. 911 "% | AI 生成图像颜色过渡通常过于平滑 |\n"
  912. 912 report = report +
  913. 913 "| 纹理特征 | " +
  914. 914● (result.features.texture_score * 100.0).to_string() +
  915. 915 "% | AI 生成图像纹理复杂度通常较低 |\n"
  916. 916 report = report +
  917. 917 "| 边缘特征 | " +
  918. 918● (result.features.edge_score * 100.0).to_string() +
  919. 919 "% | AI 生成图像边缘锐度通常较低 |\n"
  920. 920 report = report +
  921. 921 "| 压缩伪影 | " +
  922. 922● (result.features.compression_artifact_score * 100.0).to_string() +
  923. 923 "% | AI 生成图像压缩伪影通常较少 |\n"
  924. 924 report = report +
  925. 925 "| 细节特征 | " +
  926. 926● (result.features.detail_score * 100.0).to_string() +
  927. 927 "% | AI 生成图像细节一致性通常较差 |\n"
  928. 928 report = report +
  929. 929 "| 综合评分 | " +
  930. 930● (result.features.overall_score * 100.0).to_string() +
  931. 931 "% | 加权平均综合评分 |\n\n"
  932. 932
  933. 933 report = report + "### 说明\n\n"
  934. 934 report = report +
  935. 935 "- 本鉴定基于图像的统计特征分析,结果仅供参考\n"
  936. 936 report = report + "- AI 指示值越高,表示越可能是 AI 生成\n"
  937. 937 report = report + "- 置信度表示鉴定结果的可靠程度\n"
  938. 938 report = report +
  939. 939 "- 对于经过编辑、压缩或重采样的图像,鉴定准确率可能下降\n"
  940. 940
  941. 941 report
  942. 942}