static void writepixmap(fz_context *ctx, fz_pixmap *pix, char *file, int rgb) { char buf[1024]; fz_pixmap *converted = NULL; if (!pix) return; if (rgb && pix->colorspace && pix->colorspace != fz_device_rgb(ctx)) { fz_irect bbox; converted = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), fz_pixmap_bbox(ctx, pix, &bbox), pix->alpha); fz_convert_pixmap(ctx, converted, pix); pix = converted; } if (pix->n <= 4) { snprintf(buf, sizeof(buf), "%s.png", file); printf("extracting image %s\n", buf); fz_save_pixmap_as_png(ctx, pix, buf); } else { snprintf(buf, sizeof(buf), "%s.pam", file); printf("extracting image %s\n", buf); fz_save_pixmap_as_pam(ctx, pix, buf); } fz_drop_pixmap(ctx, converted); }
int main(int argc, char **argv) { char *filename = argc >= 2 ? argv[1] : ""; 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. for (i = 0; i < FZ_LOCK_MAX; i++) { if (pthread_mutex_init(&mutex[i], NULL) != 0) fail("pthread_mutex_init()"); } // Initialize the locking structure with function pointers to // the locking functions and to the user data. In this case // the user data is a pointer to the array of mutexes so the // locking functions can find the relevant lock to change when // they are called. This way we avoid global variables. locks.user = mutex; locks.lock = lock_mutex; locks.unlock = unlock_mutex; // This is the main threads context function, so supply the // locking structure. This context will be used to parse all // the pages from the document. ctx = fz_new_context(NULL, &locks, FZ_STORE_UNLIMITED); // Register default file types. fz_register_document_handlers(ctx); // 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! doc = fz_open_document(ctx, filename); // Retrieve the number of pages, which translates to the // number of threads used for rendering pages. 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. page = fz_load_page(ctx, doc, i); // Compute the bounding box for each page. 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. list = fz_new_display_list(ctx, &bbox); // Run the loaded page through a display list device // to populate the page's display list. dev = fz_new_list_device(ctx, list); fz_run_page(ctx, page, dev, &fz_identity, NULL); fz_close_device(ctx, dev); fz_drop_device(ctx, dev); // The page is no longer needed, all drawing commands // are now in the display list. fz_drop_page(ctx, page); // Create a white pixmap using the correct dimensions. pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), fz_round_rect(&rbox, &bbox), 0); fz_clear_pixmap_with_value(ctx, pix, 0xff); // Populate the data structure to be sent to the // rendering thread for this page. data = malloc(sizeof (struct data)); data->pagenumber = i + 1; data->ctx = ctx; data->list = list; data->bbox = bbox; data->pix = pix; // Create the thread and pass it the data structure. if (pthread_create(&thread[i], NULL, renderer, data) != 0) fail("pthread_create()"); } // Now each thread is rendering pages, so wait for each thread // to complete its rendering. fprintf(stderr, "joining %d threads...\n", threads); for (i = threads - 1; i >= 0; i--) { char filename[42]; struct data *data; if (pthread_join(thread[i], (void **) &data) != 0) fail("pthread_join"); sprintf(filename, "out%04d.png", i); fprintf(stderr, "\tSaving %s...\n", filename); // Write the rendered image to a PNG file fz_save_pixmap_as_png(ctx, data->pix, filename); // Free the thread's pixmap and display list since // they were allocated by the main thread above. fz_drop_pixmap(ctx, data->pix); fz_drop_display_list(ctx, data->list); // Free the data structured passed back and forth // between the main thread and rendering thread. free(data); } fprintf(stderr, "finally!\n"); fflush(NULL); free(thread); // Finally the document is closed and the main thread's // context is freed. fz_drop_document(ctx, doc); fz_drop_context(ctx); return 0; }
int main(int argc, char *argv[]) { fz_context *ctx; FILE *script = NULL; int c; while ((c = fz_getopt(argc, argv, "o:p:v")) != -1) { switch(c) { case 'o': output = fz_optarg; break; case 'p': prefix = fz_optarg; break; case 'v': verbosity ^= 1; break; default: usage(); break; } } if (fz_optind == argc) usage(); ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } pdfapp_init(ctx, &gapp); gapp.scrw = 640; gapp.scrh = 480; gapp.colorspace = fz_device_rgb(ctx); fz_try(ctx) { while (fz_optind < argc) { scriptname = argv[fz_optind++]; script = fopen(scriptname, "rb"); if (script == NULL) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open script: %s", scriptname); do { char *line = my_getline(script); if (line == NULL) continue; if (verbosity) fprintf(stderr, "'%s'\n", line); if (match(&line, "%")) { /* Comment */ } else if (match(&line, "PASSWORD")) { strcpy(pd_password, line); } else if (match(&line, "OPEN")) { char path[LONGLINE]; if (file_open) pdfapp_close(&gapp); if (prefix) { sprintf(path, "%s%s", prefix, line); } else { strcpy(path, line); } pdfapp_open(&gapp, path, 0); file_open = 1; } else if (match(&line, "GOTO")) { pdfapp_gotopage(&gapp, atoi(line)-1); } else if (match(&line, "SCREENSHOT")) { char text[1024]; sprintf(text, output, ++shotcount); if (strstr(text, ".pgm") || strstr(text, ".ppm") || strstr(text, ".pnm")) fz_save_pixmap_as_pnm(ctx, gapp.image, text); else fz_save_pixmap_as_png(ctx, gapp.image, text); } else if (match(&line, "RESIZE")) { int w, h; sscanf(line, "%d %d", &w, &h); pdfapp_onresize(&gapp, w, h); } else if (match(&line, "CLICK")) { float x, y, b; int n; n = sscanf(line, "%f %f %f", &x, &y, &b); if (n < 1) x = 0.0f; if (n < 2) y = 0.0f; if (n < 3) b = 1; /* state = 1 = transition down */ pdfapp_onmouse(&gapp, (int)x, (int)y, b, 0, 1); /* state = -1 = transition up */ pdfapp_onmouse(&gapp, (int)x, (int)y, b, 0, -1); } else if (match(&line, "TEXT")) { unescape_string(td_textinput, line); } else { fprintf(stderr, "Ignoring line without script statement.\n"); } } while (!feof(script)); fclose(script); } } fz_catch(ctx) { fprintf(stderr, "error: cannot execute '%s'\n", scriptname); } if (file_open) pdfapp_close(&gapp); fz_drop_context(ctx); return 0; }