aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/SF_FlateLzwDecode.cc
blob: dc76a32955e0a8b0f3fa9d5e5e79b88305f97136 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <qpdf/SF_FlateLzwDecode.hh>

#include <qpdf/Pl_Flate.hh>
#include <qpdf/Pl_LZWDecoder.hh>
#include <qpdf/Pl_PNGFilter.hh>
#include <qpdf/Pl_TIFFPredictor.hh>
#include <qpdf/QIntC.hh>
#include <qpdf/QTC.hh>

SF_FlateLzwDecode::SF_FlateLzwDecode(bool lzw) :
    lzw(lzw),
    // Initialize values to their defaults as per the PDF spec
    predictor(1),
    columns(0),
    colors(1),
    bits_per_component(8),
    early_code_change(true)
{
}

bool
SF_FlateLzwDecode::setDecodeParms(QPDFObjectHandle decode_parms)
{
    if (decode_parms.isNull()) {
        return true;
    }

    bool filterable = true;
    std::set<std::string> keys = decode_parms.getKeys();
    for (auto const& key: keys) {
        QPDFObjectHandle value = decode_parms.getKey(key);
        if (key == "/Predictor") {
            if (value.isInteger()) {
                this->predictor = value.getIntValueAsInt();
                if (!((this->predictor == 1) || (this->predictor == 2) ||
                      ((this->predictor >= 10) && (this->predictor <= 15)))) {
                    filterable = false;
                }
            } else {
                filterable = false;
            }
        } else if (
            (key == "/Columns") || (key == "/Colors") ||
            (key == "/BitsPerComponent")) {
            if (value.isInteger()) {
                int val = value.getIntValueAsInt();
                if (key == "/Columns") {
                    this->columns = val;
                } else if (key == "/Colors") {
                    this->colors = val;
                } else if (key == "/BitsPerComponent") {
                    this->bits_per_component = val;
                }
            } else {
                filterable = false;
            }
        } else if (lzw && (key == "/EarlyChange")) {
            if (value.isInteger()) {
                int earlychange = value.getIntValueAsInt();
                this->early_code_change = (earlychange == 1);
                if (!((earlychange == 0) || (earlychange == 1))) {
                    filterable = false;
                }
            } else {
                filterable = false;
            }
        }
    }

    if ((this->predictor > 1) && (this->columns == 0)) {
        filterable = false;
    }

    return filterable;
}

Pipeline*
SF_FlateLzwDecode::getDecodePipeline(Pipeline* next)
{
    std::shared_ptr<Pipeline> pipeline;
    if ((this->predictor >= 10) && (this->predictor <= 15)) {
        QTC::TC("qpdf", "SF_FlateLzwDecode PNG filter");
        pipeline = std::make_shared<Pl_PNGFilter>(
            "png decode",
            next,
            Pl_PNGFilter::a_decode,
            QIntC::to_uint(this->columns),
            QIntC::to_uint(this->colors),
            QIntC::to_uint(this->bits_per_component));
        this->pipelines.push_back(pipeline);
        next = pipeline.get();
    } else if (this->predictor == 2) {
        QTC::TC("qpdf", "SF_FlateLzwDecode TIFF predictor");
        pipeline = std::make_shared<Pl_TIFFPredictor>(
            "tiff decode",
            next,
            Pl_TIFFPredictor::a_decode,
            QIntC::to_uint(this->columns),
            QIntC::to_uint(this->colors),
            QIntC::to_uint(this->bits_per_component));
        this->pipelines.push_back(pipeline);
        next = pipeline.get();
    }

    if (lzw) {
        pipeline = std::make_shared<Pl_LZWDecoder>(
            "lzw decode", next, early_code_change);
    } else {
        pipeline = std::make_shared<Pl_Flate>(
            "stream inflate", next, Pl_Flate::a_inflate);
    }
    this->pipelines.push_back(pipeline);
    return pipeline.get();
}

std::shared_ptr<QPDFStreamFilter>
SF_FlateLzwDecode::flate_factory()
{
    return std::make_shared<SF_FlateLzwDecode>(false);
}

std::shared_ptr<QPDFStreamFilter>
SF_FlateLzwDecode::lzw_factory()
{
    return std::make_shared<SF_FlateLzwDecode>(true);
}