Box Blur i C++ Builder

Design tidspunkt:

Box Blur RAD Studio
Box Blur RAD Studio Design

Bildet ovenfor viser program design slik jeg bruker å gjøre det: 2 stk TImage, en for originalen og en for resultatet samt en TTrackBar og to TButton, OK og Cancel. Dialogboksen startes som vanlig opp i «DocPage»-klassen:

// --------------------------------------------------------------------------
//  				Box Blur
// --------------------------------------------------------------------------

void __fastcall DocPage::BoxBlur()
{
	try
	{
		TForm8 *dlgBoxBlur =  new TForm8(Application);

		dlgBoxBlur->Image1->Bitmap = Image1->Bitmap;
		dlgBoxBlur->Caption = _T("BoxBlur");

		dlgBoxBlur->myBoxBlur->Input = dlgBoxBlur->Image1->Bitmap;
		dlgBoxBlur->myBoxBlur->BlurAmount = dlgBoxBlur->TrackBar1->Value / 100;
		dlgBoxBlur->Image2->Bitmap = dlgBoxBlur->myBoxBlur->Output;


		dlgBoxBlur->ShowModal();
		if( dlgBoxBlur -> iretState == 1 ) {

			// Don't forget the undo-stack

			UndoItem = new TUndoItem();
			UndoItem->UndoName = new String("BoxBlur");
			UndoItem->UndoBitmap = new TBitmap();

			// Have to use "Assign()" because everything are pointers!!!

			UndoItem->UndoBitmap->Assign(Image1->Bitmap);
			mystack.push(UndoItem);

			Image1->Bitmap->Assign(dlgBoxBlur->Image2->Bitmap);

		}

		dlgBoxBlur->DisposeOf();
	}
	catch(...)
	{
		ShowMessage("Gaussian Blur did not succeed");
	}

}

Klasse definisjonen av Boxblur:

//---------------------------------------------------------------------------

#ifndef BoxBlurH
#define BoxBlurH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <FMX.Controls.hpp>
#include <FMX.Forms.hpp>
#include <FMX.Controls.Presentation.hpp>
#include <FMX.StdCtrls.hpp>
#include <FMX.Types.hpp>
#include <FMX.Effects.hpp>
#include <FMX.Filter.Effects.hpp>
#include <FMX.Objects.hpp>
///---------------------------------------------------------------------------
class TForm8 : public TForm
{
__published:	// IDE-managed Components
	TButton *OK;
	TButton *Cancel;
	TTrackBar *TrackBar1;
	TImage *Image2;
	TImage *Image1;
	void __fastcall OnCreate(TObject *Sender);
	void __fastcall CancelClick(TObject *Sender);
	void __fastcall OKClick(TObject *Sender);
	void __fastcall TrackBar1Change(TObject *Sender);
private:	// User declarations
public:		// User declarations
	int iretState;
	//TFilterSharpen * MySharpenFilter;
	//TFilterGaussianBlur *myGaussianBlur;
	TFilterBoxBlur *myBoxBlur;

	__fastcall TForm8(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm8 *Form8;
//---------------------------------------------------------------------------
#endif

Implementasjonen er ganske enkel og vises nedenfor:

//---------------------------------------------------------------------------

#include <fmx.h>
#pragma hdrstop

#include "BoxBlur.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm8 *Form8;
//---------------------------------------------------------------------------
__fastcall TForm8::TForm8(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm8::OnCreate(TObject *Sender)
{
	//myGaussianBlur = new TFilterGaussianBlur(this);
	myBoxBlur = new TFilterBoxBlur(this);

	myBoxBlur->Input = Image1->Bitmap;
	myBoxBlur->BlurAmount = TrackBar1->Value / 10;
	Image2->Bitmap = myBoxBlur->Output;

}
//---------------------------------------------------------------------------
void __fastcall TForm8::CancelClick(TObject *Sender)
{
	iretState = 0;
	Close();

}
//---------------------------------------------------------------------------
void __fastcall TForm8::OKClick(TObject *Sender)
{
	iretState = 1;
	Close();

}
//---------------------------------------------------------------------------
void __fastcall TForm8::TrackBar1Change(TObject *Sender)
{
	myBoxBlur->BlurAmount = TrackBar1->Value / 10;
    Image2->Bitmap = myBoxBlur->Output;

}
//---------------------------------------------------------------------------

Kjøring av programmet:

BoxBlur ved runtime
BoxBlur ved runtime

Og dialogboksen: