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()
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
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 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
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
img = New Bitmap(ofd.FileName) pbFoto.BackgroundImage = img
End If End Sub
Private Sub btnKprs_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnKprs.Click
Call register_warna()
For i As Integer = 0 To 255 Next
For i As Integer = 0 To warna.GetUpperBound(0) Next
w = img.Width - 1 h = img.Height - 1
For y As Integer = 0 To img.Height - 1 For x As Integer = 0 To img.Width - 1
Call hitungAC(img.GetPixel(x, y).R) Call hitungAC(img.GetPixel(x, y).G) Call hitungAC(img.GetPixel(x, y).B) Next
Next
Call saveFile() End Sub
Private Sub frmKompresiAC_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call initialize() End Sub
Private Sub saveFile()
Dim sw As New StreamWriter(lblFile.Text, False)
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 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 - 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
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
Private Sub btnFile_Click(ByVal sender As
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 System.Object, ByVal e As System.EventArgs) Handles 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
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 += 1
Loop
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)
End Sub #End Region
#Region "Langkah 6"
Private Sub nilaicabang(ByVal start As Integer, ByVal jumlah As Integer, ByVal nilai As Byte)
For i As Integer = start To start + jumlah - 1 warnaawal(i).cabang &= nilai
Next
End Sub
Private Function hitungselisih(ByVal jumlah As Integer, ByVal start As Integer) As Integer
hitungselisih = 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 nilaiatas = 0
nilaibawah = 0
For j As Integer = start To start + i nilaiatas += warnaawal(j).frek
Next
For j As Integer = start + i + 1 To akhir nilaibawah += warnaawal(j).frek
Next
selisih(i) = Math.Abs(nilaiatas - nilaibawah) + start
Next
For i As Integer = selisih.GetUpperBound(0) To 1 Step -1
If selisih(i - 1) = selisih(i) Then hitungselisih = i
ElseIf selisih(i - 1) > selisih(i) Then hitungselisih = i
Exit For
End If Next
Return hitungselisih + start End Function
Private Sub ceksf()
Dim pos As Integer = 0
Dim maxnow As Integer = 0
Do Until sf.GetUpperBound(0) = JlhSmpl 'jika jumlah sample sama dengan jumlah kelompok sf berarti sudah selesai
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(imgHexa) 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 G As Integer Dim B As Integer End Structure
Structure tabelSF
Dim warna As Integer Dim cabang As String
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 status As Boolean, ByRef status2 As Boolean) As Integer Dim hasil As Integer
Dim ketemu As Boolean = False
For i As Integer = 0 To jlhSmpl - 1 If bin = sf(i).cabang Then
hasil = sf(i).warna status = True
Return hasil
End If Next
status2 = True Return Nothing End Function
Private Sub readData()
Dim sr As New StreamReader(sfFile)
imgHexa = sr.ReadLine w = sr.ReadLine
h = sr.ReadLine ReDim img(w, h)
While Not sr.EndOfStream jlhSmpl += 1
ReDim Preserve sf(jlhSmpl - 1)
sf(jlhSmpl - 1).warna = sr.ReadLine sf(jlhSmpl - 1).cabang = sr.ReadLine rtbTabel.Text &= sf(jlhSmpl - 1).warna & vbCrLf & sf(jlhSmpl - 1).cabang & vbCrLf
End While
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