• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1092
  • Last Modified:

using Huffman/Arithmetic coding

I am new to Java. I need to test compressing an image file using Huffman coding or Arithmetic coding.

Is it possible for me to write a direct coding to achive this. (using Class Huffman, encode, or any other method for example) If so can you show me how to encode a file "test.gif"?

Thank you.
Savi
0
Savithra
Asked:
Savithra
1 Solution
 
ozymandiasCommented:
If you visit this site : http://www.coyotegulch.com/algorithm/jisp/

and download the JISP2 Source Code you will find that it has a number of classes :

Huffman.java
HuffmanEncoded.java
HuffmanException.java
HuffmanFilter.java
HuffmanHeader.java

The main one is Huffman.java and it does pretty much exactly what you describe.
0
 
ozymandiasCommented:
Huffman has a static method encode, so you can take an Object and do :

        Huffman.encode(myObject);

This returns an instance of HuffmanEncoded (i.e.an object that has been Huffman encoded.

You can then decode HuffmanEncoded objects by doing :

       Huffman.decode(myHuffmanEncoded);

which returns a decoded Object.
0
 
CEHJCommented:
You're not likely to get a very good ratio on this type of file.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SavithraAuthor Commented:
Hi Ramesses of Egypt,

I am very new to Java, can you spell me the exactly how to write that code. It has to be a method(not the main.) I have processed an image and the result is "Image3" of
image3 = createImage(new MemoryImageSource(w, h, pixels3, 0, w));

This "Image3" is what I need to be Haffman coded.

Your help is very much appriciated.
0
 
ozymandiasCommented:


image3 = createImage(new MemoryImageSource(w, h, pixels3, 0, w));
HuffmanEncoded encodedImage = Huffman.encode(image3);


That's all you need.
image3 has now been converted to the HuffmanEncoded object encodedImage.

To decode it you would write :

Image unenecodedImage = (Image)Huffman.decode(encodedImage);

0
 
SavithraAuthor Commented:
Oh King of Kings,

The site you have mentioned to get the classes is not there now. All searches lead me agin to this same site you have mentioned. Is it possible there fore to send them to me, if you have them,please.

Thank you.

Savithra
0
 
ozymandiasCommented:
The guys has resleased a new version of JISP and has just moved the page. It's here now :

http://www.coyotegulch.com/jisp/index.html
0
 
SavithraAuthor Commented:
I placed the packages as:

import com.coyotegulch.jisp.*;
This is the error at compiling. Could you test it please.


C:\Java>javac Test.java

Test.java:257: cannot resolve symbol
symbol  : method encode  (java.awt.Image)
location: class Huffman
        imagex = Huffman.encode(image3);
                        ^

0
 
ozymandiasCommented:
Please post full code.

What is imagex ?

The method encode returns an instance of HuffmanEncoded, not an image.
0
 
SavithraAuthor Commented:
Ramesses II,

Here is the program code abbriged (I have taken off all the pixel playing things for your convenience.)

By the way, I read your poem in 1990 and loved it eversince. Surpisingly I was reading it again again just last month.

This is working fine if you comment the line for HuffmanEncoded...

//import com.coyotegulch.jisp.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
//import java.lang.object.*;

// output to display
public class Test extends Panel{
     Image image, image3;
     PixelGrabber grabber;
     int h,w;
     Dimension size;
     int[] pixels;  
     int[] pixels3;
     
 //to get the image          
 public Test()
      {
      image = Toolkit.getDefaultToolkit().getImage("abcd.gif");
      MediaTracker tracker = new MediaTracker(this);
      tracker.addImage(image, 0);
     try {
          tracker.waitForID(0);
      } catch (InterruptedException e )
           {
           System.out.println("Interrupted !!!");
      }  
// Image loaded  
    h = image.getHeight(null);
     w = image.getWidth(null);
     System.out.println("height = " + h + ", width = " + w);
     
     size = new Dimension((w+10) * 3,h + 10);
     
// adjust array for pixel info
    pixels = new int[w * h];
     grabber = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);

// grab the pixels
    try {
          boolean res = grabber.grabPixels();
          System.out.println(" Grabbed image !!!");
     } catch (InterruptedException e )
          {
          System.out.println("Interrupted !!!");
     }

// create image from pixels3 array
     image3 = createImage(new MemoryImageSource(w, h, pixels, 0, w));
     HuffmanEncoded imagex = Huffman.encode(image3);
}  

// get image dimensions
public Dimension getPreferredSize()
     {
     return size;
     }  
         
public void paint(Graphics g)
     {
     // paint the the picture, the original and the filtered
    g.drawImage(image3, 5, 5 , null, null);
     }
     
public static void main (String[] args){
     Frame f = new Frame ();
     f.addWindowListener(new WindowAdapter()
          {          
          public void windowClosing(WindowEvent e)
               {
               System.exit(0);
          }
     });
     
     Test t = new Test();
     f.add(t);
    f.pack();
     f.show();
}

}

0
 
ozymandiasCommented:
OK. I have looked at this some more.

You cannot use this JISP directly for Images because it will only encode objects that implement Serializable or Externalizable.

Basically the Huffeman compression will compress a byte array. The method for converting an object to a byte array is to write it into an ObjectOutputStream and then grab the bytes out of the stream.

However, and object must be Serializable or Externalizable to do this....hence the problem.

However, the Image starts off as a file, so you could read the image's bytes directly from the file and pass those to the Huffman for encoding.

Basically, the implementatiojn of the actuall Huffman compression is hidden inside the Huffman class in private methods called compress() and decompress().

If you download the source from the JISP site you can see exactly how this is done and how the HuffmanEncoded object is used to store the compressed bytes along woth the encoding information store in an associated HuffmanHeader.

Short of rewriting the whole Huffman class there's not much I can do. The method to Huffman encode is there.
0
 
SavithraAuthor Commented:
If I save it first and then call it again will I be able to use this directly?

Savith-RA
0
 
ozymandiasCommented:
You don't need to save it first. You have alkready grabbed the pixels from the image so you could just HuffmanEncode those :

    try{
         HuffmanEncoded imagex = Huffman.encode(pixels);
     }catch(IOException ioe){
          ioe.printStackTrace();
     }

or you could create a class to hold them called SerializableImage...something like :

class SerializableImage implements java.io.Serializable{

     int[] pixels;

     public SerializableImage(int[] pixels){

           this.pixels = pixels;

     }

     public int[] getPixels(){

          return pixels;

     }
}

and then Huffman encode that :

    SerializableImage si = new SerializableImage(pixels);
    try{
         HuffmanEncoded imagex = Huffman.encode(si);
     }catch(IOException ioe){
          ioe.printStackTrace();
     }
0
 
SavithraAuthor Commented:
Oh Mighty ruler of Egypt,

I tryed with Huffman.encode as:

public static void HuffmanEncoded(){  
     try{
        HuffmanEncoded imagex = Huffman.encode(si);
     }catch(java.io.IOException ioe){
        ioe.printStackTrace();
    }
}  

But the problem is that it calls si or pixels as non-static variables. This wont compile. The following is the error
Test.java:75: non-static variable si cannot be referenced from a static context
        HuffmanEncoded imagex = Huffman.encode(si);
                                               ^

Savit-RA
0
 
SavithraAuthor Commented:
Foget about Huffman coding. Can you help me in saving the image3 as image3.gif in the hard disk.

This would be a great help.

Thank you.

Savithra
0
 
SavithraAuthor Commented:
Foget about Huffman coding. Can you help me in saving the image3 as image3.gif in the hard disk.

This would be a great help.

Thank you.

Savithra
0
 
CleanupPingCommented:
Savithra:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
jimmackCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept ozymandias' comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now