Click here to Skip to main content
6,629,377 members and growing! (21,742 online)
Email Password   helpLost your password?
Languages » C# » General     Beginner

Converting a bmp picture to wbmp format

By Sajjitha Gunawardana

This program converts a bmp picture to a wbmp format used in wireless devices
C# 2.0, Windows, .NET 2.0VS2005, Dev
Posted:27 Jul 2007
Views:19,393
Bookmarked:10 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 1.66 Rating: 2.38 out of 5
2 votes, 40.0%
1

2

3
1 vote, 20.0%
4
2 votes, 40.0%
5

Screenshot - bmptowbmp2.jpg

Introduction

This code will load a Picture and convert it into a wbmp format. The first vesion 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 if a 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 a 8 x 8 pixel image will require only 64 bits (8 bytes). The limitation of this format is that it can only 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 AForge library for this purpose).

Using the code

The main part of this program is concerned with reading a pixel from a B/W 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 writtem into the wbmp file.

If for example the 1st pixel in the bmp is Black a '0' is written into the 1st location of the output byte.
If then the 2nd pixel in the bmp is White a '1' is written into the 2nd location of the output byte.
If 3rd pixel is White a '1' is written to 3rd location of the output byte...

The above is repeated until the whole output byte is 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 above 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 Monochtome image to wbmp looked 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 folowing. Now instead of a switch stucture we are using the 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 exactly not sure what is the proper method of writing bits into a byte in C#. I guess in C for example there is an otion of specifying bit fields in a stucture and then write to every bit seperately. So what I am doing above is using bitwise OR opration in C# to write to the seperate bits of a byte. In the earlier version this was done as follows :
To write a '1' into bit location 8 of a byte I ued something like data |= (byte)128;
To write a '1' into 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 used the AForge Library to which a sample program was found @
http://www.codeproject.com/cs/media/Image_Processing_Lab.asp

You can check whether a wbmp image created is ok using any of the online emulators. First create a .wml file with the wbmp image. Upload the wml file and wbmp image to a web site, and give the url of the .wml file to the emulator.
Several sample wbmp picture created with this application can be accessed @
http://sajjitha.atspace.com/lake2.wml
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...

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
Now hopefully the program should work ok... :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sajjitha Gunawardana


Member
coding in C# C++ C

==============================================

Before C it seems, there was B. And before B there was Darkness ... Smile
Occupation: Web Developer
Location: Sri Lanka Sri Lanka

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 24 of 24 (Total in Forum: 24) (Refresh)FirstPrevNext
Generalwbmp to bmp? PinmemberUnruled Boy19:29 27 Apr '09  
GeneralFara76 - where is it wrong? Pinmemberroonui22:05 7 Aug '07  
GeneralRe: Fara76 - where is it wrong? Pinmemberroonui1:19 8 Aug '07  
GeneralCan you please explain a part of your code please? PinmemberFara7612:44 1 Aug '07  
GeneralRe: Can you please explain a part of your code please? PinmemberFara7612:52 1 Aug '07  
GeneralRe: Can you please explain a part of your code please? PinmemberSajjitha Gunawardana15:42 2 Aug '07  
GeneralRe: Can you please explain a part of your code please? PinmemberFara7616:56 2 Aug '07  
GeneralRe: Can you please explain a part of your code please? PinmemberSajjitha Gunawardana17:35 2 Aug '07  
Generalnew code doesn't work Pinmembertodd_yx23:59 28 Jul '07  
GeneralRe: new code doesn't work PinmemberSajjitha Gunawardana6:17 29 Jul '07  
GeneralRe: new code doesn't work Pinmembertodd_yx23:19 29 Jul '07  
GeneralRe: new code doesn't work PinmemberSajjitha Gunawardana5:41 30 Jul '07  
GeneralRe: new code doesn't work Pinmembertodd_yx1:27 1 Aug '07  
GeneralRe: new code doesn't work Pinmemberkjaway23:33 10 Aug '09  
GeneralThx a lot Pinmembertodd_yx14:56 28 Jul '07  
GeneralOne Suggestion PinmemberWilli Deutschmann9:56 28 Jul '07  
GeneralRe: One Suggestion PinmemberSajjitha Gunawardana10:27 28 Jul '07  
QuestionWonderful program! And one more question Pinmembertodd_yx20:32 27 Jul '07  
AnswerRe: Wonderful program! And one more question PinmemberFara762:15 28 Jul '07  
GeneralRe: Wonderful program! And one more question PinmemberSajjitha Gunawardana5:47 28 Jul '07  
AnswerRe: Wonderful program! And one more question PinmemberSajjitha Gunawardana5:43 28 Jul '07  
GeneralRe: Wonderful program! And one more question PinmemberFara7613:13 28 Jul '07  
GeneralRe: Wonderful program! And one more question PinmemberSajjitha Gunawardana7:57 29 Jul '07  
Generalthx a lot Pinmembertodd_yx15:48 27 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Jul 2007
Editor:
Copyright 2007 by Sajjitha Gunawardana
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project