Friday, December 20, 2013

Portable GreyMap

The whole family of portable pixmaps in the Netpbm family of formats have a very extensive set of tools to convert and process them. These tools are however not included as standard on many platforms you'd want to develop in RB for.

The format of the PGM file format, a greyscale bitmap image, is fairly simple and described widely on the net. Apart from being simple, it however also allows for many variations. All these possible variations make it again a bit more interesting to make a robust reader for these files.

One example of the plain-text version (type P2) is this

        P2
        # feep.pgm
        24 7
        15
        0 0  0  0  0  0  0  0  0 0  0  0  0  0  0  0  0 0  0  0  0  0  0  0
        0 3  3  3  3  0  0  7  7 7  7  0  0 11 11 11 11 0  0 15 15 15 15  0
        0 3  0  0  0  0  0  7  0 0  0  0  0 11  0  0  0 0  0 15  0  0 15  0
        0 3  3  3  0  0  0  7  7 7  0  0  0 11 11 11  0 0  0 15 15 15 15  0
        0 3  0  0  0  0  0  7  0 0  0  0  0 11  0  0  0 0  0 15  0  0  0  0
        0 3  0  0  0  0  0  7  7 7  7  0  0 11 11 11 11 0  0 15  0  0  0  0
        0 0  0  0  0  0  0  0  0 0  0  0  0  0  0  0  0 0  0  0  0  0  0  0

A quick and simple LoadFromPGM function reads the file in and stretches the greyscale values of a regular Picture object. (Blurring in the screenshot is from the scaling of the Picture.)
The LoadFromPGM method is written to be fairly forgiving on the file format. It will read one image from a PGM file, either a binary or a plaintext one. There is a lot of repetitive code, it should really be re-factored and it is not the fastest code. But then again, source is provided here so feel free :)

The source for the example PGM viewer application can be downloaded in this archive.