public
Description: Range Coder for Perl
Homepage:
Clone URL: git://github.com/naoya/perl-algorithm-rangecoder.git
100644 52 lines (36 sloc) 1.22 kb
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
NAME
    Algorithm::RangeCoder - Data compression with Range Coder
 
SYNOPSIS
      use Algorithm::RangeCoder;
      use constant UCHAR_MAX => 0x100;
 
      my $str = shift or die "usage: $0 <string>";
 
      my @char = unpack('C*', $str);
 
      my @freq;
      for (my $i = 0; $i < UCHAR_MAX; $i++) {
          $freq[$i] = 0;
      }
 
      for my $c (@char) {
          $freq[$c]++;
      }
 
      my @cum = (0);
      for (my $i = 0; $i < UCHAR_MAX; $i++) {
          $cum[$i + 1] = $cum[$i] + $freq[$i];
      }
 
      my $rc = Algorithm::RangeCoder->new;
      $rc->freq = \@freq;
      $rc->cumfreq = \@cum;
 
      my $bin = $rc->encode($str);
 
      say "origin: ", $str;
      say "decoded: ", $rc->decode( $bin );
 
DESCRIPTION
    Range coder is an algorithm used for entropy coding in compression
    algorithms.
 
SEE ALSO
    <http://en.wikipedia.org/wiki/Range_encoding>
 
AUTHOR
    Naoya Ito, <naoya at bloghackers.net<gt>
 
COPYRIGHT AND LICENSE
    Copyright (C) 2008 by Naoya Ito
 
    This library is free software; you can redistribute it and/or modify it
    under the same terms as Perl itself, either Perl version 5.8.8 or, at
    your option, any later version of Perl 5 you may have available.