Raster graphics and representation of a BMP file

Andrea Sorrentino
Computer Graphics
Published in
7 min readOct 6, 2018

--

Nowaday we’re in a technological era where computers have a primary role in our life, because we delegate tasks to them that we aren’t albe to execute in reasonable times, or simply just execute. Since ancient times, human beings used many means to communicate: from gestures to accounting, from writings to languages, from drawing to music and so on. So it’s clear that from the invention of the first computer, men have always searched a way to allow users to communicate with machines in a simple and quick way. In fact this is what a modern operating system does, providing a graphical interface to the user through which we can simply insert input data and receive output data, typically using keyboard and monitor.

But what are the elements that make up the graphical interface? Essentially we have three kind of graphics: text, images and video, where each one provides a certain level of communication. For example, images are often more immediate in messages exchange or in concept understanding. In fact Google, in its guidelines for the Android UI/UX, used an appropriate slogan which is “images are faster than words”.

Raster graphics

In computer graphics we have two kind of graphics: raster and vectorial. About the first typology, it is used for the representation of two-dimensional images, that is what we commonly call “2D images”. The word raster refers to the grid of dots that makes up the image. The atomic unit of a raster image is the pixel, to whom can be associate a certain number of bits. More bits are associated to a pixel and greater is the scale of colors the pixel can represent. The number of bits associated to a pixel is called depth. For example, if an image is made up from pixels with 1 bit each one, we are able to represent at most the white and black color, conventionally with values 1 and 0, but nothing forbids us to do the reverse, because computer science is a world made of choices.

Vectorial graphics is defined in terms of geometric primitives and it’s used for both simple 2D images and 3D images. We will deal with them in a different article.

Digitalization of an image

Basic idea of raster graphics (called also “bitmap”) is to represent the image through a finite and discrete set of dots. In fact computers have limited computational resources, which means the processor is not able to elaborate infinite values and the memory is not able to store an infinite amount of data. For this reason we speak about “digitalization” of an image, which is performed through two processes: sampling and quantization. Sampling is the phase that takes care of discretization of the continue signal, which consists in choosing samples that allow to approximate the information, bringing as effect a loss of quality. Quantization is the process that measures the samples properties and assign them a numeric value, which is encoded in binary later.

RGB 256 bit model

Most raster images use the RGB 256 colors encoding. This encoding establishes to assign a triple of 8 bit strings to each pixel (xxxxxxxx, xxxxxxxx, xxxxxxxx), from which we get as result 2^(8+8+8) = 16.777.216 combinations, which means each pixel can represent more than 16 million of colors and so the entire color spectrum visibile to the human eye. The first component of the triple represents all the shades of blu, the second one all the shades of green and the third one all the shades of red. The RGB model allows to represent the shades of colors using an additive technique, which means juxtaposing red, green and blue color (each one with a certain intensity). From a certain distance from the screen they will overlap creating the illusion of a new color.

The following image shows an RGB LED used in LED monitors:

Exampe of raster images

Examples of raster images formats are JPEG, PNG, BMP, GIF files, each of which uses an own image encoding, adapted to the optimization of particular kinds of images. For example the PNG file format is great for images with transparency (it adds an alpha channel, RGBA), or for simple graphics. Instead the JPEG file format is great for photos, in which there is much variation of color.

The structure of a BMP file

A BMP file (bitmap) is a file format used to represent digital images without compression. Because there is no compression, it makes this file very heavy and so inappropriate for the Internet transfer. But at the same time, it’s true that it makes the file simple to read and write. Going deeper, let’s see how an image is represented under the bitmap encoding, which is one of the simplest encoding, compared to other that apply also compression algorithm. The simplified structure of a bitmap file expects three main blocks (you can see the detailed structure here): Bitmap File Header, DIB Header and Bitmap Data.

The Bitmap File Header is a header block that contains all information about the file. It consists of 14 bytes. The first two bytes represent the B and M characters under the ASCII encoding, which indicate that we’re dealing with a bitmap file. The other four bytes are used to indicate the file size. Then there are other four bytes reserved to the application which has created the image. Finally there are other four bytes used to store the offset to sum to the actual position to point to the Bitmap Data block.

The DIB Header is a header block that contains information about the image. In particular: width and height in pixel, number of bit per pixel, size ecc. These information help us to correctly read the Bitmap Data block. Also this header has a fixed size, but it can change depending on his version:

The Bitmap Data contains a matrix of bit strings, each one for each pixel inside the image. This matrix is written on the file in a way that the first bit string refers to the first pixel of the image last row.

From this structure can be done some considerations. The size of the Bitmap Data is approximately:

Bitmap Data size = number of pixel in width x number of pixel in height x number of bits per pixel

For example, if a bitmap image has a resolution of 100x100 and a 16 bit depth, it means the data block will have a size of 20.000 byte ((100 x 100 x 16) / 8). The BMP file dimension is approximately:

BMP file size = BMP File Header size + DIB Header size + Bitmap Data size

In fact, if we try to create a BMP image through any raster graphics software, using the specified settings, it will create a file with a size about 20 KB.

Hexadecimal structure of a BMP file

Now, let’s analyze the hexadecimal structure of a BMP file. For this test is been used an image with a resolution of 2x2 and 24 bit depth. The image has:

  • A black pixel (hex: 000000)
  • A white pixel (hex: ffffff)
  • A blue pixel (hex: 0000ff)
  • A red pixel (hex: ff0000)

To analyze the hexadecimal code of any file I used a software for Mac OS called iHex, available on the App Store. At the file opening, the program has provided the following output:

In first instance, it reports that the file is made up 72 bytes. In fact there are 18 hexadecimal strings of 8 characters, where each character is a group of 4 bits (in fact 18 x 18 x 4 = 575 bits / 8 = 72 bytes). This is also confirmed by the Mac OS Finder:

The first two hexadecimal characters form a byte, that is the minimum unit of information addressable in a computer, because memories are byte addressable. These two characters represent the ASCII encoding of “B”, instead the next two represent the ASCII encoding of “M”. The program iHex shows on the right the ASCII interpretation of the file. Obviously, the first two characters are correct, just because it has been used the same encoding for the first 2 bytes. Instead the next values has no meaning because they are been wrongly interpreted. The part highlighted in green is the Bitmap File Header (14 bytes), the one in yellow is the DIB Header (40 bytes) and the rest is the Bitmap Data (18 bytes). Remembering that in a bitmap file the pixel are stored from the first column of the last row, the first stored value is the hexadecimal representation of the blue color in RGB, followed by red, black and white. Between the hexadecimal representations of the various pixels there are some groups of bytes called padding, and they are related to the data structure alignment in memory, thus they have no semantics at data level.

Bibliography

--

--

Andrea Sorrentino
Computer Graphics

I’m a computer engineering student particularly interested in software engineering, programming, computer graphics, game development, databases and AI.