aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/Pl_PNGFilter.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-04-02 23:14:10 +0200
committerJay Berkenbilt <ejb@ql.org>2022-04-04 14:10:40 +0200
commit12f1eb15ca3fed6310402847559a7c99d3c77847 (patch)
tree8935675b623c6f3b4914b8b44f7fa5f2816a9241 /libqpdf/Pl_PNGFilter.cc
parentf20fa61eb4c323eb1642c69c236b3d9a1f8b2cdb (diff)
downloadqpdf-12f1eb15ca3fed6310402847559a7c99d3c77847.tar.zst
Programmatically apply new formatting to code
Run this: for i in **/*.cc **/*.c **/*.h **/*.hh; do clang-format < $i >| $i.new && mv $i.new $i done
Diffstat (limited to 'libqpdf/Pl_PNGFilter.cc')
-rw-r--r--libqpdf/Pl_PNGFilter.cc120
1 files changed, 48 insertions, 72 deletions
diff --git a/libqpdf/Pl_PNGFilter.cc b/libqpdf/Pl_PNGFilter.cc
index 1dca43bb..14754fd1 100644
--- a/libqpdf/Pl_PNGFilter.cc
+++ b/libqpdf/Pl_PNGFilter.cc
@@ -3,19 +3,23 @@
#include <qpdf/QTC.hh>
#include <qpdf/QUtil.hh>
+#include <limits.h>
#include <stdexcept>
#include <string.h>
-#include <limits.h>
-static int abs_diff(int a, int b)
+static int
+abs_diff(int a, int b)
{
return a > b ? a - b : b - a;
}
-Pl_PNGFilter::Pl_PNGFilter(char const* identifier, Pipeline* next,
- action_e action, unsigned int columns,
- unsigned int samples_per_pixel,
- unsigned int bits_per_sample) :
+Pl_PNGFilter::Pl_PNGFilter(
+ char const* identifier,
+ Pipeline* next,
+ action_e action,
+ unsigned int columns,
+ unsigned int samples_per_pixel,
+ unsigned int bits_per_sample) :
Pipeline(identifier, next),
action(action),
cur_row(0),
@@ -24,17 +28,13 @@ Pl_PNGFilter::Pl_PNGFilter(char const* identifier, Pipeline* next,
buf2(0),
pos(0)
{
- if (samples_per_pixel < 1)
- {
+ if (samples_per_pixel < 1) {
throw std::runtime_error(
"PNGFilter created with invalid samples_per_pixel");
}
- if (! ((bits_per_sample == 1) ||
- (bits_per_sample == 2) ||
- (bits_per_sample == 4) ||
- (bits_per_sample == 8) ||
- (bits_per_sample == 16)))
- {
+ 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");
@@ -42,25 +42,23 @@ Pl_PNGFilter::Pl_PNGFilter(char const* identifier, Pipeline* next,
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)))
- {
+ if ((bpr == 0) || (bpr > (UINT_MAX - 1))) {
throw std::runtime_error(
"PNGFilter created with invalid columns value");
}
this->bytes_per_row = bpr & UINT_MAX;
- this->buf1 = make_array_pointer_holder<unsigned char>(
- this->bytes_per_row + 1);
- this->buf2 = make_array_pointer_holder<unsigned char>(
- this->bytes_per_row + 1);
+ this->buf1 =
+ make_array_pointer_holder<unsigned char>(this->bytes_per_row + 1);
+ this->buf2 =
+ make_array_pointer_holder<unsigned char>(this->bytes_per_row + 1);
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();
// number of bytes per incoming row
- this->incoming = (action == a_encode ?
- this->bytes_per_row :
- this->bytes_per_row + 1);
+ this->incoming =
+ (action == a_encode ? this->bytes_per_row : this->bytes_per_row + 1);
}
Pl_PNGFilter::~Pl_PNGFilter()
@@ -72,8 +70,7 @@ Pl_PNGFilter::write(unsigned char* data, size_t len)
{
size_t left = this->incoming - this->pos;
size_t offset = 0;
- while (len >= left)
- {
+ while (len >= left) {
// finish off current row
memcpy(this->cur_row + this->pos, data + offset, left);
offset += left;
@@ -89,8 +86,7 @@ Pl_PNGFilter::write(unsigned char* data, size_t len)
left = this->incoming;
this->pos = 0;
}
- if (len)
- {
+ if (len) {
memcpy(this->cur_row + this->pos, data + offset, len);
}
this->pos += len;
@@ -99,12 +95,9 @@ Pl_PNGFilter::write(unsigned char* data, size_t len)
void
Pl_PNGFilter::processRow()
{
- if (this->action == a_encode)
- {
+ if (this->action == a_encode) {
encodeRow();
- }
- else
- {
+ } else {
decodeRow();
}
}
@@ -113,25 +106,23 @@ void
Pl_PNGFilter::decodeRow()
{
int filter = this->cur_row[0];
- if (this->prev_row)
- {
- switch (filter)
- {
- case 0:
+ if (this->prev_row) {
+ switch (filter) {
+ case 0:
break;
- case 1:
+ case 1:
this->decodeSub();
break;
- case 2:
+ case 2:
this->decodeUp();
break;
- case 3:
+ case 3:
this->decodeAverage();
break;
- case 4:
+ case 4:
this->decodePaeth();
break;
- default:
+ default:
// ignore
break;
}
@@ -147,12 +138,10 @@ Pl_PNGFilter::decodeSub()
unsigned char* buffer = this->cur_row + 1;
unsigned int bpp = this->bytes_per_pixel;
- for (unsigned int i = 0; i < this->bytes_per_row; ++i)
- {
+ for (unsigned int i = 0; i < this->bytes_per_row; ++i) {
unsigned char left = 0;
- if (i >= bpp)
- {
+ if (i >= bpp) {
left = buffer[i - bpp];
}
@@ -167,8 +156,7 @@ Pl_PNGFilter::decodeUp()
unsigned char* buffer = this->cur_row + 1;
unsigned char* above_buffer = this->prev_row + 1;
- for (unsigned int i = 0; i < this->bytes_per_row; ++i)
- {
+ for (unsigned int i = 0; i < this->bytes_per_row; ++i) {
unsigned char up = above_buffer[i];
buffer[i] = static_cast<unsigned char>(buffer[i] + up);
}
@@ -182,18 +170,16 @@ Pl_PNGFilter::decodeAverage()
unsigned char* above_buffer = this->prev_row + 1;
unsigned int bpp = this->bytes_per_pixel;
- for (unsigned int i = 0; i < this->bytes_per_row; ++i)
- {
+ for (unsigned int i = 0; i < this->bytes_per_row; ++i) {
int left = 0;
int up = 0;
- if (i >= bpp)
- {
+ if (i >= bpp) {
left = buffer[i - bpp];
}
up = above_buffer[i];
- buffer[i] = static_cast<unsigned char>(buffer[i] + (left+up) / 2);
+ buffer[i] = static_cast<unsigned char>(buffer[i] + (left + up) / 2);
}
}
@@ -205,21 +191,18 @@ Pl_PNGFilter::decodePaeth()
unsigned char* above_buffer = this->prev_row + 1;
unsigned int bpp = this->bytes_per_pixel;
- for (unsigned int i = 0; i < this->bytes_per_row; ++i)
- {
+ for (unsigned int i = 0; i < this->bytes_per_row; ++i) {
int left = 0;
int up = above_buffer[i];
int upper_left = 0;
- if (i >= bpp)
- {
+ if (i >= bpp) {
left = buffer[i - bpp];
upper_left = above_buffer[i - bpp];
}
buffer[i] = static_cast<unsigned char>(
- buffer[i] +
- this->PaethPredictor(left, up, upper_left));
+ buffer[i] + this->PaethPredictor(left, up, upper_left));
}
}
@@ -231,12 +214,10 @@ Pl_PNGFilter::PaethPredictor(int a, int b, int c)
int pb = abs_diff(p, b);
int pc = abs_diff(p, c);
- if (pa <= pb && pa <= pc)
- {
+ if (pa <= pb && pa <= pc) {
return a;
}
- if (pb <= pc)
- {
+ if (pb <= pc) {
return b;
}
return c;
@@ -248,17 +229,13 @@ Pl_PNGFilter::encodeRow()
// For now, hard-code to using UP filter.
unsigned char ch = 2;
getNext()->write(&ch, 1);
- if (this->prev_row)
- {
- for (unsigned int i = 0; i < this->bytes_per_row; ++i)
- {
+ if (this->prev_row) {
+ for (unsigned int i = 0; i < this->bytes_per_row; ++i) {
ch = static_cast<unsigned char>(
this->cur_row[i] - this->prev_row[i]);
getNext()->write(&ch, 1);
}
- }
- else
- {
+ } else {
getNext()->write(this->cur_row, this->bytes_per_row);
}
}
@@ -266,8 +243,7 @@ Pl_PNGFilter::encodeRow()
void
Pl_PNGFilter::finish()
{
- if (this->pos)
- {
+ if (this->pos) {
// write partial row
processRow();
}