pluskid / pyppm

PPM (Predict by Partial Matching) compression algorithm implementation for Python

This URL has Read+Write access

pyppm / test.cpp
100644 50 lines (38 sloc) 1.199 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
#include <cstdio>
#include "ppm_model.h"
#include "buffer.h"
#include "io_adapter.h"
 
using namespace std;
 
int main(int argc, char *argv[])
{
    FILE *fout = fopen("encoded.txt", "wb");
    FILE *forig = fopen("input.txt", "rb");
 
    FileOutputAdapter foad(fout);
 
    PPMEncoder<FileOutputAdapter, DefaultContextUpdater> *penc =
        new PPMEncoder<FileOutputAdapter, DefaultContextUpdater>(foad);
 
    printf("Encoding...\n");
    penc->start_encoding();
    for (int ch = fgetc(forig); ch != EOF; ch = fgetc(forig))
        penc->encode(ch);
    penc->finish_encoding();
 
    fclose(fout);
    fclose(forig);
 
    printf("------------------------------------\n");
    
    FILE *fin = fopen("encoded.txt", "rb");
    FILE *fnew = fopen("decoded.txt", "wb");
 
    FileInputAdapter fiad(fin);
    
    PPMDecoder<FileInputAdapter, DefaultContextUpdater> *pdec =
        new PPMDecoder<FileInputAdapter, DefaultContextUpdater>(fiad);
    pdec->start_decoding();
    wsymbol_t sym;
    for (;;) {
        sym = pdec->decode();
        if (sym == EOF_symbol)
            break;
        fputc(sym, fnew);
    }
    pdec->finish_decoding();
    fclose(fin);
    fclose(fnew);
    
    return 0;
}