home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 5 / MasteringVisualBasic5.iso / olympus / ik32_15t / delphi2.shr / UBRICON.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-01  |  13.4 KB  |  296 lines

  1. {  Project BrtCon.DPR Delphi 2.0 Demos
  2.  
  3.    Description:- BrtCon.Dpr Project:-
  4.  
  5.    Demonstrates the use of:
  6.  
  7.    1) 'BrightCont'
  8.    2) 'Gamma'
  9.    3) Undo Buffers and Scroll Bars
  10.    4) 'DuplicateDib' and 'ImportDib'
  11.  
  12.    Date of Origin: 15/04/96
  13.    Original Author: Andrew Hutchison
  14.    Modification History:
  15.  
  16.    Date        Person                            Change
  17.    ----------------------------------------------------
  18.    15/04/96    A Hutchison                       Created
  19.  
  20.    (c) Copyright Media Architects Inc. 1996.
  21.    All rights reserved.   No part of this program may be
  22.    photocopied, reproduced, translated to another programming
  23.    language or transported to any computer system without the
  24.    prior written consent of Media Architects Inc.}
  25.  
  26. unit UBriCon;
  27.  
  28. interface
  29.  
  30. uses
  31.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  32.   Buttons, ComCtrls, StdCtrls, ExtCtrls, OleCtrls, ImageKnife32, Menus,
  33.   Mask, Spin;
  34.  
  35. type
  36.   TMasterform = class(TForm)                                               {Form}
  37.     MainMenu: TMainMenu;                                                   {Menu}
  38.     File1: TMenuItem;                                                      {Menu}
  39.     LoadImage: TMenuItem;                                                  {Menu}
  40.     SaveImageAs: TMenuItem;                                                {Menu}
  41.     N1: TMenuItem;                                                         {Menu}
  42.     Exit1: TMenuItem;                                                      {Menu}
  43.     Bevel1: TBevel;
  44.     PicbufVisible: TPicbuf;                                              {Picbuf}
  45.     ProcessOption: TRadioGroup;                                   {Radio Buttons}
  46.     Undo: TSpeedButton;                                                  {Picbuf}
  47.     PicbufEdit: TPicbuf;                                                 {Picbuf}
  48.     PicbufOriginal: TPicbuf;                                             {Picbuf}
  49.     OpenDialog: TOpenDialog;                                      {Common Dialog}
  50.     TrackBar: TScrollBar;                                    {Scroll Bar Control}
  51.     Amount: TLabel;
  52.     procedure LoadImageClick(Sender: TObject);
  53.     procedure PicbufOriginalChange(Sender: TObject);
  54.     procedure TrackBarChange(Sender: TObject);
  55.     procedure UndoClick(Sender: TObject);
  56.     procedure Exit1Click(Sender: TObject);
  57.     procedure SaveImageAsClick(Sender: TObject);
  58.     procedure FormCreate(Sender: TObject);
  59.     procedure ProcessOptionClick(Sender: TObject);
  60.     procedure FormActivate(Sender: TObject);
  61.   private
  62.     { Private declarations }
  63.   public
  64.     { Public declarations }
  65.   end;
  66.  
  67. {Globals}
  68. var
  69. Masterform: TMasterform;{Delphi's}
  70. ContrastLevel,BrightnessLevel,GammaLevel: Variant;       {Used to Store Settings}
  71. function ValidFormat(FileName:String):Boolean;                        {See Below}
  72.  
  73. implementation
  74.  
  75. {$R *.DFM}
  76.  
  77.  
  78. {-------------------------------------------------------------------------------}
  79. {Set up any defaults when form is created}
  80. procedure TMasterform.FormCreate(Sender: TObject);
  81. begin
  82. Application.HintPause:=10;                                           {Hint Delay}
  83. Application.HintColor:=clAqua;                                      {Hint Colour}
  84. ContrastLevel := 0.5;                                            {Set to Neutral}
  85. BrightnessLevel := 0.5;                                          {Set to Neutral}
  86. GammaLevel := 5;                                                 {Set to Neutral}
  87. end;
  88.  
  89.  
  90. {-------------------------------------------------------------------------------}
  91. {File Menu Item - Load the Image selected}
  92. procedure TMasterform.LoadImageClick(Sender: TObject);
  93. begin
  94. {Load Image into 'PicbufOriginal' buffer}
  95. if OpenDialog.Execute then
  96.  begin
  97.  Application.Processmessages;                                          {Catch Up}
  98.  {Reset TrackBar and Settings - All to Default Values upon each new load}
  99.  TrackBar.Position := 50;                                            {Scroll Bar}
  100.  Gammalevel := 5;                                                   {Gamma Level}
  101.  BrightnessLevel := 0.5;                                             {Brightness}
  102.  ContrastLevel := 0.5;                                                 {Contrast}
  103.   With OpenDialog do        {Reference Common Dialog Component called OpenDialog}
  104.   PicbufOriginal.Filename := Filename;                             {Set FileName}
  105.   PicbufOriginal.Load;                        {Use Standard Picbuf Method 'Load'}
  106.  end;
  107. end;
  108.  
  109. {-------------------------------------------------------------------------------}
  110. {USING DIBS with change events of 'PicbufOriginal' Image.
  111.  
  112. When we Load the Image from the 'FileMenu', it is loaded into the 'PicbufOriginal'
  113. Picbuf,this causes a ChangeEvent to be triggered- use this event to copy the Image
  114. to the additional Picbufs.
  115.  
  116. When this occurs we then copy the Image to the 'PicbufEdit' and 'PicbufVisible'
  117. ie make a copy of it.  We use the 'DuplicateDIB' function [or assign if desired].
  118.  
  119. Delphi Users Note:-  We Cannot use True or False with the some  functions. You
  120. must use O for False or -1 for True. DO NOT REDEFINE TRUE / FALSE as constants ie
  121.  
  122. Const
  123. True = -1;
  124. False = 0;
  125.  
  126. This will cause a fatal error with other functions - Take the ImportDIB also used
  127. below. You will see it is using False as a parameter, but this is Delphi's False
  128. constant which is a 'Word' type by default.  If you had redefined True/False in the
  129. earlier step, False would have become an Integer and hence crash the call. So do not
  130. use constants - use True/False when a function accepts the standard Delphi Flag
  131. or use -1[true] or 0[false] for functions expecting shorts}
  132. procedure TMasterform.PicbufOriginalChange(Sender: TObject);
  133. Var
  134. hDIB : LongInt;
  135. begin
  136. {Copy to 'PicbufEdit'}
  137. hDib := PicbufOriginal.DuplicateDib(0);            {PASS 0 for FALSE -1 for TRUE}
  138. PicbufEdit.ImportDib(hDIB,False);                                {Use TRUE/FALSE}
  139. {Copy to 'PicbufVisible'}
  140. hDib := PicbufEdit.DuplicateDib(0);                {PASS 0 for FALSE -1 for TRUE}
  141. PicbufVisible.ImportDib(hDIB,False);                             {Use TRUE/FALSE}
  142. end;
  143.  
  144. {-------------------------------------------------------------------------------}
  145. {Apply Values linked to Bar Change:-
  146. This event sets the Contrast/Brightness depending on the setting of the 'ScrollBar
  147. Control'.  Since the Position parameter is passed to this sub by Delphi we can
  148. simply use the Position value/100 as a Brigtness or Contrast value.  Every Time
  149. you move the Scroll Bar the Image is adjusted. All effects are applied to the
  150. 'PicBufEdit' buffer, then copied to the 'PicbufVisible'. After each effect is
  151. applied the Original 'un-altered' Image is copied back to the 'PicbufEdit' buffer
  152. so each application of a new setting is applied to a copy of the ORIGINAL Image.
  153.  
  154. Users Note:= The Label is used to display the setting of the 'Scroll Bar'. In
  155. Order to display the real value '0.15' as a Caption we use the 'Str' function +
  156. 'Delete' to remove the decimal point and turn it into a string.}
  157. procedure TMasterform.TrackBarChange(Sender: TObject);
  158. var
  159. hdib : longint;
  160. LabelText:String[4];          {Need this to convert reals to Strings for Display}
  161. begin
  162. {Use Select Case to find out which option button is checked [ItemIndex] = option}
  163. case ProcessOption.ItemIndex of
  164.    0:{Brightness Selected - Set Contrast to NO CHANGE}
  165.      begin
  166.      Brightnesslevel := (Trackbar.position / 100);        { Get and Store Value }
  167.      PicbufEdit.BrightCont(Brightnesslevel,0.5);          {Apply to Picbuf Image}
  168.      {Update Label - after conversion to string}
  169.      str(Brightnesslevel,LabelText);                    {Convert Integer to Text}
  170.      Delete(labeltext,3,1);                                {Remove Decimal Point}
  171.      Amount.Caption:= '0.' + LabelText;                  {Display level in Label}
  172.    end;
  173.    1:{Contrast Selected - Set Brightness to NO CHANGE}
  174.      begin
  175.      ContrastLevel := (Trackbar.position / 100);          { Get and Store Value }
  176.      PicbufEdit.BrightCont(0.5,ContrastLevel);          { Apply to Picbuf Image }
  177.      {Update Label - after conversion to string}
  178.      str(Contrastlevel,LabelText);                      {Convert Integer to Text}
  179.      Delete(labeltext,3,1);                                {Remove Decimal Point}
  180.      Amount.Caption := '0.' + LabelText;                 {Display level in Label}
  181.      end;
  182.    2:{Gamma Selected}
  183.      begin
  184.      {With Gamma we must make sure 0 is never set and an Integer is set}
  185.      Gammalevel:= int(Trackbar.Position / 10);
  186.      if GammaLevel < 1 then GammaLevel := 1;
  187.      PicbufEdit.Gamma(GammaLevel);                        {Apply to Picbuf Image}
  188.      {Update Label - After conversion to string}
  189.      Amount.Caption := inttostr(GammaLevel);             {Display level in Label}
  190.      end;
  191.  end;{End Case}
  192.  
  193. {Copy corrected / adjusted Image to visible Image ie- display the adjusted Image}
  194. hDib := PicbufEdit.DuplicateDib(0);                {PASS 0 for FALSE -1 for TRUE}
  195. PicbufVisible.ImportDib(hDIB,False);                             {Use TRUE/FALSE}
  196.  
  197. {Then Reload Original Image to Edit - stops cumulative results}
  198. hDib := PicbufOriginal.DuplicateDib(0);            {PASS 0 for FALSE -1 for TRUE}
  199. PicbufEdit.ImportDib(hDIB,False);                                {Use TRUE/FALSE}
  200. end;{End Sub}
  201.  
  202. {-------------------------------------------------------------------------------}
  203. {Undo Changes and Reset TrackBar}
  204. procedure TMasterform.UndoClick(Sender: TObject);
  205. begin
  206. {Undo - simply recall 'OriginalPicbuf Change Event'}
  207. Masterform.PicbufOriginalChange(Sender);                          {Restore Image}
  208. TrackBar.Position:= 50;                               {Reset to Track Bar Middle}
  209. Gammalevel := 5;                                                  {Reset Globals}
  210. BrightnessLevel := 0.5;                                           {Reset Globals}
  211. ContrastLevel := 0.5;                                             {Reset Globals}
  212. ProcessOption.ItemIndex:=0;                                       {Option Button}
  213. end;
  214.  
  215. {-------------------------------------------------------------------------------}
  216. {Close Application}
  217. procedure TMasterform.Exit1Click(Sender: TObject);
  218. begin
  219. Halt;                                                                 {Close All}
  220. end;
  221.  
  222.  
  223. {-------------------------------------------------------------------------------}
  224. {Save Image to Disk - See Below for 'ValidFormat' function}
  225. procedure TMasterform.SaveImageAsClick(Sender: TObject);
  226. begin
  227. {Save visible - reuse the 'OpenDialog' control}
  228. if OpenDialog.Execute then
  229.  begin
  230.  Application.Processmessages;
  231.   With OpenDialog do
  232.   PicbufVisible.Filename := Filename;                              {Set FileName}
  233.   if ValidFormat(PicbufVisible.Filename) then       {Is the File Extansion Valid}
  234.    PicbufVisible.Store                     {Standard call to Picbuf Store Method}
  235.     else
  236.     MessageDlg('Your File Extension is Not Valid.', mtInformation, [mbOk], 0);
  237.     end;
  238. end;
  239.  
  240.  
  241. {-------------------------------------------------------------------------------}
  242. {Set Scroll Bar to reflect current value of each variable - When you 'Pick' one of
  243. the options, set the 'TrackBar' position to reflect the current setting. Update
  244. caption labels at the same time. Note:- You must convert the real values into a
  245. form compatible with the Caption property of a Label.}
  246. procedure TMasterform.ProcessOptionClick(Sender: TObject);
  247. var
  248. LabelText:String[4];                                             {Used for Label}
  249. begin
  250. case ProcessOption.ItemIndex of             {Use ItemIndex of Radio Button Group}
  251. 0:TrackBar.Position := round(BrightnessLevel * 100);           {Set to Brigtness}
  252. 1:TrackBar.Position := round(ContrastLevel * 100);              {Set to Contrast}
  253. 2:
  254. begin
  255. TrackBar.Position := round(Gammalevel * 10);                      {Set to Gammea}
  256. Amount.Caption:= inttostr(TrackBar.Position div 10);         {Update Gamma Label}
  257. Exit;  {Make sure we Exit here if Gamma was clicked - stops further label update}
  258. end;
  259. end;{End Case}
  260. {Update Label - After conversion to string for Brightness and Contrast}
  261. str(TrackBar.Position / 100, LabelText);                {Convert Integer to Text}
  262. Delete(labeltext,3,1);                                     {Remove Decimal Point}
  263. Amount.Caption:= '0.' + LabelText;                                 {Update Label}
  264. end;{End Sub}
  265.  
  266.  
  267. {-------------------------------------------------------------------------------}
  268. { This function simply checks to see if any one of the listed ImageKnife formats
  269. exists in the filename passed to the function. If it does then the function
  270. evaluates to true - Note this is the 'RESULT' of the function}
  271. function ValidFormat(FileName:String):Boolean;
  272. Var
  273. Temp:String;
  274. begin
  275. Temp := UpperCase(Filename);{Convert FileName to Upper Case}
  276. Result:=False;{Default result if no recognised match is found - *.*}
  277. if Pos('.TIF', Temp ) > 0 then Result:= True;
  278. if Pos('.TGA', Temp ) > 0 then Result:= True;
  279. if Pos('.BMP', Temp ) > 0 then Result:= True;
  280. if Pos('.GIF', Temp ) > 0 then Result:= True;
  281. if Pos('.DIB', Temp ) > 0 then Result:= True;
  282. if Pos('.PCX', Temp ) > 0 then Result:= True;
  283. if Pos('.JPG', Temp ) > 0 then Result:= True;
  284. if Pos('.MSP', Temp ) > 0 then Result:= True;
  285. if Pos('.FIF', Temp ) > 0 then Result:= True;
  286. if Pos('.PNG', Temp ) > 0 then Result:= True;
  287. end;
  288.  
  289. procedure TMasterform.FormActivate(Sender: TObject);
  290. begin
  291.   PicBufOriginal.Filename := '..\Images\leaves8.pcx';
  292.   PicBufOriginal.Load;
  293. end;
  294.  
  295. end.
  296.