⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 com_dec.c

📁 huffman编码压缩
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <taskLib.h>
#define N 71
#define M 2*N-1

typedef struct struct_code1
{ char bits[N];
  int start;
  char ch;
  struct struct_code1 *next;
} hufm_table;

hufm_table *get_node_from_huffcode(char ch, hufm_table *head)
{
    int i;
    hufm_table *the_node;
    the_node = head;
    do{
	if(the_node->ch == ch)
	    return the_node;
	the_node = the_node->next;
    }while(the_node != head);
    return NULL;
}
	
/**/
void code_file(char *source_filename,char *target_filename, hufm_table *head)   
{
   FILE *fp1,*fp2;
   char ch, code, mask;
   char b[N+8];
   int i,j,k,blength=0;
   hufm_table *current;
printf("enter the coce file\n");
   if((fp1=fopen(source_filename,"rb"))!=NULL&&
		   (fp2=fopen(target_filename, "w"))!=NULL)
   {
       while(!feof(fp1))
       {
	    ch=fgetc(fp1);
	    current = get_node_from_huffcode(ch, head);
	    if(current!=NULL)
	    {
taskDelay(10);
printf("if((current = get_node_from_huffcode(ch, head))!=NULL)\n");
		i=current->start;
taskDelay(10);
printf("tha char is %c\nCurrent->start is %d\n", current->ch, i);
		while(i < N){
taskDelay(10);
printf("while(i != N)\n");
		    b[blength]=current->bits[i];
taskDelay(10);
printf(" b[blength]=current->bits[i];\n");
		    i++;
		    blength++;
		}
		b[blength]='\0';
		/*
		 * so, size of code is k * char (8-bit) + blength
		 * all the char is write into file
		 * the leavings is put low bit of b[]
		 */
		k=blength/8;	/*k is time of a 8-bit charactor*/
printf("k=blength/8 is %s\n", k);
		for(i=0;i<k;i++)  /*how many whole char*/
		{
		    for(j=0;j<8;j++) /*for a whole char*/ 
		    {
			code |= b[i*8+j] << (7-i);
		    }
		    fputc(code,fp2);
		}

		blength=blength%8;	/*how many leavings bit*/
		
		/*
		 * copy the leavings to low bit,
		 * the next char will fill from low bit
		 */
		for(i=0;i<blength;i++)	
		    b[i]=b[i+k*8];
	    }
       }

       /*
	* handle the last bit
	* make up a whole char put into file
	*/
       if(blength>0)
       {
	   mask = 0x80;
	   for(j=0;j<blength;j++)
	   {
	       code |= b[j] << (7-j);
	   }
	   fputc(code,fp2);
       }	  
   }
   else printf("open file error\n");
   fclose(fp1);
   fclose(fp2);
}

int decode_file(char *source_filename,char *target_filename, hufm_table *head)
{
    FILE *fp1, *fp2;
    int i,j,loop;
    char code,mask;
    char b[N];
    hufm_table *current;

    if(head == NULL)
    {
        printf("head is NULL\n");
        return -1;
    }
taskDelay(10);
printf("Enter the decode_file");
    if((fp1=fopen(source_filename,"rb"))!=NULL&&
		   (fp2=fopen(target_filename, "w"))!=NULL)
    {
	while(!feof(fp1))
	{
taskDelay(10);
printf("for(i=0;i<N;i++)\n");
	    for(i=0;i<N;i++)
		b[i] = '\0';

	    i = 0;
	    loop = 1;
	    current = head;

	    /*handle a character*/
	    while(loop)
	    {
		code = fgetc(fp1);
taskDelay(10);
printf("code = fgetc(fp1);\n");
		mask = 0x01;	/*use the mask get the bit value*/
		for(j=0;j<8;j++,current = current->next){
taskDelay(10);
printf("for(j=0;j<8;j++,current = current->next){\n");
		    mask = mask << j;
		    /*get the bit value 1, or 0*/
		    b[i*8+j] = !!(code & mask);
		    /*I can seek by link
		     * because all the node is sort by code size*/
		    if(b[i*8+j] == current->bits[i*8+j]){
taskDelay(10);
printf("if(b[i*8+j] == current->bits[i*8+j]){\n");
			fputc(current->ch, fp2);
			loop = 0;
			break;
		    }
		}
		i++;
	    }
	}
    }
    else 
    {
        printf("open file error\n");
        return -1;
    }
    fclose(fp1);
    fclose(fp2);
    return 0;
}
	    

	    
	    
	      
	
	


hufm_table *initial_codetype_from_file(char *code_file){
    FILE *fp;
    hufm_table *buf, *head, *front, *p;
    
    head = NULL;

taskDelay(10);
printf("enter the initial_codetype_from_file\n");
    if((fp = fopen(code_file, "r"))!=NULL)
    {
	while(!feof(fp)){
	    buf = (hufm_table *)malloc(sizeof(hufm_table));
	    if(buf == NULL) 
	    {
	       printf("malloc error\n");
	       return NULL;
	    }
	    fread(buf, sizeof(hufm_table), 1, fp);
taskDelay(10);
printf("the start is %d, the char is %c\n", buf->start, buf->ch);
	    /*the purpose is sort, for compress and
	     * decompress*/
	    if(head == NULL){
		head = buf;
		head->next=head;
	    }
	    else
	    {
		front = head;
		p = head->next;
		do
		{
		    /*if(n-buf->start < n-p->start)*/
		    if(buf->start > p->start)
		    {
			buf->next = p;
			front->next = buf;
			break;
		    }
		    front = p;
		    p = p->next;
		}while(p->next !=head);
	    }
	}
    }
    else 
    {
	printf("cannot open file\n");
	return NULL;
    }
    fclose(fp);
    return head;
}
	
int mainn()
{
    hufm_table *head;
    char *huffman_code_table;
    char *source_filename;
    char *target_filename;

    huffman_code_table = "huffman_code";
    source_filename = "source";
    target_filename = "target";
    head = NULL;
printf("inital codetype form file\n");
    head = initial_codetype_from_file("huffman_code");
printf("inital codetype form file\n");
taskDelay(10);

    code_file("source", "target", head);
printf("code_file\n");

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -