-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathArithmeticDecompress.cpp
More file actions
66 lines (58 loc) · 1.64 KB
/
ArithmeticDecompress.cpp
File metadata and controls
66 lines (58 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Decompression application using static arithmetic coding
*
* Usage: ArithmeticDecompress InputFile OutputFile
* This decompresses files generated by the "ArithmeticCompress" application.
*
* Copyright (c) Project Nayuki
* MIT License. See readme file.
* https://www.nayuki.io/page/reference-arithmetic-coding
*/
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <limits>
#include <vector>
#include "ArithmeticCoder.hpp"
#include "BitIoStream.hpp"
#include "FrequencyTable.hpp"
using std::uint32_t;
int main(int argc, char *argv[]) {
// Handle command line arguments
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " InputFile OutputFile" << std::endl;
return EXIT_FAILURE;
}
const char *inputFile = argv[1];
const char *outputFile = argv[2];
// Perform file decompression
std::ifstream in(inputFile, std::ios::binary);
std::ofstream out(outputFile, std::ios::binary);
BitInputStream bin(in);
try {
// Read frequency table
SimpleFrequencyTable freqs(std::vector<uint32_t>(257, 0));
for (uint32_t i = 0; i < 256; i++) {
uint32_t freq = 0;
for (int j = 0; j < 32; j++)
freq = (freq << 1) | bin.readNoEof(); // Big endian
freqs.set(i, freq);
}
freqs.increment(256); // EOF symbol
ArithmeticDecoder dec(32, bin);
while (true) {
uint32_t symbol = dec.read(freqs);
if (symbol == 256) // EOF symbol
break;
int b = static_cast<int>(symbol);
if (std::numeric_limits<char>::is_signed)
b -= (b >> 7) << 8;
out.put(static_cast<char>(b));
}
return EXIT_SUCCESS;
} catch (const char *msg) {
std::cerr << msg << std::endl;
return EXIT_FAILURE;
}
}