Enteritis a few days ago, people have no strength in the past few days. If you have no strength, forget it. So let's take a good rest at home and study. In the past two days, I found that the CDib class I used previously did not provide the image gray function, so I did not think it was quite troublesome to write a suitable project for myself, it took two days to complete the calculation. below is the code. Let's share it with you ~~
/*************************************** *************************
*
* Function name: Convert256toGray (LPSTR lpDIB, LPSTR lpDIBBits,
Long lWidth, long lHeight)
* Parameter: LPSTR lpDIB pointer to dib
LPSTR lpDIBBits pointer to dib data
Long lWidth Image Width
Long lHeight Image Height
* Return value: bool
* Function: converts a 256-color bitmap to a grayscale image.
*
**************************************** ***********************/
Bool Convert256toGray (LPSTR lpDIB, LPSTR lpDIBBits,
Long lWidth, long lHeight)
{
BYTE * lpSrc; // pointer to the DIB Pixel
LONG lLineBytes; // number of bytes in each row of the image
LPBITMAPINFO lpbmi; // pointer to the BITMAPINFO structure (Win3.0)
LPBITMAPCOREINFO lpmn; // pointer to the BITMAPCOREINFO Structure
Lpbmi = (LPBITMAPINFO) lpDIB; // obtain the pointer to the BITMAPINFO structure (Win3.0)
Lplt = (LPBITMAPCOREINFO) lpDIB; // gets a pointer to the BITMAPCOREINFO Structure
BYTE bMap [256]; // grayscale ing table
// Calculate the grayscale ing table (Save the gray value of each color) and update the DIB palette
Int I, j;
For (I = 0; I <256; I ++)
{
// Calculate the gray value corresponding to the color
/*
BMap [I] = (BYTE) (0.299 * lpbmi-> bmiColors [I]. rgbRed +
0.587 * lpbmi-> bmiColors [I]. rgbGreen +
0.114 * lpbmi-> bmiColors [I]. rgbBlue + 0.5 );
*/
BMap [I] = (BYTE) (306 * lpbmi-> bmiColors [I]. rgbRed +
601 * lpbmi-> bmiColors [I]. rgbGreen +
117 * lpbmi-> bmiColors [I]. rgbBlue + 512)> 10); // The modified Optimization Algorithm
// Update the red weight of the DIB palette
Lpbmi-> bmiColors [I]. rgbRed = I;
// Update the DIB color palette green component
Lpbmi-> bmiColors [I]. rgbGreen = I;
// Update the blue component of the DIB palette
Lpbmi-> bmiColors [I]. rgbBlue = I;
// Update the reserved position of the DIB palette
Lpbmi-> bmiColors [I]. rgbReserved = 0;
}
// Calculate the number of bytes per line of the image
LLineBytes = WIDTHBYTES (lWidth * 8 );
// Change the color index of each pixel (that is, change the color index to the gray value according to the gray ing table)
// Scan row by row
For (I = 0; I <lHeight; I ++)
{
// Scan by Column
For (j = 0; j <lWidth; j ++)
{
// Pointer to row I and pixel j of DIB
LpSrc = (unsigned char *) lpDIBBits
+ LLineBytes * (lHeight-1-I) + j;
// Transform
* LpSrc = bMap [* lpSrc];
}
}
Return true;
}
/*************************************** **********************************
* Function name: Gray24Bits (LPSTR lpDIB)
* Function type: HGLOBAL
* Parameter: LPSTR lpDIB pointer to dib
* Function: grayscale 24-bit true color image
**************************************** *********************************/
HGLOBAL Gray24Bits (LPSTR lpDIB)
{
LPBITMAPINFOHEADER lpDIBHdr; // pointer to BITMAPINFOHEADER
LpDIBHdr = (LPBITMAPINFOHEADER) lpDIB;
LPSTR lpGray;
// Calculate the information header, palette, and image data size of the bitmap, and allocate memory to the grayscale image.
Int dwinfo = lpdibhdr-> bisize;
Int dwdata = lpdibhdr-> bisizeimage;
Int dwgraydata = dwdata/3;
Int dwgraypal = 256 * sizeof (rgbquad );
Dwdata = (lpdibhdr-> biheight) * (lpdibhdr-> biwidth );
Int sizetotal = dwinfo + dwgraypal + dwdata; // grayscale chart. The color table length is 255.
Hglobal hgray = (hglobal): globalalloc (gmem_moveable | gmem_zeroinit, sizetotal );
If (hgray = 0)
{
Return false; // if the memory allocation fails, false is returned.
}
Lpgray = (lpstr): globallock (hgray );
// Create a grayscale chart and calculate the gray value of each pixel, that is, calculate the average value.
Rgbquad * lpgrayrgbquag = (rgbquad *) (lpgray + lpdibhdr-> bisize );
Char * lpbits = lpdib + lpdibhdr-> bisize;
Int rowlensr = widthbytes (24 * lpdibhdr-> biwidth );
Int rowlendes = widthbytes (8 * lpdibhdr-> biwidth );
Byte * lpgraybits = (byte *) (lpgrayrgbquag) + dwgraypal;
Int aver = 0;
Int I, J, K;
If (24 = lpdibhdr-> bibitcount)
{
For (I = 0; I <256; I ++)
{
Lpgrayrgbquag-> rgbblue = I;
Lpgrayrgbquag-> rgbgreen = I;
Lpgrayrgbquag-> rgbred = I;
Lpgrayrgbquag-> rgbreserved = 0;
LpGrayRgbQuag ++;
}
For (I = 0; I <lpDIBHdr-> biHeight; I ++)
{
For (j = 0; j <lpDIBHdr-> biWidth; j ++)
{
K = I * rowLenSr + 3 * j;
/*
LpGrayBits [I * rowLenDes + j] = (BYTE) (0.114 * lpBits [k]
+ 0.587 * lpBits [k + 1]
+ 0.299 * lpBits [k + 2] + 0.5 );
*/
LpGrayBits [I * rowLenDes + j] = (BYTE) (117 * lpBits [k]
+ 601 * lpBits [k + 1]
+ 306 * lpBits [k + 2] + 512)> 10); // The modified optimization algorithm.
}
// Modify the pixel line to be zero.
For (j = lpDIBHdr-> biWidth; j <rowLenDes; j ++)
{
Lpgraybits [I * rowlendes + J] = 0;
}
}
}
// Create the grayscale information Header
Lpbitmapinfoheader lpgrayhdr = (lpbitmapinfoheader) lpgray;
Memcpy (lpgrayhdr, lpdibhdr, dwinfo );
Lpgrayhdr-> bisizeimage = sizetotal;
Lpgrayhdr-> bibitcount = 8;
Lpgrayhdr-> maid = 256;
: Globalunlock (hgray );
Return (hglobal) hgray;
}