diff --git a/drawcontext.h b/drawcontext.h index f601de148..ae0d0dff9 100644 --- a/drawcontext.h +++ b/drawcontext.h @@ -22,6 +22,7 @@ typedef struct DrawContext { int rotate; double zoom; double gamma; + unsigned char white_threshold; int offset_x; int offset_y; } DrawContext; diff --git a/ffi/drawcontext.lua b/ffi/drawcontext.lua index 1886cbeac..14a9d8abc 100644 --- a/ffi/drawcontext.lua +++ b/ffi/drawcontext.lua @@ -11,6 +11,7 @@ typedef struct DrawContext { int rotate; double zoom; double gamma; + unsigned char white_threshold; int offset_x; int offset_y; } DrawContext; @@ -30,11 +31,13 @@ end function DC_mt.__index:getOffset() return self.offset_x, self.offset_y end function DC_mt.__index:setGamma(gamma) self.gamma = gamma end function DC_mt.__index:getGamma() return self.gamma end +function DC_mt.__index:setWhiteThreshold(white_threshold) self.white_threshold = white_threshold end +function DC_mt.__index:getWhiteThreshold() return self.white_threshold end local dctype = ffi.metatype("DrawContext", DC_mt) -function DC.new(rotate, zoom, x, y, gamma) - return dctype(rotate or 0, zoom or 1.0, gamma or -1.0, x or 0, y or 0) +function DC.new(rotate, zoom, x, y, gamma, white_threshold) + return dctype(rotate or 0, zoom or 1.0, gamma or -1.0, white_threshold or 255, x or 0, y or 0) end return DC diff --git a/ffi/koptcontext.lua b/ffi/koptcontext.lua index 83c495ec5..4d9c497cb 100644 --- a/ffi/koptcontext.lua +++ b/ffi/koptcontext.lua @@ -179,6 +179,7 @@ end function KOPTContext_mt.__index:setTrim(trim) self.trim = trim end function KOPTContext_mt.__index:setWrap(wrap) self.wrap = wrap end function KOPTContext_mt.__index:setWhite(white) self.white = white end +function KOPTContext_mt.__index:setPaintWhite(paint_white) self.paint_white = paint_white end function KOPTContext_mt.__index:setIndent(indent) self.indent = indent end function KOPTContext_mt.__index:setRotate(rotate) self.rotate = rotate end function KOPTContext_mt.__index:setColumns(columns) self.columns = columns end @@ -599,6 +600,7 @@ function KOPTContext.new() kc.trim = 1 kc.wrap = 1 kc.white = -1 + kc.paint_white = 0 kc.indent = 1 kc.rotate = 0 kc.columns = 2 @@ -669,6 +671,7 @@ function KOPTContext.totable(kc) context.trim = kc.trim context.wrap = kc.wrap context.white = kc.white + context.paint_white = kc.paint_white context.indent = kc.indent context.rotate = kc.rotate context.columns = kc.columns @@ -739,6 +742,7 @@ function KOPTContext.fromtable(context) kc.trim = context.trim kc.wrap = context.wrap kc.white = context.white + kc.paint_white = context.paint_white kc.indent = context.indent kc.rotate = context.rotate kc.columns = context.columns diff --git a/ffi/koptcontext_h.lua b/ffi/koptcontext_h.lua index 3e5b33de0..cc7ecd681 100644 --- a/ffi/koptcontext_h.lua +++ b/ffi/koptcontext_h.lua @@ -110,6 +110,7 @@ struct KOPTContext { int trim; int wrap; int white; + int paint_white; int indent; int rotate; int columns; diff --git a/ffi/mupdf.lua b/ffi/mupdf.lua index 5355adfa7..3431b1323 100644 --- a/ffi/mupdf.lua +++ b/ffi/mupdf.lua @@ -647,6 +647,29 @@ local function run_page(page, pixmap, ctm) M.fz_drop_device(page.ctx, dev) if ok == nil then merror(page.ctx, "could not run page") end end + +local function apply_white_threshold(bytes_per_pixel, samples, pixel_count, white_threshold) + if bytes_per_pixel == 4 then + for i = 0, pixel_count - 1 do + local idx = i * bytes_per_pixel + local r, g, b = samples[idx], samples[idx + 1], samples[idx + 2] + local brightness = 0.299 * r + 0.587 * g + 0.114 * b + if brightness > white_threshold then + samples[idx] = 255 + samples[idx + 1] = 255 + samples[idx + 2] = 255 + end + end + else + for i = 0, pixel_count - 1 do + local brightness = samples[i] + if brightness > white_threshold then + samples[i] = 255 + end + end + end +end + --[[ render page to blitbuffer @@ -679,12 +702,16 @@ function page_mt.__index:draw_new(draw_context, width, height, offset_x, offset_ if mupdf.bgr and self.doc.color then colorspace = M.fz_device_bgr(self.ctx) end + local samples = ffi.cast("unsigned char*", bb.data) local pix = W.mupdf_new_pixmap_with_bbox_and_data( - self.ctx, colorspace, bbox, nil, self.doc.color and 1 or 0, ffi.cast("unsigned char*", bb.data)) + self.ctx, colorspace, bbox, nil, self.doc.color and 1 or 0, samples) if pix == nil then merror(self.ctx, "cannot allocate pixmap") end run_page(self, pix, ctm) + if draw_context.white_threshold < 255 then + apply_white_threshold(self.doc.color and 4 or 1, samples, width * height, draw_context.white_threshold) + end if draw_context.gamma >= 0.0 then M.fz_gamma_pixmap(self.ctx, pix, draw_context.gamma) end diff --git a/thirdparty/libk2pdfopt/CMakeLists.txt b/thirdparty/libk2pdfopt/CMakeLists.txt index 23912a57b..80d7d2e31 100644 --- a/thirdparty/libk2pdfopt/CMakeLists.txt +++ b/thirdparty/libk2pdfopt/CMakeLists.txt @@ -20,7 +20,7 @@ if(NOT MONOLIBTIC) endif() external_project( - DOWNLOAD GIT 59ced371378312d8f332d9a35f5b4a3c33b18954 + DOWNLOAD GIT df18dfb0b7e33c001f1133c8b3c139b04f0e0a05 https://github.com/koreader/libk2pdfopt.git PATCH_COMMAND ${PATCH_CMD} CMAKE_ARGS ${CMAKE_ARGS}