How to realize BMP Image processing with C language
This article mainly introduces the C language how to achieve BMP image processing, the article is very detailed, has a certain reference value, interested friends must read it!
We know that the true color picture does not have a color palette, and each pixel uses 3 bytes to represent R, G, and B components. So the treatment is very simple, after calculating the Y value according to the value of R, G and B, the R, G and B values are all assigned to Y and can be written into the new graph. In the color representation method of YUV, the physical meaning of Y component is brightness, which contains all the information of grayscale image (grayscale). Only Y component can be used to represent a grayscale image. There is a correspondence between YUV and RGB as follows:
Let's take a look at the color map with a palette. we know that the data in the bitmap is only an index value in the corresponding palette. we only need to change the color in the palette into grayscale to form a new palette, while the bitmap data does not need to be moved.
The above explanation comes from: "introduction to digital image processing programming", code reference: C language to achieve 24-bit color image binarization
# include#include int main (int argc, char* argv []) {int bmpHeight; int bmpWidth; unsigned char* pBmpBuf; RGBQUAD * pColorTable; int biBitCount; / / read bmp file FILE * fp = fopen (". / 02.bmp", "rb"); if (fp = = 0) return 0; fseek (fp, sizeof (BITMAPFILEHEADER), 0); BITMAPINFOHEADER head; fread (& head, 40, 1, fp); bmpHeight = head.biHeight; bmpWidth = head.biWidth; biBitCount = head.biBitCount; fseek (fp, sizeof (RGBQUAD), 1) Int LineByte = (bmpWidth*biBitCount / 8 + 3) / 4 * 4 Bytes / make sure that the number of bytes per line is 4 pBmpBuf = new unsigned char [LineByte*bmpHeight]; fread (pBmpBuf, LineByte*bmpHeight, 1, fp); fclose (fp); / / Grey 24-bit true color images and save FILE * fp1 = fopen ("gray.bmp", "wb"); if (fp1 = 0) return 0; int LineByte1 = (bmpWidth* 8 / 8 + 3) / 4 * 4 / / modify the file header, including two items that need to be modified: bfSize and bfOffBits BITMAPFILEHEADER bfhead; bfhead.bfType = 0x4D42; bfhead.bfSize = 14 + 40 + 256 * sizeof (RGBQUAD) + LineByte1*bmpHeight;// modify file size bfhead.bfReserved1 = 0; bfhead.bfReserved2 = 0; bfhead.bfOffBits = 14 + 40 + 256 * sizeof (RGBQUAD); / / modify offset bytes fwrite (& bfhead, 14,1, fp1); / / save the modified file header in fp1 / / modify the information header, including two items that need to be modified, 1 bit biBitCount: true color image is 24, should be changed to 8 The other is biSizeImage: due to the change in the number of bits per pixel, the size of bitmap data changes BITMAPINFOHEADER head1; head1.biBitCount = 8; / / change the number of bits per pixel to 8 head1.biClrImportant = 0; head1.biCompression = 0; head1.biClrUsed = 0; head1.biHeight = bmpHeight; head1.biWidth = bmpWidth; head1.biPlanes = 1; head1.biSize = 40; head1.biSizeImage = LineByte1*bmpHeight;// modify the size of bitmap data head1.biXPelsPerMeter = 0; head1.biYPelsPerMeter = 0 Fwrite (& head1, 40,1, fp1); / / store the modified header in fp1; pColorTable = new RGBQUAD [256]; for (int I = 0; I < 256; iBRed +) {pColorTable [I] .rgbRed = I; pColorTable [I] .rgbGreen = I; pColorTable [I] .rgbBlue = I; / the B, G, R components in the color table are all equal, and equal to the index value} fwrite (pColorTable, sizeof (RGBQUAD), 256, fp1) / / write the color table to fp1; / / write bitmap data unsigned char * pBmpBuf1; pBmpBuf1 = new unsigned char [LineByte1*bmpHeight]; for (int I = 0; I < bmpHeight; iTunes +) {for (int j = 0; j)