djvulibre: ddjvu_page_render() may render pages with some delay
[fbpdf.git] / mupdf.c
bloba45d4d5017c570d8c39786d17071aed38ecc75b6
1 #include <stdlib.h>
2 #include <string.h>
3 #include "fitz.h"
4 #include "draw.h"
5 #include "doc.h"
7 struct doc {
8 fz_context *ctx;
9 fz_document *pdf;
12 int doc_draw(struct doc *doc, fbval_t *bitmap, int p, int rows, int cols, int zoom, int rotate)
14 fz_matrix ctm;
15 fz_bbox bbox;
16 fz_pixmap *pix;
17 fz_device *dev;
18 fz_page *page;
19 fz_rect rect;
20 int h, w;
21 int x, y;
23 if (!(page = fz_load_page(doc->pdf, p - 1)))
24 return 1;
25 ctm = fz_scale((float) zoom / 10, (float) -zoom / 10);
26 ctm = fz_concat(ctm, fz_translate(0, -100));
27 if (rotate)
28 ctm = fz_concat(ctm, fz_rotate(rotate));
29 rect = fz_bound_page(doc->pdf, page);
30 rect = fz_transform_rect(ctm, rect);
31 bbox = fz_round_rect(rect);
32 w = MIN(cols, rect.x1 - rect.x0);
33 h = MIN(rows, rect.y1 - rect.y0);
35 pix = fz_new_pixmap_with_bbox(doc->ctx, fz_device_rgb, bbox);
36 fz_clear_pixmap_with_value(doc->ctx, pix, 0xff);
38 dev = fz_new_draw_device(doc->ctx, pix);
39 fz_run_page(doc->pdf, page, dev, ctm, NULL);
40 fz_free_device(dev);
42 for (y = 0; y < h; y++) {
43 int xs = (h - y - 1) * cols + (cols - w) / 2;
44 for (x = 0; x < w; x++) {
45 unsigned char *s = fz_pixmap_samples(doc->ctx, pix) +
46 y * fz_pixmap_width(doc->ctx, pix) * 4 + x * 4;
47 bitmap[xs + x] = FB_VAL(s[0], s[1], s[2]);
51 fz_drop_pixmap(doc->ctx, pix);
52 fz_free_page(doc->pdf, page);
53 return 0;
56 int doc_pages(struct doc *doc)
58 return fz_count_pages(doc->pdf);
61 struct doc *doc_open(char *path)
63 struct doc *doc = malloc(sizeof(*doc));
64 doc->ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
65 doc->pdf = fz_open_document(doc->ctx, path);
66 if (!doc->pdf || !fz_count_pages(doc->pdf)) {
67 free(doc);
68 return NULL;
70 return doc;
73 void doc_close(struct doc *doc)
75 fz_close_document(doc->pdf);
76 fz_free_context(doc->ctx);
77 free(doc);