/*-------------------------------------------------------
PROGRAM: DEHUFF.C
USAGE : dehuff encoded.file decoded_file
PURPOSE: Huffman Decoder
Created by barkly 06/11/03
Finish Tested by mtchang
--------------------------------------------------------*/
/* Error codes returned to the caller */
#define NO_ERROR 0
#define BAD_FILE_NAME 1
#define BAD_ARGUMENT 2
#define BAD_MEM_ALLOC 3
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <values.h>
/* Global variables */
FILE *source_file,*dest_file;
/*int main(void);*/
void help(void);
void pass1(void);
void get_min2(int,int*,int*);
void pass2(void);
int get_bit(void);
typedef struct
{
float frequency;
int Lchild;
int Rchild;
int parent;
} NODE;
NODE table[256+255]; /*CONVERSION TABLE�RECORD��511*/
long total_bytes;
int tail;
/***************/
int main(argc,argv)
/* Returned parameters: Returns an error code (0=None)
Action: Main procedure
Errors: Detected, handled and an error code is returned, if any
*/
int argc;
char *argv[];
{ if (argc!=3)
{ help();
exit(BAD_ARGUMENT);
}
else if ((source_file=fopen(argv[1],"rb"))==NULL)
{ help();
exit(BAD_FILE_NAME);
}
else if ((dest_file=fopen(argv[2],"wb"))==NULL)
{ help();
exit(BAD_FILE_NAME);
}
else {
pass1();
/* fseek(source_file,0L,SEEK_SET);
rewind(source_file); */
pass2();
fclose(source_file);
fclose(dest_file);
}
printf("Execution of decode huffman completed.\n");
return (NO_ERROR);
}
/***************/
void pass1(void)
{
int i,m0,m1;
long total;
fread(&total_bytes,4,1,source_file); /*������串�度*/
for (i=0; i<256; i++)
fread(&table[i].frequency,4,1,source_file); /*è®�å�¥è³�æ��å��ç´ å�º {é »ç��*/
i = 256;
get_min2(256,&m0,&m1); /*以���建CONVERSION TABLE� {�*/
while (table[m1].frequency <= 1.0)
{
table[i].frequency = table[m0].frequency+table[m1].frequency;
table[i].Lchild = m0;
table[i].Rchild = m1;
table[m0].frequency = 2.0;
table[m1].frequency = 2.0;
table[m0].parent = i;
table[m1].parent = i;
i++;
get_min2(i,&m0,&m1);
}
table[i-1].parent = 0;
tail = i-1; /*tail��ROOT NODE*/
}
/***************/
void get_min2(int total, int *m0, int *m1)
{
int i;
if (table[1].frequency > table[0].frequency)
{
*m0 = 0;
*m1 = 1;
}
else
{
*m0 = 1;
*m1 = 0;
}
for (i=2; i<total; i++)
{
if (table[i].frequency < table[*m0].frequency)
{
*m1 = *m0;
*m0 = i;
}
else if (table[i].frequency < table[*m1].frequency)
*m1 = i;
}
}
/************/
void pass2(void)
{
int ptr;
long count=0L;
while (count < total_bytes)
{
ptr = tail; /*�ROOT NODE }�traverse down*/
while ((table[ptr].Lchild != 0) || (table[ptr].Rchild != 0))
{ /*��NODE�Lchild�Rchild�����0�*/
if (get_bit() == 0) /*�繼�traverse down */
ptr = table[ptr].Lchild;
else
ptr = table[ptr].Rchild;
}
fputc(ptr,dest_file); /*����TERMINAL NODE��*/
count++; /*����該NODE�ASCII碼�*/
} /*並�已�解碼�byte� [1�*/
}
/****************/
int get_bit(void)
{
static bit_count = 0;
static bit_buf;
int x;
if (bit_count == 0)
{
bit_buf = fgetc(source_file);
bit_count = 8;
}
x = bit_buf & 0x80;
bit_buf = bit_buf << 1;
bit_count--;
return x;
}
void help()
{
printf("\nUse: dehuff source target\n");
printf("source: Name of the file to compress\n");
printf("target: Name of the compressed file\n");
}
沒有留言:
張貼留言