Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
hi
code below is my huffman algorithm , it works correctly for text but for pictures
it dose not works.
the tree is built correct , i have checked that!
 
when decompress the pictures windows cant show that picture!
 
whats wrong ?
 
 
public class HopeHuffMan {
 
  
    public static void main(String[] args) throws IOException {
        
        
       FileInputStream input = new FileInputStream( "f:/testpic.bmp");
         
        
        String text=Change_input_to_string_Text(input);
        
             
   int[] counts = getCharacterFrequency(text); // Count frequency
     
   
    Tree tree = getHuffmanTree(counts); // Create a Huffman tree
      String[] codes = getCode(tree.root); // Get codes

                        
                          int k1=text.length();
                          Compress(text,codes);
                         Decompress (k1,tree);
                  
    }
    
       
 

    
     private static String Change_input_to_string_Text(FileInputStream input1) throws IOException{                
                   int ch;
                StringBuilder strContent = new StringBuilder("");
                                 
                 while((ch = input1.read()) != -1)
                  strContent.append((char) ch);
                 input1.close();
                String text = strContent.toString();
               
              
               
     return text;      
    }
    
 
    private static int[] getCharacterFrequency(String text) {
        int[] counts = new int[256]; // 256 ASCII characters
    
    for (int i = 0; i < text.length(); i++)
      counts[(int)text.charAt(i)]++; // Count the character in text
    
    return counts;
    }
    
    
    
      private static Tree getHuffmanTree(int[] counts) {
        // Create a heap to hold trees
    Heap<Tree> heap = new Heap<Tree>(); // Defined in Listing 24.10
    for (int i = 0; i < counts.length; i++) {
      if (counts[i] > 0)
        heap.add(new Tree(counts[i], (char)i)); // A leaf node tree
    }
    
    while (heap.getSize() > 1) { 
      Tree t1 = heap.remove(); // Remove the smallest weight tree
      Tree t2 = heap.remove(); // Remove the next smallest weight 
      heap.add(new Tree(t1, t2)); // Combine two trees
    }
 
    return heap.remove(); // The final tree
    }
    
        private static String[] getCode(Node root) {
         if (root == null) return null;    
    String[] codes = new String[2 * 128];
    assignCode(root, codes);
    return codes;
    }
 
    
   
    
    private static void assignCode(Node root, String[] codes) {
        if (root.left != null) {
      root.left.code = root.code + "0";
      assignCode(root.left, codes);
      
      root.right.code = root.code + "1";
      assignCode(root.right, codes);
    }
    else {
      codes[(int)root.element] = root.code;
    }
    }
  
 
       
    
    private static int Compress (String text,String[] codes){
                           String s="";
                        char cd = 0;
                     for(int i=0;i<text.length();++i){
                         cd=text.charAt(i);
                         s=s+codes[cd];
                     }
                     byte [] bytes;
                     bytes=s.getBytes();
                        for(int i=0;i<bytes.length;++i){
                         if(bytes[i]==48) bytes[i]=0;
                         if(bytes[i]==49) bytes[i]=1;
                     }
                      
                     BitOutputStream fop2 = new BitOutputStream("f:/is.oli");
                       for (int i = 0; i < bytes.length; i++) {
                       fop2.writeBit(bytes[i]);
            }
               fop2.close();
               return bytes.length;  
    }
           
    
    
 
    
    private static void Decompress (int b,Tree t) throws FileNotFoundException, IOException{
        
        BufferedWriter out = new BufferedWriter(new FileWriter("f:/unziped.txt"));
                   
                 BitInputStream fop2 = new BitInputStream("f:/is.oli");
                 System.out.print("\n");
               for (int i = 0;i<b ; i++) {
                        Node x = t.root;
                    while (!x.isLeaf()) {
                    int bit=fop2.readBit();
                  if (bit==1) x = x.right;
                  if (bit==0) x = x.left; 
                }
                       out.write(x.element);
                     
                      
                       }
                 System.out.print("\n");
                   out.close();      
                   fop2.close();
                                  
    }
       
    
    
}
Posted 29-Jan-12 7:52am
Edited 29-Jan-12 7:54am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi there.
 
I think the problem is when you pass in a bitmap to the "Change_input_to_string_Text" function you are expecting return text that is in the ASCII range of 256, however my guess is this may not be the case.
 
Have you tried printing out the returned text value from the function? Check to see if ASCII text is being returned and in the range from 0-256. If that's not the case you will need to modify your Huffman algorithm accordingly.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,588
1 OriginalGriff 528
2 Manas Bhardwaj 383
3 Damith Weerasinghe 330
4 Tadit Dash 315
0 Sergey Alexandrovich Kryukov 9,575
1 OriginalGriff 6,006
2 Peter Leow 4,500
3 Maciej Los 3,540
4 Abhinav S 3,513


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 31 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid