www.pudn.com > newnew.rar > Unit1.cpp


//--------------------------------------------------------------------------- 
#include 
#include  
#pragma hdrstop 
#include  
#include "Unit1.h" 
//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
#pragma resource "*.dfm" 
TForm1 *Form1; 
//--------------------------------------------------------------------------- 
__fastcall TForm1::TForm1(TComponent* Owner) 
        : TForm(Owner) 
{ 
} 
//------------------------------ 
void __fastcall TForm1::Open1Click(TObject *Sender) 
{ 
//AnsiString str=AnsiLowerCase(ExtractFileExt(OpenPictureDialog1->FileName)); 
if(OpenPictureDialog1->Execute()) 
{ 
   AnsiString str=AnsiLowerCase(ExtractFileExt(OpenPictureDialog1->FileName)); 
   if(str==".bmp") 
   { 
   Image1->Picture->Bitmap->LoadFromFile(OpenPictureDialog1->FileName); 
   Image1->Width=Image1->Picture->Bitmap->Width; 
   Image1->Height=Image1->Picture->Bitmap->Height; 
   } 
   else if(str==".jpg"||str==".jpeg") 
   { 
     TJPEGImage *Image; 
     Image=new TJPEGImage; 
     Image->LoadFromFile(OpenPictureDialog1->FileName); 
     Image1->Picture->Bitmap->Assign(Image); 
     delete Image; 
   } 
} 
int width,height,i,j; 
TColor color; 
width=Image1->Width; 
height=Image1->Height; 
rgb=new RGBColor*[width]; 
for(i=0;iCanvas->Pixels[i][j]; 
       rgb[i][j].r=GetRValue(color); 
       rgb[i][j].g=GetGValue(color); 
       rgb[i][j].b=GetBValue(color); 
       } 
   } 
draw=false; 
mousedown=false; 
Label2->Caption="image"; 
Label1->Caption="Processing"; 
pic2=new Graphics::TBitmap; 
pic2->Width=width; 
pic2->Height=height; 
pic2->Assign(Image1->Picture->Bitmap); 
pic3=new Graphics::TBitmap; 
pic3->Width=width; 
pic3->Height=height; 
pic3->Assign(Image1->Picture->Bitmap); 
pic4=new Graphics::TBitmap; 
pic4->Width=width; 
pic4->Height=height; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::SaveAs1Click(TObject *Sender) 
{ 
if (SavePictureDialog1->Execute()) 
if (SavePictureDialog1->FilterIndex==1) 
Image1->Picture->SaveToFile(SavePictureDialog1->FileName+".jpg"); 
else 
Image1->Picture->SaveToFile(SavePictureDialog1->FileName+".bmp"); 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::SobelHorizontal1Click(TObject *Sender) 
{ 
int width,height; 
width=Image1->Width; 
height=Image1->Height; 
Graphics::TBitmap *pic; 
pic=new Graphics::TBitmap; 
pic->Width=width; 
pic->Height=height; 
int R,G,B,i,j; 
for (i=0;i255)?255:R; 
      B=(B>255)?255:B; 
      G=(G>255)?255:G; 
      } 
     pic->Canvas->Pixels[i][j]=TColor(RGB(R,G,B)); 
    } 
   } 
   Label1->Caption="Sobel Horizontal"; 
   Image2->Picture->Bitmap->Assign(pic); 
   delete pic; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::Sobel1Click(TObject *Sender) 
{ 
int width,height; 
width=Image1->Width; 
height=Image1->Height; 
Graphics::TBitmap *pic; 
pic=new Graphics::TBitmap; 
pic->Width=width; 
pic->Height=height; 
int R1,G1,B1,R2,G2,B2,i,j,R,G,B; 
for (i=0;iR2)?R1:R2; 
      G=(G1>G2)?G1:G2; 
      B=(B1>B2)?B1:B2; 
      R=(R>255)?255:R; 
      B=(B>255)?255:B; 
      G=(G>255)?255:G; 
      } 
     pic->Canvas->Pixels[i][j]=TColor(RGB(R,G,B)); 
    } 
   } 
   Label1->Caption="Sobel"; 
   Image2->Picture->Bitmap->Assign(pic); 
   delete pic; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::Prewitt1Click(TObject *Sender) 
{ 
int width,height; 
width=Image1->Width; 
height=Image1->Height; 
Graphics::TBitmap *pic; 
pic=new Graphics::TBitmap; 
pic->Width=width; 
pic->Height=height; 
int R1,G1,B1,R2,G2,B2,i,j,R,G,B; 
for (i=0;iR2)?R1:R2; 
      G=(G1>G2)?G1:G2; 
      B=(B1>B2)?B1:B2; 
      R=(R>255)?255:R; 
      B=(B>255)?255:B; 
      G=(G>255)?255:G; 
      } 
     pic->Canvas->Pixels[i][j]=TColor(RGB(R,G,B)); 
    } 
   } 
   Label1->Caption="Prewitt"; 
   Image2->Picture->Bitmap->Assign(pic); 
   delete pic; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::RGB2Gray1Click(TObject *Sender) 
{ 
int width,height,i,j; 
width=Image1->Width; 
height=Image1->Height; 
Graphics::TBitmap *pic; 
pic=new Graphics::TBitmap; 
pic->Width=width; 
pic->Height=height; 
Byte gray; 
for (i=0;iCanvas->Pixels[i][j]=TColor(RGB(gray,gray,gray)); 
   } 
 } 
