Discussion:
[PATCH 4/5] Deduplicate pdf_page_render and pdf_page_render_cairo
Alexander Monakov
2013-10-30 21:49:40 UTC
Permalink
---
pdf.c | 135 +++++++++++++++++++++++++++---------------------------------------
1 file changed, 55 insertions(+), 80 deletions(-)

diff --git a/pdf.c b/pdf.c
index 13b3c6b..791751b 100644
--- a/pdf.c
+++ b/pdf.c
@@ -452,6 +452,54 @@ error_ret:
return NULL;
}

+static zathura_error_t
+pdf_page_render_to_buffer(mupdf_document_t* mupdf_document, mupdf_page_t* mupdf_page,
+ unsigned char* image, int rowstride, int components,
+ unsigned int page_width, unsigned int page_height,
+ double scalex, double scaley)
+{
+ fz_display_list* display_list = fz_new_display_list(mupdf_page->ctx);
+ fz_device* device = fz_new_list_device(mupdf_page->ctx, display_list);
+
+ fz_try (mupdf_document->ctx) {
+ fz_matrix m;
+ fz_scale(&m, scalex, scaley);
+ fz_run_page(mupdf_document->document, mupdf_page->page, device, &m, NULL);
+ } fz_catch (mupdf_document->ctx) {
+ return ZATHURA_ERROR_UNKNOWN;
+ }
+
+ fz_free_device(device);
+
+ fz_irect irect = { .x1 = page_width, .y1 = page_height };
+ fz_rect rect = { .x1 = page_width, .y1 = page_height };
+
+ fz_colorspace* colorspace = fz_device_rgb(mupdf_document->ctx);
+ fz_pixmap* pixmap = fz_new_pixmap_with_bbox(mupdf_page->ctx, colorspace, &irect);
+ fz_clear_pixmap_with_value(mupdf_page->ctx, pixmap, 0xFF);
+
+ device = fz_new_draw_device(mupdf_page->ctx, pixmap);
+ fz_run_display_list(display_list, device, &fz_identity, &rect, NULL);
+ fz_free_device(device);
+
+ unsigned char* s = fz_pixmap_samples(mupdf_page->ctx, pixmap);
+ unsigned int n = fz_pixmap_components(mupdf_page->ctx, pixmap);
+ for (unsigned int y = 0; y < fz_pixmap_height(mupdf_page->ctx, pixmap); y++) {
+ for (unsigned int x = 0; x < fz_pixmap_width(mupdf_page->ctx, pixmap); x++) {
+ guchar* p = image + y * rowstride + x * components;
+ p[0] = s[2];
+ p[1] = s[1];
+ p[2] = s[0];
+ s += n;
+ }
+ }
+
+ fz_drop_pixmap(mupdf_page->ctx, pixmap);
+ fz_drop_display_list(mupdf_page->ctx, display_list);
+
+ return ZATHURA_ERROR_OK;
+}
+
zathura_image_buffer_t*
pdf_page_render(zathura_page_t* page, mupdf_page_t* mupdf_page, zathura_error_t* error)
{
@@ -485,53 +533,20 @@ pdf_page_render(zathura_page_t* page, mupdf_page_t* mupdf_page, zathura_error_t*

int rowstride = image_buffer->rowstride;
unsigned char* image = image_buffer->data;
- int components = 3;

mupdf_document_t* mupdf_document = zathura_document_get_data(document);

- /* render */
- fz_display_list* display_list = fz_new_display_list(mupdf_page->ctx);
- fz_device* device = fz_new_list_device(mupdf_page->ctx, display_list);
+ zathura_error_t e = pdf_page_render_to_buffer(mupdf_document, mupdf_page, image, rowstride, 3,
+ page_width, page_height, scalex, scaley);

- fz_try (mupdf_document->ctx) {
- fz_matrix m;
- fz_scale(&m, scalex, scaley);
- fz_run_page(mupdf_document->document, mupdf_page->page, device, &m, NULL);
- } fz_catch (mupdf_document->ctx) {
+ if (e != ZATHURA_ERROR_OK) {
+ zathura_image_buffer_free(image_buffer);
if (error != NULL) {
- *error = ZATHURA_ERROR_OUT_OF_MEMORY;
+ *error = e;
}
return NULL;
}

- fz_free_device(device);
-
- fz_irect irect = { .x1 = page_width, .y1 = page_height };
- fz_rect rect = { .x1 = page_width, .y1 = page_height };
-
- fz_colorspace* colorspace = fz_device_rgb(mupdf_document->ctx);
- fz_pixmap* pixmap = fz_new_pixmap_with_bbox(mupdf_page->ctx, colorspace, &irect);
- fz_clear_pixmap_with_value(mupdf_page->ctx, pixmap, 0xFF);
-
- device = fz_new_draw_device(mupdf_page->ctx, pixmap);
- fz_run_display_list(display_list, device, &fz_identity, &rect, NULL);
- fz_free_device(device);
-
- unsigned char* s = fz_pixmap_samples(mupdf_page->ctx, pixmap);
- unsigned int n = fz_pixmap_components(mupdf_page->ctx, pixmap);
- for (unsigned int y = 0; y < fz_pixmap_height(mupdf_page->ctx, pixmap); y++) {
- for (unsigned int x = 0; x < fz_pixmap_width(mupdf_page->ctx, pixmap); x++) {
- guchar* p = image + y * rowstride + x * components;
- p[0] = s[2];
- p[1] = s[1];
- p[2] = s[0];
- s += n;
- }
- }
-
- fz_drop_pixmap(mupdf_page->ctx, pixmap);
- fz_drop_display_list(mupdf_page->ctx, display_list);
-
return image_buffer;
}

@@ -563,51 +578,11 @@ pdf_page_render_cairo(zathura_page_t* page, mupdf_page_t* mupdf_page, cairo_t* c

int rowstride = cairo_image_surface_get_stride(surface);
unsigned char* image = cairo_image_surface_get_data(surface);
- int components = 4;

mupdf_document_t* mupdf_document = zathura_document_get_data(document);

- /* render */
- fz_display_list* display_list = fz_new_display_list(mupdf_page->ctx);
- fz_device* device = fz_new_list_device(mupdf_page->ctx, display_list);
-
- fz_try (mupdf_document->ctx) {
- fz_matrix m;
- fz_scale(&m, scalex, scaley);
- fz_run_page(mupdf_document->document, mupdf_page->page, device, &m, NULL);
- } fz_catch (mupdf_document->ctx) {
- return ZATHURA_ERROR_UNKNOWN;
- }
-
- fz_free_device(device);
-
- fz_irect irect = { .x1 = page_width, .y1 = page_height };
- fz_rect rect = { .x1 = page_width, .y1 = page_height };
-
- fz_colorspace* colorspace = fz_device_rgb(mupdf_document->ctx);
- fz_pixmap* pixmap = fz_new_pixmap_with_bbox(mupdf_page->ctx, colorspace, &irect);
- fz_clear_pixmap_with_value(mupdf_page->ctx, pixmap, 0xFF);
-
- device = fz_new_draw_device(mupdf_page->ctx, pixmap);
- fz_run_display_list(display_list, device, &fz_identity, &rect, NULL);
- fz_free_device(device);
-
- unsigned char* s = fz_pixmap_samples(mupdf_page->ctx, pixmap);
- unsigned int n = fz_pixmap_components(mupdf_page->ctx, pixmap);
- for (unsigned int y = 0; y < fz_pixmap_height(mupdf_page->ctx, pixmap); y++) {
- for (unsigned int x = 0; x < fz_pixmap_width(mupdf_page->ctx, pixmap); x++) {
- guchar* p = image + y * rowstride + x * components;
- p[0] = s[2];
- p[1] = s[1];
- p[2] = s[0];
- s += n;
- }
- }
-
- fz_drop_pixmap(mupdf_page->ctx, pixmap);
- fz_drop_display_list(mupdf_page->ctx, display_list);
-
- return ZATHURA_ERROR_OK;;
+ return pdf_page_render_to_buffer(mupdf_document, mupdf_page, image, rowstride, 4,
+ page_width, page_height, scalex, scaley);
}
#endif
--
1.8.3.2
Loading...