Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Arithmatic Coding Com-Decompression algorithm

Tue Apr 17, 2007 10:47 am

This is java J2SE program for Arithmatic Coding com-decom algorithm that let the user to browse for the file that need to compress or decompress and then make the required operation by arithmatic coding.

1. Run the program .
2. If you have a text and you want to compress click on Compress button and then select text file.
3. The compressed file (com.txt) will be created.
3. if you want to decompress the compressed file click on Decompress button and select com.txt file.
4. The decompressed file (decom.txt) will be created.

Source code :
finished-projects/arithmetic-data-comparison-and-decompression-java-code-t10765.html



Re: Arithmatic Coding Com-Decompression algorithm

Thu Sep 13, 2007 9:12 pm

hi,
pls could u upload the source code for this algorithm?

would really appreciate it.
thnx

Re: Arithmatic Coding Com-Decompression algorithm

Thu Sep 13, 2007 11:19 pm

hi again,
tried to gett the stuff from the site u recommended...bt for some reason i couldnt find it.

pls could u kindly upload it?
thnx

Re: Arithmatic Coding Com-Decompression algorithm

Fri Sep 14, 2007 12:27 pm

Join the yahoo group first
Code:
/*
*   *       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       *
*/

Code:
/*
*   *       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       *
*/

Code:
/*
*   *       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       *
*/

:read:

Re: Arithmatic Coding Com-Decompression algorithm

Tue Sep 18, 2007 12:12 pm

thnx a lot!

Post a reply
  Related Posts  to : Arithmatic Coding Com-Decompression algorithm
 LZW data compression-decompression algorithm java code     -  
 Huffman Compression-Decompression     -  
 Huffman Data compression-decompression code C#-csharp     -  
 Arithmetic data comparison and decompression java code     -  
 servlet coding     -  
 I am new in PHP, want coding or PHP file     -  
 Java Coding     -  
 Coding serialization concepts in C++     -  
 Standard Huffman Coding     -  
 Huffman coding for image compression     -