⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.~cpp

📁 bilinear interpolation
💻 ~CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
Graphics::TBitmap *Bmp = new Graphics::TBitmap();

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
   delete Bmp;
   exit(0);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  OpenPictureDialog1->Filter = GraphicFilter(__classid(Graphics::TBitmap));
  if (OpenPictureDialog1->Execute())
  {
     Bmp->Dormant();           // Free up GDI resources
     Bmp->FreeImage();         // Free up Memory
     Bmp->ReleaseHandle();
     Bmp->LoadFromFile(OpenPictureDialog1->FileName);

     Image1->Width  = Bmp->Width;
     Image1->Height = Bmp->Height;
     Image2->Width  = Bmp->Width;
     Image2->Height = Bmp->Height;
     Image1->Picture->Bitmap = Bmp;
     Image2->Left   = Image1->Left + Image1->Width + 10;
     Image2->Picture->Bitmap = Bmp;
     Label2->Caption = IntToStr(Bmp->Width)+" x "+IntToStr(Bmp->Height);
     Edit1->Text = IntToStr(Bmp->Width);
     Edit2->Text = IntToStr(Bmp->Height);
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
  if (Image2->Picture->Bitmap->Empty==true)
  {
     ShowMessage("no Image to Process");
     return;
  }

  Graphics::TBitmap *NBmp = new Graphics::TBitmap();

  int            w,h,newW,newH;
  double         dX,dY,x,y;
  int            i,j;
  int            ix,iy;                    // integer value of (x,y)
  double         fx,fy;                    // float value of (x,y)
  unsigned char  clr,
                 clrR,clrG,clrB,
                 nclr1,nclr2,nclr3,nclr4;  // color of neighbor 4 pixel

  w = Image1->Width;
  h = Image1->Height;
  newW = StrToInt(Edit1->Text);
  newH = StrToInt(Edit2->Text);
  if((newW <=0) ||(newH <=0))
  {
    ShowMessage("Invalided parameter");
    return;
  }
  NBmp->Width  = newW;
  NBmp->Height = newH;

  /* Caculate the step size of movement */
  dX = (double)w/(double)newW;
  dY = (double)h/(double)newH;

  for(j = 0 ; j < newH ; j++)
     for(i =0 ; i < newW ; i++)
     {
       x = dX*(double)i;
       y = dY*(double)j;

       ix = (int)x;         iy = (int)y;
       fx = x - (float)ix;  fy = y - (float)iy;

       nclr1 = GetRValue(Bmp->Canvas->Pixels[ix][iy]);
       nclr2 = GetRValue(Bmp->Canvas->Pixels[ix+1][iy]);
       nclr3 = GetRValue(Bmp->Canvas->Pixels[ix][iy+1]);
       nclr4 = GetRValue(Bmp->Canvas->Pixels[ix+1][iy+1]);
       // Bilinear
       clrR=(byte)((1.0-fx)*(1.0-fy)*(double)nclr1+(fx)*(1.0-fy)*(double)nclr2
                  +(1.0-fx)*    (fy)*(double)nclr3+(fx)*    (fy)*(double)nclr4);

       nclr1 = GetGValue(Bmp->Canvas->Pixels[ix][iy]);
       nclr2 = GetGValue(Bmp->Canvas->Pixels[ix+1][iy]);
       nclr3 = GetGValue(Bmp->Canvas->Pixels[ix][iy+1]);
       nclr4 = GetGValue(Bmp->Canvas->Pixels[ix+1][iy+1]);
       // Bilinear
       clrG=(byte)((1.0-fx)*(1.0-fy)*(double)nclr1+(fx)*(1.0-fy)*(double)nclr2
                  +(1.0-fx)*    (fy)*(double)nclr3+(fx)*    (fy)*(double)nclr4);

       nclr1 = GetBValue(Bmp->Canvas->Pixels[ix][iy]);
       nclr2 = GetBValue(Bmp->Canvas->Pixels[ix+1][iy]);
       nclr3 = GetBValue(Bmp->Canvas->Pixels[ix][iy+1]);
       nclr4 = GetBValue(Bmp->Canvas->Pixels[ix+1][iy+1]);
       // Bilinear
       clrB=(byte)((1.0-fx)*(1.0-fy)*(double)nclr1+(fx)*(1.0-fy)*(double)nclr2
                  +(1.0-fx)*    (fy)*(double)nclr3+(fx)*    (fy)*(double)nclr4);

       NBmp->Canvas->Pixels[i][j] = (TColor)RGB(clrR,clrG,clrB);
     }
  Image2->Width = newW;
  Image2->Height = newH;
  Image2->Picture->Bitmap = NBmp;
  delete NBmp;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
 if (Image2->Picture->Bitmap->Empty==true)
     ShowMessage("no Image to Save");
 else
 {
  SavePictureDialog1->Filter = GraphicFilter(__classid(Graphics::TBitmap));
  if (SavePictureDialog1->Execute())
  {
     Image2->Picture->SaveToFile(SavePictureDialog1->FileName + ".bmp");
  }
 }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button5Click(TObject *Sender)
{
  Bmp->Dormant();
  Bmp->FreeImage();
  Bmp->ReleaseHandle();
  Image1->Picture->Bitmap = Bmp;
  Image2->Picture->Bitmap = Bmp;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
  if (Image2->Picture->Bitmap->Empty==true)
  {
     ShowMessage("no Image to Process");
     return;
  }

  Graphics::TBitmap *NBmp = new Graphics::TBitmap();

  int            w,h,newW,newH;
  double         dX,dY,x,y;
  double         scalar;                   // Zoom scalar
  int            i,j;
  int            ix,iy;                    // integer value of (x,y)
  double         fx,fy;                    // float value of (x,y)
  unsigned char  clr,
                 clrR,clrG,clrB,
                 nclr1,nclr2,nclr3,nclr4;  // color of neighbor 4 pixel

  w = Image1->Width;
  h = Image1->Height;
  scalar = StrToFloat(Edit3->Text);

  newW  =  (int)((double)w*scalar);
  newH  =  (int)((double)h*scalar);

  if((newW <=0) ||(newH <=0))
  {
    ShowMessage("Invalided parameter");
    return;
  }
  NBmp->Width  = newW;
  NBmp->Height = newH;

  /* Caculate the step size of movement */
  dX = (double)w/(double)newW;
  dY = (double)h/(double)newH;

  for(j = 0 ; j < newH ; j++)
     for(i =0 ; i < newW ; i++)
     {
       x = dX*(double)i;
       y = dY*(double)j;

       ix = (int)x;         iy = (int)y;
       fx = x - (float)ix;  fy = y - (float)iy;

       nclr1 = GetRValue(Bmp->Canvas->Pixels[ix][iy]);
       nclr2 = GetRValue(Bmp->Canvas->Pixels[ix+1][iy]);
       nclr3 = GetRValue(Bmp->Canvas->Pixels[ix][iy+1]);
       nclr4 = GetRValue(Bmp->Canvas->Pixels[ix+1][iy+1]);
       // Bilinear
       clrR=(byte)((1.0-fx)*(1.0-fy)*(double)nclr1+(fx)*(1.0-fy)*(double)nclr2
                  +(1.0-fx)*    (fy)*(double)nclr3+(fx)*    (fy)*(double)nclr4);

       nclr1 = GetGValue(Bmp->Canvas->Pixels[ix][iy]);
       nclr2 = GetGValue(Bmp->Canvas->Pixels[ix+1][iy]);
       nclr3 = GetGValue(Bmp->Canvas->Pixels[ix][iy+1]);
       nclr4 = GetGValue(Bmp->Canvas->Pixels[ix+1][iy+1]);
       // Bilinear
       clrG=(byte)((1.0-fx)*(1.0-fy)*(double)nclr1+(fx)*(1.0-fy)*(double)nclr2
                  +(1.0-fx)*    (fy)*(double)nclr3+(fx)*    (fy)*(double)nclr4);

       nclr1 = GetBValue(Bmp->Canvas->Pixels[ix][iy]);
       nclr2 = GetBValue(Bmp->Canvas->Pixels[ix+1][iy]);
       nclr3 = GetBValue(Bmp->Canvas->Pixels[ix][iy+1]);
       nclr4 = GetBValue(Bmp->Canvas->Pixels[ix+1][iy+1]);
       // Bilinear
       clrB=(byte)((1.0-fx)*(1.0-fy)*(double)nclr1+(fx)*(1.0-fy)*(double)nclr2
                  +(1.0-fx)*    (fy)*(double)nclr3+(fx)*    (fy)*(double)nclr4);

       NBmp->Canvas->Pixels[i][j] = (TColor)RGB(clrR,clrG,clrB);
     }
  Image2->Width = newW;
  Image2->Height = newH;
  Image2->Picture->Bitmap = NBmp;
  delete NBmp;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -