Nadesłany przez Tomasz Lubiński, 03 kwietnia 2006 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
Zmiana wielkosci obrazu - Bilinear Interpolation - C++/BI.cpp:
// Zmiana wielkosci obrazka -
// Algorytm Bilinear Interpolation - algorytm interpolacji podwójnej
// www.algorytm.org
// Tomasz Lubinski (c) 2006
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "BI.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TPixel org[122][100];
//---------------------------------------------------------------------------
float TForm1::Inter(float f1, float f2, float d)
{
return (f1*(1-d) + f2*d);
}
float TForm1::InterNorm(float f1, float f2, float d)
{
float result = (f1*(1-d) + f2*d);
if (result > 255) return 255;
if (result < 0) return 0;
return result;
}
//---------------------------------------------------------------------------
TPixel TForm1::Bilinear_Inter(float x, float y)
{
int x0,y0,x1,y1;
float dx,dy;
TPixel result;
x0 = x; //floor
y0 = y; //floor
dx = x-x0;
dy = y-y0;
if (x0 + 1 >= src->Width)
x1 = x0;
else
x1 = x0 + 1;
if (y0 + 1 >= src->Width)
y1 = y0;
else
y1 = y0 + 1;
result.r = (InterNorm(Inter(org[x0][y0].r, org[x1][y0].r, dx), Inter(org[x0][y1].r, org[x1][y1].r, dx), dy));
result.g = (InterNorm(Inter(org[x0][y0].g, org[x1][y0].g, dx), Inter(org[x0][y1].g, org[x1][y1].g, dx), dy));
result.b = (InterNorm(Inter(org[x0][y0].b, org[x1][y0].b, dx), Inter(org[x0][y1].b, org[x1][y1].b, dx), dy));
return result;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i,j;
float ratiox, ratioy;
TPixel *pixel;
//pobierz wartosci obrazu oryginalnego
src->Picture->Bitmap->PixelFormat = pf32bit;
for (j=0; j<src->Height; j++)
{
pixel = (TPixel *)src->Picture->Bitmap->ScanLine[j];
for (i=0; i<src->Width; i++)
{
org[i][j] = *pixel;
pixel++;
}
}
//pobierz wielkosc nowego obrazu
dst->Width = StrToInt(Edit2->Text);
dst->Height = StrToInt(Edit1->Text);
dst->Picture->Bitmap->Width = dst->Width;
dst->Picture->Bitmap->Height = dst->Height;
ratiox = (src->Width*1.0)/(dst->Width*1.0);
ratioy = (src->Height*1.0)/(dst->Height*1.0);
dst->Canvas->Brush->Color = clWhite;
dst->Canvas->Rectangle(0, 0, dst->Width, dst->Height);
dst->Picture->Bitmap->PixelFormat = pf32bit;
//wygeneruj nowy obraz
for (j=0; j<dst->Height; j++)
{
pixel = (TPixel *)dst->Picture->Bitmap->ScanLine[j];
for (i=0; i<dst->Width; i++)
{
*pixel = Bilinear_Inter(i*ratiox, j*ratioy);
pixel++;
}
}
}
//---------------------------------------------------------------------------

