?
Solved

using Huffman/Arithmetic coding

Posted on 2003-03-28
19
Medium Priority
?
1,076 Views
Last Modified: 2011-09-20
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
Comment
Question by:Savithra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
19 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 8228736
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 8228739
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8230179
You're not likely to get a very good ratio on this type of file.
0
Industry Leaders: 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!

 

Author Comment

by:Savithra
ID: 8241141
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 8244273


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
 

Author Comment

by:Savithra
ID: 8246700
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 8246738
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
 

Author Comment

by:Savithra
ID: 8248141
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 8248875
Please post full code.

What is imagex ?

The method encode returns an instance of HuffmanEncoded, not an image.
0
 

Author Comment

by:Savithra
ID: 8249014
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 8249495
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
 

Author Comment

by:Savithra
ID: 8257319
If I save it first and then call it again will I be able to use this directly?

Savith-RA
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 750 total points
ID: 8261822
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
 

Author Comment

by:Savithra
ID: 8300568
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
 

Author Comment

by:Savithra
ID: 8313125
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
 

Author Comment

by:Savithra
ID: 8313219
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
 

Expert Comment

by:CleanupPing
ID: 9058416
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
 
LVL 15

Expert Comment

by:jimmack
ID: 9735396
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month9 days, 8 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question