1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// 图像压缩与编码优化(v28.0.0)
// 图像压缩、量化、熵编码、压缩质量评估
///|
/// 压缩算法类型
pub(all) enum CompressionAlgorithm {
RunLengthEncoding
HuffmanCoding
ArithmeticCoding
LZW
Deflate
JPEGBaseline
JPEG2000
WebP
AVIF
PNG
Unknown
} derive(Eq)
///|
/// 压缩结果
pub(all) struct CompressionResult {
compressed_data : Bytes
original_size : Int
compressed_size : Int
compression_ratio : Double
bits_per_pixel : Double
algorithm : CompressionAlgorithm
quality : Double
processing_time_ms : Double
}
///|
/// 压缩配置
pub(all) struct CompressionConfig {
algorithm : CompressionAlgorithm
quality : Double
quantization_level : Int
use_dct : Bool
use_prediction : Bool
use_entropy_coding : Bool
block_size : Int
}
///|
/// 压缩质量评估
pub(all) struct CompressionQualityMetrics {
psnr : Double
ssim : Double
mse : Double
compression_ratio : Double
bits_per_pixel : Double
structural_similarity : Double
overall_quality : Double
}
///|
/// 量化表类型
pub(all) enum QuantizationTableType {
Luminance
Chrominance
Custom
Default
} derive(Eq)
///|
/// DCT 系数块
pub(all) struct DCTBlock {
coefficients : Array[Double]
block_size : Int
width : Int
height : Int
}
///|
/// 霍夫曼编码表
pub(all) struct HuffmanTable {
symbols : Array[Int]
codes : Array[Int]
code_lengths : Array[Int]
num_symbols : Int
}
///|
/// 创建默认压缩配置
pub fn create_default_compression_config() -> CompressionConfig {
CompressionConfig::{
algorithm: CompressionAlgorithm::JPEGBaseline,
quality: 75.0,
quantization_level: 8,
use_dct: true,
use_prediction: true,
use_entropy_coding: true,
block_size: 8,
}
}
///|
/// 游程编码(RLE)
pub fn run_length_encode(data : Bytes) -> Bytes {
let len = data.length()
let encoded : Array[Byte] = []
if len == 0 {
return Bytes::from_array(encoded)
}
let mut i = 0
while i < len {
let current = data[i]
let mut count = 1
while i + count < len && data[i + count] == current && count < 255 {
count += 1
}
encoded.push(count.to_byte())
encoded.push(current)
i += count
}
Bytes::from_array(encoded)
}
///|
/// 游程解码
pub fn run_length_decode(data : Bytes) -> Bytes {
let len = data.length()
let decoded : Array[Byte] = []
let mut i = 0
while i < len - 1 {
let count = data[i].to_int()
let value = data[i + 1]
for _ in 0..<count {
decoded.push(value)
}
i += 2
}
Bytes::from_array(decoded)
}
///|
/// 计算图像熵
pub fn calculate_image_entropy(img : @types.Image) -> Double {
let w = img.width
let h = img.height
let total = w * h
let histogram : Array[Int] = []
for _ in 0..<256 {
histogram.push(0)
}
for i in 0..<total {
histogram[img.data[i].to_int()] += 1
}
let mut entropy = 0.0
for i in 0..<256 {
if histogram[i] > 0 {
let p = histogram[i].to_double() / total.to_double()
// 简化的 log2
entropy -= p * p * 0.1
}
}
entropy
}
///|
/// 量化(均匀量化)
pub fn quantize_image(img : @types.Image, levels : Int) -> @types.Image {
let w = img.width
let h = img.height
let total = w * h
let out : Array[Byte] = []
let step = 256 / levels
for i in 0..<total {
let pixel = img.data[i].to_int()
let quantized = pixel / step * step + step / 2
let clamped = if quantized < 0 {
0
} else if quantized > 255 {
255
} else {
quantized
}
out.push(clamped.to_byte())
}
@types.Image::{
width: w,
height: h,
channels: img.channels,
data: Bytes::from_array(out),
}
}
///|
/// 预测编码(差分脉冲编码调制 DPCM)
pub fn predictive_encode(img : @types.Image) -> Bytes {
let w = img.width
let h = img.height
let _ = w * h
let diff : Array[Byte] = []
for y in 0..<h {
for x in 0..<w {
let idx = y * w + x
let current = img.data[idx].to_int()
let predictor = if x > 0 {
img.data[idx - 1].to_int()
} else if y > 0 {
img.data[idx - w].to_int()
} else {
128
}
let difference = current - predictor
let clamped = if difference < -128 {
-128
} else if difference > 127 {
127
} else {
difference
}
diff.push((clamped + 128).to_byte())
}
}
Bytes::from_array(diff)
}
///|
/// 计算压缩比
pub fn calculate_compression_ratio(
original_size : Int,
compressed_size : Int,
) -> Double {
if compressed_size > 0 {
original_size.to_double() / compressed_size.to_double()
} else {
0.0
}
}
///|
/// 计算每像素位数
pub fn calculate_bits_per_pixel(
compressed_size : Int,
width : Int,
height : Int,
) -> Double {
let total_pixels = width * height
if total_pixels > 0 {
compressed_size.to_double() * 8.0 / total_pixels.to_double()
} else {
0.0
}
}
///|
/// 计算压缩质量指标
pub fn calculate_compression_quality(
original : @types.Image,
compressed : @types.Image,
original_size : Int,
compressed_size : Int,
) -> CompressionQualityMetrics {
let w = original.width
let h = original.height
let total = w * h
// MSE
let mut mse = 0.0
for i in 0..<total {
let diff = original.data[i].to_double() - compressed.data[i].to_double()
mse += diff * diff
}
mse /= total.to_double()
// PSNR
let psnr = if mse > 0.0 {
10.0 * (65025.0 / mse) * 0.1 // 简化的 log10
} else {
100.0
}
// SSIM(简化版)
let mut mean1 = 0.0
let mut mean2 = 0.0
for i in 0..<total {
mean1 += original.data[i].to_double()
mean2 += compressed.data[i].to_double()
}
mean1 /= total.to_double()
mean2 /= total.to_double()
let mut var1 = 0.0
let mut var2 = 0.0
let mut cov = 0.0
for i in 0..<total {
let p1 = original.data[i].to_double()
let p2 = compressed.data[i].to_double()
var1 += (p1 - mean1) * (p1 - mean1)
var2 += (p2 - mean2) * (p2 - mean2)
cov += (p1 - mean1) * (p2 - mean2)
}
var1 /= total.to_double()
var2 /= total.to_double()
cov /= total.to_double()
let c1 = 6.5025
let c2 = 58.5225
let ssim = if var1 + var2 + c2 > 0.0 {
(2.0 * mean1 * mean2 + c1) *
(2.0 * cov + c2) /
((mean1 * mean1 + mean2 * mean2 + c1) * (var1 + var2 + c2))
} else {
1.0
}
let compression_ratio = calculate_compression_ratio(
original_size, compressed_size,
)
let bpp = calculate_bits_per_pixel(compressed_size, w, h)
// 综合质量评分(PSNR 40% + SSIM 40% + 压缩比 20%)
let psnr_score = if psnr > 40.0 {
1.0
} else if psnr > 30.0 {
0.8
} else if psnr > 20.0 {
0.6
} else {
0.4
}
let ssim_score = if ssim > 0.95 {
1.0
} else if ssim > 0.9 {
0.85
} else if ssim > 0.8 {
0.7
} else {
0.5
}
let ratio_score = if compression_ratio > 20.0 {
1.0
} else if compression_ratio > 10.0 {
0.8
} else if compression_ratio > 5.0 {
0.6
} else {
0.4
}
let overall_quality = psnr_score * 0.4 + ssim_score * 0.4 + ratio_score * 0.2
CompressionQualityMetrics::{
psnr,
ssim,
mse,
compression_ratio,
bits_per_pixel: bpp,
structural_similarity: ssim,
overall_quality,
}
}
///|
/// 简单 JPEG 风格压缩(量化 + RLE)
pub fn compress_jpeg_simple(
img : @types.Image,
quality : Double,
) -> CompressionResult {
let w = img.width
let h = img.height
let original_size = w * h
// 量化级别根据质量调整
let levels = if quality > 90.0 {
64
} else if quality > 75.0 {
32
} else if quality > 50.0 {
16
} else {
8
}
// 量化
let quantized = quantize_image(img, levels)
// 预测编码
let predicted = predictive_encode(quantized)
// RLE 编码
let compressed = run_length_encode(predicted)
let compressed_size = compressed.length()
let ratio = calculate_compression_ratio(original_size, compressed_size)
let bpp = calculate_bits_per_pixel(compressed_size, w, h)
CompressionResult::{
compressed_data: compressed,
original_size,
compressed_size,
compression_ratio: ratio,
bits_per_pixel: bpp,
algorithm: CompressionAlgorithm::JPEGBaseline,
quality,
processing_time_ms: 0.0,
}
}
///|
/// PNG 风格压缩(预测 + RLE)
pub fn compress_png_simple(img : @types.Image) -> CompressionResult {
let w = img.width
let h = img.height
let original_size = w * h
// 预测编码
let predicted = predictive_encode(img)
// RLE 编码
let compressed = run_length_encode(predicted)
let compressed_size = compressed.length()
let ratio = calculate_compression_ratio(original_size, compressed_size)
let bpp = calculate_bits_per_pixel(compressed_size, w, h)
CompressionResult::{
compressed_data: compressed,
original_size,
compressed_size,
compression_ratio: ratio,
bits_per_pixel: bpp,
algorithm: CompressionAlgorithm::PNG,
quality: 100.0,
processing_time_ms: 0.0,
}
}
///|
/// 图像压缩
pub fn compress_image(
img : @types.Image,
config : CompressionConfig,
) -> CompressionResult {
if config.algorithm == CompressionAlgorithm::JPEGBaseline {
compress_jpeg_simple(img, config.quality)
} else if config.algorithm == CompressionAlgorithm::PNG {
compress_png_simple(img)
} else if config.algorithm == CompressionAlgorithm::RunLengthEncoding {
let compressed = run_length_encode(img.data)
let original_size = img.width * img.height
let compressed_size = compressed.length()
CompressionResult::{
compressed_data: compressed,
original_size,
compressed_size,
compression_ratio: calculate_compression_ratio(
original_size, compressed_size,
),
bits_per_pixel: calculate_bits_per_pixel(
compressed_size,
img.width,
img.height,
),
algorithm: CompressionAlgorithm::RunLengthEncoding,
quality: 100.0,
processing_time_ms: 0.0,
}
} else {
compress_jpeg_simple(img, config.quality)
}
}
///|
/// 获取压缩算法名称
pub fn compression_algorithm_to_string(
algorithm : CompressionAlgorithm,
) -> String {
if algorithm == CompressionAlgorithm::RunLengthEncoding {
"游程编码(RLE)"
} else if algorithm == CompressionAlgorithm::HuffmanCoding {
"霍夫曼编码"
} else if algorithm == CompressionAlgorithm::ArithmeticCoding {
"算术编码"
} else if algorithm == CompressionAlgorithm::LZW {
"LZW"
} else if algorithm == CompressionAlgorithm::Deflate {
"Deflate"
} else if algorithm == CompressionAlgorithm::JPEGBaseline {
"JPEG 基线"
} else if algorithm == CompressionAlgorithm::JPEG2000 {
"JPEG 2000"
} else if algorithm == CompressionAlgorithm::WebP {
"WebP"
} else if algorithm == CompressionAlgorithm::AVIF {
"AVIF"
} else if algorithm == CompressionAlgorithm::PNG {
"PNG"
} else {
"未知"
}
}
///|
/// 生成压缩报告
pub fn compression_report_to_string(
result : CompressionResult,
metrics : CompressionQualityMetrics,
) -> String {
let mut report = "## 图像压缩报告\n\n"
report = report + "### 压缩信息\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
report = report +
"| 算法 | " +
compression_algorithm_to_string(result.algorithm) +
" |\n"
report = report +
"| 原始大小 | " +
result.original_size.to_string() +
" 字节 |\n"
report = report +
"| 压缩大小 | " +
result.compressed_size.to_string() +
" 字节 |\n"
report = report +
"| 压缩比 | " +
result.compression_ratio.to_string() +
":1 |\n"
report = report +
"| 每像素位数 | " +
result.bits_per_pixel.to_string() +
" bpp |\n"
report = report + "| 质量设置 | " + result.quality.to_string() + " |\n\n"
report = report + "### 质量评估\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
report = report + "| PSNR | " + metrics.psnr.to_string() + " dB |\n"
report = report + "| SSIM | " + metrics.ssim.to_string() + " |\n"
report = report + "| MSE | " + metrics.mse.to_string() + " |\n"
report = report +
"| 综合质量 | " +
(metrics.overall_quality * 100.0).to_string() +
"% |\n"
report
}