Grayscale BMP images-C ++ implementation

Source: Internet
Author: User
Tags bmp image scale image

The structure of a grayscale image consists of four parts: File Header, BMP Information header, color palette, and BMP data content. The gray-scale palette has a total of 256 RGBQUAD structures and stores the gray-scale values ranging from 0 to 255. Each rgbRed, rgbGreen, and rgbBlue has the same component values.


Reference: BMP Image structure, read/write, and grayscale
Grayscale 24-bit BMP Image
The specific steps for converting a 24-bit BMP image into a grayscale image of level 256 are as follows:
(1) modify the information Header
The information header consists of 11 parts, which need to be modified during grayscale
Bi2.biBitCount = 8;
Bi2.biSizeImage = (bi. biWidth + 3)/4) * 4 * bi. biHeight;


(2) modify the file header
There are five parts in the file header. You need to modify the two parts in grayscale mode.
Bf2.bfOffBits = sizeof (bf2) + sizeof (BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD );
Bf2.bfSize = bf2.bfOffBits + bi2.biSizeImage;


(3) create a color palette
RGBQUAD * ipRGB2 = (RGBQUAD *) malloc (256 * sizeof (RGBQUAD ));
For (I = 0; I <256; I ++)
IpRGB2 [I]. rgbRed = ipRGB2 [I]. rgbGreen = ipRGB2 [I]. rgbBlue = I;


(4) modify the bitmap data section
This part mainly consists of the rgbRed, rgbGreen, and rgbBlue components of the original color map to the gray-scale value Y of the gray-scale image,

You can use the following formula to obtain:
Y = 0.299 * rgbRed + 0.587 * rgbGreen + 0.114 * rgbBlue;
The specific modification code is as follows:
Int nBytesPerLine2 = (bi. biWidth + 3)/4) * 4;
NLineStart2 = nBytesPerLine2 * I;
For (int j = 0; j <nBytesPerLine2; j ++)
ImgData2 [nLineStart2 + j] = int (float) Imgdata [I] [3 * j] * 0.114 + \
(Float) Imgdata [I] [3 * j + 1] * 0.587 + \
(Float) Imgdata [I] [3 * j + 2] * 0.299); // uses a one-dimensional array to store the gray value in sequence.


(5) write parts of the BMP image in sequence
Fwrite (& bf2, sizeof (BITMAPFILEHEADER), 1, fp );
Fwrite (& bi2, sizeof (BITMAPINFOHEADER), 1, fp );
Fwrite (ipRGB2, sizeof (RGBQUAD), 256, fp );
Fwrite (ImgData2, nImageSize2, 1, fp );

 

 

