3 #include <libdjvu/ddjvuapi.h>
7 #define MIN(a, b) ((a) < (b) ? (a) : (b))
11 ddjvu_document_t
*doc
;
14 int djvu_handle(struct doc
*doc
)
17 msg
= ddjvu_message_wait(doc
->ctx
);
18 while ((msg
= ddjvu_message_peek(doc
->ctx
))) {
19 if (msg
->m_any
.tag
== DDJVU_ERROR
) {
20 fprintf(stderr
,"ddjvu: %s\n", msg
->m_error
.message
);
23 ddjvu_message_pop(doc
->ctx
);
28 static void djvu_render(ddjvu_page_t
*page
, int mode
, void *bitmap
, int cols
,
29 ddjvu_rect_t
*prect
, ddjvu_rect_t
*rrect
)
32 fmt
= ddjvu_format_create(DDJVU_FORMAT_GREY8
, 0, 0);
33 ddjvu_format_set_row_order(fmt
, 1);
35 ddjvu_page_render(page
, mode
, prect
, rrect
, fmt
, cols
, bitmap
);
36 ddjvu_format_release(fmt
);
39 #define SIZE (1 << 13)
40 static char img
[SIZE
* SIZE
];
42 int doc_draw(struct doc
*doc
, fbval_t
*bitmap
, int p
, int rows
, int cols
,
47 ddjvu_pageinfo_t info
;
50 page
= ddjvu_page_create_by_pageno(doc
->doc
, p
- 1);
53 if (!ddjvu_page_decoding_done(page
))
57 ddjvu_document_get_pageinfo(doc
->doc
, p
- 1, &info
);
60 rect
.w
= ddjvu_page_get_width(page
);
61 rect
.h
= ddjvu_page_get_height(page
);
63 /* mode: DDJVU_RENDER_(BLACK|COLOR|BACKGROUND|FOREGROUND) */
64 djvu_render(page
, DDJVU_RENDER_FOREGROUND
, img
, SIZE
, &rect
, &rect
);
65 ddjvu_page_release(page
);
67 w
= MIN(cols
, rect
.w
* zoom
/ 10);
68 h
= MIN(cols
, rect
.h
* zoom
/ 10);
69 for (i
= 0; i
< h
; i
++) {
70 int xs
= i
* cols
+ (cols
- w
) / 2;
71 for (j
= 0; j
< w
; j
++)
72 bitmap
[xs
+ j
] = img
[i
* 10 / zoom
* SIZE
+ j
* 10 / zoom
];
77 int doc_pages(struct doc
*doc
)
79 return ddjvu_document_get_pagenum(doc
->doc
);
82 struct doc
*doc_open(char *path
)
84 struct doc
*doc
= malloc(sizeof(*doc
));
85 doc
->ctx
= ddjvu_context_create("fbpdf");
88 doc
->doc
= ddjvu_document_create_by_filename(doc
->ctx
, path
, 1);
91 if (!ddjvu_document_decoding_done(doc
->doc
))
100 void doc_close(struct doc
*doc
)
103 ddjvu_context_release(doc
->ctx
);
105 ddjvu_document_release(doc
->doc
);