2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_PNGFilter.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2017-12-25 15:27:45 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
2022-02-07 16:29:12 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <climits>
|
|
|
|
#include <cstring>
|
2009-09-26 18:36:04 +00:00
|
|
|
#include <stdexcept>
|
2018-02-04 18:51:54 +00:00
|
|
|
|
2024-07-18 11:28:26 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
unsigned long long memory_limit{0};
|
|
|
|
} // namespace
|
|
|
|
|
2018-02-04 18:51:54 +00:00
|
|
|
static int
|
|
|
|
abs_diff(int a, int b)
|
|
|
|
{
|
|
|
|
return a > b ? a - b : b - a;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
Pl_PNGFilter::Pl_PNGFilter(
|
|
|
|
char const* identifier,
|
|
|
|
Pipeline* next,
|
2022-02-08 14:18:08 +00:00
|
|
|
action_e action,
|
|
|
|
unsigned int columns,
|
2017-12-25 00:18:52 +00:00
|
|
|
unsigned int samples_per_pixel,
|
|
|
|
unsigned int bits_per_sample) :
|
2008-04-29 12:55:25 +00:00
|
|
|
Pipeline(identifier, next),
|
2024-08-07 16:20:19 +00:00
|
|
|
action(action)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2024-08-07 16:20:19 +00:00
|
|
|
if (!next) {
|
|
|
|
throw std::logic_error("Attempt to create Pl_PNGFilter with nullptr as next");
|
|
|
|
}
|
2018-01-13 18:21:23 +00:00
|
|
|
if (samples_per_pixel < 1) {
|
|
|
|
throw std::runtime_error("PNGFilter created with invalid samples_per_pixel");
|
2017-12-25 00:18:52 +00:00
|
|
|
}
|
|
|
|
if (!((bits_per_sample == 1) || (bits_per_sample == 2) || (bits_per_sample == 4) ||
|
|
|
|
(bits_per_sample == 8) || (bits_per_sample == 16))) {
|
|
|
|
throw std::runtime_error(
|
|
|
|
"PNGFilter created with invalid bits_per_sample not 1, 2, 4, 8, or 16");
|
|
|
|
}
|
|
|
|
this->bytes_per_pixel = ((bits_per_sample * samples_per_pixel) + 7) / 8;
|
|
|
|
unsigned long long bpr = ((columns * bits_per_sample * samples_per_pixel) + 7) / 8;
|
|
|
|
if ((bpr == 0) || (bpr > (UINT_MAX - 1))) {
|
2017-08-31 11:26:58 +00:00
|
|
|
throw std::runtime_error("PNGFilter created with invalid columns value");
|
2017-08-29 16:27:44 +00:00
|
|
|
}
|
2024-07-18 11:28:26 +00:00
|
|
|
if (memory_limit > 0 && bpr > (memory_limit / 2U)) {
|
|
|
|
throw std::runtime_error("PNGFilter memory limit exceeded");
|
|
|
|
}
|
2017-12-25 00:18:52 +00:00
|
|
|
this->bytes_per_row = bpr & UINT_MAX;
|
2022-04-09 18:35:56 +00:00
|
|
|
this->buf1 = QUtil::make_shared_array<unsigned char>(this->bytes_per_row + 1);
|
|
|
|
this->buf2 = QUtil::make_shared_array<unsigned char>(this->bytes_per_row + 1);
|
2022-02-04 15:10:19 +00:00
|
|
|
memset(this->buf1.get(), 0, this->bytes_per_row + 1);
|
|
|
|
memset(this->buf2.get(), 0, this->bytes_per_row + 1);
|
|
|
|
this->cur_row = this->buf1.get();
|
|
|
|
this->prev_row = this->buf2.get();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// number of bytes per incoming row
|
2017-12-25 00:18:52 +00:00
|
|
|
this->incoming = (action == a_encode ? this->bytes_per_row : this->bytes_per_row + 1);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 11:28:26 +00:00
|
|
|
void
|
|
|
|
Pl_PNGFilter::setMemoryLimit(unsigned long long limit)
|
|
|
|
{
|
|
|
|
memory_limit = limit;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
2022-05-03 21:43:07 +00:00
|
|
|
Pl_PNGFilter::write(unsigned char const* data, size_t len)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2012-06-20 15:20:57 +00:00
|
|
|
size_t left = this->incoming - this->pos;
|
|
|
|
size_t offset = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
while (len >= left) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// finish off current row
|
|
|
|
memcpy(this->cur_row + this->pos, data + offset, left);
|
|
|
|
offset += left;
|
|
|
|
len -= left;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
processRow();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
// Swap rows
|
|
|
|
unsigned char* t = this->prev_row;
|
|
|
|
this->prev_row = this->cur_row;
|
|
|
|
this->cur_row = t ? t : this->buf2.get();
|
|
|
|
memset(this->cur_row, 0, this->bytes_per_row + 1);
|
|
|
|
left = this->incoming;
|
|
|
|
this->pos = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
if (len) {
|
2022-02-08 14:18:08 +00:00
|
|
|
memcpy(this->cur_row + this->pos, data + offset, len);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
this->pos += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_PNGFilter::processRow()
|
|
|
|
{
|
|
|
|
if (this->action == a_encode) {
|
2022-02-08 14:18:08 +00:00
|
|
|
encodeRow();
|
2008-04-29 12:55:25 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
decodeRow();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_PNGFilter::decodeRow()
|
|
|
|
{
|
2013-02-24 02:46:21 +00:00
|
|
|
int filter = this->cur_row[0];
|
2008-04-29 12:55:25 +00:00
|
|
|
if (this->prev_row) {
|
2017-11-07 17:12:18 +00:00
|
|
|
switch (filter) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
this->decodeSub();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
this->decodeUp();
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
this->decodeAverage();
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
this->decodePaeth();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// ignore
|
|
|
|
break;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 16:20:19 +00:00
|
|
|
next()->write(this->cur_row + 1, this->bytes_per_row);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:12:18 +00:00
|
|
|
void
|
|
|
|
Pl_PNGFilter::decodeSub()
|
|
|
|
{
|
2017-12-25 15:27:45 +00:00
|
|
|
QTC::TC("libtests", "Pl_PNGFilter decodeSub");
|
2017-11-07 17:12:18 +00:00
|
|
|
unsigned char* buffer = this->cur_row + 1;
|
2017-12-25 00:18:52 +00:00
|
|
|
unsigned int bpp = this->bytes_per_pixel;
|
2017-11-07 17:12:18 +00:00
|
|
|
|
2017-12-25 00:18:52 +00:00
|
|
|
for (unsigned int i = 0; i < this->bytes_per_row; ++i) {
|
2017-11-07 17:12:18 +00:00
|
|
|
unsigned char left = 0;
|
|
|
|
|
|
|
|
if (i >= bpp) {
|
|
|
|
left = buffer[i - bpp];
|
|
|
|
}
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
buffer[i] = static_cast<unsigned char>(buffer[i] + left);
|
2017-11-07 17:12:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_PNGFilter::decodeUp()
|
|
|
|
{
|
2017-12-25 15:27:45 +00:00
|
|
|
QTC::TC("libtests", "Pl_PNGFilter decodeUp");
|
2017-11-07 17:12:18 +00:00
|
|
|
unsigned char* buffer = this->cur_row + 1;
|
|
|
|
unsigned char* above_buffer = this->prev_row + 1;
|
|
|
|
|
2017-12-25 00:18:52 +00:00
|
|
|
for (unsigned int i = 0; i < this->bytes_per_row; ++i) {
|
2017-11-07 17:12:18 +00:00
|
|
|
unsigned char up = above_buffer[i];
|
2019-06-21 03:35:23 +00:00
|
|
|
buffer[i] = static_cast<unsigned char>(buffer[i] + up);
|
2017-11-07 17:12:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_PNGFilter::decodeAverage()
|
|
|
|
{
|
2017-12-25 15:27:45 +00:00
|
|
|
QTC::TC("libtests", "Pl_PNGFilter decodeAverage");
|
2017-12-25 00:18:52 +00:00
|
|
|
unsigned char* buffer = this->cur_row + 1;
|
|
|
|
unsigned char* above_buffer = this->prev_row + 1;
|
|
|
|
unsigned int bpp = this->bytes_per_pixel;
|
2017-11-07 17:12:18 +00:00
|
|
|
|
2017-12-25 00:18:52 +00:00
|
|
|
for (unsigned int i = 0; i < this->bytes_per_row; ++i) {
|
|
|
|
int left = 0;
|
|
|
|
int up = 0;
|
2017-11-07 17:12:18 +00:00
|
|
|
|
|
|
|
if (i >= bpp) {
|
|
|
|
left = buffer[i - bpp];
|
|
|
|
}
|
|
|
|
|
|
|
|
up = above_buffer[i];
|
2019-06-21 03:35:23 +00:00
|
|
|
buffer[i] = static_cast<unsigned char>(buffer[i] + (left + up) / 2);
|
2017-11-07 17:12:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_PNGFilter::decodePaeth()
|
|
|
|
{
|
2017-12-25 15:27:45 +00:00
|
|
|
QTC::TC("libtests", "Pl_PNGFilter decodePaeth");
|
2017-12-25 00:18:52 +00:00
|
|
|
unsigned char* buffer = this->cur_row + 1;
|
|
|
|
unsigned char* above_buffer = this->prev_row + 1;
|
|
|
|
unsigned int bpp = this->bytes_per_pixel;
|
2017-11-07 17:12:18 +00:00
|
|
|
|
2017-12-25 00:18:52 +00:00
|
|
|
for (unsigned int i = 0; i < this->bytes_per_row; ++i) {
|
|
|
|
int left = 0;
|
|
|
|
int up = above_buffer[i];
|
|
|
|
int upper_left = 0;
|
2017-11-07 17:12:18 +00:00
|
|
|
|
|
|
|
if (i >= bpp) {
|
|
|
|
left = buffer[i - bpp];
|
|
|
|
upper_left = above_buffer[i - bpp];
|
|
|
|
}
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
buffer[i] =
|
|
|
|
static_cast<unsigned char>(buffer[i] + this->PaethPredictor(left, up, upper_left));
|
2017-11-07 17:12:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Pl_PNGFilter::PaethPredictor(int a, int b, int c)
|
|
|
|
{
|
|
|
|
int p = a + b - c;
|
2018-02-04 18:51:54 +00:00
|
|
|
int pa = abs_diff(p, a);
|
|
|
|
int pb = abs_diff(p, b);
|
|
|
|
int pc = abs_diff(p, c);
|
2017-11-07 17:12:18 +00:00
|
|
|
|
2017-12-25 00:18:52 +00:00
|
|
|
if (pa <= pb && pa <= pc) {
|
2017-11-07 17:12:18 +00:00
|
|
|
return a;
|
|
|
|
}
|
2017-12-25 00:18:52 +00:00
|
|
|
if (pb <= pc) {
|
2017-11-07 17:12:18 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
Pl_PNGFilter::encodeRow()
|
|
|
|
{
|
|
|
|
// For now, hard-code to using UP filter.
|
|
|
|
unsigned char ch = 2;
|
2024-08-07 16:20:19 +00:00
|
|
|
next()->write(&ch, 1);
|
2008-04-29 12:55:25 +00:00
|
|
|
if (this->prev_row) {
|
2022-02-08 14:18:08 +00:00
|
|
|
for (unsigned int i = 0; i < this->bytes_per_row; ++i) {
|
2019-06-21 03:35:23 +00:00
|
|
|
ch = static_cast<unsigned char>(this->cur_row[i] - this->prev_row[i]);
|
2024-08-07 16:20:19 +00:00
|
|
|
next()->write(&ch, 1);
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
} else {
|
2024-08-07 16:20:19 +00:00
|
|
|
next()->write(this->cur_row, this->bytes_per_row);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_PNGFilter::finish()
|
|
|
|
{
|
|
|
|
if (this->pos) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// write partial row
|
|
|
|
processRow();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-07-26 11:37:50 +00:00
|
|
|
this->prev_row = nullptr;
|
2022-02-04 15:10:19 +00:00
|
|
|
this->cur_row = buf1.get();
|
2008-04-29 12:55:25 +00:00
|
|
|
this->pos = 0;
|
2017-12-25 00:18:52 +00:00
|
|
|
memset(this->cur_row, 0, this->bytes_per_row + 1);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2024-08-07 16:20:19 +00:00
|
|
|
next()->finish();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|