Unit1.cpp
上传用户:lulishicai
上传日期:2010-03-01
资源大小:13202k
文件大小:16k
源码类别:

Delphi/CppBuilder

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Unit1.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma resource "*.dfm"
  8. TfrmMain *frmMain;
  9. struct rgbp{
  10.             int r;
  11.             int g;
  12.             int b;
  13.             };
  14. rgbp rgbp[1000][1000];
  15. int x,y,i,j;
  16. Graphics::TBitmap * Bitmap;
  17. //---------------------------------------------------------------------------
  18. __fastcall TfrmMain::TfrmMain(TComponent* Owner)
  19.         : TForm(Owner)
  20. {
  21. }
  22. //---------------------------------------------------------------------------
  23. void __fastcall TfrmMain::Smooth1Click(TObject *Sender)
  24. {
  25.     int red,green,blue;
  26.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  27.     GetRgbp(Sender);
  28.     //取得颜色信息
  29.     for(i=1;i<x-2;i++)
  30.     {
  31.         for(j=1;j<y-2;j++)
  32.         {
  33.             red=rgbp[i-1][j-1].r+rgbp[i-1][j].r+rgbp[i-1][j+1].r+
  34.                 rgbp[i][j-1].r+rgbp[i][j].r+rgbp[i][j+1].r+
  35.                 rgbp[i+1][j-1].r+rgbp[i+1][j].r+rgbp[i+1][j+1].r;
  36.             green= rgbp[i-1][j-1].g+rgbp[i-1][j].g+rgbp[i-1][j+1].g+
  37.                    rgbp[i][j-1].g+rgbp[i][j].g+rgbp[i][j+1].g+
  38.                    rgbp[i+1][j-1].g+rgbp[i+1][j].g+rgbp[i+1][j+1].g;
  39.             blue=rgbp[i-1][j-1].b+rgbp[i-1][j].b+rgbp[i-1][j+1].b+
  40.                  rgbp[i][j-1].b+rgbp[i][j].g+rgbp[i][j+1].g+
  41.                  rgbp[i+1][j-1].g+rgbp[i+1][j].g+rgbp[i+1][j+1].g;
  42.             //颜色处理
  43.             Bitmap->Canvas->Pixels[i][j]=RGB(red/9,green/9,blue/9);
  44.         }
  45.     }
  46.     Image2->Picture->Bitmap->Assign(Bitmap);
  47. }
  48. //---------------------------------------------------------------------------
  49. TfrmMain::GetRgbp(TObject * sender)
  50. {
  51.     //TODO: Add your source code here
  52. TColor color;
  53. x=Image1->Picture->Width;
  54. y=Image1->Picture->Height;
  55. for(i=0;i<x-1;i++)
  56. {
  57.     for(j=0;j<y-1;j++)
  58.     {
  59.         color=Image1->Canvas->Pixels[i][j];
  60.         rgbp[i][j].r=GetRValue(color);
  61.         rgbp[i][j].g=GetGValue(color);
  62.         rgbp[i][j].b=GetBValue(color);
  63.      }
  64. }
  65. }
  66. void __fastcall TfrmMain::Sharp1Click(TObject *Sender)
  67. {
  68.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  69.     int red,green,blue;
  70.     GetRgbp(Sender);
  71.     //取得颜色信息
  72.     for(i=1;i<x-2;i++)
  73.     {
  74.         for(j=1;j<y-2;j++)
  75.         {
  76.             red=rgbp[i][j].r+0.5*(rgbp[i][j].r-rgbp[i-1][j-1].r);
  77.             green=rgbp[i][j].g+0.5*(rgbp[i][j].g-rgbp[i-1][j-1].g);
  78.             blue=rgbp[i][j].b+0.5*(rgbp[i][j].b-rgbp[i-1][j-1].b);
  79.             //颜色处理
  80.             if(red>255) red=255;
  81.             if(red<0) red=0;
  82.             if(green>255) green=255;
  83.             if(green<0) green=0;
  84.             if(blue>255) blue=255;
  85.             if(blue<0) blue=0;
  86.             Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
  87.         }
  88.     }
  89.     Image2->Picture->Bitmap->Assign(Bitmap);
  90. }
  91. //---------------------------------------------------------------------------
  92. void __fastcall TfrmMain::Emboss1Click(TObject *Sender)
  93. {
  94.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  95.     int red,green,blue;
  96.     GetRgbp(Sender);
  97.     //取得颜色信息
  98.     for(i=1;i<x-2;i++)
  99.     {
  100.         for(j=1;j<y-2;j++)
  101.         {
  102.             red=abs(rgbp[i][j].r-rgbp[i+1][j+1].r+128);
  103.             green=abs(rgbp[i][j].g-rgbp[i+1][j+1].g+128);
  104.             blue=abs(rgbp[i][j].b-rgbp[i+1][j+1].b+128);
  105.             //颜色处理
  106.             if(red>255) red=255;
  107.             if(red<0) red=0;
  108.             if(green>255) green=255;
  109.             if(green<0) green=0;
  110.             if(blue>255) blue=255;
  111.             if(blue<0) blue=0;
  112.             Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
  113.         }
  114.     }
  115.     Image2->Picture->Bitmap->Assign(Bitmap);
  116. }
  117. //---------------------------------------------------------------------------
  118. void __fastcall TfrmMain::Disffuse1Click(TObject *Sender)
  119. {
  120.     int rx,ry;
  121.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  122.     int red,green,blue;
  123.     GetRgbp(Sender);
  124.     //取得颜色信息
  125.     for(i=1;i<x-3;i++)
  126.     {
  127.         for(j=1;j<y-3;j++)
  128.         {
  129.             rx=random(3);
  130.             ry=random(3);
  131.             red=rgbp[i+rx][j+ry].r;
  132.             green=rgbp[i+rx][j+ry].g;
  133.             blue=rgbp[i+rx][j+ry].b;
  134.             //颜色处理
  135.             Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
  136.         }
  137.     }
  138.     Image2->Picture->Bitmap->Assign(Bitmap);
  139. }
  140. //---------------------------------------------------------------------------
  141. void __fastcall TfrmMain::Invert1Click(TObject *Sender)
  142. {
  143.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  144.     int red,green,blue;
  145.     GetRgbp(Sender);
  146.     //取得颜色信息
  147.    for(i=1;i<x-2;i++)
  148.     {
  149.         for(j=1;j<y-2;j++)
  150.         {
  151.             red=255-rgbp[i][j].r;
  152.             green=255-rgbp[i][j].g;
  153.             blue=255-rgbp[i][j].b;
  154.             //颜色处理
  155.             Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
  156.         }
  157.     }
  158.     Image2->Picture->Bitmap->Assign(Bitmap);
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TfrmMain::Mosic1Click(TObject *Sender)
  162. {
  163.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  164.     int red,green,blue;
  165.     GetRgbp(Sender);
  166.     //取得颜色信息
  167.     for(i=1;i<x-2;i+=3)
  168.     {
  169.         for(j=1;j<y-2;j+=3)
  170.         {
  171.             red=rgbp[i-1][j-1].r+rgbp[i-1][j].r+rgbp[i-1][j+1].r+
  172.                 rgbp[i][j-1].r+rgbp[i][j].r+rgbp[i][j+1].r+
  173.                 rgbp[i+1][j-1].r+rgbp[i+1][j].r+rgbp[i+1][j+1].r;
  174.             green= rgbp[i-1][j-1].g+rgbp[i-1][j].g+rgbp[i-1][j+1].g+
  175.                    rgbp[i][j-1].g+rgbp[i][j].g+rgbp[i][j+1].g+
  176.                    rgbp[i+1][j-1].g+rgbp[i+1][j].g+rgbp[i+1][j+1].g;
  177.             blue=rgbp[i-1][j-1].b+rgbp[i-1][j].b+rgbp[i-1][j+1].b+
  178.                  rgbp[i][j-1].b+rgbp[i][j].g+rgbp[i][j+1].g+
  179.                  rgbp[i+1][j-1].g+rgbp[i+1][j].g+rgbp[i+1][j+1].g;
  180.             //颜色处理
  181.             Bitmap->Canvas->Pixels[i-1][j-1]=RGB(red/9,green/9,blue/9);
  182.             Bitmap->Canvas->Pixels[i-1][j]=RGB(red/9,green/9,blue/9);
  183.             Bitmap->Canvas->Pixels[i-1][j+1]=RGB(red/9,green/9,blue/9);
  184.             Bitmap->Canvas->Pixels[i][j-1]=RGB(red/9,green/9,blue/9);
  185.             Bitmap->Canvas->Pixels[i][j]=RGB(red/9,green/9,blue/9);
  186.             Bitmap->Canvas->Pixels[i][j+1]=RGB(red/9,green/9,blue/9);
  187.             Bitmap->Canvas->Pixels[i+1][j-1]=RGB(red/9,green/9,blue/9);
  188.             Bitmap->Canvas->Pixels[i+1][j]=RGB(red/9,green/9,blue/9);
  189.             Bitmap->Canvas->Pixels[i+1][j+1]=RGB(red/9,green/9,blue/9);
  190.         }
  191.     }
  192.     Image2->Picture->Bitmap->Assign(Bitmap);
  193. }
  194. //---------------------------------------------------------------------------
  195. void __fastcall TfrmMain::D128Click(TObject *Sender)
  196. {
  197.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  198.     int red,green,blue;
  199.     GetRgbp(Sender);
  200.     //取得颜色信息
  201.     for(i=1;i<x-1;i+=2)
  202.     {
  203.         for(j=1;j<y-1;j+=2)
  204.         {
  205.             red=rgbp[i][j].r;
  206.             green=rgbp[i][j].g;
  207.             blue=rgbp[i][j].b;
  208.             //颜色处理
  209.             Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
  210.             Bitmap->Canvas->Pixels[i][j+1]=RGB(red,green,blue);
  211.             Bitmap->Canvas->Pixels[i+1][j]=RGB(red,green,blue);
  212.             Bitmap->Canvas->Pixels[i+1][j+1]=RGB(red,green,blue);
  213.         }
  214.     }
  215.     Image2->Picture->Bitmap->Assign(Bitmap);
  216. }
  217. //---------------------------------------------------------------------------
  218. void __fastcall TfrmMain::D64Click(TObject *Sender)
  219. {
  220.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  221.     int red,green,blue;
  222.     GetRgbp(Sender);
  223.     //取得颜色信息
  224.     for(i=1;i<x-1;i+=4)
  225.     {
  226.         for(j=1;j<y-1;j+=4)
  227.         {
  228.             red=rgbp[i][j].r;
  229.             green=rgbp[i][j].g;
  230.             blue=rgbp[i][j].b;
  231.             //颜色处理
  232.             Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
  233.             Bitmap->Canvas->Pixels[i][j+1]=RGB(red,green,blue);
  234.             Bitmap->Canvas->Pixels[i][j+2]=RGB(red,green,blue);
  235.             Bitmap->Canvas->Pixels[i][j+3]=RGB(red,green,blue);
  236.             Bitmap->Canvas->Pixels[i+1][j]=RGB(red,green,blue);
  237.             Bitmap->Canvas->Pixels[i+1][j+1]=RGB(red,green,blue);
  238.             Bitmap->Canvas->Pixels[i+1][j+2]=RGB(red,green,blue);
  239.             Bitmap->Canvas->Pixels[i+1][j+3]=RGB(red,green,blue);
  240.             Bitmap->Canvas->Pixels[i+2][j]=RGB(red,green,blue);
  241.             Bitmap->Canvas->Pixels[i+2][j+1]=RGB(red,green,blue);
  242.             Bitmap->Canvas->Pixels[i+2][j+2]=RGB(red,green,blue);
  243.             Bitmap->Canvas->Pixels[i+2][j+3]=RGB(red,green,blue);
  244.             Bitmap->Canvas->Pixels[i+3][j]=RGB(red,green,blue);
  245.             Bitmap->Canvas->Pixels[i+3][j+1]=RGB(red,green,blue);
  246.             Bitmap->Canvas->Pixels[i+3][j+2]=RGB(red,green,blue);
  247.             Bitmap->Canvas->Pixels[i+3][j+3]=RGB(red,green,blue);
  248.         }
  249.     }
  250.     Image2->Picture->Bitmap->Assign(Bitmap);
  251. }
  252. //---------------------------------------------------------------------------
  253. void __fastcall TfrmMain::D32Click(TObject *Sender)
  254. {
  255.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  256.     int red,green,blue;
  257.     int k1,k2;
  258.     GetRgbp(Sender);
  259.     //取得颜色信息
  260.     for(i=1;i<x-1;i+=8)
  261.     {
  262.         for(j=1;j<y-1;j+=8)
  263.         {
  264.             red=rgbp[i][j].r;
  265.             green=rgbp[i][j].g;
  266.             blue=rgbp[i][j].b;
  267.             //颜色处理
  268.             for(k1=0;k1<8;k1++)
  269.                 for(k2=0;k2<8;k2++)
  270.                     Bitmap->Canvas->Pixels[i+k1][j+k2]=RGB(red,green,blue);
  271.         }
  272.     }
  273.     Image2->Picture->Bitmap->Assign(Bitmap);
  274. }
  275. //---------------------------------------------------------------------------
  276. void __fastcall TfrmMain::D16Click(TObject *Sender)
  277. {
  278.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  279.     int red,green,blue;
  280.     int k1,k2;
  281.     GetRgbp(Sender);
  282.     //取得颜色信息
  283.     for(i=1;i<x-1;i+=16)
  284.     {
  285.         for(j=1;j<y-1;j+=16)
  286.         {
  287.             red=rgbp[i][j].r;
  288.             green=rgbp[i][j].g;
  289.             blue=rgbp[i][j].b;
  290.             //颜色处理
  291.             for(k1=0;k1<16;k1++)
  292.                 for(k2=0;k2<16;k2++)
  293.                     Bitmap->Canvas->Pixels[i+k1][j+k2]=RGB(red,green,blue);
  294.         }
  295.     }
  296.     Image2->Picture->Bitmap->Assign(Bitmap);
  297. }
  298. //---------------------------------------------------------------------------
  299. void __fastcall TfrmMain::RedTranClick(TObject *Sender)
  300. {
  301.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  302.     int red,green,blue;
  303.     GetRgbp(Sender);
  304.     //取得颜色信息
  305.     for(i=1;i<x-1;i++)
  306.     {
  307.         for(j=1;j<y-1;j++)
  308.         {
  309.             red=rgbp[i][j].r;
  310.             green=rgbp[i][j].g;
  311.             blue=rgbp[i][j].b;
  312.             //颜色处理
  313.             Bitmap->Canvas->Pixels[i][j]=RGB(red,red,red);
  314.         }
  315.     }
  316.     Image2->Picture->Bitmap->Assign(Bitmap);
  317. }
  318. //---------------------------------------------------------------------------
  319. void __fastcall TfrmMain::GreenTranClick(TObject *Sender)
  320. {
  321.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  322.     int red,green,blue;
  323.     GetRgbp(Sender);
  324.     //取得颜色信息
  325.     for(i=1;i<x-1;i++)
  326.     {
  327.         for(j=1;j<y-1;j++)
  328.         {
  329.             red=rgbp[i][j].r;
  330.             green=rgbp[i][j].g;
  331.             blue=rgbp[i][j].b;
  332.             //颜色处理
  333.             Bitmap->Canvas->Pixels[i][j]=RGB(green,green,green);
  334.         }
  335.     }
  336.     Image2->Picture->Bitmap->Assign(Bitmap);
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TfrmMain::BlueTranClick(TObject *Sender)
  340. {
  341.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  342.     int red,green,blue;
  343.     GetRgbp(Sender);
  344.     //取得颜色信息
  345.     for(i=1;i<x-1;i++)
  346.     {
  347.         for(j=1;j<y-1;j++)
  348.         {
  349.             red=rgbp[i][j].r;
  350.             green=rgbp[i][j].g;
  351.             blue=rgbp[i][j].b;
  352.             //颜色处理
  353.             Bitmap->Canvas->Pixels[i][j]=RGB(blue,blue,blue);
  354.         }
  355.     }
  356.     Image2->Picture->Bitmap->Assign(Bitmap);
  357. }
  358. //---------------------------------------------------------------------------
  359. void __fastcall TfrmMain::GrayTranClick(TObject *Sender)
  360. {
  361.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  362.     int red,green,blue;
  363.     int GrayColor;
  364.     GetRgbp(Sender);
  365.     //取得颜色信息
  366.     for(i=1;i<x-1;i++)
  367.     {
  368.         for(j=1;j<y-1;j++)
  369.         {
  370.             red=rgbp[i][j].r;
  371.             green=rgbp[i][j].g;
  372.             blue=rgbp[i][j].b;
  373.             GrayColor=(red+green+blue)/3;
  374.             //颜色处理
  375.             Bitmap->Canvas->Pixels[i][j]=RGB(GrayColor,GrayColor,GrayColor);
  376.         }
  377.     }
  378.     Image2->Picture->Bitmap->Assign(Bitmap);
  379. }
  380. //---------------------------------------------------------------------------
  381. void __fastcall TfrmMain::LumIncClick(TObject *Sender)
  382. {
  383.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  384.     int red,green,blue;
  385.     int LumColor;
  386.     GetRgbp(Sender);
  387.     //取得颜色信息
  388.     for(i=1;i<x-1;i++)
  389.     {
  390.         for(j=1;j<y-1;j++)
  391.         {
  392.             red=rgbp[i][j].r;
  393.             green=rgbp[i][j].g;
  394.             blue=rgbp[i][j].b;
  395.             red=red+10;
  396.             //颜色处理
  397.             if(red>255) red=255;
  398.             green=green+10;
  399.             if(green>255) green=255;
  400.             blue=blue+10;
  401.             if(blue>255) blue=255;
  402.             Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
  403.         }
  404.     }
  405.     Image2->Picture->Bitmap->Assign(Bitmap);
  406. }
  407. //---------------------------------------------------------------------------
  408. void __fastcall TfrmMain::LumDecClick(TObject *Sender)
  409. {
  410.     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);
  411.     int red,green,blue;
  412.     int LumColor;
  413.     GetRgbp(Sender);
  414.     //取得颜色信息
  415.     for(i=1;i<x-1;i++)
  416.     {
  417.         for(j=1;j<y-1;j++)
  418.         {
  419.             red=rgbp[i][j].r;
  420.             green=rgbp[i][j].g;
  421.             blue=rgbp[i][j].b;
  422.             red=red-10;
  423.             //颜色处理
  424.             if(red<0) red=0;
  425.             green=green-10;
  426.             if(green<0) green=0;
  427.             blue=blue-10;
  428.             if(blue<0) blue=0;
  429.             Bitmap->Canvas->Pixels[i][j]=RGB(red,green,blue);
  430.         }
  431.     }
  432.     Image2->Picture->Bitmap->Assign(Bitmap);
  433. }
  434. //---------------------------------------------------------------------------
  435. void __fastcall TfrmMain::OpenClick(TObject *Sender)
  436. {
  437.     AnsiString filename;
  438.     TColor color;
  439.     if(OpenPictureDialog1->Execute())
  440.     {
  441.         filename=OpenPictureDialog1->FileName;
  442.         Image1->Picture->LoadFromFile(filename);
  443.         Image2->Picture->LoadFromFile(filename);
  444.         x=Image1->Picture->Width;
  445.         y=Image1->Picture->Height;
  446.         for(i=0;i<x-1;i++)
  447.         {
  448.             for(j=0;j<y-1;j++)
  449.             {
  450.                 color=Image1->Canvas->Pixels[i][j];
  451.                 rgbp[i][j].r=GetRValue(color);
  452.                 rgbp[i][j].g=GetGValue(color);
  453.                 rgbp[i][j].b=GetBValue(color);
  454.              }
  455.              Bitmap=new Graphics::TBitmap;
  456.              Bitmap->Width=Image1->Picture->Width;
  457.              Bitmap->Height=Image1->Picture->Height;
  458.         }
  459.     }
  460. }
  461. //---------------------------------------------------------------------------
  462. void __fastcall TfrmMain::SaveClick(TObject *Sender)
  463. {
  464.     AnsiString filename;
  465.     if(SavePictureDialog1->Execute())
  466.         //显示对话框
  467.         {
  468.             filename=SavePictureDialog1->FileName;
  469.             //获取文件名
  470.             SavePictureDialog1->DefaultExt=".BMP";
  471.             //设置文件缺省扩展名
  472.             Image2->Picture->SaveToFile(filename);
  473.             //存储文件
  474.         }
  475. }
  476. //---------------------------------------------------------------------------
  477. void __fastcall TfrmMain::ExitClick(TObject *Sender)
  478. {
  479.     Close();
  480.     //结束程序的运行
  481. }