BEAR image compression
From ScienceZero
BEAR image compression was used on the Amiga and Acorn Archimedes computers. It was designed for digitized images with 16 levels of grey. It gives a better compression ratio than GIF on continuous tone images and is very simple to implement. The small differences between pixels is coded with short symbols, large differences are coded with 7 bits and loads the absolute value of the pixel.
Code Operation
0 - No change
10 - Decrease brightness
110 - Increase brightness
111xxxx - Set pixel to xxxx
(Codes are transmitted from left to right)
Any code that could be replaced by a shorter code or would go outside the range of 0-15 is reserved. These codes were supposed to be used for run-length encoding but this was never implemented.
This algorithm ran efficiently on the ARM CPU in the Archimedes but it was fairly slow on the MC68000 CPU in the Amiga.
C++ implementation of decompression
/*
Uncompress a sequence of bear compressed images.
pIn: compressed data
pOut: output uncompressed data
nPics: input: number of images
*/
void Bear(u32 *pIn, u8* pOut, u32 nPics) {
#define gnext ((bit>>=1) ? (D & bit) : (bit = 1<<31u, (D=pIn[cnt++]) & 1<<31))
u32 cnt = 0, pcnt = 0, D;
u32 bit = 0;
u8 C = 0; // start color
for (pcnt = 0; pcnt < 160*128 * nPics; pcnt++) { // pixels in all images
if (!gnext) pOut[pcnt] = C; // 0 = same
else if (!gnext) pOut[pcnt] = ++C; // 10 = up
else if (!gnext) pOut[pcnt] = --C; // 110 = down
else pOut[pcnt] = C = (!!gnext)<<3 | (!!gnext)<<2 | (!!gnext)<<1 | (!!gnext); // 111 = new color
}
}