Click here to Skip to main content
Licence CPOL
First Posted 27 Jul 2007
Views 32,849
Downloads 216
Bookmarked 16 times

Converting a BMP picture to WBMP format

By | 27 Jul 2007 | Article
This program converts a BMP picture to the WBMP format used in wireless devices.

Screenshot - bmptowbmp2.jpg

Introduction

This code will load a picture and convert it into WBMP format. The first version of the program supported only monochrome BMPs with width, height < 127. Currently, you can load color pictures as well, and pictures with width, height > 127.

Background

WBMP is an image format used in wireless devices. The pictures in the WBMP format are saved in bit format. That is every pixel of an image is saved as 1 bit. Thus, an 8 x 8 pixel image will require only 64 bits (8 bytes). The limitation of this format is that it can save images in Black and White only (no support for color or grey scale). Thus, if you load any non-monochrome picture, it will be internally converted to monochrome using Dithering Algorithms (using the AForge library for this purpose).

Using the code

The main part of this program is concerned with reading a pixel from a black and white BMP picture, checking whether each pixel is either black or white, and then writing either a '0' or '1' into the output WBMP. Thus, every 8 pixels of the input BMP will get compressed into one byte (8 bits), which is then written into the WBMP file.

If for example the first pixel in the BMP is black, a '0' is written into the first location of the output byte. If the second pixel in the BMP is white, a '1' is written into the second location of the output byte. If the third pixel is white, a '1' is written to the third location of the output byte...

The above is repeated until the whole output byte has been written to (which will correspond to 8 pixels in the input BMP). This output byte is then written to the WBMP file. The whole process is then repeated until all the pixels in the input BMP has been 'compressed' and written into the output WBMP file.

The original code for converting the monochrome image to WBMP looks as follows:

for (int y = 0; y < inputBmp.Height; y++)
{
    byteLocation = 0;
    data = 0;
    for (int x = 0; x < inputBmp.Width; x++)
    {
        Color color = inputBmp.GetPixel(x, y);
        byte BW = (byte)((color.R + color.G + color.B) / 3);
        //reset byteLocation every 8 bits
        if (byteLocation > 7) byteLocation = 0;
        bool black = false;
        if(BW > 125)
        {
            black = true;
        }
        switch (byteLocation)
        {
        case 0:
            if (black)
            {
                data |= (byte)128;
            }
            break;
        case 1:
            if (black)
            {
                data |= (byte)64;
            }
            break;
        case 2:
            if (black)
            {
                data |= (byte)32;
            }
            break;
        :
        :
        case 7:
            if (black)
            {
                data |= (byte)1;
            }
        // every 8 bits write the data to the output file
            bw.Write(data);
            data = 0;
            break;
        }
    byteLocation++;
    }

    // if at end of a line we have some remaining bits to be written, write it here
    // this will happen if bmp width is not a multiple of 8.
    if (byteLocation != byteLength + 1)
    {
    bw.Write(data);
    }
}

After receiving some feedback from 'Willi Deutschmann', the above code was simplified to the following. Now, instead of a switch structure, we are using this single line of code: data |= (byte)(128 >> byteLocation);.

for (int y = 0; y < inputBmp.Height; y++)
{
    byteLocation = 0;
    data = 0;
    for (int x = 0; x < inputBmp.Width; x++)
    {
        Color color = inputBmp.GetPixel(x, y);
        byte BW = (byte)((color.R + color.G + color.B) / 3);
                        
        //reset byteLocation every 8 bits
        if (byteLocation > 7) byteLocation = 0;
                        
        if(BW > 0)
        {
            data |= (byte)(128 >> byteLocation);
        }

        // every 8 bits write the data to the output file
        if (byteLocation == 7)
        {
            bw.Write(data);
            data = 0;
        }        
        byteLocation++;
    }   

    // if at end of a line we have some remaining bits to be written, write it here
    // this will happen if bmp width is not a multiple of 8.
    if (byteLocation != byteLength + 1)
    { 
        bw.Write(data);
    }
}

Points of Interest

