home *** CD-ROM | disk | FTP | other *** search
- { Project Palette.DPR Delphi 2.0 Demos
-
- Description:- Palette.Dpr Project:-
-
- Demonstrates the use of:
-
- 1) 'ConvertDepth - Increase/Reduce Colors'
- 2) 'LoadPal'
- 3) 'SavePal'
- 4) 'AddSystemColors'
- 5) 'SetScreenPal'
-
- Date of Origin: 18/04/96
- Original Author: Andrew Hutchison
- Modification History:
-
- Date Person Change
- ----------------------------------------------------
- 18/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 Upalette;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- ExtCtrls, OleCtrls, ImageKnife32, Menus, StdCtrls;
-
- type
- TForm1 = class(TForm)
- MainMenu1: TMainMenu;
- File1: TMenuItem;
- LoadImage1: TMenuItem;
- N1: TMenuItem;
- Exit1: TMenuItem;
- Palette1: TMenuItem;
- LoadPalette1: TMenuItem;
- SavePalette1: TMenuItem;
- AddSystemColours1: TMenuItem;
- RealiseCurrentPalette: TMenuItem;
- Picbuf1: TPicbuf;
- Bevel1: TBevel;
- OpenDialog: TOpenDialog;
- N2: TMenuItem;
- DepthOption: TRadioGroup;
- Label1: TLabel;
- SystemColours: TLabel;
- Label3: TLabel;
- BitDepth: TLabel;
- Bevel2: TBevel;
- Bevel3: TBevel;
- procedure LoadImage1Click(Sender: TObject);
- procedure DepthOptionClick(Sender: TObject);
- procedure LoadPalette1Click(Sender: TObject);
- procedure SavePalette1Click(Sender: TObject);
- procedure Exit1Click(Sender: TObject);
- procedure AddSystemColours1Click(Sender: TObject);
- procedure RealiseCurrentPaletteClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormActivate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- Var
- Onload : Boolean;{Used in Load Call}
- {See Below}
- Procedure ConvertDepth(ImageControl:TPicbuf;ConvertDepth:Integer);
- {See Below}
- Procedure SetInfo(ImageControl:TPicbuf);
-
- implementation
-
- {$R *.DFM}
-
- {-------------------------------------------------------------------------------}
- {Load Image into Visible Picture Buffer - Menu Item Call - Check the file does
- not have the extension '*.Pal'}
- procedure TForm1.LoadImage1Click(Sender: TObject);
- var
- Temp:String;
- begin
- Onload := True; {Set Flag - Stops Convert 'ColorDepth' being called on a load}
- if OpenDialog.Execute then
- begin
- {Set FileName}
- Picbuf1.Filename:=OpenDialog.Filename;
- {Convert FileName to Upper Case and store in 'TEMP'}
- Temp:=Uppercase(OpenDialog.Filename);
- {Use POS function to check that '*.PAL' file has not been Loaded [Pos:Delphi Help]}
- if Pos('.PAL', Temp) = 0 then
- {Call Load Method if the file extension is NOT '.PAL' - if .PAl Display Message}
- Picbuf1.Load
- else
- MessageDlg('Use Load Palette Option for Palette [.PAL] files.',mtInformation,[mbOk],0);
- end;
- SetInfo(Picbuf1); {Update Labels to Loaded Image - See Below}
- Onload := False; {Reset Flag - Stops Color Depth being called after a Load}
- end;
-
-
- {--------------------------------------------------------------------------------}
- {Load a Palette file - *.pal. Prompt user for non valid '.PAL' files}
- procedure TForm1.LoadPalette1Click(Sender: TObject);
- var
- Temp:String;{Temp String}
- begin
- OpenDialog.FilterIndex:=12;{Set Dialog to *.Pal Filter Index}
- if OpenDialog.Execute then
- begin
-
- {Set FileName}
- Picbuf1.Filename:=OpenDialog.Filename;
-
- {Convert FileName to Upper Case and store in 'TEMP'}
- Temp:=Uppercase(OpenDialog.Filename);
-
- {Use POS function to check that a valid '*.PAL' file has been Loaded [Pos:Delphi Help]}
- if Pos('.PAL', Temp) > 0 then
-
- {Call Load Method if the file had the extension .PAL - if not Display Message}
- Picbuf1.LoadPal
- else
- MessageDlg('Non Valid Palette file. File must have extension [*.PAL].',mtInformation,[mbOk],0);
- end;
- end;
-
- {-------------------------------------------------------------------------------}
- {Store Picbuf1's palette to Disk. Again prompt the user for incorrect file
- extensions. IE:- Should Save a palette file as a *.PAL [Not required but good
- programming practice}
- procedure TForm1.SavePalette1Click(Sender: TObject);
- var
- Temp:String;
- begin
- OpenDialog.FilterIndex := 12;{Set Dialog to *.Pal Filter Index}
- if OpenDialog.Execute then
- begin
-
- {Set FileName}
- Picbuf1.Filename:=OpenDialog.Filename;
-
- {Convert to upper case for match check - Win95/NT is case sensitive}
- Temp := Uppercase(OpenDialog.Filename);
-
- {Use POS function to check that a valid '*.PAL' file has been Loaded [Pos:Delphi Help]}
- if Pos('.PAL', Temp) > 0 then {Post returns start location of string '.PAL' in Temp $}
-
- {Call StorePal Method if the file had the extension .PAL - if not Display Message}
- Picbuf1.StorePal {Store if Valid}
- else
- MessageDlg('Non Valid Palette file name. File must have extension [*.PAL].',mtInformation,[mbOk],0);
- end;
- end;
-
- {-------------------------------------------------------------------------------}
- {When the user picks a depth Radio Button ['DepthOption'], using the ItemIndex to
- determine what depth is required, call the 'ConvertDepth' function, passing the
- function the control name and the required depth. The function will check for
- valid combinations. See Below for function.}
- procedure TForm1.DepthOptionClick(Sender: TObject);
- begin
- if Onload then Exit;{Do Not Call function when an Image is Loaded}
- Case DepthOption.ItemIndex of {Radio Button Group}
- 0:ConvertDepth(Picbuf1,4); {Convert to 4 Bit}
- 1:ConvertDepth(Picbuf1,8); {Convert to 8 Bit}
- 2:ConvertDepth(Picbuf1,24);{Convert to 24 Bit}
- end;
- SetInfo(Picbuf1);{Call update procedure to refresh labels}
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Convert any Picbuf Image to any given bit Depth 4/8/24. This function can be
- re-used for any picbuf control. Just pass control name and requested depth. -
- Pass the function the 'Picbuf Name', and the 'Required Depth'}
- Procedure ConvertDepth(ImageControl:TPicbuf;ConvertDepth:Integer);
- begin
- try
-
- {Prompt User if the Control is already the requested Depth}
- If ImageControl.Colordepth = ConvertDepth then
- begin
- MessageDlg('The Image already is ' + InttoStr(ConvertDepth)+ ' Bit. OK to Continue.'
- ,mtInformation,[mbOk], 0); {Image already at requested depth}
- Application.Processmessages;
- exit;{Abort}
- end;
-
- {If the Requested Depth is greater than the current depth then use IncreaseColors}
- if ConvertDepth > ImageControl.Colordepth then
- Imagecontrol.IncreaseColors(ConvertDepth)
- {If the above is not true then use ReduceColors to get requested Depth}
- else
- begin
- case ConvertDepth of {Set Requested - 'ConvertDepth' is the requested depth}
- 4: ImageControl.Reducecolors(16, True, True, True); {reduce to 4}
- 8: ImageControl.Reducecolors(254, True, True, True);{reduce to 8}
- end;
- end;
- except
- MessageDlg('Image Conversion Error. Code UPalette 194. OK to Continue.',mtInformation,[mbOk],0);
- end;
- end;
-
- {-------------------------------------------------------------------------------}
- {Add System Colors to Image - 8 Bit Images Only}
- procedure TForm1.AddSystemColours1Click(Sender: TObject);
- begin
- Picbuf1.AddSystemColors;
- end;
-
- {-------------------------------------------------------------------------------}
- {Update all the Lables as Required. This procedure can be called at any time - just
- pass the Name of the Control to get the info from - SetInfo(MyPicbuf). Note:- The
- 'Onload Flag' is used because when you load an Image this procedure is called. If
- the Image has just been Loaded the Flag is TRUE. When this procedure is called,
- the RadioButton's are set to reflect the current Bit Depth of the Image. This
- causes the RadioButtons to call the ConvertDepth Procedure. So for a new
- Image Loaded, without the Flag, everytime you loaded an Image, the ConvertDepth
- sub would be called, causing the convert Depth to Display its Message Display,
- stating that the Image already is at the requested depth. What the Flag does is
- to Skip the ConvertDepth call if the Image was just loaded.}
- procedure SetInfo(ImageControl:TPicbuf);
- begin
- case ImageControl.ColorDepth of {Color Depth of Image}
-
- 4:{4 Bit}
- begin
- Form1.DepthOption.ItemIndex:= 0; {Set Radio Button to Image Depth}
- Form1.RealiseCurrentPalette.Enabled := True; {Enable Realise Menu Option}
- Form1.SystemColours.Caption := 'N/A'; {Set Caption to N/A for 4 Bit}
- Form1.BitDepth.Caption:=Inttostr(ImageControl.ColorDepth); {Display Bitdepth}
- end;
-
- 8:{8 Bit}
- begin
- Form1.DepthOption.ItemIndex:= 1;
- Form1.RealiseCurrentPalette.Enabled := True;
- Form1.SystemColours.Caption := inttostr(ImageControl.CountSystemColors);
- Form1.BitDepth.Caption:=Inttostr(ImageControl.ColorDepth);
- end;
-
- 24:{24 Bit}
- begin
- Form1.DepthOption.ItemIndex:= 2;
- Form1.RealiseCurrentPalette.Enabled := False;
- Form1.SystemColours.Caption := 'N/A';
- Form1.BitDepth.Caption:=Inttostr(ImageControl.ColorDepth);
- end;
- end;
- end;
-
- {--------------------------------------------------------------------------------}
- {Menu Item - Realise Palette}
- procedure TForm1.RealiseCurrentPaletteClick(Sender: TObject);
- begin
- Picbuf1.SetScreenPal;
- end;
-
- {-------------------------------------------------------------------------------}
- {Exit Application}
- procedure TForm1.Exit1Click(Sender: TObject);
- begin
- Halt;
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Set Defaults}
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Application.HintPause:=10;
- Application.HintColor:=clAqua;
- end;
-
- procedure TForm1.FormActivate(Sender: TObject);
- begin
- Picbuf1.Filename:='..\images\marybeth.tif';
- PicBuf1.Load;
- PicBuf1.ScrollBars := SB_Both;
- end;
-
- end.
-