static void cbz_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) { fz_cbz_writer *wri = (fz_cbz_writer*)wri_; fz_buffer *buffer; char name[40]; fz_try(ctx) fz_close_device(ctx, dev); fz_always(ctx) fz_drop_device(ctx, dev); fz_catch(ctx) fz_rethrow(ctx); wri->count += 1; fz_snprintf(name, sizeof name, "p%04d.png", wri->count); buffer = fz_new_buffer_from_pixmap_as_png(ctx, wri->pixmap, NULL); fz_try(ctx) fz_write_zip_entry(ctx, wri->zip, name, buffer, 0); fz_always(ctx) fz_drop_buffer(ctx, buffer); fz_catch(ctx) fz_rethrow(ctx); fz_drop_pixmap(ctx, wri->pixmap); wri->pixmap = NULL; }
fz_pixmap * fz_new_pixmap_from_annot(fz_context *ctx, fz_annot *annot, const fz_matrix *ctm, fz_colorspace *cs, int alpha) { fz_rect rect; fz_irect irect; fz_pixmap *pix; fz_device *dev; fz_bound_annot(ctx, annot, &rect); fz_transform_rect(&rect, ctm); fz_round_rect(&irect, &rect); pix = fz_new_pixmap_with_bbox(ctx, cs, &irect, alpha); if (alpha) fz_clear_pixmap(ctx, pix); else fz_clear_pixmap_with_value(ctx, pix, 0xFF); fz_try(ctx) { dev = fz_new_draw_device(ctx, ctm, pix); fz_run_annot(ctx, annot, dev, &fz_identity, NULL); } fz_always(ctx) { fz_drop_device(ctx, dev); } fz_catch(ctx) { fz_drop_pixmap(ctx, pix); fz_rethrow(ctx); } return pix; }
fz_stext_page * fz_new_stext_page_from_page(fz_context *ctx, fz_page *page, fz_stext_sheet *sheet) { fz_stext_page *text; fz_device *dev; fz_rect mediabox; if (page == NULL) return NULL; text = fz_new_stext_page(ctx, fz_bound_page(ctx, page, &mediabox)); fz_try(ctx) { dev = fz_new_stext_device(ctx, sheet, text); fz_run_page(ctx, page, dev, &fz_identity, NULL); fz_close_device(ctx, dev); } fz_always(ctx) { fz_drop_device(ctx, dev); } fz_catch(ctx) { fz_drop_stext_page(ctx, text); fz_rethrow(ctx); } return text; }
static void text_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) { fz_text_writer *wri = (fz_text_writer*)wri_; fz_close_device(ctx, dev); fz_drop_device(ctx, dev); switch (wri->format) { default: case FZ_FORMAT_TEXT: fz_print_stext_page_as_text(ctx, wri->out, wri->page); break; case FZ_FORMAT_HTML: fz_print_stext_page_as_html(ctx, wri->out, wri->page); break; case FZ_FORMAT_XHTML: fz_print_stext_page_as_xhtml(ctx, wri->out, wri->page); break; case FZ_FORMAT_STEXT: fz_print_stext_page_as_xml(ctx, wri->out, wri->page); break; } fz_drop_stext_page(ctx, wri->page); wri->page = NULL; }
fz_pixmap * fz_new_pixmap_from_page_contents(fz_context *ctx, fz_page *page, const fz_matrix *ctm, fz_colorspace *cs) { fz_rect rect; fz_irect irect; fz_pixmap *pix; fz_device *dev; fz_bound_page(ctx, page, &rect); fz_transform_rect(&rect, ctm); fz_round_rect(&irect, &rect); pix = fz_new_pixmap_with_bbox(ctx, cs, &irect); fz_clear_pixmap_with_value(ctx, pix, 0xFF); fz_try(ctx) { dev = fz_new_draw_device(ctx, pix); fz_run_page_contents(ctx, page, dev, ctm, NULL); } fz_always(ctx) { fz_drop_device(ctx, dev); } fz_catch(ctx) { fz_drop_pixmap(ctx, pix); fz_rethrow(ctx); } return pix; }
static void pwg_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) { fz_pwg_writer *wri = (fz_pwg_writer*)wri_; fz_close_device(ctx, dev); fz_drop_device(ctx, dev); if (wri->mono) { fz_bitmap *bitmap = fz_new_bitmap_from_pixmap(ctx, wri->pixmap, NULL); fz_try(ctx) fz_write_bitmap_as_pwg_page(ctx, wri->out, bitmap, &wri->pwg); fz_always(ctx) fz_drop_bitmap(ctx, bitmap); fz_catch(ctx) fz_rethrow(ctx); } else { fz_write_pixmap_as_pwg_page(ctx, wri->out, wri->pixmap, &wri->pwg); } fz_drop_pixmap(ctx, wri->pixmap); wri->pixmap = NULL; }
fz_pixmap * fz_new_pixmap_from_display_list(fz_context *ctx, fz_display_list *list, const fz_matrix *ctm, fz_colorspace *cs, int background) { fz_rect rect; fz_irect irect; fz_pixmap *pix; fz_device *dev; fz_bound_display_list(ctx, list, &rect); fz_transform_rect(&rect, ctm); fz_round_rect(&irect, &rect); pix = fz_new_pixmap_with_bbox(ctx, cs, &irect); if (background) fz_clear_pixmap_with_value(ctx, pix, 0xFF); else fz_clear_pixmap(ctx, pix); fz_try(ctx) { dev = fz_new_draw_device(ctx, pix); fz_run_display_list(ctx, list, dev, ctm, NULL, NULL); } fz_always(ctx) { fz_drop_device(ctx, dev); } fz_catch(ctx) { fz_drop_pixmap(ctx, pix); fz_rethrow(ctx); } return pix; }
fz_display_list * fz_new_display_list_from_annot(fz_context *ctx, fz_annot *annot) { fz_display_list *list; fz_rect bounds; fz_device *dev; list = fz_new_display_list(ctx, fz_bound_annot(ctx, annot, &bounds)); fz_try(ctx) { dev = fz_new_list_device(ctx, list); fz_run_annot(ctx, annot, dev, &fz_identity, NULL); fz_close_device(ctx, dev); } fz_always(ctx) { fz_drop_device(ctx, dev); } fz_catch(ctx) { fz_drop_display_list(ctx, list); fz_rethrow(ctx); } return list; }
fz_display_list * fz_new_display_list_from_page(fz_context *ctx, fz_page *page) { fz_display_list *list; fz_device *dev; list = fz_new_display_list(ctx); fz_try(ctx) { dev = fz_new_list_device(ctx, list); fz_run_page(ctx, page, dev, &fz_identity, NULL); } fz_always(ctx) { fz_drop_device(ctx, dev); } fz_catch(ctx) { fz_drop_display_list(ctx, list); fz_rethrow(ctx); } return list; }
void * renderer(void *data) { int pagenumber = ((struct data *) data)->pagenumber; fz_context *ctx = ((struct data *) data)->ctx; 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); // The context pointer is pointing to the main thread's // context, so here we create a new context based on it for // use in this thread. ctx = fz_clone_context(ctx); // Next we run the display list through the draw device which // will render the request area of the page to the pixmap. fprintf(stderr, "thread at page %d rendering!\n", pagenumber); dev = fz_new_draw_device(ctx, &fz_identity, pix); fz_run_display_list(ctx, list, dev, &fz_identity, &bbox, NULL); fz_close_device(ctx, dev); fz_drop_device(ctx, dev); // This threads context is freed. fz_drop_context(ctx); fprintf(stderr, "thread at page %d done!\n", pagenumber); return data; }
void clear() { if (m_dev) { fz_drop_device(m_ctx, m_dev); m_dev = 0; } if (m_list) { fz_drop_display_list(m_ctx, m_list); m_list = 0; } }
Viewer::~Viewer() { if (dev) { fz_drop_device(ctx, dev); } if (pix) { fz_drop_pixmap(ctx, pix); } if (page) { fz_drop_page(ctx, page); } if (doc) { fz_drop_document(ctx, doc); } if (ctx) { fz_drop_context(ctx); } }
void PDFDocument::Render( Document::PixelWriter* pw, int page, float zoom, int rotation) { assert((page >= 0) && (page < GetNumPages())); std::unique_lock<std::mutex> lock(_render_mutex); // 1. Init MuPDF structures. const fz_matrix& m = Transform(zoom, rotation); pdf_page* page_struct = GetPage(page); const fz_irect& bbox = GetBoundingBox(page_struct, m); fz_pixmap* pixmap = fz_new_pixmap_with_bbox( _fz_context, fz_device_rgb(_fz_context), FZ_OBJ(bbox), nullptr, 1); fz_device* dev = fz_new_draw_device(_fz_context, FZ_OBJ(fz_identity), pixmap); // 2. Render page. fz_clear_pixmap_with_value(_fz_context, pixmap, 0xff); pdf_run_page( _fz_context, _pdf_document, page_struct, dev, FZ_OBJ(m), nullptr); // 3. Write pixmap to buffer. The page is vertically divided into n equal // stripes, each copied to pw by one thread. assert(fz_pixmap_components(_fz_context, pixmap) == 4); uint8_t* buffer = reinterpret_cast<uint8_t*>(fz_pixmap_samples(_fz_context, pixmap)); const int num_cols = fz_pixmap_width(_fz_context, pixmap); const int num_rows = fz_pixmap_height(_fz_context, pixmap); ExecuteInParallel([=](int num_threads, int i) { const int num_rows_per_thread = num_rows / num_threads; const int y_begin = i * num_rows_per_thread; const int y_end = (i == num_threads - 1) ? num_rows : (i + 1) * num_rows_per_thread; uint8_t* p = buffer + y_begin * num_cols * 4; for (int y = y_begin; y < y_end; ++y) { for (int x = 0; x < num_cols; ++x) { pw->Write(x, y, p[0], p[1], p[2]); p += 4; } } }); // 4. Clean up. fz_close_device(_fz_context, dev); fz_drop_device(_fz_context, dev); fz_drop_pixmap(_fz_context, pixmap); }
QImage render(qreal dpiX, qreal dpiY, const QRectF& rect) { if (m_init) { fz_scale(&m_matrix, dpiX / 72.0f, dpiY / 72.0f); fz_bound_page(m_ctx, m_page, &m_bound); fz_transform_rect(&m_bound, &m_matrix); m_init = false; } if (!m_list) { m_list = fz_new_display_list(m_ctx); } if (!m_dev) { m_dev = fz_new_list_device(m_ctx, m_list); fz_run_page(m_ctx, m_page, m_dev, &m_matrix, 0); } fz_matrix tile; fz_translate(&tile, -m_bound.x0, -m_bound.y0); fz_pre_translate(&tile, -rect.x(), -rect.y()); fz_rect tr; tr.x0 = tr.y0 = 0.0; int width = tr.x1 = rect.width(); int height = tr.y1 = rect.height(); QImage image(width, height, QImage::Format_RGB32); image.fill(Qt::white); // TODO: configurable fz_pixmap* pixmap = fz_new_pixmap_with_data(m_ctx, fz_device_bgr(m_ctx), image.width(), image.height(), image.bits()); fz_device *device = fz_new_draw_device(m_ctx, pixmap); fz_run_display_list(m_ctx, m_list, device, &tile, &tr, 0); fz_drop_device(m_ctx, device); fz_drop_pixmap(m_ctx, pixmap); return image; }
static void pixmap_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) { fz_pixmap_writer *wri = (fz_pixmap_writer*)wri_; char path[PATH_MAX]; fz_try(ctx) fz_close_device(ctx, dev); fz_always(ctx) fz_drop_device(ctx, dev); fz_catch(ctx) fz_rethrow(ctx); wri->count += 1; fz_format_output_path(ctx, path, sizeof path, wri->path, wri->count); wri->save(ctx, wri->pixmap, path); fz_drop_pixmap(ctx, wri->pixmap); wri->pixmap = NULL; }
fz_text_page * fz_new_text_page_from_page(fz_context *ctx, fz_page *page, fz_text_sheet *sheet) { fz_text_page *text; fz_device *dev; text = fz_new_text_page(ctx); fz_try(ctx) { dev = fz_new_text_device(ctx, sheet, text); fz_run_page(ctx, page, dev, &fz_identity, NULL); } fz_always(ctx) { fz_drop_device(ctx, dev); } fz_catch(ctx) { fz_drop_text_page(ctx, text); fz_rethrow(ctx); } return text; }
fz_stext_page * fz_new_stext_page_from_display_list(fz_context *ctx, fz_display_list *list, fz_stext_sheet *sheet) { fz_stext_page *text; fz_device *dev; text = fz_new_stext_page(ctx); fz_try(ctx) { dev = fz_new_stext_device(ctx, sheet, text); fz_run_display_list(ctx, list, dev, &fz_identity, NULL, NULL); } fz_always(ctx) { fz_drop_device(ctx, dev); } fz_catch(ctx) { fz_drop_stext_page(ctx, text); fz_rethrow(ctx); } return text; }
static int make_fake_doc(pdfapp_t *app) { fz_context *ctx = app->ctx; fz_matrix ctm = { 1, 0, 0, 1, 0, 0 }; fz_rect bounds; pdf_page *newpage = NULL; pdf_document *pdf = NULL; fz_device *dev = NULL; fz_path *path = NULL; fz_stroke_state stroke = fz_default_stroke_state; float red[3] = { 1, 0, 0 }; int i; fz_var(pdf); fz_var(dev); fz_var(newpage); fz_try(ctx) { pdf = pdf_create_document(ctx); app->doc = &pdf->super; bounds.x0 = 0; bounds.y0 = 0; bounds.x1 = app->winw; bounds.y1 = app->winh; newpage = pdf_create_page(ctx, pdf, bounds, 72, 0); dev = pdf_page_write(ctx, pdf, newpage); /* Now the page content */ fz_begin_page(ctx, dev, &bounds, &ctm); path = fz_new_path(ctx); fz_moveto(ctx, path, 0, 0); fz_lineto(ctx, path, bounds.x1, bounds.y1); fz_moveto(ctx, path, 0, bounds.y1); fz_lineto(ctx, path, bounds.x1, 0); stroke.linewidth = fz_min(bounds.x1, bounds.y1)/4; fz_stroke_path(ctx, dev, path, &stroke, &ctm, fz_device_rgb(ctx), red, 1); fz_end_page(ctx, dev); fz_drop_device(ctx, dev); dev = NULL; /* Create enough copies of our blank(ish) page so that the * page number is preserved if and when a subsequent load * works. */ for (i = 0; i < app->pagecount; i++) pdf_insert_page(ctx, pdf, newpage, INT_MAX); } fz_always(ctx) { fz_drop_path(ctx, path); pdf_drop_page(ctx, newpage); fz_drop_device(ctx, dev); dev = NULL; } fz_catch(ctx) { fz_rethrow(ctx); } return 0; }
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; }