View on
Dana Jacobsen > Data-BitStream-0.07 > Data::BitStream::Code::Rice



Annotate this POD



Open  1
View/Report Bugs
Module Version: 0.02   Source   Latest Release: Data-BitStream-0.08


Data::BitStream::Code::Rice - A Role implementing Rice codes


version 0.02


A role written for Data::BitStream that provides get and set methods for Rice codes. The role applies to a stream object.

Note that this is just the Rice code (Golomb(2^k)) themselves, and does not include algorithms for data adaptation.

These codes are sometimes called GPO2 (Golomb-power-of-2) codes.

Beware that with the default unary coding for the quotient, these codes can become extraordinarily long for values much larger than 2^k.

"...a Rice code (and by extension a Golomb code) is very well suited to peaked distributions with few small values or large values. As noted earlier, the Rice(k) code is extremely efficient for values in the general range 2^(k-1) < N < 2^(k+2)" -- Lossless Compression Handbook, page 75, by Khalid Sayood


Provided Object Methods

put_rice($k, $value)
put_rice($k, @values)

Insert one or more values as Rice codes with parameter k. Returns 1.

put_rice(sub { ... }, $k, @values)

Insert one or more values as Rice codes using the user provided subroutine instead of the traditional Unary code for the base. For example, the so-called "Exponential-Golomb" encoding can be performed using the sub:

  sub { shift->put_gamma(@_); }
get_rice($k, $count)

Decode one or more Rice codes from the stream. If count is omitted, one value will be read. If count is negative, values will be read until the end of the stream is reached. In scalar context it returns the last code read; in array context it returns an array of all codes read.

get_rice(sub { ... }, $k)

Similar to the regular get method except using the user provided subroutine instead of unary encoding the base. For example:

  sub { shift->get_gamma(@_); }


The parameter k must be an integer greater than or equal to 0.

The quotient value >> k is encoded using unary (or via the user supplied subroutine), followed by the lowest k bits.

Note: if k == 0 then the result will be coded purely using unary (or the supplied sub) coding.

Note: this is a special case of a Golomb(m) code where m = 2^k.

Rice coding is often preceeded by a step that adapts the parameter to the data seen so far. Rice's paper encodes 21-pixel prediction blocks using one of three codes. The JPEG-LS LOCO-I algorithm uses a constantly adapting k parameter to encode the prediction errors.

Required Methods


These methods are required for the role.


S.W. Golomb, "Run-length encodings", IEEE Transactions on Information Theory, vol 12, no 3, pp 399-401, 1966.
R.F. Rice and R. Plaunt, "Adaptive Variable-Length Coding for Efficient Compression of Spacecraft Television Data", IEEE Transactions on Communications, vol 16, no 9, pp 889-897, Dec. 1971.


Dana Jacobsen <>


Copyright 2011 by Dana Jacobsen <>

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: