以下的内容来自刘斌编著的《C++Builder4高级编程实例精解》
如果你使用Delphi编程,代码你改一改就可以了,以下是C++代码。
1)调整对比度的公式:
Color=(Color-128)*s+128;Color是基色的值,s是调整系数,s>1增加对比度,s<1降低对比度,128是三基色最大值255的中值,图像在对比度调整后平均亮度保持不变.
//-----------------------------------------------------------------------------------------
void TForm1:
oContrast(int s)
{
int XX;
BYTE* ptr;
Graphics::TBitmap *tmpBitmap=new Graphics::TBitmap();
tmpBitmap->Assign((TPersistent*)Image->Picture->Graphic);
tmpBitmap->PixelFormat=pf24bit;
for(int y=0;y<tmpBitmap->Height;y++)
{
ptr=(BYTE*)tmpBitmap->ScanLine[y];
for(int x=0;x<tmpBitmap->Width*3;x+=3)
{
XX=((ptr[x+2]-128)*s+12800/100);//注意s的取值范围
XX=MAX(XX,0);
XX=MIN(XX,255);
ptr[x+2]=XX;
XX=((ptr[x+1]-128)*s+12800/100);
XX=MAX(XX,0);
XX=MIN(XX,255);
ptr[x+1]=XX;
XX=((ptr[x]-128)*s+12800/100);
XX=MAX(XX,0);
XX=MIN(XX,255);
ptr[x]=XX;
}
}
Image->Picture->Graphic=tmpBitmap;
delete tmpBitmap;
}
其中:
#define MIN(A,B) ((A<B)?A:B)
#define MAX(A,B) ((A>B)?A:B)
2)调整图像亮度
void TForm1:
oBright(int b)
{
int XX;
BYTE* ptr;
Graphics::TBitmap *tmpBitmap=new Graphics::TBitmap();
tmpBitmap->Assign((TPersistent*)Image->Picture->Graphic);
tmpBitmap->PixelFormat=pf24bit;
for(int y=0;y<tmpBitmap->Height;y++)
{
ptr=(BYTE*)tmpBitmap->ScanLine[y];
for(int x=0;x<tmpBitmap->Width*3;x+=3)
{
XX=ptr[x+2]+b;
XX=MAX(XX,0);
XX=MIN(XX,255);
ptr[x+2]=XX;
XX=ptr[x+1]+b;
XX=MAX(XX,0);
XX=MIN(XX,255);
ptr[x+1]=XX;
XX=ptr[x]+b;
XX=MAX(XX,0);
XX=MIN(XX,255);
ptr[x]=XX;
}
}
Image->Picture->Graphic=tmpBitmap;
delete tmpBitmap;
}
3)色彩平衡
这里用3个TrackBar分别表示红、绿、蓝平衡,范围都是-100~100;红色的用TrackBar1,其余类推,这3个TrackBar都放在Form2上。
void TForm1:
oBalanceButtonCick(TObject* Sender)
{
int XX;
BYTE* ptr;
Graphics::TBitmap *tmpBitmap=new Graphics::TBitmap();
tmpBitmap->Assign((TPersistent*)Image->Picture->Graphic);
tmpBitmap->PixelFormat=pf24bit;
Form2->TrackBar1->Position=0;
Form2->TrackBar2->Position=0;
Form2->TrackBar3->Position=0;
if(Form2->ShowModal()==IDOK)
{
for(int y=0;y<tmpBitmap->Height;y++)
{
ptr=(BYTE*)tmpBitmap->ScanLine[y];
for(int x=0;x<tmpBitmap->Width*3;x+=3)
{
XX=ptr[x+2]+Form2->TrackBar1->Position;
XX=MAX(XX,0);
XX=MIN(XX,255);
ptr[x+2]=XX;
XX=ptr[x+1]+Form2->TrackBar2->Position;
XX=MAX(XX,0);
XX=MIN(XX,255);
ptr[x+1]=XX;
XX=ptr[x]+Form2->TrackBar3->Position;
XX=MAX(XX,0);
XX=MIN(XX,255);
ptr[x]=XX;
}
}
}
4)滤镜方面也有几个,代码较长,你自己找书看吧