mupdf: define a private MIN() macro
[fbpdf.git] / mupdf.c
blobe868dbc79fd02fe87fe474b1e526497089cedd59
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, fbval_t *bitmap, int p, int rows, int cols, int zoom, int rotate)
16 fz_matrix ctm;
17 fz_bbox bbox;
18 fz_pixmap *pix;
19 fz_device *dev;
20 fz_page *page;
21 fz_rect rect;
22 int h, w;
23 int x, y;
25 if (!(page = fz_load_page(doc->pdf, p - 1)))
26 return 1;
27 ctm = fz_scale((float) zoom / 10, (float) -zoom / 10);
28 ctm = fz_concat(ctm, fz_translate(0, -100));
29 if (rotate)
30 ctm = fz_concat(ctm, fz_rotate(rotate));
31 rect = fz_bound_page(doc->pdf, page);
32 rect = fz_transform_rect(ctm, rect);
33 bbox = fz_round_rect(rect);
34 w = MIN_(cols, rect.x1 - rect.x0);
35 h = MIN_(rows, rect.y1 - rect.y0);
37 pix = fz_new_pixmap_with_bbox(doc->ctx, fz_device_rgb, bbox);
38 fz_clear_pixmap_with_value(doc->ctx, pix, 0xff);
40 dev = fz_new_draw_device(doc->ctx, pix);
41 fz_run_page(doc->pdf, page, dev, ctm, NULL);
42 fz_free_device(dev);
44 for (y = 0; y < h; y++) {
45 int xs = (h - y - 1) * cols + (cols - w) / 2;
46 for (x = 0; x < w; x++) {
47 unsigned char *s = fz_pixmap_samples(doc->ctx, pix) +
48 y * fz_pixmap_width(doc->ctx, pix) * 4 + x * 4;
49 bitmap[xs + x] = FB_VAL(s[0], s[1], s[2]);
53 fz_drop_pixmap(doc->ctx, pix);
54 fz_free_page(doc->pdf, page);
55 return 0;
58 int doc_pages(struct doc *doc)
60 return fz_count_pages(doc->pdf);
63 struct doc *doc_open(char *path)
65 struct doc *doc = malloc(sizeof(*doc));
66 doc->ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
67 doc->pdf = fz_open_document(doc->ctx, path);
68 if (!doc->pdf || !fz_count_pages(doc->pdf)) {
69 free(doc);
70 return NULL;
72 return doc;
75 void doc_close(struct doc *doc)
77 fz_close_document(doc->pdf);
78 fz_free_context(doc->ctx);
79 free(doc);