-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_image_extractor.cpp
374 lines (336 loc) · 9.73 KB
/
pdf_image_extractor.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
#include "pdf_image_extractor.h"
#include <cstring>
#include <stdexcept>
#include <vector>
#include <map>
#include <pdfio.h>
#include "i_image_stream.h"
enum class StreamType
{
Unknown = 0,
FlateDecode = 1,
DCTDecode = 2,
};
enum class ColorSpace
{
Unknown = 0,
DeviceGray = 1,
DeviceRGB = 2,
};
class PdfImageZlibStream: public IImageStream
{
public:
PdfImageZlibStream(pdfio_obj_t* obj, unsigned int width, unsigned int height, ColorSpace colorSpace)
: m_obj(obj)
, m_width(width)
, m_height(height)
, m_colorSpace(colorSpace)
{}
unsigned int GetWidth() const override
{
return m_width;
}
unsigned int GetHeight() const override
{
return m_height;
}
void Extract(void* buffer) override
{
std::unique_ptr<pdfio_stream_t, decltype(&pdfioStreamClose)> streamHolder{ pdfioObjOpenStream(m_obj, true), pdfioStreamClose };
switch(m_colorSpace)
{
case ColorSpace::DeviceGray:
ExtractGray(streamHolder.get(), buffer);
break;
case ColorSpace::DeviceRGB:
ExtractRGB(streamHolder.get(), buffer);
break;
default:
throw std::runtime_error("Unknown color space");
}
}
private:
void ExtractGray(pdfio_stream_t* stream, void* buffer);
void ExtractRGB(pdfio_stream_t* stream, void* buffer);
private:
pdfio_obj_t* m_obj;
unsigned int m_width;
unsigned int m_height;
ColorSpace m_colorSpace;
};
void PdfImageZlibStream::ExtractGray(pdfio_stream_t* stream, void* buffer)
{
size_t expected_size = m_width * m_height;
size_t scanline = m_width;
uint8_t* buf = reinterpret_cast<uint8_t*>(buffer);
uint8_t* ptr = buf;
ssize_t cnt_read = 0;
size_t total_read = 0;
do
{
cnt_read = pdfioStreamRead(stream, ptr, scanline);
if (cnt_read > 0)
{
if (cnt_read != scanline)
{
throw std::runtime_error("Error reading image stream (bad stream)");
}
ptr += cnt_read;
total_read += cnt_read;
}
} while (cnt_read > 0 && expected_size > total_read);
if (total_read != expected_size)
{
throw std::runtime_error("Error reading image stream (bad stream)");
}
}
void PdfImageZlibStream::ExtractRGB(pdfio_stream_t* stream, void* buffer)
{
size_t expected_size = m_width * m_height;
size_t total_read = 0;
size_t scanline = 3 * m_width;
ssize_t cnt_read = 0;
uint8_t* ptr = reinterpret_cast<uint8_t*>(buffer);
std::unique_ptr<uint8_t[]> rgb(new uint8_t[scanline]);
do
{
cnt_read = pdfioStreamRead(stream, rgb.get(), scanline);
if (cnt_read > 0)
{
if (cnt_read != scanline)
{
throw std::runtime_error("Error reading image stream (bad stream)");
}
for (int count = 0; count < cnt_read/3; ++count)
{
*ptr = static_cast<uint8_t>(0.299 * rgb[3*count + 0] + 0.587 * rgb[3*count + 1] + 0.114 * rgb[3*count + 2]);
ptr++;
}
total_read += cnt_read;
}
} while(cnt_read > 0 && 3*expected_size > total_read);
}
class PdfImageExtractor: public IImageExtractor
{
public:
PdfImageExtractor(const char* file_name);
PdfImageExtractor(void* buffer, size_t buf_size);
virtual size_t GetImagesCount() const override
{
return images.size();
}
virtual IImageStream* GetImageStream(unsigned int index) const override
{
return images[index].get();
}
protected:
void Process();
void AddStream(std::unique_ptr<IImageStream> stream);
protected:
std::unique_ptr<pdfio_file_t, decltype(&pdfioFileClose)> pdf;
std::vector<std::unique_ptr<IImageStream>> images;
};
PdfImageExtractor::PdfImageExtractor(const char* file_name)
: pdf(pdfioFileOpen(file_name, nullptr, nullptr, nullptr, nullptr), pdfioFileClose)
{
if (!pdf)
{
throw std::runtime_error("Error opening PDF file");
}
Process();
}
PdfImageExtractor::PdfImageExtractor(void* buffer, size_t buf_size)
: pdf(pdfioMemoryFileOpen(buffer, buf_size, nullptr, nullptr, nullptr, nullptr), pdfioFileClose)
{
if (!pdf)
{
throw std::runtime_error("Error opening PDF file");
}
Process();
}
static StreamType GetStreamType(const char* str)
{
if (str)
{
if (0 == strcmp(str, "FlateDecode"))
{
return StreamType::FlateDecode;
}
else if (0 == strcmp(str, "DCTDecode"))
{
return StreamType::DCTDecode;
}
}
return StreamType::Unknown;
}
static StreamType GetStreamType(pdfio_dict_t* dict)
{
auto type = pdfioDictGetType(dict, "Filter");
if (type == PDFIO_VALTYPE_NAME)
{
return GetStreamType(pdfioDictGetName(dict, "Filter"));
}
else if (type == PDFIO_VALTYPE_ARRAY)
{
auto arr = pdfioDictGetArray(dict, "Filter");
if (1 == pdfioArrayGetSize(arr))
{
return GetStreamType(pdfioArrayGetName(arr, 0));
}
}
return StreamType::Unknown;
}
static ColorSpace GetColorSpace(const char* str)
{
if (str)
{
if (0 == strcmp(str, "DeviceGray"))
{
return ColorSpace::DeviceGray;
}
else if (0 == strcmp(str, "DeviceRGB"))
{
return ColorSpace::DeviceRGB;
}
}
return ColorSpace::Unknown;
}
static ColorSpace GetColorSpace(pdfio_dict_t* dict)
{
auto type = pdfioDictGetType(dict, "ColorSpace");
if (PDFIO_VALTYPE_NAME == type)
{
return GetColorSpace(pdfioDictGetName(dict, "ColorSpace"));
}
else if (PDFIO_VALTYPE_INDIRECT == type)
{
pdfio_obj_t* obj = pdfioDictGetObj(dict, "ColorSpace");
if (obj)
{
return GetColorSpace(pdfioObjGetName(obj));
}
}
return ColorSpace::Unknown;
}
struct ImageParams
{
int width;
int height;
int bpp;
StreamType streamType;
ColorSpace colorSpace;
ImageParams()
: width(0)
, height(0)
, bpp(0)
, streamType(StreamType::Unknown)
, colorSpace(ColorSpace::Unknown)
{}
};
static bool GetImageParams(pdfio_obj_t* obj, ImageParams& params)
{
const char* type = pdfioObjGetType(obj);
if (0 != strcmp(type, "XObject"))
return false;
const char* stype = pdfioObjGetSubtype(obj);
if (0 != strcmp(stype, "Image"))
return false;
pdfio_dict_t* dict = pdfioObjGetDict(obj);
if (!dict)
return false;
params.streamType = GetStreamType(dict);
params.colorSpace = GetColorSpace(dict);
params.width = static_cast<int>(pdfioDictGetNumber(dict, "Width"));
params.height = static_cast<int>(pdfioDictGetNumber(dict, "Height"));
params.bpp = static_cast<int>(pdfioDictGetNumber(dict, "BitsPerComponent"));
return true;
}
static pdfio_dict_t* GetResources(pdfio_obj_t* obj)
{
pdfio_dict_t* dict = pdfioObjGetDict(obj);
if (!dict)
return nullptr;
auto type = pdfioDictGetType(dict, "Resources");
if (PDFIO_VALTYPE_DICT == type)
{
return pdfioDictGetDict(dict, "Resources");
}
else if (PDFIO_VALTYPE_INDIRECT == type)
{
pdfio_obj_t* obj = pdfioDictGetObj(dict, "Resources");
if (obj)
return pdfioObjGetDict(obj);
}
return nullptr;
}
static bool PdfCallback(pdfio_dict_t* dict, const char* key, void* cb_data)
{
pdfio_obj_t* obj = pdfioDictGetObj(dict, key);
ImageParams imageParams;
if (!GetImageParams(obj, imageParams))
return true;
if (imageParams.width > 0 && imageParams.height > 0 && imageParams.bpp == 8 && StreamType::FlateDecode == imageParams.streamType)
{
auto* images = reinterpret_cast<std::map<pdfio_obj_t*, ImageParams>*>(cb_data);
images->insert(std::make_pair(obj, imageParams));
PdfImageExtractor* extractor = reinterpret_cast<PdfImageExtractor*>(cb_data);
}
return true;
}
static void CollectImages(pdfio_obj_t* obj, std::map<pdfio_obj_t*, ImageParams>& images)
{
pdfio_dict_t* resources = GetResources(obj);
if (!resources)
return;
pdfio_dict_t* xobject = pdfioDictGetDict(resources, "XObject");
if (!xobject)
return;
pdfioDictIterateKeys(xobject, PdfCallback, &images);
}
void PdfImageExtractor::Process()
{
std::map<pdfio_obj_t*, ImageParams> images;
pdfio_dict_t* catalog = pdfioFileGetCatalog(pdf.get());
if (!catalog)
return;
pdfio_obj_t* pages = pdfioDictGetObj(catalog, "Pages");
if (!pages)
return;
CollectImages(pages, images);
auto pagesCount = pdfioFileGetNumPages(pdf.get());
for (auto pageNo = 0u; pageNo < pagesCount; ++pageNo)
{
pdfio_obj_t* page = pdfioFileGetPage(pdf.get(), pageNo);
if (!page)
continue;
CollectImages(page, images);
}
for (auto image: images)
{
const ImageParams& imageParams = image.second;
switch(imageParams.streamType)
{
case StreamType::FlateDecode:
AddStream(std::unique_ptr<IImageStream>(
new PdfImageZlibStream(image.first,
imageParams.width,
imageParams.height,
imageParams.colorSpace)));
break;
default:
break;
}
}
}
void PdfImageExtractor::AddStream(std::unique_ptr<IImageStream> stream)
{
images.push_back(std::move(stream));
}
IImageExtractorPtr CreatePfdImageExtractor(const char* file_name)
{
return std::unique_ptr<IImageExtractor>(new PdfImageExtractor(file_name));
}
IImageExtractorPtr CreatePfdImageExtractor(void* buffer, size_t buf_size)
{
return std::unique_ptr<IImageExtractor>(new PdfImageExtractor(buffer, buf_size));
}