/* ============== ReleaseFile Filename should be gamedir reletive. Either copies the file to the release dir, or adds it to the pak file. ============== */ void ReleaseFile (char *filename) { int len; byte *buf; char source[1024]; char dest[1024]; if (!g_release) return; sprintf (source, "%s%s", gamedir, filename); if (!g_pak) { // copy it sprintf (dest, "%s/%s", g_releasedir, filename); printf ("copying to %s\n", dest); QCopyFile (source, dest); return; } // pak it printf ("paking %s\n", filename); if (strlen(filename) >= sizeof(pf->name)) Error ("Filename too long for pak: %s", filename); len = LoadFile (source, (void **)&buf); if (g_compress_pak && len < 4096*1024 ) { cblock_t in, out; cblock_t Huffman (cblock_t in); in.count = len; in.data = buf; out = Huffman (in); if (out.count < in.count) { printf (" compressed from %i to %i\n", in.count, out.count); free (in.data); buf = out.data; len = out.count; } else free (out.data); } strcpy (pf->name, filename); pf->filepos = LittleLong(ftell(pakfile)); pf->filelen = LittleLong(len); pf++; SafeWrite (pakfile, buf, len); free (buf); }
ArbreEntier * Compression(donnees d, TabHuff * Tab){ ArbreEntier* a; TriArbreTableau(d.arbre, Tab); a = Huffman(d, Tab); //if(TestMerge(&Tab,d)) //a = Merge(d, &Tab); //a = Merge(d, &Tab); return a; }
void CodeText(string file_name, string text, int sequence_length /*= 1*/) { auto occurrences = CountCharsOccurrence(text, sequence_length); auto huffman_codes = Huffman(occurrences); // write huffman codes ofstream file_out(file_name, std::ios::binary); if (!file_out.is_open()) return; // write sequence length file_out.write((char*)&sequence_length, sizeof(int)); // write max_code length int max_code_length = FindMaximumHuffmanCode(huffman_codes).size(); file_out.write((char*)&max_code_length, sizeof(int)); // write codes num int num_codes = huffman_codes.size(); file_out.write((char*)&num_codes, sizeof(int)); // write codes one by one for (unsigned i = 0; i < num_codes; ++i) { string str = huffman_codes[i].first; string code = huffman_codes[i].second; file_out.write(str.c_str(), sizeof(char)*(sequence_length+1)); file_out.write(code.c_str(), sizeof(char)*(max_code_length+1)); } // code text string text_coded = ReplaceCharForHuffmanCode(text, huffman_codes); // number bytes to write int num_data_bytes = ceil(text_coded.size()/8.0); file_out.write((char*)&num_data_bytes, sizeof(int)); // write coded text byte by byte for (unsigned i = 0; i < text_coded.size(); i+=8) { string byte_slice = text_coded.substr(i, 8); if (byte_slice.size() < 8) { byte_slice.append(8 - byte_slice.size(), '0'); } char byte = BinaryStringToULong(byte_slice); file_out.write((char*)&byte, sizeof(char)); } cout << "================ " << file_name << " ================"; cout << "\nCoded file size=" << file_out.tellp(); cout << "\n\tNum of codes=" << num_codes; cout << "\n\tMax. code length=" << max_code_length; cout << "\n\tCharacter sequence length=" << sequence_length << endl; file_out.close(); }
int main() { FILE *InputFile=fopen("#InputText.in","r"); FILE *EncodedFile=fopen("#Output.out","w"); FILE *CodesFile=fopen("#Keys.out","w"); FILE *TreeFile=fopen("#Tree.out","w"); FILE *DecodedFile=fopen("#Decoded.out","w"); FILE *CompressionFile=fopen("#CRatio.out","w"); TEXT INPUT[MAX_LEN],OUTPUT[ENCODE_LEN]; ReadFile(InputFile); Huffman(INPUT); PrintCodes(CodesFile); DisplayTree(&ROOT , LEVEL , TreeFile); // Display the Huffman Tree DisplayTreeLevels(&ROOT , TreeFile); // Display the Huffman Tree by Levels HuffmanEncoding(INPUT , OUTPUT); // Input -<Encoding>-> Encoded Sequence EncodeFile(EncodedFile); DecodingHuffman(OUTPUT , TreeTop , DecodedFile); // Encoded Sequence -<Decoding>-> Decoded Sequence compressionRatio(INPUT , OUTPUT , CompressionFile); return 0; }