Brightness Contrast

Igjen, design blir veldig lik alle de andre filtertyper, men denne gang må du ha to TTrackBars, en for Brightness og en for Contrast:

Brightness Contrast
Brightness Contrast

Håndteringen av de to TrackBars blir da det nye. Her i Klassedeklarasjonen:

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

#ifndef BrightnessContrastH
#define BrightnessContrastH
//---------------------------------------------------------------------------
#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 TForm13 : public TForm
{
__published:	// IDE-managed Components
	TImage *Image1;
	TImage *Image2;
	TTrackBar *TrackBar1;
	TTrackBar *TrackBar2;
	TButton *Cancel;
	TButton *OK;
	TLabel *Label2;
	TLabel *Label1;
	void __fastcall OnCreate(TObject *Sender);
	void __fastcall TrackBar1Change(TObject *Sender);
	void __fastcall TrackBar2Change(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;
	TFilterContrast *myContrast;

	__fastcall TForm13(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm13 *Form13;
//---------------------------------------------------------------------------
#endif

Du ser dessuten av jeg bruker omtrent den samme deklarasjonen i flere av filtrene?

Implementasjonen blir da som følger:

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

#include <fmx.h>
#pragma hdrstop

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

	 myContrast->Input = Image1->Bitmap;
	 myContrast->Brightness = 0;
	 myContrast->Brightness = TrackBar1->Value;
	 Image2->Bitmap = myContrast->Output;
	 TrackBar2->Value = 1.0;
	 myContrast->Contrast = TrackBar2->Value;

}
//---------------------------------------------------------------------------
void __fastcall TForm13::TrackBar1Change(TObject *Sender)
{
	myContrast->Brightness = TrackBar1->Value;
	Image2->Bitmap = myContrast->Output;

}
//---------------------------------------------------------------------------
void __fastcall TForm13::TrackBar2Change(TObject *Sender)
{
	myContrast->Contrast = TrackBar2->Value;
	Image2->Bitmap = myContrast->Output;

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

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

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

Kjøring av dialogboksen finner sted i DocPage-klassen som tidligere:

// --------------------------------------------------------------------------
//  				BrightnessContrast
// --------------------------------------------------------------------------

void __fastcall DocPage::BrightnessContrast()
{
	try
	{
		TForm13 *dlgBrightnessContrast =  new TForm13(Application);

		dlgBrightnessContrast->Image1->Bitmap = Image1->Bitmap;
		dlgBrightnessContrast->Caption = _T("BrightnessContrast");

		dlgBrightnessContrast->myContrast->Input = dlgBrightnessContrast->Image1->Bitmap;
		dlgBrightnessContrast->myContrast->Brightness = dlgBrightnessContrast->TrackBar1->Value / 10;
		dlgBrightnessContrast->Image2->Bitmap = dlgBrightnessContrast->myContrast->Output;
		dlgBrightnessContrast->myContrast->Contrast = dlgBrightnessContrast->TrackBar2->Value / 10;

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

			// Don't forget the undo-stack

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

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

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

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

		}

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

}

Da gjenstår det bare å vise et par bilder fra kjøring av programmet:

Brightness Contrast Runtime 1
Brightness Contrast Runtime 1
Brightness Contrast Runtime 2
Brightness Contrast Runtime 2

Jeg kunne hatt noen streker/tall under hver TrackBar for å vise verdiene, men pytt, pytt. 🙂

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.