From c5491675aa41eb6c283166a4a46fd0eeae8a9487 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Sun, 20 Mar 2011 02:09:04 +0100 Subject: [PATCH] Update rendering --- callbacks.c | 2 +- document.c | 5 +++-- document.h | 1 + render.c | 54 +++++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/callbacks.c b/callbacks.c index e6b03ae..f90d632 100644 --- a/callbacks.c +++ b/callbacks.c @@ -68,7 +68,7 @@ cb_view_vadjustment_value_changed(GtkAdjustment *adjustment, gpointer data) /*|| ( (begin <= lower) && (end >= lower) && (end <= upper) ) [> end of the page is in viewport <]*/ /*|| ( (begin >= lower) && (end >= upper) && (begin <= upper) ) [> begin of the page is in viewport <]*/ /*) {*/ - if (page_id < 5) { + if (page_id < 1) { render_page(Zathura.Sync.render_thread, Zathura.document->pages[page_id]); } /*}*/ diff --git a/document.c b/document.c index fb6901d..7af7f23 100644 --- a/document.c +++ b/document.c @@ -537,8 +537,9 @@ zathura_image_buffer_create(unsigned int width, unsigned int height) return NULL; } - image_buffer->width = width; - image_buffer->height = height; + image_buffer->width = width; + image_buffer->height = height; + image_buffer->rowstride = width * 3; return image_buffer; } diff --git a/document.h b/document.h index 5f48fae..c39d8e1 100644 --- a/document.h +++ b/document.h @@ -51,6 +51,7 @@ typedef struct zathura_image_buffer_s unsigned char* data; /**> Image buffer data */ unsigned int height; /**> Height of the image */ unsigned int width; /**> Width of the image */ + unsigned int rowstride; /**> Rowstride of the image */ } zathura_image_buffer_t; /** diff --git a/render.c b/render.c index fccf2bf..61fd660 100644 --- a/render.c +++ b/render.c @@ -20,7 +20,10 @@ render_job(void* data) girara_list_remove(render_thread->list, page); g_mutex_unlock(render_thread->lock); - render(page); + if (render(page) != true) { + fprintf(stderr, "rendering failed\n"); + } + printf("Rendered %d\n", page->number); } @@ -131,28 +134,51 @@ render(zathura_page_t* page) { gdk_threads_enter(); g_static_mutex_lock(&(page->lock)); - zathura_image_buffer_t* buffer = zathura_page_render(page); + zathura_image_buffer_t* image_buffer = zathura_page_render(page); - if (!buffer) { + if (image_buffer == NULL) { g_static_mutex_unlock(&(page->lock)); - printf("error: rendering failed\n"); gdk_threads_leave(); return false; } - /* create drawing area */ - /*GtkWidget* drawing_area = gtk_drawing_area_new();*/ + /* remove old image */ + GtkWidget* widget = gtk_bin_get_child(GTK_BIN(page->event_box)); + if (widget != NULL) { + gtk_container_remove(GTK_CONTAINER(page->event_box), widget); + } - /*[> remove old image <]*/ - /*GtkWidget* widget = gtk_bin_get_child(GTK_BIN(page->event_box));*/ - /*if (widget != NULL) {*/ - /*g_object_unref(widget);*/ - /*}*/ + /* create cairo surface */ + unsigned int page_width = page->width * Zathura.document->scale; + unsigned int page_height = page->height * Zathura.document->scale; - /*[> set new image <]*/ - /*gtk_box_pack_start(GTK_BOX(page->event_box), drawing_area, TRUE, TRUE, 0);*/ + cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height); - zathura_image_buffer_free(buffer); + int rowstride = cairo_image_surface_get_stride(surface); + unsigned char* image = cairo_image_surface_get_data(surface); + + for (unsigned int y = 0; y < page_height; y++) { + unsigned char* dst = image + y * rowstride; + unsigned char* src = image_buffer->data + y * image_buffer->rowstride; + + for (unsigned int x = 0; x < page_width; x++) { + dst[0] = src[0]; + dst[1] = src[1]; + dst[2] = src[2]; + dst += 3; + } + } + + /* draw to gtk widget */ + GtkWidget* drawing_area = gtk_drawing_area_new(); + gtk_container_add(GTK_CONTAINER(page->event_box), drawing_area); + + cairo_t* cairo = gdk_cairo_create(drawing_area->window); + cairo_set_source_surface(cairo, surface, 0, 0); + cairo_paint(cairo); + cairo_destroy(cairo); + + zathura_image_buffer_free(image_buffer); g_static_mutex_unlock(&(page->lock)); gdk_threads_leave();