-
Notifications
You must be signed in to change notification settings - Fork 34
/
mtf-delta_encoding.pl
138 lines (101 loc) · 2.89 KB
/
mtf-delta_encoding.pl
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
#!/usr/bin/perl
# Author: Trizen
# Date: 13 June 2023
# https://github.com/trizen
# Implementation of the Move-to-Front transform, combined with Delta encoding.
# References:
# Data Compression (Summer 2023) - Lecture 6 - Delta Compression and Prediction
# https://youtube.com/watch?v=-3H_eDbWNEU
#
# COMP526 Unit 7-6 2020-03-24 Compression - Move-to-front transform
# https://youtube.com/watch?v=Q2pinaj3i9Y
use 5.036;
sub mtf_encode ($bytes, $alphabet = [0 .. 255]) {
my @C;
my @table;
@table[@$alphabet] = (0 .. $#{$alphabet});
foreach my $c (@$bytes) {
push @C, (my $index = $table[$c]);
unshift(@$alphabet, splice(@{$alphabet}, $index, 1));
@table[@{$alphabet}[0 .. $index]] = (0 .. $index);
}
return \@C;
}
sub mtf_decode ($encoded, $alphabet = [0 .. 255]) {
my @S;
foreach my $p (@$encoded) {
push @S, $alphabet->[$p];
unshift(@$alphabet, splice(@{$alphabet}, $p, 1));
}
return \@S;
}
sub read_bit ($fh, $bitstring) {
if (($$bitstring // '') eq '') {
$$bitstring = unpack('b*', getc($fh) // return undef);
}
chop($$bitstring);
}
sub delta_encode ($bytes) { # all bytes in the range [0, 255]
my @deltas;
my $prev = 0;
my $integers = mtf_encode($bytes);
unshift(@$integers, scalar(@$integers));
while (@$integers) {
my $curr = shift(@$integers);
push @deltas, $curr - $prev;
$prev = $curr;
}
my $bitstring = '';
foreach my $d (@deltas) {
if ($d == 0) {
$bitstring .= '0';
}
else {
my $t = sprintf('%b', abs($d));
$bitstring .= '1' . (($d < 0) ? '0' : '1') . ('1' x (length($t) - 1)) . '0' . substr($t, 1);
}
}
pack('B*', $bitstring);
}
sub delta_decode ($str) {
open my $fh, '<:raw', \$str;
my @deltas;
my $buffer = '';
my $len = 0;
for (my $k = 0 ; $k <= $len ; ++$k) {
my $bit = read_bit($fh, \$buffer);
if ($bit eq '0') {
push @deltas, 0;
}
else {
my $bit = read_bit($fh, \$buffer);
my $n = 0;
++$n while (read_bit($fh, \$buffer) eq '1');
my $d = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. $n));
push @deltas, ($bit eq '1' ? $d : -$d);
}
if ($k == 0) {
$len = pop(@deltas);
}
}
my @acc;
my $prev = $len;
foreach my $d (@deltas) {
$prev += $d;
push @acc, $prev;
}
mtf_decode(\@acc);
}
my @bytes = do {
open my $fh, '<:raw', $^X;
local $/;
unpack('C*', <$fh>);
};
my $str = delta_encode([@bytes]);
say "Encoded length: ", length($str);
say "Rawdata length: ", length(pack('C*', @bytes));
my $decoded = delta_decode($str);
join(' ', @bytes) eq join(' ', @$decoded) or die "Decoding error";
__END__
Encoded length: 5270
Rawdata length: 14168