Label2->Caption="Gray Image"; 
Image1->Picture->Bitmap->Assign(pic); 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::ZoomIn2Click(TObject *Sender) 
{ 
Label1->Caption="50%"; 
int width,height,i,j; 
width=(Image1->Width)/2; 
height=(Image1->Height)/2; 
Graphics::TBitmap *pic; 
pic=new Graphics::TBitmap; 
pic->Width=width; 
pic->Height=height; 
for(i=0;iCanvas->Pixels[i][j]=TColor(RGB(rgb[2*i][2*j].r,rgb[2*i][2*j].g,rgb[2*i][2*j].b)); 
   } 
 } 
 Image2->Picture->Bitmap->Assign(pic); 
 delete pic; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::N2001Click(TObject *Sender) 
{ 
Label1->Caption="200%"; 
int width,height,i,j,k,l; 
Byte lr,lg,lb; 
width=2*(Image1->Width)-1; 
height=2*(Image1->Height)-1; 
Graphics::TBitmap* pic1; 
pic1=new Graphics::TBitmap; 
pic1->Width=width; 
pic1->Height=height; 
for(i=0;iCanvas->Pixels[i][j]=TColor(RGB(lr,lg,lb)); 
    } 
 } 
 Image2->Picture->Bitmap->Assign(pic1); 
 delete pic1; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::ZoomIn1Click(TObject *Sender) 
{ 
Image2->Picture->Bitmap=Image1->Picture->Bitmap; 
Label1->Caption="100%"; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::N1501Click(TObject *Sender) 
{ 
Label1->Caption="150%"; 
int width,height; 
width=(Image1->Width)*1.5-1; 
height=(Image1->Height)*1.5-1; 
Graphics::TBitmap *pic; 
pic=new Graphics::TBitmap; 
pic->Width=width; 
pic->Height=height; 
int i,j,k11,l11,k22,l22; 
float lr,lg,lb; 
float k,l; 
for (i=0;iCanvas->Pixels[i][j]=TColor(RGB(lr,lg,lb)); 
     } 
   } 
  Image2->Picture->Bitmap->Assign(pic); 
  delete pic; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::N751Click(TObject *Sender) 
{ 
Label1->Caption="75%"; 
int width,height; 
#define idd 0.75 
float id=idd; 
if (id<=1) 
{width=(Image1->Width)*id; 
 height=(Image1->Height)*id; 
 } 
else 
{width=(Image1->Width)*id-1; 
 height=(Image1->Height)*id-1; 
 } 
Graphics::TBitmap *pic; 
pic=new Graphics::TBitmap; 
pic->Width=width; 
pic->Height=height; 
int i,j,k11,l11,k22,l22; 
float lr,lg,lb; 
float k,l; 
for (i=0;iCanvas->Pixels[i][j]=TColor(RGB(lr,lg,lb)); 
     } 
   } 
  Image2->Picture->Bitmap->Assign(pic); 
  delete pic; 
} 
 
 
//--------------------------------------------------------------------------- 
 
 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::N251Click(TObject *Sender) 
{ 
Label1->Caption="25%"; 
int width,height; 
#define id2 0.25 
float id=id2; 
if (id<=1) 
{width=(Image1->Width)*id; 
 height=(Image1->Height)*id; 
 } 
else 
{width=(Image1->Width)*id-1; 
 height=(Image1->Height)*id-1; 
 } 
Graphics::TBitmap *pic; 
pic=new Graphics::TBitmap; 
pic->Width=width; 
pic->Height=height; 
int i,j,k11,l11,k22,l22; 
float lr,lg,lb; 
float k,l; 
for (i=0;iCanvas->Pixels[i][j]=TColor(RGB(lr,lg,lb)); 
     } 
   } 
  Image2->Picture->Bitmap->Assign(pic); 
  delete pic; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::N1251Click(TObject *Sender) 
{ 
Label1->Caption="125%"; 
int width,height; 
#define id1 1.25 
float id=id1; 
if (id<=1) 
{width=(Image1->Width)*id; 
 height=(Image1->Height)*id; 
 } 
else 
{width=(Image1->Width)*id-1; 
 height=(Image1->Height)*id-1; 
 } 
Graphics::TBitmap *pic; 
pic=new Graphics::TBitmap; 
pic->Width=width; 
pic->Height=height; 
int i,j,k11,l11,k22,l22; 
float lr,lg,lb; 
float k,l; 
for (i=0;iCanvas->Pixels[i][j]=TColor(RGB(lr,lg,lb)); 
     } 
   } 
  Image2->Picture->Bitmap->Assign(pic); 
  delete pic; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::N1751Click(TObject *Sender) 
{ 
Label1->Caption="175%"; 
int width,height; 
#define id3 1.75 
float id=id3; 
if (id<=1) 
{width=(Image1->Width)*id; 
 height=(Image1->Height)*id; 
 } 
else 
{width=(Image1->Width)*id-1; 
 height=(Image1->Height)*id-1; 
 } 
Graphics::TBitmap *pic; 
pic=new Graphics::TBitmap; 
pic->Width=width; 
pic->Height=height; 
int i,j,k11,l11,k22,l22; 
float lr,lg,lb; 
float k,l; 
for (i=0;iCanvas->Pixels[i][j]=TColor(RGB(lr,lg,lb)); 
     } 
   } 
  Image2->Picture->Bitmap->Assign(pic); 
  delete pic; 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 
draw=true; 
Image1->Cursor=crCross; 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::Image1MouseDown(TObject *Sender, 
      TMouseButton Button, TShiftState Shift, int X, int Y) 
{ 
 
if(draw==true&&Button==mbLeft) 
 { 
 posx=X; 
 posy=Y; 
 mousedown=true; 
 } 
else if (draw==false&&Button==mbLeft) 
 { 
  if (RadioButton1->Checked==true) 
  Shape1->Brush->Color=Image1->Canvas->Pixels[X][Y]; 
  else if (RadioButton2->Checked==true) 
  Shape2->Brush->Color=Image1->Canvas->Pixels[X][Y]; 
 } 
else if (draw==true&&Button==mbRight) 
 { 
  draw=false; 
  } 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift, 
      int X, int Y) 
{ 
int w,h; 
w=Image1->Width; 
h=Image1->Height; 
if(mousedown&&draw) 
 { pic2->Canvas->Pen->Style=psDashDot; 
   pic2->Canvas->MoveTo(posx,posy); 
   pic2->Canvas->LineTo(posx,Y); 
   pic2->Canvas->LineTo(X,Y); 
   pic2->Canvas->LineTo(X,posy); 
   pic2->Canvas->LineTo(posx,posy); 
   pic4->Canvas->CopyRect(Rect(0,0,w,h),pic2->Canvas,Rect(0,0,w,h)); 
   pic2->Canvas->CopyRect(Rect(0,0,w,h),pic3->Canvas,Rect(0,0,w,h)); 
   Image1->Canvas->Draw(0,0,pic4); 
 
   } 
 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::Button2Click(TObject *Sender) 
{ 
 
Graphics::TBitmap *pic; 
pic=new Graphics::TBitmap; 
pic->Width=kuang; 
pic->Height=gao; 
int transx,transy; 
if(posx>finalx) 
{ 
 transx=posx; 
 posx=finalx; 
 finalx=transx; 
 } 
if(posy>finaly) 
{ 
 transy=posy; 
 posy=finaly; 
 finaly=transy; 
 } 
pic->Canvas->CopyRect(Rect(0,0,kuang,gao),pic3->Canvas,Rect(posx,posy,finalx,finaly)); 
Image2->Picture->Bitmap->Assign(pic); 
Image1->Picture->Assign(pic3); 
Image1->Cursor=crDefault  ; 
Button2->Enabled=false; 
Label1->Caption="The Result"; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton mbLeft, 
      TShiftState Shift, int X, int Y) 
{ 
if(draw) 
 { 
  mousedown=false; 
  kuang=abs(X-posx); 
  gao=abs(Y-posy); 
  finalx=X; 
  finaly=Y; 
  Button2->Enabled=true; 
  draw=false; 
 } 
} 
//--------------------------------------------------------------------------- 
void blackwhite(TColor biji,TColor beijing,Graphics::TBitmap *pic) 
{ 
int r1,r2,g1,g2,b1,b2; 
int max,r,g,b; 
int w,h,i,j; 
Byte *ptr; 
w=pic->Width; 
h=pic->Height; 
r1=GetRValue(biji); 
r2=GetRValue(beijing); 
g1=GetGValue(biji); 
g2=GetGValue(beijing); 
b1=GetBValue(biji); 
b2=GetBValue(beijing); 
r=abs(r1-r2); 
g=abs(g1-g2); 
b=abs(b1-b2); 
max=(r>g)?r:g; 
max=(max>b)?max:b; 
pic->PixelFormat=pf24bit; 
for (j=0;jScanLine[j]; 
   for (i=0;i(r1+r/2)) 
       { 
        ptr[i]=255; 
        ptr[i+1]=255; 
        ptr[i+2]=255; 
        } 
       else 
       { 
        ptr[i]=0; 
        ptr[i+1]=0; 
        ptr[i+2]=0; 
        } 
      } 
      if (max==g) 
      { 
       if (ptr[i+1]>(g1+g/2)) 
        { 
         ptr[i]=255; 
         ptr[i+1]=255; 
         ptr[i+2]=255; 
        } 
       else 
        { 
         ptr[i]=0; 
         ptr[i+1]=0; 
         ptr[i+2]=0; 
        } 
      } 
       if (max==b) 
       { 
        if(ptr[i]>(b1+b/2)) 
         { 
          ptr[i]=255; 
          ptr[i+1]=255; 
          ptr[i+2]=255; 
         } 
        else 
         { 
          ptr[i]= 0; 
          ptr[i+1]=0; 
          ptr[i+2]=0; 
         } 
       } 
     } 
   } 
} 
 
//--------------------------------------------------------------------------- 
 
 
 
void __fastcall TForm1::Button3Click(TObject *Sender) 
{ 
Graphics::TBitmap *pic10; 
pic10=new Graphics::TBitmap; 
pic10->Assign(Image1->Picture->Bitmap); 
if (Shape1->Brush->Color!=clWhite&&Shape2->Brush->Color!=clWhite) 
 { 
  blackwhite(Shape1->Brush->Color,Shape2->Brush->Color,pic10); 
  Image2->Picture->Bitmap->Assign(pic10); 
  Image2->Refresh(); 
  } 
else 
 Application->MessageBoxA("请先选择颜色","提示",MB_OK); 
 delete pic10; 
} 
//--------------------------------------------------------------------------- 
 
 
//---------------------------------------------------------------------------