Source Code Gambar jpg To grayscale Using Delphi

Selamat Siang para pemirsa blog seputarinternetku berjumpa kembali dengan saya penulis blog yang simple ini, hehehe
ok awal ceritanya sih ini tugas dari dosen yang di suruh untuk mengubah gambar jpg dengan mengganti gambar menjadi hitam putih atau bisa di sebut grayscale, tugas pertama sih cuman di suruh untuk upload foto setelah itu di copy ke dalam form sampingnya dan di simpan ke format jpg akhirnya tugas pertama sucses terselesaikan alhamdulilah dengan semangat tinggi tugas apapun jika kita SEMANGAT pasti bisa teratasi :)
ok langsung saja ini scripnya
oh ya klo untuk mempercantik tampilan seperti punya saya anda harus menginstal Alphascin dulu..
 
 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, Menus, jpeg, clipbrd, ComCtrls, inifiles,
  sSkinProvider, sSkinManager;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    open1: TMenuItem;
    Open2: TMenuItem;
    BitBtn1: TBitBtn;
    OpenDialog1: TOpenDialog;
    Timer1: TTimer;
    StatusBar1: TStatusBar;
    BitBtn2: TBitBtn;
    SaveDialog1: TSaveDialog;
    sSkinManager1: TsSkinManager;
    sSkinProvider1: TsSkinProvider;
    i1: TMenuItem;
    g1: TMenuItem;
    Greyscale1: TMenuItem;
    Greyscale2: TMenuItem;
    Greyscale3: TMenuItem;
    R1: TMenuItem;
    ScrollBox2: TScrollBox;
    Image1: TImage;
    ScrollBox1: TScrollBox;
    Image2: TImage;
    procedure Open2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure g1Click(Sender: TObject);
    procedure Greyscale1Click(Sender: TObject);
//    procedure Greyscale1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}


procedure TForm1.Open2Click(Sender: TObject);
begin
if not OpenDialog1.Execute then Exit;
Image1.Picture.LoadFromFile(OpenDialog1.FileName);
statusbar1.Panels[0].Text:= opendialog1.FileName;
end;


procedure TForm1.BitBtn1Click(Sender: TObject);

begin
image2.Picture.Assign(image1.Picture);

end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
if SaveDialog1.execute then
begin
image2.picture.SaveToFile(SaveDialog1.FileName + '.jpg');
showmessage(savedialog1.FileName);
end
else
  begin
    MessageBox(0,PChar('Gagal melakukan asosiasi file'),PChar('Kesalahan'),MB_OK+MB_TASKMODAL+MB_ICONINFORMATION);
  end;
end;
// if SaveDialog1.execute then
 //begin
 // image2.picture.SaveToFile(SaveDialog1.FileName);
  //ShowMessage(SaveDialog1.FileName);
  //SaveDialog1.Filter := '(*.jpg)';
  //if Pos('.jpg',SaveDialog1) = 0 then SaveDialog1 := SaveDialog1 + '*.jpg';
procedure TForm1.g1Click(Sender: TObject);
var gg,r, g,b:
byte;
x, y,i, j:
integer;
imgBuff:TBitmap;
begin
imgBuff:=TBitmap.Create;;
imgBuff.Assign(image1.Picture.Graphic); // convert dari jpg ke bitmap

image2.Picture.Bitmap:=TBitmap.Create;
image2.Picture.Bitmap.PixelFormat:=pf24bit;
image2.Picture.Bitmap.Width:=imgBuff.Width;
image2.Picture.Bitmap.Height:=imgBuff.Height;

For i:=0 to imgBuff.width-1 do
For j:=0 to imgBuff.Height-1 do
image2.canvas.pixels[i,j]:=imgBuff.Canvas.pixels[i,j];
begin
for x:=0 to image2.picture.Width do begin
for y:=0 to image2.picture.Height do begin
r:=getRvalue(image2.canvas.Pixels[x,y]);
g:=getGvalue(image2.canvas.Pixels[x,y]);
b:=getBvalue(image2.canvas.Pixels[x,y]);
gg:=round((0.299*R) + (0.587*G) + (0.114*B));
image2.Canvas.Pixels[x,y]:=RGB(gg,gg,gg);
end;
end;
end;
end;

procedure TForm1.Greyscale1Click(Sender: TObject);
var gg,r, g,b:
byte;
x, y,i, j:
integer;
imgBuff:TBitmap;
begin
imgBuff:=TBitmap.Create;;
imgBuff.Assign(image1.Picture.Graphic); // convert dari jpg ke bitmap

image2.Picture.Bitmap:=TBitmap.Create;
image2.Picture.Bitmap.PixelFormat:=pf24bit;
image2.Picture.Bitmap.Width:=imgBuff.Width;
image2.Picture.Bitmap.Height:=imgBuff.Height;

For i:=0 to imgBuff.width-1 do
For j:=0 to imgBuff.Height-1 do
image2.canvas.pixels[i,j]:=imgBuff.Canvas.pixels[i,j];
begin
for x:=0 to image2.picture.Width do begin
for y:=0 to image2.picture.Height do begin
r:=getRvalue(image2.canvas.Pixels[x,y]);
g:=getGvalue(image2.canvas.Pixels[x,y]);
b:=getBvalue(image2.canvas.Pixels[x,y]);
gg:=round((0.333*R) + (0.333*G) + (0.333*B));
image2.Canvas.Pixels[x,y]:=RGB(gg,gg,gg);
end;
end;
end;
end;
end.

untuk source codenya bisa di download di sini

Share this

Related Posts

Latest
Previous
Next Post »

jangan lupa tinggalkan jejak :)