Perancangan Dan Pembuatan Miniatur Smarthome Berbasis Mikrokontroler Atmega328

LISTING PROGRAM

mainform.vb
Public Class mainform
Private Sub KompresiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
KompresiToolStripMenuItem.Click
kompresform.Show()
End Sub
Private Sub DekompresiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
DekompresiToolStripMenuItem.Click
dekompresform.Show()
End Sub
Private Sub BantuanToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
BantuanToolStripMenuItem.Click
bantuanform.Show()
End Sub
Private Sub TentangAppToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
TentangAppToolStripMenuItem.Click
tappform.Show()
End Sub
Private Sub MenuFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles

MenuFileToolStripMenuItem.Click
End Sub
End Class

kompressform.vb
Imports System.IO
Imports System.Text
Public Class kompresform
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim gambar As Byte()
Dim namaberkas As String
Dim ofd As New OpenFileDialog()
ofd.Filter = "BMP File|*.bmp"
Dim dr As DialogResult = ofd.ShowDialog()
If dr = DialogResult.OK Then
lokasi.Text = ofd.FileName
namaberkas = lokasi.Text.Substring(lokasi.Text.LastIndexOf("\") + 1)
nama_berkas.Text = namaberkas
gambar = File.ReadAllBytes(lokasi.Text)
ukuran_berkas.Text = gambar.Length

pb1.Image = New Bitmap(New MemoryStream(gambar))
resolusi_berkas.Text = pb1.Image.Width & "X" & pb1.Image.Height
TextBox5.Text = lokasi.Text
End If
pb1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles
ukuran_berkas.TextChanged

Universitas Sumatera Utara

End Sub
Private
Dim
Dim
Dim
Dim
Dim
For


Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
namafix As String
size As Integer
time As New Stopwatch()
sb As New StringBuilder()
gambar As Byte() = File.ReadAllBytes(lokasi.Text)
Each bit As Byte In gambar
sb.Append(ChrW(bit))
Next
namafix = nama_berkas.Text.Substring(0, nama_berkas.Text.LastIndexOf("."))
time.Start()
'string asli = Encoding.U.GetString(gambar);
LZW_Encoder.LZW_compressed = LZW.compress(sb.ToString(), size)
LZW_Encoder.tulis_file_lzw(size, "E:\kompresi\" & namafix & ".lzw")
time.Stop()
LZW_Encoder.getInfo(lokasi.Text, "E:\kompresi\" & namafix & ".lzw")
ukuranhasil.Text = LZW_Encoder.UkuranHasil.ToString()
rs.Text = LZW_Encoder.HitungRc().ToString("F2")
cr.Text = LZW_Encoder.HitungCr().ToString("F2")
ss.Text = LZW_Encoder.HitungSS().ToString("F2")

waktu.Text = time.ElapsedMilliseconds
'rd_lzw.Text = (1 - Single.Parse(cr_lzw.Text)).ToString("F2")
MessageBox.Show("Berhasil dikompresi! Lokasi file adalah E:\kompresi\" & namafix &

".lzw")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim nama As String
Dim time As New Stopwatch()
time.Start()
nama = nama_berkas.Text.Substring(0, nama_berkas.Text.LastIndexOf("."))
ArithmeticCompress.Execute(New String() {lokasi.Text, "E:\kompresi\" & nama &
".ac"})
LZW_Encoder.getInfo(lokasi.Text, "E:\kompresi\" & nama & ".ac")
time.Stop()
ukuranhasil.Text = LZW_Encoder.UkuranHasil.ToString()
rs.Text = LZW_Encoder.HitungRc().ToString("F2")
cr.Text = LZW_Encoder.HitungCr().ToString("F2")
ss.Text = LZW_Encoder.HitungSS().ToString("F2")
waktu.Text = time.ElapsedMilliseconds

'rd_ac.Text = (1 - Single.Parse(cr_ac.Text)).ToString("F2")
MessageBox.Show("Berhasil dikompresi! Lokasi file adalah E:\kompresi\" & nama &
".ac")
End Sub
Private
Dim
Dim
Dim
Dim
For

Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
size As Integer
time As New Stopwatch()
sb As New StringBuilder()
gambar As Byte() = File.ReadAllBytes(lokasi.Text)
Each bit As Byte In gambar
sb.Append(ChrW(bit))
Next
Dim nama As String

time.Start()
nama = nama_berkas.Text.Substring(0, nama_berkas.Text.LastIndexOf("."))
'string asli = Encoding.U.GetString(gambar);
LZW_Encoder.LZW_compressed = LZW.compress(sb.ToString(), size)
LZW_Encoder.tulis_file_lzw(size, "E:\kompresi\hasil.lzw_temp")
ArithmeticCompress.Execute(New String() {"E:\kompresi\hasil.lzw_temp",
"E:\kompresi\" & nama & ".lzw_ac"})
LZW_Encoder.getInfo(lokasi.Text, "E:\kompresi\" & nama & ".lzw_ac")

Universitas Sumatera Utara

time.Stop()
ukuranhasil.Text = LZW_Encoder.UkuranHasil.ToString()
rs.Text = LZW_Encoder.HitungRc().ToString("F2")
cr.Text = LZW_Encoder.HitungCr().ToString("F2")
ss.Text = LZW_Encoder.HitungSS().ToString("F2")
waktu.Text = time.ElapsedMilliseconds
'rd_gab.Text = (1 - Single.Parse(cr_gab.Text)).ToString("F2")
MessageBox.Show("Berhasil dikompresi! Lokasi file adalah E:\kompresi\" & nama &
".lzw_ac")

End Sub
Private Sub ukuranhasil_TextChanged(sender As Object, e As EventArgs) Handles
ukuranhasil.TextChanged
End Sub
End Class

dekompresform.vb
Imports System.IO
Public Class dekompresform
Dim asli As String
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim ofd As New OpenFileDialog()
ofd.InitialDirectory = "E:\kompresi"
Dim dr As DialogResult
dr = ofd.ShowDialog()
If (dr = DialogResult.OK) Then
lokasi.Text = ofd.FileName
TextBox2.Text = lokasi.Text.Substring(lokasi.Text.LastIndexOf("\") + 1)
TextBox3.Text = File.ReadAllBytes(lokasi.Text).Length
TextBox4.Text = lokasi.Text

End If
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim ofd1 As New OpenFileDialog()
If ofd1.ShowDialog() = Windows.Forms.DialogResult.OK Then
asli = ofd1.FileName
End If
End Sub
Public Function checkfiletype(tipe As String) As Boolean
If TextBox2.Text.Substring(TextBox2.Text.LastIndexOf(".") + 1) = tipe Then
Return True
End If
Return False
End Function
Public Function HitungMSE(gb1 As Bitmap, gb2 As Bitmap) As Double
Dim sum As Double
sum = 0
For i As Integer = 0 To gb1.Height - 1
For j As Integer = 0 To gb1.Width - 1
sum += Math.Pow(gb1.GetPixel(j, i).R - gb2.GetPixel(j, i).R, 2)

sum += Math.Pow(gb1.GetPixel(j, i).G - gb2.GetPixel(j, i).G, 2)
sum += Math.Pow(gb1.GetPixel(j, i).B - gb2.GetPixel(j, i).B, 2)
Next
Next
Return sum / (gb1.Height * gb2.Width)
End Function
Public Function HitungPSNR(mse As Double) As Double

Universitas Sumatera Utara

Return Math.Log10(Math.Pow(255, 2) / mse)
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If checkfiletype("lzw") = False Then
MessageBox.Show("Maaf, Tipe File Tidak cocok atau File belum dipilih!")
Return
End If
Dim time As New Stopwatch()
time.Start()
Dim decocde As List(Of Integer) = LZWDecoder.decode(File.ReadAllBytes(lokasi.Text))

Dim dekom As String = LZW.decompress(decocde)
time.Stop()
Dim nama As String
nama = TextBox2.Text.Substring(0, TextBox2.Text.LastIndexOf("."))
Dim output As New FileStream("E:\Dekompresi\" & nama & "_lzw.bmp", FileMode.Create)
For Each ch As Char In dekom
output.WriteByte(CByte(AscW(ch)))
Next
output.Close()
Dim gambar() As Byte
gambar = File.ReadAllBytes("E:\Dekompresi\" & nama & "_ac.bmp")
pb1.Image = New Bitmap(New MemoryStream(gambar))
mse.Text = HitungMSE(New Bitmap("E:\dekompresi\" & nama & "_lzw.bmp"), New
Bitmap(asli))
psnr.Text = HitungPSNR(Double.Parse(mse.Text))
cr.Text = time.ElapsedMilliseconds
MessageBox.Show("Berhasil didekompresi! Lokasi file adalah E:\Dekompresi\" & nama &
"_lzw.bmp")
pb1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If checkfiletype("ac") = False Then
MessageBox.Show("Maaf, Tipe File Tidak cocok atau File belum dipilih!")
Return
End If
Dim time As New Stopwatch()
Dim nama As String
nama = TextBox2.Text.Substring(0, TextBox2.Text.LastIndexOf("."))
time.Start()
ArithmeticDecompress.Execute(New String() {lokasi.Text, "E:\dekompresi\" & nama &
"_ac.bmp"})
time.Stop()
Dim gambar() As Byte
gambar = File.ReadAllBytes("E:\Dekompresi\" & nama & "_ac.bmp")
mse.Text = HitungMSE(New Bitmap(New MemoryStream(gambar)), New Bitmap(asli))
psnr.Text = HitungPSNR(Double.Parse(mse.Text))
cr.Text = time.ElapsedMilliseconds
pb1.Image = New Bitmap(New MemoryStream(gambar))
MessageBox.Show("Berhasil di dekompresi! Lokasi file adalah E:\Dekompresi\" & nama
& "_ac.bmp")
pb1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If checkfiletype("lzw_ac") = False Then
MessageBox.Show("Maaf, Tipe File Tidak cocok atau File belum dipilih!")
Return
End If
Dim time As New Stopwatch()
Dim nama As String
nama = TextBox2.Text.Substring(0, TextBox2.Text.LastIndexOf("."))
time.Start()
ArithmeticDecompress.Execute(New String() {lokasi.Text,
"E:\dekompresi\hasil.lzw_temp"})

Universitas Sumatera Utara

Dim decocde As List(Of Integer) =
LZWDecoder.decode(File.ReadAllBytes("E:\dekompresi\hasil.lzw_temp"))
Dim dekom As String = LZW.decompress(decocde)
time.Stop()
Dim output As New FileStream("E:\dekompresi\" & nama & "lzw_ac.bmp",
FileMode.Create)
For Each ch As Char In dekom
output.WriteByte(CByte(AscW(ch)))
Next
output.Close()
Dim gambar As Byte()
gambar = File.ReadAllBytes("E:\dekompresi\" & nama & "lzw_ac.bmp")
pb1.Image = New Bitmap(New MemoryStream(gambar))
cr.Text = time.ElapsedMilliseconds
mse.Text = HitungMSE(New Bitmap("E:\dekompresi\" & nama & "lzw_ac.bmp"), New
Bitmap(asli))
psnr.Text = HitungPSNR(Double.Parse(mse.Text))
pb1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
End Class

overviewform.vb
Imports System.IO
Public Class Form1
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = kompresform.namber
TextBox2.Text = kompresform.ukuranasli
TextBox3.Text = kompresform.ukurankompres
TextBox4.Text = kompresform.ukuranasli
TextBox5.Text = kompresform.waktukompres
TextBox6.Text = dekompresform.waktudekompres
TextBox7.Text = kompresform.crkompres
TextBox8.Text = kompresform.rskompres
TextBox9.Text = kompresform.sskompres
TextBox10.Text = dekompresform.msedekompres
TextBox11.Text = dekompresform.psnrdekompres
PictureBox1.Image = New Bitmap(New MemoryStream(dekompresform.gambarr))
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles
TextBox1.TextChanged
End Sub
End Class

bantuanform.vb
Public Class bantuanform
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles
RichTextBox1.TextChanged
End Sub
End Class

Universitas Sumatera Utara

tappform.vb
Public Class tappform
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As
LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
bantuanform.Show()
End Sub
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles
RichTextBox1.TextChanged
End Sub
End Class

LZW.vb
Imports System.Collections.Generic
Imports System.IO
Imports System.Text
'''
''' Description of LZW.
'''
Public NotInheritable Class LZW
Private Sub New()
End Sub
'* Compress a string to a list of output symbols.
Public Shared Function compress(uncompressed As [String], ByRef size As Integer) As
List(Of Integer)
' Build the dictionary.
Dim dictSize As Integer = 256
Dim dictionary As New Dictionary(Of String, Integer)()
For i As Integer = 0 To 255
dictionary.Add("" & ChrW(i), i)
Next
Dim w As [String] = ""
Dim result As New List(Of Integer)()
For Each c As Char In uncompressed.ToCharArray()
Dim wc As [String] = w & c
If dictionary.ContainsKey(wc) Then
w = wc
Else
result.Add(dictionary(w))
' Add wc to the dictionary.
dictionary.Add(wc, dictSize)
dictSize += 1
w = "" & c
End If
Next
' Output the code for w.
If Not w.Equals("") Then
result.Add(dictionary(w))
End If
size = dictionary.Count
Return result
End Function

Universitas Sumatera Utara

'* Decompress a list of output ks to a string.
Public Shared Function decompress(compressed As List(Of Integer)) As [String]
' Build the dictionary.
Dim dictSize As Integer = 256
Dim dictionary As New Dictionary(Of Integer, String)()
For i As Integer = 0 To 255
dictionary.Add(i, "" & ChrW(i))
Next
Dim w As [String] = "" & ChrW(CInt(compressed(0)))
compressed.RemoveAt(0)
Dim result As New StringBuilder(w)
For Each k As Integer In compressed
Dim entry As [String]
If dictionary.ContainsKey(k) Then
entry = dictionary(k)
ElseIf k = dictSize Then
entry = w & w(0)
Else
Throw New ArgumentException("Bad compressed k: " & k)
End If
result.Append(entry)
' Add w+entry[0] to the dictionary.
dictionary.Add(dictSize, w & entry(0))
dictSize += 1
w = entry
Next
Return result.ToString()
End Function
End Class

LZWEncoder.vb
Imports System.IO
Imports System.Collections.Generic
Imports System.Text
'''
''' Description of Tambahan.
'''
Public NotInheritable Class LZW_Encoder
Private Sub New()
End Sub
'public static int angka_LZW;
Public Shared LZW_compressed As List(Of Integer)
Private Shared _awal As Byte(), _akhir As Byte()
Public Shared Sub getInfo(awal As String, akhir As String)
_awal = File.ReadAllBytes(awal)
_akhir = File.ReadAllBytes(akhir)
End Sub
Public Shared Function UkuranHasil() As Single
Return CSng(_akhir.Length)
End Function
Public Shared Function HitungRc() As Single
Return CSng(_awal.Length) / CSng(_akhir.Length)
End Function
Public Shared Function HitungCr() As Single
Return CSng(_akhir.Length) / CSng(_awal.Length) * 100
End Function
Public Shared Function HitungSS() As Single

Universitas Sumatera Utara

Return 1 - (_akhir.Length / _awal.Length)
End Function

Public Shared Function tulis_file_lzw(size As Integer, output As String) As
MemoryStream
Dim isi As New List(Of Byte)()
Dim size_sqrt As Integer = CInt(Math.Truncate(Math.Log10(size) / Math.Log10(2))) +
1
isi.Add(CByte(size_sqrt))
Dim sb As New StringBuilder()
Dim bitbuffer As Byte = 0, bitcount As Byte = 0
Array.ForEach(LZW_compressed.ToArray(), Function(element)
sb.Append(Convert.ToString(element, 2).PadLeft(size_sqrt, "0"c)))
For Each ch As Char In sb.ToString()
bitbuffer 0
output.write(bit Xor 1)
numUnderflow -= 1
End While
End Sub

Protected Overrides Sub underflow()
If numUnderflow = Integer.MaxValue Then
Throw New ArithmeticException("Maximum underflow reached")
End If
numUnderflow += 1
End Sub
End Class

ArithmeticDecoder.vb
'''
''' Description of ArithmeticDecoder.
'''
Public Class ArithmeticDecoder
Inherits ACoderBase
'---- Fields ----

' The underlying bit input stream (not null).
Private input As BitInputStream
' The current raw code bits being buffered, which is always inner the range [low,
high].
Private code As Long

Universitas Sumatera Utara

'---- Constructor ----

'*
'
'
'
'
'
'

*
*
*
*
*

Constructs an arithmetic coding decoder based on the
specified bit input stream, and fills the code bits.
@param inner the bit input stream to read from
@if an I/O exception occurred
@throws NullPointerException if the input steam is {@code null}

Public Sub New(inner As BitInputStream)
If inner Is Nothing Then
Throw New ArgumentNullException()
End If
input = inner
code = 0
For i As Integer = 0 To STATE_SIZE - 1
'System.Diagnostics.Debug.WriteLine(code);
code = code MAX_TOTAL Then
Throw New ArgumentException("Cannot decode symbol because total is
too large")
End If
Dim range As Long = high - low + 1
Dim offset As Long = code - low
Dim value As Long = ((offset + 1) * total - 1) \ range
If value * range \ total > offset Then
Throw New ArgumentException()
End If
If value < 0 OrElse value >= total Then
Throw New ArgumentException()

Universitas Sumatera Utara

End If
' A kind of binary search. Find highest symbol such that
freqs.get_low(symbol) 1
Dim middle As Integer = CInt(CUInt(start + [end]) >> 1)
If freqs.getLow(middle) > value Then
[end] = middle
Else
start = middle
End If
End While
If start + 1 [end] Then
Throw New ArgumentException()
End If
Dim symbol As Integer = start
If offset < freqs.getLow(symbol) * range \ total OrElse
freqs.getHigh(symbol) * range \ total 32 Then
Throw New ArgumentException()
End If
For i As Integer = numBits - 1 To 0 Step -1
outer.write(CInt(CUInt(value) >> i) And 1)
Next
' Little endian

Universitas Sumatera Utara

End Sub
End Class

ArithmeticDecompress.vb
Imports System.IO
'''
''' Description of ArithmeticDecompress.
'''
Public NotInheritable Class ArithmeticDecompress
Private Sub New()
End Sub
Public Shared Sub Execute(args As String())
Dim inputFile As New FileStream(args(0), FileMode.Open)
Dim outputFile As New FileStream(args(1), FileMode.Create)
Dim inner As New BitInputStream(inputFile)
' Read input file once to compute symbol frequencies
Dim freqs As FrequencyTable = readFrequencies(inner)
'inputFile.Seek(0, SeekOrigin.Begin);
'freqs.increment(256); // EOF symbol gets a frequency of 1
' Read input file again, compress with arithmetic coding, and write output
file
'writeFrequencies(outer, freqs);
decompress(freqs, inner, outputFile)
inputFile.Close()
'outputFile.Close()
End Sub
Private Shared Function readFrequencies(inner As BitInputStream) As FrequencyTable
Dim freqs As Integer() = New Integer(256) {}
For i As Integer = 0 To 255
'System.Diagnostics.Debug.WriteLine(freqs[i].ToString("X"));
freqs(i) = readInt(inner, 32)
Next
freqs(256) = 1
' EOF symbol
Return New SimpleFrequencyTable(freqs)
End Function
Private Shared Function readInt(inner As BitInputStream, numBits As Integer) As
Integer
If numBits < 0 OrElse numBits > 32 Then
Throw New ArgumentException()
End If
Dim result As Integer = 0
For i As Integer = 0 To numBits - 1
result = (result