ManipulasiGambar;

Uses Crt,Graph;
Type Freq = Array[0..255] of Longint;
TPr = Array[0..255] of Real;
RFreq = Array[0..255] of Byte;

Var Col,Row,Ver,Hor : Integer;
image : File Of Byte;
NamaFile : String[30];
X,Y : Integer;
Color : Byte;
Ukuran : Longint;
ColFreq,ColFreq1 : Freq;
RColFreq : RFreq;
Pr,Sx : TPr;
Sigma : Longint;
Pilih : Byte;

Procedure SiapkanGrafik;
Var Gd,Gm : Integer;
ErrorCode : Integer;
Begin
ClrScr;
Writeln(‘ PILIHAN : ‘); Writeln;
Writeln(‘ 1. Moon.Img’);
Writeln(‘ 2. Tank.Img’); Writeln;
Write(‘ Nomor Pilihan = ‘); Readln(Pilih);
Case Pilih Of
1 : NamaFile := ‘Moon.Img’;
2 : NamaFile := ‘Tank.Img’;
Else Begin
Writeln(‘ Pilihan Anda Tidak Ada……….’);
Halt;
End;
End;

Gd := Detect;
Initgraph(Gd,Gm,’d:\TPascal7\BGI’);
ErrorCode := GraphResult;
If ErrorCode <> grOk then
Begin
Writeln(‘Graphics error:’);
Writeln(GraphErrorMsg(ErrorCode));
Writeln(‘Program aborted…’);
Halt(1);
End;

{ set array ColFreq,ColFreq1,RColFreq = 0 }
For X := 0 to 255 do
Begin
SetRGBPalette(X,X,X,X);
ColFreq[X] := 0;
ColFreq1[X] := 0;
RColFreq[X] := 0;
End;

End;

Procedure CenterTeks(Message : String; Baris : Integer);
Begin
SetColor(Black);
SetTextJustify(CenterText,CenterText);
SetTextStyle(4,HorizDir,3);
OutTextXY(320,Baris,Message);
End;

Procedure OpenFile;
Begin
Clrscr;

Assign(image,’d:\imagepro\’+NamaFile);
{$I-}
{ Buka File }
Reset(image);
{$I+}
If IOResult <> 0 then { Buka File Gagal }
Begin
Writeln(‘Tidak Ketemu’);
Halt(1);
End;

Ukuran := FileSize(image); { Baca ukuran File }

{ tentukan ukuran tampilan gambar }
If Ukuran = 64000 Then Begin
Ver := 199; Hor := 319;
End
Else If Ukuran = 65536 Then Begin
Ver := 255; Hor := 255;
End;
End;

{ Tampilkan Histogram }
Procedure TampilGrafik(ColFreq : Freq);
Var X,Y : Integer;
I,J : Integer;
L : Byte;
K : String;
Height : Longint;
Begin
Bar(0,0,GetMaxX,GetMaxY);
X:= 60; Y := 420;
Rectangle(X,Y-380,X+510,Y);
SetTextStyle(2,VertDir,5);
OutTextXY(15,240,’Frekuensi Warna’);
SetTextJustify(CenterText,CenterText);
SetTextStyle(2,HorizDir,5);

{ Keterangan Grafik Sumbu X }
L := 0;
For I := 0 To 12 Do
Begin
J := X+I*40;
Str(L,K);
Circle(J,Y,1);
OutTextXY(J,Y+10,K);
L := L + 20;
End;

{ Keterangan Grafik Sumbu Y }
L := 0;
For I := 0 To 16 Do
Begin
J := Y-I*20;
Str(L,K);
Circle(X,J,1);
OutTextXY(X-15,J,K+’K’);
L := L + 4;
End;

{ Penggambaran Garis Histogram }
For I := 0 To 255 Do
Begin
{ Proses Penghitungan Ketinggian Garis Grafik }
Height := ((ColFreq[I] div 4000)*20)+Round(((ColFreq[I] mod 4000)/4000)*20);
Line(X+I*2,Y,X+I*2,Y-Height);
End;

CenterTeks(‘Tekan Sembarang Tombol ‘,460);
End;

Procedure ManipulasiGambar;
Begin
SiapkanGrafik;
OpenFile;
Bar(0,0,GetMaxX,GetMaxY);
CenterTeks(‘Gambar Asli dari ‘+NamaFile,20);

{ menampilkan gambar asli }
For Row := 0 to Ver do
For Col := 0 to Hor do
Begin
Read(image,Color);
ColFreq[Color] := ColFreq[Color] + 1; { Hitung ColFreq utk penggambaran histogram}
Putpixel(192+Col,100+Row,{Pixel Asli}Color Div 16 + 2);
End;
Close(image);
CenterTeks(‘Tekan Sembarang Tombol’,440);
Readln; ClearDevice;
TampilGrafik(ColFreq);
Readln; ClearDevice;

Sigma := Ver*Hor; { Banyak pixel yang ditampilkan }

For X := 0 to 255 Do
Pr[X] := ColFreq[X]/Sigma;

Sx[0] := Pr[0];
RColFreq[0] := Round(Sx[0]);

{ Hitung nilai RColFreq dari Sx }
For X := 1 to 255 Do Begin
Sx[X] := (Sx[X-1] + Pr[X]);
RColFreq[X] := Round(Sx[X]*255);
End;

OpenFile;
Bar(0,0,GetMaxX,GetMaxY);
CenterTeks(‘Gambar Hasil Histogram Equalization ‘,20);

{ Tampilkan gambar setelah diproses }
For Row := 0 to Ver do
For Col := 0 to Hor do
Begin
Read(image,Color);
Putpixel(192+Col,100+Row,{Pixel diproses}RColFreq[Color] Div 16);
ColFreq1[RColFreq[Color]] := ColFreq1[RColFreq[Color]] + 1;
End;
Close(image);
CenterTeks(‘Tekan Sembarang Tombol’,440);
Readln; ClearDevice;

TampilGrafik(ColFreq1);
Repeat Until Keypressed;
End;

Begin
ManipulasiGambar;
CloseGraph;
End.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: