A very simplistic lossless routine to (un)pack repetitive bitmap data. Often
used in InterLeaved BitMap (ILBM) images in IFF containers (
1 2 3
InterLeaved BitMap (ILBM) images on the Amiga often use a packing algorithm referred to as ‘ByteRun1’. This routine was introduced first on the Macintosh where it was called PackBits. It is a form of run-length encoding and is very simple: when a specific byte is repeated in a bitmap, it is replaced by a (signed negative) byte telling how many times the following byte should be repeated. When a series of bytes are not repetitive, it is preceded by a (signed positive) byte telling how long the non repetitive part is.
Not very complicated, but for most images some bytes can be shaved off the file. This was very useful when everything had to be stored on 880 kilobyte floppy disks with little CPU time to spare. Note that the file size can also increase for (noisy) images.
This packing routine will pack the entire bitmap (
at once. The IFF file format requires packing of bitmap data per
scanline. This is done automatically by the
function, which calls this packing routine per scanline.
Returns packed or unpacked
raw data, depending on
unPackBitmap was called.
Pepijn de Vries
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
## generate some random raw data: dat.rnd <- as.raw(sample.int(10, 100, TRUE)) ## try to pack it: pack.rnd <- packBitmap(dat.rnd) ## due to the random nature of the source data ## the data could not be packed efficiently. ## The length of the packed data is close to ## the length of the original data: length(pack.rnd) - length(dat.rnd) ## Now generate similar data but sort it ## to generate more repetitive data: dat.srt <- as.raw(sort(sample.int(10, 100, TRUE))) pack.srt <- packBitmap(dat.srt) ## This time the packing routing is more successful: length(pack.srt) - length(dat.srt) ## The original data can always be obtained ## from the packed data: all(dat.rnd == unPackBitmap(pack.rnd)) all(dat.srt == unPackBitmap(pack.srt))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.