poppler: fix wrong parameter name in doc_draw()
[fbpdf.git] / mupdf.c
blob69ec37f517967b8122ad7693f2b66c148bfbada6
1 #include <stdlib.h>
2 #include <string.h>
3 #include "fitz.h"
4 #include "draw.h"
5 #include "doc.h"
7 #define MIN_(a, b) ((a) < (b) ? (a) : (b))
9 struct doc {
10 fz_context *ctx;
11 fz_document *pdf;
14 int doc_draw(struct doc *doc, int p, int zoom, int rotate,
15 fbval_t *bitmap, int *rows, int *cols)
17 fz_matrix ctm;
18 fz_bbox bbox;
19 fz_pixmap *pix;
20 fz_device *dev;
21 fz_page *page;
22 fz_rect rect;
23 int h, w;
24 int x, y;
26 if (!(page = fz_load_page(doc->pdf, p - 1)))
27 return 1;
28 ctm = fz_scale((float) zoom / 10, (float) -zoom / 10);
29 ctm = fz_concat(ctm, fz_translate(0, -100));
30 if (rotate)
31 ctm = fz_concat(ctm, fz_rotate(rotate));
32 rect = fz_bound_page(doc->pdf, page);
33 rect = fz_transform_rect(ctm, rect);
34 bbox = fz_round_rect(rect);
35 w = MIN_(*cols, rect.x1 - rect.x0);
36 h = MIN_(*rows, rect.y1 - rect.y0);
38 pix = fz_new_pixmap_with_bbox(doc->ctx, fz_device_rgb, bbox);
39 fz_clear_pixmap_with_value(doc->ctx, pix, 0xff);
41 dev = fz_new_draw_device(doc->ctx, pix);
42 fz_run_page(doc->pdf, page, dev, ctm, NULL);
43 fz_free_device(dev);
45 for (y = 0; y < h; y++) {
46 int xs = (h - y - 1) * *cols + (*cols - w) / 2;
47 for (x = 0; x < w; x++) {
48 unsigned char *s = fz_pixmap_samples(doc->ctx, pix) +
49 y * fz_pixmap_width(doc->ctx, pix) * 4 + x * 4;
50 bitmap[xs + x] = FB_VAL(s[0], s[1], s[2]);
54 fz_drop_pixmap(doc->ctx, pix);
55 fz_free_page(doc->pdf, page);
56 *cols = w;
57 *rows = h;
58 return 0;
61 int doc_pages(struct doc *doc)
63 return fz_count_pages(doc->pdf);
66 struct doc *doc_open(char *path)
68 struct doc *doc = malloc(sizeof(*doc));
69 doc->ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
70 doc->pdf = fz_open_document(doc->ctx, path);
71 if (!doc->pdf || !fz_count_pages(doc->pdf)) {
72 free(doc);
73 return NULL;
75 return doc;
78 void doc_close(struct doc *doc)
80 fz_close_document(doc->pdf);
81 fz_free_context(doc->ctx);
82 free(doc);