home *** CD-ROM | disk | FTP | other *** search
- { Project Mask.DPR Delphi 2.0 Demos
-
- Description:- Mask.Dpr Project:-
-
- Demonstrates the use of:
-
- 1) 'MaskCopy'
- 2) 'Init'
-
- 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 UMask;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- OleCtrls, ImageKnife32, Menus, Buttons, StdCtrls;
-
- type
- TForm1 = class(TForm)
- GroupBox1: TGroupBox;
- GroupBox2: TGroupBox;
- GroupBox3: TGroupBox;
- Composite: TSpeedButton;
- Lighten: TSpeedButton;
- Darken: TSpeedButton;
- MainMenu1: TMainMenu;
- File1: TMenuItem;
- LoadSourceImage1: TMenuItem;
- LoadMaskImage1: TMenuItem;
- LoadDestinationImage1: TMenuItem;
- N1: TMenuItem;
- SaveDestinationAs1: TMenuItem;
- N2: TMenuItem;
- Exit1: TMenuItem;
- PicbufSrc: TPicbuf;
- PicbufMask: TPicbuf;
- PicbufDest: TPicbuf;
- OpenDialog: TOpenDialog;
- procedure LoadImage(Sender: TObject);
- procedure CompositeClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure LightenClick(Sender: TObject);
- procedure DarkenClick(Sender: TObject);
- procedure Exit1Click(Sender: TObject);
- procedure SaveDestinationAs1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- {See Below}
- function GetImageLocation:String;
- {See Below}
- function ValidFormat(FileName:String):Boolean;
-
- implementation
-
- {$R *.DFM}
-
- {-------------------------------------------------------------------------------}
- {Set up Defaults Including Loading Default Sample Images.}
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Application.HintPause:=10;
- Application.HintColor:=clAqua;
-
- {Set FileNames - uses GetImageLocation function - See Below}
- PicbufSrc.filename := GetImageLocation + 'images\squirrel.bmp';
- PicbufMask.filename := GetImageLocation + 'images\sqcutout.bmp';
- PicbufDest.filename := GetImageLocation + 'images\balloon.bmp';
-
- {Load Default Source Image if it exists}
- if FileExists(PicbufSrc.filename) then{Delphi function call - True if file exists}
- PicbufSrc.Load
- else
- MessageDlg('Cannot find Sample file [\images\squirrel.bmp].' +
- ' Users should manually load this Image into the Source Picture.', mtInformation,
- [mbOk], 0);
-
- {Load Default Mask Image if it exists}
- if FileExists(PicbufSrc.filename) then{Delphi function call - True if file exists}
- PicbufMask.Load
- else
- MessageDlg('Cannot find Sample file [\images\sqcutout.bmp].' +
- ' Users should manually load this Image into the Mask Picture.', mtInformation,
- [mbOk], 0);
-
- {Load Default Destination Image if it exists}
- if FileExists(PicbufSrc.filename) then{Delphi function call - True if file exists}
- PicbufDest.Load
- else
- MessageDlg('Cannot find Sample file [\images\ballons.bmp].' +
- ' Users should manually load this Image into the Destination Picture.', mtInformation,
- [mbOk], 0);
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Load an Image into the correct picbuf control. Please note the use of a single
- event handler for more that one menu item. This saves multiple instances of
- identical code. Each Menu Item for loading is linked to the event 'LoadImage'.
- To do this simply open the menu designer, add the Menu Items. Go to the first
- Menu Item - just highlite it, do not double click it, and then using Object
- inspector, pick the Events Page for the Menu Item. Locate the 'OnClick' heading,
- and type in the Name of the Handler you wish to call - in this case 'LoadImage'.
- You can then double click the Item, and you will see Delphi creates the 'LoadImage'
- handler for you. To add other Menus to the same event, just highlite the one you
- wish to add, go to the events page, loacte the OnClick event, and using the drop
- down arrow options, pick the event handler you wish to link the menu to, again
- in this case 'LoadImage'.
-
- The final step is to allocate a number to the 'TAG' prperty of each menu item so
- you can identify which Menu Item sent the Click, in this example we have used
- 0,1,2 for Load Source, Load Mask and Load Destination. }
- procedure TForm1.LoadImage(Sender: TObject);
- begin
- {Display Common Dialog}
- if OpenDialog.Execute then
- begin
- Application.ProcessMessages; {Catch Up}
- {Make sure the Sender parameter is a 'TMenuItem' - in our Case it will be one of
- three. Either Load Source, Mask or Destination Image}
- With Sender as TMenuItem do
- Case Tag of {Reference TAG value of the MenuItem sending the Click}
- 0: {Load Source Menu Item}
- begin
- PicbufSrc.Filename:=OpenDialog.FileName; {Set FileName}
- PicbufSrc.Load; {Load Image}
- end;
- 1: {Load Mask Menu Item}
- begin
- PicbufMask.Filename:=OpenDialog.FileName; {Set FileName}
- PicbufMask.Load; {Load Image}
- end;
- 2: {Load Destination Menu Item}
- begin
- PicbufDest.Filename:=OpenDialog.FileName; {Set FileName}
- PicbufDest.Load; {Load Image}
- end;
- end;
- end;
- end;
-
- {-------------------------------------------------------------------------------}
- {Composite Button - Carry out the Mask procedure. Users should make sure they
- trap errors for 'non valid' Images - The demo has no traps.}
- procedure TForm1.CompositeClick(Sender: TObject);
- begin
- PicbufDest.MaskCopy(PicbufSrc.OLEOBJECT, PicbufMask.OLEOBJECT);
- end;
-
- {-------------------------------------------------------------------------------}
- {Lighten the Destination Image by using Masks}
- procedure TForm1.LightenClick(Sender: TObject);
- begin
- PicbufSrc.Init (24, PicbufDest.Xresolution, PicbufDest.Yresolution, RGB(255, 255, 255));
- PicbufMask.Init (24, PicbufDest.Xresolution, PicbufDest.Yresolution, RGB(75, 75, 75));
- CompositeClick(Sender); {Call the Same code used by 'composite' Button}
- end;
-
- {-------------------------------------------------------------------------------}
- {Darken the Destination Image by Using Masks}
- procedure TForm1.DarkenClick(Sender: TObject);
- begin
- PicbufSrc.Init (24, PicbufDest.Xresolution, PicbufDest.Yresolution, RGB(0, 0, 0));
- PicbufMask.Init (24, PicbufDest.Xresolution, PicbufDest.Yresolution, RGB(75, 75, 75));
- CompositeClick(Sender); {Call the Same code used by 'composite' Button}
- end;
-
- {-------------------------------------------------------------------------------}
- {Exit Application}
- procedure TForm1.Exit1Click(Sender: TObject);
- begin
- Halt;
- end;
-
- {-------------------------------------------------------------------------------}
- {Save Image based on file extension}
- procedure TForm1.SaveDestinationAs1Click(Sender: TObject);
- begin
- if OpenDialog.Execute then
- begin
- PicbufDest.FileName:=OpenDialog.Filename;
- {Check to see a Valid filename exists - See function Below}
- if ValidFormat(PicbufDest.FileName) then {Pass the fileName to the function}
- PicbufDest.Store {Save if Valid}
- else
- MessageDlg('Your File Extension is Not Valid.', mtInformation, [mbOk], 0);
- end;
- end;
-
- {-------------------------------------------------------------------------------)
- {Get Path of Default files:-
- Basically the functions gets the path name of the EXE location, strips of the last
- directory, ready for use - only applicable to this Demo. See Delphi on-line help}
- function GetImageLocation:String;
- Var
- Temp:String;
- DelphiLocation:Integer;
- begin
- Temp := ExtractFileDir(Application.exename); {Get full path of EXE}
- Temp := UpperCase(Temp); {Make Sure it is upper Case}
- DelphiLocation := Pos('\DELPHI2',Temp);
- Delete(Temp,DelphiLocation,length('\DELPHI2')); {Strip of last Directory}
- Result:=Temp + '\'; {Add the Missing '\'}
- 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;
-
- end.
-