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
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim namafix As String
Dim size As Integer
Dim time As New Stopwatch() Dim sb As New StringBuilder()
Dim gambar As Byte() = File.ReadAllBytes(lokasi.Text) For 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 Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click Dim size As Integer
Dim time As New Stopwatch() Dim sb As New StringBuilder()
Dim gambar As Byte() = File.ReadAllBytes(lokasi.Text) For 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"})
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
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
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
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
nama = TextBox2.Text.Substring(0, TextBox2.Text.LastIndexOf(".")) time.Start()
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
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 ''' <summary>
''' Description of LZW. ''' </summary>
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
'* 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
''' <summary>
''' Description of Tambahan. ''' </summary>
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
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 <<= 1
bitbuffer = bitbuffer Or If(ch = "1"c, CByte(1), CByte(0)) bitcount += 1
If bitcount = 8 Then isi.Add(bitbuffer) bitbuffer = 0 bitcount = 0 End If
Next
If bitcount > 0 Then isi.Add(bitbuffer) End If
File.WriteAllBytes(output, isi.ToArray()) Return New MemoryStream(isi.ToArray()) End Function
End Class
LZWDecoder.vb
Imports System.Collections.Generic
''' <summary>
''' Description of LZWDecoder. ''' </summary>
Public NotInheritable Class LZWDecoder Private Sub New()
End Sub
Shared bytebuffer As Integer, bytenumber As Integer
Public Shared Function decode(file As Byte()) As List(Of Integer) Dim size As Integer = file(0)
Dim i As Integer
Dim hasil As New List(Of Integer)() bytebuffer = 0
bytenumber = 0
For i = 1 To file.Length - 2 bytebuffer <<= 8
bytebuffer = bytebuffer Or file(i) bytenumber += 8
While bytenumber >= size
Dim hasil1 As Integer = bytebuffer >> (bytenumber - size) hasil.Add(hasil1)
Dim geser As Integer = (1 << (bytenumber - size)) - 1 bytebuffer = bytebuffer And geser
bytenumber -= size End While
Next
Return hasil End Function End Class
AEncoder.vb:
Imports System.IO
''' <summary>
''' Description of AEncoder. ''' </summary>
Public Class AEncoder Inherits ACoderBase '---- Fields ----
' The underlying bit output stream (not null). Private output As BitOutputStream
' Number of saved underflow bits. This value can grow without bound, ' so a truly correct implementation would use a BigInteger.
Private numUnderflow As Integer
'---- Constructor ----
'*
' * Constructs an arithmetic coding encoder based on the specified bit output stream.
' * @param out the bit output stream to write to
' * @throws NullPointerException if the output stream is {@code null} '
Public Sub New(outer As BitOutputStream) If outer Is Nothing Then
Throw New ArgumentNullException() End If
output = outer numUnderflow = 0 End Sub
'---- Methods ----
'*
' * Encodes the specified symbol based on the specified frequency table. ' * This updates this arithmetic coder's state and may write out some bits. ' * @param freqs the frequency table to use
' * @param symbol the symbol to encode
' * @throws NullPointerException if the frequency table is {@code null} ' * @if an I/O exception occurred
'
Public Sub write(freqs As FrequencyTable, symbol As Integer) write(New CheckedFrequencyTable(freqs), symbol) End Sub
'*
' * @param symbol the symbol to encode
' * @throws NullPointerException if the frequency table is {@code null} ' * @if an I/O exception occurred
'
Public Sub write(freqs As CheckedFrequencyTable, symbol As Integer) update(freqs, symbol)
End Sub
'*
' * Terminates the arithmetic coding by flushing any buffered bits, so that the output can be decoded properly.
' * It is important that this method must be called at the end of the each encoding process.
' * <p>Note that this method merely writes data to the underlying output stream but does not close it.</p>
' * @if an I/O exception occurred '
Public Sub finish() output.write(1)
End Sub
Protected Overrides Sub shift()
Dim bit As Integer = CInt(CUInt(low) >> (STATE_SIZE - 1)) output.write(bit)
' Write out the saved underflow bits While numUnderflow> 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
''' <summary>
''' Description of ArithmeticDecoder. ''' </summary>
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].
'---- 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 << 1 Or CUInt(readCodeBit())
Next End Sub
'---- Methods ---- '*
' * Decodes the next symbol based on the specified frequency table and returns it.
' * Also updates this arithmetic coder's state and may read inner some bits. ' * @param freqs the frequency table to use
' * @return the next symbol
' * @throws NullPointerException if the frequency table is {@code null} ' * @if an I/O exception occurred
'
Public Function read(freqs As FrequencyTable) As Integer Return read(New CheckedFrequencyTable(freqs)) End Function
'*
' * Decodes the next symbol based on the specified frequency table and returns it.
' * Also updates this arithmetic coder's state and may read inner some bits. ' * @param freqs the frequency table to use
' * @return the next symbol
' * @throws NullPointerException if the frequency table is {@code null} ' * @if an I/O exception occurred
'
Public Function read(freqs As CheckedFrequencyTable) As Integer ' Translate from coding range scale to frequency table scale Dim total As Long = freqs.getTotal()
If total > 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
End If
' A kind of binary search. Find highest symbol such that freqs.get_low(symbol) <= value.
Dim start As Integer = 0
Dim [end] As Integer = freqs.getSymbolLimit() While [end] - start > 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 <= offset Then
Throw New ArgumentException() End If
update(freqs, symbol)
If code <low OrElse code >high Then
Throw New ArgumentException("Code outer of range") End If
Return symbol End Function
Protected Overrides Sub shift()
code = CLng(((code << 1) And MASK) Or CUInt(readCodeBit())) End Sub
Protected Overrides Sub underflow()
code = CLng(code) And TOP_MASK Or CUInt((code << 1) And CLng(CULng(MASK >> 1))) Or CUInt(readCodeBit())
End Sub
' Returns the next bit (0 or 1) from the input stream. The end ' of stream is treated as an infinite number of trailing zeros. Private Function readCodeBit() As Integer
Dim temp As Integer = input.read()
'System.Text.StringBuilder sb = new System.Text.StringBuilder(); 'sb.Append(temp);
'System.Diagnostics.Debug.Write(sb.ToString()); If temp = -1 Then
temp = 0 End If
Return temp End Function End Class
ArithmeticCompress.vb
Imports System.IO ''' <summary>
''' Description of ArithmeticCompresscs. ''' </summary>
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) ' Read input file once to compute symbol frequencies Dim freqs As FrequencyTable = getFrequencies(inputFile) freqs.increment(256)
' EOF symbol gets a frequency of 1
' Read input file again, compress with arithmetic coding, and write output file
Dim outer As New BitOutputStream(outputFile) writeFrequencies(outer, freqs)
compress(freqs, inputFile, outer) inputFile.Close()
End Sub
Private Shared Function getFrequencies(file As FileStream) As FrequencyTable Dim freqs As FrequencyTable = New SimpleFrequencyTable(New Integer(256) {}) While True
Dim b As Integer = file.ReadByte() If b = -1 Then
Exit While End If
freqs.increment(b) End While
Return freqs End Function
Private Shared Sub writeFrequencies(outer As BitOutputStream, freqs As FrequencyTable) For i As Integer = 0 To 255
writeInt(outer, 32, freqs.[Get](i)) Next
End Sub
Private Shared Sub compress(freqs As FrequencyTable, inner As FileStream, outer As BitOutputStream)
Dim enc As New AEncoder(outer) inner.Seek(0, SeekOrigin.Begin) While True
Dim symbol As Integer = inner.ReadByte() 'System.Diagnostics.Debug.WriteLine(symbol); If symbol = -1 Then
Exit While End If
enc.write(freqs, symbol) End While
enc.write(freqs, 256) ' EOF
enc.finish() outer.close()
' Flush remaining code bits End Sub
Private Shared Sub writeInt(outer As BitOutputStream, numBits As Integer, value As Integer)
If numBits < 0 OrElse numBits > 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
End Sub End Class
ArithmeticDecompress.vb
Imports System.IO
''' <summary>
''' Description of ArithmeticDecompress. ''' </summary>
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 << 1) Or inner.readNoEof() Next
' Big endian Return result End Function
Private Shared Sub decompress(freqs As FrequencyTable, inner As BitInputStream, outer As FileStream)
Dim dec As New ArithmeticDecoder(inner) While True
Dim symbol As Integer = dec.read(freqs) If symbol = 256 Then
outer.WriteByte(CByte(symbol)) End While
'outer.Flush(); outer.Close() End Sub
CheckFrequencyTable.vb
Imports System.IO
Imports System.Collections.Generic ''' <summary>
''' Description of CheckedFrequencyTable. ''' </summary>
Public Class CheckedFrequencyTable Implements FrequencyTable '---- Fields ----
' The underlying frequency table that holds the data (not null). Private freqTable As FrequencyTable
'---- Constructor ----
Public Sub New(freq As FrequencyTable) If freq Is Nothing Then
Throw New ArgumentNullException() End If
freqTable = freq End Sub
'---- Methods ----
Public Function getSymbolLimit() As Integer Implements FrequencyTable.getSymbolLimit
Dim result As Integer = freqTable.getSymbolLimit() If result <= 0 Then
Throw New ArgumentException("Non-positive symbol limit") End If
Return result End Function
Public Function [Get](symbol As Integer) As Integer Implements FrequencyTable.[Get] Dim result As Integer = freqTable.[Get](symbol)
If Not isSymbolInRange(symbol) Then
Throw New ArgumentException("IllegalArgumentException expected") End If
If result < 0 Then
Throw New ArgumentException("Negative symbol frequency") End If
Return result End Function
Public Function getTotal() As Integer Implements FrequencyTable.getTotal Dim result As Integer = freqTable.getTotal()
If result < 0 Then
Throw New ArgumentException("Negative total frequency") End If
Public Function getLow(symbol As Integer) As Integer Implements FrequencyTable.getLow
If isSymbolInRange(symbol) Then
Dim low As Integer = freqTable.getLow(symbol) Dim high As Integer = freqTable.getHigh(symbol) If Not (0 <= low AndAlso low<= high AndAlso high<= freqTable.getTotal()) Then
Throw New ArgumentException("Symbol low cumulative frequency out of range")
End If Return low
Else
freqTable.getLow(symbol)
Throw New ArgumentException("IllegalArgumentException expected") End If
End Function
Public Function getHigh(symbol As Integer) As Integer Implements FrequencyTable.getHigh
If isSymbolInRange(symbol) Then
Dim low As Integer = freqTable.getLow(symbol) Dim high As Integer = freqTable.getHigh(symbol) If Not (0 <= low AndAlso low<= high AndAlso high<= freqTable.getTotal()) Then
Throw New ArgumentException("Symbol high cumulative frequency out of range")
End If Return high
Else
freqTable.getHigh(symbol)
Throw New ArgumentException("IllegalArgumentException expected") End If
End Function
'public String toString() {
' return "CheckFrequencyTable (" + freqTable.toString() + ")";
' }
Public Sub [Set](symbol As Integer, freq As Integer) Implements FrequencyTable.[Set]
freqTable.[Set](symbol, freq)
If Not isSymbolInRange(symbol) OrElse freq < 0 Then
Throw New ArgumentException("IllegalArgumentException expected") End If
End Sub
Public Sub increment(symbol As Integer) Implements FrequencyTable.increment freqTable.increment(symbol)
If Not isSymbolInRange(symbol) Then
Throw New ArgumentException("IllegalArgumentException expected") End If
End Sub
Private Function isSymbolInRange(symbol As Integer) As Boolean Return 0 <= symbol AndAlso symbol < getSymbolLimit() End Function