![]() |
Languages »
C# »
General
Beginner
Converting a bmp picture to wbmp formatBy Sajjitha GunawardanaThis program converts a bmp picture to a wbmp format used in wireless devices |
C#2.0, Windows, .NET2.0VS2005, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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.
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).
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 codedata |= (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); } }
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
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... :)
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 27 Jul 2007 Editor: |
Copyright 2007 by Sajjitha Gunawardana Everything else Copyright © CodeProject, 1999-2010 Web18 | Advertise on the Code Project |