Click here to Skip to main content
6,931,354 members and growing! (19,506 online)
Email Password   helpLost your password?
 
General Programming » Algorithms & Recipes » Algorithms     Intermediate License: The Code Project Open License (CPOL)

Huffman Coding Class

By handychang

A file compressor class based on the Huffman Tree
VC6, Windows, Dev
Posted:2 Jun 2004
Updated:6 Jun 2004
Views:128,817
Bookmarked:48 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
22 votes for this article.
Popularity: 5.15 Rating: 3.84 out of 5
4 votes, 18.2%
1

2
2 votes, 9.1%
3
7 votes, 31.8%
4
9 votes, 40.9%
5

Contents

Introduction

This version of file encoder and decoder program is based on the Huffman coding method. It explicitly demonstrates the details of the files during the encoding and decoding. The algorithm is encapsulated in a class En_Decode by the standard C++ language. Here is a demonstration project for the class.

Using the code

This class is implemented by standard C++, so it is very easy to use.

  1. Create an MFC application program as usual.
  2. Add the source file En_Decode.cpp and En_Decode.h to the project.
  3. Add the header file to your project by:
    //
    
    //    #include "En_Decode.h"
    
    //
    
    

    Define your own coder object, such as:

    //
    
    //    En_Decode Coder;
    
    //
    
    
  4. Use the member function of the class to set the source file name and destination encoded file name, then call Encode() to encode the source file.
    //
    
    //    Coder.SetInputFileName("C:/test.txt", "txt");
    
    //    Coder.SetOutputFileName("C:/encoded");
    
    //    Coder.Encode();
    
    //
    
    

In the same way, we can decode the encoded files to exactly the source file.

Class Members

Construction

En_Decode Constructs a En_Decode object.

File Name Functions

SetInputFileName Set the input file name.
SetOutputFileName Set the output file name.
GetExtName Get the ext name of the encoded file when decoding.

Paraneter Functions

GetMinFreq Get the minimum frequency of the symbols in the source file.
GetMaxFreq Get the maximum frequency of the symbols in the source file.
GetFreq(idx) Get the frequency value of a symbol in the source file.
GetActiveSymbols Get the number of active symbols in the source file.
GetActualDataLength Get the bytes number of the encoded file not including head.
GetUncodedFileLength Get the length of the source file.
GetEncodedFileCodeLength Get the average code length of the encoded file.
GetSourceFileEntropy Get the entropy of the source file.

Thanks to...

Derek Lakin for his help with CTabCtrlSSL in the demo project.

Contacting the Authors

This is the first time I share my code here. It is done in a hurry, so exception handling is ignored. Any one who is interested in it can enhance it freely, and please let me know about any improvement, thanks!

I am a junior student in Huangzhong University of Science and Technology, China. I am interested in C++ and MFC, and any coding project. I am glad to share my codes here with any one, though it is not that good.

Please contact me via email (zxt0123@163.com).

License

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

About the Author

handychang


Member
I am a junior student in Huangzhong University of Science and Technology, China. I am interested in C++ and MFC, and any coding project.
I am glad to share my codes here with any one, though it is not that good. Please contact me via email (zxt0123@163.com).
Occupation: Web Developer
Location: China China

Other popular Algorithms & Recipes articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh)FirstPrevNext
Questionfind some error Pinmemberbansal294510:09 4 Dec '08  
GeneralMFC coding Pinmembercuty_bujji2:53 22 Jul '08  
QuestionIs there any other compressing algorithm more efficient than huffman coding Pinmemberwavebai23:05 9 May '07  
GeneralHow to compress many files in folder? PinmemberNewcc19:12 11 Oct '06  
GeneralTab.h Pinmemberbeardy janggut15:45 14 Mar '06  
GeneralRe: Tab.h Pinmembercristitomi3:17 10 Mar '07  
GeneralStreaming Compression? Pinmemberandrew23586:06 14 Jun '04  
GeneralRe: Streaming Compression? PinsussAlan Balkany7:14 14 Jun '04  
It may not be possible to use the pure Huffman algorithm to do streaming compression, because Huffman needs to make two passes through the data.

The first pass is used to determine the frequency of each byte and build the tree. The second pass does the actual coding.

If you knew the approximate frequency distribution of your input, you could build the Huffman tree once, then reuse the same tree for later input streams. i.e. you would skip the first pass. This would give you streaming compression, but if the composition of your input streams changed, you would no longer get optimal compression.
GeneralRe: Streaming Compression? Pinmemberhandychang19:19 15 Jun '04  
GeneralRe: Streaming Compression? PinmemberJohn M. Drescher6:39 16 Jun '04  
GeneralRe: Streaming Compression? PinsussAnonymous6:06 23 Jun '05  
GeneralRe: Streaming Compression? PinmemberJohn M. Drescher6:34 23 Jun '05  
GeneralRe: Streaming Compression? PinmemberJan Rodriguez12:44 19 Oct '06  
GeneralSuggestion ! PinmemberNewGhost21:46 11 Jun '04  
GeneralRe: Suggestion ! Pinmemberhandychang21:46 12 Jun '04  
GeneralRe: Suggestion ! Pinmemberleandrobecker10:51 27 Aug '04  
GeneralRe: Suggestion ! Pinmemberjazzwithshaz19:30 13 Jan '05  
GeneralHow does Huffman coding work? PinmemberAORD17:40 7 Jun '04  
GeneralRe: How does Huffman coding work? Pinmemberhandychang21:55 7 Jun '04  
GeneralRe: How does Huffman coding work? Pinsussamirhosein22:15 15 Dec '07  
GeneralPicky!!! PinmemberPaul Charles6:49 3 Jun '04  
GeneralRe: Picky!!! PinmemberJubjub14:30 3 Jun '04  
GeneralRe: Picky!!! PinsussAnonymous19:22 3 Jul '05  
GeneralRe: Picky!!! PinmemberJubjub19:45 3 Jul '05  
GeneralRe: Picky!!! Pinmemberhandychang16:20 3 Jun '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 6 Jun 2004
Editor: Nishant Sivakumar
Copyright 2004 by handychang
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project