正文

图像处理源码,大家交流交流!!2005-06-29 21:57:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/program/2294.html

分享到:

#include <vcl.h> #pragma hdrstop #include<stdio.h> #include "Unit1.h" #include "Unit2.h" #include "Unit3.h" #include"jpeg.hpp" #include<math.h> #include<malloc.h> //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" long w,h; int ImageLoad; Byte *pixels; TRect fRect;; int ShowScale; TForm1 *Form1; bool s1; int s2,s3; AnsiString val; //char val; int m,n; Graphics::TBitmap *Bitmap; void zoomshow(int k); //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner)         : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::N18Click(TObject *Sender) {   AboutBox1->ShowModal();    } //--------------------------------------------------------------------------- void __fastcall TForm1::N19Click(TObject *Sender) {   AboutBox->ShowModal(); } //--------------------------------------------------------------------------- void zoomshow(int k) { int i,j,x,y; Byte *temp; Form1->Image1->Canvas->DrawFocusRect(fRect); fRect=Rect(0,0,0,0); if(k==1) {   Form1->Image1->Picture->Bitmap->Width=w;   Form1->Image1->Picture->Bitmap->Height=h;   for(y=0;y<h;y++)     Move(pixels+y*w*3,(Byte*)Form1->Image1->Picture->Bitmap->ScanLine[y],w*3);   Form1->Image1->Repaint(); } else {   Form1->Image1->Picture->Bitmap->PixelFormat=pf24bit;   Form1->Image1->Width=w*k;   Form1->Image1->Height=h*k;   temp=(Byte*)malloc(w*k*h*k*3*sizeof(Byte));   for(y=h-1;y>=0;y--)   {    for(x=w-1;x>=0;x--)      for(i=0;i<=k-1;i++)        for(j=0;j<=k-1;j++)          Move(pixels+((y*w+x)*3),temp+((y*k+i)*w*k+x*k+j)*3,3);    }   Form1->Image1->Picture->Bitmap->Width=w*k;   Form1->Image1->Picture->Bitmap->Height=h*k;   for(y=0;y<=h*k-1;y++)     Move(temp+y*w*k*3,(Byte*)Form1->Image1->Picture->Bitmap->ScanLine[y],w*k*3);   Form1->Image1->Repaint();   Form1->Image1->Refresh(); } } void __fastcall TForm1::N3Click(TObject *Sender) { Image1->Picture->SaveToFile(Form1->OpenPictureDialog1->FileName); } //--------------------------------------------------------------------------- void __fastcall TForm1::N4Click(TObject *Sender) {    SavePictureDialog1->DefaultExt=GraphicExtension(__classid(Graphics::TBitmap));    if(SavePictureDialog1->Execute())      {        Image1->Picture->SaveToFile(SavePictureDialog1->FileName);      } } //--------------------------------------------------------------------------- void __fastcall TForm1::N7Click(TObject *Sender) {  int i;    i=Application->MessageBox("你保存你的修改了吗?如果已保存请按确定","关闭",1);    if(i==1)    Close();         } //--------------------------------------------------------------------------- void __fastcall TForm1::N9Click(TObject *Sender) {   int x,y; TRect src,drc; x=Image1->Width; y=Image1->Height; src=Rect(0,0,x,y); drc=Rect(x,0,0,y); Image1->Canvas->CopyMode=cmSrcCopy; Image1->Width=x; Image1->Height; Image1->Canvas->CopyRect(drc,Image1->Canvas,src); } //--------------------------------------------------------------------------- void __fastcall TForm1::N10Click(TObject *Sender) {   int x,y;    TRect src,drc;    x=Image1->Width;    y=Image1->Height;    Image1->Width=x;    Image1->Height=y;    src=Rect(0,0,x,y);    drc=Rect(0,y,x,0);    Image1->Canvas->CopyMode=cmSrcCopy;    Image1->Canvas->CopyRect(drc,Image1->Canvas,src); } //--------------------------------------------------------------------------- void __fastcall TForm1::N11Click(TObject *Sender) {   int i,j,pix,pix1;    Byte cr,cg,cb;    ShowMessage("运行较慢,确定后请稍等片刻!");    for(j=0;j<Image1->Height;j++)      for(i=0;i<Image1->Width;i++)      {         pix=Image1->Picture->Bitmap->Canvas->Pixels[i][j];         cr=GetRValue(pix);         cg=GetGValue(pix);         cb=GetBValue(pix);         pix1=0.3*cr+0.6*cg+0.1*cb;         pix=RGB(pix1,pix1,pix1);         Image1->Picture->Bitmap->Canvas->Pixels[i][j]=(TColor)pix;        }         } //--------------------------------------------------------------------------- void __fastcall TForm1::N5Click(TObject *Sender) {   unsigned char c;   char ss[10];   int ct=0,ii,nn;   TFileStream *fm;   TMemoryStream *Msm;    ShowMessage("运行较慢,确定后请稍等片刻!");   fm=new TFileStream("bmp.txt",fmCreate);   Msm=new TMemoryStream();   Image1->Picture->Bitmap->SaveToStream(Msm);   Msm->Position=0;   nn=Msm->Size;   for(ii=0;ii<nn;ii++)   {     Msm->Read(&c,1);     sprintf(ss,"%.2x",c);     fm->Write(ss,2);     ct++;     if(ct>40)     {       ct=0;       ss[0]=0x0d;ss[1]=0x0a;       fm->Write(ss,2);     }   }   ss[0]=0x0d;ss[1]=0x0a;   fm->Write(ss,2);   delete Msm;   delete fm; } //--------------------------------------------------------------------------- void __fastcall TForm1::N1001Click(TObject *Sender) {    zoomshow(1);         } //--------------------------------------------------------------------------- void __fastcall TForm1::N2001Click(TObject *Sender) {   zoomshow(2);         } //--------------------------------------------------------------------------- void __fastcall TForm1::N4001Click(TObject *Sender) {   ShowMessage("运行较慢,确定后请稍等片刻!");   zoomshow(4); } //--------------------------------------------------------------------------- void __fastcall TForm1::N8001Click(TObject *Sender) {    ShowMessage("运行较慢,确定后请稍等片刻!");   zoomshow(8); } //--------------------------------------------------------------------------- void __fastcall TForm1::N12Click(TObject *Sender) {   int x,y,pos;    Byte *temp;    int val;    temp=(Byte *)malloc(w*h*3*sizeof(Byte));    for(y=1;y<=h-2;y++)    {      for(x=3;x<=w*3-4;x++)      {        pos=y*w*3+x;        val=floor((pixels[pos]*12-pixels[pos-w*3-3]-pixels[pos-w*3]-pixels[pos-w*3+3]-pixels[pos-3] -pixels[pos+3]-pixels[pos+w*3]-pixels[pos+w*3+3])/4);        if(val<255)          temp[pos]=255;         else          temp[pos]=0;        }      }      for(y=1;y<=h-2;y++)        for(x=3;x<=w*3-4;x++)          pixels[y*w*3+x]=temp[y*w*3+x];      zoomshow(1); } //--------------------------------------------------------------------------- void __fastcall TForm1::N13Click(TObject *Sender) { int x,y,pos;    int val;    for(y=0;y<=h-2;y++)    {      for(x=0;x<=w-2;x++)      {        pos=y*w*3+x*3;        val=floor((pixels[pos]+pixels[pos+w*3+3]+pixels[pos+1]-pixels[pos+w*3+4]+pixels[pos+2] -pixels[pos+w*3+5])/3)+127;        if (val>255)          pixels[pos]=255;        else if(val<0)          pixels[pos]=0;        pixels[pos]=val;        pixels[pos+1]=val;        pixels[pos+2]=val;       }      for(x=w*3-3;x<=w*3-1;x++)        pixels[y*w*3+x]=127;    }   for(x=0;x<=w*3-1;x++)     pixels[(h-1)*w*3+x]=127;   zoomshow(1);           } //--------------------------------------------------------------------------- void __fastcall TForm1::N14Click(TObject *Sender) {    int x,y,pos;    double ave;    ave=0;    for(y=0;y<=h-1;y++)      for(x=0;x<=w*3-1;x++)        ave+=pixels[y*w*3+x];    ave/=w*h*3;    for(y=0;y<=h-1;y++)      for(x=0;x<=w-1;x++)      {        pos=y*w*3+x*3;        if(pixels[pos]+pixels[pos]+pixels[pos]>=ave*3)        {          pixels[pos]=255;          pixels[pos+1]=255;          pixels[pos+2]=255;          }        else        {          pixels[pos]=0;          pixels[pos+1]=0;          pixels[pos+2]=0;          }        }   zoomshow(1); } //--------------------------------------------------------------------------- void __fastcall TForm1::JPEG1Click(TObject *Sender) {    TJPEGImage *jp;    AnsiString ext;    char *str="此图像已经是JPEG,你不必对它压缩!";    ext=UpperCase(ExtractFileExt(OpenPictureDialog1->FileName));    if(ext==".JPG")    ShowMessage(str);    else    {        if(SavePictureDialog1->Execute())          {             jp=new TJPEGImage();              jp->Assign(Image1->Picture->Bitmap);               Image1->Picture->Bitmap->Assign(jp);               jp->SaveToFile(SavePictureDialog1->FileName+".JPG");               delete jp;           }     } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) {        s1=false;        Image1->Canvas->Pen->Width=1;        Image1->Canvas->Pen->Color=clRed;        Label6->Caption=" 制作组:\n\n  廖碧锋\n\n  杨远远\n\n  陈斌"; } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) {    Label6->Top=Label6->Top-1;    if (Label6->Top<(Label6->Width-30))    Label6->Top=Panel1->Height; } //--------------------------------------------------------------------------- void __fastcall TForm1::jpeg2Click(TObject *Sender) {    long i;      Byte *s;     AnsiString ext;     OpenPictureDialog1->Title="请选择图像文件";      if(OpenPictureDialog1->Execute())       { ext=UpperCase(ExtractFileExt(OpenPictureDialog1->FileName));         if(ext==".JPG")          Image1->Picture->LoadFromFile(Form1->OpenPictureDialog1->FileName);          if(ext==".BMP")          {           { Image1->Picture->LoadFromFile(Form1->OpenPictureDialog1->FileName);             ImageLoad=1;             w=Image1->Picture->Width;             h=Image1->Picture->Height;             Image1->Width=w;             Image1->Height=h;             Image1->Picture->Bitmap->PixelFormat=pf24bit;             pixels=(Byte*)malloc(w*h*3*sizeof(Byte));               for(i=0;i<=h-1;i++)                 { s=(Byte*)Image1->Picture->Bitmap->ScanLine[i];                    Move(s,pixels+i*w*3,w*3);                 }               ImageLoad=1;                MainMenu1->Items[0].Items[0]->Items[1]->Enabled=true;                MainMenu1->Items[0].Items[0]->Items[2]->Enabled=true;               MainMenu1->Items[0].Items[0]->Items[3]->Enabled=true;                MainMenu1->Items[0].Items[0]->Items[4]->Enabled=true;                 //for(i=0;i<=1;i++)                 //MainMenu1->Items[0].Items[1]->Items[i]->Enabled=true;              // for(i=0;i<=6;i++)              // MainMenu1->Items[0].Items[2]->Items[i]->Enabled=true;            }                fRect=Rect(0,0,0,0);                ShowScale=1;                zoomshow(ShowScale);           }            }        } //--------------------------------------------------------------------------- void __fastcall TForm1::N23Click(TObject *Sender) {zoomshow(1); } //--------------------------------------------------------------------------- void __fastcall TForm1::N24Click(TObject *Sender) { zoomshow(1); } //--------------------------------------------------------------------------- void __fastcall TForm1::BMP1Click(TObject *Sender) {   Graphics::TBitmap *bmp = new Graphics::TBitmap();   TJPEGImage *jpeg = new TJPEGImage();   TJPEGImage *jp;    AnsiString ext;    char *str="此图像已经是BMP,你不必对它还原!";    ext=UpperCase(ExtractFileExt(OpenPictureDialog1->FileName));    if(ext==".BMP")    ShowMessage(str);    else    {      if(SavePictureDialog1->Execute())        {         jpeg->LoadFromFile(Form1->OpenPictureDialog1->FileName);         bmp->Assign(jpeg);         bmp->SaveToFile(SavePictureDialog1->FileName+".BMP");       }       delete bmp;        delete jpeg;     } } //--------------------------------------------------------------------------- void __fastcall TForm1::N26Click(TObject *Sender) {     Image1->Picture->Bitmap->Assign(Image1->Picture->Bitmap);     long r,g,b;     TColor color;     w=Image1->Picture->Width;     h=Image1->Picture->Height;      for(long i=0;i<=w-1;i++)        {  for(long j=0;j<=h-1;j++)            {              color=Image1->Canvas->Pixels[i][j];              r=GetRValue(color);              g=GetGValue(color);              b=GetBValue(color);              r=r+10;              if(r>255)  r=255;              g=g+10;              if(g>255)  g=255;              b=b+10;              if(b>255)  b=255;              Bitmap->Canvas->Pixels[i][j]=(TColor)RGB(r,g,b);             }        }        Image1->Picture->Bitmap->Assign(Bitmap); } //--------------------------------------------------------------------------- void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,       int X, int Y) {   int i,j;   AnsiString ext;    ext=UpperCase(ExtractFileExt(OpenPictureDialog1->FileName));    if(ext!=".JPG")     {     i=X;     j=Y;     Edit1->Text=IntToHex(GetRValue(Image1->Canvas->Pixels[i][j]),1);     Edit2->Text=IntToHex(GetGValue(Image1->Canvas->Pixels[i][j]),1);     Edit3->Text=IntToHex(GetBValue(Image1->Canvas->Pixels[i][j]),1);     Edit4->Text=Format("%6x",ARRAYOFCONST(((int)Image1->Canvas->Pixels[i][j])));     {      if(s1&&s3==1)       {         { Image1->Canvas->Pen->Width=StrToFloat(val);            Image1->Canvas->Pen->Color=ColorDialog1->Color;             Image1->Canvas->MoveTo(m,n);             Image1->Canvas->LineTo(X,Y);             //Image1->Canvas->MoveTo(m,n);          }           // m=X;            //n=Y;        }      else if(s1&&s2==1)       {         { Image1->Canvas->Pen->Width=1;            Image1->Canvas->Pen->Color=clRed;            Image1->Canvas->MoveTo(m,n);             Image1->Canvas->LineTo(X,Y);             //Image1->Canvas->MoveTo(m,n);          }           // m=X;            //n=Y;        }            m=X;            n=Y;       }     }       else     { Edit1->Text="无法取值";       Edit2->Text="请还原为";       Edit3->Text="位图后再";       Edit4->Text="进行操作";      } } //--------------------------------------------------------------------------- void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,       TShiftState Shift, int X, int Y) {   if(Button==mbRight)     PopupMenu1->Popup(X+180,Y+160);     s1=false; } //--------------------------------------------------------------------------- void __fastcall TForm1::N28Click(TObject *Sender) { TJPEGImage *jp;    AnsiString ext;    char *str="此图像已经是JPEG,你不必对它压缩!";    ext=UpperCase(ExtractFileExt(OpenPictureDialog1->FileName));    if(ext==".JPG")    ShowMessage(str);    else    {        if(SavePictureDialog1->Execute())          {             jp=new TJPEGImage();              jp->Assign(Image1->Picture->Bitmap);               Image1->Picture->Bitmap->Assign(jp);               jp->SaveToFile(SavePictureDialog1->FileName+".JPG");               delete jp;           }     } } //--------------------------------------------------------------------------- void __fastcall TForm1::N29Click(TObject *Sender) {   int i;    i=Application->MessageBox("你保存你的修改了吗?如果已保存请按确定","关闭",1);    if(i==1)    Close();         } //--------------------------------------------------------------------------- void __fastcall TForm1::BMP2Click(TObject *Sender) {    Graphics::TBitmap *bmp = new Graphics::TBitmap();   TJPEGImage *jpeg = new TJPEGImage();   TJPEGImage *jp;    AnsiString ext;    char *str="此图像已经是BMP,你不必对它还原!";    ext=UpperCase(ExtractFileExt(OpenPictureDialog1->FileName));    if(ext==".BMP")    ShowMessage(str);    else    {      if(SavePictureDialog1->Execute())        {         jpeg->LoadFromFile(Form1->OpenPictureDialog1->FileName);         bmp->Assign(jpeg);         bmp->SaveToFile(SavePictureDialog1->FileName+".BMP");       }       delete bmp;        delete jpeg;     }      } //--------------------------------------------------------------------------- void __fastcall TForm1::Image1MouseDown(TObject *Sender,       TMouseButton Button, TShiftState Shift, int X, int Y) {   s1=true;         } //--------------------------------------------------------------------------- void __fastcall TForm1::N32Click(TObject *Sender) {   zoomshow(1);   s2=0;   s3=0; } //--------------------------------------------------------------------------- void __fastcall TForm1::N34Click(TObject *Sender) {  AnsiString ext;    ext=UpperCase(ExtractFileExt(OpenPictureDialog1->FileName));    if(ext!=".JPG")      s2=1;    else    { ShowMessage("你只能在位图上作标记!");    } } //--------------------------------------------------------------------------- void __fastcall TForm1::N35Click(TObject *Sender) {   AnsiString ext;    ext=UpperCase(ExtractFileExt(OpenPictureDialog1->FileName));    if(ext!=".JPG")      {s3=1;      ShowMessage("你将在确定后选择画笔颜色!");      ColorDialog1->Execute();      val=InputBox("请输入画笔的宽度(w>=1)","画笔宽度",1);      }    else    {     ShowMessage("你只能在位图上作标记!");    } } //--------------------------------------------------------------------------- void __fastcall TForm1::N37Click(TObject *Sender) {    AnsiString ext;    ext=UpperCase(ExtractFileExt(OpenPictureDialog1->FileName));    if(ext!=".JPG")      s2=1;    else    { ShowMessage("你只能在位图上作标记!");    } } //--------------------------------------------------------------------------- void __fastcall TForm1::N38Click(TObject *Sender) { AnsiString ext;    ext=UpperCase(ExtractFileExt(OpenPictureDialog1->FileName));    if(ext!=".JPG")      {s3=1;      ShowMessage("你将在确定后选择画笔颜色!");      ColorDialog1->Execute();      val=InputBox("请输入画笔的宽度(w>=1)","宽度",1);      }    else    {     ShowMessage("你只能在位图上作标记!");    } } //--------------------------------------------------------------------------- void __fastcall TForm1::N39Click(TObject *Sender) { zoomshow(1);   s2=0;   s3=0;        } //--------------------------------------------------------------------------- void __fastcall TForm1::N1002Click(TObject *Sender) {   zoomshow(1);            } //--------------------------------------------------------------------------- void __fastcall TForm1::N2002Click(TObject *Sender) {   zoomshow(2); } //--------------------------------------------------------------------------- void __fastcall TForm1::N3001Click(TObject *Sender) {   zoomshow(3);           } //--------------------------------------------------------------------------- void __fastcall TForm1::N4002Click(TObject *Sender) { zoomshow(4);            } //---------------------------------------------------------------------------

阅读(2940) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册