Tue Apr 17, 2007 10:47 am
Thu Sep 13, 2007 9:12 pm
Thu Sep 13, 2007 11:19 pm
Fri Sep 14, 2007 12:27 pm
/*
* * Please Visit us at www.codemiles.com *
* This Program was Developed by www.codemiles.com forums Team
* * Please Don't Remove This Comment *
*/
package arithmeticcoding;
/**
*
* @author mohamed
*/
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
/**
*
* @author mohamed
*/
public class Main extends JFrame {
/** Creates a new instance of Main */
public Main() {
setFont(new Font("Aril",Font.BOLD,14));
setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setLocation(100,100);
setTitle("Arithmetic-Compersion");
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.setBackground(Color.ORANGE);
JButton myButton=new JButton("Comperss");
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
readLoop();
}
});
cp.add(myButton);
JButton myButton2=new JButton("Decomperss");
myButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
readFromFile();
}
});
cp.add(myButton2);
JButton Browse=new JButton("Browse");
Browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int rVal =c.showOpenDialog(null);
if(rVal == JFileChooser.APPROVE_OPTION) {
path=c.getSelectedFile().getAbsolutePath();
location=c.getSelectedFile().getParent();
}
}
});
cp.add(Browse);
setVisible(true);
}
public void start() {
try {
ReadHand=new BufferedReader(new FileReader(path));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
public void readLoop() {
int code=0;
String ch;
start();
while(true) {
try {
code=ReadHand.read();
if(code==-1)break;
numOfCharacter++;
ch=fromCode_toString(code);
if(mydata_str==null) {
mydata_str=ch;
} else
mydata_str+=ch;
add_to_list(ch);
} catch (IOException ex) {
ex.printStackTrace();
}
}
try {
ReadHand.close();
} catch (IOException ex) {
ex.printStackTrace();
}
sort_my_data(mylist);
make_FirstList(numOfCharacter);
newlist=(myDataList)mylist.clone();
Arithmetic_solve();
write_Compressed();
// readFromFile();
}
public void Arithmetic_solve() {
data my_Data;
for(int i=0;i< mydata_str.length();i++)
{
solve_code(getIndex_Of_It(mydata_str.substring(i,i+1)),newlist,mylist);
}
}
public int getIndex_Of_It(String sub) {
data my_Data;
for(int i=0;i<newlist.size();i++) {
my_Data=((data)mylist.get(i));
if(my_Data.getChar().equals(sub))return i;
}
return -1;
}
public void solve_code(int i,myDataList nList,myDataList mList) {
data my_Data;
lastindex=i;
if(i!=0) {
start_prob= ((data)nList.get(i-1)).get_probability();
}
end_prob= ((data)nList.get(i)).get_probability();
for(int j=0;j<mList.size();j++) {
my_Data=((data)nList.get(j));
my_Data.setProbability(start_prob+((end_prob-start_prob)*((data)mList.get(j)).get_probability()));
}
display_list_content(nList);
old_end_prob=end_prob;
old_start_prob=start_prob;
}
public void write_Compressed() {
File myFile=new File(location+"/comp.txt");
FileOutputStream fout;
double Range=0;
try {
fout = new FileOutputStream(myFile);
DataOutputStream DataOut=new DataOutputStream(fout);
DataOut.writeInt(numOfCharacter);
if(lastindex!=0)
Range=(((data)newlist.get(lastindex)).get_probability()+((data)newlist.get(lastindex-1)).get_probability())/2.0d;
else Range=(((data)newlist.get(lastindex)).get_probability()+( start_prob))/2.0d;
DataOut.writeDouble(Range);
for(int i=0;i<mylist.size();i++) {
DataOut.writeChars(((data)mylist.get(i)).getChar());
DataOut.writeDouble(((data)mylist.get(i)).get_probability());
}
DataOut.flush();
DataOut.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void readFromFile() {
File myFile=new File(path);
FileInputStream fint;
int num = 0;
double Range=0;
try {
fint = new FileInputStream(myFile);
DataInputStream DataInt=new DataInputStream(fint);
num=DataInt.readInt();
Range=DataInt.readDouble();
String c=null;
double prob=0;
while(true) {
c=Character.toString(DataInt.readChar());
prob=DataInt.readDouble();
readlist.add(new data(c,prob));
}
} catch (IOException ex) {
// ex.printStackTrace();
}
System.out.println(Range+" +++ "+num);
display_list_content(readlist);
System.out.println("----------------------------------------------------");
display_list_content(mylist);
Decompress(Range,num,mylist);
}
public void Decompress(double Range,int num,myDataList mylist) {
String mydata=null;
int i=0;
myDataList myDataprob=(myDataList) mylist.clone();
start_prob=0.0;
old_start_prob=0.0;
old_end_prob=1.0;
end_prob=1.0;
while(num>0) {
i=getIndexOfArray(Range,myDataprob);
if(mydata==null) {
mydata=((data)myDataprob.get(i)).getChar();
} else {
mydata+=((data)myDataprob.get(i)).getChar();
}
solve_code(i,myDataprob,mylist);
num--;
}
System.out.println("Data = "+mydata);
try {
WriteHand=new BufferedWriter(new FileWriter(location+"/Decomp.txt"));
WriteHand.write(mydata);
WriteHand.flush();
WriteHand.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public int getIndexOfArray(double Range,LinkedList mylist) {
for(int i=0;i<mylist.size();i++) {
if(((data)mylist.get(i)).get_probability()>=Range)
return i;
}
return -1;
}
public void display_list_content(myDataList myLink_list) {
data my_Data;
System.out.println("**************************************************");
for(int i=0;i<myLink_list.size();i++) {
my_Data=((data)myLink_list.get(i));
System.out.println(my_Data.getChar()+" "+my_Data.get_probability());
}
}
public String fromCode_toString(int code) {
return Character.toString((char)code);
}
public void add_to_list(String ch) {
for(int i=0;i<mylist.size();i++) {
if(ch.equals(((data)mylist.get(i)).getChar())) {
((data)mylist.get(i)).increment();
return;
}
}
mylist.add(new data(ch));
}
public void make_FirstList(int numOfCharacter) {
double sum_prob=0;
for(int i=0;i<mylist.size();i++) {
sum_prob+=((data)mylist.get(i)).get_probability();
((data)mylist.get(i)).setProbability(sum_prob/numOfCharacter);
}
}
public void sort_my_data(myDataList myLinklist) {
Collections.sort(myLinklist,my_comparator);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Main();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d=(Graphics2D )g;
g2d.setBackground(Color.BLACK);
g2d.setColor(Color.RED);
g2d.drawString("Arithmetic-Compression and Decompression",50,400);
g2d.drawString("Developed by www.codemiles.com forums Team",50,420);
}
private myDataList mylist=new myDataList();
private myDataList readlist=new myDataList();
private myDataList newlist;
private data_Comparator my_comparator=new data_Comparator();
private JFileChooser c = new JFileChooser();
private BufferedReader ReadHand;
private BufferedWriter WriteHand;
private String mydata_str;
private String path;
private String location;
private double start_prob=0.0;
private double old_start_prob=0.0;
private double old_end_prob=1.0;
private double end_prob=1.0;
private int lastindex;
private int numOfCharacter=0;
}
class myDataList extends LinkedList {
public Object clone() {
myDataList cp=new myDataList();
data mydata;
for(int i=0;i<size();i++) {
mydata=(data)get(i);
cp.add(new data(mydata.getChar(),mydata.get_probability()));
}
return cp;
}
}
/*
* * Please Visit us at www.codemiles.com *
* This Program was Developed by www.codemiles.com forums Team
* * Please Don't Remove This Comment *
*/
/*
* * Please Visit us at www.codemiles.com *
* This Program was Developed by www.codemiles.com forums Team
* * Please Don't Remove This Comment *
*/
package arithmeticcoding;
import java.util.Comparator;
/**
*
* @author mohamed
*/
public class data_Comparator implements Comparator {
/** Creates a new instance of data_Comparator */
public data_Comparator() {
}
public int compare(Object o1, Object o2) {
double prob_diff=((data)o2).get_probability()-((data)o1).get_probability();
if(prob_diff<0)return -1;
if(prob_diff>0)return 1;
return 0;
}
}
/*
* * Please Visit us at www.codemiles.com *
* This Program was Developed by www.codemiles.com forums Team
* * Please Don't Remove This Comment *
*/
/*
* * Please Visit us at www.codemiles.com *
* This Program was Developed by www.codemiles.com forums Team
* * Please Don't Remove This Comment *
*/
package arithmeticcoding;
/**
*
* @author mohamed
*/
class data {
data(String ch) {
this.ch=ch;
probability=1;
}
data(String ch,double prob) {
this.ch=ch;
probability=prob;
}
void increment() {
probability++;
}
double get_probability() {
return probability;
}
String getChar() {
return ch;
}
void append_data(data d) {
ch+=d.getChar();
probability+=d.get_probability();
flag=true;
}
public void setProbability(double probability) {
this.probability = probability;
}
public void Add_to_Probability(double newP) {
this.probability+=newP;
}
protected Object clone() throws CloneNotSupportedException {
return new data(ch,probability);
}
private double probability;
private String ch;
private String binary_Code;
private boolean flag;
}
/*
* * Please Visit us at www.codemiles.com *
* This Program was Developed by www.codemiles.com forums Team
* * Please Don't Remove This Comment *
*/
Tue Sep 18, 2007 12:12 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.