12
#include <poppler-global.h>
13
#include <poppler-rectangle.h>
14
#include <poppler-document.h>
15
#include <poppler-page.h>
16
#include <poppler-page-renderer.h>
26
#define DATA32 unsigned int
32
#define D(fmt, args...) fprintf(stderr, fmt, ## args)
34
#define D(fmt, args...)
37
using namespace poppler;
43
int width = 0, height = 0;
44
int crop_width = 0, crop_height = 0;
50
Eina_Bool poppler_init(const char *file, int page_nbr, int size_w, int size_h)
64
doc = document::load_from_file(file);
69
if (page_nbr >= doc->pages())
74
doc_page = doc->create_page(page_nbr);
78
w = doc_page->page_rect(media_box).width();
79
h = doc_page->page_rect(media_box).height();
80
cw = doc_page->page_rect().width();
81
ch = doc_page->page_rect().height();
86
if ((size_w > 0) || (size_h > 0))
88
double w2 = cw, h2 = ch;
91
h2 = (size_w * ch) / cw;
95
w2 = (size_h * cw) / ch;
97
D("XXXXXXXXXXXXXXXXXXXXx %3.3fx%3.3f\n", w2, h2);
98
if (w2 > h2) dpi = (w2 * DEF_DPI) / cw;
99
else dpi = (h2 * DEF_DPI) / ch;
104
cw = (cw * dpi) / DEF_DPI;
105
ch = (ch * dpi) / DEF_DPI;
106
w = (w * dpi) / DEF_DPI;
107
h = (h * dpi) / DEF_DPI;
122
void poppler_shutdown()
129
void poppler_load_image(int size_w EINA_UNUSED, int size_h EINA_UNUSED)
131
page_renderer *renderer;
136
if (dpi <= 0.0) dpi = DEF_DPI;
138
renderer = new page_renderer();
140
renderer->set_render_hint(page_renderer::text_antialiasing, 1);
141
renderer->set_render_hint(page_renderer::antialiasing, 1);
143
out = renderer->render_page(doc_page, dpi, dpi,
147
shm_alloc(crop_width * crop_height * sizeof(DATA32));
148
if (!shm_addr) goto end;
150
dst = (DATA32 *)data;
152
#define IMAGE_PIXEL_ITERATOR \
153
for (y = 0; y < crop_height; y++) \
154
for (x = 0; x < crop_width; x++)
156
#define ARGB_JOIN(a,r,g,b) \
157
(((a) << 24) + ((r) << 16) + ((g) << 8) + (b))
159
if (out.format() == image::format_mono)
165
for (y = 0; y < crop_height; y++)
167
src = (uint8_t *) out.data() + y * crop_width;
168
for (x = 0; x < crop_width; x++)
171
int c = (*src & (1 << bit)) ? 0xFF : 0x00;
172
*dst++ = ARGB_JOIN(0xFF, c, c, c);
173
if ((x & 0x7) == 0x7) src++;
177
if (out.format() == image::format_rgb24)
180
src = (RGB24*) out.data();
183
int pos = x+y*crop_width;
184
dst[pos] = ARGB_JOIN(0xFF, src[pos][0], src[pos][1], src[pos][2]);
187
else if (out.format() == image::format_argb32)
191
src = (DATA32*) out.data();
200
static Eina_Tmpstr *tmpdir = NULL;
201
static Eina_Tmpstr *generated = NULL;
212
if (rmdir(tmpdir) < 0)
214
D("Failed to delete tmpdir %s\n", tmpdir);
216
eina_tmpstr_del(tmpdir);
220
eina_tmpstr_del(generated);
235
main(int argc, char **argv)
241
int size_w = 0, size_h = 0;
246
if (argc < 2) return -1;
249
for (i = 2; i < argc; i++)
251
if (!strcmp(argv[i], "-head"))
254
else if (!strcmp(argv[i], "-key"))
257
page_num = atoi(argv[i]);
260
else if (!strcmp(argv[i], "-opt-scale-down-by"))
265
else if (!strcmp(argv[i], "-opt-dpi"))
268
dpi = ((double)atoi(argv[i])) / 1000.0;
271
else if (!strcmp(argv[i], "-opt-size"))
274
size_w = atoi(argv[i]);
276
size_h = atoi(argv[i]);
280
D("dpi....: %f\n", dpi);
281
D("page...: %d\n", page_num);
284
extension = strrchr(file, '.');
285
dir = dirname(argv[0]);
286
if (extension && dir && (!(!strcasecmp(extension, ".pdf"))))
289
signal(SIGSEGV, _crash);
290
signal(SIGBUS, _crash);
291
signal(SIGFPE, _crash);
292
signal(SIGABRT, _crash);
293
signal(SIGILL, _crash);
294
signal(SIGSYS, _crash);
295
signal(SIGINT, _crash);
296
signal(SIGTERM, _crash);
297
signal(SIGQUIT, _crash);
299
timeout_func_set(tmp_cleanup);
300
if (eina_file_mkdtemp("evas_generic_pdf_loaderXXXXXX", &tmpdir))
305
tmp = eina_strbuf_new();
306
eina_strbuf_append_printf(tmp, "%s/evas_generic_pdf_loader.%s '%s' %s", dir, extension + 1, file, tmpdir);
308
cmd = popen(eina_strbuf_string_get(tmp), "r");
309
D("running preprocessing process '%s'...\n", eina_strbuf_string_get(tmp));
310
eina_strbuf_reset(tmp);
315
const char *filename;
318
while (fgets(buf, sizeof (buf), cmd))
322
filename = basename(file);
324
generated = eina_tmpstr_add_length(filename, strlen(filename) - strlen(extension));
326
eina_strbuf_append_printf(tmp, "%s/%s.pdf", tmpdir, generated);
328
eina_tmpstr_del(generated);
331
if (stat(eina_strbuf_string_get(tmp), &st) == 0)
333
generated = eina_tmpstr_add_length(eina_strbuf_string_get(tmp),
334
eina_strbuf_length_get(tmp));
335
file = (char*) generated;
339
D("generated file: '%s'\n", generated);
340
eina_strbuf_free(tmp);
348
D("poppler_file_init\n");
349
if (!poppler_init(file, page_num, size_w, size_h))
354
D("poppler_file_init done\n");
356
D("dpi2...: %f\n", dpi);
359
poppler_load_image(size_w, size_h);
362
D("size...: %ix%i\n", width, height);
363
D("crop...: %ix%i\n", crop_width, crop_height);
366
printf("size %i %i\n", crop_width, crop_height);
376
printf("shmfile %s\n", shmfile);
382
if (fwrite(data, crop_width * crop_height * sizeof(DATA32), 1, stdout) != 1)
386
D("fwrite failed (%d bytes): %m\n", crop_width * crop_height * sizeof(DATA32));
396
timeout_func_set(NULL);