//c++ builder scanLine实现
//在画面上放置TImage控件(Image1)和2个按钮。然后。。。
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit7.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm7 *Form7;
Graphics::TBitmap *bitmap1;
Graphics::TBitmap *bitmap2;
int x,y;
AnsiString name;
//---------------------------------------------------------------------------
__fastcall TForm7::TForm7(TComponent* Owner)
: TForm(Owner)
{
name ="c:\\2.bmp";
}
//---------------------------------------------------------------------------
void __fastcall TForm7::Button1Click(TObject *Sender)
{
bitmap1=new Graphics::TBitmap;
bitmap2=new Graphics::TBitmap;
Image1->Picture->LoadFromFile(name);
bitmap1->LoadFromFile(name);
bitmap2->LoadFromFile(name);
x=bitmap1->Width;
y=bitmap1->Height;
bitmap1->PixelFormat=pf24bit;
bitmap2->PixelFormat=pf24bit;
Image1->Picture->Bitmap->Assign(bitmap1);
}
//---------------------------------------------------------------------------
void __fastcall TForm7::Button3Click(TObject *Sender)
{
Byte *ptr,*newscan;
Byte r,g,b,bgr;
bitmap1->Assign(Image1->Picture->Graphic);
for (int i; i < y; i++)//y为位图的高度
{
ptr=static_cast<Byte*> (bitmap1->ScanLine[i]);//得到这一行像素数组的首地址
newscan=static_cast<Byte*> (bitmap2->ScanLine[i]);//同上
for (int j = 0; j < x; j++)//x为位图的宽度
{
//所谓的三色分离
b=ptr[ j*3];//这一像素的blue分量
g=ptr[ j*3+1];//这一像素的green分量
r=ptr[ j*3+2];//这一像素的red分量
bgr=(b+g+r)/3;//红绿蓝取均值
//以下三分量相等,混合成灰度图
newscan[ j*3]=(Byte)bgr;//
newscan[ j*3+1]=(Byte)bgr;//
newscan[ j*3+2]=(Byte)bgr;//
}
}
Image1->Picture->Bitmap->Assign(bitmap2);
}