void HufmanList::huf(Node *N, unsigned char c, string str, string & s) const { if (N) { if (!N->left && !N->right && N->ch == c) { s = str; } else { huf(N->left, c, str+"0", s); huf(N->right, c, str+"1", s); } } }
pool_byte_string_ptr HuffmanCoding::Decode(pool_byte_string_ptr bytes, UINT8 huffmanTimes) { const BYTE* data = bytes->data(); if (huffmanTimes == 0) { return bytes; } if (huffmanTimes == 1) { UINT32 size_m = 0, k, size_i = 0, i, j; BYTE* code; //确定解压缩后code的长度 k = 510; for (i = 0; i < bytes->size(); i ++) { for (j = 0; j < 8; j ++) { if ((data[i] & (1<<j)) == 0) k = decode1[k].l; else k = decode1[k].r; if (k<256) k = 510, size_m ++; } } // if (k != 510) return emptystring; m_decodeBuffer1.ensure_size( size_m ); code = m_decodeBuffer1.data(); //code = (BYTE*)CORE_MALLOC(sizeof(BYTE)*size_m); //huff decode k = 510;size_i = 0; for (i = 0; i < bytes->size(); i ++) { for (j = 0; j < 8; j ++) { if ((data[i] & (1<<j)) == 0) k = decode1[k].l; else k = decode1[k].r; if (k<256) { code[size_i] = (BYTE)decode1[k].c; k = 510; size_i ++; } } } pool_byte_string_ptr huf( new pool_byte_string(code,size_m) ); //CORE_FREE(code); return huf; } // huffmantimes == 2 UINT32 size_m = 0, k, size_i = 0, i, j; BYTE* code; //确定解压缩后code的长度 k = 510; for (i = 0; i < bytes->size(); i ++) { for (j = 0; j < 8; j ++) { if ((data[i] & (1<<j)) == 0) k = decode2[k].l; else k = decode2[k].r; if (k<256) k = 510, size_m ++; } } // if (k != 510) return emptystring; m_decodeBuffer1.ensure_size( size_m ); code = m_decodeBuffer1.data(); //code = (BYTE*)CORE_MALLOC(sizeof(BYTE)*size_m); //huff decode k = 510;size_i = 0; for (i = 0; i < bytes->size(); i ++) for (j = 0; j < 8; j ++) { if ((data[i] & (1<<j)) == 0) k = decode2[k].l; else k = decode2[k].r; if (k<256) { code[size_i] = (BYTE)decode2[k].c; k = 510; size_i ++; } } size_t size = size_m; UINT32 size_m2 = 0, size_i2; BYTE* code2; k = 510; //确定解压缩后code的长度 for (i = 0; i < size; i ++) { for (j = 0; j < 8; j ++) { if ((code[i] & (1<<j)) == 0) k = decode1[k].l; else k = decode1[k].r; if (k<256) k = 510, size_m2 ++; } } // if (k != 510) return emptystring; m_decodeBuffer2.ensure_size(size_m2); code2 = m_decodeBuffer2.data(); //code2 = (BYTE*)CORE_MALLOC(sizeof(BYTE)*size_m2); //huff decode k = 510; size_i2 = 0; for (i = 0; i < size; i ++) { for (j = 0; j < 8; j ++) { if ((code[i] & (1<<j)) == 0) k = decode1[k].l; else k = decode1[k].r; if (k<256) { code2[size_i2] = (BYTE)decode1[k].c; k = 510, size_i2 ++; } } } pool_byte_string_ptr huf2( new pool_byte_string(code2,size_m2) ); //CORE_FREE(code); //CORE_FREE(code2); return huf2; }
void Compression::HuffmanVLD() { std::fstream huf(path + std::string(".huf"),std::ios::in); std::fstream rld(path + std::string(".rlc"),std::ios::out); std::string str; huf >> str; std::stringstream ss(str); std::vector<std::string> split; for (std::string each;std::getline(ss, each, '/');split.push_back(each)); //std::cout << split.size() << std::endl; std::map<char,int> prob_table; for(size_t i = 0;i < split.size();i += 2){ prob_table.insert(std::make_pair(split[i].at(0),std::stoi(split[i + 1]))); } std::vector<HuffmanNode*> huffman_array; CreateHuffmanTree(prob_table,huffman_array); std::vector<HuffmanNode> huffman_table; CreateHuffmanCode(huffman_table,huffman_array[huffman_array.size()-1],std::string("")); bool null_flag = false; HuffmanNode *root = huffman_array[huffman_array.size()-1]; HuffmanNode *it = root; while(huf >> str){ for(size_t i = 0;i < str.size();++i){ char c = str[i]; if(c == '0'){ if(it->L){ it = it->L; } else{ null_flag = true; } } else if(c == '1'){ if(it->R){ it = it->R; } else{ null_flag = true; } } if(null_flag){ rld << it->data; null_flag = false; it = root; --i; } else if(i == str.size() - 1){ rld << it->data; null_flag = false; it = root; } } rld << std::endl; } huf.close(); }
void Compression::HuffmanVLC() { std::fstream rld(path + std::string(".rlc"),std::ios::in); std::fstream huf(path + std::string(".huf"),std::ios::out); std::vector<std::string> rlc; std::string str; while(rld >> str){ rlc.push_back(str); } std::map<char,int> prob_table; for(size_t i = 0;i < rlc.size();++i){ for(size_t j = 0;j < rlc[i].size();++j){ if(prob_table.insert(std::make_pair(rlc[i][j],1)).second){ } else{ ++prob_table[rlc[i][j]]; } } } for(std::map<char,int>::iterator it = prob_table.begin();it != prob_table.end();++it){ huf << it->first << "/" << it->second << "/"; } huf << std::endl; std::vector<HuffmanNode*> huffman_array; CreateHuffmanTree(prob_table,huffman_array); std::vector<HuffmanNode> huffman_table; CreateHuffmanCode(huffman_table,huffman_array[huffman_array.size()-1],std::string("")); for(size_t i = 0;i < rlc.size();++i){ for(size_t j = 0;j < rlc[i].size();++j){ huf << std::find(huffman_table.begin(),huffman_table.end(),rlc[i][j])->code; huffman_total_byte += std::find(huffman_table.begin(),huffman_table.end(),rlc[i][j])->code.size(); } huf << std::endl; } huffman_total_byte /= 8.0f; /* //binary file std::vector<std::vector<bool> > binary; for(size_t i = 0;i < rlc.size();++i){ std::vector<bool> temp; for(size_t j = 0;j < rlc[i].size();++j){ std::string code = std::find(huffman_table.begin(),huffman_table.end(),rlc[i][j])->code; for(size_t k = 0;k < code.size();++k){ temp.push_back(((code[k] == '0')?0:1)); } } binary.push_back(temp); } char c; for(size_t i = 0;i < binary.size();++i){ for(size_t j = 0;j < binary[i].size();++j){ if((j % 8) == 0){ huf << c; c = 0; } c += (binary[i][j] << (j % 8)); } } // std::fstream huf2(path + std::string(".huf"),std::ios::in); std::fstream hufd(path + std::string(".ddhuf"),std::ios::out); huf2 >> str; hufd << str; hufd << std::endl; while(!huf2.eof()){ huf2.get(c); for(int j = 0;j < 8;++j){ hufd << ((c & (1 << j))?'1':'0'); } hufd << std::endl; } */ rld.close(); huf.close(); }