summaryrefslogtreecommitdiffstats
path: root/libtests/predictors.cc
blob: a8f6c5d97c535b9073c400e81ec027b0f30da415 (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <qpdf/assert_test.h>

#include <qpdf/Pl_PNGFilter.hh>
#include <qpdf/Pl_StdioFile.hh>
#include <qpdf/Pl_TIFFPredictor.hh>
#include <qpdf/QIntC.hh>
#include <qpdf/QUtil.hh>

#include <errno.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>

void
run(char const* filename,
    char const* filter,
    bool encode,
    unsigned int columns,
    unsigned int bits_per_sample,
    unsigned int samples_per_pixel)
{
    FILE* in = QUtil::safe_fopen(filename, "rb");
    FILE* o1 = QUtil::safe_fopen("out", "wb");
    Pipeline* out = new Pl_StdioFile("out", o1);
    Pipeline* pl = 0;
    if (strcmp(filter, "png") == 0) {
        pl = new Pl_PNGFilter(
            "png",
            out,
            encode ? Pl_PNGFilter::a_encode : Pl_PNGFilter::a_decode,
            columns,
            samples_per_pixel,
            bits_per_sample);
    } else if (strcmp(filter, "tiff") == 0) {
        pl = new Pl_TIFFPredictor(
            "png",
            out,
            encode ? Pl_TIFFPredictor::a_encode : Pl_TIFFPredictor::a_decode,
            columns,
            samples_per_pixel,
            bits_per_sample);
    } else {
        std::cerr << "unknown filter " << filter << std::endl;
        exit(2);
    }
    assert((2 * (columns + 1)) < 1024);
    unsigned char buf[1024];
    size_t len;
    while (true) {
        len = fread(buf, 1, (2 * columns) + 1, in);
        if (len == 0) {
            break;
        }
        pl->write(buf, len);
        len = fread(buf, 1, 1, in);
        if (len == 0) {
            break;
        }
        pl->write(buf, len);
        len = fread(buf, 1, 1, in);
        if (len == 0) {
            break;
        }
        pl->write(buf, len);
    }

    pl->finish();
    delete pl;
    delete out;
    fclose(o1);
    fclose(in);

    std::cout << "done" << std::endl;
}

int
main(int argc, char* argv[])
{
    if (argc != 7) {
        std::cerr << "Usage: predictor {png|tiff} {en,de}code filename"
                  << " columns samples-per-pixel bits-per-sample" << std::endl;
        exit(2);
    }
    char* filter = argv[1];
    bool encode = (strcmp(argv[2], "encode") == 0);
    char* filename = argv[3];
    int columns = QUtil::string_to_int(argv[4]);
    int samples_per_pixel = QUtil::string_to_int(argv[5]);
    int bits_per_sample = QUtil::string_to_int(argv[6]);

    try {
        run(filename,
            filter,
            encode,
            QIntC::to_uint(columns),
            QIntC::to_uint(bits_per_sample),
            QIntC::to_uint(samples_per_pixel));
    } catch (std::exception& e) {
        std::cout << e.what() << std::endl;
    }
    return 0;
}