/src/qpdf/libqpdf/Pl_TIFFPredictor.cc
Line | Count | Source |
1 | | #include <qpdf/Pl_TIFFPredictor.hh> |
2 | | |
3 | | #include <qpdf/BitStream.hh> |
4 | | #include <qpdf/BitWriter.hh> |
5 | | #include <qpdf/QTC.hh> |
6 | | #include <qpdf/Util.hh> |
7 | | |
8 | | #include <climits> |
9 | | #include <stdexcept> |
10 | | |
11 | | using namespace qpdf; |
12 | | |
13 | | namespace |
14 | | { |
15 | | unsigned long long memory_limit{0}; |
16 | | } // namespace |
17 | | |
18 | | Pl_TIFFPredictor::Pl_TIFFPredictor( |
19 | | char const* identifier, |
20 | | Pipeline* next, |
21 | | action_e action, |
22 | | unsigned int columns, |
23 | | unsigned int samples_per_pixel, |
24 | | unsigned int bits_per_sample) : |
25 | 0 | Pipeline(identifier, next), |
26 | 0 | action(action), |
27 | 0 | columns(columns), |
28 | 0 | samples_per_pixel(samples_per_pixel), |
29 | 0 | bits_per_sample(bits_per_sample) |
30 | 0 | { |
31 | 0 | util::assertion(next, "Attempt to create Pl_TIFFPredictor with nullptr as next"); |
32 | 0 | util::no_ci_rt_error_if( |
33 | 0 | samples_per_pixel < 1, "TIFFPredictor created with invalid samples_per_pixel"); |
34 | 0 | util::no_ci_rt_error_if( |
35 | 0 | bits_per_sample < 1 || bits_per_sample > (8 * (sizeof(unsigned long long))), |
36 | 0 | "TIFFPredictor created with invalid bits_per_sample"); |
37 | 0 | unsigned long long bpr = ((columns * bits_per_sample * samples_per_pixel) + 7) / 8; |
38 | 0 | util::no_ci_rt_error_if( |
39 | 0 | bpr == 0 || bpr > (UINT_MAX - 1), "TIFFPredictor created with invalid columns value"); |
40 | 0 | util::no_ci_rt_error_if( |
41 | 0 | memory_limit > 0 && bpr > (memory_limit / 2U), "TIFFPredictor memory limit exceeded"); |
42 | 0 | this->bytes_per_row = bpr & UINT_MAX; |
43 | 0 | } |
44 | | |
45 | | void |
46 | | Pl_TIFFPredictor::setMemoryLimit(unsigned long long limit) |
47 | 0 | { |
48 | 0 | memory_limit = limit; |
49 | 0 | } |
50 | | |
51 | | void |
52 | | Pl_TIFFPredictor::write(unsigned char const* data, size_t len) |
53 | 0 | { |
54 | 0 | auto end = data + len; |
55 | 0 | auto row_end = data + (bytes_per_row - cur_row.size()); |
56 | 0 | while (row_end <= end) { |
57 | | // finish off current row |
58 | 0 | cur_row.insert(cur_row.end(), data, row_end); |
59 | 0 | data = row_end; |
60 | 0 | row_end += bytes_per_row; |
61 | |
|
62 | 0 | processRow(); |
63 | | |
64 | | // Prepare for next row |
65 | 0 | cur_row.clear(); |
66 | 0 | } |
67 | |
|
68 | 0 | cur_row.insert(cur_row.end(), data, end); |
69 | 0 | } |
70 | | |
71 | | void |
72 | | Pl_TIFFPredictor::processRow() |
73 | 0 | { |
74 | 0 | QTC::TC("libtests", "Pl_TIFFPredictor processRow", (action == a_decode ? 0 : 1)); |
75 | 0 | previous.assign(samples_per_pixel, 0); |
76 | 0 | if (bits_per_sample != 8) { |
77 | 0 | BitWriter bw(next()); |
78 | 0 | BitStream in(cur_row.data(), cur_row.size()); |
79 | 0 | for (unsigned int col = 0; col < this->columns; ++col) { |
80 | 0 | for (auto& prev: previous) { |
81 | 0 | long long sample = in.getBitsSigned(this->bits_per_sample); |
82 | 0 | long long new_sample = sample; |
83 | 0 | if (action == a_encode) { |
84 | 0 | new_sample -= prev; |
85 | 0 | prev = sample; |
86 | 0 | } else { |
87 | 0 | new_sample += prev; |
88 | 0 | prev = new_sample; |
89 | 0 | } |
90 | 0 | bw.writeBitsSigned(new_sample, this->bits_per_sample); |
91 | 0 | } |
92 | 0 | } |
93 | 0 | bw.flush(); |
94 | 0 | } else { |
95 | 0 | out.clear(); |
96 | 0 | auto next_it = cur_row.begin(); |
97 | 0 | auto cr_end = cur_row.end(); |
98 | 0 | auto pr_end = previous.end(); |
99 | |
|
100 | 0 | while (next_it != cr_end) { |
101 | 0 | for (auto prev = previous.begin(); prev != pr_end && next_it != cr_end; |
102 | 0 | ++prev, ++next_it) { |
103 | 0 | long long sample = *next_it; |
104 | 0 | long long new_sample = sample; |
105 | 0 | if (action == a_encode) { |
106 | 0 | new_sample -= *prev; |
107 | 0 | *prev = sample; |
108 | 0 | } else { |
109 | 0 | new_sample += *prev; |
110 | 0 | *prev = new_sample; |
111 | 0 | } |
112 | 0 | out.push_back(static_cast<unsigned char>(255U & new_sample)); |
113 | 0 | } |
114 | 0 | } |
115 | 0 | next()->write(out.data(), out.size()); |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | | void |
120 | | Pl_TIFFPredictor::finish() |
121 | 0 | { |
122 | 0 | if (!cur_row.empty()) { |
123 | | // write partial row |
124 | 0 | cur_row.insert(cur_row.end(), bytes_per_row - cur_row.size(), 0); |
125 | 0 | processRow(); |
126 | 0 | } |
127 | 0 | cur_row.clear(); |
128 | 0 | next()->finish(); |
129 | 0 | } |