• Tidak ada hasil yang ditemukan

Analisis Perbandingan Kompresi Citra Menggunakan Algoritma Deflate Dan Algoritma Arithmetic Coding

N/A
N/A
Protected

Academic year: 2017

Membagikan "Analisis Perbandingan Kompresi Citra Menggunakan Algoritma Deflate Dan Algoritma Arithmetic Coding"

Copied!
15
0
0

Teks penuh

(1)

Lampiran A. SOURCE CODE PROGRAM

Frame Utama

package FrameDesign;

import ArithmeticSkripsi.ArithmeticCompress; import ArithmeticSkripsi.ArithmeticDecompress; import Deflate.DeflateContoh;

import java.io.File;

import java.io.IOException; import java.util.Date;

import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JOptionPane;

public class MainFrame extends javax.swing.JFrame { public MainFrame() {

initComponents(); }

private void jButton4ActionPerformed(

java.awt.event.ActionEvent evt) { jFileChooser1 = new JFileChooser();

jFileChooser1.showOpenDialog(this);

if(jFileChooser1.getSelectedFile() != null){ jTextField1.setText(

jFileChooser1.getSelectedFile().getAbsolutePath()); jTextField1.repaint();

if((jComboBox1.getSelectedIndex()==0) && (jComboBox2.getSelectedIndex()==0)){

if(!jTextField1.getText().endsWith(".dft")){ jTextField2.setText(

jFileChooser1.getSelectedFile().getAbsolutePath()+".dft"); }

} else if((jComboBox1.getSelectedIndex()==1) && (jComboBox2.getSelectedIndex()==0)){

if(!jTextField1.getText().endsWith(".arm")){ jTextField2.setText(

(2)

}

String input = jTextField1.getText(); String output = jTextField2.getText(); if(jComboBox2.getSelectedIndex() == 0){ if(jComboBox1.getSelectedIndex() == 0) {

double rasio = ((((inputLength-outputLength) *10000)/inputLength)/100F);

long waktu = (selesai.getTime()-mulai.getTime());

jTextArea1.setText("Input File: "+inputLength+"\n"+"Output File:" +outputLength+"\n"+"Rasio: "+ rasio+"%\nwaktu kompresi:" +waktu+"ms");

jTextArea1.repaint();

JOptionPane.showMessageDialog(this, "Selesai"); }

catch (IOException ex) { ex.printStackTrace();

(3)

long waktu = (selesai.getTime()-mulai.getTime());

jTextArea1.setText("Input File: "+inputLength+"\n"+"Output File:" +outputLength+"\n"+"Rasio: "+ rasio+"%\nwaktu kompresi:" +waktu+"ms");

double rasio = ((((inputLength-outputLength) *10000)/inputLength)/100F);

long waktu=(selesai.getTime()-mulai.getTime());

jTextArea1.setText("Input File: "+inputLength+"\n"+"Output File:" +outputLength+"\n"+"Rasio: "+ rasio+"%\nwaktu kompresi:" +waktu+"ms"); File:" +outputLength+"\n"+"Rasio: "+ rasio+"%\nwaktu kompresi:" +waktu+"ms");

(4)

{

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

if(item.equals(jComboBox1.getItemAt(0)) && jComboBox2.getSelectedIndex()==0){

if("".equals(jTextField2.getText())){

} else if(item.equals(jComboBox1.getItemAt(1)) && jComboBox2.getSelectedIndex()==0){

if("".equals(jTextField2.getText())){ return;

} else if(!jTextField2.getText().endsWith(".arm")){

jTextField2.setText(jTextField2.getText().replaceAll( ".dft", ".arm"));

if(jComboBox1.getSelectedIndex()==0 && jComboBox2.getSelectedIndex()==0){

(5)

if("".equals(jTextField2.getText())){ return;

} else if(!jTextField2.getText().endsWith(".arm")){ String replaceAll =

jTextField2.getText().replaceAll(".dft", ".arm"); jTextField2.setText(replaceAll);

jTextField2.repaint(); }

}

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() { public void run() {

new MainFrame().setVisible(true); }

}); }

private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JComboBox jComboBox1; private javax.swing.JComboBox jComboBox2;

private javax.swing.JFileChooser jFileChooser1; private javax.swing.JFileChooser jFileChooser2; private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JPanel jPanel1;

private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2;

}

ARITHMETIC CODING

Kompresi Arithmetic Coding

package ArithmeticSkripsi;

import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File;

(6)

public class ArithmeticCompress {

public static void compress(String input, String output) throws IOException {

File inputFile = new File(input); File outputFile = new File(output);

FrequencyTable freq = getFrequencies(inputFile); freq.increment(256);

InputStream in = new BufferedInputStream( new FileInputStream(inputFile));

BitOutputStream out = new BitOutputStream(

new BufferedOutputStream(new FileOutputStream(outputFile))); try {

writeFrequencies(out, freq); compress(freq, in, out); } finally {

out.close(); in.close(); }

}

private static FrequencyTable getFrequencies(File file) throws IOException {

FrequencyTable freq = new SimpleFrequencyTable( new int[257]);

InputStream input = new BufferedInputStream( new FileInputStream(file));

try {

while (true) {

int b = input.read(); if (b == -1)

break;

freq.increment(b); }

} finally {

input.close(); }

return freq; }

static void writeFrequencies(

BitOutputStream out, FrequencyTable freq) throws IOException { for (int i = 0; i < 256; i++)

writeInt(out, 32, freq.get(i)); }

static void compress(

FrequencyTable freq, InputStream in, BitOutputStream out) throws IOException {

ArithmeticEncoder enc = new ArithmeticEncoder(out); while (true) {

int b = in.read(); if (b == -1)

break;

enc.write(freq, b); }

(7)

enc.finish();

private static void writeInt(BitOutputStream out, int numBits, int value) throws IOException {

if (numBits < 0 || numBits > 32)

throw new IllegalArgumentException();

for (int i = 0; i < numBits; i++) out.write(value >>> i & 1); }

public final class ArithmeticEncoder { public final long STATE_SIZE = 32;

public final long MASK = (1L << (STATE_SIZE - 0)) - 1; public final long TOP_MASK = (1L << (STATE_SIZE - 1)); public final long SECOND_MASK = (1L << (STATE_SIZE - 2)); public final long MAX_RANGE = (1L << (STATE_SIZE - 0)); public final long MIN_RANGE = (1L << (STATE_SIZE - 2)) + 2;

public final long MAX_TOTAL = Math.min(Long.MAX_VALUE / MAX_RANGE, MIN_RANGE);

public long low; public long high;

private BitOutputStream output; private int underflow;

public ArithmeticEncoder(BitOutputStream out) { low = 0;

high = MASK; if (out == null)

throw new NullPointerException(); output = out;

underflow = 0; }

public void write(FrequencyTable freq, int symbol) throws IOException {

write(new CheckedFrequencyTable(freq), symbol); }

public void write(CheckedFrequencyTable freq, int symbol) throws IOException {

update(freq, symbol); }

public void finish() throws IOException { output.write(1);

}

public void shift() throws IOException {

int bit = (int)(low >>> (STATE_SIZE - 1)); output.write(bit);

for (; underflow > 0; underflow--)

output.write(bit ^ 1); }

public void underflow() throws IOException { if (underflow == Integer.MAX_VALUE)

throw new RuntimeException("Maximum underflow reached");

underflow++; }

(8)

IOException {

if (low >= high || (low & MASK) != low || (high & MASK) != high) throw new AssertionError("Low or high out of range");

long range = high - low + 1;

if (range < MIN_RANGE || range > MAX_RANGE)

throw new AssertionError("Range out of range"); long total = freq.getTotal();

long symLow = freq.getLow(symbol); long symHigh = freq.getHigh(symbol); if (symLow == symHigh)

throw new IllegalArgumentException("Symbol has zero frequency");

if (total > MAX_TOTAL)

throw new IllegalArgumentException("Cannot code symbol because total is too large");

long newLow = low + symLow * range / total;

long newHigh = low + symHigh * range / total - 1; low = newLow;

high = newHigh;

while (((low ^ high) & TOP_MASK) == 0) { shift();

low = (low << 1) & MASK;

high = ((high << 1) & MASK) | 1; while ((low & ~high & SECOND_MASK) != 0) {

underflow();

low = (low << 1) & (MASK >>> 1);

high = ((high << 1) & (MASK >>> 1)) | TOP_MASK | 1; }

}

}

Dekompresi Arithmetic

package ArithmeticSkripsi;

import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File;

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream;

public class ArithmeticDecompress {

public static void decompress(String input, String output) throws IOException {

File inputFile = new File(input); File outputFile = new File(output);

BitInputStream in = new BitInputStream(new

BufferedInputStream(new FileInputStream(inputFile))); OutputStream out = new BufferedOutputStream(new

FileOutputStream(outputFile)); try {

(9)

} finally {

out.close(); in.close(); }

}

static FrequencyTable readFrequencies(BitInputStream in) throws IOException {

int[] freqs = new int[257]; for (int i = 0; i < 256; i++)

freqs[i] = readInt(in, 32); freqs[256] = 1; // EOF symbol

return new SimpleFrequencyTable(freqs); }

static void decompress(FrequencyTable freq, BitInputStream in, OutputStream out) throws IOException {

ArithmeticDecoder dec = new ArithmeticDecoder(in); while (true) {

int symbol = dec.read(freq); if (symbol == 256)

break;

out.write(symbol); }

}

private static int readInt(BitInputStream in, int numBits) throws IOException {

if (numBits < 0 || numBits > 32)

throw new IllegalArgumentException();

int result = 0;

for (int i = 0; i < numBits; i++) result |= in.readNoEof() << i; return result;

}

}

public final class ArithmeticDecoder { private BitInputStream input;

public final long STATE_SIZE = 32;

public final long MASK = (1L << (STATE_SIZE - 0)) - 1; public final long TOP_MASK = (1L << (STATE_SIZE - 1)); public final long SECOND_MASK = (1L << (STATE_SIZE - 2)); public final long MAX_RANGE = (1L << (STATE_SIZE - 0)); public final long MIN_RANGE = (1L << (STATE_SIZE - 2)) + 2;

public final long MAX_TOTAL = Math.min(Long.MAX_VALUE / MAX_RANGE, MIN_RANGE);

public long low; public long high; private long code;

public ArithmeticDecoder(BitInputStream in) throws IOException { low = 0;

(10)

if (in == null)

throw new NullPointerException(); input = in;

code = 0;

for (int i = 0; i < STATE_SIZE; i++) code = code << 1 | readCodeBit(); }

public int read(FrequencyTable freq) throws IOException { return read(new CheckedFrequencyTable(freq));

}

public int read(CheckedFrequencyTable freq) throws IOException {

long total = freq.getTotal(); if (total > MAX_TOTAL)

throw new IllegalArgumentException(

"Cannot decode symbol because total is too large"); long range = high - low + 1;

long offset = code - low;

long value = ((offset + 1) * total - 1) / range; if (value * range / total > offset)

throw new AssertionError();

if (value < 0 || value >= freq.getTotal()) throw new AssertionError();

int start = 0;

int end = freq.getSymbolLimit(); while (end - start > 1) {

int middle = (start + end) >>> 1; if (freq.getLow(middle) > value)

end = middle; else

start = middle; }

if (start == end)

throw new AssertionError();

int symbol = start;

if (freq.getLow(symbol) * range / total > offset || freq.getHigh(symbol) * range / total <= offset)

throw new AssertionError(); update(freq, symbol);

if (code < low || code > high)

throw new AssertionError("Code out of range"); return symbol;

}

public void shift() throws IOException {

code = ((code << 1) & MASK) | readCodeBit(); }

public void underflow() throws IOException {

code = (code & TOP_MASK) | ((code << 1) & (MASK >>> 1)) | readCodeBit();

}

(11)

int temp = input.read(); if (temp != -1)

return temp; else

return 0; }

public void update(CheckedFrequencyTable freq, int symbol) throws IOException {

if (low >= high || (low & MASK) != low || ( high & MASK) != high)

throw new AssertionError("Low or high out of range"); long range = high - low + 1;

if (range < MIN_RANGE || range > MAX_RANGE) throw new AssertionError("Range out of range");

long total = freq.getTotal(); long symLow = freq.getLow(symbol); long symHigh = freq.getHigh(symbol); if (symLow == symHigh)

throw new IllegalArgumentException("Symbol has zero frequency");

if (total > MAX_TOTAL)

throw new IllegalArgumentException(

"Cannot code symbol because total is too large"); long newLow = low + symLow * range / total; long newHigh = low + symHigh * range / total - 1; low = newLow;

high = newHigh;

while (((low ^ high) & TOP_MASK) == 0) { shift();

low = (low << 1) & MASK;

high = ((high << 1) & MASK) | 1; }

while ((low & ~high & SECOND_MASK) != 0) { underflow();

low = (low << 1) & (MASK >>> 1); high = ((high << 1) & (

MASK >>> 1)) | TOP_MASK | 1; }

}

}

File Reader/BitInputStream

package ArithmeticSkripsi;

import java.io.EOFException; import java.io.IOException; import java.io.InputStream;

(12)

private InputStream input;

or is -1 if the end of stream is reached. private int nextBits;

private int numBitsRemaining;

private boolean isEndOfStream;

stream.

public BitInputStream(InputStream in) { if (in == null)

throw new NullPointerException( "Argument is null");

input = in;

numBitsRemaining = 0; isEndOfStream = false; }

available, or -1 if the end of stream is reached. The end of stream always occurs on a byte boundary.

public int read() throws IOException { if (isEndOfStream)

return -1;

if (numBitsRemaining == 0) { nextBits = input.read(); if (nextBits == -1) {

isEndOfStream = true; return -1;

}

numBitsRemaining = 8; }

numBitsRemaining--;

return (nextBits >>> numBitsRemaining) & 1; }

available, or throws an EOFException if the end of stream is reached. public int readNoEof() throws IOException {

int result = read(); if (result != -1)

return result; else

throw new EOFException("End of stream reached"); }

public void close() throws IOException { input.close();

}

(13)

BitOutputstream

package ArithmeticSkripsi;

import java.io.IOException; import java.io.OutputStream;

public final class BitOutputStream {

private OutputStream output;

private int currentByte;

private int numBitsInCurrentByte;

public BitOutputStream(OutputStream out) { if (out == null)

throw new NullPointerException( "Argument is null");

output = out; currentByte = 0;

numBitsInCurrentByte = 0; }

public void write(int b) throws IOException { if (!(b == 0 || b == 1))

throw new IllegalArgumentException( "Argument must be 0 or 1");

currentByte = currentByte << 1 | b; numBitsInCurrentByte++;

if (numBitsInCurrentByte == 8) { output.write(currentByte);

numBitsInCurrentByte = 0; }

}

public void close() throws IOException { while (numBitsInCurrentByte != 0)

write(0); output.close(); }

(14)

DEFLATE

package Deflate;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;

import java.util.zip.DeflaterOutputStream; import java.util.zip.InflaterInputStream; import java.util.zip.InflaterOutputStream;

public class DeflateContoh {

public static void main(String[] args) throws Exception{ new DeflateContoh().contoh();

}

public void compress(

String input, String output) throws FileNotFoundException, IOException{

DeflaterOutputStream dos = new DeflaterOutputStream( new FileOutputStream(output));

File file = new File(input);

FileInputStream fis = new FileInputStream(file); for (int i = 0; i < file.length(); i++) {

dos.write(fis.read()); dos.flush();

}

fis.close(); dos.close(); }

public void decompress(

String input, String output) throws FileNotFoundException, IOException{

InflaterOutputStream ios = new InflaterOutputStream( new FileOutputStream(output));

File file = new File(input);

FileInputStream fis = new FileInputStream(file);

for (int i = 0; i < file.length(); i++) {

ios.write(fis.read()); ios.flush();

}

fis.close();

ios.close();

}

public void contoh()throws Exception{

DeflaterOutputStream dos = new DeflaterOutputStream( new FileOutputStream("output2.ac"));

(15)

FileInputStream fis = new FileInputStream(file); for (int i = 0; i < file.length(); i++) {

dos.write(fis.read()); dos.flush();

}

fis.close(); dos.close();

InflaterOutputStream ios = new InflaterOutputStream( new FileOutputStream("output3.ac"));

file = new File("output2.ac"); fis = new FileInputStream(file);

for (int i = 0; i < file.length(); i++) { ios.write(fis.read());

ios.flush();

}

fis.close(); ios.close(); }

Referensi

Dokumen terkait

Menurut PIC ESAP, seiring berjalannya waktu pada program ESAP, timbul berbagai permasalahan seperti peningkatan kemampuan dari para peserta berkemampuan lebih tinggi dan

JURUSAN TEKNIK INFORMATIKA, KOMPUTERISASI AKUNTANSI DAN MANAJEMEN INFORMATIKA JADWAL SERTIFIKASI SEMESTER GENAP TA... Basis Data Remedia

This data is similar with a study conducted in Saudi Arabia 11 which shows visual impairment is most common in students aged 7–8 years and were of 3 rd to 4 th grade.. One

Penelitian ini bertolok ukur pada asumsi bahwa di masyarakat sudah menganut sistem sosial yang terbuka, artinya terdapat aktivitas tingkat mobilitas individu yang

Republik Indonesia, Peraturan Presiden Nomor 44 Tahun 2016 tentang Bidang Usaha Yang Tertutup Dan Bidang Usaha Yang Terbuka Dengan Persyaratan Di Bidang Penanaman Modal, pasal 1

The majority of patients were motorcyclists (92%) with most of them were not wearing safety equipment. Most of the accidents took place in 2011 in Bandung. Mandible was the

a) variabel karakteristik pedagang canang di pasar Badung, yang meliputi daerah asal, umur, tingkat pendidikan, status, jumlah anggota keluarga di rumah, jumlah saudara lain

Results: There were 3 main responses regarding the respondent’s opinions. First, respondents showed several attitudes toward drugs in general and also several attitudes