-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpdfobject.cpp
439 lines (357 loc) · 11.4 KB
/
pdfobject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
*
* Copyright: 2012-2017 Boomaga team https://github.com./Boomaga
* Authors:
* Alexander Sokoloff <[email protected]>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "pdfobject.h"
#include "pdferrors.h"
#include "pdfvalue.h"
#include <zlib.h>
#include <QDebug>
namespace PDF {
class FlateDecodeStream: public QByteArray
{
public:
FlateDecodeStream(const PDF::Dict ¶meters, const QByteArray &source);
private:
void unCompress(const QByteArray &source);
void applyPNGPredictor();
int mPredictor;
int mColors;
int mBitsPerComponent;
int mColumns;
};
} // namespace PDF
using namespace PDF;
/************************************************
*
* PNG predictors - http://www.w3.org/TR/PNG/#9Filters
************************************************/
FlateDecodeStream::FlateDecodeStream(const Dict ¶meters, const QByteArray &source)
{
unCompress(source);
// A code that selects the predictor algorithm, if any. If the value
// of this entry is 1, the filter assumes that the normal algorithm
// was used to encode the data, without prediction.
// If the value is greater than 1, the filter assumes that the data
// was differenced before being encoded, and Predictor selects the
// predictor algorithm.
mPredictor = parameters.value("Predictor").asNumber().value(1);
if (mPredictor == 1)
return;
// (Used only if Predictor is greater than 1) The number of interleaved
// color components per sample. Valid values are 1 to 4 in PDF 1.2 or
// earlier and 1 or greater in PDF 1.3 or later. Default value: 1.
mColors = parameters.value("Colors").asNumber().value(1);
// (Used only if Predictor is greater than 1) The number of bits used
// to represent each color component in a sample.
// Valid values are 1, 2, 4, 8, and 16. Default value: 8.
mBitsPerComponent = parameters.value("BitsPerComponent").asNumber().value(8);
// (Used only if Predictor is greater than 1) The number of samples
// in each row. Default value: 1.
mColumns = parameters.value("Columns").asNumber().value(1);
switch (mPredictor)
{
case 1: // No prediction
break;
case 2: // TIFF Predictor 2
throw Error("FlateDecode Predictor 2 not implemented yet.");
break;
case 10: // PNG prediction (on encoding, PNG None on all rows)
case 11: // PNG prediction (on encoding, PNG Sub on all rows)
case 12: // PNG prediction (on encoding, PNG Up on all rows)
case 13: // PNG prediction (on encoding, PNG Average on all rows)
case 14: // PNG prediction (on encoding, PNG Paeth on all rows)
case 15: // PNG prediction (on encoding, PNG optimum)
applyPNGPredictor();
break;
default:
throw Error(QString("Unknown FlateDecode Predictor '%d'.").arg(mPredictor));
}
}
/************************************************
*
************************************************/
void FlateDecodeStream::unCompress(const QByteArray &source)
{
// More typical zlib compression ratios are on the order of 2:1 to 5:1.
resize(source.size() * 2);
while (true)
{
uchar * dest = (uchar*)data();
uLongf destSize = size();
int ret = uncompress(dest, &destSize, reinterpret_cast<const uchar*>(source.data()), source.size());
switch (ret)
{
case Z_OK:
resize(destSize);
break;
case Z_MEM_ERROR:
throw Error("Z_MEM_ERROR: Not enough memory");
case Z_DATA_ERROR:
throw Error("Z_DATA_ERROR: Input data is corrupted");
case Z_BUF_ERROR:
resize(size() * 2);
continue;
}
break;
}
}
/************************************************
*
************************************************/
inline void pngPredictor0Row(const char *src, char *dest, int len)
{
for (int i=0; i<len; ++i)
dest[i] = src[i];
}
/************************************************
*
************************************************/
inline void pngPredictor2Row(const char *src, const char *prev, char *dest, int len)
{
for (int i=0; i<len; ++i)
dest[i] = src[i] + prev[i];
}
/************************************************
* https://www.w3.org/TR/2003/REC-PNG-20031110/#9Filters
*
* The postprediction data for each PNG-predicted
* row begins with an explicit algorithm tag;
************************************************/
void FlateDecodeStream::applyPNGPredictor()
{
char *data = this->data();
int rowLen = mColors * mBitsPerComponent * mColumns / 8;
// First row;
for (int pos=1; pos<rowLen+1; ++pos)
data[pos-1] = data[pos];
int size = this->size();
int w = rowLen;
int prev = 0;
for (int r=rowLen+1; r<size-rowLen;)
{
switch (data[r++])
{
case 0:
pngPredictor0Row(data+r, data+w, rowLen);
break;
case 1:
throw Error("PNG Predictor 1 not implemented yet.");
case 2:
pngPredictor2Row(data+r, data+prev, data+w, rowLen);
break;
case 3:
throw Error("PNG Predictor 3 not implemented yet.");
case 4:
throw Error("PNG Predictor 4 not implemented yet.");
default:
throw Error("Unknown PNG predictor type");
}
r+=rowLen;
w+=rowLen;
prev+=rowLen;
}
resize(w);
}
/************************************************
*
************************************************/
Object::Object(ObjNum objNum, GenNum genNum, const Value &value):
mObjNum(objNum),
mGenNum(genNum),
mValue(value),
mPos(0),
mLen(0)
{
}
/************************************************
*
************************************************/
Object::Object(const Object &other):
mObjNum( other.mObjNum),
mGenNum( other.mGenNum),
mValue( other.mValue),
mStream( other.mStream),
mPos( other.mPos),
mLen( other.mLen)
{
}
/************************************************
*
************************************************/
Object &Object::operator =(const Object &other)
{
mObjNum = other.mObjNum;
mGenNum = other.mGenNum;
mValue = other.mValue;
mStream = other.mStream;
mPos = other.mPos;
mLen = other.mLen;
return *this;
}
/************************************************
*
************************************************/
Object::~Object()
{
}
/************************************************
*
************************************************/
void Object::setObjNum(ObjNum value)
{
mObjNum = value;
}
/************************************************
*
************************************************/
void Object::setGenNum(GenNum value)
{
mGenNum = value;
}
/************************************************
*
************************************************/
void Object::setValue(const Value &value)
{
mValue = value;
}
/************************************************
*
************************************************/
void Object::setStream(const QByteArray &value)
{
mStream = value;
}
/************************************************
*
************************************************/
QByteArray Object::decodedStream() const
{
try
{
QStringList filters;
const PDF::Value &v = dict().value("Filter");
if (v.isName())
{
filters << v.asName().value();
}
else if (v.isArray())
{
const PDF::Array &arr = v.asArray();
for (int i=0; i<arr.count(); ++i)
filters << arr.at(i).asName().value();
}
QByteArray res = stream();
foreach (const QString &filter, filters)
{
if (filter == "FlateDecode")
{
res = FlateDecodeStream(dict().value("DecodeParms").asDict(), res);
continue;
}
if (filter == "ASCIIHexDecode" ||
filter == "ASCII85Decode" ||
filter == "LZWDecode" ||
filter == "FlateDecode" ||
filter == "RunLengthDecode" ||
filter == "CCITTFaxDecode" ||
filter == "JBIG2Decode" ||
filter == "DCTDecode" ||
filter == "JPXDecode" ||
filter == "Crypt" )
{
throw Error(QString("Unsupported filter '%1'").arg(filter));
}
throw Error(QString("Unknown filter '%1'").arg(filter));
}
return res;
}
catch (PDF::Error &err)
{
throw ObjectError(err.what(), mObjNum, mGenNum);
}
}
/************************************************
*
************************************************/
QString Object::type() const
{
return dict().value("Type").asName().value();
}
/************************************************
*
************************************************/
QString Object::subType() const
{
QString s = dict().value("Subtype").asName().value();
if (s.isEmpty())
return dict().value("S").asName().value();
else
return s;
}
/************************************************
*
************************************************/
QByteArray Object::streamFlateDecode(const QByteArray &source) const
{
QByteArray res;
// More typical zlib compression ratios are on the order of 2:1 to 5:1.
res.resize(source.size() * 2);
while (true)
{
uchar * dest = (uchar*)res.data();
uLongf destSize = res.size();
int ret = uncompress(dest, &destSize, reinterpret_cast<const uchar*>(source.data()), source.length());
switch (ret)
{
case Z_OK:
res.resize(destSize);
break;
case Z_MEM_ERROR:
qWarning("Z_MEM_ERROR: Not enough memory");
return QByteArray();
case Z_DATA_ERROR:
qWarning("Z_DATA_ERROR: Input data is corrupted");
return QByteArray();
case Z_BUF_ERROR:
res.resize(res.length() * 2);
continue;
}
break;
}
return res;
}
/************************************************
*
************************************************/
QDebug operator<<(QDebug dbg, const Object &obj)
{
dbg.nospace() << obj.objNum() << " " << obj.genNum() << " obj\n";
dbg.nospace() << obj.value();
if (obj.stream().length())
dbg.nospace() << "\n" << "stream length " << obj.stream().length();
else
dbg.nospace() << "\n" << "no stream";
dbg.nospace() << "\nendobj\n";
return dbg;
}