Pages

0
 KOMPRESI CITRA............

 untuk codingnya....

unit view;


//program:  jpgview
//date: sept. 04 1999
//author:  Kevin Pisarsky
//contact:  kfpisarsky@strongtool.com
//compiler:  Delphi 4 c/s edition update pack 3
//purpose:  viewing of jpeg images
//sponsor:  delphi developer

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ImgList, ToolWin, ComCtrls, ExtDlgs, ExtCtrls, IniFiles, JPEG, StdCtrls,
  ShlObj;

type
  TfrmVIEW = class(TForm)
    dlgOPENPIC: TOpenPictureDialog;
    sbrVIEW: TStatusBar;
    tbrVIEW: TToolBar;
    imlVIEW: TImageList;
    tbnOpen: TToolButton;
    tbnBROWSE: TToolButton;
    tbnSAVE: TToolButton;
    tbnSEP1: TToolButton;
    tbnEIGHTH: TToolButton;
    tbnQUARTER: TToolButton;
    tbnHALF: TToolButton;
    tbnFULL: TToolButton;
    tbnAUTO: TToolButton;
    tbnSEP2: TToolButton;
    tbnCOMPRESS: TToolButton;
    tbnSEP3: TToolButton;
    tbnGRAY: TToolButton;
    tbnLOCOLOR: TToolButton;
    tbnHICOLOR: TToolButton;
    tbnSEP4: TToolButton;
    tbnQUALITY: TToolButton;
    tbnSPEED: TToolButton;
    tbnSEP6: TToolButton;
    tbnFIRST: TToolButton;
    tbnPREVIOUS: TToolButton;
    tbnNEXT: TToolButton;
    tbnLAST: TToolButton;
    tbnLIST: TToolButton;
    tbnEXIT: TToolButton;
    cboFILES: TComboBox;
    dlgSAVEPIC: TSavePictureDialog;
    tbnDELETE: TToolButton;
    procedure tbnEXITClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure tbnOpenClick(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure tbnLISTClick(Sender: TObject);
    procedure tbnSAVEClick(Sender: TObject);
    procedure tbnEIGHTHClick(Sender: TObject);
    procedure tbnQUARTERClick(Sender: TObject);
    procedure tbnHALFClick(Sender: TObject);
    procedure tbnFULLClick(Sender: TObject);
    procedure tbnAUTOClick(Sender: TObject);
    procedure tbnBROWSEClick(Sender: TObject);
    procedure tbnNEXTClick(Sender: TObject);
    procedure tbnLASTClick(Sender: TObject);
    procedure tbnFIRSTClick(Sender: TObject);
    procedure tbnPREVIOUSClick(Sender: TObject);
    procedure cboFILESClick(Sender: TObject);
    procedure tbnGRAYClick(Sender: TObject);
    procedure tbnLOCOLORClick(Sender: TObject);
    procedure tbnHICOLORClick(Sender: TObject);
    procedure tbnCOMPRESSClick(Sender: TObject);
    procedure tbnDELETEClick(Sender: TObject);
  private      { Private declarations }
    {enable or disable buttons that are needed when a picture is showing}
    procedure ToggleButtons;
    {recurse all specified jpg files on a drive or folder into the dropdown list}
    function flngRecurseDrive(strDrive :  string) : longint;
    {find a folder using the browse for folder dialog}
    function fstrBrowseFolder : string;
    {get the number of colors supported by the video card}
    function fintNumColors : integer;
    {get the working area of the screen, excluding the taskbar}
    function frctGetWorkArea : TRect;
    {add a backslash to a path if necessary}
    function fstrAddSlash(inString : string) : string;
    {check on if a directory exists}
    function fboolDirectoryExists(const Name: string): Boolean;
    {put the current jpeg information on the status bar}
    procedure pShowInfo;
    {draw the picture of the current jpeg on the image box}
    procedure pShowPicture;
    {load a jpeg image}
    function fboolLoadJpeg : boolean;
  public       { Public declarations }

  end;

var
  frmVIEW    : TfrmVIEW;    {form}
  jpgCurrent : TJpegImage; {current picture}

implementation

{$R *.DFM}

{enable or disable buttons that are needed when a
 picture is showing}
procedure TfrmView.ToggleButtons;
begin
  {save button}
  if tbnSAVE.Enabled then
    tbnSAVE.Enabled := False
  else
    tbnSAVE.Enabled := True;
  {eighth size button}
  if tbnEIGHTH.Enabled then
    tbnEIGHTH.Enabled := False
  else
    tbnEIGHTH.Enabled := True;
  {quarter size button}
  if tbnQUARTER.Enabled then
    tbnQUARTER.Enabled := False
  else
    tbnQUARTER.Enabled := True;
  {half size button}
  if tbnHALF.Enabled then
    tbnHALF.Enabled := False
  else
    tbnHALF.Enabled := True;
  {full size button}
  if tbnFULL.Enabled then
    tbnFULL.Enabled := False
  else
    tbnFULL.Enabled := True;
  {auto size button}
  if tbnAUTO.Enabled then
    tbnAUTO.Enabled := False
  else
    tbnAUTO.Enabled := True;
  {compress button}
  if tbnCOMPRESS.Enabled then
    tbnCOMPRESS.Enabled := False
  else
    tbnCOMPRESS.Enabled := True;
  {gray button}
  if tbnGRAY.Enabled then
    tbnGRAY.Enabled := False
  else
    tbnGRAY.Enabled := True;
  {locolor button}
  if tbnLOCOLOR.Enabled then
    tbnLOCOLOR.Enabled := False
  else
    tbnLOCOLOR.Enabled := True;
  {hicolor button}
  if tbnHICOLOR.Enabled then
    tbnHICOLOR.Enabled := False
  else
    tbnHICOLOR.Enabled := True;
  {quality button}
  if tbnQUALITY.Enabled then
    tbnQUALITY.Enabled := False
  else
    tbnQUALITY.Enabled := True;
  {speed button}
  if tbnSPEED.Enabled then
    tbnSPEED.Enabled := False
  else
    tbnSPEED.Enabled := True;
  {first button}
  if tbnFIRST.Enabled then
    tbnFIRST.Enabled := False
  else
    tbnFIRST.Enabled := True;
  {previous button}
  if tbnPREVIOUS.Enabled then
    tbnPREVIOUS.Enabled := False
  else
    tbnPREVIOUS.Enabled := True;
  {next button}
  if tbnNEXT.Enabled then
    tbnNEXT.Enabled := False
  else
    tbnNEXT.Enabled := True;
  {last button}
  if tbnLAST.Enabled then
    tbnLAST.Enabled := False
  else
    tbnLAST.Enabled := True;
  {delete button}
  if tbnDELETE.Enabled then
    tbnDELETE.Enabled := False
  else
    tbnDELETE.Enabled := True;
end; {enable or disable buttons}

{recurse all specified jpg files on a drive or folder into the dropdown list}
function TfrmView.flngRecurseDrive(strDrive :  string) : longint;
var
  intCheck    : Integer;
  srcResult   : TSearchRec;
begin
  {add slash to drive path}
  strDrive := fstrAddSlash(strDrive);
  {set first file search up & get result}
  intCheck := sysutils.findfirst(strDrive + '*.*',$3f,srcResult);
  {keep checking for files until no more are found}
  while intCheck = 0 do
  begin
    if (srcResult.Attr and faDirectory) = faDirectory  then
      begin {if directory}
        {if not a directory}
        if (srcResult.name[1] <> '.')then
          begin
            {extension of file found is a jpeg, add it to the list}
            if UpperCase(ExtractFileExt(srcResult.name)) = '.JPG' then
              begin
                cboFILES.Items.Add(strDrive + srcResult.name);
                flngRecurseDrive(strDrive + srcResult.name);
              end
            else {extension was not jpg - keep recursing}
              flngRecurseDrive(strDrive + srcResult.name);
          end; {if not .}
      end {if directory}
    else
      begin {extension of file found is a jpeg, add it to the list}
        if UpperCase(ExtractFileExt(srcResult.name)) = '.JPG' then
          cboFILES.Items.Add(strDrive + srcResult.name);
      end;
    {find next search result}
    intCheck := sysutils.findnext(srcResult);
    {show the current search result on the status bar}
    sbrView.Panels[4].Text := srcResult.Name;
    sbrView.Refresh;
  end;
  {free memory allocated for search}
  sysutils.findclose(srcResult);
  {assign function result}
  result := cboFILES.Items.Count;
end; {RecurseDrive}

{find a folder using the browse for folder dialog}
function TfrmView.fstrBrowseFolder : string;
var
  lpItemID   : PItemIDList;
  brwsInfo   : TBrowseInfo;
  charName   : array[0..MAX_PATH] of char;
  charPath   : array[0..MAX_PATH] of char;
begin
  FillChar(brwsInfo,sizeOf(TBrowseInfo),#0);
  brwsInfo.hwndOwner := frmVIEW.handle;
  brwsInfo.lpszTitle := PChar('Select a Drive or Folder to search for images');
  brwsInfo.pszDisplayName := PChar('D:');
  brwsInfo.pszDisplayName := @charName;
  brwsInfo.ulFlags := BIF_RETURNONLYFSDIRS;
  lpItemID := SHBrowseForFolder(brwsInfo);
  if lpItemID <> nil then
    begin
      SHGetPathFromIDList(lpItemID,charPath);
      Result := charPath;
      GlobalFreePtr(lpItemID);
    end {lpItemID}
  else
    begin  {user chooses cancel}
      result := '';
    end; {if lpItemID}
end; {browse for data folder}

{get the number of colors supported by the video card}
function TfrmVIEW.fintNumColors : integer;
var
  desktopDC : hDC;
begin
  desktopDC := GetDC(0);
  result := GetDeviceCaps(desktopDC, BITSPIXEL) * GetDeviceCaps(desktopDC, PLANES);
  releaseDC(0,desktopDC);
end; {function numColors}

{get the working area of the screen, excluding the taskbar if
 it is in the up position}
function TfrmVIEW.frctGetWorkArea : TRect;
begin
  SystemParametersInfo(SPI_GETWORKAREA, 0, @Result, 0);
end; {get the working area of the screen, excluding the taskbar}

{add a backslash to a path if necessary}
function TfrmVIEW.fstrAddSlash(inString : string) : string;
begin

  if inString[length(inString)] = '\' then
    Result := inString
  else
    Result := inString + '\';

end; {add a backslash to a path if necessary}

{check on if a directory exists}
function TfrmVIEW.fboolDirectoryExists(const Name: string): Boolean;
var
  Code: Integer;
begin
  Code := GetFileAttributes(PChar(Name));
  Result := (Code <> -1) and (FILE_ATTRIBUTE_DIRECTORY and Code <> 0);
end; {DirectoryExists}

{put the current jpeg information on the status bar}
procedure TfrmView.pShowInfo;
var
  intPanelWidth : integer; {width of a panel}
begin
  {width}
  intPanelWidth := sbrView.Canvas.TextWidth('M' + IntToStr(jpgCurrent.Width) + 'M');
  sbrView.Panels[0].Width := intPanelWidth;
  sbrView.Panels[0].Text := IntToStr(jpgCurrent.Width);
  {height}
  intPanelWidth := sbrView.Canvas.TextWidth('M' + IntToStr(jpgCurrent.Height) + 'M');
  sbrView.Panels[1].Width := intPanelWidth;
  sbrView.Panels[1].Text := IntToStr(jpgCurrent.Height);
  {file name}
  sbrView.Panels[4].Text := cboFILES.Text;
end;

{draw the picture of the current jpeg on the form canvas}
procedure TfrmView.pShowPicture;
var
  intLeft : integer; {x-coordinate of left corner of image}
  intTop  : integer; {y-coordinate of top corner of image}
begin

  {show jpeg information on status bar}
  pShowInfo;

  {set the image scale factor}
  jpgCurrent.Scale := jsFullSize;  {set to full size initially}
  if tbnEIGHTH.Down then jpgCurrent.Scale := jsEighth;
  if tbnQUARTER.Down then jpgCurrent.Scale := jsQuarter;
  if tbnHALF.Down then jpgCurrent.Scale := jsHalf;
  if tbnFULL.Down then jpgCurrent.Scale := jsFullSize;

  {if autoscale is on, size image to fit screen}
  if tbnAUTO.Down then
    begin
      if jpgCurrent.Width > frmVIEW.ClientWidth then jpgCurrent.Scale := jsHalf;
      if jpgCurrent.Width > frmVIEW.ClientWidth then jpgCurrent.Scale := jsQuarter;
      if jpgCurrent.Width > frmVIEW.ClientWidth then jpgCurrent.Scale := jsEighth;
      if jpgCurrent.Height > frmVIEW.ClientHeight then jpgCurrent.Scale := jsHalf;
      if jpgCurrent.Height > frmVIEW.ClientHeight then jpgCurrent.Scale := jsQuarter;
      if jpgCurrent.Height > frmVIEW.ClientHeight then jpgCurrent.Scale := jsEighth;
    end; {if autoscale is on, size image to fit screen}

  {center the image if possible}
  if jpgCurrent.Width < frmVIEW.ClientWidth then
    intLeft := round((frmView.ClientWidth - jpgCurrent.Width) / 2)
  else
    intLeft := 0;
  if jpgCurrent.Height < frmVIEW.ClientHeight then
    intTop := round((frmView.ClientHeight - jpgCurrent.Height) / 2)
  else
    intTop := tbrView.Top + tbrView.Height;

  {set display format}
  if tbnHiColor.Down then
    begin
      jpgCurrent.PixelFormat := jf24Bit;
      jpgCurrent.Grayscale := False;
    end;
  {low color / 256 color}
  if tbnLoColor.Down then
    begin
      jpgCurrent.PixelFormat := jf8Bit;
      jpgCurrent.Grayscale := False;
    end;
  {grayscale}
  if tbnGray.Down then
    begin
      jpgCurrent.PixelFormat := jf8Bit;
      jpgCurrent.Grayscale := True;
    end;

  {draw the jpeg on the form canvas}
  frmView.Refresh;
  frmView.Canvas.Draw(intLeft,intTop,jpgCurrent);

end; {draw the picture of the current jpeg on the form canvas}

{load a jpeg image}
function TfrmVIEW.fboolLoadJpeg : boolean;
var
  strJpegFileName : string; {qualified filename of jpeg file}
  strLogFileName  : string; {log file for possible errors}
  fhLogFile       : TextFile; {text file to log errors to}
begin

  {get file name}
  strJpegFileName := cboFILES.Items[cboFILES.ItemIndex];
  {exit if the file does not exist}
  if not FileExists(strJpegFileName) then
    begin
      result := false;
      exit;
    end; {if not fileexists}

  {set performance factor for reading/decompressing the jpeg}
  if tbnSpeed.Down then
    jpgCurrent.Performance := jpBestSpeed
  else
    jpgCurrent.Performance := jpBestQuality;

  {try to load a file into the jpeg, log an error if it doesn't work}
  try

    jpgCurrent.LoadFromFile(strJpegFileName);

  except

    on exception do
      begin

        {log exceptions to a text file}
        strLogFileName := fstrAddSlash(ExtractFilePath(ParamStr(0))) + 'errorlog.txt';
        AssignFile(fhLogFile,strLogFileName);
        {create the log file if it doesn't exist, if it does, then add to it}
        if FileExists(strLogFileName) then
          Append(fhLogFile)
        else
          Rewrite(fhLogFile);
        {write the file name of the jpeg file which caused the exception}
        Writeln(fhLogFile, strJpegFileName);
        {close the log file}
        CloseFile(fhLogFile);
        {set result = false}
        result := False;
        {exit the function here on errors}
        exit;

      end; {on exception}
  end; {try}

  {set result = true if function reaches this point}
  result := True;

end; {load a jpeg image}

{exit app}
procedure TfrmVIEW.tbnEXITClick(Sender: TObject);
begin
  frmVIEW.Close;
  Application.Terminate;
end; {exit app}

{initial form creation}
procedure TfrmVIEW.FormCreate(Sender: TObject);
var
  iniVIEW  : TIniFile; {persist settings for application}
  iniFILE  : string; {qualified name of ini file}
  rctArea  : TRect; {working area of desktop}
begin

  {disable the buttons that are not needed until pictures are loaded}
  ToggleButtons;

  {get the working area of the screen}
  rctArea := frctGetWorkArea;

  {set ini file name}
  iniFILE := fstrAddSlash(ExtractFilePath(ParamStr(0))) + 'jpegview.ini';

  {initialize ini file}
  iniVIEW := TIniFile.Create(iniFILE);

  {read position settings from ini file}
  frmVIEW.Left := iniVIEW.ReadInteger('POSITION','Left',0);
  frmVIEW.Top := iniVIEW.ReadInteger('POSITION','Top',0);
  frmVIEW.Width := iniVIEW.ReadInteger('POSITION','Width',rctArea.Right);
  frmVIEW.Height := iniVIEW.ReadInteger('POSITION','Height',rctArea.Bottom);

  {free ini file handle}
  iniVIEW.Free;

  {set the autoscale to on by default}
  tbnAuto.Down := True;

  {check color of current video card and
   set to most appropriate viewing mode}
  if fintNumColors < 16 then
    tbnLOCOLOR.Down := True
  else
    tbnHICOLOR.Down := True;
end; {initial form creation}

{before form close}
procedure TfrmVIEW.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
  iniVIEW  : TIniFile; {persist settings for application}
  iniFILE  : string; {qualified name of ini file}
begin
  {set ini file name}
  iniFILE := fstrAddSlash(ExtractFilePath(ParamStr(0))) + 'jpegview.ini';

  {initialize ini file}
  iniVIEW := TIniFile.Create(iniFILE);

  {read position settings from ini file}
  iniVIEW.WriteInteger('POSITION','Left',frmVIEW.Left);
  iniVIEW.WriteInteger('POSITION','Top',frmVIEW.Top);
  iniVIEW.WriteInteger('POSITION','Width',frmVIEW.Width);
  iniVIEW.WriteInteger('POSITION','Height',frmVIEW.Height);

  {free ini file handle}
  iniVIEW.Free;

  {if memory was allocated for the jpeg then free it}
  if frmVIEW.Tag <> 0 then jpgCurrent.Free;

end; {before form close}

{select file(s) for viewing}
procedure TfrmVIEW.tbnOpenClick(Sender: TObject);
var
  strLastDir : string; {last directory a file was chosen from}
  iniVIEW    : TIniFile; {persist settings for application}
  iniFILE    : string; {qualified name of ini file}
begin
  {set ini file name}
  iniFILE := fstrAddSlash(ExtractFilePath(ParamStr(0))) + 'jpegview.ini';

  {initialize ini file}
  iniVIEW := TIniFile.Create(iniFILE);

  {get the last initial directory used}
  strLastDir := iniVIEW.ReadString('PATHS','LastOpenDir','');
  if fboolDirectoryExists(strLastDir) then dlgOPENPIC.InitialDir := strLastDir;

  {if files were chosen}
  if dlgOPENPIC.Execute then
    begin

      {clear the dropdown list if necessary}
      if frmVIEW.Tag <> 0 then
        cboFILES.Clear
      else
        begin
          jpgCurrent := TJpegImage.Create;
          frmVIEW.Tag := 1;
        end; {if frmVIEW.Tag <> 0}

      {add selected files to dropdown list}
      while dlgOPENPIC.Files.Count > 0 do
        begin
          cboFILES.Items.Add(dlgOPENPIC.Files[0]);
          dlgOPENPIC.Files.Delete(0);
        end; {while dlgOPENPIC.Files.Count > 0}

      {save the folder that the file was chosen from
       for future use}
      strLastDir := ExtractFilePath(cboFILES.Items[0]);
      iniVIEW.WriteString('PATHS','LastOpenDir',strLastDir);

      {show the first picture on the list}
      cboFILES.ItemIndex := 0;
      if fboolLoadJpeg then pShowPicture;

      {enable the buttons}
      ToggleButtons;

    end; {if files were chosen}

  {free memory allocated for ini file handling}
  iniVIEW.Free;

end; {select file(s) for viewing}

{form resize}
procedure TfrmVIEW.FormResize(Sender: TObject);
begin
  {size combo box to width of form}
  cboFILES.Left := 0;
  cboFILES.Width := frmVIEW.ClientWidth;
  cboFILES.Top := tbrView.Top + tbrView.Height;
  if frmVIEW.Tag > 0 then pShowPicture;
end; {form resize}

{toggle view/hide file list}
procedure TfrmVIEW.tbnLISTClick(Sender: TObject);
begin

  if tbnList.Down then
    cboFILES.Visible := True
  else
    cboFILES.Visible := False;

end; {toggle view/hide file list}

{save the current picture as a file}
procedure TfrmVIEW.tbnSAVEClick(Sender: TObject);
var
  strNewFileName : string;
begin

  {if user clicks the save button}
  if dlgSAVEPIC.Execute then
    begin
      strNewFileName := dlgSAVEPIC.FileName;
      jpgCurrent.SaveToFile(strNewFileName);
    end;

end; {save the current picture as a file}

{set picture to 1/8 size}
procedure TfrmVIEW.tbnEIGHTHClick(Sender: TObject);
begin
  if jpgCurrent <> nil then pShowPicture;
end; {set picture to 1/8 size}

{set picture to 1/4 size}
procedure TfrmVIEW.tbnQUARTERClick(Sender: TObject);
begin
  if jpgCurrent <> nil then pShowPicture;
end; {set picture to 1/4 size}

{set picture to 1/2 size}
procedure TfrmVIEW.tbnHALFClick(Sender: TObject);
begin
  if jpgCurrent <> nil then pShowPicture;
end; {set picture to 1/2 size}

{set picture to full size}
procedure TfrmVIEW.tbnFULLClick(Sender: TObject);
begin
  if jpgCurrent <> nil then pShowPicture;
end; {set picture to full size}

{toggle autoscaling of pictures}
procedure TfrmVIEW.tbnAUTOClick(Sender: TObject);
begin
  if jpgCurrent <> nil then pShowPicture;
end; {toggle autoscaling of pictures}

{select a drive or folder to search for jpegs}
procedure TfrmVIEW.tbnBROWSEClick(Sender: TObject);
var
  strFolder : string; {folder to recurse for image files}
  intCount  : integer; {resulting count of recursing drive}
begin

  {get the selected drive/folder from the user}
  strFolder := fstrBrowseFolder;

  {clear any current list}
  if frmVIEW.Tag <> 0 then cboFILES.Clear;

  {change the mousepointer to an hourglass}
  Screen.Cursor := crHourGlass;

  {recurse all jpegs on the drive into the dropdown list}
  if strFolder <> '' then
    intCount := flngRecurseDrive(strFolder)
  else
    intCount := 0;

  {change the mousepointer to an hourglass}
  Screen.Cursor := crDefault;

  {enable buttons and create the jpeg image}
  if intCount > 0 then
    begin
      {enable the buttons}
      ToggleButtons;
      {create the jpeg image to use for loading jpeg files}
      jpgCurrent := TJpegImage.Create;
      {set the tag to indicate jpeg memory was allocated}
      frmVIEW.Tag := 1;
      {show the first picture on the list}
      cboFILES.ItemIndex := 0;
      if fboolLoadJpeg then pShowPicture;
    end; {if frmVIEW.Tag <> 0}

end; {select a drive or folder to search for jpegs}

{move to next jpeg file on list}
procedure TfrmVIEW.tbnNEXTClick(Sender: TObject);
var
  intNextJpeg : integer;
begin

  {increment list index}
  intNextJpeg := cboFILES.ItemIndex + 1;
  {move back to first jpeg if past the end}
  if intNextJpeg > (cboFILES.Items.Count - 1) then intNextJpeg := 0;
  {select the new jpeg from the dropdown list}
  cboFILES.ItemIndex := intNextJpeg;
  {show the jpeg}
  if fboolLoadJpeg then pShowPicture;

end; {move to next jpeg file on list}

{move to last jpeg file on list}
procedure TfrmVIEW.tbnLASTClick(Sender: TObject);
begin

  {select the last jpeg from the dropdown list}
  cboFILES.ItemIndex := cboFILES.Items.Count - 1;
  {show the jpeg}
  if fboolLoadJpeg then pShowPicture;

end; {move to last jpeg file on list}

{move to first jpeg file on list}
procedure TfrmVIEW.tbnFIRSTClick(Sender: TObject);
begin

  {select the last jpeg from the dropdown list}
  cboFILES.ItemIndex := 0;
  {show the jpeg}
  if fboolLoadJpeg then pShowPicture;

end; {move to first jpeg file on list}

{move to previous jpeg file on list}
procedure TfrmVIEW.tbnPREVIOUSClick(Sender: TObject);
var
  intPrevJpeg : integer;
begin

  {increment list index}
  intPrevJpeg := cboFILES.ItemIndex - 1;
  {move back to last jpeg if beginning of list has been reached}
  if intPrevJpeg < 0 then intPrevJpeg := cboFILES.Items.Count - 1;
  {select the new jpeg from the dropdown list}
  cboFILES.ItemIndex := intPrevJpeg;
  {show the jpeg}
  if fboolLoadJpeg then pShowPicture;

end; {move to previous jpeg file on list}

{user selects a file from the list}
procedure TfrmVIEW.cboFILESClick(Sender: TObject);
begin
  if fboolLoadJpeg then pShowPicture;
end; {user selects a file from the list}

{switch image to grayscale}
procedure TfrmVIEW.tbnGRAYClick(Sender: TObject);
begin
  jpgCurrent.PixelFormat := jf8Bit;
  jpgCurrent.Grayscale := True;
  pShowPicture;
end; {switch image to grayscale}

{switch image to low color}
procedure TfrmVIEW.tbnLOCOLORClick(Sender: TObject);
begin
  jpgCurrent.PixelFormat := jf8Bit;
  jpgCurrent.Grayscale := False;
  pShowPicture;
end; {switch image to low color}

{switch image to high color}
procedure TfrmVIEW.tbnHICOLORClick(Sender: TObject);
begin
  jpgCurrent.PixelFormat := jf24Bit;
  jpgCurrent.Grayscale := False;
  pShowPicture;
end; {switch image to high color}

{set jpeg compression}
procedure TfrmVIEW.tbnCOMPRESSClick(Sender: TObject);
var
  intQuality  : integer; {compression quality index}
  strResponse : string; {response from input box}
  strPrompt   : string; {prompt to user}
begin
  {set prompt}
  strPrompt := 'Higher value = better quality';
  {get user setting}
  strResponse := inputbox('Compression Quality',strPrompt,'100');
  {make integer from setting}
  if strResponse <> '' then
    begin
      {change the quality response to an integer}
      intQuality := StrToIntDef(strResponse,100);
      {set the compression quality}
      jpgCurrent.CompressionQuality := intQuality;
      {call the compression method}
      jpgCurrent.Compress;
      {set to grayscale}
      jpgCurrent.Grayscale := True;
      {re-show the picture}
      pShowPicture;
      {turn off grayscale}
      jpgCurrent.Grayscale := False;
      {re-show the picture compressed}
      pShowPicture;
    end; {make integer from setting}
end; {set jpeg compression}

{delete current jpeg file}
procedure TfrmVIEW.tbnDELETEClick(Sender: TObject);
var
  strMsg      : string;
begin
  {show user confirmation message}
  strMsg := 'Are you sure you want to delete the current file?';
  {if user chooses yes, delete file & item from list}
  if MessageDlg(strMsg,mtWarning,[mbYes,mbNo],0) = mrYes then
    begin
      {delete the jpeg file}
      DeleteFile(cboFILES.Text);
      {remove the file reference from the list}
      cboFILES.Items.Delete(cboFILES.ItemIndex);
      {toggle the buttons to disabled if there are no list items}
      if cboFILES.Items.Count < 1 then ToggleButtons;
    end; {if messagedlg = yes}
end; {delete current jpeg file}

end.

untuk hasil codingnya......



 
0

Edge detection ..........

 CODING EDGE DETECTION

unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtDlgs, Menus, StdCtrls, ExtCtrls, Spin;

type
  TRGBTripleArray = array[0..10000] of TRGBTriple;
  PRGBTripleArray = ^TRGBTripleArray;

  T3x3FloatArray = array[0..2] of array[0..2] of Extended;

  TForm1 = class(TForm)
    Image1: TImage;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Open1: TMenuItem;
    OpenPictureDialog1: TOpenPictureDialog;
    GroupBox1: TGroupBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    ComboBox1: TComboBox;
    Button1: TButton;
    SavePictureDialog1: TSavePictureDialog;
    Save1: TMenuItem;
    SaveAs1: TMenuItem;
    N1: TMenuItem;
    Exit1: TMenuItem;
    Edit10: TEdit;
    Label1: TLabel;
    Edit11: TMenuItem;
    Undo1: TMenuItem;
    GroupBox2: TGroupBox;
    SpinEdit1: TSpinEdit;
    Button2: TButton;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    RadioButton5: TRadioButton;
    Reset1: TMenuItem;
    Help1: TMenuItem;
    About1: TMenuItem;
    procedure Open1Click(Sender: TObject);
    procedure EditChange(Sender : TObject);
    procedure SetMask(a1, a2, a3, a4, a5, a6, a7, a8, a9 : Extended ; ABias : integer);
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Select(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    function Convolve(ABitmap : TBitmap ; AMask : T3x3FloatArray ; ABias : integer) : TBitmap;
    procedure SaveAs1Click(Sender: TObject);
    procedure Save1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure Undo1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    function Threshold(ABitmap : TBitmap ; AThreshold : byte ;
                          Intensity,
                          Saturation,
                          Red,
                          Green,
                          Blue : boolean) : TBitmap;
    procedure Reset1Click(Sender: TObject);
    procedure About1Click(Sender: TObject);
  private
    { Private declarations }
    Mask : T3x3FloatArray;
    Bias : integer;
    UndoBitmap : TBitmap;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses AboutUnit;

{$R *.dfm}

// initialization
procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Tag := 0; Edit2.Tag := 1; Edit3.Tag := 2;
  Edit4.Tag := 3; Edit5.Tag := 4; Edit6.Tag := 5;
  Edit7.Tag := 6; Edit8.Tag := 7; Edit9.Tag := 8;
  Edit10.Tag := 9; // bias

  SetMask(1, 1, 1,
          1, 1, 1,
          1, 1, 1, 0);

  UndoBitmap := TBitmap.Create;
  UndoBitmap.PixelFormat := pf24bit;
end;

// preset masks
procedure TForm1.ComboBox1Select(Sender: TObject);
begin
  if ComboBox1.ItemIndex = 0 then // uniform smoothing
    SetMask(1, 1, 1,
            1, 1, 1,
            1, 1, 1, 0);

  if ComboBox1.ItemIndex = 1 then // gaussian smoothing
    SetMask(1/36, 1/9, 1/36,
            1/9,  4/9, 1/9,
            1/36, 1/9, 1/36, 0);

  if ComboBox1.ItemIndex = 2 then // edge detection
    SetMask(-1, -1, -1,
            -1,  8, -1,
            -1, -1, -1, 0);

  if ComboBox1.ItemIndex = 3 then // vertical edge detection
    SetMask( 0,  0,  0,
            -1,  2, -1,
             0,  0,  0, 0);

  if ComboBox1.ItemIndex = 4 then // horizontal edge detection
    SetMask( 0, -1,  0,
             0,  2,  0,
             0, -1,  0, 0);

  if ComboBox1.ItemIndex = 5 then // enhanced detail
    SetMask( 0, -1,  0,
            -1,  9, -1,
             0, -1,  0, 0);

  if ComboBox1.ItemIndex = 6 then // enhanced focus
    SetMask(-1,  0, -1,
             0,  7,  0,
            -1,  0, -1, 0);

  if ComboBox1.ItemIndex = 7 then // emboss filter
    SetMask(-1, -1,  0,
            -1,  0,  1,
             0,  1,  1, 128);

  if ComboBox1.ItemIndex = 8 then // lighten
    SetMask( 0,  0,  0,
             0,  1,  0,
             0,  0,  0, 20);

  if ComboBox1.ItemIndex = 9 then // darken
    SetMask( 0,  0,  0,
             0,  1,  0,
             0,  0,  0, -20);
end;

procedure TForm1.EditChange(Sender : TObject);
Var
  LTag : byte;
  LValue : Extended;
begin
  LTag := TEdit(Sender).Tag;

  if (TEdit(Sender).Text = '') or (TEdit(Sender).Text = '-')
    then LValue := 0
    else LValue := StrToFloat(TEdit(Sender).Text);

  if LTag = 9 then begin
    if (LValue > 255) or (Frac(LValue) <> 0) then begin
      ShowMessage('the bias has to be a whole number between -255 and 255');
      Exit;
    end;
    Bias := trunc(LValue);
    Exit;
  end;

  Mask[LTag mod 3, LTag div 3] := LValue;
end;

procedure TForm1.SetMask(a1, a2, a3, a4, a5, a6, a7, a8, a9 : Extended ; ABias : integer);
begin
  Edit1.Text := FloatToStr(a1);
  Edit2.Text := FloatToStr(a2);
  Edit3.Text := FloatToStr(a3);
  Edit4.Text := FloatToStr(a4);
  Edit5.Text := FloatToStr(a5);
  Edit6.Text := FloatToStr(a6);
  Edit7.Text := FloatToStr(a7);
  Edit8.Text := FloatToStr(a8);
  Edit9.Text := FloatToStr(a9);
  Edit10.Text := IntToStr(ABias);
end;

// open a bitmap into the image
procedure TForm1.Open1Click(Sender: TObject);
begin
  if not OpenPictureDialog1.Execute then Exit;
  Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
  SaveAs1.Enabled := True;
  Save1.Enabled := True;
  Reset1.Enabled := True;
end;

// save as new bitmap file
procedure TForm1.SaveAs1Click(Sender: TObject);
begin
  if not SavePictureDialog1.Execute then Exit;

  Image1.Picture.Bitmap.SaveToFile(SavePictureDialog1.FileName);
end;

// overwrite original bitmap file
procedure TForm1.Save1Click(Sender: TObject);
begin
  Image1.Picture.Bitmap.SaveToFile(OpenPictureDialog1.FileName);
end;

// convolve
procedure TForm1.Button1Click(Sender: TObject);
begin
  if not Save1.Enabled then Exit;

  if (Image1.Picture.Bitmap.Width < 3) or (Image1.Picture.Bitmap.Height < 3) then begin
    ShowMessage('the image is too small to perform convolution on');
    Exit;
  end; 

  UndoBitmap.Width := Image1.Picture.Bitmap.Width;
  UndoBitmap.Height := Image1.Picture.Bitmap.Height;
  UndoBitmap.Canvas.Draw(0, 0, Image1.Picture.Bitmap);
  Undo1.Enabled := True;

  Image1.Picture.Bitmap := Convolve(Image1.Picture.Bitmap, Mask, Bias);
end;

function TForm1.Convolve(ABitmap : TBitmap ; AMask : T3x3FloatArray ; ABias : integer) : TBitmap;
Var
  LRow1, LRow2, LRow3, LRowOut : PRGBTripleArray;
  LRow, LCol : integer;
  LNewBlue, LNewGreen, LNewRed : Extended;
  LCoef : Extended;
begin

  LCoef := 0;
  for LRow := 0 to 2 do for LCol := 0 to 2 do LCoef := LCoef + AMask[LCol, LRow];
  if LCoef = 0 then LCoef := 1;

  Result := TBitmap.Create;

  Result.Width := ABitmap.Width - 2;
  Result.Height := ABitmap.Height - 2;
  Result.PixelFormat := pf24bit;

  LRow2 := ABitmap.ScanLine[0];
  LRow3 := ABitmap.ScanLine[1];

  for LRow := 1 to ABitmap.Height - 2 do begin

    LRow1 := LRow2;
    LRow2 := LRow3;
    LRow3 := ABitmap.ScanLine[LRow + 1];
   
    LRowOut := Result.ScanLine[LRow - 1];

    for LCol := 1 to ABitmap.Width - 2 do begin

      LNewBlue :=
        (LRow1[LCol-1].rgbtBlue*AMask[0,0]) + (LRow1[LCol].rgbtBlue*AMask[1,0]) + (LRow1[LCol+1].rgbtBlue*AMask[2,0]) +
        (LRow2[LCol-1].rgbtBlue*AMask[0,1]) + (LRow2[LCol].rgbtBlue*AMask[1,1]) + (LRow2[LCol+1].rgbtBlue*AMask[2,1]) +
        (LRow3[LCol-1].rgbtBlue*AMask[0,2]) + (LRow3[LCol].rgbtBlue*AMask[1,2]) + (LRow3[LCol+1].rgbtBlue*AMask[2,2]);
      LNewBlue := (LNewBlue / LCoef) + ABias;
      if LNewBlue > 255 then LNewBlue := 255;
      if LNewBlue < 0 then LNewBlue := 0;

      LNewGreen :=
        (LRow1[LCol-1].rgbtGreen*AMask[0,0]) + (LRow1[LCol].rgbtGreen*AMask[1,0]) + (LRow1[LCol+1].rgbtGreen*AMask[2,0]) +
        (LRow2[LCol-1].rgbtGreen*AMask[0,1]) + (LRow2[LCol].rgbtGreen*AMask[1,1]) + (LRow2[LCol+1].rgbtGreen*AMask[2,1]) +
        (LRow3[LCol-1].rgbtGreen*AMask[0,2]) + (LRow3[LCol].rgbtGreen*AMask[1,2]) + (LRow3[LCol+1].rgbtGreen*AMask[2,2]);
      LNewGreen := (LNewGreen / LCoef) + ABias;
      if LNewGreen > 255 then LNewGreen := 255;
      if LNewGreen < 0 then LNewGreen := 0;

      LNewRed :=
        (LRow1[LCol-1].rgbtRed*AMask[0,0]) + (LRow1[LCol].rgbtRed*AMask[1,0]) + (LRow1[LCol+1].rgbtRed*AMask[2,0]) +
        (LRow2[LCol-1].rgbtRed*AMask[0,1]) + (LRow2[LCol].rgbtRed*AMask[1,1]) + (LRow2[LCol+1].rgbtRed*AMask[2,1]) +
        (LRow3[LCol-1].rgbtRed*AMask[0,2]) + (LRow3[LCol].rgbtRed*AMask[1,2]) + (LRow3[LCol+1].rgbtRed*AMask[2,2]);
      LNewRed := (LNewRed / LCoef) + ABias;
      if LNewRed > 255 then LNewRed := 255;
      if LNewRed < 0 then LNewRed := 0;

      LRowOut[LCol-1].rgbtBlue  := trunc(LNewBlue);
      LRowOut[LCol-1].rgbtGreen := trunc(LNewGreen);
      LRowOut[LCol-1].rgbtRed   := trunc(LNewRed);

    end;

  end;

end;

// threshold
procedure TForm1.Button2Click(Sender: TObject);
Var
  LThreshold : integer;
begin
  if not Save1.Enabled then Exit;

  UndoBitmap.Width := Image1.Picture.Bitmap.Width;
  UndoBitmap.Height := Image1.Picture.Bitmap.Height;
  UndoBitmap.Canvas.Draw(0, 0, Image1.Picture.Bitmap);
  Undo1.Enabled := True;

  LThreshold := SpinEdit1.Value;
  if RadioButton2.Checked then LThreshold := trunc( LThreshold / 2 );

  Image1.Picture.Bitmap := Threshold(Image1.Picture.Bitmap, LThreshold,
                                     RadioButton1.Checked,
                                     RadioButton2.Checked,
                                     RadioButton3.Checked,
                                     RadioButton4.Checked,
                                     RadioButton5.Checked);
end;

function TForm1.Threshold(ABitmap : TBitmap ; AThreshold : byte ;
                          Intensity,
                          Saturation,
                          Red,
                          Green,
                          Blue : boolean) : TBitmap;
Var
  LRowIn, LRowOut : PRGBTripleArray;
  Ly, Lx : integer;
  LBlack, LWhite : TRGBTriple;
  LR, LG, LB : byte;
  LR1, LR2 : integer;
begin

  Result := TBitmap.Create;
  Result.Width := ABitmap.Width;
  Result.Height := ABitmap.Height;
  Result.PixelFormat := pf24bit;

  LBlack.rgbtBlue := 0; LBlack.rgbtGreen := 0; LBlack.rgbtRed := 0;
  LWhite.rgbtBlue := 255; LWhite.rgbtGreen := 255; LWhite.rgbtRed := 255;

  for Ly := 0 to ABitmap.Height - 1 do begin
    LRowIn := ABitmap.ScanLine[Ly];
    LRowOut := Result.ScanLine[Ly];
    for Lx := 0 to ABitmap.Width - 1 do begin

      LR := LRowIn[Lx].rgbtRed;
      LG := LRowIn[Lx].rgbtGreen;
      LB := LRowIn[Lx].rgbtBlue;

      if Intensity then begin

        if (0.3  * LR) + (0.59 * LG) + (0.11 * LB) >= AThreshold
          then LRowOut[Lx] := LWhite
          else LRowOut[Lx] := LBlack;

      end else if Saturation then begin

        LR1 := trunc( (-0.105465 * LR) + (-0.207424 * LG) + (0.312889 * LB) );
        LR2 := trunc( (0.445942 * LR) + (-0.445942 * LG) );
        if Sqrt( Sqr(LR1) + Sqr(LR2) ) >= AThreshold
          then LRowOut[Lx] := LWhite
          else LRowOut[Lx] := LBlack;

      end else if Red then begin

        if LR >= AThreshold then LRowOut[Lx] := LWhite
                            else LRowOut[Lx] := LBlack;

      end else if Green then begin

        if LG >= AThreshold then LRowOut[Lx] := LWhite
                            else LRowOut[Lx] := LBlack;

      end else begin

        if LB >= AThreshold then LRowOut[Lx] := LWhite
                            else LRowOut[Lx] := LBlack;

      end;

    end;
  end;

end;

// restore previous bitmap
procedure TForm1.Undo1Click(Sender: TObject);
begin
  Image1.Picture.Bitmap.Width := UndoBitmap.Width;
  Image1.Picture.Bitmap.Height := UndoBitmap.Height;
  Image1.Canvas.Draw(0, 0, UndoBitmap);
  Undo1.Enabled := False;
end;

// restore original bitmap
procedure TForm1.Reset1Click(Sender: TObject);
begin
  Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.About1Click(Sender: TObject);
begin
  AboutBox.ShowModal;
end;

end.

untuk hasilnya......


0

brightness dengan menggunakan delphi

coding untuk brightness dengan menggunakan delphi dan ini masih berkaitan dengan yang kemarin dalam histogram....

unit BrightnessUnit;

interface

uses
  ExtCtrls, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TBrightnessForm = class(TForm)
    RedScrollBar: TScrollBar;
    GreenScrollBar: TScrollBar;
    BlueScrollBar: TScrollBar;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    OKButton: TButton;
    CancelButton: TButton;
    procedure RedScrollBarChange(Sender: TObject);
    procedure GreenScrollBarChange(Sender: TObject);
    procedure BlueScrollBarChange(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure CancelButtonClick(Sender: TObject);
  private
    { Private declarations }
    TemporaryImage:TImage;
    OriginalImage:TImage;
    Applied:boolean;
  public
    { Public declarations }
    procedure SetBrightness(Image: TImage);
  end;

var
  BrightnessForm: TBrightnessForm;

implementation

{$R *.dfm}

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;

procedure TBrightnessForm.RedScrollBarChange(Sender: TObject);
var
  i,j:Integer;
  temp:integer;
  pixelPointer:PByteArray;
  originalPixelPointer:PByteArray;
begin
  try
    begin
      for i:=0 to TemporaryImage.Height-1 do
      begin
        pixelPointer:=TemporaryImage.Picture.Bitmap.ScanLine[i];
        originalPixelPointer:=OriginalImage.Picture.Bitmap.ScanLine[i];
        for j:=0 to TemporaryImage.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;

procedure TBrightnessForm.GreenScrollBarChange(Sender: TObject);
var
  i,j:Integer;
  temp:integer;
  pixelPointer:PByteArray;
  originalPixelPointer:PByteArray;
begin
  try
    begin
      for i:=0 to TemporaryImage.Height-1 do
      begin
        pixelPointer:=TemporaryImage.Picture.Bitmap.ScanLine[i];
        originalPixelPointer:=OriginalImage.Picture.Bitmap.ScanLine[i];
        for j:=0 to TemporaryImage.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;

procedure TBrightnessForm.BlueScrollBarChange(Sender: TObject);
var
  i,j:Integer;
  temp:integer;
  pixelPointer:PByteArray;
  originalPixelPointer:PByteArray;
begin
  try
    begin
      for i:=0 to TemporaryImage.Height-1 do
      begin
        pixelPointer:=TemporaryImage.Picture.Bitmap.ScanLine[i];
        originalPixelPointer:=OriginalImage.Picture.Bitmap.ScanLine[i];
        for j:=0 to TemporaryImage.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;

procedure TBrightnessForm.OKButtonClick(Sender: TObject);
begin
  Applied:=true;
  Close();
end;

procedure TBrightnessForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  if Applied=false then
  TemporaryImage.Picture.Bitmap.Assign(
    OriginalImage.Picture.Bitmap);
  Action:=caFree;
end;

procedure TBrightnessForm.CancelButtonClick(Sender: TObject);
begin
  Applied:=false;
  Close;
end;

end.



unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtDlgs, Menus;

type
  TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Open1: TMenuItem;
    Save1: TMenuItem;
    CLose1: TMenuItem;
    Exit1: TMenuItem;
    OpenPictureDialog1: TOpenPictureDialog;
    SavePictureDialog1: TSavePictureDialog;
    StatusBar1: TStatusBar;
    Image1: TMenuItem;
    Histogram1: TMenuItem;
    Brightness1: TMenuItem;
    procedure Open1Click(Sender: TObject);
    procedure Save1Click(Sender: TObject);
    procedure Histogram1Click(Sender: TObject);
    procedure CLose1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure Brightness1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

uses ImageUnit, ActiveX, HistogramUnit, BrightnessUnit;

{$R *.dfm}

procedure TMainForm.Open1Click(Sender: TObject);
var
 formatInfo:string;
begin
 if OpenPictureDialog1.Execute then
 begin
 Application.CreateForm(TImageForm, ImageForm);
  ImageForm.Image1.Picture.LoadFromFile(
    OpenPictureDialog1.FileName);
  ImageForm.ClientHeight:=
    ImageForm.Image1.Picture.Height;
  ImageForm.ClientWidth:=
    ImageForm.Image1.Picture.Width;
  case (ImageForm.Image1.Picture.Bitmap.PixelFormat) of
    pf1bit : formatInfo:='Binary';
    pf8bit : formatInfo:='Gray scale';
    pf24bit: formatInfo:='True color';
    end;
  StatusBar1.SimpleText:= OpenPictureDialog1.FileName +' '+
    IntToStr(ImageForm.Image1.Picture.Width) + 'x'+
    IntToStr(ImageForm.Image1.Picture.Height) + ' '+
    formatInfo;
 end;
end;

procedure TMainForm.Save1Click(Sender: TObject);
begin
try
begin
  if SavePictureDialog1.Execute then
    TImageForm(ActiveMDIChild).Image1.Picture.SaveToFile(
    SavePictureDialog1.FileName);
end
except
  ShowMessage('Cannot complete the operation');
end;
end;

procedure TMainForm.Histogram1Click(Sender: TObject);
begin
  if ImageForm<>nil then
  begin
    ImageForm:=TImageForm(ActiveMDIChild);
    try
    begin
      Application.CreateForm(THistogramForm,HistogramForm);
      HistogramForm.ShowHistogram(ImageForm.Image1);
    end;
    except
      HistogramForm.Free;
      ShowMessage('Cannot complete the operation');
    end;
  end;
end;

procedure TMainForm.CLose1Click(Sender: TObject);
begin
  try
    ActiveMDIChild.Close;
  except
    ShowMessage('Cannot complete the operation');
  end;
end;

procedure TMainForm.Exit1Click(Sender: TObject);
begin
  Close;
end;

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;

initialization
OleInitialize(nil);
finalization
OleUninitialize
end.

untuk hasilnya.....dari coding diatas....



1

membuat histogram....

Ini coding untuk membuat histogram pengolahan citra dengan menggunakan delphi.... 


unit HistogramUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TeEngine, Series, ExtCtrls, TeeProcs, Chart;

type
  THistogramForm = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormPaint(Sender: TObject);
  private
    { Private declarations }
    MaxCount:Integer;
    HistogramGray:Array[0..255]of Integer;
    HistogramRed:Array[0..255]of Integer;
    HistogramGreen:Array[0..255]of Integer;
    HistogramBlue:Array[0..255]of Integer;
  public
    { Public declarations }
    procedure ShowHistogram(Image:TImage);
  end;

var
  HistogramForm: THistogramForm;

implementation

{$R *.dfm}
procedure THistogramForm.ShowHistogram(Image:TImage);
var
  i,j:integer;
  pixelPointer:PByteArray;
begin
try
begin
  for i:=0 to 255 do
  begin
      HistogramGray[i]:=0;
      HistogramRed[i]:=0;
      HistogramGreen[i]:=0;
      HistogramBlue[i]:=0;
  end;
  if Image.Picture.Bitmap.PixelFormat=pf8bit then
  begin
    for i:=0 to Image.Height-1 do
    begin
      pixelPointer:=Image.Picture.Bitmap.ScanLine[i];
      for j:=0 to Image.Width-1 do
      begin
        Inc(HistogramGray[pixelPointer[j]]);
      end;
    end;
    MaxCount:=0;
    for i:=0 to 255 do
      if HistogramGray[i]>MaxCount then
        MaxCount:=HistogramGray[i];
  end;
  if Image.Picture.Bitmap.PixelFormat=pf24bit then
  begin
    for i:=0 to Image.Height-1 do
    begin
      pixelPointer:=Image.Picture.Bitmap.ScanLine[i];
      for j:=0 to Image.Width-1 do
      begin
        Inc(HistogramBlue[pixelPointer[3*j]]);
        Inc(HistogramGreen[pixelPointer[3*j+1]]);
        Inc(HistogramRed[pixelPointer[3*j+2]]);
      end;
    end;
    for i:=0 to 255  do
    begin
      if HistogramRed[i]>MaxCount then
        MaxCount:=HistogramRed[i];
      if HistogramGreen[i]>MaxCount then
        MaxCount:=HistogramGreen[i];
      if HistogramBlue[i]>MaxCount then
        MaxCount:=HistogramBlue[i];
    end;
  end;
  Canvas.MoveTo(10, 160);;
  Canvas.Pen.Color:=clBlack;
  for i:=0 to 255 do
   Canvas.LineTo(10+i,
    160-round(150*HistogramGray[i]/MaxCount));
  Canvas.Pen.Color:=clRed;
  Canvas.MoveTo(10, 160);
  for i:=0 to 255 do
    Canvas.LineTo(10+i,
     160-(round(150*HistogramRed[i]/MaxCount)));
  Canvas.Pen.Color:=clGreen;
  Canvas.MoveTo(10, 160);
  for i:=0 to 255 do
    Canvas.LineTo(10+i,
      160-(round(150*HistogramGreen[i]/MaxCount)));
  Canvas.Pen.Color:=clBlue;
  Canvas.MoveTo(10, 160);
  for i:=0 to 255 do
    Canvas.LineTo(10+i,
      160-(round(150*HistogramBlue[i]/MaxCount)));
end;
except
  Free; //free the histogram form if an exception happens
  ShowMessage('Cannot complete the operation');
end;
end;

procedure THistogramForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Free;
end;

procedure THistogramForm.FormPaint(Sender: TObject);
var
  i:integer;
begin
  Canvas.MoveTo(10, 160);;
  Canvas.Pen.Color:=clBlack;
  for i:=0 to 255 do
   Canvas.LineTo(10+i,
    160-round(150*HistogramGray[i]/MaxCount));
  Canvas.Pen.Color:=clRed;
  Canvas.MoveTo(10, 160);
  for i:=0 to 255 do
    Canvas.LineTo(10+i,
     160-(round(150*HistogramRed[i]/MaxCount)));
  Canvas.Pen.Color:=clGreen;
  Canvas.MoveTo(10, 160);
  for i:=0 to 255 do
    Canvas.LineTo(10+i,
      160-(round(150*HistogramGreen[i]/MaxCount)));
  Canvas.Pen.Color:=clBlue;
  Canvas.MoveTo(10, 160);
  for i:=0 to 255 do
    Canvas.LineTo(10+i,
      160-(round(150*HistogramBlue[i]/MaxCount)));
end;
end.


unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtDlgs, Menus;

type
  TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Open1: TMenuItem;
    Save1: TMenuItem;
    CLose1: TMenuItem;
    Exit1: TMenuItem;
    OpenPictureDialog1: TOpenPictureDialog;
    SavePictureDialog1: TSavePictureDialog;
    StatusBar1: TStatusBar;
    Image1: TMenuItem;
    Histogram1: TMenuItem;
    procedure Open1Click(Sender: TObject);
    procedure Save1Click(Sender: TObject);
    procedure Histogram1Click(Sender: TObject);
    procedure CLose1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

uses ImageUnit, ActiveX, HistogramUnit;

{$R *.dfm}

procedure TMainForm.Open1Click(Sender: TObject);
var
 formatInfo:string;
begin
 if OpenPictureDialog1.Execute then
 begin
 Application.CreateForm(TImageForm, ImageForm);
  ImageForm.Image1.Picture.LoadFromFile(
    OpenPictureDialog1.FileName);
  ImageForm.ClientHeight:=
    ImageForm.Image1.Picture.Height;
  ImageForm.ClientWidth:=
    ImageForm.Image1.Picture.Width;
  case (ImageForm.Image1.Picture.Bitmap.PixelFormat) of
    pf1bit : formatInfo:='Binary';
    pf8bit : formatInfo:='Gray scale';
    pf24bit: formatInfo:='True color';
    end;
  StatusBar1.SimpleText:= OpenPictureDialog1.FileName +' '+
    IntToStr(ImageForm.Image1.Picture.Width) + 'x'+
    IntToStr(ImageForm.Image1.Picture.Height) + ' '+
    formatInfo;
 end;
end;

procedure TMainForm.Save1Click(Sender: TObject);
begin
try
begin
  if SavePictureDialog1.Execute then
    TImageForm(ActiveMDIChild).Image1.Picture.SaveToFile(
    SavePictureDialog1.FileName);
end
except
  ShowMessage('Cannot complete the operation');
end;
end;

procedure TMainForm.Histogram1Click(Sender: TObject);
begin
  if ImageForm<>nil then
  begin
    ImageForm:=TImageForm(ActiveMDIChild);
    try
    begin
      Application.CreateForm(THistogramForm,HistogramForm);
      HistogramForm.ShowHistogram(ImageForm.Image1);
    end;
    except
      HistogramForm.Free;
      ShowMessage('Cannot complete the operation');
    end;
  end;
end;

procedure TMainForm.CLose1Click(Sender: TObject);
begin
  try
    ActiveMDIChild.Close;
  except
    ShowMessage('Cannot complete the operation');
  end;
end;

procedure TMainForm.Exit1Click(Sender: TObject);
begin
  Close;
end;

initialization
OleInitialize(nil);
finalization
OleUninitialize
end.

hasil yang akan didapat untuk coding diatas...