Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Working with Bitmap and DIB
[ All Languages » VB »  Bitmap]

Total Hit ( 16045)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


Click here to download the attached file  


In this article we will learn basic concept to work with Bitmap and DIB. We will wrap all functionality in a handy class which can give you ability to work with 2D Pixel array of Bitmap loaded into the memory.

Before we implement DIB and BMP class lets understand BMP file format which is DIB (Device Independent Bitmap). Once you know the internal of Bitmap you will find a whole new world of image processing. You can use pixel array to give various effects.

Bitmap file contains 4 parts

1. Bitmap file header
2. Bitmap image information header
3. Color table information
4. Image data





Lets start with Bitmap file header

Bitmap file header

The first thing you'll find stored in a bitmap file is what's called the File Header structure. This is 14 bytes header.

Click here to copy the following block
Type BITMAPFILEHEADER
  bfType As Integer
  bfSize As Long
  bfReserved1 As Integer
  bfReserved2 As Integer
  bfOffBits As Long
End Type

Where,
  • bfType : is type of file which is always ascii string 'BM' hex value of 'BM' is &H4D42 in VB.
  • bfSize : is size of of entire file in bytes.
  • bfReserved1, bfReserved2 : reserved for future use
  • bfOffBits : tells that from where actual image data starts. So bOffBits will be sizeof(Fileheader+Bitmap info+colotable).


Bitmap info header

After file header next header is bitmap information header. This is 40 bytes header.

Click here to copy the following block
Type BITMAPINFOHEADER
  biSize As Long
  biWidth As Long
  biHeight As Long
  biPlanes As Integer
  biBitCount As Integer
  biCompression As Long
  biSizeImage As Long
  biXPelsPerMeter As Long
  biYPelsPerMeter As Long
  biClrUsed As Long
  biClrImportant As Long
End Type

Where,
  • biSize : is size of the info header which will be 40 bytes.
  • biWidth : is width of bitmap in pixels.
  • biHeight : is height of bitmap in pixels.
  • biPlanes : describes the number of planes contained in the bitmap, this is not normally used, and is set to one.
  • biBitCount : is an important one. It describes the "bit-depth" of this bitmap. It can have any of four values: 1, 4, 8, and 24. A bit depth of one indicates that the bitmap will have only two colours (monochrome), a bit depth of 4 will allow 16 colours, 8bit equals 256 colours, and 24bit is 16.8 million colours. Now the bit depth dictates whether or not a bitmap will use a colour table (discussed later). 24bit bitmaps DO NOT use a colour table, while the other bit formats do.
  • biCompression : Specifies the type of compression. Can be one of three values: BI_RGB, BI_RLE4, or BI_RLE8. The most common and useful choice, BI_RGB, defines a DIB in which all is as it seems. Each block of biBitCount bits defines an index (or RGB value for 24-bit versions) into the color table. The other two options specify that the DIB is stored (or will be stored) using either the 4-bit or the 8-bit run length encoding (RLE) scheme that Windows supports. The RLE formats are especially useful for animation applications and also usually compress the bitmap. BI_RGB format is recommended for almost all purposes. RLE versions, although possibly smaller, are slower to decode, not as widely supported, and extremely painful to band properly.
  • biSizeImage : contains the length of the bitmap image data (the actual pixels) in bytes.
  • biXPelsPerMeter, biYPelsPerMeter : Define application-specified values for the desirable dimensions of the bitmap. This information can be used to maintain the physical dimensions of an image across devices of different resolutions. GDI never touches these fields. When not filled in, they should both be set to 0.
  • biClrUsed : Provides a way for getting smaller color tables. When this field is set to 0, the number of colors in the color table is based on the biBitCount field (1 indicates 2 colors, 4 indicates 16, 8 indicates 256, and 24 indicates no color table). A nonzero value specifies the exact number of colors in the table. So, for example, if an 8-bit DIB uses only 17 colors, then only those 17 colors need to be defined in the table, and biClrUsed is set to 17. Of course, no pixel can have an index pointing past the end of the table.

    Note: This field cannot be used during a GetDIBits operation. GDI always fills a full-size color table. The field is therefore more useful for post-processing operations, when an application trims down the contents of the DIB. If nonzero for a 24-bit DIB, it indicates the existence of a color table that the application can use for color reference.
  • biClrImportant : Specifies that the first x colors of the color table are important to the DIB. If the rest of the colors are not available, the image still retains its meaning in an acceptable manner. biClrImportant is purely for application use; GDI does not touch this value. When this field is set to 0, all the colors are important, or, rather, their relative importance has not been computed.


Color table

The color table immediately follows the header information. No color table is defined for 24-bit DIBs. The table consists of an array of RGBQUAD data structures. Red, green, and blue bytes are in reverse order (red swaps position with blue) from the Windows convention. The size of the color table depends on the biBitCount value. Here is the declaration of RGBQUAD in VB.

Click here to copy the following block
Type RGBQUAD
  rgbBlue As Byte
  rgbGreen As Byte
  rgbRed As Byte
  rgbReserved As Byte
End Type

Color table size can be calculated using the following formula

Click here to copy the following block
ColorTableSize = nNumColors * Len(RGBQUAD)
Where nNumColors=2^biBitCount

Image Data

Finally the bitmap data itself can be stored in a simple array of bytes. Calculating Image Data Array Size is little tricky because of each scanline of pixels alligned to 32 bit boundary mean for 3x3 8 bit BMP you have to pad extra 1 byte to make 32 bit boundary. So your actual size of array will be (height * Width+ 1 byte) => 3*4 = 12 bytes. 32 bit boundary is easier to maintain because all hardware and CPU use 32 chunk to process CPU instructions.

Some important facts to consider when you work with Bitmap
  • Scanlines are stored upside down, with the first scan (scan 0) in memory being the bottommost scan in the image.
  • 1-bit DIBs are stored using each bit as an index into the color table. The most significant bit is the leftmost pixel.
  • 4-bit DIBs are stored with each 4 bits representing an index into the color table. The most significant nibble is the leftmost pixel.
  • 8-bit DIBs are the easiest to store because each byte is an index.
  • 24-bit DIBs have every 3 bytes representing a color, using the same ordering as the color table. This format is especially tricky during processing because a 64K boundary can exist in the middle of a color triple—an ugly condition that must be handled with care.


Now lets start with our actual class to read, write and process Bitmap data.



**** UNDER CONSTRUCTION (Will be available very soon) ****


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.