From bd1dd7dfd4206dea4bb651a88c1ac39c91753e6a Mon Sep 17 00:00:00 2001 From: kerivin Date: Wed, 20 May 2026 17:36:49 +0300 Subject: [PATCH] Add white threshold for paged documents (#15412) --- defaults.lua | 1 + frontend/document/koptinterface.lua | 15 ++++++++++++--- frontend/ui/data/koptoptions.lua | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/defaults.lua b/defaults.lua index d78c7a7ea..da7f11a6f 100644 --- a/defaults.lua +++ b/defaults.lua @@ -87,6 +87,7 @@ DKOPTREADER_CONFIG_AUTO_STRAIGHTEN = 0, -- range from 0 to 10 DKOPTREADER_CONFIG_JUSTIFICATION = 3, -- -1 = auto, 0 = left, 1 = center, 2 = right, 3 = full DKOPTREADER_CONFIG_MAX_COLUMNS = 2, -- range from 1 to 4 DKOPTREADER_CONFIG_CONTRAST = 1.0, -- range from 0.2 to 2.0 +DKOPTREADER_CONFIG_WHITE_THRESHOLD = 255, -- range from 0 to 255 -- word spacing for reflow DKOPTREADER_CONFIG_WORD_SPACINGS = {0.05, -0.2, 0.375}, -- range from (+/-)0.05 to (+/-)0.5 diff --git a/frontend/document/koptinterface.lua b/frontend/document/koptinterface.lua index 443854a7e..b7374d8d2 100644 --- a/frontend/document/koptinterface.lua +++ b/frontend/document/koptinterface.lua @@ -100,6 +100,7 @@ function KoptInterface:setDefaultConfigurable(configurable) configurable.page_margin = G_defaults:readSetting("DKOPTREADER_CONFIG_PAGE_MARGIN") configurable.quality = G_defaults:readSetting("DKOPTREADER_CONFIG_RENDER_QUALITY") configurable.contrast = G_defaults:readSetting("DKOPTREADER_CONFIG_CONTRAST") + configurable.white_threshold = G_defaults:readSetting("DKOPTREADER_CONFIG_WHITE_THRESHOLD") configurable.defect_size = G_defaults:readSetting("DKOPTREADER_CONFIG_DEFECT_SIZE") configurable.line_spacing = G_defaults:readSetting("DKOPTREADER_CONFIG_LINE_SPACING") configurable.word_spacing = G_defaults:readSetting("DKOPTREADER_CONFIG_DEFAULT_WORD_SPACING") @@ -140,6 +141,10 @@ function KoptInterface:createContext(doc, pageno, bbox) kc:setLanguage(lang) kc:setTrim(doc.configurable.trim_page) kc:setWrap(doc.configurable.text_wrap) + if doc.configurable.white_threshold ~= 255 then + kc:setWhiteThreshold(doc.configurable.white_threshold) + kc:setPaintWhiteThreshold(1) + end kc:setIndent(doc.configurable.detect_indent) kc:setColumns(doc.configurable.max_columns) kc:setDeviceDim(canvas_size.w, canvas_size.h) @@ -392,10 +397,14 @@ function KoptInterface:getCoverPageImage(doc) end end +function KoptInterface:is_optimizing_page(doc) + return doc.configurable.page_opt == 1 or doc.configurable.auto_straighten > 0 or doc.configurable.white_threshold ~= 255 +end + function KoptInterface:renderPage(doc, pageno, rect, zoom, rotation, gamma, hinting) if doc.configurable.text_wrap == 1 then return self:renderReflowedPage(doc, pageno, rect, zoom, rotation, hinting) - elseif doc.configurable.page_opt == 1 or doc.configurable.auto_straighten > 0 then + elseif self:is_optimizing_page(doc) then return self:renderOptimizedPage(doc, pageno, rect, zoom, rotation, hinting) else return Document.renderPage(doc, pageno, rect, zoom, rotation, gamma, hinting) @@ -542,7 +551,7 @@ function KoptInterface:hintPage(doc, pageno, zoom, rotation, gamma) if doc.configurable.text_wrap == 1 then self:hintReflowedPage(doc, pageno, zoom, rotation, true) - elseif doc.configurable.page_opt == 1 or doc.configurable.auto_straighten > 0 then + elseif self:is_optimizing_page(doc) then self:renderOptimizedPage(doc, pageno, nil, zoom, rotation, true) else Document.hintPage(doc, pageno, zoom, rotation, gamma) @@ -582,7 +591,7 @@ function KoptInterface:drawPage(doc, target, x, y, rect, pageno, zoom, rotation, local nightmode_invert = doc.configurable.nightmode_document == 1 and Screen.night_mode if doc.configurable.text_wrap == 1 then self:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation, nightmode_invert) - elseif doc.configurable.page_opt == 1 or doc.configurable.auto_straighten > 0 then + elseif self:is_optimizing_page(doc) then self:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation, nightmode_invert) elseif nightmode_invert then Document.drawPageInverted(doc, target, x, y, rect, pageno, zoom, rotation, gamma) diff --git a/frontend/ui/data/koptoptions.lua b/frontend/ui/data/koptoptions.lua index 87dbc85dd..ff4d370c0 100644 --- a/frontend/ui/data/koptoptions.lua +++ b/frontend/ui/data/koptoptions.lua @@ -506,12 +506,33 @@ Some of the other settings are only available when reflow mode is enabled.]]), precision = "%.1f", }, }, + { + name = "white_threshold", + name_text = _("White Threshold"), + buttonprogress = true, + values = {255, 224, 192, 160, 128, 96, 64, 32}, + default_pos = 1, + default_value = G_defaults:readSetting("DKOPTREADER_CONFIG_WHITE_THRESHOLD"), + event = "WhiteThresholdUpdate", + args = {255, 224, 192, 160, 128, 96, 64, 32}, + labels = {255, 224, 192, 160, 128, 96, 64, 32}, + name_text_hold_callback = optionsutil.showValues, + more_options = true, + more_options_param = { + value_step = 1, value_hold_step = 5, + value_min = 0, value_max = 255, + }, + help_text = _([[Render everything above the threshold white.]]), + }, { name = "page_opt", name_text = _("Dewatermark"), toggle = {C_("Dewatermark", "off"), C_("Dewatermark", "on")}, values = {0, 1}, default_value = 0, + enabled_func = function(configurable) + return optionsutil.enableIfEquals(configurable, "white_threshold", 255) + end, name_text_hold_callback = optionsutil.showValues, help_text = _([[Remove watermarks from the rendered document. This can also be used to remove some gray background or to convert a grayscale or color document to black & white and get more contrast for easier reading.]]),