image.photography\compression\image_compression.mbt

93.9% · 184 / 196 points · 742 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/// # 图像压缩 compression/image_compression
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 图像压缩与编码优化(v28.0.0)
  7. 7// 图像压缩、量化、熵编码、压缩质量评估
  8. 8
  9. 9///|
  10. 10/// 压缩算法类型
  11. 11/// `#non_exhaustive` 表示未来可能添加新构造器
  12. 12#derive(Eq, @debug.Debug)
  13. 13#non_exhaustive
  14. 14pub(all) enum CompressionAlgorithm {
  15. 15 RunLengthEncoding
  16. 16 HuffmanCoding
  17. 17 ArithmeticCoding
  18. 18 LZW
  19. 19 Deflate
  20. 20 JPEGBaseline
  21. 21 JPEG2000
  22. 22 WebP
  23. 23 AVIF
  24. 24 PNG
  25. 25 Unknown
  26. 26}
  27. 27
  28. 28///|
  29. 29pub extend CompressionAlgorithm with @debug.Debug::{to_repr}
  30. 30
  31. 31///|
  32. 32pub extend CompressionAlgorithm with Eq::{not_equal, equal}
  33. 33
  34. 34///|
  35. 35/// 压缩结果
  36. 36#derive(Eq, @debug.Debug)
  37. 37pub(all) struct CompressionResult {
  38. 38 compressed_data : Bytes
  39. 39 original_size : Int32
  40. 40 compressed_size : Int32
  41. 41 compression_ratio : Double
  42. 42 bits_per_pixel : Double
  43. 43 algorithm : CompressionAlgorithm
  44. 44 quality : Double
  45. 45 processing_time_ms : Double
  46. 46}
  47. 47
  48. 48///|
  49. 49pub extend CompressionResult with Eq::{not_equal, equal}
  50. 50
  51. 51///|
  52. 52pub extend CompressionResult with @debug.Debug::{to_repr}
  53. 53
  54. 54///|
  55. 55/// 压缩配置
  56. 56#derive(Eq, @debug.Debug)
  57. 57pub(all) struct CompressionConfig {
  58. 58 algorithm : CompressionAlgorithm
  59. 59 quality : Double
  60. 60 quantization_level : Int32
  61. 61 use_dct : Bool
  62. 62 use_prediction : Bool
  63. 63 use_entropy_coding : Bool
  64. 64 block_size : Int32
  65. 65}
  66. 66
  67. 67///|
  68. 68pub extend CompressionConfig with Eq::{not_equal, equal}
  69. 69
  70. 70///|
  71. 71pub extend CompressionConfig with @debug.Debug::{to_repr}
  72. 72
  73. 73///|
  74. 74/// 压缩质量评估
  75. 75#derive(Eq, @debug.Debug)
  76. 76pub(all) struct CompressionQualityMetrics {
  77. 77 psnr : Double
  78. 78 ssim : Double
  79. 79 mse : Double
  80. 80 compression_ratio : Double
  81. 81 bits_per_pixel : Double
  82. 82 structural_similarity : Double
  83. 83 overall_quality : Double
  84. 84}
  85. 85
  86. 86///|
  87. 87pub extend CompressionQualityMetrics with Eq::{not_equal, equal}
  88. 88
  89. 89///|
  90. 90pub extend CompressionQualityMetrics with @debug.Debug::{to_repr}
  91. 91
  92. 92///|
  93. 93/// 量化表类型
  94. 94/// `#non_exhaustive` 表示未来可能添加新构造器
  95. 95#derive(Eq, @debug.Debug)
  96. 96#non_exhaustive
  97. 97pub(all) enum QuantizationTableType {
  98. 98 Luminance
  99. 99 Chrominance
  100. 100 Custom
  101. 101 Default
  102. 102}
  103. 103
  104. 104///|
  105. 105pub extend QuantizationTableType with @debug.Debug::{to_repr}
  106. 106
  107. 107///|
  108. 108pub extend QuantizationTableType with Eq::{not_equal, equal}
  109. 109
  110. 110///|
  111. 111/// DCT 系数块
  112. 112#derive(Eq, @debug.Debug)
  113. 113pub(all) struct DCTBlock {
  114. 114 coefficients : Array[Double]
  115. 115 block_size : Int32
  116. 116 width : Int32
  117. 117 height : Int32
  118. 118}
  119. 119
  120. 120///|
  121. 121pub extend DCTBlock with Eq::{not_equal, equal}
  122. 122
  123. 123///|
  124. 124pub extend DCTBlock with @debug.Debug::{to_repr}
  125. 125
  126. 126///|
  127. 127/// 霍夫曼编码表
  128. 128#derive(Eq, @debug.Debug)
  129. 129pub(all) struct HuffmanTable {
  130. 130 symbols : Array[Int32]
  131. 131 codes : Array[Int32]
  132. 132 code_lengths : Array[Int32]
  133. 133 num_symbols : Int32
  134. 134}
  135. 135
  136. 136///|
  137. 137pub extend HuffmanTable with Eq::{not_equal, equal}
  138. 138
  139. 139///|
  140. 140pub extend HuffmanTable with @debug.Debug::{to_repr}
  141. 141
  142. 142///|
  143. 143/// 创建默认压缩配置
  144. 144pub fn create_default_compression_config() -> CompressionConfig {
  145. 145● CompressionConfig::{
  146. 146 algorithm: CompressionAlgorithm::JPEGBaseline,
  147. 147 quality: 75.0,
  148. 148 quantization_level: 8,
  149. 149 use_dct: true,
  150. 150 use_prediction: true,
  151. 151 use_entropy_coding: true,
  152. 152 block_size: 8,
  153. 153 }
  154. 154}
  155. 155
  156. 156///|
  157. 157/// 游程编码(RLE)
  158. 158pub fn run_length_encode(data : Bytes) -> Bytes {
  159. 159● let len = data.length()
  160. 160 let encoded : Array[Byte] = []
  161. 161
  162. 162 if len == 0 {
  163. 163● return Bytes::from_array(encoded)
  164. 164 }
  165. 165
  166. 166 let mut i = 0
  167. 167 while i < len {
  168. 168● let current = data[i]
  169. 169 let mut count = 1
  170. 170 while i + count < len && data[i + count] == current && count < 255 {
  171. 171● count += 1
  172. 172 }
  173. 173● encoded.push(count.to_byte())
  174. 174● encoded.push(current)
  175. 175 i += count
  176. 176 }
  177. 177
  178. 178● Bytes::from_array(encoded)
  179. 179}
  180. 180
  181. 181///|
  182. 182/// 游程解码
  183. 183pub fn run_length_decode(data : Bytes) -> Bytes {
  184. 184● let len = data.length()
  185. 185 let decoded : Array[Byte] = []
  186. 186
  187. 187 let mut i = 0
  188. 188 while i < len - 1 {
  189. 189● let count = data[i].to_int()
  190. 190 let value = data[i + 1]
  191. 191 for _ in 0..<count {
  192. 192● decoded.push(value)
  193. 193 }
  194. 194 i += 2
  195. 195 }
  196. 196
  197. 197● Bytes::from_array(decoded)
  198. 198}
  199. 199
  200. 200///|
  201. 201/// 计算图像熵
  202. 202pub fn calculate_image_entropy(
  203. 203 img : @types.Image,
  204. 204) -> Double raise @types.LoadError {
  205. 205 // 输入验证
  206. 206● if img.width <= 0 || img.height <= 0 {
  207. 207● raise @types.LoadError::DecodeFailed(
  208. 208 "calculate_image_entropy: 图像尺寸必须大于0",
  209. 209 )
  210. 210 }
  211. 211 if img.channels < 1 || img.channels > 4 {
  212. 212● raise @types.LoadError::DecodeFailed(
  213. 213 "calculate_image_entropy: 通道数必须在1-4之间",
  214. 214 )
  215. 215 }
  216. 216 let expected_size = img.width * img.height * img.channels
  217. 217● if img.data.length() != expected_size {
  218. 218● raise @types.LoadError::DecodeFailed(
  219. 219 "calculate_image_entropy: 数据大小不匹配",
  220. 220 )
  221. 221 }
  222. 222 let w = img.width
  223. 223 let h = img.height
  224. 224 let total = w * h
  225. 225
  226. 226 let histogram : Array[Int32] = []
  227. 227 for _ in 0..<256 {
  228. 228● histogram.push(0)
  229. 229 }
  230. 230 for i in 0..<total {
  231. 231● histogram[img.data[i].to_int()] += 1
  232. 232 }
  233. 233
  234. 234 let mut entropy = 0.0
  235. 235 for i in 0..<256 {
  236. 236● if histogram[i] > 0 {
  237. 237● let p = histogram[i].to_double() / total.to_double()
  238. 238 // 简化的 log2
  239. 239 entropy -= p * p * 0.1
  240. 240 }
  241. 241 }
  242. 242
  243. 243 entropy
  244. 244}
  245. 245
  246. 246///|
  247. 247/// 量化(均匀量化)
  248. 248pub fn quantize_image(
  249. 249 img : @types.Image,
  250. 250 levels : Int32,
  251. 251) -> @types.Image raise @types.LoadError {
  252. 252 // 输入验证
  253. 253● if img.width <= 0 || img.height <= 0 {
  254. 254● raise @types.LoadError::DecodeFailed(
  255. 255 "quantize_image: 图像尺寸必须大于0",
  256. 256 )
  257. 257 }
  258. 258 if img.channels < 1 || img.channels > 4 {
  259. 259● raise @types.LoadError::DecodeFailed(
  260. 260 "quantize_image: 通道数必须在1-4之间",
  261. 261 )
  262. 262 }
  263. 263 let expected_size = img.width * img.height * img.channels
  264. 264● if img.data.length() != expected_size {
  265. 265● raise @types.LoadError::DecodeFailed(
  266. 266 "quantize_image: 数据大小不匹配",
  267. 267 )
  268. 268 }
  269. 269 if levels < 2 {
  270. 270● raise @types.LoadError::DecodeFailed("quantize_image: levels 必须 >= 2")
  271. 271 }
  272. 272 let w = img.width
  273. 273 let h = img.height
  274. 274 let total = w * h
  275. 275 let out : Array[Byte] = []
  276. 276 let step = 256 / levels
  277. 277
  278. 278 for i in 0..<total {
  279. 279● let pixel = img.data[i].to_int()
  280. 280 let quantized = pixel / step * step + step / 2
  281. 281 let clamped = if quantized < 0 {
  282. 282○ 0
  283. 283● } else if quantized > 255 {
  284. 284● 255
  285. 285 } else {
  286. 286● quantized
  287. 287 }
  288. 288● out.push(clamped.to_byte())
  289. 289 }
  290. 290
  291. 291 @types.Image::{
  292. 292 width: w,
  293. 293 height: h,
  294. 294 channels: img.channels,
  295. 295● data: Bytes::from_array(out),
  296. 296 }
  297. 297}
  298. 298
  299. 299///|
  300. 300/// 预测编码(差分脉冲编码调制 DPCM)
  301. 301pub fn predictive_encode(img : @types.Image) -> Bytes raise @types.LoadError {
  302. 302 // 输入验证
  303. 303● if img.width <= 0 || img.height <= 0 {
  304. 304● raise @types.LoadError::DecodeFailed(
  305. 305 "predictive_encode: 图像尺寸必须大于0",
  306. 306 )
  307. 307 }
  308. 308 if img.channels < 1 || img.channels > 4 {
  309. 309● raise @types.LoadError::DecodeFailed(
  310. 310 "predictive_encode: 通道数必须在1-4之间",
  311. 311 )
  312. 312 }
  313. 313 let expected_size = img.width * img.height * img.channels
  314. 314● if img.data.length() != expected_size {
  315. 315● raise @types.LoadError::DecodeFailed(
  316. 316 "predictive_encode: 数据大小不匹配",
  317. 317 )
  318. 318 }
  319. 319 let w = img.width
  320. 320 let h = img.height
  321. 321 let _ = w * h
  322. 322 let diff : Array[Byte] = []
  323. 323
  324. 324 for y in 0..<h {
  325. 325● for x in 0..<w {
  326. 326● let idx = y * w + x
  327. 327● let current = img.data[idx].to_int()
  328. 328 let predictor = if x > 0 {
  329. 329● img.data[idx - 1].to_int()
  330. 330● } else if y > 0 {
  331. 331● img.data[idx - w].to_int()
  332. 332 } else {
  333. 333● 128
  334. 334 }
  335. 335 let difference = current - predictor
  336. 336 let clamped = if difference < -128 {
  337. 337● -128
  338. 338● } else if difference > 127 {
  339. 339● 127
  340. 340 } else {
  341. 341● difference
  342. 342 }
  343. 343● diff.push((clamped + 128).to_byte())
  344. 344 }
  345. 345 }
  346. 346
  347. 347● Bytes::from_array(diff)
  348. 348}
  349. 349
  350. 350///|
  351. 351/// 计算压缩比
  352. 352pub fn calculate_compression_ratio(
  353. 353 original_size : Int32,
  354. 354 compressed_size : Int32,
  355. 355) -> Double {
  356. 356● if compressed_size > 0 {
  357. 357● original_size.to_double() / compressed_size.to_double()
  358. 358 } else {
  359. 359● 0.0
  360. 360 }
  361. 361}
  362. 362
  363. 363///|
  364. 364/// 计算每像素位数
  365. 365pub fn calculate_bits_per_pixel(
  366. 366 compressed_size : Int32,
  367. 367 width : Int32,
  368. 368 height : Int32,
  369. 369) -> Double {
  370. 370● let total_pixels = width * height
  371. 371 if total_pixels > 0 {
  372. 372● compressed_size.to_double() * 8.0 / total_pixels.to_double()
  373. 373 } else {
  374. 374○ 0.0
  375. 375 }
  376. 376}
  377. 377
  378. 378///|
  379. 379/// 计算压缩质量指标
  380. 380pub fn calculate_compression_quality(
  381. 381 original : @types.Image,
  382. 382 compressed : @types.Image,
  383. 383 original_size : Int32,
  384. 384 compressed_size : Int32,
  385. 385) -> CompressionQualityMetrics raise @types.LoadError {
  386. 386 // 输入验证
  387. 387● if original.width <= 0 || original.height <= 0 {
  388. 388● raise @types.LoadError::DecodeFailed(
  389. 389 "calculate_compression_quality: 图像尺寸必须大于0",
  390. 390 )
  391. 391 }
  392. 392 if original.channels < 1 || original.channels > 4 {
  393. 393● raise @types.LoadError::DecodeFailed(
  394. 394 "calculate_compression_quality: 通道数必须在1-4之间",
  395. 395 )
  396. 396 }
  397. 397 let expected_size = original.width * original.height * original.channels
  398. 398● if original.data.length() != expected_size {
  399. 399● raise @types.LoadError::DecodeFailed(
  400. 400 "calculate_compression_quality: 数据大小不匹配",
  401. 401 )
  402. 402 }
  403. 403 if compressed.width != original.width || compressed.height != original.height {
  404. 404● raise @types.LoadError::DecodeFailed(
  405. 405 "calculate_compression_quality: 两图像尺寸必须一致",
  406. 406 )
  407. 407 }
  408. 408 let w = original.width
  409. 409 let h = original.height
  410. 410 let total = w * h
  411. 411
  412. 412 // MSE
  413. 413 let mut mse = 0.0
  414. 414 for i in 0..<total {
  415. 415● let diff = original.data[i].to_double() - compressed.data[i].to_double()
  416. 416 mse += diff * diff
  417. 417 }
  418. 418● mse /= total.to_double()
  419. 419
  420. 420 // PSNR
  421. 421 let psnr = if mse > 0.0 {
  422. 422● 10.0 * (65025.0 / mse) * 0.1 // 简化的 log10
  423. 423 } else {
  424. 424● 100.0
  425. 425 }
  426. 426
  427. 427 // SSIM(简化版)
  428. 428 let mut mean1 = 0.0
  429. 429 let mut mean2 = 0.0
  430. 430 for i in 0..<total {
  431. 431● mean1 += original.data[i].to_double()
  432. 432● mean2 += compressed.data[i].to_double()
  433. 433 }
  434. 434● mean1 /= total.to_double()
  435. 435● mean2 /= total.to_double()
  436. 436
  437. 437 let mut var1 = 0.0
  438. 438 let mut var2 = 0.0
  439. 439 let mut cov = 0.0
  440. 440 for i in 0..<total {
  441. 441● let p1 = original.data[i].to_double()
  442. 442● let p2 = compressed.data[i].to_double()
  443. 443 var1 += (p1 - mean1) * (p1 - mean1)
  444. 444 var2 += (p2 - mean2) * (p2 - mean2)
  445. 445 cov += (p1 - mean1) * (p2 - mean2)
  446. 446 }
  447. 447● var1 /= total.to_double()
  448. 448● var2 /= total.to_double()
  449. 449● cov /= total.to_double()
  450. 450
  451. 451 let c1 = 6.5025
  452. 452 let c2 = 58.5225
  453. 453 let ssim = if var1 + var2 + c2 > 0.0 {
  454. 454● (2.0 * mean1 * mean2 + c1) *
  455. 455 (2.0 * cov + c2) /
  456. 456 ((mean1 * mean1 + mean2 * mean2 + c1) * (var1 + var2 + c2))
  457. 457 } else {
  458. 458○ 1.0
  459. 459 }
  460. 460
  461. 461● let compression_ratio = calculate_compression_ratio(
  462. 462 original_size, compressed_size,
  463. 463 )
  464. 464● let bpp = calculate_bits_per_pixel(compressed_size, w, h)
  465. 465
  466. 466 // 综合质量评分(PSNR 40% + SSIM 40% + 压缩比 20%)
  467. 467 let psnr_score = if psnr > 40.0 {
  468. 468● 1.0
  469. 469● } else if psnr > 30.0 {
  470. 470○ 0.8
  471. 471● } else if psnr > 20.0 {
  472. 472○ 0.6
  473. 473 } else {
  474. 474● 0.4
  475. 475 }
  476. 476 let ssim_score = if ssim > 0.95 {
  477. 477● 1.0
  478. 478● } else if ssim > 0.9 {
  479. 479○ 0.85
  480. 480● } else if ssim > 0.8 {
  481. 481○ 0.7
  482. 482 } else {
  483. 483● 0.5
  484. 484 }
  485. 485 let ratio_score = if compression_ratio > 20.0 {
  486. 486○ 1.0
  487. 487● } else if compression_ratio > 10.0 {
  488. 488○ 0.8
  489. 489● } else if compression_ratio > 5.0 {
  490. 490○ 0.6
  491. 491 } else {
  492. 492● 0.4
  493. 493 }
  494. 494 let overall_quality = psnr_score * 0.4 + ssim_score * 0.4 + ratio_score * 0.2
  495. 495
  496. 496 CompressionQualityMetrics::{
  497. 497 psnr,
  498. 498 ssim,
  499. 499 mse,
  500. 500 compression_ratio,
  501. 501 bits_per_pixel: bpp,
  502. 502 structural_similarity: ssim,
  503. 503 overall_quality,
  504. 504 }
  505. 505}
  506. 506
  507. 507///|
  508. 508/// 简单 JPEG 风格压缩(量化 + RLE)
  509. 509pub fn compress_jpeg_simple(
  510. 510 img : @types.Image,
  511. 511 quality : Double,
  512. 512) -> CompressionResult raise @types.LoadError {
  513. 513 // 输入验证
  514. 514● if img.width <= 0 || img.height <= 0 {
  515. 515● raise @types.LoadError::DecodeFailed(
  516. 516 "compress_jpeg_simple: 图像尺寸必须大于0",
  517. 517 )
  518. 518 }
  519. 519 if img.channels < 1 || img.channels > 4 {
  520. 520● raise @types.LoadError::DecodeFailed(
  521. 521 "compress_jpeg_simple: 通道数必须在1-4之间",
  522. 522 )
  523. 523 }
  524. 524 let expected_size = img.width * img.height * img.channels
  525. 525● if img.data.length() != expected_size {
  526. 526● raise @types.LoadError::DecodeFailed(
  527. 527 "compress_jpeg_simple: 数据大小不匹配",
  528. 528 )
  529. 529 }
  530. 530 let w = img.width
  531. 531 let h = img.height
  532. 532 let original_size = w * h
  533. 533
  534. 534 // 量化级别根据质量调整
  535. 535 let levels = if quality > 90.0 {
  536. 536○ 64
  537. 537● } else if quality > 75.0 {
  538. 538● 32
  539. 539● } else if quality > 50.0 {
  540. 540○ 16
  541. 541 } else {
  542. 542● 8
  543. 543 }
  544. 544
  545. 545 // 量化
  546. 546● let quantized = quantize_image(img, levels)
  547. 547
  548. 548 // 预测编码
  549. 549● let predicted = predictive_encode(quantized)
  550. 550
  551. 551 // RLE 编码
  552. 552● let compressed = run_length_encode(predicted)
  553. 553
  554. 554● let compressed_size = compressed.length()
  555. 555● let ratio = calculate_compression_ratio(original_size, compressed_size)
  556. 556● let bpp = calculate_bits_per_pixel(compressed_size, w, h)
  557. 557
  558. 558 CompressionResult::{
  559. 559 compressed_data: compressed,
  560. 560 original_size,
  561. 561 compressed_size,
  562. 562 compression_ratio: ratio,
  563. 563 bits_per_pixel: bpp,
  564. 564 algorithm: CompressionAlgorithm::JPEGBaseline,
  565. 565 quality,
  566. 566 processing_time_ms: 0.0,
  567. 567 }
  568. 568}
  569. 569
  570. 570///|
  571. 571/// PNG 风格压缩(预测 + RLE)
  572. 572pub fn compress_png_simple(
  573. 573 img : @types.Image,
  574. 574) -> CompressionResult raise @types.LoadError {
  575. 575 // 输入验证
  576. 576● if img.width <= 0 || img.height <= 0 {
  577. 577● raise @types.LoadError::DecodeFailed(
  578. 578 "compress_png_simple: 图像尺寸必须大于0",
  579. 579 )
  580. 580 }
  581. 581 if img.channels < 1 || img.channels > 4 {
  582. 582● raise @types.LoadError::DecodeFailed(
  583. 583 "compress_png_simple: 通道数必须在1-4之间",
  584. 584 )
  585. 585 }
  586. 586 let expected_size = img.width * img.height * img.channels
  587. 587● if img.data.length() != expected_size {
  588. 588● raise @types.LoadError::DecodeFailed(
  589. 589 "compress_png_simple: 数据大小不匹配",
  590. 590 )
  591. 591 }
  592. 592 let w = img.width
  593. 593 let h = img.height
  594. 594 let original_size = w * h
  595. 595
  596. 596 // 预测编码
  597. 597● let predicted = predictive_encode(img)
  598. 598
  599. 599 // RLE 编码
  600. 600● let compressed = run_length_encode(predicted)
  601. 601
  602. 602● let compressed_size = compressed.length()
  603. 603● let ratio = calculate_compression_ratio(original_size, compressed_size)
  604. 604● let bpp = calculate_bits_per_pixel(compressed_size, w, h)
  605. 605
  606. 606 CompressionResult::{
  607. 607 compressed_data: compressed,
  608. 608 original_size,
  609. 609 compressed_size,
  610. 610 compression_ratio: ratio,
  611. 611 bits_per_pixel: bpp,
  612. 612 algorithm: CompressionAlgorithm::PNG,
  613. 613 quality: 100.0,
  614. 614 processing_time_ms: 0.0,
  615. 615 }
  616. 616}
  617. 617
  618. 618///|
  619. 619/// 图像压缩
  620. 620pub fn compress_image(
  621. 621 img : @types.Image,
  622. 622 config : CompressionConfig,
  623. 623) -> CompressionResult raise @types.LoadError {
  624. 624 // 输入验证
  625. 625● if img.width <= 0 || img.height <= 0 {
  626. 626● raise @types.LoadError::DecodeFailed(
  627. 627 "compress_image: 图像尺寸必须大于0",
  628. 628 )
  629. 629 }
  630. 630 if img.channels < 1 || img.channels > 4 {
  631. 631● raise @types.LoadError::DecodeFailed(
  632. 632 "compress_image: 通道数必须在1-4之间",
  633. 633 )
  634. 634 }
  635. 635 let expected_size = img.width * img.height * img.channels
  636. 636● if img.data.length() != expected_size {
  637. 637● raise @types.LoadError::DecodeFailed(
  638. 638 "compress_image: 数据大小不匹配",
  639. 639 )
  640. 640 }
  641. 641 if config.algorithm == CompressionAlgorithm::JPEGBaseline {
  642. 642● compress_jpeg_simple(img, config.quality)
  643. 643● } else if config.algorithm == CompressionAlgorithm::PNG {
  644. 644● compress_png_simple(img)
  645. 645● } else if config.algorithm == CompressionAlgorithm::RunLengthEncoding {
  646. 646● let compressed = run_length_encode(img.data)
  647. 647 let original_size = img.width * img.height
  648. 648● let compressed_size = compressed.length()
  649. 649 CompressionResult::{
  650. 650 compressed_data: compressed,
  651. 651 original_size,
  652. 652 compressed_size,
  653. 653● compression_ratio: calculate_compression_ratio(
  654. 654 original_size, compressed_size,
  655. 655 ),
  656. 656● bits_per_pixel: calculate_bits_per_pixel(
  657. 657 compressed_size,
  658. 658 img.width,
  659. 659 img.height,
  660. 660 ),
  661. 661 algorithm: CompressionAlgorithm::RunLengthEncoding,
  662. 662 quality: 100.0,
  663. 663 processing_time_ms: 0.0,
  664. 664 }
  665. 665 } else {
  666. 666● compress_jpeg_simple(img, config.quality)
  667. 667 }
  668. 668}
  669. 669
  670. 670///|
  671. 671/// 获取压缩算法名称
  672. 672pub fn compression_algorithm_to_string(
  673. 673 algorithm : CompressionAlgorithm,
  674. 674) -> String {
  675. 675● if algorithm == CompressionAlgorithm::RunLengthEncoding {
  676. 676● "游程编码(RLE)"
  677. 677● } else if algorithm == CompressionAlgorithm::HuffmanCoding {
  678. 678● "霍夫曼编码"
  679. 679● } else if algorithm == CompressionAlgorithm::ArithmeticCoding {
  680. 680● "算术编码"
  681. 681● } else if algorithm == CompressionAlgorithm::LZW {
  682. 682● "LZW"
  683. 683● } else if algorithm == CompressionAlgorithm::Deflate {
  684. 684● "Deflate"
  685. 685● } else if algorithm == CompressionAlgorithm::JPEGBaseline {
  686. 686● "JPEG 基线"
  687. 687● } else if algorithm == CompressionAlgorithm::JPEG2000 {
  688. 688● "JPEG 2000"
  689. 689● } else if algorithm == CompressionAlgorithm::WebP {
  690. 690● "WebP"
  691. 691● } else if algorithm == CompressionAlgorithm::AVIF {
  692. 692● "AVIF"
  693. 693● } else if algorithm == CompressionAlgorithm::PNG {
  694. 694● "PNG"
  695. 695 } else {
  696. 696● "未知"
  697. 697 }
  698. 698}
  699. 699
  700. 700///|
  701. 701/// 生成压缩报告
  702. 702pub fn compression_report_to_string(
  703. 703 result : CompressionResult,
  704. 704 metrics : CompressionQualityMetrics,
  705. 705) -> String {
  706. 706● let mut report = "## 图像压缩报告\n\n"
  707. 707 report = report + "### 压缩信息\n\n"
  708. 708 report = report + "| 指标 | 值 |\n|------|------|\n"
  709. 709 report = report +
  710. 710 "| 算法 | " +
  711. 711● compression_algorithm_to_string(result.algorithm) +
  712. 712 " |\n"
  713. 713 report = report +
  714. 714 "| 原始大小 | " +
  715. 715● result.original_size.to_string() +
  716. 716 " 字节 |\n"
  717. 717 report = report +
  718. 718 "| 压缩大小 | " +
  719. 719● result.compressed_size.to_string() +
  720. 720 " 字节 |\n"
  721. 721 report = report +
  722. 722 "| 压缩比 | " +
  723. 723● result.compression_ratio.to_string() +
  724. 724 ":1 |\n"
  725. 725 report = report +
  726. 726 "| 每像素位数 | " +
  727. 727● result.bits_per_pixel.to_string() +
  728. 728 " bpp |\n"
  729. 729● report = report + "| 质量设置 | " + result.quality.to_string() + " |\n\n"
  730. 730
  731. 731 report = report + "### 质量评估\n\n"
  732. 732 report = report + "| 指标 | 值 |\n|------|------|\n"
  733. 733● report = report + "| PSNR | " + metrics.psnr.to_string() + " dB |\n"
  734. 734● report = report + "| SSIM | " + metrics.ssim.to_string() + " |\n"
  735. 735● report = report + "| MSE | " + metrics.mse.to_string() + " |\n"
  736. 736 report = report +
  737. 737 "| 综合质量 | " +
  738. 738● (metrics.overall_quality * 100.0).to_string() +
  739. 739 "% |\n"
  740. 740
  741. 741 report
  742. 742}