image.domain\encryption\encryption_watermark.mbt

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