summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2015-04-08 12:24:14 +0200
committerRobin Watts <robin.watts@artifex.com>2015-04-08 11:29:56 +0100
commit294b5b63c568cfb2f4a56fb51e27e218c6ce89b8 (patch)
tree204cdaf2f2fd3b2c6b44939f9485f7036038acae /docs
parent7c3520a0849a51ec6e1c754c5a295dc981eea339 (diff)
downloadmupdf-294b5b63c568cfb2f4a56fb51e27e218c6ce89b8.tar.xz
Add docs/example.c and docs/multi-threaded.c to 'make examples' target.
Also update the examples for the recent API changes and make them build under MSVC.
Diffstat (limited to 'docs')
-rw-r--r--docs/example.c38
-rw-r--r--docs/multi-threaded.c50
2 files changed, 53 insertions, 35 deletions
diff --git a/docs/example.c b/docs/example.c
index bf04c805..71506c9e 100644
--- a/docs/example.c
+++ b/docs/example.c
@@ -17,9 +17,19 @@
void
render(char *filename, int pagenumber, int zoom, int rotation)
{
+ fz_context *ctx;
+ fz_document *doc;
+ int pagecount;
+ fz_page *page;
+ fz_matrix transform;
+ fz_rect bounds;
+ fz_irect bbox;
+ fz_pixmap *pix;
+ fz_device *dev;
+
// Create a context to hold the exception stack and various caches.
- fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
+ ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
// Register the default file types.
@@ -27,29 +37,27 @@ render(char *filename, int pagenumber, int zoom, int rotation)
// Open the PDF, XPS or CBZ document.
- fz_document *doc = fz_open_document(ctx, filename);
+ doc = fz_open_document(ctx, filename);
// Retrieve the number of pages (not used in this example).
- int pagecount = fz_count_pages(doc);
+ pagecount = fz_count_pages(ctx, doc);
// Load the page we want. Page numbering starts from zero.
- fz_page *page = fz_load_page(doc, pagenumber - 1);
+ page = fz_load_page(ctx, doc, pagenumber - 1);
// Calculate a transform to use when rendering. This transform
// contains the scale and rotation. Convert zoom percentage to a
// scaling factor. Without scaling the resolution is 72 dpi.
- fz_matrix transform;
fz_rotate(&transform, rotation);
fz_pre_scale(&transform, zoom / 100.0f, zoom / 100.0f);
// Take the page bounds and transform them by the same matrix that
// we will use to render the page.
- fz_rect bounds;
- fz_bound_page(doc, page, &bounds);
+ fz_bound_page(ctx, page, &bounds);
fz_transform_rect(&bounds, &transform);
// Create a blank pixmap to hold the result of rendering. The
@@ -58,9 +66,8 @@ render(char *filename, int pagenumber, int zoom, int rotation)
// space has the origin at the top left corner and the x axis
// extends to the right and the y axis extends down.
- fz_irect bbox;
fz_round_rect(&bbox, &bounds);
- fz_pixmap *pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), &bbox);
+ pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), &bbox);
fz_clear_pixmap_with_value(ctx, pix, 0xff);
// A page consists of a series of objects (text, line art, images,
@@ -81,9 +88,9 @@ render(char *filename, int pagenumber, int zoom, int rotation)
// Create a draw device with the pixmap as its target.
// Run the page with the transform.
- fz_device *dev = fz_new_draw_device(ctx, pix);
- fz_run_page(doc, page, dev, &transform, NULL);
- fz_free_device(dev);
+ dev = fz_new_draw_device(ctx, pix);
+ fz_run_page(ctx, page, dev, &transform, NULL);
+ fz_drop_device(ctx, dev);
// Save the pixmap to a file.
@@ -92,9 +99,9 @@ render(char *filename, int pagenumber, int zoom, int rotation)
// Clean up.
fz_drop_pixmap(ctx, pix);
- fz_free_page(doc, page);
- fz_drop_document(doc);
- fz_free_context(ctx);
+ fz_drop_page(ctx, page);
+ fz_drop_document(ctx, doc);
+ fz_drop_context(ctx);
}
int main(int argc, char **argv)
@@ -105,5 +112,6 @@ int main(int argc, char **argv)
int rotation = argc > 4 ? atoi(argv[4]) : 0;
render(filename, pagenumber, zoom, rotation);
+
return 0;
}
diff --git a/docs/multi-threaded.c b/docs/multi-threaded.c
index c5df1f4e..f0432b07 100644
--- a/docs/multi-threaded.c
+++ b/docs/multi-threaded.c
@@ -82,6 +82,7 @@ renderer(void *data)
fz_display_list *list = ((struct data *) data)->list;
fz_rect bbox = ((struct data *) data)->bbox;
fz_pixmap *pix = ((struct data *) data)->pix;
+ fz_device *dev;
fprintf(stderr, "thread at page %d loading!\n", pagenumber);
@@ -95,13 +96,13 @@ renderer(void *data)
// will render the request area of the page to the pixmap.
fprintf(stderr, "thread at page %d rendering!\n", pagenumber);
- fz_device *dev = fz_new_draw_device(ctx, pix);
- fz_run_display_list(list, dev, &fz_identity, &bbox, NULL);
- fz_free_device(dev);
+ dev = fz_new_draw_device(ctx, pix);
+ fz_run_display_list(ctx, list, dev, &fz_identity, &bbox, NULL);
+ fz_drop_device(ctx, dev);
// This threads context is freed.
- fz_free_context(ctx);
+ fz_drop_context(ctx);
fprintf(stderr, "thread at page %d done!\n", pagenumber);
@@ -135,6 +136,9 @@ int main(int argc, char **argv)
pthread_t *thread = NULL;
fz_locks_context locks;
pthread_mutex_t mutex[FZ_LOCK_MAX];
+ fz_context *ctx;
+ fz_document *doc;
+ int threads;
int i;
// Initialize FZ_LOCK_MAX number of non-recursive mutexes.
@@ -159,7 +163,7 @@ int main(int argc, char **argv)
// locking structure. This context will be used to parse all
// the pages from the document.
- fz_context *ctx = fz_new_context(NULL, &locks, FZ_STORE_UNLIMITED);
+ ctx = fz_new_context(NULL, &locks, FZ_STORE_UNLIMITED);
// Register default file types.
@@ -168,58 +172,64 @@ int main(int argc, char **argv)
// Open the PDF, XPS or CBZ document. Note, this binds doc to ctx.
// You must only ever use doc with ctx - never a clone of it!
- fz_document *doc = fz_open_document(ctx, filename);
+ doc = fz_open_document(ctx, filename);
// Retrieve the number of pages, which translates to the
// number of threads used for rendering pages.
- int threads = fz_count_pages(doc);
+ threads = fz_count_pages(ctx, doc);
fprintf(stderr, "spawning %d threads, one per page...\n", threads);
thread = malloc(threads * sizeof (pthread_t));
for (i = 0; i < threads; i++)
{
+ fz_page *page;
+ fz_rect bbox;
+ fz_irect rbox;
+ fz_display_list *list;
+ fz_device *dev;
+ fz_pixmap *pix;
+ struct data *data;
+
// Load the relevant page for each thread. Note, that this
// cannot be done on the worker threads, as each use of doc
// uses ctx, and only one thread can be using ctx at a time.
- fz_page *page = fz_load_page(doc, i);
+ page = fz_load_page(ctx, doc, i);
// Compute the bounding box for each page.
- fz_rect bbox;
- fz_irect rbox;
- fz_bound_page(doc, page, &bbox);
+ fz_bound_page(ctx, page, &bbox);
// Create a display list that will hold the drawing
// commands for the page. Once we have the display list
// this can safely be used on any other thread as it is
// not bound to a given context.
- fz_display_list *list = fz_new_display_list(ctx);
+ list = fz_new_display_list(ctx);
// Run the loaded page through a display list device
// to populate the page's display list.
- fz_device *dev = fz_new_list_device(ctx, list);
- fz_run_page(doc, page, dev, &fz_identity, NULL);
- fz_free_device(dev);
+ dev = fz_new_list_device(ctx, list);
+ fz_run_page(ctx, page, dev, &fz_identity, NULL);
+ fz_drop_device(ctx, dev);
// The page is no longer needed, all drawing commands
// are now in the display list.
- fz_free_page(doc, page);
+ fz_drop_page(ctx, page);
// Create a white pixmap using the correct dimensions.
- fz_pixmap *pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), fz_round_rect(&rbox, &bbox));
+ pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), fz_round_rect(&rbox, &bbox));
fz_clear_pixmap_with_value(ctx, pix, 0xff);
// Populate the data structure to be sent to the
// rendering thread for this page.
- struct data *data = malloc(sizeof (struct data));
+ data = malloc(sizeof (struct data));
data->pagenumber = i + 1;
data->ctx = ctx;
@@ -272,8 +282,8 @@ int main(int argc, char **argv)
// Finally the document is closed and the main thread's
// context is freed.
- fz_drop_document(doc);
- fz_free_context(ctx);
+ fz_drop_document(ctx, doc);
+ fz_drop_context(ctx);
return 0;
}