Programming Practice

In image processing, a bitmap is a type of file format to store digital images. The format of a bitmap file can be divided into two parts, file header and image information, as the following type declaration:

typedef struct {

  // File header: 14 bytes.
  char Type[2];               // Two fixed characters, "BM" for bitmap images.
  unsigned Size;           // File size in bytes.
  char Reserved[4];       // Reserved field.
  unsigned OffsetBits;  // Offset, i.e., the starting address of the byte where the bitmap image data (pixel array) are stored

  // Image information: 40 bytes.
  unsigned InfoSize;    // Information size in byte.
  unsigned Width;       // Image width in pixel.
  unsigned Height;      // Image height in pixel.
  unsigned short Planes;        // N
umber of image planes in the image, must be 1.
  unsigned short BitPerPixel;  //
Number of bits used to represent the data for each pixel.
  unsigned Compression;        //
Value indicating what type of compression, if any, is used, (0: uncompressed).
  unsigned ImageSize;            // Size of the actual pixel data, in bytes.
  unsigned XResolution;         // Preferred horizontal resolution of the image, in pixels per meter.
  unsigned YResolution;         // Preferred vertical resolution of the image, in pixels per meter.
  unsigned Colors;                 // Value is zero except for indexed images using fewer than the maximum number of colors.
  unsigned ImportantColors;  // Number of colors that are considered important when rendering the image.
} Header;

Following the file header and image information is the color palette with the size of OffsetBits-InfoSize-14 bytes, and then the image pixel data with the size of ImageSize bytes. The color palette and image pixel data can be declared as pointers of usigned char. The images used in this programming practice does not use color palette, i.e., OffsetBits-InfoSize-14 is 0. The pixel data area is stored using the following format:

  1. Image pixels are stored in the row-major order starting from the bottom-left corner. That is, pixels are stored from left to right and from bottom to top; the pixels in the last row are stored on the front of the pixel data area.

  2. Each pixel takes three bytes representing levels of the three original colors red, green, and blue (RGB) one byte each color. The order of the three bytes is blue color in the first byte, green in the second byte and red in the third byte.

  3. The number of bytes in a row must be a multiple of four. If the number of actual pixel bytes is not a multiple of four, 0X00's are padded at the end of the row.

Write a C program to perform the following steps:

  1. Read a color image bitmap file from disk.

  2. Peform mirror reflection based on the central vertical line of the image. That is, exchange two pixels if they are left-right symmetric along the central vertical line of the image. For example, the two pixels on the top-left corner and the top-right corner are exchanged.

  3. Write the image bitmap file after the vertical mirror reflection transformation to disk.

  4. Output the resulting file header and the image information head on the screen.

Refer to Section 1.3.2 in The C Library Reference Guide for the function main: int main(int argc, char *argv[]) {body ...}. The default file name of the vertical mirror reflection image is "vertical_mirror_reflection.bmp". You may use example color image files blue_hills.bmp, water_lilies.bmp, abraham_lake.bmp, red_dragon.bmp, and wildflowers.bmp to test the program. If you use a color image other than the bit map format, such as png, gif, or jpg format, you may use a graphics tool to convert it to a bmp format file.

Program execution example:

A color image example:

The transformed banded-filter image: