-
Notifications
You must be signed in to change notification settings - Fork 32
/
mupdf.c
67 lines (61 loc) · 1.48 KB
/
mupdf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdlib.h>
#include <string.h>
#include "mupdf/fitz.h"
#include "draw.h"
#include "doc.h"
#define MIN_(a, b) ((a) < (b) ? (a) : (b))
struct doc {
fz_context *ctx;
fz_document *pdf;
};
void *doc_draw(struct doc *doc, int p, int zoom, int rotate, int *rows, int *cols)
{
fz_matrix ctm;
fz_pixmap *pix;
fbval_t *pbuf;
int x, y;
ctm = fz_scale((float) zoom / 100, (float) zoom / 100);
ctm = fz_pre_rotate(ctm, rotate);
pix = fz_new_pixmap_from_page_number(doc->ctx, doc->pdf,
p - 1, ctm, fz_device_rgb(doc->ctx), 0);
if (!pix)
return NULL;
if (!(pbuf = malloc(pix->w * pix->h * sizeof(pbuf[0])))) {
fz_drop_pixmap(doc->ctx, pix);
return NULL;
}
for (y = 0; y < pix->h; y++) {
unsigned char *s = &pix->samples[y * pix->stride];
for (x = 0; x < pix->w; x++)
pbuf[y * pix->w + x] = FB_VAL(s[x * pix->n + 0],
s[x * pix->n + 1], s[x * pix->n + 2]);
}
fz_drop_pixmap(doc->ctx, pix);
*cols = pix->w;
*rows = pix->h;
return pbuf;
}
int doc_pages(struct doc *doc)
{
return fz_count_pages(doc->ctx, doc->pdf);
}
struct doc *doc_open(char *path)
{
struct doc *doc = malloc(sizeof(*doc));
doc->ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
fz_register_document_handlers(doc->ctx);
fz_try (doc->ctx) {
doc->pdf = fz_open_document(doc->ctx, path);
} fz_catch (doc->ctx) {
fz_drop_context(doc->ctx);
free(doc);
return NULL;
}
return doc;
}
void doc_close(struct doc *doc)
{
fz_drop_document(doc->ctx, doc->pdf);
fz_drop_context(doc->ctx);
free(doc);
}