home *** CD-ROM | disk | FTP | other *** search
- { Project Matrix.DPR Delphi 2.0 Demos
-
- Description:- Matrix.Dpr Project:-
-
- Demonstrates the use of:
-
- 1) 'Soften'
- 2) 'Sharpen'
- 3) 'ExtractEdges'
- 4) 'Blur'
- 5) 'MatrixFilter'
-
- Date of Origin: 17/04/96
- Original Author: Andrew Hutchison
- Modification History:
-
- Date Person Change
- ----------------------------------------------------
- 17/04/96 A Hutchison Created
-
- (c) Copyright Media Architects Inc. 1996.
- All rights reserved. No part of this program may be
- photocopied, reproduced, translated to another programming
- language or transported to any computer system without the
- prior written consent of Media Architects Inc.}
-
- unit UMatrix;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- ExtCtrls, OleCtrls, ImageKnife32, Menus, ComCtrls, Tabnotbk, StdCtrls,
- Spin, Buttons;
-
- type
- TForm1 = class(TForm)
- MainMenu1: TMainMenu;
- File1: TMenuItem;
- LoadImage: TMenuItem;
- SaveImageAs1: TMenuItem;
- N1: TMenuItem;
- Exit1: TMenuItem;
- OpenDialog: TOpenDialog;
- PicbufVisible: TPicbuf;
- Bevel1: TBevel;
- MatrixPage: TTabbedNotebook;
- Element00: TSpinEdit;
- Element10: TSpinEdit;
- Element11: TSpinEdit;
- Element20: TSpinEdit;
- Element21: TSpinEdit;
- Element22: TSpinEdit;
- Element01: TSpinEdit;
- Element02: TSpinEdit;
- Element12: TSpinEdit;
- Scale: TSpinEdit;
- Norm: TSpinEdit;
- Offset: TSpinEdit;
- RadioGroup: TRadioGroup;
- ScrollBar: TScrollBar;
- Process: TSpeedButton;
- Undo: TSpeedButton;
- PicbufEdit: TPicbuf;
- PicbufOriginal: TPicbuf;
- Element500: TSpinEdit;
- Element510: TSpinEdit;
- Element514: TSpinEdit;
- Element521: TSpinEdit;
- Element523: TSpinEdit;
- Element530: TSpinEdit;
- Element502: TSpinEdit;
- Element503: TSpinEdit;
- Element512: TSpinEdit;
- Element513: TSpinEdit;
- Element504: TSpinEdit;
- Element524: TSpinEdit;
- Element532: TSpinEdit;
- Element531: TSpinEdit;
- Element522: TSpinEdit;
- Element520: TSpinEdit;
- Element511: TSpinEdit;
- Element501: TSpinEdit;
- Element533: TSpinEdit;
- Element534: TSpinEdit;
- Element540: TSpinEdit;
- Element541: TSpinEdit;
- Element544: TSpinEdit;
- Element542: TSpinEdit;
- Element543: TSpinEdit;
- Scale5: TSpinEdit;
- Norm5: TSpinEdit;
- Offset5: TSpinEdit;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- procedure SaveImageAs1Click(Sender: TObject);
- procedure LoadImageClick(Sender: TObject);
- procedure PicbufOriginalChange(Sender: TObject);
- procedure PicbufEditChange(Sender: TObject);
- procedure RadioGroupClick(Sender: TObject);
- procedure ScrollBarChange(Sender: TObject);
- procedure ProcessClick(Sender: TObject);
- procedure Exit1Click(Sender: TObject);
- procedure UndoClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormActivate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- {Two functions used to apply a Matrix Filter of the given Size - See Below}
- procedure Apply33(Picbuf:TPicbuf);
- procedure Apply55(Picbuf:TPicbuf);
- {Valid File Format}
- function ValidFormat(FileName:String):Boolean; {See Below} {5*5}
-
- implementation
-
- {$R *.DFM}
-
- {-------------------------------------------------------------------------------}
- {Load Image into the 'PicbufOriginal' picbuf. After this occurs, the Image is
- copied to the 'Edit' and 'Visible' Picbuf Controls - See Below}
- procedure TForm1.LoadImageClick(Sender: TObject);
- begin
- {Open Dialog}
- if OpenDialog.Execute then
- begin
- Application.ProcessMessages;
- {Set Name}
- PicbufOriginal.Filename:=OpenDialog.Filename;
- {Load Image}
- PicbufOriginal.Load;
- end;
- end;
-
- {--------------------------------------------------------------------------------}
- {Save Visible Image to Disk based on extension}
- procedure TForm1.SaveImageAs1Click(Sender: TObject);
- begin
- if OpenDialog.Execute then
- begin
- Application.ProcessMessages;
- {Set FileName}
- PicbufVisible.FileName := OpenDialog.Filename;
- {Check to see a Valid filename exists - See function Below}
- if ValidFormat(PicbufVisible.FileName) then {Pass the fileName to the function}
- PicbufVisible.Store {Save if Valid}
- else
- MessageDlg('Your File Extension is Not Valid.', mtInformation, [mbOk], 0);
- end;
- end;
-
- {-------------------------------------------------------------------------------}
- {Use change event of the 'OriginalImage', to copy the Image to the 'PicbufEdit'
- control. This means we can then work on a 'Copy' of the Image.}
- procedure TForm1.PicbufOriginalChange(Sender: TObject);
- var
- HDIB:Integer;
- begin
- {Copy from the 'PicbufOriginal' Image to the 'PicbufEdit'}
- hDIB := PicbufOriginal.DuplicateDib(0);
- {Import into 'EditPicbuf' ready for applying an effect}
- PicbufEdit.ImportDib(hDIB, False);
- end;
-
- {-------------------------------------------------------------------------------}
- {Whenever we apply an effect to the 'PicbufEdit' it causes a change event to occur-
- use this event to copy the 'adjusted Image' to the 'PicbufVisible' Image - the
- one we see on Screen}
- procedure TForm1.PicbufEditChange(Sender: TObject);
- var
- Hdib:Integer;
- begin
- {Copy from 'Edit Picbuf' to 'Visible Picbuf'}
- hDIB := PicbufEdit.DuplicateDib(0);
- PicbufVisible.ImportDib(hDIB, False);
- end;
-
- {-------------------------------------------------------------------------------}
- {Enable/Disable Buttons/Scrollbars depending on required function. Depending
- which option the user selects, the effect selected is applied either with the
- 'ScrollBar' or with the 'Process' Button. Use the ItemIndex value to determine
- which button is selected, and enable/disable the ScrollBar/Button as required}
- procedure TForm1.RadioGroupClick(Sender: TObject);
- begin
- case RadioGroup.ItemIndex of
- 0:{Soften}
- begin
- ScrollBar.Enabled:=True; {Enable Scroll Bar}
- Process.Enabled:=False; {Disable Process Button}
- end;
- 1:{Sharpen}
- begin
- ScrollBar.Enabled:=True;
- Process.Enabled:=False;
- end;
- 2:{Blur}
- begin
- ScrollBar.Enabled:=False;
- Process.Enabled:=True;
- end;
- 3:{Edge}
- begin
- ScrollBar.Enabled:=True;
- Process.Enabled:=False;
- end;
- 4:{Matrix}
- begin
- ScrollBar.Enabled:=False;
- Process.Enabled:=True;
- end;
- end;
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Apply a 'function' using the 'ScrollBar' and based on which function is selected
- by the user. The 'Scrollbar' is used only to apply Soften, Sharpen or Edge. So
- we only refer to those buttons in the sub below - Again Note that the ItemIndex
- property of the RadioGroup indicates which button is selected}
- procedure TForm1.ScrollBarChange(Sender: TObject);
- begin
- {Apply the effect - the amount of effect is the Position of the ScrollBar [1-10]}
- case RadioGroup.ItemIndex of {Reference RadioButton Group}
- 0:PicbufEdit.Soften(ScrollBar.Position); {Soften}
- 1:PicbufEdit.Sharpen(ScrollBar.Position); {Sharpen}
- 3:PicbufEdit.ExtractEdges(ScrollBar.Position); {Edge}
- end;
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Similar to the OnClick event of the ScrollBar. This time the user can apply either
- 'Blur' or the 'Matrix Filter' to the Image using the Process Button. Again we can
- determine which one to apply by using the ItemIndex of the RadioGroup - this tells
- us which button is selected}
- procedure TForm1.ProcessClick(Sender: TObject);
- begin
- Case RadioGroup.ItemIndex of
- 2:PicbufEdit.Blur; {Blur Image}
- 4: {Matrix Filter}
- begin
- {When we apply the Matrix filter, we must apply the correct filter size. In
- order to apply the desired filter size, we apply which ever 'Matrix' filter is
- visible to the user, this can be either 3 * 3 or 5 * 5. The Matrix elements are
- contained within a 'TabbedNotebook' TObject. The 3 * 3 filter is placed on the
- first 'Page' and the 5 * 5 filter is placed on the second 'page'. So we can then
- refer to the PageIndex property of the Notebook to determine which Matrix is
- visible and hence which to apply. 'PageIndex' will be 0 for 3 * 3 and 1 for 5* 5}
- case MatrixPage.PageIndex of {Refer to NoteBook}
- 0: Apply33(PicbufEdit); {Apply 3*3 Filter to Image - See procedure Below}
- 1: Apply55(PicbufEdit); {Apply 5*5 Filter to Image - See procedure Below}
- end;
- end;
- end;
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Apply a 3 * 3 Matrix to the passed 'Picture Buffer'. This procedure applies
- a 3 * 3 Matrix Filter to the 'passed' picbuf - called 'PICBUF'. When you call
- this procedure, simply pass the name of the Picbuf you wish the matrix effect to
- be applied to :- Apply33(MYPICBUF);}
-
- procedure Apply33(Picbuf:TPicbuf);
-
- Type {Used to get a pointer to a 3*3 array}
- Matrix33 = ^Matrix33array;
- Matrix33array = array[0..2,0..2] of Integer;
-
- Var {Used to Hold and Get and Apply User Data - Global this Unit}
- Matrix33data : Matrix33array;{Array}
-
- begin
- {Fill in the array elements with the edit box values}
- with Form1 do
- begin
- Matrix33data[0,0] := Element00.Value; {Upper Left}
- Matrix33data[0,1] := Element01.Value;
- Matrix33data[0,2] := Element02.Value;
- Matrix33data[1,0] := Element10.Value; {Middle Row Left}
- Matrix33data[1,1] := Element11.Value; {Center Point}
- Matrix33data[1,2] := Element12.Value; {Middle Row Right}
- Matrix33data[2,0] := Element20.Value;
- Matrix33data[2,1] := Element21.Value;
- Matrix33data[2,2] := Element22.Value; {Lower Right}
- {Apply the Filter to the passed Picbuf}
- Picbuf.MatrixFilter(3,@Matrix33data,Scale.Value,Norm.Value,Offset.Value);
- end;
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Apply a 5 * 5 Matrix to the passed 'Picture Buffer'. This procedure applies
- a 5 * 5 Matrix Filter to the 'passed' picbuf - called 'PICBUF'. When you call
- this procedure, simply pass the name of the Picbuf you wish the matrix effect to
- be applied to :- Apply55(MYNEWPICBUF);}
-
- procedure Apply55(Picbuf:TPicbuf);
-
- Type {Used to get a pointer to a 5*5 array}
- Matrix55 = ^Matrix55array;
- Matrix55array = array[0..4,0..4] of Integer;
-
- Var {Used to Hold and Get and Apply User Data - Global this Unit}
- Matrix55data : Matrix55array;{Array}
-
- begin
- {Fill in the array elements with the edit box values}
- with Form1 do{Reference form that holds the Edit Boxes}
- begin
- {Top Row Starting Left Side}
- Matrix55data[0,0] := Element500.Value;
- Matrix55data[0,1] := Element501.Value;
- Matrix55data[0,2] := Element502.Value;
- Matrix55data[0,3] := Element503.Value;
- Matrix55data[0,4] := Element504.Value;
- {Next Row}
- Matrix55data[1,0] := Element510.Value;
- Matrix55data[1,1] := Element511.Value;
- Matrix55data[1,2] := Element512.Value;
- Matrix55data[1,3] := Element513.Value;
- Matrix55data[1,4] := Element514.Value;
- {Next Row}
- Matrix55data[2,0] := Element520.Value;
- Matrix55data[2,1] := Element521.Value;
- Matrix55data[2,2] := Element522.Value;
- Matrix55data[2,3] := Element523.Value;
- Matrix55data[2,4] := Element524.Value;
- {Next Row}
- Matrix55data[3,0] := Element530.Value;
- Matrix55data[3,1] := Element531.Value;
- Matrix55data[3,2] := Element532.Value;
- Matrix55data[3,3] := Element533.Value;
- Matrix55data[3,4] := Element534.Value;
- {Bottom Row}
- Matrix55data[4,0] := Element540.Value;
- Matrix55data[4,1] := Element541.Value;
- Matrix55data[4,2] := Element542.Value;
- Matrix55data[4,3] := Element543.Value;
- Matrix55data[4,4] := Element544.Value;
- {Apply Filter}
- Picbuf.MatrixFilter(5,@Matrix55data,Scale5.Value,Norm5.Value,Offset5.Value);
- end;
- end;
-
- {-------------------------------------------------------------------------------}
- {Undo}
- procedure TForm1.UndoClick(Sender: TObject);
- begin
- {Force a call to the 'OriginalPicbuf' Change event - this will reload the original
- Image};
- PicbufOriginalChange(Sender);
- end;
-
- {-------------------------------------------------------------------------------}
- {Exit Application}
- procedure TForm1.Exit1Click(Sender: TObject);
- begin
- Halt;
- end;
-
-
- {-------------------------------------------------------------------------------}
- { This function simply checks to see if any one of the listed ImageKnife formats
- exist in the filename passed to the function. If it does then the function
- evaluates to true - Note this is the RESULT of the function.}
- function ValidFormat(FileName:String):Boolean;
- Var
- Temp:String;
- begin
- Temp := UpperCase(Filename);{Convert FileName to Upper Case}
- Result:=False;{Default result if no recognised match is found - *.*}
- if Pos('.TIF', Temp ) > 0 then Result:= True; {for Pos see Delphi Help}
- if Pos('.TGA', Temp ) > 0 then Result:= True;
- if Pos('.BMP', Temp ) > 0 then Result:= True;
- if Pos('.GIF', Temp ) > 0 then Result:= True;
- if Pos('.DIB', Temp ) > 0 then Result:= True;
- if Pos('.PCX', Temp ) > 0 then Result:= True;
- if Pos('.JPG', Temp ) > 0 then Result:= True;
- if Pos('.MSP', Temp ) > 0 then Result:= True;
- if Pos('.FIF', Temp ) > 0 then Result:= True;
- if Pos('.PNG', Temp ) > 0 then Result:= True;
- end;
-
- {--------------------------------------------------------------------------------}
- {Set Up any Defaults}
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Application.HintPause:=10;
- Application.HintColor:=clAqua;
- end;
-
- procedure TForm1.FormActivate(Sender: TObject);
- begin
- PicbufOriginal.Filename:='..\images\marybeth.tif';
- PicbufOriginal.Load;
- PicBufVisible.ScrollBars := SB_Both;
- end;
-
- end.
-