32-Bit Bitmap to 24-Bit Bitmap Conversion
(1) Functions
In many practical applications of image processing, we need to convert the Color Representation of the image (for example, converting a 32-bit color to a 24-bit color ). This article uses a simple example to illustrate the conversion process from 32-bit color to 24-bit color. The program assumes that the current Windows Desktop color is a 32-bit color value.
(2) Preparations
Create a VC console application and select MFC support
(3) Main Functions
Function BMP 32tobmp 24 converts a 32-Bit Bitmap to a 24-Bit Bitmap format.
Note: This function assumes that the current Windows Desktop color is a 32-bit color value.
// Transform 32-Bit Bitmap format to 24-Bit Bitmap format
Void BMP 32tobmp 24 (char filename [])
{
Char filename2 [] = "output.bmp ";
// Note: If there is no lr_createdibsection, the bitmap color will be mapped to the DC color of the screen.
// That is to say, if the screen is a 16-bit color, all the images will be mapped to a 16-bit color.
Hbitmap hbmp32 = (hbitmap) LoadImage (null, filename,
Image_bitmap, 0, 0,
Lr_loadfromfile |
Lr_createdibsection );
Bitmap BMP; // retrieves bitmap information.
GetObject (hbmp32, sizeof (Bitmap), & BMP );
Printf ("image bit depth: % d/nwidth: % d, height: % d/N ",
BMP. bmbitspixel, BMP. bmwidth, BMP. bmheight); // display the bitmap color mode and image width and height.
// Calculate the number of bytes per line for a 24-bit image
Int bytesperline = 3 * BMP. bmwidth;
While (bytesperline % 4! = 0)
Bytesperline ++;
Bitmapinfoheader BiH = {0}; // Bitmap header
BiH. bibitcount = 24; // the size of each pixel in bytes
BiH. bicompression = bi_rgb;
BiH. biheight = BMP. bmheight; // height
BiH. biplanes = 1;
BiH. bisize = sizeof (bitmapinfoheader );
BiH. bisizeimage = bytesperline * BMP. bmheight; // image data size
BiH. biwidth = BMP. bmwidth; // width
Bitmapfileheader BFH = {0}; // Bitmap File Header
BFH. bfoffbits = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader); // offset of the In-Place graph data
BFH. bfsize = BFH. bfoffbits + BiH. bisizeimage; // total file size
BFH. bftype = (Word) 0x4d42;
File * fp = fopen (filename2, "W + B ");
Fwrite (& BFH, 1, sizeof (bitmapfileheader), FP); // write the bitmap file header
Fwrite (& BiH, 1, sizeof (bitmapinfoheader), FP); // write bitmap information Header
Byte * P = new byte [BiH. bisizeimage];
// Obtain the current 32-bit image data
Getdibits (getdc (null), hbmp32, 0, BMP. bmheight, P, (lpbitmapinfo) & BiH, dib_rgb_colors );
// Only obtain the RGB value and save it to the file
Byte B = 0; // fill
For (INT I = 0; I <BMP. bmwidth * BMP. bmheight; I ++)
{
// The 32-Bit Bitmap format is blue, green, red, and Alpha.
Fwrite (& (P [I * 3]), 1, 3, FP );
If (I % BMP. bmwidth = BMP. bmwidth-1) // fill the byte
{
For (int K = 0; k <(bytesperline-BMP. bmwidth * 3); k ++)
Fwrite (& B, sizeof (byte), 1, FP );
}
}
Delete [] P;
Fclose (FP );
Deleteobject (hbmp32 );
}
(4) other instructions
This program is limited to a fixed screen format. To break this restriction, you must directly operate bitmap files from the underlying layer.