comparison mupdf-source/docs/examples/example.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*
2 How to use MuPDF to render a single page and print the result as
3 a PPM to stdout.
4
5 To build this example in a source tree and render first page at
6 100% zoom with no rotation, run:
7 make examples
8 ./build/debug/example document.pdf 1 100 0 > page1.ppm
9
10 To build from installed sources, and render the same document, run:
11 gcc -I/usr/local/include -o example \
12 /usr/local/share/doc/mupdf/examples/example.c \
13 /usr/local/lib/libmupdf.a \
14 /usr/local/lib/libmupdfthird.a \
15 -lm
16 ./example document.pdf 1 100 0 > page1.ppm
17 */
18
19 #include <mupdf/fitz.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 int main(int argc, char **argv)
25 {
26 char *input;
27 float zoom, rotate;
28 int page_number, page_count;
29 fz_context *ctx;
30 fz_document *doc;
31 fz_pixmap *pix;
32 fz_matrix ctm;
33 int x, y;
34
35 if (argc < 3)
36 {
37 fprintf(stderr, "usage: example input-file page-number [ zoom [ rotate ] ]\n");
38 fprintf(stderr, "\tinput-file: path of PDF, XPS, CBZ or EPUB document to open\n");
39 fprintf(stderr, "\tPage numbering starts from one.\n");
40 fprintf(stderr, "\tZoom level is in percent (100 percent is 72 dpi).\n");
41 fprintf(stderr, "\tRotation is in degrees clockwise.\n");
42 return EXIT_FAILURE;
43 }
44
45 input = argv[1];
46 page_number = atoi(argv[2]) - 1;
47 zoom = argc > 3 ? atof(argv[3]) : 100;
48 rotate = argc > 4 ? atof(argv[4]) : 0;
49
50 /* Create a context to hold the exception stack and various caches. */
51 ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
52 if (!ctx)
53 {
54 fprintf(stderr, "cannot create mupdf context\n");
55 return EXIT_FAILURE;
56 }
57
58 /* Register the default file types to handle. */
59 fz_try(ctx)
60 fz_register_document_handlers(ctx);
61 fz_catch(ctx)
62 {
63 fz_report_error(ctx);
64 fprintf(stderr, "cannot register document handlers\n");
65 fz_drop_context(ctx);
66 return EXIT_FAILURE;
67 }
68
69 /* Open the document. */
70 fz_try(ctx)
71 doc = fz_open_document(ctx, input);
72 fz_catch(ctx)
73 {
74 fz_report_error(ctx);
75 fprintf(stderr, "cannot open document\n");
76 fz_drop_context(ctx);
77 return EXIT_FAILURE;
78 }
79
80 /* Count the number of pages. */
81 fz_try(ctx)
82 page_count = fz_count_pages(ctx, doc);
83 fz_catch(ctx)
84 {
85 fz_report_error(ctx);
86 fprintf(stderr, "cannot count number of pages\n");
87 fz_drop_document(ctx, doc);
88 fz_drop_context(ctx);
89 return EXIT_FAILURE;
90 }
91
92 if (page_number < 0 || page_number >= page_count)
93 {
94 fprintf(stderr, "page number out of range: %d (page count %d)\n", page_number + 1, page_count);
95 fz_drop_document(ctx, doc);
96 fz_drop_context(ctx);
97 return EXIT_FAILURE;
98 }
99
100 /* Compute a transformation matrix for the zoom and rotation desired. */
101 /* The default resolution without scaling is 72 dpi. */
102 ctm = fz_scale(zoom / 100, zoom / 100);
103 ctm = fz_pre_rotate(ctm, rotate);
104
105 /* Render page to an RGB pixmap. */
106 fz_try(ctx)
107 pix = fz_new_pixmap_from_page_number(ctx, doc, page_number, ctm, fz_device_rgb(ctx), 0);
108 fz_catch(ctx)
109 {
110 fz_report_error(ctx);
111 fprintf(stderr, "cannot render page\n");
112 fz_drop_document(ctx, doc);
113 fz_drop_context(ctx);
114 return EXIT_FAILURE;
115 }
116
117 /* Print image data in ascii PPM format. */
118 printf("P3\n");
119 printf("%d %d\n", pix->w, pix->h);
120 printf("255\n");
121 for (y = 0; y < pix->h; ++y)
122 {
123 unsigned char *p = &pix->samples[y * pix->stride];
124 for (x = 0; x < pix->w; ++x)
125 {
126 if (x > 0)
127 printf(" ");
128 printf("%3d %3d %3d", p[0], p[1], p[2]);
129 p += pix->n;
130 }
131 printf("\n");
132 }
133
134 /* Clean up. */
135 fz_drop_pixmap(ctx, pix);
136 fz_drop_document(ctx, doc);
137 fz_drop_context(ctx);
138 return EXIT_SUCCESS;
139 }