//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
auto_ptr<TGraphic> TForm1::BlurImage(TImage *Image, int fValue)
{
if(fValue <= 0)
{
ShowMessage("Wprowadź wartość większą od 0!");
return (auto_ptr<TGraphic>) Image1->Picture->Graphic;
}
int Div = 6;
int flt[] = {
1, 1, 1,
0, 0, 0,
1, 1, 1
};
int XX[3];
BYTE *ptr, *ptru, *ptrd, *ptr1;
auto_ptr<Graphics::TBitmap> Bitmap (new Graphics::TBitmap());
auto_ptr<Graphics::TBitmap> tmpBitmap (new Graphics::TBitmap());
Bitmap->Assign( (TPersistent*)Image->Picture->Graphic );
Bitmap->PixelFormat = pf24bit;
tmpBitmap->Assign( (TPersistent*)Image->Picture->Graphic );
tmpBitmap->PixelFormat = pf24bit;
for(int f = 0; f <= fValue; f++)
{
for(int y = 1; y < tmpBitmap->Height - 1; y++)
{
ptr = (BYTE *) Bitmap->ScanLine[y];
ptr1 = (BYTE *) tmpBitmap->ScanLine[y];
ptru = (BYTE *) tmpBitmap->ScanLine[y-1];
ptrd = (BYTE *) tmpBitmap->ScanLine[y+1];
for(int x = 3; x < (tmpBitmap->Width - 1) * 3; x += 3)
{
XX[0] = 0; XX[1] = 0; XX[2] = 0;
for (int i1 = -1; i1 <= 1; i1++)
for(int j1 = 0; j1 < 3; j1++)
{
XX[j1] += ptr1[x + 3 * i1 + j1] * flt[4 + i1];
XX[j1] += ptru[x + 3 * i1 + j1] * flt[1 + i1];
XX[j1] += ptrd[x + 3 * i1 + j1] * flt[7 + i1];
}
for(int i4 = 0; i4 < 3; i4++)
{
XX[i4] = XX[i4] / Div;
XX[i4] = max(XX[i4], 0);
XX[i4] = min(XX[i4], 255);
ptr[x + i4] = XX[i4];
}
}
}
tmpBitmap->Assign(Bitmap.get());
}
return (auto_ptr<TGraphic>) Bitmap;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Image2->Picture->Graphic = BlurImage(Image1, 50).get();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenPictureDialog1->Execute())
{
Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
}
}