Include <iostream> # include <Windows. h> using namespace std; void main () {FILE * stream = fopen ("D: \ 3.bmp", "rb"); if (stream = NULL) {cout <"file does not exist" <endl; return;} int sizeFileHeader = sizeof (BITMAPFILEHEADER); int sizeInfoHeader = sizeof (BITMAPINFOHEADER ); BITMAPFILEHEADER * bitmapFileHeader = new BITMAPFILEHEADER [sizeFileHeader + 1]; BITMAPINFOHEADER * bitmapInfoHeader = new BITMAPINFOHEADER [sizeInfoHeader + 1]; Memset (bitmapFileHeader, 0, sizeFileHeader + 1); memset (bitmapInfoHeader, 0, sizeInfoHeader + 1); fread (bitmapFileHeader, sizeof (char), sizeFileHeader, stream); fseek (stream, sizeFileHeader, 0); fread (bitmapInfoHeader, sizeof (char), sizeInfoHeader, stream); int srcImageLineByteCount = (bitmapInfoHeader-> biWidth * 24) + 31)/32) * 4; int destImageLineByteCount = (bitmapInfoHeader-> biWidth) * 8 + 31)/32) * 4; // ************* bitmap Information header ********************** BYTE ** oldImageData = new BYTE * [bitmapInfoHeader-> biHeight]; for (int I = 0; I <bitmapInfoHeader-> biHeight; I ++) {oldImageData [I] = new BYTE [srcImageLineByteCount + 1]; memset (oldImageData [I], 0, srcImageLineByteCount + 1 );} // ************************************* fseek (stream, sizeFileHeader + sizeInfoHeader, 0); // read image data for (int I = 0; I <bitmapInfoHeader-> biHeight; I ++) {for (int j = 0; j <srcI MageLineByteCount; j ++) {fread (& oldImageData [I] [j], sizeof (BYTE), 1, stream) ;}} fclose (stream ); // palette RGBQUAD * pRgbQuards = new RGBQUAD [256]; for (int I = 0; I <256; I ++) {pRgbQuards [I]. rgbBlue = I; pRgbQuards [I]. rgbRed = I; pRgbQuards [I]. rgbGreen = I;} // modify the information header bitmapInfoHeader-> biBitCount = 8; bitmapInfoHeader-> biSizeImage = (bitmapInfoHeader-> biHeight) * bytes; // modify the file header bitmapFileHeader-> bfOffBits = Sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 256; bitmapFileHeader-> bfSize = bitmapFileHeader-> bfOffBits + bitmapInfoHeader-> biSizeImage; // Write Data BYTE ** newImageData = new BYTE * [bitmapInfoHeader-> biHeight]; for (int I = 0; I <bitmapInfoHeader-> biHeight; I ++) {newImageData [I] = new BYTE [destImageLineByteCount];} for (int I = 0; I <bitmapInfoHeader-> biHeight; I ++) {for (int j = 0; j <destImageLineByte Count; j ++) {newImageData [I] [j] = (int) (float) oldImageData [I] [j * 3] * 0.114 + (float) oldImageData [I] [j * 3 + 1] * 0.587 + (float) oldImageData [I] [3 * j + 2] * 0.299 );}} // write the file # include <iostream> # include <Windows. h> using namespace std; void main () {FILE * stream = fopen ("D: \ 3.bmp", "rb"); if (stream = NULL) {cout <"file does not exist" <endl; return;} int sizeFileHeader = sizeof (BITMAPFILEHEADER); int sizeInfoHeader = sizeof (BITMAPINFOHE ADER); BITMAPFILEHEADER * bitmapFileHeader = new BITMAPFILEHEADER [sizeFileHeader + 1]; BITMAPINFOHEADER * bitmapInfoHeader = new BITMAPINFOHEADER [sizeInfoHeader + 1]; memset (bitmapFileHeader, 0, sizeFileHeader + 1 ); memset (bitmapInfoHeader, 0, sizeInfoHeader + 1); fread (bitmapFileHeader, sizeof (char), sizeFileHeader, stream); fseek (stream, sizeFileHeader, 0); fread (bitmapInfoHeader, sizeof (char), sizeInfoHeader, stream); I Nt srcImageLineByteCount = (bitmapInfoHeader-> biWidth * 24) + 31)/32) * 4; int destImageLineByteCount = (bitmapInfoHeader-> biWidth) * 8 + 31) /32) * 4; // ************* bitmap information header ************************ BYTE ** oldImageData = new BYTE * [bitmapInfoHeader-> biHeight]; for (int I = 0; I <bitmapInfoHeader-> biHeight; I ++) {oldImageData [I] = new BYTE [srcImageLineByteCount + 1]; memset (oldImageData [I], 0, srcImageLineByteCount + 1 );}//****** * **************************** Fseek (stream, sizeFileHeader + sizeInfoHeader, 0); // read image data for (int I = 0; I <bitmapInfoHeader-> biHeight; I ++) {for (int j = 0; j <srcImageLineByteCount; j ++) {fread (& oldImageData [I] [j], sizeof (BYTE), 1, stream) ;}} fclose (stream ); // palette RGBQUAD * pRgbQuards = new RGBQUAD [256]; for (int I = 0; I <256; I ++) {pRgbQuards [I]. rgbBlue = I; pRgbQuards [I]. rgbRed = I; pRgbQuards [I]. rgbGreen = I;} // repair Modify the information header bitmapInfoHeader-> biBitCount = 8; bitmapInfoHeader-> biSizeImage = (bitmapInfoHeader-> biHeight) * watermark; // modify the file header bitmapFileHeader-> bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 256; bitmapFileHeader-> bfSize = bitmapFileHeader-> bfOffBits + bitmapInfoHeader-> biSizeImage; // Write Data BYTE ** newImageData = new BYTE * [bitmapInfoHeader-> biHeight]; for (int I = 0; I <bitmapInf OHeader-> biHeight; I ++) {newImageData [I] = new BYTE [destImageLineByteCount] ;}for (int I = 0; I <bitmapInfoHeader-> biHeight; I ++) {for (int j = 0; j <destImageLineByteCount; j ++) {newImageData [I] [j] = (int) (float) oldImageData [I] [j * 3] * 0.114 + (float) oldImageData [I] [j * 3 + 1] * 0.587 + (float) oldImageData [I] [3 * j + 2] * 0.299);} // write the file [cpp] view plaincopyprint? FILE * fileWrite = fopen ("D :\\ 6.bmp", "a +"); fwrite (bitmapFileHeader, sizeof (char), sizeof (BITMAPFILEHEADER), fileWrite); fwrite (bitmapInfoHeader, sizeof (char), sizeof (BITMAPINFOHEADER), fileWrite); fwrite (pRgbQuards, sizeof (RGBQUAD), 256, fileWrite); for (int I = 0; I <bitmapInfoHeader-> biHeight; I ++) {for (int j = 0; j <destImageLineByteCount; j ++) {fwrite (& newImageData [I] [j], sizeof (BYTE), 1, fileWrite) ;}} fclose (fileWrite); cout <"success" <endl; FILE * fileWrite = fopen ("D: \ 6.bmp ", "a +"); fwrite (bitmapFileHeader, sizeof (char), sizeof (BITMAPFILEHEADER), fileWrite); fwrite (bitmapInfoHeader, sizeof (char), sizeof (BITMAPINFOHEADER), fileWrite ); fwrite (pRgbQuards, sizeof (RGBQUAD), 256, fileWrite); for (int I = 0; I <bitmapInfoHeader-> biHeight; I ++) {for (int j = 0; j <destImageLineByteCount; j ++) {fwrite (& newImageData [I] [j], sizeof (BYTE), 1, fileWrite) ;}} fclose (fileWrite ); cout <"success" <endl ;}

Int srcImageLineByteCount = (bitmapInfoHeader-> biWidth * 24) + 31)/32) * 4;
Int destImageLineByteCount = (bitmapInfoHeader-> biWidth) * 8 + 31)/32) * 4;


Reminder: no pointer is released here...

You can also use the WIDTHBYTES (bitmapInfoHeader-> biWidth * 24) and WIDTHBYTES (bitmapInfoHeader-> biWidth * 8) of the previous article)

In some cases, expressions like (bi. biWidth + 3)/4) * 4 and (bi. biWidth * 3 + 3)/4) * 4 are also used .. The principles are the same. In fact (bi. biWidth + 3)/4) * 4 Write (bi. biWidth * 1 + 3)/4) * 4 will be easy to understand ..

Because each pixel of a BMP image consists of three RGB components (24-bit, 32-bit, that is, multiple Alpha), each pixel in a grayscale image is only a gray value, from code: newImageData [I] [j] = (int) (float) oldImageData [I] [j * 3] * 0.114 + (float) oldImageData [I] [j * 3 + 1] * 0.587 + (float) oldImageData [I] [3 * j + 2] * 0.299); as you can see, each gray value is calculated by a certain formula for each RGB component of the original color image. Therefore, although the gray scale image and the original color image have the same width and height (pixel unit), the number of bytes in each line is different because of their different composition ..

As for the rest, I will not talk much about it. In the previous explanation of the WIDTHBYTES bitmap operation function and the BMP File Read and Write review --- C ++ implementation articles, I have mentioned it almost. If you still don't understand it, leave a message ..

The effect is as follows:

Original Image:

 

 

Grayscale image:


 

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.