Results 1 to 3 of 3

Thread: Huffman code generator

  1. #1
    Administrator Shelwien's Avatar
    Join Date
    May 2008
    Location
    Kharkov, Ukraine
    Posts
    3,134
    Thanks
    179
    Thanked 921 Times in 469 Posts

    Huffman code generator

    I forgot where my previous huffman implementations are hidden, so
    had to make a new one to test something, hope somebody would find
    it useful.

    Code:
    #include <stdio.h>
    
    const int CNUM=256;
    
    int freq[2*CNUM], freq1[CNUM], p[2*CNUM][2], cl[CNUM];
    
    char code[CNUM];
    void DumpCode( int c, int i=0 ) {
      if( c<CNUM ) {
        code[i]=0; cl[c]=i;
        printf( "%02X <%s>\n", c, code );
      } else {
        code[i]='0'; DumpCode( p[c][0], i+1 );
        code[i]='1'; DumpCode( p[c][1], i+1 );
      }
    }
    
    int main( int argc, char** argv ) {
    
      int c,i,j,n,m,mi;
    
      FILE* f = fopen( "book1", "rb" ); if( f==0 ) return 1;
    
      // get symbol freqs
      for( i=0; i<CNUM; i++ ) freq1[i]=0;
      for(; (c=getc(f))!=-1; freq1[c]++ );
    
      for( i=0; i<CNUM; i++ ) freq[i]=freq1[i];
      for( n=CNUM,mi=0; mi>=0; n++ ) {
        freq[n] = 0;
        for( j=0; j<2; j++ ) {
          for( i=0,mi=-1; i<n; i++ ) if( (freq[i]>0) && ((mi<0)||(freq[i]<m)) ) m=freq[i],mi=i;
          if( mi>=0 ) freq[n]+=freq[mi], freq[mi]=0, p[n][j]=mi;
        }
      }
    
      DumpCode( p[n-1][0] );
    
      int sum = 0;
      for( i=0; i<n; i++ ) sum += freq1[i]*cl[i];
    
      printf( "codelength = %i bits = %i bytes\n", sum, (sum+7)>>3 );
    
      return 0;
    }

  2. The Following 2 Users Say Thank You to Shelwien For This Useful Post:

    encode (12th September 2016),RamiroCruzo (30th December 2016)

  3. #2
    The Founder encode's Avatar
    Join Date
    May 2006
    Location
    Moscow, Russia
    Posts
    3,954
    Thanks
    359
    Thanked 332 Times in 131 Posts

    Cool

    Cool! Thanks a lot!

  4. #3
    The Founder encode's Avatar
    Join Date
    May 2006
    Location
    Moscow, Russia
    Posts
    3,954
    Thanks
    359
    Thanked 332 Times in 131 Posts

    Cool

    Slightly changed & compiled:
    Attached Files Attached Files

Similar Threads

  1. Charles Blooms new huffman optimisation
    By willvarfar in forum Data Compression
    Replies: 2
    Last Post: 18th August 2010, 10:04
  2. Test data generator
    By Lasse Reinhold in forum Data Compression
    Replies: 1
    Last Post: 28th February 2010, 14:53
  3. huffman's Coding
    By swapy in forum Data Compression
    Replies: 5
    Last Post: 12th August 2009, 22:51
  4. Advanced Huffman Encoding
    By Simon Berger in forum Data Compression
    Replies: 28
    Last Post: 15th April 2009, 14:24
  5. Searching for special file generator
    By nimdamsk in forum Data Compression
    Replies: 5
    Last Post: 19th March 2009, 01:33

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •