Brightness Manipulation in Delphi

To manipulate an image's brightness is easy, first we can make the graphical user interface (GUI) to ease the manipulation. The GUI consist of a form with three sliding bar (TScrollBar). Each scroll bar is used to adjust the brightness constant for each color element.
  1. Open our previous image histogram project (you can download the source code there if you don't have it).
  2. Create a new form (File->New->Form).
  3. Change the name property to BrightnessForm, change the Caption property to 'Brightness'. Set the FormStyle property to fsMDIChild. Resize the form to fit the main form (parent form).
  4. On the Object Inspector, click on Events tab, and double click in the OnClose event. You'll be directed to the event handler, and assign the Action variable with caFree between begin and end.

    procedure TBrightnessForm.FormClose(Sender: TObject;
    var Action: TCloseAction);
    begin
    Action:=caFree;
    end;

  5. Add three scroll bar components (from the standard component pallet) to the form. Change the Name property to RedScrollBar, GreenScrollBar, and BlueScrollBar. Set the minimum and maximum property of each scroll bar to -255 and 255.
  6. Place three label components (from the standar component pallet) to the form, place the layout to associate each label to each scroll bar. Change the Label's caption to Red, Green, and Blue, to label the scroll bar functions (See Figure 1).
  7. Add two buttons (from standard component pallet), change their caption to OK and Cancel, and set their name to OKButton and CancelButton accordingly.
  8. Save the unit as BrightnessUnit (using menu File->Save As).
  9. Go to Project->Options->Form, point to BrightnessForm to select, and press the > button, so the BrightessForm will move to Available forms box, then press OK.


Figure 1. Brightness Modification GUI

To modify the image, it's common to backup the original image, so we can do a cancel operation after displaying the change on the original image form. To do this, we have to provide TImage object in the form to temporarily store the image. A procedure (SetBrightness) to associate the image input to the brightness form is also needed.
  1. Edit the BrightnessForm abstraction to provide TImage object and the SetBrightness procedure. Add ExtCtrls under uses in the Interface section because we're gonna use TImage component.
    uses
    ExtCtrls, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls

    type= class(TForm)
    TBrightnessForm = class(TForm)
    RedScrollBar: TScrollBar;
    GreenScrollBar: TScrollBar;
    BlueScrollBar: TScrollBar;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    OKButton: TButton;
    CancelButton: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    private
    { Private declarations }
    TemporaryImage:TImage;
    OriginalImage:TImage;
    Applied:boolean;
    public
    { Public declarations }
    procedure SetBrightness(Image: TImage);
    end;
  2. Write the SetBrightness procedure in the implementation section
    procedure TBrightnessForm.SetBrightness(Image: TImage);
    begin
    try
    begin
    TemporaryImage:=Image;
    OriginalImage:=TImage.Create(self);
    OriginalImage.Picture.Bitmap.Assign(Image.Picture.Bitmap);
    end;
    except
    begin
    Free; //free the brightness form
    ShowMessage('Cannot complete the operation');
    end;
    end;
    end;

  3. Double click the RedScrollBar component, the you'll be directed to its event handler, edit as shown below

    procedure TBrightnessForm.RedScrollBarChange(Sender: TObject);
    var
    i,j:Integer;
    temp:integer;
    pixelPointer:PByteArray;
    originalPixelPointer:PByteArray;
    begin
    try
    begin
    for i:=0 to TemporaryImage.Picture.Height-1 do
    begin
    pixelPointer:=TemporaryImage.Picture.Bitmap.ScanLine[i];
    originalPixelPointer:=OriginalImage.Picture.Bitmap.ScanLine[i];
    for j:=0 to TemporaryImage.Picture.Width-1 do
    begin
    temp:=originalPixelPointer[3*j+2]+ RedScrollBar.Position;
    if temp<0 then temp:=0;
    if temp>255 then temp:=255;
    pixelPointer[3*j+2]:=temp;
    end;
    end;
    TemporaryImage.Refresh;
    end;
    except
    begin
    Free;
    ShowMessage('Cannot complete the operation');
    end;
    end;
    end;

  4. Double click the GreenScrollBar component, the you'll be directed to its event handler, edit as shown below

    procedure TBrightnessForm.GreenScrollBarChange(Sender: TObject);
    var
    i,j:Integer;
    temp:integer;
    pixelPointer:PByteArray;
    originalPixelPointer:PByteArray;
    begin
    try
    begin
    for i:=0 to TemporaryImage.Picture.Height-1 do
    begin
    pixelPointer:=TemporaryImage.Picture.Bitmap.ScanLine[i];
    originalPixelPointer:=OriginalImage.Picture.Bitmap.ScanLine[i];
    for j:=0 to TemporaryImage.Picture.Width-1 do
    begin
    temp:=originalPixelPointer[3*j+1]+ GreenScrollBar.Position;
    if temp<0 then temp:=0;
    if temp>255 then temp:=255;
    pixelPointer[3*j+1]:=temp;
    end;
    end;
    TemporaryImage.Refresh;
    end;
    except
    begin
    Free;
    ShowMessage('Cannot complete the operation');
    end;
    end;
    end;

  5. Double click the BlueScrollBar component, the you'll be directed to its event handler, edit as shown below

    procedure TBrightnessForm.BlueScrollBarChange(Sender: TObject);
    var
    i,j:Integer;
    temp:integer;
    pixelPointer:PByteArray;
    originalPixelPointer:PByteArray;
    begin
    try
    begin
    for i:=0 to TemporaryImage.Picture.Height-1 do
    begin
    pixelPointer:=TemporaryImage.Picture.Bitmap.ScanLine[i];
    originalPixelPointer:=OriginalImage.Picture.Bitmap.ScanLine[i];
    for j:=0 to TemporaryImage.Picture.Width-1 do
    begin
    temp:=originalPixelPointer[3*j]+ BlueScrollBar.Position;
    if temp<0 then temp:=0;
    if temp>255 then temp:=255;
    pixelPointer[3*j]:=temp;
    end;
    end;
    TemporaryImage.Refresh;
    end;
    except
    begin
    Free;
    ShowMessage('Cannot complete the operation');
    end;
    end;
    end;

  6. Double click OKButton component, you'll be directed to its event handler, and edit it as shown below:

    procedure TBrightnessForm.OKButtonClick(Sender: TObject);
    begin
    Applied:=true;
    Close();
    end;
  7. Double click CancelButton component, you'll be directed to its event handler, and edit it as shown below:

    procedure TBrightnessForm.OKButtonClick(Sender: TObject);
    begin
    Applied:=false;
    Close();
    end;
  8. Edit BrightnessForm's OnClose even handler as shown below:

    procedure TBrightnessForm.FormClose(Sender: TObject;
    var Action: TCloseAction);
    begin
    if Applied=false then
    TemporaryImage.Picture.Bitmap.Assign(
    OriginalImage.Picture.Bitmap);
    Action:=caFree;
    end;
Showing The Brightness Manipulation Form from Main Form
Now you can call the brightness form from main form. To do this, we need to add a menu to provide the access, double click the main menu component in the main form, and add Brightness menu item under menu Image (Image->Brightness), see Figure 2.

Figure 2. Brightness Menu

Write the event handler for the menu as shown below:

procedure TMainForm.Brightness1Click(Sender: TObject);
begin
if ImageForm<>nil then
begin
ImageForm:=TImageForm(ActiveMDIChild);
try
begin
Application.CreateForm(TBrightnessForm,BrightnessForm);
Brightnessform.SetBrightness(ImageForm.Image1);
end;
except
BrightnessForm.Free;
ShowMessage('Cannot complete the operation');
end;
end;
end;


Figure 3. Brightness Modification Execution

Don't forget to add BrightnessUnit under the uses in the MainUnit's implementation. Use menu File->Use Unit (Alt+F11). Now you can save, compile and run the executable. The execution look like figure 3. Here you can download the source code for Delphi 7 project , and Turbo Delphi Explorer project source code here.

0 comments: