Saving 8-bit grayscale image BMP

Source: Internet
Author: User
Tags bmp image color representation

In image processing, we often need to convert real-color images into black and white images. Strictly speaking, it should be a grayscale image, because the real black and white images are only pure black and pure white. Before getting started, let's briefly add the image representation principles in the computer. Images in computers can be roughly divided into bitmap and Metafile ). Bitmap can be regarded as a two-dimensional grid. The entire image is composed of multiple points. The number of points equals to the width of the bitmap multiplied by the height. Each vertex is called a pixel. Each pixel has a definite color. When many elements are combined, a complete image is formed. Most of the images we usually use are bitmaps, such as those taken by a digital camera. Because the bitmap can perfectly represent the details of the image, it can better restore the original scene of the image. However, bitmap also has disadvantages: first, it is relatively large, so many compressed image formats have been developed to store bitmap images. Currently, the most widely used format is JPEG, which has been widely used on the web, in addition, there are GIF, PNG, and so on. The second is that when bitmap is enlarged, it is inevitable that the bitmap will appear "sawtooth", which is also determined by the essential characteristics of the bitmap. Therefore, in reality, we also need to use another image format: vector image. Different from bitmap, a vector chart is different from a bitmap. A vector chart is drawn using mathematical formulas through circles, line segments, and so on. Therefore, no deformation occurs during amplification, however, vector images cannot describe very complex images. Vector graphs are used to describe graphic patterns, and various CAD software are used to save files in vector format.

Before explaining the color conversion, we need to first understand the Color Representation of Bitmap. In bitmap, color is usually represented in RGB three colors (the color palette is used when there are few digits ). Therefore, each pixel uses different digits to indicate different numbers of colors. As shown in:

Number of digits per pixel

Number of colors that a pixel can allocate

1

2 ^ 1 = 2

2

2 ^ 2 = 4

4

2 ^ 4 = 16

8

2 ^ 8 = 256

16

2 ^ 16 = 65,536

24

2 ^ 24 = 16,777,216

We can see that when we use a 24-bit color (3 bytes), we can get more than 16 million colors, which is already very rich and should be close to what the human eye can tell. Currently, the most commonly used color in the computer is the 24-bit color. Do not use the external GDI + with a 32-bit color. The extra channel is used to describe Alpha, that is, the transparent component.

The three bytes in the 24-bit color are used to describe the R, G, and B color components respectively. We can see that there is no brightness component, because in the RGB representation, brightness can also be obtained directly from the color component. The range of each color component value ranges from 0 to 255. The greater the value of a color component, it indicates that the brightness of this component is higher, so 255 indicates the brightest, and 0 indicates the lowest. So what is the brightness value of a Real-Color Pixel when it is converted to a grayscale image? The average value we first think of is R + G + B/3. In reality, we use the following formula:

Y = 0.299r + 0.587G + 0.114b

This formula is generally referred to as the grayscale formula of psychology. Here we can see that the green component accounts for the largest proportion. Scientists found that the grayscale image obtained by using the above formula is the closest to the human's perception of the grayscale image.

Because there are only 256 colors (one byte) in a grayscale image, the converted image is usually saved in the 8-bit format instead of the 24-bit format, which saves space. The 8-Bit Bitmap uses the color palette to save the color. Instead of directly saving the color value. 256 colors can be saved in the color palette, so 256 gray colors can be saved to the color palette. The following is a private example:

Bitmapfileheader targetfileheader;
Bitmapinfoheader targetinfoheader;
Memset (& targetfileheader, 0, sizeof (bitmapfileheader ));
Memset (& targetinfoheader, 0, sizeof (bitmapinfoheader ));

// Create a grayscale image file header
Targetfileheader. bfoffbits = (DWORD) sizeof (bitmapfileheader) + (DWORD) sizeof (bitmapinfoheader) + sizeof (rgbquad) * 256;
Targetfileheader. bfsize = 192*192 + sizeof (rgbquad) * 256 + sizeof (bitmapfileheader) + sizeof (bitmapinfoheader );
Targetfileheader. bfreserved1 = 0;
Targetfileheader. bfreserved2 = 0;
Targetfileheader. bftype = 0x4d42;

// Construct the grayscale information Header
Targetinfoheader. bibitcount = 8;
Targetinfoheader. bisize = sizeof (bitmapinfoheader );
Targetinfoheader. biheight = 192;
Targetinfoheader. biwidth = 192;
Targetinfoheader. biplanes = 1;
Targetinfoheader. bicompression = bi_rgb;
Targetinfoheader. bisizeimage = 0;
Targetinfoheader. bixpelspermeter = 0;
Targetinfoheader. biypelspermeter = 0;
Targetinfoheader. biclrimportant = 0;
Targetinfoheader. biclrused = 0;

// Construct the color palette of the grayscale image

Rgbquad [256];
Int I;
For (I = 0; I <256; I ++)
{
Rgbquad [I]. rgbblue = I;
Rgbquad [I]. rgbgreen = I;
Rgbquad [I]. rgbred = I;
Rgbquad [I]. rgbreserved = 0;
}
Byte * targetbuf;
Targetbuf = new byte [192*192];
// Because the BMP image is inverted, that is, the first line of the image is the last line of data, so we need to put it upside down. The pcutface here is already /// grayscale image.
For (long I = 191; I> = 0; I --)
{
For (long J = 0; j <192; j ++)
{
Targetbuf [I * 192 + J] = pcutface [(191-i) * 192 + J];
}
}
Cfile CF;

If (! Cf. Open (lpctstr ("F: \ fire. BMP"), cfile: modecreate | cfile: modewrite ))
Return;
Cf. Write (& targetfileheader, sizeof (bitmapfileheader ));
Cf. Write (& targetinfoheader, sizeof (bitmapinfoheader ));
Cf. Write (& rgbquad, sizeof (rgbquad) * 256 );
Cf. Write (targetbuf, 192*192); // The targetbuf size is 192x192.
Cf. Close ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.