2018-01-13 19:15:20 +00:00
|
|
|
#include <qpdf/Pl_TIFFPredictor.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2018-01-13 19:15:20 +00:00
|
|
|
#include <qpdf/BitStream.hh>
|
|
|
|
#include <qpdf/BitWriter.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
2022-02-07 16:29:12 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <limits.h>
|
2018-01-13 19:15:20 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <string.h>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <vector>
|
2018-01-13 19:15:20 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
Pl_TIFFPredictor::Pl_TIFFPredictor(
|
|
|
|
char const* identifier,
|
|
|
|
Pipeline* next,
|
|
|
|
action_e action,
|
|
|
|
unsigned int columns,
|
|
|
|
unsigned int samples_per_pixel,
|
|
|
|
unsigned int bits_per_sample) :
|
2018-01-13 19:15:20 +00:00
|
|
|
Pipeline(identifier, next),
|
|
|
|
action(action),
|
|
|
|
columns(columns),
|
|
|
|
samples_per_pixel(samples_per_pixel),
|
|
|
|
bits_per_sample(bits_per_sample),
|
|
|
|
pos(0)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (samples_per_pixel < 1) {
|
2018-01-13 19:15:20 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"TIFFPredictor created with invalid samples_per_pixel");
|
|
|
|
}
|
|
|
|
if ((bits_per_sample < 1) ||
|
2022-04-02 21:14:10 +00:00
|
|
|
(bits_per_sample > (8 * (sizeof(unsigned long long))))) {
|
2018-01-13 19:15:20 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"TIFFPredictor created with invalid bits_per_sample");
|
|
|
|
}
|
|
|
|
unsigned long long bpr =
|
|
|
|
((columns * bits_per_sample * samples_per_pixel) + 7) / 8;
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((bpr == 0) || (bpr > (UINT_MAX - 1))) {
|
2018-01-13 19:15:20 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"TIFFPredictor created with invalid columns value");
|
|
|
|
}
|
|
|
|
this->bytes_per_row = bpr & UINT_MAX;
|
2022-04-02 21:14:10 +00:00
|
|
|
this->cur_row =
|
2022-04-09 18:35:56 +00:00
|
|
|
QUtil::make_shared_array<unsigned char>(this->bytes_per_row);
|
2022-02-04 15:10:19 +00:00
|
|
|
memset(this->cur_row.get(), 0, this->bytes_per_row);
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_TIFFPredictor::write(unsigned char* data, size_t len)
|
|
|
|
{
|
|
|
|
size_t left = this->bytes_per_row - this->pos;
|
|
|
|
size_t offset = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (len >= left) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// finish off current row
|
|
|
|
memcpy(this->cur_row.get() + this->pos, data + offset, left);
|
|
|
|
offset += left;
|
|
|
|
len -= left;
|
2018-01-13 19:15:20 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
processRow();
|
2018-01-13 19:15:20 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// Prepare for next row
|
|
|
|
memset(this->cur_row.get(), 0, this->bytes_per_row);
|
|
|
|
left = this->bytes_per_row;
|
|
|
|
this->pos = 0;
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (len) {
|
2022-02-08 14:18:08 +00:00
|
|
|
memcpy(this->cur_row.get() + this->pos, data + offset, len);
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|
|
|
|
this->pos += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_TIFFPredictor::processRow()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
QTC::TC(
|
|
|
|
"libtests",
|
|
|
|
"Pl_TIFFPredictor processRow",
|
|
|
|
(action == a_decode ? 0 : 1));
|
2018-01-13 19:15:20 +00:00
|
|
|
BitWriter bw(this->getNext());
|
2022-02-04 15:10:19 +00:00
|
|
|
BitStream in(this->cur_row.get(), this->bytes_per_row);
|
2018-01-13 19:15:20 +00:00
|
|
|
std::vector<long long> prev;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (unsigned int i = 0; i < this->samples_per_pixel; ++i) {
|
2018-01-13 19:15:20 +00:00
|
|
|
long long sample = in.getBitsSigned(this->bits_per_sample);
|
|
|
|
bw.writeBitsSigned(sample, this->bits_per_sample);
|
|
|
|
prev.push_back(sample);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
for (unsigned int col = 1; col < this->columns; ++col) {
|
|
|
|
for (unsigned int i = 0; i < this->samples_per_pixel; ++i) {
|
2018-01-13 19:15:20 +00:00
|
|
|
long long sample = in.getBitsSigned(this->bits_per_sample);
|
|
|
|
long long new_sample = sample;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (action == a_encode) {
|
2018-01-13 19:15:20 +00:00
|
|
|
new_sample -= prev[i];
|
|
|
|
prev[i] = sample;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2018-01-13 19:15:20 +00:00
|
|
|
new_sample += prev[i];
|
|
|
|
prev[i] = new_sample;
|
|
|
|
}
|
|
|
|
bw.writeBitsSigned(new_sample, this->bits_per_sample);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bw.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_TIFFPredictor::finish()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->pos) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// write partial row
|
|
|
|
processRow();
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|
|
|
|
this->pos = 0;
|
2022-02-04 15:10:19 +00:00
|
|
|
memset(this->cur_row.get(), 0, this->bytes_per_row);
|
2018-01-13 19:15:20 +00:00
|
|
|
getNext()->finish();
|
|
|
|
}
|