aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/SF_FlateLzwDecode.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2020-12-23 12:12:49 +0100
committerJay Berkenbilt <ejb@ql.org>2020-12-28 18:58:19 +0100
commit39bfa0130713defc9abb478a70717ca07377cdab (patch)
tree18b6370d5f2f7d10a3f1ef09a8f0dd3b9281bd3c /libqpdf/SF_FlateLzwDecode.cc
parent1fb26f08ad91d08f67ac30e2557ddcadd8b9ccac (diff)
downloadqpdf-39bfa0130713defc9abb478a70717ca07377cdab.tar.zst
Implement user-provided stream filters
Refactor QPDF_Stream to use stream filter classes to handle supported stream filters as well.
Diffstat (limited to 'libqpdf/SF_FlateLzwDecode.cc')
-rw-r--r--libqpdf/SF_FlateLzwDecode.cc153
1 files changed, 153 insertions, 0 deletions
diff --git a/libqpdf/SF_FlateLzwDecode.cc b/libqpdf/SF_FlateLzwDecode.cc
new file mode 100644
index 00000000..29064cc0
--- /dev/null
+++ b/libqpdf/SF_FlateLzwDecode.cc
@@ -0,0 +1,153 @@
+#include <qpdf/SF_FlateLzwDecode.hh>
+#include <qpdf/Pl_PNGFilter.hh>
+#include <qpdf/Pl_TIFFPredictor.hh>
+#include <qpdf/Pl_Flate.hh>
+#include <qpdf/Pl_LZWDecoder.hh>
+#include <qpdf/QTC.hh>
+#include <qpdf/QIntC.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);
+}