Thanks to the correction of imageshop, the Code has been modified, mainly including:
- The 8-Bit Bitmap uses a more efficient full-point operation;
- Remove unnecessary variable detection.
TIPS: if there is an Alpha value in a 32-Bit Bitmap, can the bitmap be directly omitted when it is converted to an 8-bit grayscale bitmap?
In my first MFC small project (3) bitmap conversion, there is a grayscale bitmap that converts a 24-bit color bitmap to an 8-Bit Bitmap, after divergence, you can easily get a 32-Bit Bitmap to an 8-bit grayscale bitmap, as shown in the figure:
32-Bit Bitmap has an extra Alpha byte, which is used to describe the transparency of the image. Based on this feature, we can specifically ignore Alpha, then, follow the subsequent RGB values in accordance with "my first MFC small project (3) in bitmap conversion, the 24-Bit Bitmap to the 8-bit grayscale bitmap method can easily achieve our purpose. I optimized the bitmap conversion interface this time. The following code is provided:
Void convertto8bit (lpwstr failed, lpwstr lpdestfilename) {handle hfile; // file handle DWORD dwwritten; // records the number of bytes written in hfile = createfile (bytes, generic_read, file_cmd_read, null, open_existing, file_flag_sequential_scan, null); bitmapfileheader bmfh; // File Header readfile (hfile, & bmfh, sizeof (bitmapfileheader), & dwwritten, null); bitmapinfoheader bmif; // readfile (hfile, & bmif, 40, & dwwritten, null); DWORD dwsizeimage; // source file pixel size dwsizeimage = // get the pixel size, allocate space bmif. bisizeimage; byte * pbits = new byte [dwsizeimage]; readfile (hfile, pbits, dwsizeimage, & dwwritten, null);: closehandle (hfile); long lsrcwidth = bmif. biwidth; // the length and width of the source image long lsrcheight = bmif. biheight; long llinebytes; // The total number of bytes in each row of the source image long lscanwidth; // The width after converting to an 8-Bit Bitmap, must be a multiple of llinebytes = (lsrcwidth * 4) that is greater than the source image and is 4;/* If (llinebytes <lsrcwidth * 4) // here the conversion needs to be greater than the total number of bytes in each line of the source image llinebytes + = 4; */lscanwidth = (lsrcwidth/4) * 4; // The width of the 8-Bit Bitmap must be a multiple of 4. Here, the conversion must be greater than the original image width if (lscanwidth <lsrcwidth) lsrcwidth + = 4; DWORD dwsizenewimage = lsrcwidth * lsrcheight + 2; // Why do we reserve two places? byte * bits = new byte [dwsizenewimage]; for (INT I = 0; I <lsrcheight; I ++) {for (Int J = 0; j <lsrcwidth; j ++) {byte color [3]; // corresponding to the red, green, and Blue values of RGB DWORD dwcolortemp; // y value. The value after RGB is converted to Y is y = 0.299 * r + 0.587 * g + 0.114 * BFOR (int s = 0; S <3; s ++) // an RGB value corresponds to a Y value color [s] = pbits [I * llinebytes + J * 4 + S + 1]; /* dwcolortemp = unsigned int (color [2] * 0.299 + color [1] * 0.587 + color [0] * 0.114); * // replace it with more efficient computing, indeed much faster dwcolortemp = unsigned int (color [2] * 30 + color [1] * 59 + color [0] * 11 + 50)/100; // excess variable detection/* If (dwcolortemp> 255) dwcolortemp = 255; If (dwcolortemp <0) dwcolortemp = 0; */bits [I * lscanwidth + J] = (unsigned char) dwcolortemp;} bits [dwSizeNewImage-1] = 0; bits [dwSizeNewImage-2] = 0; rgbquad * rgbquad = new rgbquad [256]; // color table for (INT I = 0; I <256; I ++) {rgbquad [I]. rgbblue = I; rgbquad [I]. rgbgreen = I; rgbquad [I]. rgbred = I; rgbquad [I]. rgbreserved = 0;} // complete the 8-Bit Bitmap File Header and information header field bmfh. bfoffbits = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + 256 * sizeof (rgbquad); // The color table contains 256 bmfh. bfreserved1 = 0; bmfh. bfreserved2 = 0; bmfh. bfsize = bmfh. bfoffbits + dwsizenewimage; // size in byte of the filebmif. bibitcount = 8; // change bitcounts in the information header to 8bmif. bisizeimage = dwsizenewimage; // The 8-Bit Bitmap hfile = createfile (lpdestfilename, generic_write, file_into_write, null, create_always, begin, null) obtained after the conversion is written;: writefile (hfile, & bmfh, sizeof (bitmapfileheader), & dwwritten, null);: writefile (hfile, & bmif, sizeof (bitmapinfoheader), & dwwritten, null);: writefile (hfile, rgbquad, 256 * sizeof (rgbquad), & dwwritten, null);: writefile (hfile, bits, dwsizenewimage, & dwwritten, null);: closehandle (hfile );}
Do not change the dressing. In addition, I would like to ask you whether it is best to use a single document or a single document Framework for image processing projects using MFC?
Portal:
Bitmap conversion in my first MFC small project (3)
Troublemakers 2011-12-20
PS: Welcome to the discussion