mirror of
https://github.com/qpdf/qpdf.git
synced 2025-02-08 22:58:25 +00:00
Merge branch 'fz' of github.com:m-holger/qpdf into fz
This commit is contained in:
commit
b1ec5ba883
@ -3,12 +3,9 @@
|
|||||||
#include <qpdf/BitStream.hh>
|
#include <qpdf/BitStream.hh>
|
||||||
#include <qpdf/BitWriter.hh>
|
#include <qpdf/BitWriter.hh>
|
||||||
#include <qpdf/QTC.hh>
|
#include <qpdf/QTC.hh>
|
||||||
#include <qpdf/QUtil.hh>
|
|
||||||
|
|
||||||
#include <climits>
|
#include <climits>
|
||||||
#include <cstring>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
Pl_TIFFPredictor::Pl_TIFFPredictor(
|
Pl_TIFFPredictor::Pl_TIFFPredictor(
|
||||||
char const* identifier,
|
char const* identifier,
|
||||||
@ -22,7 +19,7 @@ Pl_TIFFPredictor::Pl_TIFFPredictor(
|
|||||||
columns(columns),
|
columns(columns),
|
||||||
samples_per_pixel(samples_per_pixel),
|
samples_per_pixel(samples_per_pixel),
|
||||||
bits_per_sample(bits_per_sample),
|
bits_per_sample(bits_per_sample),
|
||||||
pos(0)
|
p_next(getNext())
|
||||||
{
|
{
|
||||||
if (samples_per_pixel < 1) {
|
if (samples_per_pixel < 1) {
|
||||||
throw std::runtime_error("TIFFPredictor created with invalid samples_per_pixel");
|
throw std::runtime_error("TIFFPredictor created with invalid samples_per_pixel");
|
||||||
@ -35,71 +32,83 @@ Pl_TIFFPredictor::Pl_TIFFPredictor(
|
|||||||
throw std::runtime_error("TIFFPredictor created with invalid columns value");
|
throw std::runtime_error("TIFFPredictor created with invalid columns value");
|
||||||
}
|
}
|
||||||
this->bytes_per_row = bpr & UINT_MAX;
|
this->bytes_per_row = bpr & UINT_MAX;
|
||||||
this->cur_row = QUtil::make_shared_array<unsigned char>(this->bytes_per_row);
|
|
||||||
memset(this->cur_row.get(), 0, this->bytes_per_row);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Pl_TIFFPredictor::write(unsigned char const* data, size_t len)
|
Pl_TIFFPredictor::write(unsigned char const* data, size_t len)
|
||||||
{
|
{
|
||||||
size_t left = this->bytes_per_row - this->pos;
|
auto end = data + len;
|
||||||
size_t offset = 0;
|
auto row_end = data + (bytes_per_row - cur_row.size());
|
||||||
while (len >= left) {
|
while (row_end <= end) {
|
||||||
// finish off current row
|
// finish off current row
|
||||||
memcpy(this->cur_row.get() + this->pos, data + offset, left);
|
cur_row.insert(cur_row.end(), data, row_end);
|
||||||
offset += left;
|
data = row_end;
|
||||||
len -= left;
|
row_end += bytes_per_row;
|
||||||
|
|
||||||
processRow();
|
processRow();
|
||||||
|
|
||||||
// Prepare for next row
|
// Prepare for next row
|
||||||
memset(this->cur_row.get(), 0, this->bytes_per_row);
|
cur_row.clear();
|
||||||
left = this->bytes_per_row;
|
|
||||||
this->pos = 0;
|
|
||||||
}
|
}
|
||||||
if (len) {
|
|
||||||
memcpy(this->cur_row.get() + this->pos, data + offset, len);
|
cur_row.insert(cur_row.end(), data, end);
|
||||||
}
|
|
||||||
this->pos += len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Pl_TIFFPredictor::processRow()
|
Pl_TIFFPredictor::processRow()
|
||||||
{
|
{
|
||||||
QTC::TC("libtests", "Pl_TIFFPredictor processRow", (action == a_decode ? 0 : 1));
|
QTC::TC("libtests", "Pl_TIFFPredictor processRow", (action == a_decode ? 0 : 1));
|
||||||
BitWriter bw(this->getNext());
|
previous.assign(samples_per_pixel, 0);
|
||||||
BitStream in(this->cur_row.get(), this->bytes_per_row);
|
if (bits_per_sample != 8) {
|
||||||
std::vector<long long> prev;
|
BitWriter bw(p_next);
|
||||||
for (unsigned int i = 0; i < this->samples_per_pixel; ++i) {
|
BitStream in(cur_row.data(), cur_row.size());
|
||||||
long long sample = in.getBitsSigned(this->bits_per_sample);
|
for (unsigned int col = 0; col < this->columns; ++col) {
|
||||||
bw.writeBitsSigned(sample, this->bits_per_sample);
|
for (auto& prev: previous) {
|
||||||
prev.push_back(sample);
|
long long sample = in.getBitsSigned(this->bits_per_sample);
|
||||||
}
|
long long new_sample = sample;
|
||||||
for (unsigned int col = 1; col < this->columns; ++col) {
|
if (action == a_encode) {
|
||||||
for (unsigned int i = 0; i < this->samples_per_pixel; ++i) {
|
new_sample -= prev;
|
||||||
long long sample = in.getBitsSigned(this->bits_per_sample);
|
prev = sample;
|
||||||
long long new_sample = sample;
|
} else {
|
||||||
if (action == a_encode) {
|
new_sample += prev;
|
||||||
new_sample -= prev[i];
|
prev = new_sample;
|
||||||
prev[i] = sample;
|
}
|
||||||
} else {
|
bw.writeBitsSigned(new_sample, this->bits_per_sample);
|
||||||
new_sample += prev[i];
|
|
||||||
prev[i] = new_sample;
|
|
||||||
}
|
}
|
||||||
bw.writeBitsSigned(new_sample, this->bits_per_sample);
|
|
||||||
}
|
}
|
||||||
|
bw.flush();
|
||||||
|
} else {
|
||||||
|
out.clear();
|
||||||
|
auto next = cur_row.begin();
|
||||||
|
auto cr_end = cur_row.end();
|
||||||
|
auto pr_end = previous.end();
|
||||||
|
|
||||||
|
while (next != cr_end) {
|
||||||
|
for (auto prev = previous.begin(); prev != pr_end && next != cr_end; ++prev, ++next) {
|
||||||
|
long long sample = *next;
|
||||||
|
long long new_sample = sample;
|
||||||
|
if (action == a_encode) {
|
||||||
|
new_sample -= *prev;
|
||||||
|
*prev = sample;
|
||||||
|
} else {
|
||||||
|
new_sample += *prev;
|
||||||
|
*prev = new_sample;
|
||||||
|
}
|
||||||
|
out.push_back(static_cast<unsigned char>(255U & new_sample));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p_next->write(out.data(), out.size());
|
||||||
}
|
}
|
||||||
bw.flush();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Pl_TIFFPredictor::finish()
|
Pl_TIFFPredictor::finish()
|
||||||
{
|
{
|
||||||
if (this->pos) {
|
if (!cur_row.empty()) {
|
||||||
// write partial row
|
// write partial row
|
||||||
|
cur_row.insert(cur_row.end(), bytes_per_row - cur_row.size(), 0);
|
||||||
processRow();
|
processRow();
|
||||||
}
|
}
|
||||||
this->pos = 0;
|
cur_row.clear();
|
||||||
memset(this->cur_row.get(), 0, this->bytes_per_row);
|
p_next->finish();
|
||||||
getNext()->finish();
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
#include <qpdf/Pipeline.hh>
|
#include <qpdf/Pipeline.hh>
|
||||||
|
|
||||||
|
#include<vector>
|
||||||
|
|
||||||
class Pl_TIFFPredictor: public Pipeline
|
class Pl_TIFFPredictor: public Pipeline
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -31,8 +33,10 @@ class Pl_TIFFPredictor: public Pipeline
|
|||||||
unsigned int bytes_per_row;
|
unsigned int bytes_per_row;
|
||||||
unsigned int samples_per_pixel;
|
unsigned int samples_per_pixel;
|
||||||
unsigned int bits_per_sample;
|
unsigned int bits_per_sample;
|
||||||
std::shared_ptr<unsigned char> cur_row;
|
std::vector<unsigned char> cur_row;
|
||||||
size_t pos;
|
std::vector<long long> previous;
|
||||||
|
std::vector<unsigned char> out;
|
||||||
|
Pipeline* p_next;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PL_TIFFPREDICTOR_HH
|
#endif // PL_TIFFPREDICTOR_HH
|
||||||
|
Loading…
x
Reference in New Issue
Block a user