Pixelate Filter

Pixelate er lett å få til i Firemonkey. Min løsning er, som på de fleste andre filtre, en dialogbox med to TImage, en for originalen og en for resultatet. Videre en TTrackbar og to TButtons.

Ved design tidspunkt ser det slik ut:

Pixelate Filter Design
Pixelate Filter Design

PIxelate deklarasjonen:

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

#ifndef PixelateH
#define PixelateH
//---------------------------------------------------------------------------
#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 TForm12 : public TForm
{
__published:	// IDE-managed Components
	TImage *Image1;
	TImage *Image2;
	TTrackBar *TrackBar1;
	TButton *Cancel;
	TButton *OK;
	TLabel *Label1;
	void __fastcall OnCreate(TObject *Sender);
	void __fastcall TrackBar1Change(TObject *Sender);
	void __fastcall CancelClick(TObject *Sender);
	void __fastcall OKClick(TObject *Sender);
private:	// User declarations
public:		// User declarations
	int iretState;
	//TFilterSharpen * MySharpenFilter;
	//TFilterGaussianBlur *myGaussianBlur;
	//TFilterBoxBlur *myBoxBlur;
	//TFilterRadialBlur *myRadialBlur;
	//TFilterDirectionalBlur *myDirectionalBlur;
	//TFilterEmboss *myEmboss;
	TFilterPixelate * myPixelate;

	__fastcall TForm12(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm12 *Form12;
//---------------------------------------------------------------------------
#endif

Du ser antagelig lett ovenfor at jeg bruker omtrent den samme klasse deklarasjonen for et antall filtre? 🙂

Implementasjonen er også grei nok:

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

#include <fmx.h>
#pragma hdrstop

#include "Pixelate.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm12 *Form12;
//---------------------------------------------------------------------------
__fastcall TForm12::TForm12(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm12::OnCreate(TObject *Sender)
{
 	//myGaussianBlur = new TFilterGaussianBlur(this);
	//myBoxBlur = new TFilterBoxBlur(this);
	//myRadialBlur = new TFilterRadialBlur(this);
	//myDirectionalBlur = new TFilterDirectionalBlur(this);
	//myEmboss = new TFilterEmboss(this);
	myPixelate = new TFilterPixelate(this);

	 myPixelate->Input = Image1->Bitmap;
	 TrackBar1->Value = 25;
	 myPixelate->BlockCount = TrackBar1->Value;
	 Image2->Bitmap = myPixelate->Output;

}
//---------------------------------------------------------------------------
void __fastcall TForm12::TrackBar1Change(TObject *Sender)
{
	myPixelate->BlockCount = TrackBar1->Value;
	Image2->Bitmap = myPixelate->Output;

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

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

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

Så gjenstår det å vise hvordan jeg bruker en TForm for å vise en dialogboks. Det skjer i DocPage klassen:

// --------------------------------------------------------------------------
//  				Pixelate
// --------------------------------------------------------------------------

void __fastcall DocPage::Pixelate()
{
	try
	{
		TForm12 *dlgPixelate =  new TForm12(Application);

		dlgPixelate->Image1->Bitmap = Image1->Bitmap;
		dlgPixelate->Caption = _T("Pixelate");

		dlgPixelate->myPixelate->Input = dlgPixelate->Image1->Bitmap;
		dlgPixelate->myPixelate->BlockCount = dlgPixelate->TrackBar1->Value;
		dlgPixelate->Image2->Bitmap = dlgPixelate->myPixelate->Output;


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

			// Don't forget the undo-stack

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

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

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

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

		}

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

}

Til slutt et par bilder fra kjøring av programmet:

Pixelate Runtime

Og selve dialogboksen:

pixelate dialogbox
Pixelate Dialogbox

Legg igjen en kommentar

Din e-postadresse vil ikke bli publisert. Obligatoriske felt er merket med *

Dette nettstedet bruker Akismet for å redusere spam. Lær om hvordan dine kommentar-data prosesseres.