4
$\begingroup$

I'm a Computer Science student who's been getting into bitmaps & pixmaps recently. I clearly understand what pixmaps do. A pixmap, as I understand, defines each pixel individually. So, maybe I could write a pixmap filelike this:

255,0,0 0,255,0 0,0,255

which would give me a red pixel, a green pixel, and a blue pixel.

Seems pretty straightforward.

However, I fail to understand exactly how bitmaps work. I mean, I understand that a bitmap simply maps a bit to some color at some point on the screen. I watched a YouTube video on how to create a bitmap file, but the guy's explanation of what a bitmap file is went over my head.

Edit: I feel I now have a better conceptual grasp of what a bitmap is. Well, maybe not. Each bit in a bitmap corresponds to a certain color that is defined somewhere in the file. But how does the computer know which bit corresponds to which color?

$\endgroup$
1
  • $\begingroup$ Do you understand how frame buffers work? en.wikipedia.org/wiki/Framebuffer (Caveat, it's got alot of history.) Is that some of what you are looking for? $\endgroup$
    – user2500
    Jan 29, 2016 at 14:11

2 Answers 2

7
$\begingroup$

In addition to the pixel values, the width and height of the image are also required, and the colour depth (how many colours are available per pixel). Some formats also specify an alpha value, for transparency, which may be defined per pixel in addition to the red, green and blue components.

There are many different image file formats but a simple one to experiment with to gain understanding is ppm, one of the Netpbm formats.

Netpbm defines the following file types:

  • PBM: portable bitmap (pixels are either on or off)
  • PGM: portable graymap (grayscale pixels)
  • PPM: portable pixmap (full colour pixels)

These have the advantages of being simple to understand, and easy to generate in a text editor for working with simple examples.

For example, a PPM file is simply the characters "P3" to identify it as PPM, followed by the width, height, maximum colour value and the pixel values in English reading order (left to right then top to bottom). Each pixel colour is a red value, a green value, and a blue value. All these values are space separated and human readable.

P3                         # Indicates an ASCII PPM file
2 2                        # Width and height of the image
255                        # Maximum colour value
255 0 0     0   255 0      # A red pixel (255 0 0) followed by a green pixel
0   0 255   255 255 255    # A blue pixel followed by a white pixel

While this makes the format very easy to work with for small examples, it will also make the file size much larger than other formats which use bytes to represent colour components instead of writing out the human readable numbers. Many formats also include compression (either lossless or lossy).

Once you are accustomed to writing PPM files in ASCII characters, indicated by the file starting with "P3", you can move on to PPM files using bytes, indicated by the file starting with "P6". This is a similar format to what many programs use internally, but is no longer human readable in a text editor as not every byte will correspond to a printable ASCII character.


You also mention that a pixmap can have pixel values that refer to colours stored elsewhere in the file. I have avoided this format as it is not necessary for understanding how a pixmap works. This is known as using a palette (a list of predefined colours so each pixel is represented by an index of a colour in the palette). This is necessary for formats that only allow a small number of distinct colours, but for full colour images there is no need for a palette - each pixel is defined as red, green and blue components without having to refer to anything else.

$\endgroup$
3
$\begingroup$

Bitmap is simply means to convert a data range to bits. How that internally works and what the conversion function is, is application dependent. Each format can work differently.

Simplest possible bitmaps just store the values directly in the bits themselves. Think of the the simplest form, a pixmap but instead of ASCII values you are using full byte per color. Off course the data does not need to be byte aligned so 3 values may be crammed to 2 bytes for example. Some formats may be using indexed colors (which is what you describe in your edit), this is a form of a compression scheme.

A hypothethical bitmap

Image 1: A hypothetical bitmap with 15 bits per pixel (for 5 bits per color), similar arrangement is used by some bitmap formats (this actual pattern is available in BMP). For a actual format read for example about the BMP format on wikipedia.

Because the lower bit depths have very little space for color values these formats are often accompanied by a lookup table instead. This allows you to store a custom set of colors very tightly packed while allowing for a sensible variance.

indexed bitmap

Image 2: Indexed bitmaps store a lookup array from where they take the actual color.

Note that some formats may have pretty sophisticated transforms, or the data is compressed further by some more elaborate compression scheme. But even so a compression of $2/3$ and especially the indexed $1/6$ is pretty good for something that naturally tends to take up space. Off course if we compare with a ASCII pixmap those compression ratios are already quite significantly higher.

There are many such formats that you could type in with a text editor if you must. However, since your dealing with individual bits its much easier to do this with a hex editor instead as the data is essentially binary. In practice it is easier to write a script to dump full values in the file, except perhaps in the 1 byte is one channel scenario where a hex editor works fine.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.