ezw_code ezw_decoder::decode_value(const dom_elt& e, ibitstream& in) { bool hi = in.read_bit(); // tells whether POS/NEG or not bool lo = in.read_bit(); // ZERO_TREE or ZERO if (!in.good()) { // make sure end of file wasn't reached early return STOP; } if (hi) { sub_list_.push_back(sub_elt(e.row, e.col)); if (lo) { DBG_OUT('p'); // check size in case of reduced-size output. if (matrix_utils::in_bounds(*decoded_, e.row, e.col)) { (*decoded_)(e.row, e.col) = threshold_; } return POSITIVE; } else { DBG_OUT('n'); // check size in case of reduced-size output. if (matrix_utils::in_bounds(*decoded_, e.row, e.col)) { (*decoded_)(e.row, e.col) = -threshold_; } return NEGATIVE; } } else { DBG_OUT(lo ? 'z' : 't'); return lo ? ZERO : ZERO_TREE; } }
/// Subordinate pass of EZW algorithm. see Shapiro, 1993 for info. bool ezw_decoder::subordinate_pass(ibitstream& in) { for (size_t i=0; i < sub_list_.size(); i++) { sub_elt e = sub_list_[i]; if (in.read_bit()) { if (!in.good()) return false; // check bounds in case we're creating reduced-size output, // where we'd ignore things that are out of bounds. if (matrix_utils::in_bounds(*decoded_, e.row, e.col)) { if ((*decoded_)(e.row, e.col) < 0) { (*decoded_)(e.row, e.col) -= threshold_; } else { (*decoded_)(e.row, e.col) += threshold_; } } DBG_OUT(1); } else { if (!in.good()) return false; DBG_OUT(0); } } return true; }
void decodeData(ibitstream& input, HuffmanNode* encodingTree, ostream& output) { while (true) { int character = getCharacter(input, encodingTree); if (character == NOT_A_CHAR || input.tellg() == -1) return; output.put(character); } }
int input_9bit(ibitstream & in) { int res = 0; for (int i = 0; i < 9; ++i){ res <<= 1; res |= in.get_bit(); } return res; }
char buildCharFromByte(ibitstream& input) { int tempByte = 0; char tempChar; for(double i = 0; i <= 7; ++i){ tempByte += pow(2.0, i)*input.readBit(); } tempChar = tempByte; return tempChar; }
void decodeDataHelper(ibitstream& input, HuffmanNode* encodingTree, ostream& output){ //Noden är ett löv, skriv ut värdet if(encodingTree->isLeaf()){ int character = encodingTree->character; if(character != PSEUDO_EOF){ //Om vi inte nått slutet output.put(encodingTree->character); }else{ input.get(); //Ta bort sista tecknet } }else{ //Läs in första biten int tempBit = input.readBit(); //Traversera vänster/höger träd beroende på biten if(tempBit == 0){ decodeDataHelper(input, encodingTree->zero, output); }else if(tempBit == 1){ decodeDataHelper(input, encodingTree->one, output); } } }
//Om det är 1 i början av filen packas filen upp på samma sätt som förut //Annars läses storleken på frekvenstbellen in först och sen byte för byte värden som sedan sätts in i frekvenstabellen void decompress(ibitstream& input, ostream& output) { map<int, int> freqTable; int testForSize = input.get(); if(testForSize == '1'){ bool switchSide = false; string value = ""; string freq = ""; decompressHelper(input,freqTable,value,freq,switchSide); }else{ int inputSize = input.get(); for(int i = 0; i < inputSize; ++i){ char tempByte = input.get(); int tempValue = tempByte; int tempFreq = input.get(); --inputSize; //Eftersom både frekvensen samt karaktären tagits ut minska inputSize freqTable.insert(make_pair(tempValue,tempFreq)); } //Eftersom "endoffile" karaktären inte lades till om vi inte hade tal större än en byte läggs den till här freqTable.insert(make_pair(PSEUDO_EOF,1)); } HuffmanNode* encodingTree = buildEncodingTree(freqTable); decodeData(input,encodingTree,output); freeTree(encodingTree); }
void decodeData(ibitstream& input, HuffmanNode* encodingTree, ostream& output) { int bit; HuffmanNode* curr = encodingTree; while ((bit = input.readBit()) != -1) { if (bit == 0 && curr->zero != NULL) curr = curr->zero; else if (bit == 1 && curr->one != NULL) curr = curr->one; else if (bit == 0 && curr->zero == NULL) { output.put(curr->character); curr = encodingTree->zero; } else { output.put(curr->character); curr = encodingTree->one; } } }
int getCharacter(ibitstream& input, HuffmanNode* tree) { if (tree->isLeaf()) return tree->character; int bit = input.readBit(); return (bit == 1) ? getCharacter(input, tree->one) : getCharacter(input, tree->zero); }