Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

52 lines (36 sloc) 1.22 kb
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.
Jump to Line
Something went wrong with that request. Please try again.