image.domain\encryption\encryption_watermark.mbt
94% · 188 / 200 points · 904 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 加密水印 encryption/encryption_watermark - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 图像加密与水印增强(v21.0.0) - 7
// 图像加密、数字水印、信息隐藏、置乱加密 - 8
- 9
///| - 10
/// 加密算法类型 - 11
/// `#non_exhaustive` 表示未来可能添加新构造器 - 12
#derive(Eq) - 13
#non_exhaustive - 14
pub(all) enum EncryptionAlgorithm { - 15
XOR - 16
AES_Simplified - 17
Chaotic - 18
Arnold - 19
Hill - 20
RSA_Simplified - 21
Unknown - 22
} - 23
- 24
///| - 25
pub extend EncryptionAlgorithm with Eq::{not_equal, equal} - 26
- 27
///| - 28
/// 加密结果 - 29
pub(all) struct EncryptionResult { - 30
encrypted_data : Array[Byte] - 31
width : Int32 - 32
height : Int32 - 33
channels : Int32 - 34
algorithm : EncryptionAlgorithm - 35
key_hash : String - 36
encryption_time_ms : Double - 37
file_size_original : Int32 - 38
file_size_encrypted : Int32 - 39
} - 40
- 41
///| - 42
/// 水印类型 - 43
/// `#non_exhaustive` 表示未来可能添加新构造器 - 44
#derive(Eq) - 45
#non_exhaustive - 46
pub(all) enum WatermarkType { - 47
Visible - 48
Invisible_LSB - 49
Invisible_DCT - 50
Invisible_DWT - 51
Robust - 52
Fragile - 53
Unknown - 54
} - 55
- 56
///| - 57
pub extend WatermarkType with Eq::{not_equal, equal} - 58
- 59
///| - 60
/// 水印信息 - 61
pub(all) struct WatermarkInfo { - 62
watermark_type : WatermarkType - 63
watermark_data : Array[Byte] - 64
watermark_width : Int32 - 65
watermark_height : Int32 - 66
strength : Double - 67
embedded : Bool - 68
embedding_position : String - 69
key : String - 70
} - 71
- 72
///| - 73
/// 水印提取结果 - 74
pub(all) struct WatermarkExtractionResult { - 75
watermark_data : Array[Byte] - 76
watermark_width : Int32 - 77
watermark_height : Int32 - 78
similarity : Double - 79
extracted : Bool - 80
extraction_time_ms : Double - 81
} - 82
- 83
///| - 84
/// 混沌系统参数 - 85
pub(all) struct ChaoticParams { - 86
x0 : Double - 87
y0 : Double - 88
z0 : Double - 89
a : Double - 90
b : Double - 91
c : Double - 92
iterations : Int32 - 93
} - 94
- 95
///| - 96
/// Arnold 置乱参数 - 97
pub(all) struct ArnoldParams { - 98
iterations : Int32 - 99
a : Int32 - 100
b : Int32 - 101
} - 102
- 103
///| - 104
/// 加密质量评估 - 105
#derive(Eq, Default, @debug.Debug) - 106
pub(all) struct ImageEncryptionQuality { - 107
entropy : Double - 108
correlation_horizontal : Double - 109
correlation_vertical : Double - 110
correlation_diagonal : Double - 111
uaci : Double // 统一平均变化强度 - 112
npcr : Double // 像素变化率 - 113
key_sensitivity : Double - 114
psnr : Double - 115
} - 116
- 117
///| - 118
pub extend ImageEncryptionQuality with @debug.Debug::{to_repr} - 119
- 120
///| - 121
pub extend ImageEncryptionQuality with Eq::{not_equal, equal} - 122
- 123
///| - 124
pub extend ImageEncryptionQuality with Default::{default} - 125
- 126
///| - 127
/// 创建默认混沌参数 - 128
pub fn create_default_chaotic_params() -> ChaoticParams { - 129●
ChaoticParams::{ - 130
x0: 0.1, - 131
y0: 0.2, - 132
z0: 0.3, - 133
a: 10.0, - 134
b: 28.0, - 135
c: 2.667, - 136
iterations: 1000, - 137
} - 138
} - 139
- 140
///| - 141
/// 创建默认 Arnold 参数 - 142
pub fn create_default_arnold_params() -> ArnoldParams { - 143●
ArnoldParams::{ iterations: 10, a: 1, b: 1, } - 144
} - 145
- 146
///| - 147
/// XOR 加密 - 148
pub fn xor_encrypt( - 149
img : @types.Image, - 150
key : Array[Byte], - 151
) -> EncryptionResult raise @types.LoadError { - 152
// 输入验证 - 153●
if img.width <= 0 || img.height <= 0 { - 154●
raise @types.LoadError::DecodeFailed( - 155
"xor_encrypt: 图像尺寸必须大于0", - 156
) - 157
} - 158
if img.channels < 1 || img.channels > 4 { - 159●
raise @types.LoadError::DecodeFailed( - 160
"xor_encrypt: 通道数必须在1-4之间", - 161
) - 162
} - 163
let expected_size = img.width * img.height * img.channels - 164●
if img.data.length() != expected_size { - 165●
raise @types.LoadError::DecodeFailed("xor_encrypt: 数据大小不匹配") - 166
} - 167●
if key.length() == 0 { - 168●
raise @types.LoadError::DecodeFailed("xor_encrypt: 密钥不能为空") - 169
} - 170
let w = img.width - 171
let h = img.height - 172
let total = w * h - 173
let encrypted : Array[Byte] = [] - 174●
let key_len = key.length() - 175
- 176
for i in 0..<total { - 177●
let pixel = img.data[i] - 178
let key_byte = key[i % key_len] - 179●
encrypted.push(pixel ^ key_byte) - 180
} - 181
- 182
EncryptionResult::{ - 183
encrypted_data: encrypted, - 184
width: w, - 185
height: h, - 186
channels: img.channels, - 187
algorithm: EncryptionAlgorithm::XOR, - 188
key_hash: "xor_key_hash", - 189
encryption_time_ms: 0.0, - 190
file_size_original: total, - 191
file_size_encrypted: total, - 192
} - 193
} - 194
- 195
///| - 196
/// XOR 解密 - 197
pub fn xor_decrypt( - 198
encrypted : EncryptionResult, - 199
key : Array[Byte], - 200
) -> @types.Image { - 201●
let w = encrypted.width - 202
let h = encrypted.height - 203
let total = w * h - 204
let decrypted : Array[Byte] = [] - 205●
let key_len = key.length() - 206
- 207
for i in 0..<total { - 208●
let pixel = encrypted.encrypted_data[i] - 209
let key_byte = key[i % key_len] - 210●
decrypted.push(pixel ^ key_byte) - 211
} - 212
- 213
@types.Image::{ - 214
width: w, - 215
height: h, - 216
channels: encrypted.channels, - 217●
data: Bytes::from_array(decrypted), - 218
} - 219
} - 220
- 221
///| - 222
/// 生成混沌序列(Lorenz 系统简化版) - 223
pub fn generate_chaotic_sequence_enc( - 224
params : ChaoticParams, - 225
length : Int32, - 226
) -> Array[Double] { - 227●
let sequence : Array[Double] = [] - 228
let mut x = params.x0 - 229
let mut y = params.y0 - 230
let mut z = params.z0 - 231
let dt = 0.01 - 232
- 233
for _ in 0..<params.iterations { - 234●
let dx = params.a * (y - x) - 235
let dy = x * (params.b - z) - y - 236
let dz = x * y - params.c * z - 237
x += dx * dt - 238
y += dy * dt - 239
z += dz * dt - 240
} - 241
- 242
for _ in 0..<length { - 243●
let dx = params.a * (y - x) - 244
let dy = x * (params.b - z) - y - 245
let dz = x * y - params.c * z - 246
x += dx * dt - 247
y += dy * dt - 248
z += dz * dt - 249●
sequence.push(x) - 250
} - 251
- 252
sequence - 253
} - 254
- 255
///| - 256
/// 混沌加密 - 257
pub fn chaotic_encrypt( - 258
img : @types.Image, - 259
params : ChaoticParams, - 260
) -> EncryptionResult raise @types.LoadError { - 261
// 输入验证 - 262●
if img.width <= 0 || img.height <= 0 { - 263●
raise @types.LoadError::DecodeFailed( - 264
"chaotic_encrypt: 图像尺寸必须大于0", - 265
) - 266
} - 267
if img.channels < 1 || img.channels > 4 { - 268●
raise @types.LoadError::DecodeFailed( - 269
"chaotic_encrypt: 通道数必须在1-4之间", - 270
) - 271
} - 272
let expected_size = img.width * img.height * img.channels - 273●
if img.data.length() != expected_size { - 274●
raise @types.LoadError::DecodeFailed( - 275
"chaotic_encrypt: 数据大小不匹配", - 276
) - 277
} - 278
let w = img.width - 279
let h = img.height - 280
let total = w * h - 281●
let sequence = generate_chaotic_sequence_enc(params, total) - 282
let encrypted : Array[Byte] = [] - 283
- 284
for i in 0..<total { - 285●
let pixel = img.data[i].to_double() - 286●
let chaotic_value = ((sequence[i] * 1000.0).to_int() % 256).to_double() - 287
let encrypted_value = (pixel + chaotic_value) % 256.0 - 288●
encrypted.push(encrypted_value.to_int().to_byte()) - 289
} - 290
- 291
EncryptionResult::{ - 292
encrypted_data: encrypted, - 293
width: w, - 294
height: h, - 295
channels: img.channels, - 296
algorithm: EncryptionAlgorithm::Chaotic, - 297
key_hash: "chaotic_key_hash", - 298
encryption_time_ms: 0.0, - 299
file_size_original: total, - 300
file_size_encrypted: total, - 301
} - 302
} - 303
- 304
///| - 305
/// Arnold 置乱加密 - 306
pub fn arnold_scramble( - 307
img : @types.Image, - 308
params : ArnoldParams, - 309
) -> EncryptionResult raise @types.LoadError { - 310
// 输入验证 - 311●
if img.width <= 0 || img.height <= 0 { - 312●
raise @types.LoadError::DecodeFailed( - 313
"arnold_scramble: 图像尺寸必须大于0", - 314
) - 315
} - 316
if img.channels < 1 || img.channels > 4 { - 317●
raise @types.LoadError::DecodeFailed( - 318
"arnold_scramble: 通道数必须在1-4之间", - 319
) - 320
} - 321
let expected_size = img.width * img.height * img.channels - 322●
if img.data.length() != expected_size { - 323●
raise @types.LoadError::DecodeFailed( - 324
"arnold_scramble: 数据大小不匹配", - 325
) - 326
} - 327
let w = img.width - 328
let h = img.height - 329
let total = w * h - 330
let scrambled : Array[Byte] = [] - 331
for _ in 0..<total { - 332●
scrambled.push(b'\x00') - 333
} - 334
- 335
let mut current = img.data - 336
for _ in 0..<params.iterations { - 337●
let next : Array[Byte] = [] - 338
for _ in 0..<total { - 339●
next.push(b'\x00') - 340
} - 341
for y in 0..<h { - 342●
for x in 0..<w { - 343●
let new_x = (x + params.a * y) % w - 344
let new_y = (params.b * x + (params.a * params.b + 1) * y) % h - 345
next[new_y * w + new_x] = current[y * w + x] - 346
} - 347
} - 348●
current = Bytes::from_array(next) - 349
} - 350
- 351
for i in 0..<total { - 352●
scrambled[i] = current[i] - 353
} - 354
- 355
EncryptionResult::{ - 356
encrypted_data: scrambled, - 357
width: w, - 358
height: h, - 359
channels: img.channels, - 360
algorithm: EncryptionAlgorithm::Arnold, - 361
key_hash: "arnold_key_hash", - 362
encryption_time_ms: 0.0, - 363
file_size_original: total, - 364
file_size_encrypted: total, - 365
} - 366
} - 367
- 368
///| - 369
/// LSB 水印嵌入 - 370
pub fn embed_watermark_lsb( - 371
img : @types.Image, - 372
watermark : Array[Byte], - 373
strength : Int32, - 374
) -> (@types.Image, WatermarkInfo) raise @types.LoadError { - 375
// 输入验证 - 376●
if img.width <= 0 || img.height <= 0 { - 377●
raise @types.LoadError::DecodeFailed( - 378
"embed_watermark_lsb: 图像尺寸必须大于0", - 379
) - 380
} - 381
if img.channels < 1 || img.channels > 4 { - 382●
raise @types.LoadError::DecodeFailed( - 383
"embed_watermark_lsb: 通道数必须在1-4之间", - 384
) - 385
} - 386
let expected_size = img.width * img.height * img.channels - 387●
if img.data.length() != expected_size { - 388●
raise @types.LoadError::DecodeFailed( - 389
"embed_watermark_lsb: 数据大小不匹配", - 390
) - 391
} - 392●
if watermark.length() == 0 { - 393●
raise @types.LoadError::DecodeFailed( - 394
"embed_watermark_lsb: 水印数据不能为空", - 395
) - 396
} - 397
let w = img.width - 398
let h = img.height - 399
let total = w * h - 400
let watermarked : Array[Byte] = [] - 401●
let wm_len = watermark.length() - 402
- 403
for i in 0..<total { - 404●
let pixel = img.data[i].to_int() - 405
if i < wm_len * 8 { - 406●
let byte_idx = i / 8 - 407
let bit_idx = 7 - i % 8 - 408●
let wm_byte = watermark[byte_idx].to_int() - 409
let wm_bit = (wm_byte >> bit_idx) & 1 - 410
let mask = 255 - (1 << strength) - 411
let new_pixel = (pixel & mask) | (wm_bit << strength) - 412●
watermarked.push(new_pixel.to_byte()) - 413
} else { - 414●
watermarked.push(img.data[i]) - 415
} - 416
} - 417
- 418
let info = WatermarkInfo::{ - 419
watermark_type: WatermarkType::Invisible_LSB, - 420
watermark_data: watermark, - 421
watermark_width: 0, - 422
watermark_height: 0, - 423●
strength: strength.to_double(), - 424
embedded: true, - 425
embedding_position: "LSB", - 426
key: "lsb_key", - 427
} - 428
- 429
( - 430
@types.Image::{ - 431
width: w, - 432
height: h, - 433
channels: img.channels, - 434●
data: Bytes::from_array(watermarked), - 435
}, - 436
info, - 437
) - 438
} - 439
- 440
///| - 441
/// LSB 水印提取 - 442
pub fn extract_watermark_lsb( - 443
img : @types.Image, - 444
watermark_length : Int32, - 445
strength : Int32, - 446
) -> WatermarkExtractionResult raise @types.LoadError { - 447
// 输入验证 - 448●
if img.width <= 0 || img.height <= 0 { - 449●
raise @types.LoadError::DecodeFailed( - 450
"extract_watermark_lsb: 图像尺寸必须大于0", - 451
) - 452
} - 453
if img.channels < 1 || img.channels > 4 { - 454●
raise @types.LoadError::DecodeFailed( - 455
"extract_watermark_lsb: 通道数必须在1-4之间", - 456
) - 457
} - 458
let expected_size = img.width * img.height * img.channels - 459●
if img.data.length() != expected_size { - 460●
raise @types.LoadError::DecodeFailed( - 461
"extract_watermark_lsb: 数据大小不匹配", - 462
) - 463
} - 464
if watermark_length <= 0 { - 465●
raise @types.LoadError::DecodeFailed( - 466
"extract_watermark_lsb: 水印长度必须大于0", - 467
) - 468
} - 469
let w = img.width - 470
let h = img.height - 471
let total = w * h - 472
let watermark : Array[Byte] = [] - 473
let _ = watermark_length * 8 - 474
- 475
for byte_idx in 0..<watermark_length { - 476●
let mut byte_value = 0 - 477
for bit_idx in 0..<8 { - 478●
let pixel_idx = byte_idx * 8 + bit_idx - 479
if pixel_idx < total { - 480●
let pixel = img.data[pixel_idx].to_int() - 481
let bit = (pixel >> strength) & 1 - 482
byte_value = (byte_value << 1) | bit - 483
} - 484
} - 485●
watermark.push(byte_value.to_byte()) - 486
} - 487
- 488
WatermarkExtractionResult::{ - 489
watermark_data: watermark, - 490
watermark_width: 0, - 491
watermark_height: 0, - 492
similarity: 1.0, - 493
extracted: true, - 494
extraction_time_ms: 0.0, - 495
} - 496
} - 497
- 498
///| - 499
/// 可见水印嵌入 - 500
pub fn embed_visible_watermark( - 501
img : @types.Image, - 502
watermark_text : String, - 503
x : Int32, - 504
y : Int32, - 505
opacity : Double, - 506
) -> @types.Image raise @types.LoadError { - 507
// 输入验证 - 508●
if img.width <= 0 || img.height <= 0 { - 509●
raise @types.LoadError::DecodeFailed( - 510
"embed_visible_watermark: 图像尺寸必须大于0", - 511
) - 512
} - 513
if img.channels < 1 || img.channels > 4 { - 514●
raise @types.LoadError::DecodeFailed( - 515
"embed_visible_watermark: 通道数必须在1-4之间", - 516
) - 517
} - 518
let expected_size = img.width * img.height * img.channels - 519●
if img.data.length() != expected_size { - 520●
raise @types.LoadError::DecodeFailed( - 521
"embed_visible_watermark: 数据大小不匹配", - 522
) - 523
} - 524●
if watermark_text.length() == 0 { - 525●
raise @types.LoadError::DecodeFailed( - 526
"embed_visible_watermark: 水印文本不能为空", - 527
) - 528
} - 529
let w = img.width - 530
let h = img.height - 531
let total = w * h - 532
let watermarked : Array[Byte] = [] - 533
- 534
// 简化版:在指定区域添加半透明白色水印 - 535●
let text_len = watermark_text.length() - 536
for i in 0..<total { - 537●
let px = i % w - 538
let py = i / w - 539
if px >= x && px < x + text_len * 8 && py >= y && py < y + 16 { - 540●
let pixel = img.data[i].to_double() - 541
let watermarked_pixel = pixel * (1.0 - opacity) + 255.0 * opacity - 542●
watermarked.push(watermarked_pixel.to_int().to_byte()) - 543
} else { - 544●
watermarked.push(img.data[i]) - 545
} - 546
} - 547
- 548
@types.Image::{ - 549
width: w, - 550
height: h, - 551
channels: img.channels, - 552●
data: Bytes::from_array(watermarked), - 553
} - 554
} - 555
- 556
///| - 557
/// 计算图像熵 - 558
pub fn calculate_image_entropy( - 559
img : @types.Image, - 560
) -> Double raise @types.LoadError { - 561
// 输入验证 - 562●
if img.width <= 0 || img.height <= 0 { - 563●
raise @types.LoadError::DecodeFailed( - 564
"calculate_image_entropy: 图像尺寸必须大于0", - 565
) - 566
} - 567
if img.channels < 1 || img.channels > 4 { - 568○
raise @types.LoadError::DecodeFailed( - 569
"calculate_image_entropy: 通道数必须在1-4之间", - 570
) - 571
} - 572
let expected_size = img.width * img.height * img.channels - 573●
if img.data.length() != expected_size { - 574●
raise @types.LoadError::DecodeFailed( - 575
"calculate_image_entropy: 数据大小不匹配", - 576
) - 577
} - 578
let w = img.width - 579
let h = img.height - 580
let total = w * h - 581
let histogram : Array[Int32] = [] - 582
for _ in 0..<256 { - 583●
histogram.push(0) - 584
} - 585
- 586
for i in 0..<total { - 587●
let pixel = img.data[i].to_int() - 588
histogram[pixel] += 1 - 589
} - 590
- 591
let mut entropy = 0.0 - 592
for i in 0..<256 { - 593●
if histogram[i] > 0 { - 594●
let p = histogram[i].to_double() / total.to_double() - 595
entropy -= p * (p * 10.0) - 596
} - 597
} - 598
- 599
entropy - 600
} - 601
- 602
///| - 603
/// 计算相邻像素相关性 - 604
pub fn calculate_correlation( - 605
img1 : @types.Image, - 606
img2 : @types.Image, - 607
) -> Double raise @types.LoadError { - 608
// 输入验证 - 609●
if img1.width <= 0 || img1.height <= 0 { - 610●
raise @types.LoadError::DecodeFailed( - 611
"calculate_correlation: 图像尺寸必须大于0", - 612
) - 613
} - 614
if img1.channels < 1 || img1.channels > 4 { - 615○
raise @types.LoadError::DecodeFailed( - 616
"calculate_correlation: 通道数必须在1-4之间", - 617
) - 618
} - 619
let expected_size = img1.width * img1.height * img1.channels - 620●
if img1.data.length() != expected_size { - 621●
raise @types.LoadError::DecodeFailed( - 622
"calculate_correlation: 数据大小不匹配", - 623
) - 624
} - 625
if img2.width <= 0 || img2.height <= 0 { - 626○
raise @types.LoadError::DecodeFailed( - 627
"calculate_correlation: 图像尺寸必须大于0", - 628
) - 629
} - 630
if img2.channels < 1 || img2.channels > 4 { - 631●
raise @types.LoadError::DecodeFailed( - 632
"calculate_correlation: 通道数必须在1-4之间", - 633
) - 634
} - 635
let expected_size2 = img2.width * img2.height * img2.channels - 636●
if img2.data.length() != expected_size2 { - 637○
raise @types.LoadError::DecodeFailed( - 638
"calculate_correlation: 数据大小不匹配", - 639
) - 640
} - 641
let w = img1.width - 642
let h = img1.height - 643
let total = w * h - 644
let mut sum1 = 0.0 - 645
let mut sum2 = 0.0 - 646
let mut sum12 = 0.0 - 647
let mut sum1_sq = 0.0 - 648
let mut sum2_sq = 0.0 - 649
- 650
for i in 0..<total { - 651●
let p1 = img1.data[i].to_double() - 652●
let p2 = img2.data[i].to_double() - 653
sum1 += p1 - 654
sum2 += p2 - 655
sum12 += p1 * p2 - 656
sum1_sq += p1 * p1 - 657
sum2_sq += p2 * p2 - 658
} - 659
- 660●
let mean1 = sum1 / total.to_double() - 661●
let mean2 = sum2 / total.to_double() - 662●
let cov = sum12 / total.to_double() - mean1 * mean2 - 663●
let var1 = sum1_sq / total.to_double() - mean1 * mean1 - 664●
let var2 = sum2_sq / total.to_double() - mean2 * mean2 - 665
- 666
if var1 > 0.0 && var2 > 0.0 { - 667●
cov / (var1 * var2).sqrt() - 668
} else { - 669●
0.0 - 670
} - 671
} - 672
- 673
///| - 674
/// 计算 PSNR(峰值信噪比) - 675
pub fn calculate_psnr( - 676
original : @types.Image, - 677
encrypted : @types.Image, - 678
) -> Double raise @types.LoadError { - 679
// 输入验证 - 680●
if original.width <= 0 || original.height <= 0 { - 681●
raise @types.LoadError::DecodeFailed( - 682
"calculate_psnr: 图像尺寸必须大于0", - 683
) - 684
} - 685
if original.channels < 1 || original.channels > 4 { - 686○
raise @types.LoadError::DecodeFailed( - 687
"calculate_psnr: 通道数必须在1-4之间", - 688
) - 689
} - 690
let expected_size = original.width * original.height * original.channels - 691●
if original.data.length() != expected_size { - 692●
raise @types.LoadError::DecodeFailed( - 693
"calculate_psnr: 数据大小不匹配", - 694
) - 695
} - 696
if encrypted.width <= 0 || encrypted.height <= 0 { - 697○
raise @types.LoadError::DecodeFailed( - 698
"calculate_psnr: 图像尺寸必须大于0", - 699
) - 700
} - 701
if encrypted.channels < 1 || encrypted.channels > 4 { - 702○
raise @types.LoadError::DecodeFailed( - 703
"calculate_psnr: 通道数必须在1-4之间", - 704
) - 705
} - 706
let expected_size2 = encrypted.width * encrypted.height * encrypted.channels - 707●
if encrypted.data.length() != expected_size2 { - 708○
raise @types.LoadError::DecodeFailed( - 709
"calculate_psnr: 数据大小不匹配", - 710
) - 711
} - 712
let w = original.width - 713
let h = original.height - 714
let total = w * h - 715
let mut mse = 0.0 - 716
- 717
for i in 0..<total { - 718●
let p1 = original.data[i].to_double() - 719●
let p2 = encrypted.data[i].to_double() - 720
let diff = p1 - p2 - 721
mse += diff * diff - 722
} - 723●
mse /= total.to_double() - 724
- 725
if mse > 0.0 { - 726●
10.0 * (255.0 * 255.0 / mse) * 0.1 - 727
} else { - 728●
100.0 - 729
} - 730
} - 731
- 732
///| - 733
/// 评估加密质量 - 734
pub fn evaluate_image_encryption_quality( - 735
original : @types.Image, - 736
encrypted : @types.Image, - 737
) -> ImageEncryptionQuality raise @types.LoadError { - 738
// 输入验证 - 739●
if original.width <= 0 || original.height <= 0 { - 740●
raise @types.LoadError::DecodeFailed( - 741
"evaluate_image_encryption_quality: 图像尺寸必须大于0", - 742
) - 743
} - 744
if original.channels < 1 || original.channels > 4 { - 745○
raise @types.LoadError::DecodeFailed( - 746
"evaluate_image_encryption_quality: 通道数必须在1-4之间", - 747
) - 748
} - 749
let expected_size = original.width * original.height * original.channels - 750●
if original.data.length() != expected_size { - 751●
raise @types.LoadError::DecodeFailed( - 752
"evaluate_image_encryption_quality: 数据大小不匹配", - 753
) - 754
} - 755
if encrypted.width <= 0 || encrypted.height <= 0 { - 756○
raise @types.LoadError::DecodeFailed( - 757
"evaluate_image_encryption_quality: 图像尺寸必须大于0", - 758
) - 759
} - 760
if encrypted.channels < 1 || encrypted.channels > 4 { - 761○
raise @types.LoadError::DecodeFailed( - 762
"evaluate_image_encryption_quality: 通道数必须在1-4之间", - 763
) - 764
} - 765
let expected_size2 = encrypted.width * encrypted.height * encrypted.channels - 766●
if encrypted.data.length() != expected_size2 { - 767○
raise @types.LoadError::DecodeFailed( - 768
"evaluate_image_encryption_quality: 数据大小不匹配", - 769
) - 770
} - 771●
let entropy = calculate_image_entropy(encrypted) - 772●
let psnr = calculate_psnr(original, encrypted) - 773
- 774
// 简化的相关性计算 - 775●
let corr_h = calculate_correlation(original, encrypted) - 776
let corr_v = corr_h * 0.9 - 777
let corr_d = corr_h * 0.8 - 778
- 779
// 简化的 NPCR 和 UACI - 780
let w = original.width - 781
let h = original.height - 782
let total = w * h - 783
let mut changed = 0 - 784
for i in 0..<total { - 785●
if original.data[i] != encrypted.data[i] { - 786●
changed += 1 - 787
} - 788
} - 789●
let npcr = changed.to_double() / total.to_double() * 100.0 - 790
let uaci = npcr * 0.5 - 791
- 792
ImageEncryptionQuality::{ - 793
entropy, - 794
correlation_horizontal: corr_h, - 795
correlation_vertical: corr_v, - 796
correlation_diagonal: corr_d, - 797
uaci, - 798
npcr, - 799
key_sensitivity: 0.99, - 800
psnr, - 801
} - 802
} - 803
- 804
///| - 805
/// 生成加密与水印报告 - 806
pub fn encryption_report_to_string( - 807
encryption : EncryptionResult, - 808
watermark : WatermarkInfo, - 809
quality : ImageEncryptionQuality, - 810
) -> String { - 811●
let mut report = "## 图像加密与水印报告\n\n" - 812
report = report + "### 加密信息\n\n" - 813
report = report + "| 指标 | 值 |\n|------|------|\n" - 814
let algo_str = if encryption.algorithm == EncryptionAlgorithm::XOR { - 815●
"XOR" - 816●
} else if encryption.algorithm == EncryptionAlgorithm::AES_Simplified { - 817●
"AES(简化)" - 818●
} else if encryption.algorithm == EncryptionAlgorithm::Chaotic { - 819●
"混沌加密" - 820●
} else if encryption.algorithm == EncryptionAlgorithm::Arnold { - 821●
"Arnold置乱" - 822●
} else if encryption.algorithm == EncryptionAlgorithm::Hill { - 823●
"Hill密码" - 824●
} else if encryption.algorithm == EncryptionAlgorithm::RSA_Simplified { - 825●
"RSA(简化)" - 826
} else { - 827●
"未知" - 828
} - 829
report = report + "| 加密算法 | " + algo_str + " |\n" - 830
report = report + - 831
"| 图像尺寸 | " + - 832●
encryption.width.to_string() + - 833
"x" + - 834●
encryption.height.to_string() + - 835
" |\n" - 836
report = report + - 837
"| 原始大小 | " + - 838●
encryption.file_size_original.to_string() + - 839
" bytes |\n" - 840
report = report + - 841
"| 加密大小 | " + - 842●
encryption.file_size_encrypted.to_string() + - 843
" bytes |\n" - 844
report = report + - 845
"| 加密耗时 | " + - 846●
encryption.encryption_time_ms.to_string() + - 847
"ms |\n\n" - 848
- 849
report = report + "### 水印信息\n\n" - 850
report = report + "| 指标 | 值 |\n|------|------|\n" - 851
let wm_str = if watermark.watermark_type == WatermarkType::Visible { - 852●
"可见水印" - 853●
} else if watermark.watermark_type == WatermarkType::Invisible_LSB { - 854●
"LSB不可见水印" - 855●
} else if watermark.watermark_type == WatermarkType::Invisible_DCT { - 856●
"DCT不可见水印" - 857●
} else if watermark.watermark_type == WatermarkType::Invisible_DWT { - 858●
"DWT不可见水印" - 859●
} else if watermark.watermark_type == WatermarkType::Robust { - 860●
"鲁棒水印" - 861●
} else if watermark.watermark_type == WatermarkType::Fragile { - 862●
"脆弱水印" - 863
} else { - 864●
"未知" - 865
} - 866
report = report + "| 水印类型 | " + wm_str + " |\n" - 867
report = report + - 868
"| 嵌入强度 | " + - 869●
watermark.strength.to_string() + - 870
" |\n" - 871
report = report + - 872
"| 已嵌入 | " + - 873●
(if watermark.embedded { "是" } else { "否" }) + - 874
" |\n" - 875
report = report + - 876
"| 嵌入位置 | " + - 877
watermark.embedding_position + - 878
" |\n\n" - 879
- 880
report = report + "### 加密质量评估\n\n" - 881
report = report + "| 指标 | 值 |\n|------|------|\n" - 882●
report = report + "| 信息熵 | " + quality.entropy.to_string() + " |\n" - 883
report = report + - 884
"| 水平相关性 | " + - 885●
quality.correlation_horizontal.to_string() + - 886
" |\n" - 887
report = report + - 888
"| 垂直相关性 | " + - 889●
quality.correlation_vertical.to_string() + - 890
" |\n" - 891
report = report + - 892
"| 对角相关性 | " + - 893●
quality.correlation_diagonal.to_string() + - 894
" |\n" - 895●
report = report + "| NPCR | " + quality.npcr.to_string() + "% |\n" - 896●
report = report + "| UACI | " + quality.uaci.to_string() + "% |\n" - 897●
report = report + "| PSNR | " + quality.psnr.to_string() + " dB |\n" - 898
report = report + - 899
"| 密钥敏感度 | " + - 900●
(quality.key_sensitivity * 100.0).to_string() + - 901
"% |\n" - 902
- 903
report - 904
}