Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions drawcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typedef struct DrawContext {
int rotate;
double zoom;
double gamma;
unsigned char white_threshold;
int offset_x;
int offset_y;
} DrawContext;
Expand Down
7 changes: 5 additions & 2 deletions ffi/drawcontext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ typedef struct DrawContext {
int rotate;
double zoom;
double gamma;
unsigned char white_threshold;
int offset_x;
int offset_y;
} DrawContext;
Expand All @@ -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
4 changes: 4 additions & 0 deletions ffi/koptcontext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ffi/koptcontext_h.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ struct KOPTContext {
int trim;
int wrap;
int white;
int paint_white;
int indent;
int rotate;
int columns;
Expand Down
29 changes: 28 additions & 1 deletion ffi/mupdf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/libk2pdfopt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down