example.c 3.4 KB

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