From 003e635a56c38d0530a69fa628b7395780837884 Mon Sep 17 00:00:00 2001 From: keringo <> Date: Wed, 25 Jun 2025 21:08:54 +0300 Subject: [PATCH 01/15] black_hex & white_hex added --- drawcontext.h | 2 ++ ffi-cdecl/wrap-mupdf_cdecl.c | 1 + ffi/drawcontext.lua | 10 ++++++++-- ffi/koptcontext.lua | 8 ++++++++ ffi/koptcontext_h.lua | 2 ++ ffi/mupdf.lua | 1 + ffi/mupdf_h.lua | 1 + thirdparty/libk2pdfopt/CMakeLists.txt | 2 +- 8 files changed, 24 insertions(+), 3 deletions(-) diff --git a/drawcontext.h b/drawcontext.h index f601de148..32df06574 100644 --- a/drawcontext.h +++ b/drawcontext.h @@ -22,6 +22,8 @@ typedef struct DrawContext { int rotate; double zoom; double gamma; + int black_hex; + int white_hex; int offset_x; int offset_y; } DrawContext; diff --git a/ffi-cdecl/wrap-mupdf_cdecl.c b/ffi-cdecl/wrap-mupdf_cdecl.c index ba9610c1c..b33fa8eb8 100644 --- a/ffi-cdecl/wrap-mupdf_cdecl.c +++ b/ffi-cdecl/wrap-mupdf_cdecl.c @@ -122,6 +122,7 @@ cdecl_func(mupdf_convert_pixmap) cdecl_func(fz_drop_pixmap) cdecl_func(fz_clear_pixmap_with_value) cdecl_func(fz_gamma_pixmap) +cdecl_func(fz_tint_pixmap) cdecl_func(fz_scale_pixmap) cdecl_func(fz_pixmap_width) cdecl_func(fz_pixmap_height) diff --git a/ffi/drawcontext.lua b/ffi/drawcontext.lua index 1886cbeac..9db118260 100644 --- a/ffi/drawcontext.lua +++ b/ffi/drawcontext.lua @@ -11,6 +11,8 @@ typedef struct DrawContext { int rotate; double zoom; double gamma; + int black_hex; + int white_hex; int offset_x; int offset_y; } DrawContext; @@ -30,11 +32,15 @@ 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:setBlackHex(black_hex) self.black_hex = black_hex end +function DC_mt.__index:getBlackHex() return self.black_hex end +function DC_mt.__index:setWhiteHex(white_hex) self.white_hex = white_hex end +function DC_mt.__index:getWhiteHex() return self.white_hex 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, black_hex, white_hex) + return dctype(rotate or 0, zoom or 1.0, gamma or -1.0, x or 0, y or 0, black_hex or 0x000000, white_hex or 0xFFFFFF) end return DC diff --git a/ffi/koptcontext.lua b/ffi/koptcontext.lua index 83c495ec5..63c0d9e73 100644 --- a/ffi/koptcontext.lua +++ b/ffi/koptcontext.lua @@ -191,6 +191,8 @@ function KOPTContext_mt.__index:setMargin(margin) self.margin = margin end function KOPTContext_mt.__index:setZoom(zoom) self.zoom = zoom end function KOPTContext_mt.__index:setQuality(quality) self.quality = quality end function KOPTContext_mt.__index:setContrast(contrast) self.contrast = contrast end +function KOPTContext_mt.__index:setBlackHex(black_hex) self.black_hex = black_hex end +function KOPTContext_mt.__index:setWhiteHex(white_hex) self.white_hex = white_hex end function KOPTContext_mt.__index:setDefectSize(defect_size) self.defect_size = defect_size end function KOPTContext_mt.__index:setLineSpacing(line_spacing) self.line_spacing = line_spacing end function KOPTContext_mt.__index:setWordSpacing(word_spacing) self.word_spacing = word_spacing end @@ -614,6 +616,8 @@ function KOPTContext.new() kc.read_max_width = 3000 kc.read_max_height = 4000 kc.writing_direction = 0 + kc.black_hex = 0x000000 + kc.white_hex = 0xFFFFFF -- number values kc.zoom = 1.0 kc.margin = 0.06 @@ -684,6 +688,8 @@ function KOPTContext.totable(kc) context.read_max_width = kc.read_max_width context.read_max_height = kc.read_max_height context.writing_direction = kc.writing_direction + context.black_hex = kc.black_hex + context.white_hex = kc.white_hex -- number values context.zoom = kc.zoom context.margin = kc.margin @@ -754,6 +760,8 @@ function KOPTContext.fromtable(context) kc.read_max_width = context.read_max_width kc.read_max_height = context.read_max_height kc.writing_direction = context.writing_direction + kc.black_hex = context.black_hex + kc.white_hex = context.white_hex -- number values kc.zoom = context.zoom kc.margin = context.margin diff --git a/ffi/koptcontext_h.lua b/ffi/koptcontext_h.lua index 3e5b33de0..5f37b1ec8 100644 --- a/ffi/koptcontext_h.lua +++ b/ffi/koptcontext_h.lua @@ -125,6 +125,8 @@ struct KOPTContext { int read_max_width; int read_max_height; int writing_direction; + int black_hex; + int white_hex; double zoom; double margin; double quality; diff --git a/ffi/mupdf.lua b/ffi/mupdf.lua index a638f9286..31116cc5f 100644 --- a/ffi/mupdf.lua +++ b/ffi/mupdf.lua @@ -689,6 +689,7 @@ function page_mt.__index:draw_new(draw_context, width, height, offset_x, offset_ M.fz_gamma_pixmap(self.ctx, pix, draw_context.gamma) end + M.fz_tint_pixmap(self.ctx, pix, draw_context.black_hex, draw_context.white_hex) M.fz_drop_pixmap(self.ctx, pix) return bb diff --git a/ffi/mupdf_h.lua b/ffi/mupdf_h.lua index 91ccc08ad..e1deda680 100644 --- a/ffi/mupdf_h.lua +++ b/ffi/mupdf_h.lua @@ -226,6 +226,7 @@ fz_pixmap *mupdf_convert_pixmap(fz_context *, const fz_pixmap *, fz_colorspace * void fz_drop_pixmap(fz_context *, fz_pixmap *); void fz_clear_pixmap_with_value(fz_context *, fz_pixmap *, int); void fz_gamma_pixmap(fz_context *, fz_pixmap *, float); +void fz_tint_pixmap(fz_context *, fz_pixmap *, int, int); fz_pixmap *fz_scale_pixmap(fz_context *, fz_pixmap *, float, float, float, float, const fz_irect *); int fz_pixmap_width(fz_context *, const fz_pixmap *); int fz_pixmap_height(fz_context *, const fz_pixmap *); diff --git a/thirdparty/libk2pdfopt/CMakeLists.txt b/thirdparty/libk2pdfopt/CMakeLists.txt index 23912a57b..0287959a7 100644 --- a/thirdparty/libk2pdfopt/CMakeLists.txt +++ b/thirdparty/libk2pdfopt/CMakeLists.txt @@ -21,7 +21,7 @@ endif() external_project( DOWNLOAD GIT 59ced371378312d8f332d9a35f5b4a3c33b18954 - https://github.com/koreader/libk2pdfopt.git + https://github.com/kerivin/libk2pdfopt.git PATCH_COMMAND ${PATCH_CMD} CMAKE_ARGS ${CMAKE_ARGS} SOURCE_SUBDIR lib From 8b544c9a3b2b10aa3706a47aec5ad7429ce08030 Mon Sep 17 00:00:00 2001 From: keringo <> Date: Wed, 25 Jun 2025 21:26:51 +0300 Subject: [PATCH 02/15] tabs -> spaces --- ffi/mupdf.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ffi/mupdf.lua b/ffi/mupdf.lua index 31116cc5f..b7b9f9956 100644 --- a/ffi/mupdf.lua +++ b/ffi/mupdf.lua @@ -689,7 +689,7 @@ function page_mt.__index:draw_new(draw_context, width, height, offset_x, offset_ M.fz_gamma_pixmap(self.ctx, pix, draw_context.gamma) end - M.fz_tint_pixmap(self.ctx, pix, draw_context.black_hex, draw_context.white_hex) + M.fz_tint_pixmap(self.ctx, pix, draw_context.black_hex, draw_context.white_hex) M.fz_drop_pixmap(self.ctx, pix) return bb From 950c35b4e76e3782a0fa1e43edef02018f97c8a6 Mon Sep 17 00:00:00 2001 From: keringo <> Date: Thu, 26 Jun 2025 17:33:36 +0300 Subject: [PATCH 03/15] white_threshold --- drawcontext.h | 3 +-- ffi-cdecl/wrap-mupdf_cdecl.c | 1 - ffi/drawcontext.lua | 13 +++++-------- ffi/koptcontext.lua | 8 -------- ffi/koptcontext_h.lua | 2 -- ffi/mupdf.lua | 1 - ffi/mupdf_h.lua | 1 - 7 files changed, 6 insertions(+), 23 deletions(-) diff --git a/drawcontext.h b/drawcontext.h index 32df06574..0c4d8c635 100644 --- a/drawcontext.h +++ b/drawcontext.h @@ -22,8 +22,7 @@ typedef struct DrawContext { int rotate; double zoom; double gamma; - int black_hex; - int white_hex; + int white_threshold; int offset_x; int offset_y; } DrawContext; diff --git a/ffi-cdecl/wrap-mupdf_cdecl.c b/ffi-cdecl/wrap-mupdf_cdecl.c index b33fa8eb8..ba9610c1c 100644 --- a/ffi-cdecl/wrap-mupdf_cdecl.c +++ b/ffi-cdecl/wrap-mupdf_cdecl.c @@ -122,7 +122,6 @@ cdecl_func(mupdf_convert_pixmap) cdecl_func(fz_drop_pixmap) cdecl_func(fz_clear_pixmap_with_value) cdecl_func(fz_gamma_pixmap) -cdecl_func(fz_tint_pixmap) cdecl_func(fz_scale_pixmap) cdecl_func(fz_pixmap_width) cdecl_func(fz_pixmap_height) diff --git a/ffi/drawcontext.lua b/ffi/drawcontext.lua index 9db118260..26791d302 100644 --- a/ffi/drawcontext.lua +++ b/ffi/drawcontext.lua @@ -11,8 +11,7 @@ typedef struct DrawContext { int rotate; double zoom; double gamma; - int black_hex; - int white_hex; + unsigned char white_threshold; int offset_x; int offset_y; } DrawContext; @@ -32,15 +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:setBlackHex(black_hex) self.black_hex = black_hex end -function DC_mt.__index:getBlackHex() return self.black_hex end -function DC_mt.__index:setWhiteHex(white_hex) self.white_hex = white_hex end -function DC_mt.__index:getWhiteHex() return self.white_hex 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, black_hex, white_hex) - return dctype(rotate or 0, zoom or 1.0, gamma or -1.0, x or 0, y or 0, black_hex or 0x000000, white_hex or 0xFFFFFF) +function DC.new(rotate, zoom, x, y, gamma, white_threshold) + return dctype(rotate or 0, zoom or 1.0, gamma or -1.0, x or 0, y or 0, white_threshold or 255) end return DC diff --git a/ffi/koptcontext.lua b/ffi/koptcontext.lua index 63c0d9e73..83c495ec5 100644 --- a/ffi/koptcontext.lua +++ b/ffi/koptcontext.lua @@ -191,8 +191,6 @@ function KOPTContext_mt.__index:setMargin(margin) self.margin = margin end function KOPTContext_mt.__index:setZoom(zoom) self.zoom = zoom end function KOPTContext_mt.__index:setQuality(quality) self.quality = quality end function KOPTContext_mt.__index:setContrast(contrast) self.contrast = contrast end -function KOPTContext_mt.__index:setBlackHex(black_hex) self.black_hex = black_hex end -function KOPTContext_mt.__index:setWhiteHex(white_hex) self.white_hex = white_hex end function KOPTContext_mt.__index:setDefectSize(defect_size) self.defect_size = defect_size end function KOPTContext_mt.__index:setLineSpacing(line_spacing) self.line_spacing = line_spacing end function KOPTContext_mt.__index:setWordSpacing(word_spacing) self.word_spacing = word_spacing end @@ -616,8 +614,6 @@ function KOPTContext.new() kc.read_max_width = 3000 kc.read_max_height = 4000 kc.writing_direction = 0 - kc.black_hex = 0x000000 - kc.white_hex = 0xFFFFFF -- number values kc.zoom = 1.0 kc.margin = 0.06 @@ -688,8 +684,6 @@ function KOPTContext.totable(kc) context.read_max_width = kc.read_max_width context.read_max_height = kc.read_max_height context.writing_direction = kc.writing_direction - context.black_hex = kc.black_hex - context.white_hex = kc.white_hex -- number values context.zoom = kc.zoom context.margin = kc.margin @@ -760,8 +754,6 @@ function KOPTContext.fromtable(context) kc.read_max_width = context.read_max_width kc.read_max_height = context.read_max_height kc.writing_direction = context.writing_direction - kc.black_hex = context.black_hex - kc.white_hex = context.white_hex -- number values kc.zoom = context.zoom kc.margin = context.margin diff --git a/ffi/koptcontext_h.lua b/ffi/koptcontext_h.lua index 5f37b1ec8..3e5b33de0 100644 --- a/ffi/koptcontext_h.lua +++ b/ffi/koptcontext_h.lua @@ -125,8 +125,6 @@ struct KOPTContext { int read_max_width; int read_max_height; int writing_direction; - int black_hex; - int white_hex; double zoom; double margin; double quality; diff --git a/ffi/mupdf.lua b/ffi/mupdf.lua index b7b9f9956..a638f9286 100644 --- a/ffi/mupdf.lua +++ b/ffi/mupdf.lua @@ -689,7 +689,6 @@ function page_mt.__index:draw_new(draw_context, width, height, offset_x, offset_ M.fz_gamma_pixmap(self.ctx, pix, draw_context.gamma) end - M.fz_tint_pixmap(self.ctx, pix, draw_context.black_hex, draw_context.white_hex) M.fz_drop_pixmap(self.ctx, pix) return bb diff --git a/ffi/mupdf_h.lua b/ffi/mupdf_h.lua index e1deda680..91ccc08ad 100644 --- a/ffi/mupdf_h.lua +++ b/ffi/mupdf_h.lua @@ -226,7 +226,6 @@ fz_pixmap *mupdf_convert_pixmap(fz_context *, const fz_pixmap *, fz_colorspace * void fz_drop_pixmap(fz_context *, fz_pixmap *); void fz_clear_pixmap_with_value(fz_context *, fz_pixmap *, int); void fz_gamma_pixmap(fz_context *, fz_pixmap *, float); -void fz_tint_pixmap(fz_context *, fz_pixmap *, int, int); fz_pixmap *fz_scale_pixmap(fz_context *, fz_pixmap *, float, float, float, float, const fz_irect *); int fz_pixmap_width(fz_context *, const fz_pixmap *); int fz_pixmap_height(fz_context *, const fz_pixmap *); From 9bf6abb4caeb9088b0b7657957e481067eafdb03 Mon Sep 17 00:00:00 2001 From: keringo <> Date: Thu, 26 Jun 2025 18:27:03 +0300 Subject: [PATCH 04/15] mupdf white threshold --- ffi/mupdf.lua | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/ffi/mupdf.lua b/ffi/mupdf.lua index a638f9286..e5f5be02e 100644 --- a/ffi/mupdf.lua +++ b/ffi/mupdf.lua @@ -647,6 +647,32 @@ 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) + local strength_coeff = 4 + 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 + local strength = math.min(strength_coeff * (brightness - white_threshold) / (255 - white_threshold), 1.0) + if brightness > white_threshold then + samples[idx] = r + (255 - r) * strength + samples[idx + 1] = g + (255 - g) * strength + samples[idx + 2] = b + (255 - b) * strength + end + end + else + for i = 0, pixel_count - 1 do + local brightness = samples[i] + if brightness > white_threshold then + local strength = math.min(strength_coeff * (brightness - white_threshold) / (255 - white_threshold), 1.0) + samples[i] = brightness + (255 - brightness) * strength + end + end + end +end + --[[ render page to blitbuffer @@ -679,11 +705,13 @@ 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) + apply_white_threshold(self.doc.color and 4 or 1, samples, width * height, draw_context.white_threshold) if draw_context.gamma >= 0.0 then M.fz_gamma_pixmap(self.ctx, pix, draw_context.gamma) From fe1962ebf907014e41f2929043bb2364eeacbe6c Mon Sep 17 00:00:00 2001 From: keringo <> Date: Thu, 26 Jun 2025 18:34:35 +0300 Subject: [PATCH 05/15] max 255 channel brightness restriction --- ffi/mupdf.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ffi/mupdf.lua b/ffi/mupdf.lua index e5f5be02e..d98d6b815 100644 --- a/ffi/mupdf.lua +++ b/ffi/mupdf.lua @@ -657,9 +657,9 @@ local function apply_white_threshold(bytes_per_pixel, samples, pixel_count, whit local brightness = 0.299 * r + 0.587 * g + 0.114 * b local strength = math.min(strength_coeff * (brightness - white_threshold) / (255 - white_threshold), 1.0) if brightness > white_threshold then - samples[idx] = r + (255 - r) * strength - samples[idx + 1] = g + (255 - g) * strength - samples[idx + 2] = b + (255 - b) * strength + samples[idx] = math.max(255, r + (255 - r) * strength) + samples[idx + 1] = math.max(255, g + (255 - g) * strength) + samples[idx + 2] = math.max(255, b + (255 - b) * strength) end end else @@ -667,7 +667,7 @@ local function apply_white_threshold(bytes_per_pixel, samples, pixel_count, whit local brightness = samples[i] if brightness > white_threshold then local strength = math.min(strength_coeff * (brightness - white_threshold) / (255 - white_threshold), 1.0) - samples[i] = brightness + (255 - brightness) * strength + samples[i] = math.max(255, brightness + (255 - brightness) * strength) end end end From 33de636965fd6e506deeff0fa2cea2502535ad21 Mon Sep 17 00:00:00 2001 From: keringo <> Date: Thu, 26 Jun 2025 18:50:18 +0300 Subject: [PATCH 06/15] minor improvements --- drawcontext.h | 2 +- ffi/mupdf.lua | 2 +- thirdparty/libk2pdfopt/CMakeLists.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/drawcontext.h b/drawcontext.h index 0c4d8c635..ae0d0dff9 100644 --- a/drawcontext.h +++ b/drawcontext.h @@ -22,7 +22,7 @@ typedef struct DrawContext { int rotate; double zoom; double gamma; - int white_threshold; + unsigned char white_threshold; int offset_x; int offset_y; } DrawContext; diff --git a/ffi/mupdf.lua b/ffi/mupdf.lua index d98d6b815..162cd3d6f 100644 --- a/ffi/mupdf.lua +++ b/ffi/mupdf.lua @@ -655,8 +655,8 @@ local function apply_white_threshold(bytes_per_pixel, samples, pixel_count, whit 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 - local strength = math.min(strength_coeff * (brightness - white_threshold) / (255 - white_threshold), 1.0) if brightness > white_threshold then + local strength = math.min(strength_coeff * (brightness - white_threshold) / (255 - white_threshold), 1.0) samples[idx] = math.max(255, r + (255 - r) * strength) samples[idx + 1] = math.max(255, g + (255 - g) * strength) samples[idx + 2] = math.max(255, b + (255 - b) * strength) diff --git a/thirdparty/libk2pdfopt/CMakeLists.txt b/thirdparty/libk2pdfopt/CMakeLists.txt index 0287959a7..23912a57b 100644 --- a/thirdparty/libk2pdfopt/CMakeLists.txt +++ b/thirdparty/libk2pdfopt/CMakeLists.txt @@ -21,7 +21,7 @@ endif() external_project( DOWNLOAD GIT 59ced371378312d8f332d9a35f5b4a3c33b18954 - https://github.com/kerivin/libk2pdfopt.git + https://github.com/koreader/libk2pdfopt.git PATCH_COMMAND ${PATCH_CMD} CMAKE_ARGS ${CMAKE_ARGS} SOURCE_SUBDIR lib From ae09a452be4adcd42ff74457cbca22ca0f6ee226 Mon Sep 17 00:00:00 2001 From: keringo <> Date: Thu, 26 Jun 2025 19:10:02 +0300 Subject: [PATCH 07/15] experiments --- thirdparty/libk2pdfopt/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thirdparty/libk2pdfopt/CMakeLists.txt b/thirdparty/libk2pdfopt/CMakeLists.txt index 23912a57b..0287959a7 100644 --- a/thirdparty/libk2pdfopt/CMakeLists.txt +++ b/thirdparty/libk2pdfopt/CMakeLists.txt @@ -21,7 +21,7 @@ endif() external_project( DOWNLOAD GIT 59ced371378312d8f332d9a35f5b4a3c33b18954 - https://github.com/koreader/libk2pdfopt.git + https://github.com/kerivin/libk2pdfopt.git PATCH_COMMAND ${PATCH_CMD} CMAKE_ARGS ${CMAKE_ARGS} SOURCE_SUBDIR lib From 107807351008a6effa61b5580e3bcfb9e0090fa7 Mon Sep 17 00:00:00 2001 From: keringo <> Date: Thu, 26 Jun 2025 19:13:34 +0300 Subject: [PATCH 08/15] original libk2pdfopt repo --- thirdparty/libk2pdfopt/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thirdparty/libk2pdfopt/CMakeLists.txt b/thirdparty/libk2pdfopt/CMakeLists.txt index 0287959a7..23912a57b 100644 --- a/thirdparty/libk2pdfopt/CMakeLists.txt +++ b/thirdparty/libk2pdfopt/CMakeLists.txt @@ -21,7 +21,7 @@ endif() external_project( DOWNLOAD GIT 59ced371378312d8f332d9a35f5b4a3c33b18954 - https://github.com/kerivin/libk2pdfopt.git + https://github.com/koreader/libk2pdfopt.git PATCH_COMMAND ${PATCH_CMD} CMAKE_ARGS ${CMAKE_ARGS} SOURCE_SUBDIR lib From 0a006a6f28f351896c48dca01407f8fab8241809 Mon Sep 17 00:00:00 2001 From: keringo <> Date: Thu, 26 Jun 2025 19:18:29 +0300 Subject: [PATCH 09/15] constructor args order fix --- ffi/drawcontext.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ffi/drawcontext.lua b/ffi/drawcontext.lua index 26791d302..14a9d8abc 100644 --- a/ffi/drawcontext.lua +++ b/ffi/drawcontext.lua @@ -37,7 +37,7 @@ 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, white_threshold) - return dctype(rotate or 0, zoom or 1.0, gamma or -1.0, x or 0, y or 0, white_threshold or 255) + 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 From b7bea8465086f64c0d254f6aa7cde3dc77561ed5 Mon Sep 17 00:00:00 2001 From: keringo <> Date: Thu, 26 Jun 2025 23:33:20 +0300 Subject: [PATCH 10/15] apply threshold only if white < 255 --- ffi/mupdf.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ffi/mupdf.lua b/ffi/mupdf.lua index 162cd3d6f..baaf15419 100644 --- a/ffi/mupdf.lua +++ b/ffi/mupdf.lua @@ -649,7 +649,7 @@ local function run_page(page, pixmap, ctm) end local function apply_white_threshold(bytes_per_pixel, samples, pixel_count, white_threshold) - local strength_coeff = 4 + local strength_coeff = 0.2 if bytes_per_pixel == 4 then for i = 0, pixel_count - 1 do local idx = i * bytes_per_pixel @@ -711,8 +711,10 @@ function page_mt.__index:draw_new(draw_context, width, height, offset_x, offset_ if pix == nil then merror(self.ctx, "cannot allocate pixmap") end run_page(self, pix, ctm) - apply_white_threshold(self.doc.color and 4 or 1, samples, width * height, draw_context.white_threshold) + 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 From 11f85e47a47ebbce34ca6474dc9fd70b5b6cc607 Mon Sep 17 00:00:00 2001 From: keringo <> Date: Mon, 30 Jun 2025 13:29:01 +0300 Subject: [PATCH 11/15] removed attempt to increase brightness of pixels above white threshold without making everything pure white --- ffi/mupdf.lua | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/ffi/mupdf.lua b/ffi/mupdf.lua index baaf15419..b19243c4b 100644 --- a/ffi/mupdf.lua +++ b/ffi/mupdf.lua @@ -649,25 +649,22 @@ local function run_page(page, pixmap, ctm) end local function apply_white_threshold(bytes_per_pixel, samples, pixel_count, white_threshold) - local strength_coeff = 0.2 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 - local strength = math.min(strength_coeff * (brightness - white_threshold) / (255 - white_threshold), 1.0) - samples[idx] = math.max(255, r + (255 - r) * strength) - samples[idx + 1] = math.max(255, g + (255 - g) * strength) - samples[idx + 2] = math.max(255, b + (255 - b) * strength) + 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 - local strength = math.min(strength_coeff * (brightness - white_threshold) / (255 - white_threshold), 1.0) - samples[i] = math.max(255, brightness + (255 - brightness) * strength) + samples[i] = 255 end end end From be7bbd1f69b61313aadac7bfe5273de0581f8378 Mon Sep 17 00:00:00 2001 From: kerivin Date: Mon, 4 Aug 2025 19:49:31 +0300 Subject: [PATCH 12/15] paint_white for libk2pdfopt --- ffi/koptcontext.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ffi/koptcontext.lua b/ffi/koptcontext.lua index 83c495ec5..5dbe5478c 100644 --- a/ffi/koptcontext.lua +++ b/ffi/koptcontext.lua @@ -179,6 +179,8 @@ 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 +601,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 +672,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 +743,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 From 9ad230e6d63a25395f58642eee8f5af380fc660a Mon Sep 17 00:00:00 2001 From: kerivin Date: Mon, 4 Aug 2025 20:03:14 +0300 Subject: [PATCH 13/15] libk2pdfopt commit hash update --- thirdparty/libk2pdfopt/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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} From 98f7a0b73033766878ae2ba961d32094bf1bc49a Mon Sep 17 00:00:00 2001 From: kerivin Date: Mon, 4 Aug 2025 20:07:51 +0300 Subject: [PATCH 14/15] removed random new line --- ffi/koptcontext.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/ffi/koptcontext.lua b/ffi/koptcontext.lua index 5dbe5478c..4d9c497cb 100644 --- a/ffi/koptcontext.lua +++ b/ffi/koptcontext.lua @@ -179,7 +179,6 @@ 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 From 199c8610684de0f51dbcc1f7ed38f8f8dad1b7db Mon Sep 17 00:00:00 2001 From: kerivin Date: Mon, 4 Aug 2025 20:11:24 +0300 Subject: [PATCH 15/15] added paint_white to koptcontext_h.lua --- ffi/koptcontext_h.lua | 1 + 1 file changed, 1 insertion(+) 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;