From 95be71c1e16ff24036e12adab4317fc49a944b80 Mon Sep 17 00:00:00 2001 From: Tigran Aivazian Date: Sun, 2 Sep 2012 18:22:15 +0100 Subject: [PATCH 1/3] Small optimization in djvu.c:drawPage() --- there is no need to create and destroy djvu pixel format on each redraw of the page as this can be done once on open and close of the document. Also, set dither bits to 4 to help djvulibre choose the most optimal dithering algorithm for the Kindle. Also, make coding style of "if(" -> "if (" consistent (both instances were used, but "if (" was more frequent). --- djvu.c | 61 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/djvu.c b/djvu.c index fd90af3ca..0681053af 100644 --- a/djvu.c +++ b/djvu.c @@ -32,6 +32,7 @@ typedef struct DjvuDocument { ddjvu_context_t *context; ddjvu_document_t *doc_ref; + ddjvu_format_t *pixelformat; } DjvuDocument; typedef struct DjvuPage { @@ -93,6 +94,14 @@ static int openDocument(lua_State *L) { return luaL_error(L, "cannot open DJVU file <%s>", filename); } + doc->pixelformat = ddjvu_format_create(DDJVU_FORMAT_GREY8, 0, NULL); + if (! doc->pixelformat) { + return luaL_error(L, "cannot create DJVU pixelformat for <%s>", filename); + } + ddjvu_format_set_row_order(doc->pixelformat, 1); + ddjvu_format_set_y_direction(doc->pixelformat, 1); + ddjvu_format_set_ditherbits(doc->pixelformat, 4); + return 1; } @@ -100,14 +109,18 @@ static int closeDocument(lua_State *L) { DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); // should be save if called twice - if(doc->doc_ref != NULL) { + if (doc->doc_ref != NULL) { ddjvu_document_release(doc->doc_ref); doc->doc_ref = NULL; } - if(doc->context != NULL) { + if (doc->context != NULL) { ddjvu_context_release(doc->context); doc->context = NULL; } + if (doc->pixelformat != NULL) { + ddjvu_format_release(doc->pixelformat); + doc->pixelformat = NULL; + } return 0; } @@ -179,7 +192,7 @@ static int openPage(lua_State *L) { DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); int pageno = luaL_checkint(L, 2); - if(pageno < 1 || pageno > ddjvu_document_get_pagenum(doc->doc_ref)) { + if (pageno < 1 || pageno > ddjvu_document_get_pagenum(doc->doc_ref)) { return luaL_error(L, "cannot open page #%d, out of range (1-%d)", pageno, ddjvu_document_get_pagenum(doc->doc_ref)); } @@ -191,7 +204,7 @@ static int openPage(lua_State *L) { page->page_ref = ddjvu_page_create_by_pageno(doc->doc_ref, pageno - 1); while (! ddjvu_page_decoding_done(page->page_ref)) handle(L, doc->context, TRUE); - if(! page->page_ref) { + if (! page->page_ref) { return luaL_error(L, "cannot open page #%d", pageno); } @@ -307,7 +320,7 @@ static int getPageText(lua_State *L) { /* retrive one line entry */ se_line = miniexp_nth(i, sexp); nr_word = miniexp_length(se_line); - if(nr_word == 0) { + if (nr_word == 0) { continue; } @@ -388,8 +401,8 @@ static int getPageText(lua_State *L) { static int closePage(lua_State *L) { DjvuPage *page = (DjvuPage*) luaL_checkudata(L, 1, "djvupage"); - // should be save if called twice - if(page->page_ref != NULL) { + // should be safe if called twice + if (page->page_ref != NULL) { ddjvu_page_release(page->page_ref); page->page_ref = NULL; } @@ -403,22 +416,14 @@ static int drawPage(lua_State *L) { ddjvu_render_mode_t djvu_render_mode = (int) luaL_checkint(L, 6); unsigned char adjusted_low[16], adjusted_high[16]; int i, adjust_pixels = 0; - ddjvu_format_t *pixelformat; ddjvu_rect_t pagerect, renderrect; - uint8_t *imagebuffer = NULL; - - imagebuffer = malloc((bb->w)*(bb->h)+1); - /* fill pixel map with white color */ - memset(imagebuffer, 0xFF, (bb->w)*(bb->h)+1); - - pixelformat = ddjvu_format_create(DDJVU_FORMAT_GREY8, 0, NULL); - ddjvu_format_set_row_order(pixelformat, 1); - ddjvu_format_set_y_direction(pixelformat, 1); - ddjvu_format_set_gamma(pixelformat, dc->gamma); - /*ddjvu_format_set_ditherbits(dc->pixelformat, 2);*/ + uint8_t *imagebuffer = malloc((bb->w)*(bb->h)+1); /*printf("@page %d, @@zoom:%f, offset: (%d, %d)\n", page->num, dc->zoom, dc->offset_x, dc->offset_y);*/ + /* fill pixel map with white color */ + memset(imagebuffer, 0xFF, (bb->w)*(bb->h)+1); + /* render full page into rectangle specified by pagerect */ /*pagerect.x = luaL_checkint(L, 4);*/ /*pagerect.y = luaL_checkint(L, 5);*/ @@ -429,13 +434,12 @@ static int drawPage(lua_State *L) { /*printf("--pagerect--- (x: %d, y: %d), w: %d, h: %d.\n", 0, 0, pagerect.w, pagerect.h);*/ - /* copy pixels area from pagerect specified by renderrect. - + * * ddjvulibre library does not support negative offset, positive offset * means moving towards right and down. * - * However, djvureader.lua handles offset differently. It use negative + * However, djvureader.lua handles offset differently. It uses negative * offset to move right and down while positive offset to move left * and up. So we need to handle positive offset manually when copying * imagebuffer to blitbuffer (framebuffer). @@ -448,7 +452,7 @@ static int drawPage(lua_State *L) { /*printf("--renderrect--- (%d, %d), w:%d, h:%d\n", renderrect.x, renderrect.y, renderrect.w, renderrect.h);*/ /* ddjvulibre library only supports rotation of 0, 90, 180 and 270 degrees. - * This four kinds of rotations can already be achieved by native system. + * These four kinds of rotations can already be achieved by native system. * So we don't set rotation here. */ @@ -456,12 +460,12 @@ static int drawPage(lua_State *L) { djvu_render_mode, &pagerect, &renderrect, - pixelformat, + page->doc->pixelformat, bb->w, imagebuffer); - uint8_t *bbptr = (uint8_t*)bb->data; - uint8_t *pmptr = (uint8_t*)imagebuffer; + uint8_t *bbptr = bb->data; + uint8_t *pmptr = imagebuffer; int x, y; /* if offset is positive, we are moving towards up and left. */ int x_offset = MAX(0, dc->offset_x); @@ -488,7 +492,7 @@ static int drawPage(lua_State *L) { else bbptr[x] = (high << 4) | low; } - if(bb->w & 1) { + if (bb->w & 1) { bbptr[x] = 255 - (pmptr[x*2] & 0xF0); } /* go to next line */ @@ -497,9 +501,6 @@ static int drawPage(lua_State *L) { } free(imagebuffer); - pmptr = imagebuffer = NULL; - ddjvu_format_release(pixelformat); - return 0; } From 524c8209c031889061d268d4fd5d4f52518071c1 Mon Sep 17 00:00:00 2001 From: Tigran Aivazian Date: Sun, 2 Sep 2012 18:43:07 +0100 Subject: [PATCH 2/3] Improve customupdate target to only include the _real_ LUA source files, not the garbage settings.*.lua or any other small test/experimental *.lua files that happen to be in the current directory. --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index fe7e4414a..58e060227 100644 --- a/Makefile +++ b/Makefile @@ -207,6 +207,8 @@ thirdparty: $(MUPDFLIBS) $(THIRDPARTYLIBS) $(LUALIB) $(DJVULIBS) $(CRENGINELIBS) INSTALL_DIR=kindlepdfviewer +LUA_FILES=alt_getopt.lua commands.lua crereader.lua dialog.lua djvureader.lua extentions.lua filechooser.lua filehistory.lua fileinfo.lua filesearcher.lua font.lua graphics.lua helppage.lua image.lua inputbox.lua keys.lua pdfreader.lua reader.lua rendertext.lua screen.lua selectmenu.lua settings.lua unireader.lua widget.lua + VERSION?=$(shell git rev-parse --short HEAD) customupdate: all # ensure that build binary is for ARM @@ -215,7 +217,7 @@ customupdate: all -rm kindlepdfviewer-$(VERSION).zip rm -Rf $(INSTALL_DIR) mkdir $(INSTALL_DIR) - cp -p README.TXT COPYING kpdfview *.lua $(INSTALL_DIR) + cp -p README.TXT COPYING kpdfview $(LUA_FILES) $(INSTALL_DIR) mkdir $(INSTALL_DIR)/data cp -rpL data/*.css $(INSTALL_DIR)/data cp -rpL fonts $(INSTALL_DIR) From af37e9f14d9a52050d0918fe32b6e62c5a9dc46e Mon Sep 17 00:00:00 2001 From: Tigran Aivazian Date: Sun, 2 Sep 2012 18:57:37 +0100 Subject: [PATCH 3/3] Comment out the setting of dithering bits because djvulibre ignores anything less than 8 bits anyway. --- djvu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/djvu.c b/djvu.c index 0681053af..8be9bc694 100644 --- a/djvu.c +++ b/djvu.c @@ -100,7 +100,8 @@ static int openDocument(lua_State *L) { } ddjvu_format_set_row_order(doc->pixelformat, 1); ddjvu_format_set_y_direction(doc->pixelformat, 1); - ddjvu_format_set_ditherbits(doc->pixelformat, 4); + /* dithering bits <8 are ignored by djvulibre */ + /* ddjvu_format_set_ditherbits(doc->pixelformat, 4); */ return 1; }