Click here to Skip to main content
15,891,789 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to implement the huffman using c# for image Pin
Jochen Arndt31-Oct-16 3:46
professionalJochen Arndt31-Oct-16 3:46 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 6:13
Rıza Berkay Ayçelebi31-Oct-16 6:13 
GeneralRe: How to implement the huffman using c# for image Pin
Jochen Arndt31-Oct-16 6:19
professionalJochen Arndt31-Oct-16 6:19 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 7:39
Rıza Berkay Ayçelebi31-Oct-16 7:39 
GeneralRe: How to implement the huffman using c# for image Pin
Eddy Vluggen31-Oct-16 8:15
professionalEddy Vluggen31-Oct-16 8:15 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 9:00
Rıza Berkay Ayçelebi31-Oct-16 9:00 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 8:51
Rıza Berkay Ayçelebi31-Oct-16 8:51 
GeneralRe: How to implement the huffman using c# for image Pin
harold aptroot31-Oct-16 9:27
harold aptroot31-Oct-16 9:27 
There are problems.

I see that you store the frequency of characters in the header as bytes. That's wrong on both counts. You shouldn't store the frequency because there are multiple valid Huffman trees for almost every set of frequencies (you should therefore, and every format that uses Huffman codes does this, use Canonical Huffman codes[^] and store only the symbol lengths). Storing the frequency as a byte will obviously fail for larger files - images are likely to be large, but this wouldn't have worked for large text files either. Of course if you change to canonical codes and storing the symbol length, it will fit in a byte just fine.

Now moving on to things that aren't actually incorrect but are point for improvement, you don't need (and therefore shouldn't use) single-bit reads and writes. For encoding, just collect the pattern/length of every symbol in a little array and then use something like (not tested, just to get the idea)
C#
uint buffer = 0;
int bitsInBuffer = 0;
foreach (var symbol in imageOrWhatever) {
    buffer = (buffer << lengths[symbol]) | pattern[symbol];
    bitsInBuffer += lengths[symbol];
    while (bitsInBuffer >= 8) {
        output.WriteByte((byte)(buffer >> (bitsInBuffer - 8));
        bitsInBuffer -= 8;
    }
}
Or you can change it to not even do byte-IO either and do proper block IO. This code of course assumes that codes are 25 bits max (otherwise appending a code to the buffer can push something out that hasn't been saved), which is longer than you need, but you could use an ulong as buffer.

For decoding you can use a (big) array that has, for every index, what the first symbol (and its length) are if the index is interpreted as a string of Huffman encoded symbols. This array is easy to build and even easier to use, but depending on your maximum code length it can get pretty big. There are more sophisticated techniques that get the size of the array down, but it's a good start.

This sort of thing is why formats that use Huffman coding specify a maximum code length that isn't too long, for example DEFLATE allows lengths up to and including 15 bits.
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 10:21
Rıza Berkay Ayçelebi31-Oct-16 10:21 
GeneralRe: How to implement the huffman using c# for image Pin
harold aptroot31-Oct-16 10:26
harold aptroot31-Oct-16 10:26 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 23:09
Rıza Berkay Ayçelebi31-Oct-16 23:09 
GeneralRe: How to implement the huffman using c# for image Pin
Eddy Vluggen2-Nov-16 4:28
professionalEddy Vluggen2-Nov-16 4:28 
GeneralRe: How to implement the huffman using c# for image Pin
Pete O'Hanlon31-Oct-16 7:07
mvePete O'Hanlon31-Oct-16 7:07 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 7:36
Rıza Berkay Ayçelebi31-Oct-16 7:36 
GeneralRe: How to implement the huffman using c# for image Pin
Pete O'Hanlon31-Oct-16 9:59
mvePete O'Hanlon31-Oct-16 9:59 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 10:19
Rıza Berkay Ayçelebi31-Oct-16 10:19 
QuestionRe: How to implement the huffman using c# for image Pin
Eddy Vluggen31-Oct-16 3:49
professionalEddy Vluggen31-Oct-16 3:49 
AnswerRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 8:59
Rıza Berkay Ayçelebi31-Oct-16 8:59 
GeneralRe: How to implement the huffman using c# for image Pin
OriginalGriff31-Oct-16 9:03
mveOriginalGriff31-Oct-16 9:03 
GeneralRe: How to implement the huffman using c# for image Pin
Eddy Vluggen31-Oct-16 9:35
professionalEddy Vluggen31-Oct-16 9:35 
Question3d calculations Pin
Bernhard Hiller31-Oct-16 0:15
Bernhard Hiller31-Oct-16 0:15 
AnswerRe: 3d calculations Pin
Richard Deeming31-Oct-16 3:53
mveRichard Deeming31-Oct-16 3:53 
GeneralRe: 3d calculations Pin
Bernhard Hiller31-Oct-16 5:18
Bernhard Hiller31-Oct-16 5:18 
QuestionCan save directly from the DataTable into the Table of Access 2000 ? Pin
Member 245846730-Oct-16 15:56
Member 245846730-Oct-16 15:56 
AnswerRe: Can save directly from the DataTable into the Table of Access 2000 ? Pin
Dave Kreskowiak30-Oct-16 17:49
mveDave Kreskowiak30-Oct-16 17:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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