The encode_image() function

Finally, the encode_image() function is used to encode the raw bitmap array into a GIF compressed stream. This code was originally found in an antique version of FRACTINT (a Fractal graphics generator that I had on my system from the BIX (Byte Information Exchange) days. I've simplified the code to deal only with a limited range of colors and a fixed input format. You'll need to provide your own version.

I won't go into great detail on the encode_image() function, but here's the prototype:

encode_image (unsigned char *raster,
              int x,
              int y,
              unsigned char *output);

The parameters are:

The raw bitmap, in the same format as the input to the render_7segment() function, above.
x, y
The X and Y size of the raw bitmap.
The compressed output buffer, allocated by the caller.

Since we don't know a priori how big the compressed output buffer will be, I allocate one that's the same size as the input buffer. This is safe, because the nature of the 7-segment rendering engine is that there will be many “runs” (adjacent pixels with the same color), which are compressible. Also, the bitmap used is really only one bit (on or off), and the compressed output buffer makes full use of all 8 bits for the compressed streams. (If you modify this, beware that in certain cases, especially with “random” data, the compressed output size can be bigger than the input size.)

As another exercise, you can modify the webcounter resource manager to generate a JPEG or PNG file instead of, or in addition to, the GIF files that it currently generates.

The return value from the encode_image() function is the number of bytes placed into the compressed data buffer, and is used in io_read() to set the size of the resource.