fbpdf.1: update and improve the manual page
[fbpdf.git] / mupdf.c
blob8559e7fc3359359ae44d1511f4c2eb00a51de1c1
1 #include <stdlib.h>
2 #include <string.h>
3 #include "mupdf/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 void *doc_draw(struct doc *doc, int p, int zoom, int rotate, int *rows, int *cols)
16 fz_matrix ctm;
17 fz_pixmap *pix;
18 fbval_t *pbuf;
19 int x, y;
20 ctm = fz_scale((float) zoom / 100, (float) zoom / 100);
21 ctm = fz_pre_rotate(ctm, rotate);
22 pix = fz_new_pixmap_from_page_number(doc->ctx, doc->pdf,
23 p - 1, ctm, fz_device_rgb(doc->ctx), 0);
24 if (!pix)
25 return NULL;
26 if (!(pbuf = malloc(pix->w * pix->h * sizeof(pbuf[0])))) {
27 fz_drop_pixmap(doc->ctx, pix);
28 return NULL;
30 for (y = 0; y < pix->h; y++) {
31 unsigned char *s = &pix->samples[y * pix->stride];
32 for (x = 0; x < pix->w; x++)
33 pbuf[y * pix->w + x] = FB_VAL(s[x * pix->n + 0],
34 s[x * pix->n + 1], s[x * pix->n + 2]);
36 fz_drop_pixmap(doc->ctx, pix);
37 *cols = pix->w;
38 *rows = pix->h;
39 return pbuf;
42 int doc_pages(struct doc *doc)
44 return fz_count_pages(doc->ctx, doc->pdf);
47 struct doc *doc_open(char *path)
49 struct doc *doc = malloc(sizeof(*doc));
50 doc->ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
51 fz_register_document_handlers(doc->ctx);
52 fz_try (doc->ctx) {
53 doc->pdf = fz_open_document(doc->ctx, path);
54 } fz_catch (doc->ctx) {
55 fz_drop_context(doc->ctx);
56 free(doc);
57 return NULL;
59 return doc;
62 void doc_close(struct doc *doc)
64 fz_drop_document(doc->ctx, doc->pdf);
65 fz_drop_context(doc->ctx);
66 free(doc);