From c01fc3c2ae3a6487d310f807bc25499d5d517c61 Mon Sep 17 00:00:00 2001 From: m-holger Date: Fri, 26 Jan 2024 19:13:34 +0000 Subject: [PATCH] In Pl_TIFFPredictor::processRow avoid repeated vector allocation Also, modernize for loop. --- libqpdf/Pl_TIFFPredictor.cc | 17 +++++++++-------- libqpdf/qpdf/Pl_TIFFPredictor.hh | 1 + 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libqpdf/Pl_TIFFPredictor.cc b/libqpdf/Pl_TIFFPredictor.cc index 5454cfe6..8820f4bc 100644 --- a/libqpdf/Pl_TIFFPredictor.cc +++ b/libqpdf/Pl_TIFFPredictor.cc @@ -7,6 +7,7 @@ #include #include #include +#include Pl_TIFFPredictor::Pl_TIFFPredictor( char const* identifier, @@ -60,23 +61,23 @@ Pl_TIFFPredictor::processRow() { QTC::TC("libtests", "Pl_TIFFPredictor processRow", (action == a_decode ? 0 : 1)); BitWriter bw(p_next); - BitStream in(this->cur_row.data(), this->bytes_per_row); - std::vector prev; + BitStream in(cur_row.data(), cur_row.size()); + previous.clear(); for (unsigned int i = 0; i < this->samples_per_pixel; ++i) { long long sample = in.getBitsSigned(this->bits_per_sample); bw.writeBitsSigned(sample, this->bits_per_sample); - prev.push_back(sample); + previous.push_back(sample); } for (unsigned int col = 1; col < this->columns; ++col) { - for (unsigned int i = 0; i < this->samples_per_pixel; ++i) { + for (auto& prev : previous) { long long sample = in.getBitsSigned(this->bits_per_sample); long long new_sample = sample; if (action == a_encode) { - new_sample -= prev[i]; - prev[i] = sample; + new_sample -= prev; + prev = sample; } else { - new_sample += prev[i]; - prev[i] = new_sample; + new_sample += prev; + prev = new_sample; } bw.writeBitsSigned(new_sample, this->bits_per_sample); } diff --git a/libqpdf/qpdf/Pl_TIFFPredictor.hh b/libqpdf/qpdf/Pl_TIFFPredictor.hh index 6557e15c..4579ba1f 100644 --- a/libqpdf/qpdf/Pl_TIFFPredictor.hh +++ b/libqpdf/qpdf/Pl_TIFFPredictor.hh @@ -34,6 +34,7 @@ class Pl_TIFFPredictor: public Pipeline unsigned int samples_per_pixel; unsigned int bits_per_sample; std::vector cur_row; + std::vector previous; Pipeline* p_next; };