implementing huffman algorithm on image file
I have developed a program with C language, which is based on huffman algorithm.
Now I want to work with the same algorithm with an image file (previous program can compress a .txt
file)...
How can I input an image file with c program and use the algorithm to compress it..
void freq()
{
int i=0,j,k,f,n=0;
char ch;
clrscr();
printf("\nEnter path of the text file:");
fflush(stdin);
gets(path);
f1=fopen(path,"rb");
if(f1==NULL)
{
printf("cant open %s",path);
getch();
exit(1);
}
while(1)
{
f=0;
ch=fgetc(f1);
if(ch==EOF)
break;
for(j=0;j<n;j++)
{
if(ch==arr[j])
f=1;
}
if(f==1)
continue;
else
{
arr[i]=ch;
ptr[i][0]=arr[i];
开发者_Go百科ptr[i][1]='\0';
i++;
}
n++;
}
arr[i]='\0';
noc=i-1;
}
As Cosmin Prund already stated in his comment: "Rename your image file to a .txt". Now, of course you don't have to do that, what he meant is: the Huffman encoding algorithm is agnostic to what kind of input it gets, it doesn't care.
So instead of reading in a .txt file, just read the image file and hand it to the Huffman algorithm. For the algorithm it's all just bits, doesn't matter it it's English or Klingon or a BMP or ...
Of course, using Huffman encoding on an image file to compress it, isn't probably the best idea. There's a reason there's file formats such as JPG around. They make use of the fact that the human eye is insensitive to certain lossy transformations on the image and use this to remove that information so they have to encode less data. The Huffman encoding can't do that, since it's generic and lossless.
[Edit] I haven't gone through the code you posted, but I did notice an error on first glance: fgetc
returns an int
, not a char
as its name might lead you to believe. So better change the definition of ch
.
精彩评论