feat: saturation control (#15381)

This commit is contained in:
Noa Himesaka authored and GitHub committed 2026-05-24 08:14:27 +02:00
1 parent bc0ef5a078
commit 06c9eea0d4
14 files changed
+97 -41

No files matched your search

+1 -1
Submodule base updated: 3725903411...4d5e13ee74.
+1
View File
@@ -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_SATURATION = 1.0, -- range from 0.2 to 2.0
DKOPTREADER_CONFIG_WHITE_THRESHOLD = 255, -- range from 0 to 255
-- word spacing for reflow
@@ -18,7 +18,8 @@ function ReaderHinting:onHintPage()
self.view.state.page + i,
self.zoom:getZoom(self.view.state.page + i),
self.view.state.rotation,
self.view.state.gamma)
self.view.state.gamma,
self.view.state.saturation)
end
end
return true
@@ -23,6 +23,7 @@ function ReaderKoptListener:onReadSettings(config)
self.normal_zoom_mode = normal_zoom_mode
self:setZoomMode(normal_zoom_mode)
self.ui:handleEvent(Event:new("GammaUpdate", self.document.configurable.contrast, true)) -- no notification
self.ui:handleEvent(Event:new("SaturationUpdate", self.document.configurable.saturation, true)) -- no notification
-- since K2pdfopt v2.21 negative value of word spacing is also used, for config
-- compatibility we should manually change previous -1 to a more reasonable -0.2
if self.document.configurable.word_spacing == -1 then
@@ -716,6 +716,13 @@ function ReaderPaging:onUpdateScrollPageGamma(gamma)
return true
end
function ReaderPaging:onUpdateScrollPageSaturation(saturation)
for _, state in ipairs(self.view.page_states) do
state.saturation = saturation
end
return true
end
function ReaderPaging:getNextPageState(blank_area, image_offset)
local page_area = self.view:getPageArea(
self.view.state.page,
@@ -736,6 +743,7 @@ function ReaderPaging:getNextPageState(blank_area, image_offset)
zoom = self.view.state.zoom,
rotation = self.view.state.rotation,
gamma = self.view.state.gamma,
saturation = self.view.state.saturation,
offset = page_offset,
visible_area = visible_area,
page_area = page_area,
@@ -763,6 +771,7 @@ function ReaderPaging:getPrevPageState(blank_area, image_offset)
zoom = self.view.state.zoom,
rotation = self.view.state.rotation,
gamma = self.view.state.gamma,
saturation = self.view.state.saturation,
offset = page_offset,
visible_area = visible_area,
page_area = page_area,
+16 -2
View File
@@ -86,6 +86,7 @@ function ReaderView:init()
zoom = 1.0,
rotation = 0,
gamma = 1.0,
saturation = 1.0,
offset = nil,
bbox = nil,
}
@@ -384,7 +385,8 @@ function ReaderView:drawScrollPages(bb, x, y)
state.page,
state.zoom,
state.rotation,
state.gamma)
state.gamma,
state.saturation)
pos.y = pos.y + state.visible_area.h
-- draw page gap if not the last part
if page ~= #self.page_states then
@@ -458,7 +460,8 @@ function ReaderView:drawSinglePage(bb, x, y)
self.state.page,
self.state.zoom,
self.state.rotation,
self.state.gamma)
self.state.gamma,
self.state.saturation)
UIManager:nextTick(self.emitHintPageEvent)
end
@@ -871,6 +874,7 @@ function ReaderView:getViewContext()
zoom = self.state.zoom,
rotation = self.state.rotation,
gamma = self.state.gamma,
saturation = self.state.saturation,
offset = self.state.offset:copy(),
bbox = self.state.bbox,
},
@@ -1108,6 +1112,16 @@ function ReaderView:onGammaUpdate(gamma, no_notification)
end
end
function ReaderView:onSaturationUpdate(saturation, no_notification)
self.state.saturation = saturation
if self.page_scroll then
self.ui:handleEvent(Event:new("UpdateScrollPageSaturation", saturation))
end
if not no_notification then
Notification:notify(T(_("Saturation set to: %1."), saturation))
end
end
-- For ReaderKOptListener
function ReaderView:onDitheringUpdate()
-- Do the device cap checks again, to avoid snafus when sharing configs between devices
+6 -6
View File
@@ -285,16 +285,16 @@ function DjvuDocument:findAllText(pattern, case_insensitive, nb_context_words, m
return self.koptinterface:findAllText(self, pattern, case_insensitive, nb_context_words, max_hits)
end
function DjvuDocument:renderPage(pageno, rect, zoom, rotation, gamma, hinting)
return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, gamma, hinting)
function DjvuDocument:renderPage(pageno, rect, zoom, rotation, gamma, saturation, hinting)
return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, gamma, saturation, hinting)
end
function DjvuDocument:hintPage(pageno, zoom, rotation, gamma)
return self.koptinterface:hintPage(self, pageno, zoom, rotation, gamma)
function DjvuDocument:hintPage(pageno, zoom, rotation, gamma, saturation)
return self.koptinterface:hintPage(self, pageno, zoom, rotation, gamma, saturation)
end
function DjvuDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma)
return self.koptinterface:drawPage(self, target, x, y, rect, pageno, zoom, rotation, gamma)
function DjvuDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma, saturation)
return self.koptinterface:drawPage(self, target, x, y, rect, pageno, zoom, rotation, gamma, saturation)
end
function DjvuDocument:register(registry)
+17 -12
View File
@@ -390,32 +390,34 @@ function Document:resetTileCacheValidity()
self.tile_cache_validity_ts = os.time()
end
function Document:getFullPageHash(pageno, zoom, rotation, gamma)
function Document:getFullPageHash(pageno, zoom, rotation, gamma, saturation)
return "renderpg|"..self.file.."|"..self.mod_time.."|"..pageno.."|"
..zoom.."|"..rotation.."|"..gamma.."|"..self.configurable.background_cleanup.."|"
..self.render_mode..(self.render_color and "|color" or "|bw")
..(self.reflowable_font_size and "|"..self.reflowable_font_size or "")
.."|"..saturation
end
function Document:getPagePartHash(pageno, zoom, rotation, gamma, rect)
function Document:getPagePartHash(pageno, zoom, rotation, gamma, saturation, rect)
return "renderpgpart|"..self.file.."|"..self.mod_time.."|"..pageno.."|"
..tostring(rect).."|"..zoom.."|"..tostring(rect.scaled_rect)
.."|"..rotation.."|"..gamma.."|"..self.configurable.background_cleanup.."|"
..self.render_mode..(self.render_color and "|color" or "|bw")
..(self.reflowable_font_size and "|"..self.reflowable_font_size or "")
.."|"..saturation
end
function Document:renderPage(pageno, rect, zoom, rotation, gamma, hinting)
function Document:renderPage(pageno, rect, zoom, rotation, gamma, saturation, hinting)
-- If rect contains a nested scaled_rect object, our caller handled scaling itself (e.g., drawPagePart)
local is_prescaled = rect and rect.scaled_rect ~= nil or false
local hash, hash_excerpt, tile
if is_prescaled then
hash = self:getPagePartHash(pageno, zoom, rotation, gamma, rect)
hash = self:getPagePartHash(pageno, zoom, rotation, gamma, saturation, rect)
tile = DocCache:check(hash, TileCacheItem)
else
hash = self:getFullPageHash(pageno, zoom, rotation, gamma)
hash = self:getFullPageHash(pageno, zoom, rotation, gamma, saturation)
tile = DocCache:check(hash, TileCacheItem)
@@ -502,6 +504,9 @@ function Document:renderPage(pageno, rect, zoom, rotation, gamma, hinting)
if gamma ~= self.GAMMA_NO_GAMMA then
dc:setGamma(gamma)
end
if saturation ~= 1.0 and dc.setSaturation then
dc:setSaturation(saturation)
end
dc:setIsolateSMask(self.configurable.background_cleanup)
@@ -519,9 +524,9 @@ end
-- a hint for the cache engine to paint a full page to the cache
--- @todo this should trigger a background operation
function Document:hintPage(pageno, zoom, rotation, gamma)
function Document:hintPage(pageno, zoom, rotation, gamma, saturation)
logger.dbg("hinting page", pageno)
self:renderPage(pageno, nil, zoom, rotation, gamma, true)
self:renderPage(pageno, nil, zoom, rotation, gamma, saturation, true)
end
--[[
@@ -532,8 +537,8 @@ Draw page content to blitbuffer.
@target: target blitbuffer
@rect: visible_area inside document page
--]]
function Document:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma)
local tile = self:renderPage(pageno, rect, zoom, rotation, gamma)
function Document:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma, saturation)
local tile = self:renderPage(pageno, rect, zoom, rotation, gamma, saturation)
-- Enable SW dithering if requested (only available in koptoptions)
if self.sw_dithering then
target:ditherblitFrom(tile.bb,
@@ -550,8 +555,8 @@ function Document:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma)
end
end
function Document:drawPageInverted(target, x, y, rect, pageno, zoom, rotation, gamma)
local tile = self:renderPage(pageno, rect, zoom, rotation, gamma)
function Document:drawPageInverted(target, x, y, rect, pageno, zoom, rotation, gamma, saturation)
local tile = self:renderPage(pageno, rect, zoom, rotation, gamma, saturation)
target:invertblitFrom(tile.bb,
x, y,
rect.x - tile.excerpt.x,
@@ -582,7 +587,7 @@ function Document:drawPagePart(pageno, native_rect, rotation)
rect.scaled_rect = scaled_rect
-- Enable SMP via the hinting flag
local tile = self:renderPage(pageno, rect, zoom, rotation, 1.0, true)
local tile = self:renderPage(pageno, rect, zoom, rotation, 1.0, 1.0, true)
return tile.bb, rotate
end
+9 -9
View File
@@ -391,7 +391,7 @@ function KoptInterface:getCoverPageImage(doc)
local native_size = Document.getNativePageDimensions(doc, 1)
local canvas_size = CanvasContext:getSize()
local zoom = math.min(canvas_size.w / native_size.w, canvas_size.h / native_size.h)
local tile = Document.renderPage(doc, 1, nil, zoom, 0, 1.0, false)
local tile = Document.renderPage(doc, 1, nil, zoom, 0, 1.0, 1.0, false)
if tile then
return tile.bb:copy()
end
@@ -401,13 +401,13 @@ 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)
function KoptInterface:renderPage(doc, pageno, rect, zoom, rotation, gamma, saturation, hinting)
if doc.configurable.text_wrap == 1 then
return self:renderReflowedPage(doc, pageno, rect, zoom, rotation, hinting)
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)
return Document.renderPage(doc, pageno, rect, zoom, rotation, gamma, saturation, hinting)
end
end
@@ -545,7 +545,7 @@ function KoptInterface:renderOptimizedPage(doc, pageno, rect, zoom, rotation, hi
end
end
function KoptInterface:hintPage(doc, pageno, zoom, rotation, gamma)
function KoptInterface:hintPage(doc, pageno, zoom, rotation, gamma, saturation)
--- @note: Crappy safeguard around memory issues like in #7627: if we're eating too much RAM, drop half the cache...
DocCache:memoryPressureCheck()
@@ -554,7 +554,7 @@ function KoptInterface:hintPage(doc, pageno, zoom, rotation, gamma)
elseif self:is_optimizing_page(doc) then
self:renderOptimizedPage(doc, pageno, nil, zoom, rotation, true)
else
Document.hintPage(doc, pageno, zoom, rotation, gamma)
Document.hintPage(doc, pageno, zoom, rotation, gamma, saturation)
end
end
@@ -587,16 +587,16 @@ function KoptInterface:hintReflowedPage(doc, pageno, zoom, rotation, hinting)
end
end
function KoptInterface:drawPage(doc, target, x, y, rect, pageno, zoom, rotation, gamma)
function KoptInterface:drawPage(doc, target, x, y, rect, pageno, zoom, rotation, gamma, saturation)
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 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)
Document.drawPageInverted(doc, target, x, y, rect, pageno, zoom, rotation, gamma, saturation)
else
Document.drawPage(doc, target, x, y, rect, pageno, zoom, rotation, gamma)
Document.drawPage(doc, target, x, y, rect, pageno, zoom, rotation, gamma, saturation)
end
end
@@ -606,7 +606,7 @@ Draw cached tile pixels into target blitbuffer.
Inherited from common document interface.
--]]
function KoptInterface:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation, nightmode_invert)
local tile = self:renderPage(doc, pageno, rect, zoom, rotation, 1.0, false)
local tile = self:renderPage(doc, pageno, rect, zoom, rotation, 1.0, 1.0, false)
target:blitFrom(tile.bb,
x, y,
rect.x - tile.excerpt.x,
+6 -6
View File
@@ -357,16 +357,16 @@ function PdfDocument:findAllText(pattern, case_insensitive, nb_context_words, ma
return self.koptinterface:findAllText(self, pattern, case_insensitive, nb_context_words, max_hits)
end
function PdfDocument:renderPage(pageno, rect, zoom, rotation, gamma, hinting)
return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, gamma, hinting)
function PdfDocument:renderPage(pageno, rect, zoom, rotation, gamma, saturation, hinting)
return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, gamma, saturation, hinting)
end
function PdfDocument:hintPage(pageno, zoom, rotation, gamma)
return self.koptinterface:hintPage(self, pageno, zoom, rotation, gamma)
function PdfDocument:hintPage(pageno, zoom, rotation, gamma, saturation)
return self.koptinterface:hintPage(self, pageno, zoom, rotation, gamma, saturation)
end
function PdfDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma)
return self.koptinterface:drawPage(self, target, x, y, rect, pageno, zoom, rotation, gamma)
function PdfDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma, saturation)
return self.koptinterface:drawPage(self, target, x, y, rect, pageno, zoom, rotation, gamma, saturation)
end
function PdfDocument:register(registry)
+21
View File
@@ -506,6 +506,27 @@ Some of the other settings are only available when reflow mode is enabled.]]),
precision = "%.1f",
},
},
{
name = "saturation",
name_text = _("Saturation"),
buttonprogress = true,
values = {0.2, 0.5, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0},
default_pos = 3,
default_value = G_defaults:readSetting("DKOPTREADER_CONFIG_SATURATION"),
event = "SaturationUpdate",
args = {0.2, 0.5, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0},
labels = {0.2, 0.5, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0},
show = Device:hasColorScreen(),
name_text_hold_callback = optionsutil.showValues,
more_options = true,
more_options_param = {
value_step = 0.1, value_hold_step = 0.2,
value_min = 0.2, value_max = 2.0,
precision = "%.1f",
},
help_text = _([[Adjust color saturation for rendered pages.
Lower values desaturate colors, 1.0 keeps original colors, and higher values increase color intensity.]]),
},
{
name = "white_threshold",
name_text = _("White Threshold"),
+4 -4
View File
@@ -20,7 +20,7 @@ describe("Cache module", function()
it("should serialize blitbuffer", function()
for pageno = 1, math.min(max_page, doc.info.number_of_pages) do
doc:renderPage(pageno, nil, 1, 0, 1.0)
doc:renderPage(pageno, nil, 1, 0, 1.0, 1.0)
DocCache:serialize()
end
DocCache:clear()
@@ -28,7 +28,7 @@ describe("Cache module", function()
it("should deserialize blitbuffer", function()
for pageno = 1, math.min(max_page, doc.info.number_of_pages) do
doc:hintPage(pageno, 1, 0, 1.0)
doc:hintPage(pageno, 1, 0, 1.0, 1.0)
end
DocCache:clear()
end)
@@ -36,7 +36,7 @@ describe("Cache module", function()
it("should serialize koptcontext", function()
doc.configurable.text_wrap = 1
for pageno = 1, math.min(max_page, doc.info.number_of_pages) do
doc:renderPage(pageno, nil, 1, 0, 1.0)
doc:renderPage(pageno, nil, 1, 0, 1.0, 1.0)
doc:getPageDimensions(pageno)
DocCache:serialize()
end
@@ -46,7 +46,7 @@ describe("Cache module", function()
it("should deserialize koptcontext", function()
for pageno = 1, math.min(max_page, doc.info.number_of_pages) do
doc:renderPage(pageno, nil, 1, 0, 1.0)
doc:renderPage(pageno, nil, 1, 0, 1.0, 1.0)
end
DocCache:clear()
end)
+2
View File
@@ -118,6 +118,7 @@ describe("ReaderLink module", function()
h = 800, w = 566,
},
rotation = 0,
saturation = 1,
visible_area = {
x = 0, y = 694,
h = 106, w = 566,
@@ -133,6 +134,7 @@ describe("ReaderLink module", function()
x = 0, y = 0,
},
rotation = 0,
saturation = 1,
visible_area = {
h = 686, w = 566,
x = 0, y = 0,
+2
View File
@@ -71,6 +71,7 @@ describe("Readerview module", function()
page = 1,
pos = 0,
gamma = 1,
saturation = 1,
offset = {
x = 17, y = 0,
h = 0, w = 0,
@@ -124,6 +125,7 @@ describe("Readerview module", function()
local saved_ctx = {
{
gamma = 1,
saturation = 1,
offset = {x = 17, y = 0},
page = 1,
page_area = {