<p>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.</p>

<p>There are many different image file formats but a simple one to experiment with to gain understanding is ppm, one of the <a href="https://en.wikipedia.org/wiki/Netpbm_format">Netpbm formats</a>.</p>

<p>Netpbm defines the following file types:</p>

<ul>
<li><strong>PBM:</strong> portable bitmap (pixels are either on or off)</li>
<li><strong>PGM:</strong> portable graymap (grayscale pixels)</li>
<li><strong>PPM:</strong> portable pixmap (full colour pixels)</li>
</ul>

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

<p>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. </p>

<pre><code>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
</code></pre>

<p>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).</p>

<p>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.</p>

<hr>

<p>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.</p>

CC BY-SA 3.0
2016-01-29T02:57:40.227Z
2016-01-29T03:33:40.947Z
2016-01-29T03:33:40.947Z
trichoplax is on Codidact now
<p>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. </p>

<p>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.</p>

<p><a href="https://i.sstatic.net/j130t.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/j130t.gif" alt="A hypothethical bitmap"></a></p>

<p><strong>Image 1</strong>: 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 <a href="https://en.wikipedia.org/wiki/BMP_file_format" rel="nofollow noreferrer">BMP format</a> on wikipedia.</p>

<p>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.</p>

<p><a href="https://i.sstatic.net/iurjI.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/iurjI.gif" alt="indexed bitmap"></a></p>

<p><strong>Image 2</strong>: Indexed bitmaps store a lookup array from where they take the actual color. </p>

<p>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.</p>

<p>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 <strong><a href="https://en.wikipedia.org/wiki/Hex_editor" rel="nofollow noreferrer">hex editor</a></strong> 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.</p>

CC BY-SA 3.0
2016-01-29T09:03:42.017Z
2016-01-29T09:22:36.483Z
2016-01-29T09:22:36.483Z
joojaa
<p>I'm a Computer Science student who's been getting into bitmaps &amp; 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:</p>

<p>255,0,0 0,255,0 0,0,255</p>

<p>which would give me a red pixel, a green pixel, and a blue pixel.</p>

<p>Seems pretty straightforward.</p>

<p>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.</p>

<p>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?</p>

<bitmap-graphics>
How do I create my own bitmap using a text editor?
moonman239
# How do I create my own bitmap using a text editor?

(Asked by: username_0 on 2016-01-29)

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?

**Tags:** bitmap-graphics


---

## 2 answers

### Answer by username_1 on 2016-01-29. Score: 7

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](https://en.wikipedia.org/wiki/Netpbm_format).

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.


---

### Answer by username_2 on 2016-01-29. Score: 3

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](https://i.sstatic.net/j130t.gif)](https://i.sstatic.net/j130t.gif)

**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](https://en.wikipedia.org/wiki/BMP_file_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](https://i.sstatic.net/iurjI.gif)](https://i.sstatic.net/iurjI.gif)

**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](https://en.wikipedia.org/wiki/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.


---

