image.domain\forensics\enhanced_source_verification.mbt
91.2% · 271 / 297 points · 1141 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 增强来源验证 forensics/enhanced_source_verification - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 图像来源鉴定增强(v27.1.0) - 7
// 增强准确性:UI 元素检测、文字区域检测、几何形状分析、颜色量化分析、智能决策引擎 - 8
- 9
///| - 10
/// 增强鉴定结果 - 11
pub(all) struct EnhancedVerificationResult { - 12
source_type : ImageSourceType - 13
ai_probability : Double - 14
physical_probability : Double - 15
confidence : Double - 16
decision_threshold : Double - 17
basic_features : SourceVerificationFeatures - 18
enhanced_features : EnhancedVerificationFeatures - 19
ui_detection : UIDetectionResult - 20
text_detection : TextDetectionResult - 21
geometry_analysis : GeometryAnalysisResult - 22
color_quantization : ColorQuantizationResult - 23
decision_reason : String - 24
processing_time_ms : Double - 25
} - 26
- 27
///| - 28
/// 增强鉴定特征 - 29
pub(all) struct EnhancedVerificationFeatures { - 30
ui_indicator : Double - 31
text_indicator : Double - 32
geometry_indicator : Double - 33
color_quantization_indicator : Double - 34
solid_area_indicator : Double - 35
edge_direction_indicator : Double - 36
pixel_distribution_indicator : Double - 37
overall_enhanced_score : Double - 38
} - 39
- 40
///| - 41
/// UI 检测结果 - 42
pub(all) struct UIDetectionResult { - 43
has_window_border : Bool - 44
has_toolbar : Bool - 45
has_button_like_elements : Bool - 46
ui_element_count : Int32 - 47
ui_area_ratio : Double - 48
horizontal_line_count : Int32 - 49
vertical_line_count : Int32 - 50
ui_indicator : Double - 51
} - 52
- 53
///| - 54
/// 文字检测结果 - 55
pub(all) struct TextDetectionResult { - 56
text_area_count : Int32 - 57
text_area_ratio : Double - 58
dense_edge_region_count : Int32 - 59
small_edge_density : Double - 60
text_like_region_ratio : Double - 61
text_indicator : Double - 62
} - 63
- 64
///| - 65
/// 几何分析结果 - 66
pub(all) struct GeometryAnalysisResult { - 67
horizontal_edge_ratio : Double - 68
vertical_edge_ratio : Double - 69
diagonal_edge_ratio : Double - 70
rectangle_like_region_count : Int32 - 71
straight_line_count : Int32 - 72
geometry_regularity : Double - 73
geometry_indicator : Double - 74
} - 75
- 76
///| - 77
/// 颜色量化结果 - 78
pub(all) struct ColorQuantizationResult { - 79
unique_color_count : Int32 - 80
color_quantization_level : Double - 81
top_color_ratio : Double - 82
top_10_color_ratio : Double - 83
color_palette_size : Int32 - 84
color_quantization_indicator : Double - 85
} - 86
- 87
///| - 88
/// 增强鉴定配置 - 89
pub(all) struct EnhancedVerificationConfig { - 90
use_ui_detection : Bool - 91
use_text_detection : Bool - 92
use_geometry_analysis : Bool - 93
use_color_quantization : Bool - 94
use_solid_area_analysis : Bool - 95
use_edge_direction_analysis : Bool - 96
basic_weight : Double - 97
enhanced_weight : Double - 98
decision_threshold : Double - 99
} - 100
- 101
///| - 102
/// 创建默认增强鉴定配置 - 103
pub fn create_default_enhanced_verification_config() -> EnhancedVerificationConfig { - 104●
EnhancedVerificationConfig::{ - 105
use_ui_detection: true, - 106
use_text_detection: true, - 107
use_geometry_analysis: true, - 108
use_color_quantization: true, - 109
use_solid_area_analysis: true, - 110
use_edge_direction_analysis: true, - 111
basic_weight: 0.4, - 112
enhanced_weight: 0.6, - 113
decision_threshold: 0.5, - 114
} - 115
} - 116
- 117
///| - 118
/// UI 元素检测 - 119
pub fn detect_ui_elements( - 120
img : @types.Image, - 121
) -> UIDetectionResult raise @types.LoadError { - 122
// 输入验证 - 123●
if img.width <= 0 || img.height <= 0 { - 124●
raise @types.LoadError::DecodeFailed( - 125
"detect_ui_elements: 图像尺寸必须大于0", - 126
) - 127
} - 128
if img.channels < 1 || img.channels > 4 { - 129●
raise @types.LoadError::DecodeFailed( - 130
"detect_ui_elements: 通道数必须在1-4之间", - 131
) - 132
} - 133
let expected_size = img.width * img.height * img.channels - 134●
if img.data.length() != expected_size { - 135●
raise @types.LoadError::DecodeFailed( - 136
"detect_ui_elements: 数据大小不匹配", - 137
) - 138
} - 139
let w = img.width - 140
let h = img.height - 141
let total = w * h - 142
- 143
// 检测水平长线(窗口边框、工具栏分隔线) - 144
let mut horizontal_lines = 0 - 145
for y in 0..<h { - 146●
let mut run_length = 0 - 147
for x in 0..<w { - 148●
let idx = y * w + x - 149
// 检测边缘像素 - 150
let is_edge = if x > 0 && x < w - 1 { - 151●
let diff = (img.data[idx].to_double() - img.data[idx - 1].to_double()).abs() - 152
diff > 20.0 - 153
} else { - 154●
false - 155
} - 156
if is_edge { - 157●
run_length += 1 - 158
} else { - 159●
if run_length > w / 4 { - 160●
horizontal_lines += 1 - 161
} - 162
run_length = 0 - 163
} - 164
} - 165
if run_length > w / 4 { - 166○
horizontal_lines += 1 - 167
} - 168
} - 169
- 170
// 检测垂直长线 - 171
let mut vertical_lines = 0 - 172
for x in 0..<w { - 173●
let mut run_length = 0 - 174
for y in 0..<h { - 175●
let idx = y * w + x - 176
let is_edge = if y > 0 && y < h - 1 { - 177●
let diff = (img.data[idx].to_double() - img.data[idx - w].to_double()).abs() - 178
diff > 20.0 - 179
} else { - 180●
false - 181
} - 182
if is_edge { - 183●
run_length += 1 - 184
} else { - 185●
if run_length > h / 4 { - 186●
vertical_lines += 1 - 187
} - 188
run_length = 0 - 189
} - 190
} - 191
if run_length > h / 4 { - 192○
vertical_lines += 1 - 193
} - 194
} - 195
- 196
// 检测窗口边框(图像四周的边框) - 197
let has_window_border = horizontal_lines >= 2 && vertical_lines >= 2 - 198
- 199
// 检测工具栏(顶部的密集水平线条区域) - 200
let has_toolbar = horizontal_lines >= 3 - 201
- 202
// UI 区域比例(有边缘的区域比例) - 203
let mut ui_area = 0 - 204
for y in 0..<h { - 205●
for x in 0..<w { - 206●
let idx = y * w + x - 207
let mut has_edge = false - 208
if x > 0 && x < w - 1 { - 209●
if (img.data[idx].to_double() - img.data[idx - 1].to_double()).abs() > - 210
20.0 { - 211●
has_edge = true - 212
} - 213
} - 214
if y > 0 && y < h - 1 { - 215●
if (img.data[idx].to_double() - img.data[idx - w].to_double()).abs() > - 216
20.0 { - 217●
has_edge = true - 218
} - 219
} - 220
if has_edge { - 221●
ui_area += 1 - 222
} - 223
} - 224
} - 225●
let ui_area_ratio = ui_area.to_double() / total.to_double() - 226
- 227
// 按钮状元素检测(小矩形区域) - 228
let button_like = horizontal_lines >= 4 && vertical_lines >= 4 - 229
- 230
// UI 指示值:UI 元素越多,越可能是屏幕截图(物理生成) - 231
let ui_indicator = if has_window_border && has_toolbar { - 232●
0.9 - 233●
} else if horizontal_lines >= 3 || vertical_lines >= 3 { - 234○
0.7 - 235●
} else if horizontal_lines >= 2 || vertical_lines >= 2 { - 236○
0.5 - 237
} else { - 238●
0.2 - 239
} - 240
- 241
UIDetectionResult::{ - 242
has_window_border, - 243
has_toolbar, - 244
has_button_like_elements: button_like, - 245
ui_element_count: horizontal_lines + vertical_lines, - 246
ui_area_ratio, - 247
horizontal_line_count: horizontal_lines, - 248
vertical_line_count: vertical_lines, - 249
ui_indicator, - 250
} - 251
} - 252
- 253
///| - 254
/// 文字区域检测 - 255
pub fn detect_text_regions( - 256
img : @types.Image, - 257
) -> TextDetectionResult raise @types.LoadError { - 258
// 输入验证 - 259●
if img.width <= 0 || img.height <= 0 { - 260●
raise @types.LoadError::DecodeFailed( - 261
"detect_text_regions: 图像尺寸必须大于0", - 262
) - 263
} - 264
if img.channels < 1 || img.channels > 4 { - 265●
raise @types.LoadError::DecodeFailed( - 266
"detect_text_regions: 通道数必须在1-4之间", - 267
) - 268
} - 269
let expected_size = img.width * img.height * img.channels - 270●
if img.data.length() != expected_size { - 271●
raise @types.LoadError::DecodeFailed( - 272
"detect_text_regions: 数据大小不匹配", - 273
) - 274
} - 275
let w = img.width - 276
let h = img.height - 277
let total = w * h - 278
- 279
// 检测密集小边缘区域(文字特征) - 280
let mut dense_edge_regions = 0 - 281
let block_size = 16 - 282
let blocks_x = w / block_size - 283
let blocks_y = h / block_size - 284
- 285
for by in 0..<blocks_y { - 286●
for bx in 0..<blocks_x { - 287●
let mut edge_count = 0 - 288
for y in 0..<block_size { - 289●
for x in 0..<block_size { - 290●
let px = bx * block_size + x - 291
let py = by * block_size + y - 292
if px > 0 && px < w - 1 && py > 0 && py < h - 1 { - 293●
let idx = py * w + px - 294●
let gx = (img.data[idx + 1].to_double() - - 295●
img.data[idx - 1].to_double()).abs() - 296●
let gy = (img.data[idx + w].to_double() - - 297●
img.data[idx - w].to_double()).abs() - 298
if gx > 15.0 || gy > 15.0 { - 299●
edge_count += 1 - 300
} - 301
} - 302
} - 303
} - 304●
let edge_density = edge_count.to_double() / - 305●
(block_size * block_size).to_double() - 306
// 文字区域通常边缘密度在 0.15-0.5 之间 - 307
if edge_density > 0.15 && edge_density < 0.6 { - 308●
dense_edge_regions += 1 - 309
} - 310
} - 311
} - 312
- 313●
let text_area_ratio = dense_edge_regions.to_double() / - 314●
(blocks_x * blocks_y).to_double() - 315
- 316
// 小边缘密度(全局) - 317
let mut small_edges = 0 - 318
for y in 1..<(h - 1) { - 319●
for x in 1..<(w - 1) { - 320●
let idx = y * w + x - 321●
let gx = (img.data[idx + 1].to_double() - img.data[idx - 1].to_double()).abs() - 322●
let gy = (img.data[idx + w].to_double() - img.data[idx - w].to_double()).abs() - 323●
let mag = (gx * gx + gy * gy).sqrt() - 324
if mag > 10.0 && mag < 80.0 { - 325●
small_edges += 1 - 326
} - 327
} - 328
} - 329●
let small_edge_density = small_edges.to_double() / total.to_double() - 330
- 331
// 文字指示值:文字区域越多,越可能是屏幕截图(物理生成) - 332
let text_indicator = if text_area_ratio > 0.15 { - 333●
0.85 - 334●
} else if text_area_ratio > 0.08 { - 335○
0.65 - 336●
} else if text_area_ratio > 0.03 { - 337○
0.45 - 338
} else { - 339●
0.2 - 340
} - 341
- 342
TextDetectionResult::{ - 343
text_area_count: dense_edge_regions, - 344
text_area_ratio, - 345
dense_edge_region_count: dense_edge_regions, - 346
small_edge_density, - 347
text_like_region_ratio: text_area_ratio, - 348
text_indicator, - 349
} - 350
} - 351
- 352
///| - 353
/// 几何形状分析 - 354
pub fn analyze_geometry( - 355
img : @types.Image, - 356
) -> GeometryAnalysisResult raise @types.LoadError { - 357
// 输入验证 - 358●
if img.width <= 0 || img.height <= 0 { - 359●
raise @types.LoadError::DecodeFailed( - 360
"analyze_geometry: 图像尺寸必须大于0", - 361
) - 362
} - 363
if img.channels < 1 || img.channels > 4 { - 364●
raise @types.LoadError::DecodeFailed( - 365
"analyze_geometry: 通道数必须在1-4之间", - 366
) - 367
} - 368
let expected_size = img.width * img.height * img.channels - 369●
if img.data.length() != expected_size { - 370●
raise @types.LoadError::DecodeFailed( - 371
"analyze_geometry: 数据大小不匹配", - 372
) - 373
} - 374
let w = img.width - 375
let h = img.height - 376
- 377
// 边缘方向分析 - 378
let mut horizontal_edges = 0 - 379
let mut vertical_edges = 0 - 380
let mut diagonal_edges = 0 - 381
let mut total_edges = 0 - 382
- 383
for y in 1..<(h - 1) { - 384●
for x in 1..<(w - 1) { - 385●
let idx = y * w + x - 386●
let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double() - 387●
let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double() - 388●
let mag = (gx * gx + gy * gy).sqrt() - 389
if mag > 20.0 { - 390●
total_edges += 1 - 391●
let abs_gx = gx.abs() - 392●
let abs_gy = gy.abs() - 393
if abs_gx > abs_gy * 2.0 { - 394●
horizontal_edges += 1 - 395●
} else if abs_gy > abs_gx * 2.0 { - 396●
vertical_edges += 1 - 397
} else { - 398●
diagonal_edges += 1 - 399
} - 400
} - 401
} - 402
} - 403
- 404
let horizontal_ratio = if total_edges > 0 { - 405●
horizontal_edges.to_double() / total_edges.to_double() - 406
} else { - 407●
0.0 - 408
} - 409
let vertical_ratio = if total_edges > 0 { - 410●
vertical_edges.to_double() / total_edges.to_double() - 411
} else { - 412●
0.0 - 413
} - 414
let diagonal_ratio = if total_edges > 0 { - 415●
diagonal_edges.to_double() / total_edges.to_double() - 416
} else { - 417●
0.0 - 418
} - 419
- 420
// 几何规则性:水平+垂直边缘比例越高,越规则(UI 界面特征) - 421
let geometry_regularity = horizontal_ratio + vertical_ratio - 422
- 423
// 矩形状区域检测(规则的水平垂直线条交叉) - 424
let rectangle_regions = if geometry_regularity > 0.7 { - 425●
total_edges / 100 - 426
} else { - 427●
0 - 428
} - 429
- 430
// 几何指示值:几何越规则,越可能是屏幕截图(物理生成) - 431
let geometry_indicator = if geometry_regularity > 0.8 { - 432●
0.85 - 433●
} else if geometry_regularity > 0.65 { - 434●
0.65 - 435●
} else if geometry_regularity > 0.5 { - 436●
0.45 - 437
} else { - 438●
0.25 - 439
} - 440
- 441
GeometryAnalysisResult::{ - 442
horizontal_edge_ratio: horizontal_ratio, - 443
vertical_edge_ratio: vertical_ratio, - 444
diagonal_edge_ratio: diagonal_ratio, - 445
rectangle_like_region_count: rectangle_regions, - 446
straight_line_count: horizontal_edges + vertical_edges, - 447
geometry_regularity, - 448
geometry_indicator, - 449
} - 450
} - 451
- 452
///| - 453
/// 颜色量化分析 - 454
pub fn analyze_color_quantization( - 455
img : @types.Image, - 456
) -> ColorQuantizationResult raise @types.LoadError { - 457
// 输入验证 - 458●
if img.width <= 0 || img.height <= 0 { - 459●
raise @types.LoadError::DecodeFailed( - 460
"analyze_color_quantization: 图像尺寸必须大于0", - 461
) - 462
} - 463
if img.channels < 1 || img.channels > 4 { - 464●
raise @types.LoadError::DecodeFailed( - 465
"analyze_color_quantization: 通道数必须在1-4之间", - 466
) - 467
} - 468
let expected_size = img.width * img.height * img.channels - 469●
if img.data.length() != expected_size { - 470●
raise @types.LoadError::DecodeFailed( - 471
"analyze_color_quantization: 数据大小不匹配", - 472
) - 473
} - 474
let w = img.width - 475
let h = img.height - 476
let total = w * h - 477
- 478
// 统计唯一颜色数量(量化到 64 级) - 479
let color_histogram : Array[Int32] = [] - 480
for _ in 0..<64 { - 481●
color_histogram.push(0) - 482
} - 483
for i in 0..<total { - 484●
let bin = img.data[i].to_int() * 64 / 256 - 485
color_histogram[bin] += 1 - 486
} - 487
- 488
let mut unique_colors = 0 - 489
for i in 0..<64 { - 490●
if color_histogram[i] > 0 { - 491●
unique_colors += 1 - 492
} - 493
} - 494
- 495
// 顶部颜色比例 - 496
let sorted_colors : Array[Int32] = [] - 497
for i in 0..<64 { - 498●
sorted_colors.push(color_histogram[i]) - 499
} - 500
// 冒泡排序 - 501
for i in 0..<64 { - 502●
for j in 0..<(64 - i - 1) { - 503●
if sorted_colors[j] < sorted_colors[j + 1] { - 504●
let temp = sorted_colors[j] - 505
sorted_colors[j] = sorted_colors[j + 1] - 506
sorted_colors[j + 1] = temp - 507
} - 508
} - 509
} - 510
- 511
let top_color_ratio = if total > 0 { - 512●
sorted_colors[0].to_double() / total.to_double() - 513
} else { - 514○
0.0 - 515
} - 516
let mut top_10_sum = 0 - 517
for i in 0..<10 { - 518●
top_10_sum += sorted_colors[i] - 519
} - 520
let top_10_ratio = if total > 0 { - 521●
top_10_sum.to_double() / total.to_double() - 522
} else { - 523○
0.0 - 524
} - 525
- 526
// 颜色量化级别:唯一颜色越少,量化越严重(UI 界面特征) - 527●
let quantization_level = 1.0 - unique_colors.to_double() / 64.0 - 528
- 529
// 颜色调色板大小(估计) - 530
let palette_size = unique_colors - 531
- 532
// 颜色量化指示值:颜色越集中,越可能是屏幕截图(物理生成) - 533
let color_quantization_indicator = if top_10_ratio > 0.8 { - 534●
0.85 - 535●
} else if top_10_ratio > 0.65 { - 536○
0.65 - 537●
} else if top_10_ratio > 0.5 { - 538○
0.45 - 539
} else { - 540●
0.25 - 541
} - 542
- 543
ColorQuantizationResult::{ - 544
unique_color_count: unique_colors, - 545
color_quantization_level: quantization_level, - 546
top_color_ratio, - 547
top_10_color_ratio: top_10_ratio, - 548
color_palette_size: palette_size, - 549
color_quantization_indicator, - 550
} - 551
} - 552
- 553
///| - 554
/// 纯色区域分析 - 555
pub fn analyze_solid_areas(img : @types.Image) -> Double raise @types.LoadError { - 556
// 输入验证 - 557●
if img.width <= 0 || img.height <= 0 { - 558●
raise @types.LoadError::DecodeFailed( - 559
"analyze_solid_areas: 图像尺寸必须大于0", - 560
) - 561
} - 562
if img.channels < 1 || img.channels > 4 { - 563●
raise @types.LoadError::DecodeFailed( - 564
"analyze_solid_areas: 通道数必须在1-4之间", - 565
) - 566
} - 567
let expected_size = img.width * img.height * img.channels - 568●
if img.data.length() != expected_size { - 569●
raise @types.LoadError::DecodeFailed( - 570
"analyze_solid_areas: 数据大小不匹配", - 571
) - 572
} - 573
let w = img.width - 574
let h = img.height - 575
let _ = w * h - 576
let block_size = 8 - 577
let mut solid_blocks = 0 - 578
let total_blocks = w / block_size * (h / block_size) - 579
- 580
for by in 0..<(h / block_size) { - 581●
for bx in 0..<(w / block_size) { - 582●
let mut min_val = 255 - 583
let mut max_val = 0 - 584
for y in 0..<block_size { - 585●
for x in 0..<block_size { - 586●
let px = bx * block_size + x - 587
let py = by * block_size + y - 588●
let val = img.data[py * w + px].to_int() - 589
if val < min_val { - 590●
min_val = val - 591
} - 592
if val > max_val { - 593●
max_val = val - 594
} - 595
} - 596
} - 597
if max_val - min_val < 10 { - 598●
solid_blocks += 1 - 599
} - 600
} - 601
} - 602
- 603
let solid_ratio = if total_blocks > 0 { - 604●
solid_blocks.to_double() / total_blocks.to_double() - 605
} else { - 606○
0.0 - 607
} - 608
- 609
// 纯色区域指示值:纯色区域越多,越可能是屏幕截图(物理生成) - 610
if solid_ratio > 0.5 { - 611●
0.8 - 612●
} else if solid_ratio > 0.35 { - 613○
0.6 - 614●
} else if solid_ratio > 0.2 { - 615○
0.4 - 616
} else { - 617●
0.2 - 618
} - 619
} - 620
- 621
///| - 622
/// 边缘方向分析 - 623
pub fn analyze_edge_directions( - 624
img : @types.Image, - 625
) -> Double raise @types.LoadError { - 626
// 输入验证 - 627●
if img.width <= 0 || img.height <= 0 { - 628●
raise @types.LoadError::DecodeFailed( - 629
"analyze_edge_directions: 图像尺寸必须大于0", - 630
) - 631
} - 632
if img.channels < 1 || img.channels > 4 { - 633●
raise @types.LoadError::DecodeFailed( - 634
"analyze_edge_directions: 通道数必须在1-4之间", - 635
) - 636
} - 637
let expected_size = img.width * img.height * img.channels - 638●
if img.data.length() != expected_size { - 639●
raise @types.LoadError::DecodeFailed( - 640
"analyze_edge_directions: 数据大小不匹配", - 641
) - 642
} - 643●
let geometry = analyze_geometry(img) - 644
// 水平+垂直边缘比例越高,越可能是 UI 界面(物理生成) - 645
let hv_ratio = geometry.horizontal_edge_ratio + geometry.vertical_edge_ratio - 646
if hv_ratio > 0.8 { - 647●
0.8 - 648●
} else if hv_ratio > 0.65 { - 649●
0.6 - 650●
} else if hv_ratio > 0.5 { - 651●
0.4 - 652
} else { - 653●
0.2 - 654
} - 655
} - 656
- 657
///| - 658
/// 像素值分布分析 - 659
pub fn analyze_pixel_distribution( - 660
img : @types.Image, - 661
) -> Double raise @types.LoadError { - 662
// 输入验证 - 663●
if img.width <= 0 || img.height <= 0 { - 664●
raise @types.LoadError::DecodeFailed( - 665
"analyze_pixel_distribution: 图像尺寸必须大于0", - 666
) - 667
} - 668
if img.channels < 1 || img.channels > 4 { - 669●
raise @types.LoadError::DecodeFailed( - 670
"analyze_pixel_distribution: 通道数必须在1-4之间", - 671
) - 672
} - 673
let expected_size = img.width * img.height * img.channels - 674●
if img.data.length() != expected_size { - 675●
raise @types.LoadError::DecodeFailed( - 676
"analyze_pixel_distribution: 数据大小不匹配", - 677
) - 678
} - 679
let w = img.width - 680
let h = img.height - 681
let total = w * h - 682
- 683
// 计算像素值分布的熵 - 684
let histogram : Array[Int32] = [] - 685
for _ in 0..<256 { - 686●
histogram.push(0) - 687
} - 688
for i in 0..<total { - 689●
histogram[img.data[i].to_int()] += 1 - 690
} - 691
- 692
let mut entropy = 0.0 - 693
for i in 0..<256 { - 694●
if histogram[i] > 0 { - 695●
let p = histogram[i].to_double() / total.to_double() - 696
entropy -= p * p * 0.1 // 简化的 log2 - 697
} - 698
} - 699
- 700
// 像素分布越集中(熵越低),越可能是 UI 界面(物理生成) - 701
if entropy < 0.5 { - 702●
0.75 - 703○
} else if entropy < 1.0 { - 704○
0.55 - 705○
} else if entropy < 1.5 { - 706○
0.35 - 707
} else { - 708○
0.2 - 709
} - 710
} - 711
- 712
///| - 713
/// 智能决策引擎 - 714
pub fn intelligent_decision( - 715
basic_result : SourceVerificationResult, - 716
ui_result : UIDetectionResult, - 717
text_result : TextDetectionResult, - 718
geometry_result : GeometryAnalysisResult, - 719
color_result : ColorQuantizationResult, - 720
solid_indicator : Double, - 721
edge_dir_indicator : Double, - 722
pixel_dist_indicator : Double, - 723
config : EnhancedVerificationConfig, - 724
) -> (ImageSourceType, Double, Double, Double, String) { - 725
// 增强特征的物理生成指示值(值越高越可能是物理生成) - 726●
let enhanced_physical_score = ui_result.ui_indicator * 0.25 + - 727
text_result.text_indicator * 0.20 + - 728
geometry_result.geometry_indicator * 0.15 + - 729
color_result.color_quantization_indicator * 0.15 + - 730
solid_indicator * 0.10 + - 731
edge_dir_indicator * 0.08 + - 732
pixel_dist_indicator * 0.07 - 733
- 734
// 基础特征的 AI 生成指示值 - 735
let basic_ai_score = basic_result.ai_probability - 736
- 737
// 加权融合 - 738
let final_ai_score = basic_ai_score * config.basic_weight + - 739
(1.0 - enhanced_physical_score) * config.enhanced_weight - 740
let final_physical_score = 1.0 - final_ai_score - 741
- 742
// 智能决策规则 - 743
let mut decision_reason = "" - 744
let mut source_type = ImageSourceType::Unknown - 745
let mut confidence = 0.0 - 746
- 747
// 强 UI 特征判定 - 748
if ui_result.has_window_border && - 749
ui_result.has_toolbar && - 750
text_result.text_area_ratio > 0.05 { - 751●
source_type = ImageSourceType::PhysicalCaptured - 752
confidence = 0.9 - 753
decision_reason = "检测到窗口边框、工具栏和文字区域,强烈判定为屏幕截图(物理生成)" - 754●
} else if ui_result.ui_element_count >= 6 && - 755
geometry_result.geometry_regularity > 0.7 { - 756○
source_type = ImageSourceType::PhysicalCaptured - 757
confidence = 0.8 - 758
decision_reason = "检测到大量 UI 元素和规则几何形状,判定为屏幕截图(物理生成)" - 759●
} else if text_result.text_area_ratio > 0.1 && - 760
color_result.top_10_color_ratio > 0.7 { - 761●
source_type = ImageSourceType::PhysicalCaptured - 762
confidence = 0.75 - 763
decision_reason = "检测到大量文字区域和集中的颜色分布,判定为屏幕截图(物理生成)" - 764●
} else if enhanced_physical_score > 0.65 { - 765○
source_type = ImageSourceType::PhysicalCaptured - 766
confidence = 0.7 - 767
decision_reason = "增强特征综合评分偏向物理生成,判定为物理生成" - 768●
} else if final_ai_score >= config.decision_threshold && - 769
enhanced_physical_score < 0.4 { - 770●
source_type = ImageSourceType::AIGenerated - 771●
confidence = if final_ai_score > 0.7 { 0.75 } else { 0.55 } - 772
decision_reason = "基础特征和增强特征均偏向 AI 生成,判定为 AI 生成" - 773●
} else if final_ai_score >= config.decision_threshold { - 774●
source_type = ImageSourceType::AIGenerated - 775
confidence = 0.45 - 776
decision_reason = "基础特征偏向 AI 生成,但增强特征存在不确定性,低置信度判定为 AI 生成" - 777
} else { - 778●
source_type = ImageSourceType::PhysicalCaptured - 779
confidence = 0.55 - 780
decision_reason = "综合特征偏向物理生成,判定为物理生成" - 781
} - 782
- 783
( - 784
source_type, final_ai_score, final_physical_score, confidence, decision_reason, - 785
) - 786
} - 787
- 788
///| - 789
/// 增强图像来源鉴定 - 790
pub fn verify_image_source_enhanced( - 791
img : @types.Image, - 792
config : EnhancedVerificationConfig, - 793
) -> EnhancedVerificationResult raise @types.LoadError { - 794
// 输入验证 - 795●
if img.width <= 0 || img.height <= 0 { - 796●
raise @types.LoadError::DecodeFailed( - 797
"verify_image_source_enhanced: 图像尺寸必须大于0", - 798
) - 799
} - 800
if img.channels < 1 || img.channels > 4 { - 801●
raise @types.LoadError::DecodeFailed( - 802
"verify_image_source_enhanced: 通道数必须在1-4之间", - 803
) - 804
} - 805
let expected_size = img.width * img.height * img.channels - 806●
if img.data.length() != expected_size { - 807●
raise @types.LoadError::DecodeFailed( - 808
"verify_image_source_enhanced: 数据大小不匹配", - 809
) - 810
} - 811
// 基础鉴定 - 812●
let basic_config = create_default_source_verification_config() - 813●
let basic_result = verify_image_source(img, basic_config) - 814
- 815
// UI 检测 - 816
let ui_result = if config.use_ui_detection { - 817●
detect_ui_elements(img) - 818
} else { - 819●
UIDetectionResult::{ - 820
has_window_border: false, - 821
has_toolbar: false, - 822
has_button_like_elements: false, - 823
ui_element_count: 0, - 824
ui_area_ratio: 0.0, - 825
horizontal_line_count: 0, - 826
vertical_line_count: 0, - 827
ui_indicator: 0.5, - 828
} - 829
} - 830
- 831
// 文字检测 - 832
let text_result = if config.use_text_detection { - 833●
detect_text_regions(img) - 834
} else { - 835●
TextDetectionResult::{ - 836
text_area_count: 0, - 837
text_area_ratio: 0.0, - 838
dense_edge_region_count: 0, - 839
small_edge_density: 0.0, - 840
text_like_region_ratio: 0.0, - 841
text_indicator: 0.5, - 842
} - 843
} - 844
- 845
// 几何分析 - 846
let geometry_result = if config.use_geometry_analysis { - 847●
analyze_geometry(img) - 848
} else { - 849●
GeometryAnalysisResult::{ - 850
horizontal_edge_ratio: 0.5, - 851
vertical_edge_ratio: 0.5, - 852
diagonal_edge_ratio: 0.0, - 853
rectangle_like_region_count: 0, - 854
straight_line_count: 0, - 855
geometry_regularity: 1.0, - 856
geometry_indicator: 0.5, - 857
} - 858
} - 859
- 860
// 颜色量化分析 - 861
let color_result = if config.use_color_quantization { - 862●
analyze_color_quantization(img) - 863
} else { - 864●
ColorQuantizationResult::{ - 865
unique_color_count: 32, - 866
color_quantization_level: 0.5, - 867
top_color_ratio: 0.1, - 868
top_10_color_ratio: 0.5, - 869
color_palette_size: 32, - 870
color_quantization_indicator: 0.5, - 871
} - 872
} - 873
- 874
// 纯色区域分析 - 875
let solid_indicator = if config.use_solid_area_analysis { - 876●
analyze_solid_areas(img) - 877
} else { - 878●
0.5 - 879
} - 880
- 881
// 边缘方向分析 - 882
let edge_dir_indicator = if config.use_edge_direction_analysis { - 883●
analyze_edge_directions(img) - 884
} else { - 885●
0.5 - 886
} - 887
- 888
// 像素值分布分析 - 889●
let pixel_dist_indicator = analyze_pixel_distribution(img) - 890
- 891
// 增强特征综合评分 - 892
let enhanced_overall = ui_result.ui_indicator * 0.25 + - 893
text_result.text_indicator * 0.20 + - 894
geometry_result.geometry_indicator * 0.15 + - 895
color_result.color_quantization_indicator * 0.15 + - 896
solid_indicator * 0.10 + - 897
edge_dir_indicator * 0.08 + - 898
pixel_dist_indicator * 0.07 - 899
- 900
let enhanced_features = EnhancedVerificationFeatures::{ - 901
ui_indicator: ui_result.ui_indicator, - 902
text_indicator: text_result.text_indicator, - 903
geometry_indicator: geometry_result.geometry_indicator, - 904
color_quantization_indicator: color_result.color_quantization_indicator, - 905
solid_area_indicator: solid_indicator, - 906
edge_direction_indicator: edge_dir_indicator, - 907
pixel_distribution_indicator: pixel_dist_indicator, - 908
overall_enhanced_score: enhanced_overall, - 909
} - 910
- 911
// 智能决策 - 912●
let (source_type, ai_prob, physical_prob, confidence, reason) = intelligent_decision( - 913
basic_result, ui_result, text_result, geometry_result, color_result, solid_indicator, - 914
edge_dir_indicator, pixel_dist_indicator, config, - 915
) - 916
- 917
EnhancedVerificationResult::{ - 918
source_type, - 919
ai_probability: ai_prob, - 920
physical_probability: physical_prob, - 921
confidence, - 922
decision_threshold: config.decision_threshold, - 923
basic_features: basic_result.features, - 924
enhanced_features, - 925
ui_detection: ui_result, - 926
text_detection: text_result, - 927
geometry_analysis: geometry_result, - 928
color_quantization: color_result, - 929
decision_reason: reason, - 930
processing_time_ms: 0.0, - 931
} - 932
} - 933
- 934
///| - 935
/// 生成增强鉴定报告 - 936
pub fn enhanced_verification_report_to_string( - 937
result : EnhancedVerificationResult, - 938
) -> String { - 939●
let mut report = "## 图像来源鉴定报告(增强版)\n\n" - 940
report = report + "### 鉴定结论\n\n" - 941
report = report + "| 指标 | 值 |\n|------|------|\n" - 942
let source_str = if result.source_type == ImageSourceType::AIGenerated { - 943●
"AI 生成" - 944●
} else if result.source_type == ImageSourceType::PhysicalCaptured { - 945●
"物理生成(真实拍摄/屏幕截图)" - 946○
} else if result.source_type == ImageSourceType::Unknown { - 947○
"未知" - 948
} else { - 949○
"混合" - 950
} - 951
report = report + "| 来源类型 | " + source_str + " |\n" - 952
report = report + - 953
"| AI 生成概率 | " + - 954●
(result.ai_probability * 100.0).to_string() + - 955
"% |\n" - 956
report = report + - 957
"| 物理生成概率 | " + - 958●
(result.physical_probability * 100.0).to_string() + - 959
"% |\n" - 960
report = report + - 961
"| 置信度 | " + - 962●
(result.confidence * 100.0).to_string() + - 963
"% |\n" - 964
report = report + "| 决策依据 | " + result.decision_reason + " |\n\n" - 965
- 966
report = report + "### 基础特征分析\n\n" - 967
report = report + "| 特征 | AI 指示值 |\n|------|-----------|\n" - 968
report = report + - 969
"| 噪声模式 | " + - 970●
(result.basic_features.noise_pattern_score * 100.0).to_string() + - 971
"% |\n" - 972
report = report + - 973
"| 频域特征 | " + - 974●
(result.basic_features.frequency_domain_score * 100.0).to_string() + - 975
"% |\n" - 976
report = report + - 977
"| 颜色分布 | " + - 978●
(result.basic_features.color_distribution_score * 100.0).to_string() + - 979
"% |\n" - 980
report = report + - 981
"| 纹理特征 | " + - 982●
(result.basic_features.texture_score * 100.0).to_string() + - 983
"% |\n" - 984
report = report + - 985
"| 边缘特征 | " + - 986●
(result.basic_features.edge_score * 100.0).to_string() + - 987
"% |\n" - 988
report = report + - 989
"| 压缩伪影 | " + - 990●
(result.basic_features.compression_artifact_score * 100.0).to_string() + - 991
"% |\n" - 992
report = report + - 993
"| 细节特征 | " + - 994●
(result.basic_features.detail_score * 100.0).to_string() + - 995
"% |\n" - 996
report = report + - 997
"| 基础综合 | " + - 998●
(result.basic_features.overall_score * 100.0).to_string() + - 999
"% |\n\n" - 1000
- 1001
report = report + "### 增强特征分析\n\n" - 1002
report = report + - 1003
"| 特征 | 物理生成指示值 | 说明 |\n|------|---------------|------|\n" - 1004
report = report + - 1005
"| UI 元素检测 | " + - 1006●
(result.enhanced_features.ui_indicator * 100.0).to_string() + - 1007
"% | 窗口边框/工具栏/按钮检测 |\n" - 1008
report = report + - 1009
"| 文字区域检测 | " + - 1010●
(result.enhanced_features.text_indicator * 100.0).to_string() + - 1011
"% | 密集小边缘区域检测 |\n" - 1012
report = report + - 1013
"| 几何形状分析 | " + - 1014●
(result.enhanced_features.geometry_indicator * 100.0).to_string() + - 1015
"% | 水平/垂直边缘比例 |\n" - 1016
report = report + - 1017
"| 颜色量化分析 | " + - 1018●
(result.enhanced_features.color_quantization_indicator * 100.0).to_string() + - 1019
"% | 颜色集中度/调色板大小 |\n" - 1020
report = report + - 1021
"| 纯色区域分析 | " + - 1022●
(result.enhanced_features.solid_area_indicator * 100.0).to_string() + - 1023
"% | 大面积纯色区域比例 |\n" - 1024
report = report + - 1025
"| 边缘方向分析 | " + - 1026●
(result.enhanced_features.edge_direction_indicator * 100.0).to_string() + - 1027
"% | 水平+垂直边缘占比 |\n" - 1028
report = report + - 1029
"| 像素分布分析 | " + - 1030●
(result.enhanced_features.pixel_distribution_indicator * 100.0).to_string() + - 1031
"% | 像素值分布熵 |\n" - 1032
report = report + - 1033
"| 增强综合 | " + - 1034●
(result.enhanced_features.overall_enhanced_score * 100.0).to_string() + - 1035
"% | 加权综合评分 |\n\n" - 1036
- 1037
report = report + "### UI 检测详情\n\n" - 1038
report = report + "| 指标 | 值 |\n|------|------|\n" - 1039
report = report + - 1040
"| 窗口边框 | " + - 1041
(if result.ui_detection.has_window_border { - 1042○
"检测到" - 1043
} else { - 1044●
"未检测到" - 1045
}) + - 1046
" |\n" - 1047
report = report + - 1048
"| 工具栏 | " + - 1049◐
(if result.ui_detection.has_toolbar { "检测到" } else { "未检测到" }) + - 1050
" |\n" - 1051
report = report + - 1052
"| 按钮状元素 | " + - 1053
(if result.ui_detection.has_button_like_elements { - 1054○
"检测到" - 1055
} else { - 1056●
"未检测到" - 1057
}) + - 1058
" |\n" - 1059
report = report + - 1060
"| UI 元素总数 | " + - 1061●
result.ui_detection.ui_element_count.to_string() + - 1062
" |\n" - 1063
report = report + - 1064
"| 水平线条数 | " + - 1065●
result.ui_detection.horizontal_line_count.to_string() + - 1066
" |\n" - 1067
report = report + - 1068
"| 垂直线条数 | " + - 1069●
result.ui_detection.vertical_line_count.to_string() + - 1070
" |\n" - 1071
report = report + - 1072
"| UI 区域比例 | " + - 1073●
(result.ui_detection.ui_area_ratio * 100.0).to_string() + - 1074
"% |\n\n" - 1075
- 1076
report = report + "### 文字检测详情\n\n" - 1077
report = report + "| 指标 | 值 |\n|------|------|\n" - 1078
report = report + - 1079
"| 文字区域数 | " + - 1080●
result.text_detection.text_area_count.to_string() + - 1081
" |\n" - 1082
report = report + - 1083
"| 文字区域比例 | " + - 1084●
(result.text_detection.text_area_ratio * 100.0).to_string() + - 1085
"% |\n" - 1086
report = report + - 1087
"| 小边缘密度 | " + - 1088●
(result.text_detection.small_edge_density * 100.0).to_string() + - 1089
"% |\n\n" - 1090
- 1091
report = report + "### 几何分析详情\n\n" - 1092
report = report + "| 指标 | 值 |\n|------|------|\n" - 1093
report = report + - 1094
"| 水平边缘比例 | " + - 1095●
(result.geometry_analysis.horizontal_edge_ratio * 100.0).to_string() + - 1096
"% |\n" - 1097
report = report + - 1098
"| 垂直边缘比例 | " + - 1099●
(result.geometry_analysis.vertical_edge_ratio * 100.0).to_string() + - 1100
"% |\n" - 1101
report = report + - 1102
"| 对角边缘比例 | " + - 1103●
(result.geometry_analysis.diagonal_edge_ratio * 100.0).to_string() + - 1104
"% |\n" - 1105
report = report + - 1106
"| 几何规则性 | " + - 1107●
(result.geometry_analysis.geometry_regularity * 100.0).to_string() + - 1108
"% |\n\n" - 1109
- 1110
report = report + "### 颜色量化详情\n\n" - 1111
report = report + "| 指标 | 值 |\n|------|------|\n" - 1112
report = report + - 1113
"| 唯一颜色数(64级) | " + - 1114●
result.color_quantization.unique_color_count.to_string() + - 1115
" |\n" - 1116
report = report + - 1117
"| 顶部颜色比例 | " + - 1118●
(result.color_quantization.top_color_ratio * 100.0).to_string() + - 1119
"% |\n" - 1120
report = report + - 1121
"| 前10颜色比例 | " + - 1122●
(result.color_quantization.top_10_color_ratio * 100.0).to_string() + - 1123
"% |\n" - 1124
report = report + - 1125
"| 调色板大小 | " + - 1126●
result.color_quantization.color_palette_size.to_string() + - 1127
" |\n\n" - 1128
- 1129
report = report + "### 说明\n\n" - 1130
report = report + - 1131
"- 本鉴定基于基础统计特征 + 增强特征(UI/文字/几何/颜色)综合分析\n" - 1132
report = report + - 1133
"- 增强特征专门针对屏幕截图/数字渲染图进行优化\n" - 1134
report = report + - 1135
"- 物理生成指示值越高,表示越可能是真实拍摄或屏幕截图\n" - 1136
report = report + "- 置信度表示鉴定结果的可靠程度\n" - 1137
report = report + - 1138
"- 对于经过编辑、压缩或重采样的图像,鉴定准确率可能下降\n" - 1139
- 1140
report - 1141
}