|
A quick search brought up Huffman coding in C#[^]. This might be a good place for you to start.
This space for rent
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace HuffmanTest
{
class Program
{
static void Main(string[] args)
{
string input = "foo bar";
HuffmanTree huffmanTree = new HuffmanTree();
// Build the Huffman tree
huffmanTree.Build(input);
// Encode
BitArray encoded = huffmanTree.Encode(input);
Console.Write("Encoded: ");
foreach (bool bit in encoded)
{
Console.Write((bit ? 1 : 0) + "");
}
Console.WriteLine();
// Decode
string decoded = huffmanTree.Decode(encoded);
Console.WriteLine("Decoded: " + decoded);
Console.ReadLine();
}
}
}
Here input is "foo bar"; which is string.Input will be image.
|
|
|
|
|
So just read the image into your application, as you have been told many times already. Text, image, audio, video - they are all the same, just streams of bytes.
|
|
|
|
|
I used base64 but image file 10 mb and then base64 string 16 mb and compress by huffman this base64 string result is 12 mb .
|
|
|
|
|
That is not unusual, since image files tend to be difficult to compress. And .jpg files are already compressed.
|
|
|
|
|
If you want to test your algorithm out on an image, I would suggest that you try it against a RAW image file. RAW does what it says - it provides the raw data so you can test your compression against uncompressed data.
This space for rent
|
|
|
|
|
Just pass the bytes representing the image to the encoder. If your input parameter is actually a string, replace it by passing a byte array and the length and adapt the code as necessary (replace char by byte and use the passed length instead of the string length). Just think of a string as an array of charaters (one or two bytes per character) and a length. Then you will recognise that there is no big difference (no difference at all when the string class uses one byte per character).
|
|
|
|
|
I tried.But ı was not succesful May you help me ?.I am not good in c#.I am new.I want to do this ı want to understand.Can ı send you my project ?
|
|
|
|
|
Post the relevant (short) code snippets here (you can edit your initial question and add it there) and explain what you have tried and where you got stuck. So you will get the best help because many people can read it.
|
|
|
|
|
I am not good c# actually.I reliazed huffman with c# but whihc compress only text. not image.how can ı do that.İf you want ı can sedn my project MAybe you can help me
|
|
|
|
|
I'm sorry but I did not provide private help here (and others won't do it too). This is a public forum where questions are answered public.
So follow my advices from my previous post and provide more details.
|
|
|
|
|
[Huffman Compressor]
My program is blow the link.This program only comrpess text.What do ı add this program and then which can compress an image for example jpeg.May you investigate my program
|
|
|
|
|
Rıza Berkay Ayçelebi wrote: This program only comrpess text. It works the same for any set of bytes, so it will not "just work" on text only. Text, pictures, sounds, applications; all just a series of bytes.
You still have not answered my question
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
|
I want to compress picture.I compressed text wiht huffman but how can ı do for pictures ?
|
|
|
|
|
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)
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.
|
|
|
|
|
how ı can change sorry ı am not very good in c#
|
|
|
|
|
Your C# skill isn't really the problem here. The problems are more fundamental.
|
|
|
|
|
I am not good in c# I said aldready.But I can create program with c#.I want someone shows how to do it over my codes.I aldready sent my project.My project only take text.
|
|
|
|
|
..works only for text, because you are feeding it a string. You can feed it any bytes, but most images are already compressed - so yes, the result would often be larger than what you started with.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Did you even look at the link I put above?
This space for rent
|
|
|
|
|
I saw the link.I dont have a problems how to code huffman .I aldready coded but ı didnt comrpess image.Can this huffman code compress an image ?
|
|
|
|
|
You obviously didn't read the link. It's encoding images.
This space for rent
|
|
|
|
|
I aldready created huffman code just ı want to learn why my codes compress only text.Can ı use your codes?
|
|
|
|
|
What do you mean with "cannot compress image"? What kind of image? How large, how many colors?
Something like a jpg would already be compressed, so I would not be surprised if it does not actually compress much there.
If you have a bitmap with large repeating spaces, then huff may puff a few bits of it.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|