home *** CD-ROM | disk | FTP | other *** search
- { Project BrtCon.DPR Delphi 2.0 Demos
-
- Description:- BrtCon.Dpr Project:-
-
- Demonstrates the use of:
-
- 1) 'BrightCont'
- 2) 'Gamma'
- 3) Undo Buffers and Scroll Bars
- 4) 'DuplicateDib' and 'ImportDib'
-
- Date of Origin: 15/04/96
- Original Author: Andrew Hutchison
- Modification History:
-
- Date Person Change
- ----------------------------------------------------
- 15/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 UBriCon;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- Buttons, ComCtrls, StdCtrls, ExtCtrls, OleCtrls, ImageKnife32, Menus,
- Mask, Spin;
-
- type
- TMasterform = class(TForm) {Form}
- MainMenu: TMainMenu; {Menu}
- File1: TMenuItem; {Menu}
- LoadImage: TMenuItem; {Menu}
- SaveImageAs: TMenuItem; {Menu}
- N1: TMenuItem; {Menu}
- Exit1: TMenuItem; {Menu}
- Bevel1: TBevel;
- PicbufVisible: TPicbuf; {Picbuf}
- ProcessOption: TRadioGroup; {Radio Buttons}
- Undo: TSpeedButton; {Picbuf}
- PicbufEdit: TPicbuf; {Picbuf}
- PicbufOriginal: TPicbuf; {Picbuf}
- OpenDialog: TOpenDialog; {Common Dialog}
- TrackBar: TScrollBar; {Scroll Bar Control}
- Amount: TLabel;
- procedure LoadImageClick(Sender: TObject);
- procedure PicbufOriginalChange(Sender: TObject);
- procedure TrackBarChange(Sender: TObject);
- procedure UndoClick(Sender: TObject);
- procedure Exit1Click(Sender: TObject);
- procedure SaveImageAsClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure ProcessOptionClick(Sender: TObject);
- procedure FormActivate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- {Globals}
- var
- Masterform: TMasterform;{Delphi's}
- ContrastLevel,BrightnessLevel,GammaLevel: Variant; {Used to Store Settings}
- function ValidFormat(FileName:String):Boolean; {See Below}
-
- implementation
-
- {$R *.DFM}
-
-
- {-------------------------------------------------------------------------------}
- {Set up any defaults when form is created}
- procedure TMasterform.FormCreate(Sender: TObject);
- begin
- Application.HintPause:=10; {Hint Delay}
- Application.HintColor:=clAqua; {Hint Colour}
- ContrastLevel := 0.5; {Set to Neutral}
- BrightnessLevel := 0.5; {Set to Neutral}
- GammaLevel := 5; {Set to Neutral}
- end;
-
-
- {-------------------------------------------------------------------------------}
- {File Menu Item - Load the Image selected}
- procedure TMasterform.LoadImageClick(Sender: TObject);
- begin
- {Load Image into 'PicbufOriginal' buffer}
- if OpenDialog.Execute then
- begin
- Application.Processmessages; {Catch Up}
- {Reset TrackBar and Settings - All to Default Values upon each new load}
- TrackBar.Position := 50; {Scroll Bar}
- Gammalevel := 5; {Gamma Level}
- BrightnessLevel := 0.5; {Brightness}
- ContrastLevel := 0.5; {Contrast}
- With OpenDialog do {Reference Common Dialog Component called OpenDialog}
- PicbufOriginal.Filename := Filename; {Set FileName}
- PicbufOriginal.Load; {Use Standard Picbuf Method 'Load'}
- end;
- end;
-
- {-------------------------------------------------------------------------------}
- {USING DIBS with change events of 'PicbufOriginal' Image.
-
- When we Load the Image from the 'FileMenu', it is loaded into the 'PicbufOriginal'
- Picbuf,this causes a ChangeEvent to be triggered- use this event to copy the Image
- to the additional Picbufs.
-
- When this occurs we then copy the Image to the 'PicbufEdit' and 'PicbufVisible'
- ie make a copy of it. We use the 'DuplicateDIB' function [or assign if desired].
-
- Delphi Users Note:- We Cannot use True or False with the some functions. You
- must use O for False or -1 for True. DO NOT REDEFINE TRUE / FALSE as constants ie
-
- Const
- True = -1;
- False = 0;
-
- This will cause a fatal error with other functions - Take the ImportDIB also used
- below. You will see it is using False as a parameter, but this is Delphi's False
- constant which is a 'Word' type by default. If you had redefined True/False in the
- earlier step, False would have become an Integer and hence crash the call. So do not
- use constants - use True/False when a function accepts the standard Delphi Flag
- or use -1[true] or 0[false] for functions expecting shorts}
- procedure TMasterform.PicbufOriginalChange(Sender: TObject);
- Var
- hDIB : LongInt;
- begin
- {Copy to 'PicbufEdit'}
- hDib := PicbufOriginal.DuplicateDib(0); {PASS 0 for FALSE -1 for TRUE}
- PicbufEdit.ImportDib(hDIB,False); {Use TRUE/FALSE}
- {Copy to 'PicbufVisible'}
- hDib := PicbufEdit.DuplicateDib(0); {PASS 0 for FALSE -1 for TRUE}
- PicbufVisible.ImportDib(hDIB,False); {Use TRUE/FALSE}
- end;
-
- {-------------------------------------------------------------------------------}
- {Apply Values linked to Bar Change:-
- This event sets the Contrast/Brightness depending on the setting of the 'ScrollBar
- Control'. Since the Position parameter is passed to this sub by Delphi we can
- simply use the Position value/100 as a Brigtness or Contrast value. Every Time
- you move the Scroll Bar the Image is adjusted. All effects are applied to the
- 'PicBufEdit' buffer, then copied to the 'PicbufVisible'. After each effect is
- applied the Original 'un-altered' Image is copied back to the 'PicbufEdit' buffer
- so each application of a new setting is applied to a copy of the ORIGINAL Image.
-
- Users Note:= The Label is used to display the setting of the 'Scroll Bar'. In
- Order to display the real value '0.15' as a Caption we use the 'Str' function +
- 'Delete' to remove the decimal point and turn it into a string.}
- procedure TMasterform.TrackBarChange(Sender: TObject);
- var
- hdib : longint;
- LabelText:String[4]; {Need this to convert reals to Strings for Display}
- begin
- {Use Select Case to find out which option button is checked [ItemIndex] = option}
- case ProcessOption.ItemIndex of
- 0:{Brightness Selected - Set Contrast to NO CHANGE}
- begin
- Brightnesslevel := (Trackbar.position / 100); { Get and Store Value }
- PicbufEdit.BrightCont(Brightnesslevel,0.5); {Apply to Picbuf Image}
- {Update Label - after conversion to string}
- str(Brightnesslevel,LabelText); {Convert Integer to Text}
- Delete(labeltext,3,1); {Remove Decimal Point}
- Amount.Caption:= '0.' + LabelText; {Display level in Label}
- end;
- 1:{Contrast Selected - Set Brightness to NO CHANGE}
- begin
- ContrastLevel := (Trackbar.position / 100); { Get and Store Value }
- PicbufEdit.BrightCont(0.5,ContrastLevel); { Apply to Picbuf Image }
- {Update Label - after conversion to string}
- str(Contrastlevel,LabelText); {Convert Integer to Text}
- Delete(labeltext,3,1); {Remove Decimal Point}
- Amount.Caption := '0.' + LabelText; {Display level in Label}
- end;
- 2:{Gamma Selected}
- begin
- {With Gamma we must make sure 0 is never set and an Integer is set}
- Gammalevel:= int(Trackbar.Position / 10);
- if GammaLevel < 1 then GammaLevel := 1;
- PicbufEdit.Gamma(GammaLevel); {Apply to Picbuf Image}
- {Update Label - After conversion to string}
- Amount.Caption := inttostr(GammaLevel); {Display level in Label}
- end;
- end;{End Case}
-
- {Copy corrected / adjusted Image to visible Image ie- display the adjusted Image}
- hDib := PicbufEdit.DuplicateDib(0); {PASS 0 for FALSE -1 for TRUE}
- PicbufVisible.ImportDib(hDIB,False); {Use TRUE/FALSE}
-
- {Then Reload Original Image to Edit - stops cumulative results}
- hDib := PicbufOriginal.DuplicateDib(0); {PASS 0 for FALSE -1 for TRUE}
- PicbufEdit.ImportDib(hDIB,False); {Use TRUE/FALSE}
- end;{End Sub}
-
- {-------------------------------------------------------------------------------}
- {Undo Changes and Reset TrackBar}
- procedure TMasterform.UndoClick(Sender: TObject);
- begin
- {Undo - simply recall 'OriginalPicbuf Change Event'}
- Masterform.PicbufOriginalChange(Sender); {Restore Image}
- TrackBar.Position:= 50; {Reset to Track Bar Middle}
- Gammalevel := 5; {Reset Globals}
- BrightnessLevel := 0.5; {Reset Globals}
- ContrastLevel := 0.5; {Reset Globals}
- ProcessOption.ItemIndex:=0; {Option Button}
- end;
-
- {-------------------------------------------------------------------------------}
- {Close Application}
- procedure TMasterform.Exit1Click(Sender: TObject);
- begin
- Halt; {Close All}
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Save Image to Disk - See Below for 'ValidFormat' function}
- procedure TMasterform.SaveImageAsClick(Sender: TObject);
- begin
- {Save visible - reuse the 'OpenDialog' control}
- if OpenDialog.Execute then
- begin
- Application.Processmessages;
- With OpenDialog do
- PicbufVisible.Filename := Filename; {Set FileName}
- if ValidFormat(PicbufVisible.Filename) then {Is the File Extansion Valid}
- PicbufVisible.Store {Standard call to Picbuf Store Method}
- else
- MessageDlg('Your File Extension is Not Valid.', mtInformation, [mbOk], 0);
- end;
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Set Scroll Bar to reflect current value of each variable - When you 'Pick' one of
- the options, set the 'TrackBar' position to reflect the current setting. Update
- caption labels at the same time. Note:- You must convert the real values into a
- form compatible with the Caption property of a Label.}
- procedure TMasterform.ProcessOptionClick(Sender: TObject);
- var
- LabelText:String[4]; {Used for Label}
- begin
- case ProcessOption.ItemIndex of {Use ItemIndex of Radio Button Group}
- 0:TrackBar.Position := round(BrightnessLevel * 100); {Set to Brigtness}
- 1:TrackBar.Position := round(ContrastLevel * 100); {Set to Contrast}
- 2:
- begin
- TrackBar.Position := round(Gammalevel * 10); {Set to Gammea}
- Amount.Caption:= inttostr(TrackBar.Position div 10); {Update Gamma Label}
- Exit; {Make sure we Exit here if Gamma was clicked - stops further label update}
- end;
- end;{End Case}
- {Update Label - After conversion to string for Brightness and Contrast}
- str(TrackBar.Position / 100, LabelText); {Convert Integer to Text}
- Delete(labeltext,3,1); {Remove Decimal Point}
- Amount.Caption:= '0.' + LabelText; {Update Label}
- end;{End Sub}
-
-
- {-------------------------------------------------------------------------------}
- { This function simply checks to see if any one of the listed ImageKnife formats
- exists 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;
- 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;
-
- procedure TMasterform.FormActivate(Sender: TObject);
- begin
- PicBufOriginal.Filename := '..\Images\leaves8.pcx';
- PicBufOriginal.Load;
- end;
-
- end.
-