unit bmpform;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, Grids, ExtCtrls, CheckLst, ExtDlgs, Menus,
  ComCtrls;

type
  TformBmp = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Panel2: TPanel;
    TrackBar1: TTrackBar;
    pbox: TPaintBox;

    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure pboxPaint(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    orgbmp,newbmp: Tbitmap;
    Function EditBitmap(xbmp:Tbitmap): Boolean;
  end;

const
    MaxPixelCount   =  32768;

type
    pRGBArray  =  ^TRGBArray;
    TRGBArray  =  ARRAY[0..MaxPixelCount-1] OF TRGBTriple;

var
  formBmp: TformBmp;



implementation

{$R *.DFM}

procedure TformBmp.Button1Click(Sender: TObject);
begin
  modalresult := mrOk;
end;

procedure TformBmp.Button2Click(Sender: TObject);
begin
  modalresult := mrCancel;
end;



function TformBmp.EditBitmap(xbmp:Tbitmap): Boolean;
var w,h:Integer;
    sx,sy,s: Double;
begin
  w := xbmp.Width;
  h := xbmp.Height;
  orgbmp := Tbitmap.Create;
  orgbmp.Width := w;
  orgbmp.Height := h;
  orgbmp.PixelFormat := xbmp.PixelFormat;
  orgbmp.Canvas.Draw(0,0,xbmp);

  NewBmp := xbmp;
  sx := (panel2.Width-10) / w;
  sy := (panel2.Height-10) / h;
  if sx > sy then s := sy else s :=sx;
  pBox.Width := round(w*s);
  pBox.Height := round(h*s);
  pBox.Top := (panel2.Height - pBox.Height) div 2;
  pBox.Left := (panel2.Width - pBox.Width) div 2;

  result := (ShowModal = mrOk);
  if result then begin
    xbmp.Assign(newbmp);
  end;
  orgbmp.free;
end;

function Min(a, b: integer): integer;
begin
  if a < b then result := a
  else result := b;
end;

function Max(a, b: integer): integer;
begin
  if a > b then result := a
  else result := b;
end;

procedure TformBmp.TrackBar1Change(Sender: TObject);
var i, j, value: integer;
    OrigRow, DestRow: pRGBArray;
begin
  // get brightness increment value
  value := TTrackBar(Sender).Position;
  // for each row of pixels
  for i := 0 to orgbmp.Height - 1 do
  begin
    OrigRow := orgbmp.ScanLine[i];
    DestRow := newBmp.ScanLine[i];
    // for each pixel in row
    for j := 0 to orgBmp.Width - 1 do
    begin
      // add brightness value to pixel's RGB values
      if value > 0 then
      begin
        // RGB values must be less than 256
        DestRow[j].rgbtRed := Min(255, OrigRow[j].rgbtRed + value);
        DestRow[j].rgbtGreen := Min(255, OrigRow[j].rgbtGreen + value);
        DestRow[j].rgbtBlue := Min(255, OrigRow[j].rgbtBlue + value);
      end else begin
        // RGB values must be greater or equal than 0
        DestRow[j].rgbtRed := Max(0, OrigRow[j].rgbtRed + value);
        DestRow[j].rgbtGreen := Max(0, OrigRow[j].rgbtGreen + value);
        DestRow[j].rgbtBlue := Max(0, OrigRow[j].rgbtBlue + value);
      end;
    end;
  end;
  pBox.Repaint;
end;


procedure TformBmp.FormCreate(Sender: TObject);
begin
  panel2.doublebuffered := true;
end;

procedure TformBmp.pboxPaint(Sender: TObject);
begin
  pbox.Canvas.StretchDraw(Rect(0,0,pbox.Width,pbox.height),newBmp);
end;

end.
