2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00

DjVu enhancements: a) add support for switching the rendering mode between B&W (default, 1) and COLOUR (0) values. b) handle the gamma values correctly.

This commit is contained in:
Tigran Aivazian 2012-08-27 15:33:32 +01:00
parent 0a4e11d4af
commit 5c9a8fbec6
2 changed files with 50 additions and 6 deletions

34
djvu.c
View File

@ -15,6 +15,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <libdjvu/miniexp.h>
#include <libdjvu/ddjvuapi.h>
@ -186,7 +187,7 @@ static int openPage(lua_State *L) {
luaL_getmetatable(L, "djvupage");
lua_setmetatable(L, -2);
/* djvulibre counts page starts form 0 */
/* djvulibre counts page starts from 0 */
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);
@ -197,7 +198,7 @@ static int openPage(lua_State *L) {
page->doc = doc;
page->num = pageno;
/* djvulibre counts page starts form 0 */
/* djvulibre counts page starts from 0 */
while((r=ddjvu_document_get_pageinfo(doc->doc_ref, pageno - 1,
&(page->info)))<DDJVU_JOB_OK)
handle(L, doc->context, TRUE);
@ -399,6 +400,15 @@ static int drawPage(lua_State *L) {
DjvuPage *page = (DjvuPage*) luaL_checkudata(L, 1, "djvupage");
DrawContext *dc = (DrawContext*) luaL_checkudata(L, 2, "drawcontext");
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 3, "blitbuffer");
int render_mode = (int) luaL_checkint(L, 6);
ddjvu_render_mode_t djvu_render_mode;
unsigned char adjusted_low[16], adjusted_high[16];
int i, adjust_pixels = 0;
if (render_mode)
djvu_render_mode = DDJVU_RENDER_BLACK;
else
djvu_render_mode = DDJVU_RENDER_COLOR;
ddjvu_format_t *pixelformat;
ddjvu_rect_t pagerect, renderrect;
@ -450,7 +460,7 @@ static int drawPage(lua_State *L) {
*/
ddjvu_page_render(page->page_ref,
DDJVU_RENDER_COLOR,
djvu_render_mode,
&pagerect,
&renderrect,
pixelformat,
@ -464,12 +474,26 @@ static int drawPage(lua_State *L) {
int x_offset = MAX(0, dc->offset_x);
int y_offset = MAX(0, dc->offset_y);
/* prepare the tables for adjusting the intensity of pixels */
if (dc->gamma != -1.0) {
for (i=0; i<16; i++) {
adjusted_low[i] = MIN(15, (unsigned char)floorf(dc->gamma * (float)i));
adjusted_high[i] = adjusted_low[i] << 4;
}
adjust_pixels = 1;
}
bbptr += bb->pitch * y_offset;
for(y = y_offset; y < bb->h; y++) {
/* bbptr's line width is half of pmptr's */
for(x = x_offset/2; x < (bb->w / 2); x++) {
bbptr[x] = 255 - (((pmptr[x*2 + 1 - x_offset] & 0xF0) >> 4) |
(pmptr[x*2 - x_offset] & 0xF0));
int p = x*2 - x_offset;
unsigned char low = 15 - (pmptr[p + 1] >> 4);
unsigned char high = 15 - (pmptr[p] >> 4);
if (adjust_pixels)
bbptr[x] = adjusted_high[high] | adjusted_low[low];
else
bbptr[x] = (high << 4) | low;
}
if(bb->w & 1) {
bbptr[x] = 255 - (pmptr[x*2] & 0xF0);

View File

@ -34,6 +34,10 @@ UniReader = {
-- gamma setting:
globalgamma = 1.0, -- GAMMA_NO_GAMMA
-- rendering mode toggle (used in djvu.c:drawPage())
-- if set to 1 render in BLACK & WHITE, otherwise COLOR
render_mode = 1,
-- cached tile size
fullwidth = 0,
fullheight = 0,
@ -1154,7 +1158,7 @@ function UniReader:drawOrCache(no, preCache)
--debug ("# new biltbuffer:"..dump(self.cache[pagehash]))
dc:setOffset(-tile.x, -tile.y)
Debug("rendering page", no)
page:draw(dc, self.cache[pagehash].bb, 0, 0)
page:draw(dc, self.cache[pagehash].bb, 0, 0, self.render_mode)
page:close()
-- return hash and offset within blitbuffer
@ -1568,6 +1572,17 @@ end
function UniReader:modifyGamma(factor)
Debug("modifyGamma, gamma=", self.globalgamma, " factor=", factor)
self.globalgamma = self.globalgamma * factor;
showInfoMsgWithDelay("New gamma = "..self.globalgamma, 1000, 1)
self:redrawCurrentPage()
end
-- toggle rendering mode between colour (0) and b&w (1)
function UniReader:toggle_render_mode()
Debug("toggle_render_mode, render_mode=", self.render_mode)
self.render_mode = 1 - self.render_mode
self:clearCache()
self.doc:cleanCache()
showInfoMsgWithDelay("New render_mode = "..self.render_mode, 1000, 1)
self:redrawCurrentPage()
end
@ -2202,6 +2217,11 @@ function UniReader:addAllCommands()
self:redrawCurrentPage()
end
end)
self.commands:add(KEY_R, nil, "R",
"toggle rendering mode: b&w/colour",
function(unireader)
unireader:toggle_render_mode()
end)
self.commands:add(KEY_R, MOD_SHIFT, "R",
"manual full screen refresh",
function(unireader)