以下那个函数是我按书上添加的。
但一直不能实现将RGB转换成灰度图。
望知道的大大们提点一下,不胜感激void HuiDuBianHuanDib::RGBtoGrade()
{
if(GetRGB())
m_pData2 = m_pData;
else
{
BYTE r,g,b;
int height,wide,size;
height = this->GetHeight();
wide = this->GetWidth();
size = height*wide;
m_pData2 = (BYTE*)GlobalAllocPtr(GMEM_MOVEABLE,size);
LONG lLineBytes = GetDibWidthBytes();
for(int j = 0;j<height; j++)
{
for(int i= 0;i<wide;i++)
{
b = m_pData[j*lLineBytes+3*i];
g = m_pData[j*lLineBytes+3*i+1];
r = m_pData[j*lLineBytes+3*i+2];
m_pData2[j*wide+i]=(BYTE)(0.3*r+0.59*g+0.11*b); }
}
}
}
但一直不能实现将RGB转换成灰度图。
望知道的大大们提点一下,不胜感激void HuiDuBianHuanDib::RGBtoGrade()
{
if(GetRGB())
m_pData2 = m_pData;
else
{
BYTE r,g,b;
int height,wide,size;
height = this->GetHeight();
wide = this->GetWidth();
size = height*wide;
m_pData2 = (BYTE*)GlobalAllocPtr(GMEM_MOVEABLE,size);
LONG lLineBytes = GetDibWidthBytes();
for(int j = 0;j<height; j++)
{
for(int i= 0;i<wide;i++)
{
b = m_pData[j*lLineBytes+3*i];
g = m_pData[j*lLineBytes+3*i+1];
r = m_pData[j*lLineBytes+3*i+2];
m_pData2[j*wide+i]=(BYTE)(0.3*r+0.59*g+0.11*b); }
}
}
}
{
if(GetRGB())
m_pData2 = m_pData;
else
{
BYTE r,g,b;
int height,wide,size;
height = this->GetHeight();
wide = this->GetWidth();
size = height*wide;
m_pData2 = (BYTE*)GlobalAllocPtr(GMEM_MOVEABLE,size);
LONG lLineBytes = GetDibWidthBytes();
for(int j = 0;j<height; j++)
{
for(int i= 0;i<wide;i++)
{
b = m_pData[j*lLineBytes+3*i];
g = m_pData[j*lLineBytes+3*i+1];
r = m_pData[j*lLineBytes+3*i+2];m_pData[j*lLineBytes+3*i] = (BYTE)(0.3*r+0.59*g+0.11*b);
m_pData[j*lLineBytes+3*i+1] = (BYTE)(0.3*r+0.59*g+0.11*b);
m_pData[j*lLineBytes+3*i+2] = (BYTE)(0.3*r+0.59*g+0.11*b);}
}
}
}
void MakeBW(CImage* pimgSave)
{
if(pimgSave->IsNull())
return; // the image does not use an indexed palette, so we will change each pixel to B&W (slow)
COLORREF pixel;
int maxY = pimgSave->GetHeight(), maxX = pimgSave->GetWidth();
byte r,g,b,avg;
for (int x=0; x<maxX; x++)
{
for (int y=0; y<maxY; y++)
{
pixel = pimgSave->GetPixel(x,y);
r = GetRValue(pixel);
g = GetGValue(pixel);
b = GetBValue(pixel);
avg = ((r + g + b)/3);
pimgSave->SetPixelRGB(x,y,avg,avg,avg);
}
}
}
该函数的调用方法:
CImage img;
img.Load("a.jpg");
MakeBW(&img);
img.Save("b.bmp");
用CImage 类需要在头文件中包含该文件才可以
#include "atlimage.h"