Appendix B : The bmp Utility

Basic commands

The bmp utility mentioned in part 2 is an alternative method for breaking up a calculator image, other than a graphics package. It was originally written to understand and explore the bitmap format, and as such is an experimental program, with only minor thought given to a user interface. The interface will be more familiar to Linux/UN*X users---i.e. it is command line driven. For those more used to a Windows world, now's the time to open a DOS command window and do some real computing.

If you've downloaded bmp, then place it somewhere in your PATH. If that phrase means nothing to you, then simply place bmp.exe in the folder with your images, and where you see bmp in the description below, type .\bmp instead.

If you type bmp -h in a command window, the following usage message appears.

Usage: bmp [-dhrgVH] [-b ] [-c ] [-m ]
           [-C ] [-i ] [-o ]

    -h Display this message
    -d Increase debug output level (default no debug output)
    -b Change image brightness by specified percent (100% = normal)
    -c Change image contrast by specified percent (50% = normal)
    -g Change image to grey scale
    -r Reverse image colours
    -V Flip image about vertical axis
    -H Flip image about horizontal axis
    -m Extract monochromatic colour image:
    -C Clip image to rectangle
    -i Input filename (default test.bmp)
    -o Output filename (default no output)

All the command line options are listed. These can be used in isolation or in combination. There are three types of command option: file specification, information display and image manipulation. By default bmp reads a file test.bmp, and outputs no data. To change the input file read, the -i option is used. To enable output and specify the output file, the -o option is used (required if using manipulation commands). The -h option mentioned above, used to display the usage message, is of the information display type. The other important option of this type is -d. This increases the 'debug' level by 1. Currently there is only one debug level above 0 (off), and this displays the input file's bitmap header. So typing, for example,

  bmp -d -i calc.bmp
yields an output to the console like the following.

  Type               = BM
  File Size          = 0x00038ae8
  Offset             = 0x00000036

  Size               = 0x00000028
  Width              = 0x000000d9
  Height             = 0x00000164
  Planes             = 0x0001
  Bits per Pixel     = 0x0018
  Compression        = 0x00000000
  Image Size         = 0x00000000
  X Pixels per Meter = 0x00000b12
  Y Pixles per Meter = 0x00000b12
  Colour Used        = 0x00000000
  Colour Important   = 0x00000000
In terms of the requirements for calculator simulators, only the Width and Height are important. The numbers are in hexadecimal, which if you aren't familiar with is a bit of a pain. You might want to check you have a calculator which can do the conversion to decimal for you---the MS Calculator in windows will do this if you switch to scientific mode. (Bare in mind, though, that being frustrated with that particular utility is what started me on the road to writing my own simulators in the first place!) For those familiar with hexadecimal, the good news is that bmp will accept hexadecimal numbers (with a leading 0x). The rest of the displayed parameters may be of interest to those wanting to more of the bitmap file format. More information about the format can be found here.

Of the image manipulating command, -C is most relevant to the needs of simulation. As stated in part 2, this extracts a rectangular region from a bitmap image. With this command you must also specify an output file with -o (but do not use the same name as the input file, as it will be destroyed!). An input file name is also likely to be needed, but not strictly necessary if your input data is in a file called test.bmp. The command is used like the following example:

  bmp -i calc.bmp -o display.bmp -C "0 217 226 271"
Note that the quotes around the four specified numbers are necessary so that they are passed as a single argument to bmp. The numbers could easily have been in hexadecimal. I.e. -C "0x0 0xd9 0xe2 0x10f". Unlike the bitmap format itself, the rectangle specified here is referenced from the bottom left hand corner of the image (bitmaps--or DIBs to be precise---are stored top row first, or upside down). The numbers specify left, right, bottom and top limits, in that order. The right and top numbers actually specify the pixel after the last column/row extracted. This has two advantages. Firstly, the height (say) of the extracted file is simply the second number minus the first, with no +1 adjustment. Similarly for width. Secondly, when spliting images, the end number of the last image extracted is the beginning of the current image to be extracted, if you want them to be adjoining. I got this wrong so many times when spliting up bitmaps without this feature, that I changed the program. Trust me---it's better this way. All the commands used in calculator simulation have now been described, and you can stop here if you want. For completion, I will now describe the rest of the features, which you're welcome to read if you're interested.

Image manipluation commands

The bmp program is by no means a complete bitmap manipulation tool. Buy a commercial package for that. But it does illustrate the format and some basic methods of graphic manipulation (or my naive understanding of it at least). I think this aspect of the program is only useful with the source code, so you can get this here. It has a makefile for linux/un*x environments, and .dsw/.dsp files for Visual C++ 6.0 on windows. NB. there are 'endian' sensitivities to bitmap data, so if you compile the code in your own environment be aware of this, and check the header files (in particular bitmap.h).

The manipulation commands are pretty much self-explanatory, and I will just briefly discuss them here. The -b option allows the brightness to be increased by a specified percentage, with numbers less than 100 dimming the image. The -c option specifies a percent change in contrast, where 50% is unchanged. Values less than 50 decrease contrast and numbers greater than 50 increase it. The -g option simply converts to a grey scale image, whilst the -r produces a negative. The image may be flipped about its vertical and horizontal axes using -H and -V respectively. Finally, -m is used to extract just a primary (or its negative) component of the input bitmap. The argument can be a single letter, or the colour may be specified in full.

The above manipulation commands can be used in combination to produce different results, and it is surprising just how much can be done with just these few basic tools. Much more can be done with this, so download the source and have a go at adding features.

"How to write a calculator simulator"
<- Prev Page  Next Article->