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
|
|
|
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <climits>
|
2023-05-20 18:13:09 +00:00
|
|
|
#include <stdexcept>
|
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),
|
2024-01-22 00:32:15 +00:00
|
|
|
bits_per_sample(bits_per_sample),
|
|
|
|
p_next(getNext())
|
2018-01-13 19:15:20 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (samples_per_pixel < 1) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::runtime_error("TIFFPredictor created with invalid samples_per_pixel");
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
if ((bits_per_sample < 1) || (bits_per_sample > (8 * (sizeof(unsigned long long))))) {
|
|
|
|
throw std::runtime_error("TIFFPredictor created with invalid bits_per_sample");
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
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))) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::runtime_error("TIFFPredictor created with invalid columns value");
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|
|
|
|
this->bytes_per_row = bpr & UINT_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-05-03 21:43:07 +00:00
|
|
|
Pl_TIFFPredictor::write(unsigned char const* data, size_t len)
|
2018-01-13 19:15:20 +00:00
|
|
|
{
|
2024-01-21 18:21:01 +00:00
|
|
|
auto end = data + len;
|
|
|
|
auto row_end = data + (bytes_per_row - cur_row.size());
|
|
|
|
while (row_end <= end) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// finish off current row
|
2024-01-21 18:21:01 +00:00
|
|
|
cur_row.insert(cur_row.end(), data, row_end);
|
|
|
|
data = row_end;
|
|
|
|
row_end += bytes_per_row;
|
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
|
2024-01-21 18:21:01 +00:00
|
|
|
cur_row.clear();
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|
2024-01-21 18:21:01 +00:00
|
|
|
|
|
|
|
cur_row.insert(cur_row.end(), data, end);
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_TIFFPredictor::processRow()
|
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
QTC::TC("libtests", "Pl_TIFFPredictor processRow", (action == a_decode ? 0 : 1));
|
2024-01-26 20:56:21 +00:00
|
|
|
previous.assign(samples_per_pixel, 0);
|
2024-01-26 20:17:56 +00:00
|
|
|
if (bits_per_sample != 8) {
|
|
|
|
BitWriter bw(p_next);
|
|
|
|
BitStream in(cur_row.data(), cur_row.size());
|
|
|
|
for (unsigned int col = 0; col < this->columns; ++col) {
|
|
|
|
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;
|
|
|
|
prev = sample;
|
|
|
|
} else {
|
|
|
|
new_sample += prev;
|
|
|
|
prev = new_sample;
|
|
|
|
}
|
|
|
|
bw.writeBitsSigned(new_sample, this->bits_per_sample);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bw.flush();
|
|
|
|
} else {
|
2024-01-27 17:03:32 +00:00
|
|
|
out.clear();
|
2024-01-26 20:17:56 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-01-27 17:03:32 +00:00
|
|
|
out.push_back(static_cast<unsigned char>(255U & new_sample));
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-27 17:03:32 +00:00
|
|
|
p_next->write(out.data(), out.size());
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_TIFFPredictor::finish()
|
|
|
|
{
|
2024-01-21 17:57:10 +00:00
|
|
|
if (!cur_row.empty()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// write partial row
|
2024-01-21 17:57:10 +00:00
|
|
|
cur_row.insert(cur_row.end(), bytes_per_row - cur_row.size(), 0);
|
2022-02-08 14:18:08 +00:00
|
|
|
processRow();
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|
2024-01-21 17:57:10 +00:00
|
|
|
cur_row.clear();
|
2024-01-22 00:32:15 +00:00
|
|
|
p_next->finish();
|
2018-01-13 19:15:20 +00:00
|
|
|
}
|