📄 hu.c
字号:
/* Copyright (c) Supersoft *//* Supersoft cor.lim was built by zhang zu hou,* a sophomore from xi'an Institute of Post and* Teleconmunications.Anyone who wants to disstribute* or use it must be entitled,unless,the one,who does* not follow it,will be punished by raw*/#ifndef __HUFFMAN__ #define __HUFFMAN__#include <stdio.h>#include <stdlib.h>#include <string.h>int Huffman_Compression(char * infilename, char * outfilename);int Huffman_Decompression(char * infilename, char * outfilename);unsigned short GenerateCodeTable ();void BuildCodeTree ();void BuildInitialHeap ();void CompressImage ();void GetFrequencyCount ();void BuildDecompTree ();void DecompressImage ();short father[512];unsigned short code[256], heaplength;unsigned long compresscharcount, filesize, heap[257];unsigned char codelength[256];long frequencycount[512];short decomp_tree[512];FILE *ifile, *ofile;int main(void){ char sname[20]; char tname[20]; printf("\nCompress..."); Huffman_Compression("1.ogg", "vspdxp.huf" ); printf("\nDecompress..."); Huffman_Decompression("vspdxp.huf", "dog.EXE" ); //unlink("vspdxp.huf"); return(0);}int Huffman_Compression(char * infilename, char * outfilename){ if ((ifile = fopen (infilename, "rb")) != NULL) { fseek (ifile, 0L, 2); filesize = (unsigned long) ftell (ifile); fseek (ifile, 0L, 0); GetFrequencyCount (); BuildInitialHeap (); BuildCodeTree (); if (!GenerateCodeTable ()) { printf ("ERROR! Code Value Out of Range. Cannot Compress.\n"); return 0; } else { if ((ofile = fopen (outfilename, "wb")) != NULL) { fwrite (&filesize, sizeof (filesize), 1, ofile); fwrite (code, 2, 256, ofile); fwrite (codelength, 1, 256, ofile); fseek (ifile, 0L, 0); CompressImage (); fclose (ofile); } else { printf("\nERROR: Couldn't create output file %s\n", outfilename); return 0; } } fclose (ifile); } else { printf ("\nERROR: %s -- File not found!\n", infilename); return 0; } return 1;}void CompressImage (){ register unsigned int thebyte = 0; register short loop1; register unsigned short current_code; register unsigned long loop; unsigned short current_length, dvalue; unsigned long curbyte = 0; short curbit = 7; for (loop = 0L; loop < filesize; loop++) { dvalue = (unsigned short) getc (ifile); current_code = code[dvalue]; current_length = (unsigned short) codelength[dvalue]; for (loop1 = current_length-1; loop1 >= 0; --loop1) { if ((current_code >> loop1) & 1) thebyte |= (char) (1 << curbit); if (--curbit < 0) { putc (thebyte, ofile); thebyte = 0; curbyte++; curbit = 7; } } } putc (thebyte, ofile); compresscharcount = ++curbyte;}unsigned short GenerateCodeTable (){ register unsigned short loop; register unsigned short current_length; register unsigned short current_bit; unsigned short bitcode; short parent; for (loop = 0; loop < 256; loop++) if (frequencycount[loop]) { current_length = bitcode = 0; current_bit = 1; parent = father[loop]; while (parent) { if (parent < 0) { bitcode += current_bit; parent = -parent; } parent = father[parent]; current_bit <<= 1; current_length++; } code[loop] = bitcode; if (current_length > 16) return (0); else codelength[loop] = (unsigned char) current_length; } else code[loop] = codelength[loop] = 0; return (1);}void BuildCodeTree (){ void reheap (); register unsigned short findex; register unsigned long heap_value; while (heaplength != 1) { heap_value = heap[1]; heap[1] = heap[heaplength--]; reheap (1); findex = heaplength + 255; frequencycount[findex] = frequencycount[heap[1]] + frequencycount[heap_value]; father[heap_value] = findex; father[heap[1]] = -findex; heap[1] = findex; reheap (1); } father[256] = 0;}void reheap (heap_entry)unsigned short heap_entry;{ register unsigned short index; register unsigned short flag = 1; unsigned long heap_value; heap_value = heap[heap_entry]; while ((heap_entry <= (heaplength >> 1)) && (flag)) { index = heap_entry << 1; if (index < heaplength) if (frequencycount[heap[index]] >= frequencycount[heap[index+1]]) index++; if (frequencycount[heap_value] < frequencycount[heap[index]]) flag--; else { heap[heap_entry] = heap[index]; heap_entry = index; } } heap[heap_entry] = heap_value;}void BuildInitialHeap (){ void reheap (); register unsigned short loop; heaplength = 0; for (loop = 0; loop < 256; loop++) if (frequencycount[loop]) heap[++heaplength] = (unsigned long) loop; for (loop = heaplength; loop > 0; loop--) reheap (loop);}void GetFrequencyCount (){ register unsigned long loop; for (loop = 0; loop < filesize; loop++) frequencycount[getc (ifile)]++;}int Huffman_Decompression(char * infilename, char * outfilename){ if ((ifile = fopen (infilename, "rb")) != NULL) { fread (&filesize, sizeof (filesize), 1, ifile); fread (code, 2, 256, ifile); fread (codelength, 1, 256, ifile); BuildDecompTree (); if ((ofile = fopen (outfilename, "wb")) != NULL) { DecompressImage(); fclose (ofile); } else { printf ("\nERROR: Couldn't create output file %s\n", outfilename); return 0; } fclose (ifile); } else { printf ("\nERROR: %s -- File not found!\n", infilename); return 0; } return 1;}void BuildDecompTree (){ register unsigned short loop1; register unsigned short current_index; unsigned short loop; unsigned short current_node = 1; decomp_tree[1] = 1; for (loop = 0; loop < 256; loop++) { if (codelength[loop]) { current_index = 1; for (loop1 = codelength[loop] - 1; loop1 > 0; loop1--) { current_index = (decomp_tree[current_index] << 1) + ((code[loop] >> loop1) & 1); if (!(decomp_tree[current_index])) decomp_tree[current_index] = ++current_node; } decomp_tree[(decomp_tree[current_index] << 1) + (code[loop] & 1)] = -loop; } }}void DecompressImage (){ register unsigned short cindex = 1; register char curchar; register short bitshift; unsigned long charcount = 0L; while (charcount < filesize) { curchar = (char) getc (ifile); for (bitshift = 7; bitshift >= 0; --bitshift) { cindex = (cindex << 1) + ((curchar >> bitshift) & 1); if (decomp_tree[cindex] <= 0) { putc ((int) (-decomp_tree[cindex]), ofile); if ((++charcount) == filesize) bitshift = 0; else cindex = 1; } else cindex = decomp_tree[cindex]; } }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -