follow my heart...
分类: C/C++
2006-09-19 09:12:31
[点评:这里只是介绍用BCB转化彩色图片为灰度图片,由此可引申更多操作,像图像旋转,翻转及各种特效,不过这种算法速度有点慢.]
1\头文件中变量定义:
TColor cl;
Graphics::TBitmap *bitmap1;
Graphics::TBitmap *bitmap2;
long tm;
int thecl[320][240];
mycolor thecolor[320][240];
AnsiString str;
int hd[320][240];
2\初始化变量
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
tm=0;
bitmap2=new Graphics::TBitmap();
bitmap1=new Graphics::TBitmap();
}
3\加载位图
void __fastcall TForm1::ToolButton1Click(TObject *Sender)
{
try
{
bitmap1->LoadFromFile("cat.bmp");
}
catch(...)
{
ShowMessage("error:load file");
}
bitmap2->Assign(bitmap1);
}
4\显示位图
void __fastcall TForm1::ToolButton2Click(TObject *Sender)
{
PaintBox1->Canvas->Draw(0,0,bitmap1);
}
5\将位图的每个像素色值保存在数组中
void __fastcall TForm1::ToolButton3Click(TObject *Sender)
{
Timer1->Enabled=TRUE;
ProgressBar1->Min=0;
ProgressBar1->Max=240*320;
ProgressBar1->Position=0;
for(int i=0;i<240;i++)
{
for(int j=0;j<320;j++)
{
cl=bitmap1->Canvas->Pixels[j][i];
thecolor[j][i].r=GetRValue(cl);
thecolor[j][i].g=GetGValue(cl);
thecolor[j][i].b=GetBValue(cl);
//str.sprintf("r=%d,g=%d,b=%d",thecolor[j][i].r,thecolor[j][i].g,thecolor[j][i].b);
//ListBox1->Items->Add(str);
ProgressBar1->Position++;
}
}
Timer1->Enabled=FALSE;
Label1->Caption=tm;
}
6\将彩图转换为灰度图
void __fastcall TForm1::ToolButton4Click(TObject *Sender)
{
TColor huiduColor;
long sb;