Unit1.cpp
上传用户:lulishicai
上传日期:2010-03-01
资源大小:13202k
文件大小:16k
源码类别:
Delphi/CppBuilder
开发平台:
C++ Builder
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "Unit1.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TfrmMain *frmMain;
- struct rgbp{
- int r;
- int g;
- int b;
- };
- rgbp rgbp[1000][1000];
- int x,y,i,j;
- Graphics::TBitmap * Bitmap;
- //---------------------------------------------------------------------------
- __fastcall TfrmMain::TfrmMain(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::Smooth1Click(TObject *Sender)
- {
- int red,green,blue;
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-2;i++)
- {
- for(j=1;j<y-2;j++)
- {
- red=rgbp[i-1][j-1].r+rgbp[i-1][j].r+rgbp[i-1][j+1].r+
- rgbp[i][j-1].r+rgbp[i][j].r+rgbp[i][j+1].r+
- rgbp[i+1][j-1].r+rgbp[i+1][j].r+rgbp[i+1][j+1].r;
- green= rgbp[i-1][j-1].g+rgbp[i-1][j].g+rgbp[i-1][j+1].g+
- rgbp[i][j-1].g+rgbp[i][j].g+rgbp[i][j+1].g+
- rgbp[i+1][j-1].g+rgbp[i+1][j].g+rgbp[i+1][j+1].g;
- blue=rgbp[i-1][j-1].b+rgbp[i-1][j].b+rgbp[i-1][j+1].b+
- rgbp[i][j-1].b+rgbp[i][j].g+rgbp[i][j+1].g+
- rgbp[i+1][j-1].g+rgbp[i+1][j].g+rgbp[i+1][j+1].g;
- //颜色处理
- Bitmap->Canvas->Pixels[i][j]=RGB(red/9,green/9,blue/9);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- TfrmMain::GetRgbp(TObject * sender)
- {
- //TODO: Add your source code here
- TColor color;
- x=Image1->Picture->Width;
- y=Image1->Picture->Height;
- for(i=0;i<x-1;i++)
- {
- for(j=0;j<y-1;j++)
- {
- color=Image1->Canvas->Pixels[i][j];
- rgbp[i][j].r=GetRValue(color);
- rgbp[i][j].g=GetGValue(color);
- rgbp[i][j].b=GetBValue(color);
- }
- }
- }
- void __fastcall TfrmMain::Sharp1Click(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-2;i++)
- {
- for(j=1;j<y-2;j++)
- {
- red=rgbp[i][j].r+0.5*(rgbp[i][j].r-rgbp[i-1][j-1].r);
- green=rgbp[i][j].g+0.5*(rgbp[i][j].g-rgbp[i-1][j-1].g);
- blue=rgbp[i][j].b+0.5*(rgbp[i][j].b-rgbp[i-1][j-1].b);
- //颜色处理
- if(red>255) red=255;
- if(red<0) red=0;
- if(green>255) green=255;
- if(green<0) green=0;
- if(blue>255) blue=255;
- if(blue<0) blue=0;
- Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::Emboss1Click(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-2;i++)
- {
- for(j=1;j<y-2;j++)
- {
- red=abs(rgbp[i][j].r-rgbp[i+1][j+1].r+128);
- green=abs(rgbp[i][j].g-rgbp[i+1][j+1].g+128);
- blue=abs(rgbp[i][j].b-rgbp[i+1][j+1].b+128);
- //颜色处理
- if(red>255) red=255;
- if(red<0) red=0;
- if(green>255) green=255;
- if(green<0) green=0;
- if(blue>255) blue=255;
- if(blue<0) blue=0;
- Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::Disffuse1Click(TObject *Sender)
- {
- int rx,ry;
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-3;i++)
- {
- for(j=1;j<y-3;j++)
- {
- rx=random(3);
- ry=random(3);
- red=rgbp[i+rx][j+ry].r;
- green=rgbp[i+rx][j+ry].g;
- blue=rgbp[i+rx][j+ry].b;
- //颜色处理
- Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::Invert1Click(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-2;i++)
- {
- for(j=1;j<y-2;j++)
- {
- red=255-rgbp[i][j].r;
- green=255-rgbp[i][j].g;
- blue=255-rgbp[i][j].b;
- //颜色处理
- Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::Mosic1Click(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-2;i+=3)
- {
- for(j=1;j<y-2;j+=3)
- {
- red=rgbp[i-1][j-1].r+rgbp[i-1][j].r+rgbp[i-1][j+1].r+
- rgbp[i][j-1].r+rgbp[i][j].r+rgbp[i][j+1].r+
- rgbp[i+1][j-1].r+rgbp[i+1][j].r+rgbp[i+1][j+1].r;
- green= rgbp[i-1][j-1].g+rgbp[i-1][j].g+rgbp[i-1][j+1].g+
- rgbp[i][j-1].g+rgbp[i][j].g+rgbp[i][j+1].g+
- rgbp[i+1][j-1].g+rgbp[i+1][j].g+rgbp[i+1][j+1].g;
- blue=rgbp[i-1][j-1].b+rgbp[i-1][j].b+rgbp[i-1][j+1].b+
- rgbp[i][j-1].b+rgbp[i][j].g+rgbp[i][j+1].g+
- rgbp[i+1][j-1].g+rgbp[i+1][j].g+rgbp[i+1][j+1].g;
- //颜色处理
- Bitmap->Canvas->Pixels[i-1][j-1]=RGB(red/9,green/9,blue/9);
- Bitmap->Canvas->Pixels[i-1][j]=RGB(red/9,green/9,blue/9);
- Bitmap->Canvas->Pixels[i-1][j+1]=RGB(red/9,green/9,blue/9);
- Bitmap->Canvas->Pixels[i][j-1]=RGB(red/9,green/9,blue/9);
- Bitmap->Canvas->Pixels[i][j]=RGB(red/9,green/9,blue/9);
- Bitmap->Canvas->Pixels[i][j+1]=RGB(red/9,green/9,blue/9);
- Bitmap->Canvas->Pixels[i+1][j-1]=RGB(red/9,green/9,blue/9);
- Bitmap->Canvas->Pixels[i+1][j]=RGB(red/9,green/9,blue/9);
- Bitmap->Canvas->Pixels[i+1][j+1]=RGB(red/9,green/9,blue/9);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::D128Click(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-1;i+=2)
- {
- for(j=1;j<y-1;j+=2)
- {
- red=rgbp[i][j].r;
- green=rgbp[i][j].g;
- blue=rgbp[i][j].b;
- //颜色处理
- Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i][j+1]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+1][j]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+1][j+1]=RGB(red,green,blue);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::D64Click(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-1;i+=4)
- {
- for(j=1;j<y-1;j+=4)
- {
- red=rgbp[i][j].r;
- green=rgbp[i][j].g;
- blue=rgbp[i][j].b;
- //颜色处理
- Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i][j+1]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i][j+2]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i][j+3]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+1][j]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+1][j+1]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+1][j+2]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+1][j+3]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+2][j]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+2][j+1]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+2][j+2]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+2][j+3]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+3][j]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+3][j+1]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+3][j+2]=RGB(red,green,blue);
- Bitmap->Canvas->Pixels[i+3][j+3]=RGB(red,green,blue);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::D32Click(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- int k1,k2;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-1;i+=8)
- {
- for(j=1;j<y-1;j+=8)
- {
- red=rgbp[i][j].r;
- green=rgbp[i][j].g;
- blue=rgbp[i][j].b;
- //颜色处理
- for(k1=0;k1<8;k1++)
- for(k2=0;k2<8;k2++)
- Bitmap->Canvas->Pixels[i+k1][j+k2]=RGB(red,green,blue);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::D16Click(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- int k1,k2;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-1;i+=16)
- {
- for(j=1;j<y-1;j+=16)
- {
- red=rgbp[i][j].r;
- green=rgbp[i][j].g;
- blue=rgbp[i][j].b;
- //颜色处理
- for(k1=0;k1<16;k1++)
- for(k2=0;k2<16;k2++)
- Bitmap->Canvas->Pixels[i+k1][j+k2]=RGB(red,green,blue);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::RedTranClick(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-1;i++)
- {
- for(j=1;j<y-1;j++)
- {
- red=rgbp[i][j].r;
- green=rgbp[i][j].g;
- blue=rgbp[i][j].b;
- //颜色处理
- Bitmap->Canvas->Pixels[i][j]=RGB(red,red,red);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::GreenTranClick(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-1;i++)
- {
- for(j=1;j<y-1;j++)
- {
- red=rgbp[i][j].r;
- green=rgbp[i][j].g;
- blue=rgbp[i][j].b;
- //颜色处理
- Bitmap->Canvas->Pixels[i][j]=RGB(green,green,green);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::BlueTranClick(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-1;i++)
- {
- for(j=1;j<y-1;j++)
- {
- red=rgbp[i][j].r;
- green=rgbp[i][j].g;
- blue=rgbp[i][j].b;
- //颜色处理
- Bitmap->Canvas->Pixels[i][j]=RGB(blue,blue,blue);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::GrayTranClick(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- int GrayColor;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-1;i++)
- {
- for(j=1;j<y-1;j++)
- {
- red=rgbp[i][j].r;
- green=rgbp[i][j].g;
- blue=rgbp[i][j].b;
- GrayColor=(red+green+blue)/3;
- //颜色处理
- Bitmap->Canvas->Pixels[i][j]=RGB(GrayColor,GrayColor,GrayColor);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::LumIncClick(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- int LumColor;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-1;i++)
- {
- for(j=1;j<y-1;j++)
- {
- red=rgbp[i][j].r;
- green=rgbp[i][j].g;
- blue=rgbp[i][j].b;
- red=red+10;
- //颜色处理
- if(red>255) red=255;
- green=green+10;
- if(green>255) green=255;
- blue=blue+10;
- if(blue>255) blue=255;
- Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::LumDecClick(TObject *Sender)
- {
- Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
- int red,green,blue;
- int LumColor;
- GetRgbp(Sender);
- //取得颜色信息
- for(i=1;i<x-1;i++)
- {
- for(j=1;j<y-1;j++)
- {
- red=rgbp[i][j].r;
- green=rgbp[i][j].g;
- blue=rgbp[i][j].b;
- red=red-10;
- //颜色处理
- if(red<0) red=0;
- green=green-10;
- if(green<0) green=0;
- blue=blue-10;
- if(blue<0) blue=0;
- Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
- }
- }
- Image2->Picture->Bitmap->Assign(Bitmap);
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::OpenClick(TObject *Sender)
- {
- AnsiString filename;
- TColor color;
- if(OpenPictureDialog1->Execute())
- {
- filename=OpenPictureDialog1->FileName;
- Image1->Picture->LoadFromFile(filename);
- Image2->Picture->LoadFromFile(filename);
- x=Image1->Picture->Width;
- y=Image1->Picture->Height;
- for(i=0;i<x-1;i++)
- {
- for(j=0;j<y-1;j++)
- {
- color=Image1->Canvas->Pixels[i][j];
- rgbp[i][j].r=GetRValue(color);
- rgbp[i][j].g=GetGValue(color);
- rgbp[i][j].b=GetBValue(color);
- }
- Bitmap=new Graphics::TBitmap;
- Bitmap->Width=Image1->Picture->Width;
- Bitmap->Height=Image1->Picture->Height;
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::SaveClick(TObject *Sender)
- {
- AnsiString filename;
- if(SavePictureDialog1->Execute())
- //显示对话框
- {
- filename=SavePictureDialog1->FileName;
- //获取文件名
- SavePictureDialog1->DefaultExt=".BMP";
- //设置文件缺省扩展名
- Image2->Picture->SaveToFile(filename);
- //存储文件
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::ExitClick(TObject *Sender)
- {
- Close();
- //结束程序的运行
- }