int main(int argc, char **argv) { // Read the huffman tree at the beginning of the file Huffman<char> huff; huff.read_tree(std::cin); // huff.dump_tree(std::cerr); // read the encoded file bit by bit. With every new bit, // move one level down the huffman tree. When we reach a leaf, // emit the symbol and start over. The process expects to // be terminated by an explicit sentinel. BitReader bit_reader(std::cin); Huffman<char>::Node* state = nullptr; char symbol; while ( bit_reader ) { int bit = bit_reader.read_bit(); bool symbol_complete = huff.read_huff_bit(bit, state, symbol); // std::cerr << bit << " -> " << state->bits; // if ( symbol_complete ) { // std::cerr << " \"" << symbol << "\""; // } // std::cerr << std::endl; if ( symbol_complete ) { if ( symbol == '\0' ) { // we've reached the sentinel value, stop immediately. return 0; } else { std::cout << symbol; } } } return 1; }
void JPEGLoader::process_sos_sequential(JPEGStartOfScan &start_of_scan, std::vector<int> component_to_sof, JPEGFileReader &reader) { verify_dc_table_selector(start_of_scan); verify_ac_table_selector(start_of_scan); JPEGBitReader bit_reader(&reader); int restart_counter = 0; for (int mcu_block = 0; mcu_block < mcu_width*mcu_height; mcu_block++) { if (restart_interval != 0 && restart_counter == restart_interval) { JPEGMarker marker = reader.read_marker(); if (marker < marker_rst0 || marker > marker_rst7) { throw Exception("Restart marker missing between JPEG entropy data"); } restart_counter = 0; for (auto & elem : last_dc_values) elem = 0; bit_reader.reset(); eobrun = 0; } restart_counter++; for (size_t c = 0; c < start_of_scan.components.size(); c++) { int c_sof = component_to_sof[c]; const JPEGHuffmanTable &dc_table = huffman_dc_tables[start_of_scan.components[c].dc_table_selector]; const JPEGHuffmanTable &ac_table = huffman_ac_tables[start_of_scan.components[c].ac_table_selector]; int scale_x = start_of_frame.components[c_sof].horz_sampling_factor; int scale_y = start_of_frame.components[c_sof].vert_sampling_factor; for (int i = 0; i < scale_x * scale_y; i++) { short *dct = component_dcts[c_sof].get(mcu_block*scale_x*scale_y + i); for (int j = start_of_scan.start_dct_coefficient; j <= start_of_scan.end_dct_coefficient; j++) { if (j == 0) // DCT DC coefficient { unsigned int code = JPEGHuffmanDecoder::decode(bit_reader, dc_table); if (code != huffman_eob) dct[0] = JPEGHuffmanDecoder::decode_number(bit_reader, code); dct[0] <<= start_of_scan.point_transform; dct[0] += last_dc_values[c_sof]; last_dc_values[c_sof] = dct[0]; } else // DCT AC coefficient { unsigned int code = JPEGHuffmanDecoder::decode(bit_reader, ac_table); if (code != huffman_eob) { unsigned int zeros = (code >> 4); j += zeros; if (j <= start_of_scan.end_dct_coefficient) { dct[zigzag_map[j]] = JPEGHuffmanDecoder::decode_number(bit_reader, code & 0x0f); dct[zigzag_map[j]] <<= start_of_scan.point_transform; } } else { break; } } } } }