1 #include <qpdf/SF_FlateLzwDecode.hh>
2 #include <qpdf/Pl_PNGFilter.hh>
3 #include <qpdf/Pl_TIFFPredictor.hh>
4 #include <qpdf/Pl_Flate.hh>
5 #include <qpdf/Pl_LZWDecoder.hh>
6 #include <qpdf/QTC.hh>
7 #include <qpdf/QIntC.hh>
8
SF_FlateLzwDecode(bool lzw)9 SF_FlateLzwDecode::SF_FlateLzwDecode(bool lzw) :
10 lzw(lzw),
11 // Initialize values to their defaults as per the PDF spec
12 predictor(1),
13 columns(0),
14 colors(1),
15 bits_per_component(8),
16 early_code_change(true)
17 {
18 }
19
20 bool
setDecodeParms(QPDFObjectHandle decode_parms)21 SF_FlateLzwDecode::setDecodeParms(QPDFObjectHandle decode_parms)
22 {
23 if (decode_parms.isNull())
24 {
25 return true;
26 }
27
28 bool filterable = true;
29 std::set<std::string> keys = decode_parms.getKeys();
30 for (auto const& key: keys)
31 {
32 QPDFObjectHandle value = decode_parms.getKey(key);
33 if (key == "/Predictor")
34 {
35 if (value.isInteger())
36 {
37 this->predictor = value.getIntValueAsInt();
38 if (! ((this->predictor == 1) || (this->predictor == 2) ||
39 ((this->predictor >= 10) && (this->predictor <= 15))))
40 {
41 filterable = false;
42 }
43 }
44 else
45 {
46 filterable = false;
47 }
48 }
49 else if ((key == "/Columns") ||
50 (key == "/Colors") ||
51 (key == "/BitsPerComponent"))
52 {
53 if (value.isInteger())
54 {
55 int val = value.getIntValueAsInt();
56 if (key == "/Columns")
57 {
58 this->columns = val;
59 }
60 else if (key == "/Colors")
61 {
62 this->colors = val;
63 }
64 else if (key == "/BitsPerComponent")
65 {
66 this->bits_per_component = val;
67 }
68 }
69 else
70 {
71 filterable = false;
72 }
73 }
74 else if (lzw && (key == "/EarlyChange"))
75 {
76 if (value.isInteger())
77 {
78 int earlychange = value.getIntValueAsInt();
79 this->early_code_change = (earlychange == 1);
80 if (! ((earlychange == 0) || (earlychange == 1)))
81 {
82 filterable = false;
83 }
84 }
85 else
86 {
87 filterable = false;
88 }
89 }
90 }
91
92 if ((this->predictor > 1) && (this->columns == 0))
93 {
94 filterable = false;
95 }
96
97 return filterable;
98 }
99
100
101
102 Pipeline*
getDecodePipeline(Pipeline * next)103 SF_FlateLzwDecode::getDecodePipeline(Pipeline* next)
104 {
105 std::shared_ptr<Pipeline> pipeline;
106 if ((this->predictor >= 10) && (this->predictor <= 15))
107 {
108 QTC::TC("qpdf", "SF_FlateLzwDecode PNG filter");
109 pipeline = std::make_shared<Pl_PNGFilter>(
110 "png decode", next, Pl_PNGFilter::a_decode,
111 QIntC::to_uint(this->columns),
112 QIntC::to_uint(this->colors),
113 QIntC::to_uint(this->bits_per_component));
114 this->pipelines.push_back(pipeline);
115 next = pipeline.get();
116 }
117 else if (this->predictor == 2)
118 {
119 QTC::TC("qpdf", "SF_FlateLzwDecode TIFF predictor");
120 pipeline = std::make_shared<Pl_TIFFPredictor>(
121 "tiff decode", next, Pl_TIFFPredictor::a_decode,
122 QIntC::to_uint(this->columns),
123 QIntC::to_uint(this->colors),
124 QIntC::to_uint(this->bits_per_component));
125 this->pipelines.push_back(pipeline);
126 next = pipeline.get();
127 }
128
129 if (lzw)
130 {
131 pipeline = std::make_shared<Pl_LZWDecoder>(
132 "lzw decode", next, early_code_change);
133 }
134 else
135 {
136 pipeline = std::make_shared<Pl_Flate>(
137 "stream inflate", next, Pl_Flate::a_inflate);
138 }
139 this->pipelines.push_back(pipeline);
140 return pipeline.get();
141 }
142
143 std::shared_ptr<QPDFStreamFilter>
flate_factory()144 SF_FlateLzwDecode::flate_factory()
145 {
146 return std::make_shared<SF_FlateLzwDecode>(false);
147 }
148
149 std::shared_ptr<QPDFStreamFilter>
lzw_factory()150 SF_FlateLzwDecode::lzw_factory()
151 {
152 return std::make_shared<SF_FlateLzwDecode>(true);
153 }
154