I am not sure what the proper method of writing bits into a byte in C# is. I guess in C, for example, there is an option for specifying bit fields in a structure and then writing to every bit separately. So what I am doing above is using a bitwise OR operation in C# to write to the separate bits of a byte. In the earlier version, this was done as follows: to write a '1' into the bit location 8 of a byte, I used something like data |= (byte)128;. To write a '1' into the bit location 7 of a byte: data |= (byte)64; etc. In the new code, this has been modified to data |= (byte)(128 >> byteLocation);.

To convert a color image to monochrome, use the AForge Library for which a sample program is available here @ http://www.codeproject.com/cs/media/Image_Processing_Lab.asp.

You can check whether a WBMP image created is alright using any of the online emulators. First, create a .wml file with the WBMP image. Upload the WML file and the WBMP image to a website, and give the URL of the .wml file to the emulator. Several sample WBMP pictures created with this application can be accessed @ http://sajjitha.atspace.com/lake2.wml and http://sajjitha.atspace.com/forest.wml.

Limitation / Modification...

The program has been tested for monochrome and Color images. Several WBMP files produced by this application were uploaded into a WAP emulator and they displayed OK...

History

  • 07/29/2007: Support for color images and images with width, height > 127.
  • 07/31/2007: Corrected a bug which gave a wrong output when image width, height > 127.
  • Hopefully the program works OK now... :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sajjitha Gunawardana

Web Developer

Sri Lanka Sri Lanka

Member

coding in C# C++ C
 
==============================================
 
Code in English, not Gibberish Smile | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionwbmp to bmp? PinmemberUnruled Boy18:29 27 Apr '09  
QuestionFara76 - where is it wrong? Pinmemberroonui21:05 7 Aug '07  
AnswerRe: Fara76 - where is it wrong? Pinmemberroonui0:19 8 Aug '07  
QuestionCan you please explain a part of your code please? PinmemberFara7611:44 1 Aug '07  
AnswerRe: Can you please explain a part of your code please? PinmemberFara7611:52 1 Aug '07  
GeneralRe: Can you please explain a part of your code please? PinmemberSajjitha Gunawardana14:42 2 Aug '07  
GeneralRe: Can you please explain a part of your code please? PinmemberFara7615:56 2 Aug '07  
GeneralRe: Can you please explain a part of your code please? PinmemberSajjitha Gunawardana16:35 2 Aug '07  
Generalnew code doesn't work Pinmembertodd_yx22:59 28 Jul '07  
GeneralRe: new code doesn't work PinmemberSajjitha Gunawardana5:17 29 Jul '07  
GeneralRe: new code doesn't work Pinmembertodd_yx22:19 29 Jul '07  
GeneralRe: new code doesn't work PinmemberSajjitha Gunawardana4:41 30 Jul '07  
GeneralRe: new code doesn't work Pinmembertodd_yx0:27 1 Aug '07  
GeneralRe: new code doesn't work Pinmemberkjaway22:33 10 Aug '09  
GeneralThx a lot Pinmembertodd_yx13:56 28 Jul '07  
GeneralOne Suggestion PinmemberWilli Deutschmann8:56 28 Jul '07  
GeneralRe: One Suggestion PinmemberSajjitha Gunawardana9:27 28 Jul '07  
QuestionWonderful program! And one more question Pinmembertodd_yx19:32 27 Jul '07  
AnswerRe: Wonderful program! And one more question PinmemberFara761:15 28 Jul '07  
GeneralRe: Wonderful program! And one more question PinmemberSajjitha Gunawardana4:47 28 Jul '07  
AnswerRe: Wonderful program! And one more question PinmemberSajjitha Gunawardana4:43 28 Jul '07  
GeneralRe: Wonderful program! And one more question PinmemberFara7612:13 28 Jul '07  
GeneralRe: Wonderful program! And one more question PinmemberSajjitha Gunawardana6:57 29 Jul '07  
Generalthx a lot Pinmembertodd_yx14:48 27 Jul '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 27 Jul 2007
Article Copyright 2007 by Sajjitha Gunawardana
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid