mupdf: support version 1.0rc1
[fbpdf.git] / djvulibre.c
blob427e038010457c0cff9929678b2494ade64bfbc1
1 #include <stdlib.h>
2 #include <string.h>
3 #include <libdjvu/ddjvuapi.h>
4 #include "draw.h"
5 #include "doc.h"
7 #define MIN(a, b) ((a) < (b) ? (a) : (b))
9 struct doc {
10 ddjvu_context_t *ctx;
11 ddjvu_document_t *doc;
14 int djvu_handle(struct doc *doc)
16 ddjvu_message_t *msg;
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);
21 return 1;
23 ddjvu_message_pop(doc->ctx);
25 return 0;
28 static void djvu_render(ddjvu_page_t *page, int mode, void *bitmap, int cols,
29 ddjvu_rect_t *prect, ddjvu_rect_t *rrect)
31 ddjvu_format_t *fmt;
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,
43 int zoom, int rotate)
45 ddjvu_page_t *page;
46 ddjvu_rect_t rect;
47 ddjvu_pageinfo_t info;
48 int iw, ih;
49 int i, j;
50 page = ddjvu_page_create_by_pageno(doc->doc, p - 1);
51 if (!page)
52 return -1;
53 if (!ddjvu_page_decoding_done(page))
54 if (djvu_handle(doc))
55 return -1;
57 ddjvu_document_get_pageinfo(doc->doc, p - 1, &info);
58 iw = ddjvu_page_get_width(page);
59 ih = ddjvu_page_get_height(page);
60 rect.x = 0;
61 rect.y = 0;
62 rect.w = iw;
63 rect.h = ih;
65 /* mode: DDJVU_RENDER_(BLACK|COLOR|BACKGROUND|FOREGROUND) */
66 djvu_render(page, DDJVU_RENDER_FOREGROUND, img, SIZE, &rect, &rect);
67 ddjvu_page_release(page);
68 zoom /= 4;
69 for (i = 0; i < ih * zoom / 10; i++)
70 for (j = 0; j < iw * zoom / 10; j++)
71 bitmap[i * cols + j] = img[i * 10 / zoom * SIZE + j * 10 / zoom];
72 return 0;
75 int doc_pages(struct doc *doc)
77 return ddjvu_document_get_pagenum(doc->doc);
80 struct doc *doc_open(char *path)
82 struct doc *doc = malloc(sizeof(*doc));
83 doc->ctx = ddjvu_context_create("fbpdf");
84 if (!doc->ctx)
85 goto fail;
86 doc->doc = ddjvu_document_create_by_filename(doc->ctx, path, 1);
87 if (!doc->doc)
88 goto fail;
89 if (!ddjvu_document_decoding_done(doc->doc))
90 if (djvu_handle(doc))
91 goto fail;
92 return doc;
93 fail:
94 doc_close(doc);
95 return NULL;
98 void doc_close(struct doc *doc)
100 if (doc->doc)
101 ddjvu_context_release(doc->ctx);
102 if (doc->ctx)
103 ddjvu_document_release(doc->doc);
104 free(doc);