From the title you can see that I have written a converter for images. But what does it convert? To what? The title says ‘converters’. So does it imply more than one converter? To get answers for all these, read ahead.
The first program converts a ‘.bin’ file to a ‘.bmp’ file. You might ask me why I have written it. What is the use of such a converter? Well, if you have worked with image sensors you will realize that it’s a very handy program. The output of an image sensor is in hex values, called as raw data. Most sensors allow some kind of formatting such as RGB565 and some even provide JPEG output. But raw data is something that is available from all sensors. Let’s say we are using an image sensor with a micro-controller. Once the data is captured by the sensor, it sends this data to the micro-controller and this data has to be stored. Once stored, you want to view the image; most likely on your computer. Will Picasa open this file? Sadly, it won’t. Because the file is not in any recognized format. It’s just a huge collection of hex data and any image viewing software cannot make sense out of it.
The hex data has to be converted into some format that can be recognized by image viewing software. This is where BMP fits in. BMP is a very simple format created by Microsoft. The bitmap file consists of two main sections: the header and the data.
This can be broken into four sections as follows
- File Header
- Confirms that the file is a BMP file
- File size
- Exact location of the image data
- Image Header
- Number of rows and columns in the image
- Number of bits per pixel
- Type of compression used
- Color Table
- Provides the (optional) bit masks for bit depths of 16 and 32
- Not used for 24-bit images
- Pixel Data
- Pixel by pixel color information
- Row by row bottom to up
- Each row is column by column, left to right
All multibyte values are stored in Little Endian format. This means that the bytes within the value are stored starting with the least significant byte first and the most significant byte last.
When dealing with 16-bit or 32-bit images, then the Color Table contains a set of bit masks used to define which bits in the pixel data to associate with each color. The color masks are 32-bits long with the green mask being first and the blue mask being last. In both cases the bit masks start with the most significant bits, meaning that for the 16-bit images the least significant two bytes are zero. The format requires that the bits in each mask be contiguous and that the masks be non-overlapping.
The pixel data is organized in rows from bottom to top and, within each row, from left to right. Each row is called a “scan line”. In case of a 16-bit format, each pixel is represented by two bytes. A common representation is RGB555 which allocates 5 bits to each color allowing for 32K colors while leaving one bit unused. Since the eye is most sensitive to green, another common representation is RGB565 which allocates this unused bit to the green component.
To convert our hex values to an image, all we need to do here, is to add a header to the hex data. The file is now a ‘.bmp’ file and you can view it in your computer. As you can imagine, the program is pretty simple. It works for only images of size 320×240 and for any other size, lower or higher, the header has to be suitably modified. I forgot to mention one important thing. The converter I have written is for RGB565 hex data. If you attempt to use this program for uncompressed RGB888 hex data, you will not get a proper image. Again to make it work for RGB888 is single change in the header.
Now for the second converter. Now this finds a jpeg image in a long list of messy data. When I wrote the above program, I was working with sensors which gave uncompressed outputs. When I moved to sensors with compressed outputs, the above program wasn’t sufficient. So I set out to write a converter for jpeg images.
The most popular compression technique is probably the JPEG standard. Now I will not be explaining about the JPEG standard here. But the one important thing to know is that a jpeg image always begins with ‘FF D8’ and ends with ‘FF D9’. This makes writing this converter extremely simple. All I have to do is find ‘FF D8’. This is where the image begins. Then I have to find ‘FF D9’. This is where the image ends. All the data outside of this we are not bothered with.
Let me give a little background here. A lot of sensors provide jpeg output. But there is usually extra stuff with it, like response sequences to various commands. These extra data can either be eliminated before saving the image data or you can save the entire thing and eliminate the extra sequences later. I did the latter and hence this program.