LAMPIRAN A: LISTING PROGRAM Form Menu Utama
LAMPIRAN A
LAMPIRAN A: LISTING PROGRAM Form Menu Utama
Public Class MenuUtama Private Sub
AlgoritmaKompresiToolStripMenuItem_Click_1( ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles
AlgoritmaKompresiToolStripMenuItem.Click jenis.ShowDialog()
End Sub Private Sub KeluarToolStripMenuItem_Click( ByVal
sender As System.Object, ByVal e As System.EventArgs)
Handles KeluarToolStripMenuItem.Click End
End Sub End Class
Form Menu Jenis
Public Class jenis Private Sub Button2_Click( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim KAC As New kompresAC
KAC.ShowDialog()
End Sub Private Sub btnkompres_Click( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnkompres.Click
Dim KSF As New kompresiSF
KSF.ShowDialog()
End Sub Private Sub Button1_Click( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DAC As New DekompresAC
DAC.ShowDialog()
End Sub
Private Sub btnDkompres_Click( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnDkompres.Click
Dim DSF As New DekompresiSF
DSF.ShowDialog()
End Sub End Class
Form Kompresi Arithmetic Coding
Imports System.IO Public Class frmKompresiAC
Structure _warna Dim index As Integer Dim frek As Integer Dim high_range As Double Dim low_range As Double
End Structure Structure _high_low
Dim high As Double Dim low As Double
End Structure Private hilow As _high_low Private temp(255) As _warna Private warna(0) As _warna Private totFrek As Decimal = 0 Private img As Bitmap Private low As Decimal = 0 Private high As Decimal = 1 Private CR = 0 Private w As Decimal = 0 Private h As Decimal = 0 Private Function getHighLow( ByVal nilai As Integer )
As _high_low For i As Integer = 0 To warna.GetUpperBound(0)
If warna(i).index = nilai Then
getHighLow.high = warna(i).high_range getHighLow.low = warna(i).low_range
Return getHighLow End If
Next End Function Private Function getLow() As Double End Function Private Sub hitungAC( ByVal nilai As Integer )
CR = high - low hilow = getHighLow(nilai) high = low + (CR * hilow.high) low = low + (CR * hilow.low)
End Sub Private Sub set_warna() Private Sub initialize()
Call set_warna()
ofd.Multiselect = False ofd.Filter = "bitmap file|*.bmp" ofd.FileName = Nothing sfd.Filter = "Arithmetic-Files|*.ac" btnKprs.Enabled = True
End Sub Private Sub register_warna()
For y As Integer = 0 To img.Height - 1 For x As Integer = 0 To img.Width - 1
temp(img.GetPixel(x, y).R).frek += 1 temp(img.GetPixel(x, y).G).frek += 1 temp(img.GetPixel(x, y).B).frek += 1
Next ReDim Preserve warna(warna.GetUpperBound(0) - 1) For i As Integer = 0 To warna.GetUpperBound(0)
If i = 0 Then
warna(i).low_range = 0 warna(i).high_range = warna(i).frek / totFrek
ElseIf i = warna.GetUpperBound(0) Then
warna(i).low_range = warna(i - 1).high_range warna(i).high_range = 1
Else
warna(i).low_range = warna(i - 1).high_range
warna(i).high_range = warna(i).low_range
- (warna(i).frek / totFrek)
End If Next
End Sub
Private Sub btnOpen_Click( ByVal sender As
Call hitungAC(img.GetPixel(x, y).G) Call hitungAC(img.GetPixel(x, y).B)
Dim sw As New StreamWriter(lblFile.Text, False )
End Sub Private Sub saveFile()
MyBase .Load Call initialize()
System.Object, ByVal e As System.EventArgs) Handles
End Sub Private Sub frmKompresiAC_Load( ByVal sender As
Next Next Call saveFile()
For y As Integer = 0 To img.Height - 1 For x As Integer = 0 To img.Width - 1
System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
w = img.Width - 1 h = img.Height - 1
Next For i As Integer = 0 To warna.GetUpperBound(0) Next
Call register_warna() For i As Integer = 0 To 255
System.Object, ByVal e As System.EventArgs) Handles btnKprs.Click
End If End Sub Private Sub btnKprs_Click( ByVal sender As
img = New Bitmap(ofd.FileName) pbFoto.BackgroundImage = img
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
sw.WriteLine(low) sw.WriteLine(w) sw.WriteLine(h) sw.WriteLine(warna(i).index) sw.WriteLine(warna(i).low_range) sw.WriteLine(warna(i).high_range)
Next
sw.Close()
MessageBox.Show( "Data Berhasil Disimpan" )
End Sub End Class
Form Dekompresi Arithmetic Coding
Imports System.IO Public Class frmDkompresiAC
Private file As String Private acffile As String Private low As Decimal = 0 Private high As Decimal = 0 Private cr As Decimal = 0 Private es As Decimal = 0 Private state As String = "R" Structure _img
Dim r As Integer Dim g As Integer Dim b As Integer
End Structure Structure _warna
Dim index As Integer Dim frek As Integer Dim high_range As Double Dim low_range As Double
End Structure Structure _high_low
Dim high As Double Dim low As Double
End Structure Private img(0, 0) As _img Private hilow As _high_low Private warna(0) As _warna Private w As Integer = 0 Private h As Integer = 0
Private Sub readData() Dim index As Integer = 0 Dim sr = New StreamReader(file)
low = sr.ReadLine w = sr.ReadLine h = sr.ReadLine
While Not sr.EndOfStream
warna(index).index = sr.ReadLine warna(index).low_range = sr.ReadLine warna(index).high_range = sr.ReadLine index += 1
ReDim Preserve warna(index) End While
index -= 1
ReDim Preserve warna(index)
sr.Close()
End Sub Private Function Dkompresi( ByVal nilai As Decimal ) As
Decimal
Dkompresi = nilai hilow = getHighLow(nilai) cr = hilow.high - hilow.low Dkompresi = Dkompresi / cr
Return Dkompresi End Function Private Function getNilai( ByVal nilai As Decimal ) As
Integer For i As Integer = 0 To warna.GetUpperBound(0)
If nilai >= warna(i).low_range And nilai <=
warna(i).high_range Then
Return warna(i).index End If
Next Return Nothing
End Function Private Function getHighLow( ByVal nilai As Decimal )
As _high_low For i As Integer = 0 To warna.GetUpperBound(0)
If nilai >= warna(i).low_range And nilai <=
warna(i).high_range Then getHighLow.high = warna(i).high_range getHighLow.low = warna(i).low_range
Return getHighLow End If
Next End Function Private Sub inisialize()
Call readData()
es = low
ReDim img(w, h) End Sub Private Sub Initialize()
acfFile = lblFile.Text OFD.Multiselect = False ofd.Filter = "Arithmetic-Files|*.ac" OFD.FileName = Nothing sfd.Filter = "bitmap file|*.bmp" btnDkprs.Enabled = True
End Sub
System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
file = ofd.FileName
End If End Sub Private Sub btnDkprs_Click( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnDkprs.Click
Dim x As Integer = 0 Dim y As Integer = 0 Dim r, g, b As Integer Call inisialize() Call saveFile() Dim gbr As New Bitmap(w + 1, h + 1) Do Until y > h
Select Case state Case "R"
r = getNilai(es) state = "G"
Case "G"
g = getNilai(es) state = "B"
Case "B"
b = getNilai(es) state = "R"
gbr.SetPixel(x, y, Color.FromArgb(r,
g, b)) x += 1
End Select If x > w Then
x = 0 y += 1
End If Loop
ListBox1.Items.Add(es) pbFoto.BackgroundImage = gbr
End Sub Private Sub frmDkompresiAC_Load( ByVal sender As
MyBase .Load Call Initialize()
End Sub Private Sub btnFile1_Click( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnFile1.Click
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
lblFile.Text = sfd.FileName btnDkprs.Enabled = True
End If End Sub
End Class
Form Kompresi Shannon-Fano
Imports System.IO Public Class frmKompresiSF #Region "Variabel"
Private warnaawal(255) As color Private warnaurut(255) As color Private w, h As Integer Private JlhSmpl As Integer = 255 Private img As Bitmap Private sf() As shannonfano Private sftemp() As shannonfano Private selisih() As Long Private imgBinary As String = "" Private imgDesimal As String = "" Private imgHexa As String = "" Structure shannonfano
Dim sampel As Integer Dim start As Integer
End Structure Structure color
Dim nilai As Byte Dim frek As Integer Dim cabang As String Dim hexa As String
End Structure #End Region #Region "Langkah 1"
Private Sub Initialize()
OFD.Multiselect = False OFD.Filter = "bitmap file|*.bmp" OFD.FileName = Nothing sFd.Filter = "Shannon-Fano-Files|*.sf" btnKprs.Enabled = True
End Sub Private Sub Form1_Load( ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase .Load Call Initialize()
End Sub Private Sub btnOpen_Click( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then pbFoto.Image = img
End If End Sub Private Sub btnFile_Click( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
If sFd.ShowDialog = Windows.Forms.DialogResult.OK Then
lblFile.Text = sFd.FileName btnKprs.Enabled = True
End If End Sub Private Sub btnKprs_Click( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnKprs.Click
Call hitungPL() Call RegisterWarna() Call gambartobiner() Call bin_to_hex(imgBinary) Call saveFile()
End Sub Private Sub hitungPL()
w = img.Width h = img.Height
End Sub #End Region #Region "Langkah 2"
Private Sub resetsample() For p As Integer = 0 To 255
warnaawal(p).nilai = p warnaawal(p).frek = 0 warnaawal(p).cabang = Nothing
Next End Sub Private Sub RegisterWarna()
Call resetsample() Call sortsample() Call HitungJlhsmpl() Call registersf() Call ceksf()
End Sub #End Region
Private Sub sortsample() Dim awal As Integer = 0 Dim akhir As Integer = 255 Dim nilai As Integer = 0 Dim frek As Long = 0 Dim pos As Integer = 0 Dim temp As color Do Until awal = akhir
pos = awal nilai = warnaawal(awal).nilai frek = warnaawal(awal).frek
For i As Integer = awal To akhir If warnaawal(i).frek > frek Then
pos = i nilai = warnaawal(pos).nilai frek = warnaawal(pos).frek
End If Next
temp.nilai = nilai temp.frek = frek
warnaawal(pos).nilai = warnaawal(awal).nilai
warnaawal(pos).frek = warnaawal(awal).frek warnaawal(awal).nilai = temp.nilai warnaawal(awal).frek = temp.frek awal += 1Loop End Sub
#End Region #Region "Langkah 4"
Private Sub HitungJlhsmpl() For i As Integer = 0 To 255
If warnaawal(i).frek = 0 Then
JlhSmpl = i - 1
Exit For End If
Next
lblSampel.Text = JlhSmpl + 1
End Sub #End Region #Region "Langkah 5"
Private Sub registersf() ReDim Preserve sf(0)
sf(0).sampel = JlhSmpl + 1 sf(0).start = 0
Dim pos As Integer = 0
Next For j As Integer = start + i + 1 To akhir Next
End Function Private Sub ceksf()
Next Return hitungselisih + start
Exit For End If
hitungselisih = i
ElseIf selisih(i - 1) > selisih(i) Then
hitungselisih = i
If selisih(i - 1) = selisih(i) Then
1 Step -1
Next For i As Integer = selisih.GetUpperBound(0) To
selisih(i) = Math.Abs(nilaiatas - nilaibawah)
nilaiatas += warnaawal(j).frek
End Sub #End Region #Region "Langkah 6"
For j As Integer = start To start + i
nilaiatas = 0 nilaibawah = 0
Dim nilaiatas As Long Dim nilaibawah As Long Dim akhir As Integer = start + jumlah - 1 ReDim selisih(jumlah - 2) For i As Integer = 0 To jumlah - 2
hitungselisih = 0
Integer , ByVal start As Integer ) As Integer
Next End Sub Private Function hitungselisih( ByVal jumlah As
warnaawal(i).cabang &= nilai
For i As Integer = start To start + jumlah - 1
jumlah As Integer , ByVal nilai As Byte )
Private Sub nilaicabang( ByVal start As Integer , ByVal
- start
Dim maxnow As Integer = 0 Do Until sf.GetUpperBound(0) = JlhSmpl 'jika
prog.Value = pos maxnow = pos
For x = 1 To
dec = 0 awal = (klmpk * 4) + 1 temps = New String (Mid(bin, awal, 4)) bins = ""
Dim dec As Integer = 0 Dim str As String = "" Dim strb, strd, strh As String Dim length As Integer = Len(bin) Dim temp As Integer = Nothing Dim bins As String Dim temps As String = "" Dim awal As Integer = 0 Dim x As Integer = Nothing Dim ch As Integer = length / 4 Dim sisa As Integer = length - (ch * 4) For klmpk As Integer = 0 To ch - 1
Public Function bin_to_hex( ByVal bin As String ) As String
End Sub #Region "Langkah 8"
prog.Value = maxnow lblP.Text = maxnow
End If Loop
Exit Do End If If pos > maxnow Then
jumlah sample sama dengan jumlah kelompok sf berarti
sudah selesaiEnd If If pos > sf.GetUpperBound(0) Then
pos += 1
End If If Not ulang Then
ulang = False
Else
pos = 0 ulang = True
If sf(pos).sampel > 1 Then Call pecahsf(pos)
4 If temp <> "0" Then
dec += (2 ^ (4 - x))
End If
bins &= temp
Next
imgHexa &= Hex(dec)
Next
dec = 0 bins = ""
For x = 1 To sisa
awal = (ch * 4) + 1 temps = New String (Mid( String .Concat(bin, "0000" ), awal, 4)) temp = Val(Mid(temps, x, 1))
If temp <> "0" Then
dec += (2 ^ (4 - x))
End If
bins &= temp
Next
imgHexa &= Hex(dec)
Return str End Function
#End Region Private Sub saveFile()
Dim sw As New StreamWriter(lblFile.Text, False )
sw.WriteLine(w) sw.WriteLine(h)
For i As Integer = 0 To JlhSmpl
sw.WriteLine(warnaawal(i).nilai) sw.WriteLine(warnaawal(i).cabang)
Next
sw.Close() MessageBox.Show( "Data Berhasil Disimpan" )
End Sub End Class
Form Dekompresi Shannon-Fano
Imports System.IO Public Class frmDkompresiSF
Structure warna_ Dim R As Integer
Dim G As Integer Dim B As Integer
Dim sr As New StreamReader(sfFile)
rtbHexa.Text = imgHexa
End While
sf(jlhSmpl - 1).warna = sr.ReadLine sf(jlhSmpl - 1).cabang = sr.ReadLine rtbTabel.Text &= sf(jlhSmpl - 1).warna & vbCrLf & sf(jlhSmpl - 1).cabang & vbCrLf
ReDim Preserve sf(jlhSmpl - 1)
jlhSmpl += 1
ReDim img(w, h) While Not sr.EndOfStream
imgHexa = sr.ReadLine w = sr.ReadLine h = sr.ReadLine
End Function Private Sub readData()
End Structure Structure tabelSF
Next Return Nothing
Return hasil End If
hasil = sf(i).warna status = True
If bin = sf(i).cabang Then
Dim hasil As Integer Dim ketemu As Boolean = False For i As Integer = 0 To jlhSmpl - 1
status As Boolean , ByRef status2 As Boolean ) As Integer
End Structure Private w, h As Integer Private imgHexa As String Private imgBiner As String Private sfFile As String Private img(1, 1) As warna_ Private sf() As tabelSF Private jlhSmpl As Integer = 0 Private Function binertosf( ByVal bin As String , ByRef
Dim warna As Integer Dim cabang As String
End Sub
Private Sub Initialize()
sfFile = lblFile.Text OFD.Multiselect = False OFD.Filter = "Shannon-Fano-Files|*.sf" OFD.FileName = Nothing SFD.Filter = "bitmap file|*.bmp" btnDkprs.Enabled = True
End Sub Private Sub frmDkompresi_Load( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MyBase .Load Call Initialize()
End Sub Private Sub btnOpen_Click( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
lblFile.Text = OFD.FileName sfFile = lblFile.Text
Call readData() End If
End Sub Private Sub saveFile()
MessageBox.Show( "Data Berhasil Disimpan" )
End Sub Private Sub btnFile_Click( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
lblFile.Text = SFD.FileName btnDkprs.Enabled = True
End If End Sub
End Class